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,
31 #include <asm/ptrace.h>
35 #ifdef CONFIG_STATUS_LED
36 #include <status_led.h>
39 #if defined(CFG_NIOS_TMRBASE) && !defined(CFG_NIOS_TMRIRQ)
40 #error CFG_NIOS_TMRIRQ not defined (see documentation)
43 /****************************************************************************/
46 interrupt_handler_t *handler;
51 static struct irq_action vecs[32];
53 /*************************************************************************/
54 volatile ulong timestamp = 0;
56 void reset_timer (void)
61 ulong get_timer (ulong base)
64 return (timestamp - base);
67 void set_timer (ulong t)
73 /* The board must handle this interrupt if a timer is not
76 #if defined(CFG_NIOS_TMRBASE)
77 void tmr_isr (void *arg)
79 nios_timer_t *tmr = (nios_timer_t *)arg;
80 /* Interrupt is cleared by writing anything to the
83 writel (&tmr->status, 0);
84 timestamp += CFG_NIOS_TMRMS;
85 #ifdef CONFIG_STATUS_LED
86 status_led_tick(timestamp);
90 static void tmr_init (void)
92 nios_timer_t *tmr =(nios_timer_t *)CFG_NIOS_TMRBASE;
94 writel (&tmr->status, 0);
95 writel (&tmr->control, 0);
96 writel (&tmr->control, NIOS_TIMER_STOP);
98 #if defined(CFG_NIOS_TMRCNT)
99 writel (&tmr->periodl, CFG_NIOS_TMRCNT & 0xffff);
100 writel (&tmr->periodh, (CFG_NIOS_TMRCNT >> 16) & 0xffff);
102 writel (&tmr->control, NIOS_TIMER_ITO | NIOS_TIMER_CONT |
104 irq_install_handler (CFG_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
107 #endif /* CFG_NIOS_TMRBASE */
109 /*************************************************************************/
110 int disable_interrupts (void)
112 int val = rdctl (CTL_STATUS);
113 wrctl (CTL_STATUS, val & ~STATUS_IE);
114 return (val & STATUS_IE);
117 void enable_interrupts( void )
119 int val = rdctl (CTL_STATUS);
120 wrctl (CTL_STATUS, val | STATUS_IE);
123 void external_interrupt (struct pt_regs *regs)
126 struct irq_action *act;
128 /* Evaluate only irqs that are both enabled AND pending */
129 irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
132 /* Assume (as does the Nios2 HAL) that bit 0 is highest
133 * priority. NOTE: There is ALWAYS a handler assigned
134 * (the default if no other).
138 act->handler (act->arg);
146 static void def_hdlr (void *arg)
148 unsigned irqs = rdctl (CTL_IENABLE);
150 /* Disable the individual interrupt -- with gratuitous
153 irqs &= ~(1 << (int)arg);
154 wrctl (CTL_IENABLE, irqs);
155 printf ("WARNING: Disabling unhandled interrupt: %d\n",
159 /*************************************************************************/
160 void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
164 struct irq_action *act;
165 unsigned ena = rdctl (CTL_IENABLE);
167 if ((irq < 0) || (irq > 31))
171 flag = disable_interrupts ();
175 ena |= (1 << irq); /* enable */
177 act->handler = def_hdlr;
178 act->arg = (void *)irq;
179 ena &= ~(1 << irq); /* disable */
181 wrctl (CTL_IENABLE, ena);
182 if (flag) enable_interrupts ();
186 int interrupt_init (void)
190 /* Assign the default handler to all */
191 for (i = 0; i < 32; i++) {
192 vecs[i].handler = def_hdlr;
193 vecs[i].arg = (void *)i;
197 #if defined(CFG_NIOS_TMRBASE)
201 enable_interrupts ();
206 /*************************************************************************/
207 #if defined(CONFIG_CMD_IRQ)
208 int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
211 struct irq_action *act = vecs;
213 printf ("\nInterrupt-Information:\n\n");
214 printf ("Nr Routine Arg Count\n");
215 printf ("-----------------------------\n");
217 for (i=0; i<32; i++) {
218 if (act->handler != def_hdlr) {
219 printf ("%02d %08lx %08lx %d\n",