blob: 74192f62dd67d37b609aeb08f848545db8a3caf3 [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
Jamie Ilesf9b28cc2011-09-27 11:00:46 +010022#include <linux/export.h>
Russell Kingfa0fe482006-01-13 21:30:48 +000023#include <linux/init.h>
24#include <linux/list.h>
Russell Kingfced80c2008-09-06 12:10:45 +010025#include <linux/io.h>
Olof Johanssonbc895b52013-04-02 15:07:37 -070026#include <linux/irq.h>
Joel Porquet41a83e02015-07-07 17:11:46 -040027#include <linux/irqchip.h>
Linus Walleijf6da9fe2014-04-15 10:28:04 +020028#include <linux/irqchip/chained_irq.h>
Jamie Ilesf9b28cc2011-09-27 11:00:46 +010029#include <linux/irqdomain.h>
30#include <linux/of.h>
31#include <linux/of_address.h>
32#include <linux/of_irq.h>
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +020033#include <linux/syscore_ops.h>
Linus Walleij59fcf482009-09-14 12:25:34 +010034#include <linux/device.h>
Linus Walleijf17a1f02009-08-04 01:01:02 +010035#include <linux/amba/bus.h>
Rob Herring9e47b8b2013-01-07 09:45:59 -060036#include <linux/irqchip/arm-vic.h>
Russell Kingfa0fe482006-01-13 21:30:48 +000037
Jamie Iles15583682011-09-28 09:40:11 +010038#include <asm/exception.h>
Catalin Marinasf36a3bb12013-01-18 15:20:06 +000039#include <asm/irq.h>
Russell Kingfa0fe482006-01-13 21:30:48 +000040
Rob Herringcf21af52012-11-06 13:14:26 -060041#define VIC_IRQ_STATUS 0x00
42#define VIC_FIQ_STATUS 0x04
43#define VIC_INT_SELECT 0x0c /* 1 = FIQ, 0 = IRQ */
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 /* PL190 only */
48#define VIC_PL190_DEF_VECT_ADDR 0x34 /* PL190 only */
49
50#define VIC_VECT_ADDR0 0x100 /* 0 to 15 (0..31 PL192) */
51#define VIC_VECT_CNTL0 0x200 /* 0 to 15 (0..31 PL192) */
52#define VIC_ITCR 0x300 /* VIC test control register */
53
54#define VIC_VECT_CNTL_ENABLE (1 << 5)
55
56#define VIC_PL192_VECT_ADDR 0xF00
57
Ben Dooksc07f87f2009-03-24 15:30:07 +000058/**
59 * struct vic_device - VIC PM device
Linus Walleije641b982013-11-21 23:11:29 +010060 * @parent_irq: The parent IRQ number of the VIC if cascaded, or 0.
Ben Dooksc07f87f2009-03-24 15:30:07 +000061 * @irq: The IRQ number for the base of the VIC.
62 * @base: The register base for the VIC.
Linus Walleijce94df92012-04-20 08:02:36 +010063 * @valid_sources: A bitmask of valid interrupts
Ben Dooksc07f87f2009-03-24 15:30:07 +000064 * @resume_sources: A bitmask of interrupts for resume.
65 * @resume_irqs: The IRQs enabled for resume.
66 * @int_select: Save for VIC_INT_SELECT.
67 * @int_enable: Save for VIC_INT_ENABLE.
68 * @soft_int: Save for VIC_INT_SOFT.
69 * @protect: Save for VIC_PROTECT.
Jamie Ilesf9b28cc2011-09-27 11:00:46 +010070 * @domain: The IRQ domain for the VIC.
Ben Dooksc07f87f2009-03-24 15:30:07 +000071 */
72struct vic_device {
Ben Dooksc07f87f2009-03-24 15:30:07 +000073 void __iomem *base;
74 int irq;
Linus Walleijce94df92012-04-20 08:02:36 +010075 u32 valid_sources;
Ben Dooksc07f87f2009-03-24 15:30:07 +000076 u32 resume_sources;
77 u32 resume_irqs;
78 u32 int_select;
79 u32 int_enable;
80 u32 soft_int;
81 u32 protect;
Grant Likely75294952012-02-14 14:06:57 -070082 struct irq_domain *domain;
Ben Dooksc07f87f2009-03-24 15:30:07 +000083};
84
85/* we cannot allocate memory when VICs are initially registered */
86static struct vic_device vic_devices[CONFIG_ARM_VIC_NR];
87
Hartley Sweetenbb06b732010-01-12 19:09:12 +010088static int vic_id;
Ben Dooksc07f87f2009-03-24 15:30:07 +000089
Rob Herringa0368022012-11-05 16:32:29 -060090static void vic_handle_irq(struct pt_regs *regs);
91
Hartley Sweetenbb06b732010-01-12 19:09:12 +010092/**
93 * vic_init2 - common initialisation code
94 * @base: Base of the VIC.
95 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040096 * Common initialisation code for registration
Hartley Sweetenbb06b732010-01-12 19:09:12 +010097 * and resume.
98*/
99static void vic_init2(void __iomem *base)
100{
101 int i;
Ben Dooksc07f87f2009-03-24 15:30:07 +0000102
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100103 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
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200111#ifdef CONFIG_PM
112static void resume_one_vic(struct vic_device *vic)
Ben Dooksc07f87f2009-03-24 15:30:07 +0000113{
Ben Dooksc07f87f2009-03-24 15:30:07 +0000114 void __iomem *base = vic->base;
115
116 printk(KERN_DEBUG "%s: resuming vic at %p\n", __func__, base);
117
118 /* re-initialise static settings */
119 vic_init2(base);
120
121 writel(vic->int_select, base + VIC_INT_SELECT);
122 writel(vic->protect, base + VIC_PROTECT);
123
124 /* set the enabled ints and then clear the non-enabled */
125 writel(vic->int_enable, base + VIC_INT_ENABLE);
126 writel(~vic->int_enable, base + VIC_INT_ENABLE_CLEAR);
127
128 /* and the same for the soft-int register */
129
130 writel(vic->soft_int, base + VIC_INT_SOFT);
131 writel(~vic->soft_int, base + VIC_INT_SOFT_CLEAR);
Ben Dooksc07f87f2009-03-24 15:30:07 +0000132}
133
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200134static void vic_resume(void)
Ben Dooksc07f87f2009-03-24 15:30:07 +0000135{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200136 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{
Ben Dooksc07f87f2009-03-24 15:30:07 +0000144 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 /* set the interrupts (if any) that are used for
154 * resuming the system */
155
156 writel(vic->resume_irqs, base + VIC_INT_ENABLE);
157 writel(~vic->resume_irqs, base + VIC_INT_ENABLE_CLEAR);
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200158}
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);
Ben Dooksc07f87f2009-03-24 15:30:07 +0000166
167 return 0;
168}
169
Ben Dooksdf042a52016-06-09 11:30:12 +0100170static struct syscore_ops vic_syscore_ops = {
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200171 .suspend = vic_suspend,
172 .resume = vic_resume,
Ben Dooksc07f87f2009-03-24 15:30:07 +0000173};
174
175/**
Ben Dooksc07f87f2009-03-24 15:30:07 +0000176 * vic_pm_init - initicall to register VIC pm
177 *
178 * This is called via late_initcall() to register
179 * the resources for the VICs due to the early
180 * nature of the VIC's registration.
181*/
182static int __init vic_pm_init(void)
183{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200184 if (vic_id > 0)
185 register_syscore_ops(&vic_syscore_ops);
Ben Dooksc07f87f2009-03-24 15:30:07 +0000186
187 return 0;
188}
Ben Dooksc07f87f2009-03-24 15:30:07 +0000189late_initcall(vic_pm_init);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100190#endif /* CONFIG_PM */
Ben Dooksc07f87f2009-03-24 15:30:07 +0000191
Linus Walleijce94df92012-04-20 08:02:36 +0100192static 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 /* Skip invalid IRQs, only register handlers for the real ones */
200 if (!(v->valid_sources & (1 << hwirq)))
Grant Likelyd94ea3f2013-06-06 14:11:38 +0100201 return -EPERM;
Linus Walleijce94df92012-04-20 08:02:36 +0100202 irq_set_chip_and_handler(irq, &vic_chip, handle_level_irq);
203 irq_set_chip_data(irq, v->base);
Rob Herringd17cab42015-08-29 18:01:22 -0500204 irq_set_probe(irq);
Linus Walleijce94df92012-04-20 08:02:36 +0100205 return 0;
206}
207
Rob Herringa0368022012-11-05 16:32:29 -0600208/*
209 * Handle each interrupt in a single VIC. Returns non-zero if we've
210 * handled at least one interrupt. This reads the status register
211 * before handling each interrupt, which is necessary given that
212 * handle_IRQ may briefly re-enable interrupts for soft IRQ handling.
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;
Marc Zyngier0af83b32014-08-26 11:03:30 +0100221 handle_domain_irq(vic->domain, irq, regs);
Rob Herringa0368022012-11-05 16:32:29 -0600222 handled = 1;
223 }
224
225 return handled;
226}
227
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200228static void vic_handle_irq_cascaded(struct irq_desc *desc)
Linus Walleije641b982013-11-21 23:11:29 +0100229{
230 u32 stat, hwirq;
Linus Walleijf6da9fe2014-04-15 10:28:04 +0200231 struct irq_chip *host_chip = irq_desc_get_chip(desc);
Linus Walleije641b982013-11-21 23:11:29 +0100232 struct vic_device *vic = irq_desc_get_handler_data(desc);
233
Linus Walleijf6da9fe2014-04-15 10:28:04 +0200234 chained_irq_enter(host_chip, desc);
235
Linus Walleije641b982013-11-21 23:11:29 +0100236 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 }
Linus Walleijf6da9fe2014-04-15 10:28:04 +0200240
241 chained_irq_exit(host_chip, desc);
Linus Walleije641b982013-11-21 23:11:29 +0100242}
243
Rob Herringa0368022012-11-05 16:32:29 -0600244/*
245 * Keep iterating over all registered VIC's until there are no pending
246 * interrupts.
247 */
Stephen Boyd8783dd32014-03-04 16:40:30 -0800248static void __exception_irq_entry vic_handle_irq(struct pt_regs *regs)
Rob Herringa0368022012-11-05 16:32:29 -0600249{
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
Krzysztof Kozlowski96009732015-04-27 21:54:24 +0900258static const struct irq_domain_ops vic_irqdomain_ops = {
Linus Walleijce94df92012-04-20 08:02:36 +0100259 .map = vic_irqdomain_map,
260 .xlate = irq_domain_xlate_onetwocell,
261};
262
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100263/**
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100264 * vic_register() - Register a VIC.
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100265 * @base: The base address of the VIC.
Linus Walleije641b982013-11-21 23:11:29 +0100266 * @parent_irq: The parent IRQ if cascaded, else 0.
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100267 * @irq: The base IRQ for the VIC.
Linus Walleijfa943be2012-04-20 08:02:03 +0100268 * @valid_sources: bitmask of valid interrupts
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100269 * @resume_sources: bitmask of interrupts allowed for resume sources.
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100270 * @node: The device tree node associated with the VIC.
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100271 *
272 * Register the VIC with the system device tree so that it can be notified
273 * of suspend and resume requests and ensure that the correct actions are
274 * taken to re-instate the settings on resume.
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100275 *
276 * This also configures the IRQ domain for the VIC.
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100277 */
Linus Walleije641b982013-11-21 23:11:29 +0100278static void __init vic_register(void __iomem *base, unsigned int parent_irq,
279 unsigned int irq,
Linus Walleijfa943be2012-04-20 08:02:03 +0100280 u32 valid_sources, u32 resume_sources,
281 struct device_node *node)
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100282{
283 struct vic_device *v;
Linus Walleij5ced33b2012-12-26 01:39:16 +0100284 int i;
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100285
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100286 if (vic_id >= ARRAY_SIZE(vic_devices)) {
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100287 printk(KERN_ERR "%s: too few VICs, increase CONFIG_ARM_VIC_NR\n", __func__);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100288 return;
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100289 }
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100290
291 v = &vic_devices[vic_id];
292 v->base = base;
Linus Walleijce94df92012-04-20 08:02:36 +0100293 v->valid_sources = valid_sources;
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100294 v->resume_sources = resume_sources;
Rob Herring7fb7d8a2012-11-20 19:55:27 -0600295 set_handle_irq(vic_handle_irq);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100296 vic_id++;
Linus Walleije641b982013-11-21 23:11:29 +0100297
298 if (parent_irq) {
Thomas Gleixner9f213542015-06-21 21:11:00 +0200299 irq_set_chained_handler_and_data(parent_irq,
300 vic_handle_irq_cascaded, v);
Linus Walleije641b982013-11-21 23:11:29 +0100301 }
302
Linus Walleij07c92492012-10-16 18:50:00 +0100303 v->domain = irq_domain_add_simple(node, fls(valid_sources), irq,
Linus Walleijfa943be2012-04-20 08:02:03 +0100304 &vic_irqdomain_ops, v);
Linus Walleij5ced33b2012-12-26 01:39:16 +0100305 /* create an IRQ mapping for each valid IRQ */
306 for (i = 0; i < fls(valid_sources); i++)
307 if (valid_sources & (1 << i))
308 irq_create_mapping(v->domain, i);
Linus Walleij3b4df9d2013-11-24 20:18:57 +0100309 /* If no base IRQ was passed, figure out our allocated base */
310 if (irq)
311 v->irq = irq;
312 else
313 v->irq = irq_find_mapping(v->domain, 0);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100314}
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100315
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100316static void vic_ack_irq(struct irq_data *d)
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100317{
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100318 void __iomem *base = irq_data_get_irq_chip_data(d);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100319 unsigned int irq = d->hwirq;
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100320 writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
321 /* moreover, clear the soft-triggered, in case it was the reason */
322 writel(1 << irq, base + VIC_INT_SOFT_CLEAR);
323}
324
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100325static void vic_mask_irq(struct irq_data *d)
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100326{
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100327 void __iomem *base = irq_data_get_irq_chip_data(d);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100328 unsigned int irq = d->hwirq;
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100329 writel(1 << irq, base + VIC_INT_ENABLE_CLEAR);
330}
331
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100332static void vic_unmask_irq(struct irq_data *d)
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100333{
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100334 void __iomem *base = irq_data_get_irq_chip_data(d);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100335 unsigned int irq = d->hwirq;
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100336 writel(1 << irq, base + VIC_INT_ENABLE);
337}
338
339#if defined(CONFIG_PM)
Ben Dooksc07f87f2009-03-24 15:30:07 +0000340static 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
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100354static int vic_set_wake(struct irq_data *d, unsigned int on)
Ben Dooksc07f87f2009-03-24 15:30:07 +0000355{
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100356 struct vic_device *v = vic_from_irq(d->irq);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100357 unsigned int off = d->hwirq;
Ben Dooks3f1a5672009-06-02 09:31:03 +0100358 u32 bit = 1 << off;
Ben Dooksc07f87f2009-03-24 15:30:07 +0000359
360 if (!v)
361 return -EINVAL;
362
Ben Dooks3f1a5672009-06-02 09:31:03 +0100363 if (!(bit & v->resume_sources))
364 return -EINVAL;
365
Ben Dooksc07f87f2009-03-24 15:30:07 +0000366 if (on)
Ben Dooks3f1a5672009-06-02 09:31:03 +0100367 v->resume_irqs |= bit;
Ben Dooksc07f87f2009-03-24 15:30:07 +0000368 else
Ben Dooks3f1a5672009-06-02 09:31:03 +0100369 v->resume_irqs &= ~bit;
Ben Dooksc07f87f2009-03-24 15:30:07 +0000370
371 return 0;
372}
Ben Dooksc07f87f2009-03-24 15:30:07 +0000373#else
Ben Dooksc07f87f2009-03-24 15:30:07 +0000374#define vic_set_wake NULL
375#endif /* CONFIG_PM */
376
David Brownell38c677c2006-08-01 22:26:25 +0100377static struct irq_chip vic_chip = {
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100378 .name = "VIC",
Lennert Buytenhekf013c982010-11-29 10:20:21 +0100379 .irq_ack = vic_ack_irq,
380 .irq_mask = vic_mask_irq,
381 .irq_unmask = vic_unmask_irq,
382 .irq_set_wake = vic_set_wake,
Russell Kingfa0fe482006-01-13 21:30:48 +0000383};
384
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100385static 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);
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100390 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
Alessandro Rubini87e88242009-07-02 15:28:41 +0100407/*
408 * The PL190 cell from ARM has been modified by ST to handle 64 interrupts.
409 * The original cell has 32 interrupts, while the modified one has 64,
410 * replocating two blocks 0x00..0x1f in 0x20..0x3f. In that case
411 * the probe function is called twice, with base set to offset 000
412 * and 020 within the page. We call this "second block".
413 */
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100414static void __init vic_init_st(void __iomem *base, unsigned int irq_start,
Jamie Ilesad622672011-12-01 11:16:46 +0100415 u32 vic_sources, struct device_node *node)
Alessandro Rubini87e88242009-07-02 15:28:41 +0100416{
417 unsigned int i;
418 int vic_2nd_block = ((unsigned long)base & ~PAGE_MASK) != 0;
419
420 /* Disable all interrupts initially. */
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100421 vic_disable(base);
Alessandro Rubini87e88242009-07-02 15:28:41 +0100422
423 /*
424 * Make sure we clear all existing interrupts. The vector registers
425 * in this cell are after the second block of general registers,
426 * so we can address them using standard offsets, but only from
427 * the second base address, which is 0x20 in the page
428 */
429 if (vic_2nd_block) {
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100430 vic_clear_interrupts(base);
Alessandro Rubini87e88242009-07-02 15:28:41 +0100431
Alessandro Rubini87e88242009-07-02 15:28:41 +0100432 /* ST has 16 vectors as well, but we don't enable them by now */
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
Linus Walleije641b982013-11-21 23:11:29 +0100441 vic_register(base, 0, irq_start, vic_sources, 0, node);
Alessandro Rubini87e88242009-07-02 15:28:41 +0100442}
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100443
Linus Walleije641b982013-11-21 23:11:29 +0100444void __init __vic_init(void __iomem *base, int parent_irq, int irq_start,
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100445 u32 vic_sources, u32 resume_sources,
446 struct device_node *node)
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100447{
448 unsigned int i;
449 u32 cellid = 0;
450 enum amba_vendor vendor;
451
452 /* Identify which VIC cell this one is, by reading the ID */
453 for (i = 0; i < 4; i++) {
Arnd Bergmannd4f3add2011-09-23 10:13:49 +0200454 void __iomem *addr;
455 addr = (void __iomem *)((u32)base & PAGE_MASK) + 0xfe0 + (i * 4);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100456 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:
Jamie Ilesad622672011-12-01 11:16:46 +0100464 vic_init_st(base, irq_start, vic_sources, node);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100465 return;
466 default:
467 printk(KERN_WARNING "VIC: unknown vendor, continuing anyways\n");
468 /* fall through */
469 case AMBA_VENDOR_ARM:
470 break;
471 }
472
473 /* Disable all interrupts initially. */
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100474 vic_disable(base);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100475
Hartley Sweetenb0c4c892010-04-02 18:04:47 +0100476 /* Make sure we clear all existing interrupts */
477 vic_clear_interrupts(base);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100478
479 vic_init2(base);
480
Linus Walleije641b982013-11-21 23:11:29 +0100481 vic_register(base, parent_irq, irq_start, vic_sources, resume_sources, node);
Hartley Sweetenbb06b732010-01-12 19:09:12 +0100482}
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100483
484/**
485 * vic_init() - initialise a vectored interrupt controller
486 * @base: iomem base address
487 * @irq_start: starting interrupt number, must be muliple of 32
488 * @vic_sources: bitmask of interrupt sources to allow
489 * @resume_sources: bitmask of interrupt sources to allow for resume
490 */
491void __init vic_init(void __iomem *base, unsigned int irq_start,
492 u32 vic_sources, u32 resume_sources)
493{
Linus Walleije641b982013-11-21 23:11:29 +0100494 __vic_init(base, 0, irq_start, vic_sources, resume_sources, NULL);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100495}
496
Linus Walleije641b982013-11-21 23:11:29 +0100497/**
498 * vic_init_cascaded() - initialise a cascaded vectored interrupt controller
499 * @base: iomem base address
500 * @parent_irq: the parent IRQ we're cascaded off
Linus Walleije641b982013-11-21 23:11:29 +0100501 * @vic_sources: bitmask of interrupt sources to allow
502 * @resume_sources: bitmask of interrupt sources to allow for resume
503 *
504 * This returns the base for the new interrupts or negative on error.
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 /* Return out acquired base */
514 return v->irq;
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100515}
Arnd Bergmanna3f4fdf2014-02-24 22:19:24 +0100516EXPORT_SYMBOL_GPL(vic_init_cascaded);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100517
518#ifdef CONFIG_OF
Ben Dooksdf042a52016-06-09 11:30:12 +0100519static int __init vic_of_init(struct device_node *node,
520 struct device_node *parent)
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100521{
522 void __iomem *regs;
Tomasz Figa81e9c172013-08-26 02:37:24 +0900523 u32 interrupt_mask = ~0;
524 u32 wakeup_mask = ~0;
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100525
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
Tomasz Figa81e9c172013-08-26 02:37:24 +0900533 of_property_read_u32(node, "valid-mask", &interrupt_mask);
534 of_property_read_u32(node, "valid-wakeup-mask", &wakeup_mask);
535
Linus Walleij07c92492012-10-16 18:50:00 +0100536 /*
Linus Walleij5ced33b2012-12-26 01:39:16 +0100537 * Passing 0 as first IRQ makes the simple domain allocate descriptors
Linus Walleij07c92492012-10-16 18:50:00 +0100538 */
Linus Walleije641b982013-11-21 23:11:29 +0100539 __vic_init(regs, 0, 0, interrupt_mask, wakeup_mask, node);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100540
541 return 0;
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100542}
Rob Herring44430ec2012-10-27 17:25:26 -0500543IRQCHIP_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);
Jamie Ilesf9b28cc2011-09-27 11:00:46 +0100546#endif /* CONFIG OF */