2 * Copyright (C) 2012 Margarida Gouveia
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 THE AUTHOR ``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 THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * 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>
31 #include <sys/module.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <sys/reboot.h>
39 #include <machine/bus.h>
40 #include <machine/platform.h>
41 #include <machine/intr_machdep.h>
42 #include <machine/resource.h>
44 #include <powerpc/wii/wii_picreg.h>
48 static int wiipic_probe(device_t);
49 static int wiipic_attach(device_t);
50 static void wiipic_dispatch(device_t, struct trapframe *);
51 static void wiipic_enable(device_t, unsigned int, unsigned int);
52 static void wiipic_eoi(device_t, unsigned int);
53 static void wiipic_mask(device_t, unsigned int);
54 static void wiipic_unmask(device_t, unsigned int);
55 static void wiipic_intr(void *);
59 struct resource *sc_rres;
60 bus_space_tag_t sc_bt;
61 bus_space_handle_t sc_bh;
64 struct resource *sc_irq;
66 int sc_vector[WIIPIC_NIRQ];
69 static device_method_t wiipic_methods[] = {
70 /* Device interface */
71 DEVMETHOD(device_probe, wiipic_probe),
72 DEVMETHOD(device_attach, wiipic_attach),
75 DEVMETHOD(pic_dispatch, wiipic_dispatch),
76 DEVMETHOD(pic_enable, wiipic_enable),
77 DEVMETHOD(pic_eoi, wiipic_eoi),
78 DEVMETHOD(pic_mask, wiipic_mask),
79 DEVMETHOD(pic_unmask, wiipic_unmask),
84 static driver_t wiipic_driver = {
87 sizeof(struct wiipic_softc)
90 static devclass_t wiipic_devclass;
92 DRIVER_MODULE(wiipic, wiibus, wiipic_driver, wiipic_devclass, 0, 0);
94 static __inline uint32_t
95 wiipic_imr_read(struct wiipic_softc *sc)
98 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, WIIPIC_IMR));
102 wiipic_imr_write(struct wiipic_softc *sc, uint32_t imr)
105 bus_space_write_4(sc->sc_bt, sc->sc_bh, WIIPIC_IMR, imr);
108 static __inline uint32_t
109 wiipic_icr_read(struct wiipic_softc *sc)
112 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, WIIPIC_ICR));
116 wiipic_icr_write(struct wiipic_softc *sc, uint32_t icr)
119 bus_space_write_4(sc->sc_bt, sc->sc_bh, WIIPIC_ICR, icr);
123 wiipic_probe(device_t dev)
125 device_set_desc(dev, "Nintendo Wii PIC");
127 return (BUS_PROBE_NOWILDCARD);
131 wiipic_attach(device_t dev)
133 struct wiipic_softc *sc;
135 sc = device_get_softc(dev);
139 sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
140 &sc->sc_rrid, RF_ACTIVE);
141 if (sc->sc_rres == NULL) {
142 device_printf(dev, "could not alloc mem resource\n");
145 sc->sc_bt = rman_get_bustag(sc->sc_rres);
146 sc->sc_bh = rman_get_bushandle(sc->sc_rres);
148 /* Turn off all interrupts */
149 wiipic_imr_write(sc, 0x00000000);
150 wiipic_icr_write(sc, 0xffffffff);
152 powerpc_register_pic(dev, 0, WIIPIC_NIRQ, 0, FALSE);
155 * Setup the interrupt handler.
158 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irqid,
160 if (sc->sc_irq == NULL) {
161 device_printf(dev, "could not alloc IRQ resource\n");
164 bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE,
165 NULL, wiipic_intr, sc, &sc->sc_irqctx);
171 wiipic_dispatch(device_t dev, struct trapframe *tf)
173 struct wiipic_softc *sc;
176 sc = device_get_softc(dev);
177 irq = wiipic_icr_read(sc) & wiipic_imr_read(sc);
181 KASSERT(irq < WIIPIC_NIRQ, ("bogus irq %d", irq));
182 powerpc_dispatch_intr(sc->sc_vector[irq], tf);
186 wiipic_enable(device_t dev, unsigned int irq, unsigned int vector)
188 struct wiipic_softc *sc;
190 KASSERT(irq < WIIPIC_NIRQ, ("bogus irq %d", irq));
191 sc = device_get_softc(dev);
192 sc->sc_vector[irq] = vector;
193 wiipic_unmask(dev, irq);
197 wiipic_eoi(device_t dev, unsigned int irq)
199 struct wiipic_softc *sc;
202 sc = device_get_softc(dev);
203 icr = wiipic_icr_read(sc);
205 wiipic_icr_write(sc, icr);
209 wiipic_mask(device_t dev, unsigned int irq)
211 struct wiipic_softc *sc;
214 sc = device_get_softc(dev);
215 imr = wiipic_imr_read(sc);
217 wiipic_imr_write(sc, imr);
221 wiipic_unmask(device_t dev, unsigned int irq)
223 struct wiipic_softc *sc;
226 sc = device_get_softc(dev);
227 imr = wiipic_imr_read(sc);
229 wiipic_imr_write(sc, imr);
233 * Reset button interrupt.
236 wiipic_intr(void *xsc)
238 struct wiipic_softc *sc;
240 sc = (struct wiipic_softc *)xsc;
241 if (wiipic_icr_read(sc) & WIIPIC_RBS)
242 shutdown_nice(RB_AUTOBOOT);