3 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
5 * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Marius Groeger <mgroeger@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #if defined(CONFIG_HARD_I2C)
31 #include <asm/cpm_8260.h>
34 /* define to enable debug messages */
37 /* uSec to wait between polls of the i2c */
39 /* uSec to wait for the CPM to start processing the buffer */
40 #define START_DELAY_US 1000
43 * tx/rx per-byte timeout: we delay DELAY_US uSec between polls so the
44 * timeout will be (tx_length + rx_length) * DELAY_US * TOUT_LOOP
48 /*-----------------------------------------------------------------------
52 #define CFG_I2C_SPEED 50000
56 #define CFG_I2C_SLAVE 0xFE
58 /*-----------------------------------------------------------------------
61 typedef void (*i2c_ecb_t)(int, int); /* error callback function */
63 /* This structure keeps track of the bd and buffer space usage. */
64 typedef struct i2c_state {
65 int rx_idx; /* index to next free Rx BD */
66 int tx_idx; /* index to next free Tx BD */
67 void *rxbd; /* pointer to next free Rx BD */
68 void *txbd; /* pointer to next free Tx BD */
69 int tx_space; /* number of Tx bytes left */
70 unsigned char *tx_buf; /* pointer to free Tx area */
71 i2c_ecb_t err_cb; /* error callback function */
74 /* flags for i2c_send() and i2c_receive() */
75 #define I2CF_ENABLE_SECONDARY 0x01 /* secondary_address is valid */
76 #define I2CF_START_COND 0x02 /* tx: generate start condition */
77 #define I2CF_STOP_COND 0x04 /* tx: generate stop condition */
80 #define I2CERR_NO_BUFFERS 0x01 /* no more BDs or buffer space */
81 #define I2CERR_MSG_TOO_LONG 0x02 /* tried to send/receive to much data */
82 #define I2CERR_TIMEOUT 0x03 /* timeout in i2c_doio() */
83 #define I2CERR_QUEUE_EMPTY 0x04 /* i2c_doio called without send/receive */
85 /* error callback flags */
86 #define I2CECB_RX_ERR 0x10 /* this is a receive error */
87 #define I2CECB_RX_ERR_OV 0x02 /* receive overrun error */
88 #define I2CECB_RX_MASK 0x0f /* mask for error bits */
89 #define I2CECB_TX_ERR 0x20 /* this is a transmit error */
90 #define I2CECB_TX_CL 0x01 /* transmit collision error */
91 #define I2CECB_TX_UN 0x02 /* transmit underflow error */
92 #define I2CECB_TX_NAK 0x04 /* transmit no ack error */
93 #define I2CECB_TX_MASK 0x0f /* mask for error bits */
94 #define I2CECB_TIMEOUT 0x40 /* this is a timeout error */
96 #define ERROR_I2C_NONE 0
97 #define ERROR_I2C_LENGTH 1
99 #define I2C_WRITE_BIT 0x00
100 #define I2C_READ_BIT 0x01
102 #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
107 #define MAX_TX_SPACE 256
109 typedef struct I2C_BD
111 unsigned short status;
112 unsigned short length;
115 #define BD_I2C_TX_START 0x0400 /* special status for i2c: Start condition */
117 #define BD_I2C_TX_CL 0x0001 /* collision error */
118 #define BD_I2C_TX_UN 0x0002 /* underflow error */
119 #define BD_I2C_TX_NAK 0x0004 /* no acknowledge error */
120 #define BD_I2C_TX_ERR (BD_I2C_TX_NAK|BD_I2C_TX_UN|BD_I2C_TX_CL)
122 #define BD_I2C_RX_ERR BD_SC_OV
125 #define PRINTD(x) printf x
131 * Returns the best value of I2BRG to meet desired clock speed of I2C with
132 * input parameters (clock speed, filter, and predivider value).
133 * It returns computer speed value and the difference between it and desired
137 i2c_roundrate(int hz, int speed, int filter, int modval,
138 int *brgval, int *totspeed)
140 int moddiv = 1 << (5-(modval & 3)), brgdiv, div;
142 PRINTD(("\t[I2C] trying hz=%d, speed=%d, filter=%d, modval=%d\n",
143 hz, speed, filter, modval));
145 div = moddiv * speed;
146 brgdiv = (hz + div - 1) / div;
148 PRINTD(("\t\tmoddiv=%d, brgdiv=%d\n", moddiv, brgdiv));
150 *brgval = (brgdiv / 2) - 3 - (2*filter);
152 if ((*brgval < 0) || (*brgval > 255)) {
153 PRINTD(("\t\trejected brgval=%d\n", *brgval));
157 brgdiv = 2 * (*brgval + 3 + (2 * filter));
158 div = moddiv * brgdiv ;
159 *totspeed = (hz + div - 1) / div;
161 PRINTD(("\t\taccepted brgval=%d, totspeed=%d\n", *brgval, *totspeed));
167 * Sets the I2C clock predivider and divider to meet required clock speed.
169 static int i2c_setrate(int hz, int speed)
171 immap_t *immap = (immap_t *)CFG_IMMR ;
172 volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
175 bestspeed_diff = speed,
180 filter = 0; /* Use this fixed value */
182 for (modval = 0; modval < 4; modval++)
184 if (i2c_roundrate (hz, speed, filter, modval, &brgval, &totspeed) == 0)
186 int diff = speed - totspeed ;
188 if ((diff >= 0) && (diff < bestspeed_diff))
190 bestspeed_diff = diff ;
191 bestspeed_modval = modval;
192 bestspeed_brgval = brgval;
193 bestspeed_filter = filter;
198 PRINTD(("[I2C] Best is:\n"));
199 PRINTD(("[I2C] CPU=%dhz RATE=%d F=%d I2MOD=%08x I2BRG=%08x DIFF=%dhz\n",
201 bestspeed_filter, bestspeed_modval, bestspeed_brgval,
204 i2c->i2c_i2mod |= ((bestspeed_modval & 3) << 1) | (bestspeed_filter << 3);
205 i2c->i2c_i2brg = bestspeed_brgval & 0xff;
207 PRINTD(("[I2C] i2mod=%08x i2brg=%08x\n", i2c->i2c_i2mod, i2c->i2c_i2brg));
212 void i2c_init(int speed, int slaveadd)
214 DECLARE_GLOBAL_DATA_PTR;
216 volatile immap_t *immap = (immap_t *)CFG_IMMR ;
217 volatile cpm8260_t *cp = (cpm8260_t *)&immap->im_cpm;
218 volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
221 volatile I2C_BD *rxbd, *txbd;
224 #ifdef CFG_I2C_INIT_BOARD
225 /* call board specific i2c bus reset routine before accessing the */
226 /* environment, which might be in a chip on that bus. For details */
227 /* about this problem see doc/I2C_Edge_Conditions. */
231 dpaddr = *((unsigned short*)(&immap->im_dprambase[PROFF_I2C_BASE]));
233 /* need to allocate dual port ram */
234 dpaddr = m8260_cpm_dpalloc(64 +
235 (NUM_RX_BDS * sizeof(I2C_BD)) + (NUM_TX_BDS * sizeof(I2C_BD)) +
237 *((unsigned short*)(&immap->im_dprambase[PROFF_I2C_BASE])) = dpaddr;
241 * initialise data in dual port ram:
243 * dpaddr -> parameter ram (64 bytes)
244 * rbase -> rx BD (NUM_RX_BDS * sizeof(I2C_BD) bytes)
245 * tbase -> tx BD (NUM_TX_BDS * sizeof(I2C_BD) bytes)
246 * tx buffer (MAX_TX_SPACE bytes)
249 iip = (iic_t *)&immap->im_dprambase[dpaddr];
250 memset((void*)iip, 0, sizeof(iic_t));
253 tbase = rbase + NUM_RX_BDS * sizeof(I2C_BD);
255 /* Disable interrupts */
256 i2c->i2c_i2mod = 0x00;
257 i2c->i2c_i2cmr = 0x00;
258 i2c->i2c_i2cer = 0xff;
259 i2c->i2c_i2add = slaveadd;
262 * Set the I2C BRG Clock division factor from desired i2c rate
263 * and current CPU rate (we assume sccr dfbgr field is 0;
264 * divide BRGCLK by 1)
266 PRINTD(("[I2C] Setting rate...\n"));
267 i2c_setrate (gd->brg_clk, CFG_I2C_SPEED) ;
269 /* Set I2C controller in master mode */
270 i2c->i2c_i2com = 0x01;
272 /* Initialize Tx/Rx parameters */
273 iip->iic_rbase = rbase;
274 iip->iic_tbase = tbase;
275 rxbd = (I2C_BD *)((unsigned char *)&immap->im_dprambase[iip->iic_rbase]);
276 txbd = (I2C_BD *)((unsigned char *)&immap->im_dprambase[iip->iic_tbase]);
278 PRINTD(("[I2C] rbase = %04x\n", iip->iic_rbase));
279 PRINTD(("[I2C] tbase = %04x\n", iip->iic_tbase));
280 PRINTD(("[I2C] rxbd = %08x\n", (int)rxbd));
281 PRINTD(("[I2C] txbd = %08x\n", (int)txbd));
283 /* Set big endian byte order */
284 iip->iic_tfcr = 0x10;
285 iip->iic_rfcr = 0x10;
287 /* Set maximum receive size. */
288 iip->iic_mrblr = I2C_RXTX_LEN;
290 cp->cp_cpcr = mk_cr_cmd(CPM_CR_I2C_PAGE,
293 CPM_CR_INIT_TRX) | CPM_CR_FLG;
295 __asm__ __volatile__ ("eieio");
296 } while (cp->cp_cpcr & CPM_CR_FLG);
298 /* Clear events and interrupts */
299 i2c->i2c_i2cer = 0xff;
300 i2c->i2c_i2cmr = 0x00;
304 void i2c_newio(i2c_state_t *state)
306 volatile immap_t *immap = (immap_t *)CFG_IMMR ;
310 PRINTD(("[I2C] i2c_newio\n"));
312 dpaddr = *((unsigned short*)(&immap->im_dprambase[PROFF_I2C_BASE]));
313 iip = (iic_t *)&immap->im_dprambase[dpaddr];
316 state->rxbd = (void*)&immap->im_dprambase[iip->iic_rbase];
317 state->txbd = (void*)&immap->im_dprambase[iip->iic_tbase];
318 state->tx_space = MAX_TX_SPACE;
319 state->tx_buf = (uchar*)state->txbd + NUM_TX_BDS * sizeof(I2C_BD);
320 state->err_cb = NULL;
322 PRINTD(("[I2C] rxbd = %08x\n", (int)state->rxbd));
323 PRINTD(("[I2C] txbd = %08x\n", (int)state->txbd));
324 PRINTD(("[I2C] tx_buf = %08x\n", (int)state->tx_buf));
326 /* clear the buffer memory */
327 memset((char *)state->tx_buf, 0, MAX_TX_SPACE);
331 int i2c_send(i2c_state_t *state,
332 unsigned char address,
333 unsigned char secondary_address,
336 unsigned char *dataout)
338 volatile I2C_BD *txbd;
341 PRINTD(("[I2C] i2c_send add=%02d sec=%02d flag=%02d size=%d\n",
342 address, secondary_address, flags, size));
344 /* trying to send message larger than BD */
345 if (size > I2C_RXTX_LEN)
346 return I2CERR_MSG_TOO_LONG;
348 /* no more free bds */
349 if (state->tx_idx >= NUM_TX_BDS || state->tx_space < (2 + size))
350 return I2CERR_NO_BUFFERS;
352 txbd = (I2C_BD *)state->txbd;
353 txbd->addr = state->tx_buf;
355 PRINTD(("[I2C] txbd = %08x\n", (int)txbd));
357 if (flags & I2CF_START_COND)
359 PRINTD(("[I2C] Formatting addresses...\n"));
360 if (flags & I2CF_ENABLE_SECONDARY)
362 txbd->length = size + 2; /* Length of message plus dest addresses */
363 txbd->addr[0] = address << 1;
364 txbd->addr[1] = secondary_address;
369 txbd->length = size + 1; /* Length of message plus dest address */
370 txbd->addr[0] = address << 1; /* Write destination address to BD */
376 txbd->length = size; /* Length of message */
381 txbd->status = BD_SC_READY;
382 if (flags & I2CF_START_COND)
383 txbd->status |= BD_I2C_TX_START;
384 if (flags & I2CF_STOP_COND)
385 txbd->status |= BD_SC_LAST | BD_SC_WRAP;
387 /* Copy data to send into buffer */
388 PRINTD(("[I2C] copy data...\n"));
389 for(j = 0; j < size; i++, j++)
390 txbd->addr[i] = dataout[j];
392 PRINTD(("[I2C] txbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
399 state->tx_buf += txbd->length;
400 state->tx_space -= txbd->length;
402 state->txbd = (void*)(txbd + 1);
408 int i2c_receive(i2c_state_t *state,
409 unsigned char address,
410 unsigned char secondary_address,
412 unsigned short size_to_expect,
413 unsigned char *datain)
415 volatile I2C_BD *rxbd, *txbd;
417 PRINTD(("[I2C] i2c_receive %02d %02d %02d\n", address, secondary_address, flags));
419 /* Expected to receive too much */
420 if (size_to_expect > I2C_RXTX_LEN)
421 return I2CERR_MSG_TOO_LONG;
423 /* no more free bds */
424 if (state->tx_idx >= NUM_TX_BDS || state->rx_idx >= NUM_RX_BDS
425 || state->tx_space < 2)
426 return I2CERR_NO_BUFFERS;
428 rxbd = (I2C_BD *)state->rxbd;
429 txbd = (I2C_BD *)state->txbd;
431 PRINTD(("[I2C] rxbd = %08x\n", (int)rxbd));
432 PRINTD(("[I2C] txbd = %08x\n", (int)txbd));
434 txbd->addr = state->tx_buf;
436 /* set up TXBD for destination address */
437 if (flags & I2CF_ENABLE_SECONDARY)
440 txbd->addr[0] = address << 1; /* Write data */
441 txbd->addr[1] = secondary_address; /* Internal address */
442 txbd->status = BD_SC_READY;
446 txbd->length = 1 + size_to_expect;
447 txbd->addr[0] = (address << 1) | 0x01;
448 txbd->status = BD_SC_READY;
449 memset(&txbd->addr[1], 0, txbd->length);
452 /* set up rxbd for reception */
453 rxbd->status = BD_SC_EMPTY;
454 rxbd->length = size_to_expect;
457 txbd->status |= BD_I2C_TX_START;
458 if (flags & I2CF_STOP_COND)
460 txbd->status |= BD_SC_LAST | BD_SC_WRAP;
461 rxbd->status |= BD_SC_WRAP;
464 PRINTD(("[I2C] txbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
469 PRINTD(("[I2C] rxbd: length=0x%04x status=0x%04x addr[0]=0x%02x addr[1]=0x%02x\n",
476 state->tx_buf += txbd->length;
477 state->tx_space -= txbd->length;
479 state->txbd = (void*)(txbd + 1);
481 state->rxbd = (void*)(rxbd + 1);
488 int i2c_doio(i2c_state_t *state)
490 volatile immap_t *immap = (immap_t *)CFG_IMMR ;
492 volatile i2c8260_t *i2c = (i2c8260_t *)&immap->im_i2c;
493 volatile I2C_BD *txbd, *rxbd;
498 PRINTD(("[I2C] i2c_doio\n"));
500 timeout = TOUT_LOOP * 256; /* arbitrarily long */
502 if (state->tx_idx <= 0 && state->rx_idx <= 0) {
503 PRINTD(("[I2C] No I/O is queued\n"));
504 return I2CERR_QUEUE_EMPTY;
507 dpaddr = *((unsigned short*)(&immap->im_dprambase[PROFF_I2C_BASE]));
508 iip = (iic_t *)&immap->im_dprambase[dpaddr];
509 iip->iic_rbptr = iip->iic_rbase;
510 iip->iic_tbptr = iip->iic_tbase;
513 PRINTD(("[I2C] Enabling I2C...\n"));
514 i2c->i2c_i2mod |= 0x01;
516 /* Begin transmission */
517 i2c->i2c_i2com |= 0x80;
519 /* Loop until transmit & receive completed */
521 txbd = ((I2C_BD*)state->txbd) - 1;
523 if (state->tx_idx > 0) {
524 timeout = TOUT_LOOP * txbd->length;
526 PRINTD(("[I2C] Transmitting...(txbd=0x%08lx)\n", (ulong)txbd));
527 udelay(START_DELAY_US); /* give it time to start */
528 while((txbd->status & BD_SC_READY) && (j++ < timeout)) {
532 __asm__ __volatile__ ("eieio");
536 rxbd = ((I2C_BD*)state->rxbd) - 1;
538 if ((state->rx_idx > 0) && (j < timeout)) {
539 timeout = TOUT_LOOP * rxbd->length;
540 PRINTD(("[I2C] Receiving...(rxbd=0x%08lx)\n", (ulong)rxbd));
541 udelay(START_DELAY_US); /* give it time to start */
542 while((rxbd->status & BD_SC_EMPTY) && (j++ < timeout)) {
546 __asm__ __volatile__ ("eieio");
551 i2c->i2c_i2mod &= ~0x01;
553 if (state->err_cb != NULL) {
557 * if we have an error callback function, look at the
558 * error bits in the bd status and pass them back
561 if ((n = state->tx_idx) > 0) {
562 for (i = 0; i < n; i++) {
563 txbd = ((I2C_BD*)state->txbd) - (n - i);
564 if ((b = txbd->status & BD_I2C_TX_ERR) != 0)
565 (*state->err_cb)(I2CECB_TX_ERR|b, i);
569 if ((n = state->rx_idx) > 0) {
570 for (i = 0; i < n; i++) {
571 rxbd = ((I2C_BD*)state->rxbd) - (n - i);
572 if ((b = rxbd->status & BD_I2C_RX_ERR) != 0)
573 (*state->err_cb)(I2CECB_RX_ERR|b, i);
578 (*state->err_cb)(I2CECB_TIMEOUT, 0);
581 /* sort out errors and return appropriate good/error status */
583 return(I2CERR_TIMEOUT);
584 if((txbd->status & BD_I2C_TX_ERR) != 0)
585 return(I2CECB_TX_ERR | (txbd->status & I2CECB_TX_MASK));
586 if((rxbd->status & BD_I2C_RX_ERR) != 0)
587 return(I2CECB_RX_ERR | (rxbd->status & I2CECB_RX_MASK));
592 static int had_tx_nak;
595 i2c_test_callback(int flags, int xnum)
597 if ((flags & I2CECB_TX_ERR) && (flags & I2CECB_TX_NAK))
601 int i2c_probe(uchar chip)
609 state.err_cb = i2c_test_callback;
612 rc = i2c_receive(&state, chip, 0, I2CF_START_COND|I2CF_STOP_COND, 1, buf);
617 rc = i2c_doio(&state);
619 if ((rc != 0) && (rc != I2CERR_TIMEOUT))
627 i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
633 xaddr[0] = (addr >> 24) & 0xFF;
634 xaddr[1] = (addr >> 16) & 0xFF;
635 xaddr[2] = (addr >> 8) & 0xFF;
636 xaddr[3] = addr & 0xFF;
638 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
640 * EEPROM chips that implement "address overflow" are ones
641 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of address
642 * and the extra bits end up in the "chip address" bit slots.
643 * This makes a 24WC08 (1Kbyte) chip look like four 256 byte
646 * Note that we consider the length of the address field to still
647 * be one byte because the extra address bits are hidden in the
650 chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
655 rc = i2c_send(&state, chip, 0, I2CF_START_COND, alen, &xaddr[4-alen]);
657 printf("i2c_read: i2c_send failed (%d)\n", rc);
661 rc = i2c_receive(&state, chip, 0, I2CF_STOP_COND, len, buffer);
663 printf("i2c_read: i2c_receive failed (%d)\n", rc);
667 rc = i2c_doio(&state);
669 printf("i2c_read: i2c_doio failed (%d)\n", rc);
676 i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
682 xaddr[0] = (addr >> 24) & 0xFF;
683 xaddr[1] = (addr >> 16) & 0xFF;
684 xaddr[2] = (addr >> 8) & 0xFF;
685 xaddr[3] = addr & 0xFF;
687 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
689 * EEPROM chips that implement "address overflow" are ones
690 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of address
691 * and the extra bits end up in the "chip address" bit slots.
692 * This makes a 24WC08 (1Kbyte) chip look like four 256 byte
695 * Note that we consider the length of the address field to still
696 * be one byte because the extra address bits are hidden in the
699 chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
704 rc = i2c_send(&state, chip, 0, I2CF_START_COND, alen, &xaddr[4-alen]);
706 printf("i2c_write: first i2c_send failed (%d)\n", rc);
710 rc = i2c_send(&state, 0, 0, I2CF_STOP_COND, len, buffer);
712 printf("i2c_write: second i2c_send failed (%d)\n", rc);
716 rc = i2c_doio(&state);
718 printf("i2c_write: i2c_doio failed (%d)\n", rc);
725 i2c_reg_read(uchar chip, uchar reg)
729 i2c_read(chip, reg, 1, &buf, 1);
735 i2c_reg_write(uchar chip, uchar reg, uchar val)
737 i2c_write(chip, reg, 1, &val, 1);
740 #endif /* CONFIG_HARD_I2C */