blob: 40ba7fcfec7b4799853a1eed850ac2e14bb3b023 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Graeme Russabf0cd32009-02-24 21:13:40 +11002/*
3 * (C) Copyright 2009
Graeme Russdbf71152011-04-13 19:43:26 +10004 * Graeme Russ, <graeme.russ@gmail.com>
Graeme Russabf0cd32009-02-24 21:13:40 +11005 *
6 * (C) Copyright 2007
Graeme Russdbf71152011-04-13 19:43:26 +10007 * Daniel Hellstrom, Gaisler Research, <daniel@gaisler.com>
Graeme Russabf0cd32009-02-24 21:13:40 +11008 *
9 * (C) Copyright 2006
Graeme Russdbf71152011-04-13 19:43:26 +100010 * Detlev Zundel, DENX Software Engineering, <dzu@denx.de>
Graeme Russabf0cd32009-02-24 21:13:40 +110011 *
12 * (C) Copyright -2003
Graeme Russdbf71152011-04-13 19:43:26 +100013 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
Graeme Russabf0cd32009-02-24 21:13:40 +110014 *
15 * (C) Copyright 2002
Albert ARIBAUDfa82f872011-08-04 18:45:45 +020016 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
Graeme Russabf0cd32009-02-24 21:13:40 +110017 *
18 * (C) Copyright 2001
Graeme Russdbf71152011-04-13 19:43:26 +100019 * Josh Huber, Mission Critical Linux, Inc, <huber@mclx.com>
Graeme Russabf0cd32009-02-24 21:13:40 +110020 */
21
22/*
23 * This file contains the high-level API for the interrupt sub-system
Graeme Russdbf71152011-04-13 19:43:26 +100024 * of the x86 port of U-Boot. Most of the functionality has been
Graeme Russabf0cd32009-02-24 21:13:40 +110025 * shamelessly stolen from the leon2 / leon3 ports of U-Boot.
26 * Daniel Hellstrom, Detlev Zundel, Wolfgang Denk and Josh Huber are
27 * credited for the corresponding work on those ports. The original
Graeme Russdbf71152011-04-13 19:43:26 +100028 * interrupt handling routines for the x86 port were written by
Albert ARIBAUDfa82f872011-08-04 18:45:45 +020029 * Daniel Engström
Graeme Russabf0cd32009-02-24 21:13:40 +110030 */
31
32#include <common.h>
Simon Glassc30b7ad2019-11-14 12:57:41 -070033#include <irq_func.h>
Graeme Russabf0cd32009-02-24 21:13:40 +110034#include <asm/interrupt.h>
35
Simon Glassc2bf0df2017-01-16 07:04:14 -070036#if !CONFIG_IS_ENABLED(X86_64)
37
Graeme Russabf0cd32009-02-24 21:13:40 +110038struct irq_action {
39 interrupt_handler_t *handler;
40 void *arg;
41 unsigned int count;
42};
43
Bin Meng6c505272015-10-22 19:13:26 -070044static struct irq_action irq_handlers[SYS_NUM_IRQS] = { {0} };
Graeme Russ83088af2011-11-08 02:33:15 +000045static int spurious_irq_cnt;
46static int spurious_irq;
Graeme Russabf0cd32009-02-24 21:13:40 +110047
48void irq_install_handler(int irq, interrupt_handler_t *handler, void *arg)
49{
50 int status;
51
Bin Meng6c505272015-10-22 19:13:26 -070052 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russabf0cd32009-02-24 21:13:40 +110053 printf("irq_install_handler: bad irq number %d\n", irq);
54 return;
55 }
56
57 if (irq_handlers[irq].handler != NULL)
58 printf("irq_install_handler: 0x%08lx replacing 0x%08lx\n",
Graeme Russ83088af2011-11-08 02:33:15 +000059 (ulong) handler,
60 (ulong) irq_handlers[irq].handler);
Graeme Russabf0cd32009-02-24 21:13:40 +110061
Graeme Russ83088af2011-11-08 02:33:15 +000062 status = disable_interrupts();
Graeme Russabf0cd32009-02-24 21:13:40 +110063
Graeme Russ1c409bc2009-11-24 20:04:21 +110064 irq_handlers[irq].handler = handler;
Graeme Russabf0cd32009-02-24 21:13:40 +110065 irq_handlers[irq].arg = arg;
66 irq_handlers[irq].count = 0;
67
Bin Mengc6410102018-11-29 19:57:21 -080068 if (CONFIG_IS_ENABLED(I8259_PIC))
69 unmask_irq(irq);
Graeme Russabf0cd32009-02-24 21:13:40 +110070
71 if (status)
72 enable_interrupts();
73
74 return;
75}
76
77void irq_free_handler(int irq)
78{
79 int status;
80
Bin Meng6c505272015-10-22 19:13:26 -070081 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russabf0cd32009-02-24 21:13:40 +110082 printf("irq_free_handler: bad irq number %d\n", irq);
83 return;
84 }
85
Graeme Russ83088af2011-11-08 02:33:15 +000086 status = disable_interrupts();
Graeme Russabf0cd32009-02-24 21:13:40 +110087
Bin Mengc6410102018-11-29 19:57:21 -080088 if (CONFIG_IS_ENABLED(I8259_PIC))
89 mask_irq(irq);
Graeme Russabf0cd32009-02-24 21:13:40 +110090
91 irq_handlers[irq].handler = NULL;
92 irq_handlers[irq].arg = NULL;
93
94 if (status)
95 enable_interrupts();
96
97 return;
98}
99
Graeme Russ564a9982009-11-24 20:04:18 +1100100void do_irq(int hw_irq)
Graeme Russabf0cd32009-02-24 21:13:40 +1100101{
Graeme Russ564a9982009-11-24 20:04:18 +1100102 int irq = hw_irq - 0x20;
103
Bin Meng6c505272015-10-22 19:13:26 -0700104 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russabf0cd32009-02-24 21:13:40 +1100105 printf("do_irq: bad irq number %d\n", irq);
106 return;
107 }
108
109 if (irq_handlers[irq].handler) {
Bin Mengc6410102018-11-29 19:57:21 -0800110 if (CONFIG_IS_ENABLED(I8259_PIC))
111 mask_irq(irq);
Graeme Russabf0cd32009-02-24 21:13:40 +1100112
113 irq_handlers[irq].handler(irq_handlers[irq].arg);
114 irq_handlers[irq].count++;
115
Bin Mengc6410102018-11-29 19:57:21 -0800116 if (CONFIG_IS_ENABLED(I8259_PIC)) {
117 unmask_irq(irq);
118 specific_eoi(irq);
119 }
Graeme Russabf0cd32009-02-24 21:13:40 +1100120 } else {
121 if ((irq & 7) != 7) {
122 spurious_irq_cnt++;
123 spurious_irq = irq;
124 }
125 }
126}
Simon Glassc2bf0df2017-01-16 07:04:14 -0700127#endif
Graeme Russabf0cd32009-02-24 21:13:40 +1100128
129#if defined(CONFIG_CMD_IRQ)
Simon Glass09140112020-05-10 11:40:03 -0600130int do_irqinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Graeme Russabf0cd32009-02-24 21:13:40 +1100131{
Simon Glassc2bf0df2017-01-16 07:04:14 -0700132#if !CONFIG_IS_ENABLED(X86_64)
Graeme Russabf0cd32009-02-24 21:13:40 +1100133 int irq;
134
135 printf("Spurious IRQ: %u, last unknown IRQ: %d\n",
Graeme Russ83088af2011-11-08 02:33:15 +0000136 spurious_irq_cnt, spurious_irq);
Graeme Russabf0cd32009-02-24 21:13:40 +1100137
Graeme Russ83088af2011-11-08 02:33:15 +0000138 printf("Interrupt-Information:\n");
139 printf("Nr Routine Arg Count\n");
Graeme Russabf0cd32009-02-24 21:13:40 +1100140
Bin Meng6c505272015-10-22 19:13:26 -0700141 for (irq = 0; irq < SYS_NUM_IRQS; irq++) {
Graeme Russabf0cd32009-02-24 21:13:40 +1100142 if (irq_handlers[irq].handler != NULL) {
Graeme Russ83088af2011-11-08 02:33:15 +0000143 printf("%02d %08lx %08lx %d\n",
Graeme Russabf0cd32009-02-24 21:13:40 +1100144 irq,
145 (ulong)irq_handlers[irq].handler,
146 (ulong)irq_handlers[irq].arg,
147 irq_handlers[irq].count);
148 }
149 }
Simon Glassc2bf0df2017-01-16 07:04:14 -0700150#endif
Graeme Russabf0cd32009-02-24 21:13:40 +1100151
152 return 0;
153}
154#endif