blob: d227be1c3c4dfc7ce55eb81a3c271db33fd2fd96 [file] [log] [blame]
John Crispin171bb2f2011-03-30 09:27:47 +02001/*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
5 *
6 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
7 * Copyright (C) 2010 Thomas Langer <thomas.langer@lantiq.com>
8 */
9
10#include <linux/interrupt.h>
11#include <linux/ioport.h>
12
13#include <asm/bootinfo.h>
14#include <asm/irq_cpu.h>
15
16#include <lantiq_soc.h>
17#include <irq.h>
18
19/* register definitions */
20#define LTQ_ICU_IM0_ISR 0x0000
21#define LTQ_ICU_IM0_IER 0x0008
22#define LTQ_ICU_IM0_IOSR 0x0010
23#define LTQ_ICU_IM0_IRSR 0x0018
24#define LTQ_ICU_IM0_IMR 0x0020
25#define LTQ_ICU_IM1_ISR 0x0028
26#define LTQ_ICU_OFFSET (LTQ_ICU_IM1_ISR - LTQ_ICU_IM0_ISR)
27
28#define LTQ_EIU_EXIN_C 0x0000
29#define LTQ_EIU_EXIN_INIC 0x0004
30#define LTQ_EIU_EXIN_INEN 0x000C
31
32/* irq numbers used by the external interrupt unit (EIU) */
33#define LTQ_EIU_IR0 (INT_NUM_IM4_IRL0 + 30)
34#define LTQ_EIU_IR1 (INT_NUM_IM3_IRL0 + 31)
35#define LTQ_EIU_IR2 (INT_NUM_IM1_IRL0 + 26)
36#define LTQ_EIU_IR3 INT_NUM_IM1_IRL0
37#define LTQ_EIU_IR4 (INT_NUM_IM1_IRL0 + 1)
38#define LTQ_EIU_IR5 (INT_NUM_IM1_IRL0 + 2)
39#define LTQ_EIU_IR6 (INT_NUM_IM2_IRL0 + 30)
40
41#define MAX_EIU 6
42
John Crispin59c11572012-05-02 12:27:37 +020043/* the performance counter */
44#define LTQ_PERF_IRQ (INT_NUM_IM4_IRL0 + 31)
45
John Crispin171bb2f2011-03-30 09:27:47 +020046/* irqs generated by device attached to the EBU need to be acked in
47 * a special manner
48 */
49#define LTQ_ICU_EBU_IRQ 22
50
51#define ltq_icu_w32(x, y) ltq_w32((x), ltq_icu_membase + (y))
52#define ltq_icu_r32(x) ltq_r32(ltq_icu_membase + (x))
53
54#define ltq_eiu_w32(x, y) ltq_w32((x), ltq_eiu_membase + (y))
55#define ltq_eiu_r32(x) ltq_r32(ltq_eiu_membase + (x))
56
John Crispina8d096e2012-04-30 11:33:05 +020057/* our 2 ipi interrupts for VSMP */
58#define MIPS_CPU_IPI_RESCHED_IRQ 0
59#define MIPS_CPU_IPI_CALL_IRQ 1
60
61#if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
62int gic_present;
63#endif
64
John Crispin171bb2f2011-03-30 09:27:47 +020065static unsigned short ltq_eiu_irq[MAX_EIU] = {
66 LTQ_EIU_IR0,
67 LTQ_EIU_IR1,
68 LTQ_EIU_IR2,
69 LTQ_EIU_IR3,
70 LTQ_EIU_IR4,
71 LTQ_EIU_IR5,
72};
73
74static struct resource ltq_icu_resource = {
75 .name = "icu",
76 .start = LTQ_ICU_BASE_ADDR,
77 .end = LTQ_ICU_BASE_ADDR + LTQ_ICU_SIZE - 1,
78 .flags = IORESOURCE_MEM,
79};
80
81static struct resource ltq_eiu_resource = {
82 .name = "eiu",
83 .start = LTQ_EIU_BASE_ADDR,
84 .end = LTQ_EIU_BASE_ADDR + LTQ_ICU_SIZE - 1,
85 .flags = IORESOURCE_MEM,
86};
87
88static void __iomem *ltq_icu_membase;
89static void __iomem *ltq_eiu_membase;
90
91void ltq_disable_irq(struct irq_data *d)
92{
93 u32 ier = LTQ_ICU_IM0_IER;
94 int irq_nr = d->irq - INT_NUM_IRQ0;
95
96 ier += LTQ_ICU_OFFSET * (irq_nr / INT_NUM_IM_OFFSET);
97 irq_nr %= INT_NUM_IM_OFFSET;
98 ltq_icu_w32(ltq_icu_r32(ier) & ~(1 << irq_nr), ier);
99}
100
101void ltq_mask_and_ack_irq(struct irq_data *d)
102{
103 u32 ier = LTQ_ICU_IM0_IER;
104 u32 isr = LTQ_ICU_IM0_ISR;
105 int irq_nr = d->irq - INT_NUM_IRQ0;
106
107 ier += LTQ_ICU_OFFSET * (irq_nr / INT_NUM_IM_OFFSET);
108 isr += LTQ_ICU_OFFSET * (irq_nr / INT_NUM_IM_OFFSET);
109 irq_nr %= INT_NUM_IM_OFFSET;
110 ltq_icu_w32(ltq_icu_r32(ier) & ~(1 << irq_nr), ier);
111 ltq_icu_w32((1 << irq_nr), isr);
112}
113
114static void ltq_ack_irq(struct irq_data *d)
115{
116 u32 isr = LTQ_ICU_IM0_ISR;
117 int irq_nr = d->irq - INT_NUM_IRQ0;
118
119 isr += LTQ_ICU_OFFSET * (irq_nr / INT_NUM_IM_OFFSET);
120 irq_nr %= INT_NUM_IM_OFFSET;
121 ltq_icu_w32((1 << irq_nr), isr);
122}
123
124void ltq_enable_irq(struct irq_data *d)
125{
126 u32 ier = LTQ_ICU_IM0_IER;
127 int irq_nr = d->irq - INT_NUM_IRQ0;
128
129 ier += LTQ_ICU_OFFSET * (irq_nr / INT_NUM_IM_OFFSET);
130 irq_nr %= INT_NUM_IM_OFFSET;
131 ltq_icu_w32(ltq_icu_r32(ier) | (1 << irq_nr), ier);
132}
133
134static unsigned int ltq_startup_eiu_irq(struct irq_data *d)
135{
136 int i;
John Crispin171bb2f2011-03-30 09:27:47 +0200137
138 ltq_enable_irq(d);
139 for (i = 0; i < MAX_EIU; i++) {
John Crispin77fbdb32011-07-18 18:08:40 +0200140 if (d->irq == ltq_eiu_irq[i]) {
John Crispin171bb2f2011-03-30 09:27:47 +0200141 /* low level - we should really handle set_type */
142 ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_C) |
143 (0x6 << (i * 4)), LTQ_EIU_EXIN_C);
144 /* clear all pending */
145 ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INIC) & ~(1 << i),
146 LTQ_EIU_EXIN_INIC);
147 /* enable */
148 ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) | (1 << i),
149 LTQ_EIU_EXIN_INEN);
150 break;
151 }
152 }
153
154 return 0;
155}
156
157static void ltq_shutdown_eiu_irq(struct irq_data *d)
158{
159 int i;
John Crispin171bb2f2011-03-30 09:27:47 +0200160
161 ltq_disable_irq(d);
162 for (i = 0; i < MAX_EIU; i++) {
John Crispin77fbdb32011-07-18 18:08:40 +0200163 if (d->irq == ltq_eiu_irq[i]) {
John Crispin171bb2f2011-03-30 09:27:47 +0200164 /* disable */
165 ltq_eiu_w32(ltq_eiu_r32(LTQ_EIU_EXIN_INEN) & ~(1 << i),
166 LTQ_EIU_EXIN_INEN);
167 break;
168 }
169 }
170}
171
172static struct irq_chip ltq_irq_type = {
173 "icu",
174 .irq_enable = ltq_enable_irq,
175 .irq_disable = ltq_disable_irq,
176 .irq_unmask = ltq_enable_irq,
177 .irq_ack = ltq_ack_irq,
178 .irq_mask = ltq_disable_irq,
179 .irq_mask_ack = ltq_mask_and_ack_irq,
180};
181
182static struct irq_chip ltq_eiu_type = {
183 "eiu",
184 .irq_startup = ltq_startup_eiu_irq,
185 .irq_shutdown = ltq_shutdown_eiu_irq,
186 .irq_enable = ltq_enable_irq,
187 .irq_disable = ltq_disable_irq,
188 .irq_unmask = ltq_enable_irq,
189 .irq_ack = ltq_ack_irq,
190 .irq_mask = ltq_disable_irq,
191 .irq_mask_ack = ltq_mask_and_ack_irq,
192};
193
194static void ltq_hw_irqdispatch(int module)
195{
196 u32 irq;
197
198 irq = ltq_icu_r32(LTQ_ICU_IM0_IOSR + (module * LTQ_ICU_OFFSET));
199 if (irq == 0)
200 return;
201
202 /* silicon bug causes only the msb set to 1 to be valid. all
203 * other bits might be bogus
204 */
205 irq = __fls(irq);
206 do_IRQ((int)irq + INT_NUM_IM0_IRL0 + (INT_NUM_IM_OFFSET * module));
207
208 /* if this is a EBU irq, we need to ack it or get a deadlock */
209 if ((irq == LTQ_ICU_EBU_IRQ) && (module == 0))
210 ltq_ebu_w32(ltq_ebu_r32(LTQ_EBU_PCC_ISTAT) | 0x10,
211 LTQ_EBU_PCC_ISTAT);
212}
213
214#define DEFINE_HWx_IRQDISPATCH(x) \
215 static void ltq_hw ## x ## _irqdispatch(void) \
216 { \
217 ltq_hw_irqdispatch(x); \
218 }
219DEFINE_HWx_IRQDISPATCH(0)
220DEFINE_HWx_IRQDISPATCH(1)
221DEFINE_HWx_IRQDISPATCH(2)
222DEFINE_HWx_IRQDISPATCH(3)
223DEFINE_HWx_IRQDISPATCH(4)
224
225static void ltq_hw5_irqdispatch(void)
226{
227 do_IRQ(MIPS_CPU_TIMER_IRQ);
228}
229
John Crispina8d096e2012-04-30 11:33:05 +0200230#ifdef CONFIG_MIPS_MT_SMP
231void __init arch_init_ipiirq(int irq, struct irqaction *action)
232{
233 setup_irq(irq, action);
234 irq_set_handler(irq, handle_percpu_irq);
235}
236
237static void ltq_sw0_irqdispatch(void)
238{
239 do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_RESCHED_IRQ);
240}
241
242static void ltq_sw1_irqdispatch(void)
243{
244 do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_CALL_IRQ);
245}
246static irqreturn_t ipi_resched_interrupt(int irq, void *dev_id)
247{
248 scheduler_ipi();
249 return IRQ_HANDLED;
250}
251
252static irqreturn_t ipi_call_interrupt(int irq, void *dev_id)
253{
254 smp_call_function_interrupt();
255 return IRQ_HANDLED;
256}
257
258static struct irqaction irq_resched = {
259 .handler = ipi_resched_interrupt,
260 .flags = IRQF_PERCPU,
261 .name = "IPI_resched"
262};
263
264static struct irqaction irq_call = {
265 .handler = ipi_call_interrupt,
266 .flags = IRQF_PERCPU,
267 .name = "IPI_call"
268};
269#endif
270
John Crispin171bb2f2011-03-30 09:27:47 +0200271asmlinkage void plat_irq_dispatch(void)
272{
273 unsigned int pending = read_c0_status() & read_c0_cause() & ST0_IM;
274 unsigned int i;
275
276 if (pending & CAUSEF_IP7) {
277 do_IRQ(MIPS_CPU_TIMER_IRQ);
278 goto out;
279 } else {
280 for (i = 0; i < 5; i++) {
281 if (pending & (CAUSEF_IP2 << i)) {
282 ltq_hw_irqdispatch(i);
283 goto out;
284 }
285 }
286 }
287 pr_alert("Spurious IRQ: CAUSE=0x%08x\n", read_c0_status());
288
289out:
290 return;
291}
292
293static struct irqaction cascade = {
294 .handler = no_action,
John Crispin171bb2f2011-03-30 09:27:47 +0200295 .name = "cascade",
296};
297
298void __init arch_init_irq(void)
299{
300 int i;
301
302 if (insert_resource(&iomem_resource, &ltq_icu_resource) < 0)
Ralf Baechleab75dc02011-11-17 15:07:31 +0000303 panic("Failed to insert icu memory");
John Crispin171bb2f2011-03-30 09:27:47 +0200304
305 if (request_mem_region(ltq_icu_resource.start,
306 resource_size(&ltq_icu_resource), "icu") < 0)
Ralf Baechleab75dc02011-11-17 15:07:31 +0000307 panic("Failed to request icu memory");
John Crispin171bb2f2011-03-30 09:27:47 +0200308
309 ltq_icu_membase = ioremap_nocache(ltq_icu_resource.start,
310 resource_size(&ltq_icu_resource));
311 if (!ltq_icu_membase)
Ralf Baechleab75dc02011-11-17 15:07:31 +0000312 panic("Failed to remap icu memory");
John Crispin171bb2f2011-03-30 09:27:47 +0200313
314 if (insert_resource(&iomem_resource, &ltq_eiu_resource) < 0)
Ralf Baechleab75dc02011-11-17 15:07:31 +0000315 panic("Failed to insert eiu memory");
John Crispin171bb2f2011-03-30 09:27:47 +0200316
317 if (request_mem_region(ltq_eiu_resource.start,
318 resource_size(&ltq_eiu_resource), "eiu") < 0)
Ralf Baechleab75dc02011-11-17 15:07:31 +0000319 panic("Failed to request eiu memory");
John Crispin171bb2f2011-03-30 09:27:47 +0200320
321 ltq_eiu_membase = ioremap_nocache(ltq_eiu_resource.start,
322 resource_size(&ltq_eiu_resource));
323 if (!ltq_eiu_membase)
Ralf Baechleab75dc02011-11-17 15:07:31 +0000324 panic("Failed to remap eiu memory");
John Crispin171bb2f2011-03-30 09:27:47 +0200325
John Crispin16f70b52012-05-02 12:27:36 +0200326 /* turn off all irqs by default */
327 for (i = 0; i < 5; i++) {
328 /* make sure all irqs are turned off by default */
John Crispin171bb2f2011-03-30 09:27:47 +0200329 ltq_icu_w32(0, LTQ_ICU_IM0_IER + (i * LTQ_ICU_OFFSET));
John Crispin16f70b52012-05-02 12:27:36 +0200330 /* clear all possibly pending interrupts */
331 ltq_icu_w32(~0, LTQ_ICU_IM0_ISR + (i * LTQ_ICU_OFFSET));
332 }
John Crispin171bb2f2011-03-30 09:27:47 +0200333
334 mips_cpu_irq_init();
335
336 for (i = 2; i <= 6; i++)
337 setup_irq(i, &cascade);
338
339 if (cpu_has_vint) {
340 pr_info("Setting up vectored interrupts\n");
341 set_vi_handler(2, ltq_hw0_irqdispatch);
342 set_vi_handler(3, ltq_hw1_irqdispatch);
343 set_vi_handler(4, ltq_hw2_irqdispatch);
344 set_vi_handler(5, ltq_hw3_irqdispatch);
345 set_vi_handler(6, ltq_hw4_irqdispatch);
346 set_vi_handler(7, ltq_hw5_irqdispatch);
347 }
348
349 for (i = INT_NUM_IRQ0;
350 i <= (INT_NUM_IRQ0 + (5 * INT_NUM_IM_OFFSET)); i++)
351 if ((i == LTQ_EIU_IR0) || (i == LTQ_EIU_IR1) ||
352 (i == LTQ_EIU_IR2))
353 irq_set_chip_and_handler(i, &ltq_eiu_type,
354 handle_level_irq);
355 /* EIU3-5 only exist on ar9 and vr9 */
356 else if (((i == LTQ_EIU_IR3) || (i == LTQ_EIU_IR4) ||
357 (i == LTQ_EIU_IR5)) && (ltq_is_ar9() || ltq_is_vr9()))
358 irq_set_chip_and_handler(i, &ltq_eiu_type,
359 handle_level_irq);
360 else
361 irq_set_chip_and_handler(i, &ltq_irq_type,
362 handle_level_irq);
363
John Crispina8d096e2012-04-30 11:33:05 +0200364#if defined(CONFIG_MIPS_MT_SMP)
365 if (cpu_has_vint) {
366 pr_info("Setting up IPI vectored interrupts\n");
367 set_vi_handler(MIPS_CPU_IPI_RESCHED_IRQ, ltq_sw0_irqdispatch);
368 set_vi_handler(MIPS_CPU_IPI_CALL_IRQ, ltq_sw1_irqdispatch);
369 }
370 arch_init_ipiirq(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_RESCHED_IRQ,
371 &irq_resched);
372 arch_init_ipiirq(MIPS_CPU_IRQ_BASE + MIPS_CPU_IPI_CALL_IRQ, &irq_call);
373#endif
374
John Crispin171bb2f2011-03-30 09:27:47 +0200375#if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
376 set_c0_status(IE_IRQ0 | IE_IRQ1 | IE_IRQ2 |
377 IE_IRQ3 | IE_IRQ4 | IE_IRQ5);
378#else
379 set_c0_status(IE_SW0 | IE_SW1 | IE_IRQ0 | IE_IRQ1 |
380 IE_IRQ2 | IE_IRQ3 | IE_IRQ4 | IE_IRQ5);
381#endif
John Crispin59c11572012-05-02 12:27:37 +0200382
383 /* tell oprofile which irq to use */
384 cp0_perfcount_irq = LTQ_PERF_IRQ;
John Crispin171bb2f2011-03-30 09:27:47 +0200385}
386
387unsigned int __cpuinit get_c0_compare_int(void)
388{
389 return CP0_LEGACY_COMPARE_IRQ;
390}