blob: 1599674353c6b80214260185329c415f8e7f7e6e [file] [log] [blame]
wdenk5c952cf2004-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5c952cf2004-10-10 21:27:30 +00009 */
10
Thomas Chou37e24492015-10-03 21:02:30 +080011#include <common.h>
12#include <command.h>
Thomas Chou57cfeb52014-08-25 17:09:07 +080013#include <asm/nios2.h>
Stefan Roesef2302d42008-08-06 14:05:38 +020014#include <asm/types.h>
Scott McNuttc2ced002006-06-08 11:59:57 -040015#include <asm/io.h>
wdenk5c952cf2004-10-10 21:27:30 +000016#include <asm/ptrace.h>
wdenk5c952cf2004-10-10 21:27:30 +000017
Thomas Choue6500f82015-10-08 21:23:37 +080018/*************************************************************************/
wdenk5c952cf2004-10-10 21:27:30 +000019struct irq_action {
20 interrupt_handler_t *handler;
21 void *arg;
22 int count;
23};
24
25static struct irq_action vecs[32];
26
wdenk5c952cf2004-10-10 21:27:30 +000027int disable_interrupts (void)
28{
29 int val = rdctl (CTL_STATUS);
30 wrctl (CTL_STATUS, val & ~STATUS_IE);
31 return (val & STATUS_IE);
32}
33
34void enable_interrupts( void )
35{
36 int val = rdctl (CTL_STATUS);
37 wrctl (CTL_STATUS, val | STATUS_IE);
38}
39
40void external_interrupt (struct pt_regs *regs)
41{
42 unsigned irqs;
43 struct irq_action *act;
44
45 /* Evaluate only irqs that are both enabled AND pending */
46 irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
47 act = vecs;
48
49 /* Assume (as does the Nios2 HAL) that bit 0 is highest
50 * priority. NOTE: There is ALWAYS a handler assigned
51 * (the default if no other).
52 */
53 while (irqs) {
54 if (irqs & 1) {
55 act->handler (act->arg);
56 act->count++;
57 }
58 irqs >>=1;
59 act++;
60 }
61}
62
63static void def_hdlr (void *arg)
64{
65 unsigned irqs = rdctl (CTL_IENABLE);
66
67 /* Disable the individual interrupt -- with gratuitous
68 * warning.
69 */
70 irqs &= ~(1 << (int)arg);
71 wrctl (CTL_IENABLE, irqs);
72 printf ("WARNING: Disabling unhandled interrupt: %d\n",
73 (int)arg);
74}
75
76/*************************************************************************/
77void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
78{
79
80 int flag;
81 struct irq_action *act;
82 unsigned ena = rdctl (CTL_IENABLE);
83
84 if ((irq < 0) || (irq > 31))
85 return;
86 act = &vecs[irq];
87
88 flag = disable_interrupts ();
89 if (hdlr) {
90 act->handler = hdlr;
91 act->arg = arg;
92 ena |= (1 << irq); /* enable */
93 } else {
94 act->handler = def_hdlr;
95 act->arg = (void *)irq;
96 ena &= ~(1 << irq); /* disable */
97 }
98 wrctl (CTL_IENABLE, ena);
99 if (flag) enable_interrupts ();
100}
101
102
103int interrupt_init (void)
104{
105 int i;
106
107 /* Assign the default handler to all */
108 for (i = 0; i < 32; i++) {
109 vecs[i].handler = def_hdlr;
110 vecs[i].arg = (void *)i;
111 vecs[i].count = 0;
112 }
113
wdenk5c952cf2004-10-10 21:27:30 +0000114 enable_interrupts ();
115 return (0);
116}
117
118
119/*************************************************************************/
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500120#if defined(CONFIG_CMD_IRQ)
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200121int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk5c952cf2004-10-10 21:27:30 +0000122{
123 int i;
124 struct irq_action *act = vecs;
125
126 printf ("\nInterrupt-Information:\n\n");
127 printf ("Nr Routine Arg Count\n");
128 printf ("-----------------------------\n");
129
130 for (i=0; i<32; i++) {
131 if (act->handler != def_hdlr) {
132 printf ("%02d %08lx %08lx %d\n",
133 i,
134 (ulong)act->handler,
135 (ulong)act->arg,
136 act->count);
137 }
138 act++;
139 }
140 printf ("\n");
141
142 return (0);
143}
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500144#endif