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