omap24xx_i2c.c 24 KB

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