]>
git.ipfire.org Git - people/ms/u-boot.git/blob - arch/powerpc/cpu/mpc824x/drivers/i2c/i2c.c
3 * Gleb Natapov <gnatapov@mrv.com>
4 * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
6 * Hardware I2C driver for MPC107 PCI bridge.
8 * SPDX-License-Identifier: GPL-2.0+
15 #ifdef CONFIG_HARD_I2C
18 #define TIMEOUT (CONFIG_SYS_HZ/4)
20 #define I2C_Addr ((unsigned *)(CONFIG_SYS_EUMB_ADDR + 0x3000))
22 #define I2CADR &I2C_Addr[0]
23 #define I2CFDR &I2C_Addr[1]
24 #define I2CCCR &I2C_Addr[2]
25 #define I2CCSR &I2C_Addr[3]
26 #define I2CCDR &I2C_Addr[4]
28 #define MPC107_CCR_MEN 0x80
29 #define MPC107_CCR_MIEN 0x40
30 #define MPC107_CCR_MSTA 0x20
31 #define MPC107_CCR_MTX 0x10
32 #define MPC107_CCR_TXAK 0x08
33 #define MPC107_CCR_RSTA 0x04
35 #define MPC107_CSR_MCF 0x80
36 #define MPC107_CSR_MAAS 0x40
37 #define MPC107_CSR_MBB 0x20
38 #define MPC107_CSR_MAL 0x10
39 #define MPC107_CSR_SRW 0x04
40 #define MPC107_CSR_MIF 0x02
41 #define MPC107_CSR_RXAK 0x01
46 /* taken from linux include/asm-ppc/io.h */
47 inline unsigned in_le32 (volatile unsigned *addr
)
51 __asm__
__volatile__ ("lwbrx %0,0,%1;\n"
53 "isync":"=r" (ret
): "r" (addr
), "m" (*addr
));
57 inline void out_le32 (volatile unsigned *addr
, int val
)
59 __asm__
__volatile__ ("stwbrx %1,0,%2; eieio":"=m" (*addr
):"r" (val
),
63 #define writel(val, addr) out_le32(addr, val)
64 #define readl(addr) in_le32(addr)
66 void i2c_init (int speed
, int slaveadd
)
68 /* stop I2C controller */
71 writel (0x1020, I2CFDR
);
72 /* write slave address */
73 writel (slaveadd
, I2CADR
);
74 /* clear status register */
76 /* start I2C controller */
77 writel (MPC107_CCR_MEN
, I2CCCR
);
82 static __inline__
int i2c_wait4bus (void)
84 ulong timeval
= get_timer (0);
86 while (readl (I2CCSR
) & MPC107_CSR_MBB
)
87 if (get_timer (timeval
) > TIMEOUT
)
93 static __inline__
int i2c_wait (int write
)
96 ulong timeval
= get_timer (0);
101 if (!(csr
& MPC107_CSR_MIF
))
104 writel (0x0, I2CCSR
);
106 if (csr
& MPC107_CSR_MAL
) {
108 printf ("i2c_wait: MAL\n");
113 if (!(csr
& MPC107_CSR_MCF
)) {
115 printf ("i2c_wait: unfinished\n");
120 if (write
== I2C_WRITE
&& (csr
& MPC107_CSR_RXAK
)) {
122 printf ("i2c_wait: No RXACK\n");
128 } while (get_timer (timeval
) < TIMEOUT
);
131 printf ("i2c_wait: timed out\n");
136 static __inline__
int i2c_write_addr (u8 dev
, u8 dir
, int rsta
)
138 writel (MPC107_CCR_MEN
| MPC107_CCR_MSTA
| MPC107_CCR_MTX
|
139 (rsta
? MPC107_CCR_RSTA
: 0), I2CCCR
);
141 writel ((dev
<< 1) | dir
, I2CCDR
);
143 if (i2c_wait (I2C_WRITE
) < 0)
149 static __inline__
int __i2c_write (u8
* data
, int length
)
153 writel (MPC107_CCR_MEN
| MPC107_CCR_MSTA
| MPC107_CCR_MTX
, I2CCCR
);
155 for (i
= 0; i
< length
; i
++) {
156 writel (data
[i
], I2CCDR
);
158 if (i2c_wait (I2C_WRITE
) < 0)
165 static __inline__
int __i2c_read (u8
* data
, int length
)
169 writel (MPC107_CCR_MEN
| MPC107_CCR_MSTA
|
170 ((length
== 1) ? MPC107_CCR_TXAK
: 0), I2CCCR
);
175 for (i
= 0; i
< length
; i
++) {
176 if (i2c_wait (I2C_READ
) < 0)
179 /* Generate ack on last next to last byte */
181 writel (MPC107_CCR_MEN
| MPC107_CCR_MSTA
|
182 MPC107_CCR_TXAK
, I2CCCR
);
184 /* Generate stop on last byte */
186 writel (MPC107_CCR_MEN
| MPC107_CCR_TXAK
, I2CCCR
);
188 data
[i
] = readl (I2CCDR
);
194 int i2c_read (u8 dev
, uint addr
, int alen
, u8
* data
, int length
)
197 u8
*a
= (u8
*) & addr
;
199 if (i2c_wait4bus () < 0)
202 if (i2c_write_addr (dev
, I2C_WRITE
, 0) == 0)
205 if (__i2c_write (&a
[4 - alen
], alen
) != alen
)
208 if (i2c_write_addr (dev
, I2C_READ
, 1) == 0)
211 i
= __i2c_read (data
, length
);
214 writel (MPC107_CCR_MEN
, I2CCCR
);
216 return !(i
== length
);
219 int i2c_write (u8 dev
, uint addr
, int alen
, u8
* data
, int length
)
222 u8
*a
= (u8
*) & addr
;
224 if (i2c_wait4bus () < 0)
227 if (i2c_write_addr (dev
, I2C_WRITE
, 0) == 0)
230 if (__i2c_write (&a
[4 - alen
], alen
) != alen
)
233 i
= __i2c_write (data
, length
);
236 writel (MPC107_CCR_MEN
, I2CCCR
);
238 return !(i
== length
);
241 int i2c_probe (uchar chip
)
246 * Try to read the first location of the chip. The underlying
247 * driver doesn't appear to support sending just the chip address
248 * and looking for an <ACK> back.
251 return i2c_read (chip
, 0, 1, (uchar
*) &tmp
, 1);
254 #endif /* CONFIG_HARD_I2C */