i2c.c 19 KB

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