i2c.c 9.2 KB

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