i2c.c 21 KB

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