2 * Copyright (C) 2006, 2007 Eugene Konev <ejka@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Parts of the VLYNQ specification can be found here:
19 * http://www.ti.com/litv/pdf/sprue36a
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/string.h>
26 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/platform_device.h>
30 #include <linux/interrupt.h>
31 #include <linux/device.h>
32 #include <linux/delay.h>
35 #include <linux/vlynq.h>
37 #define VLYNQ_CTRL_PM_ENABLE 0x80000000
38 #define VLYNQ_CTRL_CLOCK_INT 0x00008000
39 #define VLYNQ_CTRL_CLOCK_DIV(x) (((x) & 7) << 16)
40 #define VLYNQ_CTRL_INT_LOCAL 0x00004000
41 #define VLYNQ_CTRL_INT_ENABLE 0x00002000
42 #define VLYNQ_CTRL_INT_VECTOR(x) (((x) & 0x1f) << 8)
43 #define VLYNQ_CTRL_INT2CFG 0x00000080
44 #define VLYNQ_CTRL_RESET 0x00000001
46 #define VLYNQ_CTRL_CLOCK_MASK (0x7 << 16)
48 #define VLYNQ_INT_OFFSET 0x00000014
49 #define VLYNQ_REMOTE_OFFSET 0x00000080
51 #define VLYNQ_STATUS_LINK 0x00000001
52 #define VLYNQ_STATUS_LERROR 0x00000080
53 #define VLYNQ_STATUS_RERROR 0x00000100
55 #define VINT_ENABLE 0x00000100
56 #define VINT_TYPE_EDGE 0x00000080
57 #define VINT_LEVEL_LOW 0x00000040
58 #define VINT_VECTOR(x) ((x) & 0x1f)
59 #define VINT_OFFSET(irq) (8 * ((irq) % 4))
61 #define VLYNQ_AUTONEGO_V2 0x00010000
72 struct vlynq_mapping rx_mapping[4];
79 #ifdef CONFIG_VLYNQ_DEBUG
80 static void vlynq_dump_regs(struct vlynq_device *dev)
84 printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n",
85 dev->local, dev->remote);
86 for (i = 0; i < 32; i++) {
87 printk(KERN_DEBUG "VLYNQ: local %d: %08x\n",
88 i + 1, ((u32 *)dev->local)[i]);
89 printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n",
90 i + 1, ((u32 *)dev->remote)[i]);
94 static void vlynq_dump_mem(u32 *base, int count)
98 for (i = 0; i < (count + 3) / 4; i++) {
100 printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4);
101 printk(KERN_DEBUG " 0x%08x", *(base + i));
103 printk(KERN_DEBUG "\n");
107 /* Check the VLYNQ link status with a given device */
108 static int vlynq_linked(struct vlynq_device *dev)
112 for (i = 0; i < 100; i++)
113 if (readl(&dev->local->status) & VLYNQ_STATUS_LINK)
121 static void vlynq_reset(struct vlynq_device *dev)
123 writel(readl(&dev->local->control) | VLYNQ_CTRL_RESET,
124 &dev->local->control);
126 /* Wait for the devices to finish resetting */
129 /* Remove reset bit */
130 writel(readl(&dev->local->control) & ~VLYNQ_CTRL_RESET,
131 &dev->local->control);
133 /* Give some time for the devices to settle */
137 static void vlynq_irq_unmask(unsigned int irq)
140 struct vlynq_device *dev = get_irq_chip_data(irq);
144 virq = irq - dev->irq_start;
145 val = readl(&dev->remote->int_device[virq >> 2]);
146 val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
147 writel(val, &dev->remote->int_device[virq >> 2]);
150 static void vlynq_irq_mask(unsigned int irq)
153 struct vlynq_device *dev = get_irq_chip_data(irq);
157 virq = irq - dev->irq_start;
158 val = readl(&dev->remote->int_device[virq >> 2]);
159 val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
160 writel(val, &dev->remote->int_device[virq >> 2]);
163 static int vlynq_irq_type(unsigned int irq, unsigned int flow_type)
166 struct vlynq_device *dev = get_irq_chip_data(irq);
170 virq = irq - dev->irq_start;
171 val = readl(&dev->remote->int_device[virq >> 2]);
172 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
173 case IRQ_TYPE_EDGE_RISING:
174 case IRQ_TYPE_EDGE_FALLING:
175 case IRQ_TYPE_EDGE_BOTH:
176 val |= VINT_TYPE_EDGE << VINT_OFFSET(virq);
177 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
179 case IRQ_TYPE_LEVEL_HIGH:
180 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
181 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
183 case IRQ_TYPE_LEVEL_LOW:
184 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
185 val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
190 writel(val, &dev->remote->int_device[virq >> 2]);
194 static void vlynq_local_ack(unsigned int irq)
196 struct vlynq_device *dev = get_irq_chip_data(irq);
198 u32 status = readl(&dev->local->status);
200 pr_debug("%s: local status: 0x%08x\n",
201 dev_name(&dev->dev), status);
202 writel(status, &dev->local->status);
205 static void vlynq_remote_ack(unsigned int irq)
207 struct vlynq_device *dev = get_irq_chip_data(irq);
209 u32 status = readl(&dev->remote->status);
211 pr_debug("%s: remote status: 0x%08x\n",
212 dev_name(&dev->dev), status);
213 writel(status, &dev->remote->status);
216 static irqreturn_t vlynq_irq(int irq, void *dev_id)
218 struct vlynq_device *dev = dev_id;
222 status = readl(&dev->local->int_status);
223 writel(status, &dev->local->int_status);
225 if (unlikely(!status))
226 spurious_interrupt();
230 do_IRQ(dev->irq_start + virq);
238 static struct irq_chip vlynq_irq_chip = {
240 .unmask = vlynq_irq_unmask,
241 .mask = vlynq_irq_mask,
242 .set_type = vlynq_irq_type,
245 static struct irq_chip vlynq_local_chip = {
246 .name = "vlynq local error",
247 .unmask = vlynq_irq_unmask,
248 .mask = vlynq_irq_mask,
249 .ack = vlynq_local_ack,
252 static struct irq_chip vlynq_remote_chip = {
253 .name = "vlynq local error",
254 .unmask = vlynq_irq_unmask,
255 .mask = vlynq_irq_mask,
256 .ack = vlynq_remote_ack,
259 static int vlynq_setup_irq(struct vlynq_device *dev)
264 if (dev->local_irq == dev->remote_irq) {
266 "%s: local vlynq irq should be different from remote\n",
267 dev_name(&dev->dev));
271 /* Clear local and remote error bits */
272 writel(readl(&dev->local->status), &dev->local->status);
273 writel(readl(&dev->remote->status), &dev->remote->status);
275 /* Now setup interrupts */
276 val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
277 val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
279 val |= readl(&dev->local->control);
280 writel(VLYNQ_INT_OFFSET, &dev->local->int_ptr);
281 writel(val, &dev->local->control);
283 val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
284 val |= VLYNQ_CTRL_INT_ENABLE;
285 val |= readl(&dev->remote->control);
286 writel(VLYNQ_INT_OFFSET, &dev->remote->int_ptr);
287 writel(val, &dev->remote->int_ptr);
288 writel(val, &dev->remote->control);
290 for (i = dev->irq_start; i <= dev->irq_end; i++) {
291 virq = i - dev->irq_start;
292 if (virq == dev->local_irq) {
293 set_irq_chip_and_handler(i, &vlynq_local_chip,
295 set_irq_chip_data(i, dev);
296 } else if (virq == dev->remote_irq) {
297 set_irq_chip_and_handler(i, &vlynq_remote_chip,
299 set_irq_chip_data(i, dev);
301 set_irq_chip_and_handler(i, &vlynq_irq_chip,
303 set_irq_chip_data(i, dev);
304 writel(0, &dev->remote->int_device[virq >> 2]);
308 if (request_irq(dev->irq, vlynq_irq, IRQF_SHARED, "vlynq", dev)) {
309 printk(KERN_ERR "%s: request_irq failed\n",
310 dev_name(&dev->dev));
317 static void vlynq_device_release(struct device *dev)
319 struct vlynq_device *vdev = to_vlynq_device(dev);
323 static int vlynq_device_match(struct device *dev,
324 struct device_driver *drv)
326 struct vlynq_device *vdev = to_vlynq_device(dev);
327 struct vlynq_driver *vdrv = to_vlynq_driver(drv);
328 struct vlynq_device_id *ids = vdrv->id_table;
331 if (ids->id == vdev->dev_id) {
332 vdev->divisor = ids->divisor;
333 vlynq_set_drvdata(vdev, ids);
334 printk(KERN_INFO "Driver found for VLYNQ "
335 "device: %08x\n", vdev->dev_id);
338 printk(KERN_DEBUG "Not using the %08x VLYNQ device's driver"
339 " for VLYNQ device: %08x\n", ids->id, vdev->dev_id);
345 static int vlynq_device_probe(struct device *dev)
347 struct vlynq_device *vdev = to_vlynq_device(dev);
348 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
349 struct vlynq_device_id *id = vlynq_get_drvdata(vdev);
350 int result = -ENODEV;
353 result = drv->probe(vdev, id);
359 static int vlynq_device_remove(struct device *dev)
361 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
364 drv->remove(to_vlynq_device(dev));
369 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
371 driver->driver.name = driver->name;
372 driver->driver.bus = &vlynq_bus_type;
373 return driver_register(&driver->driver);
375 EXPORT_SYMBOL(__vlynq_register_driver);
377 void vlynq_unregister_driver(struct vlynq_driver *driver)
379 driver_unregister(&driver->driver);
381 EXPORT_SYMBOL(vlynq_unregister_driver);
384 * A VLYNQ remote device can clock the VLYNQ bus master
385 * using a dedicated clock line. In that case, both the
386 * remove device and the bus master should have the same
387 * serial clock dividers configured. Iterate through the
388 * 8 possible dividers until we actually link with the
391 static int __vlynq_try_remote(struct vlynq_device *dev)
396 for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
397 i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
398 dev->dev_id ? i++ : i--) {
400 if (!vlynq_linked(dev))
403 writel((readl(&dev->remote->control) &
404 ~VLYNQ_CTRL_CLOCK_MASK) |
405 VLYNQ_CTRL_CLOCK_INT |
406 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
407 &dev->remote->control);
408 writel((readl(&dev->local->control)
409 & ~(VLYNQ_CTRL_CLOCK_INT |
410 VLYNQ_CTRL_CLOCK_MASK)) |
411 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
412 &dev->local->control);
414 if (vlynq_linked(dev)) {
416 "%s: using remote clock divisor %d\n",
417 dev_name(&dev->dev), i - vlynq_rdiv1 + 1);
429 * A VLYNQ remote device can be clocked by the VLYNQ bus
430 * master using a dedicated clock line. In that case, only
431 * the bus master configures the serial clock divider.
432 * Iterate through the 8 possible dividers until we
433 * actually get a link with the device.
435 static int __vlynq_try_local(struct vlynq_device *dev)
441 for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
442 i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
443 dev->dev_id ? i++ : i--) {
445 writel((readl(&dev->local->control) &
446 ~VLYNQ_CTRL_CLOCK_MASK) |
447 VLYNQ_CTRL_CLOCK_INT |
448 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1),
449 &dev->local->control);
451 if (vlynq_linked(dev)) {
453 "%s: using local clock divisor %d\n",
454 dev_name(&dev->dev), i - vlynq_ldiv1 + 1);
466 * When using external clocking method, serial clock
467 * is supplied by an external oscillator, therefore we
468 * should mask the local clock bit in the clock control
469 * register for both the bus master and the remote device.
471 static int __vlynq_try_external(struct vlynq_device *dev)
474 if (!vlynq_linked(dev))
477 writel((readl(&dev->remote->control) &
478 ~VLYNQ_CTRL_CLOCK_INT),
479 &dev->remote->control);
481 writel((readl(&dev->local->control) &
482 ~VLYNQ_CTRL_CLOCK_INT),
483 &dev->local->control);
485 if (vlynq_linked(dev)) {
486 printk(KERN_DEBUG "%s: using external clock\n",
487 dev_name(&dev->dev));
488 dev->divisor = vlynq_div_external;
495 static int __vlynq_enable_device(struct vlynq_device *dev)
498 struct plat_vlynq_ops *ops = dev->dev.platform_data;
500 result = ops->on(dev);
504 switch (dev->divisor) {
505 case vlynq_div_external:
507 /* When the device is brought from reset it should have clock
508 * generation negotiated by hardware.
509 * Check which device is generating clocks and perform setup
511 if (vlynq_linked(dev) && readl(&dev->remote->control) &
512 VLYNQ_CTRL_CLOCK_INT) {
513 if (!__vlynq_try_remote(dev) ||
514 !__vlynq_try_local(dev) ||
515 !__vlynq_try_external(dev))
518 if (!__vlynq_try_external(dev) ||
519 !__vlynq_try_local(dev) ||
520 !__vlynq_try_remote(dev))
532 writel(VLYNQ_CTRL_CLOCK_INT |
533 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
534 vlynq_ldiv1), &dev->local->control);
535 writel(0, &dev->remote->control);
536 if (vlynq_linked(dev)) {
538 "%s: using local clock divisor %d\n",
540 dev->divisor - vlynq_ldiv1 + 1);
552 writel(0, &dev->local->control);
553 writel(VLYNQ_CTRL_CLOCK_INT |
554 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
555 vlynq_rdiv1), &dev->remote->control);
556 if (vlynq_linked(dev)) {
558 "%s: using remote clock divisor %d\n",
560 dev->divisor - vlynq_rdiv1 + 1);
570 int vlynq_enable_device(struct vlynq_device *dev)
572 struct plat_vlynq_ops *ops = dev->dev.platform_data;
573 int result = -ENODEV;
575 result = __vlynq_enable_device(dev);
579 result = vlynq_setup_irq(dev);
583 dev->enabled = !result;
586 EXPORT_SYMBOL(vlynq_enable_device);
589 void vlynq_disable_device(struct vlynq_device *dev)
591 struct plat_vlynq_ops *ops = dev->dev.platform_data;
594 free_irq(dev->irq, dev);
597 EXPORT_SYMBOL(vlynq_disable_device);
599 int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
600 struct vlynq_mapping *mapping)
607 writel(tx_offset, &dev->local->tx_offset);
608 for (i = 0; i < 4; i++) {
609 writel(mapping[i].offset, &dev->local->rx_mapping[i].offset);
610 writel(mapping[i].size, &dev->local->rx_mapping[i].size);
614 EXPORT_SYMBOL(vlynq_set_local_mapping);
616 int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
617 struct vlynq_mapping *mapping)
624 writel(tx_offset, &dev->remote->tx_offset);
625 for (i = 0; i < 4; i++) {
626 writel(mapping[i].offset, &dev->remote->rx_mapping[i].offset);
627 writel(mapping[i].size, &dev->remote->rx_mapping[i].size);
631 EXPORT_SYMBOL(vlynq_set_remote_mapping);
633 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
635 int irq = dev->irq_start + virq;
639 if ((irq < dev->irq_start) || (irq > dev->irq_end))
642 if (virq == dev->remote_irq)
645 dev->local_irq = virq;
649 EXPORT_SYMBOL(vlynq_set_local_irq);
651 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
653 int irq = dev->irq_start + virq;
657 if ((irq < dev->irq_start) || (irq > dev->irq_end))
660 if (virq == dev->local_irq)
663 dev->remote_irq = virq;
667 EXPORT_SYMBOL(vlynq_set_remote_irq);
669 static int vlynq_probe(struct platform_device *pdev)
671 struct vlynq_device *dev;
672 struct resource *regs_res, *mem_res, *irq_res;
675 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
679 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
683 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
687 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
690 "vlynq: failed to allocate device structure\n");
695 dev->dev.bus = &vlynq_bus_type;
696 dev->dev.parent = &pdev->dev;
697 dev_set_name(&dev->dev, "vlynq%d", dev->id);
698 dev->dev.platform_data = pdev->dev.platform_data;
699 dev->dev.release = vlynq_device_release;
701 dev->regs_start = regs_res->start;
702 dev->regs_end = regs_res->end;
703 dev->mem_start = mem_res->start;
704 dev->mem_end = mem_res->end;
706 len = regs_res->end - regs_res->start;
707 if (!request_mem_region(regs_res->start, len, dev_name(&dev->dev))) {
708 printk(KERN_ERR "%s: Can't request vlynq registers\n",
709 dev_name(&dev->dev));
714 dev->local = ioremap(regs_res->start, len);
716 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
717 dev_name(&dev->dev));
722 dev->remote = (struct vlynq_regs *)((void *)dev->local +
723 VLYNQ_REMOTE_OFFSET);
725 dev->irq = platform_get_irq_byname(pdev, "irq");
726 dev->irq_start = irq_res->start;
727 dev->irq_end = irq_res->end;
728 dev->local_irq = dev->irq_end - dev->irq_start;
729 dev->remote_irq = dev->local_irq - 1;
731 if (device_register(&dev->dev))
733 platform_set_drvdata(pdev, dev);
735 printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
736 dev_name(&dev->dev), (void *)dev->regs_start, dev->irq,
737 (void *)dev->mem_start);
740 dev->divisor = vlynq_div_auto;
741 result = __vlynq_enable_device(dev);
743 dev->dev_id = readl(&dev->remote->chip);
744 ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
747 printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
755 release_mem_region(regs_res->start, len);
760 static int vlynq_remove(struct platform_device *pdev)
762 struct vlynq_device *dev = platform_get_drvdata(pdev);
764 device_unregister(&dev->dev);
766 release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
773 static struct platform_driver vlynq_platform_driver = {
774 .driver.name = "vlynq",
775 .probe = vlynq_probe,
776 .remove = __devexit_p(vlynq_remove),
779 struct bus_type vlynq_bus_type = {
781 .match = vlynq_device_match,
782 .probe = vlynq_device_probe,
783 .remove = vlynq_device_remove,
785 EXPORT_SYMBOL(vlynq_bus_type);
787 static int __devinit vlynq_init(void)
791 res = bus_register(&vlynq_bus_type);
795 res = platform_driver_register(&vlynq_platform_driver);
802 bus_unregister(&vlynq_bus_type);
807 static void __devexit vlynq_exit(void)
809 platform_driver_unregister(&vlynq_platform_driver);
810 bus_unregister(&vlynq_bus_type);
813 module_init(vlynq_init);
814 module_exit(vlynq_exit);