i2c.c 5.7 KB

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