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