2 * Copyright 2006 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * Version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 #ifdef CONFIG_HARD_I2C
26 #include <asm/fsl_i2c.h>
28 #define I2C_TIMEOUT (CFG_HZ / 4)
30 #define I2C ((struct fsl_i2c *)(CFG_IMMR + CFG_I2C_OFFSET))
34 i2c_init(int speed, int slaveadd)
36 /* stop I2C controller */
37 writeb(0x0 , &I2C->cr);
40 writeb(0x3f, &I2C->fdr);
42 /* set default filter */
43 writeb(0x10, &I2C->dfsrr);
45 /* write slave address */
46 writeb(slaveadd, &I2C->adr);
48 /* clear status register */
49 writeb(0x0, &I2C->sr);
51 /* start I2C controller */
52 writeb(I2C_CR_MEN, &I2C->cr);
58 ulong timeval = get_timer (0);
60 while (readb(&I2C->sr) & I2C_SR_MBB) {
61 if (get_timer(timeval) > I2C_TIMEOUT) {
73 ulong timeval = get_timer(0);
76 csr = readb(&I2C->sr);
77 if (!(csr & I2C_SR_MIF))
80 writeb(0x0, &I2C->sr);
82 if (csr & I2C_SR_MAL) {
83 debug("i2c_wait: MAL\n");
87 if (!(csr & I2C_SR_MCF)) {
88 debug("i2c_wait: unfinished\n");
92 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
93 debug("i2c_wait: No RXACK\n");
98 } while (get_timer (timeval) < I2C_TIMEOUT);
100 debug("i2c_wait: timed out\n");
104 static __inline__ int
105 i2c_write_addr (u8 dev, u8 dir, int rsta)
107 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
108 | (rsta ? I2C_CR_RSTA : 0),
111 writeb((dev << 1) | dir, &I2C->dr);
113 if (i2c_wait(I2C_WRITE) < 0)
119 static __inline__ int
120 __i2c_write(u8 *data, int length)
124 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
127 for (i = 0; i < length; i++) {
128 writeb(data[i], &I2C->dr);
130 if (i2c_wait(I2C_WRITE) < 0)
137 static __inline__ int
138 __i2c_read(u8 *data, int length)
142 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
148 for (i = 0; i < length; i++) {
149 if (i2c_wait(I2C_READ) < 0)
152 /* Generate ack on last next to last byte */
154 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
157 /* Generate stop on last byte */
159 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
161 data[i] = readb(&I2C->dr);
168 i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
173 if (i2c_wait4bus () < 0)
176 if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
179 if (__i2c_write(&a[4 - alen], alen) != alen)
182 if (i2c_write_addr(dev, I2C_READ, 1) == 0)
185 i = __i2c_read(data, length);
188 writeb(I2C_CR_MEN, &I2C->cr);
190 return !(i == length);
194 i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
199 if (i2c_wait4bus() < 0)
202 if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
205 if (__i2c_write(&a[4 - alen], alen) != alen)
208 i = __i2c_write(data, length);
211 writeb(I2C_CR_MEN, &I2C->cr);
213 return !(i == length);
217 i2c_probe(uchar chip)
222 * Try to read the first location of the chip. The underlying
223 * driver doesn't appear to support sending just the chip address
224 * and looking for an <ACK> back.
228 return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
232 i2c_reg_read(uchar i2c_addr, uchar reg)
236 i2c_read(i2c_addr, reg, 1, buf, 1);
242 i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
244 i2c_write(i2c_addr, reg, 1, &val, 1);
247 #endif /* CONFIG_HARD_I2C */