fsl_i2c.c 6.2 KB

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