]> git.ipfire.org Git - people/ms/u-boot.git/blob - cpu/mpc85xx/i2c.c
Cleanup for GCC-4.x
[people/ms/u-boot.git] / cpu / mpc85xx / 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 * Hardware I2C driver for MPC107 PCI bridge.
11 *
12 * See file CREDITS for list of people who contributed to this
13 * project.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * MA 02111-1307 USA
29 */
30
31 #include <common.h>
32 #include <command.h>
33 #include <asm/io.h>
34
35 #ifdef CONFIG_HARD_I2C
36 #include <i2c.h>
37
38 #define TIMEOUT (CFG_HZ/4)
39
40 #define I2C_Addr ((u8 *)(CFG_CCSRBAR + 0x3000))
41
42 #define I2CADR &I2C_Addr[0]
43 #define I2CFDR &I2C_Addr[4]
44 #define I2CCCR &I2C_Addr[8]
45 #define I2CCSR &I2C_Addr[12]
46 #define I2CCDR &I2C_Addr[16]
47 #define I2CDFSRR &I2C_Addr[20]
48
49 #define I2C_READ 1
50 #define I2C_WRITE 0
51
52 void
53 i2c_init(int speed, int slaveadd)
54 {
55 /* stop I2C controller */
56 writeb(0x0, I2CCCR);
57
58 /* set clock */
59 writeb(0x3f, I2CFDR);
60
61 /* set default filter */
62 writeb(0x10,I2CDFSRR);
63
64 /* write slave address */
65 writeb(slaveadd, I2CADR);
66
67 /* clear status register */
68 writeb(0x0, I2CCSR);
69
70 /* start I2C controller */
71 writeb(MPC85xx_I2CCR_MEN, I2CCCR);
72 }
73
74 static __inline__ int
75 i2c_wait4bus (void)
76 {
77 ulong timeval = get_timer (0);
78
79 while (readb(I2CCSR) & MPC85xx_I2CSR_MBB) {
80 if (get_timer (timeval) > TIMEOUT) {
81 return -1;
82 }
83 }
84
85 return 0;
86 }
87
88 static __inline__ int
89 i2c_wait (int write)
90 {
91 u32 csr;
92 ulong timeval = get_timer (0);
93
94 do {
95 csr = readb(I2CCSR);
96
97 if (!(csr & MPC85xx_I2CSR_MIF))
98 continue;
99
100 writeb(0x0, I2CCSR);
101
102 if (csr & MPC85xx_I2CSR_MAL) {
103 debug("i2c_wait: MAL\n");
104 return -1;
105 }
106
107 if (!(csr & MPC85xx_I2CSR_MCF)) {
108 debug("i2c_wait: unfinished\n");
109 return -1;
110 }
111
112 if (write == I2C_WRITE && (csr & MPC85xx_I2CSR_RXAK)) {
113 debug("i2c_wait: No RXACK\n");
114 return -1;
115 }
116
117 return 0;
118 } while (get_timer (timeval) < TIMEOUT);
119
120 debug("i2c_wait: timed out\n");
121 return -1;
122 }
123
124 static __inline__ int
125 i2c_write_addr (u8 dev, u8 dir, int rsta)
126 {
127 writeb(MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_MSTA | MPC85xx_I2CCR_MTX |
128 (rsta?MPC85xx_I2CCR_RSTA:0),
129 I2CCCR);
130
131 writeb((dev << 1) | dir, I2CCDR);
132
133 if (i2c_wait (I2C_WRITE) < 0)
134 return 0;
135
136 return 1;
137 }
138
139 static __inline__ int
140 __i2c_write (u8 *data, int length)
141 {
142 int i;
143
144 writeb(MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_MSTA | MPC85xx_I2CCR_MTX,
145 I2CCCR);
146
147 for (i=0; i < length; i++) {
148 writeb(data[i], I2CCDR);
149
150 if (i2c_wait (I2C_WRITE) < 0)
151 break;
152 }
153
154 return i;
155 }
156
157 static __inline__ int
158 __i2c_read (u8 *data, int length)
159 {
160 int i;
161
162 writeb(MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_MSTA |
163 ((length == 1) ? MPC85xx_I2CCR_TXAK : 0),
164 I2CCCR);
165
166 /* dummy read */
167 readb(I2CCDR);
168
169 for (i=0; i < length; i++) {
170 if (i2c_wait (I2C_READ) < 0)
171 break;
172
173 /* Generate ack on last next to last byte */
174 if (i == length - 2)
175 writeb(MPC85xx_I2CCR_MEN | MPC85xx_I2CCR_MSTA |
176 MPC85xx_I2CCR_TXAK,
177 I2CCCR);
178
179 /* Generate stop on last byte */
180 if (i == length - 1)
181 writeb(MPC85xx_I2CCR_MEN | MPC85xx_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(MPC85xx_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(MPC85xx_I2CCR_MEN, I2CCCR);
234
235 return !(i == length);
236 }
237
238 int i2c_probe (uchar chip)
239 {
240 int tmp;
241
242 /*
243 * Try to read the first location of the chip. The underlying
244 * driver doesn't appear to support sending just the chip address
245 * and looking for an <ACK> back.
246 */
247 udelay(10000);
248 return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
249 }
250
251 uchar i2c_reg_read (uchar i2c_addr, uchar reg)
252 {
253 uchar buf[1];
254
255 i2c_read (i2c_addr, reg, 1, buf, 1);
256
257 return (buf[0]);
258 }
259
260 void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
261 {
262 i2c_write (i2c_addr, reg, 1, &val, 1);
263 }
264
265 #endif /* CONFIG_HARD_I2C */