1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org> 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, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * 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/gpio.h> 36 #ifdef INTRNG 37 #include <sys/intr.h> 38 #endif 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 43 #include <dev/gpio/gpiobusvar.h> 44 45 #include "gpiobus_if.h" 46 47 #undef GPIOBUS_DEBUG 48 #ifdef GPIOBUS_DEBUG 49 #define dprintf printf 50 #else 51 #define dprintf(x, arg...) 52 #endif 53 54 static void gpiobus_print_pins(struct gpiobus_ivar *, char *, size_t); 55 static int gpiobus_parse_pins(struct gpiobus_softc *, device_t, int); 56 static int gpiobus_probe(device_t); 57 static int gpiobus_attach(device_t); 58 static int gpiobus_detach(device_t); 59 static int gpiobus_suspend(device_t); 60 static int gpiobus_resume(device_t); 61 static void gpiobus_probe_nomatch(device_t, device_t); 62 static int gpiobus_print_child(device_t, device_t); 63 static int gpiobus_child_location_str(device_t, device_t, char *, size_t); 64 static int gpiobus_child_pnpinfo_str(device_t, device_t, char *, size_t); 65 static device_t gpiobus_add_child(device_t, u_int, const char *, int); 66 static void gpiobus_hinted_child(device_t, const char *, int); 67 68 /* 69 * GPIOBUS interface 70 */ 71 static int gpiobus_acquire_bus(device_t, device_t, int); 72 static void gpiobus_release_bus(device_t, device_t); 73 static int gpiobus_pin_setflags(device_t, device_t, uint32_t, uint32_t); 74 static int gpiobus_pin_getflags(device_t, device_t, uint32_t, uint32_t*); 75 static int gpiobus_pin_getcaps(device_t, device_t, uint32_t, uint32_t*); 76 static int gpiobus_pin_set(device_t, device_t, uint32_t, unsigned int); 77 static int gpiobus_pin_get(device_t, device_t, uint32_t, unsigned int*); 78 static int gpiobus_pin_toggle(device_t, device_t, uint32_t); 79 80 /* 81 * gpiobus_pin flags 82 * The flags in struct gpiobus_pin are not related to the flags used by the 83 * low-level controller driver in struct gpio_pin. Currently, only pins 84 * acquired via FDT data have gpiobus_pin.flags set, sourced from the flags in 85 * the FDT properties. In theory, these flags are defined per-platform. In 86 * practice they are always the flags from the dt-bindings/gpio/gpio.h file. 87 * The only one of those flags we currently support is for handling active-low 88 * pins, so we just define that flag here instead of including a GPL'd header. 89 */ 90 #define GPIO_ACTIVE_LOW 1 91 92 /* 93 * XXX -> Move me to better place - gpio_subr.c? 94 * Also, this function must be changed when interrupt configuration 95 * data will be moved into struct resource. 96 */ 97 #ifdef INTRNG 98 99 struct resource * 100 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags, 101 gpio_pin_t pin, uint32_t intr_mode) 102 { 103 u_int irq; 104 struct intr_map_data_gpio *gpio_data; 105 struct resource *res; 106 107 gpio_data = (struct intr_map_data_gpio *)intr_alloc_map_data( 108 INTR_MAP_DATA_GPIO, sizeof(*gpio_data), M_WAITOK | M_ZERO); 109 gpio_data->gpio_pin_num = pin->pin; 110 gpio_data->gpio_pin_flags = pin->flags; 111 gpio_data->gpio_intr_mode = intr_mode; 112 113 irq = intr_map_irq(pin->dev, 0, (struct intr_map_data *)gpio_data); 114 res = bus_alloc_resource(consumer_dev, SYS_RES_IRQ, rid, irq, irq, 1, 115 alloc_flags); 116 if (res == NULL) { 117 intr_free_intr_map_data((struct intr_map_data *)gpio_data); 118 return (NULL); 119 } 120 rman_set_virtual(res, gpio_data); 121 return (res); 122 } 123 #else 124 struct resource * 125 gpio_alloc_intr_resource(device_t consumer_dev, int *rid, u_int alloc_flags, 126 gpio_pin_t pin, uint32_t intr_mode) 127 { 128 129 return (NULL); 130 } 131 #endif 132 133 int 134 gpio_check_flags(uint32_t caps, uint32_t flags) 135 { 136 137 /* Filter unwanted flags. */ 138 flags &= caps; 139 140 /* Cannot mix input/output together. */ 141 if (flags & GPIO_PIN_INPUT && flags & GPIO_PIN_OUTPUT) 142 return (EINVAL); 143 /* Cannot mix pull-up/pull-down together. */ 144 if (flags & GPIO_PIN_PULLUP && flags & GPIO_PIN_PULLDOWN) 145 return (EINVAL); 146 /* Cannot mix output and interrupt flags together */ 147 if (flags & GPIO_PIN_OUTPUT && flags & GPIO_INTR_MASK) 148 return (EINVAL); 149 /* Only one interrupt flag can be defined at once */ 150 if ((flags & GPIO_INTR_MASK) & ((flags & GPIO_INTR_MASK) - 1)) 151 return (EINVAL); 152 /* The interrupt attached flag cannot be set */ 153 if (flags & GPIO_INTR_ATTACHED) 154 return (EINVAL); 155 156 return (0); 157 } 158 159 int 160 gpio_pin_get_by_bus_pinnum(device_t busdev, uint32_t pinnum, gpio_pin_t *ppin) 161 { 162 gpio_pin_t pin; 163 int err; 164 165 err = gpiobus_acquire_pin(busdev, pinnum); 166 if (err != 0) 167 return (EBUSY); 168 169 pin = malloc(sizeof(*pin), M_DEVBUF, M_WAITOK | M_ZERO); 170 171 pin->dev = device_get_parent(busdev); 172 pin->pin = pinnum; 173 pin->flags = 0; 174 175 *ppin = pin; 176 return (0); 177 } 178 179 int 180 gpio_pin_get_by_child_index(device_t childdev, uint32_t idx, gpio_pin_t *ppin) 181 { 182 struct gpiobus_ivar *devi; 183 184 devi = GPIOBUS_IVAR(childdev); 185 if (idx >= devi->npins) 186 return (EINVAL); 187 188 return (gpio_pin_get_by_bus_pinnum(device_get_parent(childdev), 189 devi->pins[idx], ppin)); 190 } 191 192 int 193 gpio_pin_getcaps(gpio_pin_t pin, uint32_t *caps) 194 { 195 196 KASSERT(pin != NULL, ("GPIO pin is NULL.")); 197 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); 198 return (GPIO_PIN_GETCAPS(pin->dev, pin->pin, caps)); 199 } 200 201 int 202 gpio_pin_is_active(gpio_pin_t pin, bool *active) 203 { 204 int rv; 205 uint32_t tmp; 206 207 KASSERT(pin != NULL, ("GPIO pin is NULL.")); 208 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); 209 rv = GPIO_PIN_GET(pin->dev, pin->pin, &tmp); 210 if (rv != 0) { 211 return (rv); 212 } 213 214 if (pin->flags & GPIO_ACTIVE_LOW) 215 *active = tmp == 0; 216 else 217 *active = tmp != 0; 218 return (0); 219 } 220 221 void 222 gpio_pin_release(gpio_pin_t gpio) 223 { 224 device_t busdev; 225 226 if (gpio == NULL) 227 return; 228 229 KASSERT(gpio->dev != NULL, ("GPIO pin device is NULL.")); 230 231 busdev = GPIO_GET_BUS(gpio->dev); 232 if (busdev != NULL) 233 gpiobus_release_pin(busdev, gpio->pin); 234 235 free(gpio, M_DEVBUF); 236 } 237 238 int 239 gpio_pin_set_active(gpio_pin_t pin, bool active) 240 { 241 int rv; 242 uint32_t tmp; 243 244 if (pin->flags & GPIO_ACTIVE_LOW) 245 tmp = active ? 0 : 1; 246 else 247 tmp = active ? 1 : 0; 248 249 KASSERT(pin != NULL, ("GPIO pin is NULL.")); 250 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); 251 rv = GPIO_PIN_SET(pin->dev, pin->pin, tmp); 252 return (rv); 253 } 254 255 int 256 gpio_pin_setflags(gpio_pin_t pin, uint32_t flags) 257 { 258 int rv; 259 260 KASSERT(pin != NULL, ("GPIO pin is NULL.")); 261 KASSERT(pin->dev != NULL, ("GPIO pin device is NULL.")); 262 263 rv = GPIO_PIN_SETFLAGS(pin->dev, pin->pin, flags); 264 return (rv); 265 } 266 267 static void 268 gpiobus_print_pins(struct gpiobus_ivar *devi, char *buf, size_t buflen) 269 { 270 char tmp[128]; 271 int i, range_start, range_stop, need_coma; 272 273 if (devi->npins == 0) 274 return; 275 276 need_coma = 0; 277 range_start = range_stop = devi->pins[0]; 278 for (i = 1; i < devi->npins; i++) { 279 if (devi->pins[i] != (range_stop + 1)) { 280 if (need_coma) 281 strlcat(buf, ",", buflen); 282 memset(tmp, 0, sizeof(tmp)); 283 if (range_start != range_stop) 284 snprintf(tmp, sizeof(tmp) - 1, "%d-%d", 285 range_start, range_stop); 286 else 287 snprintf(tmp, sizeof(tmp) - 1, "%d", 288 range_start); 289 strlcat(buf, tmp, buflen); 290 291 range_start = range_stop = devi->pins[i]; 292 need_coma = 1; 293 } 294 else 295 range_stop++; 296 } 297 298 if (need_coma) 299 strlcat(buf, ",", buflen); 300 memset(tmp, 0, sizeof(tmp)); 301 if (range_start != range_stop) 302 snprintf(tmp, sizeof(tmp) - 1, "%d-%d", 303 range_start, range_stop); 304 else 305 snprintf(tmp, sizeof(tmp) - 1, "%d", 306 range_start); 307 strlcat(buf, tmp, buflen); 308 } 309 310 device_t 311 gpiobus_attach_bus(device_t dev) 312 { 313 device_t busdev; 314 315 busdev = device_add_child(dev, "gpiobus", -1); 316 if (busdev == NULL) 317 return (NULL); 318 if (device_add_child(dev, "gpioc", -1) == NULL) { 319 device_delete_child(dev, busdev); 320 return (NULL); 321 } 322 #ifdef FDT 323 ofw_gpiobus_register_provider(dev); 324 #endif 325 bus_generic_attach(dev); 326 327 return (busdev); 328 } 329 330 int 331 gpiobus_detach_bus(device_t dev) 332 { 333 int err; 334 335 #ifdef FDT 336 ofw_gpiobus_unregister_provider(dev); 337 #endif 338 err = bus_generic_detach(dev); 339 if (err != 0) 340 return (err); 341 342 return (device_delete_children(dev)); 343 } 344 345 int 346 gpiobus_init_softc(device_t dev) 347 { 348 struct gpiobus_softc *sc; 349 350 sc = GPIOBUS_SOFTC(dev); 351 sc->sc_busdev = dev; 352 sc->sc_dev = device_get_parent(dev); 353 sc->sc_intr_rman.rm_type = RMAN_ARRAY; 354 sc->sc_intr_rman.rm_descr = "GPIO Interrupts"; 355 if (rman_init(&sc->sc_intr_rman) != 0 || 356 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0) 357 panic("%s: failed to set up rman.", __func__); 358 359 if (GPIO_PIN_MAX(sc->sc_dev, &sc->sc_npins) != 0) 360 return (ENXIO); 361 362 KASSERT(sc->sc_npins >= 0, ("GPIO device with no pins")); 363 364 /* Pins = GPIO_PIN_MAX() + 1 */ 365 sc->sc_npins++; 366 367 sc->sc_pins = malloc(sizeof(*sc->sc_pins) * sc->sc_npins, M_DEVBUF, 368 M_NOWAIT | M_ZERO); 369 if (sc->sc_pins == NULL) 370 return (ENOMEM); 371 372 /* Initialize the bus lock. */ 373 GPIOBUS_LOCK_INIT(sc); 374 375 return (0); 376 } 377 378 int 379 gpiobus_alloc_ivars(struct gpiobus_ivar *devi) 380 { 381 382 /* Allocate pins and flags memory. */ 383 devi->pins = malloc(sizeof(uint32_t) * devi->npins, M_DEVBUF, 384 M_NOWAIT | M_ZERO); 385 if (devi->pins == NULL) 386 return (ENOMEM); 387 return (0); 388 } 389 390 void 391 gpiobus_free_ivars(struct gpiobus_ivar *devi) 392 { 393 394 if (devi->pins) { 395 free(devi->pins, M_DEVBUF); 396 devi->pins = NULL; 397 } 398 devi->npins = 0; 399 } 400 401 int 402 gpiobus_acquire_pin(device_t bus, uint32_t pin) 403 { 404 struct gpiobus_softc *sc; 405 406 sc = device_get_softc(bus); 407 /* Consistency check. */ 408 if (pin >= sc->sc_npins) { 409 device_printf(bus, 410 "invalid pin %d, max: %d\n", pin, sc->sc_npins - 1); 411 return (-1); 412 } 413 /* Mark pin as mapped and give warning if it's already mapped. */ 414 if (sc->sc_pins[pin].mapped) { 415 device_printf(bus, "warning: pin %d is already mapped\n", pin); 416 return (-1); 417 } 418 sc->sc_pins[pin].mapped = 1; 419 420 return (0); 421 } 422 423 /* Release mapped pin */ 424 int 425 gpiobus_release_pin(device_t bus, uint32_t pin) 426 { 427 struct gpiobus_softc *sc; 428 429 sc = device_get_softc(bus); 430 /* Consistency check. */ 431 if (pin >= sc->sc_npins) { 432 device_printf(bus, 433 "invalid pin %d, max=%d\n", 434 pin, sc->sc_npins - 1); 435 return (-1); 436 } 437 438 if (!sc->sc_pins[pin].mapped) { 439 device_printf(bus, "pin %d is not mapped\n", pin); 440 return (-1); 441 } 442 sc->sc_pins[pin].mapped = 0; 443 444 return (0); 445 } 446 447 static int 448 gpiobus_acquire_child_pins(device_t dev, device_t child) 449 { 450 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 451 int i; 452 453 for (i = 0; i < devi->npins; i++) { 454 /* Reserve the GPIO pin. */ 455 if (gpiobus_acquire_pin(dev, devi->pins[i]) != 0) { 456 device_printf(child, "cannot acquire pin %d\n", 457 devi->pins[i]); 458 while (--i >= 0) { 459 (void)gpiobus_release_pin(dev, 460 devi->pins[i]); 461 } 462 gpiobus_free_ivars(devi); 463 return (EBUSY); 464 } 465 } 466 for (i = 0; i < devi->npins; i++) { 467 /* Use the child name as pin name. */ 468 GPIOBUS_PIN_SETNAME(dev, devi->pins[i], 469 device_get_nameunit(child)); 470 471 } 472 return (0); 473 } 474 475 static int 476 gpiobus_parse_pins(struct gpiobus_softc *sc, device_t child, int mask) 477 { 478 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 479 int i, npins; 480 481 npins = 0; 482 for (i = 0; i < 32; i++) { 483 if (mask & (1 << i)) 484 npins++; 485 } 486 if (npins == 0) { 487 device_printf(child, "empty pin mask\n"); 488 return (EINVAL); 489 } 490 devi->npins = npins; 491 if (gpiobus_alloc_ivars(devi) != 0) { 492 device_printf(child, "cannot allocate device ivars\n"); 493 return (EINVAL); 494 } 495 npins = 0; 496 for (i = 0; i < 32; i++) { 497 if ((mask & (1 << i)) == 0) 498 continue; 499 devi->pins[npins++] = i; 500 } 501 502 return (0); 503 } 504 505 static int 506 gpiobus_parse_pin_list(struct gpiobus_softc *sc, device_t child, 507 const char *pins) 508 { 509 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 510 const char *p; 511 char *endp; 512 unsigned long pin; 513 int i, npins; 514 515 npins = 0; 516 p = pins; 517 for (;;) { 518 pin = strtoul(p, &endp, 0); 519 if (endp == p) 520 break; 521 npins++; 522 if (*endp == '\0') 523 break; 524 p = endp + 1; 525 } 526 527 if (*endp != '\0') { 528 device_printf(child, "garbage in the pin list: %s\n", endp); 529 return (EINVAL); 530 } 531 if (npins == 0) { 532 device_printf(child, "empty pin list\n"); 533 return (EINVAL); 534 } 535 536 devi->npins = npins; 537 if (gpiobus_alloc_ivars(devi) != 0) { 538 device_printf(child, "cannot allocate device ivars\n"); 539 return (EINVAL); 540 } 541 542 i = 0; 543 p = pins; 544 for (;;) { 545 pin = strtoul(p, &endp, 0); 546 547 devi->pins[i] = pin; 548 549 if (*endp == '\0') 550 break; 551 i++; 552 p = endp + 1; 553 } 554 555 return (0); 556 } 557 558 static int 559 gpiobus_probe(device_t dev) 560 { 561 device_set_desc(dev, "GPIO bus"); 562 563 return (BUS_PROBE_GENERIC); 564 } 565 566 static int 567 gpiobus_attach(device_t dev) 568 { 569 int err; 570 571 err = gpiobus_init_softc(dev); 572 if (err != 0) 573 return (err); 574 575 /* 576 * Get parent's pins and mark them as unmapped 577 */ 578 bus_generic_probe(dev); 579 bus_enumerate_hinted_children(dev); 580 581 return (bus_generic_attach(dev)); 582 } 583 584 /* 585 * Since this is not a self-enumerating bus, and since we always add 586 * children in attach, we have to always delete children here. 587 */ 588 static int 589 gpiobus_detach(device_t dev) 590 { 591 struct gpiobus_softc *sc; 592 struct gpiobus_ivar *devi; 593 device_t *devlist; 594 int i, err, ndevs; 595 596 sc = GPIOBUS_SOFTC(dev); 597 KASSERT(mtx_initialized(&sc->sc_mtx), 598 ("gpiobus mutex not initialized")); 599 GPIOBUS_LOCK_DESTROY(sc); 600 601 if ((err = bus_generic_detach(dev)) != 0) 602 return (err); 603 604 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0) 605 return (err); 606 for (i = 0; i < ndevs; i++) { 607 devi = GPIOBUS_IVAR(devlist[i]); 608 gpiobus_free_ivars(devi); 609 resource_list_free(&devi->rl); 610 free(devi, M_DEVBUF); 611 device_delete_child(dev, devlist[i]); 612 } 613 free(devlist, M_TEMP); 614 rman_fini(&sc->sc_intr_rman); 615 if (sc->sc_pins) { 616 for (i = 0; i < sc->sc_npins; i++) { 617 if (sc->sc_pins[i].name != NULL) 618 free(sc->sc_pins[i].name, M_DEVBUF); 619 sc->sc_pins[i].name = NULL; 620 } 621 free(sc->sc_pins, M_DEVBUF); 622 sc->sc_pins = NULL; 623 } 624 625 return (0); 626 } 627 628 static int 629 gpiobus_suspend(device_t dev) 630 { 631 632 return (bus_generic_suspend(dev)); 633 } 634 635 static int 636 gpiobus_resume(device_t dev) 637 { 638 639 return (bus_generic_resume(dev)); 640 } 641 642 static void 643 gpiobus_probe_nomatch(device_t dev, device_t child) 644 { 645 char pins[128]; 646 struct gpiobus_ivar *devi; 647 648 devi = GPIOBUS_IVAR(child); 649 memset(pins, 0, sizeof(pins)); 650 gpiobus_print_pins(devi, pins, sizeof(pins)); 651 if (devi->npins > 1) 652 device_printf(dev, "<unknown device> at pins %s", pins); 653 else 654 device_printf(dev, "<unknown device> at pin %s", pins); 655 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); 656 printf("\n"); 657 } 658 659 static int 660 gpiobus_print_child(device_t dev, device_t child) 661 { 662 char pins[128]; 663 int retval = 0; 664 struct gpiobus_ivar *devi; 665 666 devi = GPIOBUS_IVAR(child); 667 memset(pins, 0, sizeof(pins)); 668 retval += bus_print_child_header(dev, child); 669 if (devi->npins > 0) { 670 if (devi->npins > 1) 671 retval += printf(" at pins "); 672 else 673 retval += printf(" at pin "); 674 gpiobus_print_pins(devi, pins, sizeof(pins)); 675 retval += printf("%s", pins); 676 } 677 resource_list_print_type(&devi->rl, "irq", SYS_RES_IRQ, "%jd"); 678 retval += bus_print_child_footer(dev, child); 679 680 return (retval); 681 } 682 683 static int 684 gpiobus_child_location_str(device_t bus, device_t child, char *buf, 685 size_t buflen) 686 { 687 struct gpiobus_ivar *devi; 688 689 devi = GPIOBUS_IVAR(child); 690 if (devi->npins > 1) 691 strlcpy(buf, "pins=", buflen); 692 else 693 strlcpy(buf, "pin=", buflen); 694 gpiobus_print_pins(devi, buf, buflen); 695 696 return (0); 697 } 698 699 static int 700 gpiobus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 701 size_t buflen) 702 { 703 704 *buf = '\0'; 705 return (0); 706 } 707 708 static device_t 709 gpiobus_add_child(device_t dev, u_int order, const char *name, int unit) 710 { 711 device_t child; 712 struct gpiobus_ivar *devi; 713 714 child = device_add_child_ordered(dev, order, name, unit); 715 if (child == NULL) 716 return (child); 717 devi = malloc(sizeof(struct gpiobus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 718 if (devi == NULL) { 719 device_delete_child(dev, child); 720 return (NULL); 721 } 722 resource_list_init(&devi->rl); 723 device_set_ivars(child, devi); 724 725 return (child); 726 } 727 728 static int 729 gpiobus_rescan(device_t dev) 730 { 731 732 /* 733 * Re-scan is supposed to remove and add children, but if someone has 734 * deleted the hints for a child we attached earlier, we have no easy 735 * way to handle that. So this just attaches new children for whom new 736 * hints or drivers have arrived since we last tried. 737 */ 738 bus_enumerate_hinted_children(dev); 739 bus_generic_attach(dev); 740 return (0); 741 } 742 743 static void 744 gpiobus_hinted_child(device_t bus, const char *dname, int dunit) 745 { 746 struct gpiobus_softc *sc = GPIOBUS_SOFTC(bus); 747 struct gpiobus_ivar *devi; 748 device_t child; 749 const char *pins; 750 int irq, pinmask; 751 752 if (device_find_child(bus, dname, dunit) != NULL) { 753 return; 754 } 755 756 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 757 devi = GPIOBUS_IVAR(child); 758 if (resource_int_value(dname, dunit, "pins", &pinmask) == 0) { 759 if (gpiobus_parse_pins(sc, child, pinmask)) { 760 resource_list_free(&devi->rl); 761 free(devi, M_DEVBUF); 762 device_delete_child(bus, child); 763 return; 764 } 765 } 766 else if (resource_string_value(dname, dunit, "pin_list", &pins) == 0) { 767 if (gpiobus_parse_pin_list(sc, child, pins)) { 768 resource_list_free(&devi->rl); 769 free(devi, M_DEVBUF); 770 device_delete_child(bus, child); 771 return; 772 } 773 } 774 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 775 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0) 776 device_printf(bus, 777 "warning: bus_set_resource() failed\n"); 778 } 779 } 780 781 static int 782 gpiobus_set_resource(device_t dev, device_t child, int type, int rid, 783 rman_res_t start, rman_res_t count) 784 { 785 struct gpiobus_ivar *devi; 786 struct resource_list_entry *rle; 787 788 dprintf("%s: entry (%p, %p, %d, %d, %p, %ld)\n", 789 __func__, dev, child, type, rid, (void *)(intptr_t)start, count); 790 devi = GPIOBUS_IVAR(child); 791 rle = resource_list_add(&devi->rl, type, rid, start, 792 start + count - 1, count); 793 if (rle == NULL) 794 return (ENXIO); 795 796 return (0); 797 } 798 799 static int 800 gpiobus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 801 { 802 struct gpiobus_ivar *devi; 803 804 devi = GPIOBUS_IVAR(child); 805 switch (which) { 806 case GPIOBUS_IVAR_NPINS: 807 *result = devi->npins; 808 break; 809 case GPIOBUS_IVAR_PINS: 810 /* Children do not ever need to directly examine this. */ 811 return (ENOTSUP); 812 default: 813 return (ENOENT); 814 } 815 816 return (0); 817 } 818 819 static int 820 gpiobus_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 821 { 822 struct gpiobus_ivar *devi; 823 const uint32_t *ptr; 824 int i; 825 826 devi = GPIOBUS_IVAR(child); 827 switch (which) { 828 case GPIOBUS_IVAR_NPINS: 829 /* GPIO ivars are set once. */ 830 if (devi->npins != 0) { 831 return (EBUSY); 832 } 833 devi->npins = value; 834 if (gpiobus_alloc_ivars(devi) != 0) { 835 device_printf(child, "cannot allocate device ivars\n"); 836 devi->npins = 0; 837 return (ENOMEM); 838 } 839 break; 840 case GPIOBUS_IVAR_PINS: 841 ptr = (const uint32_t *)value; 842 for (i = 0; i < devi->npins; i++) 843 devi->pins[i] = ptr[i]; 844 if (gpiobus_acquire_child_pins(dev, child) != 0) 845 return (EBUSY); 846 break; 847 default: 848 return (ENOENT); 849 } 850 851 return (0); 852 } 853 854 static struct resource * 855 gpiobus_alloc_resource(device_t bus, device_t child, int type, int *rid, 856 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 857 { 858 struct gpiobus_softc *sc; 859 struct resource *rv; 860 struct resource_list *rl; 861 struct resource_list_entry *rle; 862 int isdefault; 863 864 if (type != SYS_RES_IRQ) 865 return (NULL); 866 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1); 867 rle = NULL; 868 if (isdefault) { 869 rl = BUS_GET_RESOURCE_LIST(bus, child); 870 if (rl == NULL) 871 return (NULL); 872 rle = resource_list_find(rl, type, *rid); 873 if (rle == NULL) 874 return (NULL); 875 if (rle->res != NULL) 876 panic("%s: resource entry is busy", __func__); 877 start = rle->start; 878 count = rle->count; 879 end = rle->end; 880 } 881 sc = device_get_softc(bus); 882 rv = rman_reserve_resource(&sc->sc_intr_rman, start, end, count, flags, 883 child); 884 if (rv == NULL) 885 return (NULL); 886 rman_set_rid(rv, *rid); 887 if ((flags & RF_ACTIVE) != 0 && 888 bus_activate_resource(child, type, *rid, rv) != 0) { 889 rman_release_resource(rv); 890 return (NULL); 891 } 892 893 return (rv); 894 } 895 896 static int 897 gpiobus_release_resource(device_t bus __unused, device_t child, int type, 898 int rid, struct resource *r) 899 { 900 int error; 901 902 if (rman_get_flags(r) & RF_ACTIVE) { 903 error = bus_deactivate_resource(child, type, rid, r); 904 if (error) 905 return (error); 906 } 907 908 return (rman_release_resource(r)); 909 } 910 911 static struct resource_list * 912 gpiobus_get_resource_list(device_t bus __unused, device_t child) 913 { 914 struct gpiobus_ivar *ivar; 915 916 ivar = GPIOBUS_IVAR(child); 917 918 return (&ivar->rl); 919 } 920 921 static int 922 gpiobus_acquire_bus(device_t busdev, device_t child, int how) 923 { 924 struct gpiobus_softc *sc; 925 926 sc = device_get_softc(busdev); 927 GPIOBUS_ASSERT_UNLOCKED(sc); 928 GPIOBUS_LOCK(sc); 929 if (sc->sc_owner != NULL) { 930 if (sc->sc_owner == child) 931 panic("%s: %s still owns the bus.", 932 device_get_nameunit(busdev), 933 device_get_nameunit(child)); 934 if (how == GPIOBUS_DONTWAIT) { 935 GPIOBUS_UNLOCK(sc); 936 return (EWOULDBLOCK); 937 } 938 while (sc->sc_owner != NULL) 939 mtx_sleep(sc, &sc->sc_mtx, 0, "gpiobuswait", 0); 940 } 941 sc->sc_owner = child; 942 GPIOBUS_UNLOCK(sc); 943 944 return (0); 945 } 946 947 static void 948 gpiobus_release_bus(device_t busdev, device_t child) 949 { 950 struct gpiobus_softc *sc; 951 952 sc = device_get_softc(busdev); 953 GPIOBUS_ASSERT_UNLOCKED(sc); 954 GPIOBUS_LOCK(sc); 955 if (sc->sc_owner == NULL) 956 panic("%s: %s releasing unowned bus.", 957 device_get_nameunit(busdev), 958 device_get_nameunit(child)); 959 if (sc->sc_owner != child) 960 panic("%s: %s trying to release bus owned by %s", 961 device_get_nameunit(busdev), 962 device_get_nameunit(child), 963 device_get_nameunit(sc->sc_owner)); 964 sc->sc_owner = NULL; 965 wakeup(sc); 966 GPIOBUS_UNLOCK(sc); 967 } 968 969 static int 970 gpiobus_pin_setflags(device_t dev, device_t child, uint32_t pin, 971 uint32_t flags) 972 { 973 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 974 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 975 uint32_t caps; 976 977 if (pin >= devi->npins) 978 return (EINVAL); 979 if (GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], &caps) != 0) 980 return (EINVAL); 981 if (gpio_check_flags(caps, flags) != 0) 982 return (EINVAL); 983 984 return (GPIO_PIN_SETFLAGS(sc->sc_dev, devi->pins[pin], flags)); 985 } 986 987 static int 988 gpiobus_pin_getflags(device_t dev, device_t child, uint32_t pin, 989 uint32_t *flags) 990 { 991 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 992 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 993 994 if (pin >= devi->npins) 995 return (EINVAL); 996 997 return GPIO_PIN_GETFLAGS(sc->sc_dev, devi->pins[pin], flags); 998 } 999 1000 static int 1001 gpiobus_pin_getcaps(device_t dev, device_t child, uint32_t pin, 1002 uint32_t *caps) 1003 { 1004 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 1005 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 1006 1007 if (pin >= devi->npins) 1008 return (EINVAL); 1009 1010 return GPIO_PIN_GETCAPS(sc->sc_dev, devi->pins[pin], caps); 1011 } 1012 1013 static int 1014 gpiobus_pin_set(device_t dev, device_t child, uint32_t pin, 1015 unsigned int value) 1016 { 1017 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 1018 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 1019 1020 if (pin >= devi->npins) 1021 return (EINVAL); 1022 1023 return GPIO_PIN_SET(sc->sc_dev, devi->pins[pin], value); 1024 } 1025 1026 static int 1027 gpiobus_pin_get(device_t dev, device_t child, uint32_t pin, 1028 unsigned int *value) 1029 { 1030 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 1031 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 1032 1033 if (pin >= devi->npins) 1034 return (EINVAL); 1035 1036 return GPIO_PIN_GET(sc->sc_dev, devi->pins[pin], value); 1037 } 1038 1039 static int 1040 gpiobus_pin_toggle(device_t dev, device_t child, uint32_t pin) 1041 { 1042 struct gpiobus_softc *sc = GPIOBUS_SOFTC(dev); 1043 struct gpiobus_ivar *devi = GPIOBUS_IVAR(child); 1044 1045 if (pin >= devi->npins) 1046 return (EINVAL); 1047 1048 return GPIO_PIN_TOGGLE(sc->sc_dev, devi->pins[pin]); 1049 } 1050 1051 static int 1052 gpiobus_pin_getname(device_t dev, uint32_t pin, char *name) 1053 { 1054 struct gpiobus_softc *sc; 1055 1056 sc = GPIOBUS_SOFTC(dev); 1057 if (pin > sc->sc_npins) 1058 return (EINVAL); 1059 /* Did we have a name for this pin ? */ 1060 if (sc->sc_pins[pin].name != NULL) { 1061 memcpy(name, sc->sc_pins[pin].name, GPIOMAXNAME); 1062 return (0); 1063 } 1064 1065 /* Return the default pin name. */ 1066 return (GPIO_PIN_GETNAME(device_get_parent(dev), pin, name)); 1067 } 1068 1069 static int 1070 gpiobus_pin_setname(device_t dev, uint32_t pin, const char *name) 1071 { 1072 struct gpiobus_softc *sc; 1073 1074 sc = GPIOBUS_SOFTC(dev); 1075 if (pin > sc->sc_npins) 1076 return (EINVAL); 1077 if (name == NULL) 1078 return (EINVAL); 1079 /* Save the pin name. */ 1080 if (sc->sc_pins[pin].name == NULL) 1081 sc->sc_pins[pin].name = malloc(GPIOMAXNAME, M_DEVBUF, 1082 M_WAITOK | M_ZERO); 1083 strlcpy(sc->sc_pins[pin].name, name, GPIOMAXNAME); 1084 1085 return (0); 1086 } 1087 1088 static device_method_t gpiobus_methods[] = { 1089 /* Device interface */ 1090 DEVMETHOD(device_probe, gpiobus_probe), 1091 DEVMETHOD(device_attach, gpiobus_attach), 1092 DEVMETHOD(device_detach, gpiobus_detach), 1093 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1094 DEVMETHOD(device_suspend, gpiobus_suspend), 1095 DEVMETHOD(device_resume, gpiobus_resume), 1096 1097 /* Bus interface */ 1098 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 1099 DEVMETHOD(bus_config_intr, bus_generic_config_intr), 1100 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1101 DEVMETHOD(bus_set_resource, gpiobus_set_resource), 1102 DEVMETHOD(bus_alloc_resource, gpiobus_alloc_resource), 1103 DEVMETHOD(bus_release_resource, gpiobus_release_resource), 1104 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 1105 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 1106 DEVMETHOD(bus_get_resource_list, gpiobus_get_resource_list), 1107 DEVMETHOD(bus_add_child, gpiobus_add_child), 1108 DEVMETHOD(bus_rescan, gpiobus_rescan), 1109 DEVMETHOD(bus_probe_nomatch, gpiobus_probe_nomatch), 1110 DEVMETHOD(bus_print_child, gpiobus_print_child), 1111 DEVMETHOD(bus_child_pnpinfo_str, gpiobus_child_pnpinfo_str), 1112 DEVMETHOD(bus_child_location_str, gpiobus_child_location_str), 1113 DEVMETHOD(bus_hinted_child, gpiobus_hinted_child), 1114 DEVMETHOD(bus_read_ivar, gpiobus_read_ivar), 1115 DEVMETHOD(bus_write_ivar, gpiobus_write_ivar), 1116 1117 /* GPIO protocol */ 1118 DEVMETHOD(gpiobus_acquire_bus, gpiobus_acquire_bus), 1119 DEVMETHOD(gpiobus_release_bus, gpiobus_release_bus), 1120 DEVMETHOD(gpiobus_pin_getflags, gpiobus_pin_getflags), 1121 DEVMETHOD(gpiobus_pin_getcaps, gpiobus_pin_getcaps), 1122 DEVMETHOD(gpiobus_pin_setflags, gpiobus_pin_setflags), 1123 DEVMETHOD(gpiobus_pin_get, gpiobus_pin_get), 1124 DEVMETHOD(gpiobus_pin_set, gpiobus_pin_set), 1125 DEVMETHOD(gpiobus_pin_toggle, gpiobus_pin_toggle), 1126 DEVMETHOD(gpiobus_pin_getname, gpiobus_pin_getname), 1127 DEVMETHOD(gpiobus_pin_setname, gpiobus_pin_setname), 1128 1129 DEVMETHOD_END 1130 }; 1131 1132 driver_t gpiobus_driver = { 1133 "gpiobus", 1134 gpiobus_methods, 1135 sizeof(struct gpiobus_softc) 1136 }; 1137 1138 devclass_t gpiobus_devclass; 1139 1140 EARLY_DRIVER_MODULE(gpiobus, gpio, gpiobus_driver, gpiobus_devclass, 0, 0, 1141 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 1142 MODULE_VERSION(gpiobus, 1); 1143