blob: 1c3566ebf95610e2e171d02775a9b2cb0c59f183 [file] [log] [blame]
wdenkef3386f2004-10-10 21:27:30 +00001/*
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>
Stefan Roese37628252008-08-06 14:05:38 +020030#include <asm/types.h>
Scott McNutta5721a32006-06-08 11:59:57 -040031#include <asm/io.h>
wdenkef3386f2004-10-10 21:27:30 +000032#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
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020040#if defined(CONFIG_SYS_NIOS_TMRBASE) && !defined(CONFIG_SYS_NIOS_TMRIRQ)
41#error CONFIG_SYS_NIOS_TMRIRQ not defined (see documentation)
wdenkef3386f2004-10-10 21:27:30 +000042#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 */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020077#if defined(CONFIG_SYS_NIOS_TMRBASE)
wdenkef3386f2004-10-10 21:27:30 +000078void 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 */
Scott McNutta5721a32006-06-08 11:59:57 -040084 writel (&tmr->status, 0);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020085 timestamp += CONFIG_SYS_NIOS_TMRMS;
wdenkef3386f2004-10-10 21:27:30 +000086#ifdef CONFIG_STATUS_LED
87 status_led_tick(timestamp);
88#endif
89}
90
91static void tmr_init (void)
92{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020093 nios_timer_t *tmr =(nios_timer_t *)CONFIG_SYS_NIOS_TMRBASE;
wdenkef3386f2004-10-10 21:27:30 +000094
Scott McNutta5721a32006-06-08 11:59:57 -040095 writel (&tmr->status, 0);
96 writel (&tmr->control, 0);
97 writel (&tmr->control, NIOS_TIMER_STOP);
98
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020099#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);
wdenkef3386f2004-10-10 21:27:30 +0000102#endif
Scott McNutta5721a32006-06-08 11:59:57 -0400103 writel (&tmr->control, NIOS_TIMER_ITO | NIOS_TIMER_CONT |
wdenkef3386f2004-10-10 21:27:30 +0000104 NIOS_TIMER_START );
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200105 irq_install_handler (CONFIG_SYS_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
wdenkef3386f2004-10-10 21:27:30 +0000106}
107
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200108#endif /* CONFIG_SYS_NIOS_TMRBASE */
wdenkef3386f2004-10-10 21:27:30 +0000109
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
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200198#if defined(CONFIG_SYS_NIOS_TMRBASE)
wdenkef3386f2004-10-10 21:27:30 +0000199 tmr_init ();
200#endif
201
202 enable_interrupts ();
203 return (0);
204}
205
206
207/*************************************************************************/
Jon Loeligera5217742007-07-09 18:57:22 -0500208#if defined(CONFIG_CMD_IRQ)
wdenkef3386f2004-10-10 21:27:30 +0000209int 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}
Jon Loeligera5217742007-07-09 18:57:22 -0500232#endif