omap24xx_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * Basic I2C functions
  3. *
  4. * Copyright (c) 2004 Texas Instruments
  5. *
  6. * This package is free software; you can redistribute it and/or
  7. * modify it under the terms of the license found in the file
  8. * named COPYING that should have accompanied this file.
  9. *
  10. * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  11. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  12. * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * Author: Jian Zhang jzhang@ti.com, Texas Instruments
  15. *
  16. * Copyright (c) 2003 Wolfgang Denk, wd@denx.de
  17. * Rewritten to fit into the current U-Boot framework
  18. *
  19. * Adapted for OMAP2420 I2C, r-woodruff2@ti.com
  20. *
  21. */
  22. #include <common.h>
  23. #include <asm/arch/i2c.h>
  24. #include <asm/io.h>
  25. #include "omap24xx_i2c.h"
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #define I2C_STAT_TIMEO (1 << 31)
  28. #define I2C_TIMEOUT 10
  29. static u32 wait_for_bb(void);
  30. static u32 wait_for_status_mask(u16 mask);
  31. static void flush_fifo(void);
  32. /*
  33. * For SPL boot some boards need i2c before SDRAM is initialised so force
  34. * variables to live in SRAM
  35. */
  36. static struct i2c __attribute__((section (".data"))) *i2c_base =
  37. (struct i2c *)I2C_DEFAULT_BASE;
  38. static unsigned int __attribute__((section (".data"))) bus_initialized[I2C_BUS_MAX] =
  39. { [0 ... (I2C_BUS_MAX-1)] = 0 };
  40. static unsigned int __attribute__((section (".data"))) current_bus = 0;
  41. void i2c_init(int speed, int slaveadd)
  42. {
  43. int psc, fsscll, fssclh;
  44. int hsscll = 0, hssclh = 0;
  45. u32 scll, sclh;
  46. /* Only handle standard, fast and high speeds */
  47. if ((speed != OMAP_I2C_STANDARD) &&
  48. (speed != OMAP_I2C_FAST_MODE) &&
  49. (speed != OMAP_I2C_HIGH_SPEED)) {
  50. printf("Error : I2C unsupported speed %d\n", speed);
  51. return;
  52. }
  53. psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
  54. psc -= 1;
  55. if (psc < I2C_PSC_MIN) {
  56. printf("Error : I2C unsupported prescalar %d\n", psc);
  57. return;
  58. }
  59. if (speed == OMAP_I2C_HIGH_SPEED) {
  60. /* High speed */
  61. /* For first phase of HS mode */
  62. fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK /
  63. (2 * OMAP_I2C_FAST_MODE);
  64. fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
  65. fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
  66. if (((fsscll < 0) || (fssclh < 0)) ||
  67. ((fsscll > 255) || (fssclh > 255))) {
  68. puts("Error : I2C initializing first phase clock\n");
  69. return;
  70. }
  71. /* For second phase of HS mode */
  72. hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
  73. hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
  74. hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
  75. if (((fsscll < 0) || (fssclh < 0)) ||
  76. ((fsscll > 255) || (fssclh > 255))) {
  77. puts("Error : I2C initializing second phase clock\n");
  78. return;
  79. }
  80. scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
  81. sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
  82. } else {
  83. /* Standard and fast speed */
  84. fsscll = fssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
  85. fsscll -= I2C_FASTSPEED_SCLL_TRIM;
  86. fssclh -= I2C_FASTSPEED_SCLH_TRIM;
  87. if (((fsscll < 0) || (fssclh < 0)) ||
  88. ((fsscll > 255) || (fssclh > 255))) {
  89. puts("Error : I2C initializing clock\n");
  90. return;
  91. }
  92. scll = (unsigned int)fsscll;
  93. sclh = (unsigned int)fssclh;
  94. }
  95. if (gd->flags & GD_FLG_RELOC)
  96. bus_initialized[current_bus] = 1;
  97. if (readw(&i2c_base->con) & I2C_CON_EN) {
  98. writew(0, &i2c_base->con);
  99. udelay(50000);
  100. }
  101. writew(psc, &i2c_base->psc);
  102. writew(scll, &i2c_base->scll);
  103. writew(sclh, &i2c_base->sclh);
  104. /* own address */
  105. writew(slaveadd, &i2c_base->oa);
  106. writew(I2C_CON_EN, &i2c_base->con);
  107. /* have to enable intrrupts or OMAP i2c module doesn't work */
  108. writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
  109. I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
  110. udelay(1000);
  111. flush_fifo();
  112. writew(0xFFFF, &i2c_base->stat);
  113. writew(0, &i2c_base->cnt);
  114. }
  115. static void flush_fifo(void)
  116. { u16 stat;
  117. /* note: if you try and read data when its not there or ready
  118. * you get a bus error
  119. */
  120. while (1) {
  121. stat = readw(&i2c_base->stat);
  122. if (stat == I2C_STAT_RRDY) {
  123. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  124. defined(CONFIG_OMAP44XX)
  125. readb(&i2c_base->data);
  126. #else
  127. readw(&i2c_base->data);
  128. #endif
  129. writew(I2C_STAT_RRDY, &i2c_base->stat);
  130. udelay(1000);
  131. } else
  132. break;
  133. }
  134. }
  135. int i2c_probe(uchar chip)
  136. {
  137. u32 status;
  138. int res = 1; /* default = fail */
  139. if (chip == readw(&i2c_base->oa))
  140. return res;
  141. /* wait until bus not busy */
  142. status = wait_for_bb();
  143. /* exit on BUS busy */
  144. if (status & I2C_STAT_TIMEO)
  145. return res;
  146. /* try to write one byte */
  147. writew(1, &i2c_base->cnt);
  148. /* set slave address */
  149. writew(chip, &i2c_base->sa);
  150. /* stop bit needed here */
  151. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT
  152. | I2C_CON_STP, &i2c_base->con);
  153. /* enough delay for the NACK bit set */
  154. udelay(9000);
  155. if (!(readw(&i2c_base->stat) & I2C_STAT_NACK)) {
  156. res = 0; /* success case */
  157. flush_fifo();
  158. writew(0xFFFF, &i2c_base->stat);
  159. } else {
  160. /* failure, clear sources*/
  161. writew(0xFFFF, &i2c_base->stat);
  162. /* finish up xfer */
  163. writew(readw(&i2c_base->con) | I2C_CON_STP, &i2c_base->con);
  164. status = wait_for_bb();
  165. /* exit on BUS busy */
  166. if (status & I2C_STAT_TIMEO)
  167. return res;
  168. }
  169. flush_fifo();
  170. /* don't allow any more data in... we don't want it. */
  171. writew(0, &i2c_base->cnt);
  172. writew(0xFFFF, &i2c_base->stat);
  173. return res;
  174. }
  175. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  176. {
  177. int i2c_error = 0, i;
  178. u32 status;
  179. if ((alen > 2) || (alen < 0))
  180. return 1;
  181. if (alen < 2) {
  182. if (addr + len > 256)
  183. return 1;
  184. } else if (addr + len > 0xFFFF) {
  185. return 1;
  186. }
  187. /* wait until bus not busy */
  188. status = wait_for_bb();
  189. /* exit on BUS busy */
  190. if (status & I2C_STAT_TIMEO)
  191. return 1;
  192. writew((alen & 0xFF), &i2c_base->cnt);
  193. /* set slave address */
  194. writew(chip, &i2c_base->sa);
  195. /* Clear the Tx & Rx FIFOs */
  196. writew((readw(&i2c_base->buf) | I2C_RXFIFO_CLEAR |
  197. I2C_TXFIFO_CLEAR), &i2c_base->buf);
  198. /* no stop bit needed here */
  199. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
  200. I2C_CON_STT, &i2c_base->con);
  201. /* wait for Transmit ready condition */
  202. status = wait_for_status_mask(I2C_STAT_XRDY | I2C_STAT_NACK);
  203. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO))
  204. i2c_error = 1;
  205. if (!i2c_error) {
  206. if (status & I2C_STAT_XRDY) {
  207. switch (alen) {
  208. case 2:
  209. /* Send address MSByte */
  210. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  211. writew(((addr >> 8) & 0xFF), &i2c_base->data);
  212. /* Clearing XRDY event */
  213. writew((status & I2C_STAT_XRDY),
  214. &i2c_base->stat);
  215. /* wait for Transmit ready condition */
  216. status = wait_for_status_mask(I2C_STAT_XRDY |
  217. I2C_STAT_NACK);
  218. if (status & (I2C_STAT_NACK |
  219. I2C_STAT_TIMEO)) {
  220. i2c_error = 1;
  221. break;
  222. }
  223. #endif
  224. case 1:
  225. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  226. /* Send address LSByte */
  227. writew((addr & 0xFF), &i2c_base->data);
  228. #else
  229. /* Send address Short word */
  230. writew((addr & 0xFFFF), &i2c_base->data);
  231. #endif
  232. /* Clearing XRDY event */
  233. writew((status & I2C_STAT_XRDY),
  234. &i2c_base->stat);
  235. /*wait for Transmit ready condition */
  236. status = wait_for_status_mask(I2C_STAT_ARDY |
  237. I2C_STAT_NACK);
  238. if (status & (I2C_STAT_NACK |
  239. I2C_STAT_TIMEO)) {
  240. i2c_error = 1;
  241. break;
  242. }
  243. }
  244. } else
  245. i2c_error = 1;
  246. }
  247. /* Wait for ARDY to set */
  248. status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK
  249. | I2C_STAT_AL);
  250. if (!i2c_error) {
  251. /* set slave address */
  252. writew(chip, &i2c_base->sa);
  253. writew((len & 0xFF), &i2c_base->cnt);
  254. /* Clear the Tx & Rx FIFOs */
  255. writew((readw(&i2c_base->buf) | I2C_RXFIFO_CLEAR |
  256. I2C_TXFIFO_CLEAR), &i2c_base->buf);
  257. /* need stop bit here */
  258. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
  259. &i2c_base->con);
  260. for (i = 0; i < len; i++) {
  261. /* wait for Receive condition */
  262. status = wait_for_status_mask(I2C_STAT_RRDY |
  263. I2C_STAT_NACK);
  264. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) {
  265. i2c_error = 1;
  266. break;
  267. }
  268. if (status & I2C_STAT_RRDY) {
  269. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  270. buffer[i] = readb(&i2c_base->data);
  271. #else
  272. *((u16 *)&buffer[i]) =
  273. readw(&i2c_base->data) & 0xFFFF;
  274. i++;
  275. #endif
  276. writew((status & I2C_STAT_RRDY),
  277. &i2c_base->stat);
  278. udelay(1000);
  279. } else {
  280. i2c_error = 1;
  281. }
  282. }
  283. }
  284. /* Wait for ARDY to set */
  285. status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK
  286. | I2C_STAT_AL);
  287. if (i2c_error) {
  288. writew(0, &i2c_base->con);
  289. return 1;
  290. }
  291. writew(I2C_CON_EN, &i2c_base->con);
  292. while (readw(&i2c_base->stat)
  293. || (readw(&i2c_base->con) & I2C_CON_MST)) {
  294. udelay(10000);
  295. writew(0xFFFF, &i2c_base->stat);
  296. }
  297. writew(I2C_CON_EN, &i2c_base->con);
  298. flush_fifo();
  299. writew(0xFFFF, &i2c_base->stat);
  300. writew(0, &i2c_base->cnt);
  301. return 0;
  302. }
  303. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  304. {
  305. int i, i2c_error = 0;
  306. u32 status;
  307. u16 writelen;
  308. if (alen > 2)
  309. return 1;
  310. if (alen < 2) {
  311. if (addr + len > 256)
  312. return 1;
  313. } else if (addr + len > 0xFFFF) {
  314. return 1;
  315. }
  316. /* wait until bus not busy */
  317. status = wait_for_bb();
  318. /* exiting on BUS busy */
  319. if (status & I2C_STAT_TIMEO)
  320. return 1;
  321. writelen = (len & 0xFFFF) + alen;
  322. /* two bytes */
  323. writew((writelen & 0xFFFF), &i2c_base->cnt);
  324. /* Clear the Tx & Rx FIFOs */
  325. writew((readw(&i2c_base->buf) | I2C_RXFIFO_CLEAR |
  326. I2C_TXFIFO_CLEAR), &i2c_base->buf);
  327. /* set slave address */
  328. writew(chip, &i2c_base->sa);
  329. /* stop bit needed here */
  330. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  331. I2C_CON_STP, &i2c_base->con);
  332. /* wait for Transmit ready condition */
  333. status = wait_for_status_mask(I2C_STAT_XRDY | I2C_STAT_NACK);
  334. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO))
  335. i2c_error = 1;
  336. if (!i2c_error) {
  337. if (status & I2C_STAT_XRDY) {
  338. switch (alen) {
  339. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  340. case 2:
  341. /* send out MSB byte */
  342. writeb(((addr >> 8) & 0xFF), &i2c_base->data);
  343. #else
  344. writeb((addr & 0xFFFF), &i2c_base->data);
  345. break;
  346. #endif
  347. /* Clearing XRDY event */
  348. writew((status & I2C_STAT_XRDY),
  349. &i2c_base->stat);
  350. /*waiting for Transmit ready * condition */
  351. status = wait_for_status_mask(I2C_STAT_XRDY |
  352. I2C_STAT_NACK);
  353. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) {
  354. i2c_error = 1;
  355. break;
  356. }
  357. case 1:
  358. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  359. /* send out MSB byte */
  360. writeb((addr & 0xFF), &i2c_base->data);
  361. #else
  362. writew(((buffer[0] << 8) | (addr & 0xFF)),
  363. &i2c_base->data);
  364. #endif
  365. }
  366. /* Clearing XRDY event */
  367. writew((status & I2C_STAT_XRDY), &i2c_base->stat);
  368. }
  369. /* waiting for Transmit ready condition */
  370. status = wait_for_status_mask(I2C_STAT_XRDY | I2C_STAT_NACK);
  371. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO))
  372. i2c_error = 1;
  373. if (!i2c_error) {
  374. for (i = ((alen > 1) ? 0 : 1); i < len; i++) {
  375. if (status & I2C_STAT_XRDY) {
  376. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  377. writeb((buffer[i] & 0xFF),
  378. &i2c_base->data);
  379. #else
  380. writew((((buffer[i] << 8) |
  381. buffer[i + 1]) & 0xFFFF),
  382. &i2c_base->data);
  383. i++;
  384. #endif
  385. } else
  386. i2c_error = 1;
  387. /* Clearing XRDY event */
  388. writew((status & I2C_STAT_XRDY),
  389. &i2c_base->stat);
  390. /* waiting for XRDY condition */
  391. status = wait_for_status_mask(
  392. I2C_STAT_XRDY |
  393. I2C_STAT_ARDY |
  394. I2C_STAT_NACK);
  395. if (status & (I2C_STAT_NACK |
  396. I2C_STAT_TIMEO)) {
  397. i2c_error = 1;
  398. break;
  399. }
  400. if (status & I2C_STAT_ARDY)
  401. break;
  402. }
  403. }
  404. }
  405. status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK |
  406. I2C_STAT_AL);
  407. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO))
  408. i2c_error = 1;
  409. if (i2c_error) {
  410. writew(0, &i2c_base->con);
  411. return 1;
  412. }
  413. if (!i2c_error) {
  414. int eout = 200;
  415. writew(I2C_CON_EN, &i2c_base->con);
  416. while ((status = readw(&i2c_base->stat)) ||
  417. (readw(&i2c_base->con) & I2C_CON_MST)) {
  418. udelay(1000);
  419. /* have to read to clear intrrupt */
  420. writew(0xFFFF, &i2c_base->stat);
  421. if (--eout == 0)
  422. /* better leave with error than hang */
  423. break;
  424. }
  425. }
  426. flush_fifo();
  427. writew(0xFFFF, &i2c_base->stat);
  428. writew(0, &i2c_base->cnt);
  429. return 0;
  430. }
  431. static u32 wait_for_bb(void)
  432. {
  433. int timeout = I2C_TIMEOUT;
  434. u32 stat;
  435. while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
  436. writew(stat, &i2c_base->stat);
  437. udelay(1000);
  438. }
  439. if (timeout <= 0) {
  440. printf("timed out in wait_for_bb: I2C_STAT=%x\n",
  441. readw(&i2c_base->stat));
  442. stat |= I2C_STAT_TIMEO;
  443. }
  444. writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
  445. return stat;
  446. }
  447. static u32 wait_for_status_mask(u16 mask)
  448. {
  449. u32 status;
  450. int timeout = I2C_TIMEOUT;
  451. do {
  452. udelay(1000);
  453. status = readw(&i2c_base->stat);
  454. } while (!(status & mask) && timeout--);
  455. if (timeout <= 0) {
  456. printf("timed out in wait_for_status_mask: I2C_STAT=%x\n",
  457. readw(&i2c_base->stat));
  458. writew(0xFFFF, &i2c_base->stat);
  459. status |= I2C_STAT_TIMEO;
  460. }
  461. return status;
  462. }
  463. int i2c_set_bus_num(unsigned int bus)
  464. {
  465. if ((bus < 0) || (bus >= I2C_BUS_MAX)) {
  466. printf("Bad bus: %d\n", bus);
  467. return -1;
  468. }
  469. #if I2C_BUS_MAX == 3
  470. if (bus == 2)
  471. i2c_base = (struct i2c *)I2C_BASE3;
  472. else
  473. #endif
  474. if (bus == 1)
  475. i2c_base = (struct i2c *)I2C_BASE2;
  476. else
  477. i2c_base = (struct i2c *)I2C_BASE1;
  478. current_bus = bus;
  479. if (!bus_initialized[current_bus])
  480. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  481. return 0;
  482. }
  483. int i2c_get_bus_num(void)
  484. {
  485. return (int) current_bus;
  486. }