1 /* $OpenBSD: i8259.c,v 1.12 2024/01/19 18:38:16 kettenis Exp $ */
2 /* $NetBSD: i8259.c,v 1.2 2003/03/02 18:27:15 fvdl Exp $ */
3
4 /*
5 * Copyright 2002 (c) Wasabi Systems, Inc.
6 * All rights reserved.
7 *
8 * Written by Frank van der Linden for Wasabi Systems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed for the NetBSD Project by
21 * Wasabi Systems, Inc.
22 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23 * or promote products derived from this software without specific prior
24 * written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1991 The Regents of the University of California.
41 * All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * William Jolitz.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)isa.c 7.2 (Berkeley) 5/13/91
71 */
72
73 #include <sys/param.h>
74
75 #include <dev/isa/isareg.h>
76
77 #include <machine/pio.h>
78 #include <machine/cpufunc.h>
79 #include <machine/cpu.h>
80 #include <machine/pic.h>
81 #include <machine/i8259.h>
82
83 static void i8259_hwmask(struct pic *, int);
84 static void i8259_hwunmask(struct pic *, int);
85 static void i8259_setup(struct pic *, struct cpu_info *, int, int, int);
86 static void i8259_reinit_irqs(void);
87
88 unsigned i8259_imen;
89
90 /*
91 * Perhaps this should be made into a real device.
92 */
93 struct pic i8259_pic = {
94 {0, {NULL}, NULL, 0, "pic0", NULL, 0, 0},
95 PIC_I8259,
96 #ifdef MULTIPROCESSOR
97 {},
98 #endif
99 i8259_hwmask,
100 i8259_hwunmask,
101 i8259_setup,
102 i8259_setup,
103 NULL,
104 i8259_stubs,
105 i8259_stubs,
106 };
107
108 void
i8259_default_setup(void)109 i8259_default_setup(void)
110 {
111 outb(IO_ICU1, 0x11); /* reset; program device, four bytes */
112
113 outb(IO_ICU1+1, ICU_OFFSET); /* starting at this vector index */
114 outb(IO_ICU1+1, 1 << IRQ_SLAVE); /* slave on line 2 */
115 #ifdef AUTO_EOI_1
116 outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */
117 #else
118 outb(IO_ICU1+1, 1); /* 8086 mode */
119 #endif
120 outb(IO_ICU1+1, 0xff); /* leave interrupts masked */
121 outb(IO_ICU1, 0x68); /* special mask mode (if available) */
122 outb(IO_ICU1, 0x0a); /* Read IRR by default. */
123 #ifdef REORDER_IRQ
124 outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */
125 #endif
126
127 outb(IO_ICU2, 0x11); /* reset; program device, four bytes */
128
129 outb(IO_ICU2+1, ICU_OFFSET+8); /* staring at this vector index */
130 outb(IO_ICU2+1, IRQ_SLAVE);
131 #ifdef AUTO_EOI_2
132 outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */
133 #else
134 outb(IO_ICU2+1, 1); /* 8086 mode */
135 #endif
136 outb(IO_ICU2+1, 0xff); /* leave interrupts masked */
137 outb(IO_ICU2, 0x68); /* special mask mode (if available) */
138 outb(IO_ICU2, 0x0a); /* Read IRR by default. */
139 }
140
141 static void
i8259_hwmask(struct pic * pic,int pin)142 i8259_hwmask(struct pic *pic, int pin)
143 {
144 unsigned port;
145 u_int8_t byte;
146
147 i8259_imen |= (1 << pin);
148 #ifdef PIC_MASKDELAY
149 delay(10);
150 #endif
151 if (pin > 7) {
152 port = IO_ICU2 + 1;
153 byte = i8259_imen >> 8;
154 } else {
155 port = IO_ICU1 + 1;
156 byte = i8259_imen & 0xff;
157 }
158 outb(port, byte);
159 }
160
161 static void
i8259_hwunmask(struct pic * pic,int pin)162 i8259_hwunmask(struct pic *pic, int pin)
163 {
164 unsigned port;
165 u_int8_t byte;
166 u_long s;
167
168 s = intr_disable();
169 i8259_imen &= ~(1 << pin);
170 #ifdef PIC_MASKDELAY
171 delay(10);
172 #endif
173 if (pin > 7) {
174 port = IO_ICU2 + 1;
175 byte = i8259_imen >> 8;
176 } else {
177 port = IO_ICU1 + 1;
178 byte = i8259_imen & 0xff;
179 }
180 outb(port, byte);
181 intr_restore(s);
182 }
183
184 static void
i8259_reinit_irqs(void)185 i8259_reinit_irqs(void)
186 {
187 int irqs, irq;
188 struct cpu_info *ci = &cpu_info_primary;
189
190 irqs = 0;
191 for (irq = 0; irq < NUM_LEGACY_IRQS; irq++)
192 if (ci->ci_isources[irq] != NULL)
193 irqs |= 1 << irq;
194 if (irqs >= 0x100) /* any IRQs >= 8 in use */
195 irqs |= 1 << IRQ_SLAVE;
196 i8259_imen = ~irqs;
197
198 outb(IO_ICU1 + 1, i8259_imen);
199 outb(IO_ICU2 + 1, i8259_imen >> 8);
200 }
201
202 static void
i8259_setup(struct pic * pic,struct cpu_info * ci,int pin,int idtvec,int type)203 i8259_setup(struct pic *pic, struct cpu_info *ci, int pin, int idtvec, int type)
204 {
205 if (CPU_IS_PRIMARY(ci))
206 i8259_reinit_irqs();
207 }
208