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/delay.h>
24 #include <linux/device.h>
25 #include <linux/ioport.h>
26 #include <linux/errno.h>
27 #include <linux/platform_device.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/device.h>
33 #include <asm/addrspace.h>
34 #include <asm/ar7/ar7.h>
35 #include <asm/ar7/vlynq.h>
37 #define PER_DEVICE_IRQS 32
39 #define VLYNQ_CTRL_PM_ENABLE 0x80000000
40 #define VLYNQ_CTRL_CLOCK_INT 0x00008000
41 #define VLYNQ_CTRL_CLOCK_DIV(x) (((x) & 7) << 16)
42 #define VLYNQ_CTRL_INT_LOCAL 0x00004000
43 #define VLYNQ_CTRL_INT_ENABLE 0x00002000
44 #define VLYNQ_CTRL_INT_VECTOR(x) (((x) & 0x1f) << 8)
45 #define VLYNQ_CTRL_INT2CFG 0x00000080
46 #define VLYNQ_CTRL_RESET 0x00000001
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];
74 } __attribute__ ((packed));
76 #define vlynq_reg_read(reg) readl(&(reg))
77 #define vlynq_reg_write(reg, val) writel(val, &(reg))
80 static void vlynq_dump_regs(struct vlynq_device *dev)
83 printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n",
84 dev->local, dev->remote);
85 for (i = 0; i < 32; i++) {
86 printk(KERN_DEBUG "VLYNQ: local %d: %08x\n",
87 i + 1, ((u32 *)dev->local)[i]);
88 printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n",
89 i + 1, ((u32 *)dev->remote)[i]);
93 static void vlynq_dump_mem(u32 *base, int count)
96 for (i = 0; i < (count + 3) / 4; i++) {
97 if (i % 4 == 0) printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4);
98 printk(KERN_DEBUG " 0x%08x", *(base + i));
100 printk(KERN_DEBUG "\n");
104 int vlynq_linked(struct vlynq_device *dev)
108 for (i = 0; i < 10; i++)
109 if (vlynq_reg_read(dev->local->status) & VLYNQ_STATUS_LINK)
117 static void vlynq_irq_unmask(unsigned int irq)
120 struct vlynq_device *dev = get_irq_chip_data(irq);
124 virq = irq - dev->irq_start;
125 val = vlynq_reg_read(dev->remote->int_device[virq >> 2]);
126 val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
127 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
130 static void vlynq_irq_mask(unsigned int irq)
133 struct vlynq_device *dev = get_irq_chip_data(irq);
137 virq = irq - dev->irq_start;
138 val = vlynq_reg_read(dev->remote->int_device[virq >> 2]);
139 val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
140 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
143 static int vlynq_irq_type(unsigned int irq, unsigned int flow_type)
146 struct vlynq_device *dev = irq_desc[irq].chip_data;
150 virq = irq - dev->irq_start;
151 val = vlynq_reg_read(dev->remote->int_device[virq >> 2]);
152 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
153 case IRQ_TYPE_EDGE_RISING:
154 case IRQ_TYPE_EDGE_FALLING:
155 case IRQ_TYPE_EDGE_BOTH:
156 val |= VINT_TYPE_EDGE << VINT_OFFSET(virq);
157 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
159 case IRQ_TYPE_LEVEL_HIGH:
160 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
161 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
163 case IRQ_TYPE_LEVEL_LOW:
164 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
165 val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
170 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
174 static irqreturn_t vlynq_irq(int irq, void *dev_id)
176 struct vlynq_device *dev = dev_id;
180 status = vlynq_reg_read(dev->local->int_status);
181 vlynq_reg_write(dev->local->int_status, status);
183 if (status & (1 << dev->local_irq)) { /* Local vlynq IRQ. Ack */
184 ack = vlynq_reg_read(dev->local->status);
185 vlynq_reg_write(dev->local->status, ack);
188 if (status & (1 << dev->remote_irq)) { /* Remote vlynq IRQ. Ack */
189 ack = vlynq_reg_read(dev->remote->status);
190 vlynq_reg_write(dev->remote->status, ack);
193 status &= ~((1 << dev->local_irq) | (1 << dev->remote_irq));
195 if (status & 1) /* Remote device IRQ. Pass. */
196 do_IRQ(dev->irq_start + virq);
204 static struct irq_chip vlynq_irq_chip = {
206 .unmask = vlynq_irq_unmask,
207 .mask = vlynq_irq_mask,
208 .set_type = vlynq_irq_type,
211 static int vlynq_setup_irq(struct vlynq_device *dev)
216 if (dev->local_irq == dev->remote_irq) {
218 "%s: local vlynq irq should be different from remote\n",
223 val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
224 val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
226 val |= vlynq_reg_read(dev->local->control);
227 vlynq_reg_write(dev->local->int_ptr, 0x14);
228 vlynq_reg_write(dev->local->control, val);
230 val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
231 val |= VLYNQ_CTRL_INT_ENABLE;
232 val |= vlynq_reg_read(dev->remote->control);
233 vlynq_reg_write(dev->remote->int_ptr, 0x14);
234 vlynq_reg_write(dev->remote->control, val);
236 for (i = 0; i < PER_DEVICE_IRQS; i++) {
237 if ((i == dev->local_irq) || (i == dev->remote_irq))
239 set_irq_chip(dev->irq_start + i, &vlynq_irq_chip);
240 set_irq_chip_data(dev->irq_start + i, dev);
241 vlynq_reg_write(dev->remote->int_device[i >> 2], 0);
244 if (request_irq(dev->irq, vlynq_irq, SA_SHIRQ, "vlynq", dev)) {
245 printk(KERN_ERR "%s: request_irq failed\n", dev->dev.bus_id);
252 static void vlynq_free_irq(struct vlynq_device *dev)
254 free_irq(dev->irq, dev);
257 static void vlynq_device_release(struct device *dev)
259 struct vlynq_device *vdev = to_vlynq_device(dev);
263 static int vlynq_device_probe(struct device *dev)
265 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
267 return drv->probe(to_vlynq_device(dev));
271 static int vlynq_device_remove(struct device *dev)
273 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
275 return drv->remove(to_vlynq_device(dev));
279 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
281 driver->driver.name = driver->name;
282 driver->driver.bus = &vlynq_bus_type;
283 /* driver->driver.owner = owner;*/
284 return driver_register(&driver->driver);
286 EXPORT_SYMBOL(__vlynq_register_driver);
288 void vlynq_unregister_driver(struct vlynq_driver *driver)
290 driver_unregister(&driver->driver);
292 EXPORT_SYMBOL(vlynq_unregister_driver);
294 int vlynq_device_enable(struct vlynq_device *dev)
298 struct plat_vlynq_ops *ops = dev->dev.platform_data;
300 result = ops->on(dev);
304 vlynq_reg_write(dev->local->control, 0);
305 vlynq_reg_write(dev->remote->control, 0);
308 if (vlynq_linked(dev)) {
309 printk(KERN_INFO "%s: linked (using external clock)\n",
311 return vlynq_setup_irq(dev);
315 for (div = 1; div <= 8; div++) {
317 vlynq_reg_write(dev->local->control, VLYNQ_CTRL_CLOCK_INT |
318 VLYNQ_CTRL_CLOCK_DIV(div - 1));
319 vlynq_reg_write(dev->remote->control, 0);
320 if (vlynq_linked(dev)) {
321 printk(KERN_INFO "%s: linked (using internal clock, div: %d)\n",
322 dev->dev.bus_id, div);
323 return vlynq_setup_irq(dev);
330 void vlynq_device_disable(struct vlynq_device *dev)
332 struct plat_vlynq_ops *ops = dev->dev.platform_data;
338 u32 vlynq_remote_id(struct vlynq_device *dev)
340 return vlynq_reg_read(dev->remote->chip);
343 void vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
344 struct vlynq_mapping *mapping)
348 vlynq_reg_write(dev->local->tx_offset, tx_offset);
349 for (i = 0; i < 4; i++) {
350 vlynq_reg_write(dev->local->rx_mapping[i].offset, mapping[i].offset);
351 vlynq_reg_write(dev->local->rx_mapping[i].size, mapping[i].size);
355 void vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
356 struct vlynq_mapping *mapping)
360 vlynq_reg_write(dev->remote->tx_offset, tx_offset);
361 for (i = 0; i < 4; i++) {
362 vlynq_reg_write(dev->remote->rx_mapping[i].offset, mapping[i].offset);
363 vlynq_reg_write(dev->remote->rx_mapping[i].size, mapping[i].size);
367 int vlynq_virq_to_irq(struct vlynq_device *dev, int virq)
369 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
372 if ((virq == dev->local_irq) || (virq == dev->remote_irq))
375 return dev->irq_start + virq;
378 int vlynq_irq_to_virq(struct vlynq_device *dev, int irq)
380 if ((irq < dev->irq_start) || (irq >= dev->irq_start + PER_DEVICE_IRQS))
383 return irq - dev->irq_start;
386 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
388 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
391 if (virq == dev->remote_irq)
394 dev->local_irq = virq;
399 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
401 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
404 if (virq == dev->local_irq)
407 dev->remote_irq = virq;
412 static int vlynq_probe(struct platform_device *pdev)
414 struct vlynq_device *dev;
415 struct resource *regs_res, *mem_res, *irq_res;
418 if (strcmp(pdev->name, "vlynq"))
421 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
425 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
429 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
433 dev = kzalloc(sizeof(struct vlynq_device), GFP_KERNEL);
435 printk(KERN_ERR "vlynq: failed to allocate device structure\n");
440 dev->dev.bus = &vlynq_bus_type;
441 dev->dev.parent = &pdev->dev;
442 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "vlynq%d", dev->id);
443 dev->dev.bus_id[BUS_ID_SIZE - 1] = 0;
444 dev->dev.platform_data = pdev->dev.platform_data;
445 dev->dev.release = vlynq_device_release;
447 dev->regs_start = regs_res->start;
448 dev->regs_end = regs_res->end;
449 dev->mem_start = mem_res->start;
450 dev->mem_end = mem_res->end;
452 len = regs_res->end - regs_res->start;
453 if (!request_mem_region(regs_res->start, len, dev->dev.bus_id)) {
454 printk(KERN_ERR "%s: Can't request vlynq registers\n",
460 dev->local = ioremap_nocache(regs_res->start, len);
462 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
468 dev->remote = (struct vlynq_regs *)((u32)dev->local + 128);
470 dev->irq = platform_get_irq_byname(pdev, "irq");
471 dev->irq_start = irq_res->start;
472 dev->irq_end = irq_res->end;
474 dev->remote_irq = 30;
476 if (device_register(&dev->dev))
478 platform_set_drvdata(pdev, dev);
480 printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
481 dev->dev.bus_id, (void *)dev->regs_start, dev->irq,
482 (void *)dev->mem_start);
490 release_mem_region(regs_res->start, len);
495 static int vlynq_remove(struct platform_device *pdev)
497 struct vlynq_device *dev = platform_get_drvdata(pdev);
499 device_unregister(&dev->dev);
500 release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
507 static struct platform_driver vlynq_driver = {
508 .driver.name = "vlynq",
509 .probe = vlynq_probe,
510 .remove = vlynq_remove,
513 struct bus_type vlynq_bus_type = {
515 .probe = vlynq_device_probe,
516 .remove = vlynq_device_remove,
518 EXPORT_SYMBOL(vlynq_bus_type);
521 extern void vlynq_pci_init(void);
523 static int __init vlynq_init(void)
527 res = bus_register(&vlynq_bus_type);
531 res = platform_driver_register(&vlynq_driver);
542 bus_unregister(&vlynq_bus_type);
548 void __devexit vlynq_exit(void)
550 platform_driver_unregister(&vlynq_driver);
551 bus_unregister(&vlynq_bus_type);
556 subsys_initcall(vlynq_init);