2 * Copyright (C) 2006, 2007 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/errno.h>
25 #include <linux/platform_device.h>
26 #include <linux/interrupt.h>
27 #include <linux/device.h>
30 #include <asm/ar7/vlynq.h>
32 #define PER_DEVICE_IRQS 32
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_INT_OFFSET 0x00000014
44 #define VLYNQ_REMOTE_OFFSET 0x00000080
46 #define VLYNQ_STATUS_LINK 0x00000001
47 #define VLYNQ_STATUS_LERROR 0x00000080
48 #define VLYNQ_STATUS_RERROR 0x00000100
50 #define VINT_ENABLE 0x00000100
51 #define VINT_TYPE_EDGE 0x00000080
52 #define VINT_LEVEL_LOW 0x00000040
53 #define VINT_VECTOR(x) ((x) & 0x1f)
54 #define VINT_OFFSET(irq) (8 * ((irq) % 4))
56 #define VLYNQ_AUTONEGO_V2 0x00010000
67 struct vlynq_mapping rx_mapping[4];
72 } __attribute__ ((packed));
74 #define vlynq_reg_read(reg) readl(&(reg))
75 #define vlynq_reg_write(reg, val) writel(val, &(reg))
78 static void vlynq_dump_regs(struct vlynq_device *dev)
81 printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n",
82 dev->local, dev->remote);
83 for (i = 0; i < 32; i++) {
84 printk(KERN_DEBUG "VLYNQ: local %d: %08x\n",
85 i + 1, ((u32 *)dev->local)[i]);
86 printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n",
87 i + 1, ((u32 *)dev->remote)[i]);
91 static void vlynq_dump_mem(u32 *base, int count)
94 for (i = 0; i < (count + 3) / 4; i++) {
95 if (i % 4 == 0) printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4);
96 printk(KERN_DEBUG " 0x%08x", *(base + i));
98 printk(KERN_DEBUG "\n");
102 int vlynq_linked(struct vlynq_device *dev)
106 for (i = 0; i < 100; i++)
107 if (vlynq_reg_read(dev->local->status) & VLYNQ_STATUS_LINK)
115 static void vlynq_irq_unmask(unsigned int irq)
118 struct vlynq_device *dev = get_irq_chip_data(irq);
122 virq = irq - dev->irq_start;
123 val = vlynq_reg_read(dev->remote->int_device[virq >> 2]);
124 val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
125 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
128 static void vlynq_irq_mask(unsigned int irq)
131 struct vlynq_device *dev = get_irq_chip_data(irq);
135 virq = irq - dev->irq_start;
136 val = vlynq_reg_read(dev->remote->int_device[virq >> 2]);
137 val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
138 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
141 static int vlynq_irq_type(unsigned int irq, unsigned int flow_type)
144 struct vlynq_device *dev = get_irq_chip_data(irq);
148 virq = irq - dev->irq_start;
149 val = vlynq_reg_read(dev->remote->int_device[virq >> 2]);
150 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
151 case IRQ_TYPE_EDGE_RISING:
152 case IRQ_TYPE_EDGE_FALLING:
153 case IRQ_TYPE_EDGE_BOTH:
154 val |= VINT_TYPE_EDGE << VINT_OFFSET(virq);
155 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
157 case IRQ_TYPE_LEVEL_HIGH:
158 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
159 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
161 case IRQ_TYPE_LEVEL_LOW:
162 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
163 val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
168 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
172 static void vlynq_local_ack(unsigned int irq)
174 struct vlynq_device *dev = get_irq_chip_data(irq);
175 u32 status = vlynq_reg_read(dev->local->status);
176 if (printk_ratelimit())
177 printk(KERN_DEBUG "%s: local status: 0x%08x\n",
178 dev->dev.bus_id, status);
179 vlynq_reg_write(dev->local->status, status);
182 static void vlynq_remote_ack(unsigned int irq)
184 struct vlynq_device *dev = get_irq_chip_data(irq);
185 u32 status = vlynq_reg_read(dev->remote->status);
186 if (printk_ratelimit())
187 printk(KERN_DEBUG "%s: remote status: 0x%08x\n",
188 dev->dev.bus_id, status);
189 vlynq_reg_write(dev->remote->status, status);
192 #warning FIXME: process one irq per call
193 static irqreturn_t vlynq_irq(int irq, void *dev_id)
195 struct vlynq_device *dev = dev_id;
199 status = vlynq_reg_read(dev->local->int_status);
200 vlynq_reg_write(dev->local->int_status, status);
202 if (unlikely(!status))
203 spurious_interrupt();
207 do_IRQ(dev->irq_start + virq);
215 static struct irq_chip vlynq_irq_chip = {
217 .unmask = vlynq_irq_unmask,
218 .mask = vlynq_irq_mask,
219 .set_type = vlynq_irq_type,
222 static struct irq_chip vlynq_local_chip = {
223 .name = "vlynq local error",
224 .unmask = vlynq_irq_unmask,
225 .mask = vlynq_irq_mask,
226 .ack = vlynq_local_ack,
229 static struct irq_chip vlynq_remote_chip = {
230 .name = "vlynq local error",
231 .unmask = vlynq_irq_unmask,
232 .mask = vlynq_irq_mask,
233 .ack = vlynq_remote_ack,
236 static int vlynq_setup_irq(struct vlynq_device *dev)
241 if (dev->local_irq == dev->remote_irq) {
243 "%s: local vlynq irq should be different from remote\n",
248 /* Clear local and remote error bits */
249 vlynq_reg_write(dev->local->status, vlynq_reg_read(dev->local->status));
250 vlynq_reg_write(dev->remote->status,
251 vlynq_reg_read(dev->remote->status));
253 /* Now setup interrupts */
254 val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
255 val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
257 val |= vlynq_reg_read(dev->local->control);
258 vlynq_reg_write(dev->local->int_ptr, VLYNQ_INT_OFFSET);
259 vlynq_reg_write(dev->local->control, val);
261 val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
262 val |= VLYNQ_CTRL_INT_ENABLE;
263 val |= vlynq_reg_read(dev->remote->control);
264 vlynq_reg_write(dev->remote->int_ptr, VLYNQ_INT_OFFSET);
265 vlynq_reg_write(dev->remote->control, val);
267 for (i = 0; i < PER_DEVICE_IRQS; i++) {
268 if (i == dev->local_irq) {
269 set_irq_chip_and_handler(dev->irq_start + i,
272 set_irq_chip_data(dev->irq_start + i, dev);
273 } else if (i == dev->remote_irq) {
274 set_irq_chip_and_handler(dev->irq_start + i,
277 set_irq_chip_data(dev->irq_start + i, dev);
279 set_irq_chip(dev->irq_start + i, &vlynq_irq_chip);
280 set_irq_chip_data(dev->irq_start + i, dev);
281 vlynq_reg_write(dev->remote->int_device[i >> 2], 0);
285 if (request_irq(dev->irq, vlynq_irq, IRQF_SHARED, "vlynq", dev)) {
286 printk(KERN_ERR "%s: request_irq failed\n", dev->dev.bus_id);
293 static void vlynq_free_irq(struct vlynq_device *dev)
295 free_irq(dev->irq, dev);
298 static void vlynq_device_release(struct device *dev)
300 struct vlynq_device *vdev = to_vlynq_device(dev);
304 static int vlynq_device_probe(struct device *dev)
306 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
308 return drv->probe(to_vlynq_device(dev));
312 static int vlynq_device_remove(struct device *dev)
314 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
316 return drv->remove(to_vlynq_device(dev));
320 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
322 driver->driver.name = driver->name;
323 driver->driver.bus = &vlynq_bus_type;
324 return driver_register(&driver->driver);
326 EXPORT_SYMBOL(__vlynq_register_driver);
328 void vlynq_unregister_driver(struct vlynq_driver *driver)
330 driver_unregister(&driver->driver);
332 EXPORT_SYMBOL(vlynq_unregister_driver);
334 int vlynq_device_enable(struct vlynq_device *dev)
337 struct plat_vlynq_ops *ops = dev->dev.platform_data;
339 result = ops->on(dev);
343 switch (dev->divisor) {
345 /* First try locally supplied clock */
346 vlynq_reg_write(dev->remote->control, 0);
347 for (i = vlynq_ldiv1; i <= vlynq_ldiv8; i++) {
348 vlynq_reg_write(dev->local->control,
349 VLYNQ_CTRL_CLOCK_INT |
350 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1));
351 if (vlynq_linked(dev)) {
353 "%s: using local clock divisor %d\n",
354 dev->dev.bus_id, i - vlynq_ldiv1 + 1);
355 return vlynq_setup_irq(dev);
358 /* Then remotely supplied clock */
359 vlynq_reg_write(dev->local->control, 0);
360 for (i = vlynq_rdiv1; i <= vlynq_rdiv8; i++) {
361 vlynq_reg_write(dev->remote->control,
362 VLYNQ_CTRL_CLOCK_INT |
363 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1));
364 if (vlynq_linked(dev)) {
366 "%s: using remote clock divisor %d\n",
367 dev->dev.bus_id, i - vlynq_rdiv1 + 1);
368 return vlynq_setup_irq(dev);
371 /* At last, externally supplied clock */
372 vlynq_reg_write(dev->remote->control, 0);
373 if (vlynq_linked(dev)) {
374 printk(KERN_DEBUG "%s: using external clock\n",
376 return vlynq_setup_irq(dev);
379 case vlynq_ldiv1: case vlynq_ldiv2: case vlynq_ldiv3: case vlynq_ldiv4:
380 case vlynq_ldiv5: case vlynq_ldiv6: case vlynq_ldiv7: case vlynq_ldiv8:
381 vlynq_reg_write(dev->remote->control, 0);
382 vlynq_reg_write(dev->local->control,
383 VLYNQ_CTRL_CLOCK_INT |
384 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
386 if (vlynq_linked(dev)) {
388 "%s: using local clock divisor %d\n",
389 dev->dev.bus_id, dev->divisor - vlynq_ldiv1 + 1);
390 return vlynq_setup_irq(dev);
393 case vlynq_rdiv1: case vlynq_rdiv2: case vlynq_rdiv3: case vlynq_rdiv4:
394 case vlynq_rdiv5: case vlynq_rdiv6: case vlynq_rdiv7: case vlynq_rdiv8:
395 vlynq_reg_write(dev->local->control, 0);
396 vlynq_reg_write(dev->remote->control,
397 VLYNQ_CTRL_CLOCK_INT |
398 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
400 if (vlynq_linked(dev)) {
402 "%s: using remote clock divisor %d\n",
403 dev->dev.bus_id, dev->divisor - vlynq_rdiv1 + 1);
404 return vlynq_setup_irq(dev);
407 case vlynq_div_external:
408 vlynq_reg_write(dev->local->control, 0);
409 vlynq_reg_write(dev->remote->control, 0);
410 if (vlynq_linked(dev)) {
411 printk(KERN_DEBUG "%s: using external clock\n",
413 return vlynq_setup_irq(dev);
421 void vlynq_device_disable(struct vlynq_device *dev)
423 struct plat_vlynq_ops *ops = dev->dev.platform_data;
429 u32 vlynq_remote_id(struct vlynq_device *dev)
431 return vlynq_reg_read(dev->remote->chip);
434 void vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
435 struct vlynq_mapping *mapping)
439 vlynq_reg_write(dev->local->tx_offset, tx_offset);
440 for (i = 0; i < 4; i++) {
441 vlynq_reg_write(dev->local->rx_mapping[i].offset, mapping[i].offset);
442 vlynq_reg_write(dev->local->rx_mapping[i].size, mapping[i].size);
446 void vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
447 struct vlynq_mapping *mapping)
451 vlynq_reg_write(dev->remote->tx_offset, tx_offset);
452 for (i = 0; i < 4; i++) {
453 vlynq_reg_write(dev->remote->rx_mapping[i].offset, mapping[i].offset);
454 vlynq_reg_write(dev->remote->rx_mapping[i].size, mapping[i].size);
458 int vlynq_virq_to_irq(struct vlynq_device *dev, int virq)
460 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
463 return dev->irq_start + virq;
466 int vlynq_irq_to_virq(struct vlynq_device *dev, int irq)
468 if ((irq < dev->irq_start) || (irq >= dev->irq_start + PER_DEVICE_IRQS))
471 return irq - dev->irq_start;
474 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
476 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
479 if (virq == dev->remote_irq)
482 dev->local_irq = virq;
487 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
489 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
492 if (virq == dev->local_irq)
495 dev->remote_irq = virq;
500 static int vlynq_probe(struct platform_device *pdev)
502 struct vlynq_device *dev;
503 struct resource *regs_res, *mem_res, *irq_res;
506 if (strcmp(pdev->name, "vlynq"))
509 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
513 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
517 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
521 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
524 "vlynq: failed to allocate device structure\n");
529 dev->dev.bus = &vlynq_bus_type;
530 dev->dev.parent = &pdev->dev;
531 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "vlynq%d", dev->id);
532 dev->dev.bus_id[BUS_ID_SIZE - 1] = 0;
533 dev->dev.platform_data = pdev->dev.platform_data;
534 dev->dev.release = vlynq_device_release;
536 dev->regs_start = regs_res->start;
537 dev->regs_end = regs_res->end;
538 dev->mem_start = mem_res->start;
539 dev->mem_end = mem_res->end;
541 len = regs_res->end - regs_res->start;
542 if (!request_mem_region(regs_res->start, len, dev->dev.bus_id)) {
543 printk(KERN_ERR "%s: Can't request vlynq registers\n",
549 dev->local = ioremap(regs_res->start, len);
551 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
557 dev->remote = (struct vlynq_regs *)((u32)dev->local +
558 VLYNQ_REMOTE_OFFSET);
560 dev->irq = platform_get_irq_byname(pdev, "irq");
561 dev->irq_start = irq_res->start;
562 dev->irq_end = irq_res->end;
564 dev->remote_irq = 30;
566 if (device_register(&dev->dev))
568 platform_set_drvdata(pdev, dev);
570 printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
571 dev->dev.bus_id, (void *)dev->regs_start, dev->irq,
572 (void *)dev->mem_start);
580 release_mem_region(regs_res->start, len);
585 static int vlynq_remove(struct platform_device *pdev)
587 struct vlynq_device *dev = platform_get_drvdata(pdev);
589 device_unregister(&dev->dev);
591 release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
598 static struct platform_driver vlynq_driver = {
599 .driver.name = "vlynq",
600 .probe = vlynq_probe,
601 .remove = vlynq_remove,
604 struct bus_type vlynq_bus_type = {
606 .probe = vlynq_device_probe,
607 .remove = vlynq_device_remove,
609 EXPORT_SYMBOL(vlynq_bus_type);
612 extern void vlynq_pci_init(void);
614 int __init vlynq_init(void)
618 res = bus_register(&vlynq_bus_type);
622 res = platform_driver_register(&vlynq_driver);
633 bus_unregister(&vlynq_bus_type);
638 /* Add this back when vlynq-pci crap is gone */
640 void __devexit vlynq_exit(void)
642 platform_driver_unregister(&vlynq_driver);
643 bus_unregister(&vlynq_bus_type);
647 subsys_initcall(vlynq_init);