]>
git.ipfire.org Git - people/ms/u-boot.git/blob - cpu/mpc85xx/i2c.c
2 * (C) Copyright 2003,Motorola Inc.
3 * Xianghua Xiao <x.xiao@motorola.com>
4 * Adapted for Motorola 85xx chip.
7 * Gleb Natapov <gnatapov@mrv.com>
8 * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
10 * Hardware I2C driver for MPC107 PCI bridge.
12 * See file CREDITS for list of people who contributed to this
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 #ifdef CONFIG_HARD_I2C
38 #define TIMEOUT (CFG_HZ/4)
40 #define I2C_Addr ((u8 *)(CFG_CCSRBAR + 0x3000))
42 #define I2CADR &I2C_Addr[0]
43 #define I2CFDR &I2C_Addr[4]
44 #define I2CCCR &I2C_Addr[8]
45 #define I2CCSR &I2C_Addr[12]
46 #define I2CCDR &I2C_Addr[16]
47 #define I2CDFSRR &I2C_Addr[20]
53 i2c_init(int speed
, int slaveadd
)
55 /* stop I2C controller */
61 /* set default filter */
62 writeb(0x10,I2CDFSRR
);
64 /* write slave address */
65 writeb(slaveadd
, I2CADR
);
67 /* clear status register */
70 /* start I2C controller */
71 writeb(MPC85xx_I2CCR_MEN
, I2CCCR
);
77 ulong timeval
= get_timer (0);
79 while (readb(I2CCSR
) & MPC85xx_I2CSR_MBB
) {
80 if (get_timer (timeval
) > TIMEOUT
) {
92 ulong timeval
= get_timer (0);
97 if (!(csr
& MPC85xx_I2CSR_MIF
))
102 if (csr
& MPC85xx_I2CSR_MAL
) {
103 debug("i2c_wait: MAL\n");
107 if (!(csr
& MPC85xx_I2CSR_MCF
)) {
108 debug("i2c_wait: unfinished\n");
112 if (write
== I2C_WRITE
&& (csr
& MPC85xx_I2CSR_RXAK
)) {
113 debug("i2c_wait: No RXACK\n");
118 } while (get_timer (timeval
) < TIMEOUT
);
120 debug("i2c_wait: timed out\n");
124 static __inline__
int
125 i2c_write_addr (u8 dev
, u8 dir
, int rsta
)
127 writeb(MPC85xx_I2CCR_MEN
| MPC85xx_I2CCR_MSTA
| MPC85xx_I2CCR_MTX
|
128 (rsta
?MPC85xx_I2CCR_RSTA
:0),
131 writeb((dev
<< 1) | dir
, I2CCDR
);
133 if (i2c_wait (I2C_WRITE
) < 0)
139 static __inline__
int
140 __i2c_write (u8
*data
, int length
)
144 writeb(MPC85xx_I2CCR_MEN
| MPC85xx_I2CCR_MSTA
| MPC85xx_I2CCR_MTX
,
147 for (i
=0; i
< length
; i
++) {
148 writeb(data
[i
], I2CCDR
);
150 if (i2c_wait (I2C_WRITE
) < 0)
157 static __inline__
int
158 __i2c_read (u8
*data
, int length
)
162 writeb(MPC85xx_I2CCR_MEN
| MPC85xx_I2CCR_MSTA
|
163 ((length
== 1) ? MPC85xx_I2CCR_TXAK
: 0),
169 for (i
=0; i
< length
; i
++) {
170 if (i2c_wait (I2C_READ
) < 0)
173 /* Generate ack on last next to last byte */
175 writeb(MPC85xx_I2CCR_MEN
| MPC85xx_I2CCR_MSTA
|
179 /* Generate stop on last byte */
181 writeb(MPC85xx_I2CCR_MEN
| MPC85xx_I2CCR_TXAK
, I2CCCR
);
183 data
[i
] = readb(I2CCDR
);
190 i2c_read (u8 dev
, uint addr
, int alen
, u8
*data
, int length
)
195 if (i2c_wait4bus () < 0)
198 if (i2c_write_addr (dev
, I2C_WRITE
, 0) == 0)
201 if (__i2c_write (&a
[4 - alen
], alen
) != alen
)
204 if (i2c_write_addr (dev
, I2C_READ
, 1) == 0)
207 i
= __i2c_read (data
, length
);
210 writeb(MPC85xx_I2CCR_MEN
, I2CCCR
);
212 return !(i
== length
);
216 i2c_write (u8 dev
, uint addr
, int alen
, u8
*data
, int length
)
221 if (i2c_wait4bus () < 0)
224 if (i2c_write_addr (dev
, I2C_WRITE
, 0) == 0)
227 if (__i2c_write (&a
[4 - alen
], alen
) != alen
)
230 i
= __i2c_write (data
, length
);
233 writeb(MPC85xx_I2CCR_MEN
, I2CCCR
);
235 return !(i
== length
);
238 int i2c_probe (uchar chip
)
243 * Try to read the first location of the chip. The underlying
244 * driver doesn't appear to support sending just the chip address
245 * and looking for an <ACK> back.
248 return i2c_read (chip
, 0, 1, (uchar
*)&tmp
, 1);
251 uchar
i2c_reg_read (uchar i2c_addr
, uchar reg
)
255 i2c_read (i2c_addr
, reg
, 1, buf
, 1);
260 void i2c_reg_write (uchar i2c_addr
, uchar reg
, uchar val
)
262 i2c_write (i2c_addr
, reg
, 1, &val
, 1);
265 #endif /* CONFIG_HARD_I2C */