2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
6 * Scott McNutt <smcnutt@psyent.com>
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <asm/types.h>
32 #include <asm/ptrace.h>
36 #ifdef CONFIG_STATUS_LED
37 #include <status_led.h>
40 #if defined(CFG_NIOS_TMRBASE) && !defined(CFG_NIOS_TMRIRQ)
41 #error CFG_NIOS_TMRIRQ not defined (see documentation)
44 /****************************************************************************/
47 interrupt_handler_t *handler;
52 static struct irq_action vecs[32];
54 /*************************************************************************/
55 volatile ulong timestamp = 0;
57 void reset_timer (void)
62 ulong get_timer (ulong base)
65 return (timestamp - base);
68 void set_timer (ulong t)
74 /* The board must handle this interrupt if a timer is not
77 #if defined(CFG_NIOS_TMRBASE)
78 void tmr_isr (void *arg)
80 nios_timer_t *tmr = (nios_timer_t *)arg;
81 /* Interrupt is cleared by writing anything to the
84 writel (&tmr->status, 0);
85 timestamp += CFG_NIOS_TMRMS;
86 #ifdef CONFIG_STATUS_LED
87 status_led_tick(timestamp);
91 static void tmr_init (void)
93 nios_timer_t *tmr =(nios_timer_t *)CFG_NIOS_TMRBASE;
95 writel (&tmr->status, 0);
96 writel (&tmr->control, 0);
97 writel (&tmr->control, NIOS_TIMER_STOP);
99 #if defined(CFG_NIOS_TMRCNT)
100 writel (&tmr->periodl, CFG_NIOS_TMRCNT & 0xffff);
101 writel (&tmr->periodh, (CFG_NIOS_TMRCNT >> 16) & 0xffff);
103 writel (&tmr->control, NIOS_TIMER_ITO | NIOS_TIMER_CONT |
105 irq_install_handler (CFG_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
108 #endif /* CFG_NIOS_TMRBASE */
110 /*************************************************************************/
111 int disable_interrupts (void)
113 int val = rdctl (CTL_STATUS);
114 wrctl (CTL_STATUS, val & ~STATUS_IE);
115 return (val & STATUS_IE);
118 void enable_interrupts( void )
120 int val = rdctl (CTL_STATUS);
121 wrctl (CTL_STATUS, val | STATUS_IE);
124 void external_interrupt (struct pt_regs *regs)
127 struct irq_action *act;
129 /* Evaluate only irqs that are both enabled AND pending */
130 irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
133 /* Assume (as does the Nios2 HAL) that bit 0 is highest
134 * priority. NOTE: There is ALWAYS a handler assigned
135 * (the default if no other).
139 act->handler (act->arg);
147 static void def_hdlr (void *arg)
149 unsigned irqs = rdctl (CTL_IENABLE);
151 /* Disable the individual interrupt -- with gratuitous
154 irqs &= ~(1 << (int)arg);
155 wrctl (CTL_IENABLE, irqs);
156 printf ("WARNING: Disabling unhandled interrupt: %d\n",
160 /*************************************************************************/
161 void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
165 struct irq_action *act;
166 unsigned ena = rdctl (CTL_IENABLE);
168 if ((irq < 0) || (irq > 31))
172 flag = disable_interrupts ();
176 ena |= (1 << irq); /* enable */
178 act->handler = def_hdlr;
179 act->arg = (void *)irq;
180 ena &= ~(1 << irq); /* disable */
182 wrctl (CTL_IENABLE, ena);
183 if (flag) enable_interrupts ();
187 int interrupt_init (void)
191 /* Assign the default handler to all */
192 for (i = 0; i < 32; i++) {
193 vecs[i].handler = def_hdlr;
194 vecs[i].arg = (void *)i;
198 #if defined(CFG_NIOS_TMRBASE)
202 enable_interrupts ();
207 /*************************************************************************/
208 #if defined(CONFIG_CMD_IRQ)
209 int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
212 struct irq_action *act = vecs;
214 printf ("\nInterrupt-Information:\n\n");
215 printf ("Nr Routine Arg Count\n");
216 printf ("-----------------------------\n");
218 for (i=0; i<32; i++) {
219 if (act->handler != def_hdlr) {
220 printf ("%02d %08lx %08lx %d\n",