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