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 int vlynq_device_remove(struct device *dev)
347 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
350 drv->remove(to_vlynq_device(dev));
355 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
357 driver->driver.name = driver->name;
358 driver->driver.bus = &vlynq_bus_type;
359 return driver_register(&driver->driver);
361 EXPORT_SYMBOL(__vlynq_register_driver);
363 void vlynq_unregister_driver(struct vlynq_driver *driver)
365 driver_unregister(&driver->driver);
367 EXPORT_SYMBOL(vlynq_unregister_driver);
370 * A VLYNQ remote device can clock the VLYNQ bus master
371 * using a dedicated clock line. In that case, both the
372 * remove device and the bus master should have the same
373 * serial clock dividers configured. Iterate through the
374 * 8 possible dividers until we actually link with the
377 static int __vlynq_try_remote(struct vlynq_device *dev)
382 for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
383 i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
384 dev->dev_id ? i++ : i--) {
386 if (!vlynq_linked(dev))
389 writel((readl(&dev->remote->control) &
390 ~VLYNQ_CTRL_CLOCK_MASK) |
391 VLYNQ_CTRL_CLOCK_INT |
392 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
393 &dev->remote->control);
394 writel((readl(&dev->local->control)
395 & ~(VLYNQ_CTRL_CLOCK_INT |
396 VLYNQ_CTRL_CLOCK_MASK)) |
397 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
398 &dev->local->control);
400 if (vlynq_linked(dev)) {
402 "%s: using remote clock divisor %d\n",
403 dev_name(&dev->dev), i - vlynq_rdiv1 + 1);
415 * A VLYNQ remote device can be clocked by the VLYNQ bus
416 * master using a dedicated clock line. In that case, only
417 * the bus master configures the serial clock divider.
418 * Iterate through the 8 possible dividers until we
419 * actually get a link with the device.
421 static int __vlynq_try_local(struct vlynq_device *dev)
427 for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
428 i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
429 dev->dev_id ? i++ : i--) {
431 writel((readl(&dev->local->control) &
432 ~VLYNQ_CTRL_CLOCK_MASK) |
433 VLYNQ_CTRL_CLOCK_INT |
434 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1),
435 &dev->local->control);
437 if (vlynq_linked(dev)) {
439 "%s: using local clock divisor %d\n",
440 dev_name(&dev->dev), i - vlynq_ldiv1 + 1);
452 * When using external clocking method, serial clock
453 * is supplied by an external oscillator, therefore we
454 * should mask the local clock bit in the clock control
455 * register for both the bus master and the remote device.
457 static int __vlynq_try_external(struct vlynq_device *dev)
460 if (!vlynq_linked(dev))
463 writel((readl(&dev->remote->control) &
464 ~VLYNQ_CTRL_CLOCK_INT),
465 &dev->remote->control);
467 writel((readl(&dev->local->control) &
468 ~VLYNQ_CTRL_CLOCK_INT),
469 &dev->local->control);
471 if (vlynq_linked(dev)) {
472 printk(KERN_DEBUG "%s: using external clock\n",
473 dev_name(&dev->dev));
474 dev->divisor = vlynq_div_external;
481 static int __vlynq_enable_device(struct vlynq_device *dev)
484 struct plat_vlynq_ops *ops = dev->dev.platform_data;
486 result = ops->on(dev);
490 switch (dev->divisor) {
491 case vlynq_div_external:
493 /* When the device is brought from reset it should have clock
494 * generation negotiated by hardware.
495 * Check which device is generating clocks and perform setup
497 if (vlynq_linked(dev) && readl(&dev->remote->control) &
498 VLYNQ_CTRL_CLOCK_INT) {
499 if (!__vlynq_try_remote(dev) ||
500 !__vlynq_try_local(dev) ||
501 !__vlynq_try_external(dev))
504 if (!__vlynq_try_external(dev) ||
505 !__vlynq_try_local(dev) ||
506 !__vlynq_try_remote(dev))
518 writel(VLYNQ_CTRL_CLOCK_INT |
519 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
520 vlynq_ldiv1), &dev->local->control);
521 writel(0, &dev->remote->control);
522 if (vlynq_linked(dev)) {
524 "%s: using local clock divisor %d\n",
526 dev->divisor - vlynq_ldiv1 + 1);
538 writel(0, &dev->local->control);
539 writel(VLYNQ_CTRL_CLOCK_INT |
540 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
541 vlynq_rdiv1), &dev->remote->control);
542 if (vlynq_linked(dev)) {
544 "%s: using remote clock divisor %d\n",
546 dev->divisor - vlynq_rdiv1 + 1);
556 int vlynq_enable_device(struct vlynq_device *dev)
558 struct plat_vlynq_ops *ops = dev->dev.platform_data;
559 int result = -ENODEV;
561 result = __vlynq_enable_device(dev);
565 result = vlynq_setup_irq(dev);
569 dev->enabled = !result;
572 EXPORT_SYMBOL(vlynq_enable_device);
575 void vlynq_disable_device(struct vlynq_device *dev)
577 struct plat_vlynq_ops *ops = dev->dev.platform_data;
580 free_irq(dev->irq, dev);
583 EXPORT_SYMBOL(vlynq_disable_device);
585 int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
586 struct vlynq_mapping *mapping)
593 writel(tx_offset, &dev->local->tx_offset);
594 for (i = 0; i < 4; i++) {
595 writel(mapping[i].offset, &dev->local->rx_mapping[i].offset);
596 writel(mapping[i].size, &dev->local->rx_mapping[i].size);
600 EXPORT_SYMBOL(vlynq_set_local_mapping);
602 int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
603 struct vlynq_mapping *mapping)
610 writel(tx_offset, &dev->remote->tx_offset);
611 for (i = 0; i < 4; i++) {
612 writel(mapping[i].offset, &dev->remote->rx_mapping[i].offset);
613 writel(mapping[i].size, &dev->remote->rx_mapping[i].size);
617 EXPORT_SYMBOL(vlynq_set_remote_mapping);
619 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
621 int irq = dev->irq_start + virq;
625 if ((irq < dev->irq_start) || (irq > dev->irq_end))
628 if (virq == dev->remote_irq)
631 dev->local_irq = virq;
635 EXPORT_SYMBOL(vlynq_set_local_irq);
637 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
639 int irq = dev->irq_start + virq;
643 if ((irq < dev->irq_start) || (irq > dev->irq_end))
646 if (virq == dev->local_irq)
649 dev->remote_irq = virq;
653 EXPORT_SYMBOL(vlynq_set_remote_irq);
655 static int vlynq_probe(struct platform_device *pdev)
657 struct vlynq_device *dev;
658 struct resource *regs_res, *mem_res, *irq_res;
661 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
665 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
669 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
673 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
676 "vlynq: failed to allocate device structure\n");
681 dev->dev.bus = &vlynq_bus_type;
682 dev->dev.parent = &pdev->dev;
683 dev_set_name(&dev->dev, "vlynq%d", dev->id);
684 dev->dev.platform_data = pdev->dev.platform_data;
685 dev->dev.release = vlynq_device_release;
687 dev->regs_start = regs_res->start;
688 dev->regs_end = regs_res->end;
689 dev->mem_start = mem_res->start;
690 dev->mem_end = mem_res->end;
692 len = resource_size(regs_res);
693 if (!request_mem_region(regs_res->start, len, dev_name(&dev->dev))) {
694 printk(KERN_ERR "%s: Can't request vlynq registers\n",
695 dev_name(&dev->dev));
700 dev->local = ioremap(regs_res->start, len);
702 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
703 dev_name(&dev->dev));
708 dev->remote = (struct vlynq_regs *)((void *)dev->local +
709 VLYNQ_REMOTE_OFFSET);
711 dev->irq = platform_get_irq_byname(pdev, "irq");
712 dev->irq_start = irq_res->start;
713 dev->irq_end = irq_res->end;
714 dev->local_irq = dev->irq_end - dev->irq_start;
715 dev->remote_irq = dev->local_irq - 1;
717 if (device_register(&dev->dev))
719 platform_set_drvdata(pdev, dev);
721 printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
722 dev_name(&dev->dev), (void *)dev->regs_start, dev->irq,
723 (void *)dev->mem_start);
726 dev->divisor = vlynq_div_auto;
727 result = __vlynq_enable_device(dev);
729 dev->dev_id = readl(&dev->remote->chip);
730 ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
733 printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
741 release_mem_region(regs_res->start, len);
746 static int vlynq_remove(struct platform_device *pdev)
748 struct vlynq_device *dev = platform_get_drvdata(pdev);
750 device_unregister(&dev->dev);
752 release_mem_region(dev->regs_start,
753 dev->regs_end - dev->regs_start + 1);
760 static struct platform_driver vlynq_platform_driver = {
761 .driver.name = "vlynq",
762 .probe = vlynq_probe,
763 .remove = vlynq_remove,
766 struct bus_type vlynq_bus_type = {
768 .match = vlynq_device_match,
769 .probe = vlynq_device_probe,
770 .remove = vlynq_device_remove,
772 EXPORT_SYMBOL(vlynq_bus_type);
774 static int vlynq_init(void)
778 res = bus_register(&vlynq_bus_type);
782 res = platform_driver_register(&vlynq_platform_driver);
789 bus_unregister(&vlynq_bus_type);
794 static void vlynq_exit(void)
796 platform_driver_unregister(&vlynq_platform_driver);
797 bus_unregister(&vlynq_bus_type);
800 module_init(vlynq_init);
801 module_exit(vlynq_exit);