Enabled working ar7_wdt, style cleanups
[openwrt/staging/wigyori.git] / target / linux / ar7 / files / arch / mips / ar7 / vlynq.c
1 /*
2 * Copyright (C) 2006, 2007 OpenWrt.org
3 *
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.
8 *
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.
13 *
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
17 */
18
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>
28 #include <linux/io.h>
29
30 #include <asm/ar7/vlynq.h>
31
32 #define PER_DEVICE_IRQS 32
33
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
42
43 #define VLYNQ_INT_OFFSET 0x00000014
44 #define VLYNQ_REMOTE_OFFSET 0x00000080
45
46 #define VLYNQ_STATUS_LINK 0x00000001
47 #define VLYNQ_STATUS_LERROR 0x00000080
48 #define VLYNQ_STATUS_RERROR 0x00000100
49
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))
55
56 #define VLYNQ_AUTONEGO_V2 0x00010000
57
58 struct vlynq_regs {
59 u32 revision;
60 u32 control;
61 u32 status;
62 u32 int_prio;
63 u32 int_status;
64 u32 int_pending;
65 u32 int_ptr;
66 u32 tx_offset;
67 struct vlynq_mapping rx_mapping[4];
68 u32 chip;
69 u32 autonego;
70 u32 unused[6];
71 u32 int_device[8];
72 } __attribute__ ((packed));
73
74 #define vlynq_reg_read(reg) readl(&(reg))
75 #define vlynq_reg_write(reg, val) writel(val, &(reg))
76
77 #ifdef VLYNQ_DEBUG
78 static void vlynq_dump_regs(struct vlynq_device *dev)
79 {
80 int i;
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]);
88 }
89 }
90
91 static void vlynq_dump_mem(u32 *base, int count)
92 {
93 int i;
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));
97 }
98 printk(KERN_DEBUG "\n");
99 }
100 #endif
101
102 int vlynq_linked(struct vlynq_device *dev)
103 {
104 int i;
105
106 for (i = 0; i < 100; i++)
107 if (vlynq_reg_read(dev->local->status) & VLYNQ_STATUS_LINK)
108 return 1;
109 else
110 cpu_relax();
111
112 return 0;
113 }
114
115 static void vlynq_irq_unmask(unsigned int irq)
116 {
117 u32 val;
118 struct vlynq_device *dev = get_irq_chip_data(irq);
119 int virq;
120
121 BUG_ON(!dev);
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);
126 }
127
128 static void vlynq_irq_mask(unsigned int irq)
129 {
130 u32 val;
131 struct vlynq_device *dev = get_irq_chip_data(irq);
132 int virq;
133
134 BUG_ON(!dev);
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);
139 }
140
141 static int vlynq_irq_type(unsigned int irq, unsigned int flow_type)
142 {
143 u32 val;
144 struct vlynq_device *dev = get_irq_chip_data(irq);
145 int virq;
146
147 BUG_ON(!dev);
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));
156 break;
157 case IRQ_TYPE_LEVEL_HIGH:
158 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
159 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
160 break;
161 case IRQ_TYPE_LEVEL_LOW:
162 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
163 val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
164 break;
165 default:
166 return -EINVAL;
167 }
168 vlynq_reg_write(dev->remote->int_device[virq >> 2], val);
169 return 0;
170 }
171
172 static void vlynq_local_ack(unsigned int irq)
173 {
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);
180 }
181
182 static void vlynq_remote_ack(unsigned int irq)
183 {
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);
190 }
191
192 #warning FIXME: process one irq per call
193 static irqreturn_t vlynq_irq(int irq, void *dev_id)
194 {
195 struct vlynq_device *dev = dev_id;
196 u32 status;
197 int virq = 0;
198
199 status = vlynq_reg_read(dev->local->int_status);
200 vlynq_reg_write(dev->local->int_status, status);
201
202 if (unlikely(!status))
203 spurious_interrupt();
204
205 while (status) {
206 if (status & 1)
207 do_IRQ(dev->irq_start + virq);
208 status >>= 1;
209 virq++;
210 }
211
212 return IRQ_HANDLED;
213 }
214
215 static struct irq_chip vlynq_irq_chip = {
216 .name = "vlynq",
217 .unmask = vlynq_irq_unmask,
218 .mask = vlynq_irq_mask,
219 .set_type = vlynq_irq_type,
220 };
221
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,
227 };
228
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,
234 };
235
236 static int vlynq_setup_irq(struct vlynq_device *dev)
237 {
238 u32 val;
239 int i;
240
241 if (dev->local_irq == dev->remote_irq) {
242 printk(KERN_ERR
243 "%s: local vlynq irq should be different from remote\n",
244 dev->dev.bus_id);
245 return -EINVAL;
246 }
247
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));
252
253 /* Now setup interrupts */
254 val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
255 val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
256 VLYNQ_CTRL_INT2CFG;
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);
260
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);
266
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,
270 &vlynq_local_chip,
271 handle_level_irq);
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,
275 &vlynq_local_chip,
276 handle_level_irq);
277 set_irq_chip_data(dev->irq_start + i, dev);
278 } else {
279 set_irq_chip(dev->irq_start + i, &vlynq_irq_chip);
280 set_irq_chip_data(dev->irq_start + i, dev);
281 vlynq_reg_write(dev->remote->int_device[i >> 2], 0);
282 }
283 }
284
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);
287 return -EAGAIN;
288 }
289
290 return 0;
291 }
292
293 static void vlynq_free_irq(struct vlynq_device *dev)
294 {
295 free_irq(dev->irq, dev);
296 }
297
298 static void vlynq_device_release(struct device *dev)
299 {
300 struct vlynq_device *vdev = to_vlynq_device(dev);
301 kfree(vdev);
302 }
303
304 static int vlynq_device_probe(struct device *dev)
305 {
306 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
307 if (drv->probe)
308 return drv->probe(to_vlynq_device(dev));
309 return 0;
310 }
311
312 static int vlynq_device_remove(struct device *dev)
313 {
314 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
315 if (drv->remove)
316 return drv->remove(to_vlynq_device(dev));
317 return 0;
318 }
319
320 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
321 {
322 driver->driver.name = driver->name;
323 driver->driver.bus = &vlynq_bus_type;
324 return driver_register(&driver->driver);
325 }
326 EXPORT_SYMBOL(__vlynq_register_driver);
327
328 void vlynq_unregister_driver(struct vlynq_driver *driver)
329 {
330 driver_unregister(&driver->driver);
331 }
332 EXPORT_SYMBOL(vlynq_unregister_driver);
333
334 int vlynq_device_enable(struct vlynq_device *dev)
335 {
336 int i, result;
337 struct plat_vlynq_ops *ops = dev->dev.platform_data;
338
339 result = ops->on(dev);
340 if (result)
341 return result;
342
343 switch (dev->divisor) {
344 case vlynq_div_auto:
345 /* First try locally supplied clock */
346 vlynq_reg_write(dev->remote->control, 0);
347 for (i = vlynq_ldiv1; i <= vlynq_ldiv8; i++) {
348 vlynq_reg_write(dev->local->control,
349 VLYNQ_CTRL_CLOCK_INT |
350 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1));
351 if (vlynq_linked(dev)) {
352 printk(KERN_DEBUG
353 "%s: using local clock divisor %d\n",
354 dev->dev.bus_id, i - vlynq_ldiv1 + 1);
355 return vlynq_setup_irq(dev);
356 }
357 }
358 /* Then remotely supplied clock */
359 vlynq_reg_write(dev->local->control, 0);
360 for (i = vlynq_rdiv1; i <= vlynq_rdiv8; i++) {
361 vlynq_reg_write(dev->remote->control,
362 VLYNQ_CTRL_CLOCK_INT |
363 VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1));
364 if (vlynq_linked(dev)) {
365 printk(KERN_DEBUG
366 "%s: using remote clock divisor %d\n",
367 dev->dev.bus_id, i - vlynq_rdiv1 + 1);
368 return vlynq_setup_irq(dev);
369 }
370 }
371 /* At last, externally supplied clock */
372 vlynq_reg_write(dev->remote->control, 0);
373 if (vlynq_linked(dev)) {
374 printk(KERN_DEBUG "%s: using external clock\n",
375 dev->dev.bus_id);
376 return vlynq_setup_irq(dev);
377 }
378 break;
379 case vlynq_ldiv1: case vlynq_ldiv2: case vlynq_ldiv3: case vlynq_ldiv4:
380 case vlynq_ldiv5: case vlynq_ldiv6: case vlynq_ldiv7: case vlynq_ldiv8:
381 vlynq_reg_write(dev->remote->control, 0);
382 vlynq_reg_write(dev->local->control,
383 VLYNQ_CTRL_CLOCK_INT |
384 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
385 vlynq_ldiv1));
386 if (vlynq_linked(dev)) {
387 printk(KERN_DEBUG
388 "%s: using local clock divisor %d\n",
389 dev->dev.bus_id, dev->divisor - vlynq_ldiv1 + 1);
390 return vlynq_setup_irq(dev);
391 }
392 break;
393 case vlynq_rdiv1: case vlynq_rdiv2: case vlynq_rdiv3: case vlynq_rdiv4:
394 case vlynq_rdiv5: case vlynq_rdiv6: case vlynq_rdiv7: case vlynq_rdiv8:
395 vlynq_reg_write(dev->local->control, 0);
396 vlynq_reg_write(dev->remote->control,
397 VLYNQ_CTRL_CLOCK_INT |
398 VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
399 vlynq_rdiv1));
400 if (vlynq_linked(dev)) {
401 printk(KERN_DEBUG
402 "%s: using remote clock divisor %d\n",
403 dev->dev.bus_id, dev->divisor - vlynq_rdiv1 + 1);
404 return vlynq_setup_irq(dev);
405 }
406 break;
407 case vlynq_div_external:
408 vlynq_reg_write(dev->local->control, 0);
409 vlynq_reg_write(dev->remote->control, 0);
410 if (vlynq_linked(dev)) {
411 printk(KERN_DEBUG "%s: using external clock\n",
412 dev->dev.bus_id);
413 return vlynq_setup_irq(dev);
414 }
415 break;
416 }
417
418 return -ENODEV;
419 }
420
421 void vlynq_device_disable(struct vlynq_device *dev)
422 {
423 struct plat_vlynq_ops *ops = dev->dev.platform_data;
424
425 vlynq_free_irq(dev);
426 ops->off(dev);
427 }
428
429 u32 vlynq_remote_id(struct vlynq_device *dev)
430 {
431 return vlynq_reg_read(dev->remote->chip);
432 }
433
434 void vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
435 struct vlynq_mapping *mapping)
436 {
437 int i;
438
439 vlynq_reg_write(dev->local->tx_offset, tx_offset);
440 for (i = 0; i < 4; i++) {
441 vlynq_reg_write(dev->local->rx_mapping[i].offset,
442 mapping[i].offset);
443 vlynq_reg_write(dev->local->rx_mapping[i].size,
444 mapping[i].size);
445 }
446 }
447
448 void vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
449 struct vlynq_mapping *mapping)
450 {
451 int i;
452
453 vlynq_reg_write(dev->remote->tx_offset, tx_offset);
454 for (i = 0; i < 4; i++) {
455 vlynq_reg_write(dev->remote->rx_mapping[i].offset,
456 mapping[i].offset);
457 vlynq_reg_write(dev->remote->rx_mapping[i].size,
458 mapping[i].size);
459 }
460 }
461
462 int vlynq_virq_to_irq(struct vlynq_device *dev, int virq)
463 {
464 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
465 return -EINVAL;
466
467 return dev->irq_start + virq;
468 }
469
470 int vlynq_irq_to_virq(struct vlynq_device *dev, int irq)
471 {
472 if ((irq < dev->irq_start) || (irq >= dev->irq_start + PER_DEVICE_IRQS))
473 return -EINVAL;
474
475 return irq - dev->irq_start;
476 }
477
478 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
479 {
480 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
481 return -EINVAL;
482
483 if (virq == dev->remote_irq)
484 return -EINVAL;
485
486 dev->local_irq = virq;
487
488 return 0;
489 }
490
491 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
492 {
493 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
494 return -EINVAL;
495
496 if (virq == dev->local_irq)
497 return -EINVAL;
498
499 dev->remote_irq = virq;
500
501 return 0;
502 }
503
504 static int vlynq_probe(struct platform_device *pdev)
505 {
506 struct vlynq_device *dev;
507 struct resource *regs_res, *mem_res, *irq_res;
508 int len, result;
509
510 if (strcmp(pdev->name, "vlynq"))
511 return -ENODEV;
512
513 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
514 if (!regs_res)
515 return -ENODEV;
516
517 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
518 if (!mem_res)
519 return -ENODEV;
520
521 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
522 if (!irq_res)
523 return -ENODEV;
524
525 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
526 if (!dev) {
527 printk(KERN_ERR
528 "vlynq: failed to allocate device structure\n");
529 return -ENOMEM;
530 }
531
532 dev->id = pdev->id;
533 dev->dev.bus = &vlynq_bus_type;
534 dev->dev.parent = &pdev->dev;
535 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "vlynq%d", dev->id);
536 dev->dev.bus_id[BUS_ID_SIZE - 1] = 0;
537 dev->dev.platform_data = pdev->dev.platform_data;
538 dev->dev.release = vlynq_device_release;
539
540 dev->regs_start = regs_res->start;
541 dev->regs_end = regs_res->end;
542 dev->mem_start = mem_res->start;
543 dev->mem_end = mem_res->end;
544
545 len = regs_res->end - regs_res->start;
546 if (!request_mem_region(regs_res->start, len, dev->dev.bus_id)) {
547 printk(KERN_ERR "%s: Can't request vlynq registers\n",
548 dev->dev.bus_id);
549 result = -ENXIO;
550 goto fail_request;
551 }
552
553 dev->local = ioremap(regs_res->start, len);
554 if (!dev->local) {
555 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
556 dev->dev.bus_id);
557 result = -ENXIO;
558 goto fail_remap;
559 }
560
561 dev->remote = (struct vlynq_regs *)((u32)dev->local +
562 VLYNQ_REMOTE_OFFSET);
563
564 dev->irq = platform_get_irq_byname(pdev, "irq");
565 dev->irq_start = irq_res->start;
566 dev->irq_end = irq_res->end;
567 dev->local_irq = 31;
568 dev->remote_irq = 30;
569
570 if (device_register(&dev->dev))
571 goto fail_register;
572 platform_set_drvdata(pdev, dev);
573
574 printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
575 dev->dev.bus_id, (void *)dev->regs_start, dev->irq,
576 (void *)dev->mem_start);
577
578 return 0;
579
580 fail_register:
581 iounmap(dev->local);
582 fail_remap:
583 fail_request:
584 release_mem_region(regs_res->start, len);
585 kfree(dev);
586 return result;
587 }
588
589 static int vlynq_remove(struct platform_device *pdev)
590 {
591 struct vlynq_device *dev = platform_get_drvdata(pdev);
592
593 device_unregister(&dev->dev);
594 iounmap(dev->local);
595 release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
596
597 kfree(dev);
598
599 return 0;
600 }
601
602 static struct platform_driver vlynq_driver = {
603 .driver.name = "vlynq",
604 .probe = vlynq_probe,
605 .remove = vlynq_remove,
606 };
607
608 struct bus_type vlynq_bus_type = {
609 .name = "vlynq",
610 .probe = vlynq_device_probe,
611 .remove = vlynq_device_remove,
612 };
613 EXPORT_SYMBOL(vlynq_bus_type);
614
615 #ifdef CONFIG_PCI
616 extern void vlynq_pci_init(void);
617 #endif
618 int __init vlynq_init(void)
619 {
620 int res = 0;
621
622 res = bus_register(&vlynq_bus_type);
623 if (res)
624 goto fail_bus;
625
626 res = platform_driver_register(&vlynq_driver);
627 if (res)
628 goto fail_platform;
629
630 #ifdef CONFIG_PCI
631 vlynq_pci_init();
632 #endif
633
634 return 0;
635
636 fail_platform:
637 bus_unregister(&vlynq_bus_type);
638 fail_bus:
639 return res;
640 }
641
642 /* Add this back when vlynq-pci crap is gone */
643 #if 0
644 void __devexit vlynq_exit(void)
645 {
646 platform_driver_unregister(&vlynq_driver);
647 bus_unregister(&vlynq_bus_type);
648 }
649 #endif
650
651 subsys_initcall(vlynq_init);