i2c.c 17 KB

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