Edimax BR-6104K/KP specific fixes * add profile for Edimax BR-6104KP which contains...
[openwrt/staging/lynxis/omap.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_remote_chip,
276 handle_level_irq);
277 set_irq_chip_data(dev->irq_start + i, dev);
278 } else {
279 set_irq_chip_and_handler(dev->irq_start + i,
280 &vlynq_irq_chip,
281 handle_simple_irq);
282 set_irq_chip_data(dev->irq_start + i, dev);
283 vlynq_reg_write(dev->remote->int_device[i >> 2], 0);
284 }
285 }
286
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);
289 return -EAGAIN;
290 }
291
292 return 0;
293 }
294
295 static void vlynq_free_irq(struct vlynq_device *dev)
296 {
297 free_irq(dev->irq, dev);
298 }
299
300 static void vlynq_device_release(struct device *dev)
301 {
302 struct vlynq_device *vdev = to_vlynq_device(dev);
303 kfree(vdev);
304 }
305
306 static int vlynq_device_probe(struct device *dev)
307 {
308 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
309 if (drv->probe)
310 return drv->probe(to_vlynq_device(dev));
311 return 0;
312 }
313
314 static int vlynq_device_remove(struct device *dev)
315 {
316 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
317 if (drv->remove)
318 return drv->remove(to_vlynq_device(dev));
319 return 0;
320 }
321
322 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
323 {
324 driver->driver.name = driver->name;
325 driver->driver.bus = &vlynq_bus_type;
326 return driver_register(&driver->driver);
327 }
328 EXPORT_SYMBOL(__vlynq_register_driver);
329
330 void vlynq_unregister_driver(struct vlynq_driver *driver)
331 {
332 driver_unregister(&driver->driver);
333 }
334 EXPORT_SYMBOL(vlynq_unregister_driver);
335
336 int vlynq_device_enable(struct vlynq_device *dev)
337 {
338 int i, result;
339 struct plat_vlynq_ops *ops = dev->dev.platform_data;
340
341 result = ops->on(dev);
342 if (result)
343 return result;
344
345 switch (dev->divisor) {
346 case vlynq_div_auto:
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)) {
354 printk(KERN_DEBUG
355 "%s: using local clock divisor %d\n",
356 dev->dev.bus_id, i - vlynq_ldiv1 + 1);
357 return vlynq_setup_irq(dev);
358 }
359 }
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)) {
367 printk(KERN_DEBUG
368 "%s: using remote clock divisor %d\n",
369 dev->dev.bus_id, i - vlynq_rdiv1 + 1);
370 return vlynq_setup_irq(dev);
371 }
372 }
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",
377 dev->dev.bus_id);
378 return vlynq_setup_irq(dev);
379 }
380 break;
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 -
387 vlynq_ldiv1));
388 if (vlynq_linked(dev)) {
389 printk(KERN_DEBUG
390 "%s: using local clock divisor %d\n",
391 dev->dev.bus_id, dev->divisor - vlynq_ldiv1 + 1);
392 return vlynq_setup_irq(dev);
393 }
394 break;
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 -
401 vlynq_rdiv1));
402 if (vlynq_linked(dev)) {
403 printk(KERN_DEBUG
404 "%s: using remote clock divisor %d\n",
405 dev->dev.bus_id, dev->divisor - vlynq_rdiv1 + 1);
406 return vlynq_setup_irq(dev);
407 }
408 break;
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",
414 dev->dev.bus_id);
415 return vlynq_setup_irq(dev);
416 }
417 break;
418 }
419
420 return -ENODEV;
421 }
422
423 void vlynq_device_disable(struct vlynq_device *dev)
424 {
425 struct plat_vlynq_ops *ops = dev->dev.platform_data;
426
427 vlynq_free_irq(dev);
428 ops->off(dev);
429 }
430
431 u32 vlynq_remote_id(struct vlynq_device *dev)
432 {
433 return vlynq_reg_read(dev->remote->chip);
434 }
435
436 void vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
437 struct vlynq_mapping *mapping)
438 {
439 int i;
440
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,
444 mapping[i].offset);
445 vlynq_reg_write(dev->local->rx_mapping[i].size,
446 mapping[i].size);
447 }
448 }
449
450 void vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
451 struct vlynq_mapping *mapping)
452 {
453 int i;
454
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,
458 mapping[i].offset);
459 vlynq_reg_write(dev->remote->rx_mapping[i].size,
460 mapping[i].size);
461 }
462 }
463
464 int vlynq_virq_to_irq(struct vlynq_device *dev, int virq)
465 {
466 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
467 return -EINVAL;
468
469 return dev->irq_start + virq;
470 }
471
472 int vlynq_irq_to_virq(struct vlynq_device *dev, int irq)
473 {
474 if ((irq < dev->irq_start) || (irq >= dev->irq_start + PER_DEVICE_IRQS))
475 return -EINVAL;
476
477 return irq - dev->irq_start;
478 }
479
480 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
481 {
482 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
483 return -EINVAL;
484
485 if (virq == dev->remote_irq)
486 return -EINVAL;
487
488 dev->local_irq = virq;
489
490 return 0;
491 }
492
493 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
494 {
495 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
496 return -EINVAL;
497
498 if (virq == dev->local_irq)
499 return -EINVAL;
500
501 dev->remote_irq = virq;
502
503 return 0;
504 }
505
506 static int vlynq_probe(struct platform_device *pdev)
507 {
508 struct vlynq_device *dev;
509 struct resource *regs_res, *mem_res, *irq_res;
510 int len, result;
511
512 if (strcmp(pdev->name, "vlynq"))
513 return -ENODEV;
514
515 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
516 if (!regs_res)
517 return -ENODEV;
518
519 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
520 if (!mem_res)
521 return -ENODEV;
522
523 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
524 if (!irq_res)
525 return -ENODEV;
526
527 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
528 if (!dev) {
529 printk(KERN_ERR
530 "vlynq: failed to allocate device structure\n");
531 return -ENOMEM;
532 }
533
534 dev->id = pdev->id;
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;
541
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;
546
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",
550 dev->dev.bus_id);
551 result = -ENXIO;
552 goto fail_request;
553 }
554
555 dev->local = ioremap(regs_res->start, len);
556 if (!dev->local) {
557 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
558 dev->dev.bus_id);
559 result = -ENXIO;
560 goto fail_remap;
561 }
562
563 dev->remote = (struct vlynq_regs *)((u32)dev->local +
564 VLYNQ_REMOTE_OFFSET);
565
566 dev->irq = platform_get_irq_byname(pdev, "irq");
567 dev->irq_start = irq_res->start;
568 dev->irq_end = irq_res->end;
569 dev->local_irq = 31;
570 dev->remote_irq = 30;
571
572 if (device_register(&dev->dev))
573 goto fail_register;
574 platform_set_drvdata(pdev, dev);
575
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);
579
580 return 0;
581
582 fail_register:
583 iounmap(dev->local);
584 fail_remap:
585 fail_request:
586 release_mem_region(regs_res->start, len);
587 kfree(dev);
588 return result;
589 }
590
591 static int vlynq_remove(struct platform_device *pdev)
592 {
593 struct vlynq_device *dev = platform_get_drvdata(pdev);
594
595 device_unregister(&dev->dev);
596 iounmap(dev->local);
597 release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
598
599 kfree(dev);
600
601 return 0;
602 }
603
604 static struct platform_driver vlynq_driver = {
605 .driver.name = "vlynq",
606 .probe = vlynq_probe,
607 .remove = vlynq_remove,
608 };
609
610 struct bus_type vlynq_bus_type = {
611 .name = "vlynq",
612 .probe = vlynq_device_probe,
613 .remove = vlynq_device_remove,
614 };
615 EXPORT_SYMBOL(vlynq_bus_type);
616
617 #ifdef CONFIG_PCI
618 extern void vlynq_pci_init(void);
619 #endif
620 static int __init vlynq_init(void)
621 {
622 int res = 0;
623
624 res = bus_register(&vlynq_bus_type);
625 if (res)
626 goto fail_bus;
627
628 res = platform_driver_register(&vlynq_driver);
629 if (res)
630 goto fail_platform;
631
632 #ifdef CONFIG_PCI
633 vlynq_pci_init();
634 #endif
635
636 return 0;
637
638 fail_platform:
639 bus_unregister(&vlynq_bus_type);
640 fail_bus:
641 return res;
642 }
643
644 /* Add this back when vlynq-pci crap is gone */
645 #if 0
646 static void __devexit vlynq_exit(void)
647 {
648 platform_driver_unregister(&vlynq_driver);
649 bus_unregister(&vlynq_bus_type);
650 }
651 #endif
652
653 subsys_initcall(vlynq_init);