2 * Copyright (C) 2002 Benno Rice.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/param.h>
29 #include <sys/systm.h>
32 #include <sys/kernel.h>
35 #include <machine/bus.h>
36 #include <machine/intr.h>
37 #include <machine/intr_machdep.h>
38 #include <machine/md_var.h>
39 #include <machine/pio.h>
40 #include <machine/resource.h>
45 #include <machine/openpicreg.h>
46 #include <machine/openpicvar.h>
50 devclass_t openpic_devclass;
56 static __inline uint32_t
57 openpic_read(struct openpic_softc *sc, u_int reg)
59 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg));
63 openpic_write(struct openpic_softc *sc, u_int reg, uint32_t val)
65 bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val);
69 openpic_set_priority(struct openpic_softc *sc, int pri)
74 tpr = OPENPIC_PCPU_TPR(PCPU_GET(cpuid));
75 x = openpic_read(sc, tpr);
76 x &= ~OPENPIC_TPR_MASK;
78 openpic_write(sc, tpr, x);
82 openpic_attach(device_t dev)
84 struct openpic_softc *sc;
88 sc = device_get_softc(dev);
92 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rid,
95 if (sc->sc_memr == NULL) {
96 device_printf(dev, "Could not alloc mem resource!\n");
100 sc->sc_bt = rman_get_bustag(sc->sc_memr);
101 sc->sc_bh = rman_get_bushandle(sc->sc_memr);
103 x = openpic_read(sc, OPENPIC_FEATURE);
104 switch (x & OPENPIC_FEATURE_VERSION_MASK) {
106 sc->sc_version = "1.0";
109 sc->sc_version = "1.2";
112 sc->sc_version = "1.3";
115 sc->sc_version = "unknown";
119 sc->sc_ncpu = ((x & OPENPIC_FEATURE_LAST_CPU_MASK) >>
120 OPENPIC_FEATURE_LAST_CPU_SHIFT) + 1;
121 sc->sc_nirq = ((x & OPENPIC_FEATURE_LAST_IRQ_MASK) >>
122 OPENPIC_FEATURE_LAST_IRQ_SHIFT) + 1;
125 * PSIM seems to report 1 too many IRQs and CPUs
134 "Version %s, supports %d CPUs and %d irqs\n",
135 sc->sc_version, sc->sc_ncpu, sc->sc_nirq);
137 for (cpu = 0; cpu < sc->sc_ncpu; cpu++)
138 openpic_write(sc, OPENPIC_PCPU_TPR(cpu), 15);
140 /* Reset and disable all interrupts. */
141 for (irq = 0; irq < sc->sc_nirq; irq++) {
142 x = irq; /* irq == vector. */
144 x |= OPENPIC_POLARITY_POSITIVE;
145 x |= OPENPIC_SENSE_LEVEL;
146 x |= 8 << OPENPIC_PRIORITY_SHIFT;
147 openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
150 /* Reset and disable all IPIs. */
151 for (ipi = 0; ipi < 4; ipi++) {
152 x = sc->sc_nirq + ipi;
154 x |= 15 << OPENPIC_PRIORITY_SHIFT;
155 openpic_write(sc, OPENPIC_IPI_VECTOR(ipi), x);
158 /* we don't need 8259 passthrough mode */
159 x = openpic_read(sc, OPENPIC_CONFIG);
160 x |= OPENPIC_CONFIG_8259_PASSTHRU_DISABLE;
161 openpic_write(sc, OPENPIC_CONFIG, x);
163 /* send all interrupts to cpu 0 */
164 for (irq = 0; irq < sc->sc_nirq; irq++)
165 openpic_write(sc, OPENPIC_IDEST(irq), 1 << 0);
167 /* clear all pending interrupts */
168 for (irq = 0; irq < sc->sc_nirq; irq++) {
169 (void)openpic_read(sc, OPENPIC_PCPU_IACK(PCPU_GET(cpuid)));
170 openpic_write(sc, OPENPIC_PCPU_EOI(PCPU_GET(cpuid)), 0);
173 for (cpu = 0; cpu < sc->sc_ncpu; cpu++)
174 openpic_write(sc, OPENPIC_PCPU_TPR(cpu), 0);
176 powerpc_register_pic(dev, sc->sc_nirq);
186 openpic_config(device_t dev, u_int irq, enum intr_trigger trig,
187 enum intr_polarity pol)
189 struct openpic_softc *sc;
192 sc = device_get_softc(dev);
193 x = openpic_read(sc, OPENPIC_SRC_VECTOR(irq));
194 if (pol == INTR_POLARITY_LOW)
195 x &= ~OPENPIC_POLARITY_POSITIVE;
197 x |= OPENPIC_POLARITY_POSITIVE;
198 if (trig == INTR_TRIGGER_EDGE)
199 x &= ~OPENPIC_SENSE_LEVEL;
201 x |= OPENPIC_SENSE_LEVEL;
202 openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
206 openpic_dispatch(device_t dev, struct trapframe *tf)
208 struct openpic_softc *sc;
211 CTR1(KTR_INTR, "%s: got interrupt", __func__);
213 cpuid = PCPU_GET(cpuid);
214 sc = device_get_softc(dev);
217 vector = openpic_read(sc, OPENPIC_PCPU_IACK(cpuid));
218 vector &= OPENPIC_VECTOR_MASK;
221 powerpc_dispatch_intr(vector, tf);
226 openpic_enable(device_t dev, u_int irq, u_int vector)
228 struct openpic_softc *sc;
231 sc = device_get_softc(dev);
232 if (irq < sc->sc_nirq) {
233 x = openpic_read(sc, OPENPIC_SRC_VECTOR(irq));
234 x &= ~(OPENPIC_IMASK | OPENPIC_VECTOR_MASK);
236 openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
238 x = openpic_read(sc, OPENPIC_IPI_VECTOR(0));
239 x &= ~(OPENPIC_IMASK | OPENPIC_VECTOR_MASK);
241 openpic_write(sc, OPENPIC_IPI_VECTOR(0), x);
246 openpic_eoi(device_t dev, u_int irq __unused)
248 struct openpic_softc *sc;
250 sc = device_get_softc(dev);
251 openpic_write(sc, OPENPIC_PCPU_EOI(PCPU_GET(cpuid)), 0);
255 openpic_ipi(device_t dev, u_int cpu)
257 struct openpic_softc *sc;
259 sc = device_get_softc(dev);
260 openpic_write(sc, OPENPIC_PCPU_IPI_DISPATCH(PCPU_GET(cpuid), 0),
265 openpic_mask(device_t dev, u_int irq)
267 struct openpic_softc *sc;
270 sc = device_get_softc(dev);
271 if (irq < sc->sc_nirq) {
272 x = openpic_read(sc, OPENPIC_SRC_VECTOR(irq));
274 openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
276 x = openpic_read(sc, OPENPIC_IPI_VECTOR(0));
278 openpic_write(sc, OPENPIC_IPI_VECTOR(0), x);
280 openpic_write(sc, OPENPIC_PCPU_EOI(PCPU_GET(cpuid)), 0);
284 openpic_unmask(device_t dev, u_int irq)
286 struct openpic_softc *sc;
289 sc = device_get_softc(dev);
290 if (irq < sc->sc_nirq) {
291 x = openpic_read(sc, OPENPIC_SRC_VECTOR(irq));
293 openpic_write(sc, OPENPIC_SRC_VECTOR(irq), x);
295 x = openpic_read(sc, OPENPIC_IPI_VECTOR(0));
297 openpic_write(sc, OPENPIC_IPI_VECTOR(0), x);