omap24xx_i2c.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  325. /*
  326. * EEPROM chips that implement "address overflow" are ones
  327. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  328. * address and the extra bits end up in the "chip address"
  329. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  330. * four 256 byte chips.
  331. *
  332. * Note that we consider the length of the address field to
  333. * still be one byte because the extra address bits are
  334. * hidden in the chip address.
  335. */
  336. if (alen > 0)
  337. chip |= ((addr >> (alen * 8)) &
  338. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  339. #endif
  340. /* Wait until bus not busy */
  341. if (wait_for_bb(adap))
  342. return 1;
  343. /* Zero, one or two bytes reg address (offset) */
  344. writew(alen, &i2c_base->cnt);
  345. /* Set slave address */
  346. writew(chip, &i2c_base->sa);
  347. if (alen) {
  348. /* Must write reg offset first */
  349. #ifdef CONFIG_I2C_REPEATED_START
  350. /* No stop bit, use Repeated Start (Sr) */
  351. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
  352. I2C_CON_TRX, &i2c_base->con);
  353. #else
  354. /* Stop - Start (P-S) */
  355. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
  356. I2C_CON_TRX, &i2c_base->con);
  357. #endif
  358. /* Send register offset */
  359. while (1) {
  360. status = wait_for_event(adap);
  361. /* Try to identify bus that is not padconf'd for I2C */
  362. if (status == I2C_STAT_XRDY) {
  363. i2c_error = 2;
  364. printf("i2c_read (addr phase): pads on bus %d probably not configured (status=0x%x)\n",
  365. adap->hwadapnr, status);
  366. goto rd_exit;
  367. }
  368. if (status == 0 || (status & I2C_STAT_NACK)) {
  369. i2c_error = 1;
  370. printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
  371. status);
  372. goto rd_exit;
  373. }
  374. if (alen) {
  375. if (status & I2C_STAT_XRDY) {
  376. alen--;
  377. /* Do we have to use byte access? */
  378. writeb((addr >> (8 * alen)) & 0xff,
  379. &i2c_base->data);
  380. writew(I2C_STAT_XRDY, &i2c_base->stat);
  381. }
  382. }
  383. if (status & I2C_STAT_ARDY) {
  384. writew(I2C_STAT_ARDY, &i2c_base->stat);
  385. break;
  386. }
  387. }
  388. }
  389. /* Set slave address */
  390. writew(chip, &i2c_base->sa);
  391. /* Read len bytes from slave */
  392. writew(len, &i2c_base->cnt);
  393. /* Need stop bit here */
  394. writew(I2C_CON_EN | I2C_CON_MST |
  395. I2C_CON_STT | I2C_CON_STP,
  396. &i2c_base->con);
  397. /* Receive data */
  398. while (1) {
  399. status = wait_for_event(adap);
  400. /*
  401. * Try to identify bus that is not padconf'd for I2C. This
  402. * state could be left over from previous transactions if
  403. * the address phase is skipped due to alen=0.
  404. */
  405. if (status == I2C_STAT_XRDY) {
  406. i2c_error = 2;
  407. printf("i2c_read (data phase): pads on bus %d probably not configured (status=0x%x)\n",
  408. adap->hwadapnr, status);
  409. goto rd_exit;
  410. }
  411. if (status == 0 || (status & I2C_STAT_NACK)) {
  412. i2c_error = 1;
  413. goto rd_exit;
  414. }
  415. if (status & I2C_STAT_RRDY) {
  416. *buffer++ = readb(&i2c_base->data);
  417. writew(I2C_STAT_RRDY, &i2c_base->stat);
  418. }
  419. if (status & I2C_STAT_ARDY) {
  420. writew(I2C_STAT_ARDY, &i2c_base->stat);
  421. break;
  422. }
  423. }
  424. rd_exit:
  425. flush_fifo(adap);
  426. writew(0xFFFF, &i2c_base->stat);
  427. return i2c_error;
  428. }
  429. /* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
  430. static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  431. int alen, uchar *buffer, int len)
  432. {
  433. struct i2c *i2c_base = omap24_get_base(adap);
  434. int i;
  435. u16 status;
  436. int i2c_error = 0;
  437. int timeout = I2C_TIMEOUT;
  438. if (alen < 0) {
  439. puts("I2C write: addr len < 0\n");
  440. return 1;
  441. }
  442. if (len < 0) {
  443. puts("I2C write: data len < 0\n");
  444. return 1;
  445. }
  446. if (buffer == NULL) {
  447. puts("I2C write: NULL pointer passed\n");
  448. return 1;
  449. }
  450. if (alen > 2) {
  451. printf("I2C write: addr len %d not supported\n", alen);
  452. return 1;
  453. }
  454. if (addr + len > (1 << 16)) {
  455. printf("I2C write: address 0x%x + 0x%x out of range\n",
  456. addr, len);
  457. return 1;
  458. }
  459. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  460. /*
  461. * EEPROM chips that implement "address overflow" are ones
  462. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  463. * address and the extra bits end up in the "chip address"
  464. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  465. * four 256 byte chips.
  466. *
  467. * Note that we consider the length of the address field to
  468. * still be one byte because the extra address bits are
  469. * hidden in the chip address.
  470. */
  471. if (alen > 0)
  472. chip |= ((addr >> (alen * 8)) &
  473. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  474. #endif
  475. /* Wait until bus not busy */
  476. if (wait_for_bb(adap))
  477. return 1;
  478. /* Start address phase - will write regoffset + len bytes data */
  479. writew(alen + len, &i2c_base->cnt);
  480. /* Set slave address */
  481. writew(chip, &i2c_base->sa);
  482. /* Stop bit needed here */
  483. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  484. I2C_CON_STP, &i2c_base->con);
  485. while (alen) {
  486. /* Must write reg offset (one or two bytes) */
  487. status = wait_for_event(adap);
  488. /* Try to identify bus that is not padconf'd for I2C */
  489. if (status == I2C_STAT_XRDY) {
  490. i2c_error = 2;
  491. printf("i2c_write: pads on bus %d probably not configured (status=0x%x)\n",
  492. adap->hwadapnr, status);
  493. goto wr_exit;
  494. }
  495. if (status == 0 || (status & I2C_STAT_NACK)) {
  496. i2c_error = 1;
  497. printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
  498. status);
  499. goto wr_exit;
  500. }
  501. if (status & I2C_STAT_XRDY) {
  502. alen--;
  503. writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
  504. writew(I2C_STAT_XRDY, &i2c_base->stat);
  505. } else {
  506. i2c_error = 1;
  507. printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
  508. status);
  509. goto wr_exit;
  510. }
  511. }
  512. /* Address phase is over, now write data */
  513. for (i = 0; i < len; i++) {
  514. status = wait_for_event(adap);
  515. if (status == 0 || (status & I2C_STAT_NACK)) {
  516. i2c_error = 1;
  517. printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
  518. status);
  519. goto wr_exit;
  520. }
  521. if (status & I2C_STAT_XRDY) {
  522. writeb(buffer[i], &i2c_base->data);
  523. writew(I2C_STAT_XRDY, &i2c_base->stat);
  524. } else {
  525. i2c_error = 1;
  526. printf("i2c_write: bus not ready for data Tx (i=%d)\n",
  527. i);
  528. goto wr_exit;
  529. }
  530. }
  531. /*
  532. * poll ARDY bit for making sure that last byte really has been
  533. * transferred on the bus.
  534. */
  535. do {
  536. status = wait_for_event(adap);
  537. } while (!(status & I2C_STAT_ARDY) && timeout--);
  538. if (timeout <= 0)
  539. printf("i2c_write: timed out writig last byte!\n");
  540. wr_exit:
  541. flush_fifo(adap);
  542. writew(0xFFFF, &i2c_base->stat);
  543. return i2c_error;
  544. }
  545. /*
  546. * Wait for the bus to be free by checking the Bus Busy (BB)
  547. * bit to become clear
  548. */
  549. static int wait_for_bb(struct i2c_adapter *adap)
  550. {
  551. struct i2c *i2c_base = omap24_get_base(adap);
  552. int timeout = I2C_TIMEOUT;
  553. u16 stat;
  554. writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
  555. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  556. while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
  557. #else
  558. /* Read RAW status */
  559. while ((stat = readw(&i2c_base->irqstatus_raw) &
  560. I2C_STAT_BB) && timeout--) {
  561. #endif
  562. writew(stat, &i2c_base->stat);
  563. udelay(adap->waitdelay);
  564. }
  565. if (timeout <= 0) {
  566. printf("Timed out in wait_for_bb: status=%04x\n",
  567. stat);
  568. return 1;
  569. }
  570. writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
  571. return 0;
  572. }
  573. /*
  574. * Wait for the I2C controller to complete current action
  575. * and update status
  576. */
  577. static u16 wait_for_event(struct i2c_adapter *adap)
  578. {
  579. struct i2c *i2c_base = omap24_get_base(adap);
  580. u16 status;
  581. int timeout = I2C_TIMEOUT;
  582. do {
  583. udelay(adap->waitdelay);
  584. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  585. status = readw(&i2c_base->stat);
  586. #else
  587. /* Read RAW status */
  588. status = readw(&i2c_base->irqstatus_raw);
  589. #endif
  590. } while (!(status &
  591. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  592. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  593. I2C_STAT_AL)) && timeout--);
  594. if (timeout <= 0) {
  595. printf("Timed out in wait_for_event: status=%04x\n",
  596. status);
  597. /*
  598. * If status is still 0 here, probably the bus pads have
  599. * not been configured for I2C, and/or pull-ups are missing.
  600. */
  601. printf("Check if pads/pull-ups of bus %d are properly configured\n",
  602. adap->hwadapnr);
  603. writew(0xFFFF, &i2c_base->stat);
  604. status = 0;
  605. }
  606. return status;
  607. }
  608. static struct i2c *omap24_get_base(struct i2c_adapter *adap)
  609. {
  610. switch (adap->hwadapnr) {
  611. case 0:
  612. return (struct i2c *)I2C_BASE1;
  613. break;
  614. case 1:
  615. return (struct i2c *)I2C_BASE2;
  616. break;
  617. #if (I2C_BUS_MAX > 2)
  618. case 2:
  619. return (struct i2c *)I2C_BASE3;
  620. break;
  621. #if (I2C_BUS_MAX > 3)
  622. case 3:
  623. return (struct i2c *)I2C_BASE4;
  624. break;
  625. #if (I2C_BUS_MAX > 4)
  626. case 4:
  627. return (struct i2c *)I2C_BASE5;
  628. break;
  629. #endif
  630. #endif
  631. #endif
  632. default:
  633. printf("wrong hwadapnr: %d\n", adap->hwadapnr);
  634. break;
  635. }
  636. return NULL;
  637. }
  638. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
  639. #define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
  640. #endif
  641. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
  642. #define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
  643. #endif
  644. U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
  645. omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
  646. CONFIG_SYS_OMAP24_I2C_SPEED,
  647. CONFIG_SYS_OMAP24_I2C_SLAVE,
  648. 0)
  649. U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
  650. omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
  651. CONFIG_SYS_OMAP24_I2C_SPEED1,
  652. CONFIG_SYS_OMAP24_I2C_SLAVE1,
  653. 1)
  654. #if (I2C_BUS_MAX > 2)
  655. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
  656. #define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
  657. #endif
  658. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
  659. #define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
  660. #endif
  661. U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
  662. omap24_i2c_read, omap24_i2c_write, NULL,
  663. CONFIG_SYS_OMAP24_I2C_SPEED2,
  664. CONFIG_SYS_OMAP24_I2C_SLAVE2,
  665. 2)
  666. #if (I2C_BUS_MAX > 3)
  667. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
  668. #define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
  669. #endif
  670. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
  671. #define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
  672. #endif
  673. U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
  674. omap24_i2c_read, omap24_i2c_write, NULL,
  675. CONFIG_SYS_OMAP24_I2C_SPEED3,
  676. CONFIG_SYS_OMAP24_I2C_SLAVE3,
  677. 3)
  678. #if (I2C_BUS_MAX > 4)
  679. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
  680. #define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
  681. #endif
  682. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
  683. #define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
  684. #endif
  685. U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
  686. omap24_i2c_read, omap24_i2c_write, NULL,
  687. CONFIG_SYS_OMAP24_I2C_SPEED4,
  688. CONFIG_SYS_OMAP24_I2C_SLAVE4,
  689. 4)
  690. #endif
  691. #endif
  692. #endif