blob: 62bb840c613f2a660966333f858426af5ab3d93e [file] [log] [blame]
Boris BREZILLONb1479eb2014-07-10 19:14:18 +02001/*
2 * Atmel AT91 AIC5 (Advanced Interrupt Controller) driver
3 *
4 * Copyright (C) 2004 SAN People
5 * Copyright (C) 2004 ATMEL
6 * Copyright (C) Rick Bronson
7 * Copyright (C) 2014 Free Electrons
8 *
9 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
10 *
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
14 */
15
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/mm.h>
19#include <linux/bitmap.h>
20#include <linux/types.h>
21#include <linux/irq.h>
Joel Porquet41a83e062015-07-07 17:11:46 -040022#include <linux/irqchip.h>
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020023#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/of_irq.h>
26#include <linux/irqdomain.h>
27#include <linux/err.h>
28#include <linux/slab.h>
29#include <linux/io.h>
30
31#include <asm/exception.h>
32#include <asm/mach/irq.h>
33
34#include "irq-atmel-aic-common.h"
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020035
36/* Number of irq lines managed by AIC */
37#define NR_AIC5_IRQS 128
38
39#define AT91_AIC5_SSR 0x0
40#define AT91_AIC5_INTSEL_MSK (0x7f << 0)
41
42#define AT91_AIC5_SMR 0x4
43
44#define AT91_AIC5_SVR 0x8
45#define AT91_AIC5_IVR 0x10
46#define AT91_AIC5_FVR 0x14
47#define AT91_AIC5_ISR 0x18
48
49#define AT91_AIC5_IPR0 0x20
50#define AT91_AIC5_IPR1 0x24
51#define AT91_AIC5_IPR2 0x28
52#define AT91_AIC5_IPR3 0x2c
53#define AT91_AIC5_IMR 0x30
54#define AT91_AIC5_CISR 0x34
55
56#define AT91_AIC5_IECR 0x40
57#define AT91_AIC5_IDCR 0x44
58#define AT91_AIC5_ICCR 0x48
59#define AT91_AIC5_ISCR 0x4c
60#define AT91_AIC5_EOICR 0x38
61#define AT91_AIC5_SPU 0x3c
62#define AT91_AIC5_DCR 0x6c
63
64#define AT91_AIC5_FFER 0x50
65#define AT91_AIC5_FFDR 0x54
66#define AT91_AIC5_FFSR 0x58
67
68static struct irq_domain *aic5_domain;
69
70static asmlinkage void __exception_irq_entry
71aic5_handle(struct pt_regs *regs)
72{
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +020073 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(aic5_domain, 0);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020074 u32 irqnr;
75 u32 irqstat;
76
Ludovic Desroches414a4312015-09-21 15:46:05 +020077 irqnr = irq_reg_readl(bgc, AT91_AIC5_IVR);
78 irqstat = irq_reg_readl(bgc, AT91_AIC5_ISR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020079
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020080 if (!irqstat)
Ludovic Desroches414a4312015-09-21 15:46:05 +020081 irq_reg_writel(bgc, 0, AT91_AIC5_EOICR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020082 else
Marc Zyngier31b7b6a2014-08-26 11:03:35 +010083 handle_domain_irq(aic5_domain, irqnr, regs);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020084}
85
86static void aic5_mask(struct irq_data *d)
87{
88 struct irq_domain *domain = d->domain;
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +020089 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
Ludovic Desrochesd32dc9a2015-09-21 15:46:04 +020090 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020091
Ludovic Desrochesd32dc9a2015-09-21 15:46:04 +020092 /*
93 * Disable interrupt on AIC5. We always take the lock of the
94 * first irq chip as all chips share the same registers.
95 */
96 irq_gc_lock(bgc);
Kevin Cernekee332fd7c2014-11-06 22:44:17 -080097 irq_reg_writel(gc, d->hwirq, AT91_AIC5_SSR);
98 irq_reg_writel(gc, 1, AT91_AIC5_IDCR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +020099 gc->mask_cache &= ~d->mask;
Ludovic Desrochesd32dc9a2015-09-21 15:46:04 +0200100 irq_gc_unlock(bgc);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200101}
102
103static void aic5_unmask(struct irq_data *d)
104{
105 struct irq_domain *domain = d->domain;
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +0200106 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
Ludovic Desrochesd32dc9a2015-09-21 15:46:04 +0200107 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200108
Ludovic Desrochesd32dc9a2015-09-21 15:46:04 +0200109 /*
110 * Enable interrupt on AIC5. We always take the lock of the
111 * first irq chip as all chips share the same registers.
112 */
113 irq_gc_lock(bgc);
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800114 irq_reg_writel(gc, d->hwirq, AT91_AIC5_SSR);
115 irq_reg_writel(gc, 1, AT91_AIC5_IECR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200116 gc->mask_cache |= d->mask;
Ludovic Desrochesd32dc9a2015-09-21 15:46:04 +0200117 irq_gc_unlock(bgc);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200118}
119
120static int aic5_retrigger(struct irq_data *d)
121{
122 struct irq_domain *domain = d->domain;
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +0200123 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200124
125 /* Enable interrupt on AIC5 */
Ludovic Desroches414a4312015-09-21 15:46:05 +0200126 irq_gc_lock(bgc);
127 irq_reg_writel(bgc, d->hwirq, AT91_AIC5_SSR);
128 irq_reg_writel(bgc, 1, AT91_AIC5_ISCR);
129 irq_gc_unlock(bgc);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200130
131 return 0;
132}
133
134static int aic5_set_type(struct irq_data *d, unsigned type)
135{
136 struct irq_domain *domain = d->domain;
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +0200137 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200138 unsigned int smr;
139 int ret;
140
Ludovic Desroches414a4312015-09-21 15:46:05 +0200141 irq_gc_lock(bgc);
142 irq_reg_writel(bgc, d->hwirq, AT91_AIC5_SSR);
143 smr = irq_reg_readl(bgc, AT91_AIC5_SMR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200144 ret = aic_common_set_type(d, type, &smr);
145 if (!ret)
Ludovic Desroches414a4312015-09-21 15:46:05 +0200146 irq_reg_writel(bgc, smr, AT91_AIC5_SMR);
147 irq_gc_unlock(bgc);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200148
149 return ret;
150}
151
152#ifdef CONFIG_PM
153static void aic5_suspend(struct irq_data *d)
154{
155 struct irq_domain *domain = d->domain;
156 struct irq_domain_chip_generic *dgc = domain->gc;
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +0200157 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200158 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
159 int i;
160 u32 mask;
161
162 irq_gc_lock(bgc);
163 for (i = 0; i < dgc->irqs_per_chip; i++) {
164 mask = 1 << i;
165 if ((mask & gc->mask_cache) == (mask & gc->wake_active))
166 continue;
167
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800168 irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200169 if (mask & gc->wake_active)
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800170 irq_reg_writel(bgc, 1, AT91_AIC5_IECR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200171 else
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800172 irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200173 }
174 irq_gc_unlock(bgc);
175}
176
177static void aic5_resume(struct irq_data *d)
178{
179 struct irq_domain *domain = d->domain;
180 struct irq_domain_chip_generic *dgc = domain->gc;
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +0200181 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200182 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
183 int i;
184 u32 mask;
185
186 irq_gc_lock(bgc);
187 for (i = 0; i < dgc->irqs_per_chip; i++) {
188 mask = 1 << i;
189 if ((mask & gc->mask_cache) == (mask & gc->wake_active))
190 continue;
191
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800192 irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200193 if (mask & gc->mask_cache)
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800194 irq_reg_writel(bgc, 1, AT91_AIC5_IECR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200195 else
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800196 irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200197 }
198 irq_gc_unlock(bgc);
199}
200
201static void aic5_pm_shutdown(struct irq_data *d)
202{
203 struct irq_domain *domain = d->domain;
204 struct irq_domain_chip_generic *dgc = domain->gc;
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +0200205 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(domain, 0);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200206 struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
207 int i;
208
209 irq_gc_lock(bgc);
210 for (i = 0; i < dgc->irqs_per_chip; i++) {
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800211 irq_reg_writel(bgc, i + gc->irq_base, AT91_AIC5_SSR);
212 irq_reg_writel(bgc, 1, AT91_AIC5_IDCR);
213 irq_reg_writel(bgc, 1, AT91_AIC5_ICCR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200214 }
215 irq_gc_unlock(bgc);
216}
217#else
218#define aic5_suspend NULL
219#define aic5_resume NULL
220#define aic5_pm_shutdown NULL
221#endif /* CONFIG_PM */
222
223static void __init aic5_hw_init(struct irq_domain *domain)
224{
225 struct irq_chip_generic *gc = irq_get_domain_generic_chip(domain, 0);
226 int i;
227
228 /*
229 * Perform 8 End Of Interrupt Command to make sure AIC
230 * will not Lock out nIRQ
231 */
232 for (i = 0; i < 8; i++)
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800233 irq_reg_writel(gc, 0, AT91_AIC5_EOICR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200234
235 /*
236 * Spurious Interrupt ID in Spurious Vector Register.
237 * When there is no current interrupt, the IRQ Vector Register
238 * reads the value stored in AIC_SPU
239 */
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800240 irq_reg_writel(gc, 0xffffffff, AT91_AIC5_SPU);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200241
242 /* No debugging in AIC: Debug (Protect) Control Register */
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800243 irq_reg_writel(gc, 0, AT91_AIC5_DCR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200244
245 /* Disable and clear all interrupts initially */
246 for (i = 0; i < domain->revmap_size; i++) {
Kevin Cernekee332fd7c2014-11-06 22:44:17 -0800247 irq_reg_writel(gc, i, AT91_AIC5_SSR);
248 irq_reg_writel(gc, i, AT91_AIC5_SVR);
249 irq_reg_writel(gc, 1, AT91_AIC5_IDCR);
250 irq_reg_writel(gc, 1, AT91_AIC5_ICCR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200251 }
252}
253
254static int aic5_irq_domain_xlate(struct irq_domain *d,
255 struct device_node *ctrlr,
256 const u32 *intspec, unsigned int intsize,
257 irq_hw_number_t *out_hwirq,
258 unsigned int *out_type)
259{
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +0200260 struct irq_chip_generic *bgc = irq_get_domain_generic_chip(d, 0);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200261 unsigned smr;
262 int ret;
263
Ludovic Desrochesb55a3bb2015-09-21 15:46:06 +0200264 if (!bgc)
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200265 return -EINVAL;
266
267 ret = aic_common_irq_domain_xlate(d, ctrlr, intspec, intsize,
268 out_hwirq, out_type);
269 if (ret)
270 return ret;
271
Ludovic Desroches414a4312015-09-21 15:46:05 +0200272 irq_gc_lock(bgc);
273 irq_reg_writel(bgc, *out_hwirq, AT91_AIC5_SSR);
274 smr = irq_reg_readl(bgc, AT91_AIC5_SMR);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200275 ret = aic_common_set_priority(intspec[2], &smr);
276 if (!ret)
Ludovic Desroches414a4312015-09-21 15:46:05 +0200277 irq_reg_writel(bgc, intspec[2] | smr, AT91_AIC5_SMR);
278 irq_gc_unlock(bgc);
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200279
280 return ret;
281}
282
283static const struct irq_domain_ops aic5_irq_ops = {
284 .map = irq_map_generic_chip,
285 .xlate = aic5_irq_domain_xlate,
286};
287
Boris BREZILLON6704d122014-07-10 20:25:41 +0200288static void __init sama5d3_aic_irq_fixup(struct device_node *root)
289{
290 aic_common_rtc_irq_fixup(root);
291}
292
Nicolas Pitrec3760232015-07-24 15:24:45 -0400293static const struct of_device_id aic5_irq_fixups[] __initconst = {
Boris BREZILLON6704d122014-07-10 20:25:41 +0200294 { .compatible = "atmel,sama5d3", .data = sama5d3_aic_irq_fixup },
Alexandre Belloni20afdeb2014-09-12 17:43:00 +0200295 { .compatible = "atmel,sama5d4", .data = sama5d3_aic_irq_fixup },
Boris BREZILLON6704d122014-07-10 20:25:41 +0200296 { /* sentinel */ },
297};
298
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200299static int __init aic5_of_init(struct device_node *node,
300 struct device_node *parent,
301 int nirqs)
302{
303 struct irq_chip_generic *gc;
304 struct irq_domain *domain;
305 int nchips;
306 int i;
307
308 if (nirqs > NR_AIC5_IRQS)
309 return -EINVAL;
310
311 if (aic5_domain)
312 return -EEXIST;
313
314 domain = aic_common_of_init(node, &aic5_irq_ops, "atmel-aic5",
315 nirqs);
316 if (IS_ERR(domain))
317 return PTR_ERR(domain);
318
Boris BREZILLON6704d122014-07-10 20:25:41 +0200319 aic_common_irq_fixup(aic5_irq_fixups);
320
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200321 aic5_domain = domain;
322 nchips = aic5_domain->revmap_size / 32;
323 for (i = 0; i < nchips; i++) {
324 gc = irq_get_domain_generic_chip(domain, i * 32);
325
326 gc->chip_types[0].regs.eoi = AT91_AIC5_EOICR;
327 gc->chip_types[0].chip.irq_mask = aic5_mask;
328 gc->chip_types[0].chip.irq_unmask = aic5_unmask;
329 gc->chip_types[0].chip.irq_retrigger = aic5_retrigger;
330 gc->chip_types[0].chip.irq_set_type = aic5_set_type;
331 gc->chip_types[0].chip.irq_suspend = aic5_suspend;
332 gc->chip_types[0].chip.irq_resume = aic5_resume;
333 gc->chip_types[0].chip.irq_pm_shutdown = aic5_pm_shutdown;
334 }
335
336 aic5_hw_init(domain);
337 set_handle_irq(aic5_handle);
338
339 return 0;
340}
341
Nicolas Ferre62a993d2015-06-18 15:07:35 +0200342#define NR_SAMA5D2_IRQS 77
343
344static int __init sama5d2_aic5_of_init(struct device_node *node,
345 struct device_node *parent)
346{
347 return aic5_of_init(node, parent, NR_SAMA5D2_IRQS);
348}
349IRQCHIP_DECLARE(sama5d2_aic5, "atmel,sama5d2-aic", sama5d2_aic5_of_init);
350
Alexandre Belloni0cae1652014-09-11 16:41:51 +0200351#define NR_SAMA5D3_IRQS 48
Boris BREZILLONb1479eb2014-07-10 19:14:18 +0200352
353static int __init sama5d3_aic5_of_init(struct device_node *node,
354 struct device_node *parent)
355{
356 return aic5_of_init(node, parent, NR_SAMA5D3_IRQS);
357}
358IRQCHIP_DECLARE(sama5d3_aic5, "atmel,sama5d3-aic", sama5d3_aic5_of_init);
Alexandre Belloni20afdeb2014-09-12 17:43:00 +0200359
360#define NR_SAMA5D4_IRQS 68
361
362static int __init sama5d4_aic5_of_init(struct device_node *node,
363 struct device_node *parent)
364{
365 return aic5_of_init(node, parent, NR_SAMA5D4_IRQS);
366}
367IRQCHIP_DECLARE(sama5d4_aic5, "atmel,sama5d4-aic", sama5d4_aic5_of_init);