import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / arch / mips / ite-boards / generic / irq.c
1 /*
2  *
3  * BRIEF MODULE DESCRIPTION
4  *      ITE 8172G interrupt/setup routines.
5  *
6  * Copyright 2000,2001 MontaVista Software Inc.
7  * Author: MontaVista Software, Inc.
8  *              ppopov@mvista.com or source@mvista.com
9  *
10  * Part of this file was derived from Carsten Langgaard's
11  * arch/mips/mips-boards/atlas/atlas_int.c.
12  *
13  * Carsten Langgaard, carstenl@mips.com
14  * Copyright (C) 1999,2000 MIPS Technologies, Inc.  All rights reserved.
15  *
16  *  This program is free software; you can redistribute  it and/or modify it
17  *  under  the terms of  the GNU General  Public License as published by the
18  *  Free Software Foundation;  either version 2 of the  License, or (at your
19  *  option) any later version.
20  *
21  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
22  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
23  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
24  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
25  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
27  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *  You should have received a copy of the  GNU General Public License along
33  *  with this program; if not, write  to the Free Software Foundation, Inc.,
34  *  675 Mass Ave, Cambridge, MA 02139, USA.
35  */
36 #include <linux/config.h>
37 #include <linux/errno.h>
38 #include <linux/init.h>
39 #include <linux/irq.h>
40 #include <linux/kernel_stat.h>
41 #include <linux/module.h>
42 #include <linux/signal.h>
43 #include <linux/sched.h>
44 #include <linux/types.h>
45 #include <linux/interrupt.h>
46 #include <linux/ioport.h>
47 #include <linux/timex.h>
48 #include <linux/slab.h>
49 #include <linux/random.h>
50 #include <linux/serial_reg.h>
51
52 #include <asm/bitops.h>
53 #include <asm/bootinfo.h>
54 #include <asm/io.h>
55 #include <asm/mipsregs.h>
56 #include <asm/system.h>
57 #include <asm/it8172/it8172.h>
58 #include <asm/it8172/it8172_int.h>
59 #include <asm/it8172/it8172_dbg.h>
60
61 #undef DEBUG_IRQ
62 #ifdef DEBUG_IRQ
63 /* note: prints function name for you */
64 #define DPRINTK(fmt, args...) printk("%s: " fmt, __FUNCTION__ , ## args)
65 #else
66 #define DPRINTK(fmt, args...)
67 #endif
68
69 #ifdef CONFIG_KGDB
70 extern void breakpoint(void);
71 #endif
72
73 /* revisit */
74 #define EXT_IRQ0_TO_IP 2 /* IP 2 */
75 #define EXT_IRQ5_TO_IP 7 /* IP 7 */
76
77 #define ALLINTS_NOTIMER (IE_IRQ0 | IE_IRQ1 | IE_IRQ2 | IE_IRQ3 | IE_IRQ4)
78
79 unsigned int local_bh_count[NR_CPUS];
80 unsigned int local_irq_count[NR_CPUS];
81 void disable_it8172_irq(unsigned int irq_nr);
82 void enable_it8172_irq(unsigned int irq_nr);
83
84 extern void set_debug_traps(void);
85 extern void mips_timer_interrupt(int irq, struct pt_regs *regs);
86 extern asmlinkage void it8172_IRQ(void);
87
88 struct it8172_intc_regs volatile *it8172_hw0_icregs
89         = (struct it8172_intc_regs volatile *)(KSEG1ADDR(IT8172_PCI_IO_BASE + IT_INTC_BASE));
90
91 /* Function for careful CP0 interrupt mask access */
92 static inline void modify_cp0_intmask(unsigned clr_mask, unsigned set_mask)
93 {
94         unsigned long status = read_c0_status();
95         status &= ~((clr_mask & 0xFF) << 8);
96         status |=   (set_mask & 0xFF) << 8;
97         write_c0_status(status);
98 }
99
100 static inline void mask_irq(unsigned int irq_nr)
101 {
102         modify_cp0_intmask(irq_nr, 0);
103 }
104
105 static inline void unmask_irq(unsigned int irq_nr)
106 {
107         modify_cp0_intmask(0, irq_nr);
108 }
109
110 void local_disable_irq(unsigned int irq_nr)
111 {
112         unsigned long flags;
113
114         save_and_cli(flags);
115         disable_it8172_irq(irq_nr);
116         restore_flags(flags);
117 }
118
119 void local_enable_irq(unsigned int irq_nr)
120 {
121         unsigned long flags;
122
123         save_and_cli(flags);
124         enable_it8172_irq(irq_nr);
125         restore_flags(flags);
126 }
127
128
129 void disable_it8172_irq(unsigned int irq_nr)
130 {
131         DPRINTK("disable_it8172_irq %d\n", irq_nr);
132
133         if ( (irq_nr >= IT8172_LPC_IRQ_BASE) && (irq_nr <= IT8172_SERIRQ_15)) {
134                 /* LPC interrupt */
135                 DPRINTK("DB lpc_mask  %x\n", it8172_hw0_icregs->lpc_mask);
136                 it8172_hw0_icregs->lpc_mask |=
137                         (1 << (irq_nr - IT8172_LPC_IRQ_BASE));
138                 DPRINTK("DA lpc_mask  %x\n", it8172_hw0_icregs->lpc_mask);
139         }
140         else if ( (irq_nr >= IT8172_LB_IRQ_BASE) && (irq_nr <= IT8172_IOCHK_IRQ)) {
141                 /* Local Bus interrupt */
142                 DPRINTK("DB lb_mask  %x\n", it8172_hw0_icregs->lb_mask);
143                 it8172_hw0_icregs->lb_mask |=
144                         (1 << (irq_nr - IT8172_LB_IRQ_BASE));
145                 DPRINTK("DA lb_mask  %x\n", it8172_hw0_icregs->lb_mask);
146         }
147         else if ( (irq_nr >= IT8172_PCI_DEV_IRQ_BASE) && (irq_nr <= IT8172_DMA_IRQ)) {
148                 /* PCI and other interrupts */
149                 DPRINTK("DB pci_mask  %x\n", it8172_hw0_icregs->pci_mask);
150                 it8172_hw0_icregs->pci_mask |=
151                         (1 << (irq_nr - IT8172_PCI_DEV_IRQ_BASE));
152                 DPRINTK("DA pci_mask  %x\n", it8172_hw0_icregs->pci_mask);
153         }
154         else if ( (irq_nr >= IT8172_NMI_IRQ_BASE) && (irq_nr <= IT8172_POWER_NMI_IRQ)) {
155                 /* NMI interrupts */
156                 DPRINTK("DB nmi_mask  %x\n", it8172_hw0_icregs->nmi_mask);
157                 it8172_hw0_icregs->nmi_mask |=
158                         (1 << (irq_nr - IT8172_NMI_IRQ_BASE));
159                 DPRINTK("DA nmi_mask  %x\n", it8172_hw0_icregs->nmi_mask);
160         }
161         else {
162                 panic("disable_it8172_irq: bad irq %d", irq_nr);
163         }
164 }
165
166 void enable_it8172_irq(unsigned int irq_nr)
167 {
168         DPRINTK("enable_it8172_irq %d\n", irq_nr);
169         if ( (irq_nr >= IT8172_LPC_IRQ_BASE) && (irq_nr <= IT8172_SERIRQ_15)) {
170                 /* LPC interrupt */
171                 DPRINTK("EB before lpc_mask  %x\n", it8172_hw0_icregs->lpc_mask);
172                 it8172_hw0_icregs->lpc_mask &=
173                         ~(1 << (irq_nr - IT8172_LPC_IRQ_BASE));
174                 DPRINTK("EA after lpc_mask  %x\n", it8172_hw0_icregs->lpc_mask);
175         }
176         else if ( (irq_nr >= IT8172_LB_IRQ_BASE) && (irq_nr <= IT8172_IOCHK_IRQ)) {
177                 /* Local Bus interrupt */
178                 DPRINTK("EB lb_mask  %x\n", it8172_hw0_icregs->lb_mask);
179                 it8172_hw0_icregs->lb_mask &=
180                         ~(1 << (irq_nr - IT8172_LB_IRQ_BASE));
181                 DPRINTK("EA lb_mask  %x\n", it8172_hw0_icregs->lb_mask);
182         }
183         else if ( (irq_nr >= IT8172_PCI_DEV_IRQ_BASE) && (irq_nr <= IT8172_DMA_IRQ)) {
184                 /* PCI and other interrupts */
185                 DPRINTK("EB pci_mask  %x\n", it8172_hw0_icregs->pci_mask);
186                 it8172_hw0_icregs->pci_mask &=
187                         ~(1 << (irq_nr - IT8172_PCI_DEV_IRQ_BASE));
188                 DPRINTK("EA pci_mask  %x\n", it8172_hw0_icregs->pci_mask);
189         }
190         else if ( (irq_nr >= IT8172_NMI_IRQ_BASE) && (irq_nr <= IT8172_POWER_NMI_IRQ)) {
191                 /* NMI interrupts */
192                 DPRINTK("EB nmi_mask  %x\n", it8172_hw0_icregs->nmi_mask);
193                 it8172_hw0_icregs->nmi_mask &=
194                         ~(1 << (irq_nr - IT8172_NMI_IRQ_BASE));
195                 DPRINTK("EA nmi_mask  %x\n", it8172_hw0_icregs->nmi_mask);
196         }
197         else {
198                 panic("enable_it8172_irq: bad irq %d", irq_nr);
199         }
200 }
201
202 static unsigned int startup_ite_irq(unsigned int irq)
203 {
204         enable_it8172_irq(irq);
205         return 0;
206 }
207
208 #define shutdown_ite_irq        disable_it8172_irq
209 #define mask_and_ack_ite_irq    disable_it8172_irq
210
211 static void end_ite_irq(unsigned int irq)
212 {
213         if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
214                 enable_it8172_irq(irq);
215 }
216
217 static struct hw_interrupt_type it8172_irq_type = {
218         "ITE8172",
219         startup_ite_irq,
220         shutdown_ite_irq,
221         enable_it8172_irq,
222         disable_it8172_irq,
223         mask_and_ack_ite_irq,
224         end_ite_irq,
225         NULL
226 };
227
228
229 static void enable_none(unsigned int irq) { }
230 static unsigned int startup_none(unsigned int irq) { return 0; }
231 static void disable_none(unsigned int irq) { }
232 static void ack_none(unsigned int irq) { }
233
234 /* startup is the same as "enable", shutdown is same as "disable" */
235 #define shutdown_none   disable_none
236 #define end_none        enable_none
237
238 static struct hw_interrupt_type cp0_irq_type = {
239         "CP0 Count",
240         startup_none,
241         shutdown_none,
242         enable_none,
243         disable_none,
244         ack_none,
245         end_none
246 };
247
248
249 void enable_cpu_timer(void)
250 {
251         unsigned long flags;
252
253         save_and_cli(flags);
254         unmask_irq(1<<EXT_IRQ5_TO_IP); /* timer interrupt */
255         restore_flags(flags);
256 }
257
258
259 void __init init_IRQ(void)
260 {
261         int i;
262         unsigned long flags;
263
264         memset(irq_desc, 0, sizeof(irq_desc));
265         set_except_vector(0, it8172_IRQ);
266
267         init_generic_irq();
268
269         /* mask all interrupts */
270         it8172_hw0_icregs->lb_mask  = 0xffff;
271         it8172_hw0_icregs->lpc_mask = 0xffff;
272         it8172_hw0_icregs->pci_mask = 0xffff;
273         it8172_hw0_icregs->nmi_mask = 0xffff;
274
275         /* make all interrupts level triggered */
276         it8172_hw0_icregs->lb_trigger  = 0;
277         it8172_hw0_icregs->lpc_trigger = 0;
278         it8172_hw0_icregs->pci_trigger = 0;
279         it8172_hw0_icregs->nmi_trigger = 0;
280
281         /* active level setting */
282         /* uart, keyboard, and mouse are active high */
283         it8172_hw0_icregs->lpc_level = (0x10 | 0x2 | 0x1000);
284         it8172_hw0_icregs->lb_level |= 0x20;
285
286         /* keyboard and mouse are edge triggered */
287         it8172_hw0_icregs->lpc_trigger |= (0x2 | 0x1000);
288
289
290 #if 0
291         // Enable this piece of code to make internal USB interrupt
292         // edge triggered.
293         it8172_hw0_icregs->pci_trigger |=
294                 (1 << (IT8172_USB_IRQ - IT8172_PCI_DEV_IRQ_BASE));
295         it8172_hw0_icregs->pci_level &=
296                 ~(1 << (IT8172_USB_IRQ - IT8172_PCI_DEV_IRQ_BASE));
297 #endif
298
299         for (i = 0; i <= IT8172_LAST_IRQ; i++) {
300                 irq_desc[i].handler = &it8172_irq_type;
301         }
302         irq_desc[MIPS_CPU_TIMER_IRQ].handler = &cp0_irq_type;
303         set_c0_status(ALLINTS_NOTIMER);
304
305 #ifdef CONFIG_KGDB
306         /* If local serial I/O used for debug port, enter kgdb at once */
307         puts("Waiting for kgdb to connect...");
308         set_debug_traps();
309         breakpoint();
310 #endif
311 }
312
313 void mips_spurious_interrupt(struct pt_regs *regs)
314 {
315 #if 1
316         return;
317 #else
318         unsigned long status, cause;
319
320         printk("got spurious interrupt\n");
321         status = read_c0_status();
322         cause = read_c0_cause();
323         printk("status %x cause %x\n", status, cause);
324         printk("epc %x badvaddr %x \n", regs->cp0_epc, regs->cp0_badvaddr);
325 //      while(1);
326 #endif
327 }
328
329 void it8172_hw0_irqdispatch(struct pt_regs *regs)
330 {
331         int irq;
332         unsigned short intstatus = 0, status = 0;
333
334         intstatus = it8172_hw0_icregs->intstatus;
335         if (intstatus & 0x8) {
336                 panic("Got NMI interrupt");
337         }
338         else if (intstatus & 0x4) {
339                 /* PCI interrupt */
340                 irq = 0;
341                 status |= it8172_hw0_icregs->pci_req;
342                 while (!(status & 0x1)) {
343                         irq++;
344                         status >>= 1;
345                 }
346                 irq += IT8172_PCI_DEV_IRQ_BASE;
347                 //printk("pci int %d\n", irq);
348         }
349         else if (intstatus & 0x1) {
350                 /* Local Bus interrupt */
351                 irq = 0;
352                 status |= it8172_hw0_icregs->lb_req;
353                 while (!(status & 0x1)) {
354                         irq++;
355                         status >>= 1;
356                 }
357                 irq += IT8172_LB_IRQ_BASE;
358                 //printk("lb int %d\n", irq);
359         }
360         else if (intstatus & 0x2) {
361                 /* LPC interrupt */
362                 /* Since some lpc interrupts are edge triggered,
363                  * we could lose an interrupt this way because
364                  * we acknowledge all ints at onces. Revisit.
365                  */
366                 status |= it8172_hw0_icregs->lpc_req;
367                 it8172_hw0_icregs->lpc_req = 0; /* acknowledge ints */
368                 irq = 0;
369                 while (!(status & 0x1)) {
370                         irq++;
371                         status >>= 1;
372                 }
373                 irq += IT8172_LPC_IRQ_BASE;
374                 //printk("LPC int %d\n", irq);
375         }
376         else {
377                 return;
378         }
379         do_IRQ(irq, regs);
380 }
381
382 void show_pending_irqs(void)
383 {
384         fputs("intstatus:  ");
385         put32(it8172_hw0_icregs->intstatus);
386         puts("");
387
388         fputs("pci_req:  ");
389         put32(it8172_hw0_icregs->pci_req);
390         puts("");
391
392         fputs("lb_req:  ");
393         put32(it8172_hw0_icregs->lb_req);
394         puts("");
395
396         fputs("lpc_req:  ");
397         put32(it8172_hw0_icregs->lpc_req);
398         puts("");
399 }