omap24xx_i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. * Copyright (c) 2013 Lubomir Popov <lpopov@mm-sol.com>, MM Solutions
  22. * New i2c_read, i2c_write and i2c_probe functions, tested on OMAP4
  23. * (4430/60/70), OMAP5 (5430) and AM335X (3359); should work on older
  24. * OMAPs and derivatives as well. The only anticipated exception would
  25. * be the OMAP2420, which shall require driver modification.
  26. * - Rewritten i2c_read to operate correctly with all types of chips
  27. * (old function could not read consistent data from some I2C slaves).
  28. * - Optimized i2c_write.
  29. * - New i2c_probe, performs write access vs read. The old probe could
  30. * hang the system under certain conditions (e.g. unconfigured pads).
  31. * - The read/write/probe functions try to identify unconfigured bus.
  32. * - Status functions now read irqstatus_raw as per TRM guidelines
  33. * (except for OMAP243X and OMAP34XX).
  34. * - Driver now supports up to I2C5 (OMAP5).
  35. *
  36. * Copyright (c) 2014 Hannes Schmelzer <oe5hpm@oevsv.at>, B&R
  37. * - Added support for set_speed
  38. *
  39. */
  40. #include <common.h>
  41. #include <i2c.h>
  42. #include <asm/arch/i2c.h>
  43. #include <asm/io.h>
  44. #include "omap24xx_i2c.h"
  45. DECLARE_GLOBAL_DATA_PTR;
  46. #define I2C_TIMEOUT 1000
  47. /* Absolutely safe for status update at 100 kHz I2C: */
  48. #define I2C_WAIT 200
  49. static int wait_for_bb(struct i2c_adapter *adap);
  50. static struct i2c *omap24_get_base(struct i2c_adapter *adap);
  51. static u16 wait_for_event(struct i2c_adapter *adap);
  52. static void flush_fifo(struct i2c_adapter *adap);
  53. static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
  54. {
  55. unsigned int sampleclk, prescaler;
  56. int fsscll, fssclh;
  57. speed <<= 1;
  58. prescaler = 0;
  59. /*
  60. * some divisors may cause a precission loss, but shouldn't
  61. * be a big thing, because i2c_clk is then allready very slow.
  62. */
  63. while (prescaler <= 0xFF) {
  64. sampleclk = I2C_IP_CLK / (prescaler+1);
  65. fsscll = sampleclk / speed;
  66. fssclh = fsscll;
  67. fsscll -= I2C_FASTSPEED_SCLL_TRIM;
  68. fssclh -= I2C_FASTSPEED_SCLH_TRIM;
  69. if (((fsscll > 0) && (fssclh > 0)) &&
  70. ((fsscll <= (255-I2C_FASTSPEED_SCLL_TRIM)) &&
  71. (fssclh <= (255-I2C_FASTSPEED_SCLH_TRIM)))) {
  72. if (pscl)
  73. *pscl = fsscll;
  74. if (psch)
  75. *psch = fssclh;
  76. return prescaler;
  77. }
  78. prescaler++;
  79. }
  80. return -1;
  81. }
  82. static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
  83. {
  84. struct i2c *i2c_base = omap24_get_base(adap);
  85. int psc, fsscll = 0, fssclh = 0;
  86. int hsscll = 0, hssclh = 0;
  87. u32 scll = 0, sclh = 0;
  88. if (speed >= OMAP_I2C_HIGH_SPEED) {
  89. /* High speed */
  90. psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
  91. psc -= 1;
  92. if (psc < I2C_PSC_MIN) {
  93. printf("Error : I2C unsupported prescaler %d\n", psc);
  94. return -1;
  95. }
  96. /* For first phase of HS mode */
  97. fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
  98. fssclh = fsscll;
  99. fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
  100. fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
  101. if (((fsscll < 0) || (fssclh < 0)) ||
  102. ((fsscll > 255) || (fssclh > 255))) {
  103. puts("Error : I2C initializing first phase clock\n");
  104. return -1;
  105. }
  106. /* For second phase of HS mode */
  107. hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
  108. hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
  109. hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
  110. if (((fsscll < 0) || (fssclh < 0)) ||
  111. ((fsscll > 255) || (fssclh > 255))) {
  112. puts("Error : I2C initializing second phase clock\n");
  113. return -1;
  114. }
  115. scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
  116. sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
  117. } else {
  118. /* Standard and fast speed */
  119. psc = omap24_i2c_findpsc(&scll, &sclh, speed);
  120. if (0 > psc) {
  121. puts("Error : I2C initializing clock\n");
  122. return -1;
  123. }
  124. }
  125. adap->speed = speed;
  126. adap->waitdelay = (10000000 / speed) * 2; /* wait for 20 clkperiods */
  127. writew(0, &i2c_base->con);
  128. writew(psc, &i2c_base->psc);
  129. writew(scll, &i2c_base->scll);
  130. writew(sclh, &i2c_base->sclh);
  131. writew(I2C_CON_EN, &i2c_base->con);
  132. writew(0xFFFF, &i2c_base->stat); /* clear all pending status */
  133. return 0;
  134. }
  135. static void omap24_i2c_deblock(struct i2c_adapter *adap)
  136. {
  137. struct i2c *i2c_base = omap24_get_base(adap);
  138. int i;
  139. u16 systest;
  140. u16 orgsystest;
  141. /* set test mode ST_EN = 1 */
  142. orgsystest = readw(&i2c_base->systest);
  143. systest = orgsystest;
  144. /* enable testmode */
  145. systest |= I2C_SYSTEST_ST_EN;
  146. writew(systest, &i2c_base->systest);
  147. systest &= ~I2C_SYSTEST_TMODE_MASK;
  148. systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
  149. writew(systest, &i2c_base->systest);
  150. /* set SCL, SDA = 1 */
  151. systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
  152. writew(systest, &i2c_base->systest);
  153. udelay(10);
  154. /* toggle scl 9 clocks */
  155. for (i = 0; i < 9; i++) {
  156. /* SCL = 0 */
  157. systest &= ~I2C_SYSTEST_SCL_O;
  158. writew(systest, &i2c_base->systest);
  159. udelay(10);
  160. /* SCL = 1 */
  161. systest |= I2C_SYSTEST_SCL_O;
  162. writew(systest, &i2c_base->systest);
  163. udelay(10);
  164. }
  165. /* send stop */
  166. systest &= ~I2C_SYSTEST_SDA_O;
  167. writew(systest, &i2c_base->systest);
  168. udelay(10);
  169. systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
  170. writew(systest, &i2c_base->systest);
  171. udelay(10);
  172. /* restore original mode */
  173. writew(orgsystest, &i2c_base->systest);
  174. }
  175. static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  176. {
  177. struct i2c *i2c_base = omap24_get_base(adap);
  178. int timeout = I2C_TIMEOUT;
  179. int deblock = 1;
  180. retry:
  181. if (readw(&i2c_base->con) & I2C_CON_EN) {
  182. writew(0, &i2c_base->con);
  183. udelay(50000);
  184. }
  185. writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
  186. udelay(1000);
  187. writew(I2C_CON_EN, &i2c_base->con);
  188. while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
  189. if (timeout <= 0) {
  190. puts("ERROR: Timeout in soft-reset\n");
  191. return;
  192. }
  193. udelay(1000);
  194. }
  195. if (0 != omap24_i2c_setspeed(adap, speed)) {
  196. printf("ERROR: failed to setup I2C bus-speed!\n");
  197. return;
  198. }
  199. /* own address */
  200. writew(slaveadd, &i2c_base->oa);
  201. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  202. /*
  203. * Have to enable interrupts for OMAP2/3, these IPs don't have
  204. * an 'irqstatus_raw' register and we shall have to poll 'stat'
  205. */
  206. writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
  207. I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
  208. #endif
  209. udelay(1000);
  210. flush_fifo(adap);
  211. writew(0xFFFF, &i2c_base->stat);
  212. /* Handle possible failed I2C state */
  213. if (wait_for_bb(adap))
  214. if (deblock == 1) {
  215. omap24_i2c_deblock(adap);
  216. deblock = 0;
  217. goto retry;
  218. }
  219. }
  220. static void flush_fifo(struct i2c_adapter *adap)
  221. {
  222. struct i2c *i2c_base = omap24_get_base(adap);
  223. u16 stat;
  224. /*
  225. * note: if you try and read data when its not there or ready
  226. * you get a bus error
  227. */
  228. while (1) {
  229. stat = readw(&i2c_base->stat);
  230. if (stat == I2C_STAT_RRDY) {
  231. readb(&i2c_base->data);
  232. writew(I2C_STAT_RRDY, &i2c_base->stat);
  233. udelay(1000);
  234. } else
  235. break;
  236. }
  237. }
  238. /*
  239. * i2c_probe: Use write access. Allows to identify addresses that are
  240. * write-only (like the config register of dual-port EEPROMs)
  241. */
  242. static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
  243. {
  244. struct i2c *i2c_base = omap24_get_base(adap);
  245. u16 status;
  246. int res = 1; /* default = fail */
  247. if (chip == readw(&i2c_base->oa))
  248. return res;
  249. /* Wait until bus is free */
  250. if (wait_for_bb(adap))
  251. return res;
  252. /* No data transfer, slave addr only */
  253. writew(chip, &i2c_base->sa);
  254. /* Stop bit needed here */
  255. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  256. I2C_CON_STP, &i2c_base->con);
  257. status = wait_for_event(adap);
  258. if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
  259. /*
  260. * With current high-level command implementation, notifying
  261. * the user shall flood the console with 127 messages. If
  262. * silent exit is desired upon unconfigured bus, remove the
  263. * following 'if' section:
  264. */
  265. if (status == I2C_STAT_XRDY)
  266. printf("i2c_probe: pads on bus %d probably not configured (status=0x%x)\n",
  267. adap->hwadapnr, status);
  268. goto pr_exit;
  269. }
  270. /* Check for ACK (!NAK) */
  271. if (!(status & I2C_STAT_NACK)) {
  272. res = 0; /* Device found */
  273. udelay(adap->waitdelay);/* Required by AM335X in SPL */
  274. /* Abort transfer (force idle state) */
  275. writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
  276. udelay(1000);
  277. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
  278. I2C_CON_STP, &i2c_base->con); /* STP */
  279. }
  280. pr_exit:
  281. flush_fifo(adap);
  282. writew(0xFFFF, &i2c_base->stat);
  283. return res;
  284. }
  285. /*
  286. * i2c_read: Function now uses a single I2C read transaction with bulk transfer
  287. * of the requested number of bytes (note that the 'i2c md' command
  288. * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
  289. * defined in the board config header, this transaction shall be with
  290. * Repeated Start (Sr) between the address and data phases; otherwise
  291. * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
  292. * The address (reg offset) may be 0, 1 or 2 bytes long.
  293. * Function now reads correctly from chips that return more than one
  294. * byte of data per addressed register (like TI temperature sensors),
  295. * or that do not need a register address at all (such as some clock
  296. * distributors).
  297. */
  298. static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  299. int alen, uchar *buffer, int len)
  300. {
  301. struct i2c *i2c_base = omap24_get_base(adap);
  302. int i2c_error = 0;
  303. u16 status;
  304. if (alen < 0) {
  305. puts("I2C read: addr len < 0\n");
  306. return 1;
  307. }
  308. if (len < 0) {
  309. puts("I2C read: data len < 0\n");
  310. return 1;
  311. }
  312. if (buffer == NULL) {
  313. puts("I2C read: NULL pointer passed\n");
  314. return 1;
  315. }
  316. if (alen > 2) {
  317. printf("I2C read: addr len %d not supported\n", alen);
  318. return 1;
  319. }
  320. if (addr + len > (1 << 16)) {
  321. puts("I2C read: address out of range\n");
  322. return 1;
  323. }
  324. /* Wait until bus not busy */
  325. if (wait_for_bb(adap))
  326. return 1;
  327. /* Zero, one or two bytes reg address (offset) */
  328. writew(alen, &i2c_base->cnt);
  329. /* Set slave address */
  330. writew(chip, &i2c_base->sa);
  331. if (alen) {
  332. /* Must write reg offset first */
  333. #ifdef CONFIG_I2C_REPEATED_START
  334. /* No stop bit, use Repeated Start (Sr) */
  335. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
  336. I2C_CON_TRX, &i2c_base->con);
  337. #else
  338. /* Stop - Start (P-S) */
  339. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
  340. I2C_CON_TRX, &i2c_base->con);
  341. #endif
  342. /* Send register offset */
  343. while (1) {
  344. status = wait_for_event(adap);
  345. /* Try to identify bus that is not padconf'd for I2C */
  346. if (status == I2C_STAT_XRDY) {
  347. i2c_error = 2;
  348. printf("i2c_read (addr phase): pads on bus %d probably not configured (status=0x%x)\n",
  349. adap->hwadapnr, status);
  350. goto rd_exit;
  351. }
  352. if (status == 0 || (status & I2C_STAT_NACK)) {
  353. i2c_error = 1;
  354. printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
  355. status);
  356. goto rd_exit;
  357. }
  358. if (alen) {
  359. if (status & I2C_STAT_XRDY) {
  360. alen--;
  361. /* Do we have to use byte access? */
  362. writeb((addr >> (8 * alen)) & 0xff,
  363. &i2c_base->data);
  364. writew(I2C_STAT_XRDY, &i2c_base->stat);
  365. }
  366. }
  367. if (status & I2C_STAT_ARDY) {
  368. writew(I2C_STAT_ARDY, &i2c_base->stat);
  369. break;
  370. }
  371. }
  372. }
  373. /* Set slave address */
  374. writew(chip, &i2c_base->sa);
  375. /* Read len bytes from slave */
  376. writew(len, &i2c_base->cnt);
  377. /* Need stop bit here */
  378. writew(I2C_CON_EN | I2C_CON_MST |
  379. I2C_CON_STT | I2C_CON_STP,
  380. &i2c_base->con);
  381. /* Receive data */
  382. while (1) {
  383. status = wait_for_event(adap);
  384. /*
  385. * Try to identify bus that is not padconf'd for I2C. This
  386. * state could be left over from previous transactions if
  387. * the address phase is skipped due to alen=0.
  388. */
  389. if (status == I2C_STAT_XRDY) {
  390. i2c_error = 2;
  391. printf("i2c_read (data phase): pads on bus %d probably not configured (status=0x%x)\n",
  392. adap->hwadapnr, status);
  393. goto rd_exit;
  394. }
  395. if (status == 0 || (status & I2C_STAT_NACK)) {
  396. i2c_error = 1;
  397. goto rd_exit;
  398. }
  399. if (status & I2C_STAT_RRDY) {
  400. *buffer++ = readb(&i2c_base->data);
  401. writew(I2C_STAT_RRDY, &i2c_base->stat);
  402. }
  403. if (status & I2C_STAT_ARDY) {
  404. writew(I2C_STAT_ARDY, &i2c_base->stat);
  405. break;
  406. }
  407. }
  408. rd_exit:
  409. flush_fifo(adap);
  410. writew(0xFFFF, &i2c_base->stat);
  411. return i2c_error;
  412. }
  413. /* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
  414. static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  415. int alen, uchar *buffer, int len)
  416. {
  417. struct i2c *i2c_base = omap24_get_base(adap);
  418. int i;
  419. u16 status;
  420. int i2c_error = 0;
  421. int timeout = I2C_TIMEOUT;
  422. if (alen < 0) {
  423. puts("I2C write: addr len < 0\n");
  424. return 1;
  425. }
  426. if (len < 0) {
  427. puts("I2C write: data len < 0\n");
  428. return 1;
  429. }
  430. if (buffer == NULL) {
  431. puts("I2C write: NULL pointer passed\n");
  432. return 1;
  433. }
  434. if (alen > 2) {
  435. printf("I2C write: addr len %d not supported\n", alen);
  436. return 1;
  437. }
  438. if (addr + len > (1 << 16)) {
  439. printf("I2C write: address 0x%x + 0x%x out of range\n",
  440. addr, len);
  441. return 1;
  442. }
  443. /* Wait until bus not busy */
  444. if (wait_for_bb(adap))
  445. return 1;
  446. /* Start address phase - will write regoffset + len bytes data */
  447. writew(alen + len, &i2c_base->cnt);
  448. /* Set slave address */
  449. writew(chip, &i2c_base->sa);
  450. /* Stop bit needed here */
  451. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  452. I2C_CON_STP, &i2c_base->con);
  453. while (alen) {
  454. /* Must write reg offset (one or two bytes) */
  455. status = wait_for_event(adap);
  456. /* Try to identify bus that is not padconf'd for I2C */
  457. if (status == I2C_STAT_XRDY) {
  458. i2c_error = 2;
  459. printf("i2c_write: pads on bus %d probably not configured (status=0x%x)\n",
  460. adap->hwadapnr, status);
  461. goto wr_exit;
  462. }
  463. if (status == 0 || (status & I2C_STAT_NACK)) {
  464. i2c_error = 1;
  465. printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
  466. status);
  467. goto wr_exit;
  468. }
  469. if (status & I2C_STAT_XRDY) {
  470. alen--;
  471. writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
  472. writew(I2C_STAT_XRDY, &i2c_base->stat);
  473. } else {
  474. i2c_error = 1;
  475. printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
  476. status);
  477. goto wr_exit;
  478. }
  479. }
  480. /* Address phase is over, now write data */
  481. for (i = 0; i < len; i++) {
  482. status = wait_for_event(adap);
  483. if (status == 0 || (status & I2C_STAT_NACK)) {
  484. i2c_error = 1;
  485. printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
  486. status);
  487. goto wr_exit;
  488. }
  489. if (status & I2C_STAT_XRDY) {
  490. writeb(buffer[i], &i2c_base->data);
  491. writew(I2C_STAT_XRDY, &i2c_base->stat);
  492. } else {
  493. i2c_error = 1;
  494. printf("i2c_write: bus not ready for data Tx (i=%d)\n",
  495. i);
  496. goto wr_exit;
  497. }
  498. }
  499. /*
  500. * poll ARDY bit for making sure that last byte really has been
  501. * transferred on the bus.
  502. */
  503. do {
  504. status = wait_for_event(adap);
  505. } while (!(status & I2C_STAT_ARDY) && timeout--);
  506. if (timeout <= 0)
  507. printf("i2c_write: timed out writig last byte!\n");
  508. wr_exit:
  509. flush_fifo(adap);
  510. writew(0xFFFF, &i2c_base->stat);
  511. return i2c_error;
  512. }
  513. /*
  514. * Wait for the bus to be free by checking the Bus Busy (BB)
  515. * bit to become clear
  516. */
  517. static int wait_for_bb(struct i2c_adapter *adap)
  518. {
  519. struct i2c *i2c_base = omap24_get_base(adap);
  520. int timeout = I2C_TIMEOUT;
  521. u16 stat;
  522. writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
  523. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  524. while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
  525. #else
  526. /* Read RAW status */
  527. while ((stat = readw(&i2c_base->irqstatus_raw) &
  528. I2C_STAT_BB) && timeout--) {
  529. #endif
  530. writew(stat, &i2c_base->stat);
  531. udelay(adap->waitdelay);
  532. }
  533. if (timeout <= 0) {
  534. printf("Timed out in wait_for_bb: status=%04x\n",
  535. stat);
  536. return 1;
  537. }
  538. writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
  539. return 0;
  540. }
  541. /*
  542. * Wait for the I2C controller to complete current action
  543. * and update status
  544. */
  545. static u16 wait_for_event(struct i2c_adapter *adap)
  546. {
  547. struct i2c *i2c_base = omap24_get_base(adap);
  548. u16 status;
  549. int timeout = I2C_TIMEOUT;
  550. do {
  551. udelay(adap->waitdelay);
  552. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  553. status = readw(&i2c_base->stat);
  554. #else
  555. /* Read RAW status */
  556. status = readw(&i2c_base->irqstatus_raw);
  557. #endif
  558. } while (!(status &
  559. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  560. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  561. I2C_STAT_AL)) && timeout--);
  562. if (timeout <= 0) {
  563. printf("Timed out in wait_for_event: status=%04x\n",
  564. status);
  565. /*
  566. * If status is still 0 here, probably the bus pads have
  567. * not been configured for I2C, and/or pull-ups are missing.
  568. */
  569. printf("Check if pads/pull-ups of bus %d are properly configured\n",
  570. adap->hwadapnr);
  571. writew(0xFFFF, &i2c_base->stat);
  572. status = 0;
  573. }
  574. return status;
  575. }
  576. static struct i2c *omap24_get_base(struct i2c_adapter *adap)
  577. {
  578. switch (adap->hwadapnr) {
  579. case 0:
  580. return (struct i2c *)I2C_BASE1;
  581. break;
  582. case 1:
  583. return (struct i2c *)I2C_BASE2;
  584. break;
  585. #if (I2C_BUS_MAX > 2)
  586. case 2:
  587. return (struct i2c *)I2C_BASE3;
  588. break;
  589. #if (I2C_BUS_MAX > 3)
  590. case 3:
  591. return (struct i2c *)I2C_BASE4;
  592. break;
  593. #if (I2C_BUS_MAX > 4)
  594. case 4:
  595. return (struct i2c *)I2C_BASE5;
  596. break;
  597. #endif
  598. #endif
  599. #endif
  600. default:
  601. printf("wrong hwadapnr: %d\n", adap->hwadapnr);
  602. break;
  603. }
  604. return NULL;
  605. }
  606. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
  607. #define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
  608. #endif
  609. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
  610. #define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
  611. #endif
  612. U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
  613. omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
  614. CONFIG_SYS_OMAP24_I2C_SPEED,
  615. CONFIG_SYS_OMAP24_I2C_SLAVE,
  616. 0)
  617. U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
  618. omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
  619. CONFIG_SYS_OMAP24_I2C_SPEED1,
  620. CONFIG_SYS_OMAP24_I2C_SLAVE1,
  621. 1)
  622. #if (I2C_BUS_MAX > 2)
  623. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
  624. #define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
  625. #endif
  626. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
  627. #define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
  628. #endif
  629. U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
  630. omap24_i2c_read, omap24_i2c_write, NULL,
  631. CONFIG_SYS_OMAP24_I2C_SPEED2,
  632. CONFIG_SYS_OMAP24_I2C_SLAVE2,
  633. 2)
  634. #if (I2C_BUS_MAX > 3)
  635. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
  636. #define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
  637. #endif
  638. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
  639. #define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
  640. #endif
  641. U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
  642. omap24_i2c_read, omap24_i2c_write, NULL,
  643. CONFIG_SYS_OMAP24_I2C_SPEED3,
  644. CONFIG_SYS_OMAP24_I2C_SLAVE3,
  645. 3)
  646. #if (I2C_BUS_MAX > 4)
  647. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
  648. #define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
  649. #endif
  650. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
  651. #define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
  652. #endif
  653. U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
  654. omap24_i2c_read, omap24_i2c_write, NULL,
  655. CONFIG_SYS_OMAP24_I2C_SPEED4,
  656. CONFIG_SYS_OMAP24_I2C_SLAVE4,
  657. 4)
  658. #endif
  659. #endif
  660. #endif