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>
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_gpioreg.h>
48 struct wiigpio_softc {
50 struct resource *sc_rres;
51 bus_space_tag_t sc_bt;
52 bus_space_handle_t sc_bh;
55 struct gpio_pin sc_pins[WIIGPIO_NPINS];
58 #define WIIGPIO_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
59 #define WIIGPIO_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
61 static int wiigpio_probe(device_t);
62 static int wiigpio_attach(device_t);
63 static int wiigpio_detach(device_t);
64 static int wiigpio_pin_max(device_t, int *);
65 static int wiigpio_pin_getname(device_t, uint32_t, char *);
66 static int wiigpio_pin_getflags(device_t, uint32_t, uint32_t *);
67 static int wiigpio_pin_setflags(device_t, uint32_t, uint32_t);
68 static int wiigpio_pin_getcaps(device_t, uint32_t, uint32_t *);
69 static int wiigpio_pin_get(device_t, uint32_t, unsigned int *);
70 static int wiigpio_pin_set(device_t, uint32_t, unsigned int);
71 static int wiigpio_pin_toggle(device_t, uint32_t);
73 static device_method_t wiigpio_methods[] = {
74 /* Device interface */
75 DEVMETHOD(device_probe, wiigpio_probe),
76 DEVMETHOD(device_attach, wiigpio_attach),
77 DEVMETHOD(device_detach, wiigpio_detach),
80 DEVMETHOD(gpio_pin_max, wiigpio_pin_max),
81 DEVMETHOD(gpio_pin_getname, wiigpio_pin_getname),
82 DEVMETHOD(gpio_pin_getflags, wiigpio_pin_getflags),
83 DEVMETHOD(gpio_pin_setflags, wiigpio_pin_setflags),
84 DEVMETHOD(gpio_pin_getcaps, wiigpio_pin_getcaps),
85 DEVMETHOD(gpio_pin_get, wiigpio_pin_get),
86 DEVMETHOD(gpio_pin_set, wiigpio_pin_set),
87 DEVMETHOD(gpio_pin_toggle, wiigpio_pin_toggle),
92 static driver_t wiigpio_driver = {
95 sizeof(struct wiigpio_softc)
98 static devclass_t wiigpio_devclass;
100 DRIVER_MODULE(wiigpio, wiibus, wiigpio_driver, wiigpio_devclass, 0, 0);
102 static __inline uint32_t
103 wiigpio_read(struct wiigpio_softc *sc)
106 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, 0));
110 wiigpio_write(struct wiigpio_softc *sc, uint32_t reg)
113 bus_space_write_4(sc->sc_bt, sc->sc_bh, 0, reg);
117 wiigpio_probe(device_t dev)
119 device_set_desc(dev, "Nintendo Wii GPIO");
121 return (BUS_PROBE_NOWILDCARD);
125 wiigpio_attach(device_t dev)
127 struct wiigpio_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 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
145 for (i = 0; i < WIIGPIO_NPINS; i++) {
146 sc->sc_pins[i].gp_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
147 sc->sc_pins[i].gp_pin = i;
148 sc->sc_pins[i].gp_flags = 0;
149 snprintf(sc->sc_pins[i].gp_name, GPIOMAXNAME, "PIN %d", i);
152 device_add_child(dev, "gpioc", device_get_unit(dev));
153 device_add_child(dev, "gpiobus", device_get_unit(dev));
155 return (bus_generic_attach(dev));
159 wiigpio_detach(device_t dev)
161 struct wiigpio_softc *sc;
163 sc = device_get_softc(dev);
164 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, sc->sc_rres);
165 mtx_destroy(&sc->sc_mtx);
171 wiigpio_pin_max(device_t dev, int *maxpin)
174 *maxpin = WIIGPIO_NPINS - 1;
180 wiigpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
182 struct wiigpio_softc *sc;
184 if (pin >= WIIGPIO_NPINS)
186 sc = device_get_softc(dev);
187 *caps = sc->sc_pins[pin].gp_caps;
193 wiigpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
195 struct wiigpio_softc *sc;
198 if (pin >= WIIGPIO_NPINS)
200 sc = device_get_softc(dev);
202 reg = wiigpio_read(sc);
203 *val = !!(reg & (1 << pin));
210 wiigpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
212 struct wiigpio_softc *sc;
213 uint32_t reg, pinmask = 1 << pin;
215 if (pin >= WIIGPIO_NPINS)
217 sc = device_get_softc(dev);
219 reg = wiigpio_read(sc) & ~pinmask;
222 wiigpio_write(sc, reg);
229 wiigpio_pin_toggle(device_t dev, uint32_t pin)
231 struct wiigpio_softc *sc;
232 uint32_t val, pinmask = 1 << pin;
234 sc = device_get_softc(dev);
236 val = wiigpio_read(sc) & pinmask;
238 wiigpio_write(sc, wiigpio_read(sc) & ~pinmask);
240 wiigpio_write(sc, wiigpio_read(sc) | pinmask);
247 wiigpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
249 struct wiigpio_softc *sc;
251 if (pin >= WIIGPIO_NPINS)
253 sc = device_get_softc(dev);
255 sc->sc_pins[pin].gp_flags = flags;
262 wiigpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
264 struct wiigpio_softc *sc;
266 if (pin >= WIIGPIO_NPINS)
268 sc = device_get_softc(dev);
270 *flags = sc->sc_pins[pin].gp_flags;
277 wiigpio_pin_getname(device_t dev, uint32_t pin, char *name)
279 struct wiigpio_softc *sc;
281 if (pin >= WIIGPIO_NPINS)
283 sc = device_get_softc(dev);
285 memcpy(name, sc->sc_pins[pin].gp_name, GPIOMAXNAME);