i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * (C) Copyright 2006 Freescale Semiconductor, Inc.
  3. *
  4. * (C) Copyright 2003,Motorola Inc.
  5. * Xianghua Xiao <x.xiao@motorola.com>
  6. * Adapted for Motorola 85xx chip.
  7. *
  8. * (C) Copyright 2003
  9. * Gleb Natapov <gnatapov@mrv.com>
  10. * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
  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. * Change log:
  33. *
  34. * 20050101: Eran Liberty (liberty@freescale.com)
  35. * Initial file creating (porting from 85XX & 8260)
  36. * 20060601: Dave Liu (daveliu@freescale.com)
  37. * Unified variable names for mpc83xx
  38. */
  39. #include <common.h>
  40. #include <command.h>
  41. #include <asm/io.h>
  42. #ifdef CONFIG_HARD_I2C
  43. #include <i2c.h>
  44. #include <asm/i2c.h>
  45. #if defined(CONFIG_MPC8349EMDS) || defined(CONFIG_TQM834X)
  46. i2c_t * mpc83xx_i2c = (i2c_t*)(CFG_IMMRBAR + CFG_I2C_OFFSET);
  47. #endif
  48. void
  49. i2c_init(int speed, int slaveadd)
  50. {
  51. /* stop I2C controller */
  52. writeb(0x00 , &I2C->cr);
  53. /* set clock */
  54. writeb(speed, &I2C->fdr);
  55. /* set default filter */
  56. writeb(0x10,&I2C->dfsrr);
  57. /* write slave address */
  58. writeb(slaveadd, &I2C->adr);
  59. /* clear status register */
  60. writeb(0x00, &I2C->sr);
  61. /* start I2C controller */
  62. writeb(I2C_CR_MEN, &I2C->cr);
  63. }
  64. static __inline__ int
  65. i2c_wait4bus (void)
  66. {
  67. ulong timeval = get_timer (0);
  68. while (readb(&I2C->sr) & I2C_SR_MBB) {
  69. if (get_timer (timeval) > I2C_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(&I2C->sr);
  82. if (!(csr & I2C_SR_MIF))
  83. continue;
  84. writeb(0x0, &I2C->sr);
  85. if (csr & I2C_SR_MAL) {
  86. debug("i2c_wait: MAL\n");
  87. return -1;
  88. }
  89. if (!(csr & I2C_SR_MCF)) {
  90. debug("i2c_wait: unfinished\n");
  91. return -1;
  92. }
  93. if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
  94. debug("i2c_wait: No RXACK\n");
  95. return -1;
  96. }
  97. return 0;
  98. } while (get_timer (timeval) < I2C_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. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
  106. (rsta?I2C_CR_RSTA:0),
  107. &I2C->cr);
  108. writeb((dev << 1) | dir, &I2C->dr);
  109. if (i2c_wait (I2C_WRITE) < 0)
  110. return 0;
  111. return 1;
  112. }
  113. static __inline__ int
  114. __i2c_write (u8 *data, int length)
  115. {
  116. int i;
  117. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  118. &I2C->cr);
  119. for (i=0; i < length; i++) {
  120. writeb(data[i], &I2C->dr);
  121. if (i2c_wait (I2C_WRITE) < 0)
  122. break;
  123. }
  124. return i;
  125. }
  126. static __inline__ int
  127. __i2c_read (u8 *data, int length)
  128. {
  129. int i;
  130. writeb(I2C_CR_MEN | I2C_CR_MSTA |
  131. ((length == 1) ? I2C_CR_TXAK : 0),
  132. &I2C->cr);
  133. /* dummy read */
  134. readb(&I2C->dr);
  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. writeb(I2C_CR_MEN | I2C_CR_MSTA |
  141. I2C_CR_TXAK,
  142. &I2C->cr);
  143. /* Generate stop on last byte */
  144. if (i == length - 1)
  145. writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
  146. data[i] = readb(&I2C->dr);
  147. }
  148. return i;
  149. }
  150. int
  151. i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
  152. {
  153. int i = 0;
  154. u8 *a = (u8*)&addr;
  155. if (i2c_wait4bus () < 0)
  156. goto exit;
  157. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  158. goto exit;
  159. if (__i2c_write (&a[4 - alen], alen) != alen)
  160. goto exit;
  161. if (i2c_write_addr (dev, I2C_READ, 1) == 0)
  162. goto exit;
  163. i = __i2c_read (data, length);
  164. exit:
  165. writeb(I2C_CR_MEN, &I2C->cr);
  166. return !(i == length);
  167. }
  168. int
  169. i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
  170. {
  171. int i = 0;
  172. u8 *a = (u8*)&addr;
  173. if (i2c_wait4bus () < 0)
  174. goto exit;
  175. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  176. goto exit;
  177. if (__i2c_write (&a[4 - alen], alen) != alen)
  178. goto exit;
  179. i = __i2c_write (data, length);
  180. exit:
  181. writeb(I2C_CR_MEN, &I2C->cr);
  182. return !(i == length);
  183. }
  184. int i2c_probe (uchar chip)
  185. {
  186. int tmp;
  187. /*
  188. * Try to read the first location of the chip. The underlying
  189. * driver doesn't appear to support sending just the chip address
  190. * and looking for an <ACK> back.
  191. */
  192. udelay(10000);
  193. return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
  194. }
  195. uchar i2c_reg_read (uchar i2c_addr, uchar reg)
  196. {
  197. uchar buf[1];
  198. i2c_read (i2c_addr, reg, 1, buf, 1);
  199. return (buf[0]);
  200. }
  201. void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
  202. {
  203. i2c_write (i2c_addr, reg, 1, &val, 1);
  204. }
  205. #endif /* CONFIG_HARD_I2C */