MIPS: Markeins: Extract ll_emma2rh_* functions
[h-e-n] / arch / mips / emma / markeins / irq.c
1 /*
2  *  arch/mips/emma2rh/markeins/irq.c
3  *      This file defines the irq handler for EMMA2RH.
4  *
5  *  Copyright (C) NEC Electronics Corporation 2004-2006
6  *
7  *  This file is based on the arch/mips/ddb5xxx/ddb5477/irq.c
8  *
9  *      Copyright 2001 MontaVista Software Inc.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/types.h>
29 #include <linux/ptrace.h>
30 #include <linux/delay.h>
31
32 #include <asm/irq_cpu.h>
33 #include <asm/system.h>
34 #include <asm/mipsregs.h>
35 #include <asm/addrspace.h>
36 #include <asm/bootinfo.h>
37
38 #include <asm/emma/emma2rh.h>
39
40 /* number of total irqs supported by EMMA2RH */
41 #define NUM_EMMA2RH_IRQ         96
42
43 /*
44  * IRQ mapping
45  *
46  *  0-7: 8 CPU interrupts
47  *      0 -     software interrupt 0
48  *      1 -     software interrupt 1
49  *      2 -     most Vrc5477 interrupts are routed to this pin
50  *      3 -     (optional) some other interrupts routed to this pin for debugg
51  *      4 -     not used
52  *      5 -     not used
53  *      6 -     not used
54  *      7 -     cpu timer (used by default)
55  *
56  */
57
58 static void emma2rh_irq_enable(unsigned int irq)
59 {
60         u32 reg_value;
61         u32 reg_bitmask;
62         u32 reg_index;
63
64         irq -= EMMA2RH_IRQ_BASE;
65
66         reg_index = EMMA2RH_BHIF_INT_EN_0 +
67                     (EMMA2RH_BHIF_INT_EN_1 - EMMA2RH_BHIF_INT_EN_0) * (irq / 32);
68         reg_value = emma2rh_in32(reg_index);
69         reg_bitmask = 0x1 << (irq % 32);
70         emma2rh_out32(reg_index, reg_value | reg_bitmask);
71 }
72
73 static void emma2rh_irq_disable(unsigned int irq)
74 {
75         u32 reg_value;
76         u32 reg_bitmask;
77         u32 reg_index;
78
79         irq -= EMMA2RH_IRQ_BASE;
80
81         reg_index = EMMA2RH_BHIF_INT_EN_0 +
82                     (EMMA2RH_BHIF_INT_EN_1 - EMMA2RH_BHIF_INT_EN_0) * (irq / 32);
83         reg_value = emma2rh_in32(reg_index);
84         reg_bitmask = 0x1 << (irq % 32);
85         emma2rh_out32(reg_index, reg_value & ~reg_bitmask);
86 }
87
88 struct irq_chip emma2rh_irq_controller = {
89         .name = "emma2rh_irq",
90         .ack = emma2rh_irq_disable,
91         .mask = emma2rh_irq_disable,
92         .mask_ack = emma2rh_irq_disable,
93         .unmask = emma2rh_irq_enable,
94 };
95
96 void emma2rh_irq_init(void)
97 {
98         u32 i;
99
100         for (i = 0; i < NUM_EMMA2RH_IRQ; i++)
101                 set_irq_chip_and_handler(EMMA2RH_IRQ_BASE + i,
102                                          &emma2rh_irq_controller,
103                                          handle_level_irq);
104 }
105
106 static void emma2rh_sw_irq_enable(unsigned int irq)
107 {
108         u32 reg;
109
110         irq -= EMMA2RH_SW_IRQ_BASE;
111
112         reg = emma2rh_in32(EMMA2RH_BHIF_SW_INT_EN);
113         reg |= 1 << irq;
114         emma2rh_out32(EMMA2RH_BHIF_SW_INT_EN, reg);
115 }
116
117 static void emma2rh_sw_irq_disable(unsigned int irq)
118 {
119         u32 reg;
120
121         irq -= EMMA2RH_SW_IRQ_BASE;
122
123         reg = emma2rh_in32(EMMA2RH_BHIF_SW_INT_EN);
124         reg &= ~(1 << irq);
125         emma2rh_out32(EMMA2RH_BHIF_SW_INT_EN, reg);
126 }
127
128 struct irq_chip emma2rh_sw_irq_controller = {
129         .name = "emma2rh_sw_irq",
130         .ack = emma2rh_sw_irq_disable,
131         .mask = emma2rh_sw_irq_disable,
132         .mask_ack = emma2rh_sw_irq_disable,
133         .unmask = emma2rh_sw_irq_enable,
134 };
135
136 void emma2rh_sw_irq_init(void)
137 {
138         u32 i;
139
140         for (i = 0; i < NUM_EMMA2RH_IRQ_SW; i++)
141                 set_irq_chip_and_handler(EMMA2RH_SW_IRQ_BASE + i,
142                                          &emma2rh_sw_irq_controller,
143                                          handle_level_irq);
144 }
145
146 static void emma2rh_gpio_irq_enable(unsigned int irq)
147 {
148         u32 reg;
149
150         irq -= EMMA2RH_GPIO_IRQ_BASE;
151
152         reg = emma2rh_in32(EMMA2RH_GPIO_INT_MASK);
153         reg |= 1 << irq;
154         emma2rh_out32(EMMA2RH_GPIO_INT_MASK, reg);
155 }
156
157 static void emma2rh_gpio_irq_disable(unsigned int irq)
158 {
159         u32 reg;
160
161         irq -= EMMA2RH_GPIO_IRQ_BASE;
162
163         reg = emma2rh_in32(EMMA2RH_GPIO_INT_MASK);
164         reg &= ~(1 << irq);
165         emma2rh_out32(EMMA2RH_GPIO_INT_MASK, reg);
166 }
167
168 static void emma2rh_gpio_irq_ack(unsigned int irq)
169 {
170         u32 reg;
171
172         irq -= EMMA2RH_GPIO_IRQ_BASE;
173         emma2rh_out32(EMMA2RH_GPIO_INT_ST, ~(1 << irq));
174
175         reg = emma2rh_in32(EMMA2RH_GPIO_INT_MASK);
176         reg &= ~(1 << irq);
177         emma2rh_out32(EMMA2RH_GPIO_INT_MASK, reg);
178 }
179
180 static void emma2rh_gpio_irq_end(unsigned int irq)
181 {
182         u32 reg;
183
184         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
185
186                 irq -= EMMA2RH_GPIO_IRQ_BASE;
187
188                 reg = emma2rh_in32(EMMA2RH_GPIO_INT_MASK);
189                 reg |= 1 << irq;
190                 emma2rh_out32(EMMA2RH_GPIO_INT_MASK, reg);
191         }
192 }
193
194 struct irq_chip emma2rh_gpio_irq_controller = {
195         .name = "emma2rh_gpio_irq",
196         .ack = emma2rh_gpio_irq_ack,
197         .mask = emma2rh_gpio_irq_disable,
198         .mask_ack = emma2rh_gpio_irq_ack,
199         .unmask = emma2rh_gpio_irq_enable,
200         .end = emma2rh_gpio_irq_end,
201 };
202
203 void emma2rh_gpio_irq_init(void)
204 {
205         u32 i;
206
207         for (i = 0; i < NUM_EMMA2RH_IRQ_GPIO; i++)
208                 set_irq_chip(EMMA2RH_GPIO_IRQ_BASE + i,
209                              &emma2rh_gpio_irq_controller);
210 }
211
212 static struct irqaction irq_cascade = {
213            .handler = no_action,
214            .flags = 0,
215            .mask = CPU_MASK_NONE,
216            .name = "cascade",
217            .dev_id = NULL,
218            .next = NULL,
219 };
220
221 /*
222  * the first level int-handler will jump here if it is a emma2rh irq
223  */
224 void emma2rh_irq_dispatch(void)
225 {
226         u32 intStatus;
227         u32 bitmask;
228         u32 i;
229
230         intStatus = emma2rh_in32(EMMA2RH_BHIF_INT_ST_0) &
231                     emma2rh_in32(EMMA2RH_BHIF_INT_EN_0);
232
233 #ifdef EMMA2RH_SW_CASCADE
234         if (intStatus &
235             (1 << ((EMMA2RH_SW_CASCADE - EMMA2RH_IRQ_INT0) & (32 - 1)))) {
236                 u32 swIntStatus;
237                 swIntStatus = emma2rh_in32(EMMA2RH_BHIF_SW_INT)
238                     & emma2rh_in32(EMMA2RH_BHIF_SW_INT_EN);
239                 for (i = 0, bitmask = 1; i < 32; i++, bitmask <<= 1) {
240                         if (swIntStatus & bitmask) {
241                                 do_IRQ(EMMA2RH_SW_IRQ_BASE + i);
242                                 return;
243                         }
244                 }
245         }
246 #endif
247
248         for (i = 0, bitmask = 1; i < 32; i++, bitmask <<= 1) {
249                 if (intStatus & bitmask) {
250                         do_IRQ(EMMA2RH_IRQ_BASE + i);
251                         return;
252                 }
253         }
254
255         intStatus = emma2rh_in32(EMMA2RH_BHIF_INT_ST_1) &
256                     emma2rh_in32(EMMA2RH_BHIF_INT_EN_1);
257
258 #ifdef EMMA2RH_GPIO_CASCADE
259         if (intStatus &
260             (1 << ((EMMA2RH_GPIO_CASCADE - EMMA2RH_IRQ_INT0) & (32 - 1)))) {
261                 u32 gpioIntStatus;
262                 gpioIntStatus = emma2rh_in32(EMMA2RH_GPIO_INT_ST)
263                     & emma2rh_in32(EMMA2RH_GPIO_INT_MASK);
264                 for (i = 0, bitmask = 1; i < 32; i++, bitmask <<= 1) {
265                         if (gpioIntStatus & bitmask) {
266                                 do_IRQ(EMMA2RH_GPIO_IRQ_BASE + i);
267                                 return;
268                         }
269                 }
270         }
271 #endif
272
273         for (i = 32, bitmask = 1; i < 64; i++, bitmask <<= 1) {
274                 if (intStatus & bitmask) {
275                         do_IRQ(EMMA2RH_IRQ_BASE + i);
276                         return;
277                 }
278         }
279
280         intStatus = emma2rh_in32(EMMA2RH_BHIF_INT_ST_2) &
281                     emma2rh_in32(EMMA2RH_BHIF_INT_EN_2);
282
283         for (i = 64, bitmask = 1; i < 96; i++, bitmask <<= 1) {
284                 if (intStatus & bitmask) {
285                         do_IRQ(EMMA2RH_IRQ_BASE + i);
286                         return;
287                 }
288         }
289 }
290
291 void __init arch_init_irq(void)
292 {
293         u32 reg;
294
295         /* by default, interrupts are disabled. */
296         emma2rh_out32(EMMA2RH_BHIF_INT_EN_0, 0);
297         emma2rh_out32(EMMA2RH_BHIF_INT_EN_1, 0);
298         emma2rh_out32(EMMA2RH_BHIF_INT_EN_2, 0);
299         emma2rh_out32(EMMA2RH_BHIF_INT1_EN_0, 0);
300         emma2rh_out32(EMMA2RH_BHIF_INT1_EN_1, 0);
301         emma2rh_out32(EMMA2RH_BHIF_INT1_EN_2, 0);
302         emma2rh_out32(EMMA2RH_BHIF_SW_INT_EN, 0);
303
304         clear_c0_status(0xff00);
305         set_c0_status(0x0400);
306
307 #define GPIO_PCI (0xf<<15)
308         /* setup GPIO interrupt for PCI interface */
309         /* direction input */
310         reg = emma2rh_in32(EMMA2RH_GPIO_DIR);
311         emma2rh_out32(EMMA2RH_GPIO_DIR, reg & ~GPIO_PCI);
312         /* disable interrupt */
313         reg = emma2rh_in32(EMMA2RH_GPIO_INT_MASK);
314         emma2rh_out32(EMMA2RH_GPIO_INT_MASK, reg & ~GPIO_PCI);
315         /* level triggerd */
316         reg = emma2rh_in32(EMMA2RH_GPIO_INT_MODE);
317         emma2rh_out32(EMMA2RH_GPIO_INT_MODE, reg | GPIO_PCI);
318         reg = emma2rh_in32(EMMA2RH_GPIO_INT_CND_A);
319         emma2rh_out32(EMMA2RH_GPIO_INT_CND_A, reg & (~GPIO_PCI));
320         /* interrupt clear */
321         emma2rh_out32(EMMA2RH_GPIO_INT_ST, ~GPIO_PCI);
322
323         /* init all controllers */
324         emma2rh_irq_init();
325         emma2rh_sw_irq_init();
326         emma2rh_gpio_irq_init();
327         mips_cpu_irq_init();
328
329         /* setup cascade interrupts */
330         setup_irq(EMMA2RH_IRQ_BASE + EMMA2RH_SW_CASCADE, &irq_cascade);
331         setup_irq(EMMA2RH_IRQ_BASE + EMMA2RH_GPIO_CASCADE, &irq_cascade);
332         setup_irq(CPU_IRQ_BASE + CPU_EMMA2RH_CASCADE, &irq_cascade);
333 }
334
335 asmlinkage void plat_irq_dispatch(void)
336 {
337         unsigned int pending = read_c0_status() & read_c0_cause() & ST0_IM;
338
339         if (pending & STATUSF_IP7)
340                 do_IRQ(CPU_IRQ_BASE + 7);
341         else if (pending & STATUSF_IP2)
342                 emma2rh_irq_dispatch();
343         else if (pending & STATUSF_IP1)
344                 do_IRQ(CPU_IRQ_BASE + 1);
345         else if (pending & STATUSF_IP0)
346                 do_IRQ(CPU_IRQ_BASE + 0);
347         else
348                 spurious_interrupt();
349 }
350
351