blob: e9d1ff911be70ddddb3e045948ac589a1937e22d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkef3386f2004-10-10 21:27:30 +00002/*
3 * (C) Copyright 2000-2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
7 * Scott McNutt <smcnutt@psyent.com>
wdenkef3386f2004-10-10 21:27:30 +00008 */
9
Thomas Chou4e1acb72015-10-03 21:02:30 +080010#include <common.h>
11#include <command.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070012#include <irq_func.h>
Thomas Chou0f9763f2014-08-25 17:09:07 +080013#include <asm/nios2.h>
Stefan Roese37628252008-08-06 14:05:38 +020014#include <asm/types.h>
Scott McNutta5721a32006-06-08 11:59:57 -040015#include <asm/io.h>
wdenkef3386f2004-10-10 21:27:30 +000016#include <asm/ptrace.h>
wdenkef3386f2004-10-10 21:27:30 +000017
Thomas Chou16dc04b2015-10-08 21:23:37 +080018/*************************************************************************/
wdenkef3386f2004-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
Simon Glassf87959b2019-11-14 12:57:40 -070027int disable_interrupts(void)
wdenkef3386f2004-10-10 21:27:30 +000028{
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
Simon Glassf87959b2019-11-14 12:57:40 -070040void external_interrupt(struct pt_regs *regs)
wdenkef3386f2004-10-10 21:27:30 +000041{
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/*************************************************************************/
Simon Glassf87959b2019-11-14 12:57:40 -070077void irq_install_handler(int irq, interrupt_handler_t *hdlr, void *arg)
wdenkef3386f2004-10-10 21:27:30 +000078{
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
Simon Glassf87959b2019-11-14 12:57:40 -070088 flag = disable_interrupts();
wdenkef3386f2004-10-10 21:27:30 +000089 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);
Simon Glassf87959b2019-11-14 12:57:40 -070099 if (flag) enable_interrupts();
wdenkef3386f2004-10-10 21:27:30 +0000100}
101
102
Simon Glassf87959b2019-11-14 12:57:40 -0700103int interrupt_init(void)
wdenkef3386f2004-10-10 21:27:30 +0000104{
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
Simon Glassf87959b2019-11-14 12:57:40 -0700114 enable_interrupts();
wdenkef3386f2004-10-10 21:27:30 +0000115 return (0);
116}
117
118
119/*************************************************************************/
Jon Loeligera5217742007-07-09 18:57:22 -0500120#if defined(CONFIG_CMD_IRQ)
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200121int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkef3386f2004-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 Loeligera5217742007-07-09 18:57:22 -0500144#endif