2 * Copyright (C) 2005 Sandburst Corporation
4 * SPDX-License-Identifier: GPL-2.0+
8 * Ported from arch/powerpc/cpu/ppc4xx/i2c.c by AS HARNOIS by
10 * Sandburst Corporation.
13 #include <asm/ppc4xx.h>
14 #include <asm/ppc4xx-i2c.h>
17 #include "ppc440gx_i2c.h"
20 #ifdef CONFIG_I2C_BUS1
24 #define IIC_NOK_LA 2 /* Lost arbitration */
25 #define IIC_NOK_ICT 3 /* Incomplete transfer */
26 #define IIC_NOK_XFRA 4 /* Transfer aborted */
27 #define IIC_NOK_DATA 5 /* No data in buffer */
28 #define IIC_NOK_TOUT 6 /* Transfer timeout */
30 #define IIC_TIMEOUT 1 /* 1 second */
31 #if defined(CONFIG_SYS_I2C_NOPROBES)
32 static uchar i2c_no_probes
[] = CONFIG_SYS_I2C_NOPROBES
;
35 static struct ppc4xx_i2c
*i2c
= (struct ppc4xx_i2c
*)I2C_REGISTERS_BUS1_BASE_ADDRESS
;
37 static void _i2c_bus1_reset (void)
41 /* Reset status register */
42 /* write 1 in SCMP and IRQA to clear these fields */
43 out_8 (IIC_STS1
, 0x0A);
45 /* write 1 in IRQP IRQD LA ICT XFRA to clear these fields */
46 out_8 (IIC_EXTSTS1
, 0x8F);
47 __asm__
volatile ("eieio");
50 * Get current state, reset bus
51 * only if no transfers are pending.
56 status
= in_8 (IIC_STS1
);
57 udelay (500); /* 500us */
59 } while ((status
& IIC_STS_PT
) && (i
> 0));
60 /* Soft reset controller */
61 status
= in_8 (IIC_XTCNTLSS1
);
62 out_8 (IIC_XTCNTLSS1
, (status
| IIC_XTCNTLSS_SRST
));
63 __asm__
volatile ("eieio");
65 /* make sure where in initial state, data hi, clock hi */
66 out_8 (IIC_DIRECTCNTL1
, 0xC);
67 for (i
= 0; i
< 10; i
++) {
68 if ((in_8 (IIC_DIRECTCNTL1
) & 0x3) != 0x3) {
69 /* clock until we get to known state */
70 out_8 (IIC_DIRECTCNTL1
, 0x8); /* clock lo */
71 udelay (100); /* 100us */
72 out_8 (IIC_DIRECTCNTL1
, 0xC); /* clock hi */
73 udelay (100); /* 100us */
78 /* send start condition */
79 out_8 (IIC_DIRECTCNTL1
, 0x4);
80 udelay (1000); /* 1ms */
81 /* send stop condition */
82 out_8 (IIC_DIRECTCNTL1
, 0xC);
83 udelay (1000); /* 1ms */
84 /* Unreset controller */
85 out_8 (IIC_XTCNTLSS1
, (status
& ~IIC_XTCNTLSS_SRST
));
86 udelay (1000); /* 1ms */
89 void i2c1_init (int speed
, int slaveadd
)
92 unsigned long freqOPB
;
95 #ifdef CONFIG_SYS_I2C_INIT_BOARD
96 /* call board specific i2c bus reset routine before accessing the */
97 /* environment, which might be in a chip on that bus. For details */
98 /* about this problem see doc/I2C_Edge_Conditions. */
102 /* Handle possible failed I2C state */
103 /* FIXME: put this into i2c_init_board()? */
106 /* clear lo master address */
107 out_8 (IIC_LMADR1
, 0);
109 /* clear hi master address */
110 out_8 (IIC_HMADR1
, 0);
112 /* clear lo slave address */
113 out_8 (IIC_LSADR1
, 0);
115 /* clear hi slave address */
116 out_8 (IIC_HSADR1
, 0);
118 /* Clock divide Register */
119 /* get OPB frequency */
120 get_sys_info (&sysInfo
);
121 freqOPB
= sysInfo
.freqPLB
/ sysInfo
.pllOpbDiv
;
122 /* set divisor according to freqOPB */
123 divisor
= (freqOPB
- 1) / 10000000;
126 out_8 (IIC_CLKDIV1
, divisor
);
129 out_8 (IIC_INTRMSK1
, 0);
131 /* clear transfer count */
132 out_8 (IIC_XFRCNT1
, 0);
134 /* clear extended control & stat */
135 /* write 1 in SRC SRS SWC SWS to clear these fields */
136 out_8 (IIC_XTCNTLSS1
, 0xF0);
138 /* Mode Control Register
139 Flush Slave/Master data buffer */
140 out_8 (IIC_MDCNTL1
, IIC_MDCNTL_FSDB
| IIC_MDCNTL_FMDB
);
141 __asm__
volatile ("eieio");
144 val
= in_8(IIC_MDCNTL1
);
145 __asm__
volatile ("eieio");
147 /* Ignore General Call, slave transfers are ignored,
148 disable interrupts, exit unknown bus state, enable hold
150 100kHz normaly or FastMode for 400kHz and above
153 val
|= IIC_MDCNTL_EUBS
|IIC_MDCNTL_HSCL
;
154 if( speed
>= 400000 ){
155 val
|= IIC_MDCNTL_FSM
;
157 out_8 (IIC_MDCNTL1
, val
);
159 /* clear control reg */
160 out_8 (IIC_CNTL1
, 0x00);
161 __asm__
volatile ("eieio");
166 This code tries to use the features of the 405GP i2c
167 controller. It will transfer up to 4 bytes in one pass
168 on the loop. It only does out_8(lbz) to the buffer when it
169 is possible to do out16(lhz) transfers.
171 cmd_type is 0 for write 1 for read.
173 addr_len can take any value from 0-255, it is only limited
174 by the char, we could make it larger if needed. If it is
175 0 we skip the address write cycle.
177 Typical case is a Write of an addr followd by a Read. The
178 IBM FAQ does not cover this. On the last byte of the write
179 we don't set the creg CHT bit, and on the first bytes of the
180 read we set the RPST bit.
182 It does not support address only transfers, there must be
183 a data part. If you want to write the address yourself, put
184 it in the data pointer.
186 It does not support transfer to/from address 0.
188 It does not check XFRCNT.
191 int i2c_transfer1(unsigned char cmd_type
,
193 unsigned char addr
[],
194 unsigned char addr_len
,
195 unsigned char data
[],
196 unsigned short data_len
)
206 if( data
== 0 || data_len
== 0 ){
207 /*Don't support data transfer of no length or to address 0*/
208 printf( "i2c_transfer: bad call\n" );
211 if( addr
&& addr_len
){
221 /*Clear Stop Complete Bit*/
222 out_8(IIC_STS1
,IIC_STS_SCMP
);
227 status
= in_8(IIC_STS1
);
228 __asm__
volatile("eieio");
230 } while ((status
& IIC_STS_PT
) && (i
>0));
232 if (status
& IIC_STS_PT
) {
233 result
= IIC_NOK_TOUT
;
236 /*flush the Master/Slave Databuffers*/
237 out_8(IIC_MDCNTL1
, ((in_8(IIC_MDCNTL1
))|IIC_MDCNTL_FMDB
|IIC_MDCNTL_FSDB
));
238 /*need to wait 4 OPB clocks? code below should take that long*/
240 /* 7-bit adressing */
242 out_8(IIC_LMADR1
, chip
);
243 __asm__
volatile("eieio");
249 while ( tran
!= cnt
&& (result
== IIC_OK
)) {
252 /* Control register =
253 Normal transfer, 7-bits adressing, Transfer up to bc bytes, Normal start,
254 Transfer is a sequence of transfers
258 bc
= (cnt
- tran
) > 4 ? 4 :
261 /* if the real cmd type is write continue trans*/
262 if ( (!cmd_type
&& (ptr
== addr
)) || ((tran
+bc
) != cnt
) )
263 creg
|= IIC_CNTL_CHT
;
266 creg
|= IIC_CNTL_READ
;
268 for(j
=0; j
<bc
; j
++) {
270 out_8(IIC_MDBUF1
,ptr
[tran
+j
]);
271 __asm__
volatile("eieio");
274 out_8(IIC_CNTL1
, creg
);
275 __asm__
volatile("eieio");
277 /* Transfer is in progress
278 we have to wait for upto 5 bytes of data
279 1 byte chip address+r/w bit then bc bytes
281 udelay(10) is 1 bit time at 100khz
282 Doubled for slop. 20 is too small.
287 status
= in_8(IIC_STS1
);
288 __asm__
volatile("eieio");
291 } while ((status
& IIC_STS_PT
) && !(status
& IIC_STS_ERR
)
294 if (status
& IIC_STS_ERR
) {
296 status
= in_8 (IIC_EXTSTS1
);
297 /* Lost arbitration? */
298 if (status
& IIC_EXTSTS_LA
)
300 /* Incomplete transfer? */
301 if (status
& IIC_EXTSTS_ICT
)
302 result
= IIC_NOK_ICT
;
303 /* Transfer aborted? */
304 if (status
& IIC_EXTSTS_XFRA
)
305 result
= IIC_NOK_XFRA
;
306 } else if ( status
& IIC_STS_PT
) {
307 result
= IIC_NOK_TOUT
;
309 /* Command is reading => get buffer */
310 if ((reading
) && (result
== IIC_OK
)) {
311 /* Are there data in buffer */
312 if (status
& IIC_STS_MDBS
) {
314 even if we have data we have to wait 4OPB clocks
315 for it to hit the front of the FIFO, after that
316 we can just read. We should check XFCNT here and
317 if the FIFO is full there is no need to wait.
321 ptr
[tran
+j
] = in_8(IIC_MDBUF1
);
322 __asm__
volatile("eieio");
325 result
= IIC_NOK_DATA
;
329 if( ptr
== addr
&& tran
== cnt
) {
335 creg
= IIC_CNTL_RPST
;
341 int i2c_probe1 (uchar chip
)
348 * What is needed is to send the chip address and verify that the
349 * address was <ACK>ed (i.e. there was a chip at that address which
350 * drove the data line low).
352 return(i2c_transfer1 (1, chip
<< 1, 0,0, buf
, 1) != 0);
356 int i2c_read1 (uchar chip
, uint addr
, int alen
, uchar
* buffer
, int len
)
362 printf ("I2C read: addr len %d not supported\n", alen
);
367 xaddr
[0] = (addr
>> 24) & 0xFF;
368 xaddr
[1] = (addr
>> 16) & 0xFF;
369 xaddr
[2] = (addr
>> 8) & 0xFF;
370 xaddr
[3] = addr
& 0xFF;
374 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
376 * EEPROM chips that implement "address overflow" are ones
377 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
378 * address and the extra bits end up in the "chip address"
379 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
380 * four 256 byte chips.
382 * Note that we consider the length of the address field to
383 * still be one byte because the extra address bits are
384 * hidden in the chip address.
387 chip
|= ((addr
>> (alen
* 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
);
389 if( (ret
= i2c_transfer1( 1, chip
<<1, &xaddr
[4-alen
], alen
, buffer
, len
)) != 0) {
390 printf( "I2c read: failed %d\n", ret
);
396 int i2c_write1 (uchar chip
, uint addr
, int alen
, uchar
* buffer
, int len
)
401 printf ("I2C write: addr len %d not supported\n", alen
);
406 xaddr
[0] = (addr
>> 24) & 0xFF;
407 xaddr
[1] = (addr
>> 16) & 0xFF;
408 xaddr
[2] = (addr
>> 8) & 0xFF;
409 xaddr
[3] = addr
& 0xFF;
412 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
414 * EEPROM chips that implement "address overflow" are ones
415 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
416 * address and the extra bits end up in the "chip address"
417 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
418 * four 256 byte chips.
420 * Note that we consider the length of the address field to
421 * still be one byte because the extra address bits are
422 * hidden in the chip address.
425 chip
|= ((addr
>> (alen
* 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
);
428 return (i2c_transfer1( 0, chip
<<1, &xaddr
[4-alen
], alen
, buffer
, len
) != 0);
431 /*-----------------------------------------------------------------------
434 uchar
i2c_reg_read1(uchar i2c_addr
, uchar reg
)
438 i2c_read1(i2c_addr
, reg
, 1, &buf
, (uchar
)1);
443 /*-----------------------------------------------------------------------
446 void i2c_reg_write1(uchar i2c_addr
, uchar reg
, uchar val
)
448 i2c_write1(i2c_addr
, reg
, 1, &val
, 1);
452 int do_i2c1_probe(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
455 #if defined(CONFIG_SYS_I2C_NOPROBES)
459 puts ("Valid chip addresses:");
460 for(j
= 0; j
< 128; j
++) {
461 #if defined(CONFIG_SYS_I2C_NOPROBES)
463 for (k
= 0; k
< sizeof(i2c_no_probes
); k
++){
464 if (j
== i2c_no_probes
[k
]){
472 if(i2c_probe1(j
) == 0) {
478 #if defined(CONFIG_SYS_I2C_NOPROBES)
479 puts ("Excluded chip addresses:");
480 for( k
= 0; k
< sizeof(i2c_no_probes
); k
++ )
481 printf(" %02X", i2c_no_probes
[k
] );
489 iprobe1
, 1, 1, do_i2c1_probe
,
490 "probe to discover valid I2C chip addresses",
494 #endif /* CONFIG_I2C_BUS1 */