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,
22 #ifdef CONFIG_HARD_I2C
25 #include <i2c.h> /* Functional interface */
28 #include <asm/fsl_i2c.h> /* HW definitions */
30 #define I2C_TIMEOUT (CFG_HZ / 4)
31 #define I2C ((struct fsl_i2c *)(CFG_IMMR + CFG_I2C_OFFSET))
35 i2c_init(int speed, int slaveadd)
37 /* stop I2C controller */
38 writeb(0x0, &I2C->cr);
41 writeb(0x3f, &I2C->fdr);
43 /* set default filter */
44 writeb(0x10, &I2C->dfsrr);
46 /* write slave address */
47 writeb(slaveadd, &I2C->adr);
49 /* clear status register */
50 writeb(0x0, &I2C->sr);
52 /* start I2C controller */
53 writeb(I2C_CR_MEN, &I2C->cr);
59 ulong timeval = get_timer(0);
61 while (readb(&I2C->sr) & I2C_SR_MBB) {
62 if (get_timer(timeval) > I2C_TIMEOUT) {
74 ulong timeval = get_timer(0);
77 csr = readb(&I2C->sr);
78 if (!(csr & I2C_SR_MIF))
81 writeb(0x0, &I2C->sr);
83 if (csr & I2C_SR_MAL) {
84 debug("i2c_wait: MAL\n");
88 if (!(csr & I2C_SR_MCF)) {
89 debug("i2c_wait: unfinished\n");
93 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
94 debug("i2c_wait: No RXACK\n");
99 } while (get_timer (timeval) < I2C_TIMEOUT);
101 debug("i2c_wait: timed out\n");
105 static __inline__ int
106 i2c_write_addr (u8 dev, u8 dir, int rsta)
108 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
109 | (rsta ? I2C_CR_RSTA : 0),
112 writeb((dev << 1) | dir, &I2C->dr);
114 if (i2c_wait(I2C_WRITE) < 0)
120 static __inline__ int
121 __i2c_write(u8 *data, int length)
125 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
128 for (i = 0; i < length; i++) {
129 writeb(data[i], &I2C->dr);
131 if (i2c_wait(I2C_WRITE) < 0)
138 static __inline__ int
139 __i2c_read(u8 *data, int length)
143 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
149 for (i = 0; i < length; i++) {
150 if (i2c_wait(I2C_READ) < 0)
153 /* Generate ack on last next to last byte */
155 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
158 /* Generate stop on last byte */
160 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
162 data[i] = readb(&I2C->dr);
169 i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
174 if (i2c_wait4bus() >= 0
175 && i2c_write_addr(dev, I2C_WRITE, 0) != 0
176 && __i2c_write(&a[4 - alen], alen) == alen
177 && i2c_write_addr(dev, I2C_READ, 1) != 0) {
178 i = __i2c_read(data, length);
181 writeb(I2C_CR_MEN, &I2C->cr);
190 i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
195 if (i2c_wait4bus() >= 0
196 && i2c_write_addr(dev, I2C_WRITE, 0) != 0
197 && __i2c_write(&a[4 - alen], alen) == alen) {
198 i = __i2c_write(data, length);
201 writeb(I2C_CR_MEN, &I2C->cr);
210 i2c_probe(uchar chip)
215 * Try to read the first location of the chip. The underlying
216 * driver doesn't appear to support sending just the chip address
217 * and looking for an <ACK> back.
221 return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
225 i2c_reg_read(uchar i2c_addr, uchar reg)
229 i2c_read(i2c_addr, reg, 1, buf, 1);
235 i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
237 i2c_write(i2c_addr, reg, 1, &val, 1);
240 #endif /* CONFIG_HARD_I2C */
241 #endif /* CONFIG_FSL_I2C */