i2c.c 18 KB

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