i2c-microchip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Microchip I2C controller driver
  4. *
  5. * Copyright (C) 2021-2022 Microchip Technology Inc.
  6. * Padmarao Begari <padmarao.begari@microchip.com>
  7. * Conor Dooley <conor.dooley@microchip.com>
  8. */
  9. #include <common.h>
  10. #include <clk.h>
  11. #include <dm.h>
  12. #include <i2c.h>
  13. #include <asm/io.h>
  14. #include <dm/device_compat.h>
  15. #include <linux/bitops.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #define MICROCHIP_I2C_TIMEOUT (1000 * 60)
  19. #define MPFS_I2C_CTRL (0x00)
  20. #define CTRL_CR0 (0x00)
  21. #define CTRL_CR1 (0x01)
  22. #define CTRL_AA BIT(2)
  23. #define CTRL_SI BIT(3)
  24. #define CTRL_STO BIT(4)
  25. #define CTRL_STA BIT(5)
  26. #define CTRL_ENS1 BIT(6)
  27. #define CTRL_CR2 (0x07)
  28. #define MPFS_I2C_STATUS (0x04)
  29. #define STATUS_BUS_ERROR (0x00)
  30. #define STATUS_M_START_SENT (0x08)
  31. #define STATUS_M_REPEATED_START_SENT (0x10)
  32. #define STATUS_M_SLAW_ACK (0x18)
  33. #define STATUS_M_SLAW_NACK (0x20)
  34. #define STATUS_M_TX_DATA_ACK (0x28)
  35. #define STATUS_M_TX_DATA_NACK (0x30)
  36. #define STATUS_M_ARB_LOST (0x38)
  37. #define STATUS_M_SLAR_ACK (0x40)
  38. #define STATUS_M_SLAR_NACK (0x48)
  39. #define STATUS_M_RX_DATA_ACKED (0x50)
  40. #define STATUS_M_RX_DATA_NACKED (0x58)
  41. #define STATUS_S_SLAW_ACKED (0x60)
  42. #define STATUS_S_ARB_LOST_SLAW_ACKED (0x68)
  43. #define STATUS_S_GENERAL_CALL_ACKED (0x70)
  44. #define STATUS_S_ARB_LOST_GENERAL_CALL_ACKED (0x78)
  45. #define STATUS_S_RX_DATA_ACKED (0x80)
  46. #define STATUS_S_RX_DATA_NACKED (0x88)
  47. #define STATUS_S_GENERAL_CALL_RX_DATA_ACKED (0x90)
  48. #define STATUS_S_GENERAL_CALL_RX_DATA_NACKED (0x98)
  49. #define STATUS_S_RX_STOP (0xA0)
  50. #define STATUS_S_SLAR_ACKED (0xA8)
  51. #define STATUS_S_ARB_LOST_SLAR_ACKED (0xB0)
  52. #define STATUS_S_TX_DATA_ACK (0xb8)
  53. #define STATUS_S_TX_DATA_NACK (0xC0)
  54. #define STATUS_LAST_DATA_ACK (0xC8)
  55. #define STATUS_M_SMB_MASTER_RESET (0xD0)
  56. #define STATUS_S_SCL_LOW_TIMEOUT (0xD8)
  57. #define STATUS_NO_STATE_INFO (0xF8)
  58. #define MPFS_I2C_DATA (0x08)
  59. #define MPFS_I2C_SLAVE0_ADDR (0x0c)
  60. #define MPFS_I2C_SMBUS (0x10)
  61. #define MPFS_I2C_FREQ (0x14)
  62. #define MPFS_I2C_GLITCHREG (0x18)
  63. #define MPFS_I2C_SLAVE1_ADDR (0x1c)
  64. #define PCLK_DIV_256 ((0 << CTRL_CR0) | (0 << CTRL_CR1) | (0 << CTRL_CR2))
  65. #define PCLK_DIV_224 ((1 << CTRL_CR0) | (0 << CTRL_CR1) | (0 << CTRL_CR2))
  66. #define PCLK_DIV_192 ((0 << CTRL_CR0) | (1 << CTRL_CR1) | (0 << CTRL_CR2))
  67. #define PCLK_DIV_160 ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (0 << CTRL_CR2))
  68. #define PCLK_DIV_960 ((0 << CTRL_CR0) | (0 << CTRL_CR1) | (1 << CTRL_CR2))
  69. #define PCLK_DIV_120 ((1 << CTRL_CR0) | (0 << CTRL_CR1) | (1 << CTRL_CR2))
  70. #define PCLK_DIV_60 ((0 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
  71. #define BCLK_DIV_8 ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
  72. #define CLK_MASK ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
  73. /*
  74. * mpfs_i2c_bus - I2C bus context
  75. * @base: pointer to register struct
  76. * @msg_len: number of bytes transferred in msg
  77. * @msg_err: error code for completed message
  78. * @i2c_clk: clock reference for i2c input clock
  79. * @clk_rate: current i2c bus clock rate
  80. * @buf: ptr to msg buffer for easier use.
  81. * @addr: i2c address.
  82. * @isr_status: cached copy of local ISR status.
  83. */
  84. struct mpfs_i2c_bus {
  85. void __iomem *base;
  86. size_t msg_len;
  87. int msg_err;
  88. struct clk i2c_clk;
  89. u32 clk_rate;
  90. u8 *buf;
  91. u8 addr;
  92. u32 isr_status;
  93. };
  94. static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
  95. {
  96. return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
  97. }
  98. static void mpfs_i2c_int_clear(struct mpfs_i2c_bus *bus)
  99. {
  100. u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
  101. ctrl &= ~CTRL_SI;
  102. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  103. }
  104. static void mpfs_i2c_core_disable(struct mpfs_i2c_bus *bus)
  105. {
  106. u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
  107. ctrl &= ~CTRL_ENS1;
  108. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  109. }
  110. static void mpfs_i2c_core_enable(struct mpfs_i2c_bus *bus)
  111. {
  112. u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
  113. ctrl |= CTRL_ENS1;
  114. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  115. }
  116. static void mpfs_i2c_reset(struct mpfs_i2c_bus *bus)
  117. {
  118. mpfs_i2c_core_disable(bus);
  119. mpfs_i2c_core_enable(bus);
  120. }
  121. static inline void mpfs_i2c_stop(struct mpfs_i2c_bus *bus)
  122. {
  123. u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
  124. ctrl |= CTRL_STO;
  125. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  126. }
  127. static inline int mpfs_generate_divisor(u32 rate, u8 *code)
  128. {
  129. int ret = 0;
  130. if (rate >= 960)
  131. *code = PCLK_DIV_960;
  132. else if (rate >= 256)
  133. *code = PCLK_DIV_256;
  134. else if (rate >= 224)
  135. *code = PCLK_DIV_224;
  136. else if (rate >= 192)
  137. *code = PCLK_DIV_192;
  138. else if (rate >= 160)
  139. *code = PCLK_DIV_160;
  140. else if (rate >= 120)
  141. *code = PCLK_DIV_120;
  142. else if (rate >= 60)
  143. *code = PCLK_DIV_60;
  144. else if (rate >= 8)
  145. *code = BCLK_DIV_8;
  146. else
  147. ret = -EINVAL;
  148. return ret;
  149. }
  150. static int mpfs_i2c_init(struct mpfs_i2c_bus *bus, struct udevice *dev)
  151. {
  152. u32 clk_rate, divisor;
  153. u8 clkval, ctrl;
  154. int ret;
  155. ret = clk_get_by_index(dev, 0, &bus->i2c_clk);
  156. if (ret)
  157. return -EINVAL;
  158. ret = clk_enable(&bus->i2c_clk);
  159. if (ret)
  160. return ret;
  161. clk_rate = clk_get_rate(&bus->i2c_clk);
  162. if (!clk_rate)
  163. return -EINVAL;
  164. clk_free(&bus->i2c_clk);
  165. divisor = clk_rate / bus->clk_rate;
  166. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  167. ctrl &= ~CLK_MASK;
  168. ret = mpfs_generate_divisor(divisor, &clkval);
  169. if (ret)
  170. return -EINVAL;
  171. ctrl |= clkval;
  172. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  173. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  174. /* Reset I2C core */
  175. mpfs_i2c_reset(bus);
  176. return 0;
  177. }
  178. static void mpfs_i2c_transfer(struct mpfs_i2c_bus *bus, u32 data)
  179. {
  180. if (bus->msg_len > 0)
  181. writel(data, bus->base + MPFS_I2C_DATA);
  182. }
  183. static void mpfs_i2c_empty_rx(struct mpfs_i2c_bus *bus)
  184. {
  185. u8 ctrl;
  186. u8 data_read;
  187. if (bus->msg_len > 0) {
  188. data_read = readl(bus->base + MPFS_I2C_DATA);
  189. *bus->buf++ = data_read;
  190. bus->msg_len--;
  191. }
  192. if (bus->msg_len <= 1) {
  193. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  194. ctrl &= ~CTRL_AA;
  195. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  196. }
  197. }
  198. static int mpfs_i2c_fill_tx(struct mpfs_i2c_bus *bus)
  199. {
  200. mpfs_i2c_transfer(bus, *bus->buf++);
  201. bus->msg_len--;
  202. return 0;
  203. }
  204. static int mpfs_i2c_service_handler(struct mpfs_i2c_bus *bus)
  205. {
  206. bool finish = false;
  207. u32 status;
  208. u8 ctrl;
  209. status = bus->isr_status;
  210. switch (status) {
  211. case STATUS_M_START_SENT:
  212. case STATUS_M_REPEATED_START_SENT:
  213. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  214. ctrl &= ~CTRL_STA;
  215. writel(bus->addr, bus->base + MPFS_I2C_DATA);
  216. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  217. break;
  218. case STATUS_M_SLAW_ACK:
  219. case STATUS_M_TX_DATA_ACK:
  220. if (bus->msg_len > 0) {
  221. mpfs_i2c_fill_tx(bus);
  222. } else {
  223. /* On the last byte to be transmitted, send STOP */
  224. mpfs_i2c_stop(bus);
  225. finish = true;
  226. }
  227. break;
  228. case STATUS_M_SLAR_ACK:
  229. if (bus->msg_len > 1u) {
  230. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  231. ctrl |= CTRL_AA;
  232. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  233. } else if (bus->msg_len == 1u) {
  234. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  235. ctrl &= ~CTRL_AA;
  236. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  237. } else {
  238. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  239. ctrl |= CTRL_AA;
  240. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  241. /* On the last byte to be transmitted, send STOP */
  242. mpfs_i2c_stop(bus);
  243. finish = true;
  244. }
  245. break;
  246. case STATUS_M_RX_DATA_ACKED:
  247. mpfs_i2c_empty_rx(bus);
  248. break;
  249. case STATUS_M_RX_DATA_NACKED:
  250. mpfs_i2c_empty_rx(bus);
  251. if (bus->msg_len == 0) {
  252. /* On the last byte to be transmitted, send STOP */
  253. mpfs_i2c_stop(bus);
  254. finish = true;
  255. }
  256. break;
  257. case STATUS_M_TX_DATA_NACK:
  258. case STATUS_M_SLAR_NACK:
  259. case STATUS_M_SLAW_NACK:
  260. bus->msg_err = -ENXIO;
  261. mpfs_i2c_stop(bus);
  262. finish = true;
  263. break;
  264. case STATUS_M_ARB_LOST:
  265. /* Handle Lost Arbitration */
  266. bus->msg_err = -EAGAIN;
  267. finish = true;
  268. break;
  269. default:
  270. break;
  271. }
  272. if (finish) {
  273. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  274. ctrl &= ~CTRL_AA;
  275. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  276. return 0;
  277. }
  278. return 1;
  279. }
  280. static int mpfs_i2c_service(struct mpfs_i2c_bus *bus)
  281. {
  282. int ret = 0;
  283. int si_bit;
  284. si_bit = readl(bus->base + MPFS_I2C_CTRL);
  285. if (si_bit & CTRL_SI) {
  286. bus->isr_status = readl(bus->base + MPFS_I2C_STATUS);
  287. ret = mpfs_i2c_service_handler(bus);
  288. }
  289. /* Clear the si flag */
  290. mpfs_i2c_int_clear(bus);
  291. si_bit = readl(bus->base + MPFS_I2C_CTRL);
  292. return ret;
  293. }
  294. static int mpfs_i2c_check_service_change(struct mpfs_i2c_bus *bus)
  295. {
  296. u8 ctrl;
  297. u32 count = 0;
  298. while (1) {
  299. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  300. if (ctrl & CTRL_SI)
  301. break;
  302. udelay(1);
  303. count += 1;
  304. if (count == MICROCHIP_I2C_TIMEOUT)
  305. return -ETIMEDOUT;
  306. }
  307. return 0;
  308. }
  309. static int mpfs_i2c_poll_device(struct mpfs_i2c_bus *bus)
  310. {
  311. int ret;
  312. while (1) {
  313. ret = mpfs_i2c_check_service_change(bus);
  314. if (ret)
  315. return ret;
  316. ret = mpfs_i2c_service(bus);
  317. if (!ret)
  318. /* all messages have been transferred */
  319. return ret;
  320. }
  321. }
  322. static int mpfs_i2c_xfer_msg(struct mpfs_i2c_bus *bus, struct i2c_msg *msg)
  323. {
  324. u8 ctrl;
  325. int ret;
  326. if (!msg->len || !msg->buf)
  327. return -EINVAL;
  328. bus->addr = i2c_8bit_addr_from_msg(msg);
  329. bus->msg_len = msg->len;
  330. bus->buf = msg->buf;
  331. bus->msg_err = 0;
  332. mpfs_i2c_core_enable(bus);
  333. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  334. ctrl |= CTRL_STA;
  335. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  336. ret = mpfs_i2c_poll_device(bus);
  337. if (ret)
  338. return ret;
  339. return bus->msg_err;
  340. }
  341. static int mpfs_i2c_xfer(struct udevice *dev, struct i2c_msg *msgs, int num_msgs)
  342. {
  343. struct mpfs_i2c_bus *bus = dev_get_priv(dev);
  344. int idx, ret;
  345. if (!msgs || !num_msgs)
  346. return -EINVAL;
  347. for (idx = 0; idx < num_msgs; idx++) {
  348. ret = mpfs_i2c_xfer_msg(bus, msgs++);
  349. if (ret)
  350. return ret;
  351. }
  352. return ret;
  353. }
  354. static int mpfs_i2c_probe_chip(struct udevice *dev, uint addr, uint flags)
  355. {
  356. struct mpfs_i2c_bus *bus = dev_get_priv(dev);
  357. int ret;
  358. u8 ctrl, reg = 0;
  359. /*
  360. * Send the chip address and verify that the
  361. * address was <ACK>ed.
  362. */
  363. bus->addr = addr << 1 | I2C_M_RD;
  364. bus->buf = &reg;
  365. bus->msg_len = 0;
  366. bus->msg_err = 0;
  367. mpfs_i2c_core_enable(bus);
  368. ctrl = readl(bus->base + MPFS_I2C_CTRL);
  369. ctrl |= CTRL_STA;
  370. writel(ctrl, bus->base + MPFS_I2C_CTRL);
  371. ret = mpfs_i2c_poll_device(bus);
  372. if (ret)
  373. return ret;
  374. return bus->msg_err;
  375. }
  376. static int mpfs_i2c_probe(struct udevice *dev)
  377. {
  378. int ret;
  379. u32 val;
  380. struct mpfs_i2c_bus *bus = dev_get_priv(dev);
  381. bus->base = dev_read_addr_ptr(dev);
  382. if (!bus->base)
  383. return -EINVAL;
  384. val = dev_read_u32(dev, "clock-frequency", &bus->clk_rate);
  385. if (val) {
  386. printf("Default to 100kHz\n");
  387. /* default clock rate */
  388. bus->clk_rate = 100000;
  389. }
  390. if (bus->clk_rate > 400000 || bus->clk_rate <= 0) {
  391. printf("Invalid clock-frequency %d\n", bus->clk_rate);
  392. return -EINVAL;
  393. }
  394. ret = mpfs_i2c_init(bus, dev);
  395. return ret;
  396. }
  397. static const struct dm_i2c_ops mpfs_i2c_ops = {
  398. .xfer = mpfs_i2c_xfer,
  399. .probe_chip = mpfs_i2c_probe_chip,
  400. };
  401. static const struct udevice_id mpfs_i2c_ids[] = {
  402. {.compatible = "microchip,mpfs-i2c"},
  403. {}
  404. };
  405. U_BOOT_DRIVER(mpfs_i2c) = {
  406. .name = "mpfs_i2c",
  407. .id = UCLASS_I2C,
  408. .of_match = mpfs_i2c_ids,
  409. .ops = &mpfs_i2c_ops,
  410. .probe = mpfs_i2c_probe,
  411. .priv_auto = sizeof(struct mpfs_i2c_bus),
  412. };