omap24xx_i2c.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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 <dm.h>
  42. #include <i2c.h>
  43. #include <asm/arch/i2c.h>
  44. #include <asm/io.h>
  45. #include "omap24xx_i2c.h"
  46. DECLARE_GLOBAL_DATA_PTR;
  47. #define I2C_TIMEOUT 1000
  48. /* Absolutely safe for status update at 100 kHz I2C: */
  49. #define I2C_WAIT 200
  50. struct omap_i2c {
  51. struct udevice *clk;
  52. struct i2c *regs;
  53. unsigned int speed;
  54. int waitdelay;
  55. int clk_id;
  56. };
  57. static int omap24_i2c_findpsc(u32 *pscl, u32 *psch, uint speed)
  58. {
  59. unsigned int sampleclk, prescaler;
  60. int fsscll, fssclh;
  61. speed <<= 1;
  62. prescaler = 0;
  63. /*
  64. * some divisors may cause a precission loss, but shouldn't
  65. * be a big thing, because i2c_clk is then allready very slow.
  66. */
  67. while (prescaler <= 0xFF) {
  68. sampleclk = I2C_IP_CLK / (prescaler+1);
  69. fsscll = sampleclk / speed;
  70. fssclh = fsscll;
  71. fsscll -= I2C_FASTSPEED_SCLL_TRIM;
  72. fssclh -= I2C_FASTSPEED_SCLH_TRIM;
  73. if (((fsscll > 0) && (fssclh > 0)) &&
  74. ((fsscll <= (255-I2C_FASTSPEED_SCLL_TRIM)) &&
  75. (fssclh <= (255-I2C_FASTSPEED_SCLH_TRIM)))) {
  76. if (pscl)
  77. *pscl = fsscll;
  78. if (psch)
  79. *psch = fssclh;
  80. return prescaler;
  81. }
  82. prescaler++;
  83. }
  84. return -1;
  85. }
  86. /*
  87. * Wait for the bus to be free by checking the Bus Busy (BB)
  88. * bit to become clear
  89. */
  90. static int wait_for_bb(struct i2c *i2c_base, int waitdelay)
  91. {
  92. int timeout = I2C_TIMEOUT;
  93. u16 stat;
  94. writew(0xFFFF, &i2c_base->stat); /* clear current interrupts...*/
  95. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  96. while ((stat = readw(&i2c_base->stat) & I2C_STAT_BB) && timeout--) {
  97. #else
  98. /* Read RAW status */
  99. while ((stat = readw(&i2c_base->irqstatus_raw) &
  100. I2C_STAT_BB) && timeout--) {
  101. #endif
  102. writew(stat, &i2c_base->stat);
  103. udelay(waitdelay);
  104. }
  105. if (timeout <= 0) {
  106. printf("Timed out in wait_for_bb: status=%04x\n",
  107. stat);
  108. return 1;
  109. }
  110. writew(0xFFFF, &i2c_base->stat); /* clear delayed stuff*/
  111. return 0;
  112. }
  113. /*
  114. * Wait for the I2C controller to complete current action
  115. * and update status
  116. */
  117. static u16 wait_for_event(struct i2c *i2c_base, int waitdelay)
  118. {
  119. u16 status;
  120. int timeout = I2C_TIMEOUT;
  121. do {
  122. udelay(waitdelay);
  123. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  124. status = readw(&i2c_base->stat);
  125. #else
  126. /* Read RAW status */
  127. status = readw(&i2c_base->irqstatus_raw);
  128. #endif
  129. } while (!(status &
  130. (I2C_STAT_ROVR | I2C_STAT_XUDF | I2C_STAT_XRDY |
  131. I2C_STAT_RRDY | I2C_STAT_ARDY | I2C_STAT_NACK |
  132. I2C_STAT_AL)) && timeout--);
  133. if (timeout <= 0) {
  134. printf("Timed out in wait_for_event: status=%04x\n",
  135. status);
  136. /*
  137. * If status is still 0 here, probably the bus pads have
  138. * not been configured for I2C, and/or pull-ups are missing.
  139. */
  140. printf("Check if pads/pull-ups of bus are properly configured\n");
  141. writew(0xFFFF, &i2c_base->stat);
  142. status = 0;
  143. }
  144. return status;
  145. }
  146. static void flush_fifo(struct i2c *i2c_base)
  147. {
  148. u16 stat;
  149. /*
  150. * note: if you try and read data when its not there or ready
  151. * you get a bus error
  152. */
  153. while (1) {
  154. stat = readw(&i2c_base->stat);
  155. if (stat == I2C_STAT_RRDY) {
  156. readb(&i2c_base->data);
  157. writew(I2C_STAT_RRDY, &i2c_base->stat);
  158. udelay(1000);
  159. } else
  160. break;
  161. }
  162. }
  163. static int __omap24_i2c_setspeed(struct i2c *i2c_base, uint speed,
  164. int *waitdelay)
  165. {
  166. int psc, fsscll = 0, fssclh = 0;
  167. int hsscll = 0, hssclh = 0;
  168. u32 scll = 0, sclh = 0;
  169. if (speed >= OMAP_I2C_HIGH_SPEED) {
  170. /* High speed */
  171. psc = I2C_IP_CLK / I2C_INTERNAL_SAMPLING_CLK;
  172. psc -= 1;
  173. if (psc < I2C_PSC_MIN) {
  174. printf("Error : I2C unsupported prescaler %d\n", psc);
  175. return -1;
  176. }
  177. /* For first phase of HS mode */
  178. fsscll = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
  179. fssclh = fsscll;
  180. fsscll -= I2C_HIGHSPEED_PHASE_ONE_SCLL_TRIM;
  181. fssclh -= I2C_HIGHSPEED_PHASE_ONE_SCLH_TRIM;
  182. if (((fsscll < 0) || (fssclh < 0)) ||
  183. ((fsscll > 255) || (fssclh > 255))) {
  184. puts("Error : I2C initializing first phase clock\n");
  185. return -1;
  186. }
  187. /* For second phase of HS mode */
  188. hsscll = hssclh = I2C_INTERNAL_SAMPLING_CLK / (2 * speed);
  189. hsscll -= I2C_HIGHSPEED_PHASE_TWO_SCLL_TRIM;
  190. hssclh -= I2C_HIGHSPEED_PHASE_TWO_SCLH_TRIM;
  191. if (((fsscll < 0) || (fssclh < 0)) ||
  192. ((fsscll > 255) || (fssclh > 255))) {
  193. puts("Error : I2C initializing second phase clock\n");
  194. return -1;
  195. }
  196. scll = (unsigned int)hsscll << 8 | (unsigned int)fsscll;
  197. sclh = (unsigned int)hssclh << 8 | (unsigned int)fssclh;
  198. } else {
  199. /* Standard and fast speed */
  200. psc = omap24_i2c_findpsc(&scll, &sclh, speed);
  201. if (0 > psc) {
  202. puts("Error : I2C initializing clock\n");
  203. return -1;
  204. }
  205. }
  206. *waitdelay = (10000000 / speed) * 2; /* wait for 20 clkperiods */
  207. writew(0, &i2c_base->con);
  208. writew(psc, &i2c_base->psc);
  209. writew(scll, &i2c_base->scll);
  210. writew(sclh, &i2c_base->sclh);
  211. writew(I2C_CON_EN, &i2c_base->con);
  212. writew(0xFFFF, &i2c_base->stat); /* clear all pending status */
  213. return 0;
  214. }
  215. static void omap24_i2c_deblock(struct i2c *i2c_base)
  216. {
  217. int i;
  218. u16 systest;
  219. u16 orgsystest;
  220. /* set test mode ST_EN = 1 */
  221. orgsystest = readw(&i2c_base->systest);
  222. systest = orgsystest;
  223. /* enable testmode */
  224. systest |= I2C_SYSTEST_ST_EN;
  225. writew(systest, &i2c_base->systest);
  226. systest &= ~I2C_SYSTEST_TMODE_MASK;
  227. systest |= 3 << I2C_SYSTEST_TMODE_SHIFT;
  228. writew(systest, &i2c_base->systest);
  229. /* set SCL, SDA = 1 */
  230. systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
  231. writew(systest, &i2c_base->systest);
  232. udelay(10);
  233. /* toggle scl 9 clocks */
  234. for (i = 0; i < 9; i++) {
  235. /* SCL = 0 */
  236. systest &= ~I2C_SYSTEST_SCL_O;
  237. writew(systest, &i2c_base->systest);
  238. udelay(10);
  239. /* SCL = 1 */
  240. systest |= I2C_SYSTEST_SCL_O;
  241. writew(systest, &i2c_base->systest);
  242. udelay(10);
  243. }
  244. /* send stop */
  245. systest &= ~I2C_SYSTEST_SDA_O;
  246. writew(systest, &i2c_base->systest);
  247. udelay(10);
  248. systest |= I2C_SYSTEST_SCL_O | I2C_SYSTEST_SDA_O;
  249. writew(systest, &i2c_base->systest);
  250. udelay(10);
  251. /* restore original mode */
  252. writew(orgsystest, &i2c_base->systest);
  253. }
  254. static void __omap24_i2c_init(struct i2c *i2c_base, int speed, int slaveadd,
  255. int *waitdelay)
  256. {
  257. int timeout = I2C_TIMEOUT;
  258. int deblock = 1;
  259. retry:
  260. if (readw(&i2c_base->con) & I2C_CON_EN) {
  261. writew(0, &i2c_base->con);
  262. udelay(50000);
  263. }
  264. writew(0x2, &i2c_base->sysc); /* for ES2 after soft reset */
  265. udelay(1000);
  266. writew(I2C_CON_EN, &i2c_base->con);
  267. while (!(readw(&i2c_base->syss) & I2C_SYSS_RDONE) && timeout--) {
  268. if (timeout <= 0) {
  269. puts("ERROR: Timeout in soft-reset\n");
  270. return;
  271. }
  272. udelay(1000);
  273. }
  274. if (0 != __omap24_i2c_setspeed(i2c_base, speed, waitdelay)) {
  275. printf("ERROR: failed to setup I2C bus-speed!\n");
  276. return;
  277. }
  278. /* own address */
  279. writew(slaveadd, &i2c_base->oa);
  280. #if defined(CONFIG_OMAP243X) || defined(CONFIG_OMAP34XX)
  281. /*
  282. * Have to enable interrupts for OMAP2/3, these IPs don't have
  283. * an 'irqstatus_raw' register and we shall have to poll 'stat'
  284. */
  285. writew(I2C_IE_XRDY_IE | I2C_IE_RRDY_IE | I2C_IE_ARDY_IE |
  286. I2C_IE_NACK_IE | I2C_IE_AL_IE, &i2c_base->ie);
  287. #endif
  288. udelay(1000);
  289. flush_fifo(i2c_base);
  290. writew(0xFFFF, &i2c_base->stat);
  291. /* Handle possible failed I2C state */
  292. if (wait_for_bb(i2c_base, *waitdelay))
  293. if (deblock == 1) {
  294. omap24_i2c_deblock(i2c_base);
  295. deblock = 0;
  296. goto retry;
  297. }
  298. }
  299. /*
  300. * i2c_probe: Use write access. Allows to identify addresses that are
  301. * write-only (like the config register of dual-port EEPROMs)
  302. */
  303. static int __omap24_i2c_probe(struct i2c *i2c_base, int waitdelay, uchar chip)
  304. {
  305. u16 status;
  306. int res = 1; /* default = fail */
  307. if (chip == readw(&i2c_base->oa))
  308. return res;
  309. /* Wait until bus is free */
  310. if (wait_for_bb(i2c_base, waitdelay))
  311. return res;
  312. /* No data transfer, slave addr only */
  313. writew(chip, &i2c_base->sa);
  314. /* Stop bit needed here */
  315. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  316. I2C_CON_STP, &i2c_base->con);
  317. status = wait_for_event(i2c_base, waitdelay);
  318. if ((status & ~I2C_STAT_XRDY) == 0 || (status & I2C_STAT_AL)) {
  319. /*
  320. * With current high-level command implementation, notifying
  321. * the user shall flood the console with 127 messages. If
  322. * silent exit is desired upon unconfigured bus, remove the
  323. * following 'if' section:
  324. */
  325. if (status == I2C_STAT_XRDY)
  326. printf("i2c_probe: pads on bus probably not configured (status=0x%x)\n",
  327. status);
  328. goto pr_exit;
  329. }
  330. /* Check for ACK (!NAK) */
  331. if (!(status & I2C_STAT_NACK)) {
  332. res = 0; /* Device found */
  333. udelay(waitdelay);/* Required by AM335X in SPL */
  334. /* Abort transfer (force idle state) */
  335. writew(I2C_CON_MST | I2C_CON_TRX, &i2c_base->con); /* Reset */
  336. udelay(1000);
  337. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_TRX |
  338. I2C_CON_STP, &i2c_base->con); /* STP */
  339. }
  340. pr_exit:
  341. flush_fifo(i2c_base);
  342. writew(0xFFFF, &i2c_base->stat);
  343. return res;
  344. }
  345. /*
  346. * i2c_read: Function now uses a single I2C read transaction with bulk transfer
  347. * of the requested number of bytes (note that the 'i2c md' command
  348. * limits this to 16 bytes anyway). If CONFIG_I2C_REPEATED_START is
  349. * defined in the board config header, this transaction shall be with
  350. * Repeated Start (Sr) between the address and data phases; otherwise
  351. * Stop-Start (P-S) shall be used (some I2C chips do require a P-S).
  352. * The address (reg offset) may be 0, 1 or 2 bytes long.
  353. * Function now reads correctly from chips that return more than one
  354. * byte of data per addressed register (like TI temperature sensors),
  355. * or that do not need a register address at all (such as some clock
  356. * distributors).
  357. */
  358. static int __omap24_i2c_read(struct i2c *i2c_base, int waitdelay, uchar chip,
  359. uint addr, int alen, uchar *buffer, int len)
  360. {
  361. int i2c_error = 0;
  362. u16 status;
  363. if (alen < 0) {
  364. puts("I2C read: addr len < 0\n");
  365. return 1;
  366. }
  367. if (len < 0) {
  368. puts("I2C read: data len < 0\n");
  369. return 1;
  370. }
  371. if (buffer == NULL) {
  372. puts("I2C read: NULL pointer passed\n");
  373. return 1;
  374. }
  375. if (alen > 2) {
  376. printf("I2C read: addr len %d not supported\n", alen);
  377. return 1;
  378. }
  379. if (addr + len > (1 << 16)) {
  380. puts("I2C read: address out of range\n");
  381. return 1;
  382. }
  383. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  384. /*
  385. * EEPROM chips that implement "address overflow" are ones
  386. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  387. * address and the extra bits end up in the "chip address"
  388. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  389. * four 256 byte chips.
  390. *
  391. * Note that we consider the length of the address field to
  392. * still be one byte because the extra address bits are
  393. * hidden in the chip address.
  394. */
  395. if (alen > 0)
  396. chip |= ((addr >> (alen * 8)) &
  397. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  398. #endif
  399. /* Wait until bus not busy */
  400. if (wait_for_bb(i2c_base, waitdelay))
  401. return 1;
  402. /* Zero, one or two bytes reg address (offset) */
  403. writew(alen, &i2c_base->cnt);
  404. /* Set slave address */
  405. writew(chip, &i2c_base->sa);
  406. if (alen) {
  407. /* Must write reg offset first */
  408. #ifdef CONFIG_I2C_REPEATED_START
  409. /* No stop bit, use Repeated Start (Sr) */
  410. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT |
  411. I2C_CON_TRX, &i2c_base->con);
  412. #else
  413. /* Stop - Start (P-S) */
  414. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_STP |
  415. I2C_CON_TRX, &i2c_base->con);
  416. #endif
  417. /* Send register offset */
  418. while (1) {
  419. status = wait_for_event(i2c_base, waitdelay);
  420. /* Try to identify bus that is not padconf'd for I2C */
  421. if (status == I2C_STAT_XRDY) {
  422. i2c_error = 2;
  423. printf("i2c_read (addr phase): pads on bus probably not configured (status=0x%x)\n",
  424. status);
  425. goto rd_exit;
  426. }
  427. if (status == 0 || (status & I2C_STAT_NACK)) {
  428. i2c_error = 1;
  429. printf("i2c_read: error waiting for addr ACK (status=0x%x)\n",
  430. status);
  431. goto rd_exit;
  432. }
  433. if (alen) {
  434. if (status & I2C_STAT_XRDY) {
  435. alen--;
  436. /* Do we have to use byte access? */
  437. writeb((addr >> (8 * alen)) & 0xff,
  438. &i2c_base->data);
  439. writew(I2C_STAT_XRDY, &i2c_base->stat);
  440. }
  441. }
  442. if (status & I2C_STAT_ARDY) {
  443. writew(I2C_STAT_ARDY, &i2c_base->stat);
  444. break;
  445. }
  446. }
  447. }
  448. /* Set slave address */
  449. writew(chip, &i2c_base->sa);
  450. /* Read len bytes from slave */
  451. writew(len, &i2c_base->cnt);
  452. /* Need stop bit here */
  453. writew(I2C_CON_EN | I2C_CON_MST |
  454. I2C_CON_STT | I2C_CON_STP,
  455. &i2c_base->con);
  456. /* Receive data */
  457. while (1) {
  458. status = wait_for_event(i2c_base, waitdelay);
  459. /*
  460. * Try to identify bus that is not padconf'd for I2C. This
  461. * state could be left over from previous transactions if
  462. * the address phase is skipped due to alen=0.
  463. */
  464. if (status == I2C_STAT_XRDY) {
  465. i2c_error = 2;
  466. printf("i2c_read (data phase): pads on bus probably not configured (status=0x%x)\n",
  467. status);
  468. goto rd_exit;
  469. }
  470. if (status == 0 || (status & I2C_STAT_NACK)) {
  471. i2c_error = 1;
  472. goto rd_exit;
  473. }
  474. if (status & I2C_STAT_RRDY) {
  475. *buffer++ = readb(&i2c_base->data);
  476. writew(I2C_STAT_RRDY, &i2c_base->stat);
  477. }
  478. if (status & I2C_STAT_ARDY) {
  479. writew(I2C_STAT_ARDY, &i2c_base->stat);
  480. break;
  481. }
  482. }
  483. rd_exit:
  484. flush_fifo(i2c_base);
  485. writew(0xFFFF, &i2c_base->stat);
  486. return i2c_error;
  487. }
  488. /* i2c_write: Address (reg offset) may be 0, 1 or 2 bytes long. */
  489. static int __omap24_i2c_write(struct i2c *i2c_base, int waitdelay, uchar chip,
  490. uint addr, int alen, uchar *buffer, int len)
  491. {
  492. int i;
  493. u16 status;
  494. int i2c_error = 0;
  495. int timeout = I2C_TIMEOUT;
  496. if (alen < 0) {
  497. puts("I2C write: addr len < 0\n");
  498. return 1;
  499. }
  500. if (len < 0) {
  501. puts("I2C write: data len < 0\n");
  502. return 1;
  503. }
  504. if (buffer == NULL) {
  505. puts("I2C write: NULL pointer passed\n");
  506. return 1;
  507. }
  508. if (alen > 2) {
  509. printf("I2C write: addr len %d not supported\n", alen);
  510. return 1;
  511. }
  512. if (addr + len > (1 << 16)) {
  513. printf("I2C write: address 0x%x + 0x%x out of range\n",
  514. addr, len);
  515. return 1;
  516. }
  517. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  518. /*
  519. * EEPROM chips that implement "address overflow" are ones
  520. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
  521. * address and the extra bits end up in the "chip address"
  522. * bit slots. This makes a 24WC08 (1Kbyte) chip look like
  523. * four 256 byte chips.
  524. *
  525. * Note that we consider the length of the address field to
  526. * still be one byte because the extra address bits are
  527. * hidden in the chip address.
  528. */
  529. if (alen > 0)
  530. chip |= ((addr >> (alen * 8)) &
  531. CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  532. #endif
  533. /* Wait until bus not busy */
  534. if (wait_for_bb(i2c_base, waitdelay))
  535. return 1;
  536. /* Start address phase - will write regoffset + len bytes data */
  537. writew(alen + len, &i2c_base->cnt);
  538. /* Set slave address */
  539. writew(chip, &i2c_base->sa);
  540. /* Stop bit needed here */
  541. writew(I2C_CON_EN | I2C_CON_MST | I2C_CON_STT | I2C_CON_TRX |
  542. I2C_CON_STP, &i2c_base->con);
  543. while (alen) {
  544. /* Must write reg offset (one or two bytes) */
  545. status = wait_for_event(i2c_base, waitdelay);
  546. /* Try to identify bus that is not padconf'd for I2C */
  547. if (status == I2C_STAT_XRDY) {
  548. i2c_error = 2;
  549. printf("i2c_write: pads on bus probably not configured (status=0x%x)\n",
  550. status);
  551. goto wr_exit;
  552. }
  553. if (status == 0 || (status & I2C_STAT_NACK)) {
  554. i2c_error = 1;
  555. printf("i2c_write: error waiting for addr ACK (status=0x%x)\n",
  556. status);
  557. goto wr_exit;
  558. }
  559. if (status & I2C_STAT_XRDY) {
  560. alen--;
  561. writeb((addr >> (8 * alen)) & 0xff, &i2c_base->data);
  562. writew(I2C_STAT_XRDY, &i2c_base->stat);
  563. } else {
  564. i2c_error = 1;
  565. printf("i2c_write: bus not ready for addr Tx (status=0x%x)\n",
  566. status);
  567. goto wr_exit;
  568. }
  569. }
  570. /* Address phase is over, now write data */
  571. for (i = 0; i < len; i++) {
  572. status = wait_for_event(i2c_base, waitdelay);
  573. if (status == 0 || (status & I2C_STAT_NACK)) {
  574. i2c_error = 1;
  575. printf("i2c_write: error waiting for data ACK (status=0x%x)\n",
  576. status);
  577. goto wr_exit;
  578. }
  579. if (status & I2C_STAT_XRDY) {
  580. writeb(buffer[i], &i2c_base->data);
  581. writew(I2C_STAT_XRDY, &i2c_base->stat);
  582. } else {
  583. i2c_error = 1;
  584. printf("i2c_write: bus not ready for data Tx (i=%d)\n",
  585. i);
  586. goto wr_exit;
  587. }
  588. }
  589. /*
  590. * poll ARDY bit for making sure that last byte really has been
  591. * transferred on the bus.
  592. */
  593. do {
  594. status = wait_for_event(i2c_base, waitdelay);
  595. } while (!(status & I2C_STAT_ARDY) && timeout--);
  596. if (timeout <= 0)
  597. printf("i2c_write: timed out writig last byte!\n");
  598. wr_exit:
  599. flush_fifo(i2c_base);
  600. writew(0xFFFF, &i2c_base->stat);
  601. return i2c_error;
  602. }
  603. #ifndef CONFIG_DM_I2C
  604. /*
  605. * The legacy I2C functions. These need to get removed once
  606. * all users of this driver are converted to DM.
  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. static int omap24_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
  639. int alen, uchar *buffer, int len)
  640. {
  641. struct i2c *i2c_base = omap24_get_base(adap);
  642. return __omap24_i2c_read(i2c_base, adap->waitdelay, chip, addr,
  643. alen, buffer, len);
  644. }
  645. static int omap24_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
  646. int alen, uchar *buffer, int len)
  647. {
  648. struct i2c *i2c_base = omap24_get_base(adap);
  649. return __omap24_i2c_write(i2c_base, adap->waitdelay, chip, addr,
  650. alen, buffer, len);
  651. }
  652. static uint omap24_i2c_setspeed(struct i2c_adapter *adap, uint speed)
  653. {
  654. struct i2c *i2c_base = omap24_get_base(adap);
  655. int ret;
  656. ret = __omap24_i2c_setspeed(i2c_base, speed, &adap->waitdelay);
  657. if (ret) {
  658. error("%s: set i2c speed failed\n", __func__);
  659. return ret;
  660. }
  661. adap->speed = speed;
  662. return 0;
  663. }
  664. static void omap24_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
  665. {
  666. struct i2c *i2c_base = omap24_get_base(adap);
  667. return __omap24_i2c_init(i2c_base, speed, slaveadd, &adap->waitdelay);
  668. }
  669. static int omap24_i2c_probe(struct i2c_adapter *adap, uchar chip)
  670. {
  671. struct i2c *i2c_base = omap24_get_base(adap);
  672. return __omap24_i2c_probe(i2c_base, adap->waitdelay, chip);
  673. }
  674. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED1)
  675. #define CONFIG_SYS_OMAP24_I2C_SPEED1 CONFIG_SYS_OMAP24_I2C_SPEED
  676. #endif
  677. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE1)
  678. #define CONFIG_SYS_OMAP24_I2C_SLAVE1 CONFIG_SYS_OMAP24_I2C_SLAVE
  679. #endif
  680. U_BOOT_I2C_ADAP_COMPLETE(omap24_0, omap24_i2c_init, omap24_i2c_probe,
  681. omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
  682. CONFIG_SYS_OMAP24_I2C_SPEED,
  683. CONFIG_SYS_OMAP24_I2C_SLAVE,
  684. 0)
  685. U_BOOT_I2C_ADAP_COMPLETE(omap24_1, omap24_i2c_init, omap24_i2c_probe,
  686. omap24_i2c_read, omap24_i2c_write, omap24_i2c_setspeed,
  687. CONFIG_SYS_OMAP24_I2C_SPEED1,
  688. CONFIG_SYS_OMAP24_I2C_SLAVE1,
  689. 1)
  690. #if (I2C_BUS_MAX > 2)
  691. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED2)
  692. #define CONFIG_SYS_OMAP24_I2C_SPEED2 CONFIG_SYS_OMAP24_I2C_SPEED
  693. #endif
  694. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE2)
  695. #define CONFIG_SYS_OMAP24_I2C_SLAVE2 CONFIG_SYS_OMAP24_I2C_SLAVE
  696. #endif
  697. U_BOOT_I2C_ADAP_COMPLETE(omap24_2, omap24_i2c_init, omap24_i2c_probe,
  698. omap24_i2c_read, omap24_i2c_write, NULL,
  699. CONFIG_SYS_OMAP24_I2C_SPEED2,
  700. CONFIG_SYS_OMAP24_I2C_SLAVE2,
  701. 2)
  702. #if (I2C_BUS_MAX > 3)
  703. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED3)
  704. #define CONFIG_SYS_OMAP24_I2C_SPEED3 CONFIG_SYS_OMAP24_I2C_SPEED
  705. #endif
  706. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE3)
  707. #define CONFIG_SYS_OMAP24_I2C_SLAVE3 CONFIG_SYS_OMAP24_I2C_SLAVE
  708. #endif
  709. U_BOOT_I2C_ADAP_COMPLETE(omap24_3, omap24_i2c_init, omap24_i2c_probe,
  710. omap24_i2c_read, omap24_i2c_write, NULL,
  711. CONFIG_SYS_OMAP24_I2C_SPEED3,
  712. CONFIG_SYS_OMAP24_I2C_SLAVE3,
  713. 3)
  714. #if (I2C_BUS_MAX > 4)
  715. #if !defined(CONFIG_SYS_OMAP24_I2C_SPEED4)
  716. #define CONFIG_SYS_OMAP24_I2C_SPEED4 CONFIG_SYS_OMAP24_I2C_SPEED
  717. #endif
  718. #if !defined(CONFIG_SYS_OMAP24_I2C_SLAVE4)
  719. #define CONFIG_SYS_OMAP24_I2C_SLAVE4 CONFIG_SYS_OMAP24_I2C_SLAVE
  720. #endif
  721. U_BOOT_I2C_ADAP_COMPLETE(omap24_4, omap24_i2c_init, omap24_i2c_probe,
  722. omap24_i2c_read, omap24_i2c_write, NULL,
  723. CONFIG_SYS_OMAP24_I2C_SPEED4,
  724. CONFIG_SYS_OMAP24_I2C_SLAVE4,
  725. 4)
  726. #endif
  727. #endif
  728. #endif
  729. #else /* CONFIG_DM_I2C */
  730. static int omap_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  731. {
  732. struct omap_i2c *priv = dev_get_priv(bus);
  733. int ret;
  734. debug("i2c_xfer: %d messages\n", nmsgs);
  735. for (; nmsgs > 0; nmsgs--, msg++) {
  736. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  737. if (msg->flags & I2C_M_RD) {
  738. ret = __omap24_i2c_read(priv->regs, priv->waitdelay,
  739. msg->addr, 0, 0, msg->buf,
  740. msg->len);
  741. } else {
  742. ret = __omap24_i2c_write(priv->regs, priv->waitdelay,
  743. msg->addr, 0, 0, msg->buf,
  744. msg->len);
  745. }
  746. if (ret) {
  747. debug("i2c_write: error sending\n");
  748. return -EREMOTEIO;
  749. }
  750. }
  751. return 0;
  752. }
  753. static int omap_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  754. {
  755. struct omap_i2c *priv = dev_get_priv(bus);
  756. priv->speed = speed;
  757. return __omap24_i2c_setspeed(priv->regs, speed, &priv->waitdelay);
  758. }
  759. static int omap_i2c_probe_chip(struct udevice *bus, uint chip_addr,
  760. uint chip_flags)
  761. {
  762. struct omap_i2c *priv = dev_get_priv(bus);
  763. return __omap24_i2c_probe(priv->regs, priv->waitdelay, chip_addr);
  764. }
  765. static int omap_i2c_probe(struct udevice *bus)
  766. {
  767. struct omap_i2c *priv = dev_get_priv(bus);
  768. __omap24_i2c_init(priv->regs, priv->speed, 0, &priv->waitdelay);
  769. return 0;
  770. }
  771. static int omap_i2c_ofdata_to_platdata(struct udevice *bus)
  772. {
  773. struct omap_i2c *priv = dev_get_priv(bus);
  774. priv->regs = map_physmem(dev_get_addr(bus), sizeof(void *),
  775. MAP_NOCACHE);
  776. priv->speed = CONFIG_SYS_OMAP24_I2C_SPEED;
  777. return 0;
  778. }
  779. static const struct dm_i2c_ops omap_i2c_ops = {
  780. .xfer = omap_i2c_xfer,
  781. .probe_chip = omap_i2c_probe_chip,
  782. .set_bus_speed = omap_i2c_set_bus_speed,
  783. };
  784. static const struct udevice_id omap_i2c_ids[] = {
  785. { .compatible = "ti,omap4-i2c" },
  786. { }
  787. };
  788. U_BOOT_DRIVER(i2c_omap) = {
  789. .name = "i2c_omap",
  790. .id = UCLASS_I2C,
  791. .of_match = omap_i2c_ids,
  792. .ofdata_to_platdata = omap_i2c_ofdata_to_platdata,
  793. .probe = omap_i2c_probe,
  794. .priv_auto_alloc_size = sizeof(struct omap_i2c),
  795. .ops = &omap_i2c_ops,
  796. .flags = DM_FLAG_PRE_RELOC,
  797. };
  798. #endif /* CONFIG_DM_I2C */