2 * Copyright (c) 2009 Marcel Moolenaar
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/cpuset.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
37 #include <machine/bus.h>
38 #include <machine/intr_machdep.h>
39 #include <machine/pio.h>
41 #include <powerpc/mpc85xx/mpc85xx.h>
43 #include <dev/ic/i8259.h>
45 #include <isa/isareg.h>
46 #include <isa/isavar.h>
50 #define ATPIC_MASTER 0
56 /* I/O port resources for master & slave. */
57 struct resource *sc_res[2];
60 /* Our "routing" interrupt */
61 struct resource *sc_ires;
69 static int atpic_isa_attach(device_t);
70 static void atpic_isa_identify(driver_t *, device_t);
71 static int atpic_isa_probe(device_t);
73 static void atpic_config(device_t, u_int, enum intr_trigger,
75 static void atpic_dispatch(device_t, struct trapframe *);
76 static void atpic_enable(device_t, u_int, u_int);
77 static void atpic_eoi(device_t, u_int);
78 static void atpic_ipi(device_t, u_int);
79 static void atpic_mask(device_t, u_int);
80 static void atpic_unmask(device_t, u_int);
82 static void atpic_ofw_translate_code(device_t, u_int irq, int code,
83 enum intr_trigger *trig, enum intr_polarity *pol);
85 static device_method_t atpic_isa_methods[] = {
86 /* Device interface */
87 DEVMETHOD(device_identify, atpic_isa_identify),
88 DEVMETHOD(device_probe, atpic_isa_probe),
89 DEVMETHOD(device_attach, atpic_isa_attach),
92 DEVMETHOD(pic_config, atpic_config),
93 DEVMETHOD(pic_dispatch, atpic_dispatch),
94 DEVMETHOD(pic_enable, atpic_enable),
95 DEVMETHOD(pic_eoi, atpic_eoi),
96 DEVMETHOD(pic_ipi, atpic_ipi),
97 DEVMETHOD(pic_mask, atpic_mask),
98 DEVMETHOD(pic_unmask, atpic_unmask),
100 DEVMETHOD(pic_translate_code, atpic_ofw_translate_code),
105 static driver_t atpic_isa_driver = {
108 sizeof(struct atpic_softc)
111 static devclass_t atpic_devclass;
113 DRIVER_MODULE(atpic, isa, atpic_isa_driver, atpic_devclass, 0, 0);
115 static struct isa_pnp_id atpic_ids[] = {
116 { 0x0000d041 /* PNP0000 */, "AT interrupt controller" },
120 static __inline uint8_t
121 atpic_read(struct atpic_softc *sc, int icu, int ofs)
125 val = bus_read_1(sc->sc_res[icu], ofs);
130 atpic_write(struct atpic_softc *sc, int icu, int ofs, uint8_t val)
133 bus_write_1(sc->sc_res[icu], ofs, val);
134 bus_barrier(sc->sc_res[icu], ofs, 2 - ofs,
135 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
139 atpic_intr(void *arg)
142 atpic_dispatch(arg, NULL);
146 atpic_isa_identify(driver_t *drv, device_t parent)
150 child = BUS_ADD_CHILD(parent, ISA_ORDER_SENSITIVE, drv->name, -1);
151 device_set_driver(child, drv);
152 isa_set_logicalid(child, atpic_ids[0].ip_id);
153 isa_set_vendorid(child, atpic_ids[0].ip_id);
155 bus_set_resource(child, SYS_RES_IOPORT, ATPIC_MASTER, IO_ICU1, 2);
156 bus_set_resource(child, SYS_RES_IOPORT, ATPIC_SLAVE, IO_ICU2, 2);
158 /* ISA interrupts are routed through external interrupt 0. */
159 bus_set_resource(child, SYS_RES_IRQ, 0, 16, 1);
163 atpic_isa_probe(device_t dev)
167 res = ISA_PNP_PROBE(device_get_parent(dev), dev, atpic_ids);
171 device_set_desc(dev, "PC/AT compatible PIC");
176 atpic_init(struct atpic_softc *sc, int icu)
179 sc->sc_mask[icu] = 0xff - ((icu == ATPIC_MASTER) ? 4 : 0);
181 atpic_write(sc, icu, 0, ICW1_RESET | ICW1_IC4);
182 atpic_write(sc, icu, 1, (icu == ATPIC_SLAVE) ? 8 : 0);
183 atpic_write(sc, icu, 1, (icu == ATPIC_SLAVE) ? 2 : 4);
184 atpic_write(sc, icu, 1, ICW4_8086);
185 atpic_write(sc, icu, 1, sc->sc_mask[icu]);
186 atpic_write(sc, icu, 0, OCW3_SEL | OCW3_RR);
190 atpic_isa_attach(device_t dev)
192 struct atpic_softc *sc;
195 sc = device_get_softc(dev);
200 sc->sc_rid[ATPIC_MASTER] = 0;
201 sc->sc_res[ATPIC_MASTER] = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
202 &sc->sc_rid[ATPIC_MASTER], RF_ACTIVE);
203 if (sc->sc_res[ATPIC_MASTER] == NULL)
206 sc->sc_rid[ATPIC_SLAVE] = 1;
207 sc->sc_res[ATPIC_SLAVE] = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
208 &sc->sc_rid[ATPIC_SLAVE], RF_ACTIVE);
209 if (sc->sc_res[ATPIC_SLAVE] == NULL)
213 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
215 if (sc->sc_ires == NULL)
218 error = bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_MISC | INTR_MPSAFE,
219 NULL, atpic_intr, dev, &sc->sc_icookie);
223 atpic_init(sc, ATPIC_SLAVE);
224 atpic_init(sc, ATPIC_MASTER);
226 powerpc_register_pic(dev, 0, 16, 0, TRUE);
230 if (sc->sc_ires != NULL)
231 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
233 if (sc->sc_res[ATPIC_SLAVE] != NULL)
234 bus_release_resource(dev, SYS_RES_IOPORT,
235 sc->sc_rid[ATPIC_SLAVE], sc->sc_res[ATPIC_SLAVE]);
236 if (sc->sc_res[ATPIC_MASTER] != NULL)
237 bus_release_resource(dev, SYS_RES_IOPORT,
238 sc->sc_rid[ATPIC_MASTER], sc->sc_res[ATPIC_MASTER]);
248 atpic_config(device_t dev, u_int irq, enum intr_trigger trig,
249 enum intr_polarity pol)
254 atpic_dispatch(device_t dev, struct trapframe *tf)
256 struct atpic_softc *sc;
259 sc = device_get_softc(dev);
260 atpic_write(sc, ATPIC_MASTER, 0, OCW3_SEL | OCW3_P);
261 irq = atpic_read(sc, ATPIC_MASTER, 0);
262 atpic_write(sc, ATPIC_MASTER, 0, OCW3_SEL | OCW3_RR);
263 if ((irq & 0x80) == 0)
267 atpic_write(sc, ATPIC_SLAVE, 0, OCW3_SEL | OCW3_P);
268 irq = atpic_read(sc, ATPIC_SLAVE, 0) + 8;
269 atpic_write(sc, ATPIC_SLAVE, 0, OCW3_SEL | OCW3_RR);
270 if ((irq & 0x80) == 0)
274 powerpc_dispatch_intr(sc->sc_vector[irq & 0x0f], tf);
278 atpic_enable(device_t dev, u_int irq, u_int vector)
280 struct atpic_softc *sc;
282 sc = device_get_softc(dev);
283 sc->sc_vector[irq] = vector;
284 atpic_unmask(dev, irq);
288 atpic_eoi(device_t dev, u_int irq)
290 struct atpic_softc *sc;
292 sc = device_get_softc(dev);
294 atpic_write(sc, ATPIC_SLAVE, 0, OCW2_EOI);
295 atpic_write(sc, ATPIC_MASTER, 0, OCW2_EOI);
299 atpic_ipi(device_t dev, u_int cpu)
301 /* No SMP support. */
305 atpic_mask(device_t dev, u_int irq)
307 struct atpic_softc *sc;
309 sc = device_get_softc(dev);
311 sc->sc_mask[ATPIC_SLAVE] |= 1 << (irq - 8);
312 atpic_write(sc, ATPIC_SLAVE, 1, sc->sc_mask[ATPIC_SLAVE]);
314 sc->sc_mask[ATPIC_MASTER] |= 1 << irq;
315 atpic_write(sc, ATPIC_MASTER, 1, sc->sc_mask[ATPIC_MASTER]);
320 atpic_unmask(device_t dev, u_int irq)
322 struct atpic_softc *sc;
324 sc = device_get_softc(dev);
326 sc->sc_mask[ATPIC_SLAVE] &= ~(1 << (irq - 8));
327 atpic_write(sc, ATPIC_SLAVE, 1, sc->sc_mask[ATPIC_SLAVE]);
329 sc->sc_mask[ATPIC_MASTER] &= ~(1 << irq);
330 atpic_write(sc, ATPIC_MASTER, 1, sc->sc_mask[ATPIC_MASTER]);
335 atpic_ofw_translate_code(device_t dev, u_int irq, int code,
336 enum intr_trigger *trig, enum intr_polarity *pol)
341 *trig = INTR_TRIGGER_LEVEL;
342 *pol = INTR_POLARITY_LOW;
346 *trig = INTR_TRIGGER_LEVEL;
347 *pol = INTR_POLARITY_HIGH;
351 *trig = INTR_TRIGGER_EDGE;
352 *pol = INTR_POLARITY_LOW;
356 *trig = INTR_TRIGGER_EDGE;
357 *pol = INTR_POLARITY_HIGH;
360 *trig = INTR_TRIGGER_CONFORM;
361 *pol = INTR_POLARITY_CONFORM;