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> 30 __FBSDID("$FreeBSD$"); 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 42 #include "gpiobus_if.h" 43 44 static struct ofw_gpiobus_devinfo *ofw_gpiobus_setup_devinfo(device_t, 45 device_t, phandle_t); 46 static void ofw_gpiobus_destroy_devinfo(device_t, struct ofw_gpiobus_devinfo *); 47 static int ofw_gpiobus_parse_gpios_impl(device_t, phandle_t, char *, 48 struct gpiobus_softc *, struct gpiobus_pin **); 49 50 device_t 51 ofw_gpiobus_add_fdt_child(device_t bus, const char *drvname, phandle_t child) 52 { 53 device_t childdev; 54 int i; 55 struct gpiobus_ivar *devi; 56 struct ofw_gpiobus_devinfo *dinfo; 57 58 /* 59 * Check to see if we already have a child for @p child, and if so 60 * return it. 61 */ 62 childdev = ofw_bus_find_child_device_by_phandle(bus, child); 63 if (childdev != NULL) 64 return (childdev); 65 66 /* 67 * Set up the GPIO child and OFW bus layer devinfo and add it to bus. 68 */ 69 childdev = device_add_child(bus, drvname, -1); 70 if (childdev == NULL) 71 return (NULL); 72 dinfo = ofw_gpiobus_setup_devinfo(bus, childdev, child); 73 if (dinfo == NULL) { 74 device_delete_child(bus, childdev); 75 return (NULL); 76 } 77 if (device_probe_and_attach(childdev) != 0) { 78 ofw_gpiobus_destroy_devinfo(bus, dinfo); 79 device_delete_child(bus, childdev); 80 return (NULL); 81 } 82 /* Use the child name as pin name. */ 83 devi = &dinfo->opd_dinfo; 84 for (i = 0; i < devi->npins; i++) 85 GPIOBUS_PIN_SETNAME(bus, devi->pins[i], 86 device_get_nameunit(childdev)); 87 88 return (childdev); 89 } 90 91 int 92 ofw_gpiobus_parse_gpios(device_t consumer, char *pname, 93 struct gpiobus_pin **pins) 94 { 95 96 return (ofw_gpiobus_parse_gpios_impl(consumer, 97 ofw_bus_get_node(consumer), pname, NULL, pins)); 98 } 99 100 void 101 ofw_gpiobus_register_provider(device_t provider) 102 { 103 phandle_t node; 104 105 node = ofw_bus_get_node(provider); 106 OF_device_register_xref(OF_xref_from_node(node), provider); 107 } 108 109 void 110 ofw_gpiobus_unregister_provider(device_t provider) 111 { 112 phandle_t node; 113 114 node = ofw_bus_get_node(provider); 115 OF_device_register_xref(OF_xref_from_node(node), NULL); 116 } 117 118 static struct ofw_gpiobus_devinfo * 119 ofw_gpiobus_setup_devinfo(device_t bus, device_t child, phandle_t node) 120 { 121 int i, npins; 122 struct gpiobus_ivar *devi; 123 struct gpiobus_pin *pins; 124 struct gpiobus_softc *sc; 125 struct ofw_gpiobus_devinfo *dinfo; 126 127 sc = device_get_softc(bus); 128 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_NOWAIT | M_ZERO); 129 if (dinfo == NULL) 130 return (NULL); 131 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, node) != 0) { 132 free(dinfo, M_DEVBUF); 133 return (NULL); 134 } 135 /* Parse the gpios property for the child. */ 136 npins = ofw_gpiobus_parse_gpios_impl(child, node, "gpios", sc, &pins); 137 if (npins <= 0) { 138 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); 139 free(dinfo, M_DEVBUF); 140 return (NULL); 141 } 142 /* Initialize the irq resource list. */ 143 resource_list_init(&dinfo->opd_dinfo.rl); 144 /* Allocate the child ivars and copy the parsed pin data. */ 145 devi = &dinfo->opd_dinfo; 146 devi->npins = (uint32_t)npins; 147 if (gpiobus_alloc_ivars(devi) != 0) { 148 free(pins, M_DEVBUF); 149 ofw_gpiobus_destroy_devinfo(bus, dinfo); 150 return (NULL); 151 } 152 for (i = 0; i < devi->npins; i++) { 153 devi->flags[i] = pins[i].flags; 154 devi->pins[i] = pins[i].pin; 155 } 156 free(pins, M_DEVBUF); 157 /* Parse the interrupt resources. */ 158 if (ofw_bus_intr_to_rl(bus, node, &dinfo->opd_dinfo.rl, NULL) != 0) { 159 ofw_gpiobus_destroy_devinfo(bus, dinfo); 160 return (NULL); 161 } 162 device_set_ivars(child, dinfo); 163 164 return (dinfo); 165 } 166 167 static void 168 ofw_gpiobus_destroy_devinfo(device_t bus, struct ofw_gpiobus_devinfo *dinfo) 169 { 170 int i; 171 struct gpiobus_ivar *devi; 172 struct gpiobus_softc *sc; 173 174 sc = device_get_softc(bus); 175 devi = &dinfo->opd_dinfo; 176 for (i = 0; i < devi->npins; i++) { 177 if (devi->pins[i] > sc->sc_npins) 178 continue; 179 sc->sc_pins[devi->pins[i]].mapped = 0; 180 } 181 gpiobus_free_ivars(devi); 182 resource_list_free(&dinfo->opd_dinfo.rl); 183 ofw_bus_gen_destroy_devinfo(&dinfo->opd_obdinfo); 184 free(dinfo, M_DEVBUF); 185 } 186 187 static int 188 ofw_gpiobus_parse_gpios_impl(device_t consumer, phandle_t cnode, char *pname, 189 struct gpiobus_softc *bussc, struct gpiobus_pin **pins) 190 { 191 int gpiocells, i, j, ncells, npins; 192 pcell_t *gpios; 193 phandle_t gpio; 194 195 ncells = OF_getencprop_alloc(cnode, pname, sizeof(*gpios), 196 (void **)&gpios); 197 if (ncells == -1) { 198 device_printf(consumer, 199 "Warning: No %s specified in fdt data; " 200 "device may not function.\n", pname); 201 return (-1); 202 } 203 /* 204 * The gpio-specifier is controller independent, the first pcell has 205 * the reference to the GPIO controller phandler. 206 * Count the number of encoded gpio-specifiers on the first pass. 207 */ 208 i = 0; 209 npins = 0; 210 while (i < ncells) { 211 /* Allow NULL specifiers. */ 212 if (gpios[i] == 0) { 213 npins++; 214 i++; 215 continue; 216 } 217 gpio = OF_node_from_xref(gpios[i]); 218 /* If we have bussc, ignore devices from other gpios. */ 219 if (bussc != NULL) 220 if (ofw_bus_get_node(bussc->sc_dev) != gpio) 221 return (0); 222 /* 223 * Check for gpio-controller property and read the #gpio-cells 224 * for this GPIO controller. 225 */ 226 if (!OF_hasprop(gpio, "gpio-controller") || 227 OF_getencprop(gpio, "#gpio-cells", &gpiocells, 228 sizeof(gpiocells)) < 0) { 229 device_printf(consumer, 230 "gpio reference is not a gpio-controller.\n"); 231 free(gpios, M_OFWPROP); 232 return (-1); 233 } 234 if (ncells - i < gpiocells + 1) { 235 device_printf(consumer, 236 "%s cells doesn't match #gpio-cells.\n", pname); 237 return (-1); 238 } 239 npins++; 240 i += gpiocells + 1; 241 } 242 if (npins == 0 || pins == NULL) { 243 if (npins == 0) 244 device_printf(consumer, "no pin specified in %s.\n", 245 pname); 246 free(gpios, M_OFWPROP); 247 return (npins); 248 } 249 *pins = malloc(sizeof(struct gpiobus_pin) * npins, M_DEVBUF, 250 M_NOWAIT | M_ZERO); 251 if (*pins == NULL) { 252 free(gpios, M_OFWPROP); 253 return (-1); 254 } 255 /* Decode the gpio specifier on the second pass. */ 256 i = 0; 257 j = 0; 258 while (i < ncells) { 259 /* Allow NULL specifiers. */ 260 if (gpios[i] == 0) { 261 j++; 262 i++; 263 continue; 264 } 265 gpio = OF_node_from_xref(gpios[i]); 266 /* Read gpio-cells property for this GPIO controller. */ 267 if (OF_getencprop(gpio, "#gpio-cells", &gpiocells, 268 sizeof(gpiocells)) < 0) { 269 device_printf(consumer, 270 "gpio does not have the #gpio-cells property.\n"); 271 goto fail; 272 } 273 /* Return the device reference for the GPIO controller. */ 274 (*pins)[j].dev = OF_device_from_xref(gpios[i]); 275 if ((*pins)[j].dev == NULL) { 276 device_printf(consumer, 277 "no device registered for the gpio controller.\n"); 278 goto fail; 279 } 280 /* 281 * If the gpiobus softc is NULL we use the GPIO_GET_BUS() to 282 * retrieve it. The GPIO_GET_BUS() method is only valid after 283 * the child is probed and attached. 284 */ 285 if (bussc == NULL) { 286 if (GPIO_GET_BUS((*pins)[j].dev) == NULL) { 287 device_printf(consumer, 288 "no gpiobus reference for %s.\n", 289 device_get_nameunit((*pins)[j].dev)); 290 goto fail; 291 } 292 bussc = device_get_softc(GPIO_GET_BUS((*pins)[j].dev)); 293 } 294 /* Get the GPIO pin number and flags. */ 295 if (gpio_map_gpios((*pins)[j].dev, cnode, gpio, gpiocells, 296 &gpios[i + 1], &(*pins)[j].pin, &(*pins)[j].flags) != 0) { 297 device_printf(consumer, 298 "cannot map the gpios specifier.\n"); 299 goto fail; 300 } 301 /* Reserve the GPIO pin. */ 302 if (gpiobus_map_pin(bussc->sc_busdev, (*pins)[j].pin) != 0) 303 goto fail; 304 j++; 305 i += gpiocells + 1; 306 } 307 free(gpios, M_OFWPROP); 308 309 return (npins); 310 311 fail: 312 free(gpios, M_OFWPROP); 313 free(*pins, M_DEVBUF); 314 return (-1); 315 } 316 317 static int 318 ofw_gpiobus_probe(device_t dev) 319 { 320 321 if (ofw_bus_get_node(dev) == -1) 322 return (ENXIO); 323 device_set_desc(dev, "OFW GPIO bus"); 324 325 return (0); 326 } 327 328 static int 329 ofw_gpiobus_attach(device_t dev) 330 { 331 int err; 332 phandle_t child; 333 334 err = gpiobus_init_softc(dev); 335 if (err != 0) 336 return (err); 337 bus_generic_probe(dev); 338 bus_enumerate_hinted_children(dev); 339 /* 340 * Attach the children represented in the device tree. 341 */ 342 for (child = OF_child(ofw_bus_get_node(dev)); child != 0; 343 child = OF_peer(child)) { 344 if (!OF_hasprop(child, "gpios")) 345 continue; 346 if (ofw_gpiobus_add_fdt_child(dev, NULL, child) == NULL) 347 continue; 348 } 349 350 return (bus_generic_attach(dev)); 351 } 352 353 static device_t 354 ofw_gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) 355 { 356 device_t child; 357 struct ofw_gpiobus_devinfo *devi; 358 359 child = device_add_child_ordered(dev, order, name, unit); 360 if (child == NULL) 361 return (child); 362 devi = malloc(sizeof(struct ofw_gpiobus_devinfo), M_DEVBUF, 363 M_NOWAIT | M_ZERO); 364 if (devi == NULL) { 365 device_delete_child(dev, child); 366 return (0); 367 } 368 369 /* 370 * NULL all the OFW-related parts of the ivars for non-OFW 371 * children. 372 */ 373 devi->opd_obdinfo.obd_node = -1; 374 devi->opd_obdinfo.obd_name = NULL; 375 devi->opd_obdinfo.obd_compat = NULL; 376 devi->opd_obdinfo.obd_type = NULL; 377 devi->opd_obdinfo.obd_model = NULL; 378 379 device_set_ivars(child, devi); 380 381 return (child); 382 } 383 384 static const struct ofw_bus_devinfo * 385 ofw_gpiobus_get_devinfo(device_t bus, device_t dev) 386 { 387 struct ofw_gpiobus_devinfo *dinfo; 388 389 dinfo = device_get_ivars(dev); 390 391 return (&dinfo->opd_obdinfo); 392 } 393 394 static device_method_t ofw_gpiobus_methods[] = { 395 /* Device interface */ 396 DEVMETHOD(device_probe, ofw_gpiobus_probe), 397 DEVMETHOD(device_attach, ofw_gpiobus_attach), 398 399 /* Bus interface */ 400 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 401 DEVMETHOD(bus_add_child, ofw_gpiobus_add_child), 402 403 /* ofw_bus interface */ 404 DEVMETHOD(ofw_bus_get_devinfo, ofw_gpiobus_get_devinfo), 405 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 406 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 407 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 408 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 409 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 410 411 DEVMETHOD_END 412 }; 413 414 static devclass_t ofwgpiobus_devclass; 415 416 DEFINE_CLASS_1(gpiobus, ofw_gpiobus_driver, ofw_gpiobus_methods, 417 sizeof(struct gpiobus_softc), gpiobus_driver); 418 DRIVER_MODULE(ofw_gpiobus, gpio, ofw_gpiobus_driver, ofwgpiobus_devclass, 0, 0); 419 MODULE_VERSION(ofw_gpiobus, 1); 420 MODULE_DEPEND(ofw_gpiobus, gpiobus, 1, 1, 1); 421