ddad0ef87f13027fd638c48a0c9822e99c7c1395
[bcm963xx.git] / kernel / linux / arch / mips / brcm-boards / bcm963xx / irq.c
1 /*
2 <:copyright-gpl 
3  Copyright 2002 Broadcom Corp. All Rights Reserved. 
4  
5  This program is free software; you can distribute it and/or modify it 
6  under the terms of the GNU General Public License (Version 2) as 
7  published by the Free Software Foundation. 
8  
9  This program is distributed in the hope it will be useful, but WITHOUT 
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
11  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
12  for more details. 
13  
14  You should have received a copy of the GNU General Public License along 
15  with this program; if not, write to the Free Software Foundation, Inc., 
16  59 Temple Place - Suite 330, Boston MA 02111-1307, USA. 
17 :>
18 */
19 /*
20  * Interrupt control functions for Broadcom 963xx MIPS boards
21  */
22
23 #include <asm/atomic.h>
24
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33
34 #include <asm/irq.h>
35 #include <asm/mipsregs.h>
36 #include <asm/addrspace.h>
37 #include <asm/signal.h>
38 #include <bcm_map_part.h>
39 #include <bcm_intr.h>
40
41 extern asmlinkage unsigned int do_IRQ(int irq, struct pt_regs *regs);
42
43 static void irq_dispatch_int(struct pt_regs *regs)
44 {
45     unsigned int pendingIrqs;
46     static unsigned int irqBit;
47     static unsigned int isrNumber = 31;
48
49     pendingIrqs = PERF->IrqStatus & PERF->IrqMask;
50     if (!pendingIrqs) {
51         printk("***no pending IRQ***\n");
52         return;
53     }
54
55     while (1) {
56         irqBit <<= 1;
57         isrNumber++;
58         if (isrNumber == 32) {
59                 isrNumber = 0;
60                 irqBit = 0x1;
61         }
62         if (pendingIrqs & irqBit) {
63                 PERF->IrqMask &= ~irqBit; // mask
64                 do_IRQ(isrNumber + INTERNAL_ISR_TABLE_OFFSET, regs);
65                 break;
66         }
67     }
68 }
69
70 static void irq_dispatch_ext(uint32 irq, struct pt_regs *regs)
71 {
72     if (!(PERF->ExtIrqCfg & (1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_MASK_SHFT)))) {
73         printk("**** Ext IRQ mask. Should not dispatch ****\n");
74     }
75     /* disable and clear interrupt in the controller */
76     PERF->ExtIrqCfg |= (1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_CLEAR_SHFT));
77     PERF->ExtIrqCfg &= ~(1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_MASK_SHFT));
78     do_IRQ(irq, regs);
79 }
80
81 void brcm_irq_dispatch(struct pt_regs *regs)
82 {
83     u32 cause;
84     while((cause = (read_c0_cause()& CAUSEF_IP))) {
85         if (cause & CAUSEF_IP7)
86                 do_IRQ(MIPS_TIMER_INT, regs);
87         else if (cause & CAUSEF_IP2)
88                 irq_dispatch_int(regs);
89         else if (cause & CAUSEF_IP3)
90                 irq_dispatch_ext(INTERRUPT_ID_EXTERNAL_0, regs);
91         else if (cause & CAUSEF_IP4)
92                 irq_dispatch_ext(INTERRUPT_ID_EXTERNAL_1, regs);
93         else if (cause & CAUSEF_IP5)
94                 irq_dispatch_ext(INTERRUPT_ID_EXTERNAL_2, regs);
95         else if (cause & CAUSEF_IP6)
96                 irq_dispatch_ext(INTERRUPT_ID_EXTERNAL_3, regs);
97         cli();
98     }
99 }
100
101
102 void enable_brcm_irq(unsigned int irq)
103 {
104     unsigned long flags;
105
106     local_irq_save(flags);
107     if( irq >= INTERNAL_ISR_TABLE_OFFSET ) {
108         PERF->IrqMask |= (1 << (irq - INTERNAL_ISR_TABLE_OFFSET));
109     }
110     else if (irq >= INTERRUPT_ID_EXTERNAL_0 && irq <= INTERRUPT_ID_EXTERNAL_3) {
111     /* enable and clear interrupt in the controller */
112         PERF->ExtIrqCfg |= (1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_CLEAR_SHFT));
113         PERF->ExtIrqCfg |= (1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_MASK_SHFT));
114     }
115     local_irq_restore(flags);
116 }
117
118 void disable_brcm_irq(unsigned int irq)
119 {
120     unsigned long flags;
121
122     local_irq_save(flags);
123     if( irq >= INTERNAL_ISR_TABLE_OFFSET ) {
124         PERF->IrqMask &= ~(1 << (irq - INTERNAL_ISR_TABLE_OFFSET));
125     }
126     else if (irq >= INTERRUPT_ID_EXTERNAL_0 && irq <= INTERRUPT_ID_EXTERNAL_3) {
127     /* disable interrupt in the controller */
128         PERF->ExtIrqCfg &= ~(1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_MASK_SHFT));
129     }
130     local_irq_restore(flags);
131 }
132
133 void ack_brcm_irq(unsigned int irq)
134 {
135     /* Already done in brcm_irq_dispatch */
136 }
137
138 unsigned int startup_brcm_irq(unsigned int irq)
139 {
140     enable_brcm_irq(irq);
141
142     return 0; /* never anything pending */
143 }
144
145 unsigned int startup_brcm_none(unsigned int irq)
146 {
147     return 0;
148 }
149
150 void end_brcm_irq(unsigned int irq)
151 {
152     if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
153         enable_brcm_irq(irq);
154 }
155
156 void end_brcm_none(unsigned int irq)
157 {
158 }
159
160 #define ALLINTS_NOTIMER (IE_IRQ0 | IE_IRQ1 | IE_IRQ2 | IE_IRQ3 | IE_IRQ4)
161
162 static void __init brcm_irq_setup(void)
163 {
164         extern asmlinkage void brcmIRQ(void);
165
166         clear_c0_status(ST0_BEV);
167         set_except_vector(0, brcmIRQ);
168         change_c0_status(ST0_IM, ALLINTS_NOTIMER);
169
170 #ifdef CONFIG_REMOTE_DEBUG
171         rs_kgdb_hook(0);
172 #endif
173 }
174
175 static struct hw_interrupt_type brcm_irq_type = {
176     .typename   = "MIPS",
177     .startup    = startup_brcm_irq,
178     .shutdown   = disable_brcm_irq,
179     .enable     = enable_brcm_irq,
180     .disable    = disable_brcm_irq,
181     .ack        = ack_brcm_irq,
182     .end        = end_brcm_irq,
183     .set_affinity = NULL
184 };
185
186 static struct hw_interrupt_type brcm_irq_no_end_type = {
187     .typename   = "MIPS",
188     .startup    = startup_brcm_none,
189     .shutdown   = disable_brcm_irq,
190     .enable     = enable_brcm_irq,
191     .disable    = disable_brcm_irq,
192     .ack        = ack_brcm_irq,
193     .end        = end_brcm_none,
194     .set_affinity = NULL
195 };
196
197 void __init arch_init_irq(void)
198 {
199     int i;
200
201     for (i = 0; i < NR_IRQS; i++) {
202         irq_desc[i].status = IRQ_DISABLED;
203         irq_desc[i].action = 0;
204         irq_desc[i].depth = 1;
205         irq_desc[i].handler = &brcm_irq_type;
206     }
207
208     brcm_irq_setup();
209 }
210
211 int request_external_irq(unsigned int irq, 
212         FN_HANDLER handler,
213         unsigned long irqflags, 
214         const char * devname,
215         void *dev_id)
216 {
217     unsigned long flags;
218
219     local_irq_save(flags);
220
221     PERF->ExtIrqCfg |= (1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_CLEAR_SHFT));      // Clear
222     PERF->ExtIrqCfg &= ~(1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_MASK_SHFT));      // Mask
223     PERF->ExtIrqCfg &= ~(1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_INSENS_SHFT));    // Edge insesnsitive
224     PERF->ExtIrqCfg |= (1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_LEVEL_SHFT));      // Level triggered
225     PERF->ExtIrqCfg &= ~(1 << (irq - INTERRUPT_ID_EXTERNAL_0 + EI_SENSE_SHFT));     // Low level
226
227     local_irq_restore(flags);
228
229     return( request_irq(irq, handler, irqflags, devname, dev_id) );
230 }
231
232 /* VxWorks compatibility function(s). */
233
234 unsigned int BcmHalMapInterrupt(FN_HANDLER pfunc, unsigned int param,
235     unsigned int interruptId)
236 {
237     int nRet = -1;
238     char *devname;
239
240     devname = kmalloc(16, GFP_KERNEL);
241     if (devname)
242         sprintf( devname, "brcm_%d", interruptId );
243
244     /* Set the IRQ description to not automatically enable the interrupt at
245      * the end of an ISR.  The driver that handles the interrupt must
246      * explicitly call BcmHalInterruptEnable or enable_brcm_irq.  This behavior
247      * is consistent with interrupt handling on VxWorks.
248      */
249     irq_desc[interruptId].handler = &brcm_irq_no_end_type;
250
251     if( interruptId >= INTERNAL_ISR_TABLE_OFFSET )
252     {
253         nRet = request_irq( interruptId, pfunc, SA_SAMPLE_RANDOM | SA_INTERRUPT,
254             devname, (void *) param );
255     }
256     else if (interruptId >= INTERRUPT_ID_EXTERNAL_0 && interruptId <= INTERRUPT_ID_EXTERNAL_3)
257     {
258         nRet = request_external_irq( interruptId, pfunc, SA_SAMPLE_RANDOM | SA_INTERRUPT,
259             devname, (void *) param );
260     }
261
262     return( nRet );
263 }
264
265
266 /* Debug function. */
267
268 void dump_intr_regs(void)
269 {
270     printk("PERF->ExtIrqCfg [%08x]\n", *(&(PERF->ExtIrqCfg)));
271
272
273 EXPORT_SYMBOL(enable_brcm_irq);
274 EXPORT_SYMBOL(disable_brcm_irq);
275 EXPORT_SYMBOL(request_external_irq);
276 EXPORT_SYMBOL(BcmHalMapInterrupt);
277