blob: 01f18a421b17c6e20c6b0bf07a10c75378accc02 [file] [log] [blame]
Russell Kingfa0fe482006-01-13 21:30:48 +00001/*
2 * linux/arch/arm/common/vic.c
3 *
4 * Copyright (C) 1999 - 2003 ARM Limited
5 * Copyright (C) 2000 Deep Blue Solutions Ltd
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
Hartley Sweetenbb06b732010-01-12 19:09:12 +010021
Russell Kingfa0fe482006-01-13 21:30:48 +000022#include <linux/init.h>
23#include <linux/list.h>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +020025#include <linux/syscore_ops.h>
Linus Walleij59fcf482009-09-14 12:25:34 +010026#include <linux/device.h>
Linus Walleijf17a1f02009-08-04 01:01:02 +010027#include <linux/amba/bus.h>
Russell Kingfa0fe482006-01-13 21:30:48 +000028
Russell Kingfa0fe482006-01-13 21:30:48 +000029#include <asm/mach/irq.h>
30#include <asm/hardware/vic.h>
31
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +020032#ifdef CONFIG_PM
Ben Dooksc07f87f2009-03-24 15:30:07 +000033/**
34 * struct vic_device - VIC PM device
Ben Dooksc07f87f2009-03-24 15:30:07 +000035 * @irq: The IRQ number for the base of the VIC.
36 * @base: The register base for the VIC.
37 * @resume_sources: A bitmask of interrupts for resume.
38 * @resume_irqs: The IRQs enabled for resume.
39 * @int_select: Save for VIC_INT_SELECT.
40 * @int_enable: Save for VIC_INT_ENABLE.
41 * @soft_int: Save for VIC_INT_SOFT.
42 * @protect: Save for VIC_PROTECT.
43 */
44struct vic_device {
Ben Dooksc07f87f2009-03-24 15:30:07 +000045 void __iomem *base;
46 int irq;
47 u32 resume_sources;
48 u32 resume_irqs;
49 u32 int_select;
50 u32 int_enable;
51 u32 soft_int;
52 u32 protect;
53};
54
55/* we cannot allocate memory when VICs are initially registered */
56static struct vic_device vic_devices[CONFIG_ARM_VIC_NR];
57
Hartley Sweetenbb06b732010-01-12 19:09:12 +010058static int vic_id;
Hartley Sweetenbb06b732010-01-12 19:09:12 +010059#endif /* CONFIG_PM */
Ben Dooksc07f87f2009-03-24 15:30:07 +000060
Hartley Sweetenbb06b732010-01-12 19:09:12 +010061/**
62 * vic_init2 - common initialisation code
63 * @base: Base of the VIC.
64 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040065 * Common initialisation code for registration
Hartley Sweetenbb06b732010-01-12 19:09:12 +010066 * and resume.
67*/
68static void vic_init2(void __iomem *base)
69{
70 int i;
Ben Dooksc07f87f2009-03-24 15:30:07 +000071
Hartley Sweetenbb06b732010-01-12 19:09:12 +010072 for (i = 0; i < 16; i++) {
73 void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
74 writel(VIC_VECT_CNTL_ENABLE | i, reg);
75 }
76
77 writel(32, base + VIC_PL190_DEF_VECT_ADDR);
78}
79
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +020080#ifdef CONFIG_PM
81static void resume_one_vic(struct vic_device *vic)
Ben Dooksc07f87f2009-03-24 15:30:07 +000082{
Ben Dooksc07f87f2009-03-24 15:30:07 +000083 void __iomem *base = vic->base;
84
85 printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base);
86
87 /* re-initialise static settings */
88 vic_init2(base);
89
90 writel(vic->int_select, base + VIC_INT_SELECT);
91 writel(vic->protect, base + VIC_PROTECT);
92
93 /* set the enabled ints and then clear the non-enabled */
94 writel(vic->int_enable, base + VIC_INT_ENABLE);
95 writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR);
96
97 /* and the same for the soft-int register */
98
99 writel(vic->soft_int, base + VIC_INT_SOFT);
100 writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR);
Ben Dooksc07f87f2009-03-24 15:30:07 +0000101}
102
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200103static void vic_resume(void)
Ben Dooksc07f87f2009-03-24 15:30:07 +0000104{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200105 int id;
106
107 for (id = vic_id - 1; id >= 0; id--)
108 resume_one_vic(vic_devices + id);
109}
110
111static void suspend_one_vic(struct vic_device *vic)
112{
Ben Dooksc07f87f2009-03-24 15:30:07 +0000113 void __iomem *base = vic->base;
114
115 printk(KERN_DEBUG "%s: suspending vic at %p\n", __func__, base);
116
117 vic->int_select = readl(base + VIC_INT_SELECT);
118 vic->int_enable = readl(base + VIC_INT_ENABLE);
119 vic->soft_int = readl(base + VIC_INT_SOFT);
120 vic->protect = readl(base + VIC_PROTECT);
121
122 /* set the interrupts (if any) that are used for
123 * resuming the system */
124
125 writel(vic->resume_irqs, base + VIC_INT_ENABLE);
126 writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR);
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200127}
128
129static int vic_suspend(void)
130{
131 int id;
132
133 for (id = 0; id < vic_id; id++)
134 suspend_one_vic(vic_devices + id);
Ben Dooksc07f87f2009-03-24 15:30:07 +0000135
136 return 0;
137}
138
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200139struct syscore_ops vic_syscore_ops = {
140 .suspend = vic_suspend,
141 .resume = vic_resume,
Ben Dooksc07f87f2009-03-24 15:30:07 +0000142};
143
144/**
Ben Dooksc07f87f2009-03-24 15:30:07 +0000145 * vic_pm_init - initicall to register VIC pm
146 *
147 * This is called via late_initcall() to register
148 * the resources for the VICs due to the early
149 * nature of the VIC's registration.
150*/
151static int __init vic_pm_init(void)
152{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200153 if (vic_id > 0)
154 register_syscore_ops(&vic_syscore_ops);
Ben Dooksc07f87f2009-03-24 15:30:07 +0000155
156 return 0;
157}
Ben Dooksc07f87f2009-03-24 15:30:07 +0000158late_initcall(vic_pm_init);
159
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100160/**
161 * vic_pm_register - Register a VIC for later power management control
162 * @base: The base address of the VIC.
163 * @irq: The base IRQ for the VIC.
164 * @resume_sources: bitmask of interrupts allowed for resume sources.
165 *
166 * Register the VIC with the system device tree so that it can be notified
167 * of suspend and resume requests and ensure that the correct actions are
168 * taken to re-instate the settings on resume.
169 */
170static void __init vic_pm_register(void __iomem *base, unsigned int irq, u32 resume_sources)
171{
172 struct vic_device *v;
173
174 if (vic_id >= ARRAY_SIZE(vic_devices))
175 printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__);
176 else {
177 v = &vic_devices[vic_id];
178 v->base = base;
179 v->resume_sources = resume_sources;
180 v->irq = irq;
181 vic_id++;
182 }
183}
184#else
185static inline void vic_pm_register(void __iomem *base, unsigned int irq, u32 arg1) { }
186#endif /* CONFIG_PM */
187
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100188static void vic_ack_irq(struct irq_data *d)
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100189{
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100190 void __iomem *base = irq_data_get_irq_chip_data(d);
191 unsigned int irq = d->irq & 31;
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100192 writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
193 /* moreover, clear the soft-triggered, in case it was the reason */
194 writel(1 << irq, base + VIC_INT_SOFT_CLEAR);
195}
196
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100197static void vic_mask_irq(struct irq_data *d)
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100198{
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100199 void __iomem *base = irq_data_get_irq_chip_data(d);
200 unsigned int irq = d->irq & 31;
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100201 writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
202}
203
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100204static void vic_unmask_irq(struct irq_data *d)
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100205{
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100206 void __iomem *base = irq_data_get_irq_chip_data(d);
207 unsigned int irq = d->irq & 31;
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100208 writel(1 << irq, base + VIC_INT_ENABLE);
209}
210
211#if defined(CONFIG_PM)
Ben Dooksc07f87f2009-03-24 15:30:07 +0000212static struct vic_device *vic_from_irq(unsigned int irq)
213{
214 struct vic_device *v = vic_devices;
215 unsigned int base_irq = irq & ~31;
216 int id;
217
218 for (id = 0; id < vic_id; id++, v++) {
219 if (v->irq == base_irq)
220 return v;
221 }
222
223 return NULL;
224}
225
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100226static int vic_set_wake(struct irq_data *d, unsigned int on)
Ben Dooksc07f87f2009-03-24 15:30:07 +0000227{
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100228 struct vic_device *v = vic_from_irq(d->irq);
229 unsigned int off = d->irq & 31;
Ben Dooks3f1a5672009-06-02 09:31:03 +0100230 u32 bit = 1 << off;
Ben Dooksc07f87f2009-03-24 15:30:07 +0000231
232 if (!v)
233 return -EINVAL;
234
Ben Dooks3f1a5672009-06-02 09:31:03 +0100235 if (!(bit & v->resume_sources))
236 return -EINVAL;
237
Ben Dooksc07f87f2009-03-24 15:30:07 +0000238 if (on)
Ben Dooks3f1a5672009-06-02 09:31:03 +0100239 v->resume_irqs |= bit;
Ben Dooksc07f87f2009-03-24 15:30:07 +0000240 else
Ben Dooks3f1a5672009-06-02 09:31:03 +0100241 v->resume_irqs &= ~bit;
Ben Dooksc07f87f2009-03-24 15:30:07 +0000242
243 return 0;
244}
Ben Dooksc07f87f2009-03-24 15:30:07 +0000245#else
Ben Dooksc07f87f2009-03-24 15:30:07 +0000246#define vic_set_wake NULL
247#endif /* CONFIG_PM */
248
David Brownell38c677c2006-08-01 22:26:25 +0100249static struct irq_chip vic_chip = {
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100250 .name = "VIC",
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100251 .irq_ack = vic_ack_irq,
252 .irq_mask = vic_mask_irq,
253 .irq_unmask = vic_unmask_irq,
254 .irq_set_wake = vic_set_wake,
Russell Kingfa0fe482006-01-13 21:30:48 +0000255};
256
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100257static void __init vic_disable(void __iomem *base)
258{
259 writel(0, base + VIC_INT_SELECT);
260 writel(0, base + VIC_INT_ENABLE);
261 writel(~0, base + VIC_INT_ENABLE_CLEAR);
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100262 writel(0, base + VIC_ITCR);
263 writel(~0, base + VIC_INT_SOFT_CLEAR);
264}
265
266static void __init vic_clear_interrupts(void __iomem *base)
267{
268 unsigned int i;
269
270 writel(0, base + VIC_PL190_VECT_ADDR);
271 for (i = 0; i < 19; i++) {
272 unsigned int value;
273
274 value = readl(base + VIC_PL190_VECT_ADDR);
275 writel(value, base + VIC_PL190_VECT_ADDR);
276 }
277}
278
279static void __init vic_set_irq_sources(void __iomem *base,
280 unsigned int irq_start, u32 vic_sources)
281{
282 unsigned int i;
283
284 for (i = 0; i < 32; i++) {
285 if (vic_sources & (1 << i)) {
286 unsigned int irq = irq_start + i;
287
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100288 irq_set_chip_and_handler(irq, &vic_chip,
289 handle_level_irq);
Thomas Gleixner9323f2612011-03-24 13:29:39 +0100290 irq_set_chip_data(irq, base);
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100291 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
292 }
293 }
294}
295
Alessandro Rubini87e88242009-07-02 15:28:41 +0100296/*
297 * The PL190 cell from ARM has been modified by ST to handle 64 interrupts.
298 * The original cell has 32 interrupts, while the modified one has 64,
299 * replocating two blocks 0x00..0x1f in 0x20..0x3f. In that case
300 * the probe function is called twice, with base set to offset 000
301 * and 020 within the page. We call this "second block".
302 */
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100303static void __init vic_init_st(void __iomem *base, unsigned int irq_start,
Alessandro Rubini87e88242009-07-02 15:28:41 +0100304 u32 vic_sources)
305{
306 unsigned int i;
307 int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0;
308
309 /* Disable all interrupts initially. */
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100310 vic_disable(base);
Alessandro Rubini87e88242009-07-02 15:28:41 +0100311
312 /*
313 * Make sure we clear all existing interrupts. The vector registers
314 * in this cell are after the second block of general registers,
315 * so we can address them using standard offsets, but only from
316 * the second base address, which is 0x20 in the page
317 */
318 if (vic_2nd_block) {
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100319 vic_clear_interrupts(base);
Alessandro Rubini87e88242009-07-02 15:28:41 +0100320
Alessandro Rubini87e88242009-07-02 15:28:41 +0100321 /* ST has 16 vectors as well, but we don't enable them by now */
322 for (i = 0; i < 16; i++) {
323 void __iomem *reg = base + VIC_VECT_CNTL0 + (i * 4);
324 writel(0, reg);
325 }
326
327 writel(32, base + VIC_PL190_DEF_VECT_ADDR);
328 }
329
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100330 vic_set_irq_sources(base, irq_start, vic_sources);
Alessandro Rubini87e88242009-07-02 15:28:41 +0100331}
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100332
333/**
334 * vic_init - initialise a vectored interrupt controller
335 * @base: iomem base address
336 * @irq_start: starting interrupt number, must be muliple of 32
337 * @vic_sources: bitmask of interrupt sources to allow
338 * @resume_sources: bitmask of interrupt sources to allow for resume
339 */
340void __init vic_init(void __iomem *base, unsigned int irq_start,
341 u32 vic_sources, u32 resume_sources)
342{
343 unsigned int i;
344 u32 cellid = 0;
345 enum amba_vendor vendor;
346
347 /* Identify which VIC cell this one is, by reading the ID */
348 for (i = 0; i < 4; i++) {
Arnd Bergmannd4f3add2011-09-23 10:13:49 +0200349 void __iomem *addr;
350 addr = (void __iomem *)((u32)base & PAGE_MASK) + 0xfe0 + (i * 4);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100351 cellid |= (readl(addr) & 0xff) << (8 * i);
352 }
353 vendor = (cellid >> 12) & 0xff;
354 printk(KERN_INFO "VIC @%p: id 0x%08x, vendor 0x%02x\n",
355 base, cellid, vendor);
356
357 switch(vendor) {
358 case AMBA_VENDOR_ST:
359 vic_init_st(base, irq_start, vic_sources);
360 return;
361 default:
362 printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n");
363 /* fall through */
364 case AMBA_VENDOR_ARM:
365 break;
366 }
367
368 /* Disable all interrupts initially. */
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100369 vic_disable(base);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100370
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100371 /* Make sure we clear all existing interrupts */
372 vic_clear_interrupts(base);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100373
374 vic_init2(base);
375
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100376 vic_set_irq_sources(base, irq_start, vic_sources);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100377
378 vic_pm_register(base, irq_start, resume_sources);
379}