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 <sys/reboot.h>
40 #include <machine/bus.h>
41 #include <machine/platform.h>
42 #include <machine/intr_machdep.h>
43 #include <machine/resource.h>
45 #include <powerpc/wii/wii_gpioreg.h>
49 struct wiigpio_softc {
51 struct resource *sc_rres;
52 bus_space_tag_t sc_bt;
53 bus_space_handle_t sc_bh;
56 struct gpio_pin sc_pins[WIIGPIO_NPINS];
60 #define WIIGPIO_PINBANK(_p) ((_p) / (WIIGPIO_NPINS / 2))
61 #define WIIGPIO_PINMASK(_p) (1 << ((_p) % (WIIGPIO_NPINS / 2)))
62 #define WIIGPIO_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
63 #define WIIGPIO_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
65 static int wiigpio_probe(device_t);
66 static int wiigpio_attach(device_t);
67 static int wiigpio_detach(device_t);
68 static int wiigpio_pin_max(device_t, int *);
69 static int wiigpio_pin_getname(device_t, uint32_t, char *);
70 static int wiigpio_pin_getflags(device_t, uint32_t, uint32_t *);
71 static int wiigpio_pin_setflags(device_t, uint32_t, uint32_t);
72 static int wiigpio_pin_getcaps(device_t, uint32_t, uint32_t *);
73 static int wiigpio_pin_get(device_t, uint32_t, unsigned int *);
74 static int wiigpio_pin_set(device_t, uint32_t, unsigned int);
75 static int wiigpio_pin_toggle(device_t, uint32_t);
76 static void wiigpio_shutdown(void *, int);
78 static device_method_t wiigpio_methods[] = {
79 /* Device interface */
80 DEVMETHOD(device_probe, wiigpio_probe),
81 DEVMETHOD(device_attach, wiigpio_attach),
82 DEVMETHOD(device_detach, wiigpio_detach),
85 DEVMETHOD(gpio_pin_max, wiigpio_pin_max),
86 DEVMETHOD(gpio_pin_getname, wiigpio_pin_getname),
87 DEVMETHOD(gpio_pin_getflags, wiigpio_pin_getflags),
88 DEVMETHOD(gpio_pin_setflags, wiigpio_pin_setflags),
89 DEVMETHOD(gpio_pin_getcaps, wiigpio_pin_getcaps),
90 DEVMETHOD(gpio_pin_get, wiigpio_pin_get),
91 DEVMETHOD(gpio_pin_set, wiigpio_pin_set),
92 DEVMETHOD(gpio_pin_toggle, wiigpio_pin_toggle),
97 static driver_t wiigpio_driver = {
100 sizeof(struct wiigpio_softc)
103 static devclass_t wiigpio_devclass;
105 DRIVER_MODULE(wiigpio, wiibus, wiigpio_driver, wiigpio_devclass, 0, 0);
107 static __inline uint32_t
108 wiigpio_read(struct wiigpio_softc *sc, int n)
111 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, n * 0x20));
115 wiigpio_write(struct wiigpio_softc *sc, int n, uint32_t reg)
118 bus_space_write_4(sc->sc_bt, sc->sc_bh, n * 0x20, reg);
121 static __inline uint32_t
122 wiigpio_dir_read(struct wiigpio_softc *sc, int n)
125 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, n * 0x20 + 4));
129 wiigpio_dir_write(struct wiigpio_softc *sc, int n, uint32_t reg)
132 bus_space_write_4(sc->sc_bt, sc->sc_bh, n * 0x20 + 4, reg);
136 wiigpio_probe(device_t dev)
138 device_set_desc(dev, "Nintendo Wii GPIO");
140 return (BUS_PROBE_NOWILDCARD);
144 wiigpio_attach(device_t dev)
146 struct wiigpio_softc *sc;
150 sc = device_get_softc(dev);
153 sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
154 &sc->sc_rrid, RF_ACTIVE);
155 if (sc->sc_rres == NULL) {
156 device_printf(dev, "could not alloc mem resource\n");
159 sc->sc_bt = rman_get_bustag(sc->sc_rres);
160 sc->sc_bh = rman_get_bushandle(sc->sc_rres);
161 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
163 device_printf(dev, "dir bank0=0x%08x bank1=0x%08x\n",
164 wiigpio_dir_read(sc, 0), wiigpio_dir_read(sc, 1));
165 device_printf(dev, "val bank0=0x%08x bank1=0x%08x\n",
166 wiigpio_read(sc, 0), wiigpio_read(sc, 1));
168 for (i = 0; i < WIIGPIO_NPINS; i++) {
169 sc->sc_pins[i].gp_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
170 sc->sc_pins[i].gp_pin = i;
171 d = wiigpio_dir_read(sc, WIIGPIO_PINBANK(i));
172 if (d & WIIGPIO_PINMASK(i))
173 sc->sc_pins[i].gp_flags = GPIO_PIN_OUTPUT;
175 sc->sc_pins[i].gp_flags = GPIO_PIN_INPUT;
176 snprintf(sc->sc_pins[i].gp_name, GPIOMAXNAME, "PIN %d", i);
178 device_printf(dev, "PIN %d state %d flag %s\n", i,
179 wiigpio_read(sc, WIIGPIO_PINBANK(i)) >>
180 (i % (WIIGPIO_NPINS / 2)) & 1,
181 sc->sc_pins[i].gp_flags == GPIO_PIN_INPUT ?
182 "GPIO_PIN_INPUT" : "GPIO_PIN_OUTPUT");
185 device_add_child(dev, "gpioc", -1);
186 device_add_child(dev, "gpiobus", -1);
188 * We will be responsible for powering off the system.
190 EVENTHANDLER_REGISTER(shutdown_final, wiigpio_shutdown, dev,
193 return (bus_generic_attach(dev));
197 wiigpio_detach(device_t dev)
199 struct wiigpio_softc *sc;
201 sc = device_get_softc(dev);
202 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, sc->sc_rres);
203 mtx_destroy(&sc->sc_mtx);
209 wiigpio_pin_max(device_t dev, int *maxpin)
212 *maxpin = WIIGPIO_NPINS - 1;
218 wiigpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
220 struct wiigpio_softc *sc;
222 if (pin >= WIIGPIO_NPINS)
224 sc = device_get_softc(dev);
225 *caps = sc->sc_pins[pin].gp_caps;
231 wiigpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
233 struct wiigpio_softc *sc;
236 if (pin >= WIIGPIO_NPINS)
238 sc = device_get_softc(dev);
240 reg = wiigpio_read(sc, WIIGPIO_PINBANK(pin));
241 *val = !!(reg & WIIGPIO_PINMASK(pin));
248 wiigpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
250 struct wiigpio_softc *sc;
251 uint32_t reg, pinbank, pinmask;
253 if (pin >= WIIGPIO_NPINS)
255 sc = device_get_softc(dev);
256 pinbank = WIIGPIO_PINBANK(pin);
257 pinmask = WIIGPIO_PINMASK(pin);
259 reg = wiigpio_read(sc, pinbank) & ~pinmask;
262 wiigpio_write(sc, pinbank, reg);
269 wiigpio_pin_toggle(device_t dev, uint32_t pin)
271 struct wiigpio_softc *sc;
272 uint32_t val, pinbank, pinmask;
274 if (pin >= WIIGPIO_NPINS)
276 sc = device_get_softc(dev);
277 pinbank = WIIGPIO_PINBANK(pin);
278 pinmask = WIIGPIO_PINMASK(pin);
280 val = wiigpio_read(sc, pinbank);
282 wiigpio_write(sc, pinbank, val & ~pinmask);
284 wiigpio_write(sc, pinbank, val | pinmask);
291 wiigpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
293 struct wiigpio_softc *sc;
294 uint32_t reg, pinbank, pinmask;
296 if (pin >= WIIGPIO_NPINS)
298 sc = device_get_softc(dev);
299 pinbank = WIIGPIO_PINBANK(pin);
300 pinmask = WIIGPIO_PINMASK(pin);
302 reg = wiigpio_dir_read(sc, WIIGPIO_PINBANK(pin));
303 if (flags & GPIO_PIN_OUTPUT)
304 wiigpio_dir_write(sc, pinbank, reg | pinmask);
306 wiigpio_dir_write(sc, pinbank, reg & ~pinmask);
307 sc->sc_pins[pin].gp_flags = flags;
314 wiigpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
316 struct wiigpio_softc *sc;
318 if (pin >= WIIGPIO_NPINS)
320 sc = device_get_softc(dev);
322 *flags = sc->sc_pins[pin].gp_flags;
329 wiigpio_pin_getname(device_t dev, uint32_t pin, char *name)
331 struct wiigpio_softc *sc;
333 if (pin >= WIIGPIO_NPINS)
335 sc = device_get_softc(dev);
337 memcpy(name, sc->sc_pins[pin].gp_name, GPIOMAXNAME);
344 wiigpio_shutdown(void *xdev, int howto)
348 if (!(howto & RB_POWEROFF))
350 dev = (device_t)xdev;
351 wiigpio_pin_setflags(dev, WIIGPIO_POWEROFF_PIN, GPIO_PIN_OUTPUT);
352 wiigpio_pin_set(dev, WIIGPIO_POWEROFF_PIN, 1);