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 * Modified for MPC86xx by Jeff Brown
12 * Hardware I2C driver for MPC107 PCI bridge.
14 * See file CREDITS for list of people who contributed to this
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 #ifdef CONFIG_HARD_I2C
40 #define TIMEOUT (CFG_HZ/4)
42 #define I2C_Addr ((u8 *)(CFG_CCSRBAR + 0x3100))
44 #define I2CADR &I2C_Addr[0]
45 #define I2CFDR &I2C_Addr[4]
46 #define I2CCCR &I2C_Addr[8]
47 #define I2CCSR &I2C_Addr[12]
48 #define I2CCDR &I2C_Addr[16]
49 #define I2CDFSRR &I2C_Addr[20]
55 i2c_init(int speed, int slaveadd)
57 /* stop I2C controller */
63 /* set default filter */
64 writeb(0x10,I2CDFSRR);
66 /* write slave address */
67 writeb(slaveadd, I2CADR);
69 /* clear status register */
72 /* start I2C controller */
73 writeb(MPC86xx_I2CCR_MEN, I2CCCR);
79 ulong timeval = get_timer (0);
81 while (readb(I2CCSR) & MPC86xx_I2CSR_MBB) {
82 if (get_timer(timeval) > TIMEOUT) {
94 ulong timeval = get_timer (0);
98 if (!(csr & MPC86xx_I2CSR_MIF))
103 if (csr & MPC86xx_I2CSR_MAL) {
104 debug("i2c_wait: MAL\n");
108 if (!(csr & MPC86xx_I2CSR_MCF)) {
109 debug("i2c_wait: unfinished\n");
113 if (write == I2C_WRITE && (csr & MPC86xx_I2CSR_RXAK)) {
114 debug("i2c_wait: No RXACK\n");
119 } while (get_timer(timeval) < TIMEOUT);
121 debug("i2c_wait: timed out\n");
125 static __inline__ int
126 i2c_write_addr (u8 dev, u8 dir, int rsta)
128 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX
129 | (rsta ? MPC86xx_I2CCR_RSTA : 0),
132 writeb((dev << 1) | dir, I2CCDR);
134 if (i2c_wait(I2C_WRITE) < 0)
140 static __inline__ int
141 __i2c_write (u8 *data, int length)
145 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX,
148 for (i = 0; i < length; i++) {
149 writeb(data[i], I2CCDR);
151 if (i2c_wait(I2C_WRITE) < 0)
158 static __inline__ int
159 __i2c_read (u8 *data, int length)
163 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA
164 | ((length == 1) ? MPC86xx_I2CCR_TXAK : 0),
170 for (i = 0; i < length; i++) {
171 if (i2c_wait(I2C_READ) < 0)
174 /* Generate ack on last next to last byte */
176 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA
177 | MPC86xx_I2CCR_TXAK,
180 /* Generate stop on last byte */
182 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_TXAK, I2CCCR);
184 data[i] = readb(I2CCDR);
191 i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
196 if (i2c_wait4bus() < 0)
199 if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
202 if (__i2c_write(&a[4 - alen], alen) != alen)
205 if (i2c_write_addr(dev, I2C_READ, 1) == 0)
208 i = __i2c_read(data, length);
211 writeb(MPC86xx_I2CCR_MEN, I2CCCR);
213 return !(i == length);
217 i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
222 if (i2c_wait4bus() < 0)
225 if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
228 if (__i2c_write(&a[4 - alen], alen) != alen)
231 i = __i2c_write(data, length);
234 writeb(MPC86xx_I2CCR_MEN, I2CCCR);
236 return !(i == length);
239 int i2c_probe (uchar chip)
244 * Try to read the first location of the chip. The underlying
245 * driver doesn't appear to support sending just the chip address
246 * and looking for an <ACK> back.
250 return i2c_read(chip, 0, 1, (char *)&tmp, 1);
253 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
257 i2c_read(i2c_addr, reg, 1, buf, 1);
262 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
264 i2c_write(i2c_addr, reg, 1, &val, 1);
267 #endif /* CONFIG_HARD_I2C */