i2c.c 17 KB

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