i2c.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * Modified for MPC86xx by Jeff Brown (jeffrey@freescale.com)
  11. *
  12. * Hardware I2C driver for MPC107 PCI bridge.
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. #include <common.h>
  33. #include <command.h>
  34. #include <asm/io.h>
  35. #ifdef CONFIG_HARD_I2C
  36. #include <i2c.h>
  37. #define TIMEOUT (CFG_HZ/4)
  38. #define I2C_Addr ((u8 *)(CFG_CCSRBAR + 0x3100))
  39. #define I2CADR &I2C_Addr[0]
  40. #define I2CFDR &I2C_Addr[4]
  41. #define I2CCCR &I2C_Addr[8]
  42. #define I2CCSR &I2C_Addr[12]
  43. #define I2CCDR &I2C_Addr[16]
  44. #define I2CDFSRR &I2C_Addr[20]
  45. #define I2C_READ 1
  46. #define I2C_WRITE 0
  47. void
  48. i2c_init(int speed, int slaveadd)
  49. {
  50. /* stop I2C controller */
  51. writeb(0x0, I2CCCR);
  52. /* set clock */
  53. writeb(0x3f, I2CFDR);
  54. /* set default filter */
  55. writeb(0x10,I2CDFSRR);
  56. /* write slave address */
  57. writeb(slaveadd, I2CADR);
  58. /* clear status register */
  59. writeb(0x0, I2CCSR);
  60. /* start I2C controller */
  61. writeb(MPC86xx_I2CCR_MEN, I2CCCR);
  62. }
  63. static __inline__ int
  64. i2c_wait4bus (void)
  65. {
  66. ulong timeval = get_timer (0);
  67. // debug("I2C: Wait for bus\n");
  68. while (readb(I2CCSR) & MPC86xx_I2CSR_MBB) {
  69. if (get_timer (timeval) > TIMEOUT) {
  70. return -1;
  71. }
  72. }
  73. return 0;
  74. }
  75. static __inline__ int
  76. i2c_wait (int write)
  77. {
  78. u32 csr;
  79. ulong timeval = get_timer (0);
  80. do {
  81. csr = readb(I2CCSR);
  82. if (!(csr & MPC86xx_I2CSR_MIF))
  83. continue;
  84. writeb(0x0, I2CCSR);
  85. if (csr & MPC86xx_I2CSR_MAL) {
  86. debug("i2c_wait: MAL\n");
  87. return -1;
  88. }
  89. if (!(csr & MPC86xx_I2CSR_MCF)) {
  90. debug("i2c_wait: unfinished\n");
  91. return -1;
  92. }
  93. if (write == I2C_WRITE && (csr & MPC86xx_I2CSR_RXAK)) {
  94. debug("i2c_wait: No RXACK\n");
  95. return -1;
  96. }
  97. return 0;
  98. } while (get_timer (timeval) < TIMEOUT);
  99. debug("i2c_wait: timed out\n");
  100. return -1;
  101. }
  102. static __inline__ int
  103. i2c_write_addr (u8 dev, u8 dir, int rsta)
  104. {
  105. // debug("I2C: Write Addr\n");
  106. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX |
  107. (rsta?MPC86xx_I2CCR_RSTA:0),
  108. I2CCCR);
  109. writeb((dev << 1) | dir, I2CCDR);
  110. if (i2c_wait (I2C_WRITE) < 0)
  111. return 0;
  112. return 1;
  113. }
  114. static __inline__ int
  115. __i2c_write (u8 *data, int length)
  116. {
  117. int i;
  118. // debug("I2C: __i2c_write\n");
  119. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX,
  120. I2CCCR);
  121. for (i=0; i < length; i++) {
  122. writeb(data[i], I2CCDR);
  123. if (i2c_wait (I2C_WRITE) < 0)
  124. break;
  125. }
  126. return i;
  127. }
  128. static __inline__ int
  129. __i2c_read (u8 *data, int length)
  130. {
  131. int i;
  132. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA |
  133. ((length == 1) ? MPC86xx_I2CCR_TXAK : 0),
  134. I2CCCR);
  135. /* dummy read */
  136. readb(I2CCDR);
  137. // debug("length = %d\n", length);
  138. for (i=0; i < length; i++) {
  139. if (i2c_wait (I2C_READ) < 0)
  140. break;
  141. /* Generate ack on last next to last byte */
  142. if (i == length - 2)
  143. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA |
  144. MPC86xx_I2CCR_TXAK,
  145. I2CCCR);
  146. /* Generate stop on last byte */
  147. if (i == length - 1)
  148. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_TXAK, I2CCCR);
  149. // debug("I2CCR = 0x%08x\n", readb(I2CCCR));
  150. data[i] = readb(I2CCDR);
  151. // debug("data[i] = 0x%08x\n", data[i]);
  152. }
  153. // debug("Returning i = %d\n", i);
  154. return i;
  155. }
  156. int
  157. i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
  158. {
  159. int i = 0;
  160. u8 *a = (u8*)&addr;
  161. if (i2c_wait4bus () < 0)
  162. goto exit;
  163. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  164. goto exit;
  165. if (__i2c_write (&a[4 - alen], alen) != alen)
  166. goto exit;
  167. if (i2c_write_addr (dev, I2C_READ, 1) == 0)
  168. goto exit;
  169. i = __i2c_read (data, length);
  170. exit:
  171. writeb(MPC86xx_I2CCR_MEN, I2CCCR);
  172. return !(i == length);
  173. }
  174. int
  175. i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
  176. {
  177. int i = 0;
  178. u8 *a = (u8*)&addr;
  179. if (i2c_wait4bus () < 0)
  180. goto exit;
  181. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  182. goto exit;
  183. if (__i2c_write (&a[4 - alen], alen) != alen)
  184. goto exit;
  185. i = __i2c_write (data, length);
  186. exit:
  187. writeb(MPC86xx_I2CCR_MEN, I2CCCR);
  188. return !(i == length);
  189. }
  190. int i2c_probe (uchar chip)
  191. {
  192. int tmp;
  193. /*
  194. * Try to read the first location of the chip. The underlying
  195. * driver doesn't appear to support sending just the chip address
  196. * and looking for an <ACK> back.
  197. */
  198. udelay(10000);
  199. return i2c_read (chip, 0, 1, (char *)&tmp, 1);
  200. }
  201. uchar i2c_reg_read (uchar i2c_addr, uchar reg)
  202. {
  203. char buf[1];
  204. i2c_read (i2c_addr, reg, 1, buf, 1);
  205. return (buf[0]);
  206. }
  207. void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
  208. {
  209. i2c_write (i2c_addr, reg, 1, &val, 1);
  210. }
  211. #endif /* CONFIG_HARD_I2C */