blob: 088f78f466196cb2e55f881947cfab9840ebcdc6 [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 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02007 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
Graeme Russ77290ee2009-02-24 21:13:40 +11008 */
9
10/*
11 * This file provides the interrupt handling functionality for systems
12 * based on the standard PC/AT architecture using two cascaded i8259
13 * Programmable Interrupt Controllers.
14 */
15
Simon Glassba23a612025-03-15 14:25:28 +000016#define LOG_CATEGORY UCLASS_IRQ
17
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Graeme Russ77290ee2009-02-24 21:13:40 +110019#include <asm/io.h>
20#include <asm/i8259.h>
21#include <asm/ibmpc.h>
22#include <asm/interrupt.h>
23
Bin Mengcb9d9cb2014-11-20 16:11:16 +080024int i8259_init(void)
Graeme Russ77290ee2009-02-24 21:13:40 +110025{
26 u8 i;
27
Graeme Russ77290ee2009-02-24 21:13:40 +110028 /* Mask all interrupts */
29 outb(0xff, MASTER_PIC + IMR);
30 outb(0xff, SLAVE_PIC + IMR);
31
Bin Mengcb68a1a2015-10-22 19:13:28 -070032 /*
33 * Master PIC
34 * Place master PIC interrupts at INT20
35 */
36 outb(ICW1_SEL | ICW1_EICW4, MASTER_PIC + ICW1);
Graeme Russ77290ee2009-02-24 21:13:40 +110037 outb(0x20, MASTER_PIC + ICW2);
38 outb(IR2, MASTER_PIC + ICW3);
39 outb(ICW4_PM, MASTER_PIC + ICW4);
40
41 for (i = 0; i < 8; i++)
42 outb(OCW2_SEOI | i, MASTER_PIC + OCW2);
43
Bin Mengcb68a1a2015-10-22 19:13:28 -070044 /*
45 * Slave PIC
46 * Place slave PIC interrupts at INT28
47 */
48 outb(ICW1_SEL | ICW1_EICW4, SLAVE_PIC + ICW1);
Graeme Russ77290ee2009-02-24 21:13:40 +110049 outb(0x28, SLAVE_PIC + ICW2);
50 outb(0x02, SLAVE_PIC + ICW3);
51 outb(ICW4_PM, SLAVE_PIC + ICW4);
52
53 for (i = 0; i < 8; i++)
54 outb(OCW2_SEOI | i, SLAVE_PIC + OCW2);
55
56 /*
57 * Enable cascaded interrupts by unmasking the cascade IRQ pin of
58 * the master PIC
59 */
Graeme Russ883c6032011-11-08 02:33:15 +000060 unmask_irq(2);
Graeme Russ77290ee2009-02-24 21:13:40 +110061
Simon Glass0e9c6332014-11-14 18:18:31 -070062 /* Interrupt 9 should be level triggered (SCI). The OS might do this */
63 configure_irq_trigger(9, true);
64
Graeme Russ77290ee2009-02-24 21:13:40 +110065 return 0;
66}
67
68void mask_irq(int irq)
69{
70 int imr_port;
71
Bin Menge7a76e52015-10-22 19:13:26 -070072 if (irq >= SYS_NUM_IRQS)
Graeme Russ77290ee2009-02-24 21:13:40 +110073 return;
74
75 if (irq > 7)
76 imr_port = SLAVE_PIC + IMR;
77 else
78 imr_port = MASTER_PIC + IMR;
79
80 outb(inb(imr_port) | (1 << (irq & 7)), imr_port);
81}
82
83void unmask_irq(int irq)
84{
85 int imr_port;
86
Bin Menge7a76e52015-10-22 19:13:26 -070087 if (irq >= SYS_NUM_IRQS)
Graeme Russ77290ee2009-02-24 21:13:40 +110088 return;
89
90 if (irq > 7)
91 imr_port = SLAVE_PIC + IMR;
92 else
93 imr_port = MASTER_PIC + IMR;
94
95 outb(inb(imr_port) & ~(1 << (irq & 7)), imr_port);
96}
97
98void specific_eoi(int irq)
99{
Bin Menge7a76e52015-10-22 19:13:26 -0700100 if (irq >= SYS_NUM_IRQS)
Graeme Russ77290ee2009-02-24 21:13:40 +1100101 return;
102
103 if (irq > 7) {
104 /*
105 * IRQ is on the slave - Issue a corresponding EOI to the
106 * slave PIC and an EOI for IRQ2 (the cascade interrupt)
107 * on the master PIC
108 */
109 outb(OCW2_SEOI | (irq & 7), SLAVE_PIC + OCW2);
110 irq = SEOI_IR2;
111 }
112
113 outb(OCW2_SEOI | irq, MASTER_PIC + OCW2);
114}
Simon Glass0e9c6332014-11-14 18:18:31 -0700115
Simon Glass0e9c6332014-11-14 18:18:31 -0700116void configure_irq_trigger(int int_num, bool is_level_triggered)
117{
118 u16 int_bits = inb(ELCR1) | (((u16)inb(ELCR2)) << 8);
119
120 debug("%s: current interrupts are 0x%x\n", __func__, int_bits);
121 if (is_level_triggered)
122 int_bits |= (1 << int_num);
123 else
124 int_bits &= ~(1 << int_num);
125
126 /* Write new values */
127 debug("%s: try to set interrupts 0x%x\n", __func__, int_bits);
128 outb((u8)(int_bits & 0xff), ELCR1);
129 outb((u8)(int_bits >> 8), ELCR2);
Simon Glass0e9c6332014-11-14 18:18:31 -0700130}