fsl_i2c.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #ifdef CONFIG_FSL_I2C
  20. #ifdef CONFIG_HARD_I2C
  21. #include <command.h>
  22. #include <i2c.h> /* Functional interface */
  23. #include <asm/io.h>
  24. #include <asm/fsl_i2c.h> /* HW definitions */
  25. #define I2C_TIMEOUT (CFG_HZ / 4)
  26. /* Initialize the bus pointer to whatever one the SPD EEPROM is on.
  27. * Default is bus 0. This is necessary because the DDR initialization
  28. * runs from ROM, and we can't switch buses because we can't modify
  29. * the global variables.
  30. */
  31. #ifdef CFG_SPD_BUS_NUM
  32. static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = CFG_SPD_BUS_NUM;
  33. #else
  34. static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = 0;
  35. #endif
  36. static volatile struct fsl_i2c *i2c_dev[2] = {
  37. (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET),
  38. #ifdef CFG_I2C2_OFFSET
  39. (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET)
  40. #endif
  41. };
  42. void
  43. i2c_init(int speed, int slaveadd)
  44. {
  45. volatile struct fsl_i2c *dev;
  46. dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET);
  47. writeb(0, &dev->cr); /* stop I2C controller */
  48. writeb(0x3F, &dev->fdr); /* set bus speed */
  49. writeb(0x3F, &dev->dfsrr); /* set default filter */
  50. writeb(slaveadd << 1, &dev->adr); /* write slave address */
  51. writeb(0x0, &dev->sr); /* clear status register */
  52. writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */
  53. #ifdef CFG_I2C2_OFFSET
  54. dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET);
  55. writeb(0, &dev->cr); /* stop I2C controller */
  56. writeb(0x3F, &dev->fdr); /* set bus speed */
  57. writeb(0x3F, &dev->dfsrr); /* set default filter */
  58. writeb(slaveadd, &dev->adr); /* write slave address */
  59. writeb(0x0, &dev->sr); /* clear status register */
  60. writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */
  61. #endif /* CFG_I2C2_OFFSET */
  62. }
  63. static __inline__ int
  64. i2c_wait4bus(void)
  65. {
  66. ulong timeval = get_timer(0);
  67. while (readb(&i2c_dev[i2c_bus_num]->sr) & I2C_SR_MBB) {
  68. if (get_timer(timeval) > I2C_TIMEOUT) {
  69. return -1;
  70. }
  71. }
  72. return 0;
  73. }
  74. static __inline__ int
  75. i2c_wait(int write)
  76. {
  77. u32 csr;
  78. ulong timeval = get_timer(0);
  79. do {
  80. csr = readb(&i2c_dev[i2c_bus_num]->sr);
  81. if (!(csr & I2C_SR_MIF))
  82. continue;
  83. writeb(0x0, &i2c_dev[i2c_bus_num]->sr);
  84. if (csr & I2C_SR_MAL) {
  85. debug("i2c_wait: MAL\n");
  86. return -1;
  87. }
  88. if (!(csr & I2C_SR_MCF)) {
  89. debug("i2c_wait: unfinished\n");
  90. return -1;
  91. }
  92. if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
  93. debug("i2c_wait: No RXACK\n");
  94. return -1;
  95. }
  96. return 0;
  97. } while (get_timer (timeval) < I2C_TIMEOUT);
  98. debug("i2c_wait: timed out\n");
  99. return -1;
  100. }
  101. static __inline__ int
  102. i2c_write_addr (u8 dev, u8 dir, int rsta)
  103. {
  104. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
  105. | (rsta ? I2C_CR_RSTA : 0),
  106. &i2c_dev[i2c_bus_num]->cr);
  107. writeb((dev << 1) | dir, &i2c_dev[i2c_bus_num]->dr);
  108. if (i2c_wait(I2C_WRITE) < 0)
  109. return 0;
  110. return 1;
  111. }
  112. static __inline__ int
  113. __i2c_write(u8 *data, int length)
  114. {
  115. int i;
  116. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  117. &i2c_dev[i2c_bus_num]->cr);
  118. for (i = 0; i < length; i++) {
  119. writeb(data[i], &i2c_dev[i2c_bus_num]->dr);
  120. if (i2c_wait(I2C_WRITE) < 0)
  121. break;
  122. }
  123. return i;
  124. }
  125. static __inline__ int
  126. __i2c_read(u8 *data, int length)
  127. {
  128. int i;
  129. writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
  130. &i2c_dev[i2c_bus_num]->cr);
  131. /* dummy read */
  132. readb(&i2c_dev[i2c_bus_num]->dr);
  133. for (i = 0; i < length; i++) {
  134. if (i2c_wait(I2C_READ) < 0)
  135. break;
  136. /* Generate ack on last next to last byte */
  137. if (i == length - 2)
  138. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
  139. &i2c_dev[i2c_bus_num]->cr);
  140. /* Generate stop on last byte */
  141. if (i == length - 1)
  142. writeb(I2C_CR_MEN | I2C_CR_TXAK, &i2c_dev[i2c_bus_num]->cr);
  143. data[i] = readb(&i2c_dev[i2c_bus_num]->dr);
  144. }
  145. return i;
  146. }
  147. int
  148. i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
  149. {
  150. int i = 0;
  151. u8 *a = (u8*)&addr;
  152. if (i2c_wait4bus() >= 0
  153. && i2c_write_addr(dev, I2C_WRITE, 0) != 0
  154. && __i2c_write(&a[4 - alen], alen) == alen
  155. && i2c_write_addr(dev, I2C_READ, 1) != 0) {
  156. i = __i2c_read(data, length);
  157. }
  158. writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr);
  159. if (i == length)
  160. return 0;
  161. return -1;
  162. }
  163. int
  164. i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
  165. {
  166. int i = 0;
  167. u8 *a = (u8*)&addr;
  168. if (i2c_wait4bus() >= 0
  169. && i2c_write_addr(dev, I2C_WRITE, 0) != 0
  170. && __i2c_write(&a[4 - alen], alen) == alen) {
  171. i = __i2c_write(data, length);
  172. }
  173. writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr);
  174. if (i == length)
  175. return 0;
  176. return -1;
  177. }
  178. int
  179. i2c_probe(uchar chip)
  180. {
  181. int tmp;
  182. /*
  183. * Try to read the first location of the chip. The underlying
  184. * driver doesn't appear to support sending just the chip address
  185. * and looking for an <ACK> back.
  186. */
  187. udelay(10000);
  188. return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
  189. }
  190. uchar
  191. i2c_reg_read(uchar i2c_addr, uchar reg)
  192. {
  193. uchar buf[1];
  194. i2c_read(i2c_addr, reg, 1, buf, 1);
  195. return buf[0];
  196. }
  197. void
  198. i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
  199. {
  200. i2c_write(i2c_addr, reg, 1, &val, 1);
  201. }
  202. int i2c_set_bus_num(unsigned int bus)
  203. {
  204. #ifdef CFG_I2C2_OFFSET
  205. if (bus > 1) {
  206. #else
  207. if (bus > 0) {
  208. #endif
  209. return -1;
  210. }
  211. i2c_bus_num = bus;
  212. return 0;
  213. }
  214. int i2c_set_bus_speed(unsigned int speed)
  215. {
  216. return -1;
  217. }
  218. unsigned int i2c_get_bus_num(void)
  219. {
  220. return i2c_bus_num;
  221. }
  222. unsigned int i2c_get_bus_speed(void)
  223. {
  224. return 0;
  225. }
  226. #endif /* CONFIG_HARD_I2C */
  227. #endif /* CONFIG_FSL_I2C */