This commit was generated by cvs2svn to compensate for changes in r517,
[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 #include <linux/bitops.h>
51
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 /* revisit */
69 #define EXT_IRQ0_TO_IP 2 /* IP 2 */
70 #define EXT_IRQ5_TO_IP 7 /* IP 7 */
71
72 #define ALLINTS_NOTIMER (IE_IRQ0 | IE_IRQ1 | IE_IRQ2 | IE_IRQ3 | IE_IRQ4)
73
74 void disable_it8172_irq(unsigned int irq_nr);
75 void enable_it8172_irq(unsigned int irq_nr);
76
77 extern void set_debug_traps(void);
78 extern void mips_timer_interrupt(int irq, struct pt_regs *regs);
79 extern asmlinkage void it8172_IRQ(void);
80
81 struct it8172_intc_regs volatile *it8172_hw0_icregs =
82         (struct it8172_intc_regs volatile *)(KSEG1ADDR(IT8172_PCI_IO_BASE + IT_INTC_BASE));
83
84 /* Function for careful CP0 interrupt mask access */
85 static inline void modify_cp0_intmask(unsigned clr_mask, unsigned set_mask)
86 {
87         unsigned long status = read_c0_status();
88         status &= ~((clr_mask & 0xFF) << 8);
89         status |=   (set_mask & 0xFF) << 8;
90         write_c0_status(status);
91 }
92
93 static inline void mask_irq(unsigned int irq_nr)
94 {
95         modify_cp0_intmask(irq_nr, 0);
96 }
97
98 static inline void unmask_irq(unsigned int irq_nr)
99 {
100         modify_cp0_intmask(0, irq_nr);
101 }
102
103 void local_disable_irq(unsigned int irq_nr)
104 {
105         unsigned long flags;
106
107         local_irq_save(flags);
108         disable_it8172_irq(irq_nr);
109         local_irq_restore(flags);
110 }
111
112 void local_enable_irq(unsigned int irq_nr)
113 {
114         unsigned long flags;
115
116         local_irq_save(flags);
117         enable_it8172_irq(irq_nr);
118         local_irq_restore(flags);
119 }
120
121
122 void disable_it8172_irq(unsigned int irq_nr)
123 {
124         DPRINTK("disable_it8172_irq %d\n", irq_nr);
125
126         if ( (irq_nr >= IT8172_LPC_IRQ_BASE) && (irq_nr <= IT8172_SERIRQ_15)) {
127                 /* LPC interrupt */
128                 DPRINTK("DB lpc_mask  %x\n", it8172_hw0_icregs->lpc_mask);
129                 it8172_hw0_icregs->lpc_mask |=
130                         (1 << (irq_nr - IT8172_LPC_IRQ_BASE));
131                 DPRINTK("DA lpc_mask  %x\n", it8172_hw0_icregs->lpc_mask);
132         }
133         else if ( (irq_nr >= IT8172_LB_IRQ_BASE) && (irq_nr <= IT8172_IOCHK_IRQ)) {
134                 /* Local Bus interrupt */
135                 DPRINTK("DB lb_mask  %x\n", it8172_hw0_icregs->lb_mask);
136                 it8172_hw0_icregs->lb_mask |=
137                         (1 << (irq_nr - IT8172_LB_IRQ_BASE));
138                 DPRINTK("DA lb_mask  %x\n", it8172_hw0_icregs->lb_mask);
139         }
140         else if ( (irq_nr >= IT8172_PCI_DEV_IRQ_BASE) && (irq_nr <= IT8172_DMA_IRQ)) {
141                 /* PCI and other interrupts */
142                 DPRINTK("DB pci_mask  %x\n", it8172_hw0_icregs->pci_mask);
143                 it8172_hw0_icregs->pci_mask |=
144                         (1 << (irq_nr - IT8172_PCI_DEV_IRQ_BASE));
145                 DPRINTK("DA pci_mask  %x\n", it8172_hw0_icregs->pci_mask);
146         }
147         else if ( (irq_nr >= IT8172_NMI_IRQ_BASE) && (irq_nr <= IT8172_POWER_NMI_IRQ)) {
148                 /* NMI interrupts */
149                 DPRINTK("DB nmi_mask  %x\n", it8172_hw0_icregs->nmi_mask);
150                 it8172_hw0_icregs->nmi_mask |=
151                         (1 << (irq_nr - IT8172_NMI_IRQ_BASE));
152                 DPRINTK("DA nmi_mask  %x\n", it8172_hw0_icregs->nmi_mask);
153         }
154         else {
155                 panic("disable_it8172_irq: bad irq %d", irq_nr);
156         }
157 }
158
159 void enable_it8172_irq(unsigned int irq_nr)
160 {
161         DPRINTK("enable_it8172_irq %d\n", irq_nr);
162         if ( (irq_nr >= IT8172_LPC_IRQ_BASE) && (irq_nr <= IT8172_SERIRQ_15)) {
163                 /* LPC interrupt */
164                 DPRINTK("EB before lpc_mask  %x\n", it8172_hw0_icregs->lpc_mask);
165                 it8172_hw0_icregs->lpc_mask &=
166                         ~(1 << (irq_nr - IT8172_LPC_IRQ_BASE));
167                 DPRINTK("EA after lpc_mask  %x\n", it8172_hw0_icregs->lpc_mask);
168         }
169         else if ( (irq_nr >= IT8172_LB_IRQ_BASE) && (irq_nr <= IT8172_IOCHK_IRQ)) {
170                 /* Local Bus interrupt */
171                 DPRINTK("EB lb_mask  %x\n", it8172_hw0_icregs->lb_mask);
172                 it8172_hw0_icregs->lb_mask &=
173                         ~(1 << (irq_nr - IT8172_LB_IRQ_BASE));
174                 DPRINTK("EA lb_mask  %x\n", it8172_hw0_icregs->lb_mask);
175         }
176         else if ( (irq_nr >= IT8172_PCI_DEV_IRQ_BASE) && (irq_nr <= IT8172_DMA_IRQ)) {
177                 /* PCI and other interrupts */
178                 DPRINTK("EB pci_mask  %x\n", it8172_hw0_icregs->pci_mask);
179                 it8172_hw0_icregs->pci_mask &=
180                         ~(1 << (irq_nr - IT8172_PCI_DEV_IRQ_BASE));
181                 DPRINTK("EA pci_mask  %x\n", it8172_hw0_icregs->pci_mask);
182         }
183         else if ( (irq_nr >= IT8172_NMI_IRQ_BASE) && (irq_nr <= IT8172_POWER_NMI_IRQ)) {
184                 /* NMI interrupts */
185                 DPRINTK("EB nmi_mask  %x\n", it8172_hw0_icregs->nmi_mask);
186                 it8172_hw0_icregs->nmi_mask &=
187                         ~(1 << (irq_nr - IT8172_NMI_IRQ_BASE));
188                 DPRINTK("EA nmi_mask  %x\n", it8172_hw0_icregs->nmi_mask);
189         }
190         else {
191                 panic("enable_it8172_irq: bad irq %d", irq_nr);
192         }
193 }
194
195 static unsigned int startup_ite_irq(unsigned int irq)
196 {
197         enable_it8172_irq(irq);
198         return 0;
199 }
200
201 #define shutdown_ite_irq        disable_it8172_irq
202 #define mask_and_ack_ite_irq    disable_it8172_irq
203
204 static void end_ite_irq(unsigned int irq)
205 {
206         if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
207                 enable_it8172_irq(irq);
208 }
209
210 static struct hw_interrupt_type it8172_irq_type = {
211         "ITE8172",
212         startup_ite_irq,
213         shutdown_ite_irq,
214         enable_it8172_irq,
215         disable_it8172_irq,
216         mask_and_ack_ite_irq,
217         end_ite_irq,
218         NULL
219 };
220
221
222 static void enable_none(unsigned int irq) { }
223 static unsigned int startup_none(unsigned int irq) { return 0; }
224 static void disable_none(unsigned int irq) { }
225 static void ack_none(unsigned int irq) { }
226
227 /* startup is the same as "enable", shutdown is same as "disable" */
228 #define shutdown_none   disable_none
229 #define end_none        enable_none
230
231 static struct hw_interrupt_type cp0_irq_type = {
232         "CP0 Count",
233         startup_none,
234         shutdown_none,
235         enable_none,
236         disable_none,
237         ack_none,
238         end_none
239 };
240
241 void enable_cpu_timer(void)
242 {
243         unsigned long flags;
244
245         local_irq_save(flags);
246         unmask_irq(1<<EXT_IRQ5_TO_IP); /* timer interrupt */
247         local_irq_restore(flags);
248 }
249
250 void __init arch_init_irq(void)
251 {
252         int i;
253         unsigned long flags;
254
255         memset(irq_desc, 0, sizeof(irq_desc));
256         set_except_vector(0, it8172_IRQ);
257
258         /* mask all interrupts */
259         it8172_hw0_icregs->lb_mask  = 0xffff;
260         it8172_hw0_icregs->lpc_mask = 0xffff;
261         it8172_hw0_icregs->pci_mask = 0xffff;
262         it8172_hw0_icregs->nmi_mask = 0xffff;
263
264         /* make all interrupts level triggered */
265         it8172_hw0_icregs->lb_trigger  = 0;
266         it8172_hw0_icregs->lpc_trigger = 0;
267         it8172_hw0_icregs->pci_trigger = 0;
268         it8172_hw0_icregs->nmi_trigger = 0;
269
270         /* active level setting */
271         /* uart, keyboard, and mouse are active high */
272         it8172_hw0_icregs->lpc_level = (0x10 | 0x2 | 0x1000);
273         it8172_hw0_icregs->lb_level |= 0x20;
274
275         /* keyboard and mouse are edge triggered */
276         it8172_hw0_icregs->lpc_trigger |= (0x2 | 0x1000);
277
278
279 #if 0
280         // Enable this piece of code to make internal USB interrupt
281         // edge triggered.
282         it8172_hw0_icregs->pci_trigger |=
283                 (1 << (IT8172_USB_IRQ - IT8172_PCI_DEV_IRQ_BASE));
284         it8172_hw0_icregs->pci_level &=
285                 ~(1 << (IT8172_USB_IRQ - IT8172_PCI_DEV_IRQ_BASE));
286 #endif
287
288         for (i = 0; i <= IT8172_LAST_IRQ; i++) {
289                 irq_desc[i].handler = &it8172_irq_type;
290                 spin_lock_init(&irq_desc[i].lock);
291         }
292         irq_desc[MIPS_CPU_TIMER_IRQ].handler = &cp0_irq_type;
293         set_c0_status(ALLINTS_NOTIMER);
294 }
295
296 void mips_spurious_interrupt(struct pt_regs *regs)
297 {
298 #if 1
299         return;
300 #else
301         unsigned long status, cause;
302
303         printk("got spurious interrupt\n");
304         status = read_c0_status();
305         cause = read_c0_cause();
306         printk("status %x cause %x\n", status, cause);
307         printk("epc %x badvaddr %x \n", regs->cp0_epc, regs->cp0_badvaddr);
308 //      while(1);
309 #endif
310 }
311
312 void it8172_hw0_irqdispatch(struct pt_regs *regs)
313 {
314         int irq;
315         unsigned short intstatus = 0, status = 0;
316
317         intstatus = it8172_hw0_icregs->intstatus;
318         if (intstatus & 0x8) {
319                 panic("Got NMI interrupt");
320         } else if (intstatus & 0x4) {
321                 /* PCI interrupt */
322                 irq = 0;
323                 status |= it8172_hw0_icregs->pci_req;
324                 while (!(status & 0x1)) {
325                         irq++;
326                         status >>= 1;
327                 }
328                 irq += IT8172_PCI_DEV_IRQ_BASE;
329                 //printk("pci int %d\n", irq);
330         }
331         else if (intstatus & 0x1) {
332                 /* Local Bus interrupt */
333                 irq = 0;
334                 status |= it8172_hw0_icregs->lb_req;
335                 while (!(status & 0x1)) {
336                         irq++;
337                         status >>= 1;
338                 }
339                 irq += IT8172_LB_IRQ_BASE;
340                 //printk("lb int %d\n", irq);
341         }
342         else if (intstatus & 0x2) {
343                 /* LPC interrupt */
344                 /* Since some lpc interrupts are edge triggered,
345                  * we could lose an interrupt this way because
346                  * we acknowledge all ints at onces. Revisit.
347                  */
348                 status |= it8172_hw0_icregs->lpc_req;
349                 it8172_hw0_icregs->lpc_req = 0; /* acknowledge ints */
350                 irq = 0;
351                 while (!(status & 0x1)) {
352                         irq++;
353                         status >>= 1;
354                 }
355                 irq += IT8172_LPC_IRQ_BASE;
356                 //printk("LPC int %d\n", irq);
357         } else
358                 return;
359
360         do_IRQ(irq, regs);
361 }
362
363 void show_pending_irqs(void)
364 {
365         fputs("intstatus:  ");
366         put32(it8172_hw0_icregs->intstatus);
367         puts("");
368
369         fputs("pci_req:  ");
370         put32(it8172_hw0_icregs->pci_req);
371         puts("");
372
373         fputs("lb_req:  ");
374         put32(it8172_hw0_icregs->lb_req);
375         puts("");
376
377         fputs("lpc_req:  ");
378         put32(it8172_hw0_icregs->lpc_req);
379         puts("");
380 }