1
2
3
4
5
6
7
8
9
10#include <common.h>
11#include <i2c.h>
12#include <linux/errno.h>
13#include <asm/io.h>
14#include <linux/bitops.h>
15#include <linux/compat.h>
16#ifdef CONFIG_DM_I2C
17#include <dm.h>
18#endif
19
20DECLARE_GLOBAL_DATA_PTR;
21
22
23
24
25
26
27#ifndef CONFIG_DM_I2C
28#if defined(CONFIG_ORION5X)
29#include <asm/arch/orion5x.h>
30#elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
31#include <asm/arch/soc.h>
32#elif defined(CONFIG_ARCH_SUNXI)
33#include <asm/arch/i2c.h>
34#else
35#error Driver mvtwsi not supported by SoC or board
36#endif
37#endif
38
39
40
41
42
43#if defined(CONFIG_DM_I2C) && defined(CONFIG_ARCH_SUNXI)
44#include <asm/arch/i2c.h>
45#endif
46
47
48
49
50
51#ifdef CONFIG_ARCH_SUNXI
52
53struct mvtwsi_registers {
54 u32 slave_address;
55 u32 xtnd_slave_addr;
56 u32 data;
57 u32 control;
58 u32 status;
59 u32 baudrate;
60 u32 soft_reset;
61 u32 debug;
62};
63
64#else
65
66struct mvtwsi_registers {
67 u32 slave_address;
68 u32 data;
69 u32 control;
70 union {
71 u32 status;
72 u32 baudrate;
73 };
74 u32 xtnd_slave_addr;
75 u32 reserved0[2];
76 u32 soft_reset;
77 u32 reserved1[27];
78 u32 debug;
79};
80
81#endif
82
83#ifdef CONFIG_DM_I2C
84struct mvtwsi_i2c_dev {
85
86 struct mvtwsi_registers *base;
87
88 int index;
89
90 u8 slaveadd;
91
92 uint speed;
93
94 uint tick;
95};
96#endif
97
98
99
100
101
102enum mvtwsi_ctrl_register_fields {
103
104 MVTWSI_CONTROL_ACK = 0x00000004,
105
106 MVTWSI_CONTROL_IFLG = 0x00000008,
107
108 MVTWSI_CONTROL_STOP = 0x00000010,
109
110 MVTWSI_CONTROL_START = 0x00000020,
111
112 MVTWSI_CONTROL_TWSIEN = 0x00000040,
113
114 MVTWSI_CONTROL_INTEN = 0x00000080,
115};
116
117
118
119
120
121
122#ifdef CONFIG_SUNXI_GEN_SUN6I
123#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
124#else
125#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
126#endif
127
128
129
130
131
132
133
134
135
136
137
138
139enum mvstwsi_status_values {
140
141 MVTWSI_STATUS_START = 0x08,
142
143 MVTWSI_STATUS_REPEATED_START = 0x10,
144
145 MVTWSI_STATUS_ADDR_W_ACK = 0x18,
146
147 MVTWSI_STATUS_DATA_W_ACK = 0x28,
148
149 MVTWSI_STATUS_ADDR_R_ACK = 0x40,
150
151 MVTWSI_STATUS_ADDR_R_NAK = 0x48,
152
153 MVTWSI_STATUS_DATA_R_ACK = 0x50,
154
155 MVTWSI_STATUS_DATA_R_NAK = 0x58,
156
157 MVTWSI_STATUS_IDLE = 0xF8,
158};
159
160
161
162
163
164enum mvtwsi_ack_flags {
165
166 MVTWSI_READ_NAK = 0,
167
168 MVTWSI_READ_ACK = 1,
169};
170
171
172
173
174
175
176
177inline uint calc_tick(uint speed)
178{
179
180
181 return (1000000000u / speed) + 100;
182}
183
184#ifndef CONFIG_DM_I2C
185
186
187
188
189
190
191
192static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
193{
194 switch (adap->hwadapnr) {
195#ifdef CONFIG_I2C_MVTWSI_BASE0
196 case 0:
197 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
198#endif
199#ifdef CONFIG_I2C_MVTWSI_BASE1
200 case 1:
201 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
202#endif
203#ifdef CONFIG_I2C_MVTWSI_BASE2
204 case 2:
205 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
206#endif
207#ifdef CONFIG_I2C_MVTWSI_BASE3
208 case 3:
209 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
210#endif
211#ifdef CONFIG_I2C_MVTWSI_BASE4
212 case 4:
213 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
214#endif
215#ifdef CONFIG_I2C_MVTWSI_BASE5
216 case 5:
217 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
218#endif
219 default:
220 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
221 break;
222 }
223
224 return NULL;
225}
226#endif
227
228
229
230
231enum mvtwsi_error_class {
232
233 MVTWSI_ERROR_WRONG_STATUS = 0x01,
234
235 MVTWSI_ERROR_TIMEOUT = 0x02,
236};
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
252{
253 return ((ec << 24) & 0xFF000000)
254 | ((lc << 16) & 0x00FF0000)
255 | ((ls << 8) & 0x0000FF00)
256 | (es & 0xFF);
257}
258
259
260
261
262
263
264
265static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
266 uint tick)
267{
268 int control, status;
269 int timeout = 1000;
270
271 do {
272 control = readl(&twsi->control);
273 if (control & MVTWSI_CONTROL_IFLG) {
274 status = readl(&twsi->status);
275 if (status == expected_status)
276 return 0;
277 else
278 return mvtwsi_error(
279 MVTWSI_ERROR_WRONG_STATUS,
280 control, status, expected_status);
281 }
282 ndelay(tick);
283 } while (timeout--);
284 status = readl(&twsi->status);
285 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
286 expected_status);
287}
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
303 uint tick)
304{
305
306 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
307 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
308
309 return twsi_wait(twsi, expected_status, tick);
310}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
326 int expected_status, uint tick)
327{
328
329 writel(byte, &twsi->data);
330
331 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
332 &twsi->control);
333
334 return twsi_wait(twsi, expected_status, tick);
335}
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
351 uint tick)
352{
353 int expected_status, status, control;
354
355
356 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
357 MVTWSI_STATUS_DATA_R_NAK;
358
359 control = MVTWSI_CONTROL_TWSIEN;
360 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
361 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
362
363 status = twsi_wait(twsi, expected_status, tick);
364
365 if (status == 0)
366 *byte = readl(&twsi->data);
367 return status;
368}
369
370
371
372
373
374
375
376
377
378
379
380
381static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
382{
383 int control, stop_status;
384 int status = 0;
385 int timeout = 1000;
386
387
388 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
389 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
390
391 do {
392 stop_status = readl(&twsi->status);
393 if (stop_status == MVTWSI_STATUS_IDLE)
394 break;
395 ndelay(tick);
396 } while (timeout--);
397 control = readl(&twsi->control);
398 if (stop_status != MVTWSI_STATUS_IDLE)
399 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
400 control, status, MVTWSI_STATUS_IDLE);
401 return status;
402}
403
404
405
406
407
408
409
410
411static uint twsi_calc_freq(const int n, const int m)
412{
413#ifdef CONFIG_ARCH_SUNXI
414 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
415#else
416 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
417#endif
418}
419
420
421
422
423
424
425
426
427
428static void twsi_reset(struct mvtwsi_registers *twsi)
429{
430
431 writel(0, &twsi->soft_reset);
432
433 udelay(20000);
434}
435
436
437
438
439
440
441
442
443
444
445
446
447static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
448 uint requested_speed)
449{
450 uint tmp_speed, highest_speed, n, m;
451 uint baud = 0x44;
452
453 highest_speed = 0;
454
455
456
457 for (n = 0; n < 8; n++) {
458 for (m = 0; m < 16; m++) {
459 tmp_speed = twsi_calc_freq(n, m);
460 if ((tmp_speed <= requested_speed) &&
461 (tmp_speed > highest_speed)) {
462 highest_speed = tmp_speed;
463 baud = (m << 3) | n;
464 }
465 }
466 }
467 writel(baud, &twsi->baudrate);
468
469
470#ifdef CONFIG_DM_I2C
471 ndelay(calc_tick(highest_speed));
472#else
473 ndelay(10000);
474#endif
475 return highest_speed;
476}
477
478
479
480
481
482
483
484
485
486
487
488
489
490static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
491 int slaveadd, uint *actual_speed)
492{
493 uint tmp_speed;
494
495
496 twsi_reset(twsi);
497
498 tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
499 if (actual_speed)
500 *actual_speed = tmp_speed;
501
502 writel(slaveadd, &twsi->slave_address);
503 writel(0, &twsi->xtnd_slave_addr);
504
505#ifdef CONFIG_DM_I2C
506 (void) twsi_stop(twsi, calc_tick(*actual_speed));
507#else
508 (void) twsi_stop(twsi, 10000);
509#endif
510}
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
530 u8 addr, uint tick)
531{
532 int status, expected_addr_status;
533
534
535
536 if (addr & 1)
537 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
538 else
539 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
540
541 status = twsi_start(twsi, expected_start_status, tick);
542
543 if (status == 0)
544 status = twsi_send(twsi, addr, expected_addr_status, tick);
545
546 return status;
547}
548
549
550
551
552
553
554
555
556
557
558
559
560
561static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
562 uint tick)
563{
564 u8 dummy_byte;
565 int status;
566
567
568 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
569
570 if (status == 0)
571 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
572
573 twsi_stop(twsi, tick);
574
575 return status;
576}
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
601 u8 *addr, int alen, uchar *data, int length,
602 uint tick)
603{
604 int status = 0;
605 int stop_status;
606 int expected_start = MVTWSI_STATUS_START;
607
608 if (alen > 0) {
609
610 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
611
612 while ((status == 0) && alen--)
613 status = twsi_send(twsi, addr[alen],
614 MVTWSI_STATUS_DATA_W_ACK, tick);
615
616 expected_start = MVTWSI_STATUS_REPEATED_START;
617 }
618
619 if (status == 0)
620 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
621
622
623 while ((status == 0) && length--)
624 status = twsi_recv(twsi, data++,
625 length > 0 ?
626 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
627
628 stop_status = twsi_stop(twsi, tick);
629
630 return status != 0 ? status : stop_status;
631}
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
650 u8 *addr, int alen, uchar *data, int length,
651 uint tick)
652{
653 int status, stop_status;
654
655
656
657 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
658
659 while ((status == 0) && (alen-- > 0))
660 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
661 tick);
662
663 while ((status == 0) && (length-- > 0))
664 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
665 tick);
666
667 stop_status = twsi_stop(twsi, tick);
668
669 return status != 0 ? status : stop_status;
670}
671
672#ifndef CONFIG_DM_I2C
673static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
674 int slaveadd)
675{
676 struct mvtwsi_registers *twsi = twsi_get_base(adap);
677 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
678}
679
680static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
681 uint requested_speed)
682{
683 struct mvtwsi_registers *twsi = twsi_get_base(adap);
684 __twsi_i2c_set_bus_speed(twsi, requested_speed);
685 return 0;
686}
687
688static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
689{
690 struct mvtwsi_registers *twsi = twsi_get_base(adap);
691 return __twsi_i2c_probe_chip(twsi, chip, 10000);
692}
693
694static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
695 int alen, uchar *data, int length)
696{
697 struct mvtwsi_registers *twsi = twsi_get_base(adap);
698 u8 addr_bytes[4];
699
700 addr_bytes[0] = (addr >> 0) & 0xFF;
701 addr_bytes[1] = (addr >> 8) & 0xFF;
702 addr_bytes[2] = (addr >> 16) & 0xFF;
703 addr_bytes[3] = (addr >> 24) & 0xFF;
704
705 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
706 10000);
707}
708
709static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
710 int alen, uchar *data, int length)
711{
712 struct mvtwsi_registers *twsi = twsi_get_base(adap);
713 u8 addr_bytes[4];
714
715 addr_bytes[0] = (addr >> 0) & 0xFF;
716 addr_bytes[1] = (addr >> 8) & 0xFF;
717 addr_bytes[2] = (addr >> 16) & 0xFF;
718 addr_bytes[3] = (addr >> 24) & 0xFF;
719
720 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
721 10000);
722}
723
724#ifdef CONFIG_I2C_MVTWSI_BASE0
725U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
726 twsi_i2c_read, twsi_i2c_write,
727 twsi_i2c_set_bus_speed,
728 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
729#endif
730#ifdef CONFIG_I2C_MVTWSI_BASE1
731U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
732 twsi_i2c_read, twsi_i2c_write,
733 twsi_i2c_set_bus_speed,
734 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
735
736#endif
737#ifdef CONFIG_I2C_MVTWSI_BASE2
738U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
739 twsi_i2c_read, twsi_i2c_write,
740 twsi_i2c_set_bus_speed,
741 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
742
743#endif
744#ifdef CONFIG_I2C_MVTWSI_BASE3
745U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
746 twsi_i2c_read, twsi_i2c_write,
747 twsi_i2c_set_bus_speed,
748 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
749
750#endif
751#ifdef CONFIG_I2C_MVTWSI_BASE4
752U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
753 twsi_i2c_read, twsi_i2c_write,
754 twsi_i2c_set_bus_speed,
755 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
756
757#endif
758#ifdef CONFIG_I2C_MVTWSI_BASE5
759U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
760 twsi_i2c_read, twsi_i2c_write,
761 twsi_i2c_set_bus_speed,
762 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
763
764#endif
765#else
766
767static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
768 u32 chip_flags)
769{
770 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
771 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
772}
773
774static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
775{
776 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
777
778 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
779 dev->tick = calc_tick(dev->speed);
780
781 return 0;
782}
783
784static int mvtwsi_i2c_ofdata_to_platdata(struct udevice *bus)
785{
786 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
787
788 dev->base = devfdt_get_addr_ptr(bus);
789
790 if (!dev->base)
791 return -ENOMEM;
792
793 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
794 "cell-index", -1);
795 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
796 "u-boot,i2c-slave-addr", 0x0);
797 dev->speed = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
798 "clock-frequency", 100000);
799 return 0;
800}
801
802static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
803{
804 clrbits_le32(&twsi->debug, BIT(18));
805}
806
807static int mvtwsi_i2c_bind(struct udevice *bus)
808{
809 struct mvtwsi_registers *twsi = devfdt_get_addr_ptr(bus);
810
811
812 if ((IS_ENABLED(CONFIG_ARMADA_38X) || IS_ENABLED(CONFIG_KIRKWOOD))
813 && bus->req_seq == 0)
814 twsi_disable_i2c_slave(twsi);
815
816 return 0;
817}
818
819static int mvtwsi_i2c_probe(struct udevice *bus)
820{
821 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
822 uint actual_speed;
823
824 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
825 dev->speed = actual_speed;
826 dev->tick = calc_tick(dev->speed);
827 return 0;
828}
829
830static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
831{
832 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
833 struct i2c_msg *dmsg, *omsg, dummy;
834
835 memset(&dummy, 0, sizeof(struct i2c_msg));
836
837
838
839 if (nmsgs > 2 || nmsgs == 0) {
840 debug("%s: Only one or two messages are supported.", __func__);
841 return -1;
842 }
843
844 omsg = nmsgs == 1 ? &dummy : msg;
845 dmsg = nmsgs == 1 ? msg : msg + 1;
846
847 if (dmsg->flags & I2C_M_RD)
848 return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
849 omsg->len, dmsg->buf, dmsg->len,
850 dev->tick);
851 else
852 return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
853 omsg->len, dmsg->buf, dmsg->len,
854 dev->tick);
855}
856
857static const struct dm_i2c_ops mvtwsi_i2c_ops = {
858 .xfer = mvtwsi_i2c_xfer,
859 .probe_chip = mvtwsi_i2c_probe_chip,
860 .set_bus_speed = mvtwsi_i2c_set_bus_speed,
861};
862
863static const struct udevice_id mvtwsi_i2c_ids[] = {
864 { .compatible = "marvell,mv64xxx-i2c", },
865 { .compatible = "marvell,mv78230-i2c", },
866 { .compatible = "allwinner,sun6i-a31-i2c", },
867 { }
868};
869
870U_BOOT_DRIVER(i2c_mvtwsi) = {
871 .name = "i2c_mvtwsi",
872 .id = UCLASS_I2C,
873 .of_match = mvtwsi_i2c_ids,
874 .bind = mvtwsi_i2c_bind,
875 .probe = mvtwsi_i2c_probe,
876 .ofdata_to_platdata = mvtwsi_i2c_ofdata_to_platdata,
877 .priv_auto_alloc_size = sizeof(struct mvtwsi_i2c_dev),
878 .ops = &mvtwsi_i2c_ops,
879};
880#endif
881