2 * Copyright (C) 2006, 2007 Eugene Konev <ejka@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>
29 #include <linux/delay.h>
32 #include <linux/vlynq.h>
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];
74 #define vlynq_reg_read(reg) readl(&(reg))
75 #define vlynq_reg_write(reg, val) writel(val, &(reg))
77 static int __vlynq_enable_device(struct vlynq_device
*dev
);
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
< 100; 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
= get_irq_chip_data(irq
);
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 void vlynq_local_ack(unsigned int irq
)
176 struct vlynq_device
*dev
= get_irq_chip_data(irq
);
177 u32 status
= vlynq_reg_read(dev
->local
->status
);
178 if (printk_ratelimit())
179 printk(KERN_DEBUG
"%s: local status: 0x%08x\n",
180 dev
->dev
.bus_id
, status
);
181 vlynq_reg_write(dev
->local
->status
, status
);
184 static void vlynq_remote_ack(unsigned int irq
)
186 struct vlynq_device
*dev
= get_irq_chip_data(irq
);
187 u32 status
= vlynq_reg_read(dev
->remote
->status
);
188 if (printk_ratelimit())
189 printk(KERN_DEBUG
"%s: remote status: 0x%08x\n",
190 dev
->dev
.bus_id
, status
);
191 vlynq_reg_write(dev
->remote
->status
, status
);
194 static irqreturn_t
vlynq_irq(int irq
, void *dev_id
)
196 struct vlynq_device
*dev
= dev_id
;
200 status
= vlynq_reg_read(dev
->local
->int_status
);
201 vlynq_reg_write(dev
->local
->int_status
, status
);
203 if (unlikely(!status
))
204 spurious_interrupt();
208 do_IRQ(dev
->irq_start
+ virq
);
216 static struct irq_chip vlynq_irq_chip
= {
218 .unmask
= vlynq_irq_unmask
,
219 .mask
= vlynq_irq_mask
,
220 .set_type
= vlynq_irq_type
,
223 static struct irq_chip vlynq_local_chip
= {
224 .name
= "vlynq local error",
225 .unmask
= vlynq_irq_unmask
,
226 .mask
= vlynq_irq_mask
,
227 .ack
= vlynq_local_ack
,
230 static struct irq_chip vlynq_remote_chip
= {
231 .name
= "vlynq local error",
232 .unmask
= vlynq_irq_unmask
,
233 .mask
= vlynq_irq_mask
,
234 .ack
= vlynq_remote_ack
,
237 static int vlynq_setup_irq(struct vlynq_device
*dev
)
242 if (dev
->local_irq
== dev
->remote_irq
) {
244 "%s: local vlynq irq should be different from remote\n",
249 /* Clear local and remote error bits */
250 vlynq_reg_write(dev
->local
->status
, vlynq_reg_read(dev
->local
->status
));
251 vlynq_reg_write(dev
->remote
->status
,
252 vlynq_reg_read(dev
->remote
->status
));
254 /* Now setup interrupts */
255 val
= VLYNQ_CTRL_INT_VECTOR(dev
->local_irq
);
256 val
|= VLYNQ_CTRL_INT_ENABLE
| VLYNQ_CTRL_INT_LOCAL
|
258 val
|= vlynq_reg_read(dev
->local
->control
);
259 vlynq_reg_write(dev
->local
->int_ptr
, VLYNQ_INT_OFFSET
);
260 vlynq_reg_write(dev
->local
->control
, val
);
262 val
= VLYNQ_CTRL_INT_VECTOR(dev
->remote_irq
);
263 val
|= VLYNQ_CTRL_INT_ENABLE
;
264 val
|= vlynq_reg_read(dev
->remote
->control
);
265 vlynq_reg_write(dev
->remote
->int_ptr
, VLYNQ_INT_OFFSET
);
266 vlynq_reg_write(dev
->remote
->control
, val
);
268 for (i
= dev
->irq_start
; i
<= dev
->irq_end
; i
++) {
269 virq
= i
- dev
->irq_start
;
270 if (virq
== dev
->local_irq
) {
271 set_irq_chip_and_handler(i
, &vlynq_local_chip
,
273 set_irq_chip_data(i
, dev
);
274 } else if (virq
== dev
->remote_irq
) {
275 set_irq_chip_and_handler(i
, &vlynq_remote_chip
,
277 set_irq_chip_data(i
, dev
);
279 set_irq_chip_and_handler(i
, &vlynq_irq_chip
,
281 set_irq_chip_data(i
, dev
);
282 vlynq_reg_write(dev
->remote
->int_device
[virq
>> 2], 0);
286 if (request_irq(dev
->irq
, vlynq_irq
, IRQF_SHARED
, "vlynq", dev
)) {
287 printk(KERN_ERR
"%s: request_irq failed\n", dev
->dev
.bus_id
);
294 static void vlynq_device_release(struct device
*dev
)
296 struct vlynq_device
*vdev
= to_vlynq_device(dev
);
300 static int vlynq_device_match(struct device
*dev
,
301 struct device_driver
*drv
)
303 struct vlynq_device
*vdev
= to_vlynq_device(dev
);
304 struct vlynq_driver
*vdrv
= to_vlynq_driver(drv
);
305 struct vlynq_device_id
*ids
= vdrv
->id_table
;
308 if (ids
->id
== vdev
->dev_id
) {
309 vdev
->divisor
= ids
->divisor
;
310 vlynq_set_drvdata(vdev
, ids
);
311 printk(KERN_INFO
"Driver found for VLYNQ " \
312 "device: %08x\n", vdev
->dev_id
);
315 printk(KERN_DEBUG
"Not using the %08x VLYNQ device's driver" \
316 " for VLYNQ device: %08x\n", ids
->id
, vdev
->dev_id
);
322 static int vlynq_device_probe(struct device
*dev
)
324 struct vlynq_device
*vdev
= to_vlynq_device(dev
);
325 struct vlynq_driver
*drv
= to_vlynq_driver(dev
->driver
);
326 struct vlynq_device_id
*id
= vlynq_get_drvdata(vdev
);
327 int result
= -ENODEV
;
330 if (drv
&& drv
->probe
)
331 result
= drv
->probe(vdev
, id
);
337 static int vlynq_device_remove(struct device
*dev
)
339 struct vlynq_driver
*drv
= to_vlynq_driver(dev
->driver
);
340 if (drv
&& drv
->remove
)
341 drv
->remove(to_vlynq_device(dev
));
346 int __vlynq_register_driver(struct vlynq_driver
*driver
, struct module
*owner
)
348 driver
->driver
.name
= driver
->name
;
349 driver
->driver
.bus
= &vlynq_bus_type
;
350 return driver_register(&driver
->driver
);
352 EXPORT_SYMBOL(__vlynq_register_driver
);
354 void vlynq_unregister_driver(struct vlynq_driver
*driver
)
356 driver_unregister(&driver
->driver
);
358 EXPORT_SYMBOL(vlynq_unregister_driver
);
360 static int __vlynq_enable_device(struct vlynq_device
*dev
)
363 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
365 result
= ops
->on(dev
);
369 switch (dev
->divisor
) {
370 case vlynq_div_external
:
372 vlynq_reg_write(dev
->local
->control
, 0);
373 vlynq_reg_write(dev
->remote
->control
, 0);
374 if (vlynq_linked(dev
)) {
375 dev
->divisor
= vlynq_div_external
;
376 printk(KERN_DEBUG
"%s: using external clock\n",
381 /* Only try locally supplied clock, others cause problems */
382 for (i
= dev
->dev_id
? vlynq_ldiv2
: vlynq_ldiv8
; dev
->dev_id
?
383 i
<= vlynq_ldiv8
: i
>= vlynq_ldiv2
;
384 dev
->dev_id
? i
++ : i
--) {
385 vlynq_reg_write(dev
->local
->control
,
386 VLYNQ_CTRL_CLOCK_INT
|
387 VLYNQ_CTRL_CLOCK_DIV(i
- vlynq_ldiv1
));
388 if (vlynq_linked(dev
)) {
390 "%s: using local clock divisor %d\n",
391 dev
->dev
.bus_id
, i
- vlynq_ldiv1
+ 1);
396 case vlynq_ldiv1
: case vlynq_ldiv2
: case vlynq_ldiv3
: case vlynq_ldiv4
:
397 case vlynq_ldiv5
: case vlynq_ldiv6
: case vlynq_ldiv7
: case vlynq_ldiv8
:
398 vlynq_reg_write(dev
->local
->control
,
399 VLYNQ_CTRL_CLOCK_INT
|
400 VLYNQ_CTRL_CLOCK_DIV(dev
->divisor
-
402 vlynq_reg_write(dev
->remote
->control
, 0);
403 if (vlynq_linked(dev
)) {
405 "%s: using local clock divisor %d\n",
406 dev
->dev
.bus_id
, dev
->divisor
- vlynq_ldiv1
+ 1);
410 case vlynq_rdiv1
: case vlynq_rdiv2
: case vlynq_rdiv3
: case vlynq_rdiv4
:
411 case vlynq_rdiv5
: case vlynq_rdiv6
: case vlynq_rdiv7
: case vlynq_rdiv8
:
412 vlynq_reg_write(dev
->local
->control
, 0);
413 vlynq_reg_write(dev
->remote
->control
,
414 VLYNQ_CTRL_CLOCK_INT
|
415 VLYNQ_CTRL_CLOCK_DIV(dev
->divisor
-
417 if (vlynq_linked(dev
)) {
419 "%s: using remote clock divisor %d\n",
420 dev
->dev
.bus_id
, dev
->divisor
- vlynq_rdiv1
+ 1);
430 int vlynq_enable_device(struct vlynq_device
*dev
)
432 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
433 int result
= -ENODEV
;
435 result
= __vlynq_enable_device(dev
);
439 result
= vlynq_setup_irq(dev
);
443 dev
->enabled
= !result
;
446 EXPORT_SYMBOL(vlynq_enable_device
);
449 void vlynq_disable_device(struct vlynq_device
*dev
)
451 struct plat_vlynq_ops
*ops
= dev
->dev
.platform_data
;
454 free_irq(dev
->irq
, dev
);
457 EXPORT_SYMBOL(vlynq_disable_device
);
459 int vlynq_set_local_mapping(struct vlynq_device
*dev
, u32 tx_offset
,
460 struct vlynq_mapping
*mapping
)
467 vlynq_reg_write(dev
->local
->tx_offset
, tx_offset
);
468 for (i
= 0; i
< 4; i
++) {
469 vlynq_reg_write(dev
->local
->rx_mapping
[i
].offset
,
471 vlynq_reg_write(dev
->local
->rx_mapping
[i
].size
,
476 EXPORT_SYMBOL(vlynq_set_local_mapping
);
478 int vlynq_set_remote_mapping(struct vlynq_device
*dev
, u32 tx_offset
,
479 struct vlynq_mapping
*mapping
)
486 vlynq_reg_write(dev
->remote
->tx_offset
, tx_offset
);
487 for (i
= 0; i
< 4; i
++) {
488 vlynq_reg_write(dev
->remote
->rx_mapping
[i
].offset
,
490 vlynq_reg_write(dev
->remote
->rx_mapping
[i
].size
,
495 EXPORT_SYMBOL(vlynq_set_remote_mapping
);
497 int vlynq_set_local_irq(struct vlynq_device
*dev
, int virq
)
499 int irq
= dev
->irq_start
+ virq
;
503 if ((irq
< dev
->irq_start
) || (irq
> dev
->irq_end
))
506 if (virq
== dev
->remote_irq
)
509 dev
->local_irq
= virq
;
513 EXPORT_SYMBOL(vlynq_set_local_irq
);
515 int vlynq_set_remote_irq(struct vlynq_device
*dev
, int virq
)
517 int irq
= dev
->irq_start
+ virq
;
521 if ((irq
< dev
->irq_start
) || (irq
> dev
->irq_end
))
524 if (virq
== dev
->local_irq
)
527 dev
->remote_irq
= virq
;
531 EXPORT_SYMBOL(vlynq_set_remote_irq
);
533 static int vlynq_probe(struct platform_device
*pdev
)
535 struct vlynq_device
*dev
;
536 struct resource
*regs_res
, *mem_res
, *irq_res
;
539 regs_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
543 mem_res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mem");
547 irq_res
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "devirq");
551 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
554 "vlynq: failed to allocate device structure\n");
559 dev
->dev
.bus
= &vlynq_bus_type
;
560 dev
->dev
.parent
= &pdev
->dev
;
561 snprintf(dev
->dev
.bus_id
, BUS_ID_SIZE
, "vlynq%d", dev
->id
);
562 dev
->dev
.bus_id
[BUS_ID_SIZE
- 1] = 0;
563 dev
->dev
.platform_data
= pdev
->dev
.platform_data
;
564 dev
->dev
.release
= vlynq_device_release
;
566 dev
->regs_start
= regs_res
->start
;
567 dev
->regs_end
= regs_res
->end
;
568 dev
->mem_start
= mem_res
->start
;
569 dev
->mem_end
= mem_res
->end
;
571 len
= regs_res
->end
- regs_res
->start
;
572 if (!request_mem_region(regs_res
->start
, len
, dev
->dev
.bus_id
)) {
573 printk(KERN_ERR
"%s: Can't request vlynq registers\n",
579 dev
->local
= ioremap(regs_res
->start
, len
);
581 printk(KERN_ERR
"%s: Can't remap vlynq registers\n",
587 dev
->remote
= (struct vlynq_regs
*)((void *)dev
->local
+
588 VLYNQ_REMOTE_OFFSET
);
590 dev
->irq
= platform_get_irq_byname(pdev
, "irq");
591 dev
->irq_start
= irq_res
->start
;
592 dev
->irq_end
= irq_res
->end
;
593 dev
->local_irq
= dev
->irq_end
- dev
->irq_start
;
594 dev
->remote_irq
= dev
->local_irq
- 1;
596 if (device_register(&dev
->dev
))
598 platform_set_drvdata(pdev
, dev
);
600 printk(KERN_INFO
"%s: regs 0x%p, irq %d, mem 0x%p\n",
601 dev
->dev
.bus_id
, (void *)dev
->regs_start
, dev
->irq
,
602 (void *)dev
->mem_start
);
605 dev
->divisor
= vlynq_div_auto
;
606 result
= __vlynq_enable_device(dev
);
608 dev
->dev_id
= vlynq_reg_read(dev
->remote
->chip
);
609 ((struct plat_vlynq_ops
*)(dev
->dev
.platform_data
))->off(dev
);
612 printk(KERN_INFO
"Found a VLYNQ device: %08x\n", dev
->dev_id
);
620 release_mem_region(regs_res
->start
, len
);
625 static int vlynq_remove(struct platform_device
*pdev
)
627 struct vlynq_device
*dev
= platform_get_drvdata(pdev
);
629 device_unregister(&dev
->dev
);
631 release_mem_region(dev
->regs_start
, dev
->regs_end
- dev
->regs_start
);
638 static struct platform_driver vlynq_platform_driver
= {
639 .driver
.name
= "vlynq",
640 .probe
= vlynq_probe
,
641 .remove
= __devexit_p(vlynq_remove
),
644 struct bus_type vlynq_bus_type
= {
646 .match
= vlynq_device_match
,
647 .probe
= vlynq_device_probe
,
648 .remove
= vlynq_device_remove
,
650 EXPORT_SYMBOL(vlynq_bus_type
);
652 static int __devinit
vlynq_init(void)
656 res
= bus_register(&vlynq_bus_type
);
660 res
= platform_driver_register(&vlynq_platform_driver
);
667 bus_unregister(&vlynq_bus_type
);
672 static void __devexit
vlynq_exit(void)
674 platform_driver_unregister(&vlynq_platform_driver
);
675 bus_unregister(&vlynq_bus_type
);
678 module_init(vlynq_init
);
679 module_exit(vlynq_exit
);