1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <common.h>
27#include <i2c.h>
28
29#include "u8500_i2c.h"
30#include <asm/io.h>
31#include <asm/arch/clock.h>
32
33#define U8500_I2C_ENDAD_COUNTER (CONFIG_SYS_HZ/100)
34#define U8500_I2C_FIFO_FLUSH_COUNTER 500000
35#define U8500_I2C_SCL_FREQ 100000
36#define U8500_I2C_INPUT_FREQ 48000000
37#define TX_FIFO_THRESHOLD 0x4
38#define RX_FIFO_THRESHOLD 0x4
39#define SLAVE_SETUP_TIME 14
40
41#define WRITE_FIELD(var, mask, shift, value) \
42 (var = ((var & ~(mask)) | ((value) << (shift))))
43
44static unsigned int bus_initialized[CONFIG_SYS_U8500_I2C_BUS_MAX];
45static unsigned int i2c_bus_num;
46static unsigned int i2c_bus_speed[] = {
47 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SPEED,
48 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SPEED
49};
50static struct u8500_i2c_regs *i2c_dev[] = {
51 (struct u8500_i2c_regs *)CONFIG_SYS_U8500_I2C0_BASE,
52 (struct u8500_i2c_regs *)CONFIG_SYS_U8500_I2C1_BASE,
53 (struct u8500_i2c_regs *)CONFIG_SYS_U8500_I2C2_BASE,
54 (struct u8500_i2c_regs *)CONFIG_SYS_U8500_I2C3_BASE,
55};
56
57static struct {
58 int periph;
59 int pcken;
60 int kcken;
61} i2c_clock_bits[] = {
62 {3, 3, 3},
63 {1, 2, 2},
64 {1, 6, 6},
65 {2, 0, 0},
66};
67
68static void i2c_set_bit(void *reg, u32 mask)
69{
70 writel(readl(reg) | mask, reg);
71}
72
73static void i2c_clr_bit(void *reg, u32 mask)
74{
75 writel(readl(reg) & ~mask, reg);
76}
77
78static void i2c_write_field(void *reg, u32 mask, uint shift, u32 value)
79{
80 writel((readl(reg) & ~mask) | (value << shift), reg);
81}
82
83static int __i2c_set_bus_speed(unsigned int speed)
84{
85 u32 value;
86 struct u8500_i2c_regs *i2c_regs;
87
88 i2c_regs = i2c_dev[i2c_bus_num];
89
90
91 i2c_write_field(&i2c_regs->cr, U8500_I2C_CR_SM,
92 U8500_I2C_CR_SHIFT_SM, 0x0);
93
94
95
96
97
98
99 value = (u32) (U8500_I2C_INPUT_FREQ / (speed * 2));
100 i2c_write_field(&i2c_regs->brcr, U8500_I2C_BRCR_BRCNT2,
101 U8500_I2C_BRCR_SHIFT_BRCNT2, value);
102
103
104 i2c_write_field(&i2c_regs->brcr, U8500_I2C_BRCR_BRCNT1,
105 U8500_I2C_BRCR_SHIFT_BRCNT1, 0);
106
107 return U8500_I2C_INPUT_FREQ/(value * 2);
108}
109
110
111
112
113
114
115
116
117
118void i2c_init(int speed, int slaveaddr)
119{
120 struct u8500_i2c_regs *i2c_regs;
121
122 debug("i2c_init bus %d, speed %d\n", i2c_bus_num, speed);
123
124 u8500_clock_enable(i2c_clock_bits[i2c_bus_num].periph,
125 i2c_clock_bits[i2c_bus_num].pcken,
126 i2c_clock_bits[i2c_bus_num].kcken);
127
128 i2c_regs = i2c_dev[i2c_bus_num];
129
130
131 i2c_clr_bit(&i2c_regs->cr, U8500_I2C_CR_PE);
132
133
134 writel(0, &i2c_regs->cr);
135 writel(0, &i2c_regs->scr);
136 writel(0, &i2c_regs->hsmcr);
137 writel(0, &i2c_regs->tftr);
138 writel(0, &i2c_regs->rftr);
139 writel(0, &i2c_regs->dmar);
140
141 i2c_bus_speed[i2c_bus_num] = __i2c_set_bus_speed(speed);
142
143
144
145
146
147 i2c_clr_bit(&i2c_regs->cr, U8500_I2C_CR_SAM);
148 i2c_write_field(&i2c_regs->scr, U8500_I2C_SCR_ADDR,
149 U8500_I2C_SCR_SHIFT_ADDR, slaveaddr);
150
151 i2c_write_field(&i2c_regs->scr, U8500_I2C_SCR_DATA_SETUP_TIME,
152 U8500_I2C_SCR_SHIFT_DATA_SETUP_TIME, SLAVE_SETUP_TIME);
153
154
155 i2c_write_field(&i2c_regs->cr, U8500_I2C_CR_DMA_SLE,
156 U8500_I2C_CR_SHIFT_DMA_SLE, 0);
157
158
159 writel(0, &i2c_regs->imscr);
160
161
162 i2c_write_field(&i2c_regs->cr, U8500_I2C_CR_OM, U8500_I2C_CR_SHIFT_OM,
163 U8500_I2C_BUS_MASTER_MODE);
164
165 writel(TX_FIFO_THRESHOLD, &i2c_regs->tftr);
166 writel(RX_FIFO_THRESHOLD, &i2c_regs->rftr);
167
168
169 i2c_set_bit(&i2c_regs->cr, U8500_I2C_CR_PE);
170
171 bus_initialized[i2c_bus_num] = 1;
172}
173
174
175
176
177
178
179
180
181static int loop_till_bit_clear(void *io_reg, u32 mask, unsigned long timeout)
182{
183 unsigned long timebase = get_timer(0);
184
185 do {
186 if ((readl(io_reg) & mask) == 0x0UL)
187 return 0;
188 } while (get_timer(timebase) < timeout);
189
190 debug("loop_till_bit_clear timed out\n");
191 return -1;
192}
193
194
195
196
197
198
199
200static int loop_till_bit_set(void *io_reg, u32 mask, unsigned long timeout)
201{
202 unsigned long timebase = get_timer(0);
203
204 do {
205 if ((readl(io_reg) & mask) != 0x0UL)
206 return 0;
207 } while (get_timer(timebase) < timeout);
208
209 debug("loop_till_bit_set timed out\n");
210 return -1;
211}
212
213
214
215
216static void flush_fifo(struct u8500_i2c_regs *i2c_regs)
217{
218 int counter = U8500_I2C_FIFO_FLUSH_COUNTER;
219
220
221 i2c_set_bit(&i2c_regs->cr, U8500_I2C_CR_FTX);
222
223 i2c_set_bit(&i2c_regs->cr, U8500_I2C_CR_FRX);
224 while (counter--) {
225 if (!(readl(&i2c_regs->cr) &
226 (U8500_I2C_CR_FTX | U8500_I2C_CR_FRX)))
227 break;
228 }
229 return;
230}
231
232#ifdef DEBUG
233static void print_abort_reason(struct u8500_i2c_regs *i2c_regs)
234{
235 int cause;
236
237 printf("abort: risr %08x, sr %08x\n", i2c_regs->risr, i2c_regs->sr);
238 cause = (readl(&i2c_regs->sr) & U8500_I2C_SR_CAUSE) >>
239 U8500_I2C_SR_SHIFT_CAUSE;
240 switch (cause) {
241 case U8500_I2C_NACK_ADDR:
242 printf("No Ack received after Slave Address xmission\n");
243 break;
244 case U8500_I2C_NACK_DATA:
245 printf("Valid for MASTER_WRITE: No Ack received "
246 "during data phase\n");
247 break;
248 case U8500_I2C_ACK_MCODE:
249 printf("Master recv ack after xmission of master code"
250 "in hs mode\n");
251 break;
252 case U8500_I2C_ARB_LOST:
253 printf("Master Lost arbitration\n");
254 break;
255 case U8500_I2C_BERR_START:
256 printf("Slave restarts\n");
257 break;
258 case U8500_I2C_BERR_STOP:
259 printf("Slave reset\n");
260 break;
261 case U8500_I2C_OVFL:
262 printf("Overflow\n");
263 break;
264 default:
265 printf("Unknown error type\n");
266 }
267}
268#endif
269
270
271
272
273static void i2c_abort(struct u8500_i2c_regs *i2c_regs)
274{
275#ifdef DEBUG
276 print_abort_reason(i2c_regs);
277#endif
278
279 flush_fifo(i2c_regs);
280
281
282 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTD);
283
284
285 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
286
287 i2c_init(i2c_bus_speed[i2c_bus_num], CONFIG_SYS_I2C_SLAVE);
288}
289
290
291
292
293static int i2c_write_addr(struct u8500_i2c_regs *i2c_regs, uint addr, int alen)
294{
295 while (alen--) {
296
297 if (loop_till_bit_clear((void *)&i2c_regs->risr,
298 U8500_I2C_INT_TXFF,
299 U8500_I2C_ENDAD_COUNTER)) {
300 i2c_abort(i2c_regs);
301 return -1;
302 }
303
304
305 writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->tfr);
306 }
307
308 return 0;
309}
310
311
312
313
314
315
316
317
318
319
320
321
322static int i2c_read_byte(struct u8500_i2c_regs *i2c_regs, uchar chip,
323 uint addr, int alen, uchar *value)
324{
325 u32 mcr = 0;
326
327
328 WRITE_FIELD(mcr, U8500_I2C_MCR_AM, U8500_I2C_MCR_SHIFT_AM, 1);
329
330
331 WRITE_FIELD(mcr, U8500_I2C_MCR_A7, U8500_I2C_MCR_SHIFT_A7, chip);
332
333 if (alen != 0) {
334
335 mcr &= ~(U8500_I2C_MCR_OP);
336
337
338 WRITE_FIELD(mcr, U8500_I2C_MCR_LENGTH,
339 U8500_I2C_MCR_SHIFT_LENGTH, 1);
340
341
342 mcr &= ~(U8500_I2C_MCR_STOP);
343
344
345 writel(mcr, &i2c_regs->mcr);
346
347
348 if (i2c_write_addr(i2c_regs, addr, alen) != 0)
349 return -1;
350
351
352 if (loop_till_bit_set((void *)&i2c_regs->risr,
353 U8500_I2C_INT_MTDWS,
354 U8500_I2C_ENDAD_COUNTER)) {
355 return -1;
356 }
357
358
359 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
360 }
361
362
363 mcr |= U8500_I2C_MCR_OP;
364
365
366 mcr |= U8500_I2C_MCR_STOP;
367
368
369 WRITE_FIELD(mcr, U8500_I2C_MCR_LENGTH, U8500_I2C_MCR_SHIFT_LENGTH, 1);
370
371 i2c_write_field(&i2c_regs->mcr, U8500_I2C_MCR_LENGTH_STOP_OP,
372 U8500_I2C_MCR_SHIFT_LENGTH_STOP_OP, mcr);
373
374
375
376
377
378
379 if (loop_till_bit_clear((void *)&i2c_regs->risr,
380 U8500_I2C_INT_RXFE,
381 U8500_I2C_ENDAD_COUNTER))
382 return -1;
383
384
385 *value = readb(&i2c_regs->rfr);
386
387
388 if (loop_till_bit_set((void *)&i2c_regs->risr, U8500_I2C_INT_MTD,
389 U8500_I2C_ENDAD_COUNTER))
390 return -1;
391
392
393 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTD);
394
395
396 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
397
398 return 0;
399}
400
401
402
403
404
405
406
407
408
409
410
411
412
413static int __i2c_write(struct u8500_i2c_regs *i2c_regs, u8 chip, uint addr,
414 int alen, u8 *data, int len)
415{
416 int i;
417 u32 mcr = 0;
418
419
420 WRITE_FIELD(mcr, U8500_I2C_MCR_AM, U8500_I2C_MCR_SHIFT_AM, 1);
421
422
423 WRITE_FIELD(mcr, U8500_I2C_MCR_A7, U8500_I2C_MCR_SHIFT_A7, chip);
424
425
426 mcr &= ~(U8500_I2C_MCR_OP);
427
428
429 mcr |= U8500_I2C_MCR_STOP;
430
431
432 WRITE_FIELD(mcr, U8500_I2C_MCR_LENGTH, U8500_I2C_MCR_SHIFT_LENGTH,
433 (alen + len));
434
435
436 writel(mcr, &i2c_regs->mcr);
437
438 if (i2c_write_addr(i2c_regs, addr, alen) != 0)
439 return -1;
440
441 for (i = 0; i < len; i++) {
442
443 if (loop_till_bit_clear((void *)&i2c_regs->risr,
444 U8500_I2C_INT_TXFF,
445 U8500_I2C_ENDAD_COUNTER))
446 return -1;
447
448
449 writeb(data[i], &i2c_regs->tfr);
450 }
451
452
453 if (loop_till_bit_set((void *)&i2c_regs->risr,
454 U8500_I2C_INT_MTD,
455 U8500_I2C_ENDAD_COUNTER)) {
456 printf("i2c_write_byte error2: risr %08x\n",
457 i2c_regs->risr);
458 return -1;
459 }
460
461
462 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTD);
463
464
465 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
466
467 return 0;
468}
469
470
471
472
473
474int i2c_probe(uchar chip)
475{
476 u32 mcr = 0;
477 struct u8500_i2c_regs *i2c_regs;
478
479 if (chip == CONFIG_SYS_I2C_SLAVE)
480 return 1;
481
482 i2c_regs = i2c_dev[i2c_bus_num];
483
484
485 WRITE_FIELD(mcr, U8500_I2C_MCR_AM, U8500_I2C_MCR_SHIFT_AM, 1);
486
487
488 WRITE_FIELD(mcr, U8500_I2C_MCR_A10, U8500_I2C_MCR_SHIFT_A7, chip);
489
490
491 mcr |= U8500_I2C_MCR_OP;
492
493
494 WRITE_FIELD(mcr, U8500_I2C_MCR_LENGTH, U8500_I2C_MCR_SHIFT_LENGTH, 1);
495
496
497 mcr |= U8500_I2C_MCR_STOP;
498
499
500 writel(mcr, &i2c_regs->mcr);
501
502
503 if (loop_till_bit_clear((void *)&i2c_regs->risr,
504 U8500_I2C_INT_RXFE,
505 U8500_I2C_ENDAD_COUNTER)) {
506 i2c_abort(i2c_regs);
507 return -1;
508 }
509
510 flush_fifo(i2c_regs);
511
512
513 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTD);
514
515
516 i2c_set_bit(&i2c_regs->icr, U8500_I2C_INT_MTDWS);
517
518 return 0;
519}
520
521
522
523
524
525
526
527
528
529
530
531
532
533int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
534{
535 int i;
536 int rc;
537 struct u8500_i2c_regs *i2c_regs;
538
539 if (alen > 2) {
540 debug("I2C read: addr len %d not supported\n", alen);
541 return 1;
542 }
543
544 i2c_regs = i2c_dev[i2c_bus_num];
545
546 for (i = 0; i < len; i++) {
547 rc = i2c_read_byte(i2c_regs, chip, addr + i, alen, &buffer[i]);
548 if (rc != 0) {
549 debug("I2C read: I/O error: %d\n", rc);
550 i2c_abort(i2c_regs);
551 return rc;
552 }
553 }
554
555 return 0;
556}
557
558int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
559{
560 int rc;
561 struct u8500_i2c_regs *i2c_regs;
562 i2c_regs = i2c_dev[i2c_bus_num];
563
564 rc = __i2c_write(i2c_regs, chip, addr, alen, buffer,
565 len);
566 if (rc != 0) {
567 debug("I2C write: I/O error\n");
568 i2c_abort(i2c_regs);
569 return rc;
570 }
571 return 0;
572}
573
574int i2c_set_bus_num(unsigned int bus)
575{
576 if (bus > ARRAY_SIZE(i2c_dev) - 1) {
577 debug("i2c_set_bus_num: only up to bus %d supported\n",
578 ARRAY_SIZE(i2c_dev)-1);
579 return -1;
580 }
581
582 i2c_bus_num = bus;
583
584 if (!bus_initialized[i2c_bus_num])
585 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
586
587 return 0;
588}
589
590int i2c_set_bus_speed(unsigned int speed)
591{
592
593 if (speed > U8500_I2C_MAX_STANDARD_SCL) {
594 debug("i2c_set_bus_speed: only up to %d supported\n",
595 U8500_I2C_MAX_STANDARD_SCL);
596 return -1;
597 }
598
599
600 i2c_init(speed, CONFIG_SYS_I2C_SLAVE);
601
602 return 0;
603}
604
605unsigned int i2c_get_bus_num(void)
606{
607 return i2c_bus_num;
608}
609
610unsigned int i2c_get_bus_speed(void)
611{
612 return i2c_bus_speed[i2c_bus_num];
613}
614