more AR7 cleanups & fixes
[openwrt.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         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(KERN_ERR "%s: Can't request vlynq registers\n",
455                                                         dev->dev.bus_id);
456                 result = -ENXIO;
457                 goto fail_request;
458         }
459
460         dev->local = ioremap_nocache(regs_res->start, len);
461         if (!dev->local) {
462                 printk(KERN_ERR "%s: Can't remap vlynq registers\n",
463                                                         dev->dev.bus_id);
464                 result = -ENXIO;
465                 goto fail_remap;
466         }
467
468         dev->remote = (struct vlynq_regs *)((u32)dev->local + 128);
469
470         dev->irq = platform_get_irq_byname(pdev, "irq");
471         dev->irq_start = irq_res->start;
472         dev->irq_end = irq_res->end;
473         dev->local_irq = 31;
474         dev->remote_irq = 30;
475
476         if (device_register(&dev->dev))
477                 goto fail_register;
478         platform_set_drvdata(pdev, dev);
479
480         printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
481                dev->dev.bus_id, (void *)dev->regs_start, dev->irq,
482                (void *)dev->mem_start);
483
484         return 0;
485
486 fail_register:
487 fail_remap:
488         iounmap(dev->local);
489 fail_request:
490         release_mem_region(regs_res->start, len);
491         kfree(dev);
492         return result;
493 }
494
495 static int vlynq_remove(struct platform_device *pdev)
496 {
497         struct vlynq_device *dev = platform_get_drvdata(pdev);
498
499         device_unregister(&dev->dev);
500         release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
501
502         kfree(dev);
503
504         return 0;
505 }
506
507 static struct platform_driver vlynq_driver = {
508         .driver.name = "vlynq",
509         .probe = vlynq_probe,
510         .remove = vlynq_remove,
511 };
512
513 struct bus_type vlynq_bus_type = {
514         .name = "vlynq",
515         .probe = vlynq_device_probe,
516         .remove = vlynq_device_remove,
517 };
518 EXPORT_SYMBOL(vlynq_bus_type);
519
520 #ifdef CONFIG_PCI
521 extern void vlynq_pci_init(void);
522 #endif
523 static int __init vlynq_init(void)
524 {
525         int res = 0;
526
527         res = bus_register(&vlynq_bus_type);
528         if (res)
529                 goto fail_bus;
530
531         res = platform_driver_register(&vlynq_driver);
532         if (res)
533                 goto fail_platform;
534
535 #ifdef CONFIG_PCI
536         vlynq_pci_init();
537 #endif
538
539         return 0;
540
541 fail_platform:
542         bus_unregister(&vlynq_bus_type);
543 fail_bus:
544         return res;
545 }
546
547 /*
548 void __devexit vlynq_exit(void)
549 {
550         platform_driver_unregister(&vlynq_driver);
551         bus_unregister(&vlynq_bus_type);
552 }
553 */
554
555
556 subsys_initcall(vlynq_init);