i2c.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. DECLARE_GLOBAL_DATA_PTR;
  25. #ifdef CONFIG_HARD_I2C
  26. #include <mpc5xxx.h>
  27. #include <i2c.h>
  28. #if !defined(CONFIG_I2C_MULTI_BUS)
  29. #if (CONFIG_SYS_I2C_MODULE == 2)
  30. #define I2C_BASE MPC5XXX_I2C2
  31. #elif (CONFIG_SYS_I2C_MODULE == 1)
  32. #define I2C_BASE MPC5XXX_I2C1
  33. #else
  34. #error CONFIG_SYS_I2C_MODULE is not properly configured
  35. #endif
  36. #else
  37. static unsigned int i2c_bus_num __attribute__ ((section (".data"))) =
  38. CONFIG_SYS_SPD_BUS_NUM;
  39. static unsigned int i2c_bus_speed[2] = {CONFIG_SYS_I2C_SPEED,
  40. CONFIG_SYS_I2C_SPEED};
  41. static const unsigned long i2c_dev[2] = {
  42. MPC5XXX_I2C1,
  43. MPC5XXX_I2C2,
  44. };
  45. #define I2C_BASE ((struct mpc5xxx_i2c *)i2c_dev[i2c_bus_num])
  46. #endif
  47. #define I2C_TIMEOUT 6667
  48. #define I2C_RETRIES 3
  49. struct mpc5xxx_i2c_tap {
  50. int scl2tap;
  51. int tap2tap;
  52. };
  53. static int mpc_reg_in (volatile u32 *reg);
  54. static void mpc_reg_out (volatile u32 *reg, int val, int mask);
  55. static int wait_for_bb (void);
  56. static int wait_for_pin (int *status);
  57. static int do_address (uchar chip, char rdwr_flag);
  58. static int send_bytes (uchar chip, char *buf, int len);
  59. static int receive_bytes (uchar chip, char *buf, int len);
  60. static int mpc_get_fdr (int);
  61. static int mpc_reg_in(volatile u32 *reg)
  62. {
  63. int ret = *reg >> 24;
  64. __asm__ __volatile__ ("eieio");
  65. return ret;
  66. }
  67. static void mpc_reg_out(volatile u32 *reg, int val, int mask)
  68. {
  69. int tmp;
  70. if (!mask) {
  71. *reg = val << 24;
  72. } else {
  73. tmp = mpc_reg_in(reg);
  74. *reg = ((tmp & ~mask) | (val & mask)) << 24;
  75. }
  76. __asm__ __volatile__ ("eieio");
  77. return;
  78. }
  79. static int wait_for_bb(void)
  80. {
  81. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  82. int timeout = I2C_TIMEOUT;
  83. int status;
  84. status = mpc_reg_in(&regs->msr);
  85. while (timeout-- && (status & I2C_BB)) {
  86. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  87. (void)mpc_reg_in(&regs->mdr);
  88. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  89. mpc_reg_out(&regs->mcr, 0, 0);
  90. mpc_reg_out(&regs->mcr, I2C_EN, 0);
  91. udelay(15);
  92. status = mpc_reg_in(&regs->msr);
  93. }
  94. return (status & I2C_BB);
  95. }
  96. static int wait_for_pin(int *status)
  97. {
  98. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  99. int timeout = I2C_TIMEOUT;
  100. *status = mpc_reg_in(&regs->msr);
  101. while (timeout-- && !(*status & I2C_IF)) {
  102. udelay(15);
  103. *status = mpc_reg_in(&regs->msr);
  104. }
  105. if (!(*status & I2C_IF)) {
  106. return -1;
  107. }
  108. mpc_reg_out(&regs->msr, 0, I2C_IF);
  109. return 0;
  110. }
  111. static int do_address(uchar chip, char rdwr_flag)
  112. {
  113. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  114. int status;
  115. chip <<= 1;
  116. if (rdwr_flag) {
  117. chip |= 1;
  118. }
  119. mpc_reg_out(&regs->mcr, I2C_TX, I2C_TX);
  120. mpc_reg_out(&regs->mdr, chip, 0);
  121. if (wait_for_pin(&status)) {
  122. return -2;
  123. }
  124. if (status & I2C_RXAK) {
  125. return -3;
  126. }
  127. return 0;
  128. }
  129. static int send_bytes(uchar chip, char *buf, int len)
  130. {
  131. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  132. int wrcount;
  133. int status;
  134. for (wrcount = 0; wrcount < len; ++wrcount) {
  135. mpc_reg_out(&regs->mdr, buf[wrcount], 0);
  136. if (wait_for_pin(&status)) {
  137. break;
  138. }
  139. if (status & I2C_RXAK) {
  140. break;
  141. }
  142. }
  143. return !(wrcount == len);
  144. }
  145. static int receive_bytes(uchar chip, char *buf, int len)
  146. {
  147. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  148. int dummy = 1;
  149. int rdcount = 0;
  150. int status;
  151. int i;
  152. mpc_reg_out(&regs->mcr, 0, I2C_TX);
  153. for (i = 0; i < len; ++i) {
  154. buf[rdcount] = mpc_reg_in(&regs->mdr);
  155. if (dummy) {
  156. dummy = 0;
  157. } else {
  158. rdcount++;
  159. }
  160. if (wait_for_pin(&status)) {
  161. return -4;
  162. }
  163. }
  164. mpc_reg_out(&regs->mcr, I2C_TXAK, I2C_TXAK);
  165. buf[rdcount++] = mpc_reg_in(&regs->mdr);
  166. if (wait_for_pin(&status)) {
  167. return -5;
  168. }
  169. mpc_reg_out(&regs->mcr, 0, I2C_TXAK);
  170. return 0;
  171. }
  172. #if defined(CONFIG_SYS_I2C_INIT_MPC5XXX)
  173. #define FDR510(x) (u8) (((x & 0x20) >> 3) | (x & 0x3))
  174. #define FDR432(x) (u8) ((x & 0x1C) >> 2)
  175. /*
  176. * Reset any i2c devices that may have been interrupted during a system reset.
  177. * Normally this would be accomplished by clocking the line until SCL and SDA
  178. * are released and then sending a start condtiion (From an Atmel datasheet).
  179. * There is no direct access to the i2c pins so instead create start commands
  180. * through the i2c interface. Send a start command then delay for the SDA Hold
  181. * time, repeat this by disabling/enabling the bus a total of 9 times.
  182. */
  183. static void send_reset(void)
  184. {
  185. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  186. int i;
  187. u32 delay;
  188. u8 fdr;
  189. int SDA_Tap[] = { 3, 3, 4, 4, 1, 1, 2, 2};
  190. struct mpc5xxx_i2c_tap scltap[] = {
  191. {4, 1},
  192. {4, 2},
  193. {6, 4},
  194. {6, 8},
  195. {14, 16},
  196. {30, 32},
  197. {62, 64},
  198. {126, 128}
  199. };
  200. fdr = (u8)mpc_reg_in(&regs->mfdr);
  201. delay = scltap[FDR432(fdr)].scl2tap + ((SDA_Tap[FDR510(fdr)] - 1) * \
  202. scltap[FDR432(fdr)].tap2tap) + 3;
  203. for (i = 0; i < 9; i++) {
  204. mpc_reg_out(&regs->mcr, I2C_EN|I2C_STA|I2C_TX, I2C_INIT_MASK);
  205. udelay(delay);
  206. mpc_reg_out(&regs->mcr, 0, I2C_INIT_MASK);
  207. udelay(delay);
  208. }
  209. mpc_reg_out(&regs->mcr, I2C_EN, I2C_INIT_MASK);
  210. }
  211. #endif /* CONFIG_SYS_I2c_INIT_MPC5XXX */
  212. /**************** I2C API ****************/
  213. void i2c_init(int speed, int saddr)
  214. {
  215. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  216. mpc_reg_out(&regs->mcr, 0, 0);
  217. mpc_reg_out(&regs->madr, saddr << 1, 0);
  218. /* Set clock
  219. */
  220. mpc_reg_out(&regs->mfdr, mpc_get_fdr(speed), 0);
  221. /* Enable module
  222. */
  223. mpc_reg_out(&regs->mcr, I2C_EN, I2C_INIT_MASK);
  224. mpc_reg_out(&regs->msr, 0, I2C_IF);
  225. #if defined(CONFIG_SYS_I2C_INIT_MPC5XXX)
  226. send_reset();
  227. #endif
  228. return;
  229. }
  230. static int mpc_get_fdr(int speed)
  231. {
  232. static int fdr = -1;
  233. if (fdr == -1) {
  234. ulong best_speed = 0;
  235. ulong divider;
  236. ulong ipb, scl;
  237. ulong bestmatch = 0xffffffffUL;
  238. int best_i = 0, best_j = 0, i, j;
  239. int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
  240. struct mpc5xxx_i2c_tap scltap[] = {
  241. {4, 1},
  242. {4, 2},
  243. {6, 4},
  244. {6, 8},
  245. {14, 16},
  246. {30, 32},
  247. {62, 64},
  248. {126, 128}
  249. };
  250. ipb = gd->ipb_clk;
  251. for (i = 7; i >= 0; i--) {
  252. for (j = 7; j >= 0; j--) {
  253. scl = 2 * (scltap[j].scl2tap +
  254. (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
  255. if (ipb <= speed*scl) {
  256. if ((speed*scl - ipb) < bestmatch) {
  257. bestmatch = speed*scl - ipb;
  258. best_i = i;
  259. best_j = j;
  260. best_speed = ipb/scl;
  261. }
  262. }
  263. }
  264. }
  265. divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
  266. if (gd->flags & GD_FLG_RELOC) {
  267. fdr = divider;
  268. } else {
  269. printf("%ld kHz, ", best_speed / 1000);
  270. return divider;
  271. }
  272. }
  273. return fdr;
  274. }
  275. int i2c_probe(uchar chip)
  276. {
  277. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  278. int i;
  279. for (i = 0; i < I2C_RETRIES; i++) {
  280. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  281. if (! do_address(chip, 0)) {
  282. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  283. udelay(500);
  284. break;
  285. }
  286. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  287. udelay(500);
  288. }
  289. return (i == I2C_RETRIES);
  290. }
  291. int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
  292. {
  293. char xaddr[4];
  294. struct mpc5xxx_i2c * regs = (struct mpc5xxx_i2c *)I2C_BASE;
  295. int ret = -1;
  296. xaddr[0] = (addr >> 24) & 0xFF;
  297. xaddr[1] = (addr >> 16) & 0xFF;
  298. xaddr[2] = (addr >> 8) & 0xFF;
  299. xaddr[3] = addr & 0xFF;
  300. if (wait_for_bb()) {
  301. printf("i2c_read: bus is busy\n");
  302. goto Done;
  303. }
  304. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  305. if (do_address(chip, 0)) {
  306. printf("i2c_read: failed to address chip\n");
  307. goto Done;
  308. }
  309. if (send_bytes(chip, &xaddr[4-alen], alen)) {
  310. printf("i2c_read: send_bytes failed\n");
  311. goto Done;
  312. }
  313. mpc_reg_out(&regs->mcr, I2C_RSTA, I2C_RSTA);
  314. if (do_address(chip, 1)) {
  315. printf("i2c_read: failed to address chip\n");
  316. goto Done;
  317. }
  318. if (receive_bytes(chip, (char *)buf, len)) {
  319. printf("i2c_read: receive_bytes failed\n");
  320. goto Done;
  321. }
  322. ret = 0;
  323. Done:
  324. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  325. return ret;
  326. }
  327. int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
  328. {
  329. char xaddr[4];
  330. struct mpc5xxx_i2c *regs = (struct mpc5xxx_i2c *)I2C_BASE;
  331. int ret = -1;
  332. xaddr[0] = (addr >> 24) & 0xFF;
  333. xaddr[1] = (addr >> 16) & 0xFF;
  334. xaddr[2] = (addr >> 8) & 0xFF;
  335. xaddr[3] = addr & 0xFF;
  336. if (wait_for_bb()) {
  337. printf("i2c_write: bus is busy\n");
  338. goto Done;
  339. }
  340. mpc_reg_out(&regs->mcr, I2C_STA, I2C_STA);
  341. if (do_address(chip, 0)) {
  342. printf("i2c_write: failed to address chip\n");
  343. goto Done;
  344. }
  345. if (send_bytes(chip, &xaddr[4-alen], alen)) {
  346. printf("i2c_write: send_bytes failed\n");
  347. goto Done;
  348. }
  349. if (send_bytes(chip, (char *)buf, len)) {
  350. printf("i2c_write: send_bytes failed\n");
  351. goto Done;
  352. }
  353. ret = 0;
  354. Done:
  355. mpc_reg_out(&regs->mcr, 0, I2C_STA);
  356. return ret;
  357. }
  358. #if defined(CONFIG_I2C_MULTI_BUS)
  359. int i2c_set_bus_num(unsigned int bus)
  360. {
  361. if (bus > 1)
  362. return -1;
  363. i2c_bus_num = bus;
  364. i2c_init(i2c_bus_speed[bus], CONFIG_SYS_I2C_SLAVE);
  365. return 0;
  366. }
  367. int i2c_set_bus_speed(unsigned int speed)
  368. {
  369. i2c_init(speed, CONFIG_SYS_I2C_SLAVE);
  370. return 0;
  371. }
  372. unsigned int i2c_get_bus_num(void)
  373. {
  374. return i2c_bus_num;
  375. }
  376. unsigned int i2c_get_bus_speed(void)
  377. {
  378. return i2c_bus_speed[i2c_bus_num];
  379. }
  380. #endif
  381. #endif /* CONFIG_HARD_I2C */