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