i2c.c 20 KB

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