blob: f96b2bfd70eee26c6a2b048011aa8f3906ee6a97 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Graeme Russ77290ee2009-02-24 21:13:40 +11002/*
3 * (C) Copyright 2009
Graeme Russ45fc1d82011-04-13 19:43:26 +10004 * Graeme Russ, <graeme.russ@gmail.com>
Graeme Russ77290ee2009-02-24 21:13:40 +11005 *
6 * (C) Copyright 2007
Graeme Russ45fc1d82011-04-13 19:43:26 +10007 * Daniel Hellstrom, Gaisler Research, <daniel@gaisler.com>
Graeme Russ77290ee2009-02-24 21:13:40 +11008 *
9 * (C) Copyright 2006
Graeme Russ45fc1d82011-04-13 19:43:26 +100010 * Detlev Zundel, DENX Software Engineering, <dzu@denx.de>
Graeme Russ77290ee2009-02-24 21:13:40 +110011 *
12 * (C) Copyright -2003
Graeme Russ45fc1d82011-04-13 19:43:26 +100013 * Wolfgang Denk, DENX Software Engineering, <wd@denx.de>
Graeme Russ77290ee2009-02-24 21:13:40 +110014 *
15 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +020016 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
Graeme Russ77290ee2009-02-24 21:13:40 +110017 *
18 * (C) Copyright 2001
Graeme Russ45fc1d82011-04-13 19:43:26 +100019 * Josh Huber, Mission Critical Linux, Inc, <huber@mclx.com>
Graeme Russ77290ee2009-02-24 21:13:40 +110020 */
21
22/*
23 * This file contains the high-level API for the interrupt sub-system
Graeme Russ45fc1d82011-04-13 19:43:26 +100024 * of the x86 port of U-Boot. Most of the functionality has been
Graeme Russ77290ee2009-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 Russ45fc1d82011-04-13 19:43:26 +100028 * interrupt handling routines for the x86 port were written by
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +020029 * Daniel Engström
Graeme Russ77290ee2009-02-24 21:13:40 +110030 */
31
Simon Glass0f2af882020-05-10 11:40:05 -060032#include <command.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070033#include <irq_func.h>
Graeme Russ77290ee2009-02-24 21:13:40 +110034#include <asm/interrupt.h>
35
Simon Glassf5156f02017-01-16 07:04:14 -070036#if !CONFIG_IS_ENABLED(X86_64)
37
Graeme Russ77290ee2009-02-24 21:13:40 +110038struct irq_action {
39 interrupt_handler_t *handler;
40 void *arg;
41 unsigned int count;
42};
43
Bin Menge7a76e52015-10-22 19:13:26 -070044static struct irq_action irq_handlers[SYS_NUM_IRQS] = { {0} };
Graeme Russ883c6032011-11-08 02:33:15 +000045static int spurious_irq_cnt;
46static int spurious_irq;
Graeme Russ77290ee2009-02-24 21:13:40 +110047
48void irq_install_handler(int irq, interrupt_handler_t *handler, void *arg)
49{
50 int status;
51
Bin Menge7a76e52015-10-22 19:13:26 -070052 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russ77290ee2009-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 Russ883c6032011-11-08 02:33:15 +000059 (ulong) handler,
60 (ulong) irq_handlers[irq].handler);
Graeme Russ77290ee2009-02-24 21:13:40 +110061
Graeme Russ883c6032011-11-08 02:33:15 +000062 status = disable_interrupts();
Graeme Russ77290ee2009-02-24 21:13:40 +110063
Graeme Russ078395c2009-11-24 20:04:21 +110064 irq_handlers[irq].handler = handler;
Graeme Russ77290ee2009-02-24 21:13:40 +110065 irq_handlers[irq].arg = arg;
66 irq_handlers[irq].count = 0;
67
Bin Meng465c7102018-11-29 19:57:21 -080068 if (CONFIG_IS_ENABLED(I8259_PIC))
69 unmask_irq(irq);
Graeme Russ77290ee2009-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 Menge7a76e52015-10-22 19:13:26 -070081 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russ77290ee2009-02-24 21:13:40 +110082 printf("irq_free_handler: bad irq number %d\n", irq);
83 return;
84 }
85
Graeme Russ883c6032011-11-08 02:33:15 +000086 status = disable_interrupts();
Graeme Russ77290ee2009-02-24 21:13:40 +110087
Bin Meng465c7102018-11-29 19:57:21 -080088 if (CONFIG_IS_ENABLED(I8259_PIC))
89 mask_irq(irq);
Graeme Russ77290ee2009-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 Russd11b0852009-11-24 20:04:18 +1100100void do_irq(int hw_irq)
Graeme Russ77290ee2009-02-24 21:13:40 +1100101{
Graeme Russd11b0852009-11-24 20:04:18 +1100102 int irq = hw_irq - 0x20;
103
Bin Menge7a76e52015-10-22 19:13:26 -0700104 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russ77290ee2009-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 Meng465c7102018-11-29 19:57:21 -0800110 if (CONFIG_IS_ENABLED(I8259_PIC))
111 mask_irq(irq);
Graeme Russ77290ee2009-02-24 21:13:40 +1100112
113 irq_handlers[irq].handler(irq_handlers[irq].arg);
114 irq_handlers[irq].count++;
115
Bin Meng465c7102018-11-29 19:57:21 -0800116 if (CONFIG_IS_ENABLED(I8259_PIC)) {
117 unmask_irq(irq);
118 specific_eoi(irq);
119 }
Graeme Russ77290ee2009-02-24 21:13:40 +1100120 } else {
121 if ((irq & 7) != 7) {
122 spurious_irq_cnt++;
123 spurious_irq = irq;
124 }
125 }
126}
Simon Glassf5156f02017-01-16 07:04:14 -0700127#endif
Graeme Russ77290ee2009-02-24 21:13:40 +1100128
129#if defined(CONFIG_CMD_IRQ)
Simon Glassed38aef2020-05-10 11:40:03 -0600130int do_irqinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Graeme Russ77290ee2009-02-24 21:13:40 +1100131{
Simon Glassf5156f02017-01-16 07:04:14 -0700132#if !CONFIG_IS_ENABLED(X86_64)
Simon Glass495afca2020-11-04 09:57:28 -0700133 struct idt_ptr ptr;
Graeme Russ77290ee2009-02-24 21:13:40 +1100134 int irq;
135
Simon Glass495afca2020-11-04 09:57:28 -0700136 interrupt_read_idt(&ptr);
137 printf("IDT at %lx, size %x\n", ptr.address, ptr.size);
Graeme Russ77290ee2009-02-24 21:13:40 +1100138 printf("Spurious IRQ: %u, last unknown IRQ: %d\n",
Graeme Russ883c6032011-11-08 02:33:15 +0000139 spurious_irq_cnt, spurious_irq);
Graeme Russ77290ee2009-02-24 21:13:40 +1100140
Graeme Russ883c6032011-11-08 02:33:15 +0000141 printf("Interrupt-Information:\n");
142 printf("Nr Routine Arg Count\n");
Graeme Russ77290ee2009-02-24 21:13:40 +1100143
Bin Menge7a76e52015-10-22 19:13:26 -0700144 for (irq = 0; irq < SYS_NUM_IRQS; irq++) {
Graeme Russ77290ee2009-02-24 21:13:40 +1100145 if (irq_handlers[irq].handler != NULL) {
Graeme Russ883c6032011-11-08 02:33:15 +0000146 printf("%02d %08lx %08lx %d\n",
Graeme Russ77290ee2009-02-24 21:13:40 +1100147 irq,
148 (ulong)irq_handlers[irq].handler,
149 (ulong)irq_handlers[irq].arg,
150 irq_handlers[irq].count);
151 }
152 }
Simon Glassf5156f02017-01-16 07:04:14 -0700153#endif
Graeme Russ77290ee2009-02-24 21:13:40 +1100154
155 return 0;
156}
157#endif