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