[MIPS] IRQ cleanups
[safe/jmp/linux-2.6] / arch / mips / mips-boards / atlas / atlas_int.c
1 /*
2  * Copyright (C) 1999, 2000, 2006  MIPS Technologies, Inc.
3  *      All rights reserved.
4  *      Authors: Carsten Langgaard <carstenl@mips.com>
5  *               Maciej W. Rozycki <macro@mips.com>
6  *
7  * ########################################################################
8  *
9  *  This program is free software; you can distribute it and/or modify it
10  *  under the terms of the GNU General Public License (Version 2) as
11  *  published by the Free Software Foundation.
12  *
13  *  This program is distributed in the hope it will be useful, but WITHOUT
14  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16  *  for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
21  *
22  * ########################################################################
23  *
24  * Routines for generic manipulation of the interrupts found on the MIPS
25  * Atlas board.
26  *
27  */
28 #include <linux/compiler.h>
29 #include <linux/init.h>
30 #include <linux/irq.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/interrupt.h>
34 #include <linux/kernel_stat.h>
35
36 #include <asm/gdb-stub.h>
37 #include <asm/io.h>
38 #include <asm/irq_cpu.h>
39 #include <asm/msc01_ic.h>
40
41 #include <asm/mips-boards/atlas.h>
42 #include <asm/mips-boards/atlasint.h>
43 #include <asm/mips-boards/generic.h>
44
45 static struct atlas_ictrl_regs *atlas_hw0_icregs;
46
47 #if 0
48 #define DEBUG_INT(x...) printk(x)
49 #else
50 #define DEBUG_INT(x...)
51 #endif
52
53 void disable_atlas_irq(unsigned int irq_nr)
54 {
55         atlas_hw0_icregs->intrsten = 1 << (irq_nr - ATLAS_INT_BASE);
56         iob();
57 }
58
59 void enable_atlas_irq(unsigned int irq_nr)
60 {
61         atlas_hw0_icregs->intseten = 1 << (irq_nr - ATLAS_INT_BASE);
62         iob();
63 }
64
65 static void end_atlas_irq(unsigned int irq)
66 {
67         if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
68                 enable_atlas_irq(irq);
69 }
70
71 static struct irq_chip atlas_irq_type = {
72         .typename = "Atlas",
73         .ack = disable_atlas_irq,
74         .mask = disable_atlas_irq,
75         .mask_ack = disable_atlas_irq,
76         .unmask = enable_atlas_irq,
77         .end = end_atlas_irq,
78 };
79
80 static inline int ls1bit32(unsigned int x)
81 {
82         int b = 31, s;
83
84         s = 16; if (x << 16 == 0) s = 0; b -= s; x <<= s;
85         s =  8; if (x <<  8 == 0) s = 0; b -= s; x <<= s;
86         s =  4; if (x <<  4 == 0) s = 0; b -= s; x <<= s;
87         s =  2; if (x <<  2 == 0) s = 0; b -= s; x <<= s;
88         s =  1; if (x <<  1 == 0) s = 0; b -= s;
89
90         return b;
91 }
92
93 static inline void atlas_hw0_irqdispatch(void)
94 {
95         unsigned long int_status;
96         int irq;
97
98         int_status = atlas_hw0_icregs->intstatus;
99
100         /* if int_status == 0, then the interrupt has already been cleared */
101         if (unlikely(int_status == 0))
102                 return;
103
104         irq = ATLAS_INT_BASE + ls1bit32(int_status);
105
106         DEBUG_INT("atlas_hw0_irqdispatch: irq=%d\n", irq);
107
108         do_IRQ(irq);
109 }
110
111 static inline int clz(unsigned long x)
112 {
113         __asm__ (
114         "       .set    push                                    \n"
115         "       .set    mips32                                  \n"
116         "       clz     %0, %1                                  \n"
117         "       .set    pop                                     \n"
118         : "=r" (x)
119         : "r" (x));
120
121         return x;
122 }
123
124 /*
125  * Version of ffs that only looks at bits 12..15.
126  */
127 static inline unsigned int irq_ffs(unsigned int pending)
128 {
129 #if defined(CONFIG_CPU_MIPS32) || defined(CONFIG_CPU_MIPS64)
130         return -clz(pending) + 31 - CAUSEB_IP;
131 #else
132         unsigned int a0 = 7;
133         unsigned int t0;
134
135         t0 = s0 & 0xf000;
136         t0 = t0 < 1;
137         t0 = t0 << 2;
138         a0 = a0 - t0;
139         s0 = s0 << t0;
140
141         t0 = s0 & 0xc000;
142         t0 = t0 < 1;
143         t0 = t0 << 1;
144         a0 = a0 - t0;
145         s0 = s0 << t0;
146
147         t0 = s0 & 0x8000;
148         t0 = t0 < 1;
149         //t0 = t0 << 2;
150         a0 = a0 - t0;
151         //s0 = s0 << t0;
152
153         return a0;
154 #endif
155 }
156
157 /*
158  * IRQs on the Atlas board look basically like (all external interrupt
159  * sources are combined together on hardware interrupt 0 (MIPS IRQ 2)):
160  *
161  *      MIPS IRQ        Source
162  *      --------        ------
163  *             0        Software 0 (reschedule IPI on MT)
164  *             1        Software 1 (remote call IPI on MT)
165  *             2        Combined Atlas hardware interrupt (hw0)
166  *             3        Hardware (ignored)
167  *             4        Hardware (ignored)
168  *             5        Hardware (ignored)
169  *             6        Hardware (ignored)
170  *             7        R4k timer (what we use)
171  *
172  * We handle the IRQ according to _our_ priority which is:
173  *
174  * Highest ----     R4k Timer
175  * Lowest  ----     Software 0
176  *
177  * then we just return, if multiple IRQs are pending then we will just take
178  * another exception, big deal.
179  */
180 asmlinkage void plat_irq_dispatch(void)
181 {
182         unsigned int pending = read_c0_cause() & read_c0_status() & ST0_IM;
183         int irq;
184
185         irq = irq_ffs(pending);
186
187         if (irq == MIPSCPU_INT_ATLAS)
188                 atlas_hw0_irqdispatch();
189         else if (irq >= 0)
190                 do_IRQ(MIPSCPU_INT_BASE + irq);
191         else
192                 spurious_interrupt();
193 }
194
195 static inline void init_atlas_irqs (int base)
196 {
197         int i;
198
199         atlas_hw0_icregs = (struct atlas_ictrl_regs *)
200                            ioremap(ATLAS_ICTRL_REGS_BASE,
201                                    sizeof(struct atlas_ictrl_regs *));
202
203         /*
204          * Mask out all interrupt by writing "1" to all bit position in
205          * the interrupt reset reg.
206          */
207         atlas_hw0_icregs->intrsten = 0xffffffff;
208
209         for (i = ATLAS_INT_BASE; i <= ATLAS_INT_END; i++)
210                 set_irq_chip(i, &atlas_irq_type);
211 }
212
213 static struct irqaction atlasirq = {
214         .handler = no_action,
215         .name = "Atlas cascade"
216 };
217
218 msc_irqmap_t __initdata msc_irqmap[] = {
219         {MSC01C_INT_TMR,                MSC01_IRQ_EDGE, 0},
220         {MSC01C_INT_PCI,                MSC01_IRQ_LEVEL, 0},
221 };
222 int __initdata msc_nr_irqs = sizeof(msc_irqmap) / sizeof(*msc_irqmap);
223
224 msc_irqmap_t __initdata msc_eicirqmap[] = {
225         {MSC01E_INT_SW0,                MSC01_IRQ_LEVEL, 0},
226         {MSC01E_INT_SW1,                MSC01_IRQ_LEVEL, 0},
227         {MSC01E_INT_ATLAS,              MSC01_IRQ_LEVEL, 0},
228         {MSC01E_INT_TMR,                MSC01_IRQ_EDGE, 0},
229         {MSC01E_INT_PCI,                MSC01_IRQ_LEVEL, 0},
230         {MSC01E_INT_PERFCTR,            MSC01_IRQ_LEVEL, 0},
231         {MSC01E_INT_CPUCTR,             MSC01_IRQ_LEVEL, 0}
232 };
233 int __initdata msc_nr_eicirqs = sizeof(msc_eicirqmap) / sizeof(*msc_eicirqmap);
234
235 void __init arch_init_irq(void)
236 {
237         init_atlas_irqs(ATLAS_INT_BASE);
238
239         if (!cpu_has_veic)
240                 mips_cpu_irq_init(MIPSCPU_INT_BASE);
241
242         switch(mips_revision_corid) {
243         case MIPS_REVISION_CORID_CORE_MSC:
244         case MIPS_REVISION_CORID_CORE_FPGA2:
245         case MIPS_REVISION_CORID_CORE_FPGA3:
246         case MIPS_REVISION_CORID_CORE_24K:
247         case MIPS_REVISION_CORID_CORE_EMUL_MSC:
248                 if (cpu_has_veic)
249                         init_msc_irqs (MSC01E_INT_BASE,
250                                        msc_eicirqmap, msc_nr_eicirqs);
251                 else
252                         init_msc_irqs (MSC01C_INT_BASE,
253                                        msc_irqmap, msc_nr_irqs);
254         }
255
256
257         if (cpu_has_veic) {
258                 set_vi_handler (MSC01E_INT_ATLAS, atlas_hw0_irqdispatch);
259                 setup_irq (MSC01E_INT_BASE + MSC01E_INT_ATLAS, &atlasirq);
260         } else if (cpu_has_vint) {
261                 set_vi_handler (MIPSCPU_INT_ATLAS, atlas_hw0_irqdispatch);
262 #ifdef CONFIG_MIPS_MT_SMTC
263                 setup_irq_smtc (MIPSCPU_INT_BASE + MIPSCPU_INT_ATLAS,
264                                 &atlasirq, (0x100 << MIPSCPU_INT_ATLAS));
265 #else /* Not SMTC */
266                 setup_irq(MIPSCPU_INT_BASE + MIPSCPU_INT_ATLAS, &atlasirq);
267 #endif /* CONFIG_MIPS_MT_SMTC */
268         } else
269                 setup_irq(MIPSCPU_INT_BASE + MIPSCPU_INT_ATLAS, &atlasirq);
270 }