76dbd55ef4477625e281e279c7547e296746e72f
[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, mapping[i].offset);
442 vlynq_reg_write(dev->local->rx_mapping[i].size, mapping[i].size);
443 }
444 }
445
446 void vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
447 struct vlynq_mapping *mapping)
448 {
449 int i;
450
451 vlynq_reg_write(dev->remote->tx_offset, tx_offset);
452 for (i = 0; i < 4; i++) {
453 vlynq_reg_write(dev->remote->rx_mapping[i].offset, mapping[i].offset);
454 vlynq_reg_write(dev->remote->rx_mapping[i].size, mapping[i].size);
455 }
456 }
457
458 int vlynq_virq_to_irq(struct vlynq_device *dev, int virq)
459 {
460 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
461 return -EINVAL;
462
463 return dev->irq_start + virq;
464 }
465
466 int vlynq_irq_to_virq(struct vlynq_device *dev, int irq)
467 {
468 if ((irq < dev->irq_start) || (irq >= dev->irq_start + PER_DEVICE_IRQS))
469 return -EINVAL;
470
471 return irq - dev->irq_start;
472 }
473
474 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
475 {
476 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
477 return -EINVAL;
478
479 if (virq == dev->remote_irq)
480 return -EINVAL;
481
482 dev->local_irq = virq;
483
484 return 0;
485 }
486
487 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
488 {
489 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
490 return -EINVAL;
491
492 if (virq == dev->local_irq)
493 return -EINVAL;
494
495 dev->remote_irq = virq;
496
497 return 0;
498 }
499
500 static int vlynq_probe(struct platform_device *pdev)
501 {
502 struct vlynq_device *dev;
503 struct resource *regs_res, *mem_res, *irq_res;
504 int len, result;
505
506 if (strcmp(pdev->name, "vlynq"))
507 return -ENODEV;
508
509 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
510 if (!regs_res)
511 return -ENODEV;
512
513 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
514 if (!mem_res)
515 return -ENODEV;
516
517 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
518 if (!irq_res)
519 return -ENODEV;
520
521 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
522 if (!dev) {
523 printk(KERN_ERR
524 "vlynq: failed to allocate device structure\n");
525 return -ENOMEM;
526 }
527
528 dev->id = pdev->id;
529 dev->dev.bus = &vlynq_bus_type;
530 dev->dev.parent = &pdev->dev;
531 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "vlynq%d", dev->id);
532 dev->dev.bus_id[BUS_ID_SIZE - 1] = 0;
533 dev->dev.platform_data = pdev->dev.platform_data;
534 dev->dev.release = vlynq_device_release;
535
536 dev->regs_start = regs_res->start;
537 dev->regs_end = regs_res->end;
538 dev->mem_start = mem_res->start;
539 dev->mem_end = mem_res->end;
540
541 len = regs_res->end - regs_res->start;
542 if (!request_mem_region(regs_res->start, len, dev->dev.bus_id)) {
543 printk(KERN_ERR "%s: Can't request vlynq registers\n",
544 dev->dev.bus_id);
545 result = -ENXIO;
546 goto fail_request;
547 }
548
549 dev->local = ioremap(regs_res->start, len);
550 if (!dev->local) {
551 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
552 dev->dev.bus_id);
553 result = -ENXIO;
554 goto fail_remap;
555 }
556
557 dev->remote = (struct vlynq_regs *)((u32)dev->local +
558 VLYNQ_REMOTE_OFFSET);
559
560 dev->irq = platform_get_irq_byname(pdev, "irq");
561 dev->irq_start = irq_res->start;
562 dev->irq_end = irq_res->end;
563 dev->local_irq = 31;
564 dev->remote_irq = 30;
565
566 if (device_register(&dev->dev))
567 goto fail_register;
568 platform_set_drvdata(pdev, dev);
569
570 printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
571 dev->dev.bus_id, (void *)dev->regs_start, dev->irq,
572 (void *)dev->mem_start);
573
574 return 0;
575
576 fail_register:
577 iounmap(dev->local);
578 fail_remap:
579 fail_request:
580 release_mem_region(regs_res->start, len);
581 kfree(dev);
582 return result;
583 }
584
585 static int vlynq_remove(struct platform_device *pdev)
586 {
587 struct vlynq_device *dev = platform_get_drvdata(pdev);
588
589 device_unregister(&dev->dev);
590 iounmap(dev->local);
591 release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
592
593 kfree(dev);
594
595 return 0;
596 }
597
598 static struct platform_driver vlynq_driver = {
599 .driver.name = "vlynq",
600 .probe = vlynq_probe,
601 .remove = vlynq_remove,
602 };
603
604 struct bus_type vlynq_bus_type = {
605 .name = "vlynq",
606 .probe = vlynq_device_probe,
607 .remove = vlynq_device_remove,
608 };
609 EXPORT_SYMBOL(vlynq_bus_type);
610
611 #ifdef CONFIG_PCI
612 extern void vlynq_pci_init(void);
613 #endif
614 int __init vlynq_init(void)
615 {
616 int res = 0;
617
618 res = bus_register(&vlynq_bus_type);
619 if (res)
620 goto fail_bus;
621
622 res = platform_driver_register(&vlynq_driver);
623 if (res)
624 goto fail_platform;
625
626 #ifdef CONFIG_PCI
627 vlynq_pci_init();
628 #endif
629
630 return 0;
631
632 fail_platform:
633 bus_unregister(&vlynq_bus_type);
634 fail_bus:
635 return res;
636 }
637
638 /* Add this back when vlynq-pci crap is gone */
639 #if 0
640 void __devexit vlynq_exit(void)
641 {
642 platform_driver_unregister(&vlynq_driver);
643 bus_unregister(&vlynq_bus_type);
644 }
645 #endif
646
647 subsys_initcall(vlynq_init);