omap24xx_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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) || defined(CONFIG_AM33XX)
  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. defined(CONFIG_AM33XX)
  212. writew(((addr >> 8) & 0xFF), &i2c_base->data);
  213. /* Clearing XRDY event */
  214. writew((status & I2C_STAT_XRDY),
  215. &i2c_base->stat);
  216. /* wait for Transmit ready condition */
  217. status = wait_for_status_mask(I2C_STAT_XRDY |
  218. I2C_STAT_NACK);
  219. if (status & (I2C_STAT_NACK |
  220. I2C_STAT_TIMEO)) {
  221. i2c_error = 1;
  222. break;
  223. }
  224. #endif
  225. case 1:
  226. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  227. defined(CONFIG_AM33XX)
  228. /* Send address LSByte */
  229. writew((addr & 0xFF), &i2c_base->data);
  230. #else
  231. /* Send address Short word */
  232. writew((addr & 0xFFFF), &i2c_base->data);
  233. #endif
  234. /* Clearing XRDY event */
  235. writew((status & I2C_STAT_XRDY),
  236. &i2c_base->stat);
  237. /*wait for Transmit ready condition */
  238. status = wait_for_status_mask(I2C_STAT_ARDY |
  239. I2C_STAT_NACK);
  240. if (status & (I2C_STAT_NACK |
  241. I2C_STAT_TIMEO)) {
  242. i2c_error = 1;
  243. break;
  244. }
  245. }
  246. } else
  247. i2c_error = 1;
  248. }
  249. /* Wait for ARDY to set */
  250. status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK
  251. | I2C_STAT_AL);
  252. if (!i2c_error) {
  253. /* set slave address */
  254. writew(chip, &i2c_base->sa);
  255. writew((len & 0xFF), &i2c_base->cnt);
  256. /* Clear the Tx & Rx FIFOs */
  257. writew((readw(&i2c_base->buf) | I2C_RXFIFO_CLEAR |
  258. I2C_TXFIFO_CLEAR), &i2c_base->buf);
  259. /* need stop bit here */
  260. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP,
  261. &i2c_base->con);
  262. for (i = 0; i < len; i++) {
  263. /* wait for Receive condition */
  264. status = wait_for_status_mask(I2C_STAT_RRDY |
  265. I2C_STAT_NACK);
  266. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) {
  267. i2c_error = 1;
  268. break;
  269. }
  270. if (status & I2C_STAT_RRDY) {
  271. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  272. defined(CONFIG_AM33XX)
  273. buffer[i] = readb(&i2c_base->data);
  274. #else
  275. *((u16 *)&buffer[i]) =
  276. readw(&i2c_base->data) & 0xFFFF;
  277. i++;
  278. #endif
  279. writew((status & I2C_STAT_RRDY),
  280. &i2c_base->stat);
  281. udelay(1000);
  282. } else {
  283. i2c_error = 1;
  284. }
  285. }
  286. }
  287. /* Wait for ARDY to set */
  288. status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK
  289. | I2C_STAT_AL);
  290. if (i2c_error) {
  291. writew(0, &i2c_base->con);
  292. return 1;
  293. }
  294. writew(I2C_CON_EN, &i2c_base->con);
  295. while (readw(&i2c_base->stat)
  296. || (readw(&i2c_base->con) & I2C_CON_MST)) {
  297. udelay(10000);
  298. writew(0xFFFF, &i2c_base->stat);
  299. }
  300. writew(I2C_CON_EN, &i2c_base->con);
  301. flush_fifo();
  302. writew(0xFFFF, &i2c_base->stat);
  303. writew(0, &i2c_base->cnt);
  304. return 0;
  305. }
  306. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  307. {
  308. int i, i2c_error = 0;
  309. u32 status;
  310. u16 writelen;
  311. if (alen > 2)
  312. return 1;
  313. if (alen < 2) {
  314. if (addr + len > 256)
  315. return 1;
  316. } else if (addr + len > 0xFFFF) {
  317. return 1;
  318. }
  319. /* wait until bus not busy */
  320. status = wait_for_bb();
  321. /* exiting on BUS busy */
  322. if (status & I2C_STAT_TIMEO)
  323. return 1;
  324. writelen = (len & 0xFFFF) + alen;
  325. /* two bytes */
  326. writew((writelen & 0xFFFF), &i2c_base->cnt);
  327. /* Clear the Tx & Rx FIFOs */
  328. writew((readw(&i2c_base->buf) | I2C_RXFIFO_CLEAR |
  329. I2C_TXFIFO_CLEAR), &i2c_base->buf);
  330. /* set slave address */
  331. writew(chip, &i2c_base->sa);
  332. /* stop bit needed here */
  333. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  334. I2C_CON_STP, &i2c_base->con);
  335. /* wait for Transmit ready condition */
  336. status = wait_for_status_mask(I2C_STAT_XRDY | I2C_STAT_NACK);
  337. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO))
  338. i2c_error = 1;
  339. if (!i2c_error) {
  340. if (status & I2C_STAT_XRDY) {
  341. switch (alen) {
  342. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  343. defined(CONFIG_AM33XX)
  344. case 2:
  345. /* send out MSB byte */
  346. writeb(((addr >> 8) & 0xFF), &i2c_base->data);
  347. #else
  348. writeb((addr & 0xFFFF), &i2c_base->data);
  349. break;
  350. #endif
  351. /* Clearing XRDY event */
  352. writew((status & I2C_STAT_XRDY),
  353. &i2c_base->stat);
  354. /*waiting for Transmit ready * condition */
  355. status = wait_for_status_mask(I2C_STAT_XRDY |
  356. I2C_STAT_NACK);
  357. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO)) {
  358. i2c_error = 1;
  359. break;
  360. }
  361. case 1:
  362. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  363. defined(CONFIG_AM33XX)
  364. /* send out MSB byte */
  365. writeb((addr & 0xFF), &i2c_base->data);
  366. #else
  367. writew(((buffer[0] << 8) | (addr & 0xFF)),
  368. &i2c_base->data);
  369. #endif
  370. }
  371. /* Clearing XRDY event */
  372. writew((status & I2C_STAT_XRDY), &i2c_base->stat);
  373. }
  374. /* waiting for Transmit ready condition */
  375. status = wait_for_status_mask(I2C_STAT_XRDY | I2C_STAT_NACK);
  376. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO))
  377. i2c_error = 1;
  378. if (!i2c_error) {
  379. for (i = ((alen > 1) ? 0 : 1); i < len; i++) {
  380. if (status & I2C_STAT_XRDY) {
  381. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX) || \
  382. defined(CONFIG_AM33XX)
  383. writeb((buffer[i] & 0xFF),
  384. &i2c_base->data);
  385. #else
  386. writew((((buffer[i] << 8) |
  387. buffer[i + 1]) & 0xFFFF),
  388. &i2c_base->data);
  389. i++;
  390. #endif
  391. } else
  392. i2c_error = 1;
  393. /* Clearing XRDY event */
  394. writew((status & I2C_STAT_XRDY),
  395. &i2c_base->stat);
  396. /* waiting for XRDY condition */
  397. status = wait_for_status_mask(
  398. I2C_STAT_XRDY |
  399. I2C_STAT_ARDY |
  400. I2C_STAT_NACK);
  401. if (status & (I2C_STAT_NACK |
  402. I2C_STAT_TIMEO)) {
  403. i2c_error = 1;
  404. break;
  405. }
  406. if (status & I2C_STAT_ARDY)
  407. break;
  408. }
  409. }
  410. }
  411. status = wait_for_status_mask(I2C_STAT_ARDY | I2C_STAT_NACK |
  412. I2C_STAT_AL);
  413. if (status & (I2C_STAT_NACK | I2C_STAT_TIMEO))
  414. i2c_error = 1;
  415. if (i2c_error) {
  416. writew(0, &i2c_base->con);
  417. return 1;
  418. }
  419. if (!i2c_error) {
  420. int eout = 200;
  421. writew(I2C_CON_EN, &i2c_base->con);
  422. while ((status = readw(&i2c_base->stat)) ||
  423. (readw(&i2c_base->con) & I2C_CON_MST)) {
  424. udelay(1000);
  425. /* have to read to clear intrrupt */
  426. writew(0xFFFF, &i2c_base->stat);
  427. if (--eout == 0)
  428. /* better leave with error than hang */
  429. break;
  430. }
  431. }
  432. flush_fifo();
  433. writew(0xFFFF, &i2c_base->stat);
  434. writew(0, &i2c_base->cnt);
  435. return 0;
  436. }
  437. static u32 wait_for_bb(void)
  438. {
  439. int timeout = I2C_TIMEOUT;
  440. u32 stat;
  441. while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
  442. writew(stat, &i2c_base->stat);
  443. udelay(1000);
  444. }
  445. if (timeout <= 0) {
  446. printf("timed out in wait_for_bb: I2C_STAT=%x\n",
  447. readw(&i2c_base->stat));
  448. stat |= I2C_STAT_TIMEO;
  449. }
  450. writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
  451. return stat;
  452. }
  453. static u32 wait_for_status_mask(u16 mask)
  454. {
  455. u32 status;
  456. int timeout = I2C_TIMEOUT;
  457. do {
  458. udelay(1000);
  459. status = readw(&i2c_base->stat);
  460. } while (!(status & mask) && timeout--);
  461. if (timeout <= 0) {
  462. printf("timed out in wait_for_status_mask: I2C_STAT=%x\n",
  463. readw(&i2c_base->stat));
  464. writew(0xFFFF, &i2c_base->stat);
  465. status |= I2C_STAT_TIMEO;
  466. }
  467. return status;
  468. }
  469. int i2c_set_bus_num(unsigned int bus)
  470. {
  471. if ((bus < 0) || (bus >= I2C_BUS_MAX)) {
  472. printf("Bad bus: %d\n", bus);
  473. return -1;
  474. }
  475. #if I2C_BUS_MAX == 3
  476. if (bus == 2)
  477. i2c_base = (struct i2c *)I2C_BASE3;
  478. else
  479. #endif
  480. if (bus == 1)
  481. i2c_base = (struct i2c *)I2C_BASE2;
  482. else
  483. i2c_base = (struct i2c *)I2C_BASE1;
  484. current_bus = bus;
  485. if (!bus_initialized[current_bus])
  486. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  487. return 0;
  488. }
  489. int i2c_get_bus_num(void)
  490. {
  491. return (int) current_bus;
  492. }