1b1479ebbSBoris BREZILLON /*
2b1479ebbSBoris BREZILLON * Atmel AT91 AIC (Advanced Interrupt Controller) driver
3b1479ebbSBoris BREZILLON *
4b1479ebbSBoris BREZILLON * Copyright (C) 2004 SAN People
5b1479ebbSBoris BREZILLON * Copyright (C) 2004 ATMEL
6b1479ebbSBoris BREZILLON * Copyright (C) Rick Bronson
7b1479ebbSBoris BREZILLON * Copyright (C) 2014 Free Electrons
8b1479ebbSBoris BREZILLON *
9b1479ebbSBoris BREZILLON * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
10b1479ebbSBoris BREZILLON *
11b1479ebbSBoris BREZILLON * This file is licensed under the terms of the GNU General Public
12b1479ebbSBoris BREZILLON * License version 2. This program is licensed "as is" without any
13b1479ebbSBoris BREZILLON * warranty of any kind, whether express or implied.
14b1479ebbSBoris BREZILLON */
15b1479ebbSBoris BREZILLON
16b1479ebbSBoris BREZILLON #include <linux/init.h>
17b1479ebbSBoris BREZILLON #include <linux/module.h>
18b1479ebbSBoris BREZILLON #include <linux/mm.h>
19b1479ebbSBoris BREZILLON #include <linux/bitmap.h>
20b1479ebbSBoris BREZILLON #include <linux/types.h>
21b1479ebbSBoris BREZILLON #include <linux/irq.h>
2241a83e06SJoel Porquet #include <linux/irqchip.h>
23b1479ebbSBoris BREZILLON #include <linux/of.h>
24b1479ebbSBoris BREZILLON #include <linux/of_address.h>
25b1479ebbSBoris BREZILLON #include <linux/of_irq.h>
26b1479ebbSBoris BREZILLON #include <linux/irqdomain.h>
27b1479ebbSBoris BREZILLON #include <linux/err.h>
28b1479ebbSBoris BREZILLON #include <linux/slab.h>
29b1479ebbSBoris BREZILLON #include <linux/io.h>
30b1479ebbSBoris BREZILLON
31b1479ebbSBoris BREZILLON #include <asm/exception.h>
32b1479ebbSBoris BREZILLON #include <asm/mach/irq.h>
33b1479ebbSBoris BREZILLON
34b1479ebbSBoris BREZILLON #include "irq-atmel-aic-common.h"
35b1479ebbSBoris BREZILLON
36b1479ebbSBoris BREZILLON /* Number of irq lines managed by AIC */
37b1479ebbSBoris BREZILLON #define NR_AIC_IRQS 32
38b1479ebbSBoris BREZILLON
39b1479ebbSBoris BREZILLON #define AT91_AIC_SMR(n) ((n) * 4)
40b1479ebbSBoris BREZILLON
41b1479ebbSBoris BREZILLON #define AT91_AIC_SVR(n) (0x80 + ((n) * 4))
42b1479ebbSBoris BREZILLON #define AT91_AIC_IVR 0x100
43b1479ebbSBoris BREZILLON #define AT91_AIC_FVR 0x104
44b1479ebbSBoris BREZILLON #define AT91_AIC_ISR 0x108
45b1479ebbSBoris BREZILLON
46b1479ebbSBoris BREZILLON #define AT91_AIC_IPR 0x10c
47b1479ebbSBoris BREZILLON #define AT91_AIC_IMR 0x110
48b1479ebbSBoris BREZILLON #define AT91_AIC_CISR 0x114
49b1479ebbSBoris BREZILLON
50b1479ebbSBoris BREZILLON #define AT91_AIC_IECR 0x120
51b1479ebbSBoris BREZILLON #define AT91_AIC_IDCR 0x124
52b1479ebbSBoris BREZILLON #define AT91_AIC_ICCR 0x128
53b1479ebbSBoris BREZILLON #define AT91_AIC_ISCR 0x12c
54b1479ebbSBoris BREZILLON #define AT91_AIC_EOICR 0x130
55b1479ebbSBoris BREZILLON #define AT91_AIC_SPU 0x134
56b1479ebbSBoris BREZILLON #define AT91_AIC_DCR 0x138
57b1479ebbSBoris BREZILLON
58b1479ebbSBoris BREZILLON static struct irq_domain *aic_domain;
59b1479ebbSBoris BREZILLON
60b1479ebbSBoris BREZILLON static asmlinkage void __exception_irq_entry
aic_handle(struct pt_regs * regs)61b1479ebbSBoris BREZILLON aic_handle(struct pt_regs *regs)
62b1479ebbSBoris BREZILLON {
63b1479ebbSBoris BREZILLON struct irq_domain_chip_generic *dgc = aic_domain->gc;
64b1479ebbSBoris BREZILLON struct irq_chip_generic *gc = dgc->gc[0];
65b1479ebbSBoris BREZILLON u32 irqnr;
66b1479ebbSBoris BREZILLON u32 irqstat;
67b1479ebbSBoris BREZILLON
68332fd7c4SKevin Cernekee irqnr = irq_reg_readl(gc, AT91_AIC_IVR);
69332fd7c4SKevin Cernekee irqstat = irq_reg_readl(gc, AT91_AIC_ISR);
70b1479ebbSBoris BREZILLON
71b1479ebbSBoris BREZILLON if (!irqstat)
72332fd7c4SKevin Cernekee irq_reg_writel(gc, 0, AT91_AIC_EOICR);
73b1479ebbSBoris BREZILLON else
74*0953fb26SMark Rutland generic_handle_domain_irq(aic_domain, irqnr);
75b1479ebbSBoris BREZILLON }
76b1479ebbSBoris BREZILLON
aic_retrigger(struct irq_data * d)77b1479ebbSBoris BREZILLON static int aic_retrigger(struct irq_data *d)
78b1479ebbSBoris BREZILLON {
79b1479ebbSBoris BREZILLON struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
80b1479ebbSBoris BREZILLON
81b1479ebbSBoris BREZILLON /* Enable interrupt on AIC5 */
82b1479ebbSBoris BREZILLON irq_gc_lock(gc);
83332fd7c4SKevin Cernekee irq_reg_writel(gc, d->mask, AT91_AIC_ISCR);
84b1479ebbSBoris BREZILLON irq_gc_unlock(gc);
85b1479ebbSBoris BREZILLON
867177144aSMarc Zyngier return 1;
87b1479ebbSBoris BREZILLON }
88b1479ebbSBoris BREZILLON
aic_set_type(struct irq_data * d,unsigned type)89b1479ebbSBoris BREZILLON static int aic_set_type(struct irq_data *d, unsigned type)
90b1479ebbSBoris BREZILLON {
91b1479ebbSBoris BREZILLON struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
92b1479ebbSBoris BREZILLON unsigned int smr;
93b1479ebbSBoris BREZILLON int ret;
94b1479ebbSBoris BREZILLON
95332fd7c4SKevin Cernekee smr = irq_reg_readl(gc, AT91_AIC_SMR(d->hwirq));
96b1479ebbSBoris BREZILLON ret = aic_common_set_type(d, type, &smr);
97b1479ebbSBoris BREZILLON if (ret)
98b1479ebbSBoris BREZILLON return ret;
99b1479ebbSBoris BREZILLON
100332fd7c4SKevin Cernekee irq_reg_writel(gc, smr, AT91_AIC_SMR(d->hwirq));
101b1479ebbSBoris BREZILLON
102b1479ebbSBoris BREZILLON return 0;
103b1479ebbSBoris BREZILLON }
104b1479ebbSBoris BREZILLON
105b1479ebbSBoris BREZILLON #ifdef CONFIG_PM
aic_suspend(struct irq_data * d)106b1479ebbSBoris BREZILLON static void aic_suspend(struct irq_data *d)
107b1479ebbSBoris BREZILLON {
108b1479ebbSBoris BREZILLON struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
109b1479ebbSBoris BREZILLON
110b1479ebbSBoris BREZILLON irq_gc_lock(gc);
111332fd7c4SKevin Cernekee irq_reg_writel(gc, gc->mask_cache, AT91_AIC_IDCR);
112332fd7c4SKevin Cernekee irq_reg_writel(gc, gc->wake_active, AT91_AIC_IECR);
113b1479ebbSBoris BREZILLON irq_gc_unlock(gc);
114b1479ebbSBoris BREZILLON }
115b1479ebbSBoris BREZILLON
aic_resume(struct irq_data * d)116b1479ebbSBoris BREZILLON static void aic_resume(struct irq_data *d)
117b1479ebbSBoris BREZILLON {
118b1479ebbSBoris BREZILLON struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
119b1479ebbSBoris BREZILLON
120b1479ebbSBoris BREZILLON irq_gc_lock(gc);
121332fd7c4SKevin Cernekee irq_reg_writel(gc, gc->wake_active, AT91_AIC_IDCR);
122332fd7c4SKevin Cernekee irq_reg_writel(gc, gc->mask_cache, AT91_AIC_IECR);
123b1479ebbSBoris BREZILLON irq_gc_unlock(gc);
124b1479ebbSBoris BREZILLON }
125b1479ebbSBoris BREZILLON
aic_pm_shutdown(struct irq_data * d)126b1479ebbSBoris BREZILLON static void aic_pm_shutdown(struct irq_data *d)
127b1479ebbSBoris BREZILLON {
128b1479ebbSBoris BREZILLON struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
129b1479ebbSBoris BREZILLON
130b1479ebbSBoris BREZILLON irq_gc_lock(gc);
131332fd7c4SKevin Cernekee irq_reg_writel(gc, 0xffffffff, AT91_AIC_IDCR);
132332fd7c4SKevin Cernekee irq_reg_writel(gc, 0xffffffff, AT91_AIC_ICCR);
133b1479ebbSBoris BREZILLON irq_gc_unlock(gc);
134b1479ebbSBoris BREZILLON }
135b1479ebbSBoris BREZILLON #else
136b1479ebbSBoris BREZILLON #define aic_suspend NULL
137b1479ebbSBoris BREZILLON #define aic_resume NULL
138b1479ebbSBoris BREZILLON #define aic_pm_shutdown NULL
139b1479ebbSBoris BREZILLON #endif /* CONFIG_PM */
140b1479ebbSBoris BREZILLON
aic_hw_init(struct irq_domain * domain)141b1479ebbSBoris BREZILLON static void __init aic_hw_init(struct irq_domain *domain)
142b1479ebbSBoris BREZILLON {
143b1479ebbSBoris BREZILLON struct irq_chip_generic *gc = irq_get_domain_generic_chip(domain, 0);
144b1479ebbSBoris BREZILLON int i;
145b1479ebbSBoris BREZILLON
146b1479ebbSBoris BREZILLON /*
147b1479ebbSBoris BREZILLON * Perform 8 End Of Interrupt Command to make sure AIC
148b1479ebbSBoris BREZILLON * will not Lock out nIRQ
149b1479ebbSBoris BREZILLON */
150b1479ebbSBoris BREZILLON for (i = 0; i < 8; i++)
151332fd7c4SKevin Cernekee irq_reg_writel(gc, 0, AT91_AIC_EOICR);
152b1479ebbSBoris BREZILLON
153b1479ebbSBoris BREZILLON /*
154b1479ebbSBoris BREZILLON * Spurious Interrupt ID in Spurious Vector Register.
155b1479ebbSBoris BREZILLON * When there is no current interrupt, the IRQ Vector Register
156b1479ebbSBoris BREZILLON * reads the value stored in AIC_SPU
157b1479ebbSBoris BREZILLON */
158332fd7c4SKevin Cernekee irq_reg_writel(gc, 0xffffffff, AT91_AIC_SPU);
159b1479ebbSBoris BREZILLON
160b1479ebbSBoris BREZILLON /* No debugging in AIC: Debug (Protect) Control Register */
161332fd7c4SKevin Cernekee irq_reg_writel(gc, 0, AT91_AIC_DCR);
162b1479ebbSBoris BREZILLON
163b1479ebbSBoris BREZILLON /* Disable and clear all interrupts initially */
164332fd7c4SKevin Cernekee irq_reg_writel(gc, 0xffffffff, AT91_AIC_IDCR);
165332fd7c4SKevin Cernekee irq_reg_writel(gc, 0xffffffff, AT91_AIC_ICCR);
166b1479ebbSBoris BREZILLON
167b1479ebbSBoris BREZILLON for (i = 0; i < 32; i++)
168332fd7c4SKevin Cernekee irq_reg_writel(gc, i, AT91_AIC_SVR(i));
169b1479ebbSBoris BREZILLON }
170b1479ebbSBoris BREZILLON
aic_irq_domain_xlate(struct irq_domain * d,struct device_node * ctrlr,const u32 * intspec,unsigned int intsize,irq_hw_number_t * out_hwirq,unsigned int * out_type)171b1479ebbSBoris BREZILLON static int aic_irq_domain_xlate(struct irq_domain *d,
172b1479ebbSBoris BREZILLON struct device_node *ctrlr,
173b1479ebbSBoris BREZILLON const u32 *intspec, unsigned int intsize,
174b1479ebbSBoris BREZILLON irq_hw_number_t *out_hwirq,
175b1479ebbSBoris BREZILLON unsigned int *out_type)
176b1479ebbSBoris BREZILLON {
177b1479ebbSBoris BREZILLON struct irq_domain_chip_generic *dgc = d->gc;
178b1479ebbSBoris BREZILLON struct irq_chip_generic *gc;
1795eb0d6ebSBoris Brezillon unsigned long flags;
180b1479ebbSBoris BREZILLON unsigned smr;
181b1479ebbSBoris BREZILLON int idx;
182b1479ebbSBoris BREZILLON int ret;
183b1479ebbSBoris BREZILLON
184b1479ebbSBoris BREZILLON if (!dgc)
185b1479ebbSBoris BREZILLON return -EINVAL;
186b1479ebbSBoris BREZILLON
187b1479ebbSBoris BREZILLON ret = aic_common_irq_domain_xlate(d, ctrlr, intspec, intsize,
188b1479ebbSBoris BREZILLON out_hwirq, out_type);
189b1479ebbSBoris BREZILLON if (ret)
190b1479ebbSBoris BREZILLON return ret;
191b1479ebbSBoris BREZILLON
192b1479ebbSBoris BREZILLON idx = intspec[0] / dgc->irqs_per_chip;
193b1479ebbSBoris BREZILLON if (idx >= dgc->num_chips)
194b1479ebbSBoris BREZILLON return -EINVAL;
195b1479ebbSBoris BREZILLON
196b1479ebbSBoris BREZILLON gc = dgc->gc[idx];
197b1479ebbSBoris BREZILLON
1985eb0d6ebSBoris Brezillon irq_gc_lock_irqsave(gc, flags);
199332fd7c4SKevin Cernekee smr = irq_reg_readl(gc, AT91_AIC_SMR(*out_hwirq));
2005fd26a0bSMilo Kim aic_common_set_priority(intspec[2], &smr);
201332fd7c4SKevin Cernekee irq_reg_writel(gc, smr, AT91_AIC_SMR(*out_hwirq));
2025eb0d6ebSBoris Brezillon irq_gc_unlock_irqrestore(gc, flags);
203b1479ebbSBoris BREZILLON
204b1479ebbSBoris BREZILLON return ret;
205b1479ebbSBoris BREZILLON }
206b1479ebbSBoris BREZILLON
207b1479ebbSBoris BREZILLON static const struct irq_domain_ops aic_irq_ops = {
208b1479ebbSBoris BREZILLON .map = irq_map_generic_chip,
209b1479ebbSBoris BREZILLON .xlate = aic_irq_domain_xlate,
210b1479ebbSBoris BREZILLON };
211b1479ebbSBoris BREZILLON
at91rm9200_aic_irq_fixup(void)2120a46230bSBoris Brezillon static void __init at91rm9200_aic_irq_fixup(void)
2136704d12dSBoris BREZILLON {
2140a46230bSBoris Brezillon aic_common_rtc_irq_fixup();
2156704d12dSBoris BREZILLON }
2166704d12dSBoris BREZILLON
at91sam9260_aic_irq_fixup(void)2170a46230bSBoris Brezillon static void __init at91sam9260_aic_irq_fixup(void)
218ae25eac2SBoris BREZILLON {
2190a46230bSBoris Brezillon aic_common_rtt_irq_fixup();
220ae25eac2SBoris BREZILLON }
221ae25eac2SBoris BREZILLON
at91sam9g45_aic_irq_fixup(void)2220a46230bSBoris Brezillon static void __init at91sam9g45_aic_irq_fixup(void)
223f3b7bf1bSBoris BREZILLON {
2240a46230bSBoris Brezillon aic_common_rtc_irq_fixup();
2250a46230bSBoris Brezillon aic_common_rtt_irq_fixup();
226f3b7bf1bSBoris BREZILLON }
227f3b7bf1bSBoris BREZILLON
228c376023bSNicolas Pitre static const struct of_device_id aic_irq_fixups[] __initconst = {
22925963dbdSBoris BREZILLON { .compatible = "atmel,at91rm9200", .data = at91rm9200_aic_irq_fixup },
230f3b7bf1bSBoris BREZILLON { .compatible = "atmel,at91sam9g45", .data = at91sam9g45_aic_irq_fixup },
231624cba57SBoris BREZILLON { .compatible = "atmel,at91sam9n12", .data = at91rm9200_aic_irq_fixup },
232f3b7bf1bSBoris BREZILLON { .compatible = "atmel,at91sam9rl", .data = at91sam9g45_aic_irq_fixup },
233624cba57SBoris BREZILLON { .compatible = "atmel,at91sam9x5", .data = at91rm9200_aic_irq_fixup },
234ae25eac2SBoris BREZILLON { .compatible = "atmel,at91sam9260", .data = at91sam9260_aic_irq_fixup },
235ae25eac2SBoris BREZILLON { .compatible = "atmel,at91sam9261", .data = at91sam9260_aic_irq_fixup },
236ae25eac2SBoris BREZILLON { .compatible = "atmel,at91sam9263", .data = at91sam9260_aic_irq_fixup },
237ae25eac2SBoris BREZILLON { .compatible = "atmel,at91sam9g20", .data = at91sam9260_aic_irq_fixup },
2386704d12dSBoris BREZILLON { /* sentinel */ },
2396704d12dSBoris BREZILLON };
2406704d12dSBoris BREZILLON
aic_of_init(struct device_node * node,struct device_node * parent)241b1479ebbSBoris BREZILLON static int __init aic_of_init(struct device_node *node,
242b1479ebbSBoris BREZILLON struct device_node *parent)
243b1479ebbSBoris BREZILLON {
244b1479ebbSBoris BREZILLON struct irq_chip_generic *gc;
245b1479ebbSBoris BREZILLON struct irq_domain *domain;
246b1479ebbSBoris BREZILLON
247b1479ebbSBoris BREZILLON if (aic_domain)
248b1479ebbSBoris BREZILLON return -EEXIST;
249b1479ebbSBoris BREZILLON
250b1479ebbSBoris BREZILLON domain = aic_common_of_init(node, &aic_irq_ops, "atmel-aic",
251dd85c791SMilo Kim NR_AIC_IRQS, aic_irq_fixups);
252b1479ebbSBoris BREZILLON if (IS_ERR(domain))
253b1479ebbSBoris BREZILLON return PTR_ERR(domain);
254b1479ebbSBoris BREZILLON
255b1479ebbSBoris BREZILLON aic_domain = domain;
256b1479ebbSBoris BREZILLON gc = irq_get_domain_generic_chip(domain, 0);
257b1479ebbSBoris BREZILLON
258b1479ebbSBoris BREZILLON gc->chip_types[0].regs.eoi = AT91_AIC_EOICR;
259b1479ebbSBoris BREZILLON gc->chip_types[0].regs.enable = AT91_AIC_IECR;
260b1479ebbSBoris BREZILLON gc->chip_types[0].regs.disable = AT91_AIC_IDCR;
261b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_mask = irq_gc_mask_disable_reg;
262b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_unmask = irq_gc_unmask_enable_reg;
263b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_retrigger = aic_retrigger;
264b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_set_type = aic_set_type;
265b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_suspend = aic_suspend;
266b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_resume = aic_resume;
267b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_pm_shutdown = aic_pm_shutdown;
268b1479ebbSBoris BREZILLON
269b1479ebbSBoris BREZILLON aic_hw_init(domain);
270b1479ebbSBoris BREZILLON set_handle_irq(aic_handle);
271b1479ebbSBoris BREZILLON
272b1479ebbSBoris BREZILLON return 0;
273b1479ebbSBoris BREZILLON }
274b1479ebbSBoris BREZILLON IRQCHIP_DECLARE(at91rm9200_aic, "atmel,at91rm9200-aic", aic_of_init);
275