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
21 #define IIC_NOK_LA 2 /* Lost arbitration */
22 #define IIC_NOK_ICT 3 /* Incomplete transfer */
23 #define IIC_NOK_XFRA 4 /* Transfer aborted */
24 #define IIC_NOK_DATA 5 /* No data in buffer */
25 #define IIC_NOK_TOUT 6 /* Transfer timeout */
27 #define IIC_TIMEOUT 1 /* 1 seconde */
30 static void _i2c_bus_reset (void)
34 /* Reset status register */
35 /* write 1 in SCMP and IRQA to clear these fields */
38 /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
39 out8 (IIC_EXTSTS, 0x8F);
40 __asm__ volatile ("eieio");
43 * Get current state, reset bus
44 * only if no transfers are pending.
49 status = in8 (IIC_STS);
50 udelay (500); /* 500us */
52 } while ((status & IIC_STS_PT) && (i > 0));
53 /* Soft reset controller */
54 status = in8 (IIC_XTCNTLSS);
55 out8 (IIC_XTCNTLSS, (status | IIC_XTCNTLSS_SRST));
56 __asm__ volatile ("eieio");
58 /* make sure where in initial state, data hi, clock hi */
59 out8 (IIC_DIRECTCNTL, 0xC);
60 for (i = 0; i < 10; i++) {
61 if ((in8 (IIC_DIRECTCNTL) & 0x3) != 0x3) {
62 /* clock until we get to known state */
63 out8 (IIC_DIRECTCNTL, 0x8); /* clock lo */
64 udelay (100); /* 100us */
65 out8 (IIC_DIRECTCNTL, 0xC); /* clock hi */
66 udelay (100); /* 100us */
71 /* send start condition */
72 out8 (IIC_DIRECTCNTL, 0x4);
73 udelay (1000); /* 1ms */
74 /* send stop condition */
75 out8 (IIC_DIRECTCNTL, 0xC);
76 udelay (1000); /* 1ms */
77 /* Unreset controller */
78 out8 (IIC_XTCNTLSS, (status & ~IIC_XTCNTLSS_SRST));
79 udelay (1000); /* 1ms */
82 void i2c_init (int speed, int slaveadd)
85 unsigned long freqOPB;
88 /* Handle possible failed I2C state */
91 /* clear lo master address */
94 /* clear hi master address */
97 /* clear lo slave address */
100 /* clear hi slave address */
103 /* Clock divide Register */
104 /* get OPB frequency */
105 get_sys_info (&sysInfo);
106 freqOPB = sysInfo.freqPLB / sysInfo.pllOpbDiv;
107 /* set divisor according to freqOPB */
108 divisor = (freqOPB - 1) / 10000000;
111 out8 (IIC_CLKDIV, divisor);
114 out8 (IIC_INTRMSK, 0);
116 /* clear transfer count */
117 out8 (IIC_XFRCNT, 0);
119 /* clear extended control & stat */
120 /* write 1 in SRC SRS SWC SWS to clear these fields */
121 out8 (IIC_XTCNTLSS, 0xF0);
123 /* Mode Control Register
124 Flush Slave/Master data buffer */
125 out8 (IIC_MDCNTL, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
126 __asm__ volatile ("eieio");
129 val = in8(IIC_MDCNTL);
130 __asm__ volatile ("eieio");
132 /* Ignore General Call, slave transfers are ignored,
133 disable interrupts, exit unknown bus state, enable hold
135 100kHz normaly or FastMode for 400kHz and above
138 val |= IIC_MDCNTL_EUBS|IIC_MDCNTL_HSCL;
139 if( speed >= 400000 ){
140 val |= IIC_MDCNTL_FSM;
142 out8 (IIC_MDCNTL, val);
144 /* clear control reg */
145 out8 (IIC_CNTL, 0x00);
146 __asm__ volatile ("eieio");
151 This code tries to use the features of the 405GP i2c
152 controller. It will transfer up to 4 bytes in one pass
153 on the loop. It only does out8(lbz) to the buffer when it
154 is possible to do out16(lhz) transfers.
156 cmd_type is 0 for write 1 for read.
158 addr_len can take any value from 0-255, it is only limited
159 by the char, we could make it larger if needed. If it is
160 0 we skip the address write cycle.
162 Typical case is a Write of an addr followd by a Read. The
163 IBM FAQ does not cover this. On the last byte of the write
164 we don't set the creg CHT bit, and on the first bytes of the
165 read we set the RPST bit.
167 It does not support address only transfers, there must be
168 a data part. If you want to write the address yourself, put
169 it in the data pointer.
171 It does not support transfer to/from address 0.
173 It does not check XFRCNT.
176 int i2c_transfer(unsigned char cmd_type,
178 unsigned char addr[],
179 unsigned char addr_len,
180 unsigned char data[],
181 unsigned short data_len )
191 if( data == 0 || data_len == 0 ){
192 /*Don't support data transfer of no length or to address 0*/
193 printf( "i2c_transfer: bad call\n" );
196 if( addr && addr_len ){
206 /*Clear Stop Complete Bit*/
207 out8(IIC_STS,IIC_STS_SCMP);
212 status = in8(IIC_STS);
213 __asm__ volatile("eieio");
215 } while ((status & IIC_STS_PT) && (i>0));
217 if (status & IIC_STS_PT) {
218 result = IIC_NOK_TOUT;
221 /*flush the Master/Slave Databuffers*/
222 out8(IIC_MDCNTL, ((in8(IIC_MDCNTL))|IIC_MDCNTL_FMDB|IIC_MDCNTL_FSDB));
223 /*need to wait 4 OPB clocks? code below should take that long*/
225 /* 7-bit adressing */
227 out8(IIC_LMADR, chip);
228 __asm__ volatile("eieio");
234 while ( tran != cnt && (result == IIC_OK)) {
237 /* Control register =
238 Normal transfer, 7-bits adressing, Transfer up to bc bytes, Normal start,
239 Transfer is a sequence of transfers
243 bc = (cnt - tran) > 4 ? 4 :
246 /* if the real cmd type is write continue trans*/
247 if ( (!cmd_type && (ptr == addr)) || ((tran+bc) != cnt) )
248 creg |= IIC_CNTL_CHT;
251 creg |= IIC_CNTL_READ;
253 for(j=0; j<bc; j++) {
255 out8(IIC_MDBUF,ptr[tran+j]);
256 __asm__ volatile("eieio");
259 out8(IIC_CNTL, creg );
260 __asm__ volatile("eieio");
262 /* Transfer is in progress
263 we have to wait for upto 5 bytes of data
264 1 byte chip address+r/w bit then bc bytes
266 udelay(10) is 1 bit time at 100khz
267 Doubled for slop. 20 is too small.
272 status = in8(IIC_STS);
273 __asm__ volatile("eieio");
276 } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR)
279 if (status & IIC_STS_ERR) {
281 status = in8 (IIC_EXTSTS);
282 /* Lost arbitration? */
283 if (status & IIC_EXTSTS_LA)
285 /* Incomplete transfer? */
286 if (status & IIC_EXTSTS_ICT)
287 result = IIC_NOK_ICT;
288 /* Transfer aborted? */
289 if (status & IIC_EXTSTS_XFRA)
290 result = IIC_NOK_XFRA;
291 } else if ( status & IIC_STS_PT) {
292 result = IIC_NOK_TOUT;
294 /* Command is reading => get buffer */
295 if ((reading) && (result == IIC_OK)) {
296 /* Are there data in buffer */
297 if (status & IIC_STS_MDBS) {
299 even if we have data we have to wait 4OPB clocks
300 for it to hit the front of the FIFO, after that
301 we can just read. We should check XFCNT here and
302 if the FIFO is full there is no need to wait.
306 ptr[tran+j] = in8(IIC_MDBUF);
307 __asm__ volatile("eieio");
310 result = IIC_NOK_DATA;
314 if( ptr == addr && tran == cnt ) {
320 creg = IIC_CNTL_RPST;
326 int i2c_probe (uchar chip)
333 * What is needed is to send the chip address and verify that the
334 * address was <ACK>ed (i.e. there was a chip at that address which
335 * drove the data line low).
337 return(i2c_transfer (1, chip << 1, 0,0, buf, 1) != 0);
342 int i2c_read (uchar chip, uint addr, int alen, uchar * buffer, int len)
348 printf ("I2C read: addr len %d not supported\n", alen);
353 xaddr[0] = (addr >> 24) & 0xFF;
354 xaddr[1] = (addr >> 16) & 0xFF;
355 xaddr[2] = (addr >> 8) & 0xFF;
356 xaddr[3] = addr & 0xFF;
360 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
362 * EEPROM chips that implement "address overflow" are ones
363 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
364 * address and the extra bits end up in the "chip address"
365 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
366 * four 256 byte chips.
368 * Note that we consider the length of the address field to
369 * still be one byte because the extra address bits are
370 * hidden in the chip address.
373 chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
375 if( (ret = i2c_transfer( 1, chip<<1, &xaddr[4-alen], alen, buffer, len )) != 0) {
376 printf( "I2c read: failed %d\n", ret);
382 int i2c_write (uchar chip, uint addr, int alen, uchar * buffer, int len)
387 printf ("I2C write: addr len %d not supported\n", alen);
392 xaddr[0] = (addr >> 24) & 0xFF;
393 xaddr[1] = (addr >> 16) & 0xFF;
394 xaddr[2] = (addr >> 8) & 0xFF;
395 xaddr[3] = addr & 0xFF;
398 #ifdef CFG_I2C_EEPROM_ADDR_OVERFLOW
400 * EEPROM chips that implement "address overflow" are ones
401 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
402 * address and the extra bits end up in the "chip address"
403 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
404 * four 256 byte chips.
406 * Note that we consider the length of the address field to
407 * still be one byte because the extra address bits are
408 * hidden in the chip address.
411 chip |= ((addr >> (alen * 8)) & CFG_I2C_EEPROM_ADDR_OVERFLOW);
414 return (i2c_transfer( 0, chip<<1, &xaddr[4-alen], alen, buffer, len ) != 0);
417 #endif /* CONFIG_HARD_I2C */