2 * Copyright 2001 MontaVista Software Inc.
3 * Author: MontaVista Software, Inc.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
10 * Copyright (C) 2000-2001 Toshiba Corporation
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
20 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * You should have received a copy of the GNU General Public License along
29 * with this program; if not, write to the Free Software Foundation, Inc.,
30 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <linux/init.h>
34 #include <linux/errno.h>
35 #include <linux/irq.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 #include <linux/types.h>
40 #include <linux/interrupt.h>
41 #include <linux/ioport.h>
42 #include <linux/timex.h>
43 #include <linux/slab.h>
44 #include <linux/random.h>
45 #include <linux/smp.h>
46 #include <linux/smp_lock.h>
47 #include <linux/bitops.h>
50 #include <asm/mipsregs.h>
51 #include <asm/system.h>
53 #include <asm/ptrace.h>
54 #include <asm/processor.h>
55 #include <asm/jmr3927/irq.h>
56 #include <asm/debug.h>
57 #include <asm/jmr3927/jmr3927.h>
59 #if JMR3927_IRQ_END > NR_IRQS
60 #error JMR3927_IRQ_END > NR_IRQS
63 struct tb_irq_space* tb_irq_spaces;
65 static int jmr3927_irq_base = -1;
68 static int jmr3927_gen_iack(void)
70 /* generate ACK cycle */
72 return (tx3927_pcicptr->iiadp >> 24) & 0xff;
74 return tx3927_pcicptr->iiadp & 0xff;
82 static unsigned char irc_level[TX3927_NUM_IR] = {
83 5, 5, 5, 5, 5, 5, /* INT[5:0] */
85 5, 5, 5, 0, 0, /* DMA, PIO, PCI */
89 static void jmr3927_irq_disable(unsigned int irq_nr);
90 static void jmr3927_irq_enable(unsigned int irq_nr);
92 static DEFINE_SPINLOCK(jmr3927_irq_lock);
94 static unsigned int jmr3927_irq_startup(unsigned int irq)
96 jmr3927_irq_enable(irq);
101 #define jmr3927_irq_shutdown jmr3927_irq_disable
103 static void jmr3927_irq_ack(unsigned int irq)
105 if (irq == JMR3927_IRQ_IRC_TMR0)
106 jmr3927_tmrptr->tisr = 0; /* ack interrupt */
108 jmr3927_irq_disable(irq);
111 static void jmr3927_irq_end(unsigned int irq)
113 if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
114 jmr3927_irq_enable(irq);
117 static void jmr3927_irq_disable(unsigned int irq_nr)
119 struct tb_irq_space* sp;
122 spin_lock_irqsave(&jmr3927_irq_lock, flags);
123 for (sp = tb_irq_spaces; sp; sp = sp->next) {
124 if (sp->start_irqno <= irq_nr &&
125 irq_nr < sp->start_irqno + sp->nr_irqs) {
127 sp->mask_func(irq_nr - sp->start_irqno,
132 spin_unlock_irqrestore(&jmr3927_irq_lock, flags);
135 static void jmr3927_irq_enable(unsigned int irq_nr)
137 struct tb_irq_space* sp;
140 spin_lock_irqsave(&jmr3927_irq_lock, flags);
141 for (sp = tb_irq_spaces; sp; sp = sp->next) {
142 if (sp->start_irqno <= irq_nr &&
143 irq_nr < sp->start_irqno + sp->nr_irqs) {
145 sp->unmask_func(irq_nr - sp->start_irqno,
150 spin_unlock_irqrestore(&jmr3927_irq_lock, flags);
154 * CP0_STATUS is a thread's resource (saved/restored on context switch).
155 * So disable_irq/enable_irq MUST handle IOC/ISAC/IRC registers.
157 static void mask_irq_isac(int irq_nr, int space_id)
160 unsigned char imask =
161 jmr3927_isac_reg_in(JMR3927_ISAC_INTM_ADDR);
162 unsigned int bit = 1 << irq_nr;
163 jmr3927_isac_reg_out(imask & ~bit, JMR3927_ISAC_INTM_ADDR);
164 /* flush write buffer */
165 (void)jmr3927_ioc_reg_in(JMR3927_IOC_REV_ADDR);
167 static void unmask_irq_isac(int irq_nr, int space_id)
170 unsigned char imask = jmr3927_isac_reg_in(JMR3927_ISAC_INTM_ADDR);
171 unsigned int bit = 1 << irq_nr;
172 jmr3927_isac_reg_out(imask | bit, JMR3927_ISAC_INTM_ADDR);
173 /* flush write buffer */
174 (void)jmr3927_ioc_reg_in(JMR3927_IOC_REV_ADDR);
177 static void mask_irq_ioc(int irq_nr, int space_id)
180 unsigned char imask = jmr3927_ioc_reg_in(JMR3927_IOC_INTM_ADDR);
181 unsigned int bit = 1 << irq_nr;
182 jmr3927_ioc_reg_out(imask & ~bit, JMR3927_IOC_INTM_ADDR);
183 /* flush write buffer */
184 (void)jmr3927_ioc_reg_in(JMR3927_IOC_REV_ADDR);
186 static void unmask_irq_ioc(int irq_nr, int space_id)
189 unsigned char imask = jmr3927_ioc_reg_in(JMR3927_IOC_INTM_ADDR);
190 unsigned int bit = 1 << irq_nr;
191 jmr3927_ioc_reg_out(imask | bit, JMR3927_IOC_INTM_ADDR);
192 /* flush write buffer */
193 (void)jmr3927_ioc_reg_in(JMR3927_IOC_REV_ADDR);
196 static void mask_irq_irc(int irq_nr, int space_id)
198 volatile unsigned long *ilrp = &tx3927_ircptr->ilr[irq_nr / 2];
200 *ilrp = (*ilrp & 0x00ff) | (irc_dlevel << 8);
202 *ilrp = (*ilrp & 0xff00) | irc_dlevel;
204 tx3927_ircptr->imr = 0;
205 tx3927_ircptr->imr = irc_elevel;
206 /* flush write buffer */
207 (void)tx3927_ircptr->ssr;
210 static void unmask_irq_irc(int irq_nr, int space_id)
212 volatile unsigned long *ilrp = &tx3927_ircptr->ilr[irq_nr / 2];
214 *ilrp = (*ilrp & 0x00ff) | (irc_level[irq_nr] << 8);
216 *ilrp = (*ilrp & 0xff00) | irc_level[irq_nr];
218 tx3927_ircptr->imr = 0;
219 tx3927_ircptr->imr = irc_elevel;
222 struct tb_irq_space jmr3927_isac_irqspace = {
224 .start_irqno = JMR3927_IRQ_ISAC,
225 nr_irqs : JMR3927_NR_IRQ_ISAC,
226 .mask_func = mask_irq_isac,
227 .unmask_func = unmask_irq_isac,
232 struct tb_irq_space jmr3927_ioc_irqspace = {
234 .start_irqno = JMR3927_IRQ_IOC,
235 nr_irqs : JMR3927_NR_IRQ_IOC,
236 .mask_func = mask_irq_ioc,
237 .unmask_func = unmask_irq_ioc,
242 struct tb_irq_space jmr3927_irc_irqspace = {
244 .start_irqno = JMR3927_IRQ_IRC,
245 nr_irqs : JMR3927_NR_IRQ_IRC,
246 .mask_func = mask_irq_irc,
247 .unmask_func = unmask_irq_irc,
253 void jmr3927_spurious(struct pt_regs *regs)
255 #ifdef CONFIG_TX_BRANCH_LIKELY_BUG_WORKAROUND
256 tx_branch_likely_bug_fixup(regs);
258 printk(KERN_WARNING "spurious interrupt (cause 0x%lx, pc 0x%lx, ra 0x%lx).\n",
259 regs->cp0_cause, regs->cp0_epc, regs->regs[31]);
262 asmlinkage void plat_irq_dispatch(struct pt_regs *regs)
266 #ifdef CONFIG_TX_BRANCH_LIKELY_BUG_WORKAROUND
267 tx_branch_likely_bug_fixup(regs);
269 if ((regs->cp0_cause & CAUSEF_IP7) == 0) {
271 jmr3927_spurious(regs);
275 irq = (regs->cp0_cause >> CAUSEB_IP2) & 0x0f;
277 do_IRQ(irq + JMR3927_IRQ_IRC, regs);
280 static irqreturn_t jmr3927_ioc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
282 unsigned char istat = jmr3927_ioc_reg_in(JMR3927_IOC_INTS2_ADDR);
285 for (i = 0; i < JMR3927_NR_IRQ_IOC; i++) {
286 if (istat & (1 << i)) {
287 irq = JMR3927_IRQ_IOC + i;
294 static struct irqaction ioc_action = {
295 jmr3927_ioc_interrupt, 0, CPU_MASK_NONE, "IOC", NULL, NULL,
298 static irqreturn_t jmr3927_isac_interrupt(int irq, void *dev_id, struct pt_regs *regs)
300 unsigned char istat = jmr3927_isac_reg_in(JMR3927_ISAC_INTS2_ADDR);
303 for (i = 0; i < JMR3927_NR_IRQ_ISAC; i++) {
304 if (istat & (1 << i)) {
305 irq = JMR3927_IRQ_ISAC + i;
312 static struct irqaction isac_action = {
313 jmr3927_isac_interrupt, 0, CPU_MASK_NONE, "ISAC", NULL, NULL,
317 static irqreturn_t jmr3927_isaerr_interrupt(int irq, void * dev_id, struct pt_regs * regs)
319 printk(KERN_WARNING "ISA error interrupt (irq 0x%x).\n", irq);
323 static struct irqaction isaerr_action = {
324 jmr3927_isaerr_interrupt, 0, CPU_MASK_NONE, "ISA error", NULL, NULL,
327 static irqreturn_t jmr3927_pcierr_interrupt(int irq, void * dev_id, struct pt_regs * regs)
329 printk(KERN_WARNING "PCI error interrupt (irq 0x%x).\n", irq);
330 printk(KERN_WARNING "pcistat:%02x, lbstat:%04lx\n",
331 tx3927_pcicptr->pcistat, tx3927_pcicptr->lbstat);
335 static struct irqaction pcierr_action = {
336 jmr3927_pcierr_interrupt, 0, CPU_MASK_NONE, "PCI error", NULL, NULL,
339 int jmr3927_ether1_irq = 0;
341 void jmr3927_irq_init(u32 irq_base);
343 void __init arch_init_irq(void)
345 /* look for io board's presence */
346 int have_isac = jmr3927_have_isac();
348 /* Now, interrupt control disabled, */
349 /* all IRC interrupts are masked, */
350 /* all IRC interrupt mode are Low Active. */
354 /* ETHER1 (NE2000 compatible 10M-Ether) parameter setup */
355 /* temporary enable interrupt control */
356 tx3927_ircptr->cer = 1;
357 /* ETHER1 Int. Is High-Active. */
358 if (tx3927_ircptr->ssr & (1 << 0))
359 jmr3927_ether1_irq = JMR3927_IRQ_IRC_INT0;
360 #if 0 /* INT3 may be asserted by ether0 (even after reboot...) */
361 else if (tx3927_ircptr->ssr & (1 << 3))
362 jmr3927_ether1_irq = JMR3927_IRQ_IRC_INT3;
364 /* disable interrupt control */
365 tx3927_ircptr->cer = 0;
367 /* Ether1: High Active */
368 if (jmr3927_ether1_irq) {
369 int ether1_irc = jmr3927_ether1_irq - JMR3927_IRQ_IRC;
370 tx3927_ircptr->cr[ether1_irc / 8] |=
371 TX3927_IRCR_HIGH << ((ether1_irc % 8) * 2);
375 /* mask all IOC interrupts */
376 jmr3927_ioc_reg_out(0, JMR3927_IOC_INTM_ADDR);
377 /* setup IOC interrupt mode (SOFT:High Active, Others:Low Active) */
378 jmr3927_ioc_reg_out(JMR3927_IOC_INTF_SOFT, JMR3927_IOC_INTP_ADDR);
381 /* mask all ISAC interrupts */
382 jmr3927_isac_reg_out(0, JMR3927_ISAC_INTM_ADDR);
383 /* setup ISAC interrupt mode (ISAIRQ3,ISAIRQ5:Low Active ???) */
384 jmr3927_isac_reg_out(JMR3927_ISAC_INTF_IRQ3|JMR3927_ISAC_INTF_IRQ5, JMR3927_ISAC_INTP_ADDR);
387 /* clear PCI Soft interrupts */
388 jmr3927_ioc_reg_out(0, JMR3927_IOC_INTS1_ADDR);
389 /* clear PCI Reset interrupts */
390 jmr3927_ioc_reg_out(0, JMR3927_IOC_RESET_ADDR);
392 /* enable interrupt control */
393 tx3927_ircptr->cer = TX3927_IRCER_ICE;
394 tx3927_ircptr->imr = irc_elevel;
396 jmr3927_irq_init(NR_ISA_IRQS);
398 /* setup irq space */
399 add_tb_irq_space(&jmr3927_isac_irqspace);
400 add_tb_irq_space(&jmr3927_ioc_irqspace);
401 add_tb_irq_space(&jmr3927_irc_irqspace);
403 /* setup IOC interrupt 1 (PCI, MODEM) */
404 setup_irq(JMR3927_IRQ_IOCINT, &ioc_action);
407 setup_irq(JMR3927_IRQ_ISACINT, &isac_action);
408 setup_irq(JMR3927_IRQ_ISAC_ISAER, &isaerr_action);
412 setup_irq(JMR3927_IRQ_IRC_PCI, &pcierr_action);
415 /* enable all CPU interrupt bits. */
416 set_c0_status(ST0_IM); /* IE bit is still 0. */
419 static hw_irq_controller jmr3927_irq_controller = {
420 .typename = "jmr3927_irq",
421 .startup = jmr3927_irq_startup,
422 .shutdown = jmr3927_irq_shutdown,
423 .enable = jmr3927_irq_enable,
424 .disable = jmr3927_irq_disable,
425 .ack = jmr3927_irq_ack,
426 .end = jmr3927_irq_end,
429 void jmr3927_irq_init(u32 irq_base)
433 for (i= irq_base; i< irq_base + JMR3927_NR_IRQ_IRC + JMR3927_NR_IRQ_IOC; i++) {
434 irq_desc[i].status = IRQ_DISABLED;
435 irq_desc[i].action = NULL;
436 irq_desc[i].depth = 1;
437 irq_desc[i].chip = &jmr3927_irq_controller;
440 jmr3927_irq_base = irq_base;
443 #ifdef CONFIG_TX_BRANCH_LIKELY_BUG_WORKAROUND
444 static int tx_branch_likely_bug_count = 0;
445 static int have_tx_branch_likely_bug = 0;
446 void tx_branch_likely_bug_fixup(struct pt_regs *regs)
448 /* TX39/49-BUG: Under this condition, the insn in delay slot
449 of the branch likely insn is executed (not nullified) even
450 the branch condition is false. */
451 if (!have_tx_branch_likely_bug)
453 if ((regs->cp0_epc & 0xfff) == 0xffc &&
454 KSEGX(regs->cp0_epc) != KSEG0 &&
455 KSEGX(regs->cp0_epc) != KSEG1) {
456 unsigned int insn = *(unsigned int*)(regs->cp0_epc - 4);
457 /* beql,bnel,blezl,bgtzl */
458 /* bltzl,bgezl,blezall,bgezall */
460 if ((insn & 0xf0000000) == 0x50000000 ||
461 (insn & 0xfc0e0000) == 0x04020000 ||
462 (insn & 0xf3fe0000) == 0x41020000) {
464 tx_branch_likely_bug_count++;
466 "fix branch-likery bug in %s (insn %08x)\n",
467 current->comm, insn);