]> git.ipfire.org Git - people/ms/u-boot.git/blame - cpu/nios2/interrupts.c
rename CFG_ macros to CONFIG_SYS
[people/ms/u-boot.git] / cpu / nios2 / interrupts.c
CommitLineData
5c952cf0
WD
1/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2004, 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 <nios2.h>
29#include <nios2-io.h>
f2302d44 30#include <asm/types.h>
c2ced000 31#include <asm/io.h>
5c952cf0
WD
32#include <asm/ptrace.h>
33#include <common.h>
34#include <command.h>
35#include <watchdog.h>
36#ifdef CONFIG_STATUS_LED
37#include <status_led.h>
38#endif
39
6d0f6bcf
JCPV
40#if defined(CONFIG_SYS_NIOS_TMRBASE) && !defined(CONFIG_SYS_NIOS_TMRIRQ)
41#error CONFIG_SYS_NIOS_TMRIRQ not defined (see documentation)
5c952cf0
WD
42#endif
43
44/****************************************************************************/
45
46struct irq_action {
47 interrupt_handler_t *handler;
48 void *arg;
49 int count;
50};
51
52static struct irq_action vecs[32];
53
54/*************************************************************************/
55volatile ulong timestamp = 0;
56
57void reset_timer (void)
58{
59 timestamp = 0;
60}
61
62ulong get_timer (ulong base)
63{
64 WATCHDOG_RESET ();
65 return (timestamp - base);
66}
67
68void set_timer (ulong t)
69{
70 timestamp = t;
71}
72
73
74/* The board must handle this interrupt if a timer is not
75 * provided.
76 */
6d0f6bcf 77#if defined(CONFIG_SYS_NIOS_TMRBASE)
5c952cf0
WD
78void tmr_isr (void *arg)
79{
80 nios_timer_t *tmr = (nios_timer_t *)arg;
81 /* Interrupt is cleared by writing anything to the
82 * status register.
83 */
c2ced000 84 writel (&tmr->status, 0);
6d0f6bcf 85 timestamp += CONFIG_SYS_NIOS_TMRMS;
5c952cf0
WD
86#ifdef CONFIG_STATUS_LED
87 status_led_tick(timestamp);
88#endif
89}
90
91static void tmr_init (void)
92{
6d0f6bcf 93 nios_timer_t *tmr =(nios_timer_t *)CONFIG_SYS_NIOS_TMRBASE;
c2ced000
SM
94
95 writel (&tmr->status, 0);
96 writel (&tmr->control, 0);
97 writel (&tmr->control, NIOS_TIMER_STOP);
5c952cf0 98
6d0f6bcf
JCPV
99#if defined(CONFIG_SYS_NIOS_TMRCNT)
100 writel (&tmr->periodl, CONFIG_SYS_NIOS_TMRCNT & 0xffff);
101 writel (&tmr->periodh, (CONFIG_SYS_NIOS_TMRCNT >> 16) & 0xffff);
5c952cf0 102#endif
c2ced000 103 writel (&tmr->control, NIOS_TIMER_ITO | NIOS_TIMER_CONT |
5c952cf0 104 NIOS_TIMER_START );
6d0f6bcf 105 irq_install_handler (CONFIG_SYS_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
5c952cf0
WD
106}
107
6d0f6bcf 108#endif /* CONFIG_SYS_NIOS_TMRBASE */
5c952cf0
WD
109
110/*************************************************************************/
111int disable_interrupts (void)
112{
113 int val = rdctl (CTL_STATUS);
114 wrctl (CTL_STATUS, val & ~STATUS_IE);
115 return (val & STATUS_IE);
116}
117
118void enable_interrupts( void )
119{
120 int val = rdctl (CTL_STATUS);
121 wrctl (CTL_STATUS, val | STATUS_IE);
122}
123
124void external_interrupt (struct pt_regs *regs)
125{
126 unsigned irqs;
127 struct irq_action *act;
128
129 /* Evaluate only irqs that are both enabled AND pending */
130 irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
131 act = vecs;
132
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).
136 */
137 while (irqs) {
138 if (irqs & 1) {
139 act->handler (act->arg);
140 act->count++;
141 }
142 irqs >>=1;
143 act++;
144 }
145}
146
147static void def_hdlr (void *arg)
148{
149 unsigned irqs = rdctl (CTL_IENABLE);
150
151 /* Disable the individual interrupt -- with gratuitous
152 * warning.
153 */
154 irqs &= ~(1 << (int)arg);
155 wrctl (CTL_IENABLE, irqs);
156 printf ("WARNING: Disabling unhandled interrupt: %d\n",
157 (int)arg);
158}
159
160/*************************************************************************/
161void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
162{
163
164 int flag;
165 struct irq_action *act;
166 unsigned ena = rdctl (CTL_IENABLE);
167
168 if ((irq < 0) || (irq > 31))
169 return;
170 act = &vecs[irq];
171
172 flag = disable_interrupts ();
173 if (hdlr) {
174 act->handler = hdlr;
175 act->arg = arg;
176 ena |= (1 << irq); /* enable */
177 } else {
178 act->handler = def_hdlr;
179 act->arg = (void *)irq;
180 ena &= ~(1 << irq); /* disable */
181 }
182 wrctl (CTL_IENABLE, ena);
183 if (flag) enable_interrupts ();
184}
185
186
187int interrupt_init (void)
188{
189 int i;
190
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;
195 vecs[i].count = 0;
196 }
197
6d0f6bcf 198#if defined(CONFIG_SYS_NIOS_TMRBASE)
5c952cf0
WD
199 tmr_init ();
200#endif
201
202 enable_interrupts ();
203 return (0);
204}
205
206
207/*************************************************************************/
3a1ed1e1 208#if defined(CONFIG_CMD_IRQ)
5c952cf0
WD
209int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
210{
211 int i;
212 struct irq_action *act = vecs;
213
214 printf ("\nInterrupt-Information:\n\n");
215 printf ("Nr Routine Arg Count\n");
216 printf ("-----------------------------\n");
217
218 for (i=0; i<32; i++) {
219 if (act->handler != def_hdlr) {
220 printf ("%02d %08lx %08lx %d\n",
221 i,
222 (ulong)act->handler,
223 (ulong)act->arg,
224 act->count);
225 }
226 act++;
227 }
228 printf ("\n");
229
230 return (0);
231}
3a1ed1e1 232#endif