i2c.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #ifdef CONFIG_HARD_I2C
  25. #include <mpc5xxx.h>
  26. #include <i2c.h>
  27. #ifdef CFG_I2C_MODULE
  28. #define I2C_BASE MPC5XXX_I2C2
  29. #else
  30. #define I2C_BASE MPC5XXX_I2C1
  31. #endif
  32. #define I2C_TIMEOUT 100
  33. #define I2C_RETRIES 3
  34. static int mpc_reg_in (volatile u32 *reg);
  35. static void mpc_reg_out (volatile u32 *reg, int val, int mask);
  36. static int wait_for_bb (void);
  37. static int wait_for_pin (int *status);
  38. static int do_address (uchar chip, char rdwr_flag);
  39. static int send_bytes (uchar chip, char *buf, int len);
  40. static int receive_bytes (uchar chip, char *buf, int len);
  41. static int mpc_reg_in(volatile u32 *reg)
  42. {
  43. return *reg >> 24;
  44. __asm__ __volatile__ ("eieio");
  45. }
  46. static void mpc_reg_out(volatile u32 *reg, int val, int mask)
  47. {
  48. int tmp;
  49. if (!mask) {
  50. *reg = val << 24;
  51. } else {
  52. tmp = mpc_reg_in(reg);
  53. *reg = ((tmp & ~mask) | (val & mask)) << 24;
  54. }
  55. __asm__ __volatile__ ("eieio");
  56. return;
  57. }
  58. static int wait_for_bb(void)
  59. {
  60. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  61. int timeout = I2C_TIMEOUT;
  62. int status;
  63. status = mpc_reg_in(&regs->msr);
  64. while (timeout-- && (status & I2C_BB)) {
  65. #if 1
  66. volatile int temp;
  67. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  68. temp = mpc_reg_in(&regs->mdr);
  69. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  70. mpc_reg_out(&regs->mcr, 0, 0);
  71. mpc_reg_out(&regs->mcr, I2C_EN, 0);
  72. #endif
  73. udelay(1000);
  74. status = mpc_reg_in(&regs->msr);
  75. }
  76. return (status & I2C_BB);
  77. }
  78. static int wait_for_pin(int *status)
  79. {
  80. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  81. int timeout = I2C_TIMEOUT;
  82. *status = mpc_reg_in(&regs->msr);
  83. while (timeout-- && !(*status & I2C_IF)) {
  84. udelay(1000);
  85. *status = mpc_reg_in(&regs->msr);
  86. }
  87. if (!(*status & I2C_IF)) {
  88. return -1;
  89. }
  90. mpc_reg_out(&regs->msr, 0, I2C_IF);
  91. return 0;
  92. }
  93. static int do_address(uchar chip, char rdwr_flag)
  94. {
  95. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  96. int status;
  97. chip <<= 1;
  98. if (rdwr_flag) {
  99. chip |= 1;
  100. }
  101. mpc_reg_out(&regs->mcr, I2C_TX, I2C_TX);
  102. mpc_reg_out(&regs->mdr, chip, 0);
  103. if (wait_for_pin(&status)) {
  104. return -2;
  105. }
  106. if (status & I2C_RXAK) {
  107. return -3;
  108. }
  109. return 0;
  110. }
  111. static int send_bytes(uchar chip, char *buf, int len)
  112. {
  113. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  114. int wrcount;
  115. int status;
  116. for (wrcount = 0; wrcount < len; ++wrcount) {
  117. mpc_reg_out(&regs->mdr, buf[wrcount], 0);
  118. if (wait_for_pin(&status)) {
  119. break;
  120. }
  121. if (status & I2C_RXAK) {
  122. break;
  123. }
  124. }
  125. return !(wrcount == len);
  126. }
  127. static int receive_bytes(uchar chip, char *buf, int len)
  128. {
  129. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  130. int dummy = 1;
  131. int rdcount = 0;
  132. int status;
  133. int i;
  134. mpc_reg_out(&regs->mcr, 0, I2C_TX);
  135. for (i = 0; i < len; ++i) {
  136. buf[rdcount] = mpc_reg_in(&regs->mdr);
  137. if (dummy) {
  138. dummy = 0;
  139. } else {
  140. rdcount++;
  141. }
  142. if (wait_for_pin(&status)) {
  143. return -4;
  144. }
  145. }
  146. mpc_reg_out(&regs->mcr, I2C_TXAK, I2C_TXAK);
  147. buf[rdcount++] = mpc_reg_in(&regs->mdr);
  148. if (wait_for_pin(&status)) {
  149. return -5;
  150. }
  151. mpc_reg_out(&regs->mcr, 0, I2C_TXAK);
  152. return 0;
  153. }
  154. /**************** I2C API ****************/
  155. void i2c_init(int speed, int saddr)
  156. {
  157. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  158. mpc_reg_out(&regs->mcr, 0, 0);
  159. mpc_reg_out(&regs->madr, saddr << 1, 0);
  160. /* Set clock
  161. */
  162. mpc_reg_out(&regs->mfdr, speed, 0);
  163. /* Enable module
  164. */
  165. mpc_reg_out(&regs->mcr, I2C_EN, I2C_INIT_MASK);
  166. mpc_reg_out(&regs->msr, 0, I2C_IF);
  167. return;
  168. }
  169. int i2c_probe(uchar chip)
  170. {
  171. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  172. int i;
  173. for (i = 0; i < I2C_RETRIES; i++) {
  174. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  175. if (! do_address(chip, 0)) {
  176. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  177. break;
  178. }
  179. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  180. udelay(500);
  181. }
  182. return (i == I2C_RETRIES);
  183. }
  184. int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
  185. {
  186. uchar xaddr[4];
  187. struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
  188. int ret = -1;
  189. xaddr[0] = (addr >> 24) & 0xFF;
  190. xaddr[1] = (addr >> 16) & 0xFF;
  191. xaddr[2] = (addr >> 8) & 0xFF;
  192. xaddr[3] = addr & 0xFF;
  193. if (wait_for_bb()) {
  194. printf("i2c_read: bus is busy\n");
  195. goto Done;
  196. }
  197. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  198. if (do_address(chip, 0)) {
  199. printf("i2c_read: failed to address chip\n");
  200. goto Done;
  201. }
  202. if (send_bytes(chip, &xaddr[4-alen], alen)) {
  203. printf("i2c_read: send_bytes failed\n");
  204. goto Done;
  205. }
  206. mpc_reg_out(&regs->mcr, I2C_RSTA, I2C_RSTA);
  207. if (do_address(chip, 1)) {
  208. printf("i2c_read: failed to address chip\n");
  209. goto Done;
  210. }
  211. if (receive_bytes(chip, buf, len)) {
  212. printf("i2c_read: receive_bytes failed\n");
  213. goto Done;
  214. }
  215. ret = 0;
  216. Done:
  217. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  218. return ret;
  219. }
  220. int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
  221. {
  222. uchar xaddr[4];
  223. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  224. int ret = -1;
  225. xaddr[0] = (addr >> 24) & 0xFF;
  226. xaddr[1] = (addr >> 16) & 0xFF;
  227. xaddr[2] = (addr >> 8) & 0xFF;
  228. xaddr[3] = addr & 0xFF;
  229. if (wait_for_bb()) {
  230. printf("i2c_write: bus is busy\n");
  231. goto Done;
  232. }
  233. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  234. if (do_address(chip, 0)) {
  235. printf("i2c_write: failed to address chip\n");
  236. goto Done;
  237. }
  238. if (send_bytes(chip, &xaddr[4-alen], alen)) {
  239. printf("i2c_write: send_bytes failed\n");
  240. goto Done;
  241. }
  242. if (send_bytes(chip, buf, len)) {
  243. printf("i2c_write: send_bytes failed\n");
  244. goto Done;
  245. }
  246. ret = 0;
  247. Done:
  248. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  249. return ret;
  250. }
  251. uchar i2c_reg_read(uchar chip, uchar reg)
  252. {
  253. char buf;
  254. i2c_read(chip, reg, 1, &buf, 1);
  255. return buf;
  256. }
  257. void i2c_reg_write(uchar chip, uchar reg, uchar val)
  258. {
  259. i2c_write(chip, reg, 1, &val, 1);
  260. return;
  261. }
  262. #endif /* CONFIG_HARD_I2C */