add SDHC support in mmc driver
[u-boot-openmoko/mini2440.git] / cpu / nios2 / interrupts.c
blobaeb5b65b330bb6c24ebe88024ed78f8b1e35835b
1 /*
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
9 * project.
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,
24 * MA 02111-1307 USA
28 #include <nios2.h>
29 #include <nios2-io.h>
30 #include <asm/io.h>
31 #include <asm/ptrace.h>
32 #include <common.h>
33 #include <command.h>
34 #include <watchdog.h>
35 #ifdef CONFIG_STATUS_LED
36 #include <status_led.h>
37 #endif
39 #if defined(CFG_NIOS_TMRBASE) && !defined(CFG_NIOS_TMRIRQ)
40 #error CFG_NIOS_TMRIRQ not defined (see documentation)
41 #endif
43 /****************************************************************************/
45 struct irq_action {
46 interrupt_handler_t *handler;
47 void *arg;
48 int count;
51 static struct irq_action vecs[32];
53 /*************************************************************************/
54 volatile ulong timestamp = 0;
56 void reset_timer (void)
58 timestamp = 0;
61 ulong get_timer (ulong base)
63 WATCHDOG_RESET ();
64 return (timestamp - base);
67 void set_timer (ulong t)
69 timestamp = t;
73 /* The board must handle this interrupt if a timer is not
74 * provided.
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
81 * status register.
83 writel (&tmr->status, 0);
84 timestamp += CFG_NIOS_TMRMS;
85 #ifdef CONFIG_STATUS_LED
86 status_led_tick(timestamp);
87 #endif
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);
101 #endif
102 writel (&tmr->control, NIOS_TIMER_ITO | NIOS_TIMER_CONT |
103 NIOS_TIMER_START );
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)
125 unsigned irqs;
126 struct irq_action *act;
128 /* Evaluate only irqs that are both enabled AND pending */
129 irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
130 act = vecs;
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).
136 while (irqs) {
137 if (irqs & 1) {
138 act->handler (act->arg);
139 act->count++;
141 irqs >>=1;
142 act++;
146 static void def_hdlr (void *arg)
148 unsigned irqs = rdctl (CTL_IENABLE);
150 /* Disable the individual interrupt -- with gratuitous
151 * warning.
153 irqs &= ~(1 << (int)arg);
154 wrctl (CTL_IENABLE, irqs);
155 printf ("WARNING: Disabling unhandled interrupt: %d\n",
156 (int)arg);
159 /*************************************************************************/
160 void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
163 int flag;
164 struct irq_action *act;
165 unsigned ena = rdctl (CTL_IENABLE);
167 if ((irq < 0) || (irq > 31))
168 return;
169 act = &vecs[irq];
171 flag = disable_interrupts ();
172 if (hdlr) {
173 act->handler = hdlr;
174 act->arg = arg;
175 ena |= (1 << irq); /* enable */
176 } else {
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)
188 int i;
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;
194 vecs[i].count = 0;
197 #if defined(CFG_NIOS_TMRBASE)
198 tmr_init ();
199 #endif
201 enable_interrupts ();
202 return (0);
206 /*************************************************************************/
207 #if defined(CONFIG_CMD_IRQ)
208 int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
210 int i;
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",
221 (ulong)act->handler,
222 (ulong)act->arg,
223 act->count);
225 act++;
227 printf ("\n");
229 return (0);
231 #endif