blob: ff52959ed285e241523bbdb960c5d2e1981d3b86 [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
32#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060033#include <command.h>
Simon Glass9b61c7c2019-11-14 12:57:41 -070034#include <irq_func.h>
Graeme Russ77290ee2009-02-24 21:13:40 +110035#include <asm/interrupt.h>
36
Simon Glassf5156f02017-01-16 07:04:14 -070037#if !CONFIG_IS_ENABLED(X86_64)
38
Graeme Russ77290ee2009-02-24 21:13:40 +110039struct irq_action {
40 interrupt_handler_t *handler;
41 void *arg;
42 unsigned int count;
43};
44
Bin Menge7a76e52015-10-22 19:13:26 -070045static struct irq_action irq_handlers[SYS_NUM_IRQS] = { {0} };
Graeme Russ883c6032011-11-08 02:33:15 +000046static int spurious_irq_cnt;
47static int spurious_irq;
Graeme Russ77290ee2009-02-24 21:13:40 +110048
49void irq_install_handler(int irq, interrupt_handler_t *handler, void *arg)
50{
51 int status;
52
Bin Menge7a76e52015-10-22 19:13:26 -070053 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russ77290ee2009-02-24 21:13:40 +110054 printf("irq_install_handler: bad irq number %d\n", irq);
55 return;
56 }
57
58 if (irq_handlers[irq].handler != NULL)
59 printf("irq_install_handler: 0x%08lx replacing 0x%08lx\n",
Graeme Russ883c6032011-11-08 02:33:15 +000060 (ulong) handler,
61 (ulong) irq_handlers[irq].handler);
Graeme Russ77290ee2009-02-24 21:13:40 +110062
Graeme Russ883c6032011-11-08 02:33:15 +000063 status = disable_interrupts();
Graeme Russ77290ee2009-02-24 21:13:40 +110064
Graeme Russ078395c2009-11-24 20:04:21 +110065 irq_handlers[irq].handler = handler;
Graeme Russ77290ee2009-02-24 21:13:40 +110066 irq_handlers[irq].arg = arg;
67 irq_handlers[irq].count = 0;
68
Bin Meng465c7102018-11-29 19:57:21 -080069 if (CONFIG_IS_ENABLED(I8259_PIC))
70 unmask_irq(irq);
Graeme Russ77290ee2009-02-24 21:13:40 +110071
72 if (status)
73 enable_interrupts();
74
75 return;
76}
77
78void irq_free_handler(int irq)
79{
80 int status;
81
Bin Menge7a76e52015-10-22 19:13:26 -070082 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russ77290ee2009-02-24 21:13:40 +110083 printf("irq_free_handler: bad irq number %d\n", irq);
84 return;
85 }
86
Graeme Russ883c6032011-11-08 02:33:15 +000087 status = disable_interrupts();
Graeme Russ77290ee2009-02-24 21:13:40 +110088
Bin Meng465c7102018-11-29 19:57:21 -080089 if (CONFIG_IS_ENABLED(I8259_PIC))
90 mask_irq(irq);
Graeme Russ77290ee2009-02-24 21:13:40 +110091
92 irq_handlers[irq].handler = NULL;
93 irq_handlers[irq].arg = NULL;
94
95 if (status)
96 enable_interrupts();
97
98 return;
99}
100
Graeme Russd11b0852009-11-24 20:04:18 +1100101void do_irq(int hw_irq)
Graeme Russ77290ee2009-02-24 21:13:40 +1100102{
Graeme Russd11b0852009-11-24 20:04:18 +1100103 int irq = hw_irq - 0x20;
104
Bin Menge7a76e52015-10-22 19:13:26 -0700105 if (irq < 0 || irq >= SYS_NUM_IRQS) {
Graeme Russ77290ee2009-02-24 21:13:40 +1100106 printf("do_irq: bad irq number %d\n", irq);
107 return;
108 }
109
110 if (irq_handlers[irq].handler) {
Bin Meng465c7102018-11-29 19:57:21 -0800111 if (CONFIG_IS_ENABLED(I8259_PIC))
112 mask_irq(irq);
Graeme Russ77290ee2009-02-24 21:13:40 +1100113
114 irq_handlers[irq].handler(irq_handlers[irq].arg);
115 irq_handlers[irq].count++;
116
Bin Meng465c7102018-11-29 19:57:21 -0800117 if (CONFIG_IS_ENABLED(I8259_PIC)) {
118 unmask_irq(irq);
119 specific_eoi(irq);
120 }
Graeme Russ77290ee2009-02-24 21:13:40 +1100121 } else {
122 if ((irq & 7) != 7) {
123 spurious_irq_cnt++;
124 spurious_irq = irq;
125 }
126 }
127}
Simon Glassf5156f02017-01-16 07:04:14 -0700128#endif
Graeme Russ77290ee2009-02-24 21:13:40 +1100129
130#if defined(CONFIG_CMD_IRQ)
Simon Glassed38aef2020-05-10 11:40:03 -0600131int do_irqinfo(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Graeme Russ77290ee2009-02-24 21:13:40 +1100132{
Simon Glassf5156f02017-01-16 07:04:14 -0700133#if !CONFIG_IS_ENABLED(X86_64)
Simon Glass495afca2020-11-04 09:57:28 -0700134 struct idt_ptr ptr;
Graeme Russ77290ee2009-02-24 21:13:40 +1100135 int irq;
136
Simon Glass495afca2020-11-04 09:57:28 -0700137 interrupt_read_idt(&ptr);
138 printf("IDT at %lx, size %x\n", ptr.address, ptr.size);
Graeme Russ77290ee2009-02-24 21:13:40 +1100139 printf("Spurious IRQ: %u, last unknown IRQ: %d\n",
Graeme Russ883c6032011-11-08 02:33:15 +0000140 spurious_irq_cnt, spurious_irq);
Graeme Russ77290ee2009-02-24 21:13:40 +1100141
Graeme Russ883c6032011-11-08 02:33:15 +0000142 printf("Interrupt-Information:\n");
143 printf("Nr Routine Arg Count\n");
Graeme Russ77290ee2009-02-24 21:13:40 +1100144
Bin Menge7a76e52015-10-22 19:13:26 -0700145 for (irq = 0; irq < SYS_NUM_IRQS; irq++) {
Graeme Russ77290ee2009-02-24 21:13:40 +1100146 if (irq_handlers[irq].handler != NULL) {
Graeme Russ883c6032011-11-08 02:33:15 +0000147 printf("%02d %08lx %08lx %d\n",
Graeme Russ77290ee2009-02-24 21:13:40 +1100148 irq,
149 (ulong)irq_handlers[irq].handler,
150 (ulong)irq_handlers[irq].arg,
151 irq_handlers[irq].count);
152 }
153 }
Simon Glassf5156f02017-01-16 07:04:14 -0700154#endif
Graeme Russ77290ee2009-02-24 21:13:40 +1100155
156 return 0;
157}
158#endif