Index: head/sys/dev/gpio/gpiobusvar.h =================================================================== --- head/sys/dev/gpio/gpiobusvar.h (revision 279407) +++ head/sys/dev/gpio/gpiobusvar.h (revision 279408) @@ -1,108 +1,116 @@ /*- * Copyright (c) 2009 Oleksandr Tymoshenko * 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. * * 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. * * $FreeBSD$ * */ #ifndef __GPIOBUS_H__ #define __GPIOBUS_H__ #include "opt_platform.h" #include #include #include #ifdef FDT #include #endif #include "gpio_if.h" #ifdef FDT #define GPIOBUS_IVAR(d) (struct gpiobus_ivar *) \ &((struct ofw_gpiobus_devinfo *)device_get_ivars(d))->opd_dinfo #else #define GPIOBUS_IVAR(d) (struct gpiobus_ivar *) device_get_ivars(d) #endif #define GPIOBUS_SOFTC(d) (struct gpiobus_softc *) device_get_softc(d) #define GPIOBUS_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define GPIOBUS_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define GPIOBUS_LOCK_INIT(_sc) mtx_init(&_sc->sc_mtx, \ device_get_nameunit(_sc->sc_dev), "gpiobus", MTX_DEF) #define GPIOBUS_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx) #define GPIOBUS_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED) #define GPIOBUS_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED) #define GPIOBUS_WAIT 1 #define GPIOBUS_DONTWAIT 2 struct gpiobus_softc { struct mtx sc_mtx; /* bus mutex */ struct rman sc_intr_rman; /* isr resources */ device_t sc_busdev; /* bus device */ device_t sc_owner; /* bus owner */ device_t sc_dev; /* driver device */ int sc_npins; /* total pins on bus */ int *sc_pins_mapped; /* mark mapped pins */ }; +struct gpiobus_pin +{ + device_t dev; /* gpio device */ + uint32_t flags; /* pin flags */ + uint32_t pin; /* pin number */ +}; + struct gpiobus_ivar { struct resource_list rl; /* isr resource list */ uint32_t npins; /* pins total */ uint32_t *flags; /* pins flags */ uint32_t *pins; /* pins map */ }; #ifdef FDT struct ofw_gpiobus_devinfo { struct gpiobus_ivar opd_dinfo; struct ofw_bus_devinfo opd_obdinfo; }; static __inline int gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags) { return (GPIO_MAP_GPIOS(bus, dev, gparent, gcells, gpios, pin, flags)); } -device_t ofw_gpiobus_add_fdt_child(device_t, phandle_t); +device_t ofw_gpiobus_add_fdt_child(device_t, const char *, phandle_t); +int ofw_gpiobus_parse_gpios(device_t, char *, struct gpiobus_pin **); void ofw_gpiobus_register_provider(device_t); void ofw_gpiobus_unregister_provider(device_t); #endif int gpio_check_flags(uint32_t, uint32_t); device_t gpiobus_attach_bus(device_t); int gpiobus_detach_bus(device_t); int gpiobus_init_softc(device_t); int gpiobus_alloc_ivars(struct gpiobus_ivar *); void gpiobus_free_ivars(struct gpiobus_ivar *); extern driver_t gpiobus_driver; #endif /* __GPIOBUS_H__ */ Index: head/sys/dev/gpio/gpioled.c =================================================================== --- head/sys/dev/gpio/gpioled.c (revision 279407) +++ head/sys/dev/gpio/gpioled.c (revision 279408) @@ -1,233 +1,235 @@ /*- * Copyright (c) 2009 Oleksandr Tymoshenko * 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. * * 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. */ #include __FBSDID("$FreeBSD$"); #include "opt_platform.h" #include #include #include #include #include #include #include #include #include #ifdef FDT #include #include #endif #include #include #include "gpiobus_if.h" /* * Only one pin for led */ #define GPIOLED_PIN 0 #define GPIOLED_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define GPIOLED_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define GPIOLED_LOCK_INIT(_sc) \ mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ "gpioled", MTX_DEF) #define GPIOLED_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); struct gpioled_softc { device_t sc_dev; device_t sc_busdev; struct mtx sc_mtx; struct cdev *sc_leddev; }; static void gpioled_control(void *, int); static int gpioled_probe(device_t); static int gpioled_attach(device_t); static int gpioled_detach(device_t); static void gpioled_control(void *priv, int onoff) { int error; struct gpioled_softc *sc; sc = (struct gpioled_softc *)priv; GPIOLED_LOCK(sc); error = GPIOBUS_ACQUIRE_BUS(sc->sc_busdev, sc->sc_dev, GPIOBUS_DONTWAIT); if (error != 0) { GPIOLED_UNLOCK(sc); return; } error = GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, GPIO_PIN_OUTPUT); if (error == 0) GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, GPIOLED_PIN, onoff ? GPIO_PIN_HIGH : GPIO_PIN_LOW); GPIOBUS_RELEASE_BUS(sc->sc_busdev, sc->sc_dev); GPIOLED_UNLOCK(sc); } #ifdef FDT static void gpioled_identify(driver_t *driver, device_t bus) { phandle_t child, leds, root; root = OF_finddevice("/"); if (root == 0) return; leds = fdt_find_compatible(root, "gpio-leds", 1); if (leds == 0) return; - /* Traverse the 'gpio-leds' node and add its children. */ - for (child = OF_child(leds); child != 0; child = OF_peer(child)) - if (ofw_gpiobus_add_fdt_child(bus, child) == NULL) + for (child = OF_child(leds); child != 0; child = OF_peer(child)) { + if (!OF_hasprop(child, "gpios")) continue; + if (ofw_gpiobus_add_fdt_child(bus, driver->name, child) == NULL) + continue; + } } #endif static int gpioled_probe(device_t dev) { #ifdef FDT int match; phandle_t node; char *compat; /* * We can match against our own node compatible string and also against * our parent node compatible string. The first is normally used to * describe leds on a gpiobus and the later when there is a common node * compatible with 'gpio-leds' which is used to concentrate all the * leds nodes on the dts. */ match = 0; if (ofw_bus_is_compatible(dev, "gpioled")) match = 1; if (match == 0) { if ((node = ofw_bus_get_node(dev)) == -1) return (ENXIO); if ((node = OF_parent(node)) == -1) return (ENXIO); if (OF_getprop_alloc(node, "compatible", 1, (void **)&compat) == -1) return (ENXIO); if (strcasecmp(compat, "gpio-leds") == 0) match = 1; free(compat, M_OFWPROP); } if (match == 0) return (ENXIO); #endif device_set_desc(dev, "GPIO led"); return (0); } static int gpioled_attach(device_t dev) { struct gpioled_softc *sc; #ifdef FDT phandle_t node; char *name; #else const char *name; #endif sc = device_get_softc(dev); sc->sc_dev = dev; sc->sc_busdev = device_get_parent(dev); GPIOLED_LOCK_INIT(sc); #ifdef FDT name = NULL; if ((node = ofw_bus_get_node(dev)) == -1) return (ENXIO); if (OF_getprop_alloc(node, "label", 1, (void **)&name) == -1) OF_getprop_alloc(node, "name", 1, (void **)&name); #else if (resource_string_value(device_get_name(dev), device_get_unit(dev), "name", &name)) name = NULL; #endif sc->sc_leddev = led_create(gpioled_control, sc, name ? name : device_get_nameunit(dev)); #ifdef FDT if (name != NULL) free(name, M_OFWPROP); #endif return (0); } static int gpioled_detach(device_t dev) { struct gpioled_softc *sc; sc = device_get_softc(dev); if (sc->sc_leddev) { led_destroy(sc->sc_leddev); sc->sc_leddev = NULL; } GPIOLED_LOCK_DESTROY(sc); return (0); } static devclass_t gpioled_devclass; static device_method_t gpioled_methods[] = { /* Device interface */ #ifdef FDT DEVMETHOD(device_identify, gpioled_identify), #endif DEVMETHOD(device_probe, gpioled_probe), DEVMETHOD(device_attach, gpioled_attach), DEVMETHOD(device_detach, gpioled_detach), { 0, 0 } }; static driver_t gpioled_driver = { "gpioled", gpioled_methods, sizeof(struct gpioled_softc), }; DRIVER_MODULE(gpioled, gpiobus, gpioled_driver, gpioled_devclass, 0, 0); Index: head/sys/dev/gpio/ofw_gpiobus.c =================================================================== --- head/sys/dev/gpio/ofw_gpiobus.c (revision 279407) +++ head/sys/dev/gpio/ofw_gpiobus.c (revision 279408) @@ -1,354 +1,407 @@ /*- * Copyright (c) 2009, Nathan Whitehorn * Copyright (c) 2013, Luiz Otavio O Souza * Copyright (c) 2013 The FreeBSD Foundation * 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 unmodified, 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include -static int ofw_gpiobus_parse_gpios(struct gpiobus_softc *, - struct gpiobus_ivar *, phandle_t); static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t, - phandle_t); + device_t, phandle_t); static void ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *); +static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *, + struct gpiobus_softc *, struct gpiobus_pin **); device_t -ofw_gpiobus_add_fdt_child(device_t bus, phandle_t child) +ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child) { - struct ofw_gpiobus_devinfo *dinfo; device_t childdev; + struct ofw_gpiobus_devinfo *dinfo; /* * Set up the GPIO child and OFW bus layer devinfo and add it to bus. */ - dinfo = ofw_gpiobus_setup_devinfo(bus, child); - if (dinfo == NULL) + childdev = device_add_child(bus, drvname, -1); + if (childdev == NULL) return (NULL); - childdev = device_add_child(bus, NULL, -1); - if (childdev == NULL) { - device_printf(bus, "could not add child: %s\n", - dinfo->opd_obdinfo.obd_name); + dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child); + if (dinfo == NULL) { + device_delete_child(bus, childdev); + return (NULL); + } + if (device_probe_and_attach(childdev) != 0) { ofw_gpiobus_destroy_devinfo(dinfo); + device_delete_child(bus, childdev); return (NULL); } - device_set_ivars(childdev, dinfo); return (childdev); } -static int -ofw_gpiobus_parse_gpios(struct gpiobus_softc *sc, struct gpiobus_ivar *dinfo, - phandle_t child) +int +ofw_gpiobus_parse_gpios(device_t consumer, char *pname, + struct gpiobus_pin **pins) { - int cells, i, j, len; - pcell_t *gpios; - phandle_t gpio; - /* Retrieve the gpios property. */ - if ((len = OF_getproplen(child, "gpios")) < 0) - return (EINVAL); - gpios = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO); - if (gpios == NULL) - return (ENOMEM); - if (OF_getencprop(child, "gpios", gpios, len) < 0) { - free(gpios, M_DEVBUF); - return (EINVAL); - } - - /* - * The gpio-specifier is controller independent, but the first pcell - * has the reference to the GPIO controller phandler. - * One the first pass we count the number of encoded gpio-specifiers. - */ - i = 0; - len /= sizeof(pcell_t); - while (i < len) { - /* Allow NULL specifiers. */ - if (gpios[i] == 0) { - dinfo->npins++; - i++; - continue; - } - gpio = OF_node_from_xref(gpios[i]); - /* Verify if we're attaching to the correct GPIO controller. */ - if (!OF_hasprop(gpio, "gpio-controller") || - gpio != ofw_bus_get_node(sc->sc_dev)) { - free(gpios, M_DEVBUF); - return (EINVAL); - } - /* Read gpio-cells property for this GPIO controller. */ - if (OF_getencprop(gpio, "#gpio-cells", &cells, - sizeof(cells)) < 0) { - free(gpios, M_DEVBUF); - return (EINVAL); - } - dinfo->npins++; - i += cells + 1; - } - - if (dinfo->npins == 0) { - free(gpios, M_DEVBUF); - return (EINVAL); - } - - /* Allocate the child resources. */ - if (gpiobus_alloc_ivars(dinfo) != 0) { - free(gpios, M_DEVBUF); - return (ENOMEM); - } - - /* Decode the gpio specifier on the second pass. */ - i = 0; - j = 0; - while (i < len) { - /* Allow NULL specifiers. */ - if (gpios[i] == 0) { - i++; - j++; - continue; - } - - gpio = OF_node_from_xref(gpios[i]); - /* Read gpio-cells property for this GPIO controller. */ - if (OF_getencprop(gpio, "#gpio-cells", &cells, - sizeof(cells)) < 0) { - gpiobus_free_ivars(dinfo); - free(gpios, M_DEVBUF); - return (EINVAL); - } - - /* Get the GPIO pin number and flags. */ - if (gpio_map_gpios(sc->sc_dev, child, gpio, cells, - &gpios[i + 1], &dinfo->pins[j], &dinfo->flags[j]) != 0) { - gpiobus_free_ivars(dinfo); - free(gpios, M_DEVBUF); - return (EINVAL); - } - - /* Consistency check. */ - if (dinfo->pins[j] > sc->sc_npins) { - device_printf(sc->sc_busdev, - "invalid pin %d, max: %d\n", - dinfo->pins[j], sc->sc_npins - 1); - gpiobus_free_ivars(dinfo); - free(gpios, M_DEVBUF); - return (EINVAL); - } - - /* - * Mark pin as mapped and give warning if it's already mapped. - */ - if (sc->sc_pins_mapped[dinfo->pins[j]]) { - device_printf(sc->sc_busdev, - "warning: pin %d is already mapped\n", - dinfo->pins[j]); - gpiobus_free_ivars(dinfo); - free(gpios, M_DEVBUF); - return (EINVAL); - } - sc->sc_pins_mapped[dinfo->pins[j]] = 1; - - i += cells + 1; - j++; - } - - free(gpios, M_DEVBUF); - - return (0); + return (ofw_gpiobus_parse_gpios_impl(consumer, + ofw_bus_get_node(consumer), pname, NULL, pins)); } void ofw_gpiobus_register_provider(device_t provider) { phandle_t node; node = ofw_bus_get_node(provider); OF_device_register_xref(OF_xref_from_node(node), provider); } void ofw_gpiobus_unregister_provider(device_t provider) { phandle_t node; node = ofw_bus_get_node(provider); OF_device_register_xref(OF_xref_from_node(node), NULL); } static struct ofw_gpiobus_devinfo * -ofw_gpiobus_setup_devinfo(device_t dev, phandle_t node) +ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node) { + int i, npins; + struct gpiobus_ivar *devi; + struct gpiobus_pin *pins; struct gpiobus_softc *sc; struct ofw_gpiobus_devinfo *dinfo; - sc = device_get_softc(dev); + sc = device_get_softc(bus); dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO); if (dinfo == NULL) return (NULL); if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) { free(dinfo, M_DEVBUF); return (NULL); } - /* Parse the gpios property for the child. */ - if (ofw_gpiobus_parse_gpios(sc, &dinfo->opd_dinfo, node) != 0) { - ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); - free(dinfo, M_DEVBUF); - return (NULL); + npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins); + if (npins <= 0) + goto fail; + devi = &dinfo->opd_dinfo; + devi->npins = (uint32_t)npins; + if (gpiobus_alloc_ivars(devi) != 0) { + free(pins, M_DEVBUF); + goto fail; } + for (i = 0; i < devi->npins; i++) { + devi->flags[i] = pins[i].flags; + devi->pins[i] = pins[i].pin; + } + free(pins, M_DEVBUF); /* And now the interrupt resources. */ resource_list_init(&dinfo->opd_dinfo.rl); - if (ofw_bus_intr_to_rl(dev, node, &dinfo->opd_dinfo.rl) != 0) { - ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); - free(dinfo, M_DEVBUF); - return (NULL); + if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl) != 0) { + gpiobus_free_ivars(devi); + goto fail; } + device_set_ivars(child, dinfo); return (dinfo); + +fail: + ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); + free(dinfo, M_DEVBUF); + return (NULL); } static void ofw_gpiobus_destroy_devinfo(struct ofw_gpiobus_devinfo *dinfo) { + struct gpiobus_ivar *devi; + devi = &dinfo->opd_dinfo; + gpiobus_free_ivars(devi); resource_list_free(&dinfo->opd_dinfo.rl); ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); free(dinfo, M_DEVBUF); } static int +ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname, + struct gpiobus_softc *bussc, struct gpiobus_pin **pins) +{ + int gpiocells, i, j, ncells, npins; + pcell_t *gpios; + phandle_t gpio; + + ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios), + (void **)&gpios); + if (ncells == -1) { + device_printf(consumer, + "Warning: No %s specified in fdt data; " + "device may not function.\n", pname); + return (-1); + } + /* + * The gpio-specifier is controller independent, the first pcell has + * the reference to the GPIO controller phandler. + * Count the number of encoded gpio-specifiers on the first pass. + */ + i = 0; + npins = 0; + while (i < ncells) { + /* Allow NULL specifiers. */ + if (gpios[i] == 0) { + npins++; + i++; + continue; + } + gpio = OF_node_from_xref(gpios[i]); + /* If we have bussc, ignore devices from other gpios. */ + if (bussc != NULL) + if (ofw_bus_get_node(bussc->sc_dev) != gpio) + return (0); + /* + * Check for gpio-controller property and read the #gpio-cells + * for this GPIO controller. + */ + if (!OF_hasprop(gpio, "gpio-controller") || + OF_getencprop(gpio, "#gpio-cells", &gpiocells, + sizeof(gpiocells)) < 0) { + device_printf(consumer, + "gpio reference is not a gpio-controller.\n"); + free(gpios, M_OFWPROP); + return (-1); + } + if (ncells - i < gpiocells + 1) { + device_printf(consumer, + "%s cells doesn't match #gpio-cells.\n", pname); + return (-1); + } + npins++; + i += gpiocells + 1; + } + if (npins == 0 || pins == NULL) { + if (npins == 0) + device_printf(consumer, "no pin specified in %s.\n", + pname); + free(gpios, M_OFWPROP); + return (npins); + } + *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF, + M_NOWAIT | M_ZERO); + if (*pins == NULL) { + free(gpios, M_OFWPROP); + return (-1); + } + /* Decode the gpio specifier on the second pass. */ + i = 0; + j = 0; + while (i < ncells) { + /* Allow NULL specifiers. */ + if (gpios[i] == 0) { + j++; + i++; + continue; + } + gpio = OF_node_from_xref(gpios[i]); + /* Read gpio-cells property for this GPIO controller. */ + if (OF_getencprop(gpio, "#gpio-cells", &gpiocells, + sizeof(gpiocells)) < 0) { + device_printf(consumer, + "gpio does not have the #gpio-cells property.\n"); + goto fail; + } + /* Return the device reference for the GPIO controller. */ + (*pins)[j].dev = OF_device_from_xref(gpios[i]); + if ((*pins)[j].dev == NULL) { + device_printf(consumer, + "no device registered for the gpio controller.\n"); + goto fail; + } + /* + * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to + * retrieve it. The GPIO_GET_BUS() method is only valid after + * the child is probed and attached. + */ + if (bussc == NULL) { + if (GPIO_GET_BUS((*pins)[j].dev) == NULL) { + device_printf(consumer, + "no gpiobus reference for %s.\n", + device_get_nameunit((*pins)[j].dev)); + goto fail; + } + bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev)); + } + /* Get the GPIO pin number and flags. */ + if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells, + &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) { + device_printf(consumer, + "cannot map the gpios specifier.\n"); + goto fail; + } + /* Consistency check. */ + if ((*pins)[j].pin >= bussc->sc_npins) { + device_printf(consumer, "invalid pin %d, max: %d\n", + (*pins)[j].pin, bussc->sc_npins - 1); + goto fail; + } + /* + * Mark pin as mapped and give warning if it's already mapped. + */ + if (bussc->sc_pins_mapped[(*pins)[j].pin]) { + device_printf(consumer, + "warning: pin %d is already mapped\n", + pins[j]->pin); + goto fail; + } + bussc->sc_pins_mapped[(*pins)[j].pin] = 1; + j++; + i += gpiocells + 1; + } + free(gpios, M_OFWPROP); + + return (npins); + +fail: + free(gpios, M_OFWPROP); + free(*pins, M_DEVBUF); + return (-1); +} + +static int ofw_gpiobus_probe(device_t dev) { if (ofw_bus_get_node(dev) == -1) return (ENXIO); device_set_desc(dev, "OFW GPIO bus"); return (0); } static int ofw_gpiobus_attach(device_t dev) { int err; phandle_t child; err = gpiobus_init_softc(dev); if (err != 0) return (err); - bus_generic_probe(dev); bus_enumerate_hinted_children(dev); - /* * Attach the children represented in the device tree. */ for (child = OF_child(ofw_bus_get_node(dev)); child != 0; - child = OF_peer(child)) - if (ofw_gpiobus_add_fdt_child(dev, child) == NULL) + child = OF_peer(child)) { + if (!OF_hasprop(child, "gpios")) continue; + if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL) + continue; + } return (bus_generic_attach(dev)); } static device_t ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) { device_t child; struct ofw_gpiobus_devinfo *devi; child = device_add_child_ordered(dev, order, name, unit); if (child == NULL) return (child); devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF, M_NOWAIT | M_ZERO); if (devi == NULL) { device_delete_child(dev, child); return (0); } /* * NULL all the OFW-related parts of the ivars for non-OFW * children. */ devi->opd_obdinfo.obd_node = -1; devi->opd_obdinfo.obd_name = NULL; devi->opd_obdinfo.obd_compat = NULL; devi->opd_obdinfo.obd_type = NULL; devi->opd_obdinfo.obd_model = NULL; device_set_ivars(child, devi); return (child); } static const struct ofw_bus_devinfo * ofw_gpiobus_get_devinfo(device_t bus, device_t dev) { struct ofw_gpiobus_devinfo *dinfo; dinfo = device_get_ivars(dev); return (&dinfo->opd_obdinfo); } static device_method_t ofw_gpiobus_methods[] = { /* Device interface */ DEVMETHOD(device_probe, ofw_gpiobus_probe), DEVMETHOD(device_attach, ofw_gpiobus_attach), /* Bus interface */ DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), DEVMETHOD(bus_add_child, ofw_gpiobus_add_child), /* ofw_bus interface */ DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo), DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), DEVMETHOD_END }; static devclass_t ofwgpiobus_devclass; DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods, sizeof(struct gpiobus_softc), gpiobus_driver); DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0); MODULE_VERSION(ofw_gpiobus, 1); MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);