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/ptrace.h>
34 #ifdef CONFIG_STATUS_LED
35 #include <status_led.h>
38 #if defined(CFG_NIOS_TMRBASE) && !defined(CFG_NIOS_TMRIRQ)
39 #error CFG_NIOS_TMRIRQ not defined (see documentation)
42 /****************************************************************************/
45 interrupt_handler_t *handler;
50 static struct irq_action vecs[32];
52 /*************************************************************************/
53 volatile ulong timestamp = 0;
55 void reset_timer (void)
60 ulong get_timer (ulong base)
63 return (timestamp - base);
66 void set_timer (ulong t)
72 /* The board must handle this interrupt if a timer is not
75 #if defined(CFG_NIOS_TMRBASE)
76 void tmr_isr (void *arg)
78 nios_timer_t *tmr = (nios_timer_t *)arg;
79 /* Interrupt is cleared by writing anything to the
83 timestamp += CFG_NIOS_TMRMS;
84 #ifdef CONFIG_STATUS_LED
85 status_led_tick(timestamp);
89 static void tmr_init (void)
91 nios_timer_t *tmr =(nios_timer_t *)CACHE_BYPASS(CFG_NIOS_TMRBASE);
93 tmr->control &= ~(NIOS_TIMER_START | NIOS_TIMER_ITO);
94 tmr->control |= NIOS_TIMER_STOP;
95 #if defined(CFG_NIOS_TMRCNT)
96 tmr->periodl = CFG_NIOS_TMRCNT & 0xffff;
97 tmr->periodh = (CFG_NIOS_TMRCNT >> 16) & 0xffff;
99 tmr->control |= ( NIOS_TIMER_ITO |
102 irq_install_handler (CFG_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
105 #endif /* CFG_NIOS_TMRBASE */
107 /*************************************************************************/
108 int disable_interrupts (void)
110 int val = rdctl (CTL_STATUS);
111 wrctl (CTL_STATUS, val & ~STATUS_IE);
112 return (val & STATUS_IE);
115 void enable_interrupts( void )
117 int val = rdctl (CTL_STATUS);
118 wrctl (CTL_STATUS, val | STATUS_IE);
121 void external_interrupt (struct pt_regs *regs)
124 struct irq_action *act;
126 /* Evaluate only irqs that are both enabled AND pending */
127 irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
130 /* Assume (as does the Nios2 HAL) that bit 0 is highest
131 * priority. NOTE: There is ALWAYS a handler assigned
132 * (the default if no other).
136 act->handler (act->arg);
144 static void def_hdlr (void *arg)
146 unsigned irqs = rdctl (CTL_IENABLE);
148 /* Disable the individual interrupt -- with gratuitous
151 irqs &= ~(1 << (int)arg);
152 wrctl (CTL_IENABLE, irqs);
153 printf ("WARNING: Disabling unhandled interrupt: %d\n",
157 /*************************************************************************/
158 void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
162 struct irq_action *act;
163 unsigned ena = rdctl (CTL_IENABLE);
165 if ((irq < 0) || (irq > 31))
169 flag = disable_interrupts ();
173 ena |= (1 << irq); /* enable */
175 act->handler = def_hdlr;
176 act->arg = (void *)irq;
177 ena &= ~(1 << irq); /* disable */
179 wrctl (CTL_IENABLE, ena);
180 if (flag) enable_interrupts ();
184 int interrupt_init (void)
188 /* Assign the default handler to all */
189 for (i = 0; i < 32; i++) {
190 vecs[i].handler = def_hdlr;
191 vecs[i].arg = (void *)i;
195 #if defined(CFG_NIOS_TMRBASE)
199 enable_interrupts ();
204 /*************************************************************************/
205 #if (CONFIG_COMMANDS & CFG_CMD_IRQ)
206 int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
209 struct irq_action *act = vecs;
211 printf ("\nInterrupt-Information:\n\n");
212 printf ("Nr Routine Arg Count\n");
213 printf ("-----------------------------\n");
215 for (i=0; i<32; i++) {
216 if (act->handler != def_hdlr) {
217 printf ("%02d %08lx %08lx %d\n",
229 #endif /* CONFIG_COMMANDS & CFG_CMD_IRQ */