fsl_i2c.c 6.1 KB

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