omap24xx_i2c.c 24 KB

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