i2c.c 19 KB

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