1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <nios2.h>
29#include <nios2-io.h>
30#include <asm/types.h>
31#include <asm/io.h>
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
40#if defined(CONFIG_SYS_NIOS_TMRBASE) && !defined(CONFIG_SYS_NIOS_TMRIRQ)
41#error CONFIG_SYS_NIOS_TMRIRQ not defined (see documentation)
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
75
76
77#if defined(CONFIG_SYS_NIOS_TMRBASE)
78void tmr_isr (void *arg)
79{
80 nios_timer_t *tmr = (nios_timer_t *)arg;
81
82
83
84 writel (&tmr->status, 0);
85 timestamp += CONFIG_SYS_NIOS_TMRMS;
86#ifdef CONFIG_STATUS_LED
87 status_led_tick(timestamp);
88#endif
89}
90
91static void tmr_init (void)
92{
93 nios_timer_t *tmr =(nios_timer_t *)CONFIG_SYS_NIOS_TMRBASE;
94
95 writel (&tmr->status, 0);
96 writel (&tmr->control, 0);
97 writel (&tmr->control, NIOS_TIMER_STOP);
98
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);
102#endif
103 writel (&tmr->control, NIOS_TIMER_ITO | NIOS_TIMER_CONT |
104 NIOS_TIMER_START );
105 irq_install_handler (CONFIG_SYS_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
106}
107
108#endif
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
130 irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
131 act = vecs;
132
133
134
135
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
152
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);
177 } else {
178 act->handler = def_hdlr;
179 act->arg = (void *)irq;
180 ena &= ~(1 << irq);
181 }
182 wrctl (CTL_IENABLE, ena);
183 if (flag) enable_interrupts ();
184}
185
186
187int interrupt_init (void)
188{
189 int i;
190
191
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
198#if defined(CONFIG_SYS_NIOS_TMRBASE)
199 tmr_init ();
200#endif
201
202 enable_interrupts ();
203 return (0);
204}
205
206
207
208#if defined(CONFIG_CMD_IRQ)
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}
232#endif
233