2010-09-28 03:24:53 +00:00
|
|
|
/*-
|
2017-11-27 14:52:40 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2010-09-28 03:24:53 +00:00
|
|
|
* Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2010-09-29 20:53:33 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2010-09-28 03:24:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2014-05-03 20:33:00 +00:00
|
|
|
#include <sys/systm.h>
|
2014-05-01 14:09:47 +00:00
|
|
|
#include <sys/bus.h>
|
2014-11-18 17:22:08 +00:00
|
|
|
#include <sys/gpio.h>
|
2016-08-19 10:52:39 +00:00
|
|
|
#ifdef INTRNG
|
2016-04-28 12:03:22 +00:00
|
|
|
#include <sys/intr.h>
|
2016-08-19 10:52:39 +00:00
|
|
|
#endif
|
2014-05-01 14:09:47 +00:00
|
|
|
#include <sys/kernel.h>
|
2010-09-28 03:24:53 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/module.h>
|
2021-06-23 02:27:57 +00:00
|
|
|
#include <sys/sbuf.h>
|
2010-09-28 03:24:53 +00:00
|
|
|
|
|
|
|
#include <dev/gpio/gpiobusvar.h>
|
2014-05-01 14:09:47 +00:00
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
#include "gpiobus_if.h"
|
|
|
|
|
2014-11-18 01:38:39 +00:00
|
|
|
#undef GPIOBUS_DEBUG
|
|
|
|
#ifdef GPIOBUS_DEBUG
|
|
|
|
#define dprintf printf
|
|
|
|
#else
|
|
|
|
#define dprintf(x, arg...)
|
|
|
|
#endif
|
|
|
|
|
2021-06-23 02:27:57 +00:00
|
|
|
static void gpiobus_print_pins(struct gpiobus_ivar *, struct sbuf *);
|
2010-09-28 03:24:53 +00:00
|
|
|
static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int);
|
|
|
|
static int gpiobus_probe(device_t);
|
|
|
|
static int gpiobus_attach(device_t);
|
|
|
|
static int gpiobus_detach(device_t);
|
|
|
|
static int gpiobus_suspend(device_t);
|
|
|
|
static int gpiobus_resume(device_t);
|
2015-03-05 01:49:58 +00:00
|
|
|
static void gpiobus_probe_nomatch(device_t, device_t);
|
2010-09-28 03:24:53 +00:00
|
|
|
static int gpiobus_print_child(device_t, device_t);
|
2021-06-23 02:27:57 +00:00
|
|
|
static int gpiobus_child_location(device_t, device_t, struct sbuf *);
|
2010-09-28 03:24:53 +00:00
|
|
|
static device_t gpiobus_add_child(device_t, u_int, const char *, int);
|
|
|
|
static void gpiobus_hinted_child(device_t, const char *, int);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GPIOBUS interface
|
|
|
|
*/
|
2014-10-31 19:15:14 +00:00
|
|
|
static int gpiobus_acquire_bus(device_t, device_t, int);
|
2010-09-28 03:24:53 +00:00
|
|
|
static void gpiobus_release_bus(device_t, device_t);
|
|
|
|
static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t);
|
|
|
|
static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*);
|
|
|
|
static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*);
|
|
|
|
static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int);
|
|
|
|
static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*);
|
|
|
|
static int gpiobus_pin_toggle(device_t, device_t, uint32_t);
|
|
|
|
|
2019-12-02 19:57:20 +00:00
|
|
|
/*
|
|
|
|
* gpiobus_pin flags
|
|
|
|
* The flags in struct gpiobus_pin are not related to the flags used by the
|
|
|
|
* low-level controller driver in struct gpio_pin. Currently, only pins
|
|
|
|
* acquired via FDT data have gpiobus_pin.flags set, sourced from the flags in
|
|
|
|
* the FDT properties. In theory, these flags are defined per-platform. In
|
|
|
|
* practice they are always the flags from the dt-bindings/gpio/gpio.h file.
|
|
|
|
* The only one of those flags we currently support is for handling active-low
|
|
|
|
* pins, so we just define that flag here instead of including a GPL'd header.
|
|
|
|
*/
|
|
|
|
#define GPIO_ACTIVE_LOW 1
|
2019-12-01 21:27:09 +00:00
|
|
|
|
2016-04-28 12:03:22 +00:00
|
|
|
/*
|
|
|
|
* XXX -> Move me to better place - gpio_subr.c?
|
|
|
|
* Also, this function must be changed when interrupt configuration
|
|
|
|
* data will be moved into struct resource.
|
|
|
|
*/
|
|
|
|
#ifdef INTRNG
|
2016-06-07 05:08:24 +00:00
|
|
|
|
2016-04-28 12:03:22 +00:00
|
|
|
struct resource *
|
|
|
|
gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags,
|
|
|
|
gpio_pin_t pin, uint32_t intr_mode)
|
|
|
|
{
|
2016-06-07 05:08:24 +00:00
|
|
|
u_int irq;
|
|
|
|
struct intr_map_data_gpio *gpio_data;
|
|
|
|
struct resource *res;
|
|
|
|
|
2016-08-19 10:52:39 +00:00
|
|
|
gpio_data = (struct intr_map_data_gpio *)intr_alloc_map_data(
|
|
|
|
INTR_MAP_DATA_GPIO, sizeof(*gpio_data), M_WAITOK | M_ZERO);
|
2016-06-07 05:08:24 +00:00
|
|
|
gpio_data->gpio_pin_num = pin->pin;
|
|
|
|
gpio_data->gpio_pin_flags = pin->flags;
|
|
|
|
gpio_data->gpio_intr_mode = intr_mode;
|
2016-04-28 12:03:22 +00:00
|
|
|
|
2016-08-19 10:52:39 +00:00
|
|
|
irq = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data);
|
2016-06-07 05:08:24 +00:00
|
|
|
res = bus_alloc_resource(consumer_dev, SYS_RES_IRQ, rid, irq, irq, 1,
|
|
|
|
alloc_flags);
|
|
|
|
if (res == NULL) {
|
2016-08-19 10:52:39 +00:00
|
|
|
intr_free_intr_map_data((struct intr_map_data *)gpio_data);
|
2016-06-07 05:08:24 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
rman_set_virtual(res, gpio_data);
|
|
|
|
return (res);
|
2016-04-28 12:03:22 +00:00
|
|
|
}
|
2016-05-27 20:41:25 +00:00
|
|
|
#else
|
|
|
|
struct resource *
|
|
|
|
gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags,
|
|
|
|
gpio_pin_t pin, uint32_t intr_mode)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
2016-04-28 12:03:22 +00:00
|
|
|
#endif
|
|
|
|
|
2014-11-18 17:22:08 +00:00
|
|
|
int
|
|
|
|
gpio_check_flags(uint32_t caps, uint32_t flags)
|
|
|
|
{
|
|
|
|
|
2016-12-13 03:36:41 +00:00
|
|
|
/* Filter unwanted flags. */
|
|
|
|
flags &= caps;
|
|
|
|
|
2014-11-18 17:22:08 +00:00
|
|
|
/* Cannot mix input/output together. */
|
|
|
|
if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT)
|
|
|
|
return (EINVAL);
|
|
|
|
/* Cannot mix pull-up/pull-down together. */
|
|
|
|
if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN)
|
|
|
|
return (EINVAL);
|
2020-12-12 18:34:15 +00:00
|
|
|
/* Cannot mix output and interrupt flags together */
|
|
|
|
if (flags & GPIO_PIN_OUTPUT && flags & GPIO_INTR_MASK)
|
|
|
|
return (EINVAL);
|
|
|
|
/* Only one interrupt flag can be defined at once */
|
|
|
|
if ((flags & GPIO_INTR_MASK) & ((flags & GPIO_INTR_MASK) - 1))
|
|
|
|
return (EINVAL);
|
|
|
|
/* The interrupt attached flag cannot be set */
|
|
|
|
if (flags & GPIO_INTR_ATTACHED)
|
|
|
|
return (EINVAL);
|
2014-11-18 17:22:08 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:27:09 +00:00
|
|
|
int
|
|
|
|
gpio_pin_get_by_bus_pinnum(device_t busdev, uint32_t pinnum, gpio_pin_t *ppin)
|
|
|
|
{
|
|
|
|
gpio_pin_t pin;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = gpiobus_acquire_pin(busdev, pinnum);
|
|
|
|
if (err != 0)
|
|
|
|
return (EBUSY);
|
|
|
|
|
|
|
|
pin = malloc(sizeof(*pin), M_DEVBUF, M_WAITOK | M_ZERO);
|
|
|
|
|
|
|
|
pin->dev = device_get_parent(busdev);
|
|
|
|
pin->pin = pinnum;
|
2019-12-02 19:57:20 +00:00
|
|
|
pin->flags = 0;
|
2019-12-01 21:27:09 +00:00
|
|
|
|
|
|
|
*ppin = pin;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gpio_pin_get_by_child_index(device_t childdev, uint32_t idx, gpio_pin_t *ppin)
|
|
|
|
{
|
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
|
|
|
|
devi = GPIOBUS_IVAR(childdev);
|
|
|
|
if (idx >= devi->npins)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return (gpio_pin_get_by_bus_pinnum(device_get_parent(childdev),
|
|
|
|
devi->pins[idx], ppin));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(pin != NULL, ("GPIO pin is NULL."));
|
|
|
|
KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
|
|
|
|
return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gpio_pin_is_active(gpio_pin_t pin, bool *active)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
uint32_t tmp;
|
|
|
|
|
|
|
|
KASSERT(pin != NULL, ("GPIO pin is NULL."));
|
|
|
|
KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
|
|
|
|
rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp);
|
|
|
|
if (rv != 0) {
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pin->flags & GPIO_ACTIVE_LOW)
|
|
|
|
*active = tmp == 0;
|
|
|
|
else
|
|
|
|
*active = tmp != 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gpio_pin_release(gpio_pin_t gpio)
|
|
|
|
{
|
|
|
|
device_t busdev;
|
|
|
|
|
|
|
|
if (gpio == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
KASSERT(gpio->dev != NULL, ("GPIO pin device is NULL."));
|
|
|
|
|
|
|
|
busdev = GPIO_GET_BUS(gpio->dev);
|
|
|
|
if (busdev != NULL)
|
|
|
|
gpiobus_release_pin(busdev, gpio->pin);
|
|
|
|
|
|
|
|
free(gpio, M_DEVBUF);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gpio_pin_set_active(gpio_pin_t pin, bool active)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
uint32_t tmp;
|
|
|
|
|
|
|
|
if (pin->flags & GPIO_ACTIVE_LOW)
|
|
|
|
tmp = active ? 0 : 1;
|
|
|
|
else
|
|
|
|
tmp = active ? 1 : 0;
|
|
|
|
|
|
|
|
KASSERT(pin != NULL, ("GPIO pin is NULL."));
|
|
|
|
KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
|
|
|
|
rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp);
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gpio_pin_setflags(gpio_pin_t pin, uint32_t flags)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
KASSERT(pin != NULL, ("GPIO pin is NULL."));
|
|
|
|
KASSERT(pin->dev != NULL, ("GPIO pin device is NULL."));
|
|
|
|
|
|
|
|
rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags);
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
2014-11-18 02:41:35 +00:00
|
|
|
static void
|
2021-06-23 02:27:57 +00:00
|
|
|
gpiobus_print_pins(struct gpiobus_ivar *devi, struct sbuf *sb)
|
2010-09-28 03:24:53 +00:00
|
|
|
{
|
2014-11-18 02:41:35 +00:00
|
|
|
int i, range_start, range_stop, need_coma;
|
2010-09-28 03:24:53 +00:00
|
|
|
|
|
|
|
if (devi->npins == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
need_coma = 0;
|
|
|
|
range_start = range_stop = devi->pins[0];
|
|
|
|
for (i = 1; i < devi->npins; i++) {
|
|
|
|
if (devi->pins[i] != (range_stop + 1)) {
|
|
|
|
if (need_coma)
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_cat(sb, ",");
|
2010-09-28 03:24:53 +00:00
|
|
|
if (range_start != range_stop)
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_printf(sb, "%d-%d", range_start, range_stop);
|
2010-09-28 03:24:53 +00:00
|
|
|
else
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_printf(sb, "%d", range_start);
|
2010-09-28 03:24:53 +00:00
|
|
|
range_start = range_stop = devi->pins[i];
|
|
|
|
need_coma = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
range_stop++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_coma)
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_cat(sb, ",");
|
2010-09-28 03:24:53 +00:00
|
|
|
if (range_start != range_stop)
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_printf(sb, "%d-%d", range_start, range_stop);
|
2010-09-28 03:24:53 +00:00
|
|
|
else
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_printf(sb, "%d", range_start);
|
2010-09-28 03:24:53 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 19:32:14 +00:00
|
|
|
device_t
|
|
|
|
gpiobus_attach_bus(device_t dev)
|
|
|
|
{
|
|
|
|
device_t busdev;
|
|
|
|
|
|
|
|
busdev = device_add_child(dev, "gpiobus", -1);
|
|
|
|
if (busdev == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (device_add_child(dev, "gpioc", -1) == NULL) {
|
|
|
|
device_delete_child(dev, busdev);
|
|
|
|
return (NULL);
|
|
|
|
}
|
2015-02-02 19:22:34 +00:00
|
|
|
#ifdef FDT
|
|
|
|
ofw_gpiobus_register_provider(dev);
|
|
|
|
#endif
|
2015-01-31 19:32:14 +00:00
|
|
|
bus_generic_attach(dev);
|
|
|
|
|
|
|
|
return (busdev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
gpiobus_detach_bus(device_t dev)
|
|
|
|
{
|
2015-08-17 16:51:37 +00:00
|
|
|
int err;
|
2015-01-31 19:32:14 +00:00
|
|
|
|
2015-02-02 19:22:34 +00:00
|
|
|
#ifdef FDT
|
|
|
|
ofw_gpiobus_unregister_provider(dev);
|
|
|
|
#endif
|
2015-08-17 16:51:37 +00:00
|
|
|
err = bus_generic_detach(dev);
|
|
|
|
if (err != 0)
|
|
|
|
return (err);
|
2015-02-02 19:22:34 +00:00
|
|
|
|
2015-08-17 16:51:37 +00:00
|
|
|
return (device_delete_children(dev));
|
2015-01-31 19:32:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 23:20:04 +00:00
|
|
|
int
|
|
|
|
gpiobus_init_softc(device_t dev)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
|
|
|
|
sc = GPIOBUS_SOFTC(dev);
|
|
|
|
sc->sc_busdev = dev;
|
|
|
|
sc->sc_dev = device_get_parent(dev);
|
2014-11-18 01:38:39 +00:00
|
|
|
sc->sc_intr_rman.rm_type = RMAN_ARRAY;
|
|
|
|
sc->sc_intr_rman.rm_descr = "GPIO Interrupts";
|
|
|
|
if (rman_init(&sc->sc_intr_rman) != 0 ||
|
|
|
|
rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0)
|
|
|
|
panic("%s: failed to set up rman.", __func__);
|
2014-10-23 23:20:04 +00:00
|
|
|
|
|
|
|
if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0)
|
|
|
|
return (ENXIO);
|
|
|
|
|
2016-01-14 02:58:28 +00:00
|
|
|
KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins"));
|
2014-10-23 23:20:04 +00:00
|
|
|
|
|
|
|
/* Pins = GPIO_PIN_MAX() + 1 */
|
|
|
|
sc->sc_npins++;
|
|
|
|
|
2018-01-21 15:42:36 +00:00
|
|
|
sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF,
|
2014-10-23 23:20:04 +00:00
|
|
|
M_NOWAIT | M_ZERO);
|
2015-03-08 00:47:50 +00:00
|
|
|
if (sc->sc_pins == NULL)
|
2014-10-23 23:20:04 +00:00
|
|
|
return (ENOMEM);
|
|
|
|
|
|
|
|
/* Initialize the bus lock. */
|
|
|
|
GPIOBUS_LOCK_INIT(sc);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-02-28 20:02:41 +00:00
|
|
|
int
|
|
|
|
gpiobus_alloc_ivars(struct gpiobus_ivar *devi)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Allocate pins and flags memory. */
|
2018-01-21 15:42:36 +00:00
|
|
|
devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF,
|
2015-02-28 20:02:41 +00:00
|
|
|
M_NOWAIT | M_ZERO);
|
|
|
|
if (devi->pins == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gpiobus_free_ivars(struct gpiobus_ivar *devi)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (devi->pins) {
|
|
|
|
free(devi->pins, M_DEVBUF);
|
|
|
|
devi->pins = NULL;
|
|
|
|
}
|
2019-06-27 15:46:06 +00:00
|
|
|
devi->npins = 0;
|
2015-02-28 20:02:41 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 22:28:47 +00:00
|
|
|
int
|
2016-05-26 15:45:36 +00:00
|
|
|
gpiobus_acquire_pin(device_t bus, uint32_t pin)
|
2015-03-02 22:28:47 +00:00
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
|
|
|
|
sc = device_get_softc(bus);
|
|
|
|
/* Consistency check. */
|
|
|
|
if (pin >= sc->sc_npins) {
|
2015-03-05 03:11:47 +00:00
|
|
|
device_printf(bus,
|
2015-03-02 22:28:47 +00:00
|
|
|
"invalid pin %d, max: %d\n", pin, sc->sc_npins - 1);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/* Mark pin as mapped and give warning if it's already mapped. */
|
2015-03-08 00:47:50 +00:00
|
|
|
if (sc->sc_pins[pin].mapped) {
|
2015-03-05 03:11:47 +00:00
|
|
|
device_printf(bus, "warning: pin %d is already mapped\n", pin);
|
2015-03-02 22:28:47 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2015-03-08 00:47:50 +00:00
|
|
|
sc->sc_pins[pin].mapped = 1;
|
2015-03-02 22:28:47 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2016-05-12 20:12:45 +00:00
|
|
|
/* Release mapped pin */
|
|
|
|
int
|
|
|
|
gpiobus_release_pin(device_t bus, uint32_t pin)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
|
|
|
|
sc = device_get_softc(bus);
|
|
|
|
/* Consistency check. */
|
|
|
|
if (pin >= sc->sc_npins) {
|
|
|
|
device_printf(bus,
|
2020-06-22 10:32:41 +00:00
|
|
|
"invalid pin %d, max=%d\n",
|
2016-05-12 20:12:45 +00:00
|
|
|
pin, sc->sc_npins - 1);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sc->sc_pins[pin].mapped) {
|
2020-06-22 10:32:41 +00:00
|
|
|
device_printf(bus, "pin %d is not mapped\n", pin);
|
2016-05-12 20:12:45 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
sc->sc_pins[pin].mapped = 0;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:46:06 +00:00
|
|
|
static int
|
|
|
|
gpiobus_acquire_child_pins(device_t dev, device_t child)
|
|
|
|
{
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < devi->npins; i++) {
|
|
|
|
/* Reserve the GPIO pin. */
|
|
|
|
if (gpiobus_acquire_pin(dev, devi->pins[i]) != 0) {
|
|
|
|
device_printf(child, "cannot acquire pin %d\n",
|
|
|
|
devi->pins[i]);
|
|
|
|
while (--i >= 0) {
|
|
|
|
(void)gpiobus_release_pin(dev,
|
|
|
|
devi->pins[i]);
|
|
|
|
}
|
|
|
|
gpiobus_free_ivars(devi);
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < devi->npins; i++) {
|
|
|
|
/* Use the child name as pin name. */
|
|
|
|
GPIOBUS_PIN_SETNAME(dev, devi->pins[i],
|
|
|
|
device_get_nameunit(child));
|
|
|
|
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
static int
|
|
|
|
gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask)
|
|
|
|
{
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
|
|
|
int i, npins;
|
|
|
|
|
|
|
|
npins = 0;
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
if (mask & (1 << i))
|
|
|
|
npins++;
|
|
|
|
}
|
|
|
|
if (npins == 0) {
|
2013-09-05 16:38:26 +00:00
|
|
|
device_printf(child, "empty pin mask\n");
|
2010-09-28 03:24:53 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
devi->npins = npins;
|
2015-02-28 20:02:41 +00:00
|
|
|
if (gpiobus_alloc_ivars(devi) != 0) {
|
|
|
|
device_printf(child, "cannot allocate device ivars\n");
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2010-09-28 03:24:53 +00:00
|
|
|
npins = 0;
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
if ((mask & (1 << i)) == 0)
|
|
|
|
continue;
|
|
|
|
devi->pins[npins++] = i;
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:46:06 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_parse_pin_list(struct gpiobus_softc *sc, device_t child,
|
|
|
|
const char *pins)
|
|
|
|
{
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
|
|
|
const char *p;
|
|
|
|
char *endp;
|
|
|
|
unsigned long pin;
|
|
|
|
int i, npins;
|
|
|
|
|
|
|
|
npins = 0;
|
|
|
|
p = pins;
|
|
|
|
for (;;) {
|
|
|
|
pin = strtoul(p, &endp, 0);
|
|
|
|
if (endp == p)
|
|
|
|
break;
|
|
|
|
npins++;
|
|
|
|
if (*endp == '\0')
|
|
|
|
break;
|
|
|
|
p = endp + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*endp != '\0') {
|
|
|
|
device_printf(child, "garbage in the pin list: %s\n", endp);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
if (npins == 0) {
|
|
|
|
device_printf(child, "empty pin list\n");
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
devi->npins = npins;
|
|
|
|
if (gpiobus_alloc_ivars(devi) != 0) {
|
|
|
|
device_printf(child, "cannot allocate device ivars\n");
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
p = pins;
|
|
|
|
for (;;) {
|
|
|
|
pin = strtoul(p, &endp, 0);
|
|
|
|
|
|
|
|
devi->pins[i] = pin;
|
|
|
|
|
|
|
|
if (*endp == '\0')
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
p = endp + 1;
|
|
|
|
}
|
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_probe(device_t dev)
|
|
|
|
{
|
|
|
|
device_set_desc(dev, "GPIO bus");
|
2013-11-12 16:08:23 +00:00
|
|
|
|
|
|
|
return (BUS_PROBE_GENERIC);
|
2010-09-28 03:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_attach(device_t dev)
|
|
|
|
{
|
2014-10-23 23:20:04 +00:00
|
|
|
int err;
|
2010-09-28 03:24:53 +00:00
|
|
|
|
2014-10-23 23:20:04 +00:00
|
|
|
err = gpiobus_init_softc(dev);
|
|
|
|
if (err != 0)
|
|
|
|
return (err);
|
2010-09-28 03:24:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get parent's pins and mark them as unmapped
|
|
|
|
*/
|
2013-11-12 16:08:23 +00:00
|
|
|
bus_generic_probe(dev);
|
2010-09-28 03:24:53 +00:00
|
|
|
bus_enumerate_hinted_children(dev);
|
2013-11-12 16:08:23 +00:00
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
return (bus_generic_attach(dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since this is not a self-enumerating bus, and since we always add
|
|
|
|
* children in attach, we have to always delete children here.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gpiobus_detach(device_t dev)
|
|
|
|
{
|
2013-08-28 14:39:24 +00:00
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
device_t *devlist;
|
|
|
|
int i, err, ndevs;
|
2010-09-28 03:24:53 +00:00
|
|
|
|
2013-08-28 14:39:24 +00:00
|
|
|
sc = GPIOBUS_SOFTC(dev);
|
2010-09-28 03:24:53 +00:00
|
|
|
KASSERT(mtx_initialized(&sc->sc_mtx),
|
|
|
|
("gpiobus mutex not initialized"));
|
|
|
|
GPIOBUS_LOCK_DESTROY(sc);
|
|
|
|
|
|
|
|
if ((err = bus_generic_detach(dev)) != 0)
|
|
|
|
return (err);
|
2011-11-19 10:11:50 +00:00
|
|
|
|
2013-08-28 14:39:24 +00:00
|
|
|
if ((err = device_get_children(dev, &devlist, &ndevs)) != 0)
|
|
|
|
return (err);
|
|
|
|
for (i = 0; i < ndevs; i++) {
|
|
|
|
devi = GPIOBUS_IVAR(devlist[i]);
|
2015-02-28 20:02:41 +00:00
|
|
|
gpiobus_free_ivars(devi);
|
2015-08-17 16:51:37 +00:00
|
|
|
resource_list_free(&devi->rl);
|
|
|
|
free(devi, M_DEVBUF);
|
|
|
|
device_delete_child(dev, devlist[i]);
|
2013-08-28 14:39:24 +00:00
|
|
|
}
|
|
|
|
free(devlist, M_TEMP);
|
2015-08-17 16:51:37 +00:00
|
|
|
rman_fini(&sc->sc_intr_rman);
|
2015-03-08 00:47:50 +00:00
|
|
|
if (sc->sc_pins) {
|
|
|
|
for (i = 0; i < sc->sc_npins; i++) {
|
|
|
|
if (sc->sc_pins[i].name != NULL)
|
|
|
|
free(sc->sc_pins[i].name, M_DEVBUF);
|
|
|
|
sc->sc_pins[i].name = NULL;
|
|
|
|
}
|
|
|
|
free(sc->sc_pins, M_DEVBUF);
|
|
|
|
sc->sc_pins = NULL;
|
2010-09-28 03:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_suspend(device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (bus_generic_suspend(dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_resume(device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (bus_generic_resume(dev));
|
|
|
|
}
|
|
|
|
|
2015-03-05 01:49:58 +00:00
|
|
|
static void
|
|
|
|
gpiobus_probe_nomatch(device_t dev, device_t child)
|
|
|
|
{
|
|
|
|
char pins[128];
|
2021-06-23 02:27:57 +00:00
|
|
|
struct sbuf sb;
|
2015-03-05 01:49:58 +00:00
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
|
|
|
|
devi = GPIOBUS_IVAR(child);
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_new(&sb, pins, sizeof(pins), SBUF_FIXEDLEN);
|
|
|
|
gpiobus_print_pins(devi, &sb);
|
|
|
|
sbuf_finish(&sb);
|
|
|
|
device_printf(dev, "<unknown device> at pin%s %s",
|
|
|
|
devi->npins > 1 ? "s" : "", sbuf_data(&sb));
|
2016-03-22 22:25:08 +00:00
|
|
|
resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
|
2015-03-05 01:49:58 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
static int
|
|
|
|
gpiobus_print_child(device_t dev, device_t child)
|
|
|
|
{
|
2014-11-18 02:41:35 +00:00
|
|
|
char pins[128];
|
2021-06-23 02:27:57 +00:00
|
|
|
struct sbuf sb;
|
2010-09-28 03:24:53 +00:00
|
|
|
int retval = 0;
|
2014-11-18 02:41:35 +00:00
|
|
|
struct gpiobus_ivar *devi;
|
2010-09-28 03:24:53 +00:00
|
|
|
|
2014-11-18 02:41:35 +00:00
|
|
|
devi = GPIOBUS_IVAR(child);
|
2010-09-28 03:24:53 +00:00
|
|
|
retval += bus_print_child_header(dev, child);
|
2015-07-22 04:18:33 +00:00
|
|
|
if (devi->npins > 0) {
|
|
|
|
if (devi->npins > 1)
|
|
|
|
retval += printf(" at pins ");
|
|
|
|
else
|
|
|
|
retval += printf(" at pin ");
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_new(&sb, pins, sizeof(pins), SBUF_FIXEDLEN);
|
|
|
|
gpiobus_print_pins(devi, &sb);
|
|
|
|
sbuf_finish(&sb);
|
|
|
|
retval += printf("%s", sbuf_data(&sb));
|
2015-07-22 04:18:33 +00:00
|
|
|
}
|
2016-03-22 22:25:08 +00:00
|
|
|
resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd");
|
2010-09-28 03:24:53 +00:00
|
|
|
retval += bus_print_child_footer(dev, child);
|
|
|
|
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-06-23 02:27:57 +00:00
|
|
|
gpiobus_child_location(device_t bus, device_t child, struct sbuf *sb)
|
2010-09-28 03:24:53 +00:00
|
|
|
{
|
2014-11-18 02:41:35 +00:00
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
|
|
|
|
devi = GPIOBUS_IVAR(child);
|
2021-06-23 02:27:57 +00:00
|
|
|
sbuf_printf(sb, "pins=");
|
|
|
|
gpiobus_print_pins(devi, sb);
|
2010-09-28 03:24:53 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static device_t
|
|
|
|
gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
|
|
|
|
{
|
|
|
|
device_t child;
|
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
|
|
|
|
child = device_add_child_ordered(dev, order, name, unit);
|
|
|
|
if (child == NULL)
|
|
|
|
return (child);
|
|
|
|
devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
|
|
|
|
if (devi == NULL) {
|
|
|
|
device_delete_child(dev, child);
|
2015-08-17 16:51:37 +00:00
|
|
|
return (NULL);
|
2010-09-28 03:24:53 +00:00
|
|
|
}
|
2014-11-18 01:38:39 +00:00
|
|
|
resource_list_init(&devi->rl);
|
2010-09-28 03:24:53 +00:00
|
|
|
device_set_ivars(child, devi);
|
2014-11-18 01:38:39 +00:00
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
return (child);
|
|
|
|
}
|
|
|
|
|
2019-12-06 22:32:06 +00:00
|
|
|
static int
|
|
|
|
gpiobus_rescan(device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-scan is supposed to remove and add children, but if someone has
|
|
|
|
* deleted the hints for a child we attached earlier, we have no easy
|
|
|
|
* way to handle that. So this just attaches new children for whom new
|
|
|
|
* hints or drivers have arrived since we last tried.
|
|
|
|
*/
|
|
|
|
bus_enumerate_hinted_children(dev);
|
|
|
|
bus_generic_attach(dev);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
static void
|
|
|
|
gpiobus_hinted_child(device_t bus, const char *dname, int dunit)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus);
|
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
device_t child;
|
2019-06-27 15:46:06 +00:00
|
|
|
const char *pins;
|
|
|
|
int irq, pinmask;
|
2010-09-28 03:24:53 +00:00
|
|
|
|
2019-12-06 22:32:06 +00:00
|
|
|
if (device_find_child(bus, dname, dunit) != NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
child = BUS_ADD_CHILD(bus, 0, dname, dunit);
|
|
|
|
devi = GPIOBUS_IVAR(child);
|
2019-06-27 15:46:06 +00:00
|
|
|
if (resource_int_value(dname, dunit, "pins", &pinmask) == 0) {
|
|
|
|
if (gpiobus_parse_pins(sc, child, pinmask)) {
|
|
|
|
resource_list_free(&devi->rl);
|
|
|
|
free(devi, M_DEVBUF);
|
|
|
|
device_delete_child(bus, child);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (resource_string_value(dname, dunit, "pin_list", &pins) == 0) {
|
|
|
|
if (gpiobus_parse_pin_list(sc, child, pins)) {
|
|
|
|
resource_list_free(&devi->rl);
|
|
|
|
free(devi, M_DEVBUF);
|
|
|
|
device_delete_child(bus, child);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-17 16:51:37 +00:00
|
|
|
}
|
2014-11-18 01:38:39 +00:00
|
|
|
if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
|
|
|
|
if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0)
|
|
|
|
device_printf(bus,
|
|
|
|
"warning: bus_set_resource() failed\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_set_resource(device_t dev, device_t child, int type, int rid,
|
2016-01-27 02:23:54 +00:00
|
|
|
rman_res_t start, rman_res_t count)
|
2014-11-18 01:38:39 +00:00
|
|
|
{
|
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
struct resource_list_entry *rle;
|
|
|
|
|
|
|
|
dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n",
|
|
|
|
__func__, dev, child, type, rid, (void *)(intptr_t)start, count);
|
|
|
|
devi = GPIOBUS_IVAR(child);
|
|
|
|
rle = resource_list_add(&devi->rl, type, rid, start,
|
|
|
|
start + count - 1, count);
|
|
|
|
if (rle == NULL)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2019-06-27 15:46:06 +00:00
|
|
|
static int
|
|
|
|
gpiobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
|
|
|
|
{
|
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
|
|
|
|
devi = GPIOBUS_IVAR(child);
|
|
|
|
switch (which) {
|
|
|
|
case GPIOBUS_IVAR_NPINS:
|
|
|
|
*result = devi->npins;
|
|
|
|
break;
|
|
|
|
case GPIOBUS_IVAR_PINS:
|
|
|
|
/* Children do not ever need to directly examine this. */
|
|
|
|
return (ENOTSUP);
|
|
|
|
default:
|
|
|
|
return (ENOENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
|
|
|
|
{
|
|
|
|
struct gpiobus_ivar *devi;
|
|
|
|
const uint32_t *ptr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
devi = GPIOBUS_IVAR(child);
|
|
|
|
switch (which) {
|
|
|
|
case GPIOBUS_IVAR_NPINS:
|
|
|
|
/* GPIO ivars are set once. */
|
|
|
|
if (devi->npins != 0) {
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
|
|
|
devi->npins = value;
|
|
|
|
if (gpiobus_alloc_ivars(devi) != 0) {
|
|
|
|
device_printf(child, "cannot allocate device ivars\n");
|
|
|
|
devi->npins = 0;
|
|
|
|
return (ENOMEM);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GPIOBUS_IVAR_PINS:
|
|
|
|
ptr = (const uint32_t *)value;
|
|
|
|
for (i = 0; i < devi->npins; i++)
|
|
|
|
devi->pins[i] = ptr[i];
|
|
|
|
if (gpiobus_acquire_child_pins(dev, child) != 0)
|
|
|
|
return (EBUSY);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ENOENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-11-18 01:38:39 +00:00
|
|
|
static struct resource *
|
|
|
|
gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid,
|
2016-01-27 02:23:54 +00:00
|
|
|
rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
|
2014-11-18 01:38:39 +00:00
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
struct resource *rv;
|
|
|
|
struct resource_list *rl;
|
|
|
|
struct resource_list_entry *rle;
|
|
|
|
int isdefault;
|
|
|
|
|
|
|
|
if (type != SYS_RES_IRQ)
|
|
|
|
return (NULL);
|
2016-02-20 01:32:58 +00:00
|
|
|
isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
|
2014-11-18 01:38:39 +00:00
|
|
|
rle = NULL;
|
|
|
|
if (isdefault) {
|
|
|
|
rl = BUS_GET_RESOURCE_LIST(bus, child);
|
|
|
|
if (rl == NULL)
|
|
|
|
return (NULL);
|
|
|
|
rle = resource_list_find(rl, type, *rid);
|
|
|
|
if (rle == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (rle->res != NULL)
|
|
|
|
panic("%s: resource entry is busy", __func__);
|
|
|
|
start = rle->start;
|
|
|
|
count = rle->count;
|
|
|
|
end = rle->end;
|
|
|
|
}
|
|
|
|
sc = device_get_softc(bus);
|
|
|
|
rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags,
|
|
|
|
child);
|
|
|
|
if (rv == NULL)
|
|
|
|
return (NULL);
|
|
|
|
rman_set_rid(rv, *rid);
|
|
|
|
if ((flags & RF_ACTIVE) != 0 &&
|
|
|
|
bus_activate_resource(child, type, *rid, rv) != 0) {
|
|
|
|
rman_release_resource(rv);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_release_resource(device_t bus __unused, device_t child, int type,
|
|
|
|
int rid, struct resource *r)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (rman_get_flags(r) & RF_ACTIVE) {
|
|
|
|
error = bus_deactivate_resource(child, type, rid, r);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rman_release_resource(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct resource_list *
|
|
|
|
gpiobus_get_resource_list(device_t bus __unused, device_t child)
|
|
|
|
{
|
|
|
|
struct gpiobus_ivar *ivar;
|
|
|
|
|
|
|
|
ivar = GPIOBUS_IVAR(child);
|
|
|
|
|
|
|
|
return (&ivar->rl);
|
2010-09-28 03:24:53 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 19:15:14 +00:00
|
|
|
static int
|
|
|
|
gpiobus_acquire_bus(device_t busdev, device_t child, int how)
|
2010-09-28 03:24:53 +00:00
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
|
|
|
|
sc = device_get_softc(busdev);
|
|
|
|
GPIOBUS_ASSERT_UNLOCKED(sc);
|
|
|
|
GPIOBUS_LOCK(sc);
|
2014-10-31 19:15:14 +00:00
|
|
|
if (sc->sc_owner != NULL) {
|
2015-07-24 04:56:46 +00:00
|
|
|
if (sc->sc_owner == child)
|
|
|
|
panic("%s: %s still owns the bus.",
|
|
|
|
device_get_nameunit(busdev),
|
|
|
|
device_get_nameunit(child));
|
2014-10-31 19:15:14 +00:00
|
|
|
if (how == GPIOBUS_DONTWAIT) {
|
|
|
|
GPIOBUS_UNLOCK(sc);
|
|
|
|
return (EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
while (sc->sc_owner != NULL)
|
|
|
|
mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0);
|
|
|
|
}
|
|
|
|
sc->sc_owner = child;
|
2010-09-28 03:24:53 +00:00
|
|
|
GPIOBUS_UNLOCK(sc);
|
|
|
|
|
2014-10-31 19:15:14 +00:00
|
|
|
return (0);
|
2010-09-28 03:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gpiobus_release_bus(device_t busdev, device_t child)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
|
|
|
|
sc = device_get_softc(busdev);
|
2014-10-31 19:15:14 +00:00
|
|
|
GPIOBUS_ASSERT_UNLOCKED(sc);
|
|
|
|
GPIOBUS_LOCK(sc);
|
|
|
|
if (sc->sc_owner == NULL)
|
2015-07-24 04:56:46 +00:00
|
|
|
panic("%s: %s releasing unowned bus.",
|
|
|
|
device_get_nameunit(busdev),
|
|
|
|
device_get_nameunit(child));
|
2010-09-28 03:24:53 +00:00
|
|
|
if (sc->sc_owner != child)
|
2015-07-24 04:56:46 +00:00
|
|
|
panic("%s: %s trying to release bus owned by %s",
|
|
|
|
device_get_nameunit(busdev),
|
|
|
|
device_get_nameunit(child),
|
|
|
|
device_get_nameunit(sc->sc_owner));
|
2010-09-28 03:24:53 +00:00
|
|
|
sc->sc_owner = NULL;
|
2014-10-31 19:15:14 +00:00
|
|
|
wakeup(sc);
|
|
|
|
GPIOBUS_UNLOCK(sc);
|
2010-09-28 03:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin,
|
|
|
|
uint32_t flags)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
2014-11-18 17:22:08 +00:00
|
|
|
uint32_t caps;
|
2010-09-28 03:24:53 +00:00
|
|
|
|
|
|
|
if (pin >= devi->npins)
|
|
|
|
return (EINVAL);
|
2014-11-18 17:22:08 +00:00
|
|
|
if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
if (gpio_check_flags(caps, flags) != 0)
|
|
|
|
return (EINVAL);
|
2010-09-28 03:24:53 +00:00
|
|
|
|
2014-11-18 17:22:08 +00:00
|
|
|
return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags));
|
2010-09-28 03:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin,
|
|
|
|
uint32_t *flags)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
|
|
|
|
|
|
|
if (pin >= devi->npins)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin,
|
|
|
|
uint32_t *caps)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
|
|
|
|
|
|
|
if (pin >= devi->npins)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_pin_set(device_t dev, device_t child, uint32_t pin,
|
|
|
|
unsigned int value)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
|
|
|
|
|
|
|
if (pin >= devi->npins)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_pin_get(device_t dev, device_t child, uint32_t pin,
|
|
|
|
unsigned int *value)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
|
|
|
|
|
|
|
if (pin >= devi->npins)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev);
|
|
|
|
struct gpiobus_ivar *devi = GPIOBUS_IVAR(child);
|
|
|
|
|
|
|
|
if (pin >= devi->npins)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]);
|
|
|
|
}
|
|
|
|
|
2015-03-08 00:47:50 +00:00
|
|
|
static int
|
|
|
|
gpiobus_pin_getname(device_t dev, uint32_t pin, char *name)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
|
|
|
|
sc = GPIOBUS_SOFTC(dev);
|
|
|
|
if (pin > sc->sc_npins)
|
|
|
|
return (EINVAL);
|
|
|
|
/* Did we have a name for this pin ? */
|
|
|
|
if (sc->sc_pins[pin].name != NULL) {
|
|
|
|
memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the default pin name. */
|
|
|
|
return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name)
|
|
|
|
{
|
|
|
|
struct gpiobus_softc *sc;
|
|
|
|
|
|
|
|
sc = GPIOBUS_SOFTC(dev);
|
|
|
|
if (pin > sc->sc_npins)
|
|
|
|
return (EINVAL);
|
|
|
|
if (name == NULL)
|
|
|
|
return (EINVAL);
|
|
|
|
/* Save the pin name. */
|
|
|
|
if (sc->sc_pins[pin].name == NULL)
|
|
|
|
sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF,
|
|
|
|
M_WAITOK | M_ZERO);
|
|
|
|
strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-09-28 03:24:53 +00:00
|
|
|
static device_method_t gpiobus_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_probe, gpiobus_probe),
|
|
|
|
DEVMETHOD(device_attach, gpiobus_attach),
|
|
|
|
DEVMETHOD(device_detach, gpiobus_detach),
|
|
|
|
DEVMETHOD(device_shutdown, bus_generic_shutdown),
|
|
|
|
DEVMETHOD(device_suspend, gpiobus_suspend),
|
|
|
|
DEVMETHOD(device_resume, gpiobus_resume),
|
|
|
|
|
|
|
|
/* Bus interface */
|
2014-11-18 01:38:39 +00:00
|
|
|
DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
|
|
|
|
DEVMETHOD(bus_config_intr, bus_generic_config_intr),
|
|
|
|
DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
|
|
|
|
DEVMETHOD(bus_set_resource, gpiobus_set_resource),
|
|
|
|
DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource),
|
|
|
|
DEVMETHOD(bus_release_resource, gpiobus_release_resource),
|
|
|
|
DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
|
|
|
|
DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
|
|
|
|
DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list),
|
2010-09-28 03:24:53 +00:00
|
|
|
DEVMETHOD(bus_add_child, gpiobus_add_child),
|
2019-12-06 22:32:06 +00:00
|
|
|
DEVMETHOD(bus_rescan, gpiobus_rescan),
|
2015-03-05 01:49:58 +00:00
|
|
|
DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch),
|
2010-09-28 03:24:53 +00:00
|
|
|
DEVMETHOD(bus_print_child, gpiobus_print_child),
|
2021-06-23 02:27:57 +00:00
|
|
|
DEVMETHOD(bus_child_location, gpiobus_child_location),
|
2010-09-28 03:24:53 +00:00
|
|
|
DEVMETHOD(bus_hinted_child, gpiobus_hinted_child),
|
2019-06-27 15:46:06 +00:00
|
|
|
DEVMETHOD(bus_read_ivar, gpiobus_read_ivar),
|
|
|
|
DEVMETHOD(bus_write_ivar, gpiobus_write_ivar),
|
2010-09-28 03:24:53 +00:00
|
|
|
|
|
|
|
/* GPIO protocol */
|
|
|
|
DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus),
|
|
|
|
DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus),
|
|
|
|
DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags),
|
|
|
|
DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps),
|
|
|
|
DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags),
|
|
|
|
DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get),
|
|
|
|
DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set),
|
|
|
|
DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle),
|
2015-03-08 00:47:50 +00:00
|
|
|
DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname),
|
|
|
|
DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname),
|
2010-09-28 03:24:53 +00:00
|
|
|
|
2011-11-22 21:28:20 +00:00
|
|
|
DEVMETHOD_END
|
2010-09-28 03:24:53 +00:00
|
|
|
};
|
|
|
|
|
2010-11-11 20:18:33 +00:00
|
|
|
driver_t gpiobus_driver = {
|
2010-09-28 03:24:53 +00:00
|
|
|
"gpiobus",
|
|
|
|
gpiobus_methods,
|
|
|
|
sizeof(struct gpiobus_softc)
|
|
|
|
};
|
|
|
|
|
|
|
|
devclass_t gpiobus_devclass;
|
|
|
|
|
2016-11-17 15:37:44 +00:00
|
|
|
EARLY_DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0,
|
|
|
|
BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
|
2010-09-28 03:24:53 +00:00
|
|
|
MODULE_VERSION(gpiobus, 1);
|