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_and_handler(dev
->irq_start
+ i
,
282 set_irq_chip_data(dev
->irq_start
+ i
, dev
);
283 vlynq_reg_write(dev
->remote
->int_device
[i
>> 2], 0);
287 if (request_irq(dev
->irq
, vlynq_irq
, IRQF_SHARED
, "vlynq", dev
)) {
288 printk(KERN_ERR
"%s: request_irq failed\n", dev
->dev
.bus_id
);
295 static void vlynq_free_irq(struct vlynq_device
*dev
)
297 free_irq(dev
->irq
, dev
);
300 static void vlynq_device_release(struct device
*dev
)
302 struct vlynq_device
*vdev
= to_vlynq_device(dev
);
306 static int vlynq_device_probe(struct device
*dev
)
308 struct vlynq_driver
*drv
= to_vlynq_driver(dev
->driver
);
310 return drv
->probe(to_vlynq_device(dev
));
314 static int vlynq_device_remove(struct device
*dev
)
316 struct vlynq_driver
*drv
= to_vlynq_driver(dev
->driver
);
318 return drv
->remove(to_vlynq_device(dev
));
322 int __vlynq_register_driver(struct vlynq_driver
*driver
, struct module
*owner
)
324 driver
->driver
.name
= driver
->name
;
325 driver
->driver
.bus
= &vlynq_bus_type
;
326 return driver_register(&driver
->driver
);
328 EXPORT_SYMBOL(__vlynq_register_driver
);
330 void vlynq_unregister_driver(struct vlynq_driver
*driver
)
332 driver_unregister(&driver
->driver
);
334 EXPORT_SYMBOL(vlynq_unregister_driver
);
336 int vlynq_device_enable(struct vlynq_device
*dev
)
339 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
341 result
= ops
->on(dev
);
345 switch (dev
->divisor
) {
347 /* First try locally supplied clock */
348 vlynq_reg_write(dev
->remote
->control
, 0);
349 for (i
= vlynq_ldiv1
; i
<= vlynq_ldiv8
; i
++) {
350 vlynq_reg_write(dev
->local
->control
,
351 VLYNQ_CTRL_CLOCK_INT
|
352 VLYNQ_CTRL_CLOCK_DIV(i
- vlynq_ldiv1
));
353 if (vlynq_linked(dev
)) {
355 "%s: using local clock divisor %d\n",
356 dev
->dev
.bus_id
, i
- vlynq_ldiv1
+ 1);
357 return vlynq_setup_irq(dev
);
360 /* Then remotely supplied clock */
361 vlynq_reg_write(dev
->local
->control
, 0);
362 for (i
= vlynq_rdiv1
; i
<= vlynq_rdiv8
; i
++) {
363 vlynq_reg_write(dev
->remote
->control
,
364 VLYNQ_CTRL_CLOCK_INT
|
365 VLYNQ_CTRL_CLOCK_DIV(i
- vlynq_rdiv1
));
366 if (vlynq_linked(dev
)) {
368 "%s: using remote clock divisor %d\n",
369 dev
->dev
.bus_id
, i
- vlynq_rdiv1
+ 1);
370 return vlynq_setup_irq(dev
);
373 /* At last, externally supplied clock */
374 vlynq_reg_write(dev
->remote
->control
, 0);
375 if (vlynq_linked(dev
)) {
376 printk(KERN_DEBUG
"%s: using external clock\n",
378 return vlynq_setup_irq(dev
);
381 case vlynq_ldiv1
: case vlynq_ldiv2
: case vlynq_ldiv3
: case vlynq_ldiv4
:
382 case vlynq_ldiv5
: case vlynq_ldiv6
: case vlynq_ldiv7
: case vlynq_ldiv8
:
383 vlynq_reg_write(dev
->remote
->control
, 0);
384 vlynq_reg_write(dev
->local
->control
,
385 VLYNQ_CTRL_CLOCK_INT
|
386 VLYNQ_CTRL_CLOCK_DIV(dev
->divisor
-
388 if (vlynq_linked(dev
)) {
390 "%s: using local clock divisor %d\n",
391 dev
->dev
.bus_id
, dev
->divisor
- vlynq_ldiv1
+ 1);
392 return vlynq_setup_irq(dev
);
395 case vlynq_rdiv1
: case vlynq_rdiv2
: case vlynq_rdiv3
: case vlynq_rdiv4
:
396 case vlynq_rdiv5
: case vlynq_rdiv6
: case vlynq_rdiv7
: case vlynq_rdiv8
:
397 vlynq_reg_write(dev
->local
->control
, 0);
398 vlynq_reg_write(dev
->remote
->control
,
399 VLYNQ_CTRL_CLOCK_INT
|
400 VLYNQ_CTRL_CLOCK_DIV(dev
->divisor
-
402 if (vlynq_linked(dev
)) {
404 "%s: using remote clock divisor %d\n",
405 dev
->dev
.bus_id
, dev
->divisor
- vlynq_rdiv1
+ 1);
406 return vlynq_setup_irq(dev
);
409 case vlynq_div_external
:
410 vlynq_reg_write(dev
->local
->control
, 0);
411 vlynq_reg_write(dev
->remote
->control
, 0);
412 if (vlynq_linked(dev
)) {
413 printk(KERN_DEBUG
"%s: using external clock\n",
415 return vlynq_setup_irq(dev
);
423 void vlynq_device_disable(struct vlynq_device
*dev
)
425 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
431 u32
vlynq_remote_id(struct vlynq_device
*dev
)
433 return vlynq_reg_read(dev
->remote
->chip
);
436 void vlynq_set_local_mapping(struct vlynq_device
*dev
, u32 tx_offset
,
437 struct vlynq_mapping
*mapping
)
441 vlynq_reg_write(dev
->local
->tx_offset
, tx_offset
);
442 for (i
= 0; i
< 4; i
++) {
443 vlynq_reg_write(dev
->local
->rx_mapping
[i
].offset
,
445 vlynq_reg_write(dev
->local
->rx_mapping
[i
].size
,
450 void vlynq_set_remote_mapping(struct vlynq_device
*dev
, u32 tx_offset
,
451 struct vlynq_mapping
*mapping
)
455 vlynq_reg_write(dev
->remote
->tx_offset
, tx_offset
);
456 for (i
= 0; i
< 4; i
++) {
457 vlynq_reg_write(dev
->remote
->rx_mapping
[i
].offset
,
459 vlynq_reg_write(dev
->remote
->rx_mapping
[i
].size
,
464 int vlynq_virq_to_irq(struct vlynq_device
*dev
, int virq
)
466 if ((virq
< 0) || (virq
>= PER_DEVICE_IRQS
))
469 return dev
->irq_start
+ virq
;
472 int vlynq_irq_to_virq(struct vlynq_device
*dev
, int irq
)
474 if ((irq
< dev
->irq_start
) || (irq
>= dev
->irq_start
+ PER_DEVICE_IRQS
))
477 return irq
- dev
->irq_start
;
480 int vlynq_set_local_irq(struct vlynq_device
*dev
, int virq
)
482 if ((virq
< 0) || (virq
>= PER_DEVICE_IRQS
))
485 if (virq
== dev
->remote_irq
)
488 dev
->local_irq
= virq
;
493 int vlynq_set_remote_irq(struct vlynq_device
*dev
, int virq
)
495 if ((virq
< 0) || (virq
>= PER_DEVICE_IRQS
))
498 if (virq
== dev
->local_irq
)
501 dev
->remote_irq
= virq
;
506 static int vlynq_probe(struct platform_device
*pdev
)
508 struct vlynq_device
*dev
;
509 struct resource
*regs_res
, *mem_res
, *irq_res
;
512 if (strcmp(pdev
->name
, "vlynq"))
515 regs_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
519 mem_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mem");
523 irq_res
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "devirq");
527 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
530 "vlynq: failed to allocate device structure\n");
535 dev
->dev
.bus
= &vlynq_bus_type
;
536 dev
->dev
.parent
= &pdev
->dev
;
537 snprintf(dev
->dev
.bus_id
, BUS_ID_SIZE
, "vlynq%d", dev
->id
);
538 dev
->dev
.bus_id
[BUS_ID_SIZE
- 1] = 0;
539 dev
->dev
.platform_data
= pdev
->dev
.platform_data
;
540 dev
->dev
.release
= vlynq_device_release
;
542 dev
->regs_start
= regs_res
->start
;
543 dev
->regs_end
= regs_res
->end
;
544 dev
->mem_start
= mem_res
->start
;
545 dev
->mem_end
= mem_res
->end
;
547 len
= regs_res
->end
- regs_res
->start
;
548 if (!request_mem_region(regs_res
->start
, len
, dev
->dev
.bus_id
)) {
549 printk(KERN_ERR
"%s: Can't request vlynq registers\n",
555 dev
->local
= ioremap(regs_res
->start
, len
);
557 printk(KERN_ERR
"%s: Can't remap vlynq registers\n",
563 dev
->remote
= (struct vlynq_regs
*)((u32
)dev
->local
+
564 VLYNQ_REMOTE_OFFSET
);
566 dev
->irq
= platform_get_irq_byname(pdev
, "irq");
567 dev
->irq_start
= irq_res
->start
;
568 dev
->irq_end
= irq_res
->end
;
570 dev
->remote_irq
= 30;
572 if (device_register(&dev
->dev
))
574 platform_set_drvdata(pdev
, dev
);
576 printk(KERN_INFO
"%s: regs 0x%p, irq %d, mem 0x%p\n",
577 dev
->dev
.bus_id
, (void *)dev
->regs_start
, dev
->irq
,
578 (void *)dev
->mem_start
);
586 release_mem_region(regs_res
->start
, len
);
591 static int vlynq_remove(struct platform_device
*pdev
)
593 struct vlynq_device
*dev
= platform_get_drvdata(pdev
);
595 device_unregister(&dev
->dev
);
597 release_mem_region(dev
->regs_start
, dev
->regs_end
- dev
->regs_start
);
604 static struct platform_driver vlynq_driver
= {
605 .driver
.name
= "vlynq",
606 .probe
= vlynq_probe
,
607 .remove
= vlynq_remove
,
610 struct bus_type vlynq_bus_type
= {
612 .probe
= vlynq_device_probe
,
613 .remove
= vlynq_device_remove
,
615 EXPORT_SYMBOL(vlynq_bus_type
);
618 extern void vlynq_pci_init(void);
620 static int __init
vlynq_init(void)
624 res
= bus_register(&vlynq_bus_type
);
628 res
= platform_driver_register(&vlynq_driver
);
639 bus_unregister(&vlynq_bus_type
);
644 /* Add this back when vlynq-pci crap is gone */
646 static void __devexit
vlynq_exit(void)
648 platform_driver_unregister(&vlynq_driver
);
649 bus_unregister(&vlynq_bus_type
);
653 subsys_initcall(vlynq_init
);