1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <common.h>
17#include <asm/ppc4xx.h>
18#include <asm/ppc4xx-i2c.h>
19#include <i2c.h>
20#include <asm/io.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24static inline struct ppc4xx_i2c *ppc4xx_get_i2c(int hwadapnr)
25{
26 unsigned long base;
27
28#if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
29 defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
30 defined(CONFIG_460EX) || defined(CONFIG_460GT)
31 base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000700 + (hwadapnr * 0x100);
32#elif defined(CONFIG_440) || defined(CONFIG_405EX)
33
34 base = CONFIG_SYS_PERIPHERAL_BASE + 0x00000400 + (hwadapnr * 0x100);
35#else
36
37 base = 0xEF600500 + (hwadapnr * 0x100);
38#endif
39 return (struct ppc4xx_i2c *)base;
40}
41
42static void _i2c_bus_reset(struct i2c_adapter *adap)
43{
44 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
45 int i;
46 u8 dc;
47
48
49
50 out_8(&i2c->sts, 0x0A);
51
52
53 out_8(&i2c->extsts, 0x8F);
54
55
56 out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
57
58
59 dc = in_8(&i2c->directcntl);
60 if (!DIRCTNL_FREE(dc)){
61
62 out_8(&i2c->directcntl, IIC_DIRCNTL_SDAC | IIC_DIRCNTL_SCC);
63
64
65 for (i = 0; i < 100; ++i) {
66 dc = in_8(&i2c->directcntl);
67 if (DIRCTNL_FREE(dc))
68 break;
69
70
71 dc ^= IIC_DIRCNTL_SCC;
72 out_8(&i2c->directcntl, dc);
73 udelay(10);
74 dc ^= IIC_DIRCNTL_SCC;
75 out_8(&i2c->directcntl, dc);
76 }
77 }
78
79
80 out_8(&i2c->xtcntlss, 0);
81}
82
83static void ppc4xx_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
84{
85 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
86 int val, divisor;
87
88#ifdef CONFIG_SYS_I2C_INIT_BOARD
89
90
91
92
93
94 i2c_init_board();
95#endif
96
97
98
99 _i2c_bus_reset(adap);
100
101
102 out_8(&i2c->lmadr, 0);
103
104
105 out_8(&i2c->hmadr, 0);
106
107
108 out_8(&i2c->lsadr, 0);
109
110
111 out_8(&i2c->hsadr, 0);
112
113
114
115 divisor = (get_OPB_freq() - 1) / 10000000;
116 if (divisor == 0)
117 divisor = 1;
118 out_8(&i2c->clkdiv, divisor);
119
120
121 out_8(&i2c->intrmsk, 0);
122
123
124 out_8(&i2c->xfrcnt, 0);
125
126
127
128 out_8(&i2c->xtcntlss, 0xF0);
129
130
131
132 out_8(&i2c->mdcntl, IIC_MDCNTL_FSDB | IIC_MDCNTL_FMDB);
133
134 val = in_8(&i2c->mdcntl);
135
136
137
138
139
140
141 val |= IIC_MDCNTL_EUBS | IIC_MDCNTL_HSCL;
142 if (speed >= 400000)
143 val |= IIC_MDCNTL_FSM;
144 out_8(&i2c->mdcntl, val);
145
146
147 out_8(&i2c->cntl, 0x00);
148}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174static int _i2c_transfer(struct i2c_adapter *adap,
175 unsigned char cmd_type,
176 unsigned char chip,
177 unsigned char addr[],
178 unsigned char addr_len,
179 unsigned char data[],
180 unsigned short data_len)
181{
182 struct ppc4xx_i2c *i2c = ppc4xx_get_i2c(adap->hwadapnr);
183 u8 *ptr;
184 int reading;
185 int tran, cnt;
186 int result;
187 int status;
188 int i;
189 u8 creg;
190
191 if (data == 0 || data_len == 0) {
192
193 printf( "i2c_transfer: bad call\n" );
194 return IIC_NOK;
195 }
196 if (addr && addr_len) {
197 ptr = addr;
198 cnt = addr_len;
199 reading = 0;
200 } else {
201 ptr = data;
202 cnt = data_len;
203 reading = cmd_type;
204 }
205
206
207 out_8(&i2c->sts, IIC_STS_SCMP);
208
209
210 i = 10;
211 do {
212
213 status = in_8(&i2c->sts);
214 i--;
215 } while ((status & IIC_STS_PT) && (i > 0));
216
217 if (status & IIC_STS_PT) {
218 result = IIC_NOK_TOUT;
219 return(result);
220 }
221
222
223 out_8(&i2c->mdcntl, in_8(&i2c->mdcntl) |
224 IIC_MDCNTL_FMDB | IIC_MDCNTL_FSDB);
225
226
227
228
229 out_8(&i2c->hmadr, 0);
230 out_8(&i2c->lmadr, chip);
231
232 tran = 0;
233 result = IIC_OK;
234 creg = 0;
235
236 while (tran != cnt && (result == IIC_OK)) {
237 int bc,j;
238
239
240
241
242
243
244 creg |= IIC_CNTL_PT;
245
246 bc = (cnt - tran) > 4 ? 4 : cnt - tran;
247 creg |= (bc - 1) << 4;
248
249 if ((!cmd_type && (ptr == addr)) || ((tran + bc) != cnt))
250 creg |= IIC_CNTL_CHT;
251
252
253 if (cmd_type && (ptr == addr) && ((tran + bc) == cnt))
254 creg |= IIC_CNTL_RPST;
255
256 if (reading) {
257 creg |= IIC_CNTL_READ;
258 } else {
259 for(j = 0; j < bc; j++) {
260
261 out_8(&i2c->mdbuf, ptr[tran + j]);
262 }
263 }
264 out_8(&i2c->cntl, creg);
265
266
267
268
269
270
271
272
273
274 i = 2 * 5 * 8;
275 do {
276
277 status = in_8(&i2c->sts);
278 udelay(10);
279 i--;
280 } while ((status & IIC_STS_PT) && !(status & IIC_STS_ERR) &&
281 (i > 0));
282
283 if (status & IIC_STS_ERR) {
284 result = IIC_NOK;
285 status = in_8(&i2c->extsts);
286
287 if (status & IIC_EXTSTS_LA)
288 result = IIC_NOK_LA;
289
290 if (status & IIC_EXTSTS_ICT)
291 result = IIC_NOK_ICT;
292
293 if (status & IIC_EXTSTS_XFRA)
294 result = IIC_NOK_XFRA;
295
296
297
298
299
300
301 if ((status & IIC_EXTSTS_BCS_MASK)
302 != IIC_EXTSTS_BCS_FREE){
303 u8 mdcntl = in_8(&i2c->mdcntl);
304
305
306 out_8(&i2c->xtcntlss, IIC_XTCNTLSS_SRST);
307 out_8(&i2c->directcntl, IIC_DIRCNTL_SCC);
308 udelay(10);
309 out_8(&i2c->directcntl,
310 IIC_DIRCNTL_SCC | IIC_DIRCNTL_SDAC);
311 out_8(&i2c->xtcntlss, 0);
312
313 ppc4xx_i2c_init(adap, (mdcntl & IIC_MDCNTL_FSM)
314 ? 400000 : 100000, 0);
315 }
316 } else if ( status & IIC_STS_PT) {
317 result = IIC_NOK_TOUT;
318 }
319
320
321 if ((reading) && (result == IIC_OK)) {
322
323 if (status & IIC_STS_MDBS) {
324
325
326
327
328
329
330
331 udelay(1);
332 for (j = 0; j < bc; j++)
333 ptr[tran + j] = in_8(&i2c->mdbuf);
334 } else
335 result = IIC_NOK_DATA;
336 }
337 creg = 0;
338 tran += bc;
339 if (ptr == addr && tran == cnt) {
340 ptr = data;
341 cnt = data_len;
342 tran = 0;
343 reading = cmd_type;
344 }
345 }
346 return result;
347}
348
349static int ppc4xx_i2c_probe(struct i2c_adapter *adap, uchar chip)
350{
351 uchar buf[1];
352
353 buf[0] = 0;
354
355
356
357
358
359
360 return (_i2c_transfer(adap, 1, chip << 1, 0, 0, buf, 1) != 0);
361}
362
363static int ppc4xx_i2c_transfer(struct i2c_adapter *adap, uchar chip, uint addr,
364 int alen, uchar *buffer, int len, int read)
365{
366 uchar xaddr[4];
367 int ret;
368
369 if (alen > 4) {
370 printf("I2C: addr len %d not supported\n", alen);
371 return 1;
372 }
373
374 if (alen > 0) {
375 xaddr[0] = (addr >> 24) & 0xFF;
376 xaddr[1] = (addr >> 16) & 0xFF;
377 xaddr[2] = (addr >> 8) & 0xFF;
378 xaddr[3] = addr & 0xFF;
379 }
380
381
382#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
383
384
385
386
387
388
389
390
391
392
393
394 if (alen > 0)
395 chip |= ((addr >> (alen * 8)) &
396 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
397#endif
398 ret = _i2c_transfer(adap, read, chip << 1, &xaddr[4 - alen], alen,
399 buffer, len);
400 if (ret) {
401 printf("I2C %s: failed %d\n", read ? "read" : "write", ret);
402 return 1;
403 }
404
405 return 0;
406}
407
408static int ppc4xx_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
409 int alen, uchar *buffer, int len)
410{
411 return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 1);
412}
413
414static int ppc4xx_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
415 int alen, uchar *buffer, int len)
416{
417 return ppc4xx_i2c_transfer(adap, chip, addr, alen, buffer, len, 0);
418}
419
420static unsigned int ppc4xx_i2c_set_bus_speed(struct i2c_adapter *adap,
421 unsigned int speed)
422{
423 if (speed != adap->speed)
424 return -1;
425 return speed;
426}
427
428
429
430
431#ifdef CONFIG_SYS_I2C_PPC4XX_CH0
432U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_0, ppc4xx_i2c_init, ppc4xx_i2c_probe,
433 ppc4xx_i2c_read, ppc4xx_i2c_write,
434 ppc4xx_i2c_set_bus_speed,
435 CONFIG_SYS_I2C_PPC4XX_SPEED_0,
436 CONFIG_SYS_I2C_PPC4XX_SLAVE_0, 0)
437#endif
438#ifdef CONFIG_SYS_I2C_PPC4XX_CH1
439U_BOOT_I2C_ADAP_COMPLETE(ppc4xx_1, ppc4xx_i2c_init, ppc4xx_i2c_probe,
440 ppc4xx_i2c_read, ppc4xx_i2c_write,
441 ppc4xx_i2c_set_bus_speed,
442 CONFIG_SYS_I2C_PPC4XX_SPEED_1,
443 CONFIG_SYS_I2C_PPC4XX_SLAVE_1, 1)
444#endif
445