mxs_i2c.c 7.0 KB

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