i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * Change log:
  31. *
  32. * 20050101: Eran Liberty (liberty@freescale.com)
  33. * Initial file creating (porting from 85XX & 8260)
  34. */
  35. #include <common.h>
  36. #include <command.h>
  37. #include <asm/io.h>
  38. #ifdef CONFIG_HARD_I2C
  39. #include <i2c.h>
  40. #include <asm/i2c.h>
  41. #if defined(CONFIG_MPC8349ADS) || defined(CONFIG_TQM834X)
  42. i2c_t * mpc8349_i2c = (i2c_t*)(CFG_IMMRBAR + CFG_I2C_OFFSET);
  43. #endif
  44. void
  45. i2c_init(int speed, int slaveadd)
  46. {
  47. /* stop I2C controller */
  48. writeb(0x00 , &I2C->cr);
  49. /* set clock */
  50. writeb(0x3f, &I2C->fdr);
  51. /* set default filter */
  52. writeb(0x10,&I2C->dfsrr);
  53. /* write slave address */
  54. writeb(slaveadd, &I2C->adr);
  55. /* clear status register */
  56. writeb(0x00, &I2C->sr);
  57. /* start I2C controller */
  58. writeb(I2C_CR_MEN, &I2C->cr);
  59. }
  60. static __inline__ int
  61. i2c_wait4bus (void)
  62. {
  63. ulong timeval = get_timer (0);
  64. while (readb(&I2C->sr) & I2C_SR_MBB) {
  65. if (get_timer (timeval) > I2C_TIMEOUT) {
  66. return -1;
  67. }
  68. }
  69. return 0;
  70. }
  71. static __inline__ int
  72. i2c_wait (int write)
  73. {
  74. u32 csr;
  75. ulong timeval = get_timer(0);
  76. do {
  77. csr = readb(&I2C->sr);
  78. if (!(csr & I2C_SR_MIF))
  79. continue;
  80. writeb(0x0, &I2C->sr);
  81. if (csr & I2C_SR_MAL) {
  82. debug("i2c_wait: MAL\n");
  83. return -1;
  84. }
  85. if (!(csr & I2C_SR_MCF)) {
  86. debug("i2c_wait: unfinished\n");
  87. return -1;
  88. }
  89. if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
  90. debug("i2c_wait: No RXACK\n");
  91. return -1;
  92. }
  93. return 0;
  94. } while (get_timer (timeval) < I2C_TIMEOUT);
  95. debug("i2c_wait: timed out\n");
  96. return -1;
  97. }
  98. static __inline__ int
  99. i2c_write_addr (u8 dev, u8 dir, int rsta)
  100. {
  101. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
  102. (rsta?I2C_CR_RSTA:0),
  103. &I2C->cr);
  104. writeb((dev << 1) | dir, &I2C->dr);
  105. if (i2c_wait (I2C_WRITE) < 0)
  106. return 0;
  107. return 1;
  108. }
  109. static __inline__ int
  110. __i2c_write (u8 *data, int length)
  111. {
  112. int i;
  113. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  114. &I2C->cr);
  115. for (i=0; i < length; i++) {
  116. writeb(data[i], &I2C->dr);
  117. if (i2c_wait (I2C_WRITE) < 0)
  118. break;
  119. }
  120. return i;
  121. }
  122. static __inline__ int
  123. __i2c_read (u8 *data, int length)
  124. {
  125. int i;
  126. writeb(I2C_CR_MEN | I2C_CR_MSTA |
  127. ((length == 1) ? I2C_CR_TXAK : 0),
  128. &I2C->cr);
  129. /* dummy read */
  130. readb(&I2C->dr);
  131. for (i=0; i < length; i++) {
  132. if (i2c_wait (I2C_READ) < 0)
  133. break;
  134. /* Generate ack on last next to last byte */
  135. if (i == length - 2)
  136. writeb(I2C_CR_MEN | I2C_CR_MSTA |
  137. I2C_CR_TXAK,
  138. &I2C->cr);
  139. /* Generate stop on last byte */
  140. if (i == length - 1)
  141. writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
  142. data[i] = readb(&I2C->dr);
  143. }
  144. return i;
  145. }
  146. int
  147. i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
  148. {
  149. int i = 0;
  150. u8 *a = (u8*)&addr;
  151. if (i2c_wait4bus () < 0)
  152. goto exit;
  153. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  154. goto exit;
  155. if (__i2c_write (&a[4 - alen], alen) != alen)
  156. goto exit;
  157. if (i2c_write_addr (dev, I2C_READ, 1) == 0)
  158. goto exit;
  159. i = __i2c_read (data, length);
  160. exit:
  161. writeb(I2C_CR_MEN, &I2C->cr);
  162. return !(i == length);
  163. }
  164. int
  165. i2c_write (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. i = __i2c_write (data, length);
  176. exit:
  177. writeb(I2C_CR_MEN, &I2C->cr);
  178. return !(i == length);
  179. }
  180. int i2c_probe (uchar chip)
  181. {
  182. int tmp;
  183. /*
  184. * Try to read the first location of the chip. The underlying
  185. * driver doesn't appear to support sending just the chip address
  186. * and looking for an <ACK> back.
  187. */
  188. udelay(10000);
  189. return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
  190. }
  191. uchar i2c_reg_read (uchar i2c_addr, uchar reg)
  192. {
  193. uchar buf[1];
  194. i2c_read (i2c_addr, reg, 1, buf, 1);
  195. return (buf[0]);
  196. }
  197. void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
  198. {
  199. i2c_write (i2c_addr, reg, 1, &val, 1);
  200. }
  201. #endif /* CONFIG_HARD_I2C */