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