]> git.sur5r.net Git - u-boot/blob - drivers/fsl_i2c.c
Moved i2c driver out of cpu/mpc86xx/i2c.c into drivers/fsl_i2c.c
[u-boot] / drivers / fsl_i2c.c
1 /*
2  * Copyright 2006 Freescale Semiconductor, Inc.
3  *
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.
7  *
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.
12  *
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
17  */
18
19
20 #include <common.h>
21 #include <command.h>
22
23 #ifdef CONFIG_HARD_I2C
24
25 #include <asm/io.h>
26 #include <asm/fsl_i2c.h>
27
28 #define I2C_TIMEOUT     (CFG_HZ / 4)
29
30 #define I2C     ((struct fsl_i2c *)(CFG_IMMR + CFG_I2C_OFFSET))
31
32
33 void
34 i2c_init(int speed, int slaveadd)
35 {
36         /* stop I2C controller */
37         writeb(0x0 , &I2C->cr);
38
39         /* set clock */
40         writeb(0x3f, &I2C->fdr);
41
42         /* set default filter */
43         writeb(0x10, &I2C->dfsrr);
44
45         /* write slave address */
46         writeb(slaveadd, &I2C->adr);
47
48         /* clear status register */
49         writeb(0x0, &I2C->sr);
50
51         /* start I2C controller */
52         writeb(I2C_CR_MEN, &I2C->cr);
53 }
54
55 static __inline__ int
56 i2c_wait4bus(void)
57 {
58         ulong timeval = get_timer (0);
59
60         while (readb(&I2C->sr) & I2C_SR_MBB) {
61                 if (get_timer(timeval) > I2C_TIMEOUT) {
62                         return -1;
63                 }
64         }
65
66         return 0;
67 }
68
69 static __inline__ int
70 i2c_wait(int write)
71 {
72         u32 csr;
73         ulong timeval = get_timer(0);
74
75         do {
76                 csr = readb(&I2C->sr);
77                 if (!(csr & I2C_SR_MIF))
78                         continue;
79
80                 writeb(0x0, &I2C->sr);
81
82                 if (csr & I2C_SR_MAL) {
83                         debug("i2c_wait: MAL\n");
84                         return -1;
85                 }
86
87                 if (!(csr & I2C_SR_MCF))        {
88                         debug("i2c_wait: unfinished\n");
89                         return -1;
90                 }
91
92                 if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
93                         debug("i2c_wait: No RXACK\n");
94                         return -1;
95                 }
96
97                 return 0;
98         } while (get_timer (timeval) < I2C_TIMEOUT);
99
100         debug("i2c_wait: timed out\n");
101         return -1;
102 }
103
104 static __inline__ int
105 i2c_write_addr (u8 dev, u8 dir, int rsta)
106 {
107         writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
108                | (rsta ? I2C_CR_RSTA : 0),
109                &I2C->cr);
110
111         writeb((dev << 1) | dir, &I2C->dr);
112
113         if (i2c_wait(I2C_WRITE) < 0)
114                 return 0;
115
116         return 1;
117 }
118
119 static __inline__ int
120 __i2c_write(u8 *data, int length)
121 {
122         int i;
123
124         writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
125                &I2C->cr);
126
127         for (i = 0; i < length; i++) {
128                 writeb(data[i], &I2C->dr);
129
130                 if (i2c_wait(I2C_WRITE) < 0)
131                         break;
132         }
133
134         return i;
135 }
136
137 static __inline__ int
138 __i2c_read(u8 *data, int length)
139 {
140         int i;
141
142         writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
143                &I2C->cr);
144
145         /* dummy read */
146         readb(&I2C->dr);
147
148         for (i = 0; i < length; i++) {
149                 if (i2c_wait(I2C_READ) < 0)
150                         break;
151
152                 /* Generate ack on last next to last byte */
153                 if (i == length - 2)
154                         writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
155                                &I2C->cr);
156
157                 /* Generate stop on last byte */
158                 if (i == length - 1)
159                         writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
160
161                 data[i] = readb(&I2C->dr);
162         }
163
164         return i;
165 }
166
167 int
168 i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
169 {
170         int i = 0;
171         u8 *a = (u8*)&addr;
172
173         if (i2c_wait4bus () < 0)
174                 goto exit;
175
176         if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
177                 goto exit;
178
179         if (__i2c_write(&a[4 - alen], alen) != alen)
180                 goto exit;
181
182         if (i2c_write_addr(dev, I2C_READ, 1) == 0)
183                 goto exit;
184
185         i = __i2c_read(data, length);
186
187  exit:
188         writeb(I2C_CR_MEN, &I2C->cr);
189
190         return !(i == length);
191 }
192
193 int
194 i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
195 {
196         int i = 0;
197         u8 *a = (u8*)&addr;
198
199         if (i2c_wait4bus() < 0)
200                 goto exit;
201
202         if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
203                 goto exit;
204
205         if (__i2c_write(&a[4 - alen], alen) != alen)
206                 goto exit;
207
208         i = __i2c_write(data, length);
209
210  exit:
211         writeb(I2C_CR_MEN, &I2C->cr);
212
213         return !(i == length);
214 }
215
216 int
217 i2c_probe(uchar chip)
218 {
219         int tmp;
220
221         /*
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.
225          */
226         udelay(10000);
227
228         return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
229 }
230
231 uchar
232 i2c_reg_read(uchar i2c_addr, uchar reg)
233 {
234         uchar buf[1];
235
236         i2c_read(i2c_addr, reg, 1, buf, 1);
237
238         return buf[0];
239 }
240
241 void
242 i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
243 {
244         i2c_write(i2c_addr, reg, 1, &val, 1);
245 }
246
247 #endif /* CONFIG_HARD_I2C */