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
19 #include <linux/init.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/device.h>
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/platform_device.h>
27 #include <linux/interrupt.h>
28 #include <linux/device.h>
29 #include <linux/delay.h>
32 #include <linux/vlynq.h>
34 #define VLYNQ_CTRL_PM_ENABLE 0x80000000
35 #define VLYNQ_CTRL_CLOCK_INT 0x00008000
36 #define VLYNQ_CTRL_CLOCK_DIV(x) (((x) & 7) << 16)
37 #define VLYNQ_CTRL_INT_LOCAL 0x00004000
38 #define VLYNQ_CTRL_INT_ENABLE 0x00002000
39 #define VLYNQ_CTRL_INT_VECTOR(x) (((x) & 0x1f) << 8)
40 #define VLYNQ_CTRL_INT2CFG 0x00000080
41 #define VLYNQ_CTRL_RESET 0x00000001
43 #define VLYNQ_CTRL_CLOCK_MASK (0x7 << 16)
45 #define VLYNQ_INT_OFFSET 0x00000014
46 #define VLYNQ_REMOTE_OFFSET 0x00000080
48 #define VLYNQ_STATUS_LINK 0x00000001
49 #define VLYNQ_STATUS_LERROR 0x00000080
50 #define VLYNQ_STATUS_RERROR 0x00000100
52 #define VINT_ENABLE 0x00000100
53 #define VINT_TYPE_EDGE 0x00000080
54 #define VINT_LEVEL_LOW 0x00000040
55 #define VINT_VECTOR(x) ((x) & 0x1f)
56 #define VINT_OFFSET(irq) (8 * ((irq) % 4))
58 #define VLYNQ_AUTONEGO_V2 0x00010000
69 struct vlynq_mapping rx_mapping[4];
76 #define vlynq_reg_read(reg) readl(&(reg))
77 #define vlynq_reg_write(reg, val) writel(val, &(reg))
79 static int __vlynq_enable_device(struct vlynq_device *dev);
82 static void vlynq_dump_regs(struct vlynq_device *dev)
85 printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n",
86 dev->local, dev->remote);
87 for (i = 0; i < 32; i++) {
88 printk(KERN_DEBUG "VLYNQ: local %d: %08x\n",
89 i + 1, ((u32 *)dev->local)[i]);
90 printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n",
91 i + 1, ((u32 *)dev->remote)[i]);
95 static void vlynq_dump_mem(u32 *base, int count)
98 for (i = 0; i < (count + 3) / 4; i++) {
99 if (i % 4 == 0) printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4);
100 printk(KERN_DEBUG " 0x%08x", *(base + i));
102 printk(KERN_DEBUG "\n");
106 int vlynq_linked(struct vlynq_device *dev)
110 for (i = 0; i < 100; i++)
111 if (vlynq_reg_read(dev->local->status) & VLYNQ_STATUS_LINK)
119 static void vlynq_reset(struct vlynq_device *dev)
121 vlynq_reg_write(dev->local->control,
122 vlynq_reg_read(dev->local->control) |
125 /* Wait for the devices to finish resetting */
128 /* Remove reset bit */
129 vlynq_reg_write(dev->local->control,
130 vlynq_reg_read(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 = vlynq_reg_read(dev->remote->int_device[virq >> 2]);
146 val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
147 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
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 = vlynq_reg_read(dev->remote->int_device[virq >> 2]);
159 val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
160 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
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 = vlynq_reg_read(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 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
194 static void vlynq_local_ack(unsigned int irq)
196 struct vlynq_device *dev = get_irq_chip_data(irq);
197 u32 status = vlynq_reg_read(dev->local->status);
198 if (printk_ratelimit())
199 printk(KERN_DEBUG "%s: local status: 0x%08x\n",
200 dev->dev.bus_id, status);
201 vlynq_reg_write(dev->local->status, status);
204 static void vlynq_remote_ack(unsigned int irq)
206 struct vlynq_device *dev = get_irq_chip_data(irq);
207 u32 status = vlynq_reg_read(dev->remote->status);
208 if (printk_ratelimit())
209 printk(KERN_DEBUG "%s: remote status: 0x%08x\n",
210 dev->dev.bus_id, status);
211 vlynq_reg_write(dev->remote->status, status);
214 static irqreturn_t vlynq_irq(int irq, void *dev_id)
216 struct vlynq_device *dev = dev_id;
220 status = vlynq_reg_read(dev->local->int_status);
221 vlynq_reg_write(dev->local->int_status, status);
223 if (unlikely(!status))
224 spurious_interrupt();
228 do_IRQ(dev->irq_start + virq);
236 static struct irq_chip vlynq_irq_chip = {
238 .unmask = vlynq_irq_unmask,
239 .mask = vlynq_irq_mask,
240 .set_type = vlynq_irq_type,
243 static struct irq_chip vlynq_local_chip = {
244 .name = "vlynq local error",
245 .unmask = vlynq_irq_unmask,
246 .mask = vlynq_irq_mask,
247 .ack = vlynq_local_ack,
250 static struct irq_chip vlynq_remote_chip = {
251 .name = "vlynq local error",
252 .unmask = vlynq_irq_unmask,
253 .mask = vlynq_irq_mask,
254 .ack = vlynq_remote_ack,
257 static int vlynq_setup_irq(struct vlynq_device *dev)
262 if (dev->local_irq == dev->remote_irq) {
264 "%s: local vlynq irq should be different from remote\n",
269 /* Clear local and remote error bits */
270 vlynq_reg_write(dev->local->status, vlynq_reg_read(dev->local->status));
271 vlynq_reg_write(dev->remote->status,
272 vlynq_reg_read(dev->remote->status));
274 /* Now setup interrupts */
275 val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
276 val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
278 val |= vlynq_reg_read(dev->local->control);
279 vlynq_reg_write(dev->local->int_ptr, VLYNQ_INT_OFFSET);
280 vlynq_reg_write(dev->local->control, val);
282 val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
283 val |= VLYNQ_CTRL_INT_ENABLE;
284 val |= vlynq_reg_read(dev->remote->control);
285 vlynq_reg_write(dev->remote->int_ptr, VLYNQ_INT_OFFSET);
286 vlynq_reg_write(dev->remote->control, val);
288 for (i = dev->irq_start; i <= dev->irq_end; i++) {
289 virq = i - dev->irq_start;
290 if (virq == dev->local_irq) {
291 set_irq_chip_and_handler(i, &vlynq_local_chip,
293 set_irq_chip_data(i, dev);
294 } else if (virq == dev->remote_irq) {
295 set_irq_chip_and_handler(i, &vlynq_remote_chip,
297 set_irq_chip_data(i, dev);
299 set_irq_chip_and_handler(i, &vlynq_irq_chip,
301 set_irq_chip_data(i, dev);
302 vlynq_reg_write(dev->remote->int_device[virq >> 2], 0);
306 if (request_irq(dev->irq, vlynq_irq, IRQF_SHARED, "vlynq", dev)) {
307 printk(KERN_ERR "%s: request_irq failed\n", dev->dev.bus_id);
314 static void vlynq_device_release(struct device *dev)
316 struct vlynq_device *vdev = to_vlynq_device(dev);
320 static int vlynq_device_match(struct device *dev,
321 struct device_driver *drv)
323 struct vlynq_device *vdev = to_vlynq_device(dev);
324 struct vlynq_driver *vdrv = to_vlynq_driver(drv);
325 struct vlynq_device_id *ids = vdrv->id_table;
328 if (ids->id == vdev->dev_id) {
329 vdev->divisor = ids->divisor;
330 vlynq_set_drvdata(vdev, ids);
331 printk(KERN_INFO "Driver found for VLYNQ " \
332 "device: %08x\n", vdev->dev_id);
335 printk(KERN_DEBUG "Not using the %08x VLYNQ device's driver" \
336 " for VLYNQ device: %08x\n", ids->id, vdev->dev_id);
342 static int vlynq_device_probe(struct device *dev)
344 struct vlynq_device *vdev = to_vlynq_device(dev);
345 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
346 struct vlynq_device_id *id = vlynq_get_drvdata(vdev);
347 int result = -ENODEV;
350 if (drv && drv->probe)
351 result = drv->probe(vdev, id);
357 static int vlynq_device_remove(struct device *dev)
359 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
360 if (drv && drv->remove)
361 drv->remove(to_vlynq_device(dev));
366 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
368 driver->driver.name = driver->name;
369 driver->driver.bus = &vlynq_bus_type;
370 return driver_register(&driver->driver);
372 EXPORT_SYMBOL(__vlynq_register_driver);
374 void vlynq_unregister_driver(struct vlynq_driver *driver)
376 driver_unregister(&driver->driver);
378 EXPORT_SYMBOL(vlynq_unregister_driver);
380 static int __vlynq_try_remote(struct vlynq_device *dev)
385 for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
386 i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
387 dev->dev_id ? i++ : i--) {
389 if (!vlynq_linked(dev))
392 vlynq_reg_write(dev->remote->control,
393 (vlynq_reg_read(dev->remote->control) &
394 ~VLYNQ_CTRL_CLOCK_MASK) |
395 VLYNQ_CTRL_CLOCK_INT |
396 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1));
397 vlynq_reg_write(dev->local->control,
398 ((vlynq_reg_read(dev->local->control)
399 & ~(VLYNQ_CTRL_CLOCK_INT |
400 VLYNQ_CTRL_CLOCK_MASK)) |
401 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1)));
403 if (vlynq_linked(dev)) {
405 "%s: using remote clock divisor %d\n",
406 dev->dev.bus_id, i - vlynq_rdiv1 + 1);
417 static int __vlynq_try_local(struct vlynq_device *dev)
423 for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
424 i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
425 dev->dev_id ? i++ : i--) {
427 vlynq_reg_write(dev->local->control,
428 (vlynq_reg_read(dev->local->control) &
429 ~VLYNQ_CTRL_CLOCK_MASK) |
430 VLYNQ_CTRL_CLOCK_INT |
431 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1));
433 if (vlynq_linked(dev)) {
435 "%s: using local clock divisor %d\n",
436 dev->dev.bus_id, i - vlynq_ldiv1 + 1);
447 static int __vlynq_try_external(struct vlynq_device *dev)
450 if (!vlynq_linked(dev))
453 vlynq_reg_write(dev->remote->control,
454 (vlynq_reg_read(dev->remote->control) &
455 ~VLYNQ_CTRL_CLOCK_INT));
457 vlynq_reg_write(dev->local->control,
458 (vlynq_reg_read(dev->local->control) &
459 ~VLYNQ_CTRL_CLOCK_INT));
461 if (vlynq_linked(dev)) {
462 printk(KERN_DEBUG "%s: using external clock\n",
464 dev->divisor = vlynq_div_external;
471 static int __vlynq_enable_device(struct vlynq_device *dev)
474 struct plat_vlynq_ops *ops = dev->dev.platform_data;
476 result = ops->on(dev);
480 switch (dev->divisor) {
481 case vlynq_div_external:
483 /* When the device is brought from reset it should have clock
484 generation negotiated by hardware.
485 Check which device is generating clocks and perform setup
487 if (vlynq_linked(dev) && vlynq_reg_read(dev->remote->control) &
488 VLYNQ_CTRL_CLOCK_INT) {
489 if (!__vlynq_try_remote(dev) ||
490 !__vlynq_try_local(dev) ||
491 !__vlynq_try_external(dev))
494 if (!__vlynq_try_external(dev) ||
495 !__vlynq_try_local(dev) ||
496 !__vlynq_try_remote(dev))
500 case vlynq_ldiv1: case vlynq_ldiv2: case vlynq_ldiv3: case vlynq_ldiv4:
501 case vlynq_ldiv5: case vlynq_ldiv6: case vlynq_ldiv7: case vlynq_ldiv8:
502 vlynq_reg_write(dev->local->control,
503 VLYNQ_CTRL_CLOCK_INT |
504 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
506 vlynq_reg_write(dev->remote->control, 0);
507 if (vlynq_linked(dev)) {
509 "%s: using local clock divisor %d\n",
510 dev->dev.bus_id, dev->divisor - vlynq_ldiv1 + 1);
514 case vlynq_rdiv1: case vlynq_rdiv2: case vlynq_rdiv3: case vlynq_rdiv4:
515 case vlynq_rdiv5: case vlynq_rdiv6: case vlynq_rdiv7: case vlynq_rdiv8:
516 vlynq_reg_write(dev->local->control, 0);
517 vlynq_reg_write(dev->remote->control,
518 VLYNQ_CTRL_CLOCK_INT |
519 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
521 if (vlynq_linked(dev)) {
523 "%s: using remote clock divisor %d\n",
524 dev->dev.bus_id, dev->divisor - vlynq_rdiv1 + 1);
534 int vlynq_enable_device(struct vlynq_device *dev)
536 struct plat_vlynq_ops *ops = dev->dev.platform_data;
537 int result = -ENODEV;
539 result = __vlynq_enable_device(dev);
543 result = vlynq_setup_irq(dev);
547 dev->enabled = !result;
550 EXPORT_SYMBOL(vlynq_enable_device);
553 void vlynq_disable_device(struct vlynq_device *dev)
555 struct plat_vlynq_ops *ops = dev->dev.platform_data;
558 free_irq(dev->irq, dev);
561 EXPORT_SYMBOL(vlynq_disable_device);
563 int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
564 struct vlynq_mapping *mapping)
571 vlynq_reg_write(dev->local->tx_offset, tx_offset);
572 for (i = 0; i < 4; i++) {
573 vlynq_reg_write(dev->local->rx_mapping[i].offset,
575 vlynq_reg_write(dev->local->rx_mapping[i].size,
580 EXPORT_SYMBOL(vlynq_set_local_mapping);
582 int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
583 struct vlynq_mapping *mapping)
590 vlynq_reg_write(dev->remote->tx_offset, tx_offset);
591 for (i = 0; i < 4; i++) {
592 vlynq_reg_write(dev->remote->rx_mapping[i].offset,
594 vlynq_reg_write(dev->remote->rx_mapping[i].size,
599 EXPORT_SYMBOL(vlynq_set_remote_mapping);
601 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
603 int irq = dev->irq_start + virq;
607 if ((irq < dev->irq_start) || (irq > dev->irq_end))
610 if (virq == dev->remote_irq)
613 dev->local_irq = virq;
617 EXPORT_SYMBOL(vlynq_set_local_irq);
619 int vlynq_set_remote_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->local_irq)
631 dev->remote_irq = virq;
635 EXPORT_SYMBOL(vlynq_set_remote_irq);
637 static int vlynq_probe(struct platform_device *pdev)
639 struct vlynq_device *dev;
640 struct resource *regs_res, *mem_res, *irq_res;
643 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
647 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
651 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
655 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
658 "vlynq: failed to allocate device structure\n");
663 dev->dev.bus = &vlynq_bus_type;
664 dev->dev.parent = &pdev->dev;
665 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "vlynq%d", dev->id);
666 dev->dev.bus_id[BUS_ID_SIZE - 1] = 0;
667 dev->dev.platform_data = pdev->dev.platform_data;
668 dev->dev.release = vlynq_device_release;
670 dev->regs_start = regs_res->start;
671 dev->regs_end = regs_res->end;
672 dev->mem_start = mem_res->start;
673 dev->mem_end = mem_res->end;
675 len = regs_res->end - regs_res->start;
676 if (!request_mem_region(regs_res->start, len, dev->dev.bus_id)) {
677 printk(KERN_ERR "%s: Can't request vlynq registers\n",
683 dev->local = ioremap(regs_res->start, len);
685 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
691 dev->remote = (struct vlynq_regs *)((void *)dev->local +
692 VLYNQ_REMOTE_OFFSET);
694 dev->irq = platform_get_irq_byname(pdev, "irq");
695 dev->irq_start = irq_res->start;
696 dev->irq_end = irq_res->end;
697 dev->local_irq = dev->irq_end - dev->irq_start;
698 dev->remote_irq = dev->local_irq - 1;
700 if (device_register(&dev->dev))
702 platform_set_drvdata(pdev, dev);
704 printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
705 dev->dev.bus_id, (void *)dev->regs_start, dev->irq,
706 (void *)dev->mem_start);
709 dev->divisor = vlynq_div_auto;
710 result = __vlynq_enable_device(dev);
712 dev->dev_id = vlynq_reg_read(dev->remote->chip);
713 ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
716 printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
724 release_mem_region(regs_res->start, len);
729 static int vlynq_remove(struct platform_device *pdev)
731 struct vlynq_device *dev = platform_get_drvdata(pdev);
733 device_unregister(&dev->dev);
735 release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
742 static struct platform_driver vlynq_platform_driver = {
743 .driver.name = "vlynq",
744 .probe = vlynq_probe,
745 .remove = __devexit_p(vlynq_remove),
748 struct bus_type vlynq_bus_type = {
750 .match = vlynq_device_match,
751 .probe = vlynq_device_probe,
752 .remove = vlynq_device_remove,
754 EXPORT_SYMBOL(vlynq_bus_type);
756 static int __devinit vlynq_init(void)
760 res = bus_register(&vlynq_bus_type);
764 res = platform_driver_register(&vlynq_platform_driver);
771 bus_unregister(&vlynq_bus_type);
776 static void __devexit vlynq_exit(void)
778 platform_driver_unregister(&vlynq_platform_driver);
779 bus_unregister(&vlynq_bus_type);
782 module_init(vlynq_init);
783 module_exit(vlynq_exit);