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
45 #include "i2c-ibm_iic.h"
71 # define DBG(f,x...) printk(KERN_DEBUG "ibm-iic" f, ##x)
83 volatile struct iic_regs __iomem *iic = dev->vaddr; in dump_iic_regs()
84 printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header); in dump_iic_regs()
90 in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts), in dump_iic_regs()
91 in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt), in dump_iic_regs()
92 in_8(&iic->xtcntlss), in_8(&iic->directcntl)); in dump_iic_regs()
99 /* Bus timings (in ns) for bit-banging */
127 out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0); in iic_interrupt_mode()
135 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_dev_init()
137 DBG("%d: init\n", dev->idx); in iic_dev_init()
140 out_8(&iic->lmadr, 0); in iic_dev_init()
141 out_8(&iic->hmadr, 0); in iic_dev_init()
144 out_8(&iic->lsadr, 0); in iic_dev_init()
145 out_8(&iic->hsadr, 0); in iic_dev_init()
148 out_8(&iic->sts, STS_SCMP | STS_IRQA); in iic_dev_init()
149 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA in iic_dev_init()
153 out_8(&iic->clkdiv, dev->clckdiv); in iic_dev_init()
156 out_8(&iic->xfrcnt, 0); in iic_dev_init()
159 out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC in iic_dev_init()
163 out_8(&iic->cntl, 0); in iic_dev_init()
166 iic_interrupt_mode(dev, dev->irq >= 0); in iic_dev_init()
169 out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS in iic_dev_init()
170 | (dev->fast_mode ? MDCNTL_FSM : 0)); in iic_dev_init()
180 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_dev_reset()
184 DBG("%d: soft reset\n", dev->idx); in iic_dev_reset()
188 out_8(&iic->xtcntlss, XTCNTLSS_SRST); in iic_dev_reset()
191 dc = in_8(&iic->directcntl); in iic_dev_reset()
193 DBG("%d: trying to regain bus control\n", dev->idx); in iic_dev_reset()
196 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC); in iic_dev_reset()
200 dc = in_8(&iic->directcntl); in iic_dev_reset()
206 out_8(&iic->directcntl, dc); in iic_dev_reset()
209 out_8(&iic->directcntl, dc); in iic_dev_reset()
217 out_8(&iic->xtcntlss, 0); in iic_dev_reset()
224 * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register.
231 while ((in_8(&iic->directcntl) & mask) != mask){ in iic_dc_wait()
233 return -1; in iic_dc_wait()
241 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_smbus_quick()
242 const struct ibm_iic_timings *t = &timings[dev->fast_mode ? 1 : 0]; in iic_smbus_quick()
246 /* Only 7-bit addresses are supported */ in iic_smbus_quick()
247 if (unlikely(p->flags & I2C_M_TEN)){ in iic_smbus_quick()
248 DBG("%d: smbus_quick - 10 bit addresses are not supported\n", in iic_smbus_quick()
249 dev->idx); in iic_smbus_quick()
250 return -EINVAL; in iic_smbus_quick()
253 DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr); in iic_smbus_quick()
256 out_8(&iic->xtcntlss, XTCNTLSS_SRST); in iic_smbus_quick()
259 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC); in iic_smbus_quick()
262 ndelay(t->buf); in iic_smbus_quick()
265 out_8(&iic->directcntl, DIRCNTL_SCC); in iic_smbus_quick()
267 ndelay(t->hd_sta); in iic_smbus_quick()
272 out_8(&iic->directcntl, sda); in iic_smbus_quick()
273 ndelay(t->low / 2); in iic_smbus_quick()
275 out_8(&iic->directcntl, sda); in iic_smbus_quick()
276 ndelay(t->low / 2); in iic_smbus_quick()
278 out_8(&iic->directcntl, DIRCNTL_SCC | sda); in iic_smbus_quick()
281 ndelay(t->high); in iic_smbus_quick()
285 out_8(&iic->directcntl, sda); in iic_smbus_quick()
286 ndelay(t->low / 2); in iic_smbus_quick()
287 out_8(&iic->directcntl, DIRCNTL_SDAC); in iic_smbus_quick()
288 ndelay(t->low / 2); in iic_smbus_quick()
289 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC); in iic_smbus_quick()
292 res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1; in iic_smbus_quick()
293 ndelay(t->high); in iic_smbus_quick()
296 out_8(&iic->directcntl, 0); in iic_smbus_quick()
297 ndelay(t->low); in iic_smbus_quick()
298 out_8(&iic->directcntl, DIRCNTL_SCC); in iic_smbus_quick()
301 ndelay(t->su_sto); in iic_smbus_quick()
302 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC); in iic_smbus_quick()
304 ndelay(t->buf); in iic_smbus_quick()
306 DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK"); in iic_smbus_quick()
309 out_8(&iic->xtcntlss, 0); in iic_smbus_quick()
316 DBG("%d: smbus_quick - bus is stuck\n", dev->idx); in iic_smbus_quick()
317 res = -EREMOTEIO; in iic_smbus_quick()
327 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_handler()
330 dev->idx, in_8(&iic->sts), in_8(&iic->extsts)); in iic_handler()
333 out_8(&iic->sts, STS_IRQA | STS_SCMP); in iic_handler()
334 wake_up_interruptible(&dev->wq); in iic_handler()
345 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_xfer_result()
347 if (unlikely(in_8(&iic->sts) & STS_ERR)){ in iic_xfer_result()
348 DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx, in iic_xfer_result()
349 in_8(&iic->extsts)); in iic_xfer_result()
352 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | in iic_xfer_result()
356 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB); in iic_xfer_result()
361 * state, the only way out - soft reset. in iic_xfer_result()
363 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){ in iic_xfer_result()
364 DBG("%d: bus is stuck, resetting\n", dev->idx); in iic_xfer_result()
367 return -EREMOTEIO; in iic_xfer_result()
370 return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK; in iic_xfer_result()
378 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_abort_xfer()
381 DBG("%d: iic_abort_xfer\n", dev->idx); in iic_abort_xfer()
383 out_8(&iic->cntl, CNTL_HMT); in iic_abort_xfer()
390 while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){ in iic_abort_xfer()
392 DBG("%d: abort timeout, resetting...\n", dev->idx); in iic_abort_xfer()
410 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_wait_for_tc()
413 if (dev->irq >= 0){ in iic_wait_for_tc()
415 ret = wait_event_interruptible_timeout(dev->wq, in iic_wait_for_tc()
416 !(in_8(&iic->sts) & STS_PT), dev->adap.timeout); in iic_wait_for_tc()
419 DBG("%d: wait interrupted\n", dev->idx); in iic_wait_for_tc()
420 else if (unlikely(in_8(&iic->sts) & STS_PT)){ in iic_wait_for_tc()
421 DBG("%d: wait timeout\n", dev->idx); in iic_wait_for_tc()
422 ret = -ETIMEDOUT; in iic_wait_for_tc()
427 unsigned long x = jiffies + dev->adap.timeout; in iic_wait_for_tc()
429 while (in_8(&iic->sts) & STS_PT){ in iic_wait_for_tc()
431 DBG("%d: poll timeout\n", dev->idx); in iic_wait_for_tc()
432 ret = -ETIMEDOUT; in iic_wait_for_tc()
437 DBG("%d: poll interrupted\n", dev->idx); in iic_wait_for_tc()
438 ret = -ERESTARTSYS; in iic_wait_for_tc()
450 DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret); in iic_wait_for_tc()
458 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_xfer_bytes()
459 char* buf = pm->buf; in iic_xfer_bytes()
461 int len = pm->len; in iic_xfer_bytes()
463 u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT; in iic_xfer_bytes()
464 if (pm->flags & I2C_M_RD) in iic_xfer_bytes()
468 for (i = 0; i < loops; ++i, len -= 4){ in iic_xfer_bytes()
470 u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT); in iic_xfer_bytes()
474 out_8((void __iomem *)&iic->mdbuf, *buf++); in iic_xfer_bytes()
476 if (i < loops - 1) in iic_xfer_bytes()
481 DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd); in iic_xfer_bytes()
484 out_8(&iic->cntl, cmd); in iic_xfer_bytes()
493 dev->idx, count, ret); in iic_xfer_bytes()
496 if (combined_xfer || (i < loops - 1)) in iic_xfer_bytes()
499 ret = -EREMOTEIO; in iic_xfer_bytes()
505 *buf++ = in_8((void __iomem *)&iic->mdbuf); in iic_xfer_bytes()
514 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_address()
515 u16 addr = msg->addr; in iic_address()
517 DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx, in iic_address()
518 addr, msg->flags & I2C_M_TEN ? 10 : 7); in iic_address()
520 if (msg->flags & I2C_M_TEN){ in iic_address()
521 out_8(&iic->cntl, CNTL_AMD); in iic_address()
522 out_8(&iic->lmadr, addr); in iic_address()
523 out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06)); in iic_address()
526 out_8(&iic->cntl, 0); in iic_address()
527 out_8(&iic->lmadr, addr << 1); in iic_address()
533 return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f)); in iic_invalid_address()
539 return (p1->addr != p2->addr) in iic_address_neq()
540 || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN)); in iic_address_neq()
550 volatile struct iic_regs __iomem *iic = dev->vaddr; in iic_xfer()
553 DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num); in iic_xfer()
559 DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx, in iic_xfer()
561 return -EINVAL; in iic_xfer()
567 * IBM IIC doesn't support 0-length transactions in iic_xfer()
568 * so we have to emulate them using bit-banging. in iic_xfer()
572 DBG("%d: invalid len %d in msg[%d]\n", dev->idx, in iic_xfer()
574 return -EINVAL; in iic_xfer()
577 DBG("%d: invalid addr in msg[%d]\n", dev->idx, i); in iic_xfer()
578 return -EINVAL; in iic_xfer()
583 if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){ in iic_xfer()
584 DBG("%d: iic_xfer, bus is not free\n", dev->idx); in iic_xfer()
588 * so it doesn't make any sense to try to stop it. in iic_xfer()
592 * is soft reset. --ebs in iic_xfer()
596 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){ in iic_xfer()
597 DBG("%d: iic_xfer, bus is still not free\n", dev->idx); in iic_xfer()
598 return -EREMOTEIO; in iic_xfer()
603 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB); in iic_xfer()
611 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1); in iic_xfer()
637 printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq," in iic_clckdiv()
646 printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n", in iic_clckdiv()
650 return (u8)((opb + 9) / 10 - 1); in iic_clckdiv()
656 struct device_node *np = ofdev->dev.of_node; in iic_request_irq()
664 dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n"); in iic_request_irq()
669 * level-sensitive IRQ setup... in iic_request_irq()
673 dev_err(&ofdev->dev, "request_irq %d failed\n", irq); in iic_request_irq()
686 struct device_node *np = ofdev->dev.of_node; in iic_probe()
689 const u32 *freq; in iic_probe() local
694 return -ENOMEM; in iic_probe()
698 dev->vaddr = of_iomap(np, 0); in iic_probe()
699 if (dev->vaddr == NULL) { in iic_probe()
700 dev_err(&ofdev->dev, "failed to iomap device\n"); in iic_probe()
701 ret = -ENXIO; in iic_probe()
705 init_waitqueue_head(&dev->wq); in iic_probe()
707 dev->irq = iic_request_irq(ofdev, dev); in iic_probe()
708 if (!dev->irq) in iic_probe()
709 dev_warn(&ofdev->dev, "using polling mode\n"); in iic_probe()
712 if (iic_force_fast || of_get_property(np, "fast-mode", NULL)) in iic_probe()
713 dev->fast_mode = 1; in iic_probe()
715 freq = of_get_property(np, "clock-frequency", NULL); in iic_probe()
716 if (freq == NULL) { in iic_probe()
717 freq = of_get_property(np->parent, "clock-frequency", NULL); in iic_probe()
718 if (freq == NULL) { in iic_probe()
719 dev_err(&ofdev->dev, "Unable to get bus frequency\n"); in iic_probe()
720 ret = -EINVAL; in iic_probe()
725 dev->clckdiv = iic_clckdiv(*freq); in iic_probe()
726 dev_dbg(&ofdev->dev, "clckdiv = %d\n", dev->clckdiv); in iic_probe()
732 adap = &dev->adap; in iic_probe()
733 adap->dev.parent = &ofdev->dev; in iic_probe()
734 adap->dev.of_node = of_node_get(np); in iic_probe()
735 strscpy(adap->name, "IBM IIC", sizeof(adap->name)); in iic_probe()
737 adap->class = I2C_CLASS_HWMON; in iic_probe()
738 adap->algo = &iic_algo; in iic_probe()
739 adap->timeout = HZ; in iic_probe()
745 dev_info(&ofdev->dev, "using %s mode\n", in iic_probe()
746 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)"); in iic_probe()
751 if (dev->irq) { in iic_probe()
753 free_irq(dev->irq, dev); in iic_probe()
756 if (dev->vaddr) in iic_probe()
757 iounmap(dev->vaddr); in iic_probe()
770 i2c_del_adapter(&dev->adap); in iic_remove()
772 if (dev->irq) { in iic_remove()
774 free_irq(dev->irq, dev); in iic_remove()
777 iounmap(dev->vaddr); in iic_remove()
789 .name = "ibm-iic",