blob: cfcfcbe362699d1ed04be46e2d9680bc89917e58 [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>
SAN People73a59c12006-01-09 17:05:41 +000033
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/hardware.h>
SAN People73a59c12006-01-09 17:05:41 +000035#include <asm/irq.h>
SAN People73a59c12006-01-09 17:05:41 +000036#include <asm/setup.h>
37
38#include <asm/mach/arch.h>
39#include <asm/mach/irq.h>
40#include <asm/mach/map.h>
41
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +080042void __iomem *at91_aic_base;
Nicolas Ferree2615012011-11-22 22:26:09 +010043static struct irq_domain *at91_aic_domain;
44static struct device_node *at91_aic_np;
SAN People73a59c12006-01-09 17:05:41 +000045
Lennert Buytenhekda0f9402010-11-29 10:26:19 +010046static void at91_aic_mask_irq(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +000047{
48 /* Disable interrupt on AIC */
Nicolas Ferree2615012011-11-22 22:26:09 +010049 at91_aic_write(AT91_AIC_IDCR, 1 << d->hwirq);
SAN People73a59c12006-01-09 17:05:41 +000050}
51
Lennert Buytenhekda0f9402010-11-29 10:26:19 +010052static void at91_aic_unmask_irq(struct irq_data *d)
SAN People73a59c12006-01-09 17:05:41 +000053{
54 /* Enable interrupt on AIC */
Nicolas Ferree2615012011-11-22 22:26:09 +010055 at91_aic_write(AT91_AIC_IECR, 1 << d->hwirq);
SAN People73a59c12006-01-09 17:05:41 +000056}
57
Andrew Victor1f4fd0a2006-11-30 10:01:47 +010058unsigned int at91_extern_irq;
59
Nicolas Ferree2615012011-11-22 22:26:09 +010060#define is_extern_irq(hwirq) ((1 << (hwirq)) & at91_extern_irq)
Andrew Victor1f4fd0a2006-11-30 10:01:47 +010061
Lennert Buytenhekda0f9402010-11-29 10:26:19 +010062static int at91_aic_set_type(struct irq_data *d, unsigned type)
SAN People73a59c12006-01-09 17:05:41 +000063{
64 unsigned int smr, srctype;
65
SAN People73a59c12006-01-09 17:05:41 +000066 switch (type) {
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010067 case IRQ_TYPE_LEVEL_HIGH:
SAN People73a59c12006-01-09 17:05:41 +000068 srctype = AT91_AIC_SRCTYPE_HIGH;
69 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010070 case IRQ_TYPE_EDGE_RISING:
SAN People73a59c12006-01-09 17:05:41 +000071 srctype = AT91_AIC_SRCTYPE_RISING;
72 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010073 case IRQ_TYPE_LEVEL_LOW:
Nicolas Ferree2615012011-11-22 22:26:09 +010074 if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
Andrew Victor1f4fd0a2006-11-30 10:01:47 +010075 srctype = AT91_AIC_SRCTYPE_LOW;
76 else
Andrew Victor37f2e4bc2006-06-19 15:26:52 +010077 return -EINVAL;
SAN People73a59c12006-01-09 17:05:41 +000078 break;
Dmitry Baryshkov6cab4862008-07-27 04:23:31 +010079 case IRQ_TYPE_EDGE_FALLING:
Nicolas Ferree2615012011-11-22 22:26:09 +010080 if ((d->hwirq == AT91_ID_FIQ) || is_extern_irq(d->hwirq)) /* only supported on external interrupts */
Andrew Victor1f4fd0a2006-11-30 10:01:47 +010081 srctype = AT91_AIC_SRCTYPE_FALLING;
82 else
Andrew Victor37f2e4bc2006-06-19 15:26:52 +010083 return -EINVAL;
SAN People73a59c12006-01-09 17:05:41 +000084 break;
85 default:
86 return -EINVAL;
87 }
88
Nicolas Ferree2615012011-11-22 22:26:09 +010089 smr = at91_aic_read(AT91_AIC_SMR(d->hwirq)) & ~AT91_AIC_SRCTYPE;
90 at91_aic_write(AT91_AIC_SMR(d->hwirq), smr | srctype);
SAN People73a59c12006-01-09 17:05:41 +000091 return 0;
92}
93
Andrew Victor683c66b2006-06-19 15:26:53 +010094#ifdef CONFIG_PM
95
96static u32 wakeups;
97static u32 backups;
98
Lennert Buytenhekda0f9402010-11-29 10:26:19 +010099static int at91_aic_set_wake(struct irq_data *d, unsigned value)
Andrew Victor683c66b2006-06-19 15:26:53 +0100100{
Nicolas Ferree2615012011-11-22 22:26:09 +0100101 if (unlikely(d->hwirq >= NR_AIC_IRQS))
Andrew Victor683c66b2006-06-19 15:26:53 +0100102 return -EINVAL;
103
104 if (value)
Nicolas Ferree2615012011-11-22 22:26:09 +0100105 wakeups |= (1 << d->hwirq);
Andrew Victor683c66b2006-06-19 15:26:53 +0100106 else
Nicolas Ferree2615012011-11-22 22:26:09 +0100107 wakeups &= ~(1 << d->hwirq);
Andrew Victor683c66b2006-06-19 15:26:53 +0100108
109 return 0;
110}
111
112void at91_irq_suspend(void)
113{
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800114 backups = at91_aic_read(AT91_AIC_IMR);
115 at91_aic_write(AT91_AIC_IDCR, backups);
116 at91_aic_write(AT91_AIC_IECR, wakeups);
Andrew Victor683c66b2006-06-19 15:26:53 +0100117}
118
119void at91_irq_resume(void)
120{
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800121 at91_aic_write(AT91_AIC_IDCR, wakeups);
122 at91_aic_write(AT91_AIC_IECR, backups);
Andrew Victor683c66b2006-06-19 15:26:53 +0100123}
124
125#else
Andrew Victorba854e12006-07-05 17:22:52 +0100126#define at91_aic_set_wake NULL
Andrew Victor683c66b2006-06-19 15:26:53 +0100127#endif
128
David Brownell38c677c2006-08-01 22:26:25 +0100129static struct irq_chip at91_aic_chip = {
130 .name = "AIC",
Lennert Buytenhekda0f9402010-11-29 10:26:19 +0100131 .irq_ack = at91_aic_mask_irq,
132 .irq_mask = at91_aic_mask_irq,
133 .irq_unmask = at91_aic_unmask_irq,
134 .irq_set_type = at91_aic_set_type,
135 .irq_set_wake = at91_aic_set_wake,
SAN People73a59c12006-01-09 17:05:41 +0000136};
137
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100138static void __init at91_aic_hw_init(unsigned int spu_vector)
139{
140 int i;
141
142 /*
143 * Perform 8 End Of Interrupt Command to make sure AIC
144 * will not Lock out nIRQ
145 */
146 for (i = 0; i < 8; i++)
147 at91_aic_write(AT91_AIC_EOICR, 0);
148
149 /*
150 * Spurious Interrupt ID in Spurious Vector Register.
151 * When there is no current interrupt, the IRQ Vector Register
152 * reads the value stored in AIC_SPU
153 */
154 at91_aic_write(AT91_AIC_SPU, spu_vector);
155
156 /* No debugging in AIC: Debug (Protect) Control Register */
157 at91_aic_write(AT91_AIC_DCR, 0);
158
159 /* Disable and clear all interrupts initially */
160 at91_aic_write(AT91_AIC_IDCR, 0xFFFFFFFF);
161 at91_aic_write(AT91_AIC_ICCR, 0xFFFFFFFF);
162}
163
Nicolas Ferree2615012011-11-22 22:26:09 +0100164#if defined(CONFIG_OF)
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100165static int at91_aic_irq_map(struct irq_domain *h, unsigned int virq,
166 irq_hw_number_t hw)
167{
168 /* Put virq number in Source Vector Register */
169 at91_aic_write(AT91_AIC_SVR(hw), virq);
170
171 /* Active Low interrupt, without priority */
172 at91_aic_write(AT91_AIC_SMR(hw), AT91_AIC_SRCTYPE_LOW);
173
174 irq_set_chip_and_handler(virq, &at91_aic_chip, handle_level_irq);
175 set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
176
177 return 0;
178}
179
180static struct irq_domain_ops at91_aic_irq_ops = {
181 .map = at91_aic_irq_map,
182 .xlate = irq_domain_xlate_twocell,
183};
184
185int __init at91_aic_of_init(struct device_node *node,
Nicolas Ferree2615012011-11-22 22:26:09 +0100186 struct device_node *parent)
187{
188 at91_aic_base = of_iomap(node, 0);
189 at91_aic_np = node;
190
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100191 at91_aic_domain = irq_domain_add_linear(at91_aic_np, NR_AIC_IRQS,
192 &at91_aic_irq_ops, NULL);
193 if (!at91_aic_domain)
194 panic("Unable to add AIC irq domain (DT)\n");
195
196 irq_set_default_host(at91_aic_domain);
197
198 at91_aic_hw_init(NR_AIC_IRQS);
199
Nicolas Ferree2615012011-11-22 22:26:09 +0100200 return 0;
201}
Nicolas Ferree2615012011-11-22 22:26:09 +0100202#endif
203
SAN People73a59c12006-01-09 17:05:41 +0000204/*
205 * Initialize the AIC interrupt controller.
206 */
Andrew Victorba854e12006-07-05 17:22:52 +0100207void __init at91_aic_init(unsigned int priority[NR_AIC_IRQS])
SAN People73a59c12006-01-09 17:05:41 +0000208{
209 unsigned int i;
Nicolas Ferree2615012011-11-22 22:26:09 +0100210 int irq_base;
SAN People73a59c12006-01-09 17:05:41 +0000211
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100212 at91_aic_base = ioremap(AT91_AIC, 512);
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800213 if (!at91_aic_base)
Nicolas Ferree2615012011-11-22 22:26:09 +0100214 panic("Unable to ioremap AIC registers\n");
215
216 /* Add irq domain for AIC */
217 irq_base = irq_alloc_descs(-1, 0, NR_AIC_IRQS, 0);
218 if (irq_base < 0) {
219 WARN(1, "Cannot allocate irq_descs, assuming pre-allocated\n");
220 irq_base = 0;
221 }
222 at91_aic_domain = irq_domain_add_legacy(at91_aic_np, NR_AIC_IRQS,
223 irq_base, 0,
224 &irq_domain_simple_ops, NULL);
225
226 if (!at91_aic_domain)
227 panic("Unable to add AIC irq domain\n");
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800228
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100229 irq_set_default_host(at91_aic_domain);
230
SAN People73a59c12006-01-09 17:05:41 +0000231 /*
232 * The IVR is used by macro get_irqnr_and_base to read and verify.
233 * The irq number is NR_AIC_IRQS when a spurious interrupt has occurred.
234 */
235 for (i = 0; i < NR_AIC_IRQS; i++) {
Nicolas Ferree2615012011-11-22 22:26:09 +0100236 /* Put hardware irq number in Source Vector Register: */
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800237 at91_aic_write(AT91_AIC_SVR(i), i);
Andrew Victorba854e12006-07-05 17:22:52 +0100238 /* Active Low interrupt, with the specified priority */
Jean-Christophe PLAGNIOL-VILLARDbe6d4322011-11-03 01:12:50 +0800239 at91_aic_write(AT91_AIC_SMR(i), AT91_AIC_SRCTYPE_LOW | priority[i]);
SAN People73a59c12006-01-09 17:05:41 +0000240
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100241 irq_set_chip_and_handler(i, &at91_aic_chip, handle_level_irq);
SAN People73a59c12006-01-09 17:05:41 +0000242 set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
SAN People73a59c12006-01-09 17:05:41 +0000243 }
244
Nicolas Ferre8014d6f2012-02-14 18:08:14 +0100245 at91_aic_hw_init(NR_AIC_IRQS);
SAN People73a59c12006-01-09 17:05:41 +0000246}