* Patch by Scott McNutt, 04 Oct 2003:
[platform/kernel/u-boot.git] / cpu / nios / interrupts.c
1 /*
2  * (C) Copyright 2000-2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2003, Psyent Corporation <www.psyent.com>
6  * Scott McNutt <smcnutt@psyent.com>
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
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.
15  *
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.
20  *
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
25  */
26
27
28 #include <nios.h>
29 #include <nios-io.h>
30 #include <asm/ptrace.h>
31 #include <common.h>
32 #include <command.h>
33
34 /****************************************************************************/
35
36 struct  irq_action {
37         interrupt_handler_t *handler;
38         void *arg;
39         int count;
40 };
41
42 static struct irq_action irq_vecs[64];
43
44 /*************************************************************************/
45 volatile ulong timestamp = 0;
46
47 void reset_timer (void)
48 {
49         timestamp = 0;
50 }
51
52 ulong get_timer (ulong base)
53 {
54         return (timestamp - base);
55 }
56
57 void set_timer (ulong t)
58 {
59         timestamp = t;
60 }
61
62
63 /* The board must handle this interrupt if a timer is not
64  * provided.
65  */
66 #if defined(CFG_NIOS_TMRBASE)
67 void timer_interrupt (struct pt_regs *regs)
68 {
69         /* Interrupt is cleared by writing anything to the
70          * status register.
71          */
72         nios_timer_t *tmr = (nios_timer_t *)CFG_NIOS_TMRBASE;
73         tmr->status = 0;
74         timestamp += CFG_NIOS_TMRMS;
75 }
76 #endif
77
78 /*************************************************************************/
79 int disable_interrupts (void)
80 {
81         int val = 0;
82
83         /* Writing anything to CLR_IE disables interrupts */
84         val = rdctl (CTL_STATUS);
85         wrctl (CTL_CLR_IE, 0);
86         return (val & STATUS_IE);
87 }
88
89 void enable_interrupts( void )
90 {
91         /* Writing anything SET_IE enables interrupts */
92         wrctl (CTL_SET_IE, 0);
93 }
94
95 void external_interrupt (struct pt_regs *regs)
96 {
97         unsigned vec;
98
99         vec = (regs->status & STATUS_IPRI) >> 9;        /* ipri */
100
101         irq_vecs[vec].count++;
102         if (irq_vecs[vec].handler != NULL) {
103                 (*irq_vecs[vec].handler)(irq_vecs[vec].arg);
104         } else {
105                 /* A sad side-effect of masking a bogus interrupt is
106                  * that lower priority interrupts will also be disabled.
107                  * This is probably not what we want ... so hang insted.
108                  */
109                 printf ("Unhandled interrupt: 0x%x\n", vec);
110                 disable_interrupts ();
111                 hang ();
112         }
113 }
114
115 /*************************************************************************/
116 int interrupt_init (void)
117 {
118         int vec;
119
120 #if defined(CFG_NIOS_TMRBASE)
121         nios_timer_t *tmr = (nios_timer_t *)CFG_NIOS_TMRBASE;
122
123         tmr->control &= ~NIOS_TIMER_ITO;
124         tmr->control |= NIOS_TIMER_STOP;
125 #endif
126
127         for (vec=0; vec<64; vec++ ) {
128                 irq_vecs[vec].handler = NULL;
129                 irq_vecs[vec].arg = NULL;
130                 irq_vecs[vec].count = 0;
131         }
132
133         /* Need timus interruptus -- start the lopri timer */
134 #if defined(CFG_NIOS_TMRBASE)
135         tmr->control |= ( NIOS_TIMER_ITO |
136                           NIOS_TIMER_CONT |
137                           NIOS_TIMER_START );
138         ipri (CFG_NIOS_TMRIRQ + 1);
139 #endif
140         enable_interrupts ();
141         return (0);
142 }
143
144 void irq_install_handler (int vec, interrupt_handler_t *handler, void *arg)
145 {
146         struct irq_action *irqa = irq_vecs;
147         int   i = vec;
148         int flag;
149
150         if (irqa[i].handler != NULL) {
151                 printf ("Interrupt vector %d: handler 0x%x "
152                         "replacing 0x%x\n",
153                         vec, (uint)handler, (uint)irqa[i].handler);
154         }
155
156         flag = disable_interrupts ();
157         irqa[i].handler = handler;
158         irqa[i].arg = arg;
159         if (flag )
160                 enable_interrupts ();
161 }
162
163 /*************************************************************************/
164 #if (CONFIG_COMMANDS & CFG_CMD_IRQ)
165 int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
166 {
167         int vec;
168
169         printf ("\nInterrupt-Information:\n");
170         printf ("Nr  Routine   Arg       CouIt's ok to cnt\n");
171
172         for (vec=0; vec<64; vec++) {
173                 if (irq_vecs[vec].handler != NULL) {
174                         printf ("%02d  %08lx  %08lx  %d\n",
175                                 vec,
176                                 (ulong)irq_vecs[vec].handler<<1,
177                                 (ulong)irq_vecs[vec].arg,
178                                 irq_vecs[vec].count);
179                 }
180         }
181
182         return (0);
183 }
184 #endif  /* CONFIG_COMMANDS & CFG_CMD_IRQ */