1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/export.h>
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/io.h>
26#include <linux/irq.h>
27#include <linux/irqchip.h>
28#include <linux/irqchip/chained_irq.h>
29#include <linux/irqdomain.h>
30#include <linux/of.h>
31#include <linux/of_address.h>
32#include <linux/of_irq.h>
33#include <linux/syscore_ops.h>
34#include <linux/device.h>
35#include <linux/amba/bus.h>
36#include <linux/irqchip/arm-vic.h>
37
38#include <asm/exception.h>
39#include <asm/irq.h>
40
41#define VIC_IRQ_STATUS 0x00
42#define VIC_FIQ_STATUS 0x04
43#define VIC_INT_SELECT 0x0c
44#define VIC_INT_SOFT 0x18
45#define VIC_INT_SOFT_CLEAR 0x1c
46#define VIC_PROTECT 0x20
47#define VIC_PL190_VECT_ADDR 0x30
48#define VIC_PL190_DEF_VECT_ADDR 0x34
49
50#define VIC_VECT_ADDR0 0x100
51#define VIC_VECT_CNTL0 0x200
52#define VIC_ITCR 0x300
53
54#define VIC_VECT_CNTL_ENABLE (1 << 5)
55
56#define VIC_PL192_VECT_ADDR 0xF00
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72struct vic_device {
73 void __iomem *base;
74 int irq;
75 u32 valid_sources;
76 u32 resume_sources;
77 u32 resume_irqs;
78 u32 int_select;
79 u32 int_enable;
80 u32 soft_int;
81 u32 protect;
82 struct irq_domain *domain;
83};
84
85
86static struct vic_device vic_devices[CONFIG_ARM_VIC_NR];
87
88static int vic_id;
89
90static void vic_handle_irq(struct pt_regs *regs);
91
92
93
94
95
96
97
98
99static void vic_init2(void __iomem *base)
100{
101 int i;
102
103 for (i = 0; i < 16; i++) {
104 void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
105 writel(VIC_VECT_CNTL_ENABLE | i, reg);
106 }
107
108 writel(32, base + VIC_PL190_DEF_VECT_ADDR);
109}
110
111#ifdef CONFIG_PM
112static void resume_one_vic(struct vic_device *vic)
113{
114 void __iomem *base = vic->base;
115
116 printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base);
117
118
119 vic_init2(base);
120
121 writel(vic->int_select, base + VIC_INT_SELECT);
122 writel(vic->protect, base + VIC_PROTECT);
123
124
125 writel(vic->int_enable, base + VIC_INT_ENABLE);
126 writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR);
127
128
129
130 writel(vic->soft_int, base + VIC_INT_SOFT);
131 writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR);
132}
133
134static void vic_resume(void)
135{
136 int id;
137
138 for (id = vic_id - 1; id >= 0; id--)
139 resume_one_vic(vic_devices + id);
140}
141
142static void suspend_one_vic(struct vic_device *vic)
143{
144 void __iomem *base = vic->base;
145
146 printk(KERN_DEBUG "%s: suspending vic at %p\n", __func__, base);
147
148 vic->int_select = readl(base + VIC_INT_SELECT);
149 vic->int_enable = readl(base + VIC_INT_ENABLE);
150 vic->soft_int = readl(base + VIC_INT_SOFT);
151 vic->protect = readl(base + VIC_PROTECT);
152
153
154
155
156 writel(vic->resume_irqs, base + VIC_INT_ENABLE);
157 writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR);
158}
159
160static int vic_suspend(void)
161{
162 int id;
163
164 for (id = 0; id < vic_id; id++)
165 suspend_one_vic(vic_devices + id);
166
167 return 0;
168}
169
170static struct syscore_ops vic_syscore_ops = {
171 .suspend = vic_suspend,
172 .resume = vic_resume,
173};
174
175
176
177
178
179
180
181
182static int __init vic_pm_init(void)
183{
184 if (vic_id > 0)
185 register_syscore_ops(&vic_syscore_ops);
186
187 return 0;
188}
189late_initcall(vic_pm_init);
190#endif
191
192static struct irq_chip vic_chip;
193
194static int vic_irqdomain_map(struct irq_domain *d, unsigned int irq,
195 irq_hw_number_t hwirq)
196{
197 struct vic_device *v = d->host_data;
198
199
200 if (!(v->valid_sources & (1 << hwirq)))
201 return -EPERM;
202 irq_set_chip_and_handler(irq, &vic_chip, handle_level_irq);
203 irq_set_chip_data(irq, v->base);
204 irq_set_probe(irq);
205 return 0;
206}
207
208
209
210
211
212
213
214static int handle_one_vic(struct vic_device *vic, struct pt_regs *regs)
215{
216 u32 stat, irq;
217 int handled = 0;
218
219 while ((stat = readl_relaxed(vic->base + VIC_IRQ_STATUS))) {
220 irq = ffs(stat) - 1;
221 handle_domain_irq(vic->domain, irq, regs);
222 handled = 1;
223 }
224
225 return handled;
226}
227
228static void vic_handle_irq_cascaded(struct irq_desc *desc)
229{
230 u32 stat, hwirq;
231 struct irq_chip *host_chip = irq_desc_get_chip(desc);
232 struct vic_device *vic = irq_desc_get_handler_data(desc);
233
234 chained_irq_enter(host_chip, desc);
235
236 while ((stat = readl_relaxed(vic->base + VIC_IRQ_STATUS))) {
237 hwirq = ffs(stat) - 1;
238 generic_handle_irq(irq_find_mapping(vic->domain, hwirq));
239 }
240
241 chained_irq_exit(host_chip, desc);
242}
243
244
245
246
247
248static void __exception_irq_entry vic_handle_irq(struct pt_regs *regs)
249{
250 int i, handled;
251
252 do {
253 for (i = 0, handled = 0; i < vic_id; ++i)
254 handled |= handle_one_vic(&vic_devices[i], regs);
255 } while (handled);
256}
257
258static const struct irq_domain_ops vic_irqdomain_ops = {
259 .map = vic_irqdomain_map,
260 .xlate = irq_domain_xlate_onetwocell,
261};
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278static void __init vic_register(void __iomem *base, unsigned int parent_irq,
279 unsigned int irq,
280 u32 valid_sources, u32 resume_sources,
281 struct device_node *node)
282{
283 struct vic_device *v;
284 int i;
285
286 if (vic_id >= ARRAY_SIZE(vic_devices)) {
287 printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__);
288 return;
289 }
290
291 v = &vic_devices[vic_id];
292 v->base = base;
293 v->valid_sources = valid_sources;
294 v->resume_sources = resume_sources;
295 set_handle_irq(vic_handle_irq);
296 vic_id++;
297
298 if (parent_irq) {
299 irq_set_chained_handler_and_data(parent_irq,
300 vic_handle_irq_cascaded, v);
301 }
302
303 v->domain = irq_domain_add_simple(node, fls(valid_sources), irq,
304 &vic_irqdomain_ops, v);
305
306 for (i = 0; i < fls(valid_sources); i++)
307 if (valid_sources & (1 << i))
308 irq_create_mapping(v->domain, i);
309
310 if (irq)
311 v->irq = irq;
312 else
313 v->irq = irq_find_mapping(v->domain, 0);
314}
315
316static void vic_ack_irq(struct irq_data *d)
317{
318 void __iomem *base = irq_data_get_irq_chip_data(d);
319 unsigned int irq = d->hwirq;
320 writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
321
322 writel(1 << irq, base + VIC_INT_SOFT_CLEAR);
323}
324
325static void vic_mask_irq(struct irq_data *d)
326{
327 void __iomem *base = irq_data_get_irq_chip_data(d);
328 unsigned int irq = d->hwirq;
329 writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
330}
331
332static void vic_unmask_irq(struct irq_data *d)
333{
334 void __iomem *base = irq_data_get_irq_chip_data(d);
335 unsigned int irq = d->hwirq;
336 writel(1 << irq, base + VIC_INT_ENABLE);
337}
338
339#if defined(CONFIG_PM)
340static struct vic_device *vic_from_irq(unsigned int irq)
341{
342 struct vic_device *v = vic_devices;
343 unsigned int base_irq = irq & ~31;
344 int id;
345
346 for (id = 0; id < vic_id; id++, v++) {
347 if (v->irq == base_irq)
348 return v;
349 }
350
351 return NULL;
352}
353
354static int vic_set_wake(struct irq_data *d, unsigned int on)
355{
356 struct vic_device *v = vic_from_irq(d->irq);
357 unsigned int off = d->hwirq;
358 u32 bit = 1 << off;
359
360 if (!v)
361 return -EINVAL;
362
363 if (!(bit & v->resume_sources))
364 return -EINVAL;
365
366 if (on)
367 v->resume_irqs |= bit;
368 else
369 v->resume_irqs &= ~bit;
370
371 return 0;
372}
373#else
374#define vic_set_wake NULL
375#endif
376
377static struct irq_chip vic_chip = {
378 .name = "VIC",
379 .irq_ack = vic_ack_irq,
380 .irq_mask = vic_mask_irq,
381 .irq_unmask = vic_unmask_irq,
382 .irq_set_wake = vic_set_wake,
383};
384
385static void __init vic_disable(void __iomem *base)
386{
387 writel(0, base + VIC_INT_SELECT);
388 writel(0, base + VIC_INT_ENABLE);
389 writel(~0, base + VIC_INT_ENABLE_CLEAR);
390 writel(0, base + VIC_ITCR);
391 writel(~0, base + VIC_INT_SOFT_CLEAR);
392}
393
394static void __init vic_clear_interrupts(void __iomem *base)
395{
396 unsigned int i;
397
398 writel(0, base + VIC_PL190_VECT_ADDR);
399 for (i = 0; i < 19; i++) {
400 unsigned int value;
401
402 value = readl(base + VIC_PL190_VECT_ADDR);
403 writel(value, base + VIC_PL190_VECT_ADDR);
404 }
405}
406
407
408
409
410
411
412
413
414static void __init vic_init_st(void __iomem *base, unsigned int irq_start,
415 u32 vic_sources, struct device_node *node)
416{
417 unsigned int i;
418 int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0;
419
420
421 vic_disable(base);
422
423
424
425
426
427
428
429 if (vic_2nd_block) {
430 vic_clear_interrupts(base);
431
432
433 for (i = 0; i < 16; i++) {
434 void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
435 writel(0, reg);
436 }
437
438 writel(32, base + VIC_PL190_DEF_VECT_ADDR);
439 }
440
441 vic_register(base, 0, irq_start, vic_sources, 0, node);
442}
443
444void __init __vic_init(void __iomem *base, int parent_irq, int irq_start,
445 u32 vic_sources, u32 resume_sources,
446 struct device_node *node)
447{
448 unsigned int i;
449 u32 cellid = 0;
450 enum amba_vendor vendor;
451
452
453 for (i = 0; i < 4; i++) {
454 void __iomem *addr;
455 addr = (void __iomem *)((u32)base & PAGE_MASK) + 0xfe0 + (i * 4);
456 cellid |= (readl(addr) & 0xff) << (8 * i);
457 }
458 vendor = (cellid >> 12) & 0xff;
459 printk(KERN_INFO "VIC @%p: id 0x%08x, vendor 0x%02x\n",
460 base, cellid, vendor);
461
462 switch(vendor) {
463 case AMBA_VENDOR_ST:
464 vic_init_st(base, irq_start, vic_sources, node);
465 return;
466 default:
467 printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n");
468
469 case AMBA_VENDOR_ARM:
470 break;
471 }
472
473
474 vic_disable(base);
475
476
477 vic_clear_interrupts(base);
478
479 vic_init2(base);
480
481 vic_register(base, parent_irq, irq_start, vic_sources, resume_sources, node);
482}
483
484
485
486
487
488
489
490
491void __init vic_init(void __iomem *base, unsigned int irq_start,
492 u32 vic_sources, u32 resume_sources)
493{
494 __vic_init(base, 0, irq_start, vic_sources, resume_sources, NULL);
495}
496
497
498
499
500
501
502
503
504
505
506int __init vic_init_cascaded(void __iomem *base, unsigned int parent_irq,
507 u32 vic_sources, u32 resume_sources)
508{
509 struct vic_device *v;
510
511 v = &vic_devices[vic_id];
512 __vic_init(base, parent_irq, 0, vic_sources, resume_sources, NULL);
513
514 return v->irq;
515}
516EXPORT_SYMBOL_GPL(vic_init_cascaded);
517
518#ifdef CONFIG_OF
519static int __init vic_of_init(struct device_node *node,
520 struct device_node *parent)
521{
522 void __iomem *regs;
523 u32 interrupt_mask = ~0;
524 u32 wakeup_mask = ~0;
525
526 if (WARN(parent, "non-root VICs are not supported"))
527 return -EINVAL;
528
529 regs = of_iomap(node, 0);
530 if (WARN_ON(!regs))
531 return -EIO;
532
533 of_property_read_u32(node, "valid-mask", &interrupt_mask);
534 of_property_read_u32(node, "valid-wakeup-mask", &wakeup_mask);
535
536
537
538
539 __vic_init(regs, 0, 0, interrupt_mask, wakeup_mask, node);
540
541 return 0;
542}
543IRQCHIP_DECLARE(arm_pl190_vic, "arm,pl190-vic", vic_of_init);
544IRQCHIP_DECLARE(arm_pl192_vic, "arm,pl192-vic", vic_of_init);
545IRQCHIP_DECLARE(arm_versatile_vic, "arm,versatile-vic", vic_of_init);
546#endif
547