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>
38 #include <machine/bus.h>
39 #include <machine/platform.h>
40 #include <machine/intr_machdep.h>
41 #include <machine/resource.h>
43 #include <powerpc/wii/wii_picreg.h>
47 static int wiipic_probe(device_t);
48 static int wiipic_attach(device_t);
49 static void wiipic_dispatch(device_t, struct trapframe *);
50 static void wiipic_enable(device_t, unsigned int, unsigned int);
51 static void wiipic_eoi(device_t, unsigned int);
52 static void wiipic_mask(device_t, unsigned int);
53 static void wiipic_unmask(device_t, unsigned int);
57 struct resource *sc_rres;
58 bus_space_tag_t sc_bt;
59 bus_space_handle_t sc_bh;
61 int sc_vector[WIIPIC_NIRQ];
64 static device_method_t wiipic_methods[] = {
65 /* Device interface */
66 DEVMETHOD(device_probe, wiipic_probe),
67 DEVMETHOD(device_attach, wiipic_attach),
70 DEVMETHOD(pic_dispatch, wiipic_dispatch),
71 DEVMETHOD(pic_enable, wiipic_enable),
72 DEVMETHOD(pic_eoi, wiipic_eoi),
73 DEVMETHOD(pic_mask, wiipic_mask),
74 DEVMETHOD(pic_unmask, wiipic_unmask),
79 static driver_t wiipic_driver = {
82 sizeof(struct wiipic_softc)
85 static devclass_t wiipic_devclass;
87 DRIVER_MODULE(wiipic, wiibus, wiipic_driver, wiipic_devclass, 0, 0);
89 static __inline uint32_t
90 wiipic_imr_read(struct wiipic_softc *sc)
93 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, WIIPIC_IMR));
97 wiipic_imr_write(struct wiipic_softc *sc, uint32_t imr)
100 bus_space_write_4(sc->sc_bt, sc->sc_bh, WIIPIC_IMR, imr);
103 static __inline uint32_t
104 wiipic_icr_read(struct wiipic_softc *sc)
107 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, WIIPIC_ICR));
111 wiipic_icr_write(struct wiipic_softc *sc, uint32_t icr)
114 bus_space_write_4(sc->sc_bt, sc->sc_bh, WIIPIC_ICR, icr);
118 wiipic_probe(device_t dev)
120 device_set_desc(dev, "Nintendo Wii PIC");
122 return (BUS_PROBE_NOWILDCARD);
126 wiipic_attach(device_t dev)
128 struct wiipic_softc *sc;
130 sc = device_get_softc(dev);
134 sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
135 &sc->sc_rrid, RF_ACTIVE);
136 if (sc->sc_rres == NULL) {
137 device_printf(dev, "could not alloc mem resource\n");
140 sc->sc_bt = rman_get_bustag(sc->sc_rres);
141 sc->sc_bh = rman_get_bushandle(sc->sc_rres);
143 /* Turn off all interrupts */
144 wiipic_imr_write(sc, 0x00000000);
145 wiipic_icr_write(sc, 0xffffffff);
147 powerpc_register_pic(dev, 0, WIIPIC_NIRQ, 0, FALSE);
153 wiipic_dispatch(device_t dev, struct trapframe *tf)
155 struct wiipic_softc *sc;
158 sc = device_get_softc(dev);
159 irq = ffs(wiipic_icr_read(sc) & wiipic_imr_read(sc));
160 KASSERT(irq < WIIPIC_NIRQ, ("bogus irq %d", irq));
161 powerpc_dispatch_intr(sc->sc_vector[irq], tf);
165 wiipic_enable(device_t dev, unsigned int irq, unsigned int vector)
167 struct wiipic_softc *sc;
169 KASSERT(irq < WIIPIC_NIRQ, ("bogus irq %d", irq));
170 sc = device_get_softc(dev);
171 sc->sc_vector[irq] = vector;
172 wiipic_unmask(dev, irq);
176 wiipic_eoi(device_t dev, unsigned int irq)
178 struct wiipic_softc *sc;
181 sc = device_get_softc(dev);
182 icr = wiipic_icr_read(sc);
184 wiipic_icr_write(sc, icr);
188 wiipic_mask(device_t dev, unsigned int irq)
190 struct wiipic_softc *sc;
193 sc = device_get_softc(dev);
194 imr = wiipic_imr_read(sc);
196 wiipic_imr_write(sc, imr);
200 wiipic_unmask(device_t dev, unsigned int irq)
202 struct wiipic_softc *sc;
205 sc = device_get_softc(dev);
206 imr = wiipic_imr_read(sc);
208 wiipic_imr_write(sc, imr);