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 (jeffrey@freescale.com)
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 // debug("I2C: Wait for bus\n");
82 while (readb(I2CCSR) & MPC86xx_I2CSR_MBB) {
83 if (get_timer (timeval) > TIMEOUT) {
95 ulong timeval = get_timer (0);
100 if (!(csr & MPC86xx_I2CSR_MIF))
105 if (csr & MPC86xx_I2CSR_MAL) {
106 debug("i2c_wait: MAL\n");
110 if (!(csr & MPC86xx_I2CSR_MCF)) {
111 debug("i2c_wait: unfinished\n");
115 if (write == I2C_WRITE && (csr & MPC86xx_I2CSR_RXAK)) {
116 debug("i2c_wait: No RXACK\n");
121 } while (get_timer (timeval) < TIMEOUT);
123 debug("i2c_wait: timed out\n");
127 static __inline__ int
128 i2c_write_addr (u8 dev, u8 dir, int rsta)
130 // debug("I2C: Write Addr\n");
131 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX |
132 (rsta?MPC86xx_I2CCR_RSTA:0),
135 writeb((dev << 1) | dir, I2CCDR);
137 if (i2c_wait (I2C_WRITE) < 0)
143 static __inline__ int
144 __i2c_write (u8 *data, int length)
147 // debug("I2C: __i2c_write\n");
148 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX,
151 for (i=0; i < length; i++) {
152 writeb(data[i], I2CCDR);
154 if (i2c_wait (I2C_WRITE) < 0)
161 static __inline__ int
162 __i2c_read (u8 *data, int length)
166 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA |
167 ((length == 1) ? MPC86xx_I2CCR_TXAK : 0),
172 // debug("length = %d\n", length);
174 for (i=0; i < length; i++) {
175 if (i2c_wait (I2C_READ) < 0)
178 /* Generate ack on last next to last byte */
180 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA |
184 /* Generate stop on last byte */
186 writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_TXAK, I2CCCR);
188 // debug("I2CCR = 0x%08x\n", readb(I2CCCR));
189 data[i] = readb(I2CCDR);
190 // debug("data[i] = 0x%08x\n", data[i]);
192 // debug("Returning i = %d\n", i);
197 i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
202 if (i2c_wait4bus () < 0)
205 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
208 if (__i2c_write (&a[4 - alen], alen) != alen)
211 if (i2c_write_addr (dev, I2C_READ, 1) == 0)
214 i = __i2c_read (data, length);
217 writeb(MPC86xx_I2CCR_MEN, I2CCCR);
219 return !(i == length);
223 i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
228 if (i2c_wait4bus () < 0)
231 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
234 if (__i2c_write (&a[4 - alen], alen) != alen)
237 i = __i2c_write (data, length);
240 writeb(MPC86xx_I2CCR_MEN, I2CCCR);
242 return !(i == length);
245 int i2c_probe (uchar chip)
250 * Try to read the first location of the chip. The underlying
251 * driver doesn't appear to support sending just the chip address
252 * and looking for an <ACK> back.
256 return i2c_read (chip, 0, 1, (char *)&tmp, 1);
259 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
263 i2c_read (i2c_addr, reg, 1, buf, 1);
268 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
270 i2c_write (i2c_addr, reg, 1, &val, 1);
273 #endif /* CONFIG_HARD_I2C */