i2c.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #if (CFG_I2C_MODULE == 2)
  28. #define I2C_BASE MPC5XXX_I2C2
  29. #elif (CFG_I2C_MODULE == 1)
  30. #define I2C_BASE MPC5XXX_I2C1
  31. #else
  32. #error CFG_I2C_MODULE is not properly configured
  33. #endif
  34. #define I2C_TIMEOUT 100
  35. #define I2C_RETRIES 3
  36. struct mpc5xxx_i2c_tap {
  37. int scl2tap;
  38. int tap2tap;
  39. };
  40. static int mpc_reg_in (volatile u32 *reg);
  41. static void mpc_reg_out (volatile u32 *reg, int val, int mask);
  42. static int wait_for_bb (void);
  43. static int wait_for_pin (int *status);
  44. static int do_address (uchar chip, char rdwr_flag);
  45. static int send_bytes (uchar chip, char *buf, int len);
  46. static int receive_bytes (uchar chip, char *buf, int len);
  47. static int mpc_get_fdr (int);
  48. static int mpc_reg_in(volatile u32 *reg)
  49. {
  50. return *reg >> 24;
  51. __asm__ __volatile__ ("eieio");
  52. }
  53. static void mpc_reg_out(volatile u32 *reg, int val, int mask)
  54. {
  55. int tmp;
  56. if (!mask) {
  57. *reg = val << 24;
  58. } else {
  59. tmp = mpc_reg_in(reg);
  60. *reg = ((tmp & ~mask) | (val & mask)) << 24;
  61. }
  62. __asm__ __volatile__ ("eieio");
  63. return;
  64. }
  65. static int wait_for_bb(void)
  66. {
  67. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  68. int timeout = I2C_TIMEOUT;
  69. int status;
  70. status = mpc_reg_in(&regs->msr);
  71. while (timeout-- && (status & I2C_BB)) {
  72. #if 1
  73. volatile int temp;
  74. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  75. temp = mpc_reg_in(&regs->mdr);
  76. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  77. mpc_reg_out(&regs->mcr, 0, 0);
  78. mpc_reg_out(&regs->mcr, I2C_EN, 0);
  79. #endif
  80. udelay(1000);
  81. status = mpc_reg_in(&regs->msr);
  82. }
  83. return (status & I2C_BB);
  84. }
  85. static int wait_for_pin(int *status)
  86. {
  87. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  88. int timeout = I2C_TIMEOUT;
  89. *status = mpc_reg_in(&regs->msr);
  90. while (timeout-- && !(*status & I2C_IF)) {
  91. udelay(1000);
  92. *status = mpc_reg_in(&regs->msr);
  93. }
  94. if (!(*status & I2C_IF)) {
  95. return -1;
  96. }
  97. mpc_reg_out(&regs->msr, 0, I2C_IF);
  98. return 0;
  99. }
  100. static int do_address(uchar chip, char rdwr_flag)
  101. {
  102. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  103. int status;
  104. chip <<= 1;
  105. if (rdwr_flag) {
  106. chip |= 1;
  107. }
  108. mpc_reg_out(&regs->mcr, I2C_TX, I2C_TX);
  109. mpc_reg_out(&regs->mdr, chip, 0);
  110. if (wait_for_pin(&status)) {
  111. return -2;
  112. }
  113. if (status & I2C_RXAK) {
  114. return -3;
  115. }
  116. return 0;
  117. }
  118. static int send_bytes(uchar chip, char *buf, int len)
  119. {
  120. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  121. int wrcount;
  122. int status;
  123. for (wrcount = 0; wrcount < len; ++wrcount) {
  124. mpc_reg_out(&regs->mdr, buf[wrcount], 0);
  125. if (wait_for_pin(&status)) {
  126. break;
  127. }
  128. if (status & I2C_RXAK) {
  129. break;
  130. }
  131. }
  132. return !(wrcount == len);
  133. }
  134. static int receive_bytes(uchar chip, char *buf, int len)
  135. {
  136. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  137. int dummy = 1;
  138. int rdcount = 0;
  139. int status;
  140. int i;
  141. mpc_reg_out(&regs->mcr, 0, I2C_TX);
  142. for (i = 0; i < len; ++i) {
  143. buf[rdcount] = mpc_reg_in(&regs->mdr);
  144. if (dummy) {
  145. dummy = 0;
  146. } else {
  147. rdcount++;
  148. }
  149. if (wait_for_pin(&status)) {
  150. return -4;
  151. }
  152. }
  153. mpc_reg_out(&regs->mcr, I2C_TXAK, I2C_TXAK);
  154. buf[rdcount++] = mpc_reg_in(&regs->mdr);
  155. if (wait_for_pin(&status)) {
  156. return -5;
  157. }
  158. mpc_reg_out(&regs->mcr, 0, I2C_TXAK);
  159. return 0;
  160. }
  161. /**************** I2C API ****************/
  162. void i2c_init(int speed, int saddr)
  163. {
  164. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  165. mpc_reg_out(&regs->mcr, 0, 0);
  166. mpc_reg_out(&regs->madr, saddr << 1, 0);
  167. /* Set clock
  168. */
  169. mpc_reg_out(&regs->mfdr, mpc_get_fdr(speed), 0);
  170. /* Enable module
  171. */
  172. mpc_reg_out(&regs->mcr, I2C_EN, I2C_INIT_MASK);
  173. mpc_reg_out(&regs->msr, 0, I2C_IF);
  174. return;
  175. }
  176. static int mpc_get_fdr(int speed)
  177. {
  178. DECLARE_GLOBAL_DATA_PTR;
  179. static int fdr = -1;
  180. static int best_speed = 0;
  181. if (fdr == -1) {
  182. ulong ipb, scl;
  183. ulong bestmatch = 0xffffffffUL;
  184. int best_i = 0, best_j = 0, i, j;
  185. int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
  186. struct mpc5xxx_i2c_tap scltap[] = {
  187. {4, 1},
  188. {4, 2},
  189. {6, 4},
  190. {6, 8},
  191. {14, 16},
  192. {30, 32},
  193. {62, 64},
  194. {126, 128}
  195. };
  196. ipb = gd->ipb_clk;
  197. for (i = 7; i >= 0; i--) {
  198. for (j = 7; j >= 0; j--) {
  199. scl = 2 * (scltap[j].scl2tap +
  200. (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
  201. if (ipb <= speed*scl) {
  202. if ((speed*scl - ipb) < bestmatch) {
  203. bestmatch = speed*scl - ipb;
  204. best_i = i;
  205. best_j = j;
  206. best_speed = ipb/scl;
  207. }
  208. }
  209. }
  210. }
  211. fdr = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
  212. printf("%d kHz, ", best_speed / 1000);
  213. }
  214. return fdr;
  215. }
  216. int i2c_probe(uchar chip)
  217. {
  218. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  219. int i;
  220. for (i = 0; i < I2C_RETRIES; i++) {
  221. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  222. if (! do_address(chip, 0)) {
  223. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  224. break;
  225. }
  226. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  227. udelay(500);
  228. }
  229. return (i == I2C_RETRIES);
  230. }
  231. int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
  232. {
  233. uchar xaddr[4];
  234. struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
  235. int ret = -1;
  236. xaddr[0] = (addr >> 24) & 0xFF;
  237. xaddr[1] = (addr >> 16) & 0xFF;
  238. xaddr[2] = (addr >> 8) & 0xFF;
  239. xaddr[3] = addr & 0xFF;
  240. if (wait_for_bb()) {
  241. printf("i2c_read: bus is busy\n");
  242. goto Done;
  243. }
  244. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  245. if (do_address(chip, 0)) {
  246. printf("i2c_read: failed to address chip\n");
  247. goto Done;
  248. }
  249. if (send_bytes(chip, &xaddr[4-alen], alen)) {
  250. printf("i2c_read: send_bytes failed\n");
  251. goto Done;
  252. }
  253. mpc_reg_out(&regs->mcr, I2C_RSTA, I2C_RSTA);
  254. if (do_address(chip, 1)) {
  255. printf("i2c_read: failed to address chip\n");
  256. goto Done;
  257. }
  258. if (receive_bytes(chip, buf, len)) {
  259. printf("i2c_read: receive_bytes failed\n");
  260. goto Done;
  261. }
  262. ret = 0;
  263. Done:
  264. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  265. return ret;
  266. }
  267. int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
  268. {
  269. uchar xaddr[4];
  270. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  271. int ret = -1;
  272. xaddr[0] = (addr >> 24) & 0xFF;
  273. xaddr[1] = (addr >> 16) & 0xFF;
  274. xaddr[2] = (addr >> 8) & 0xFF;
  275. xaddr[3] = addr & 0xFF;
  276. if (wait_for_bb()) {
  277. printf("i2c_write: bus is busy\n");
  278. goto Done;
  279. }
  280. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  281. if (do_address(chip, 0)) {
  282. printf("i2c_write: failed to address chip\n");
  283. goto Done;
  284. }
  285. if (send_bytes(chip, &xaddr[4-alen], alen)) {
  286. printf("i2c_write: send_bytes failed\n");
  287. goto Done;
  288. }
  289. if (send_bytes(chip, buf, len)) {
  290. printf("i2c_write: send_bytes failed\n");
  291. goto Done;
  292. }
  293. ret = 0;
  294. Done:
  295. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  296. return ret;
  297. }
  298. uchar i2c_reg_read(uchar chip, uchar reg)
  299. {
  300. char buf;
  301. i2c_read(chip, reg, 1, &buf, 1);
  302. return buf;
  303. }
  304. void i2c_reg_write(uchar chip, uchar reg, uchar val)
  305. {
  306. i2c_write(chip, reg, 1, &val, 1);
  307. return;
  308. }
  309. #endif /* CONFIG_HARD_I2C */