i2c.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * (C) Copyright 2003
  3. * Gleb Natapov <gnatapov@mrv.com>
  4. * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
  5. *
  6. * Hardware I2C driver for MPC107 PCI bridge.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #undef I2CDBG
  28. #ifdef CONFIG_HARD_I2C
  29. #include <i2c.h>
  30. #define TIMEOUT (CONFIG_SYS_HZ/4)
  31. #define I2C_Addr ((unsigned *)(CONFIG_SYS_EUMB_ADDR + 0x3000))
  32. #define I2CADR &I2C_Addr[0]
  33. #define I2CFDR &I2C_Addr[1]
  34. #define I2CCCR &I2C_Addr[2]
  35. #define I2CCSR &I2C_Addr[3]
  36. #define I2CCDR &I2C_Addr[4]
  37. #define MPC107_CCR_MEN 0x80
  38. #define MPC107_CCR_MIEN 0x40
  39. #define MPC107_CCR_MSTA 0x20
  40. #define MPC107_CCR_MTX 0x10
  41. #define MPC107_CCR_TXAK 0x08
  42. #define MPC107_CCR_RSTA 0x04
  43. #define MPC107_CSR_MCF 0x80
  44. #define MPC107_CSR_MAAS 0x40
  45. #define MPC107_CSR_MBB 0x20
  46. #define MPC107_CSR_MAL 0x10
  47. #define MPC107_CSR_SRW 0x04
  48. #define MPC107_CSR_MIF 0x02
  49. #define MPC107_CSR_RXAK 0x01
  50. #define I2C_READ 1
  51. #define I2C_WRITE 0
  52. /* taken from linux include/asm-ppc/io.h */
  53. inline unsigned in_le32 (volatile unsigned *addr)
  54. {
  55. unsigned ret;
  56. __asm__ __volatile__ ("lwbrx %0,0,%1;\n"
  57. "twi 0,%0,0;\n"
  58. "isync":"=r" (ret): "r" (addr), "m" (*addr));
  59. return ret;
  60. }
  61. inline void out_le32 (volatile unsigned *addr, int val)
  62. {
  63. __asm__ __volatile__ ("stwbrx %1,0,%2; eieio":"=m" (*addr):"r" (val),
  64. "r" (addr));
  65. }
  66. #define writel(val, addr) out_le32(addr, val)
  67. #define readl(addr) in_le32(addr)
  68. void i2c_init (int speed, int slaveadd)
  69. {
  70. /* stop I2C controller */
  71. writel (0x0, I2CCCR);
  72. /* set clock */
  73. writel (0x1020, I2CFDR);
  74. /* write slave address */
  75. writel (slaveadd, I2CADR);
  76. /* clear status register */
  77. writel (0x0, I2CCSR);
  78. /* start I2C controller */
  79. writel (MPC107_CCR_MEN, I2CCCR);
  80. return;
  81. }
  82. static __inline__ int i2c_wait4bus (void)
  83. {
  84. ulong timeval = get_timer (0);
  85. while (readl (I2CCSR) & MPC107_CSR_MBB)
  86. if (get_timer (timeval) > TIMEOUT)
  87. return -1;
  88. return 0;
  89. }
  90. static __inline__ int i2c_wait (int write)
  91. {
  92. u32 csr;
  93. ulong timeval = get_timer (0);
  94. do {
  95. csr = readl (I2CCSR);
  96. if (!(csr & MPC107_CSR_MIF))
  97. continue;
  98. writel (0x0, I2CCSR);
  99. if (csr & MPC107_CSR_MAL) {
  100. #ifdef I2CDBG
  101. printf ("i2c_wait: MAL\n");
  102. #endif
  103. return -1;
  104. }
  105. if (!(csr & MPC107_CSR_MCF)) {
  106. #ifdef I2CDBG
  107. printf ("i2c_wait: unfinished\n");
  108. #endif
  109. return -1;
  110. }
  111. if (write == I2C_WRITE && (csr & MPC107_CSR_RXAK)) {
  112. #ifdef I2CDBG
  113. printf ("i2c_wait: No RXACK\n");
  114. #endif
  115. return -1;
  116. }
  117. return 0;
  118. } while (get_timer (timeval) < TIMEOUT);
  119. #ifdef I2CDBG
  120. printf ("i2c_wait: timed out\n");
  121. #endif
  122. return -1;
  123. }
  124. static __inline__ int i2c_write_addr (u8 dev, u8 dir, int rsta)
  125. {
  126. writel (MPC107_CCR_MEN | MPC107_CCR_MSTA | MPC107_CCR_MTX |
  127. (rsta ? MPC107_CCR_RSTA : 0), I2CCCR);
  128. writel ((dev << 1) | dir, I2CCDR);
  129. if (i2c_wait (I2C_WRITE) < 0)
  130. return 0;
  131. return 1;
  132. }
  133. static __inline__ int __i2c_write (u8 * data, int length)
  134. {
  135. int i;
  136. writel (MPC107_CCR_MEN | MPC107_CCR_MSTA | MPC107_CCR_MTX, I2CCCR);
  137. for (i = 0; i < length; i++) {
  138. writel (data[i], I2CCDR);
  139. if (i2c_wait (I2C_WRITE) < 0)
  140. break;
  141. }
  142. return i;
  143. }
  144. static __inline__ int __i2c_read (u8 * data, int length)
  145. {
  146. int i;
  147. writel (MPC107_CCR_MEN | MPC107_CCR_MSTA |
  148. ((length == 1) ? MPC107_CCR_TXAK : 0), I2CCCR);
  149. /* dummy read */
  150. readl (I2CCDR);
  151. for (i = 0; i < length; i++) {
  152. if (i2c_wait (I2C_READ) < 0)
  153. break;
  154. /* Generate ack on last next to last byte */
  155. if (i == length - 2)
  156. writel (MPC107_CCR_MEN | MPC107_CCR_MSTA |
  157. MPC107_CCR_TXAK, I2CCCR);
  158. /* Generate stop on last byte */
  159. if (i == length - 1)
  160. writel (MPC107_CCR_MEN | MPC107_CCR_TXAK, I2CCCR);
  161. data[i] = readl (I2CCDR);
  162. }
  163. return i;
  164. }
  165. int i2c_read (u8 dev, uint addr, int alen, u8 * data, int length)
  166. {
  167. int i = 0;
  168. u8 *a = (u8 *) & addr;
  169. if (i2c_wait4bus () < 0)
  170. goto exit;
  171. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  172. goto exit;
  173. if (__i2c_write (&a[4 - alen], alen) != alen)
  174. goto exit;
  175. if (i2c_write_addr (dev, I2C_READ, 1) == 0)
  176. goto exit;
  177. i = __i2c_read (data, length);
  178. exit:
  179. writel (MPC107_CCR_MEN, I2CCCR);
  180. return !(i == length);
  181. }
  182. int i2c_write (u8 dev, uint addr, int alen, u8 * data, int length)
  183. {
  184. int i = 0;
  185. u8 *a = (u8 *) & addr;
  186. if (i2c_wait4bus () < 0)
  187. goto exit;
  188. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  189. goto exit;
  190. if (__i2c_write (&a[4 - alen], alen) != alen)
  191. goto exit;
  192. i = __i2c_write (data, length);
  193. exit:
  194. writel (MPC107_CCR_MEN, I2CCCR);
  195. return !(i == length);
  196. }
  197. int i2c_probe (uchar chip)
  198. {
  199. int tmp;
  200. /*
  201. * Try to read the first location of the chip. The underlying
  202. * driver doesn't appear to support sending just the chip address
  203. * and looking for an <ACK> back.
  204. */
  205. udelay (10000);
  206. return i2c_read (chip, 0, 1, (uchar *) &tmp, 1);
  207. }
  208. #endif /* CONFIG_HARD_I2C */