blob: db8e14112edaf5fa4007762a3c4d4421fd656b7e [file] [log] [blame]
SAN People73a59c12006-01-09 17:05:41 +00001/*
Andrew Victor9d041262007-02-05 11:42:07 +01002 * linux/arch/arm/mach-at91/irq.c
SAN People73a59c12006-01-09 17:05:41 +00003 *
4 * Copyright (C) 2004 SAN People
5 * Copyright (C) 2004 ATMEL
6 * Copyright (C) Rick Bronson
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
SAN People73a59c12006-01-09 17:05:41 +000023#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/mm.h>
26#include <linux/types.h>
Nicolas Ferree2615012011-11-22 22:26:09 +010027#include <linux/irq.h>
28#include <linux/of.h>
29#include <linux/of_address.h>
30#include <linux/of_irq.h>
31#include <linux/irqdomain.h>
32#include <linux/err.h>
Ludovic Desrochesf8a073e2012-06-20 16:13:30 +020033#include <linux/slab.h>
SAN People73a59c12006-01-09 17:05:41 +000034
Russell Kinga09e64f2008-08-05 16:14:15 +010035#include <mach/hardware.h>
SAN People73a59c12006-01-09 17:05:41 +000036#include <asm/irq.h>
SAN People73a59c12006-01-09 17:05:41 +000037#include <asm/setup.h>
38
39#include <asm/mach/arch.h>
40#include <asm/mach/irq.h>
41#include <asm/mach/map.h>
42
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +080043void __iomem *at91_aic_base;
Nicolas Ferree2615012011-11-22 22:26:09 +010044static struct irq_domain *at91_aic_domain;
45static struct device_node *at91_aic_np;
Ludovic Desrochesf8a073e2012-06-20 16:13:30 +020046static unsigned int *at91_aic_irq_priorities;
SAN People73a59c12006-01-09 17:05:41 +000047
Lennert Buytenhekda0f9402010-11-29 10:26:19 +010048static void at91_aic_mask_irq(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +000049{
50 /* Disable interrupt on AIC */
Nicolas Ferree2615012011-11-22 22:26:09 +010051 at91_aic_write(AT91_AIC_IDCR, 1 << d->hwirq);
SAN People73a59c12006-01-09 17:05:41 +000052}
53
Lennert Buytenhekda0f9402010-11-29 10:26:19 +010054static void at91_aic_unmask_irq(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +000055{
56 /* Enable interrupt on AIC */
Nicolas Ferree2615012011-11-22 22:26:09 +010057 at91_aic_write(AT91_AIC_IECR, 1 << d->hwirq);
SAN People73a59c12006-01-09 17:05:41 +000058}
59
Ludovic Desroches42a859d2012-05-25 14:11:51 +020060static void at91_aic_eoi(struct irq_data *d)
61{
62 /*
63 * Mark end-of-interrupt on AIC, the controller doesn't care about
64 * the value written. Moreover it's a write-only register.
65 */
66 at91_aic_write(AT91_AIC_EOICR, 0);
67}
68
Andrew Victor1f4fd0a2006-11-30 10:01:47 +010069unsigned int at91_extern_irq;
70
Nicolas Ferree2615012011-11-22 22:26:09 +010071#define is_extern_irq(hwirq) ((1 << (hwirq)) & at91_extern_irq)
Andrew Victor1f4fd0a2006-11-30 10:01:47 +010072
Lennert Buytenhekda0f9402010-11-29 10:26:19 +010073static int at91_aic_set_type(struct irq_data *d, unsigned type)
SAN People73a59c12006-01-09 17:05:41 +000074{
75 unsigned int smr, srctype;
76
SAN People73a59c12006-01-09 17:05:41 +000077 switch (type) {
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010078 case IRQ_TYPE_LEVEL_HIGH:
SAN People73a59c12006-01-09 17:05:41 +000079 srctype = AT91_AIC_SRCTYPE_HIGH;
80 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010081 case IRQ_TYPE_EDGE_RISING:
SAN People73a59c12006-01-09 17:05:41 +000082 srctype = AT91_AIC_SRCTYPE_RISING;
83 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010084 case IRQ_TYPE_LEVEL_LOW:
Nicolas Ferree2615012011-11-22 22:26:09 +010085 if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
Andrew Victor1f4fd0a2006-11-30 10:01:47 +010086 srctype = AT91_AIC_SRCTYPE_LOW;
87 else
Andrew Victor37f2e4bc2006-06-19 15:26:52 +010088 return -EINVAL;
SAN People73a59c12006-01-09 17:05:41 +000089 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010090 case IRQ_TYPE_EDGE_FALLING:
Nicolas Ferree2615012011-11-22 22:26:09 +010091 if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
Andrew Victor1f4fd0a2006-11-30 10:01:47 +010092 srctype = AT91_AIC_SRCTYPE_FALLING;
93 else
Andrew Victor37f2e4bc2006-06-19 15:26:52 +010094 return -EINVAL;
SAN People73a59c12006-01-09 17:05:41 +000095 break;
96 default:
97 return -EINVAL;
98 }
99
Nicolas Ferree2615012011-11-22 22:26:09 +0100100 smr = at91_aic_read(AT91_AIC_SMR(d->hwirq)) & ~AT91_AIC_SRCTYPE;
101 at91_aic_write(AT91_AIC_SMR(d->hwirq), smr | srctype);
SAN People73a59c12006-01-09 17:05:41 +0000102 return 0;
103}
104
Andrew Victor683c66b2006-06-19 15:26:53 +0100105#ifdef CONFIG_PM
106
107static u32 wakeups;
108static u32 backups;
109
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100110static int at91_aic_set_wake(struct irq_data *d, unsigned value)
Andrew Victor683c66b2006-06-19 15:26:53 +0100111{
Nicolas Ferree2615012011-11-22 22:26:09 +0100112 if (unlikely(d->hwirq >= NR_AIC_IRQS))
Andrew Victor683c66b2006-06-19 15:26:53 +0100113 return -EINVAL;
114
115 if (value)
Nicolas Ferree2615012011-11-22 22:26:09 +0100116 wakeups |= (1 << d->hwirq);
Andrew Victor683c66b2006-06-19 15:26:53 +0100117 else
Nicolas Ferree2615012011-11-22 22:26:09 +0100118 wakeups &= ~(1 << d->hwirq);
Andrew Victor683c66b2006-06-19 15:26:53 +0100119
120 return 0;
121}
122
123void at91_irq_suspend(void)
124{
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800125 backups = at91_aic_read(AT91_AIC_IMR);
126 at91_aic_write(AT91_AIC_IDCR, backups);
127 at91_aic_write(AT91_AIC_IECR, wakeups);
Andrew Victor683c66b2006-06-19 15:26:53 +0100128}
129
130void at91_irq_resume(void)
131{
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800132 at91_aic_write(AT91_AIC_IDCR, wakeups);
133 at91_aic_write(AT91_AIC_IECR, backups);
Andrew Victor683c66b2006-06-19 15:26:53 +0100134}
135
136#else
Andrew Victorba854e12006-07-05 17:22:52 +0100137#define at91_aic_set_wake NULL
Andrew Victor683c66b2006-06-19 15:26:53 +0100138#endif
139
David Brownell38c677c2006-08-01 22:26:25 +0100140static struct irq_chip at91_aic_chip = {
141 .name = "AIC",
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100142 .irq_mask = at91_aic_mask_irq,
143 .irq_unmask = at91_aic_unmask_irq,
144 .irq_set_type = at91_aic_set_type,
145 .irq_set_wake = at91_aic_set_wake,
Ludovic Desroches42a859d2012-05-25 14:11:51 +0200146 .irq_eoi = at91_aic_eoi,
SAN People73a59c12006-01-09 17:05:41 +0000147};
148
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100149static void __init at91_aic_hw_init(unsigned int spu_vector)
150{
151 int i;
152
153 /*
154 * Perform 8 End Of Interrupt Command to make sure AIC
155 * will not Lock out nIRQ
156 */
157 for (i = 0; i < 8; i++)
158 at91_aic_write(AT91_AIC_EOICR, 0);
159
160 /*
161 * Spurious Interrupt ID in Spurious Vector Register.
162 * When there is no current interrupt, the IRQ Vector Register
163 * reads the value stored in AIC_SPU
164 */
165 at91_aic_write(AT91_AIC_SPU, spu_vector);
166
167 /* No debugging in AIC: Debug (Protect) Control Register */
168 at91_aic_write(AT91_AIC_DCR, 0);
169
170 /* Disable and clear all interrupts initially */
171 at91_aic_write(AT91_AIC_IDCR, 0xFFFFFFFF);
172 at91_aic_write(AT91_AIC_ICCR, 0xFFFFFFFF);
173}
174
Nicolas Ferree2615012011-11-22 22:26:09 +0100175#if defined(CONFIG_OF)
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100176static int at91_aic_irq_map(struct irq_domain *h, unsigned int virq,
177 irq_hw_number_t hw)
178{
179 /* Put virq number in Source Vector Register */
180 at91_aic_write(AT91_AIC_SVR(hw), virq);
181
Ludovic Desrochesf8a073e2012-06-20 16:13:30 +0200182 /* Active Low interrupt, with priority */
183 at91_aic_write(AT91_AIC_SMR(hw),
184 AT91_AIC_SRCTYPE_LOW | at91_aic_irq_priorities[hw]);
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100185
Ludovic Desroches42a859d2012-05-25 14:11:51 +0200186 irq_set_chip_and_handler(virq, &at91_aic_chip, handle_fasteoi_irq);
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100187 set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
188
189 return 0;
190}
191
Ludovic Desrochesf8a073e2012-06-20 16:13:30 +0200192static int at91_aic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
193 const u32 *intspec, unsigned int intsize,
194 irq_hw_number_t *out_hwirq, unsigned int *out_type)
195{
196 if (WARN_ON(intsize < 3))
197 return -EINVAL;
198 if (WARN_ON(intspec[0] >= NR_AIC_IRQS))
199 return -EINVAL;
200 if (WARN_ON((intspec[2] < AT91_AIC_IRQ_MIN_PRIORITY)
201 || (intspec[2] > AT91_AIC_IRQ_MAX_PRIORITY)))
202 return -EINVAL;
203
204 *out_hwirq = intspec[0];
205 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
206 at91_aic_irq_priorities[*out_hwirq] = intspec[2];
207
208 return 0;
209}
210
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100211static struct irq_domain_ops at91_aic_irq_ops = {
212 .map = at91_aic_irq_map,
Ludovic Desrochesf8a073e2012-06-20 16:13:30 +0200213 .xlate = at91_aic_irq_domain_xlate,
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100214};
215
216int __init at91_aic_of_init(struct device_node *node,
Nicolas Ferree2615012011-11-22 22:26:09 +0100217 struct device_node *parent)
218{
Jean-Christophe PLAGNIOL-VILLARDc6573942012-04-09 19:36:36 +0800219 struct property *prop;
220 const __be32 *p;
221 u32 val;
222
Ludovic Desrochesf8a073e2012-06-20 16:13:30 +0200223 at91_aic_irq_priorities = kzalloc(NR_AIC_IRQS
224 * sizeof(*at91_aic_irq_priorities),
225 GFP_KERNEL);
226 if (!at91_aic_irq_priorities)
227 return -ENOMEM;
228
Nicolas Ferree2615012011-11-22 22:26:09 +0100229 at91_aic_base = of_iomap(node, 0);
230 at91_aic_np = node;
231
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100232 at91_aic_domain = irq_domain_add_linear(at91_aic_np, NR_AIC_IRQS,
233 &at91_aic_irq_ops, NULL);
234 if (!at91_aic_domain)
235 panic("Unable to add AIC irq domain (DT)\n");
236
Jean-Christophe PLAGNIOL-VILLARDc6573942012-04-09 19:36:36 +0800237 at91_extern_irq = 0;
238 of_property_for_each_u32(node, "atmel,external-irqs", prop, p, val) {
239 if (val > 31)
240 pr_warn("AIC: external irq %d > 31 skip it\n", val);
241 else
242 at91_extern_irq |= (1 << val);
243 }
244
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100245 irq_set_default_host(at91_aic_domain);
246
247 at91_aic_hw_init(NR_AIC_IRQS);
248
Nicolas Ferree2615012011-11-22 22:26:09 +0100249 return 0;
250}
Nicolas Ferree2615012011-11-22 22:26:09 +0100251#endif
252
SAN People73a59c12006-01-09 17:05:41 +0000253/*
254 * Initialize the AIC interrupt controller.
255 */
Andrew Victorba854e12006-07-05 17:22:52 +0100256void __init at91_aic_init(unsigned int priority[NR_AIC_IRQS])
SAN People73a59c12006-01-09 17:05:41 +0000257{
258 unsigned int i;
Nicolas Ferree2615012011-11-22 22:26:09 +0100259 int irq_base;
SAN People73a59c12006-01-09 17:05:41 +0000260
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100261 at91_aic_base = ioremap(AT91_AIC, 512);
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800262 if (!at91_aic_base)
Nicolas Ferree2615012011-11-22 22:26:09 +0100263 panic("Unable to ioremap AIC registers\n");
264
265 /* Add irq domain for AIC */
266 irq_base = irq_alloc_descs(-1, 0, NR_AIC_IRQS, 0);
267 if (irq_base < 0) {
268 WARN(1, "Cannot allocate irq_descs, assuming pre-allocated\n");
269 irq_base = 0;
270 }
271 at91_aic_domain = irq_domain_add_legacy(at91_aic_np, NR_AIC_IRQS,
272 irq_base, 0,
273 &irq_domain_simple_ops, NULL);
274
275 if (!at91_aic_domain)
276 panic("Unable to add AIC irq domain\n");
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800277
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100278 irq_set_default_host(at91_aic_domain);
279
SAN People73a59c12006-01-09 17:05:41 +0000280 /*
281 * The IVR is used by macro get_irqnr_and_base to read and verify.
282 * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
283 */
284 for (i = 0; i < NR_AIC_IRQS; i++) {
Nicolas Ferree2615012011-11-22 22:26:09 +0100285 /* Put hardware irq number in Source Vector Register: */
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800286 at91_aic_write(AT91_AIC_SVR(i), i);
Andrew Victorba854e12006-07-05 17:22:52 +0100287 /* Active Low interrupt, with the specified priority */
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800288 at91_aic_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
SAN People73a59c12006-01-09 17:05:41 +0000289
Ludovic Desroches42a859d2012-05-25 14:11:51 +0200290 irq_set_chip_and_handler(i, &at91_aic_chip, handle_fasteoi_irq);
SAN People73a59c12006-01-09 17:05:41 +0000291 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
SAN People73a59c12006-01-09 17:05:41 +0000292 }
293
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100294 at91_aic_hw_init(NR_AIC_IRQS);
SAN People73a59c12006-01-09 17:05:41 +0000295}