mxs_i2c.c 7.0 KB

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