mxs_i2c.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <asm/errno.h>
  31. #include <asm/io.h>
  32. #include <asm/arch/clock.h>
  33. #include <asm/arch/imx-regs.h>
  34. #include <asm/arch/sys_proto.h>
  35. #define MXS_I2C_MAX_TIMEOUT 1000000
  36. void mxs_i2c_reset(void)
  37. {
  38. struct mx28_i2c_regs *i2c_regs = (struct mx28_i2c_regs *)MXS_I2C0_BASE;
  39. int ret;
  40. ret = mx28_reset_block(&i2c_regs->hw_i2c_ctrl0_reg);
  41. if (ret) {
  42. debug("MXS I2C: Block reset timeout\n");
  43. return;
  44. }
  45. writel(I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | I2C_CTRL1_NO_SLAVE_ACK_IRQ |
  46. I2C_CTRL1_EARLY_TERM_IRQ | I2C_CTRL1_MASTER_LOSS_IRQ |
  47. I2C_CTRL1_SLAVE_STOP_IRQ | I2C_CTRL1_SLAVE_IRQ,
  48. &i2c_regs->hw_i2c_ctrl1_clr);
  49. writel(I2C_QUEUECTRL_PIO_QUEUE_MODE, &i2c_regs->hw_i2c_queuectrl_set);
  50. }
  51. void mxs_i2c_setup_read(uint8_t chip, int len)
  52. {
  53. struct mx28_i2c_regs *i2c_regs = (struct mx28_i2c_regs *)MXS_I2C0_BASE;
  54. writel(I2C_QUEUECMD_RETAIN_CLOCK | I2C_QUEUECMD_PRE_SEND_START |
  55. I2C_QUEUECMD_MASTER_MODE | I2C_QUEUECMD_DIRECTION |
  56. (1 << I2C_QUEUECMD_XFER_COUNT_OFFSET),
  57. &i2c_regs->hw_i2c_queuecmd);
  58. writel((chip << 1) | 1, &i2c_regs->hw_i2c_data);
  59. writel(I2C_QUEUECMD_SEND_NAK_ON_LAST | I2C_QUEUECMD_MASTER_MODE |
  60. (len << I2C_QUEUECMD_XFER_COUNT_OFFSET) |
  61. I2C_QUEUECMD_POST_SEND_STOP, &i2c_regs->hw_i2c_queuecmd);
  62. writel(I2C_QUEUECTRL_QUEUE_RUN, &i2c_regs->hw_i2c_queuectrl_set);
  63. }
  64. void mxs_i2c_write(uchar chip, uint addr, int alen,
  65. uchar *buf, int blen, int stop)
  66. {
  67. struct mx28_i2c_regs *i2c_regs = (struct mx28_i2c_regs *)MXS_I2C0_BASE;
  68. uint32_t data;
  69. int i, remain, off;
  70. if ((alen > 4) || (alen == 0)) {
  71. debug("MXS I2C: Invalid address length\n");
  72. return;
  73. }
  74. if (stop)
  75. stop = I2C_QUEUECMD_POST_SEND_STOP;
  76. writel(I2C_QUEUECMD_PRE_SEND_START |
  77. I2C_QUEUECMD_MASTER_MODE | I2C_QUEUECMD_DIRECTION |
  78. ((blen + alen + 1) << I2C_QUEUECMD_XFER_COUNT_OFFSET) | stop,
  79. &i2c_regs->hw_i2c_queuecmd);
  80. data = (chip << 1) << 24;
  81. for (i = 0; i < alen; i++) {
  82. data >>= 8;
  83. data |= ((char *)&addr)[alen - i - 1] << 24;
  84. if ((i & 3) == 2)
  85. writel(data, &i2c_regs->hw_i2c_data);
  86. }
  87. off = i;
  88. for (; i < off + blen; i++) {
  89. data >>= 8;
  90. data |= buf[i - off] << 24;
  91. if ((i & 3) == 2)
  92. writel(data, &i2c_regs->hw_i2c_data);
  93. }
  94. remain = 24 - ((i & 3) * 8);
  95. if (remain)
  96. writel(data >> remain, &i2c_regs->hw_i2c_data);
  97. writel(I2C_QUEUECTRL_QUEUE_RUN, &i2c_regs->hw_i2c_queuectrl_set);
  98. }
  99. int mxs_i2c_wait_for_ack(void)
  100. {
  101. struct mx28_i2c_regs *i2c_regs = (struct mx28_i2c_regs *)MXS_I2C0_BASE;
  102. uint32_t tmp;
  103. int timeout = MXS_I2C_MAX_TIMEOUT;
  104. for (;;) {
  105. tmp = readl(&i2c_regs->hw_i2c_ctrl1);
  106. if (tmp & I2C_CTRL1_NO_SLAVE_ACK_IRQ) {
  107. debug("MXS I2C: No slave ACK\n");
  108. goto err;
  109. }
  110. if (tmp & (
  111. I2C_CTRL1_EARLY_TERM_IRQ | I2C_CTRL1_MASTER_LOSS_IRQ |
  112. I2C_CTRL1_SLAVE_STOP_IRQ | I2C_CTRL1_SLAVE_IRQ)) {
  113. debug("MXS I2C: Error (CTRL1 = %08x)\n", tmp);
  114. goto err;
  115. }
  116. if (tmp & I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ)
  117. break;
  118. if (!timeout--) {
  119. debug("MXS I2C: Operation timed out\n");
  120. goto err;
  121. }
  122. udelay(1);
  123. }
  124. return 0;
  125. err:
  126. mxs_i2c_reset();
  127. return 1;
  128. }
  129. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  130. {
  131. struct mx28_i2c_regs *i2c_regs = (struct mx28_i2c_regs *)MXS_I2C0_BASE;
  132. uint32_t tmp = 0;
  133. int ret;
  134. int i;
  135. mxs_i2c_write(chip, addr, alen, NULL, 0, 0);
  136. ret = mxs_i2c_wait_for_ack();
  137. if (ret) {
  138. debug("MXS I2C: Failed writing address\n");
  139. return ret;
  140. }
  141. mxs_i2c_setup_read(chip, len);
  142. ret = mxs_i2c_wait_for_ack();
  143. if (ret) {
  144. debug("MXS I2C: Failed reading address\n");
  145. return ret;
  146. }
  147. for (i = 0; i < len; i++) {
  148. if (!(i & 3)) {
  149. while (readl(&i2c_regs->hw_i2c_queuestat) &
  150. I2C_QUEUESTAT_RD_QUEUE_EMPTY)
  151. ;
  152. tmp = readl(&i2c_regs->hw_i2c_queuedata);
  153. }
  154. buffer[i] = tmp & 0xff;
  155. tmp >>= 8;
  156. }
  157. return 0;
  158. }
  159. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  160. {
  161. int ret;
  162. mxs_i2c_write(chip, addr, alen, buffer, len, 1);
  163. ret = mxs_i2c_wait_for_ack();
  164. if (ret)
  165. debug("MXS I2C: Failed writing address\n");
  166. return ret;
  167. }
  168. int i2c_probe(uchar chip)
  169. {
  170. int ret;
  171. mxs_i2c_write(chip, 0, 1, NULL, 0, 1);
  172. ret = mxs_i2c_wait_for_ack();
  173. mxs_i2c_reset();
  174. return ret;
  175. }
  176. void i2c_init(int speed, int slaveadd)
  177. {
  178. struct mx28_i2c_regs *i2c_regs = (struct mx28_i2c_regs *)MXS_I2C0_BASE;
  179. mxs_i2c_reset();
  180. switch (speed) {
  181. case 100000:
  182. writel((0x0078 << I2C_TIMING0_HIGH_COUNT_OFFSET) |
  183. (0x0030 << I2C_TIMING0_RCV_COUNT_OFFSET),
  184. &i2c_regs->hw_i2c_timing0);
  185. writel((0x0080 << I2C_TIMING1_LOW_COUNT_OFFSET) |
  186. (0x0030 << I2C_TIMING1_XMIT_COUNT_OFFSET),
  187. &i2c_regs->hw_i2c_timing1);
  188. break;
  189. case 400000:
  190. writel((0x000f << I2C_TIMING0_HIGH_COUNT_OFFSET) |
  191. (0x0007 << I2C_TIMING0_RCV_COUNT_OFFSET),
  192. &i2c_regs->hw_i2c_timing0);
  193. writel((0x001f << I2C_TIMING1_LOW_COUNT_OFFSET) |
  194. (0x000f << I2C_TIMING1_XMIT_COUNT_OFFSET),
  195. &i2c_regs->hw_i2c_timing1);
  196. break;
  197. default:
  198. printf("MXS I2C: Invalid speed selected (%d Hz)\n", speed);
  199. return;
  200. }
  201. writel((0x0015 << I2C_TIMING2_BUS_FREE_OFFSET) |
  202. (0x000d << I2C_TIMING2_LEADIN_COUNT_OFFSET),
  203. &i2c_regs->hw_i2c_timing2);
  204. return;
  205. }