2 * Copyright (c) 2015 Justin Hibbits
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 AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, 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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <machine/stdarg.h>
46 #include <dev/gpio/gpiobusvar.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
54 #define VALID_PIN(u) ((u) >= 0 && (u) <= MAXPIN)
56 #define GPIO_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
57 #define GPIO_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
58 #define GPIO_LOCK_INIT(sc) \
59 mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->dev), \
61 #define GPIO_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
63 #define GPIO_GPDIR 0x0
64 #define GPIO_GPODR 0x4
65 #define GPIO_GPDAT 0x8
66 #define GPIO_GPIER 0xc
67 #define GPIO_GPIMR 0x10
68 #define GPIO_GPICR 0x14
70 struct qoriq_gpio_softc {
74 struct resource *sc_mem; /* Memory resource */
78 qoriq_gpio_get_bus(device_t dev)
80 struct qoriq_gpio_softc *sc;
82 sc = device_get_softc(dev);
88 qoriq_gpio_pin_max(device_t dev, int *maxpin)
95 /* Get a specific pin's capabilities. */
97 qoriq_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
103 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN);
108 /* Get a specific pin's name. */
110 qoriq_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
116 snprintf(name, GPIOMAXNAME, "qoriq_gpio%d.%d",
117 device_get_unit(dev), pin);
118 name[GPIOMAXNAME-1] = '\0';
123 /* Set flags for the pin. */
125 qoriq_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
127 struct qoriq_gpio_softc *sc = device_get_softc(dev);
133 if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) ==
134 (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT))
138 if (flags & GPIO_PIN_INPUT) {
139 reg = bus_read_4(sc->sc_mem, GPIO_GPDIR);
140 reg &= ~(1 << (31 - pin));
141 bus_write_4(sc->sc_mem, GPIO_GPDIR, reg);
143 else if (flags & GPIO_PIN_OUTPUT) {
144 reg = bus_read_4(sc->sc_mem, GPIO_GPDIR);
145 reg |= (1 << (31 - pin));
146 bus_write_4(sc->sc_mem, GPIO_GPDIR, reg);
147 reg = bus_read_4(sc->sc_mem, GPIO_GPODR);
148 if (flags & GPIO_PIN_OPENDRAIN)
149 reg |= (1 << (31 - pin));
151 reg &= ~(1 << (31 - pin));
152 bus_write_4(sc->sc_mem, GPIO_GPODR, reg);
158 /* Set a specific output pin's value. */
160 qoriq_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
162 struct qoriq_gpio_softc *sc = device_get_softc(dev);
166 if (!VALID_PIN(pin) || value > 1)
172 outvals = bus_read_4(sc->sc_mem, GPIO_GPDAT);
173 outvals &= ~(1 << pinbit);
174 outvals |= (value << pinbit);
175 bus_write_4(sc->sc_mem, GPIO_GPDAT, outvals);
182 /* Get a specific pin's input value. */
184 qoriq_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
186 struct qoriq_gpio_softc *sc = device_get_softc(dev);
191 *value = (bus_read_4(sc->sc_mem, GPIO_GPDAT) >> (31 - pin)) & 1;
196 /* Toggle a pin's output value. */
198 qoriq_gpio_pin_toggle(device_t dev, uint32_t pin)
200 struct qoriq_gpio_softc *sc = device_get_softc(dev);
208 val = bus_read_4(sc->sc_mem, GPIO_GPDAT);
209 val ^= (1 << (31 - pin));
210 bus_write_4(sc->sc_mem, GPIO_GPDAT, val);
217 static struct ofw_compat_data gpio_matches[] = {
218 {"fsl,qoriq-gpio", 1},
220 {"fsl,mpc8572-gpio", 1},
225 qoriq_gpio_probe(device_t dev)
228 if (ofw_bus_search_compatible(dev, gpio_matches)->ocd_data == 0)
231 device_set_desc(dev, "Freescale QorIQ GPIO driver");
236 static int qoriq_gpio_detach(device_t dev);
239 qoriq_gpio_attach(device_t dev)
241 struct qoriq_gpio_softc *sc = device_get_softc(dev);
248 /* Allocate memory. */
250 sc->sc_mem = bus_alloc_resource_any(dev,
251 SYS_RES_MEMORY, &rid, RF_ACTIVE);
252 if (sc->sc_mem == NULL) {
253 device_printf(dev, "Can't allocate memory for device output port");
254 qoriq_gpio_detach(dev);
258 sc->busdev = gpiobus_attach_bus(dev);
259 if (sc->busdev == NULL) {
260 qoriq_gpio_detach(dev);
264 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
270 qoriq_gpio_detach(device_t dev)
272 struct qoriq_gpio_softc *sc = device_get_softc(dev);
274 gpiobus_detach_bus(dev);
276 if (sc->sc_mem != NULL) {
277 /* Release output port resource. */
278 bus_release_resource(dev, SYS_RES_MEMORY,
279 rman_get_rid(sc->sc_mem), sc->sc_mem);
282 GPIO_LOCK_DESTROY(sc);
287 static device_method_t qoriq_gpio_methods[] = {
289 DEVMETHOD(device_probe, qoriq_gpio_probe),
290 DEVMETHOD(device_attach, qoriq_gpio_attach),
291 DEVMETHOD(device_detach, qoriq_gpio_detach),
294 DEVMETHOD(gpio_get_bus, qoriq_gpio_get_bus),
295 DEVMETHOD(gpio_pin_max, qoriq_gpio_pin_max),
296 DEVMETHOD(gpio_pin_getname, qoriq_gpio_pin_getname),
297 DEVMETHOD(gpio_pin_getcaps, qoriq_gpio_pin_getcaps),
298 DEVMETHOD(gpio_pin_get, qoriq_gpio_pin_get),
299 DEVMETHOD(gpio_pin_set, qoriq_gpio_pin_set),
300 DEVMETHOD(gpio_pin_setflags, qoriq_gpio_pin_setflags),
301 DEVMETHOD(gpio_pin_toggle, qoriq_gpio_pin_toggle),
306 static driver_t qoriq_gpio_driver = {
309 sizeof(struct qoriq_gpio_softc),
311 static devclass_t qoriq_gpio_devclass;
313 EARLY_DRIVER_MODULE(qoriq_gpio, simplebus, qoriq_gpio_driver,
314 qoriq_gpio_devclass, NULL, NULL,
315 BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);