i2c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. while (readb(I2CCSR) & MPC86xx_I2CSR_MBB) {
  68. if (get_timer(timeval) > TIMEOUT) {
  69. return -1;
  70. }
  71. }
  72. return 0;
  73. }
  74. static __inline__ int
  75. i2c_wait(int write)
  76. {
  77. u32 csr;
  78. ulong timeval = get_timer (0);
  79. do {
  80. csr = readb(I2CCSR);
  81. if (!(csr & MPC86xx_I2CSR_MIF))
  82. continue;
  83. writeb(0x0, I2CCSR);
  84. if (csr & MPC86xx_I2CSR_MAL) {
  85. debug("i2c_wait: MAL\n");
  86. return -1;
  87. }
  88. if (!(csr & MPC86xx_I2CSR_MCF)) {
  89. debug("i2c_wait: unfinished\n");
  90. return -1;
  91. }
  92. if (write == I2C_WRITE && (csr & MPC86xx_I2CSR_RXAK)) {
  93. debug("i2c_wait: No RXACK\n");
  94. return -1;
  95. }
  96. return 0;
  97. } while (get_timer(timeval) < TIMEOUT);
  98. debug("i2c_wait: timed out\n");
  99. return -1;
  100. }
  101. static __inline__ int
  102. i2c_write_addr (u8 dev, u8 dir, int rsta)
  103. {
  104. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX
  105. | (rsta ? MPC86xx_I2CCR_RSTA : 0),
  106. I2CCCR);
  107. writeb((dev << 1) | dir, I2CCDR);
  108. if (i2c_wait(I2C_WRITE) < 0)
  109. return 0;
  110. return 1;
  111. }
  112. static __inline__ int
  113. __i2c_write (u8 *data, int length)
  114. {
  115. int i;
  116. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX,
  117. I2CCCR);
  118. for (i = 0; i < length; i++) {
  119. writeb(data[i], I2CCDR);
  120. if (i2c_wait(I2C_WRITE) < 0)
  121. break;
  122. }
  123. return i;
  124. }
  125. static __inline__ int
  126. __i2c_read (u8 *data, int length)
  127. {
  128. int i;
  129. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA
  130. | ((length == 1) ? MPC86xx_I2CCR_TXAK : 0),
  131. I2CCCR);
  132. /* dummy read */
  133. readb(I2CCDR);
  134. for (i = 0; i < length; i++) {
  135. if (i2c_wait(I2C_READ) < 0)
  136. break;
  137. /* Generate ack on last next to last byte */
  138. if (i == length - 2)
  139. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA
  140. | MPC86xx_I2CCR_TXAK,
  141. I2CCCR);
  142. /* Generate stop on last byte */
  143. if (i == length - 1)
  144. writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_TXAK, I2CCCR);
  145. data[i] = readb(I2CCDR);
  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. goto exit;
  156. if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
  157. goto exit;
  158. if (__i2c_write(&a[4 - alen], alen) != alen)
  159. goto exit;
  160. if (i2c_write_addr(dev, I2C_READ, 1) == 0)
  161. goto exit;
  162. i = __i2c_read(data, length);
  163. exit:
  164. writeb(MPC86xx_I2CCR_MEN, I2CCCR);
  165. return !(i == length);
  166. }
  167. int
  168. i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
  169. {
  170. int i = 0;
  171. u8 *a = (u8*)&addr;
  172. if (i2c_wait4bus() < 0)
  173. goto exit;
  174. if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
  175. goto exit;
  176. if (__i2c_write(&a[4 - alen], alen) != alen)
  177. goto exit;
  178. i = __i2c_write(data, length);
  179. exit:
  180. writeb(MPC86xx_I2CCR_MEN, I2CCCR);
  181. return !(i == length);
  182. }
  183. int i2c_probe (uchar chip)
  184. {
  185. int tmp;
  186. /*
  187. * Try to read the first location of the chip. The underlying
  188. * driver doesn't appear to support sending just the chip address
  189. * and looking for an <ACK> back.
  190. */
  191. udelay(10000);
  192. return i2c_read(chip, 0, 1, (char *)&tmp, 1);
  193. }
  194. uchar i2c_reg_read (uchar i2c_addr, uchar reg)
  195. {
  196. char buf[1];
  197. i2c_read(i2c_addr, reg, 1, buf, 1);
  198. return buf[0];
  199. }
  200. void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
  201. {
  202. i2c_write(i2c_addr, reg, 1, &val, 1);
  203. }
  204. #endif /* CONFIG_HARD_I2C */