2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Alstom Group.
5 * Copyright (c) 2020 Semihalf.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * 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>
35 #include <sys/endian.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <dev/gpio/gpiobusvar.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <machine/bus.h>
58 #define DEFAULT_CAPS \
59 (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
60 GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL)
61 #define GPIO(n) (1 << (31 - (n)))
65 struct resource *mem_res;
68 /* software context */
73 struct gpio_pin setup[PIN_COUNT];
77 #define QORIQ_GPIO_LOCK(_sc) mtx_lock_spin(&(_sc)->mutex)
78 #define QORIQ_GPIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->mutex)
79 #define QORIQ_GPIO_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->mutex, MA_OWNED)
83 static bool qoriq_make_gpio_res(device_t, struct gpio_res*);
84 static uint32_t qoriq_gpio_reg_read(device_t, uint32_t);
85 static void qoriq_gpio_reg_write(device_t, uint32_t, uint32_t);
86 static void qoriq_gpio_set(device_t, uint32_t, uint32_t, uint32_t);
87 static int qoriq_gpio_configure(device_t, uint32_t, uint32_t);
90 static int qoriq_gpio_probe(device_t);
91 static int qoriq_gpio_attach(device_t);
92 static device_t qoriq_gpio_get_bus(device_t);
93 static int qoriq_gpio_pin_max(device_t, int*);
94 static int qoriq_gpio_pin_getname(device_t, uint32_t, char*);
95 static int qoriq_gpio_pin_getflags(device_t, uint32_t, uint32_t*);
96 static int qoriq_gpio_pin_setflags(device_t, uint32_t, uint32_t);
97 static int qoriq_gpio_pin_getcaps(device_t, uint32_t, uint32_t*);
98 static int qoriq_gpio_pin_get(device_t, uint32_t, uint32_t*);
99 static int qoriq_gpio_pin_set(device_t, uint32_t, uint32_t);
100 static int qoriq_gpio_pin_toggle(device_t, uint32_t);
101 static int qoriq_gpio_map_gpios(device_t, phandle_t, phandle_t,
102 int, pcell_t*, uint32_t*, uint32_t*);
103 static int qoriq_gpio_pin_access_32(device_t, uint32_t, uint32_t, uint32_t,
105 static int qoriq_gpio_pin_config_32(device_t, uint32_t, uint32_t, uint32_t*);
107 static device_method_t qoriq_gpio_methods[] = {
108 DEVMETHOD(device_probe, qoriq_gpio_probe),
109 DEVMETHOD(device_attach, qoriq_gpio_attach),
112 DEVMETHOD(gpio_get_bus, qoriq_gpio_get_bus),
113 DEVMETHOD(gpio_pin_max, qoriq_gpio_pin_max),
114 DEVMETHOD(gpio_pin_getname, qoriq_gpio_pin_getname),
115 DEVMETHOD(gpio_pin_getflags, qoriq_gpio_pin_getflags),
116 DEVMETHOD(gpio_pin_setflags, qoriq_gpio_pin_setflags),
117 DEVMETHOD(gpio_pin_getcaps, qoriq_gpio_pin_getcaps),
118 DEVMETHOD(gpio_pin_get, qoriq_gpio_pin_get),
119 DEVMETHOD(gpio_pin_set, qoriq_gpio_pin_set),
120 DEVMETHOD(gpio_pin_toggle, qoriq_gpio_pin_toggle),
121 DEVMETHOD(gpio_map_gpios, qoriq_gpio_map_gpios),
122 DEVMETHOD(gpio_pin_access_32, qoriq_gpio_pin_access_32),
123 DEVMETHOD(gpio_pin_config_32, qoriq_gpio_pin_config_32),
128 static driver_t gpio_driver = {
131 sizeof(struct gpio_softc),
134 static devclass_t gpio_devclass;
136 DRIVER_MODULE(gpio, simplebus, gpio_driver, gpio_devclass, 0, 0);
137 MODULE_VERSION(gpio, 1);
143 qoriq_make_gpio_res(device_t dev, struct gpio_res *out)
146 out->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
147 &out->mem_rid, RF_ACTIVE | RF_SHAREABLE);
149 return (out->mem_res == NULL);
153 qoriq_gpio_reg_read(device_t dev, uint32_t reg)
155 struct gpio_softc *sc = device_get_softc(dev);
158 QORIQ_GPIO_ASSERT_LOCKED(sc);
159 result = bus_read_4(sc->res.mem_res, reg);
161 return be32toh(result);
165 qoriq_gpio_reg_write(device_t dev, uint32_t reg, uint32_t val)
167 struct gpio_softc *sc = device_get_softc(dev);
169 QORIQ_GPIO_ASSERT_LOCKED(sc);
172 bus_write_4(sc->res.mem_res, reg, val);
173 bus_barrier(sc->res.mem_res, reg, 4, BUS_SPACE_BARRIER_READ |
174 BUS_SPACE_BARRIER_WRITE);
178 qoriq_gpio_set(device_t dev, uint32_t reg, uint32_t pin, uint32_t set)
183 val = (qoriq_gpio_reg_read(dev, reg) & ~(1U << pin)) | (set << pin);
184 qoriq_gpio_reg_write(dev, reg, val);
188 qoriq_gpio_configure(device_t dev, uint32_t pin, uint32_t flags)
190 struct gpio_softc *sc = device_get_softc(dev);
193 if (pin >= PIN_COUNT)
197 * Pin cannot function as input and output at the same time.
198 * The same applies to open-drain and push-pull functionality.
200 if (((flags & GPIO_PIN_INPUT) && (flags & GPIO_PIN_OUTPUT)) ||
201 ((flags & GPIO_PIN_OPENDRAIN) && (flags & GPIO_PIN_PUSHPULL)))
204 QORIQ_GPIO_ASSERT_LOCKED(sc);
206 if (flags & GPIO_PIN_INPUT) {
207 newflags = GPIO_PIN_INPUT;
208 qoriq_gpio_set(dev, DIRECTION, pin, 0);
211 if (flags & GPIO_PIN_OUTPUT) {
212 newflags = GPIO_PIN_OUTPUT;
213 qoriq_gpio_set(dev, DIRECTION, pin, 1);
215 if (flags & GPIO_PIN_OPENDRAIN) {
216 newflags |= GPIO_PIN_OPENDRAIN;
217 qoriq_gpio_set(dev, OPEN_DRAIN, pin, 1);
219 newflags |= GPIO_PIN_PUSHPULL;
220 qoriq_gpio_set(dev, OPEN_DRAIN, pin, 0);
224 sc->setup[pin].gp_flags = newflags;
231 qoriq_gpio_probe(device_t dev)
234 if (!ofw_bus_status_okay(dev))
237 if (!ofw_bus_is_compatible(dev, "fsl,qoriq-gpio"))
240 device_set_desc(dev, "Integrated GPIO Controller");
242 return (BUS_PROBE_DEFAULT);
246 qoriq_gpio_attach(device_t dev)
248 struct gpio_softc *sc = device_get_softc(dev);
251 if (qoriq_make_gpio_res(dev, &sc->res))
254 for (i = 0; i < PIN_COUNT; i++)
255 sc->setup[i].gp_caps = DEFAULT_CAPS;
259 sc->busdev = gpiobus_attach_bus(dev);
260 if (sc->busdev == NULL)
267 qoriq_gpio_get_bus(device_t dev)
269 struct gpio_softc *softc = device_get_softc(dev);
271 return (softc->busdev);
275 qoriq_gpio_pin_max(device_t dev, int *maxpin)
281 *maxpin = PIN_COUNT - 1;
287 qoriq_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
290 if (name == NULL || pin >= PIN_COUNT)
293 snprintf(name, GPIOMAXNAME, "pin %d", pin);
299 qoriq_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *pflags)
301 struct gpio_softc *sc = device_get_softc(dev);
303 if (pflags == NULL || pin >= PIN_COUNT)
307 *pflags = sc->setup[pin].gp_flags;
308 QORIQ_GPIO_UNLOCK(sc);
314 qoriq_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
316 struct gpio_softc *sc = device_get_softc(dev);
319 if (pin >= PIN_COUNT)
322 /* Check for unwanted flags. */
325 if ((flags & sc->setup[pin].gp_caps) != flags) {
326 QORIQ_GPIO_UNLOCK(sc);
330 ret = qoriq_gpio_configure(dev, pin, flags);
332 QORIQ_GPIO_UNLOCK(sc);
338 qoriq_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
340 struct gpio_softc *sc = device_get_softc(dev);
342 if (caps == NULL || pin >= PIN_COUNT)
346 *caps = sc->setup[pin].gp_caps;
347 QORIQ_GPIO_UNLOCK(sc);
353 qoriq_gpio_pin_get(device_t dev, uint32_t pin, uint32_t *value)
355 struct gpio_softc *sc = device_get_softc(dev);
357 if (value == NULL || pin >= PIN_COUNT)
361 *value = (qoriq_gpio_reg_read(dev, DATA) & GPIO(pin)) != 0;
362 QORIQ_GPIO_UNLOCK(sc);
368 qoriq_gpio_pin_set(device_t dev, uint32_t pin, uint32_t value)
370 struct gpio_softc *sc = device_get_softc(dev);
372 if (pin >= PIN_COUNT)
376 qoriq_gpio_set(dev, DATA, pin, value);
377 QORIQ_GPIO_UNLOCK(sc);
383 qoriq_gpio_pin_toggle(device_t dev, uint32_t pin)
385 struct gpio_softc *sc;
388 if (pin >= PIN_COUNT)
391 sc = device_get_softc(dev);
394 value = qoriq_gpio_reg_read(dev, DATA) ^ (1 << pin);
395 qoriq_gpio_reg_write(dev, DATA, value);
396 QORIQ_GPIO_UNLOCK(sc);
402 qoriq_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, int gcells,
403 pcell_t *gpios, uint32_t *pin, uint32_t *flags)
405 struct gpio_softc *sc = device_get_softc(bus);
408 if (gpios[0] >= PIN_COUNT)
412 err = qoriq_gpio_configure(bus, gpios[0], gpios[1]);
413 QORIQ_GPIO_UNLOCK(sc);
425 qoriq_gpio_pin_access_32(device_t dev, uint32_t first_pin, uint32_t clear_pins,
426 uint32_t change_pins, uint32_t *orig_pins)
428 struct gpio_softc *sc;
431 sc = device_get_softc(dev);
437 hwstate = qoriq_gpio_reg_read(dev, DATA);
438 qoriq_gpio_reg_write(dev, DATA, (hwstate & ~clear_pins) ^ change_pins);
439 QORIQ_GPIO_UNLOCK(sc);
441 if (orig_pins != NULL)
442 *orig_pins = hwstate;
448 qoriq_gpio_pin_config_32(device_t dev, uint32_t first_pin, uint32_t num_pins,
451 uint32_t dir, odr, mask, reg;
452 struct gpio_softc *sc;
453 uint32_t newflags[32];
456 if (first_pin != 0 || num_pins > PIN_COUNT)
459 sc = device_get_softc(dev);
461 dir = odr = mask = 0;
463 for (i = 0; i < num_pins; i++) {
467 if (pin_flags[i] & GPIO_PIN_INPUT) {
468 newflags[i] = GPIO_PIN_INPUT;
471 newflags[i] = GPIO_PIN_OUTPUT;
474 if (pin_flags[i] & GPIO_PIN_OPENDRAIN) {
475 newflags[i] |= GPIO_PIN_OPENDRAIN;
478 newflags[i] |= GPIO_PIN_PUSHPULL;
486 reg = (qoriq_gpio_reg_read(dev, DIRECTION) & ~mask) | dir;
487 qoriq_gpio_reg_write(dev, DIRECTION, reg);
489 reg = (qoriq_gpio_reg_read(dev, OPEN_DRAIN) & ~mask) | odr;
490 qoriq_gpio_reg_write(dev, OPEN_DRAIN, reg);
492 for (i = 0; i < num_pins; i++)
493 sc->setup[i].gp_flags = newflags[i];
495 QORIQ_GPIO_UNLOCK(sc);