The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/gpio/ofw_gpiobus.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
    5  * Copyright (c) 2013, Luiz Otavio O Souza <loos@FreeBSD.org>
    6  * Copyright (c) 2013 The FreeBSD Foundation
    7  * All rights reserved.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice unmodified, this list of conditions, and the following
   14  *    disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   29  */
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD$");
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/bus.h>
   37 #include <sys/kernel.h>
   38 #include <sys/malloc.h>
   39 #include <sys/module.h>
   40 
   41 #include <dev/gpio/gpiobusvar.h>
   42 #include <dev/ofw/ofw_bus.h>
   43 
   44 #include "gpiobus_if.h"
   45 
   46 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t,
   47         device_t, phandle_t);
   48 static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *);
   49 static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *,
   50         struct gpiobus_softc *, struct gpiobus_pin **);
   51 
   52 /*
   53  * Utility functions for easier handling of OFW GPIO pins.
   54  *
   55  * !!! BEWARE !!!
   56  * GPIOBUS uses children's IVARs, so we cannot use this interface for cross
   57  * tree consumers.
   58  *
   59  */
   60 int
   61 gpio_pin_get_by_ofw_propidx(device_t consumer, phandle_t cnode,
   62     char *prop_name, int idx, gpio_pin_t *out_pin)
   63 {
   64         phandle_t xref;
   65         pcell_t *cells;
   66         device_t busdev;
   67         struct gpiobus_pin pin;
   68         int ncells, rv;
   69 
   70         KASSERT(consumer != NULL && cnode > 0,
   71             ("both consumer and cnode required"));
   72 
   73         rv = ofw_bus_parse_xref_list_alloc(cnode, prop_name, "#gpio-cells",
   74             idx, &xref, &ncells, &cells);
   75         if (rv != 0)
   76                 return (rv);
   77 
   78         /* Translate provider to device. */
   79         pin.dev = OF_device_from_xref(xref);
   80         if (pin.dev == NULL) {
   81                 OF_prop_free(cells);
   82                 return (ENODEV);
   83         }
   84 
   85         /* Test if GPIO bus already exist. */
   86         busdev = GPIO_GET_BUS(pin.dev);
   87         if (busdev == NULL) {
   88                 OF_prop_free(cells);
   89                 return (ENODEV);
   90         }
   91 
   92         /* Map GPIO pin. */
   93         rv = gpio_map_gpios(pin.dev, cnode, OF_node_from_xref(xref), ncells,
   94             cells, &pin.pin, &pin.flags);
   95         OF_prop_free(cells);
   96         if (rv != 0)
   97                 return (ENXIO);
   98 
   99         /* Reserve GPIO pin. */
  100         rv = gpiobus_acquire_pin(busdev, pin.pin);
  101         if (rv != 0)
  102                 return (EBUSY);
  103 
  104         *out_pin = malloc(sizeof(struct gpiobus_pin), M_DEVBUF,
  105             M_WAITOK | M_ZERO);
  106         **out_pin = pin;
  107         return (0);
  108 }
  109 
  110 int
  111 gpio_pin_get_by_ofw_idx(device_t consumer, phandle_t node,
  112     int idx, gpio_pin_t *pin)
  113 {
  114 
  115         return (gpio_pin_get_by_ofw_propidx(consumer, node, "gpios", idx, pin));
  116 }
  117 
  118 int
  119 gpio_pin_get_by_ofw_property(device_t consumer, phandle_t node,
  120     char *name, gpio_pin_t *pin)
  121 {
  122 
  123         return (gpio_pin_get_by_ofw_propidx(consumer, node, name, 0, pin));
  124 }
  125 
  126 int
  127 gpio_pin_get_by_ofw_name(device_t consumer, phandle_t node,
  128     char *name, gpio_pin_t *pin)
  129 {
  130         int rv, idx;
  131 
  132         KASSERT(consumer != NULL && node > 0,
  133             ("both consumer and node required"));
  134 
  135         rv = ofw_bus_find_string_index(node, "gpio-names", name, &idx);
  136         if (rv != 0)
  137                 return (rv);
  138         return (gpio_pin_get_by_ofw_idx(consumer, node, idx, pin));
  139 }
  140 
  141 /*
  142  * OFW_GPIOBUS driver.
  143  */
  144 device_t
  145 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child)
  146 {
  147         device_t childdev;
  148         int i;
  149         struct gpiobus_ivar *devi;
  150         struct ofw_gpiobus_devinfo *dinfo;
  151 
  152         /*
  153          * Check to see if we already have a child for @p child, and if so
  154          * return it.
  155          */
  156         childdev = ofw_bus_find_child_device_by_phandle(bus, child);
  157         if (childdev != NULL)
  158                 return (childdev);
  159 
  160         /*
  161          * Set up the GPIO child and OFW bus layer devinfo and add it to bus.
  162          */
  163         childdev = device_add_child(bus, drvname, -1);
  164         if (childdev == NULL)
  165                 return (NULL);
  166         dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child);
  167         if (dinfo == NULL) {
  168                 device_delete_child(bus, childdev);
  169                 return (NULL);
  170         }
  171         if (device_probe_and_attach(childdev) != 0) {
  172                 ofw_gpiobus_destroy_devinfo(bus, dinfo);
  173                 device_delete_child(bus, childdev);
  174                 return (NULL);
  175         }
  176         /* Use the child name as pin name. */
  177         devi = &dinfo->opd_dinfo;
  178         for (i = 0; i < devi->npins; i++)
  179                 GPIOBUS_PIN_SETNAME(bus, devi->pins[i],
  180                     device_get_nameunit(childdev));
  181 
  182         return (childdev);
  183 }
  184 
  185 int
  186 ofw_gpiobus_parse_gpios(device_t consumer, char *pname,
  187         struct gpiobus_pin **pins)
  188 {
  189 
  190         return (ofw_gpiobus_parse_gpios_impl(consumer,
  191             ofw_bus_get_node(consumer), pname, NULL, pins));
  192 }
  193 
  194 void
  195 ofw_gpiobus_register_provider(device_t provider)
  196 {
  197         phandle_t node;
  198 
  199         node = ofw_bus_get_node(provider);
  200         if (node != -1)
  201                 OF_device_register_xref(OF_xref_from_node(node), provider);
  202 }
  203 
  204 void
  205 ofw_gpiobus_unregister_provider(device_t provider)
  206 {
  207         phandle_t node;
  208 
  209         node = ofw_bus_get_node(provider);
  210         if (node != -1)
  211                 OF_device_register_xref(OF_xref_from_node(node), NULL);
  212 }
  213 
  214 static struct ofw_gpiobus_devinfo *
  215 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node)
  216 {
  217         int i, npins;
  218         struct gpiobus_ivar *devi;
  219         struct gpiobus_pin *pins;
  220         struct gpiobus_softc *sc;
  221         struct ofw_gpiobus_devinfo *dinfo;
  222 
  223         sc = device_get_softc(bus);
  224         dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO);
  225         if (dinfo == NULL)
  226                 return (NULL);
  227         if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) {
  228                 free(dinfo, M_DEVBUF);
  229                 return (NULL);
  230         }
  231         /* Parse the gpios property for the child. */
  232         npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins);
  233         if (npins <= 0) {
  234                 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
  235                 free(dinfo, M_DEVBUF);
  236                 return (NULL);
  237         }
  238         /* Initialize the irq resource list. */
  239         resource_list_init(&dinfo->opd_dinfo.rl);
  240         /* Allocate the child ivars and copy the parsed pin data. */
  241         devi = &dinfo->opd_dinfo;
  242         devi->npins = (uint32_t)npins;
  243         if (gpiobus_alloc_ivars(devi) != 0) {
  244                 free(pins, M_DEVBUF);
  245                 ofw_gpiobus_destroy_devinfo(bus, dinfo);
  246                 return (NULL);
  247         }
  248         for (i = 0; i < devi->npins; i++)
  249                 devi->pins[i] = pins[i].pin;
  250         free(pins, M_DEVBUF);
  251         /* Parse the interrupt resources. */
  252         if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) {
  253                 ofw_gpiobus_destroy_devinfo(bus, dinfo);
  254                 return (NULL);
  255         }
  256         device_set_ivars(child, dinfo);
  257 
  258         return (dinfo);
  259 }
  260 
  261 static void
  262 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo)
  263 {
  264         int i;
  265         struct gpiobus_ivar *devi;
  266         struct gpiobus_softc *sc;
  267 
  268         sc = device_get_softc(bus);
  269         devi = &dinfo->opd_dinfo;
  270         for (i = 0; i < devi->npins; i++) {
  271                 if (devi->pins[i] > sc->sc_npins)
  272                         continue;
  273                 sc->sc_pins[devi->pins[i]].mapped = 0;
  274         }
  275         gpiobus_free_ivars(devi);
  276         resource_list_free(&dinfo->opd_dinfo.rl);
  277         ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo);
  278         free(dinfo, M_DEVBUF);
  279 }
  280 
  281 static int
  282 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname,
  283         struct gpiobus_softc *bussc, struct gpiobus_pin **pins)
  284 {
  285         int gpiocells, i, j, ncells, npins;
  286         pcell_t *gpios;
  287         phandle_t gpio;
  288 
  289         ncells = OF_getencprop_alloc_multi(cnode, pname, sizeof(*gpios),
  290             (void **)&gpios);
  291         if (ncells == -1) {
  292                 device_printf(consumer,
  293                     "Warning: No %s specified in fdt data; "
  294                     "device may not function.\n", pname);
  295                 return (-1);
  296         }
  297         /*
  298          * The gpio-specifier is controller independent, the first pcell has
  299          * the reference to the GPIO controller phandler.
  300          * Count the number of encoded gpio-specifiers on the first pass.
  301          */
  302         i = 0;
  303         npins = 0;
  304         while (i < ncells) {
  305                 /* Allow NULL specifiers. */
  306                 if (gpios[i] == 0) {
  307                         npins++;
  308                         i++;
  309                         continue;
  310                 }
  311                 gpio = OF_node_from_xref(gpios[i]);
  312                 /* If we have bussc, ignore devices from other gpios. */
  313                 if (bussc != NULL)
  314                         if (ofw_bus_get_node(bussc->sc_dev) != gpio)
  315                                 return (0);
  316                 /*
  317                  * Check for gpio-controller property and read the #gpio-cells
  318                  * for this GPIO controller.
  319                  */
  320                 if (!OF_hasprop(gpio, "gpio-controller") ||
  321                     OF_getencprop(gpio, "#gpio-cells", &gpiocells,
  322                     sizeof(gpiocells)) < 0) {
  323                         device_printf(consumer,
  324                             "gpio reference is not a gpio-controller.\n");
  325                         OF_prop_free(gpios);
  326                         return (-1);
  327                 }
  328                 if (ncells - i < gpiocells + 1) {
  329                         device_printf(consumer,
  330                             "%s cells doesn't match #gpio-cells.\n", pname);
  331                         return (-1);
  332                 }
  333                 npins++;
  334                 i += gpiocells + 1;
  335         }
  336         if (npins == 0 || pins == NULL) {
  337                 if (npins == 0)
  338                         device_printf(consumer, "no pin specified in %s.\n",
  339                             pname);
  340                 OF_prop_free(gpios);
  341                 return (npins);
  342         }
  343         *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF,
  344             M_NOWAIT | M_ZERO);
  345         if (*pins == NULL) {
  346                 OF_prop_free(gpios);
  347                 return (-1);
  348         }
  349         /* Decode the gpio specifier on the second pass. */
  350         i = 0;
  351         j = 0;
  352         while (i < ncells) {
  353                 /* Allow NULL specifiers. */
  354                 if (gpios[i] == 0) {
  355                         j++;
  356                         i++;
  357                         continue;
  358                 }
  359                 gpio = OF_node_from_xref(gpios[i]);
  360                 /* Read gpio-cells property for this GPIO controller. */
  361                 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells,
  362                     sizeof(gpiocells)) < 0) {
  363                         device_printf(consumer,
  364                             "gpio does not have the #gpio-cells property.\n");
  365                         goto fail;
  366                 }
  367                 /* Return the device reference for the GPIO controller. */
  368                 (*pins)[j].dev = OF_device_from_xref(gpios[i]);
  369                 if ((*pins)[j].dev == NULL) {
  370                         device_printf(consumer,
  371                             "no device registered for the gpio controller.\n");
  372                         goto fail;
  373                 }
  374                 /*
  375                  * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to
  376                  * retrieve it.  The GPIO_GET_BUS() method is only valid after
  377                  * the child is probed and attached.
  378                  */
  379                 if (bussc == NULL) {
  380                         if (GPIO_GET_BUS((*pins)[j].dev) == NULL) {
  381                                 device_printf(consumer,
  382                                     "no gpiobus reference for %s.\n",
  383                                     device_get_nameunit((*pins)[j].dev));
  384                                 goto fail;
  385                         }
  386                         bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev));
  387                 }
  388                 /* Get the GPIO pin number and flags. */
  389                 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells,
  390                     &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) {
  391                         device_printf(consumer,
  392                             "cannot map the gpios specifier.\n");
  393                         goto fail;
  394                 }
  395                 /* Reserve the GPIO pin. */
  396                 if (gpiobus_acquire_pin(bussc->sc_busdev, (*pins)[j].pin) != 0)
  397                         goto fail;
  398                 j++;
  399                 i += gpiocells + 1;
  400         }
  401         OF_prop_free(gpios);
  402 
  403         return (npins);
  404 
  405 fail:
  406         OF_prop_free(gpios);
  407         free(*pins, M_DEVBUF);
  408         return (-1);
  409 }
  410 
  411 static int
  412 ofw_gpiobus_probe(device_t dev)
  413 {
  414 
  415         if (ofw_bus_get_node(dev) == -1)
  416                 return (ENXIO);
  417         device_set_desc(dev, "OFW GPIO bus");
  418 
  419         return (BUS_PROBE_DEFAULT);
  420 }
  421 
  422 static int
  423 ofw_gpiobus_attach(device_t dev)
  424 {
  425         int err;
  426         phandle_t child;
  427 
  428         err = gpiobus_init_softc(dev);
  429         if (err != 0)
  430                 return (err);
  431         bus_generic_probe(dev);
  432         bus_enumerate_hinted_children(dev);
  433         /*
  434          * Attach the children represented in the device tree.
  435          */
  436         for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
  437             child = OF_peer(child)) {
  438                 if (OF_hasprop(child, "gpio-hog"))
  439                         continue;
  440                 if (!OF_hasprop(child, "gpios"))
  441                         continue;
  442                 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL)
  443                         continue;
  444         }
  445 
  446         return (bus_generic_attach(dev));
  447 }
  448 
  449 static device_t
  450 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit)
  451 {
  452         device_t child;
  453         struct ofw_gpiobus_devinfo *devi;
  454 
  455         child = device_add_child_ordered(dev, order, name, unit);
  456         if (child == NULL)
  457                 return (child);
  458         devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF,
  459             M_NOWAIT | M_ZERO);
  460         if (devi == NULL) {
  461                 device_delete_child(dev, child);
  462                 return (0);
  463         }
  464 
  465         /*
  466          * NULL all the OFW-related parts of the ivars for non-OFW
  467          * children.
  468          */
  469         devi->opd_obdinfo.obd_node = -1;
  470         devi->opd_obdinfo.obd_name = NULL;
  471         devi->opd_obdinfo.obd_compat = NULL;
  472         devi->opd_obdinfo.obd_type = NULL;
  473         devi->opd_obdinfo.obd_model = NULL;
  474 
  475         device_set_ivars(child, devi);
  476 
  477         return (child);
  478 }
  479 
  480 static const struct ofw_bus_devinfo *
  481 ofw_gpiobus_get_devinfo(device_t bus, device_t dev)
  482 {
  483         struct ofw_gpiobus_devinfo *dinfo;
  484 
  485         dinfo = device_get_ivars(dev);
  486 
  487         return (&dinfo->opd_obdinfo);
  488 }
  489 
  490 static device_method_t ofw_gpiobus_methods[] = {
  491         /* Device interface */
  492         DEVMETHOD(device_probe,         ofw_gpiobus_probe),
  493         DEVMETHOD(device_attach,        ofw_gpiobus_attach),
  494 
  495         /* Bus interface */
  496         DEVMETHOD(bus_child_pnpinfo,    ofw_bus_gen_child_pnpinfo),
  497         DEVMETHOD(bus_add_child,        ofw_gpiobus_add_child),
  498 
  499         /* ofw_bus interface */
  500         DEVMETHOD(ofw_bus_get_devinfo,  ofw_gpiobus_get_devinfo),
  501         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
  502         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
  503         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
  504         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
  505         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
  506 
  507         DEVMETHOD_END
  508 };
  509 
  510 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods,
  511     sizeof(struct gpiobus_softc), gpiobus_driver);
  512 EARLY_DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, 0, 0, BUS_PASS_BUS);
  513 MODULE_VERSION(ofw_gpiobus, 1);
  514 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1);

Cache object: c773103c472399f8fa0df21d449d0c28


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.