]> git.sur5r.net Git - u-boot/blob - cpu/mpc86xx/i2c.c
General indent and whitespace cleanups.
[u-boot] / cpu / mpc86xx / i2c.c
1 /*
2  * (C) Copyright 2003,Motorola Inc.
3  * Xianghua Xiao <x.xiao@motorola.com>
4  * Adapted for Motorola 85xx chip.
5  *
6  * (C) Copyright 2003
7  * Gleb Natapov <gnatapov@mrv.com>
8  * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
9  *
10  * Modified for MPC86xx by Jeff Brown
11  *
12  * Hardware I2C driver for MPC107 PCI bridge.
13  *
14  * See file CREDITS for list of people who contributed to this
15  * project.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30  * MA 02111-1307 USA
31  */
32
33 #include <common.h>
34 #include <command.h>
35 #include <asm/io.h>
36
37 #ifdef CONFIG_HARD_I2C
38 #include <i2c.h>
39
40 #define TIMEOUT (CFG_HZ/4)
41
42 #define I2C_Addr ((u8 *)(CFG_CCSRBAR + 0x3100))
43
44 #define I2CADR  &I2C_Addr[0]
45 #define I2CFDR  &I2C_Addr[4]
46 #define I2CCCR  &I2C_Addr[8]
47 #define I2CCSR  &I2C_Addr[12]
48 #define I2CCDR  &I2C_Addr[16]
49 #define I2CDFSRR &I2C_Addr[20]
50
51 #define I2C_READ  1
52 #define I2C_WRITE 0
53
54 void
55 i2c_init(int speed, int slaveadd)
56 {
57         /* stop I2C controller */
58         writeb(0x0, I2CCCR);
59
60         /* set clock */
61         writeb(0x3f, I2CFDR);
62
63         /* set default filter */
64         writeb(0x10, I2CDFSRR);
65
66         /* write slave address */
67         writeb(slaveadd, I2CADR);
68
69         /* clear status register */
70         writeb(0x0, I2CCSR);
71
72         /* start I2C controller */
73         writeb(MPC86xx_I2CCR_MEN, I2CCCR);
74 }
75
76 static __inline__ int
77 i2c_wait4bus(void)
78 {
79         ulong timeval = get_timer(0);
80
81         while (readb(I2CCSR) & MPC86xx_I2CSR_MBB) {
82                 if (get_timer(timeval) > TIMEOUT) {
83                         return -1;
84                 }
85         }
86
87         return 0;
88 }
89
90 static __inline__ int
91 i2c_wait(int write)
92 {
93         u32 csr;
94         ulong timeval = get_timer(0);
95
96         do {
97                 csr = readb(I2CCSR);
98                 if (!(csr & MPC86xx_I2CSR_MIF))
99                         continue;
100
101                 writeb(0x0, I2CCSR);
102
103                 if (csr & MPC86xx_I2CSR_MAL) {
104                         debug("i2c_wait: MAL\n");
105                         return -1;
106                 }
107
108                 if (!(csr & MPC86xx_I2CSR_MCF)) {
109                         debug("i2c_wait: unfinished\n");
110                         return -1;
111                 }
112
113                 if (write == I2C_WRITE && (csr & MPC86xx_I2CSR_RXAK)) {
114                         debug("i2c_wait: No RXACK\n");
115                         return -1;
116                 }
117
118                 return 0;
119         } while (get_timer(timeval) < TIMEOUT);
120
121         debug("i2c_wait: timed out\n");
122         return -1;
123 }
124
125 static __inline__ int
126 i2c_write_addr(u8 dev, u8 dir, int rsta)
127 {
128         writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX
129                | (rsta ? MPC86xx_I2CCR_RSTA : 0),
130                I2CCCR);
131
132         writeb((dev << 1) | dir, I2CCDR);
133
134         if (i2c_wait(I2C_WRITE) < 0)
135                 return 0;
136
137         return 1;
138 }
139
140 static __inline__ int
141 __i2c_write(u8 *data, int length)
142 {
143         int i;
144
145         writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA | MPC86xx_I2CCR_MTX,
146                I2CCCR);
147
148         for (i = 0; i < length; i++) {
149                 writeb(data[i], I2CCDR);
150
151                 if (i2c_wait(I2C_WRITE) < 0)
152                         break;
153         }
154
155         return i;
156 }
157
158 static __inline__ int
159 __i2c_read(u8 *data, int length)
160 {
161         int i;
162
163         writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA
164                | ((length == 1) ? MPC86xx_I2CCR_TXAK : 0),
165                I2CCCR);
166
167         /* dummy read */
168         readb(I2CCDR);
169
170         for (i = 0; i < length; i++) {
171                 if (i2c_wait(I2C_READ) < 0)
172                         break;
173
174                 /* Generate ack on last next to last byte */
175                 if (i == length - 2)
176                         writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_MSTA
177                                | MPC86xx_I2CCR_TXAK, I2CCCR);
178
179                 /* Generate stop on last byte */
180                 if (i == length - 1)
181                         writeb(MPC86xx_I2CCR_MEN | MPC86xx_I2CCR_TXAK, I2CCCR);
182
183                 data[i] = readb(I2CCDR);
184         }
185
186         return i;
187 }
188
189 int
190 i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
191 {
192         int i = 0;
193         u8 *a = (u8 *) &addr;
194
195         if (i2c_wait4bus() < 0)
196                 goto exit;
197
198         if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
199                 goto exit;
200
201         if (__i2c_write(&a[4 - alen], alen) != alen)
202                 goto exit;
203
204         if (i2c_write_addr(dev, I2C_READ, 1) == 0)
205                 goto exit;
206
207         i = __i2c_read(data, length);
208
209 exit:
210         writeb(MPC86xx_I2CCR_MEN, I2CCCR);
211
212         return !(i == length);
213 }
214
215 int
216 i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
217 {
218         int i = 0;
219         u8 *a = (u8 *) &addr;
220
221         if (i2c_wait4bus() < 0)
222                 goto exit;
223
224         if (i2c_write_addr(dev, I2C_WRITE, 0) == 0)
225                 goto exit;
226
227         if (__i2c_write(&a[4 - alen], alen) != alen)
228                 goto exit;
229
230         i = __i2c_write(data, length);
231
232 exit:
233         writeb(MPC86xx_I2CCR_MEN, I2CCCR);
234
235         return !(i == length);
236 }
237
238 int
239 i2c_probe(uchar chip)
240 {
241         int tmp;
242
243         /*
244          * Try to read the first location of the chip.  The underlying
245          * driver doesn't appear to support sending just the chip address
246          * and looking for an <ACK> back.
247          */
248         udelay(10000);
249
250         return i2c_read(chip, 0, 1, (char *)&tmp, 1);
251 }
252
253 uchar
254 i2c_reg_read(uchar i2c_addr, uchar reg)
255 {
256         char buf[1];
257
258         i2c_read(i2c_addr, reg, 1, buf, 1);
259
260         return buf[0];
261 }
262
263 void
264 i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
265 {
266         i2c_write(i2c_addr, reg, 1, &val, 1);
267 }
268
269 #endif /* CONFIG_HARD_I2C */