4 * Copyright (C) 2006, 2007 OpenWrt.org
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/init.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/string.h>
25 #include <linux/delay.h>
26 #include <linux/device.h>
27 #include <linux/ioport.h>
28 #include <linux/errno.h>
29 #include <linux/platform_device.h>
30 #include <linux/irq.h>
31 #include <linux/interrupt.h>
32 #include <linux/device.h>
33 #include <asm/addrspace.h>
35 #include <asm/ar7/ar7.h>
36 #include <asm/ar7/vlynq.h>
38 #define PER_DEVICE_IRQS 32
40 #define VLYNQ_CTRL_PM_ENABLE 0x80000000
41 #define VLYNQ_CTRL_CLOCK_INT 0x00008000
42 #define VLYNQ_CTRL_CLOCK_DIV(x) (((x) & 7) << 16)
43 #define VLYNQ_CTRL_INT_LOCAL 0x00004000
44 #define VLYNQ_CTRL_INT_ENABLE 0x00002000
45 #define VLYNQ_CTRL_INT_VECTOR(x) (((x) & 0x1f) << 8)
46 #define VLYNQ_CTRL_INT2CFG 0x00000080
47 #define VLYNQ_CTRL_RESET 0x00000001
49 #define VLYNQ_STATUS_LINK 0x00000001
50 #define VLYNQ_STATUS_LERROR 0x00000080
51 #define VLYNQ_STATUS_RERROR 0x00000100
53 #define VINT_ENABLE 0x00000100
54 #define VINT_TYPE_EDGE 0x00000080
55 #define VINT_LEVEL_LOW 0x00000040
56 #define VINT_VECTOR(x) ((x) & 0x1f)
57 #define VINT_OFFSET(irq) (8 * ((irq) % 4))
59 #define VLYNQ_AUTONEGO_V2 0x00010000
70 struct vlynq_mapping rx_mapping
[4];
75 } __attribute__ ((packed
));
77 #define vlynq_reg_read(reg) readl(&(reg))
78 #define vlynq_reg_write(reg, val) writel(val, &(reg))
81 static void vlynq_dump_regs(struct vlynq_device
*dev
)
84 printk("VLYNQ local=%p remote=%p\n", dev
->local
, dev
->remote
);
85 for (i
= 0; i
< 32; i
++) {
86 printk("VLYNQ: local %d: %08x\n", i
+ 1, ((u32
*)dev
->local
)[i
]);
87 printk("VLYNQ: remote %d: %08x\n", 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("\nMEM[0x%04x]:", i
* 4);
96 printk(" 0x%08x", *(base
+ i
));
102 int vlynq_linked(struct vlynq_device
*dev
)
106 for (i
= 0; i
< 10; 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
= irq_desc
[irq
].chip_data
;
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 irqreturn_t
vlynq_irq(int irq
, void *dev_id
)
174 struct vlynq_device
*dev
= dev_id
;
178 status
= vlynq_reg_read(dev
->local
->int_status
);
179 vlynq_reg_write(dev
->local
->int_status
, status
);
181 if (status
& (1 << dev
->local_irq
)) { /* Local vlynq IRQ. Ack */
182 ack
= vlynq_reg_read(dev
->local
->status
);
183 vlynq_reg_write(dev
->local
->status
, ack
);
186 if (status
& (1 << dev
->remote_irq
)) { /* Remote vlynq IRQ. Ack */
187 ack
= vlynq_reg_read(dev
->remote
->status
);
188 vlynq_reg_write(dev
->remote
->status
, ack
);
191 status
&= ~((1 << dev
->local_irq
) | (1 << dev
->remote_irq
));
193 if (status
& 1) /* Remote device IRQ. Pass. */
194 do_IRQ(dev
->irq_start
+ virq
);
202 static struct irq_chip vlynq_irq_chip
= {
204 .unmask
= vlynq_irq_unmask
,
205 .mask
= vlynq_irq_mask
,
206 .set_type
= vlynq_irq_type
,
209 static int vlynq_setup_irq(struct vlynq_device
*dev
)
214 if (dev
->local_irq
== dev
->remote_irq
) {
215 printk("%s: local vlynq irq should be different from remote\n",
220 val
= VLYNQ_CTRL_INT_VECTOR(dev
->local_irq
);
221 val
|= VLYNQ_CTRL_INT_ENABLE
| VLYNQ_CTRL_INT_LOCAL
|
223 val
|= vlynq_reg_read(dev
->local
->control
);
224 vlynq_reg_write(dev
->local
->int_ptr
, 0x14);
225 vlynq_reg_write(dev
->local
->control
, val
);
227 val
= VLYNQ_CTRL_INT_VECTOR(dev
->remote_irq
);
228 val
|= VLYNQ_CTRL_INT_ENABLE
;
229 val
|= vlynq_reg_read(dev
->remote
->control
);
230 vlynq_reg_write(dev
->remote
->int_ptr
, 0x14);
231 vlynq_reg_write(dev
->remote
->control
, val
);
233 for (i
= 0; i
< PER_DEVICE_IRQS
; i
++) {
234 if ((i
== dev
->local_irq
) || (i
== dev
->remote_irq
))
236 set_irq_chip(dev
->irq_start
+ i
, &vlynq_irq_chip
);
237 set_irq_chip_data(dev
->irq_start
+ i
, dev
);
238 vlynq_reg_write(dev
->remote
->int_device
[i
>> 2], 0);
241 if (request_irq(dev
->irq
, vlynq_irq
, SA_SHIRQ
, "vlynq", dev
)) {
242 printk("%s: request_irq failed\n", dev
->dev
.bus_id
);
249 static void vlynq_free_irq(struct vlynq_device
*dev
)
251 free_irq(dev
->irq
, dev
);
254 static void vlynq_device_release(struct device
*dev
)
256 struct vlynq_device
*vdev
= to_vlynq_device(dev
);
260 static int vlynq_device_probe(struct device
*dev
)
262 struct vlynq_driver
*drv
= to_vlynq_driver(dev
->driver
);
264 return drv
->probe(to_vlynq_device(dev
));
268 static int vlynq_device_remove(struct device
*dev
)
270 struct vlynq_driver
*drv
= to_vlynq_driver(dev
->driver
);
272 return drv
->remove(to_vlynq_device(dev
));
276 int __vlynq_register_driver(struct vlynq_driver
*driver
, struct module
*owner
)
278 driver
->driver
.name
= driver
->name
;
279 driver
->driver
.bus
= &vlynq_bus_type
;
280 /* driver->driver.owner = owner;*/
281 return driver_register(&driver
->driver
);
283 EXPORT_SYMBOL(__vlynq_register_driver
);
285 void vlynq_unregister_driver(struct vlynq_driver
*driver
)
287 driver_unregister(&driver
->driver
);
289 EXPORT_SYMBOL(vlynq_unregister_driver
);
291 int vlynq_device_enable(struct vlynq_device
*dev
)
295 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
297 result
= ops
->on(dev
);
301 vlynq_reg_write(dev
->local
->control
, 0);
302 vlynq_reg_write(dev
->remote
->control
, 0);
305 if (vlynq_linked(dev)) {
306 printk(KERN_INFO "%s: linked (using external clock)\n",
308 return vlynq_setup_irq(dev);
312 for (div
= 1; div
<= 8; div
++) {
314 vlynq_reg_write(dev
->local
->control
, VLYNQ_CTRL_CLOCK_INT
|
315 VLYNQ_CTRL_CLOCK_DIV(div
- 1));
316 vlynq_reg_write(dev
->remote
->control
, 0);
317 if (vlynq_linked(dev
)) {
318 printk(KERN_INFO
"%s: linked (using internal clock, div: %d)\n",
319 dev
->dev
.bus_id
, div
);
320 return vlynq_setup_irq(dev
);
327 void vlynq_device_disable(struct vlynq_device
*dev
)
329 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
335 u32
vlynq_remote_id(struct vlynq_device
*dev
)
337 return vlynq_reg_read(dev
->remote
->chip
);
340 void vlynq_set_local_mapping(struct vlynq_device
*dev
, u32 tx_offset
,
341 struct vlynq_mapping
*mapping
)
345 vlynq_reg_write(dev
->local
->tx_offset
, tx_offset
);
346 for (i
= 0; i
< 4; i
++) {
347 vlynq_reg_write(dev
->local
->rx_mapping
[i
].offset
, mapping
[i
].offset
);
348 vlynq_reg_write(dev
->local
->rx_mapping
[i
].size
, mapping
[i
].size
);
352 void vlynq_set_remote_mapping(struct vlynq_device
*dev
, u32 tx_offset
,
353 struct vlynq_mapping
*mapping
)
357 vlynq_reg_write(dev
->remote
->tx_offset
, tx_offset
);
358 for (i
= 0; i
< 4; i
++) {
359 vlynq_reg_write(dev
->remote
->rx_mapping
[i
].offset
, mapping
[i
].offset
);
360 vlynq_reg_write(dev
->remote
->rx_mapping
[i
].size
, mapping
[i
].size
);
364 int vlynq_virq_to_irq(struct vlynq_device
*dev
, int virq
)
366 if ((virq
< 0) || (virq
>= PER_DEVICE_IRQS
))
369 if ((virq
== dev
->local_irq
) || (virq
== dev
->remote_irq
))
372 return dev
->irq_start
+ virq
;
375 int vlynq_irq_to_virq(struct vlynq_device
*dev
, int irq
)
377 if ((irq
< dev
->irq_start
) || (irq
>= dev
->irq_start
+ PER_DEVICE_IRQS
))
380 return irq
- dev
->irq_start
;
383 int vlynq_set_local_irq(struct vlynq_device
*dev
, int virq
)
385 if ((virq
< 0) || (virq
>= PER_DEVICE_IRQS
))
388 if (virq
== dev
->remote_irq
)
391 dev
->local_irq
= virq
;
396 int vlynq_set_remote_irq(struct vlynq_device
*dev
, int virq
)
398 if ((virq
< 0) || (virq
>= PER_DEVICE_IRQS
))
401 if (virq
== dev
->local_irq
)
404 dev
->remote_irq
= virq
;
409 static int vlynq_probe(struct platform_device
*pdev
)
411 struct vlynq_device
*dev
;
412 struct resource
*regs_res
, *mem_res
, *irq_res
;
415 if (strcmp(pdev
->name
, "vlynq"))
418 regs_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
422 mem_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mem");
426 irq_res
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "devirq");
430 dev
= kzalloc(sizeof(struct vlynq_device
), GFP_KERNEL
);
432 printk(KERN_ERR
"vlynq: failed to allocate device structure\n");
436 memset(dev
, 0, sizeof(struct vlynq_device
));
439 dev
->dev
.bus
= &vlynq_bus_type
;
440 dev
->dev
.parent
= &pdev
->dev
;
441 snprintf(dev
->dev
.bus_id
, BUS_ID_SIZE
, "vlynq%d", dev
->id
);
442 dev
->dev
.bus_id
[BUS_ID_SIZE
- 1] = 0;
443 dev
->dev
.platform_data
= pdev
->dev
.platform_data
;
444 dev
->dev
.release
= vlynq_device_release
;
446 dev
->regs_start
= regs_res
->start
;
447 dev
->regs_end
= regs_res
->end
;
448 dev
->mem_start
= mem_res
->start
;
449 dev
->mem_end
= mem_res
->end
;
451 len
= regs_res
->end
- regs_res
->start
;
452 if (!request_mem_region(regs_res
->start
, len
, dev
->dev
.bus_id
)) {
453 printk("%s: Can't request vlynq registers\n", dev
->dev
.bus_id
);
458 dev
->local
= ioremap_nocache(regs_res
->start
, len
);
460 printk("%s: Can't remap vlynq registers\n", dev
->dev
.bus_id
);
465 dev
->remote
= (struct vlynq_regs
*)((u32
)dev
->local
+ 128);
467 dev
->irq
= platform_get_irq_byname(pdev
, "irq");
468 dev
->irq_start
= irq_res
->start
;
469 dev
->irq_end
= irq_res
->end
;
471 dev
->remote_irq
= 30;
473 if (device_register(&dev
->dev
))
475 platform_set_drvdata(pdev
, dev
);
477 printk("%s: regs 0x%p, irq %d, mem 0x%p\n",
478 dev
->dev
.bus_id
, (void *)dev
->regs_start
, dev
->irq
,
479 (void *)dev
->mem_start
);
487 release_mem_region(regs_res
->start
, len
);
492 static int vlynq_remove(struct platform_device
*pdev
)
494 struct vlynq_device
*dev
= platform_get_drvdata(pdev
);
496 device_unregister(&dev
->dev
);
497 release_mem_region(dev
->regs_start
, dev
->regs_end
- dev
->regs_start
);
504 static struct platform_driver vlynq_driver
= {
505 .driver
.name
= "vlynq",
506 .probe
= vlynq_probe
,
507 .remove
= vlynq_remove
,
510 struct bus_type vlynq_bus_type
= {
512 .probe
= vlynq_device_probe
,
513 .remove
= vlynq_device_remove
,
515 EXPORT_SYMBOL(vlynq_bus_type
);
518 extern void vlynq_pci_init(void);
520 static int __init
vlynq_init(void)
524 res
= bus_register(&vlynq_bus_type
);
528 res
= platform_driver_register(&vlynq_driver
);
539 bus_unregister(&vlynq_bus_type
);
545 void __devexit vlynq_exit(void)
547 platform_driver_unregister(&vlynq_driver);
548 bus_unregister(&vlynq_bus_type);
553 subsys_initcall(vlynq_init
);