blob: 722174481467e96b31dd314a226f27b78bc70f54 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2001 MontaVista Software Inc.
3 * Author: MontaVista Software, Inc.
4 * ahennessy@mvista.com
5 *
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
8 * for more details.
9 *
10 * Copyright (C) 2000-2001 Toshiba Corporation
11 *
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.
16 *
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.
27 *
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.
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/init.h>
33
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>
48
49#include <asm/io.h>
50#include <asm/mipsregs.h>
51#include <asm/system.h>
52
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>
58
59#if JMR3927_IRQ_END > NR_IRQS
60#error JMR3927_IRQ_END > NR_IRQS
61#endif
62
63struct tb_irq_space* tb_irq_spaces;
64
65static int jmr3927_irq_base = -1;
66
67#ifdef CONFIG_PCI
68static int jmr3927_gen_iack(void)
69{
70 /* generate ACK cycle */
71#ifdef __BIG_ENDIAN
72 return (tx3927_pcicptr->iiadp >> 24) & 0xff;
73#else
74 return tx3927_pcicptr->iiadp & 0xff;
75#endif
76}
77#endif
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define irc_dlevel 0
80#define irc_elevel 1
81
82static unsigned char irc_level[TX3927_NUM_IR] = {
83 5, 5, 5, 5, 5, 5, /* INT[5:0] */
84 7, 7, /* SIO */
85 5, 5, 5, 0, 0, /* DMA, PIO, PCI */
86 6, 6, 6 /* TMR */
87};
88
89static void jmr3927_irq_disable(unsigned int irq_nr);
90static void jmr3927_irq_enable(unsigned int irq_nr);
91
92static DEFINE_SPINLOCK(jmr3927_irq_lock);
93
94static unsigned int jmr3927_irq_startup(unsigned int irq)
95{
96 jmr3927_irq_enable(irq);
97
98 return 0;
99}
100
101#define jmr3927_irq_shutdown jmr3927_irq_disable
102
103static void jmr3927_irq_ack(unsigned int irq)
104{
105 if (irq == JMR3927_IRQ_IRC_TMR0)
106 jmr3927_tmrptr->tisr = 0; /* ack interrupt */
107
108 jmr3927_irq_disable(irq);
109}
110
111static void jmr3927_irq_end(unsigned int irq)
112{
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300113 if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
114 jmr3927_irq_enable(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static void jmr3927_irq_disable(unsigned int irq_nr)
118{
119 struct tb_irq_space* sp;
120 unsigned long flags;
121
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300122 spin_lock_irqsave(&jmr3927_irq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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) {
126 if (sp->mask_func)
127 sp->mask_func(irq_nr - sp->start_irqno,
128 sp->space_id);
129 break;
130 }
131 }
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300132 spin_unlock_irqrestore(&jmr3927_irq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135static void jmr3927_irq_enable(unsigned int irq_nr)
136{
137 struct tb_irq_space* sp;
138 unsigned long flags;
139
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300140 spin_lock_irqsave(&jmr3927_irq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 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) {
144 if (sp->unmask_func)
145 sp->unmask_func(irq_nr - sp->start_irqno,
146 sp->space_id);
147 break;
148 }
149 }
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300150 spin_unlock_irqrestore(&jmr3927_irq_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153/*
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.
156 */
157static void mask_irq_isac(int irq_nr, int space_id)
158{
159 /* 0: mask */
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);
166}
167static void unmask_irq_isac(int irq_nr, int space_id)
168{
169 /* 0: mask */
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);
175}
176
177static void mask_irq_ioc(int irq_nr, int space_id)
178{
179 /* 0: mask */
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);
185}
186static void unmask_irq_ioc(int irq_nr, int space_id)
187{
188 /* 0: mask */
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);
194}
195
196static void mask_irq_irc(int irq_nr, int space_id)
197{
198 volatile unsigned long *ilrp = &tx3927_ircptr->ilr[irq_nr / 2];
199 if (irq_nr & 1)
200 *ilrp = (*ilrp & 0x00ff) | (irc_dlevel << 8);
201 else
202 *ilrp = (*ilrp & 0xff00) | irc_dlevel;
203 /* update IRCSR */
204 tx3927_ircptr->imr = 0;
205 tx3927_ircptr->imr = irc_elevel;
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300206 /* flush write buffer */
207 (void)tx3927_ircptr->ssr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static void unmask_irq_irc(int irq_nr, int space_id)
211{
212 volatile unsigned long *ilrp = &tx3927_ircptr->ilr[irq_nr / 2];
213 if (irq_nr & 1)
214 *ilrp = (*ilrp & 0x00ff) | (irc_level[irq_nr] << 8);
215 else
216 *ilrp = (*ilrp & 0xff00) | irc_level[irq_nr];
217 /* update IRCSR */
218 tx3927_ircptr->imr = 0;
219 tx3927_ircptr->imr = irc_elevel;
220}
221
222struct tb_irq_space jmr3927_isac_irqspace = {
223 .next = NULL,
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,
228 .name = "ISAC",
229 .space_id = 0,
230 can_share : 0
231};
232struct tb_irq_space jmr3927_ioc_irqspace = {
233 .next = NULL,
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,
238 .name = "IOC",
239 .space_id = 0,
240 can_share : 1
241};
242struct tb_irq_space jmr3927_irc_irqspace = {
243 .next = NULL,
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,
248 .name = "on-chip",
249 .space_id = 0,
250 can_share : 0
251};
252
253void jmr3927_spurious(struct pt_regs *regs)
254{
255#ifdef CONFIG_TX_BRANCH_LIKELY_BUG_WORKAROUND
256 tx_branch_likely_bug_fixup(regs);
257#endif
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]);
260}
261
Ralf Baechlee4ac58a2006-04-03 17:56:36 +0100262asmlinkage void plat_irq_dispatch(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int irq;
265
266#ifdef CONFIG_TX_BRANCH_LIKELY_BUG_WORKAROUND
267 tx_branch_likely_bug_fixup(regs);
268#endif
269 if ((regs->cp0_cause & CAUSEF_IP7) == 0) {
270#if 0
271 jmr3927_spurious(regs);
272#endif
273 return;
274 }
275 irq = (regs->cp0_cause >> CAUSEB_IP2) & 0x0f;
276
277 do_IRQ(irq + JMR3927_IRQ_IRC, regs);
278}
279
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300280static irqreturn_t jmr3927_ioc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 unsigned char istat = jmr3927_ioc_reg_in(JMR3927_IOC_INTS2_ADDR);
283 int i;
284
285 for (i = 0; i < JMR3927_NR_IRQ_IOC; i++) {
286 if (istat & (1 << i)) {
287 irq = JMR3927_IRQ_IOC + i;
288 do_IRQ(irq, regs);
289 }
290 }
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300291 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294static struct irqaction ioc_action = {
295 jmr3927_ioc_interrupt, 0, CPU_MASK_NONE, "IOC", NULL, NULL,
296};
297
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300298static irqreturn_t jmr3927_isac_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned char istat = jmr3927_isac_reg_in(JMR3927_ISAC_INTS2_ADDR);
301 int i;
302
303 for (i = 0; i < JMR3927_NR_IRQ_ISAC; i++) {
304 if (istat & (1 << i)) {
305 irq = JMR3927_IRQ_ISAC + i;
306 do_IRQ(irq, regs);
307 }
308 }
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300309 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312static struct irqaction isac_action = {
313 jmr3927_isac_interrupt, 0, CPU_MASK_NONE, "ISAC", NULL, NULL,
314};
315
316
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300317static irqreturn_t jmr3927_isaerr_interrupt(int irq, void * dev_id, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 printk(KERN_WARNING "ISA error interrupt (irq 0x%x).\n", irq);
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300320
321 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323static struct irqaction isaerr_action = {
324 jmr3927_isaerr_interrupt, 0, CPU_MASK_NONE, "ISA error", NULL, NULL,
325};
326
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300327static irqreturn_t jmr3927_pcierr_interrupt(int irq, void * dev_id, struct pt_regs * regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
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);
Sergei Shtylylov702a96a2005-11-18 22:20:31 +0300332
333 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335static struct irqaction pcierr_action = {
336 jmr3927_pcierr_interrupt, 0, CPU_MASK_NONE, "PCI error", NULL, NULL,
337};
338
339int jmr3927_ether1_irq = 0;
340
341void jmr3927_irq_init(u32 irq_base);
342
343void __init arch_init_irq(void)
344{
345 /* look for io board's presence */
346 int have_isac = jmr3927_have_isac();
347
348 /* Now, interrupt control disabled, */
349 /* all IRC interrupts are masked, */
350 /* all IRC interrupt mode are Low Active. */
351
352 if (have_isac) {
353
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;
363#endif
364 /* disable interrupt control */
365 tx3927_ircptr->cer = 0;
366
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);
372 }
373 }
374
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);
379
380 if (have_isac) {
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);
385 }
386
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);
391
392 /* enable interrupt control */
393 tx3927_ircptr->cer = TX3927_IRCER_ICE;
394 tx3927_ircptr->imr = irc_elevel;
395
396 jmr3927_irq_init(NR_ISA_IRQS);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* 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);
402
403 /* setup IOC interrupt 1 (PCI, MODEM) */
404 setup_irq(JMR3927_IRQ_IOCINT, &ioc_action);
405
406 if (have_isac) {
407 setup_irq(JMR3927_IRQ_ISACINT, &isac_action);
408 setup_irq(JMR3927_IRQ_ISAC_ISAER, &isaerr_action);
409 }
410
411#ifdef CONFIG_PCI
412 setup_irq(JMR3927_IRQ_IRC_PCI, &pcierr_action);
413#endif
414
415 /* enable all CPU interrupt bits. */
416 set_c0_status(ST0_IM); /* IE bit is still 0. */
417}
418
Ralf Baechle94dee172006-07-02 14:41:42 +0100419static struct irq_chip jmr3927_irq_controller = {
Ralf Baechle8ab00b92005-02-28 13:39:57 +0000420 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
429void jmr3927_irq_init(u32 irq_base)
430{
431 u32 i;
432
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;
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700437 irq_desc[i].chip = &jmr3927_irq_controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 jmr3927_irq_base = irq_base;
441}
442
443#ifdef CONFIG_TX_BRANCH_LIKELY_BUG_WORKAROUND
444static int tx_branch_likely_bug_count = 0;
445static int have_tx_branch_likely_bug = 0;
446void tx_branch_likely_bug_fixup(struct pt_regs *regs)
447{
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)
452 return;
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 */
459 /* bczfl, bcztl */
460 if ((insn & 0xf0000000) == 0x50000000 ||
461 (insn & 0xfc0e0000) == 0x04020000 ||
462 (insn & 0xf3fe0000) == 0x41020000) {
463 regs->cp0_epc -= 4;
464 tx_branch_likely_bug_count++;
465 printk(KERN_INFO
466 "fix branch-likery bug in %s (insn %08x)\n",
467 current->comm, insn);
468 }
469 }
470}
471#endif