i2c-microchip-corei2c.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip CoreI2C I2C controller driver
  4. *
  5. * Copyright (c) 2018-2022 Microchip Corporation. All rights reserved.
  6. *
  7. * Author: Daire McNamara <daire.mcnamara@microchip.com>
  8. * Author: Conor Dooley <conor.dooley@microchip.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/err.h>
  13. #include <linux/i2c.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #define CORE_I2C_CTRL (0x00)
  20. #define CTRL_CR0 BIT(0)
  21. #define CTRL_CR1 BIT(1)
  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 BIT(7)
  28. #define STATUS_BUS_ERROR (0x00)
  29. #define STATUS_M_START_SENT (0x08)
  30. #define STATUS_M_REPEATED_START_SENT (0x10)
  31. #define STATUS_M_SLAW_ACK (0x18)
  32. #define STATUS_M_SLAW_NACK (0x20)
  33. #define STATUS_M_TX_DATA_ACK (0x28)
  34. #define STATUS_M_TX_DATA_NACK (0x30)
  35. #define STATUS_M_ARB_LOST (0x38)
  36. #define STATUS_M_SLAR_ACK (0x40)
  37. #define STATUS_M_SLAR_NACK (0x48)
  38. #define STATUS_M_RX_DATA_ACKED (0x50)
  39. #define STATUS_M_RX_DATA_NACKED (0x58)
  40. #define STATUS_S_SLAW_ACKED (0x60)
  41. #define STATUS_S_ARB_LOST_SLAW_ACKED (0x68)
  42. #define STATUS_S_GENERAL_CALL_ACKED (0x70)
  43. #define STATUS_S_ARB_LOST_GENERAL_CALL_ACKED (0x78)
  44. #define STATUS_S_RX_DATA_ACKED (0x80)
  45. #define STATUS_S_RX_DATA_NACKED (0x88)
  46. #define STATUS_S_GENERAL_CALL_RX_DATA_ACKED (0x90)
  47. #define STATUS_S_GENERAL_CALL_RX_DATA_NACKED (0x98)
  48. #define STATUS_S_RX_STOP (0xA0)
  49. #define STATUS_S_SLAR_ACKED (0xA8)
  50. #define STATUS_S_ARB_LOST_SLAR_ACKED (0xB0)
  51. #define STATUS_S_TX_DATA_ACK (0xB8)
  52. #define STATUS_S_TX_DATA_NACK (0xC0)
  53. #define STATUS_LAST_DATA_ACK (0xC8)
  54. #define STATUS_M_SMB_MASTER_RESET (0xD0)
  55. #define STATUS_S_SCL_LOW_TIMEOUT (0xD8) /* 25 ms */
  56. #define STATUS_NO_STATE_INFO (0xF8)
  57. #define CORE_I2C_STATUS (0x04)
  58. #define CORE_I2C_DATA (0x08)
  59. #define WRITE_BIT (0x0)
  60. #define READ_BIT (0x1)
  61. #define SLAVE_ADDR_SHIFT (1)
  62. #define CORE_I2C_SLAVE0_ADDR (0x0c)
  63. #define GENERAL_CALL_BIT (0x0)
  64. #define CORE_I2C_SMBUS (0x10)
  65. #define SMBALERT_INT_ENB (0x0)
  66. #define SMBSUS_INT_ENB (0x1)
  67. #define SMBUS_ENB (0x2)
  68. #define SMBALERT_NI_STATUS (0x3)
  69. #define SMBALERT_NO_CTRL (0x4)
  70. #define SMBSUS_NI_STATUS (0x5)
  71. #define SMBSUS_NO_CTRL (0x6)
  72. #define SMBUS_RESET (0x7)
  73. #define CORE_I2C_FREQ (0x14)
  74. #define CORE_I2C_GLITCHREG (0x18)
  75. #define CORE_I2C_SLAVE1_ADDR (0x1c)
  76. #define PCLK_DIV_960 (CTRL_CR2)
  77. #define PCLK_DIV_256 (0)
  78. #define PCLK_DIV_224 (CTRL_CR0)
  79. #define PCLK_DIV_192 (CTRL_CR1)
  80. #define PCLK_DIV_160 (CTRL_CR0 | CTRL_CR1)
  81. #define PCLK_DIV_120 (CTRL_CR0 | CTRL_CR2)
  82. #define PCLK_DIV_60 (CTRL_CR1 | CTRL_CR2)
  83. #define BCLK_DIV_8 (CTRL_CR0 | CTRL_CR1 | CTRL_CR2)
  84. #define CLK_MASK (CTRL_CR0 | CTRL_CR1 | CTRL_CR2)
  85. /**
  86. * struct mchp_corei2c_dev - Microchip CoreI2C device private data
  87. *
  88. * @base: pointer to register struct
  89. * @dev: device reference
  90. * @i2c_clk: clock reference for i2c input clock
  91. * @msg_queue: pointer to the messages requiring sending
  92. * @buf: pointer to msg buffer for easier use
  93. * @msg_complete: xfer completion object
  94. * @adapter: core i2c abstraction
  95. * @msg_err: error code for completed message
  96. * @bus_clk_rate: current i2c bus clock rate
  97. * @isr_status: cached copy of local ISR status
  98. * @total_num: total number of messages to be sent/received
  99. * @current_num: index of the current message being sent/received
  100. * @msg_len: number of bytes transferred in msg
  101. * @addr: address of the current slave
  102. * @restart_needed: whether or not a repeated start is required after current message
  103. */
  104. struct mchp_corei2c_dev {
  105. void __iomem *base;
  106. struct device *dev;
  107. struct clk *i2c_clk;
  108. struct i2c_msg *msg_queue;
  109. u8 *buf;
  110. struct completion msg_complete;
  111. struct i2c_adapter adapter;
  112. int msg_err;
  113. int total_num;
  114. int current_num;
  115. u32 bus_clk_rate;
  116. u32 isr_status;
  117. u16 msg_len;
  118. u8 addr;
  119. bool restart_needed;
  120. };
  121. static void mchp_corei2c_core_disable(struct mchp_corei2c_dev *idev)
  122. {
  123. u8 ctrl = readb(idev->base + CORE_I2C_CTRL);
  124. ctrl &= ~CTRL_ENS1;
  125. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  126. }
  127. static void mchp_corei2c_core_enable(struct mchp_corei2c_dev *idev)
  128. {
  129. u8 ctrl = readb(idev->base + CORE_I2C_CTRL);
  130. ctrl |= CTRL_ENS1;
  131. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  132. }
  133. static void mchp_corei2c_reset(struct mchp_corei2c_dev *idev)
  134. {
  135. mchp_corei2c_core_disable(idev);
  136. mchp_corei2c_core_enable(idev);
  137. }
  138. static inline void mchp_corei2c_stop(struct mchp_corei2c_dev *idev)
  139. {
  140. u8 ctrl = readb(idev->base + CORE_I2C_CTRL);
  141. ctrl |= CTRL_STO;
  142. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  143. }
  144. static inline int mchp_corei2c_set_divisor(u32 rate,
  145. struct mchp_corei2c_dev *idev)
  146. {
  147. u8 clkval, ctrl;
  148. if (rate >= 960)
  149. clkval = PCLK_DIV_960;
  150. else if (rate >= 256)
  151. clkval = PCLK_DIV_256;
  152. else if (rate >= 224)
  153. clkval = PCLK_DIV_224;
  154. else if (rate >= 192)
  155. clkval = PCLK_DIV_192;
  156. else if (rate >= 160)
  157. clkval = PCLK_DIV_160;
  158. else if (rate >= 120)
  159. clkval = PCLK_DIV_120;
  160. else if (rate >= 60)
  161. clkval = PCLK_DIV_60;
  162. else if (rate >= 8)
  163. clkval = BCLK_DIV_8;
  164. else
  165. return -EINVAL;
  166. ctrl = readb(idev->base + CORE_I2C_CTRL);
  167. ctrl &= ~CLK_MASK;
  168. ctrl |= clkval;
  169. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  170. ctrl = readb(idev->base + CORE_I2C_CTRL);
  171. if ((ctrl & CLK_MASK) != clkval)
  172. return -EIO;
  173. return 0;
  174. }
  175. static int mchp_corei2c_init(struct mchp_corei2c_dev *idev)
  176. {
  177. u32 clk_rate = clk_get_rate(idev->i2c_clk);
  178. u32 divisor = clk_rate / idev->bus_clk_rate;
  179. int ret;
  180. ret = mchp_corei2c_set_divisor(divisor, idev);
  181. if (ret)
  182. return ret;
  183. mchp_corei2c_reset(idev);
  184. return 0;
  185. }
  186. static void mchp_corei2c_empty_rx(struct mchp_corei2c_dev *idev)
  187. {
  188. u8 ctrl;
  189. if (idev->msg_len > 0) {
  190. *idev->buf++ = readb(idev->base + CORE_I2C_DATA);
  191. idev->msg_len--;
  192. }
  193. if (idev->msg_len <= 1) {
  194. ctrl = readb(idev->base + CORE_I2C_CTRL);
  195. ctrl &= ~CTRL_AA;
  196. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  197. }
  198. }
  199. static int mchp_corei2c_fill_tx(struct mchp_corei2c_dev *idev)
  200. {
  201. if (idev->msg_len > 0)
  202. writeb(*idev->buf++, idev->base + CORE_I2C_DATA);
  203. idev->msg_len--;
  204. return 0;
  205. }
  206. static void mchp_corei2c_next_msg(struct mchp_corei2c_dev *idev)
  207. {
  208. struct i2c_msg *this_msg;
  209. u8 ctrl;
  210. if (idev->current_num >= idev->total_num) {
  211. complete(&idev->msg_complete);
  212. return;
  213. }
  214. /*
  215. * If there's been an error, the isr needs to return control
  216. * to the "main" part of the driver, so as not to keep sending
  217. * messages once it completes and clears the SI bit.
  218. */
  219. if (idev->msg_err) {
  220. complete(&idev->msg_complete);
  221. return;
  222. }
  223. this_msg = idev->msg_queue++;
  224. if (idev->current_num < (idev->total_num - 1)) {
  225. struct i2c_msg *next_msg = idev->msg_queue;
  226. idev->restart_needed = next_msg->flags & I2C_M_RD;
  227. } else {
  228. idev->restart_needed = false;
  229. }
  230. idev->addr = i2c_8bit_addr_from_msg(this_msg);
  231. idev->msg_len = this_msg->len;
  232. idev->buf = this_msg->buf;
  233. ctrl = readb(idev->base + CORE_I2C_CTRL);
  234. ctrl |= CTRL_STA;
  235. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  236. idev->current_num++;
  237. }
  238. static irqreturn_t mchp_corei2c_handle_isr(struct mchp_corei2c_dev *idev)
  239. {
  240. u32 status = idev->isr_status;
  241. u8 ctrl;
  242. bool last_byte = false, finished = false;
  243. if (!idev->buf)
  244. return IRQ_NONE;
  245. switch (status) {
  246. case STATUS_M_START_SENT:
  247. case STATUS_M_REPEATED_START_SENT:
  248. ctrl = readb(idev->base + CORE_I2C_CTRL);
  249. ctrl &= ~CTRL_STA;
  250. writeb(idev->addr, idev->base + CORE_I2C_DATA);
  251. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  252. break;
  253. case STATUS_M_ARB_LOST:
  254. idev->msg_err = -EAGAIN;
  255. finished = true;
  256. break;
  257. case STATUS_M_SLAW_ACK:
  258. case STATUS_M_TX_DATA_ACK:
  259. if (idev->msg_len > 0) {
  260. mchp_corei2c_fill_tx(idev);
  261. } else {
  262. if (idev->restart_needed)
  263. finished = true;
  264. else
  265. last_byte = true;
  266. }
  267. break;
  268. case STATUS_M_TX_DATA_NACK:
  269. case STATUS_M_SLAR_NACK:
  270. case STATUS_M_SLAW_NACK:
  271. idev->msg_err = -ENXIO;
  272. last_byte = true;
  273. break;
  274. case STATUS_M_SLAR_ACK:
  275. ctrl = readb(idev->base + CORE_I2C_CTRL);
  276. if (idev->msg_len == 1u) {
  277. ctrl &= ~CTRL_AA;
  278. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  279. } else {
  280. ctrl |= CTRL_AA;
  281. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  282. }
  283. if (idev->msg_len < 1u)
  284. last_byte = true;
  285. break;
  286. case STATUS_M_RX_DATA_ACKED:
  287. mchp_corei2c_empty_rx(idev);
  288. break;
  289. case STATUS_M_RX_DATA_NACKED:
  290. mchp_corei2c_empty_rx(idev);
  291. if (idev->msg_len == 0)
  292. last_byte = true;
  293. break;
  294. default:
  295. break;
  296. }
  297. /* On the last byte to be transmitted, send STOP */
  298. if (last_byte)
  299. mchp_corei2c_stop(idev);
  300. if (last_byte || finished)
  301. mchp_corei2c_next_msg(idev);
  302. return IRQ_HANDLED;
  303. }
  304. static irqreturn_t mchp_corei2c_isr(int irq, void *_dev)
  305. {
  306. struct mchp_corei2c_dev *idev = _dev;
  307. irqreturn_t ret = IRQ_NONE;
  308. u8 ctrl;
  309. ctrl = readb(idev->base + CORE_I2C_CTRL);
  310. if (ctrl & CTRL_SI) {
  311. idev->isr_status = readb(idev->base + CORE_I2C_STATUS);
  312. ret = mchp_corei2c_handle_isr(idev);
  313. }
  314. ctrl = readb(idev->base + CORE_I2C_CTRL);
  315. ctrl &= ~CTRL_SI;
  316. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  317. return ret;
  318. }
  319. static int mchp_corei2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
  320. int num)
  321. {
  322. struct mchp_corei2c_dev *idev = i2c_get_adapdata(adap);
  323. struct i2c_msg *this_msg = msgs;
  324. unsigned long time_left;
  325. u8 ctrl;
  326. mchp_corei2c_core_enable(idev);
  327. /*
  328. * The isr controls the flow of a transfer, this info needs to be saved
  329. * to a location that it can access the queue information from.
  330. */
  331. idev->restart_needed = false;
  332. idev->msg_queue = msgs;
  333. idev->total_num = num;
  334. idev->current_num = 0;
  335. /*
  336. * But the first entry to the isr is triggered by the start in this
  337. * function, so the first message needs to be "dequeued".
  338. */
  339. idev->addr = i2c_8bit_addr_from_msg(this_msg);
  340. idev->msg_len = this_msg->len;
  341. idev->buf = this_msg->buf;
  342. idev->msg_err = 0;
  343. if (idev->total_num > 1) {
  344. struct i2c_msg *next_msg = msgs + 1;
  345. idev->restart_needed = next_msg->flags & I2C_M_RD;
  346. }
  347. idev->current_num++;
  348. idev->msg_queue++;
  349. reinit_completion(&idev->msg_complete);
  350. /*
  351. * Send the first start to pass control to the isr
  352. */
  353. ctrl = readb(idev->base + CORE_I2C_CTRL);
  354. ctrl |= CTRL_STA;
  355. writeb(ctrl, idev->base + CORE_I2C_CTRL);
  356. time_left = wait_for_completion_timeout(&idev->msg_complete,
  357. idev->adapter.timeout);
  358. if (!time_left)
  359. return -ETIMEDOUT;
  360. if (idev->msg_err)
  361. return idev->msg_err;
  362. return num;
  363. }
  364. static u32 mchp_corei2c_func(struct i2c_adapter *adap)
  365. {
  366. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  367. }
  368. static const struct i2c_algorithm mchp_corei2c_algo = {
  369. .master_xfer = mchp_corei2c_xfer,
  370. .functionality = mchp_corei2c_func,
  371. };
  372. static int mchp_corei2c_probe(struct platform_device *pdev)
  373. {
  374. struct mchp_corei2c_dev *idev;
  375. struct resource *res;
  376. int irq, ret;
  377. idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
  378. if (!idev)
  379. return -ENOMEM;
  380. idev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  381. if (IS_ERR(idev->base))
  382. return PTR_ERR(idev->base);
  383. irq = platform_get_irq(pdev, 0);
  384. if (irq < 0)
  385. return irq;
  386. idev->i2c_clk = devm_clk_get(&pdev->dev, NULL);
  387. if (IS_ERR(idev->i2c_clk))
  388. return dev_err_probe(&pdev->dev, PTR_ERR(idev->i2c_clk),
  389. "missing clock\n");
  390. idev->dev = &pdev->dev;
  391. init_completion(&idev->msg_complete);
  392. ret = device_property_read_u32(idev->dev, "clock-frequency",
  393. &idev->bus_clk_rate);
  394. if (ret || !idev->bus_clk_rate) {
  395. dev_info(&pdev->dev, "default to 100kHz\n");
  396. idev->bus_clk_rate = 100000;
  397. }
  398. if (idev->bus_clk_rate > 400000)
  399. return dev_err_probe(&pdev->dev, -EINVAL,
  400. "clock-frequency too high: %d\n",
  401. idev->bus_clk_rate);
  402. /*
  403. * This driver supports both the hard peripherals & soft FPGA cores.
  404. * The hard peripherals do not have shared IRQs, but we don't have
  405. * control over what way the interrupts are wired for the soft cores.
  406. */
  407. ret = devm_request_irq(&pdev->dev, irq, mchp_corei2c_isr, IRQF_SHARED,
  408. pdev->name, idev);
  409. if (ret)
  410. return dev_err_probe(&pdev->dev, ret,
  411. "failed to claim irq %d\n", irq);
  412. ret = clk_prepare_enable(idev->i2c_clk);
  413. if (ret)
  414. return dev_err_probe(&pdev->dev, ret,
  415. "failed to enable clock\n");
  416. ret = mchp_corei2c_init(idev);
  417. if (ret) {
  418. clk_disable_unprepare(idev->i2c_clk);
  419. return dev_err_probe(&pdev->dev, ret, "failed to program clock divider\n");
  420. }
  421. i2c_set_adapdata(&idev->adapter, idev);
  422. snprintf(idev->adapter.name, sizeof(idev->adapter.name),
  423. "Microchip I2C hw bus at %08lx", (unsigned long)res->start);
  424. idev->adapter.owner = THIS_MODULE;
  425. idev->adapter.algo = &mchp_corei2c_algo;
  426. idev->adapter.dev.parent = &pdev->dev;
  427. idev->adapter.dev.of_node = pdev->dev.of_node;
  428. idev->adapter.timeout = HZ;
  429. platform_set_drvdata(pdev, idev);
  430. ret = i2c_add_adapter(&idev->adapter);
  431. if (ret) {
  432. clk_disable_unprepare(idev->i2c_clk);
  433. return ret;
  434. }
  435. dev_info(&pdev->dev, "registered CoreI2C bus driver\n");
  436. return 0;
  437. }
  438. static void mchp_corei2c_remove(struct platform_device *pdev)
  439. {
  440. struct mchp_corei2c_dev *idev = platform_get_drvdata(pdev);
  441. clk_disable_unprepare(idev->i2c_clk);
  442. i2c_del_adapter(&idev->adapter);
  443. }
  444. static const struct of_device_id mchp_corei2c_of_match[] = {
  445. { .compatible = "microchip,mpfs-i2c" },
  446. { .compatible = "microchip,corei2c-rtl-v7" },
  447. {},
  448. };
  449. MODULE_DEVICE_TABLE(of, mchp_corei2c_of_match);
  450. static struct platform_driver mchp_corei2c_driver = {
  451. .probe = mchp_corei2c_probe,
  452. .remove_new = mchp_corei2c_remove,
  453. .driver = {
  454. .name = "microchip-corei2c",
  455. .of_match_table = mchp_corei2c_of_match,
  456. },
  457. };
  458. module_platform_driver(mchp_corei2c_driver);
  459. MODULE_DESCRIPTION("Microchip CoreI2C bus driver");
  460. MODULE_AUTHOR("Daire McNamara <daire.mcnamara@microchip.com>");
  461. MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
  462. MODULE_LICENSE("GPL");