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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/resource.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <machine/stdarg.h>
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/gpio/gpiobusvar.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
56 #define VALID_PIN(u) ((u) >= 0 && (u) <= MAXPIN)
58 #define GPIO_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
59 #define GPIO_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
60 #define GPIO_LOCK_INIT(sc) \
61 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
63 #define GPIO_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
65 struct mpc85xx_gpio_softc {
69 struct resource *out_res; /* Memory resource */
70 struct resource *in_res;
74 mpc85xx_gpio_get_bus(device_t dev)
76 struct mpc85xx_gpio_softc *sc;
78 sc = device_get_softc(dev);
84 mpc85xx_gpio_pin_max(device_t dev, int *maxpin)
91 /* Get a specific pin's capabilities. */
93 mpc85xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
99 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
104 /* Get a specific pin's name. */
106 mpc85xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
112 snprintf(name, GPIOMAXNAME, "GPIO%d", pin);
113 name[GPIOMAXNAME-1] = '\0';
118 /* Set a specific output pin's value. */
120 mpc85xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
122 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
126 if (!VALID_PIN(pin) || value > 1)
132 outvals = bus_read_4(sc->out_res, 0);
133 outvals &= ~(1 << pinbit);
134 outvals |= (value << pinbit);
135 bus_write_4(sc->out_res, 0, outvals);
142 /* Get a specific pin's input value. */
144 mpc85xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
146 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
151 *value = (bus_read_4(sc->in_res, 0) >> (31 - pin)) & 1;
156 /* Toggle a pin's output value. */
158 mpc85xx_gpio_pin_toggle(device_t dev, uint32_t pin)
160 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
168 val = bus_read_4(sc->out_res, 0);
169 val ^= (1 << (31 - pin));
170 bus_write_4(sc->out_res, 0, val);
178 mpc85xx_gpio_probe(device_t dev)
182 if (!ofw_bus_status_okay(dev))
185 if (!ofw_bus_is_compatible(dev, "gpio"))
188 svr = mfspr(SPR_SVR);
189 switch (SVR_VER(svr)) {
197 device_set_desc(dev, "MPC85xx GPIO driver");
201 static int mpc85xx_gpio_detach(device_t dev);
204 mpc85xx_gpio_attach(device_t dev)
206 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
213 /* Allocate memory. */
215 sc->out_res = bus_alloc_resource_any(dev,
216 SYS_RES_MEMORY, &rid, RF_ACTIVE);
217 if (sc->out_res == NULL) {
218 device_printf(dev, "Can't allocate memory for device output port");
219 mpc85xx_gpio_detach(dev);
224 sc->in_res = bus_alloc_resource_any(dev,
225 SYS_RES_MEMORY, &rid, RF_ACTIVE);
226 if (sc->in_res == NULL) {
227 device_printf(dev, "Can't allocate memory for device input port");
228 mpc85xx_gpio_detach(dev);
232 sc->busdev = gpiobus_attach_bus(dev);
233 if (sc->busdev == NULL) {
234 mpc85xx_gpio_detach(dev);
238 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
244 mpc85xx_gpio_detach(device_t dev)
246 struct mpc85xx_gpio_softc *sc = device_get_softc(dev);
248 gpiobus_detach_bus(dev);
250 if (sc->out_res != NULL) {
251 /* Release output port resource. */
252 bus_release_resource(dev, SYS_RES_MEMORY,
253 rman_get_rid(sc->out_res), sc->out_res);
256 if (sc->in_res != NULL) {
257 /* Release input port resource. */
258 bus_release_resource(dev, SYS_RES_MEMORY,
259 rman_get_rid(sc->in_res), sc->in_res);
262 GPIO_LOCK_DESTROY(sc);
267 static device_method_t mpc85xx_gpio_methods[] = {
269 DEVMETHOD(device_probe, mpc85xx_gpio_probe),
270 DEVMETHOD(device_attach, mpc85xx_gpio_attach),
271 DEVMETHOD(device_detach, mpc85xx_gpio_detach),
274 DEVMETHOD(gpio_get_bus, mpc85xx_gpio_get_bus),
275 DEVMETHOD(gpio_pin_max, mpc85xx_gpio_pin_max),
276 DEVMETHOD(gpio_pin_getname, mpc85xx_gpio_pin_getname),
277 DEVMETHOD(gpio_pin_getcaps, mpc85xx_gpio_pin_getcaps),
278 DEVMETHOD(gpio_pin_get, mpc85xx_gpio_pin_get),
279 DEVMETHOD(gpio_pin_set, mpc85xx_gpio_pin_set),
280 DEVMETHOD(gpio_pin_toggle, mpc85xx_gpio_pin_toggle),
285 static driver_t mpc85xx_gpio_driver = {
287 mpc85xx_gpio_methods,
288 sizeof(struct mpc85xx_gpio_softc),
291 EARLY_DRIVER_MODULE(mpc85xx_gpio, simplebus, mpc85xx_gpio_driver, NULL, NULL,
292 BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);