i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #undef I2CDBG
  12. #ifdef CONFIG_HARD_I2C
  13. #include <i2c.h>
  14. #define TIMEOUT (CONFIG_SYS_HZ/4)
  15. #define I2C_Addr ((unsigned *)(CONFIG_SYS_EUMB_ADDR + 0x3000))
  16. #define I2CADR &I2C_Addr[0]
  17. #define I2CFDR &I2C_Addr[1]
  18. #define I2CCCR &I2C_Addr[2]
  19. #define I2CCSR &I2C_Addr[3]
  20. #define I2CCDR &I2C_Addr[4]
  21. #define MPC107_CCR_MEN 0x80
  22. #define MPC107_CCR_MIEN 0x40
  23. #define MPC107_CCR_MSTA 0x20
  24. #define MPC107_CCR_MTX 0x10
  25. #define MPC107_CCR_TXAK 0x08
  26. #define MPC107_CCR_RSTA 0x04
  27. #define MPC107_CSR_MCF 0x80
  28. #define MPC107_CSR_MAAS 0x40
  29. #define MPC107_CSR_MBB 0x20
  30. #define MPC107_CSR_MAL 0x10
  31. #define MPC107_CSR_SRW 0x04
  32. #define MPC107_CSR_MIF 0x02
  33. #define MPC107_CSR_RXAK 0x01
  34. #define I2C_READ 1
  35. #define I2C_WRITE 0
  36. /* taken from linux include/asm-ppc/io.h */
  37. inline unsigned in_le32 (volatile unsigned *addr)
  38. {
  39. unsigned ret;
  40. __asm__ __volatile__ ("lwbrx %0,0,%1;\n"
  41. "twi 0,%0,0;\n"
  42. "isync":"=r" (ret): "r" (addr), "m" (*addr));
  43. return ret;
  44. }
  45. inline void out_le32 (volatile unsigned *addr, int val)
  46. {
  47. __asm__ __volatile__ ("stwbrx %1,0,%2; eieio":"=m" (*addr):"r" (val),
  48. "r" (addr));
  49. }
  50. #define writel(val, addr) out_le32(addr, val)
  51. #define readl(addr) in_le32(addr)
  52. void i2c_init (int speed, int slaveadd)
  53. {
  54. /* stop I2C controller */
  55. writel (0x0, I2CCCR);
  56. /* set clock */
  57. writel (0x1020, I2CFDR);
  58. /* write slave address */
  59. writel (slaveadd, I2CADR);
  60. /* clear status register */
  61. writel (0x0, I2CCSR);
  62. /* start I2C controller */
  63. writel (MPC107_CCR_MEN, I2CCCR);
  64. return;
  65. }
  66. static __inline__ int i2c_wait4bus (void)
  67. {
  68. ulong timeval = get_timer (0);
  69. while (readl (I2CCSR) & MPC107_CSR_MBB)
  70. if (get_timer (timeval) > TIMEOUT)
  71. return -1;
  72. return 0;
  73. }
  74. static __inline__ int i2c_wait (int write)
  75. {
  76. u32 csr;
  77. ulong timeval = get_timer (0);
  78. do {
  79. csr = readl (I2CCSR);
  80. if (!(csr & MPC107_CSR_MIF))
  81. continue;
  82. writel (0x0, I2CCSR);
  83. if (csr & MPC107_CSR_MAL) {
  84. #ifdef I2CDBG
  85. printf ("i2c_wait: MAL\n");
  86. #endif
  87. return -1;
  88. }
  89. if (!(csr & MPC107_CSR_MCF)) {
  90. #ifdef I2CDBG
  91. printf ("i2c_wait: unfinished\n");
  92. #endif
  93. return -1;
  94. }
  95. if (write == I2C_WRITE && (csr & MPC107_CSR_RXAK)) {
  96. #ifdef I2CDBG
  97. printf ("i2c_wait: No RXACK\n");
  98. #endif
  99. return -1;
  100. }
  101. return 0;
  102. } while (get_timer (timeval) < TIMEOUT);
  103. #ifdef I2CDBG
  104. printf ("i2c_wait: timed out\n");
  105. #endif
  106. return -1;
  107. }
  108. static __inline__ int i2c_write_addr (u8 dev, u8 dir, int rsta)
  109. {
  110. writel (MPC107_CCR_MEN | MPC107_CCR_MSTA | MPC107_CCR_MTX |
  111. (rsta ? MPC107_CCR_RSTA : 0), I2CCCR);
  112. writel ((dev << 1) | dir, I2CCDR);
  113. if (i2c_wait (I2C_WRITE) < 0)
  114. return 0;
  115. return 1;
  116. }
  117. static __inline__ int __i2c_write (u8 * data, int length)
  118. {
  119. int i;
  120. writel (MPC107_CCR_MEN | MPC107_CCR_MSTA | MPC107_CCR_MTX, I2CCCR);
  121. for (i = 0; i < length; i++) {
  122. writel (data[i], I2CCDR);
  123. if (i2c_wait (I2C_WRITE) < 0)
  124. break;
  125. }
  126. return i;
  127. }
  128. static __inline__ int __i2c_read (u8 * data, int length)
  129. {
  130. int i;
  131. writel (MPC107_CCR_MEN | MPC107_CCR_MSTA |
  132. ((length == 1) ? MPC107_CCR_TXAK : 0), I2CCCR);
  133. /* dummy read */
  134. readl (I2CCDR);
  135. for (i = 0; i < length; i++) {
  136. if (i2c_wait (I2C_READ) < 0)
  137. break;
  138. /* Generate ack on last next to last byte */
  139. if (i == length - 2)
  140. writel (MPC107_CCR_MEN | MPC107_CCR_MSTA |
  141. MPC107_CCR_TXAK, I2CCCR);
  142. /* Generate stop on last byte */
  143. if (i == length - 1)
  144. writel (MPC107_CCR_MEN | MPC107_CCR_TXAK, I2CCCR);
  145. data[i] = readl (I2CCDR);
  146. }
  147. return i;
  148. }
  149. int i2c_read (u8 dev, uint addr, int alen, u8 * data, int length)
  150. {
  151. int i = 0;
  152. u8 *a = (u8 *) & addr;
  153. if (i2c_wait4bus () < 0)
  154. goto exit;
  155. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  156. goto exit;
  157. if (__i2c_write (&a[4 - alen], alen) != alen)
  158. goto exit;
  159. if (i2c_write_addr (dev, I2C_READ, 1) == 0)
  160. goto exit;
  161. i = __i2c_read (data, length);
  162. exit:
  163. writel (MPC107_CCR_MEN, I2CCCR);
  164. return !(i == length);
  165. }
  166. int 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. goto exit;
  172. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  173. goto exit;
  174. if (__i2c_write (&a[4 - alen], alen) != alen)
  175. goto exit;
  176. i = __i2c_write (data, length);
  177. exit:
  178. writel (MPC107_CCR_MEN, I2CCCR);
  179. return !(i == length);
  180. }
  181. int 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. #endif /* CONFIG_HARD_I2C */