Lines Matching +full:sense +full:- +full:freq
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/i2c/busses/i2c-ibm_iic.c
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
44 #include "i2c-ibm_iic.h"
70 # define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
82 volatile struct iic_regs __iomem *iic = dev->vaddr; in dump_iic_regs()
83 printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header); in dump_iic_regs()
89 in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts), in dump_iic_regs()
90 in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt), in dump_iic_regs()
91 in_8(&iic->xtcntlss), in_8(&iic->directcntl)); in dump_iic_regs()
98 /* Bus timings (in ns) for bit-banging */
126 out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0); in iic_interrupt_mode()
134 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_dev_init()
136 DBG("%d: init\n", dev->idx); in iic_dev_init()
139 out_8(&iic->lmadr, 0); in iic_dev_init()
140 out_8(&iic->hmadr, 0); in iic_dev_init()
143 out_8(&iic->lsadr, 0); in iic_dev_init()
144 out_8(&iic->hsadr, 0); in iic_dev_init()
147 out_8(&iic->sts, STS_SCMP | STS_IRQA); in iic_dev_init()
148 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA in iic_dev_init()
152 out_8(&iic->clkdiv, dev->clckdiv); in iic_dev_init()
155 out_8(&iic->xfrcnt, 0); in iic_dev_init()
158 out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC in iic_dev_init()
162 out_8(&iic->cntl, 0); in iic_dev_init()
165 iic_interrupt_mode(dev, dev->irq >= 0); in iic_dev_init()
168 out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS in iic_dev_init()
169 | (dev->fast_mode ? MDCNTL_FSM : 0)); in iic_dev_init()
179 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_dev_reset()
183 DBG("%d: soft reset\n", dev->idx); in iic_dev_reset()
187 out_8(&iic->xtcntlss, XTCNTLSS_SRST); in iic_dev_reset()
190 dc = in_8(&iic->directcntl); in iic_dev_reset()
192 DBG("%d: trying to regain bus control\n", dev->idx); in iic_dev_reset()
195 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC); in iic_dev_reset()
199 dc = in_8(&iic->directcntl); in iic_dev_reset()
205 out_8(&iic->directcntl, dc); in iic_dev_reset()
208 out_8(&iic->directcntl, dc); in iic_dev_reset()
216 out_8(&iic->xtcntlss, 0); in iic_dev_reset()
223 * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
230 while ((in_8(&iic->directcntl) & mask) != mask){ in iic_dc_wait()
232 return -1; in iic_dc_wait()
240 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_smbus_quick()
241 const struct ibm_iic_timings *t = &timings[dev->fast_mode ? 1 : 0]; in iic_smbus_quick()
245 /* Only 7-bit addresses are supported */ in iic_smbus_quick()
246 if (unlikely(p->flags & I2C_M_TEN)){ in iic_smbus_quick()
247 DBG("%d: smbus_quick - 10 bit addresses are not supported\n", in iic_smbus_quick()
248 dev->idx); in iic_smbus_quick()
249 return -EINVAL; in iic_smbus_quick()
252 DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr); in iic_smbus_quick()
255 out_8(&iic->xtcntlss, XTCNTLSS_SRST); in iic_smbus_quick()
258 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC); in iic_smbus_quick()
261 ndelay(t->buf); in iic_smbus_quick()
264 out_8(&iic->directcntl, DIRCNTL_SCC); in iic_smbus_quick()
266 ndelay(t->hd_sta); in iic_smbus_quick()
271 out_8(&iic->directcntl, sda); in iic_smbus_quick()
272 ndelay(t->low / 2); in iic_smbus_quick()
274 out_8(&iic->directcntl, sda); in iic_smbus_quick()
275 ndelay(t->low / 2); in iic_smbus_quick()
277 out_8(&iic->directcntl, DIRCNTL_SCC | sda); in iic_smbus_quick()
280 ndelay(t->high); in iic_smbus_quick()
284 out_8(&iic->directcntl, sda); in iic_smbus_quick()
285 ndelay(t->low / 2); in iic_smbus_quick()
286 out_8(&iic->directcntl, DIRCNTL_SDAC); in iic_smbus_quick()
287 ndelay(t->low / 2); in iic_smbus_quick()
288 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC); in iic_smbus_quick()
291 res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1; in iic_smbus_quick()
292 ndelay(t->high); in iic_smbus_quick()
295 out_8(&iic->directcntl, 0); in iic_smbus_quick()
296 ndelay(t->low); in iic_smbus_quick()
297 out_8(&iic->directcntl, DIRCNTL_SCC); in iic_smbus_quick()
300 ndelay(t->su_sto); in iic_smbus_quick()
301 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC); in iic_smbus_quick()
303 ndelay(t->buf); in iic_smbus_quick()
305 DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK"); in iic_smbus_quick()
308 out_8(&iic->xtcntlss, 0); in iic_smbus_quick()
315 DBG("%d: smbus_quick - bus is stuck\n", dev->idx); in iic_smbus_quick()
316 res = -EREMOTEIO; in iic_smbus_quick()
326 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_handler()
329 dev->idx, in_8(&iic->sts), in_8(&iic->extsts)); in iic_handler()
332 out_8(&iic->sts, STS_IRQA | STS_SCMP); in iic_handler()
333 wake_up_interruptible(&dev->wq); in iic_handler()
344 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_xfer_result()
346 if (unlikely(in_8(&iic->sts) & STS_ERR)){ in iic_xfer_result()
347 DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx, in iic_xfer_result()
348 in_8(&iic->extsts)); in iic_xfer_result()
351 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | in iic_xfer_result()
355 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB); in iic_xfer_result()
360 * state, the only way out - soft reset. in iic_xfer_result()
362 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){ in iic_xfer_result()
363 DBG("%d: bus is stuck, resetting\n", dev->idx); in iic_xfer_result()
366 return -EREMOTEIO; in iic_xfer_result()
369 return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK; in iic_xfer_result()
377 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_abort_xfer()
380 DBG("%d: iic_abort_xfer\n", dev->idx); in iic_abort_xfer()
382 out_8(&iic->cntl, CNTL_HMT); in iic_abort_xfer()
389 while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){ in iic_abort_xfer()
391 DBG("%d: abort timeout, resetting...\n", dev->idx); in iic_abort_xfer()
409 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_wait_for_tc()
412 if (dev->irq >= 0){ in iic_wait_for_tc()
414 ret = wait_event_interruptible_timeout(dev->wq, in iic_wait_for_tc()
415 !(in_8(&iic->sts) & STS_PT), dev->adap.timeout); in iic_wait_for_tc()
418 DBG("%d: wait interrupted\n", dev->idx); in iic_wait_for_tc()
419 else if (unlikely(in_8(&iic->sts) & STS_PT)){ in iic_wait_for_tc()
420 DBG("%d: wait timeout\n", dev->idx); in iic_wait_for_tc()
421 ret = -ETIMEDOUT; in iic_wait_for_tc()
426 unsigned long x = jiffies + dev->adap.timeout; in iic_wait_for_tc()
428 while (in_8(&iic->sts) & STS_PT){ in iic_wait_for_tc()
430 DBG("%d: poll timeout\n", dev->idx); in iic_wait_for_tc()
431 ret = -ETIMEDOUT; in iic_wait_for_tc()
436 DBG("%d: poll interrupted\n", dev->idx); in iic_wait_for_tc()
437 ret = -ERESTARTSYS; in iic_wait_for_tc()
449 DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret); in iic_wait_for_tc()
460 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_xfer_bytes()
461 char* buf = pm->buf; in iic_xfer_bytes()
463 int len = pm->len; in iic_xfer_bytes()
465 u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT; in iic_xfer_bytes()
466 if (pm->flags & I2C_M_RD) in iic_xfer_bytes()
470 for (i = 0; i < loops; ++i, len -= 4){ in iic_xfer_bytes()
472 u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT); in iic_xfer_bytes()
476 out_8((void __iomem *)&iic->mdbuf, *buf++); in iic_xfer_bytes()
478 if (i < loops - 1) in iic_xfer_bytes()
483 DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd); in iic_xfer_bytes()
486 out_8(&iic->cntl, cmd); in iic_xfer_bytes()
495 dev->idx, count, ret); in iic_xfer_bytes()
498 if (combined_xfer || (i < loops - 1)) in iic_xfer_bytes()
501 ret = -EREMOTEIO; in iic_xfer_bytes()
507 *buf++ = in_8((void __iomem *)&iic->mdbuf); in iic_xfer_bytes()
518 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_address()
519 u16 addr = msg->addr; in iic_address()
521 DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx, in iic_address()
522 addr, msg->flags & I2C_M_TEN ? 10 : 7); in iic_address()
524 if (msg->flags & I2C_M_TEN){ in iic_address()
525 out_8(&iic->cntl, CNTL_AMD); in iic_address()
526 out_8(&iic->lmadr, addr); in iic_address()
527 out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06)); in iic_address()
530 out_8(&iic->cntl, 0); in iic_address()
531 out_8(&iic->lmadr, addr << 1); in iic_address()
537 return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f)); in iic_invalid_address()
543 return (p1->addr != p2->addr) in iic_address_neq()
544 || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN)); in iic_address_neq()
554 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_xfer()
557 DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num); in iic_xfer()
563 DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx, in iic_xfer()
565 return -EINVAL; in iic_xfer()
571 * IBM IIC doesn't support 0-length transactions in iic_xfer()
572 * so we have to emulate them using bit-banging. in iic_xfer()
576 DBG("%d: invalid len %d in msg[%d]\n", dev->idx, in iic_xfer()
578 return -EINVAL; in iic_xfer()
581 DBG("%d: invalid addr in msg[%d]\n", dev->idx, i); in iic_xfer()
582 return -EINVAL; in iic_xfer()
587 if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){ in iic_xfer()
588 DBG("%d: iic_xfer, bus is not free\n", dev->idx); in iic_xfer()
592 * so it doesn't make any sense to try to stop it. in iic_xfer()
596 * is soft reset. --ebs in iic_xfer()
600 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){ in iic_xfer()
601 DBG("%d: iic_xfer, bus is still not free\n", dev->idx); in iic_xfer()
602 return -EREMOTEIO; in iic_xfer()
607 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB); in iic_xfer()
615 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1); in iic_xfer()
641 printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq," in iic_clckdiv()
650 printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n", in iic_clckdiv()
654 return (u8)((opb + 9) / 10 - 1); in iic_clckdiv()
660 struct device_node *np = ofdev->dev.of_node; in iic_request_irq()
668 dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n"); in iic_request_irq()
673 * level-sensitive IRQ setup... in iic_request_irq()
677 dev_err(&ofdev->dev, "request_irq %d failed\n", irq); in iic_request_irq()
690 struct device_node *np = ofdev->dev.of_node; in iic_probe()
693 const u32 *freq; in iic_probe() local
698 dev_err(&ofdev->dev, "failed to allocate device data\n"); in iic_probe()
699 return -ENOMEM; in iic_probe()
704 dev->vaddr = of_iomap(np, 0); in iic_probe()
705 if (dev->vaddr == NULL) { in iic_probe()
706 dev_err(&ofdev->dev, "failed to iomap device\n"); in iic_probe()
707 ret = -ENXIO; in iic_probe()
711 init_waitqueue_head(&dev->wq); in iic_probe()
713 dev->irq = iic_request_irq(ofdev, dev); in iic_probe()
714 if (!dev->irq) in iic_probe()
715 dev_warn(&ofdev->dev, "using polling mode\n"); in iic_probe()
718 if (iic_force_fast || of_get_property(np, "fast-mode", NULL)) in iic_probe()
719 dev->fast_mode = 1; in iic_probe()
721 freq = of_get_property(np, "clock-frequency", NULL); in iic_probe()
722 if (freq == NULL) { in iic_probe()
723 freq = of_get_property(np->parent, "clock-frequency", NULL); in iic_probe()
724 if (freq == NULL) { in iic_probe()
725 dev_err(&ofdev->dev, "Unable to get bus frequency\n"); in iic_probe()
726 ret = -EINVAL; in iic_probe()
731 dev->clckdiv = iic_clckdiv(*freq); in iic_probe()
732 dev_dbg(&ofdev->dev, "clckdiv = %d\n", dev->clckdiv); in iic_probe()
738 adap = &dev->adap; in iic_probe()
739 adap->dev.parent = &ofdev->dev; in iic_probe()
740 adap->dev.of_node = of_node_get(np); in iic_probe()
741 strlcpy(adap->name, "IBM IIC", sizeof(adap->name)); in iic_probe()
743 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; in iic_probe()
744 adap->algo = &iic_algo; in iic_probe()
745 adap->timeout = HZ; in iic_probe()
751 dev_info(&ofdev->dev, "using %s mode\n", in iic_probe()
752 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)"); in iic_probe()
757 if (dev->irq) { in iic_probe()
759 free_irq(dev->irq, dev); in iic_probe()
762 if (dev->vaddr) in iic_probe()
763 iounmap(dev->vaddr); in iic_probe()
776 i2c_del_adapter(&dev->adap); in iic_remove()
778 if (dev->irq) { in iic_remove()
780 free_irq(dev->irq, dev); in iic_remove()
783 iounmap(dev->vaddr); in iic_remove()
797 .name = "ibm-iic",