1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2006, 2007 Eugene Konev <ejka@openwrt.org>
5 * Parts of the VLYNQ specification can be found here:
6 * http://www.ti.com/litv/pdf/sprue36a
9 #include <linux/init.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/platform_device.h>
17 #include <linux/interrupt.h>
18 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/irq.h>
23 #include <linux/vlynq.h>
25 #define VLYNQ_CTRL_PM_ENABLE 0x80000000
26 #define VLYNQ_CTRL_CLOCK_INT 0x00008000
27 #define VLYNQ_CTRL_CLOCK_DIV(x) (((x) & 7) << 16)
28 #define VLYNQ_CTRL_INT_LOCAL 0x00004000
29 #define VLYNQ_CTRL_INT_ENABLE 0x00002000
30 #define VLYNQ_CTRL_INT_VECTOR(x) (((x) & 0x1f) << 8)
31 #define VLYNQ_CTRL_INT2CFG 0x00000080
32 #define VLYNQ_CTRL_RESET 0x00000001
34 #define VLYNQ_CTRL_CLOCK_MASK (0x7 << 16)
36 #define VLYNQ_INT_OFFSET 0x00000014
37 #define VLYNQ_REMOTE_OFFSET 0x00000080
39 #define VLYNQ_STATUS_LINK 0x00000001
40 #define VLYNQ_STATUS_LERROR 0x00000080
41 #define VLYNQ_STATUS_RERROR 0x00000100
43 #define VINT_ENABLE 0x00000100
44 #define VINT_TYPE_EDGE 0x00000080
45 #define VINT_LEVEL_LOW 0x00000040
46 #define VINT_VECTOR(x) ((x) & 0x1f)
47 #define VINT_OFFSET(irq) (8 * ((irq) % 4))
49 #define VLYNQ_AUTONEGO_V2 0x00010000
60 struct vlynq_mapping rx_mapping[4];
67 #ifdef CONFIG_VLYNQ_DEBUG
68 static void vlynq_dump_regs(struct vlynq_device *dev)
72 printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n",
73 dev->local, dev->remote);
74 for (i = 0; i < 32; i++) {
75 printk(KERN_DEBUG "VLYNQ: local %d: %08x\n",
76 i + 1, ((u32 *)dev->local)[i]);
77 printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n",
78 i + 1, ((u32 *)dev->remote)[i]);
82 static void vlynq_dump_mem(u32 *base, int count)
86 for (i = 0; i < (count + 3) / 4; i++) {
88 printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4);
89 printk(KERN_DEBUG " 0x%08x", *(base + i));
91 printk(KERN_DEBUG "\n");
95 /* Check the VLYNQ link status with a given device */
96 static int vlynq_linked(struct vlynq_device *dev)
100 for (i = 0; i < 100; i++)
101 if (readl(&dev->local->status) & VLYNQ_STATUS_LINK)
109 static void vlynq_reset(struct vlynq_device *dev)
111 writel(readl(&dev->local->control) | VLYNQ_CTRL_RESET,
112 &dev->local->control);
114 /* Wait for the devices to finish resetting */
117 /* Remove reset bit */
118 writel(readl(&dev->local->control) & ~VLYNQ_CTRL_RESET,
119 &dev->local->control);
121 /* Give some time for the devices to settle */
125 static void vlynq_irq_unmask(struct irq_data *d)
127 struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
132 virq = d->irq - dev->irq_start;
133 val = readl(&dev->remote->int_device[virq >> 2]);
134 val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
135 writel(val, &dev->remote->int_device[virq >> 2]);
138 static void vlynq_irq_mask(struct irq_data *d)
140 struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
145 virq = d->irq - dev->irq_start;
146 val = readl(&dev->remote->int_device[virq >> 2]);
147 val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
148 writel(val, &dev->remote->int_device[virq >> 2]);
151 static int vlynq_irq_type(struct irq_data *d, unsigned int flow_type)
153 struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
158 virq = d->irq - dev->irq_start;
159 val = readl(&dev->remote->int_device[virq >> 2]);
160 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
161 case IRQ_TYPE_EDGE_RISING:
162 case IRQ_TYPE_EDGE_FALLING:
163 case IRQ_TYPE_EDGE_BOTH:
164 val |= VINT_TYPE_EDGE << VINT_OFFSET(virq);
165 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
167 case IRQ_TYPE_LEVEL_HIGH:
168 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
169 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
171 case IRQ_TYPE_LEVEL_LOW:
172 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
173 val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
178 writel(val, &dev->remote->int_device[virq >> 2]);
182 static void vlynq_local_ack(struct irq_data *d)
184 struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
185 u32 status = readl(&dev->local->status);
187 pr_debug("%s: local status: 0x%08x\n",
188 dev_name(&dev->dev), status);
189 writel(status, &dev->local->status);
192 static void vlynq_remote_ack(struct irq_data *d)
194 struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
195 u32 status = readl(&dev->remote->status);
197 pr_debug("%s: remote status: 0x%08x\n",
198 dev_name(&dev->dev), status);
199 writel(status, &dev->remote->status);
202 static irqreturn_t vlynq_irq(int irq, void *dev_id)
204 struct vlynq_device *dev = dev_id;
208 status = readl(&dev->local->int_status);
209 writel(status, &dev->local->int_status);
211 if (unlikely(!status))
212 spurious_interrupt();
216 do_IRQ(dev->irq_start + virq);
224 static struct irq_chip vlynq_irq_chip = {
226 .irq_unmask = vlynq_irq_unmask,
227 .irq_mask = vlynq_irq_mask,
228 .irq_set_type = vlynq_irq_type,
231 static struct irq_chip vlynq_local_chip = {
232 .name = "vlynq local error",
233 .irq_unmask = vlynq_irq_unmask,
234 .irq_mask = vlynq_irq_mask,
235 .irq_ack = vlynq_local_ack,
238 static struct irq_chip vlynq_remote_chip = {
239 .name = "vlynq local error",
240 .irq_unmask = vlynq_irq_unmask,
241 .irq_mask = vlynq_irq_mask,
242 .irq_ack = vlynq_remote_ack,
245 static int vlynq_setup_irq(struct vlynq_device *dev)
250 if (dev->local_irq == dev->remote_irq) {
252 "%s: local vlynq irq should be different from remote\n",
253 dev_name(&dev->dev));
257 /* Clear local and remote error bits */
258 writel(readl(&dev->local->status), &dev->local->status);
259 writel(readl(&dev->remote->status), &dev->remote->status);
261 /* Now setup interrupts */
262 val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
263 val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
265 val |= readl(&dev->local->control);
266 writel(VLYNQ_INT_OFFSET, &dev->local->int_ptr);
267 writel(val, &dev->local->control);
269 val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
270 val |= VLYNQ_CTRL_INT_ENABLE;
271 val |= readl(&dev->remote->control);
272 writel(VLYNQ_INT_OFFSET, &dev->remote->int_ptr);
273 writel(val, &dev->remote->int_ptr);
274 writel(val, &dev->remote->control);
276 for (i = dev->irq_start; i <= dev->irq_end; i++) {
277 virq = i - dev->irq_start;
278 if (virq == dev->local_irq) {
279 irq_set_chip_and_handler(i, &vlynq_local_chip,
281 irq_set_chip_data(i, dev);
282 } else if (virq == dev->remote_irq) {
283 irq_set_chip_and_handler(i, &vlynq_remote_chip,
285 irq_set_chip_data(i, dev);
287 irq_set_chip_and_handler(i, &vlynq_irq_chip,
289 irq_set_chip_data(i, dev);
290 writel(0, &dev->remote->int_device[virq >> 2]);
294 if (request_irq(dev->irq, vlynq_irq, IRQF_SHARED, "vlynq", dev)) {
295 printk(KERN_ERR "%s: request_irq failed\n",
296 dev_name(&dev->dev));
303 static void vlynq_device_release(struct device *dev)
305 struct vlynq_device *vdev = to_vlynq_device(dev);
309 static int vlynq_device_match(struct device *dev,
310 struct device_driver *drv)
312 struct vlynq_device *vdev = to_vlynq_device(dev);
313 struct vlynq_driver *vdrv = to_vlynq_driver(drv);
314 struct vlynq_device_id *ids = vdrv->id_table;
317 if (ids->id == vdev->dev_id) {
318 vdev->divisor = ids->divisor;
319 vlynq_set_drvdata(vdev, ids);
320 printk(KERN_INFO "Driver found for VLYNQ "
321 "device: %08x\n", vdev->dev_id);
324 printk(KERN_DEBUG "Not using the %08x VLYNQ device's driver"
325 " for VLYNQ device: %08x\n", ids->id, vdev->dev_id);
331 static int vlynq_device_probe(struct device *dev)
333 struct vlynq_device *vdev = to_vlynq_device(dev);
334 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
335 struct vlynq_device_id *id = vlynq_get_drvdata(vdev);
336 int result = -ENODEV;
339 result = drv->probe(vdev, id);
345 static void vlynq_device_remove(struct device *dev)
347 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
350 drv->remove(to_vlynq_device(dev));
353 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
355 driver->driver.name = driver->name;
356 driver->driver.bus = &vlynq_bus_type;
357 return driver_register(&driver->driver);
359 EXPORT_SYMBOL(__vlynq_register_driver);
361 void vlynq_unregister_driver(struct vlynq_driver *driver)
363 driver_unregister(&driver->driver);
365 EXPORT_SYMBOL(vlynq_unregister_driver);
368 * A VLYNQ remote device can clock the VLYNQ bus master
369 * using a dedicated clock line. In that case, both the
370 * remove device and the bus master should have the same
371 * serial clock dividers configured. Iterate through the
372 * 8 possible dividers until we actually link with the
375 static int __vlynq_try_remote(struct vlynq_device *dev)
380 for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
381 i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
382 dev->dev_id ? i++ : i--) {
384 if (!vlynq_linked(dev))
387 writel((readl(&dev->remote->control) &
388 ~VLYNQ_CTRL_CLOCK_MASK) |
389 VLYNQ_CTRL_CLOCK_INT |
390 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
391 &dev->remote->control);
392 writel((readl(&dev->local->control)
393 & ~(VLYNQ_CTRL_CLOCK_INT |
394 VLYNQ_CTRL_CLOCK_MASK)) |
395 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
396 &dev->local->control);
398 if (vlynq_linked(dev)) {
400 "%s: using remote clock divisor %d\n",
401 dev_name(&dev->dev), i - vlynq_rdiv1 + 1);
413 * A VLYNQ remote device can be clocked by the VLYNQ bus
414 * master using a dedicated clock line. In that case, only
415 * the bus master configures the serial clock divider.
416 * Iterate through the 8 possible dividers until we
417 * actually get a link with the device.
419 static int __vlynq_try_local(struct vlynq_device *dev)
425 for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
426 i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
427 dev->dev_id ? i++ : i--) {
429 writel((readl(&dev->local->control) &
430 ~VLYNQ_CTRL_CLOCK_MASK) |
431 VLYNQ_CTRL_CLOCK_INT |
432 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1),
433 &dev->local->control);
435 if (vlynq_linked(dev)) {
437 "%s: using local clock divisor %d\n",
438 dev_name(&dev->dev), i - vlynq_ldiv1 + 1);
450 * When using external clocking method, serial clock
451 * is supplied by an external oscillator, therefore we
452 * should mask the local clock bit in the clock control
453 * register for both the bus master and the remote device.
455 static int __vlynq_try_external(struct vlynq_device *dev)
458 if (!vlynq_linked(dev))
461 writel((readl(&dev->remote->control) &
462 ~VLYNQ_CTRL_CLOCK_INT),
463 &dev->remote->control);
465 writel((readl(&dev->local->control) &
466 ~VLYNQ_CTRL_CLOCK_INT),
467 &dev->local->control);
469 if (vlynq_linked(dev)) {
470 printk(KERN_DEBUG "%s: using external clock\n",
471 dev_name(&dev->dev));
472 dev->divisor = vlynq_div_external;
479 static int __vlynq_enable_device(struct vlynq_device *dev)
482 struct plat_vlynq_ops *ops = dev->dev.platform_data;
484 result = ops->on(dev);
488 switch (dev->divisor) {
489 case vlynq_div_external:
491 /* When the device is brought from reset it should have clock
492 * generation negotiated by hardware.
493 * Check which device is generating clocks and perform setup
495 if (vlynq_linked(dev) && readl(&dev->remote->control) &
496 VLYNQ_CTRL_CLOCK_INT) {
497 if (!__vlynq_try_remote(dev) ||
498 !__vlynq_try_local(dev) ||
499 !__vlynq_try_external(dev))
502 if (!__vlynq_try_external(dev) ||
503 !__vlynq_try_local(dev) ||
504 !__vlynq_try_remote(dev))
516 writel(VLYNQ_CTRL_CLOCK_INT |
517 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
518 vlynq_ldiv1), &dev->local->control);
519 writel(0, &dev->remote->control);
520 if (vlynq_linked(dev)) {
522 "%s: using local clock divisor %d\n",
524 dev->divisor - vlynq_ldiv1 + 1);
536 writel(0, &dev->local->control);
537 writel(VLYNQ_CTRL_CLOCK_INT |
538 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
539 vlynq_rdiv1), &dev->remote->control);
540 if (vlynq_linked(dev)) {
542 "%s: using remote clock divisor %d\n",
544 dev->divisor - vlynq_rdiv1 + 1);
554 int vlynq_enable_device(struct vlynq_device *dev)
556 struct plat_vlynq_ops *ops = dev->dev.platform_data;
557 int result = -ENODEV;
559 result = __vlynq_enable_device(dev);
563 result = vlynq_setup_irq(dev);
567 dev->enabled = !result;
570 EXPORT_SYMBOL(vlynq_enable_device);
573 void vlynq_disable_device(struct vlynq_device *dev)
575 struct plat_vlynq_ops *ops = dev->dev.platform_data;
578 free_irq(dev->irq, dev);
581 EXPORT_SYMBOL(vlynq_disable_device);
583 int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
584 struct vlynq_mapping *mapping)
591 writel(tx_offset, &dev->local->tx_offset);
592 for (i = 0; i < 4; i++) {
593 writel(mapping[i].offset, &dev->local->rx_mapping[i].offset);
594 writel(mapping[i].size, &dev->local->rx_mapping[i].size);
598 EXPORT_SYMBOL(vlynq_set_local_mapping);
600 int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
601 struct vlynq_mapping *mapping)
608 writel(tx_offset, &dev->remote->tx_offset);
609 for (i = 0; i < 4; i++) {
610 writel(mapping[i].offset, &dev->remote->rx_mapping[i].offset);
611 writel(mapping[i].size, &dev->remote->rx_mapping[i].size);
615 EXPORT_SYMBOL(vlynq_set_remote_mapping);
617 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
619 int irq = dev->irq_start + virq;
623 if ((irq < dev->irq_start) || (irq > dev->irq_end))
626 if (virq == dev->remote_irq)
629 dev->local_irq = virq;
633 EXPORT_SYMBOL(vlynq_set_local_irq);
635 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
637 int irq = dev->irq_start + virq;
641 if ((irq < dev->irq_start) || (irq > dev->irq_end))
644 if (virq == dev->local_irq)
647 dev->remote_irq = virq;
651 EXPORT_SYMBOL(vlynq_set_remote_irq);
653 static int vlynq_probe(struct platform_device *pdev)
655 struct vlynq_device *dev;
656 struct resource *regs_res, *mem_res, *irq_res;
659 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
663 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
667 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
671 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
674 "vlynq: failed to allocate device structure\n");
679 dev->dev.bus = &vlynq_bus_type;
680 dev->dev.parent = &pdev->dev;
681 dev_set_name(&dev->dev, "vlynq%d", dev->id);
682 dev->dev.platform_data = pdev->dev.platform_data;
683 dev->dev.release = vlynq_device_release;
685 dev->regs_start = regs_res->start;
686 dev->regs_end = regs_res->end;
687 dev->mem_start = mem_res->start;
688 dev->mem_end = mem_res->end;
690 len = resource_size(regs_res);
691 if (!request_mem_region(regs_res->start, len, dev_name(&dev->dev))) {
692 printk(KERN_ERR "%s: Can't request vlynq registers\n",
693 dev_name(&dev->dev));
698 dev->local = ioremap(regs_res->start, len);
700 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
701 dev_name(&dev->dev));
706 dev->remote = (struct vlynq_regs *)((void *)dev->local +
707 VLYNQ_REMOTE_OFFSET);
709 dev->irq = platform_get_irq_byname(pdev, "irq");
710 dev->irq_start = irq_res->start;
711 dev->irq_end = irq_res->end;
712 dev->local_irq = dev->irq_end - dev->irq_start;
713 dev->remote_irq = dev->local_irq - 1;
715 if (device_register(&dev->dev))
717 platform_set_drvdata(pdev, dev);
719 printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
720 dev_name(&dev->dev), (void *)dev->regs_start, dev->irq,
721 (void *)dev->mem_start);
724 dev->divisor = vlynq_div_auto;
725 result = __vlynq_enable_device(dev);
727 dev->dev_id = readl(&dev->remote->chip);
728 ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
731 printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
739 release_mem_region(regs_res->start, len);
744 static int vlynq_remove(struct platform_device *pdev)
746 struct vlynq_device *dev = platform_get_drvdata(pdev);
748 device_unregister(&dev->dev);
750 release_mem_region(dev->regs_start,
751 dev->regs_end - dev->regs_start + 1);
758 static struct platform_driver vlynq_platform_driver = {
759 .driver.name = "vlynq",
760 .probe = vlynq_probe,
761 .remove = vlynq_remove,
764 struct bus_type vlynq_bus_type = {
766 .match = vlynq_device_match,
767 .probe = vlynq_device_probe,
768 .remove = vlynq_device_remove,
770 EXPORT_SYMBOL(vlynq_bus_type);
772 static int vlynq_init(void)
776 res = bus_register(&vlynq_bus_type);
780 res = platform_driver_register(&vlynq_platform_driver);
787 bus_unregister(&vlynq_bus_type);
792 static void vlynq_exit(void)
794 platform_driver_unregister(&vlynq_platform_driver);
795 bus_unregister(&vlynq_bus_type);
798 module_init(vlynq_init);
799 module_exit(vlynq_exit);