1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/i2c/busses/i2c-ibm_iic.c
5 * Support for the IIC peripheral on IBM PPC 4xx
7 * Copyright (c) 2003, 2004 Zultys Technologies.
10 * Copyright (c) 2008 PIKA Technologies
13 * Based on original work by
18 * Copyright 2000-2003 MontaVista Software Inc.
20 * Original driver version was highly leveraged from i2c-elektor.c
22 * Copyright 1995-97 Simon G. Vogl
23 * 1998-99 Hans Berglund
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/ioport.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched/signal.h>
39 #include <linux/i2c.h>
40 #include <linux/of_address.h>
41 #include <linux/of_irq.h>
42 #include <linux/of_platform.h>
44 #include "i2c-ibm_iic.h"
46 #define DRIVER_VERSION "2.2"
48 MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
49 MODULE_LICENSE("GPL");
51 static bool iic_force_poll;
52 module_param(iic_force_poll, bool, 0);
53 MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
55 static bool iic_force_fast;
56 module_param(iic_force_fast, bool, 0);
57 MODULE_PARM_DESC(iic_force_fast, "Force fast mode (400 kHz)");
70 # define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
72 # define DBG(f,x...) ((void)0)
75 # define DBG2(f,x...) DBG(f, ##x)
77 # define DBG2(f,x...) ((void)0)
80 static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
82 volatile struct iic_regs __iomem *iic = dev->vaddr;
83 printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
85 " cntl = 0x%02x, mdcntl = 0x%02x\n"
86 " sts = 0x%02x, extsts = 0x%02x\n"
87 " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
88 " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
89 in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
90 in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
91 in_8(&iic->xtcntlss), in_8(&iic->directcntl));
93 # define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
95 # define DUMP_REGS(h,dev) ((void)0)
98 /* Bus timings (in ns) for bit-banging */
99 static struct ibm_iic_timings {
106 /* Standard mode (100 KHz) */
114 /* Fast mode (400 KHz) */
123 /* Enable/disable interrupt generation */
124 static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
126 out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
130 * Initialize IIC interface.
132 static void iic_dev_init(struct ibm_iic_private* dev)
134 volatile struct iic_regs __iomem *iic = dev->vaddr;
136 DBG("%d: init\n", dev->idx);
138 /* Clear master address */
139 out_8(&iic->lmadr, 0);
140 out_8(&iic->hmadr, 0);
142 /* Clear slave address */
143 out_8(&iic->lsadr, 0);
144 out_8(&iic->hsadr, 0);
146 /* Clear status & extended status */
147 out_8(&iic->sts, STS_SCMP | STS_IRQA);
148 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
149 | EXTSTS_ICT | EXTSTS_XFRA);
151 /* Set clock divider */
152 out_8(&iic->clkdiv, dev->clckdiv);
154 /* Clear transfer count */
155 out_8(&iic->xfrcnt, 0);
157 /* Clear extended control and status */
158 out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
161 /* Clear control register */
162 out_8(&iic->cntl, 0);
164 /* Enable interrupts if possible */
165 iic_interrupt_mode(dev, dev->irq >= 0);
167 /* Set mode control */
168 out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
169 | (dev->fast_mode ? MDCNTL_FSM : 0));
171 DUMP_REGS("iic_init", dev);
175 * Reset IIC interface
177 static void iic_dev_reset(struct ibm_iic_private* dev)
179 volatile struct iic_regs __iomem *iic = dev->vaddr;
183 DBG("%d: soft reset\n", dev->idx);
184 DUMP_REGS("reset", dev);
186 /* Place chip in the reset state */
187 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
189 /* Check if bus is free */
190 dc = in_8(&iic->directcntl);
191 if (!DIRCTNL_FREE(dc)){
192 DBG("%d: trying to regain bus control\n", dev->idx);
194 /* Try to set bus free state */
195 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
197 /* Wait until we regain bus control */
198 for (i = 0; i < 100; ++i){
199 dc = in_8(&iic->directcntl);
200 if (DIRCTNL_FREE(dc))
203 /* Toggle SCL line */
205 out_8(&iic->directcntl, dc);
208 out_8(&iic->directcntl, dc);
216 out_8(&iic->xtcntlss, 0);
218 /* Reinitialize interface */
223 * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
226 /* Wait for SCL and/or SDA to be high */
227 static int iic_dc_wait(volatile struct iic_regs __iomem *iic, u8 mask)
229 unsigned long x = jiffies + HZ / 28 + 2;
230 while ((in_8(&iic->directcntl) & mask) != mask){
231 if (unlikely(time_after(jiffies, x)))
238 static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p)
240 volatile struct iic_regs __iomem *iic = dev->vaddr;
241 const struct ibm_iic_timings *t = &timings[dev->fast_mode ? 1 : 0];
245 /* Only 7-bit addresses are supported */
246 if (unlikely(p->flags & I2C_M_TEN)){
247 DBG("%d: smbus_quick - 10 bit addresses are not supported\n",
252 DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);
254 /* Reset IIC interface */
255 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
257 /* Wait for bus to become free */
258 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
259 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))
264 out_8(&iic->directcntl, DIRCNTL_SCC);
269 v = i2c_8bit_addr_from_msg(p);
270 for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){
271 out_8(&iic->directcntl, sda);
273 sda = (v & mask) ? DIRCNTL_SDAC : 0;
274 out_8(&iic->directcntl, sda);
277 out_8(&iic->directcntl, DIRCNTL_SCC | sda);
278 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
284 out_8(&iic->directcntl, sda);
286 out_8(&iic->directcntl, DIRCNTL_SDAC);
288 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
289 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
291 res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;
295 out_8(&iic->directcntl, 0);
297 out_8(&iic->directcntl, DIRCNTL_SCC);
298 if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))
301 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
305 DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");
308 out_8(&iic->xtcntlss, 0);
310 /* Reinitialize interface */
315 DBG("%d: smbus_quick - bus is stuck\n", dev->idx);
321 * IIC interrupt handler
323 static irqreturn_t iic_handler(int irq, void *dev_id)
325 struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
326 volatile struct iic_regs __iomem *iic = dev->vaddr;
328 DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
329 dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
331 /* Acknowledge IRQ and wakeup iic_wait_for_tc */
332 out_8(&iic->sts, STS_IRQA | STS_SCMP);
333 wake_up_interruptible(&dev->wq);
339 * Get master transfer result and clear errors if any.
340 * Returns the number of actually transferred bytes or error (<0)
342 static int iic_xfer_result(struct ibm_iic_private* dev)
344 volatile struct iic_regs __iomem *iic = dev->vaddr;
346 if (unlikely(in_8(&iic->sts) & STS_ERR)){
347 DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
350 /* Clear errors and possible pending IRQs */
351 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
352 EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
354 /* Flush master data buffer */
355 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
358 * If error happened during combined xfer
359 * IIC interface is usually stuck in some strange
360 * state, the only way out - soft reset.
362 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
363 DBG("%d: bus is stuck, resetting\n", dev->idx);
369 return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
373 * Try to abort active transfer.
375 static void iic_abort_xfer(struct ibm_iic_private* dev)
377 volatile struct iic_regs __iomem *iic = dev->vaddr;
380 DBG("%d: iic_abort_xfer\n", dev->idx);
382 out_8(&iic->cntl, CNTL_HMT);
385 * Wait for the abort command to complete.
386 * It's not worth to be optimized, just poll (timeout >= 1 tick)
389 while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
390 if (time_after(jiffies, x)){
391 DBG("%d: abort timeout, resetting...\n", dev->idx);
398 /* Just to clear errors */
399 iic_xfer_result(dev);
403 * Wait for master transfer to complete.
404 * It puts current process to sleep until we get interrupt or timeout expires.
405 * Returns the number of transferred bytes or error (<0)
407 static int iic_wait_for_tc(struct ibm_iic_private* dev){
409 volatile struct iic_regs __iomem *iic = dev->vaddr;
414 ret = wait_event_interruptible_timeout(dev->wq,
415 !(in_8(&iic->sts) & STS_PT), dev->adap.timeout);
417 if (unlikely(ret < 0))
418 DBG("%d: wait interrupted\n", dev->idx);
419 else if (unlikely(in_8(&iic->sts) & STS_PT)){
420 DBG("%d: wait timeout\n", dev->idx);
426 unsigned long x = jiffies + dev->adap.timeout;
428 while (in_8(&iic->sts) & STS_PT){
429 if (unlikely(time_after(jiffies, x))){
430 DBG("%d: poll timeout\n", dev->idx);
435 if (signal_pending(current)){
436 DBG("%d: poll interrupted\n", dev->idx);
444 if (unlikely(ret < 0))
447 ret = iic_xfer_result(dev);
449 DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
455 * Low level master transfer routine
457 static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
460 volatile struct iic_regs __iomem *iic = dev->vaddr;
462 int i, j, loops, ret = 0;
465 u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
466 if (pm->flags & I2C_M_RD)
469 loops = (len + 3) / 4;
470 for (i = 0; i < loops; ++i, len -= 4){
471 int count = len > 4 ? 4 : len;
472 u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
474 if (!(cntl & CNTL_RW))
475 for (j = 0; j < count; ++j)
476 out_8((void __iomem *)&iic->mdbuf, *buf++);
480 else if (combined_xfer)
483 DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
486 out_8(&iic->cntl, cmd);
488 /* Wait for completion */
489 ret = iic_wait_for_tc(dev);
491 if (unlikely(ret < 0))
493 else if (unlikely(ret != count)){
494 DBG("%d: xfer_bytes, requested %d, transferred %d\n",
495 dev->idx, count, ret);
497 /* If it's not a last part of xfer, abort it */
498 if (combined_xfer || (i < loops - 1))
506 for (j = 0; j < count; ++j)
507 *buf++ = in_8((void __iomem *)&iic->mdbuf);
510 return ret > 0 ? 0 : ret;
514 * Set target slave address for master transfer
516 static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
518 volatile struct iic_regs __iomem *iic = dev->vaddr;
519 u16 addr = msg->addr;
521 DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
522 addr, msg->flags & I2C_M_TEN ? 10 : 7);
524 if (msg->flags & I2C_M_TEN){
525 out_8(&iic->cntl, CNTL_AMD);
526 out_8(&iic->lmadr, addr);
527 out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
530 out_8(&iic->cntl, 0);
531 out_8(&iic->lmadr, addr << 1);
535 static inline int iic_invalid_address(const struct i2c_msg* p)
537 return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
540 static inline int iic_address_neq(const struct i2c_msg* p1,
541 const struct i2c_msg* p2)
543 return (p1->addr != p2->addr)
544 || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
548 * Generic master transfer entrypoint.
549 * Returns the number of processed messages or error (<0)
551 static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
553 struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
554 volatile struct iic_regs __iomem *iic = dev->vaddr;
557 DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
559 /* Check the sanity of the passed messages.
560 * Uhh, generic i2c layer is more suitable place for such code...
562 if (unlikely(iic_invalid_address(&msgs[0]))){
563 DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
564 msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
567 for (i = 0; i < num; ++i){
568 if (unlikely(msgs[i].len <= 0)){
569 if (num == 1 && !msgs[0].len){
570 /* Special case for I2C_SMBUS_QUICK emulation.
571 * IBM IIC doesn't support 0-length transactions
572 * so we have to emulate them using bit-banging.
574 return iic_smbus_quick(dev, &msgs[0]);
576 DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
580 if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
581 DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
586 /* Check bus state */
587 if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
588 DBG("%d: iic_xfer, bus is not free\n", dev->idx);
590 /* Usually it means something serious has happened.
591 * We *cannot* have unfinished previous transfer
592 * so it doesn't make any sense to try to stop it.
593 * Probably we were not able to recover from the
595 * The only *reasonable* thing I can think of here
596 * is soft reset. --ebs
600 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
601 DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
606 /* Flush master data buffer (just in case) */
607 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
610 /* Load slave address */
611 iic_address(dev, &msgs[0]);
613 /* Do real transfer */
614 for (i = 0; i < num && !ret; ++i)
615 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
617 return ret < 0 ? ret : num;
620 static u32 iic_func(struct i2c_adapter *adap)
622 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
625 static const struct i2c_algorithm iic_algo = {
626 .master_xfer = iic_xfer,
627 .functionality = iic_func
631 * Calculates IICx_CLCKDIV value for a specific OPB clock frequency
633 static inline u8 iic_clckdiv(unsigned int opb)
635 /* Compatibility kludge, should go away after all cards
636 * are fixed to fill correct value for opbfreq.
637 * Previous driver version used hardcoded divider value 4,
638 * it corresponds to OPB frequency from the range (40, 50] MHz
641 printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
642 " fix your board specific setup\n");
649 if (opb < 20 || opb > 150){
650 printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n",
652 opb = opb < 20 ? 20 : 150;
654 return (u8)((opb + 9) / 10 - 1);
657 static int iic_request_irq(struct platform_device *ofdev,
658 struct ibm_iic_private *dev)
660 struct device_node *np = ofdev->dev.of_node;
666 irq = irq_of_parse_and_map(np, 0);
668 dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n");
672 /* Disable interrupts until we finish initialization, assumes
673 * level-sensitive IRQ setup...
675 iic_interrupt_mode(dev, 0);
676 if (request_irq(irq, iic_handler, 0, "IBM IIC", dev)) {
677 dev_err(&ofdev->dev, "request_irq %d failed\n", irq);
678 /* Fallback to the polling mode */
686 * Register single IIC interface
688 static int iic_probe(struct platform_device *ofdev)
690 struct device_node *np = ofdev->dev.of_node;
691 struct ibm_iic_private *dev;
692 struct i2c_adapter *adap;
696 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
698 dev_err(&ofdev->dev, "failed to allocate device data\n");
702 platform_set_drvdata(ofdev, dev);
704 dev->vaddr = of_iomap(np, 0);
705 if (dev->vaddr == NULL) {
706 dev_err(&ofdev->dev, "failed to iomap device\n");
711 init_waitqueue_head(&dev->wq);
713 dev->irq = iic_request_irq(ofdev, dev);
715 dev_warn(&ofdev->dev, "using polling mode\n");
717 /* Board specific settings */
718 if (iic_force_fast || of_get_property(np, "fast-mode", NULL))
721 freq = of_get_property(np, "clock-frequency", NULL);
723 freq = of_get_property(np->parent, "clock-frequency", NULL);
725 dev_err(&ofdev->dev, "Unable to get bus frequency\n");
731 dev->clckdiv = iic_clckdiv(*freq);
732 dev_dbg(&ofdev->dev, "clckdiv = %d\n", dev->clckdiv);
734 /* Initialize IIC interface */
737 /* Register it with i2c layer */
739 adap->dev.parent = &ofdev->dev;
740 adap->dev.of_node = of_node_get(np);
741 strscpy(adap->name, "IBM IIC", sizeof(adap->name));
742 i2c_set_adapdata(adap, dev);
743 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
744 adap->algo = &iic_algo;
747 ret = i2c_add_adapter(adap);
751 dev_info(&ofdev->dev, "using %s mode\n",
752 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
758 iic_interrupt_mode(dev, 0);
759 free_irq(dev->irq, dev);
770 * Cleanup initialized IIC interface
772 static int iic_remove(struct platform_device *ofdev)
774 struct ibm_iic_private *dev = platform_get_drvdata(ofdev);
776 i2c_del_adapter(&dev->adap);
779 iic_interrupt_mode(dev, 0);
780 free_irq(dev->irq, dev);
789 static const struct of_device_id ibm_iic_match[] = {
790 { .compatible = "ibm,iic", },
793 MODULE_DEVICE_TABLE(of, ibm_iic_match);
795 static struct platform_driver ibm_iic_driver = {
798 .of_match_table = ibm_iic_match,
801 .remove = iic_remove,
804 module_platform_driver(ibm_iic_driver);