1b1479ebbSBoris BREZILLON /*
2b1479ebbSBoris BREZILLON * Atmel AT91 common AIC (Advanced Interrupt Controller) code shared by
3b1479ebbSBoris BREZILLON * irq-atmel-aic and irq-atmel-aic5 drivers
4b1479ebbSBoris BREZILLON *
5b1479ebbSBoris BREZILLON * Copyright (C) 2004 SAN People
6b1479ebbSBoris BREZILLON * Copyright (C) 2004 ATMEL
7b1479ebbSBoris BREZILLON * Copyright (C) Rick Bronson
8b1479ebbSBoris BREZILLON * Copyright (C) 2014 Free Electrons
9b1479ebbSBoris BREZILLON *
10b1479ebbSBoris BREZILLON * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
11b1479ebbSBoris BREZILLON *
12b1479ebbSBoris BREZILLON * This file is licensed under the terms of the GNU General Public
13b1479ebbSBoris BREZILLON * License version 2. This program is licensed "as is" without any
14b1479ebbSBoris BREZILLON * warranty of any kind, whether express or implied.
15b1479ebbSBoris BREZILLON */
16b1479ebbSBoris BREZILLON
17b1479ebbSBoris BREZILLON #include <linux/errno.h>
18b1479ebbSBoris BREZILLON #include <linux/io.h>
19b1479ebbSBoris BREZILLON #include <linux/irq.h>
20b1479ebbSBoris BREZILLON #include <linux/irqdomain.h>
21b1479ebbSBoris BREZILLON #include <linux/of.h>
22b1479ebbSBoris BREZILLON #include <linux/of_address.h>
23b1479ebbSBoris BREZILLON #include <linux/slab.h>
24b1479ebbSBoris BREZILLON
25b1479ebbSBoris BREZILLON #include "irq-atmel-aic-common.h"
26b1479ebbSBoris BREZILLON
27b1479ebbSBoris BREZILLON #define AT91_AIC_PRIOR GENMASK(2, 0)
28b1479ebbSBoris BREZILLON #define AT91_AIC_IRQ_MIN_PRIORITY 0
29b1479ebbSBoris BREZILLON #define AT91_AIC_IRQ_MAX_PRIORITY 7
30b1479ebbSBoris BREZILLON
3191d11792SGavin Li #define AT91_AIC_SRCTYPE GENMASK(6, 5)
32b1479ebbSBoris BREZILLON #define AT91_AIC_SRCTYPE_LOW (0 << 5)
33b1479ebbSBoris BREZILLON #define AT91_AIC_SRCTYPE_FALLING (1 << 5)
34b1479ebbSBoris BREZILLON #define AT91_AIC_SRCTYPE_HIGH (2 << 5)
35b1479ebbSBoris BREZILLON #define AT91_AIC_SRCTYPE_RISING (3 << 5)
36b1479ebbSBoris BREZILLON
37b1479ebbSBoris BREZILLON struct aic_chip_data {
38b1479ebbSBoris BREZILLON u32 ext_irqs;
39b1479ebbSBoris BREZILLON };
40b1479ebbSBoris BREZILLON
aic_common_shutdown(struct irq_data * d)41b1479ebbSBoris BREZILLON static void aic_common_shutdown(struct irq_data *d)
42b1479ebbSBoris BREZILLON {
43b1479ebbSBoris BREZILLON struct irq_chip_type *ct = irq_data_get_chip_type(d);
44b1479ebbSBoris BREZILLON
45b1479ebbSBoris BREZILLON ct->chip.irq_mask(d);
46b1479ebbSBoris BREZILLON }
47b1479ebbSBoris BREZILLON
aic_common_set_type(struct irq_data * d,unsigned type,unsigned * val)48b1479ebbSBoris BREZILLON int aic_common_set_type(struct irq_data *d, unsigned type, unsigned *val)
49b1479ebbSBoris BREZILLON {
50b1479ebbSBoris BREZILLON struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
51b1479ebbSBoris BREZILLON struct aic_chip_data *aic = gc->private;
52b1479ebbSBoris BREZILLON unsigned aic_type;
53b1479ebbSBoris BREZILLON
54b1479ebbSBoris BREZILLON switch (type) {
55b1479ebbSBoris BREZILLON case IRQ_TYPE_LEVEL_HIGH:
56b1479ebbSBoris BREZILLON aic_type = AT91_AIC_SRCTYPE_HIGH;
57b1479ebbSBoris BREZILLON break;
58b1479ebbSBoris BREZILLON case IRQ_TYPE_EDGE_RISING:
59b1479ebbSBoris BREZILLON aic_type = AT91_AIC_SRCTYPE_RISING;
60b1479ebbSBoris BREZILLON break;
61b1479ebbSBoris BREZILLON case IRQ_TYPE_LEVEL_LOW:
62b1479ebbSBoris BREZILLON if (!(d->mask & aic->ext_irqs))
63b1479ebbSBoris BREZILLON return -EINVAL;
64b1479ebbSBoris BREZILLON
65b1479ebbSBoris BREZILLON aic_type = AT91_AIC_SRCTYPE_LOW;
66b1479ebbSBoris BREZILLON break;
67b1479ebbSBoris BREZILLON case IRQ_TYPE_EDGE_FALLING:
68b1479ebbSBoris BREZILLON if (!(d->mask & aic->ext_irqs))
69b1479ebbSBoris BREZILLON return -EINVAL;
70b1479ebbSBoris BREZILLON
71b1479ebbSBoris BREZILLON aic_type = AT91_AIC_SRCTYPE_FALLING;
72b1479ebbSBoris BREZILLON break;
73b1479ebbSBoris BREZILLON default:
74b1479ebbSBoris BREZILLON return -EINVAL;
75b1479ebbSBoris BREZILLON }
76b1479ebbSBoris BREZILLON
7791d11792SGavin Li *val &= ~AT91_AIC_SRCTYPE;
78b1479ebbSBoris BREZILLON *val |= aic_type;
79b1479ebbSBoris BREZILLON
80b1479ebbSBoris BREZILLON return 0;
81b1479ebbSBoris BREZILLON }
82b1479ebbSBoris BREZILLON
aic_common_set_priority(int priority,unsigned * val)835fd26a0bSMilo Kim void aic_common_set_priority(int priority, unsigned *val)
84b1479ebbSBoris BREZILLON {
8549f34134SMilo Kim *val &= ~AT91_AIC_PRIOR;
86b1479ebbSBoris BREZILLON *val |= priority;
87b1479ebbSBoris BREZILLON }
88b1479ebbSBoris BREZILLON
aic_common_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)89b1479ebbSBoris BREZILLON int aic_common_irq_domain_xlate(struct irq_domain *d,
90b1479ebbSBoris BREZILLON struct device_node *ctrlr,
91b1479ebbSBoris BREZILLON const u32 *intspec,
92b1479ebbSBoris BREZILLON unsigned int intsize,
93b1479ebbSBoris BREZILLON irq_hw_number_t *out_hwirq,
94b1479ebbSBoris BREZILLON unsigned int *out_type)
95b1479ebbSBoris BREZILLON {
96b1479ebbSBoris BREZILLON if (WARN_ON(intsize < 3))
97b1479ebbSBoris BREZILLON return -EINVAL;
98b1479ebbSBoris BREZILLON
99b1479ebbSBoris BREZILLON if (WARN_ON((intspec[2] < AT91_AIC_IRQ_MIN_PRIORITY) ||
100b1479ebbSBoris BREZILLON (intspec[2] > AT91_AIC_IRQ_MAX_PRIORITY)))
101b1479ebbSBoris BREZILLON return -EINVAL;
102b1479ebbSBoris BREZILLON
103b1479ebbSBoris BREZILLON *out_hwirq = intspec[0];
104b1479ebbSBoris BREZILLON *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
105b1479ebbSBoris BREZILLON
106b1479ebbSBoris BREZILLON return 0;
107b1479ebbSBoris BREZILLON }
108b1479ebbSBoris BREZILLON
aic_common_ext_irq_of_init(struct irq_domain * domain)109b1479ebbSBoris BREZILLON static void __init aic_common_ext_irq_of_init(struct irq_domain *domain)
110b1479ebbSBoris BREZILLON {
1115d4c9bc7SMarc Zyngier struct device_node *node = irq_domain_get_of_node(domain);
112b1479ebbSBoris BREZILLON struct irq_chip_generic *gc;
113b1479ebbSBoris BREZILLON struct aic_chip_data *aic;
114b1479ebbSBoris BREZILLON struct property *prop;
115b1479ebbSBoris BREZILLON const __be32 *p;
116b1479ebbSBoris BREZILLON u32 hwirq;
117b1479ebbSBoris BREZILLON
118b1479ebbSBoris BREZILLON gc = irq_get_domain_generic_chip(domain, 0);
119b1479ebbSBoris BREZILLON
120b1479ebbSBoris BREZILLON aic = gc->private;
121b1479ebbSBoris BREZILLON aic->ext_irqs |= 1;
122b1479ebbSBoris BREZILLON
123b1479ebbSBoris BREZILLON of_property_for_each_u32(node, "atmel,external-irqs", prop, p, hwirq) {
124b1479ebbSBoris BREZILLON gc = irq_get_domain_generic_chip(domain, hwirq);
125b1479ebbSBoris BREZILLON if (!gc) {
126b1479ebbSBoris BREZILLON pr_warn("AIC: external irq %d >= %d skip it\n",
127b1479ebbSBoris BREZILLON hwirq, domain->revmap_size);
128b1479ebbSBoris BREZILLON continue;
129b1479ebbSBoris BREZILLON }
130b1479ebbSBoris BREZILLON
131b1479ebbSBoris BREZILLON aic = gc->private;
132b1479ebbSBoris BREZILLON aic->ext_irqs |= (1 << (hwirq % 32));
133b1479ebbSBoris BREZILLON }
134b1479ebbSBoris BREZILLON }
135b1479ebbSBoris BREZILLON
1363d61467fSBoris BREZILLON #define AT91_RTC_IDR 0x24
1373d61467fSBoris BREZILLON #define AT91_RTC_IMR 0x28
1383d61467fSBoris BREZILLON #define AT91_RTC_IRQ_MASK 0x1f
1393d61467fSBoris BREZILLON
aic_common_rtc_irq_fixup(void)140*0a46230bSBoris Brezillon void __init aic_common_rtc_irq_fixup(void)
1413d61467fSBoris BREZILLON {
1423d61467fSBoris BREZILLON struct device_node *np;
1433d61467fSBoris BREZILLON void __iomem *regs;
1443d61467fSBoris BREZILLON
145277867adSBoris Brezillon np = of_find_compatible_node(NULL, NULL, "atmel,at91rm9200-rtc");
1463d61467fSBoris BREZILLON if (!np)
147277867adSBoris Brezillon np = of_find_compatible_node(NULL, NULL,
1483d61467fSBoris BREZILLON "atmel,at91sam9x5-rtc");
1493d61467fSBoris BREZILLON
1503d61467fSBoris BREZILLON if (!np)
1513d61467fSBoris BREZILLON return;
1523d61467fSBoris BREZILLON
1533d61467fSBoris BREZILLON regs = of_iomap(np, 0);
1543d61467fSBoris BREZILLON of_node_put(np);
1553d61467fSBoris BREZILLON
1563d61467fSBoris BREZILLON if (!regs)
1573d61467fSBoris BREZILLON return;
1583d61467fSBoris BREZILLON
1593d61467fSBoris BREZILLON writel(AT91_RTC_IRQ_MASK, regs + AT91_RTC_IDR);
1603d61467fSBoris BREZILLON
1613d61467fSBoris BREZILLON iounmap(regs);
1623d61467fSBoris BREZILLON }
1633d61467fSBoris BREZILLON
1644185315aSBoris BREZILLON #define AT91_RTT_MR 0x00 /* Real-time Mode Register */
1654185315aSBoris BREZILLON #define AT91_RTT_ALMIEN (1 << 16) /* Alarm Interrupt Enable */
1664185315aSBoris BREZILLON #define AT91_RTT_RTTINCIEN (1 << 17) /* Real Time Timer Increment Interrupt Enable */
1674185315aSBoris BREZILLON
aic_common_rtt_irq_fixup(void)168*0a46230bSBoris Brezillon void __init aic_common_rtt_irq_fixup(void)
1694185315aSBoris BREZILLON {
1704185315aSBoris BREZILLON struct device_node *np;
1714185315aSBoris BREZILLON void __iomem *regs;
1724185315aSBoris BREZILLON
1734185315aSBoris BREZILLON /*
1744185315aSBoris BREZILLON * The at91sam9263 SoC has 2 instances of the RTT block, hence we
1754185315aSBoris BREZILLON * iterate over the DT to find each occurrence.
1764185315aSBoris BREZILLON */
1774185315aSBoris BREZILLON for_each_compatible_node(np, NULL, "atmel,at91sam9260-rtt") {
1784185315aSBoris BREZILLON regs = of_iomap(np, 0);
1794185315aSBoris BREZILLON if (!regs)
1804185315aSBoris BREZILLON continue;
1814185315aSBoris BREZILLON
1824185315aSBoris BREZILLON writel(readl(regs + AT91_RTT_MR) &
1834185315aSBoris BREZILLON ~(AT91_RTT_ALMIEN | AT91_RTT_RTTINCIEN),
1844185315aSBoris BREZILLON regs + AT91_RTT_MR);
1854185315aSBoris BREZILLON
1864185315aSBoris BREZILLON iounmap(regs);
1874185315aSBoris BREZILLON }
1884185315aSBoris BREZILLON }
1894185315aSBoris BREZILLON
aic_common_irq_fixup(const struct of_device_id * matches)190dd85c791SMilo Kim static void __init aic_common_irq_fixup(const struct of_device_id *matches)
191b2f579b5SBoris BREZILLON {
192b2f579b5SBoris BREZILLON struct device_node *root = of_find_node_by_path("/");
193b2f579b5SBoris BREZILLON const struct of_device_id *match;
194b2f579b5SBoris BREZILLON
195b2f579b5SBoris BREZILLON if (!root)
196b2f579b5SBoris BREZILLON return;
197b2f579b5SBoris BREZILLON
198b2f579b5SBoris BREZILLON match = of_match_node(matches, root);
199b2f579b5SBoris BREZILLON
200b2f579b5SBoris BREZILLON if (match) {
201*0a46230bSBoris Brezillon void (*fixup)(void) = match->data;
202*0a46230bSBoris Brezillon fixup();
203b2f579b5SBoris BREZILLON }
204b2f579b5SBoris BREZILLON
205b2f579b5SBoris BREZILLON of_node_put(root);
206b2f579b5SBoris BREZILLON }
207b2f579b5SBoris BREZILLON
aic_common_of_init(struct device_node * node,const struct irq_domain_ops * ops,const char * name,int nirqs,const struct of_device_id * matches)208b1479ebbSBoris BREZILLON struct irq_domain *__init aic_common_of_init(struct device_node *node,
209b1479ebbSBoris BREZILLON const struct irq_domain_ops *ops,
210dd85c791SMilo Kim const char *name, int nirqs,
211dd85c791SMilo Kim const struct of_device_id *matches)
212b1479ebbSBoris BREZILLON {
213b1479ebbSBoris BREZILLON struct irq_chip_generic *gc;
214b1479ebbSBoris BREZILLON struct irq_domain *domain;
215b1479ebbSBoris BREZILLON struct aic_chip_data *aic;
216b1479ebbSBoris BREZILLON void __iomem *reg_base;
217b1479ebbSBoris BREZILLON int nchips;
218b1479ebbSBoris BREZILLON int ret;
219b1479ebbSBoris BREZILLON int i;
220b1479ebbSBoris BREZILLON
221b1479ebbSBoris BREZILLON nchips = DIV_ROUND_UP(nirqs, 32);
222b1479ebbSBoris BREZILLON
223b1479ebbSBoris BREZILLON reg_base = of_iomap(node, 0);
224b1479ebbSBoris BREZILLON if (!reg_base)
225b1479ebbSBoris BREZILLON return ERR_PTR(-ENOMEM);
226b1479ebbSBoris BREZILLON
227b1479ebbSBoris BREZILLON aic = kcalloc(nchips, sizeof(*aic), GFP_KERNEL);
228b1479ebbSBoris BREZILLON if (!aic) {
229b1479ebbSBoris BREZILLON ret = -ENOMEM;
230b1479ebbSBoris BREZILLON goto err_iounmap;
231b1479ebbSBoris BREZILLON }
232b1479ebbSBoris BREZILLON
233b1479ebbSBoris BREZILLON domain = irq_domain_add_linear(node, nchips * 32, ops, aic);
234b1479ebbSBoris BREZILLON if (!domain) {
235b1479ebbSBoris BREZILLON ret = -ENOMEM;
236b1479ebbSBoris BREZILLON goto err_free_aic;
237b1479ebbSBoris BREZILLON }
238b1479ebbSBoris BREZILLON
239b1479ebbSBoris BREZILLON ret = irq_alloc_domain_generic_chips(domain, 32, 1, name,
24045977fe3SBoris Brezillon handle_fasteoi_irq,
24145977fe3SBoris Brezillon IRQ_NOREQUEST | IRQ_NOPROBE |
24245977fe3SBoris Brezillon IRQ_NOAUTOEN, 0, 0);
243b1479ebbSBoris BREZILLON if (ret)
244b1479ebbSBoris BREZILLON goto err_domain_remove;
245b1479ebbSBoris BREZILLON
246b1479ebbSBoris BREZILLON for (i = 0; i < nchips; i++) {
247b1479ebbSBoris BREZILLON gc = irq_get_domain_generic_chip(domain, i * 32);
248b1479ebbSBoris BREZILLON
249b1479ebbSBoris BREZILLON gc->reg_base = reg_base;
250b1479ebbSBoris BREZILLON
251b1479ebbSBoris BREZILLON gc->unused = 0;
252b1479ebbSBoris BREZILLON gc->wake_enabled = ~0;
253b1479ebbSBoris BREZILLON gc->chip_types[0].type = IRQ_TYPE_SENSE_MASK;
254b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_eoi = irq_gc_eoi;
255b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
256b1479ebbSBoris BREZILLON gc->chip_types[0].chip.irq_shutdown = aic_common_shutdown;
257b1479ebbSBoris BREZILLON gc->private = &aic[i];
258b1479ebbSBoris BREZILLON }
259b1479ebbSBoris BREZILLON
260b1479ebbSBoris BREZILLON aic_common_ext_irq_of_init(domain);
261dd85c791SMilo Kim aic_common_irq_fixup(matches);
262b1479ebbSBoris BREZILLON
263b1479ebbSBoris BREZILLON return domain;
264b1479ebbSBoris BREZILLON
265b1479ebbSBoris BREZILLON err_domain_remove:
266b1479ebbSBoris BREZILLON irq_domain_remove(domain);
267b1479ebbSBoris BREZILLON
268b1479ebbSBoris BREZILLON err_free_aic:
269b1479ebbSBoris BREZILLON kfree(aic);
270b1479ebbSBoris BREZILLON
271b1479ebbSBoris BREZILLON err_iounmap:
272b1479ebbSBoris BREZILLON iounmap(reg_base);
273b1479ebbSBoris BREZILLON
274b1479ebbSBoris BREZILLON return ERR_PTR(ret);
275b1479ebbSBoris BREZILLON }
276