to make u-boot work for fat32 filesystem
[jz_uboot.git] / drivers / fsl_i2c.c
blob65c27439e36a4c320dd7989ea06dc9b96ac0b1d7
1 /*
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,
16 * MA 02111-1307 USA
19 #include <common.h>
21 #ifdef CONFIG_FSL_I2C
22 #ifdef CONFIG_HARD_I2C
24 #include <command.h>
25 #include <i2c.h> /* Functional interface */
27 #include <asm/io.h>
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))
34 void
35 i2c_init(int speed, int slaveadd)
37 /* stop I2C controller */
38 writeb(0x0, &I2C->cr);
40 /* set clock */
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);
56 static __inline__ int
57 i2c_wait4bus(void)
59 ulong timeval = get_timer(0);
61 while (readb(&I2C->sr) & I2C_SR_MBB) {
62 if (get_timer(timeval) > I2C_TIMEOUT) {
63 return -1;
67 return 0;
70 static __inline__ int
71 i2c_wait(int write)
73 u32 csr;
74 ulong timeval = get_timer(0);
76 do {
77 csr = readb(&I2C->sr);
78 if (!(csr & I2C_SR_MIF))
79 continue;
81 writeb(0x0, &I2C->sr);
83 if (csr & I2C_SR_MAL) {
84 debug("i2c_wait: MAL\n");
85 return -1;
88 if (!(csr & I2C_SR_MCF)) {
89 debug("i2c_wait: unfinished\n");
90 return -1;
93 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
94 debug("i2c_wait: No RXACK\n");
95 return -1;
98 return 0;
99 } while (get_timer (timeval) < I2C_TIMEOUT);
101 debug("i2c_wait: timed out\n");
102 return -1;
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),
110 &I2C->cr);
112 writeb((dev << 1) | dir, &I2C->dr);
114 if (i2c_wait(I2C_WRITE) < 0)
115 return 0;
117 return 1;
120 static __inline__ int
121 __i2c_write(u8 *data, int length)
123 int i;
125 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
126 &I2C->cr);
128 for (i = 0; i < length; i++) {
129 writeb(data[i], &I2C->dr);
131 if (i2c_wait(I2C_WRITE) < 0)
132 break;
135 return i;
138 static __inline__ int
139 __i2c_read(u8 *data, int length)
141 int i;
143 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
144 &I2C->cr);
146 /* dummy read */
147 readb(&I2C->dr);
149 for (i = 0; i < length; i++) {
150 if (i2c_wait(I2C_READ) < 0)
151 break;
153 /* Generate ack on last next to last byte */
154 if (i == length - 2)
155 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
156 &I2C->cr);
158 /* Generate stop on last byte */
159 if (i == length - 1)
160 writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
162 data[i] = readb(&I2C->dr);
165 return i;
169 i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
171 int i = 0;
172 u8 *a = (u8*)&addr;
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);
183 if (i == length)
184 return 0;
186 return -1;
190 i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
192 int i = 0;
193 u8 *a = (u8*)&addr;
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);
203 if (i == length)
204 return 0;
206 return -1;
210 i2c_probe(uchar chip)
212 int tmp;
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.
219 udelay(10000);
221 return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
224 uchar
225 i2c_reg_read(uchar i2c_addr, uchar reg)
227 uchar buf[1];
229 i2c_read(i2c_addr, reg, 1, buf, 1);
231 return buf[0];
234 void
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 */