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,
21 #ifdef CONFIG_HARD_I2C
25 #include <asm/fsl_i2c.h>
27 #define I2C_TIMEOUT (CFG_HZ / 4)
28 #define I2C ((struct fsl_i2c *)(CFG_IMMR + CFG_I2C_OFFSET))
32 i2c_init(int speed, int slaveadd)
34 /* stop I2C controller */
35 writeb(0x0 , &I2C->cr);
38 writeb(0x3f, &I2C->fdr);
40 /* set default filter */
41 writeb(0x10, &I2C->dfsrr);
43 /* write slave address */
44 writeb(slaveadd, &I2C->adr);
46 /* clear status register */
47 writeb(0x0, &I2C->sr);
49 /* start I2C controller */
50 writeb(I2C_CR_MEN, &I2C->cr);
56 ulong timeval = get_timer (0);
58 while (readb(&I2C->sr) & I2C_SR_MBB) {
59 if (get_timer(timeval) > I2C_TIMEOUT) {
71 ulong timeval = get_timer(0);
74 csr = readb(&I2C->sr);
75 if (!(csr & I2C_SR_MIF))
78 writeb(0x0, &I2C->sr);
80 if (csr & I2C_SR_MAL) {
81 debug("i2c_wait: MAL\n");
85 if (!(csr & I2C_SR_MCF)) {
86 debug("i2c_wait: unfinished\n");
90 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
91 debug("i2c_wait: No RXACK\n");
96 } while (get_timer (timeval) < I2C_TIMEOUT);
98 debug("i2c_wait: timed out\n");
102 static __inline__ int
103 i2c_write_addr (u8 dev, u8 dir, int rsta)
105 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
106 | (rsta ? I2C_CR_RSTA : 0),
109 writeb((dev << 1) | dir, &I2C->dr);
111 if (i2c_wait(I2C_WRITE) < 0)
117 static __inline__ int
118 __i2c_write(u8 *data, int length)
122 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
125 for (i = 0; i < length; i++) {
126 writeb(data[i], &I2C->dr);
128 if (i2c_wait(I2C_WRITE) < 0)
135 static __inline__ int
136 __i2c_read(u8 *data, int length)
140 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
146 for (i = 0; i < length; i++) {
147 if (i2c_wait(I2C_READ) < 0)
150 /* Generate ack on last next to last byte */
152 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
155 /* Generate stop on last byte */
157 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
159 data[i] = readb(&I2C->dr);
166 i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
171 if (i2c_wait4bus() >= 0
172 && i2c_write_addr(dev, I2C_WRITE, 0) != 0
173 && __i2c_write(&a[4 - alen], alen) == alen
174 && i2c_write_addr(dev, I2C_READ, 1) != 0) {
175 i = __i2c_read(data, length);
178 writeb(I2C_CR_MEN, &I2C->cr);
187 i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
192 if (i2c_wait4bus() >= 0
193 && i2c_write_addr(dev, I2C_WRITE, 0) != 0
194 && __i2c_write(&a[4 - alen], alen) == alen) {
195 i = __i2c_write(data, length);
198 writeb(I2C_CR_MEN, &I2C->cr);
207 i2c_probe(uchar chip)
212 * Try to read the first location of the chip. The underlying
213 * driver doesn't appear to support sending just the chip address
214 * and looking for an <ACK> back.
218 return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
222 i2c_reg_read(uchar i2c_addr, uchar reg)
226 i2c_read(i2c_addr, reg, 1, buf, 1);
232 i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
234 i2c_write(i2c_addr, reg, 1, &val, 1);
237 #endif /* CONFIG_HARD_I2C */