mxs_i2c.c 7.4 KB

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