2 * Copyright (c) 2015 Justin Hibbits
3 * Copyright (c) 2013 Thomas Skibo
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/resource.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <machine/stdarg.h>
49 #include <dev/fdt/fdt_common.h>
50 #include <dev/gpio/gpiobusvar.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
58 #define VALID_PIN(u) ((u) >= 0 && (u) <= MAXPIN)
60 #define GPIO_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
61 #define GPIO_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
62 #define GPIO_LOCK_INIT(sc) \
63 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
65 #define GPIO_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
67 struct mpc85xx_gpio_softc {
71 struct resource *out_res; /* Memory resource */
72 struct resource *in_res;
76 mpc85xx_gpio_get_bus(device_t dev)
78 struct mpc85xx_gpio_softc *sc;
80 sc = device_get_softc(dev);
86 mpc85xx_gpio_pin_max(device_t dev, int *maxpin)
93 /* Get a specific pin's capabilities. */
95 mpc85xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
101 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
106 /* Get a specific pin's name. */
108 mpc85xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
114 snprintf(name, GPIOMAXNAME, "GPIO%d", pin);
115 name[GPIOMAXNAME-1] = '\0';
120 /* Set a specific output pin's value. */
122 mpc85xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
124 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
128 if (!VALID_PIN(pin) || value > 1)
134 outvals = bus_read_4(sc->out_res, 0);
135 outvals &= ~(1 << pinbit);
136 outvals |= (value << pinbit);
137 bus_write_4(sc->out_res, 0, outvals);
144 /* Get a specific pin's input value. */
146 mpc85xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
148 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
153 *value = (bus_read_4(sc->in_res, 0) >> (31 - pin)) & 1;
158 /* Toggle a pin's output value. */
160 mpc85xx_gpio_pin_toggle(device_t dev, uint32_t pin)
162 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
170 val = bus_read_4(sc->out_res, 0);
171 val ^= (1 << (31 - pin));
172 bus_write_4(sc->out_res, 0, val);
180 mpc85xx_gpio_probe(device_t dev)
184 if (!ofw_bus_status_okay(dev))
187 if (!ofw_bus_is_compatible(dev, "gpio"))
190 svr = mfspr(SPR_SVR);
191 switch (SVR_VER(svr)) {
199 device_set_desc(dev, "MPC85xx GPIO driver");
203 static int mpc85xx_gpio_detach(device_t dev);
206 mpc85xx_gpio_attach(device_t dev)
208 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
215 /* Allocate memory. */
217 sc->out_res = bus_alloc_resource_any(dev,
218 SYS_RES_MEMORY, &rid, RF_ACTIVE);
219 if (sc->out_res == NULL) {
220 device_printf(dev, "Can't allocate memory for device output port");
221 mpc85xx_gpio_detach(dev);
226 sc->in_res = bus_alloc_resource_any(dev,
227 SYS_RES_MEMORY, &rid, RF_ACTIVE);
228 if (sc->in_res == NULL) {
229 device_printf(dev, "Can't allocate memory for device input port");
230 mpc85xx_gpio_detach(dev);
234 sc->busdev = gpiobus_attach_bus(dev);
235 if (sc->busdev == NULL) {
236 mpc85xx_gpio_detach(dev);
240 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
246 mpc85xx_gpio_detach(device_t dev)
248 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
250 gpiobus_detach_bus(dev);
252 if (sc->out_res != NULL) {
253 /* Release output port resource. */
254 bus_release_resource(dev, SYS_RES_MEMORY,
255 rman_get_rid(sc->out_res), sc->out_res);
258 if (sc->in_res != NULL) {
259 /* Release input port resource. */
260 bus_release_resource(dev, SYS_RES_MEMORY,
261 rman_get_rid(sc->in_res), sc->in_res);
264 GPIO_LOCK_DESTROY(sc);
269 static device_method_t mpc85xx_gpio_methods[] = {
271 DEVMETHOD(device_probe, mpc85xx_gpio_probe),
272 DEVMETHOD(device_attach, mpc85xx_gpio_attach),
273 DEVMETHOD(device_detach, mpc85xx_gpio_detach),
276 DEVMETHOD(gpio_get_bus, mpc85xx_gpio_get_bus),
277 DEVMETHOD(gpio_pin_max, mpc85xx_gpio_pin_max),
278 DEVMETHOD(gpio_pin_getname, mpc85xx_gpio_pin_getname),
279 DEVMETHOD(gpio_pin_getcaps, mpc85xx_gpio_pin_getcaps),
280 DEVMETHOD(gpio_pin_get, mpc85xx_gpio_pin_get),
281 DEVMETHOD(gpio_pin_set, mpc85xx_gpio_pin_set),
282 DEVMETHOD(gpio_pin_toggle, mpc85xx_gpio_pin_toggle),
287 static driver_t mpc85xx_gpio_driver = {
289 mpc85xx_gpio_methods,
290 sizeof(struct mpc85xx_gpio_softc),
292 static devclass_t mpc85xx_gpio_devclass;
294 EARLY_DRIVER_MODULE(mpc85xx_gpio, simplebus, mpc85xx_gpio_driver,
295 mpc85xx_gpio_devclass, NULL, NULL,
296 BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);