]>
git.ipfire.org Git - people/ms/u-boot.git/blob - 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 * 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,
31 #ifdef CONFIG_HARD_I2C
34 #define TIMEOUT (CONFIG_SYS_HZ/4)
36 #define I2C_Addr ((unsigned *)(CONFIG_SYS_EUMB_ADDR + 0x3000))
38 #define I2CADR &I2C_Addr[0]
39 #define I2CFDR &I2C_Addr[1]
40 #define I2CCCR &I2C_Addr[2]
41 #define I2CCSR &I2C_Addr[3]
42 #define I2CCDR &I2C_Addr[4]
44 #define MPC107_CCR_MEN 0x80
45 #define MPC107_CCR_MIEN 0x40
46 #define MPC107_CCR_MSTA 0x20
47 #define MPC107_CCR_MTX 0x10
48 #define MPC107_CCR_TXAK 0x08
49 #define MPC107_CCR_RSTA 0x04
51 #define MPC107_CSR_MCF 0x80
52 #define MPC107_CSR_MAAS 0x40
53 #define MPC107_CSR_MBB 0x20
54 #define MPC107_CSR_MAL 0x10
55 #define MPC107_CSR_SRW 0x04
56 #define MPC107_CSR_MIF 0x02
57 #define MPC107_CSR_RXAK 0x01
62 /* taken from linux include/asm-ppc/io.h */
63 inline unsigned in_le32 (volatile unsigned *addr
)
67 __asm__
__volatile__ ("lwbrx %0,0,%1;\n"
69 "isync":"=r" (ret
): "r" (addr
), "m" (*addr
));
73 inline void out_le32 (volatile unsigned *addr
, int val
)
75 __asm__
__volatile__ ("stwbrx %1,0,%2; eieio":"=m" (*addr
):"r" (val
),
79 #define writel(val, addr) out_le32(addr, val)
80 #define readl(addr) in_le32(addr)
82 void i2c_init (int speed
, int slaveadd
)
84 /* stop I2C controller */
87 writel (0x1020, I2CFDR
);
88 /* write slave address */
89 writel (slaveadd
, I2CADR
);
90 /* clear status register */
92 /* start I2C controller */
93 writel (MPC107_CCR_MEN
, I2CCCR
);
98 static __inline__
int i2c_wait4bus (void)
100 ulong timeval
= get_timer (0);
102 while (readl (I2CCSR
) & MPC107_CSR_MBB
)
103 if (get_timer (timeval
) > TIMEOUT
)
109 static __inline__
int i2c_wait (int write
)
112 ulong timeval
= get_timer (0);
115 csr
= readl (I2CCSR
);
117 if (!(csr
& MPC107_CSR_MIF
))
120 writel (0x0, I2CCSR
);
122 if (csr
& MPC107_CSR_MAL
) {
124 printf ("i2c_wait: MAL\n");
129 if (!(csr
& MPC107_CSR_MCF
)) {
131 printf ("i2c_wait: unfinished\n");
136 if (write
== I2C_WRITE
&& (csr
& MPC107_CSR_RXAK
)) {
138 printf ("i2c_wait: No RXACK\n");
144 } while (get_timer (timeval
) < TIMEOUT
);
147 printf ("i2c_wait: timed out\n");
152 static __inline__
int i2c_write_addr (u8 dev
, u8 dir
, int rsta
)
154 writel (MPC107_CCR_MEN
| MPC107_CCR_MSTA
| MPC107_CCR_MTX
|
155 (rsta
? MPC107_CCR_RSTA
: 0), I2CCCR
);
157 writel ((dev
<< 1) | dir
, I2CCDR
);
159 if (i2c_wait (I2C_WRITE
) < 0)
165 static __inline__
int __i2c_write (u8
* data
, int length
)
169 writel (MPC107_CCR_MEN
| MPC107_CCR_MSTA
| MPC107_CCR_MTX
, I2CCCR
);
171 for (i
= 0; i
< length
; i
++) {
172 writel (data
[i
], I2CCDR
);
174 if (i2c_wait (I2C_WRITE
) < 0)
181 static __inline__
int __i2c_read (u8
* data
, int length
)
185 writel (MPC107_CCR_MEN
| MPC107_CCR_MSTA
|
186 ((length
== 1) ? MPC107_CCR_TXAK
: 0), I2CCCR
);
191 for (i
= 0; i
< length
; i
++) {
192 if (i2c_wait (I2C_READ
) < 0)
195 /* Generate ack on last next to last byte */
197 writel (MPC107_CCR_MEN
| MPC107_CCR_MSTA
|
198 MPC107_CCR_TXAK
, I2CCCR
);
200 /* Generate stop on last byte */
202 writel (MPC107_CCR_MEN
| MPC107_CCR_TXAK
, I2CCCR
);
204 data
[i
] = readl (I2CCDR
);
210 int i2c_read (u8 dev
, uint addr
, int alen
, u8
* data
, int length
)
213 u8
*a
= (u8
*) & addr
;
215 if (i2c_wait4bus () < 0)
218 if (i2c_write_addr (dev
, I2C_WRITE
, 0) == 0)
221 if (__i2c_write (&a
[4 - alen
], alen
) != alen
)
224 if (i2c_write_addr (dev
, I2C_READ
, 1) == 0)
227 i
= __i2c_read (data
, length
);
230 writel (MPC107_CCR_MEN
, I2CCCR
);
232 return !(i
== length
);
235 int i2c_write (u8 dev
, uint addr
, int alen
, u8
* data
, int length
)
238 u8
*a
= (u8
*) & addr
;
240 if (i2c_wait4bus () < 0)
243 if (i2c_write_addr (dev
, I2C_WRITE
, 0) == 0)
246 if (__i2c_write (&a
[4 - alen
], alen
) != alen
)
249 i
= __i2c_write (data
, length
);
252 writel (MPC107_CCR_MEN
, I2CCCR
);
254 return !(i
== length
);
257 int i2c_probe (uchar chip
)
262 * Try to read the first location of the chip. The underlying
263 * driver doesn't appear to support sending just the chip address
264 * and looking for an <ACK> back.
267 return i2c_read (chip
, 0, 1, (uchar
*) &tmp
, 1);
270 uchar
i2c_reg_read (uchar i2c_addr
, uchar reg
)
274 i2c_read (i2c_addr
, reg
, 1, buf
, 1);
279 void i2c_reg_write (uchar i2c_addr
, uchar reg
, uchar val
)
281 i2c_write (i2c_addr
, reg
, 1, &val
, 1);
284 #endif /* CONFIG_HARD_I2C */