fix ar7 compile
[openwrt.git] / target / linux / ar7-2.6 / files / arch / mips / ar7 / vlynq.c
1 /*
2 * $Id$
3 *
4 * Copyright (C) 2006, 2007 OpenWrt.org
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <linux/init.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/string.h>
25 #include <linux/delay.h>
26 #include <linux/device.h>
27 #include <linux/ioport.h>
28 #include <linux/errno.h>
29 #include <linux/platform_device.h>
30 #include <linux/irq.h>
31 #include <linux/interrupt.h>
32 #include <linux/device.h>
33 #include <asm/addrspace.h>
34 #include <asm/io.h>
35 #include <asm/ar7/vlynq.h>
36
37 #define PER_DEVICE_IRQS 32
38
39 #define VLYNQ_CTRL_PM_ENABLE 0x80000000
40 #define VLYNQ_CTRL_CLOCK_INT 0x00008000
41 #define VLYNQ_CTRL_CLOCK_DIV(x) ((x & 7) << 16)
42 #define VLYNQ_CTRL_INT_LOCAL 0x00004000
43 #define VLYNQ_CTRL_INT_ENABLE 0x00002000
44 #define VLYNQ_CTRL_INT_VECTOR(x) ((x & 0x1f) << 8)
45 #define VLYNQ_CTRL_INT2CFG 0x00000080
46 #define VLYNQ_CTRL_RESET 0x00000001
47
48 #define VLYNQ_STATUS_RERROR 0x00000100
49 #define VLYNQ_STATUS_LERROR 0x00000080
50 #define VLYNQ_STATUS_LINK 0x00000001
51
52 #define VINT_ENABLE 0x00000100
53 #define VINT_TYPE_EDGE 0x00000080
54 #define VINT_LEVEL_LOW 0x00000040
55 #define VINT_VECTOR(x) (x & 0x1f)
56 #define VINT_OFFSET(irq) (8 * ((irq) % 4))
57
58 #define VLYNQ_AUTONEGO_V2 0x00010000
59
60 struct vlynq_regs {
61 volatile u32 revision;
62 volatile u32 control;
63 volatile u32 status;
64 volatile u32 int_prio;
65 volatile u32 int_status;
66 volatile u32 int_pending;
67 volatile u32 int_ptr;
68 volatile u32 tx_offset;
69 volatile struct vlynq_mapping rx_mapping[4];
70 volatile u32 chip;
71 volatile u32 autonego;
72 volatile u32 unused[6];
73 volatile u32 int_device[8];
74 } __attribute__ ((packed));
75
76 #ifdef VLYNQ_DEBUG
77 static void vlynq_dump_regs(struct vlynq_device *dev)
78 {
79 int i;
80 printk("VLYNQ local=%p remote=%p\n", dev->local, dev->remote);
81 for (i = 0; i < 32; i++) {
82 printk("VLYNQ: local %d: %08x\n", i + 1, ((u32 *)dev->local)[i]);
83 printk("VLYNQ: remote %d: %08x\n", i + 1, ((u32 *)dev->remote)[i]);
84 }
85 }
86
87 static void vlynq_dump_mem(u32 *base, int count)
88 {
89 int i;
90 for (i = 0; i < (count + 3) / 4; i++) {
91 if (i % 4 == 0) printk("\nMEM[0x%04x]:", i * 4);
92 printk(" 0x%08x", *(base + i));
93 }
94 printk("\n");
95 }
96 #endif
97
98 int vlynq_linked(struct vlynq_device *dev)
99 {
100 int i;
101 for (i = 0; i < 10; i++)
102 if (dev->local->status & VLYNQ_STATUS_LINK) {
103 printk("%s: linked\n", dev->dev.bus_id);
104 return 1;
105 } else {
106 mdelay(1);
107 }
108 return 0;
109 }
110
111 static void vlynq_irq_unmask(unsigned int irq)
112 {
113 volatile u32 val;
114 struct vlynq_device *dev = irq_desc[irq].chip_data;
115 int virq;
116
117 BUG_ON(!dev);
118 virq = irq - dev->irq_start;
119 val = dev->remote->int_device[virq >> 2];
120 val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
121 dev->remote->int_device[virq >> 2] = val;
122 }
123
124 static void vlynq_irq_mask(unsigned int irq)
125 {
126 volatile u32 val;
127 struct vlynq_device *dev = irq_desc[irq].chip_data;
128 int virq;
129
130 BUG_ON(!dev);
131 virq = irq - dev->irq_start;
132 val = dev->remote->int_device[virq >> 2];
133 val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
134 dev->remote->int_device[virq >> 2] = val;
135 }
136
137 static int vlynq_irq_type(unsigned int irq, unsigned int flow_type)
138 {
139 volatile u32 val;
140 struct vlynq_device *dev = irq_desc[irq].chip_data;
141 int virq;
142
143 BUG_ON(!dev);
144 virq = irq - dev->irq_start;
145 val = dev->remote->int_device[virq >> 2];
146 switch (flow_type & IRQ_TYPE_SENSE_MASK) {
147 case IRQ_TYPE_EDGE_RISING:
148 case IRQ_TYPE_EDGE_FALLING:
149 case IRQ_TYPE_EDGE_BOTH:
150 val |= VINT_TYPE_EDGE << VINT_OFFSET(virq);
151 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
152 break;
153 case IRQ_TYPE_LEVEL_HIGH:
154 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
155 val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
156 break;
157 case IRQ_TYPE_LEVEL_LOW:
158 val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
159 val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
160 break;
161 default:
162 return -EINVAL;
163 }
164 dev->remote->int_device[virq >> 2] = val;
165 return 0;
166 }
167
168 static irqreturn_t vlynq_irq(int irq, void *dev_id)
169 {
170 struct vlynq_device *dev = dev_id;
171 u32 status, ack;
172 int virq = 0;
173
174 status = dev->local->int_status;
175 dev->local->int_status = status;
176
177 if (status & (1 << dev->local_irq)) { /* Local vlynq IRQ. Ack */
178 ack = dev->local->status;
179 dev->local->status = ack;
180 }
181
182 if (status & (1 << dev->remote_irq)) { /* Remote vlynq IRQ. Ack */
183 ack = dev->remote->status;
184 dev->remote->status = ack;
185 }
186
187 status &= ~((1 << dev->local_irq) | (1 << dev->remote_irq));
188 while (status) {
189 if (status & 1) /* Remote device IRQ. Pass. */
190 do_IRQ(dev->irq_start + virq);
191 status >>= 1;
192 virq++;
193 }
194
195 return IRQ_HANDLED;
196 }
197
198 static struct irq_chip vlynq_irq_chip = {
199 .name = "vlynq",
200 .unmask = vlynq_irq_unmask,
201 .mask = vlynq_irq_mask,
202 .set_type = vlynq_irq_type,
203 };
204
205 static int vlynq_setup_irq(struct vlynq_device *dev)
206 {
207 u32 val;
208 int i;
209
210 if (dev->local_irq == dev->remote_irq) {
211 printk("%s: local vlynq irq should be different from remote\n",
212 dev->dev.bus_id);
213 return -EINVAL;
214 }
215
216 val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
217 val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
218 VLYNQ_CTRL_INT2CFG;
219 dev->local->int_ptr = 0x14;
220 dev->local->control |= val;
221
222 val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
223 val |= VLYNQ_CTRL_INT_ENABLE;
224 dev->remote->int_ptr = 0x14;
225 dev->remote->control |= val;
226
227 for (i = 0; i < PER_DEVICE_IRQS; i++) {
228 if ((i == dev->local_irq) || (i == dev->remote_irq))
229 continue;
230 irq_desc[dev->irq_start + i].status = IRQ_DISABLED;
231 irq_desc[dev->irq_start + i].action = 0;
232 irq_desc[dev->irq_start + i].depth = 1;
233 irq_desc[dev->irq_start + i].chip = &vlynq_irq_chip;
234 irq_desc[dev->irq_start + i].chip_data = dev;
235 dev->remote->int_device[i >> 2] = 0;
236 }
237
238 if (request_irq(dev->irq, vlynq_irq, SA_SHIRQ, "AR7 VLYNQ", dev)) {
239 printk("%s: request_irq failed\n", dev->dev.bus_id);
240 return -EAGAIN;
241 }
242
243 return 0;
244 }
245
246 static void vlynq_free_irq(struct vlynq_device *dev)
247 {
248 free_irq(dev->irq, dev);
249 }
250
251 static void vlynq_device_release(struct device *dev)
252 {
253 struct vlynq_device *vdev = to_vlynq_device(dev);
254 kfree(vdev);
255 }
256
257 static int vlynq_device_probe(struct device *dev)
258 {
259 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
260 if (drv->probe)
261 return drv->probe(to_vlynq_device(dev));
262 return 0;
263 }
264
265 static int vlynq_device_remove(struct device *dev)
266 {
267 struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
268 if (drv->remove)
269 return drv->remove(to_vlynq_device(dev));
270 return 0;
271 }
272
273 int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
274 {
275 driver->driver.name = driver->name;
276 driver->driver.bus = &vlynq_bus_type;
277 /* driver->driver.owner = owner;*/
278 return driver_register(&driver->driver);
279 }
280 EXPORT_SYMBOL(__vlynq_register_driver);
281
282 void vlynq_unregister_driver(struct vlynq_driver *driver)
283 {
284 driver_unregister(&driver->driver);
285 }
286 EXPORT_SYMBOL(vlynq_unregister_driver);
287
288 int vlynq_device_enable(struct vlynq_device *dev)
289 {
290 u32 val;
291 int result;
292 struct plat_vlynq_ops *ops = dev->dev.platform_data;
293
294 result = ops->on(dev);
295 if (result)
296 return result;
297
298 dev->local->control = 0;
299 dev->remote->control = 0;
300
301 if (vlynq_linked(dev))
302 return vlynq_setup_irq(dev);
303
304 for (val = 0; val < 8; val++) {
305 dev->local->control = VLYNQ_CTRL_CLOCK_DIV(val) |
306 VLYNQ_CTRL_CLOCK_INT;
307 if (vlynq_linked(dev))
308 return vlynq_setup_irq(dev);
309 }
310
311 return -ENODEV;
312 }
313
314 void vlynq_device_disable(struct vlynq_device *dev)
315 {
316 struct plat_vlynq_ops *ops = dev->dev.platform_data;
317
318 vlynq_free_irq(dev);
319 ops->off(dev);
320 }
321
322 u32 vlynq_local_id(struct vlynq_device *dev)
323 {
324 return dev->local->chip;
325 }
326
327 u32 vlynq_remote_id(struct vlynq_device *dev)
328 {
329 return dev->remote->chip;
330 }
331
332 void vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
333 struct vlynq_mapping *mapping)
334 {
335 int i;
336
337 dev->local->tx_offset = tx_offset;
338 for (i = 0; i < 4; i++) {
339 dev->local->rx_mapping[i].offset = mapping[i].offset;
340 dev->local->rx_mapping[i].size = mapping[i].size;
341 }
342 }
343
344 void vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
345 struct vlynq_mapping *mapping)
346 {
347 int i;
348
349 dev->remote->tx_offset = tx_offset;
350 for (i = 0; i < 4; i++) {
351 dev->remote->rx_mapping[i].offset = mapping[i].offset;
352 dev->remote->rx_mapping[i].size = mapping[i].size;
353 }
354 }
355
356 int vlynq_virq_to_irq(struct vlynq_device *dev, int virq)
357 {
358 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
359 return -EINVAL;
360
361 if ((virq == dev->local_irq) || (virq == dev->remote_irq))
362 return -EINVAL;
363
364 return dev->irq_start + virq;
365 }
366
367 int vlynq_irq_to_virq(struct vlynq_device *dev, int irq)
368 {
369 if ((irq < dev->irq_start) || (irq >= dev->irq_start + PER_DEVICE_IRQS))
370 return -EINVAL;
371
372 return irq - dev->irq_start;
373 }
374
375 int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
376 {
377 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
378 return -EINVAL;
379
380 if (virq == dev->remote_irq)
381 return -EINVAL;
382
383 dev->local_irq = virq;
384
385 return 0;
386 }
387
388 int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
389 {
390 if ((virq < 0) || (virq >= PER_DEVICE_IRQS))
391 return -EINVAL;
392
393 if (virq == dev->local_irq)
394 return -EINVAL;
395
396 dev->remote_irq = virq;
397
398 return 0;
399 }
400
401 static int vlynq_probe(struct platform_device *pdev)
402 {
403 struct vlynq_device *dev;
404 struct resource *regs_res, *mem_res, *irq_res;
405 int len, result;
406
407 if (strcmp(pdev->name, "vlynq"))
408 return -ENODEV;
409
410 regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
411 if (!regs_res)
412 return -ENODEV;
413
414 mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
415 if (!mem_res)
416 return -ENODEV;
417
418 irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
419 if (!irq_res)
420 return -ENODEV;
421
422 dev = kmalloc(sizeof(struct vlynq_device), GFP_KERNEL);
423 if (!dev) {
424 printk(KERN_ERR "vlynq: failed to allocate device structure\n");
425 return -ENOMEM;
426 }
427
428 memset(dev, 0, sizeof(struct vlynq_device));
429
430 dev->id = pdev->id;
431 dev->dev.bus = &vlynq_bus_type;
432 dev->dev.parent = &pdev->dev;
433 snprintf(dev->dev.bus_id, BUS_ID_SIZE, "vlynq%d", dev->id);
434 dev->dev.bus_id[BUS_ID_SIZE - 1] = 0;
435 dev->dev.platform_data = pdev->dev.platform_data;
436 dev->dev.release = vlynq_device_release;
437
438 dev->regs_start = regs_res->start;
439 dev->regs_end = regs_res->end;
440 dev->mem_start = mem_res->start;
441 dev->mem_end = mem_res->end;
442
443 len = regs_res->end - regs_res->start;
444 if (!request_mem_region(regs_res->start, len, dev->dev.bus_id)) {
445 printk("%s: Can't request vlynq registers\n", dev->dev.bus_id);
446 result = -ENXIO;
447 goto fail_request;
448 }
449
450 dev->local = ioremap_nocache(regs_res->start, len);
451 if (!dev->local) {
452 printk("%s: Can't remap vlynq registers\n", dev->dev.bus_id);
453 result = -ENXIO;
454 goto fail_remap;
455 }
456
457 dev->remote = (struct vlynq_regs *)((u32)dev->local + 128);
458
459 dev->irq = platform_get_irq_byname(pdev, "irq");
460 dev->irq_start = irq_res->start;
461 dev->irq_end = irq_res->end;
462 dev->local_irq = 31;
463 dev->remote_irq = 30;
464
465 if (device_register(&dev->dev))
466 goto fail_register;
467 platform_set_drvdata(pdev, dev);
468
469 printk("%s: regs 0x%p, irq %d, mem 0x%p\n",
470 dev->dev.bus_id, (void *)dev->regs_start, dev->irq,
471 (void *)dev->mem_start);
472
473 return 0;
474
475 fail_register:
476 fail_remap:
477 iounmap(dev->local);
478 fail_request:
479 release_mem_region(regs_res->start, len);
480 kfree(dev);
481 return result;
482 }
483
484 static int vlynq_remove(struct platform_device *pdev)
485 {
486 struct vlynq_device *dev = platform_get_drvdata(pdev);
487
488 device_unregister(&dev->dev);
489 release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
490
491 kfree(dev);
492
493 return 0;
494 }
495
496 static struct platform_driver vlynq_driver = {
497 .driver.name = "vlynq",
498 .probe = vlynq_probe,
499 .remove = vlynq_remove,
500 };
501
502 struct bus_type vlynq_bus_type = {
503 .name = "vlynq",
504 .probe = vlynq_device_probe,
505 .remove = vlynq_device_remove,
506 };
507 EXPORT_SYMBOL(vlynq_bus_type);
508
509 #ifdef CONFIG_PCI
510 extern void vlynq_pci_init(void);
511 #endif
512 int __init vlynq_init(void)
513 {
514 int res = 0;
515
516 res = bus_register(&vlynq_bus_type);
517 if (res)
518 goto fail_bus;
519
520 res = platform_driver_register(&vlynq_driver);
521 if (res)
522 goto fail_platform;
523
524 #ifdef CONFIG_PCI
525 vlynq_pci_init();
526 #endif
527
528 return 0;
529
530 fail_platform:
531 bus_unregister(&vlynq_bus_type);
532 fail_bus:
533 return res;
534 }
535
536 /*
537 void __devexit vlynq_exit(void)
538 {
539 platform_driver_unregister(&vlynq_driver);
540 bus_unregister(&vlynq_bus_type);
541 }
542 */
543
544
545 subsys_initcall(vlynq_init);
This page took 0.078412 seconds and 5 git commands to generate.