i2c.c 19 KB

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