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
71 struct qoriq_gpio_softc {
75 struct resource *sc_mem; /* Memory resource */
79 qoriq_gpio_get_bus(device_t dev)
81 struct qoriq_gpio_softc *sc;
83 sc = device_get_softc(dev);
89 qoriq_gpio_pin_max(device_t dev, int *maxpin)
96 /* Get a specific pin's capabilities. */
98 qoriq_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
104 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN);
109 /* Get a specific pin's name. */
111 qoriq_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
117 snprintf(name, GPIOMAXNAME, "qoriq_gpio%d.%d",
118 device_get_unit(dev), pin);
119 name[GPIOMAXNAME-1] = '\0';
124 /* Set flags for the pin. */
126 qoriq_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
128 struct qoriq_gpio_softc *sc = device_get_softc(dev);
134 if ((flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) ==
135 (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT))
139 if (flags & GPIO_PIN_INPUT) {
140 reg = bus_read_4(sc->sc_mem, GPIO_GPDIR);
141 reg &= ~(1 << (31 - pin));
142 bus_write_4(sc->sc_mem, GPIO_GPDIR, reg);
144 else if (flags & GPIO_PIN_OUTPUT) {
145 reg = bus_read_4(sc->sc_mem, GPIO_GPDIR);
146 reg |= (1 << (31 - pin));
147 bus_write_4(sc->sc_mem, GPIO_GPDIR, reg);
148 reg = bus_read_4(sc->sc_mem, GPIO_GPODR);
149 if (flags & GPIO_PIN_OPENDRAIN)
150 reg |= (1 << (31 - pin));
152 reg &= ~(1 << (31 - pin));
153 bus_write_4(sc->sc_mem, GPIO_GPODR, reg);
159 /* Set a specific output pin's value. */
161 qoriq_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
163 struct qoriq_gpio_softc *sc = device_get_softc(dev);
167 if (!VALID_PIN(pin) || value > 1)
173 outvals = bus_read_4(sc->sc_mem, GPIO_GPDAT);
174 outvals &= ~(1 << pinbit);
175 outvals |= (value << pinbit);
176 bus_write_4(sc->sc_mem, GPIO_GPDAT, outvals);
183 /* Get a specific pin's input value. */
185 qoriq_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
187 struct qoriq_gpio_softc *sc = device_get_softc(dev);
192 *value = (bus_read_4(sc->sc_mem, GPIO_GPDAT) >> (31 - pin)) & 1;
197 /* Toggle a pin's output value. */
199 qoriq_gpio_pin_toggle(device_t dev, uint32_t pin)
201 struct qoriq_gpio_softc *sc = device_get_softc(dev);
209 val = bus_read_4(sc->sc_mem, GPIO_GPDAT);
210 val ^= (1 << (31 - pin));
211 bus_write_4(sc->sc_mem, GPIO_GPDAT, val);
218 static struct ofw_compat_data gpio_matches[] = {
219 {"fsl,qoriq-gpio", 1},
221 {"fsl,mpc8572-gpio", 1},
226 qoriq_gpio_probe(device_t dev)
229 if (ofw_bus_search_compatible(dev, gpio_matches)->ocd_data == 0)
232 device_set_desc(dev, "Freescale QorIQ GPIO driver");
237 static int qoriq_gpio_detach(device_t dev);
240 qoriq_gpio_attach(device_t dev)
242 struct qoriq_gpio_softc *sc = device_get_softc(dev);
249 /* Allocate memory. */
251 sc->sc_mem = bus_alloc_resource_any(dev,
252 SYS_RES_MEMORY, &rid, RF_ACTIVE);
253 if (sc->sc_mem == NULL) {
254 device_printf(dev, "Can't allocate memory for device output port");
255 qoriq_gpio_detach(dev);
259 sc->busdev = gpiobus_attach_bus(dev);
260 if (sc->busdev == NULL) {
261 qoriq_gpio_detach(dev);
265 OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
271 qoriq_gpio_detach(device_t dev)
273 struct qoriq_gpio_softc *sc = device_get_softc(dev);
275 gpiobus_detach_bus(dev);
277 if (sc->sc_mem != NULL) {
278 /* Release output port resource. */
279 bus_release_resource(dev, SYS_RES_MEMORY,
280 rman_get_rid(sc->sc_mem), sc->sc_mem);
283 GPIO_LOCK_DESTROY(sc);
288 static device_method_t qoriq_gpio_methods[] = {
290 DEVMETHOD(device_probe, qoriq_gpio_probe),
291 DEVMETHOD(device_attach, qoriq_gpio_attach),
292 DEVMETHOD(device_detach, qoriq_gpio_detach),
295 DEVMETHOD(gpio_get_bus, qoriq_gpio_get_bus),
296 DEVMETHOD(gpio_pin_max, qoriq_gpio_pin_max),
297 DEVMETHOD(gpio_pin_getname, qoriq_gpio_pin_getname),
298 DEVMETHOD(gpio_pin_getcaps, qoriq_gpio_pin_getcaps),
299 DEVMETHOD(gpio_pin_get, qoriq_gpio_pin_get),
300 DEVMETHOD(gpio_pin_set, qoriq_gpio_pin_set),
301 DEVMETHOD(gpio_pin_setflags, qoriq_gpio_pin_setflags),
302 DEVMETHOD(gpio_pin_toggle, qoriq_gpio_pin_toggle),
307 static driver_t qoriq_gpio_driver = {
310 sizeof(struct qoriq_gpio_softc),
312 static devclass_t qoriq_gpio_devclass;
314 EARLY_DRIVER_MODULE(qoriq_gpio, simplebus, qoriq_gpio_driver,
315 qoriq_gpio_devclass, NULL, NULL,
316 BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);