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,
42 #ifdef CONFIG_HARD_I2C
45 #define TIMEOUT (CFG_HZ/4)
47 #define I2C_Addr ((unsigned *)(CFG_CCSRBAR + 0x3000))
49 #define I2CADR &I2C_Addr[0]
50 #define I2CFDR &I2C_Addr[1]
51 #define I2CCCR &I2C_Addr[2]
52 #define I2CCSR &I2C_Addr[3]
53 #define I2CCDR &I2C_Addr[4]
54 #define I2CDFSRR &I2C_Addr[5]
59 /* taken from linux include/asm-ppc/io.h */
60 inline unsigned in_le32(volatile unsigned *addr)
64 __asm__ __volatile__("lwbrx %0,0,%1;\n"
66 "isync" : "=r" (ret) :
67 "r" (addr), "m" (*addr));
71 inline void out_le32(volatile unsigned *addr, int val)
73 __asm__ __volatile__("stwbrx %1,0,%2; eieio" : "=m" (*addr) :
74 "r" (val), "r" (addr));
77 #define writel(val, addr) out_le32(addr, val)
78 #define readl(addr) in_le32(addr)
81 i2c_init(int speed, int slaveadd)
83 /* stop I2C controller */
86 writel (0x3f, I2CFDR);
87 /* set default filter */
88 writel (0x10,I2CDFSRR);
89 /* write slave address */
90 writel (slaveadd, I2CADR);
91 /* clear status register */
93 /* start I2C controller */
94 writel (MPC85xx_I2CCR_MEN, I2CCCR);
100 ulong timeval = get_timer (0);
102 while (readl (I2CCSR) & MPC85xx_I2CSR_MBB)
103 if (get_timer (timeval) > TIMEOUT)
109 static __inline__ int
113 ulong timeval = get_timer (0);
117 csr = readl (I2CCSR);
119 if (!(csr & MPC85xx_I2CSR_MIF))
122 writel (0x0, I2CCSR);
124 if (csr & MPC85xx_I2CSR_MAL)
126 DEB(printf ("i2c_wait: MAL\n"));
130 if (!(csr & MPC85xx_I2CSR_MCF))
132 DEB(printf ("i2c_wait: unfinished\n"));
136 if (write == I2C_WRITE && (csr & MPC85xx_I2CSR_RXAK))
138 DEB(printf ("i2c_wait: No RXACK\n"));
143 } while (get_timer (timeval) < TIMEOUT);
145 DEB(printf ("i2c_wait: timed out\n"));
149 static __inline__ int
150 i2c_write_addr (u8 dev, u8 dir, int rsta)
152 writel (MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_MSTA | MPC85xx_I2CCR_MTX |
153 (rsta?MPC85xx_I2CCR_RSTA:0), I2CCCR);
155 writel ((dev << 1) | dir, I2CCDR);
157 if (i2c_wait (I2C_WRITE) < 0)
163 static __inline__ int
164 __i2c_write (u8 *data, int length)
168 writel (MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_MSTA | MPC85xx_I2CCR_MTX, I2CCCR);
170 for (i=0; i < length; i++)
172 writel (data[i], I2CCDR);
174 if (i2c_wait (I2C_WRITE) < 0)
181 static __inline__ int
182 __i2c_read (u8 *data, int length)
186 writel (MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_MSTA |
187 ((length == 1) ? MPC85xx_I2CCR_TXAK : 0), I2CCCR);
192 for (i=0; i < length; i++)
194 if (i2c_wait (I2C_READ) < 0)
197 /* Generate ack on last next to last byte */
199 writel (MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_MSTA |
200 MPC85xx_I2CCR_TXAK, I2CCCR);
202 /* Generate stop on last byte */
204 writel (MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_TXAK, I2CCCR);
206 data[i] = readl (I2CCDR);
213 i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
218 if (i2c_wait4bus () < 0)
221 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
224 if (__i2c_write (&a[4 - alen], alen) != alen)
227 if (i2c_write_addr (dev, I2C_READ, 1) == 0)
230 i = __i2c_read (data, length);
233 writel (MPC85xx_I2CCR_MEN, I2CCCR);
235 return !(i == length);
239 i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
244 if (i2c_wait4bus () < 0)
247 if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
250 if (__i2c_write (&a[4 - alen], alen) != alen)
253 i = __i2c_write (data, length);
256 writel (MPC85xx_I2CCR_MEN, I2CCCR);
258 return !(i == length);
261 int i2c_probe (uchar chip)
266 * Try to read the first location of the chip. The underlying
267 * driver doesn't appear to support sending just the chip address
268 * and looking for an <ACK> back.
271 return i2c_read (chip, 0, 1, (char *)&tmp, 1);
274 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
278 i2c_read (i2c_addr, reg, 1, buf, 1);
283 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
285 i2c_write (i2c_addr, reg, 1, &val, 1);
288 #endif /* CONFIG_HARD_I2C */