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
27
28
29
30
31
32
33
34#include <linux/module.h>
35#include <linux/kernel.h>
36#include <linux/ioport.h>
37#include <linux/delay.h>
38#include <linux/slab.h>
39#include <linux/interrupt.h>
40#include <linux/sched/signal.h>
41
42#include <asm/irq.h>
43#include <linux/io.h>
44#include <linux/i2c.h>
45#include <linux/of_address.h>
46#include <linux/of_irq.h>
47#include <linux/of_platform.h>
48
49#include "i2c-ibm_iic.h"
50
51#define DRIVER_VERSION "2.2"
52
53MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
54MODULE_LICENSE("GPL");
55
56static bool iic_force_poll;
57module_param(iic_force_poll, bool, 0);
58MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
59
60static bool iic_force_fast;
61module_param(iic_force_fast, bool, 0);
62MODULE_PARM_DESC(iic_force_fast, "Force fast mode (400 kHz)");
63
64#define DBG_LEVEL 0
65
66#ifdef DBG
67#undef DBG
68#endif
69
70#ifdef DBG2
71#undef DBG2
72#endif
73
74#if DBG_LEVEL > 0
75# define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
76#else
77# define DBG(f,x...) ((void)0)
78#endif
79#if DBG_LEVEL > 1
80# define DBG2(f,x...) DBG(f, ##x)
81#else
82# define DBG2(f,x...) ((void)0)
83#endif
84#if DBG_LEVEL > 2
85static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
86{
87 volatile struct iic_regs __iomem *iic = dev->vaddr;
88 printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
89 printk(KERN_DEBUG
90 " cntl = 0x%02x, mdcntl = 0x%02x\n"
91 " sts = 0x%02x, extsts = 0x%02x\n"
92 " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
93 " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
94 in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
95 in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
96 in_8(&iic->xtcntlss), in_8(&iic->directcntl));
97}
98# define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
99#else
100# define DUMP_REGS(h,dev) ((void)0)
101#endif
102
103
104static struct ibm_iic_timings {
105 unsigned int hd_sta;
106 unsigned int su_sto;
107 unsigned int low;
108 unsigned int high;
109 unsigned int buf;
110} timings [] = {
111
112{
113 .hd_sta = 4000,
114 .su_sto = 4000,
115 .low = 4700,
116 .high = 4000,
117 .buf = 4700,
118},
119
120{
121 .hd_sta = 600,
122 .su_sto = 600,
123 .low = 1300,
124 .high = 600,
125 .buf = 1300,
126}};
127
128
129static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
130{
131 out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
132}
133
134
135
136
137static void iic_dev_init(struct ibm_iic_private* dev)
138{
139 volatile struct iic_regs __iomem *iic = dev->vaddr;
140
141 DBG("%d: init\n", dev->idx);
142
143
144 out_8(&iic->lmadr, 0);
145 out_8(&iic->hmadr, 0);
146
147
148 out_8(&iic->lsadr, 0);
149 out_8(&iic->hsadr, 0);
150
151
152 out_8(&iic->sts, STS_SCMP | STS_IRQA);
153 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
154 | EXTSTS_ICT | EXTSTS_XFRA);
155
156
157 out_8(&iic->clkdiv, dev->clckdiv);
158
159
160 out_8(&iic->xfrcnt, 0);
161
162
163 out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
164 | XTCNTLSS_SWS);
165
166
167 out_8(&iic->cntl, 0);
168
169
170 iic_interrupt_mode(dev, dev->irq >= 0);
171
172
173 out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
174 | (dev->fast_mode ? MDCNTL_FSM : 0));
175
176 DUMP_REGS("iic_init", dev);
177}
178
179
180
181
182static void iic_dev_reset(struct ibm_iic_private* dev)
183{
184 volatile struct iic_regs __iomem *iic = dev->vaddr;
185 int i;
186 u8 dc;
187
188 DBG("%d: soft reset\n", dev->idx);
189 DUMP_REGS("reset", dev);
190
191
192 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
193
194
195 dc = in_8(&iic->directcntl);
196 if (!DIRCTNL_FREE(dc)){
197 DBG("%d: trying to regain bus control\n", dev->idx);
198
199
200 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
201
202
203 for (i = 0; i < 100; ++i){
204 dc = in_8(&iic->directcntl);
205 if (DIRCTNL_FREE(dc))
206 break;
207
208
209 dc ^= DIRCNTL_SCC;
210 out_8(&iic->directcntl, dc);
211 udelay(10);
212 dc ^= DIRCNTL_SCC;
213 out_8(&iic->directcntl, dc);
214
215
216 cond_resched();
217 }
218 }
219
220
221 out_8(&iic->xtcntlss, 0);
222
223
224 iic_dev_init(dev);
225}
226
227
228
229
230
231
232static int iic_dc_wait(volatile struct iic_regs __iomem *iic, u8 mask)
233{
234 unsigned long x = jiffies + HZ / 28 + 2;
235 while ((in_8(&iic->directcntl) & mask) != mask){
236 if (unlikely(time_after(jiffies, x)))
237 return -1;
238 cond_resched();
239 }
240 return 0;
241}
242
243static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p)
244{
245 volatile struct iic_regs __iomem *iic = dev->vaddr;
246 const struct ibm_iic_timings *t = &timings[dev->fast_mode ? 1 : 0];
247 u8 mask, v, sda;
248 int i, res;
249
250
251 if (unlikely(p->flags & I2C_M_TEN)){
252 DBG("%d: smbus_quick - 10 bit addresses are not supported\n",
253 dev->idx);
254 return -EINVAL;
255 }
256
257 DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);
258
259
260 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
261
262
263 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
264 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))
265 goto err;
266 ndelay(t->buf);
267
268
269 out_8(&iic->directcntl, DIRCNTL_SCC);
270 sda = 0;
271 ndelay(t->hd_sta);
272
273
274 v = i2c_8bit_addr_from_msg(p);
275 for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
276 out_8(&iic->directcntl, sda);
277 ndelay(t->low / 2);
278 sda = (v & mask) ? DIRCNTL_SDAC : 0;
279 out_8(&iic->directcntl, sda);
280 ndelay(t->low / 2);
281
282 out_8(&iic->directcntl, DIRCNTL_SCC | sda);
283 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
284 goto err;
285 ndelay(t->high);
286 }
287
288
289 out_8(&iic->directcntl, sda);
290 ndelay(t->low / 2);
291 out_8(&iic->directcntl, DIRCNTL_SDAC);
292 ndelay(t->low / 2);
293 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
294 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
295 goto err;
296 res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;
297 ndelay(t->high);
298
299
300 out_8(&iic->directcntl, 0);
301 ndelay(t->low);
302 out_8(&iic->directcntl, DIRCNTL_SCC);
303 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
304 goto err;
305 ndelay(t->su_sto);
306 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
307
308 ndelay(t->buf);
309
310 DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");
311out:
312
313 out_8(&iic->xtcntlss, 0);
314
315
316 iic_dev_init(dev);
317
318 return res;
319err:
320 DBG("%d: smbus_quick - bus is stuck\n", dev->idx);
321 res = -EREMOTEIO;
322 goto out;
323}
324
325
326
327
328static irqreturn_t iic_handler(int irq, void *dev_id)
329{
330 struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
331 volatile struct iic_regs __iomem *iic = dev->vaddr;
332
333 DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
334 dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
335
336
337 out_8(&iic->sts, STS_IRQA | STS_SCMP);
338 wake_up_interruptible(&dev->wq);
339
340 return IRQ_HANDLED;
341}
342
343
344
345
346
347static int iic_xfer_result(struct ibm_iic_private* dev)
348{
349 volatile struct iic_regs __iomem *iic = dev->vaddr;
350
351 if (unlikely(in_8(&iic->sts) & STS_ERR)){
352 DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
353 in_8(&iic->extsts));
354
355
356 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
357 EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
358
359
360 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
361
362
363
364
365
366
367 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
368 DBG("%d: bus is stuck, resetting\n", dev->idx);
369 iic_dev_reset(dev);
370 }
371 return -EREMOTEIO;
372 }
373 else
374 return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
375}
376
377
378
379
380static void iic_abort_xfer(struct ibm_iic_private* dev)
381{
382 volatile struct iic_regs __iomem *iic = dev->vaddr;
383 unsigned long x;
384
385 DBG("%d: iic_abort_xfer\n", dev->idx);
386
387 out_8(&iic->cntl, CNTL_HMT);
388
389
390
391
392
393 x = jiffies + 2;
394 while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
395 if (time_after(jiffies, x)){
396 DBG("%d: abort timeout, resetting...\n", dev->idx);
397 iic_dev_reset(dev);
398 return;
399 }
400 schedule();
401 }
402
403
404 iic_xfer_result(dev);
405}
406
407
408
409
410
411
412static int iic_wait_for_tc(struct ibm_iic_private* dev){
413
414 volatile struct iic_regs __iomem *iic = dev->vaddr;
415 int ret = 0;
416
417 if (dev->irq >= 0){
418
419 ret = wait_event_interruptible_timeout(dev->wq,
420 !(in_8(&iic->sts) & STS_PT), dev->adap.timeout);
421
422 if (unlikely(ret < 0))
423 DBG("%d: wait interrupted\n", dev->idx);
424 else if (unlikely(in_8(&iic->sts) & STS_PT)){
425 DBG("%d: wait timeout\n", dev->idx);
426 ret = -ETIMEDOUT;
427 }
428 }
429 else {
430
431 unsigned long x = jiffies + dev->adap.timeout;
432
433 while (in_8(&iic->sts) & STS_PT){
434 if (unlikely(time_after(jiffies, x))){
435 DBG("%d: poll timeout\n", dev->idx);
436 ret = -ETIMEDOUT;
437 break;
438 }
439
440 if (unlikely(signal_pending(current))){
441 DBG("%d: poll interrupted\n", dev->idx);
442 ret = -ERESTARTSYS;
443 break;
444 }
445 schedule();
446 }
447 }
448
449 if (unlikely(ret < 0))
450 iic_abort_xfer(dev);
451 else
452 ret = iic_xfer_result(dev);
453
454 DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
455
456 return ret;
457}
458
459
460
461
462static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
463 int combined_xfer)
464{
465 volatile struct iic_regs __iomem *iic = dev->vaddr;
466 char* buf = pm->buf;
467 int i, j, loops, ret = 0;
468 int len = pm->len;
469
470 u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
471 if (pm->flags & I2C_M_RD)
472 cntl |= CNTL_RW;
473
474 loops = (len + 3) / 4;
475 for (i = 0; i < loops; ++i, len -= 4){
476 int count = len > 4 ? 4 : len;
477 u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
478
479 if (!(cntl & CNTL_RW))
480 for (j = 0; j < count; ++j)
481 out_8((void __iomem *)&iic->mdbuf, *buf++);
482
483 if (i < loops - 1)
484 cmd |= CNTL_CHT;
485 else if (combined_xfer)
486 cmd |= CNTL_RPST;
487
488 DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
489
490
491 out_8(&iic->cntl, cmd);
492
493
494 ret = iic_wait_for_tc(dev);
495
496 if (unlikely(ret < 0))
497 break;
498 else if (unlikely(ret != count)){
499 DBG("%d: xfer_bytes, requested %d, transferred %d\n",
500 dev->idx, count, ret);
501
502
503 if (combined_xfer || (i < loops - 1))
504 iic_abort_xfer(dev);
505
506 ret = -EREMOTEIO;
507 break;
508 }
509
510 if (cntl & CNTL_RW)
511 for (j = 0; j < count; ++j)
512 *buf++ = in_8((void __iomem *)&iic->mdbuf);
513 }
514
515 return ret > 0 ? 0 : ret;
516}
517
518
519
520
521static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
522{
523 volatile struct iic_regs __iomem *iic = dev->vaddr;
524 u16 addr = msg->addr;
525
526 DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
527 addr, msg->flags & I2C_M_TEN ? 10 : 7);
528
529 if (msg->flags & I2C_M_TEN){
530 out_8(&iic->cntl, CNTL_AMD);
531 out_8(&iic->lmadr, addr);
532 out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
533 }
534 else {
535 out_8(&iic->cntl, 0);
536 out_8(&iic->lmadr, addr << 1);
537 }
538}
539
540static inline int iic_invalid_address(const struct i2c_msg* p)
541{
542 return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
543}
544
545static inline int iic_address_neq(const struct i2c_msg* p1,
546 const struct i2c_msg* p2)
547{
548 return (p1->addr != p2->addr)
549 || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
550}
551
552
553
554
555
556static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
557{
558 struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
559 volatile struct iic_regs __iomem *iic = dev->vaddr;
560 int i, ret = 0;
561
562 DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
563
564
565
566
567 if (unlikely(iic_invalid_address(&msgs[0]))){
568 DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
569 msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
570 return -EINVAL;
571 }
572 for (i = 0; i < num; ++i){
573 if (unlikely(msgs[i].len <= 0)){
574 if (num == 1 && !msgs[0].len){
575
576
577
578
579 return iic_smbus_quick(dev, &msgs[0]);
580 }
581 DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
582 msgs[i].len, i);
583 return -EINVAL;
584 }
585 if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
586 DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
587 return -EINVAL;
588 }
589 }
590
591
592 if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
593 DBG("%d: iic_xfer, bus is not free\n", dev->idx);
594
595
596
597
598
599
600
601
602
603 iic_dev_reset(dev);
604
605 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
606 DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
607 return -EREMOTEIO;
608 }
609 }
610 else {
611
612 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
613 }
614
615
616 iic_address(dev, &msgs[0]);
617
618
619 for (i = 0; i < num && !ret; ++i)
620 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
621
622 return ret < 0 ? ret : num;
623}
624
625static u32 iic_func(struct i2c_adapter *adap)
626{
627 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
628}
629
630static const struct i2c_algorithm iic_algo = {
631 .master_xfer = iic_xfer,
632 .functionality = iic_func
633};
634
635
636
637
638static inline u8 iic_clckdiv(unsigned int opb)
639{
640
641
642
643
644
645 if (!opb){
646 printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
647 " fix your board specific setup\n");
648 opb = 50000000;
649 }
650
651
652 opb /= 1000000;
653
654 if (opb < 20 || opb > 150){
655 printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n",
656 opb);
657 opb = opb < 20 ? 20 : 150;
658 }
659 return (u8)((opb + 9) / 10 - 1);
660}
661
662static int iic_request_irq(struct platform_device *ofdev,
663 struct ibm_iic_private *dev)
664{
665 struct device_node *np = ofdev->dev.of_node;
666 int irq;
667
668 if (iic_force_poll)
669 return 0;
670
671 irq = irq_of_parse_and_map(np, 0);
672 if (!irq) {
673 dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n");
674 return 0;
675 }
676
677
678
679
680 iic_interrupt_mode(dev, 0);
681 if (request_irq(irq, iic_handler, 0, "IBM IIC", dev)) {
682 dev_err(&ofdev->dev, "request_irq %d failed\n", irq);
683
684 return 0;
685 }
686
687 return irq;
688}
689
690
691
692
693static int iic_probe(struct platform_device *ofdev)
694{
695 struct device_node *np = ofdev->dev.of_node;
696 struct ibm_iic_private *dev;
697 struct i2c_adapter *adap;
698 const u32 *freq;
699 int ret;
700
701 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
702 if (!dev) {
703 dev_err(&ofdev->dev, "failed to allocate device data\n");
704 return -ENOMEM;
705 }
706
707 platform_set_drvdata(ofdev, dev);
708
709 dev->vaddr = of_iomap(np, 0);
710 if (dev->vaddr == NULL) {
711 dev_err(&ofdev->dev, "failed to iomap device\n");
712 ret = -ENXIO;
713 goto error_cleanup;
714 }
715
716 init_waitqueue_head(&dev->wq);
717
718 dev->irq = iic_request_irq(ofdev, dev);
719 if (!dev->irq)
720 dev_warn(&ofdev->dev, "using polling mode\n");
721
722
723 if (iic_force_fast || of_get_property(np, "fast-mode", NULL))
724 dev->fast_mode = 1;
725
726 freq = of_get_property(np, "clock-frequency", NULL);
727 if (freq == NULL) {
728 freq = of_get_property(np->parent, "clock-frequency", NULL);
729 if (freq == NULL) {
730 dev_err(&ofdev->dev, "Unable to get bus frequency\n");
731 ret = -EINVAL;
732 goto error_cleanup;
733 }
734 }
735
736 dev->clckdiv = iic_clckdiv(*freq);
737 dev_dbg(&ofdev->dev, "clckdiv = %d\n", dev->clckdiv);
738
739
740 iic_dev_init(dev);
741
742
743 adap = &dev->adap;
744 adap->dev.parent = &ofdev->dev;
745 adap->dev.of_node = of_node_get(np);
746 strlcpy(adap->name, "IBM IIC", sizeof(adap->name));
747 i2c_set_adapdata(adap, dev);
748 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
749 adap->algo = &iic_algo;
750 adap->timeout = HZ;
751
752 ret = i2c_add_adapter(adap);
753 if (ret < 0)
754 goto error_cleanup;
755
756 dev_info(&ofdev->dev, "using %s mode\n",
757 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
758
759 return 0;
760
761error_cleanup:
762 if (dev->irq) {
763 iic_interrupt_mode(dev, 0);
764 free_irq(dev->irq, dev);
765 }
766
767 if (dev->vaddr)
768 iounmap(dev->vaddr);
769
770 kfree(dev);
771 return ret;
772}
773
774
775
776
777static int iic_remove(struct platform_device *ofdev)
778{
779 struct ibm_iic_private *dev = platform_get_drvdata(ofdev);
780
781 i2c_del_adapter(&dev->adap);
782
783 if (dev->irq) {
784 iic_interrupt_mode(dev, 0);
785 free_irq(dev->irq, dev);
786 }
787
788 iounmap(dev->vaddr);
789 kfree(dev);
790
791 return 0;
792}
793
794static const struct of_device_id ibm_iic_match[] = {
795 { .compatible = "ibm,iic", },
796 {}
797};
798MODULE_DEVICE_TABLE(of, ibm_iic_match);
799
800static struct platform_driver ibm_iic_driver = {
801 .driver = {
802 .name = "ibm-iic",
803 .of_match_table = ibm_iic_match,
804 },
805 .probe = iic_probe,
806 .remove = iic_remove,
807};
808
809module_platform_driver(ibm_iic_driver);
810