i2c-riic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Renesas RIIC driver
  3. *
  4. * Copyright (C) 2013 Wolfram Sang <wsa@sang-engineering.com>
  5. * Copyright (C) 2013 Renesas Solutions Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. */
  11. /*
  12. * This i2c core has a lot of interrupts, namely 8. We use their chaining as
  13. * some kind of state machine.
  14. *
  15. * 1) The main xfer routine kicks off a transmission by putting the start bit
  16. * (or repeated start) on the bus and enabling the transmit interrupt (TIE)
  17. * since we need to send the slave address + RW bit in every case.
  18. *
  19. * 2) TIE sends slave address + RW bit and selects how to continue.
  20. *
  21. * 3a) Write case: We keep utilizing TIE as long as we have data to send. If we
  22. * are done, we switch over to the transmission done interrupt (TEIE) and mark
  23. * the message as completed (includes sending STOP) there.
  24. *
  25. * 3b) Read case: We switch over to receive interrupt (RIE). One dummy read is
  26. * needed to start clocking, then we keep receiving until we are done. Note
  27. * that we use the RDRFS mode all the time, i.e. we ACK/NACK every byte by
  28. * writing to the ACKBT bit. I tried using the RDRFS mode only at the end of a
  29. * message to create the final NACK as sketched in the datasheet. This caused
  30. * some subtle races (when byte n was processed and byte n+1 was already
  31. * waiting), though, and I started with the safe approach.
  32. *
  33. * 4) If we got a NACK somewhere, we flag the error and stop the transmission
  34. * via NAKIE.
  35. *
  36. * Also check the comments in the interrupt routines for some gory details.
  37. */
  38. #include <linux/clk.h>
  39. #include <linux/completion.h>
  40. #include <linux/err.h>
  41. #include <linux/i2c.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/io.h>
  44. #include <linux/module.h>
  45. #include <linux/of.h>
  46. #include <linux/platform_device.h>
  47. #define RIIC_ICCR1 0x00
  48. #define RIIC_ICCR2 0x04
  49. #define RIIC_ICMR1 0x08
  50. #define RIIC_ICMR3 0x10
  51. #define RIIC_ICSER 0x18
  52. #define RIIC_ICIER 0x1c
  53. #define RIIC_ICSR2 0x24
  54. #define RIIC_ICBRL 0x34
  55. #define RIIC_ICBRH 0x38
  56. #define RIIC_ICDRT 0x3c
  57. #define RIIC_ICDRR 0x40
  58. #define ICCR1_ICE 0x80
  59. #define ICCR1_IICRST 0x40
  60. #define ICCR1_SOWP 0x10
  61. #define ICCR2_BBSY 0x80
  62. #define ICCR2_SP 0x08
  63. #define ICCR2_RS 0x04
  64. #define ICCR2_ST 0x02
  65. #define ICMR1_CKS_MASK 0x70
  66. #define ICMR1_BCWP 0x08
  67. #define ICMR1_CKS(_x) ((((_x) << 4) & ICMR1_CKS_MASK) | ICMR1_BCWP)
  68. #define ICMR3_RDRFS 0x20
  69. #define ICMR3_ACKWP 0x10
  70. #define ICMR3_ACKBT 0x08
  71. #define ICIER_TIE 0x80
  72. #define ICIER_TEIE 0x40
  73. #define ICIER_RIE 0x20
  74. #define ICIER_NAKIE 0x10
  75. #define ICIER_SPIE 0x08
  76. #define ICSR2_NACKF 0x10
  77. /* ICBRx (@ PCLK 33MHz) */
  78. #define ICBR_RESERVED 0xe0 /* Should be 1 on writes */
  79. #define ICBRL_SP100K (19 | ICBR_RESERVED)
  80. #define ICBRH_SP100K (16 | ICBR_RESERVED)
  81. #define ICBRL_SP400K (21 | ICBR_RESERVED)
  82. #define ICBRH_SP400K (9 | ICBR_RESERVED)
  83. #define RIIC_INIT_MSG -1
  84. struct riic_dev {
  85. void __iomem *base;
  86. u8 *buf;
  87. struct i2c_msg *msg;
  88. int bytes_left;
  89. int err;
  90. int is_last;
  91. struct completion msg_done;
  92. struct i2c_adapter adapter;
  93. struct clk *clk;
  94. };
  95. struct riic_irq_desc {
  96. int res_num;
  97. irq_handler_t isr;
  98. char *name;
  99. };
  100. static inline void riic_clear_set_bit(struct riic_dev *riic, u8 clear, u8 set, u8 reg)
  101. {
  102. writeb((readb(riic->base + reg) & ~clear) | set, riic->base + reg);
  103. }
  104. static int riic_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
  105. {
  106. struct riic_dev *riic = i2c_get_adapdata(adap);
  107. unsigned long time_left;
  108. int i, ret;
  109. u8 start_bit;
  110. ret = clk_prepare_enable(riic->clk);
  111. if (ret)
  112. return ret;
  113. if (readb(riic->base + RIIC_ICCR2) & ICCR2_BBSY) {
  114. riic->err = -EBUSY;
  115. goto out;
  116. }
  117. reinit_completion(&riic->msg_done);
  118. riic->err = 0;
  119. writeb(0, riic->base + RIIC_ICSR2);
  120. for (i = 0, start_bit = ICCR2_ST; i < num; i++) {
  121. riic->bytes_left = RIIC_INIT_MSG;
  122. riic->buf = msgs[i].buf;
  123. riic->msg = &msgs[i];
  124. riic->is_last = (i == num - 1);
  125. writeb(ICIER_NAKIE | ICIER_TIE, riic->base + RIIC_ICIER);
  126. writeb(start_bit, riic->base + RIIC_ICCR2);
  127. time_left = wait_for_completion_timeout(&riic->msg_done, riic->adapter.timeout);
  128. if (time_left == 0)
  129. riic->err = -ETIMEDOUT;
  130. if (riic->err)
  131. break;
  132. start_bit = ICCR2_RS;
  133. }
  134. out:
  135. clk_disable_unprepare(riic->clk);
  136. return riic->err ?: num;
  137. }
  138. static irqreturn_t riic_tdre_isr(int irq, void *data)
  139. {
  140. struct riic_dev *riic = data;
  141. u8 val;
  142. if (!riic->bytes_left)
  143. return IRQ_NONE;
  144. if (riic->bytes_left == RIIC_INIT_MSG) {
  145. val = !!(riic->msg->flags & I2C_M_RD);
  146. if (val)
  147. /* On read, switch over to receive interrupt */
  148. riic_clear_set_bit(riic, ICIER_TIE, ICIER_RIE, RIIC_ICIER);
  149. else
  150. /* On write, initialize length */
  151. riic->bytes_left = riic->msg->len;
  152. val |= (riic->msg->addr << 1);
  153. } else {
  154. val = *riic->buf;
  155. riic->buf++;
  156. riic->bytes_left--;
  157. }
  158. /*
  159. * Switch to transmission ended interrupt when done. Do check here
  160. * after bytes_left was initialized to support SMBUS_QUICK (new msg has
  161. * 0 length then)
  162. */
  163. if (riic->bytes_left == 0)
  164. riic_clear_set_bit(riic, ICIER_TIE, ICIER_TEIE, RIIC_ICIER);
  165. /*
  166. * This acks the TIE interrupt. We get another TIE immediately if our
  167. * value could be moved to the shadow shift register right away. So
  168. * this must be after updates to ICIER (where we want to disable TIE)!
  169. */
  170. writeb(val, riic->base + RIIC_ICDRT);
  171. return IRQ_HANDLED;
  172. }
  173. static irqreturn_t riic_tend_isr(int irq, void *data)
  174. {
  175. struct riic_dev *riic = data;
  176. if (readb(riic->base + RIIC_ICSR2) & ICSR2_NACKF) {
  177. /* We got a NACKIE */
  178. readb(riic->base + RIIC_ICDRR); /* dummy read */
  179. riic->err = -ENXIO;
  180. } else if (riic->bytes_left) {
  181. return IRQ_NONE;
  182. }
  183. if (riic->is_last || riic->err) {
  184. riic_clear_set_bit(riic, ICIER_TEIE, ICIER_SPIE, RIIC_ICIER);
  185. writeb(ICCR2_SP, riic->base + RIIC_ICCR2);
  186. } else {
  187. /* Transfer is complete, but do not send STOP */
  188. riic_clear_set_bit(riic, ICIER_TEIE, 0, RIIC_ICIER);
  189. complete(&riic->msg_done);
  190. }
  191. return IRQ_HANDLED;
  192. }
  193. static irqreturn_t riic_rdrf_isr(int irq, void *data)
  194. {
  195. struct riic_dev *riic = data;
  196. if (!riic->bytes_left)
  197. return IRQ_NONE;
  198. if (riic->bytes_left == RIIC_INIT_MSG) {
  199. riic->bytes_left = riic->msg->len;
  200. readb(riic->base + RIIC_ICDRR); /* dummy read */
  201. return IRQ_HANDLED;
  202. }
  203. if (riic->bytes_left == 1) {
  204. /* STOP must come before we set ACKBT! */
  205. if (riic->is_last) {
  206. riic_clear_set_bit(riic, 0, ICIER_SPIE, RIIC_ICIER);
  207. writeb(ICCR2_SP, riic->base + RIIC_ICCR2);
  208. }
  209. riic_clear_set_bit(riic, 0, ICMR3_ACKBT, RIIC_ICMR3);
  210. } else {
  211. riic_clear_set_bit(riic, ICMR3_ACKBT, 0, RIIC_ICMR3);
  212. }
  213. /* Reading acks the RIE interrupt */
  214. *riic->buf = readb(riic->base + RIIC_ICDRR);
  215. riic->buf++;
  216. riic->bytes_left--;
  217. return IRQ_HANDLED;
  218. }
  219. static irqreturn_t riic_stop_isr(int irq, void *data)
  220. {
  221. struct riic_dev *riic = data;
  222. /* read back registers to confirm writes have fully propagated */
  223. writeb(0, riic->base + RIIC_ICSR2);
  224. readb(riic->base + RIIC_ICSR2);
  225. writeb(0, riic->base + RIIC_ICIER);
  226. readb(riic->base + RIIC_ICIER);
  227. complete(&riic->msg_done);
  228. return IRQ_HANDLED;
  229. }
  230. static u32 riic_func(struct i2c_adapter *adap)
  231. {
  232. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  233. }
  234. static const struct i2c_algorithm riic_algo = {
  235. .master_xfer = riic_xfer,
  236. .functionality = riic_func,
  237. };
  238. static int riic_init_hw(struct riic_dev *riic, u32 spd)
  239. {
  240. int ret;
  241. unsigned long rate;
  242. ret = clk_prepare_enable(riic->clk);
  243. if (ret)
  244. return ret;
  245. /*
  246. * TODO: Implement formula to calculate the timing values depending on
  247. * variable parent clock rate and arbitrary bus speed
  248. */
  249. rate = clk_get_rate(riic->clk);
  250. if (rate != 33325000) {
  251. dev_err(&riic->adapter.dev,
  252. "invalid parent clk (%lu). Must be 33325000Hz\n", rate);
  253. clk_disable_unprepare(riic->clk);
  254. return -EINVAL;
  255. }
  256. /* Changing the order of accessing IICRST and ICE may break things! */
  257. writeb(ICCR1_IICRST | ICCR1_SOWP, riic->base + RIIC_ICCR1);
  258. riic_clear_set_bit(riic, 0, ICCR1_ICE, RIIC_ICCR1);
  259. switch (spd) {
  260. case 100000:
  261. writeb(ICMR1_CKS(3), riic->base + RIIC_ICMR1);
  262. writeb(ICBRH_SP100K, riic->base + RIIC_ICBRH);
  263. writeb(ICBRL_SP100K, riic->base + RIIC_ICBRL);
  264. break;
  265. case 400000:
  266. writeb(ICMR1_CKS(1), riic->base + RIIC_ICMR1);
  267. writeb(ICBRH_SP400K, riic->base + RIIC_ICBRH);
  268. writeb(ICBRL_SP400K, riic->base + RIIC_ICBRL);
  269. break;
  270. default:
  271. dev_err(&riic->adapter.dev,
  272. "unsupported bus speed (%dHz). Use 100000 or 400000\n", spd);
  273. clk_disable_unprepare(riic->clk);
  274. return -EINVAL;
  275. }
  276. writeb(0, riic->base + RIIC_ICSER);
  277. writeb(ICMR3_ACKWP | ICMR3_RDRFS, riic->base + RIIC_ICMR3);
  278. riic_clear_set_bit(riic, ICCR1_IICRST, 0, RIIC_ICCR1);
  279. clk_disable_unprepare(riic->clk);
  280. return 0;
  281. }
  282. static struct riic_irq_desc riic_irqs[] = {
  283. { .res_num = 0, .isr = riic_tend_isr, .name = "riic-tend" },
  284. { .res_num = 1, .isr = riic_rdrf_isr, .name = "riic-rdrf" },
  285. { .res_num = 2, .isr = riic_tdre_isr, .name = "riic-tdre" },
  286. { .res_num = 3, .isr = riic_stop_isr, .name = "riic-stop" },
  287. { .res_num = 5, .isr = riic_tend_isr, .name = "riic-nack" },
  288. };
  289. static int riic_i2c_probe(struct platform_device *pdev)
  290. {
  291. struct device_node *np = pdev->dev.of_node;
  292. struct riic_dev *riic;
  293. struct i2c_adapter *adap;
  294. struct resource *res;
  295. u32 bus_rate = 0;
  296. int i, ret;
  297. riic = devm_kzalloc(&pdev->dev, sizeof(*riic), GFP_KERNEL);
  298. if (!riic)
  299. return -ENOMEM;
  300. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  301. riic->base = devm_ioremap_resource(&pdev->dev, res);
  302. if (IS_ERR(riic->base))
  303. return PTR_ERR(riic->base);
  304. riic->clk = devm_clk_get(&pdev->dev, NULL);
  305. if (IS_ERR(riic->clk)) {
  306. dev_err(&pdev->dev, "missing controller clock");
  307. return PTR_ERR(riic->clk);
  308. }
  309. for (i = 0; i < ARRAY_SIZE(riic_irqs); i++) {
  310. res = platform_get_resource(pdev, IORESOURCE_IRQ, riic_irqs[i].res_num);
  311. if (!res)
  312. return -ENODEV;
  313. ret = devm_request_irq(&pdev->dev, res->start, riic_irqs[i].isr,
  314. 0, riic_irqs[i].name, riic);
  315. if (ret) {
  316. dev_err(&pdev->dev, "failed to request irq %s\n", riic_irqs[i].name);
  317. return ret;
  318. }
  319. }
  320. adap = &riic->adapter;
  321. i2c_set_adapdata(adap, riic);
  322. strlcpy(adap->name, "Renesas RIIC adapter", sizeof(adap->name));
  323. adap->owner = THIS_MODULE;
  324. adap->algo = &riic_algo;
  325. adap->dev.parent = &pdev->dev;
  326. adap->dev.of_node = pdev->dev.of_node;
  327. init_completion(&riic->msg_done);
  328. of_property_read_u32(np, "clock-frequency", &bus_rate);
  329. ret = riic_init_hw(riic, bus_rate);
  330. if (ret)
  331. return ret;
  332. ret = i2c_add_adapter(adap);
  333. if (ret) {
  334. dev_err(&pdev->dev, "failed to add adapter\n");
  335. return ret;
  336. }
  337. platform_set_drvdata(pdev, riic);
  338. dev_info(&pdev->dev, "registered with %dHz bus speed\n", bus_rate);
  339. return 0;
  340. }
  341. static int riic_i2c_remove(struct platform_device *pdev)
  342. {
  343. struct riic_dev *riic = platform_get_drvdata(pdev);
  344. writeb(0, riic->base + RIIC_ICIER);
  345. i2c_del_adapter(&riic->adapter);
  346. return 0;
  347. }
  348. static const struct of_device_id riic_i2c_dt_ids[] = {
  349. { .compatible = "renesas,riic-rz" },
  350. { /* Sentinel */ },
  351. };
  352. static struct platform_driver riic_i2c_driver = {
  353. .probe = riic_i2c_probe,
  354. .remove = riic_i2c_remove,
  355. .driver = {
  356. .name = "i2c-riic",
  357. .of_match_table = riic_i2c_dt_ids,
  358. },
  359. };
  360. module_platform_driver(riic_i2c_driver);
  361. MODULE_DESCRIPTION("Renesas RIIC adapter");
  362. MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
  363. MODULE_LICENSE("GPL v2");
  364. MODULE_DEVICE_TABLE(of, riic_i2c_dt_ids);