fsl_i2c.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright 2006 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * Version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  16. * MA 02111-1307 USA
  17. */
  18. #include <common.h>
  19. #include <command.h>
  20. #ifdef CONFIG_HARD_I2C
  21. #include <asm/io.h>
  22. #include <asm/fsl_i2c.h>
  23. #define I2C_TIMEOUT (CFG_HZ / 4)
  24. #define I2C ((struct fsl_i2c *)(CFG_IMMR + CFG_I2C_OFFSET))
  25. void
  26. i2c_init(int speed, int slaveadd)
  27. {
  28. /* stop I2C controller */
  29. writeb(0x0 , &I2C->cr);
  30. /* set clock */
  31. writeb(0x3f, &I2C->fdr);
  32. /* set default filter */
  33. writeb(0x10, &I2C->dfsrr);
  34. /* write slave address */
  35. writeb(slaveadd, &I2C->adr);
  36. /* clear status register */
  37. writeb(0x0, &I2C->sr);
  38. /* start I2C controller */
  39. writeb(I2C_CR_MEN, &I2C->cr);
  40. }
  41. static __inline__ int
  42. i2c_wait4bus(void)
  43. {
  44. ulong timeval = get_timer (0);
  45. while (readb(&I2C->sr) & I2C_SR_MBB) {
  46. if (get_timer(timeval) > I2C_TIMEOUT) {
  47. return -1;
  48. }
  49. }
  50. return 0;
  51. }
  52. static __inline__ int
  53. i2c_wait(int write)
  54. {
  55. u32 csr;
  56. ulong timeval = get_timer(0);
  57. do {
  58. csr = readb(&I2C->sr);
  59. if (!(csr & I2C_SR_MIF))
  60. continue;
  61. writeb(0x0, &I2C->sr);
  62. if (csr & I2C_SR_MAL) {
  63. debug("i2c_wait: MAL\n");
  64. return -1;
  65. }
  66. if (!(csr & I2C_SR_MCF)) {
  67. debug("i2c_wait: unfinished\n");
  68. return -1;
  69. }
  70. if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
  71. debug("i2c_wait: No RXACK\n");
  72. return -1;
  73. }
  74. return 0;
  75. } while (get_timer (timeval) < I2C_TIMEOUT);
  76. debug("i2c_wait: timed out\n");
  77. return -1;
  78. }
  79. static __inline__ int
  80. i2c_write_addr (u8 dev, u8 dir, int rsta)
  81. {
  82. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
  83. | (rsta ? I2C_CR_RSTA : 0),
  84. &I2C->cr);
  85. writeb((dev << 1) | dir, &I2C->dr);
  86. if (i2c_wait(I2C_WRITE) < 0)
  87. return 0;
  88. return 1;
  89. }
  90. static __inline__ int
  91. __i2c_write(u8 *data, int length)
  92. {
  93. int i;
  94. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  95. &I2C->cr);
  96. for (i = 0; i < length; i++) {
  97. writeb(data[i], &I2C->dr);
  98. if (i2c_wait(I2C_WRITE) < 0)
  99. break;
  100. }
  101. return i;
  102. }
  103. static __inline__ int
  104. __i2c_read(u8 *data, int length)
  105. {
  106. int i;
  107. writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
  108. &I2C->cr);
  109. /* dummy read */
  110. readb(&I2C->dr);
  111. for (i = 0; i < length; i++) {
  112. if (i2c_wait(I2C_READ) < 0)
  113. break;
  114. /* Generate ack on last next to last byte */
  115. if (i == length - 2)
  116. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
  117. &I2C->cr);
  118. /* Generate stop on last byte */
  119. if (i == length - 1)
  120. writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
  121. data[i] = readb(&I2C->dr);
  122. }
  123. return i;
  124. }
  125. int
  126. i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
  127. {
  128. int i = 0;
  129. u8 *a = (u8*)&addr;
  130. if (i2c_wait4bus () < 0)
  131. goto exit;
  132. if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
  133. goto exit;
  134. if (__i2c_write(&a[4 - alen], alen) != alen)
  135. goto exit;
  136. if (i2c_write_addr(dev, I2C_READ, 1) == 0)
  137. goto exit;
  138. i = __i2c_read(data, length);
  139. exit:
  140. writeb(I2C_CR_MEN, &I2C->cr);
  141. return !(i == length);
  142. }
  143. int
  144. i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
  145. {
  146. int i = 0;
  147. u8 *a = (u8*)&addr;
  148. if (i2c_wait4bus() < 0)
  149. goto exit;
  150. if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
  151. goto exit;
  152. if (__i2c_write(&a[4 - alen], alen) != alen)
  153. goto exit;
  154. i = __i2c_write(data, length);
  155. exit:
  156. writeb(I2C_CR_MEN, &I2C->cr);
  157. return !(i == length);
  158. }
  159. int
  160. i2c_probe(uchar chip)
  161. {
  162. int tmp;
  163. /*
  164. * Try to read the first location of the chip. The underlying
  165. * driver doesn't appear to support sending just the chip address
  166. * and looking for an <ACK> back.
  167. */
  168. udelay(10000);
  169. return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
  170. }
  171. uchar
  172. i2c_reg_read(uchar i2c_addr, uchar reg)
  173. {
  174. uchar buf[1];
  175. i2c_read(i2c_addr, reg, 1, buf, 1);
  176. return buf[0];
  177. }
  178. void
  179. i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
  180. {
  181. i2c_write(i2c_addr, reg, 1, &val, 1);
  182. }
  183. #endif /* CONFIG_HARD_I2C */