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