i2c.c 18 KB

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