mxs_i2c.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Freescale i.MX28 I2C Driver
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. * on behalf of DENX Software Engineering GmbH
  6. *
  7. * Partly based on Linux kernel i2c-mxs.c driver:
  8. * Copyright (C) 2011 Wolfram Sang, Pengutronix e.K.
  9. *
  10. * Which was based on a (non-working) driver which was:
  11. * Copyright (C) 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
  12. *
  13. * SPDX-License-Identifier: GPL-2.0+
  14. */
  15. #include <common.h>
  16. #include <malloc.h>
  17. #include <i2c.h>
  18. #include <linux/errno.h>
  19. #include <asm/io.h>
  20. #include <asm/arch/clock.h>
  21. #include <asm/arch/imx-regs.h>
  22. #include <asm/arch/sys_proto.h>
  23. #define MXS_I2C_MAX_TIMEOUT 1000000
  24. static struct mxs_i2c_regs *mxs_i2c_get_base(struct i2c_adapter *adap)
  25. {
  26. if (adap->hwadapnr == 0)
  27. return (struct mxs_i2c_regs *)MXS_I2C0_BASE;
  28. else
  29. return (struct mxs_i2c_regs *)MXS_I2C1_BASE;
  30. }
  31. static unsigned int mxs_i2c_get_bus_speed(struct i2c_adapter *adap)
  32. {
  33. struct mxs_i2c_regs *i2c_regs = mxs_i2c_get_base(adap);
  34. uint32_t clk = mxc_get_clock(MXC_XTAL_CLK);
  35. uint32_t timing0;
  36. timing0 = readl(&i2c_regs->hw_i2c_timing0);
  37. /*
  38. * This is a reverse version of the algorithm presented in
  39. * i2c_set_bus_speed(). Please refer there for details.
  40. */
  41. return clk / ((((timing0 >> 16) - 3) * 2) + 38);
  42. }
  43. static uint mxs_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
  44. {
  45. struct mxs_i2c_regs *i2c_regs = mxs_i2c_get_base(adap);
  46. /*
  47. * The timing derivation algorithm. There is no documentation for this
  48. * algorithm available, it was derived by using the scope and fiddling
  49. * with constants until the result observed on the scope was good enough
  50. * for 20kHz, 50kHz, 100kHz, 200kHz, 300kHz and 400kHz. It should be
  51. * possible to assume the algorithm works for other frequencies as well.
  52. *
  53. * Note it was necessary to cap the frequency on both ends as it's not
  54. * possible to configure completely arbitrary frequency for the I2C bus
  55. * clock.
  56. */
  57. uint32_t clk = mxc_get_clock(MXC_XTAL_CLK);
  58. uint32_t base = ((clk / speed) - 38) / 2;
  59. uint16_t high_count = base + 3;
  60. uint16_t low_count = base - 3;
  61. uint16_t rcv_count = (high_count * 3) / 4;
  62. uint16_t xmit_count = low_count / 4;
  63. if (speed > 540000) {
  64. printf("MXS I2C: Speed too high (%d Hz)\n", speed);
  65. return -EINVAL;
  66. }
  67. if (speed < 12000) {
  68. printf("MXS I2C: Speed too low (%d Hz)\n", speed);
  69. return -EINVAL;
  70. }
  71. writel((high_count << 16) | rcv_count, &i2c_regs->hw_i2c_timing0);
  72. writel((low_count << 16) | xmit_count, &i2c_regs->hw_i2c_timing1);
  73. writel((0x0030 << I2C_TIMING2_BUS_FREE_OFFSET) |
  74. (0x0030 << I2C_TIMING2_LEADIN_COUNT_OFFSET),
  75. &i2c_regs->hw_i2c_timing2);
  76. return 0;
  77. }
  78. static void mxs_i2c_reset(struct i2c_adapter *adap)
  79. {
  80. struct mxs_i2c_regs *i2c_regs = mxs_i2c_get_base(adap);
  81. int ret;
  82. int speed = mxs_i2c_get_bus_speed(adap);
  83. ret = mxs_reset_block(&i2c_regs->hw_i2c_ctrl0_reg);
  84. if (ret) {
  85. debug("MXS I2C: Block reset timeout\n");
  86. return;
  87. }
  88. writel(I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | I2C_CTRL1_NO_SLAVE_ACK_IRQ |
  89. I2C_CTRL1_EARLY_TERM_IRQ | I2C_CTRL1_MASTER_LOSS_IRQ |
  90. I2C_CTRL1_SLAVE_STOP_IRQ | I2C_CTRL1_SLAVE_IRQ,
  91. &i2c_regs->hw_i2c_ctrl1_clr);
  92. writel(I2C_QUEUECTRL_PIO_QUEUE_MODE, &i2c_regs->hw_i2c_queuectrl_set);
  93. mxs_i2c_set_bus_speed(adap, speed);
  94. }
  95. static void mxs_i2c_setup_read(struct i2c_adapter *adap, uint8_t chip, int len)
  96. {
  97. struct mxs_i2c_regs *i2c_regs = mxs_i2c_get_base(adap);
  98. writel(I2C_QUEUECMD_RETAIN_CLOCK | I2C_QUEUECMD_PRE_SEND_START |
  99. I2C_QUEUECMD_MASTER_MODE | I2C_QUEUECMD_DIRECTION |
  100. (1 << I2C_QUEUECMD_XFER_COUNT_OFFSET),
  101. &i2c_regs->hw_i2c_queuecmd);
  102. writel((chip << 1) | 1, &i2c_regs->hw_i2c_data);
  103. writel(I2C_QUEUECMD_SEND_NAK_ON_LAST | I2C_QUEUECMD_MASTER_MODE |
  104. (len << I2C_QUEUECMD_XFER_COUNT_OFFSET) |
  105. I2C_QUEUECMD_POST_SEND_STOP, &i2c_regs->hw_i2c_queuecmd);
  106. writel(I2C_QUEUECTRL_QUEUE_RUN, &i2c_regs->hw_i2c_queuectrl_set);
  107. }
  108. static int mxs_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  109. int alen, uchar *buf, int blen, int stop)
  110. {
  111. struct mxs_i2c_regs *i2c_regs = mxs_i2c_get_base(adap);
  112. uint32_t data, tmp;
  113. int i, remain, off;
  114. int timeout = MXS_I2C_MAX_TIMEOUT;
  115. if ((alen > 4) || (alen == 0)) {
  116. debug("MXS I2C: Invalid address length\n");
  117. return -EINVAL;
  118. }
  119. if (stop)
  120. stop = I2C_QUEUECMD_POST_SEND_STOP;
  121. writel(I2C_QUEUECMD_PRE_SEND_START |
  122. I2C_QUEUECMD_MASTER_MODE | I2C_QUEUECMD_DIRECTION |
  123. ((blen + alen + 1) << I2C_QUEUECMD_XFER_COUNT_OFFSET) | stop,
  124. &i2c_regs->hw_i2c_queuecmd);
  125. data = (chip << 1) << 24;
  126. for (i = 0; i < alen; i++) {
  127. data >>= 8;
  128. data |= ((char *)&addr)[alen - i - 1] << 24;
  129. if ((i & 3) == 2)
  130. writel(data, &i2c_regs->hw_i2c_data);
  131. }
  132. off = i;
  133. for (; i < off + blen; i++) {
  134. data >>= 8;
  135. data |= buf[i - off] << 24;
  136. if ((i & 3) == 2)
  137. writel(data, &i2c_regs->hw_i2c_data);
  138. }
  139. remain = 24 - ((i & 3) * 8);
  140. if (remain)
  141. writel(data >> remain, &i2c_regs->hw_i2c_data);
  142. writel(I2C_QUEUECTRL_QUEUE_RUN, &i2c_regs->hw_i2c_queuectrl_set);
  143. while (--timeout) {
  144. tmp = readl(&i2c_regs->hw_i2c_queuestat);
  145. if (tmp & I2C_QUEUESTAT_WR_QUEUE_EMPTY)
  146. break;
  147. }
  148. if (!timeout) {
  149. debug("MXS I2C: Failed transmitting data!\n");
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. static int mxs_i2c_wait_for_ack(struct i2c_adapter *adap)
  155. {
  156. struct mxs_i2c_regs *i2c_regs = mxs_i2c_get_base(adap);
  157. uint32_t tmp;
  158. int timeout = MXS_I2C_MAX_TIMEOUT;
  159. for (;;) {
  160. tmp = readl(&i2c_regs->hw_i2c_ctrl1);
  161. if (tmp & I2C_CTRL1_NO_SLAVE_ACK_IRQ) {
  162. debug("MXS I2C: No slave ACK\n");
  163. goto err;
  164. }
  165. if (tmp & (
  166. I2C_CTRL1_EARLY_TERM_IRQ | I2C_CTRL1_MASTER_LOSS_IRQ |
  167. I2C_CTRL1_SLAVE_STOP_IRQ | I2C_CTRL1_SLAVE_IRQ)) {
  168. debug("MXS I2C: Error (CTRL1 = %08x)\n", tmp);
  169. goto err;
  170. }
  171. if (tmp & I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ)
  172. break;
  173. if (!timeout--) {
  174. debug("MXS I2C: Operation timed out\n");
  175. goto err;
  176. }
  177. udelay(1);
  178. }
  179. return 0;
  180. err:
  181. mxs_i2c_reset(adap);
  182. return 1;
  183. }
  184. static int mxs_i2c_if_read(struct i2c_adapter *adap, uint8_t chip,
  185. uint addr, int alen, uint8_t *buffer,
  186. int len)
  187. {
  188. struct mxs_i2c_regs *i2c_regs = mxs_i2c_get_base(adap);
  189. uint32_t tmp = 0;
  190. int timeout = MXS_I2C_MAX_TIMEOUT;
  191. int ret;
  192. int i;
  193. ret = mxs_i2c_write(adap, chip, addr, alen, NULL, 0, 0);
  194. if (ret) {
  195. debug("MXS I2C: Failed writing address\n");
  196. return ret;
  197. }
  198. ret = mxs_i2c_wait_for_ack(adap);
  199. if (ret) {
  200. debug("MXS I2C: Failed writing address\n");
  201. return ret;
  202. }
  203. mxs_i2c_setup_read(adap, chip, len);
  204. ret = mxs_i2c_wait_for_ack(adap);
  205. if (ret) {
  206. debug("MXS I2C: Failed reading address\n");
  207. return ret;
  208. }
  209. for (i = 0; i < len; i++) {
  210. if (!(i & 3)) {
  211. while (--timeout) {
  212. tmp = readl(&i2c_regs->hw_i2c_queuestat);
  213. if (!(tmp & I2C_QUEUESTAT_RD_QUEUE_EMPTY))
  214. break;
  215. }
  216. if (!timeout) {
  217. debug("MXS I2C: Failed receiving data!\n");
  218. return -ETIMEDOUT;
  219. }
  220. tmp = readl(&i2c_regs->hw_i2c_queuedata);
  221. }
  222. buffer[i] = tmp & 0xff;
  223. tmp >>= 8;
  224. }
  225. return 0;
  226. }
  227. static int mxs_i2c_if_write(struct i2c_adapter *adap, uint8_t chip,
  228. uint addr, int alen, uint8_t *buffer,
  229. int len)
  230. {
  231. int ret;
  232. ret = mxs_i2c_write(adap, chip, addr, alen, buffer, len, 1);
  233. if (ret) {
  234. debug("MXS I2C: Failed writing address\n");
  235. return ret;
  236. }
  237. ret = mxs_i2c_wait_for_ack(adap);
  238. if (ret)
  239. debug("MXS I2C: Failed writing address\n");
  240. return ret;
  241. }
  242. static int mxs_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
  243. {
  244. int ret;
  245. ret = mxs_i2c_write(adap, chip, 0, 1, NULL, 0, 1);
  246. if (!ret)
  247. ret = mxs_i2c_wait_for_ack(adap);
  248. mxs_i2c_reset(adap);
  249. return ret;
  250. }
  251. static void mxs_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  252. {
  253. mxs_i2c_reset(adap);
  254. mxs_i2c_set_bus_speed(adap, speed);
  255. return;
  256. }
  257. U_BOOT_I2C_ADAP_COMPLETE(mxs0, mxs_i2c_init, mxs_i2c_probe,
  258. mxs_i2c_if_read, mxs_i2c_if_write,
  259. mxs_i2c_set_bus_speed,
  260. CONFIG_SYS_I2C_SPEED, 0, 0)
  261. U_BOOT_I2C_ADAP_COMPLETE(mxs1, mxs_i2c_init, mxs_i2c_probe,
  262. mxs_i2c_if_read, mxs_i2c_if_write,
  263. mxs_i2c_set_bus_speed,
  264. CONFIG_SYS_I2C_SPEED, 0, 1)