i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /*
  2. * (C) Copyright 2000
  3. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  4. *
  5. * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  6. * Marius Groeger <mgroeger@sysgo.de>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <console.h>
  12. #if defined(CONFIG_HARD_I2C)
  13. #include <asm/cpm_8260.h>
  14. #include <i2c.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #if defined(CONFIG_I2C_MULTI_BUS)
  17. static unsigned int i2c_bus_num __attribute__ ((section(".data"))) = 0;
  18. #endif /* CONFIG_I2C_MULTI_BUS */
  19. /* uSec to wait between polls of the i2c */
  20. #define DELAY_US 100
  21. /* uSec to wait for the CPM to start processing the buffer */
  22. #define START_DELAY_US 1000
  23. /*
  24. * tx/rx per-byte timeout: we delay DELAY_US uSec between polls so the
  25. * timeout will be (tx_length + rx_length) * DELAY_US * TOUT_LOOP
  26. */
  27. #define TOUT_LOOP 5
  28. /*
  29. * Set default values
  30. */
  31. #ifndef CONFIG_SYS_I2C_SPEED
  32. #define CONFIG_SYS_I2C_SPEED 50000
  33. #endif
  34. typedef void (*i2c_ecb_t) (int, int, void *); /* error callback function */
  35. /* This structure keeps track of the bd and buffer space usage. */
  36. typedef struct i2c_state {
  37. int rx_idx; /* index to next free Rx BD */
  38. int tx_idx; /* index to next free Tx BD */
  39. void *rxbd; /* pointer to next free Rx BD */
  40. void *txbd; /* pointer to next free Tx BD */
  41. int tx_space; /* number of Tx bytes left */
  42. unsigned char *tx_buf; /* pointer to free Tx area */
  43. i2c_ecb_t err_cb; /* error callback function */
  44. void *cb_data; /* private data to be passed */
  45. } i2c_state_t;
  46. /* flags for i2c_send() and i2c_receive() */
  47. #define I2CF_ENABLE_SECONDARY 0x01 /* secondary_address is valid */
  48. #define I2CF_START_COND 0x02 /* tx: generate start condition */
  49. #define I2CF_STOP_COND 0x04 /* tx: generate stop condition */
  50. /* return codes */
  51. #define I2CERR_NO_BUFFERS 1 /* no more BDs or buffer space */
  52. #define I2CERR_MSG_TOO_LONG 2 /* tried to send/receive to much data */
  53. #define I2CERR_TIMEOUT 3 /* timeout in i2c_doio() */
  54. #define I2CERR_QUEUE_EMPTY 4 /* i2c_doio called without send/rcv */
  55. #define I2CERR_IO_ERROR 5 /* had an error during comms */
  56. /* error callback flags */
  57. #define I2CECB_RX_ERR 0x10 /* this is a receive error */
  58. #define I2CECB_RX_OV 0x02 /* receive overrun error */
  59. #define I2CECB_RX_MASK 0x0f /* mask for error bits */
  60. #define I2CECB_TX_ERR 0x20 /* this is a transmit error */
  61. #define I2CECB_TX_CL 0x01 /* transmit collision error */
  62. #define I2CECB_TX_UN 0x02 /* transmit underflow error */
  63. #define I2CECB_TX_NAK 0x04 /* transmit no ack error */
  64. #define I2CECB_TX_MASK 0x0f /* mask for error bits */
  65. #define I2CECB_TIMEOUT 0x40 /* this is a timeout error */
  66. #define ERROR_I2C_NONE 0
  67. #define ERROR_I2C_LENGTH 1
  68. #define I2C_WRITE_BIT 0x00
  69. #define I2C_READ_BIT 0x01
  70. #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
  71. #define NUM_RX_BDS 4
  72. #define NUM_TX_BDS 4
  73. #define MAX_TX_SPACE 256
  74. typedef struct I2C_BD {
  75. unsigned short status;
  76. unsigned short length;
  77. unsigned char *addr;
  78. } I2C_BD;
  79. #define BD_I2C_TX_START 0x0400 /* special status for i2c: Start condition */
  80. #define BD_I2C_TX_CL 0x0001 /* collision error */
  81. #define BD_I2C_TX_UN 0x0002 /* underflow error */
  82. #define BD_I2C_TX_NAK 0x0004 /* no acknowledge error */
  83. #define BD_I2C_TX_ERR (BD_I2C_TX_NAK|BD_I2C_TX_UN|BD_I2C_TX_CL)
  84. #define BD_I2C_RX_ERR BD_SC_OV
  85. /*
  86. * Returns the best value of I2BRG to meet desired clock speed of I2C with
  87. * input parameters (clock speed, filter, and predivider value).
  88. * It returns computer speed value and the difference between it and desired
  89. * speed.
  90. */
  91. static inline int
  92. i2c_roundrate(int hz, int speed, int filter, int modval,
  93. int *brgval, int *totspeed)
  94. {
  95. int moddiv = 1 << (5 - (modval & 3)), brgdiv, div;
  96. debug("\t[I2C] trying hz=%d, speed=%d, filter=%d, modval=%d\n",
  97. hz, speed, filter, modval);
  98. div = moddiv * speed;
  99. brgdiv = (hz + div - 1) / div;
  100. debug("\t\tmoddiv=%d, brgdiv=%d\n", moddiv, brgdiv);
  101. *brgval = ((brgdiv + 1) / 2) - 3 - (2 * filter);
  102. if ((*brgval < 0) || (*brgval > 255)) {
  103. debug("\t\trejected brgval=%d\n", *brgval);
  104. return -1;
  105. }
  106. brgdiv = 2 * (*brgval + 3 + (2 * filter));
  107. div = moddiv * brgdiv;
  108. *totspeed = hz / div;
  109. debug("\t\taccepted brgval=%d, totspeed=%d\n", *brgval, *totspeed);
  110. return 0;
  111. }
  112. /*
  113. * Sets the I2C clock predivider and divider to meet required clock speed.
  114. */
  115. static int i2c_setrate(int hz, int speed)
  116. {
  117. immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  118. volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
  119. int brgval,
  120. modval, /* 0-3 */
  121. bestspeed_diff = speed,
  122. bestspeed_brgval = 0,
  123. bestspeed_modval = 0,
  124. bestspeed_filter = 0,
  125. totspeed,
  126. filter = 0; /* Use this fixed value */
  127. for (modval = 0; modval < 4; modval++) {
  128. if (i2c_roundrate(hz, speed, filter, modval, &brgval, &totspeed)
  129. == 0) {
  130. int diff = speed - totspeed;
  131. if ((diff >= 0) && (diff < bestspeed_diff)) {
  132. bestspeed_diff = diff;
  133. bestspeed_modval = modval;
  134. bestspeed_brgval = brgval;
  135. bestspeed_filter = filter;
  136. }
  137. }
  138. }
  139. debug("[I2C] Best is:\n");
  140. debug("[I2C] CPU=%dhz RATE=%d F=%d I2MOD=%08x I2BRG=%08x DIFF=%dhz\n",
  141. hz, speed, bestspeed_filter, bestspeed_modval, bestspeed_brgval,
  142. bestspeed_diff);
  143. i2c->i2c_i2mod |= ((bestspeed_modval & 3) << 1) |
  144. (bestspeed_filter << 3);
  145. i2c->i2c_i2brg = bestspeed_brgval & 0xff;
  146. debug("[I2C] i2mod=%08x i2brg=%08x\n", i2c->i2c_i2mod,
  147. i2c->i2c_i2brg);
  148. return 1;
  149. }
  150. void i2c_init(int speed, int slaveadd)
  151. {
  152. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  153. volatile cpm8260_t *cp = (cpm8260_t *)&immap->im_cpm;
  154. volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
  155. volatile iic_t *iip;
  156. ulong rbase, tbase;
  157. volatile I2C_BD *rxbd, *txbd;
  158. uint dpaddr;
  159. #ifdef CONFIG_SYS_I2C_INIT_BOARD
  160. /*
  161. * call board specific i2c bus reset routine before accessing the
  162. * environment, which might be in a chip on that bus. For details
  163. * about this problem see doc/I2C_Edge_Conditions.
  164. */
  165. i2c_init_board();
  166. #endif
  167. dpaddr = immap->im_dprambase16[PROFF_I2C_BASE / sizeof(u16)];
  168. if (dpaddr == 0) {
  169. /* need to allocate dual port ram */
  170. dpaddr = m8260_cpm_dpalloc(64 +
  171. (NUM_RX_BDS * sizeof(I2C_BD)) +
  172. (NUM_TX_BDS * sizeof(I2C_BD)) +
  173. MAX_TX_SPACE, 64);
  174. immap->im_dprambase16[PROFF_I2C_BASE / sizeof(u16)] =
  175. dpaddr;
  176. }
  177. /*
  178. * initialise data in dual port ram:
  179. *
  180. * dpaddr -> parameter ram (64 bytes)
  181. * rbase -> rx BD (NUM_RX_BDS * sizeof(I2C_BD) bytes)
  182. * tbase -> tx BD (NUM_TX_BDS * sizeof(I2C_BD) bytes)
  183. * tx buffer (MAX_TX_SPACE bytes)
  184. */
  185. iip = (iic_t *)&immap->im_dprambase[dpaddr];
  186. memset((void *)iip, 0, sizeof(iic_t));
  187. rbase = dpaddr + 64;
  188. tbase = rbase + NUM_RX_BDS * sizeof(I2C_BD);
  189. /* Disable interrupts */
  190. i2c->i2c_i2mod = 0x00;
  191. i2c->i2c_i2cmr = 0x00;
  192. i2c->i2c_i2cer = 0xff;
  193. i2c->i2c_i2add = slaveadd;
  194. /*
  195. * Set the I2C BRG Clock division factor from desired i2c rate
  196. * and current CPU rate (we assume sccr dfbgr field is 0;
  197. * divide BRGCLK by 1)
  198. */
  199. debug("[I2C] Setting rate...\n");
  200. i2c_setrate(gd->arch.brg_clk, CONFIG_SYS_I2C_SPEED);
  201. /* Set I2C controller in master mode */
  202. i2c->i2c_i2com = 0x01;
  203. /* Initialize Tx/Rx parameters */
  204. iip->iic_rbase = rbase;
  205. iip->iic_tbase = tbase;
  206. rxbd = (I2C_BD *)((unsigned char *) &immap->
  207. im_dprambase[iip->iic_rbase]);
  208. txbd = (I2C_BD *)((unsigned char *) &immap->
  209. im_dprambase[iip->iic_tbase]);
  210. debug("[I2C] rbase = %04x\n", iip->iic_rbase);
  211. debug("[I2C] tbase = %04x\n", iip->iic_tbase);
  212. debug("[I2C] rxbd = %08x\n", (int) rxbd);
  213. debug("[I2C] txbd = %08x\n", (int) txbd);
  214. /* Set big endian byte order */
  215. iip->iic_tfcr = 0x10;
  216. iip->iic_rfcr = 0x10;
  217. /* Set maximum receive size. */
  218. iip->iic_mrblr = I2C_RXTX_LEN;
  219. cp->cp_cpcr = mk_cr_cmd(CPM_CR_I2C_PAGE,
  220. CPM_CR_I2C_SBLOCK,
  221. 0x00, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  222. do {
  223. __asm__ __volatile__("eieio");
  224. } while (cp->cp_cpcr & CPM_CR_FLG);
  225. /* Clear events and interrupts */
  226. i2c->i2c_i2cer = 0xff;
  227. i2c->i2c_i2cmr = 0x00;
  228. }
  229. static
  230. void i2c_newio(i2c_state_t *state)
  231. {
  232. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  233. volatile iic_t *iip;
  234. uint dpaddr;
  235. debug("[I2C] i2c_newio\n");
  236. dpaddr = immap->im_dprambase16[PROFF_I2C_BASE / sizeof(u16)];
  237. iip = (iic_t *)&immap->im_dprambase[dpaddr];
  238. state->rx_idx = 0;
  239. state->tx_idx = 0;
  240. state->rxbd = (void *)&immap->im_dprambase[iip->iic_rbase];
  241. state->txbd = (void *)&immap->im_dprambase[iip->iic_tbase];
  242. state->tx_space = MAX_TX_SPACE;
  243. state->tx_buf = (uchar *)state->txbd + NUM_TX_BDS * sizeof(I2C_BD);
  244. state->err_cb = NULL;
  245. state->cb_data = NULL;
  246. debug("[I2C] rxbd = %08x\n", (int)state->rxbd);
  247. debug("[I2C] txbd = %08x\n", (int)state->txbd);
  248. debug("[I2C] tx_buf = %08x\n", (int)state->tx_buf);
  249. /* clear the buffer memory */
  250. memset((char *) state->tx_buf, 0, MAX_TX_SPACE);
  251. }
  252. static
  253. int i2c_send(i2c_state_t *state,
  254. unsigned char address,
  255. unsigned char secondary_address,
  256. unsigned int flags, unsigned short size, unsigned char *dataout)
  257. {
  258. volatile I2C_BD *txbd;
  259. int i, j;
  260. debug("[I2C] i2c_send add=%02d sec=%02d flag=%02d size=%d\n",
  261. address, secondary_address, flags, size);
  262. /* trying to send message larger than BD */
  263. if (size > I2C_RXTX_LEN)
  264. return I2CERR_MSG_TOO_LONG;
  265. /* no more free bds */
  266. if (state->tx_idx >= NUM_TX_BDS || state->tx_space < (2 + size))
  267. return I2CERR_NO_BUFFERS;
  268. txbd = (I2C_BD *)state->txbd;
  269. txbd->addr = state->tx_buf;
  270. debug("[I2C] txbd = %08x\n", (int) txbd);
  271. if (flags & I2CF_START_COND) {
  272. debug("[I2C] Formatting addresses...\n");
  273. if (flags & I2CF_ENABLE_SECONDARY) {
  274. /* Length of message plus dest addresses */
  275. txbd->length = size + 2;
  276. txbd->addr[0] = address << 1;
  277. txbd->addr[1] = secondary_address;
  278. i = 2;
  279. } else {
  280. /* Length of message plus dest address */
  281. txbd->length = size + 1;
  282. /* Write destination address to BD */
  283. txbd->addr[0] = address << 1;
  284. i = 1;
  285. }
  286. } else {
  287. txbd->length = size; /* Length of message */
  288. i = 0;
  289. }
  290. /* set up txbd */
  291. txbd->status = BD_SC_READY;
  292. if (flags & I2CF_START_COND)
  293. txbd->status |= BD_I2C_TX_START;
  294. if (flags & I2CF_STOP_COND)
  295. txbd->status |= BD_SC_LAST | BD_SC_WRAP;
  296. /* Copy data to send into buffer */
  297. debug("[I2C] copy data...\n");
  298. for (j = 0; j < size; i++, j++)
  299. txbd->addr[i] = dataout[j];
  300. debug("[I2C] txbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
  301. txbd->length, txbd->status, txbd->addr[0], txbd->addr[1]);
  302. /* advance state */
  303. state->tx_buf += txbd->length;
  304. state->tx_space -= txbd->length;
  305. state->tx_idx++;
  306. state->txbd = (void *) (txbd + 1);
  307. return 0;
  308. }
  309. static
  310. int i2c_receive(i2c_state_t *state,
  311. unsigned char address,
  312. unsigned char secondary_address,
  313. unsigned int flags,
  314. unsigned short size_to_expect, unsigned char *datain)
  315. {
  316. volatile I2C_BD *rxbd, *txbd;
  317. debug("[I2C] i2c_receive %02d %02d %02d\n", address,
  318. secondary_address, flags);
  319. /* Expected to receive too much */
  320. if (size_to_expect > I2C_RXTX_LEN)
  321. return I2CERR_MSG_TOO_LONG;
  322. /* no more free bds */
  323. if (state->tx_idx >= NUM_TX_BDS || state->rx_idx >= NUM_RX_BDS
  324. || state->tx_space < 2)
  325. return I2CERR_NO_BUFFERS;
  326. rxbd = (I2C_BD *) state->rxbd;
  327. txbd = (I2C_BD *) state->txbd;
  328. debug("[I2C] rxbd = %08x\n", (int) rxbd);
  329. debug("[I2C] txbd = %08x\n", (int) txbd);
  330. txbd->addr = state->tx_buf;
  331. /* set up TXBD for destination address */
  332. if (flags & I2CF_ENABLE_SECONDARY) {
  333. txbd->length = 2;
  334. txbd->addr[0] = address << 1; /* Write data */
  335. txbd->addr[1] = secondary_address; /* Internal address */
  336. txbd->status = BD_SC_READY;
  337. } else {
  338. txbd->length = 1 + size_to_expect;
  339. txbd->addr[0] = (address << 1) | 0x01;
  340. txbd->status = BD_SC_READY;
  341. memset(&txbd->addr[1], 0, txbd->length);
  342. }
  343. /* set up rxbd for reception */
  344. rxbd->status = BD_SC_EMPTY;
  345. rxbd->length = size_to_expect;
  346. rxbd->addr = datain;
  347. txbd->status |= BD_I2C_TX_START;
  348. if (flags & I2CF_STOP_COND) {
  349. txbd->status |= BD_SC_LAST | BD_SC_WRAP;
  350. rxbd->status |= BD_SC_WRAP;
  351. }
  352. debug("[I2C] txbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
  353. txbd->length, txbd->status, txbd->addr[0], txbd->addr[1]);
  354. debug("[I2C] rxbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
  355. rxbd->length, rxbd->status, rxbd->addr[0], rxbd->addr[1]);
  356. /* advance state */
  357. state->tx_buf += txbd->length;
  358. state->tx_space -= txbd->length;
  359. state->tx_idx++;
  360. state->txbd = (void *) (txbd + 1);
  361. state->rx_idx++;
  362. state->rxbd = (void *) (rxbd + 1);
  363. return 0;
  364. }
  365. static
  366. int i2c_doio(i2c_state_t *state)
  367. {
  368. volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
  369. volatile iic_t *iip;
  370. volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
  371. volatile I2C_BD *txbd, *rxbd;
  372. int n, i, b, rxcnt = 0, rxtimeo = 0, txcnt = 0, txtimeo = 0, rc = 0;
  373. uint dpaddr;
  374. debug("[I2C] i2c_doio\n");
  375. if (state->tx_idx <= 0 && state->rx_idx <= 0) {
  376. debug("[I2C] No I/O is queued\n");
  377. return I2CERR_QUEUE_EMPTY;
  378. }
  379. dpaddr = immap->im_dprambase16[PROFF_I2C_BASE / sizeof(u16)];
  380. iip = (iic_t *)&immap->im_dprambase[dpaddr];
  381. iip->iic_rbptr = iip->iic_rbase;
  382. iip->iic_tbptr = iip->iic_tbase;
  383. /* Enable I2C */
  384. debug("[I2C] Enabling I2C...\n");
  385. i2c->i2c_i2mod |= 0x01;
  386. /* Begin transmission */
  387. i2c->i2c_i2com |= 0x80;
  388. /* Loop until transmit & receive completed */
  389. n = state->tx_idx;
  390. if (n > 0) {
  391. txbd = ((I2C_BD *) state->txbd) - n;
  392. for (i = 0; i < n; i++) {
  393. txtimeo += TOUT_LOOP * txbd->length;
  394. txbd++;
  395. }
  396. txbd--; /* wait until last in list is done */
  397. debug("[I2C] Transmitting...(txbd=0x%08lx)\n",
  398. (ulong) txbd);
  399. udelay(START_DELAY_US); /* give it time to start */
  400. while ((txbd->status & BD_SC_READY) && (++txcnt < txtimeo)) {
  401. udelay(DELAY_US);
  402. if (ctrlc())
  403. return -1;
  404. __asm__ __volatile__("eieio");
  405. }
  406. }
  407. n = state->rx_idx;
  408. if (txcnt < txtimeo && n > 0) {
  409. rxbd = ((I2C_BD *) state->rxbd) - n;
  410. for (i = 0; i < n; i++) {
  411. rxtimeo += TOUT_LOOP * rxbd->length;
  412. rxbd++;
  413. }
  414. rxbd--; /* wait until last in list is done */
  415. debug("[I2C] Receiving...(rxbd=0x%08lx)\n", (ulong) rxbd);
  416. udelay(START_DELAY_US); /* give it time to start */
  417. while ((rxbd->status & BD_SC_EMPTY) && (++rxcnt < rxtimeo)) {
  418. udelay(DELAY_US);
  419. if (ctrlc())
  420. return -1;
  421. __asm__ __volatile__("eieio");
  422. }
  423. }
  424. /* Turn off I2C */
  425. i2c->i2c_i2mod &= ~0x01;
  426. n = state->tx_idx;
  427. if (n > 0) {
  428. for (i = 0; i < n; i++) {
  429. txbd = ((I2C_BD *) state->txbd) - (n - i);
  430. b = txbd->status & BD_I2C_TX_ERR;
  431. if (b != 0) {
  432. if (state->err_cb != NULL)
  433. (*state->err_cb) (I2CECB_TX_ERR | b,
  434. i, state->cb_data);
  435. if (rc == 0)
  436. rc = I2CERR_IO_ERROR;
  437. }
  438. }
  439. }
  440. n = state->rx_idx;
  441. if (n > 0) {
  442. for (i = 0; i < n; i++) {
  443. rxbd = ((I2C_BD *) state->rxbd) - (n - i);
  444. b = rxbd->status & BD_I2C_RX_ERR;
  445. if (b != 0) {
  446. if (state->err_cb != NULL)
  447. (*state->err_cb) (I2CECB_RX_ERR | b,
  448. i, state->cb_data);
  449. if (rc == 0)
  450. rc = I2CERR_IO_ERROR;
  451. }
  452. }
  453. }
  454. if ((txtimeo > 0 && txcnt >= txtimeo) ||
  455. (rxtimeo > 0 && rxcnt >= rxtimeo)) {
  456. if (state->err_cb != NULL)
  457. (*state->err_cb) (I2CECB_TIMEOUT, -1, state->cb_data);
  458. if (rc == 0)
  459. rc = I2CERR_TIMEOUT;
  460. }
  461. return rc;
  462. }
  463. static void i2c_probe_callback(int flags, int xnum, void *data)
  464. {
  465. /*
  466. * the only acceptable errors are a transmit NAK or a receive
  467. * overrun - tx NAK means the device does not exist, rx OV
  468. * means the device must have responded to the slave address
  469. * even though the transfer failed
  470. */
  471. if (flags == (I2CECB_TX_ERR | I2CECB_TX_NAK))
  472. *(int *) data |= 1;
  473. if (flags == (I2CECB_RX_ERR | I2CECB_RX_OV))
  474. *(int *) data |= 2;
  475. }
  476. int i2c_probe(uchar chip)
  477. {
  478. i2c_state_t state;
  479. int rc, err_flag;
  480. uchar buf[1];
  481. i2c_newio(&state);
  482. state.err_cb = i2c_probe_callback;
  483. state.cb_data = (void *) &err_flag;
  484. err_flag = 0;
  485. rc = i2c_receive(&state, chip, 0, I2CF_START_COND | I2CF_STOP_COND, 1,
  486. buf);
  487. if (rc != 0)
  488. return rc; /* probe failed */
  489. rc = i2c_doio(&state);
  490. if (rc == 0)
  491. return 0; /* device exists - read succeeded */
  492. if (rc == I2CERR_TIMEOUT)
  493. return -1; /* device does not exist - timeout */
  494. if (rc != I2CERR_IO_ERROR || err_flag == 0)
  495. return rc; /* probe failed */
  496. if (err_flag & 1)
  497. return -1; /* device does not exist - had transmit NAK */
  498. return 0; /* device exists - had receive overrun */
  499. }
  500. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
  501. {
  502. i2c_state_t state;
  503. uchar xaddr[4];
  504. int rc;
  505. xaddr[0] = (addr >> 24) & 0xFF;
  506. xaddr[1] = (addr >> 16) & 0xFF;
  507. xaddr[2] = (addr >> 8) & 0xFF;
  508. xaddr[3] = addr & 0xFF;
  509. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  510. /*
  511. * EEPROM chips that implement "address overflow" are ones
  512. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of address
  513. * and the extra bits end up in the "chip address" bit slots.
  514. * This makes a 24WC08 (1Kbyte) chip look like four 256 byte
  515. * chips.
  516. *
  517. * Note that we consider the length of the address field to still
  518. * be one byte because the extra address bits are hidden in the
  519. * chip address.
  520. */
  521. chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  522. #endif
  523. i2c_newio(&state);
  524. rc = i2c_send(&state, chip, 0, I2CF_START_COND, alen,
  525. &xaddr[4 - alen]);
  526. if (rc != 0) {
  527. printf("i2c_read: i2c_send failed (%d)\n", rc);
  528. return 1;
  529. }
  530. rc = i2c_receive(&state, chip, 0, I2CF_STOP_COND, len, buffer);
  531. if (rc != 0) {
  532. printf("i2c_read: i2c_receive failed (%d)\n", rc);
  533. return 1;
  534. }
  535. rc = i2c_doio(&state);
  536. if (rc != 0) {
  537. printf("i2c_read: i2c_doio failed (%d)\n", rc);
  538. return 1;
  539. }
  540. return 0;
  541. }
  542. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
  543. {
  544. i2c_state_t state;
  545. uchar xaddr[4];
  546. int rc;
  547. xaddr[0] = (addr >> 24) & 0xFF;
  548. xaddr[1] = (addr >> 16) & 0xFF;
  549. xaddr[2] = (addr >> 8) & 0xFF;
  550. xaddr[3] = addr & 0xFF;
  551. #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
  552. /*
  553. * EEPROM chips that implement "address overflow" are ones
  554. * like Catalyst 24WC04/08/16 which has 9/10/11 bits of address
  555. * and the extra bits end up in the "chip address" bit slots.
  556. * This makes a 24WC08 (1Kbyte) chip look like four 256 byte
  557. * chips.
  558. *
  559. * Note that we consider the length of the address field to still
  560. * be one byte because the extra address bits are hidden in the
  561. * chip address.
  562. */
  563. chip |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
  564. #endif
  565. i2c_newio(&state);
  566. rc = i2c_send(&state, chip, 0, I2CF_START_COND, alen,
  567. &xaddr[4 - alen]);
  568. if (rc != 0) {
  569. printf("i2c_write: first i2c_send failed (%d)\n", rc);
  570. return 1;
  571. }
  572. rc = i2c_send(&state, 0, 0, I2CF_STOP_COND, len, buffer);
  573. if (rc != 0) {
  574. printf("i2c_write: second i2c_send failed (%d)\n", rc);
  575. return 1;
  576. }
  577. rc = i2c_doio(&state);
  578. if (rc != 0) {
  579. printf("i2c_write: i2c_doio failed (%d)\n", rc);
  580. return 1;
  581. }
  582. return 0;
  583. }
  584. #if defined(CONFIG_I2C_MULTI_BUS)
  585. /*
  586. * Functions for multiple I2C bus handling
  587. */
  588. unsigned int i2c_get_bus_num(void)
  589. {
  590. return i2c_bus_num;
  591. }
  592. int i2c_set_bus_num(unsigned int bus)
  593. {
  594. if (bus >= CONFIG_SYS_MAX_I2C_BUS)
  595. return -1;
  596. i2c_bus_num = bus;
  597. return 0;
  598. }
  599. #endif /* CONFIG_I2C_MULTI_BUS */
  600. #endif /* CONFIG_HARD_I2C */