1 /*****************************************************************************/
2 /* I2C Bus interface initialisation and I2C Commands */
4 /* Author : AS HARNOIS */
6 /*****************************************************************************/
10 #if defined(CONFIG_440)
13 # include <405gp_i2c.h>
17 #ifdef CONFIG_HARD_I2C
19 DECLARE_GLOBAL_DATA_PTR;
23 #define IIC_NOK_LA 2 /* Lost arbitration */
24 #define IIC_NOK_ICT 3 /* Incomplete transfer */
25 #define IIC_NOK_XFRA 4 /* Transfer aborted */
26 #define IIC_NOK_DATA 5 /* No data in buffer */
27 #define IIC_NOK_TOUT 6 /* Transfer timeout */
29 #define IIC_TIMEOUT 1 /* 1 seconde */
32 static void _i2c_bus_reset (void)
36 /* Reset status register */
37 /* write 1 in SCMP and IRQA to clear these fields */
40 /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
41 out8 (IIC_EXTSTS, 0x8F);
42 __asm__ volatile ("eieio");
45 * Get current state, reset bus
46 * only if no transfers are pending.
51 status = in8 (IIC_STS);
52 udelay (500); /* 500us */
54 } while ((status & IIC_STS_PT) && (i > 0));
55 /* Soft reset controller */
56 status = in8 (IIC_XTCNTLSS);
57 out8 (IIC_XTCNTLSS, (status | IIC_XTCNTLSS_SRST));
58 __asm__ volatile ("eieio");
60 /* make sure where in initial state, data hi, clock hi */
61 out8 (IIC_DIRECTCNTL, 0xC);
62 for (i = 0; i < 10; i++) {
63 if ((in8 (IIC_DIRECTCNTL) & 0x3) != 0x3) {
64 /* clock until we get to known state */
65 out8 (IIC_DIRECTCNTL, 0x8); /* clock lo */
66 udelay (100); /* 100us */
67 out8 (IIC_DIRECTCNTL, 0xC); /* clock hi */
68 udelay (100); /* 100us */
73 /* send start condition */
74 out8 (IIC_DIRECTCNTL, 0x4);
75 udelay (1000); /* 1ms */
76 /* send stop condition */
77 out8 (IIC_DIRECTCNTL, 0xC);
78 udelay (1000); /* 1ms */
79 /* Unreset controller */
80 out8 (IIC_XTCNTLSS, (status & ~IIC_XTCNTLSS_SRST));
81 udelay (1000); /* 1ms */
84 void i2c_init (int speed, int slaveadd)
87 unsigned long freqOPB;
90 #ifdef CFG_I2C_INIT_BOARD
91 /* call board specific i2c bus reset routine before accessing the */
92 /* environment, which might be in a chip on that bus. For details */
93 /* about this problem see doc/I2C_Edge_Conditions. */
97 /* Handle possible failed I2C state */
98 /* FIXME: put this into i2c_init_board()? */
101 /* clear lo master address */
104 /* clear hi master address */
107 /* clear lo slave address */
110 /* clear hi slave address */
113 /* Clock divide Register */
114 /* get OPB frequency */
115 get_sys_info (&sysInfo);
116 freqOPB = sysInfo.freqPLB / sysInfo.pllOpbDiv;
117 /* set divisor according to freqOPB */
118 divisor = (freqOPB - 1) / 10000000;
121 out8 (IIC_CLKDIV, divisor);
124 out8 (IIC_INTRMSK, 0);
126 /* clear transfer count */
127 out8 (IIC_XFRCNT, 0);
129 /* clear extended control & stat */
130 /* write 1 in SRC SRS SWC SWS to clear these fields */
131 out8 (IIC_XTCNTLSS, 0xF0);
133 /* Mode Control Register
134 Flush Slave/Master data buffer */
135 out8 (IIC_MDCNTL, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
136 __asm__ volatile ("eieio");
139 val = in8(IIC_MDCNTL);
140 __asm__ volatile ("eieio");
142 /* Ignore General Call, slave transfers are ignored,
143 disable interrupts, exit unknown bus state, enable hold
145 100kHz normaly or FastMode for 400kHz and above
148 val |= IIC_MDCNTL_EUBS|IIC_MDCNTL_HSCL;
149 if( speed >= 400000 ){
150 val |= IIC_MDCNTL_FSM;
152 out8 (IIC_MDCNTL, val);
154 /* clear control reg */
155 out8 (IIC_CNTL, 0x00);
156 __asm__ volatile ("eieio");
161 This code tries to use the features of the 405GP i2c
162 controller. It will transfer up to 4 bytes in one pass
163 on the loop. It only does out8(lbz) to the buffer when it
164 is possible to do out16(lhz) transfers.
166 cmd_type is 0 for write 1 for read.
168 addr_len can take any value from 0-255, it is only limited
169 by the char, we could make it larger if needed. If it is
170 0 we skip the address write cycle.
172 Typical case is a Write of an addr followd by a Read. The
173 IBM FAQ does not cover this. On the last byte of the write
174 we don't set the creg CHT bit, and on the first bytes of the
175 read we set the RPST bit.
177 It does not support address only transfers, there must be
178 a data part. If you want to write the address yourself, put
179 it in the data pointer.
181 It does not support transfer to/from address 0.
183 It does not check XFRCNT.
186 int i2c_transfer(unsigned char cmd_type,
188 unsigned char addr[],
189 unsigned char addr_len,
190 unsigned char data[],
191 unsigned short data_len )
201 if( data == 0 || data_len == 0 ){
202 /*Don't support data transfer of no length or to address 0*/
203 printf( "i2c_transfer: bad call\n" );
206 if( addr && addr_len ){
216 /*Clear Stop Complete Bit*/
217 out8(IIC_STS,IIC_STS_SCMP);
222 status = in8(IIC_STS);
223 __asm__ volatile("eieio");
225 } while ((status & IIC_STS_PT) && (i>0));
227 if (status & IIC_STS_PT) {
228 result = IIC_NOK_TOUT;
231 /*flush the Master/Slave Databuffers*/
232 out8(IIC_MDCNTL, ((in8(IIC_MDCNTL))|IIC_MDCNTL_FMDB|IIC_MDCNTL_FSDB));
233 /*need to wait 4 OPB clocks? code below should take that long*/
235 /* 7-bit adressing */
237 out8(IIC_LMADR, chip);
238 __asm__ volatile("eieio");
244 while ( tran != cnt && (result == IIC_OK)) {
247 /* Control register =
248 Normal transfer, 7-bits adressing, Transfer up to bc bytes, Normal start,
249 Transfer is a sequence of transfers
253 bc = (cnt - tran) > 4 ? 4 :
256 /* if the real cmd type is write continue trans*/
257 if ( (!cmd_type && (ptr == addr)) || ((tran+bc) != cnt) )
258 creg |= IIC_CNTL_CHT;
261 creg |= IIC_CNTL_READ;
263 for(j=0; j<bc; j++) {
265 out8(IIC_MDBUF,ptr[tran+j]);
266 __asm__ volatile("eieio");
269 out8(IIC_CNTL, creg );
270 __asm__ volatile("eieio");
272 /* Transfer is in progress
273 we have to wait for upto 5 bytes of data
274 1 byte chip address+r/w bit then bc bytes
276 udelay(10) is 1 bit time at 100khz
277 Doubled for slop. 20 is too small.
282 status = in8(IIC_STS);
283 __asm__ volatile("eieio");
286 } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR)
289 if (status & IIC_STS_ERR) {
291 status = in8 (IIC_EXTSTS);
292 /* Lost arbitration? */
293 if (status & IIC_EXTSTS_LA)
295 /* Incomplete transfer? */
296 if (status & IIC_EXTSTS_ICT)
297 result = IIC_NOK_ICT;
298 /* Transfer aborted? */
299 if (status & IIC_EXTSTS_XFRA)
300 result = IIC_NOK_XFRA;
301 } else if ( status & IIC_STS_PT) {
302 result = IIC_NOK_TOUT;
304 /* Command is reading => get buffer */
305 if ((reading) && (result == IIC_OK)) {
306 /* Are there data in buffer */
307 if (status & IIC_STS_MDBS) {
309 even if we have data we have to wait 4OPB clocks
310 for it to hit the front of the FIFO, after that
311 we can just read. We should check XFCNT here and
312 if the FIFO is full there is no need to wait.
316 ptr[tran+j] = in8(IIC_MDBUF);
317 __asm__ volatile("eieio");
320 result = IIC_NOK_DATA;
324 if( ptr == addr && tran == cnt ) {
330 creg = IIC_CNTL_RPST;
336 int i2c_probe (uchar chip)
343 * What is needed is to send the chip address and verify that the
344 * address was <ACK>ed (i.e. there was a chip at that address which
345 * drove the data line low).
347 return(i2c_transfer (1, chip << 1, 0,0, buf, 1) != 0);
351 int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
357 printf ("I2C read: addr len %d not supported\n", alen);
362 xaddr[0] = (addr >> 24) & 0xFF;
363 xaddr[1] = (addr >> 16) & 0xFF;
364 xaddr[2] = (addr >> 8) & 0xFF;
365 xaddr[3] = addr & 0xFF;
369 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
371 * EEPROM chips that implement "address overflow" are ones
372 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
373 * address and the extra bits end up in the "chip address"
374 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
375 * four 256 byte chips.
377 * Note that we consider the length of the address field to
378 * still be one byte because the extra address bits are
379 * hidden in the chip address.
382 chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
384 if( (ret = i2c_transfer( 1, chip<<1, &xaddr[4-alen], alen, buffer, len )) != 0) {
385 if (gd->have_console)
386 printf( "I2c read: failed %d\n", ret);
392 int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
397 printf ("I2C write: addr len %d not supported\n", alen);
402 xaddr[0] = (addr >> 24) & 0xFF;
403 xaddr[1] = (addr >> 16) & 0xFF;
404 xaddr[2] = (addr >> 8) & 0xFF;
405 xaddr[3] = addr & 0xFF;
408 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
410 * EEPROM chips that implement "address overflow" are ones
411 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
412 * address and the extra bits end up in the "chip address"
413 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
414 * four 256 byte chips.
416 * Note that we consider the length of the address field to
417 * still be one byte because the extra address bits are
418 * hidden in the chip address.
421 chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
424 return (i2c_transfer( 0, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0);
427 /*-----------------------------------------------------------------------
430 uchar i2c_reg_read(uchar i2c_addr, uchar reg)
434 i2c_read(i2c_addr, reg, 1, &buf, 1);
439 /*-----------------------------------------------------------------------
442 void i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
444 i2c_write(i2c_addr, reg, 1, &val, 1);
446 #endif /* CONFIG_HARD_I2C */