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/module.h>
25 #include <linux/errno.h>
26 #include <linux/platform_device.h>
27 #include <linux/interrupt.h>
28 #include <linux/device.h>
31 #include <linux/vlynq.h>
33 #define VLYNQ_CTRL_PM_ENABLE 0x80000000
34 #define VLYNQ_CTRL_CLOCK_INT 0x00008000
35 #define VLYNQ_CTRL_CLOCK_DIV(x) (((x) & 7) << 16)
36 #define VLYNQ_CTRL_INT_LOCAL 0x00004000
37 #define VLYNQ_CTRL_INT_ENABLE 0x00002000
38 #define VLYNQ_CTRL_INT_VECTOR(x) (((x) & 0x1f) << 8)
39 #define VLYNQ_CTRL_INT2CFG 0x00000080
40 #define VLYNQ_CTRL_RESET 0x00000001
42 #define VLYNQ_INT_OFFSET 0x00000014
43 #define VLYNQ_REMOTE_OFFSET 0x00000080
45 #define VLYNQ_STATUS_LINK 0x00000001
46 #define VLYNQ_STATUS_LERROR 0x00000080
47 #define VLYNQ_STATUS_RERROR 0x00000100
49 #define VINT_ENABLE 0x00000100
50 #define VINT_TYPE_EDGE 0x00000080
51 #define VINT_LEVEL_LOW 0x00000040
52 #define VINT_VECTOR(x) ((x) & 0x1f)
53 #define VINT_OFFSET(irq) (8 * ((irq) % 4))
55 #define VLYNQ_AUTONEGO_V2 0x00010000
66 struct vlynq_mapping rx_mapping
[4];
71 } __attribute__ ((packed
));
73 #define vlynq_reg_read(reg) readl(&(reg))
74 #define vlynq_reg_write(reg, val) writel(val, &(reg))
76 static int __vlynq_enable_device(struct vlynq_device
*dev
);
79 static void vlynq_dump_regs(struct vlynq_device
*dev
)
82 printk(KERN_DEBUG
"VLYNQ local=%p remote=%p\n",
83 dev
->local
, dev
->remote
);
84 for (i
= 0; i
< 32; i
++) {
85 printk(KERN_DEBUG
"VLYNQ: local %d: %08x\n",
86 i
+ 1, ((u32
*)dev
->local
)[i
]);
87 printk(KERN_DEBUG
"VLYNQ: remote %d: %08x\n",
88 i
+ 1, ((u32
*)dev
->remote
)[i
]);
92 static void vlynq_dump_mem(u32
*base
, int count
)
95 for (i
= 0; i
< (count
+ 3) / 4; i
++) {
96 if (i
% 4 == 0) printk(KERN_DEBUG
"\nMEM[0x%04x]:", i
* 4);
97 printk(KERN_DEBUG
" 0x%08x", *(base
+ i
));
99 printk(KERN_DEBUG
"\n");
103 int vlynq_linked(struct vlynq_device
*dev
)
107 for (i
= 0; i
< 100; i
++)
108 if (vlynq_reg_read(dev
->local
->status
) & VLYNQ_STATUS_LINK
)
116 static void vlynq_irq_unmask(unsigned int irq
)
119 struct vlynq_device
*dev
= get_irq_chip_data(irq
);
123 virq
= irq
- dev
->irq_start
;
124 val
= vlynq_reg_read(dev
->remote
->int_device
[virq
>> 2]);
125 val
|= (VINT_ENABLE
| virq
) << VINT_OFFSET(virq
);
126 vlynq_reg_write(dev
->remote
->int_device
[virq
>> 2], val
);
129 static void vlynq_irq_mask(unsigned int irq
)
132 struct vlynq_device
*dev
= get_irq_chip_data(irq
);
136 virq
= irq
- dev
->irq_start
;
137 val
= vlynq_reg_read(dev
->remote
->int_device
[virq
>> 2]);
138 val
&= ~(VINT_ENABLE
<< VINT_OFFSET(virq
));
139 vlynq_reg_write(dev
->remote
->int_device
[virq
>> 2], val
);
142 static int vlynq_irq_type(unsigned int irq
, unsigned int flow_type
)
145 struct vlynq_device
*dev
= get_irq_chip_data(irq
);
149 virq
= irq
- dev
->irq_start
;
150 val
= vlynq_reg_read(dev
->remote
->int_device
[virq
>> 2]);
151 switch (flow_type
& IRQ_TYPE_SENSE_MASK
) {
152 case IRQ_TYPE_EDGE_RISING
:
153 case IRQ_TYPE_EDGE_FALLING
:
154 case IRQ_TYPE_EDGE_BOTH
:
155 val
|= VINT_TYPE_EDGE
<< VINT_OFFSET(virq
);
156 val
&= ~(VINT_LEVEL_LOW
<< VINT_OFFSET(virq
));
158 case IRQ_TYPE_LEVEL_HIGH
:
159 val
&= ~(VINT_TYPE_EDGE
<< VINT_OFFSET(virq
));
160 val
&= ~(VINT_LEVEL_LOW
<< VINT_OFFSET(virq
));
162 case IRQ_TYPE_LEVEL_LOW
:
163 val
&= ~(VINT_TYPE_EDGE
<< VINT_OFFSET(virq
));
164 val
|= VINT_LEVEL_LOW
<< VINT_OFFSET(virq
);
169 vlynq_reg_write(dev
->remote
->int_device
[virq
>> 2], val
);
173 static void vlynq_local_ack(unsigned int irq
)
175 struct vlynq_device
*dev
= get_irq_chip_data(irq
);
176 u32 status
= vlynq_reg_read(dev
->local
->status
);
177 if (printk_ratelimit())
178 printk(KERN_DEBUG
"%s: local status: 0x%08x\n",
179 dev
->dev
.bus_id
, status
);
180 vlynq_reg_write(dev
->local
->status
, status
);
183 static void vlynq_remote_ack(unsigned int irq
)
185 struct vlynq_device
*dev
= get_irq_chip_data(irq
);
186 u32 status
= vlynq_reg_read(dev
->remote
->status
);
187 if (printk_ratelimit())
188 printk(KERN_DEBUG
"%s: remote status: 0x%08x\n",
189 dev
->dev
.bus_id
, status
);
190 vlynq_reg_write(dev
->remote
->status
, status
);
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
= dev
->irq_start
; i
<= dev
->irq_end
; i
++) {
268 virq
= i
- dev
->irq_start
;
269 if (virq
== dev
->local_irq
) {
270 set_irq_chip_and_handler(i
, &vlynq_local_chip
,
272 set_irq_chip_data(i
, dev
);
273 } else if (virq
== dev
->remote_irq
) {
274 set_irq_chip_and_handler(i
, &vlynq_remote_chip
,
276 set_irq_chip_data(i
, dev
);
278 set_irq_chip_and_handler(i
, &vlynq_irq_chip
,
280 set_irq_chip_data(i
, dev
);
281 vlynq_reg_write(dev
->remote
->int_device
[virq
>> 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_device_release(struct device
*dev
)
295 struct vlynq_device
*vdev
= to_vlynq_device(dev
);
299 static int vlynq_device_match(struct device
*dev
,
300 struct device_driver
*drv
)
302 struct vlynq_device
*vdev
= to_vlynq_device(dev
);
303 struct vlynq_driver
*vdrv
= to_vlynq_driver(drv
);
304 struct plat_vlynq_ops
*ops
= dev
->platform_data
;
305 struct vlynq_device_id
*ids
= vdrv
->id_table
;
310 vdev
->divisor
= ids
->divisor
;
311 result
= __vlynq_enable_device(vdev
);
313 id
= vlynq_reg_read(vdev
->remote
->chip
);
316 vlynq_set_drvdata(vdev
, ids
);
325 static int vlynq_device_probe(struct device
*dev
)
327 struct vlynq_device
*vdev
= to_vlynq_device(dev
);
328 struct vlynq_driver
*drv
= to_vlynq_driver(dev
->driver
);
329 struct vlynq_device_id
*id
= vlynq_get_drvdata(vdev
);
330 int result
= -ENODEV
;
333 if (drv
&& drv
->probe
)
334 result
= drv
->probe(vdev
, id
);
340 static int vlynq_device_remove(struct device
*dev
)
342 struct vlynq_driver
*drv
= to_vlynq_driver(dev
->driver
);
343 if (drv
&& drv
->remove
)
344 drv
->remove(to_vlynq_device(dev
));
349 int __vlynq_register_driver(struct vlynq_driver
*driver
, struct module
*owner
)
351 driver
->driver
.name
= driver
->name
;
352 driver
->driver
.bus
= &vlynq_bus_type
;
353 return driver_register(&driver
->driver
);
355 EXPORT_SYMBOL(__vlynq_register_driver
);
357 void vlynq_unregister_driver(struct vlynq_driver
*driver
)
359 driver_unregister(&driver
->driver
);
361 EXPORT_SYMBOL(vlynq_unregister_driver
);
363 static int __vlynq_enable_device(struct vlynq_device
*dev
)
366 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
368 result
= ops
->on(dev
);
372 switch (dev
->divisor
) {
374 /* Only try locally supplied clock, others cause problems */
375 vlynq_reg_write(dev
->remote
->control
, 0);
376 for (i
= vlynq_ldiv1
; i
<= vlynq_ldiv8
; i
++) {
377 vlynq_reg_write(dev
->local
->control
,
378 VLYNQ_CTRL_CLOCK_INT
|
379 VLYNQ_CTRL_CLOCK_DIV(i
- vlynq_ldiv1
));
380 if (vlynq_linked(dev
)) {
382 "%s: using local clock divisor %d\n",
383 dev
->dev
.bus_id
, i
- vlynq_ldiv1
+ 1);
388 case vlynq_ldiv1
: case vlynq_ldiv2
: case vlynq_ldiv3
: case vlynq_ldiv4
:
389 case vlynq_ldiv5
: case vlynq_ldiv6
: case vlynq_ldiv7
: case vlynq_ldiv8
:
390 vlynq_reg_write(dev
->remote
->control
, 0);
391 vlynq_reg_write(dev
->local
->control
,
392 VLYNQ_CTRL_CLOCK_INT
|
393 VLYNQ_CTRL_CLOCK_DIV(dev
->divisor
-
395 if (vlynq_linked(dev
)) {
397 "%s: using local clock divisor %d\n",
398 dev
->dev
.bus_id
, dev
->divisor
- vlynq_ldiv1
+ 1);
402 case vlynq_rdiv1
: case vlynq_rdiv2
: case vlynq_rdiv3
: case vlynq_rdiv4
:
403 case vlynq_rdiv5
: case vlynq_rdiv6
: case vlynq_rdiv7
: case vlynq_rdiv8
:
404 vlynq_reg_write(dev
->local
->control
, 0);
405 vlynq_reg_write(dev
->remote
->control
,
406 VLYNQ_CTRL_CLOCK_INT
|
407 VLYNQ_CTRL_CLOCK_DIV(dev
->divisor
-
409 if (vlynq_linked(dev
)) {
411 "%s: using remote clock divisor %d\n",
412 dev
->dev
.bus_id
, dev
->divisor
- vlynq_rdiv1
+ 1);
416 case vlynq_div_external
:
417 vlynq_reg_write(dev
->local
->control
, 0);
418 vlynq_reg_write(dev
->remote
->control
, 0);
419 if (vlynq_linked(dev
)) {
420 printk(KERN_DEBUG
"%s: using external clock\n",
431 int vlynq_enable_device(struct vlynq_device
*dev
)
433 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
434 int result
= -ENODEV
;
436 result
= __vlynq_enable_device(dev
);
440 result
= vlynq_setup_irq(dev
);
444 dev
->enabled
= !result
;
447 EXPORT_SYMBOL(vlynq_enable_device
);
450 void vlynq_disable_device(struct vlynq_device
*dev
)
452 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
455 free_irq(dev
->irq
, dev
);
458 EXPORT_SYMBOL(vlynq_disable_device
);
460 int vlynq_set_local_mapping(struct vlynq_device
*dev
, u32 tx_offset
,
461 struct vlynq_mapping
*mapping
)
468 vlynq_reg_write(dev
->local
->tx_offset
, tx_offset
);
469 for (i
= 0; i
< 4; i
++) {
470 vlynq_reg_write(dev
->local
->rx_mapping
[i
].offset
,
472 vlynq_reg_write(dev
->local
->rx_mapping
[i
].size
,
477 EXPORT_SYMBOL(vlynq_set_local_mapping
);
479 int vlynq_set_remote_mapping(struct vlynq_device
*dev
, u32 tx_offset
,
480 struct vlynq_mapping
*mapping
)
487 vlynq_reg_write(dev
->remote
->tx_offset
, tx_offset
);
488 for (i
= 0; i
< 4; i
++) {
489 vlynq_reg_write(dev
->remote
->rx_mapping
[i
].offset
,
491 vlynq_reg_write(dev
->remote
->rx_mapping
[i
].size
,
496 EXPORT_SYMBOL(vlynq_set_remote_mapping
);
498 int vlynq_set_local_irq(struct vlynq_device
*dev
, int virq
)
500 int irq
= dev
->irq_start
+ virq
;
504 if ((irq
< dev
->irq_start
) || (irq
> dev
->irq_end
))
507 if (virq
== dev
->remote_irq
)
510 dev
->local_irq
= virq
;
514 EXPORT_SYMBOL(vlynq_set_local_irq
);
516 int vlynq_set_remote_irq(struct vlynq_device
*dev
, int virq
)
518 int irq
= dev
->irq_start
+ virq
;
522 if ((irq
< dev
->irq_start
) || (irq
> dev
->irq_end
))
525 if (virq
== dev
->local_irq
)
528 dev
->remote_irq
= virq
;
532 EXPORT_SYMBOL(vlynq_set_remote_irq
);
534 static int vlynq_probe(struct platform_device
*pdev
)
536 struct vlynq_device
*dev
;
537 struct resource
*regs_res
, *mem_res
, *irq_res
;
540 regs_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
544 mem_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mem");
548 irq_res
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "devirq");
552 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
555 "vlynq: failed to allocate device structure\n");
560 dev
->dev
.bus
= &vlynq_bus_type
;
561 dev
->dev
.parent
= &pdev
->dev
;
562 snprintf(dev
->dev
.bus_id
, BUS_ID_SIZE
, "vlynq%d", dev
->id
);
563 dev
->dev
.bus_id
[BUS_ID_SIZE
- 1] = 0;
564 dev
->dev
.platform_data
= pdev
->dev
.platform_data
;
565 dev
->dev
.release
= vlynq_device_release
;
567 dev
->regs_start
= regs_res
->start
;
568 dev
->regs_end
= regs_res
->end
;
569 dev
->mem_start
= mem_res
->start
;
570 dev
->mem_end
= mem_res
->end
;
572 len
= regs_res
->end
- regs_res
->start
;
573 if (!request_mem_region(regs_res
->start
, len
, dev
->dev
.bus_id
)) {
574 printk(KERN_ERR
"%s: Can't request vlynq registers\n",
580 dev
->local
= ioremap(regs_res
->start
, len
);
582 printk(KERN_ERR
"%s: Can't remap vlynq registers\n",
588 dev
->remote
= (struct vlynq_regs
*)((void *)dev
->local
+
589 VLYNQ_REMOTE_OFFSET
);
591 dev
->irq
= platform_get_irq_byname(pdev
, "irq");
592 dev
->irq_start
= irq_res
->start
;
593 dev
->irq_end
= irq_res
->end
;
594 dev
->local_irq
= dev
->irq_end
- dev
->irq_start
;
595 dev
->remote_irq
= dev
->local_irq
- 1;
597 if (device_register(&dev
->dev
))
599 platform_set_drvdata(pdev
, dev
);
601 printk(KERN_INFO
"%s: regs 0x%p, irq %d, mem 0x%p\n",
602 dev
->dev
.bus_id
, (void *)dev
->regs_start
, dev
->irq
,
603 (void *)dev
->mem_start
);
611 release_mem_region(regs_res
->start
, len
);
616 static int vlynq_remove(struct platform_device
*pdev
)
618 struct vlynq_device
*dev
= platform_get_drvdata(pdev
);
620 device_unregister(&dev
->dev
);
622 release_mem_region(dev
->regs_start
, dev
->regs_end
- dev
->regs_start
);
629 static struct platform_driver vlynq_driver
= {
630 .driver
.name
= "vlynq",
631 .probe
= vlynq_probe
,
632 .remove
= __devexit_p(vlynq_remove
),
635 struct bus_type vlynq_bus_type
= {
637 .match
= vlynq_device_match
,
638 .probe
= vlynq_device_probe
,
639 .remove
= vlynq_device_remove
,
641 EXPORT_SYMBOL(vlynq_bus_type
);
643 static int __devinit
vlynq_init(void)
647 res
= bus_register(&vlynq_bus_type
);
651 res
= platform_driver_register(&vlynq_driver
);
658 bus_unregister(&vlynq_bus_type
);
663 static void __devexit
vlynq_exit(void)
665 platform_driver_unregister(&vlynq_driver
);
666 bus_unregister(&vlynq_bus_type
);
669 module_init(vlynq_init
);
670 module_exit(vlynq_exit
);