1 /*- 2 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@FreeBSD.org> 3 * Copyright (c) 2012-2015 Luiz Otavio O Souza <loos@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_platform.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/bus.h> 36 #include <sys/gpio.h> 37 #include <sys/interrupt.h> 38 #include <sys/kernel.h> 39 #include <sys/lock.h> 40 #include <sys/module.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/rman.h> 44 #include <sys/sysctl.h> 45 46 #include <machine/bus.h> 47 #include <machine/intr.h> 48 49 #include <dev/gpio/gpiobusvar.h> 50 #include <dev/ofw/ofw_bus.h> 51 52 #include <arm/broadcom/bcm2835/bcm2835_gpio.h> 53 54 #include "gpio_if.h" 55 56 #ifdef INTRNG 57 #include "pic_if.h" 58 #endif 59 60 #ifdef DEBUG 61 #define dprintf(fmt, args...) do { printf("%s(): ", __func__); \ 62 printf(fmt,##args); } while (0) 63 #else 64 #define dprintf(fmt, args...) 65 #endif 66 67 #define BCM_GPIO_IRQS 4 68 #define BCM_GPIO_PINS 54 69 #define BCM_GPIO_PINS_PER_BANK 32 70 71 #ifdef INTRNG 72 #define BCM_GPIO_DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \ 73 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN | GPIO_INTR_LEVEL_LOW | \ 74 GPIO_INTR_LEVEL_HIGH | GPIO_INTR_EDGE_RISING | \ 75 GPIO_INTR_EDGE_FALLING | GPIO_INTR_EDGE_BOTH) 76 #else 77 #define BCM_GPIO_DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \ 78 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN) 79 #endif 80 81 static struct resource_spec bcm_gpio_res_spec[] = { 82 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 83 { SYS_RES_IRQ, 0, RF_ACTIVE }, /* bank 0 interrupt */ 84 { SYS_RES_IRQ, 1, RF_ACTIVE }, /* bank 1 interrupt */ 85 { SYS_RES_IRQ, 2, RF_ACTIVE }, /* bank 1 interrupt (mirrored) */ 86 { SYS_RES_IRQ, 3, RF_ACTIVE }, /* bank 0-1 interrupt (united) */ 87 { -1, 0, 0 } 88 }; 89 90 struct bcm_gpio_sysctl { 91 struct bcm_gpio_softc *sc; 92 uint32_t pin; 93 }; 94 95 #ifdef INTRNG 96 struct bcm_gpio_irqsrc { 97 struct intr_irqsrc bgi_isrc; 98 uint32_t bgi_irq; 99 uint32_t bgi_mode; 100 uint32_t bgi_mask; 101 }; 102 #endif 103 104 struct bcm_gpio_softc { 105 device_t sc_dev; 106 device_t sc_busdev; 107 struct mtx sc_mtx; 108 struct resource * sc_res[BCM_GPIO_IRQS + 1]; 109 bus_space_tag_t sc_bst; 110 bus_space_handle_t sc_bsh; 111 void * sc_intrhand[BCM_GPIO_IRQS]; 112 int sc_gpio_npins; 113 int sc_ro_npins; 114 int sc_ro_pins[BCM_GPIO_PINS]; 115 struct gpio_pin sc_gpio_pins[BCM_GPIO_PINS]; 116 #ifndef INTRNG 117 struct intr_event * sc_events[BCM_GPIO_PINS]; 118 #endif 119 struct bcm_gpio_sysctl sc_sysctl[BCM_GPIO_PINS]; 120 #ifdef INTRNG 121 struct bcm_gpio_irqsrc sc_isrcs[BCM_GPIO_PINS]; 122 #else 123 enum intr_trigger sc_irq_trigger[BCM_GPIO_PINS]; 124 enum intr_polarity sc_irq_polarity[BCM_GPIO_PINS]; 125 #endif 126 }; 127 128 enum bcm_gpio_pud { 129 BCM_GPIO_NONE, 130 BCM_GPIO_PULLDOWN, 131 BCM_GPIO_PULLUP, 132 }; 133 134 #define BCM_GPIO_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx) 135 #define BCM_GPIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx) 136 #define BCM_GPIO_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED) 137 #define BCM_GPIO_WRITE(_sc, _off, _val) \ 138 bus_space_write_4((_sc)->sc_bst, (_sc)->sc_bsh, _off, _val) 139 #define BCM_GPIO_READ(_sc, _off) \ 140 bus_space_read_4((_sc)->sc_bst, (_sc)->sc_bsh, _off) 141 #define BCM_GPIO_CLEAR_BITS(_sc, _off, _bits) \ 142 BCM_GPIO_WRITE(_sc, _off, BCM_GPIO_READ(_sc, _off) & ~(_bits)) 143 #define BCM_GPIO_SET_BITS(_sc, _off, _bits) \ 144 BCM_GPIO_WRITE(_sc, _off, BCM_GPIO_READ(_sc, _off) | _bits) 145 #define BCM_GPIO_BANK(a) (a / BCM_GPIO_PINS_PER_BANK) 146 #define BCM_GPIO_MASK(a) (1U << (a % BCM_GPIO_PINS_PER_BANK)) 147 148 #define BCM_GPIO_GPFSEL(_bank) (0x00 + _bank * 4) /* Function Select */ 149 #define BCM_GPIO_GPSET(_bank) (0x1c + _bank * 4) /* Pin Out Set */ 150 #define BCM_GPIO_GPCLR(_bank) (0x28 + _bank * 4) /* Pin Out Clear */ 151 #define BCM_GPIO_GPLEV(_bank) (0x34 + _bank * 4) /* Pin Level */ 152 #define BCM_GPIO_GPEDS(_bank) (0x40 + _bank * 4) /* Event Status */ 153 #define BCM_GPIO_GPREN(_bank) (0x4c + _bank * 4) /* Rising Edge irq */ 154 #define BCM_GPIO_GPFEN(_bank) (0x58 + _bank * 4) /* Falling Edge irq */ 155 #define BCM_GPIO_GPHEN(_bank) (0x64 + _bank * 4) /* High Level irq */ 156 #define BCM_GPIO_GPLEN(_bank) (0x70 + _bank * 4) /* Low Level irq */ 157 #define BCM_GPIO_GPAREN(_bank) (0x7c + _bank * 4) /* Async Rising Edge */ 158 #define BCM_GPIO_GPAFEN(_bank) (0x88 + _bank * 4) /* Async Falling Egde */ 159 #define BCM_GPIO_GPPUD(_bank) (0x94) /* Pin Pull up/down */ 160 #define BCM_GPIO_GPPUDCLK(_bank) (0x98 + _bank * 4) /* Pin Pull up clock */ 161 162 static struct bcm_gpio_softc *bcm_gpio_sc = NULL; 163 164 #ifdef INTRNG 165 static int bcm_gpio_intr_bank0(void *arg); 166 static int bcm_gpio_intr_bank1(void *arg); 167 static int bcm_gpio_pic_attach(struct bcm_gpio_softc *sc); 168 static int bcm_gpio_pic_detach(struct bcm_gpio_softc *sc); 169 #endif 170 171 static int 172 bcm_gpio_pin_is_ro(struct bcm_gpio_softc *sc, int pin) 173 { 174 int i; 175 176 for (i = 0; i < sc->sc_ro_npins; i++) 177 if (pin == sc->sc_ro_pins[i]) 178 return (1); 179 return (0); 180 } 181 182 static uint32_t 183 bcm_gpio_get_function(struct bcm_gpio_softc *sc, uint32_t pin) 184 { 185 uint32_t bank, func, offset; 186 187 /* Five banks, 10 pins per bank, 3 bits per pin. */ 188 bank = pin / 10; 189 offset = (pin - bank * 10) * 3; 190 191 BCM_GPIO_LOCK(sc); 192 func = (BCM_GPIO_READ(sc, BCM_GPIO_GPFSEL(bank)) >> offset) & 7; 193 BCM_GPIO_UNLOCK(sc); 194 195 return (func); 196 } 197 198 static void 199 bcm_gpio_func_str(uint32_t nfunc, char *buf, int bufsize) 200 { 201 202 switch (nfunc) { 203 case BCM_GPIO_INPUT: 204 strncpy(buf, "input", bufsize); 205 break; 206 case BCM_GPIO_OUTPUT: 207 strncpy(buf, "output", bufsize); 208 break; 209 case BCM_GPIO_ALT0: 210 strncpy(buf, "alt0", bufsize); 211 break; 212 case BCM_GPIO_ALT1: 213 strncpy(buf, "alt1", bufsize); 214 break; 215 case BCM_GPIO_ALT2: 216 strncpy(buf, "alt2", bufsize); 217 break; 218 case BCM_GPIO_ALT3: 219 strncpy(buf, "alt3", bufsize); 220 break; 221 case BCM_GPIO_ALT4: 222 strncpy(buf, "alt4", bufsize); 223 break; 224 case BCM_GPIO_ALT5: 225 strncpy(buf, "alt5", bufsize); 226 break; 227 default: 228 strncpy(buf, "invalid", bufsize); 229 } 230 } 231 232 static int 233 bcm_gpio_str_func(char *func, uint32_t *nfunc) 234 { 235 236 if (strcasecmp(func, "input") == 0) 237 *nfunc = BCM_GPIO_INPUT; 238 else if (strcasecmp(func, "output") == 0) 239 *nfunc = BCM_GPIO_OUTPUT; 240 else if (strcasecmp(func, "alt0") == 0) 241 *nfunc = BCM_GPIO_ALT0; 242 else if (strcasecmp(func, "alt1") == 0) 243 *nfunc = BCM_GPIO_ALT1; 244 else if (strcasecmp(func, "alt2") == 0) 245 *nfunc = BCM_GPIO_ALT2; 246 else if (strcasecmp(func, "alt3") == 0) 247 *nfunc = BCM_GPIO_ALT3; 248 else if (strcasecmp(func, "alt4") == 0) 249 *nfunc = BCM_GPIO_ALT4; 250 else if (strcasecmp(func, "alt5") == 0) 251 *nfunc = BCM_GPIO_ALT5; 252 else 253 return (-1); 254 255 return (0); 256 } 257 258 static uint32_t 259 bcm_gpio_func_flag(uint32_t nfunc) 260 { 261 262 switch (nfunc) { 263 case BCM_GPIO_INPUT: 264 return (GPIO_PIN_INPUT); 265 case BCM_GPIO_OUTPUT: 266 return (GPIO_PIN_OUTPUT); 267 } 268 return (0); 269 } 270 271 static void 272 bcm_gpio_set_function(struct bcm_gpio_softc *sc, uint32_t pin, uint32_t f) 273 { 274 uint32_t bank, data, offset; 275 276 /* Must be called with lock held. */ 277 BCM_GPIO_LOCK_ASSERT(sc); 278 279 /* Five banks, 10 pins per bank, 3 bits per pin. */ 280 bank = pin / 10; 281 offset = (pin - bank * 10) * 3; 282 283 data = BCM_GPIO_READ(sc, BCM_GPIO_GPFSEL(bank)); 284 data &= ~(7 << offset); 285 data |= (f << offset); 286 BCM_GPIO_WRITE(sc, BCM_GPIO_GPFSEL(bank), data); 287 } 288 289 static void 290 bcm_gpio_set_pud(struct bcm_gpio_softc *sc, uint32_t pin, uint32_t state) 291 { 292 uint32_t bank; 293 294 /* Must be called with lock held. */ 295 BCM_GPIO_LOCK_ASSERT(sc); 296 297 bank = BCM_GPIO_BANK(pin); 298 BCM_GPIO_WRITE(sc, BCM_GPIO_GPPUD(0), state); 299 BCM_GPIO_WRITE(sc, BCM_GPIO_GPPUDCLK(bank), BCM_GPIO_MASK(pin)); 300 BCM_GPIO_WRITE(sc, BCM_GPIO_GPPUD(0), 0); 301 BCM_GPIO_WRITE(sc, BCM_GPIO_GPPUDCLK(bank), 0); 302 } 303 304 void 305 bcm_gpio_set_alternate(device_t dev, uint32_t pin, uint32_t nfunc) 306 { 307 struct bcm_gpio_softc *sc; 308 int i; 309 310 sc = device_get_softc(dev); 311 BCM_GPIO_LOCK(sc); 312 313 /* Disable pull-up or pull-down on pin. */ 314 bcm_gpio_set_pud(sc, pin, BCM_GPIO_NONE); 315 316 /* And now set the pin function. */ 317 bcm_gpio_set_function(sc, pin, nfunc); 318 319 /* Update the pin flags. */ 320 for (i = 0; i < sc->sc_gpio_npins; i++) { 321 if (sc->sc_gpio_pins[i].gp_pin == pin) 322 break; 323 } 324 if (i < sc->sc_gpio_npins) 325 sc->sc_gpio_pins[i].gp_flags = bcm_gpio_func_flag(nfunc); 326 327 BCM_GPIO_UNLOCK(sc); 328 } 329 330 static void 331 bcm_gpio_pin_configure(struct bcm_gpio_softc *sc, struct gpio_pin *pin, 332 unsigned int flags) 333 { 334 335 BCM_GPIO_LOCK(sc); 336 337 /* 338 * Manage input/output. 339 */ 340 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) { 341 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT); 342 if (flags & GPIO_PIN_OUTPUT) { 343 pin->gp_flags |= GPIO_PIN_OUTPUT; 344 bcm_gpio_set_function(sc, pin->gp_pin, 345 BCM_GPIO_OUTPUT); 346 } else { 347 pin->gp_flags |= GPIO_PIN_INPUT; 348 bcm_gpio_set_function(sc, pin->gp_pin, 349 BCM_GPIO_INPUT); 350 } 351 } 352 353 /* Manage Pull-up/pull-down. */ 354 pin->gp_flags &= ~(GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN); 355 if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) { 356 if (flags & GPIO_PIN_PULLUP) { 357 pin->gp_flags |= GPIO_PIN_PULLUP; 358 bcm_gpio_set_pud(sc, pin->gp_pin, BCM_GPIO_PULLUP); 359 } else { 360 pin->gp_flags |= GPIO_PIN_PULLDOWN; 361 bcm_gpio_set_pud(sc, pin->gp_pin, BCM_GPIO_PULLDOWN); 362 } 363 } else 364 bcm_gpio_set_pud(sc, pin->gp_pin, BCM_GPIO_NONE); 365 366 BCM_GPIO_UNLOCK(sc); 367 } 368 369 static device_t 370 bcm_gpio_get_bus(device_t dev) 371 { 372 struct bcm_gpio_softc *sc; 373 374 sc = device_get_softc(dev); 375 376 return (sc->sc_busdev); 377 } 378 379 static int 380 bcm_gpio_pin_max(device_t dev, int *maxpin) 381 { 382 383 *maxpin = BCM_GPIO_PINS - 1; 384 return (0); 385 } 386 387 static int 388 bcm_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps) 389 { 390 struct bcm_gpio_softc *sc = device_get_softc(dev); 391 int i; 392 393 for (i = 0; i < sc->sc_gpio_npins; i++) { 394 if (sc->sc_gpio_pins[i].gp_pin == pin) 395 break; 396 } 397 398 if (i >= sc->sc_gpio_npins) 399 return (EINVAL); 400 401 BCM_GPIO_LOCK(sc); 402 *caps = sc->sc_gpio_pins[i].gp_caps; 403 BCM_GPIO_UNLOCK(sc); 404 405 return (0); 406 } 407 408 static int 409 bcm_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags) 410 { 411 struct bcm_gpio_softc *sc = device_get_softc(dev); 412 int i; 413 414 for (i = 0; i < sc->sc_gpio_npins; i++) { 415 if (sc->sc_gpio_pins[i].gp_pin == pin) 416 break; 417 } 418 419 if (i >= sc->sc_gpio_npins) 420 return (EINVAL); 421 422 BCM_GPIO_LOCK(sc); 423 *flags = sc->sc_gpio_pins[i].gp_flags; 424 BCM_GPIO_UNLOCK(sc); 425 426 return (0); 427 } 428 429 static int 430 bcm_gpio_pin_getname(device_t dev, uint32_t pin, char *name) 431 { 432 struct bcm_gpio_softc *sc = device_get_softc(dev); 433 int i; 434 435 for (i = 0; i < sc->sc_gpio_npins; i++) { 436 if (sc->sc_gpio_pins[i].gp_pin == pin) 437 break; 438 } 439 440 if (i >= sc->sc_gpio_npins) 441 return (EINVAL); 442 443 BCM_GPIO_LOCK(sc); 444 memcpy(name, sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME); 445 BCM_GPIO_UNLOCK(sc); 446 447 return (0); 448 } 449 450 static int 451 bcm_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) 452 { 453 struct bcm_gpio_softc *sc = device_get_softc(dev); 454 int i; 455 456 for (i = 0; i < sc->sc_gpio_npins; i++) { 457 if (sc->sc_gpio_pins[i].gp_pin == pin) 458 break; 459 } 460 461 if (i >= sc->sc_gpio_npins) 462 return (EINVAL); 463 464 /* We never touch on read-only/reserved pins. */ 465 if (bcm_gpio_pin_is_ro(sc, pin)) 466 return (EINVAL); 467 468 bcm_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags); 469 470 return (0); 471 } 472 473 static int 474 bcm_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value) 475 { 476 struct bcm_gpio_softc *sc = device_get_softc(dev); 477 uint32_t bank, reg; 478 int i; 479 480 for (i = 0; i < sc->sc_gpio_npins; i++) { 481 if (sc->sc_gpio_pins[i].gp_pin == pin) 482 break; 483 } 484 if (i >= sc->sc_gpio_npins) 485 return (EINVAL); 486 /* We never write to read-only/reserved pins. */ 487 if (bcm_gpio_pin_is_ro(sc, pin)) 488 return (EINVAL); 489 BCM_GPIO_LOCK(sc); 490 bank = BCM_GPIO_BANK(pin); 491 if (value) 492 reg = BCM_GPIO_GPSET(bank); 493 else 494 reg = BCM_GPIO_GPCLR(bank); 495 BCM_GPIO_WRITE(sc, reg, BCM_GPIO_MASK(pin)); 496 BCM_GPIO_UNLOCK(sc); 497 498 return (0); 499 } 500 501 static int 502 bcm_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val) 503 { 504 struct bcm_gpio_softc *sc = device_get_softc(dev); 505 uint32_t bank, reg_data; 506 int i; 507 508 for (i = 0; i < sc->sc_gpio_npins; i++) { 509 if (sc->sc_gpio_pins[i].gp_pin == pin) 510 break; 511 } 512 if (i >= sc->sc_gpio_npins) 513 return (EINVAL); 514 bank = BCM_GPIO_BANK(pin); 515 BCM_GPIO_LOCK(sc); 516 reg_data = BCM_GPIO_READ(sc, BCM_GPIO_GPLEV(bank)); 517 BCM_GPIO_UNLOCK(sc); 518 *val = (reg_data & BCM_GPIO_MASK(pin)) ? 1 : 0; 519 520 return (0); 521 } 522 523 static int 524 bcm_gpio_pin_toggle(device_t dev, uint32_t pin) 525 { 526 struct bcm_gpio_softc *sc = device_get_softc(dev); 527 uint32_t bank, data, reg; 528 int i; 529 530 for (i = 0; i < sc->sc_gpio_npins; i++) { 531 if (sc->sc_gpio_pins[i].gp_pin == pin) 532 break; 533 } 534 if (i >= sc->sc_gpio_npins) 535 return (EINVAL); 536 /* We never write to read-only/reserved pins. */ 537 if (bcm_gpio_pin_is_ro(sc, pin)) 538 return (EINVAL); 539 BCM_GPIO_LOCK(sc); 540 bank = BCM_GPIO_BANK(pin); 541 data = BCM_GPIO_READ(sc, BCM_GPIO_GPLEV(bank)); 542 if (data & BCM_GPIO_MASK(pin)) 543 reg = BCM_GPIO_GPCLR(bank); 544 else 545 reg = BCM_GPIO_GPSET(bank); 546 BCM_GPIO_WRITE(sc, reg, BCM_GPIO_MASK(pin)); 547 BCM_GPIO_UNLOCK(sc); 548 549 return (0); 550 } 551 552 static int 553 bcm_gpio_func_proc(SYSCTL_HANDLER_ARGS) 554 { 555 char buf[16]; 556 struct bcm_gpio_softc *sc; 557 struct bcm_gpio_sysctl *sc_sysctl; 558 uint32_t nfunc; 559 int error; 560 561 sc_sysctl = arg1; 562 sc = sc_sysctl->sc; 563 564 /* Get the current pin function. */ 565 nfunc = bcm_gpio_get_function(sc, sc_sysctl->pin); 566 bcm_gpio_func_str(nfunc, buf, sizeof(buf)); 567 568 error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 569 if (error != 0 || req->newptr == NULL) 570 return (error); 571 /* Ignore changes on read-only pins. */ 572 if (bcm_gpio_pin_is_ro(sc, sc_sysctl->pin)) 573 return (0); 574 /* Parse the user supplied string and check for a valid pin function. */ 575 if (bcm_gpio_str_func(buf, &nfunc) != 0) 576 return (EINVAL); 577 578 /* Update the pin alternate function. */ 579 bcm_gpio_set_alternate(sc->sc_dev, sc_sysctl->pin, nfunc); 580 581 return (0); 582 } 583 584 static void 585 bcm_gpio_sysctl_init(struct bcm_gpio_softc *sc) 586 { 587 char pinbuf[3]; 588 struct bcm_gpio_sysctl *sc_sysctl; 589 struct sysctl_ctx_list *ctx; 590 struct sysctl_oid *tree_node, *pin_node, *pinN_node; 591 struct sysctl_oid_list *tree, *pin_tree, *pinN_tree; 592 int i; 593 594 /* 595 * Add per-pin sysctl tree/handlers. 596 */ 597 ctx = device_get_sysctl_ctx(sc->sc_dev); 598 tree_node = device_get_sysctl_tree(sc->sc_dev); 599 tree = SYSCTL_CHILDREN(tree_node); 600 pin_node = SYSCTL_ADD_NODE(ctx, tree, OID_AUTO, "pin", 601 CTLFLAG_RD, NULL, "GPIO Pins"); 602 pin_tree = SYSCTL_CHILDREN(pin_node); 603 604 for (i = 0; i < sc->sc_gpio_npins; i++) { 605 606 snprintf(pinbuf, sizeof(pinbuf), "%d", i); 607 pinN_node = SYSCTL_ADD_NODE(ctx, pin_tree, OID_AUTO, pinbuf, 608 CTLFLAG_RD, NULL, "GPIO Pin"); 609 pinN_tree = SYSCTL_CHILDREN(pinN_node); 610 611 sc->sc_sysctl[i].sc = sc; 612 sc_sysctl = &sc->sc_sysctl[i]; 613 sc_sysctl->sc = sc; 614 sc_sysctl->pin = sc->sc_gpio_pins[i].gp_pin; 615 SYSCTL_ADD_PROC(ctx, pinN_tree, OID_AUTO, "function", 616 CTLFLAG_RW | CTLTYPE_STRING, sc_sysctl, 617 sizeof(struct bcm_gpio_sysctl), bcm_gpio_func_proc, 618 "A", "Pin Function"); 619 } 620 } 621 622 static int 623 bcm_gpio_get_ro_pins(struct bcm_gpio_softc *sc, phandle_t node, 624 const char *propname, const char *label) 625 { 626 int i, need_comma, npins, range_start, range_stop; 627 pcell_t *pins; 628 629 /* Get the property data. */ 630 npins = OF_getencprop_alloc(node, propname, sizeof(*pins), 631 (void **)&pins); 632 if (npins < 0) 633 return (-1); 634 if (npins == 0) { 635 OF_prop_free(pins); 636 return (0); 637 } 638 for (i = 0; i < npins; i++) 639 sc->sc_ro_pins[i + sc->sc_ro_npins] = pins[i]; 640 sc->sc_ro_npins += npins; 641 need_comma = 0; 642 device_printf(sc->sc_dev, "%s pins: ", label); 643 range_start = range_stop = pins[0]; 644 for (i = 1; i < npins; i++) { 645 if (pins[i] != range_stop + 1) { 646 if (need_comma) 647 printf(","); 648 if (range_start != range_stop) 649 printf("%d-%d", range_start, range_stop); 650 else 651 printf("%d", range_start); 652 range_start = range_stop = pins[i]; 653 need_comma = 1; 654 } else 655 range_stop++; 656 } 657 if (need_comma) 658 printf(","); 659 if (range_start != range_stop) 660 printf("%d-%d.\n", range_start, range_stop); 661 else 662 printf("%d.\n", range_start); 663 OF_prop_free(pins); 664 665 return (0); 666 } 667 668 static int 669 bcm_gpio_get_reserved_pins(struct bcm_gpio_softc *sc) 670 { 671 char *name; 672 phandle_t gpio, node, reserved; 673 ssize_t len; 674 675 /* Get read-only pins. */ 676 gpio = ofw_bus_get_node(sc->sc_dev); 677 if (bcm_gpio_get_ro_pins(sc, gpio, "broadcom,read-only", 678 "read-only") != 0) 679 return (-1); 680 /* Traverse the GPIO subnodes to find the reserved pins node. */ 681 reserved = 0; 682 node = OF_child(gpio); 683 while ((node != 0) && (reserved == 0)) { 684 len = OF_getprop_alloc(node, "name", 1, (void **)&name); 685 if (len == -1) 686 return (-1); 687 if (strcmp(name, "reserved") == 0) 688 reserved = node; 689 OF_prop_free(name); 690 node = OF_peer(node); 691 } 692 if (reserved == 0) 693 return (-1); 694 /* Get the reserved pins. */ 695 if (bcm_gpio_get_ro_pins(sc, reserved, "broadcom,pins", 696 "reserved") != 0) 697 return (-1); 698 699 return (0); 700 } 701 702 #ifndef INTRNG 703 static int 704 bcm_gpio_intr(void *arg) 705 { 706 int bank_last, irq; 707 struct bcm_gpio_softc *sc; 708 struct intr_event *event; 709 uint32_t bank, mask, reg; 710 711 sc = (struct bcm_gpio_softc *)arg; 712 reg = 0; 713 bank_last = -1; 714 for (irq = 0; irq < BCM_GPIO_PINS; irq++) { 715 bank = BCM_GPIO_BANK(irq); 716 mask = BCM_GPIO_MASK(irq); 717 if (bank != bank_last) { 718 reg = BCM_GPIO_READ(sc, BCM_GPIO_GPEDS(bank)); 719 bank_last = bank; 720 } 721 if (reg & mask) { 722 event = sc->sc_events[irq]; 723 if (event != NULL && !TAILQ_EMPTY(&event->ie_handlers)) 724 intr_event_handle(event, NULL); 725 else { 726 device_printf(sc->sc_dev, "Stray IRQ %d\n", 727 irq); 728 } 729 /* Clear the Status bit by writing '1' to it. */ 730 BCM_GPIO_WRITE(sc, BCM_GPIO_GPEDS(bank), mask); 731 } 732 } 733 734 return (FILTER_HANDLED); 735 } 736 #endif 737 738 static int 739 bcm_gpio_probe(device_t dev) 740 { 741 742 if (!ofw_bus_status_okay(dev)) 743 return (ENXIO); 744 745 if (!ofw_bus_is_compatible(dev, "broadcom,bcm2835-gpio")) 746 return (ENXIO); 747 748 device_set_desc(dev, "BCM2708/2835 GPIO controller"); 749 return (BUS_PROBE_DEFAULT); 750 } 751 752 #ifdef INTRNG 753 static int 754 bcm_gpio_intr_attach(device_t dev) 755 { 756 struct bcm_gpio_softc *sc; 757 758 /* 759 * Only first two interrupt lines are used. Third line is 760 * mirrored second line and forth line is common for all banks. 761 */ 762 sc = device_get_softc(dev); 763 if (sc->sc_res[1] == NULL || sc->sc_res[2] == NULL) 764 return (-1); 765 766 if (bcm_gpio_pic_attach(sc) != 0) { 767 device_printf(dev, "unable to attach PIC\n"); 768 return (-1); 769 } 770 if (bus_setup_intr(dev, sc->sc_res[1], INTR_TYPE_MISC | INTR_MPSAFE, 771 bcm_gpio_intr_bank0, NULL, sc, &sc->sc_intrhand[0]) != 0) 772 return (-1); 773 if (bus_setup_intr(dev, sc->sc_res[2], INTR_TYPE_MISC | INTR_MPSAFE, 774 bcm_gpio_intr_bank1, NULL, sc, &sc->sc_intrhand[1]) != 0) 775 return (-1); 776 777 return (0); 778 } 779 780 static void 781 bcm_gpio_intr_detach(device_t dev) 782 { 783 struct bcm_gpio_softc *sc; 784 785 sc = device_get_softc(dev); 786 if (sc->sc_intrhand[0] != NULL) 787 bus_teardown_intr(dev, sc->sc_res[1], sc->sc_intrhand[0]); 788 if (sc->sc_intrhand[1] != NULL) 789 bus_teardown_intr(dev, sc->sc_res[2], sc->sc_intrhand[1]); 790 791 bcm_gpio_pic_detach(sc); 792 } 793 794 #else 795 static int 796 bcm_gpio_intr_attach(device_t dev) 797 { 798 struct bcm_gpio_softc *sc; 799 int i; 800 801 sc = device_get_softc(dev); 802 for (i = 0; i < BCM_GPIO_IRQS; i++) { 803 if (bus_setup_intr(dev, sc->sc_res[i + 1], 804 INTR_TYPE_MISC | INTR_MPSAFE, bcm_gpio_intr, 805 NULL, sc, &sc->sc_intrhand[i]) != 0) { 806 return (-1); 807 } 808 } 809 810 return (0); 811 } 812 813 static void 814 bcm_gpio_intr_detach(device_t dev) 815 { 816 struct bcm_gpio_softc *sc; 817 int i; 818 819 sc = device_get_softc(dev); 820 for (i = 0; i < BCM_GPIO_IRQS; i++) { 821 if (sc->sc_intrhand[i]) { 822 bus_teardown_intr(dev, sc->sc_res[i + 1], 823 sc->sc_intrhand[i]); 824 } 825 } 826 } 827 #endif 828 829 static int 830 bcm_gpio_attach(device_t dev) 831 { 832 int i, j; 833 phandle_t gpio; 834 struct bcm_gpio_softc *sc; 835 uint32_t func; 836 837 if (bcm_gpio_sc != NULL) 838 return (ENXIO); 839 840 bcm_gpio_sc = sc = device_get_softc(dev); 841 sc->sc_dev = dev; 842 mtx_init(&sc->sc_mtx, "bcm gpio", "gpio", MTX_SPIN); 843 if (bus_alloc_resources(dev, bcm_gpio_res_spec, sc->sc_res) != 0) { 844 device_printf(dev, "cannot allocate resources\n"); 845 goto fail; 846 } 847 sc->sc_bst = rman_get_bustag(sc->sc_res[0]); 848 sc->sc_bsh = rman_get_bushandle(sc->sc_res[0]); 849 /* Setup the GPIO interrupt handler. */ 850 if (bcm_gpio_intr_attach(dev)) { 851 device_printf(dev, "unable to setup the gpio irq handler\n"); 852 goto fail; 853 } 854 /* Find our node. */ 855 gpio = ofw_bus_get_node(sc->sc_dev); 856 if (!OF_hasprop(gpio, "gpio-controller")) 857 /* Node is not a GPIO controller. */ 858 goto fail; 859 /* 860 * Find the read-only pins. These are pins we never touch or bad 861 * things could happen. 862 */ 863 if (bcm_gpio_get_reserved_pins(sc) == -1) 864 goto fail; 865 /* Initialize the software controlled pins. */ 866 for (i = 0, j = 0; j < BCM_GPIO_PINS; j++) { 867 snprintf(sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME, 868 "pin %d", j); 869 func = bcm_gpio_get_function(sc, j); 870 sc->sc_gpio_pins[i].gp_pin = j; 871 sc->sc_gpio_pins[i].gp_caps = BCM_GPIO_DEFAULT_CAPS; 872 sc->sc_gpio_pins[i].gp_flags = bcm_gpio_func_flag(func); 873 #ifndef INTRNG 874 /* The default is active-low interrupts. */ 875 sc->sc_irq_trigger[i] = INTR_TRIGGER_LEVEL; 876 sc->sc_irq_polarity[i] = INTR_POLARITY_LOW; 877 #endif 878 i++; 879 } 880 sc->sc_gpio_npins = i; 881 bcm_gpio_sysctl_init(sc); 882 sc->sc_busdev = gpiobus_attach_bus(dev); 883 if (sc->sc_busdev == NULL) 884 goto fail; 885 886 return (0); 887 888 fail: 889 bcm_gpio_intr_detach(dev); 890 bus_release_resources(dev, bcm_gpio_res_spec, sc->sc_res); 891 mtx_destroy(&sc->sc_mtx); 892 893 return (ENXIO); 894 } 895 896 static int 897 bcm_gpio_detach(device_t dev) 898 { 899 900 return (EBUSY); 901 } 902 903 #ifdef INTRNG 904 static inline void 905 bcm_gpio_modify(struct bcm_gpio_softc *sc, uint32_t reg, uint32_t mask, 906 bool set_bits) 907 { 908 909 if (set_bits) 910 BCM_GPIO_SET_BITS(sc, reg, mask); 911 else 912 BCM_GPIO_CLEAR_BITS(sc, reg, mask); 913 } 914 915 static inline void 916 bcm_gpio_isrc_eoi(struct bcm_gpio_softc *sc, struct bcm_gpio_irqsrc *bgi) 917 { 918 uint32_t bank; 919 920 /* Write 1 to clear. */ 921 bank = BCM_GPIO_BANK(bgi->bgi_irq); 922 BCM_GPIO_WRITE(sc, BCM_GPIO_GPEDS(bank), bgi->bgi_mask); 923 } 924 925 static inline bool 926 bcm_gpio_isrc_is_level(struct bcm_gpio_irqsrc *bgi) 927 { 928 929 return (bgi->bgi_mode == GPIO_INTR_LEVEL_LOW || 930 bgi->bgi_mode == GPIO_INTR_LEVEL_HIGH); 931 } 932 933 static inline void 934 bcm_gpio_isrc_mask(struct bcm_gpio_softc *sc, struct bcm_gpio_irqsrc *bgi) 935 { 936 uint32_t bank; 937 938 bank = BCM_GPIO_BANK(bgi->bgi_irq); 939 BCM_GPIO_LOCK(sc); 940 switch (bgi->bgi_mode) { 941 case GPIO_INTR_LEVEL_LOW: 942 BCM_GPIO_CLEAR_BITS(sc, BCM_GPIO_GPLEN(bank), bgi->bgi_mask); 943 break; 944 case GPIO_INTR_LEVEL_HIGH: 945 BCM_GPIO_CLEAR_BITS(sc, BCM_GPIO_GPHEN(bank), bgi->bgi_mask); 946 break; 947 case GPIO_INTR_EDGE_RISING: 948 BCM_GPIO_CLEAR_BITS(sc, BCM_GPIO_GPREN(bank), bgi->bgi_mask); 949 break; 950 case GPIO_INTR_EDGE_FALLING: 951 BCM_GPIO_CLEAR_BITS(sc, BCM_GPIO_GPFEN(bank), bgi->bgi_mask); 952 break; 953 case GPIO_INTR_EDGE_BOTH: 954 BCM_GPIO_CLEAR_BITS(sc, BCM_GPIO_GPREN(bank), bgi->bgi_mask); 955 BCM_GPIO_CLEAR_BITS(sc, BCM_GPIO_GPFEN(bank), bgi->bgi_mask); 956 break; 957 } 958 BCM_GPIO_UNLOCK(sc); 959 } 960 961 static inline void 962 bcm_gpio_isrc_unmask(struct bcm_gpio_softc *sc, struct bcm_gpio_irqsrc *bgi) 963 { 964 uint32_t bank; 965 966 bank = BCM_GPIO_BANK(bgi->bgi_irq); 967 BCM_GPIO_LOCK(sc); 968 switch (bgi->bgi_mode) { 969 case GPIO_INTR_LEVEL_LOW: 970 BCM_GPIO_SET_BITS(sc, BCM_GPIO_GPLEN(bank), bgi->bgi_mask); 971 break; 972 case GPIO_INTR_LEVEL_HIGH: 973 BCM_GPIO_SET_BITS(sc, BCM_GPIO_GPHEN(bank), bgi->bgi_mask); 974 break; 975 case GPIO_INTR_EDGE_RISING: 976 BCM_GPIO_SET_BITS(sc, BCM_GPIO_GPREN(bank), bgi->bgi_mask); 977 break; 978 case GPIO_INTR_EDGE_FALLING: 979 BCM_GPIO_SET_BITS(sc, BCM_GPIO_GPFEN(bank), bgi->bgi_mask); 980 break; 981 case GPIO_INTR_EDGE_BOTH: 982 BCM_GPIO_SET_BITS(sc, BCM_GPIO_GPREN(bank), bgi->bgi_mask); 983 BCM_GPIO_SET_BITS(sc, BCM_GPIO_GPFEN(bank), bgi->bgi_mask); 984 break; 985 } 986 BCM_GPIO_UNLOCK(sc); 987 } 988 989 static int 990 bcm_gpio_intr_internal(struct bcm_gpio_softc *sc, uint32_t bank) 991 { 992 u_int irq; 993 struct bcm_gpio_irqsrc *bgi; 994 uint32_t reg; 995 996 /* Do not care of spurious interrupt on GPIO. */ 997 reg = BCM_GPIO_READ(sc, BCM_GPIO_GPEDS(bank)); 998 while (reg != 0) { 999 irq = BCM_GPIO_PINS_PER_BANK * bank + ffs(reg) - 1; 1000 bgi = sc->sc_isrcs + irq; 1001 if (!bcm_gpio_isrc_is_level(bgi)) 1002 bcm_gpio_isrc_eoi(sc, bgi); 1003 if (intr_isrc_dispatch(&bgi->bgi_isrc, 1004 curthread->td_intr_frame) != 0) { 1005 bcm_gpio_isrc_mask(sc, bgi); 1006 if (bcm_gpio_isrc_is_level(bgi)) 1007 bcm_gpio_isrc_eoi(sc, bgi); 1008 device_printf(sc->sc_dev, "Stray irq %u disabled\n", 1009 irq); 1010 } 1011 reg &= ~bgi->bgi_mask; 1012 } 1013 return (FILTER_HANDLED); 1014 } 1015 1016 static int 1017 bcm_gpio_intr_bank0(void *arg) 1018 { 1019 1020 return (bcm_gpio_intr_internal(arg, 0)); 1021 } 1022 1023 static int 1024 bcm_gpio_intr_bank1(void *arg) 1025 { 1026 1027 return (bcm_gpio_intr_internal(arg, 1)); 1028 } 1029 1030 static int 1031 bcm_gpio_pic_attach(struct bcm_gpio_softc *sc) 1032 { 1033 int error; 1034 uint32_t irq; 1035 const char *name; 1036 1037 name = device_get_nameunit(sc->sc_dev); 1038 for (irq = 0; irq < BCM_GPIO_PINS; irq++) { 1039 sc->sc_isrcs[irq].bgi_irq = irq; 1040 sc->sc_isrcs[irq].bgi_mask = BCM_GPIO_MASK(irq); 1041 sc->sc_isrcs[irq].bgi_mode = GPIO_INTR_CONFORM; 1042 1043 error = intr_isrc_register(&sc->sc_isrcs[irq].bgi_isrc, 1044 sc->sc_dev, 0, "%s,%u", name, irq); 1045 if (error != 0) 1046 return (error); /* XXX deregister ISRCs */ 1047 } 1048 if (intr_pic_register(sc->sc_dev, 1049 OF_xref_from_node(ofw_bus_get_node(sc->sc_dev))) == NULL) 1050 return (ENXIO); 1051 1052 return (0); 1053 } 1054 1055 static int 1056 bcm_gpio_pic_detach(struct bcm_gpio_softc *sc) 1057 { 1058 1059 /* 1060 * There has not been established any procedure yet 1061 * how to detach PIC from living system correctly. 1062 */ 1063 device_printf(sc->sc_dev, "%s: not implemented yet\n", __func__); 1064 return (EBUSY); 1065 } 1066 1067 static void 1068 bcm_gpio_pic_config_intr(struct bcm_gpio_softc *sc, struct bcm_gpio_irqsrc *bgi, 1069 uint32_t mode) 1070 { 1071 uint32_t bank; 1072 1073 bank = BCM_GPIO_BANK(bgi->bgi_irq); 1074 BCM_GPIO_LOCK(sc); 1075 bcm_gpio_modify(sc, BCM_GPIO_GPREN(bank), bgi->bgi_mask, 1076 mode == GPIO_INTR_EDGE_RISING || mode == GPIO_INTR_EDGE_BOTH); 1077 bcm_gpio_modify(sc, BCM_GPIO_GPFEN(bank), bgi->bgi_mask, 1078 mode == GPIO_INTR_EDGE_FALLING || mode == GPIO_INTR_EDGE_BOTH); 1079 bcm_gpio_modify(sc, BCM_GPIO_GPHEN(bank), bgi->bgi_mask, 1080 mode == GPIO_INTR_LEVEL_HIGH); 1081 bcm_gpio_modify(sc, BCM_GPIO_GPLEN(bank), bgi->bgi_mask, 1082 mode == GPIO_INTR_LEVEL_LOW); 1083 bgi->bgi_mode = mode; 1084 BCM_GPIO_UNLOCK(sc); 1085 } 1086 1087 static void 1088 bcm_gpio_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc) 1089 { 1090 struct bcm_gpio_softc *sc = device_get_softc(dev); 1091 struct bcm_gpio_irqsrc *bgi = (struct bcm_gpio_irqsrc *)isrc; 1092 1093 bcm_gpio_isrc_mask(sc, bgi); 1094 } 1095 1096 static void 1097 bcm_gpio_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc) 1098 { 1099 struct bcm_gpio_softc *sc = device_get_softc(dev); 1100 struct bcm_gpio_irqsrc *bgi = (struct bcm_gpio_irqsrc *)isrc; 1101 1102 arm_irq_memory_barrier(bgi->bgi_irq); 1103 bcm_gpio_isrc_unmask(sc, bgi); 1104 } 1105 1106 static int 1107 bcm_gpio_pic_map_fdt(struct bcm_gpio_softc *sc, struct intr_map_data_fdt *daf, 1108 u_int *irqp, uint32_t *modep) 1109 { 1110 u_int irq; 1111 uint32_t mode, bank; 1112 1113 /* 1114 * The first cell is the interrupt number. 1115 * The second cell is used to specify flags: 1116 * bits[3:0] trigger type and level flags: 1117 * 1 = low-to-high edge triggered. 1118 * 2 = high-to-low edge triggered. 1119 * 4 = active high level-sensitive. 1120 * 8 = active low level-sensitive. 1121 */ 1122 if (daf->ncells != 2) 1123 return (EINVAL); 1124 1125 irq = daf->cells[0]; 1126 if (irq >= BCM_GPIO_PINS || bcm_gpio_pin_is_ro(sc, irq)) 1127 return (EINVAL); 1128 1129 /* Only reasonable modes are supported. */ 1130 bank = BCM_GPIO_BANK(irq); 1131 if (daf->cells[1] == 1) 1132 mode = GPIO_INTR_EDGE_RISING; 1133 else if (daf->cells[1] == 2) 1134 mode = GPIO_INTR_EDGE_FALLING; 1135 else if (daf->cells[1] == 3) 1136 mode = GPIO_INTR_EDGE_BOTH; 1137 else if (daf->cells[1] == 4) 1138 mode = GPIO_INTR_LEVEL_HIGH; 1139 else if (daf->cells[1] == 8) 1140 mode = GPIO_INTR_LEVEL_LOW; 1141 else 1142 return (EINVAL); 1143 1144 *irqp = irq; 1145 if (modep != NULL) 1146 *modep = mode; 1147 return (0); 1148 } 1149 1150 static int 1151 bcm_gpio_pic_map_gpio(struct bcm_gpio_softc *sc, struct intr_map_data_gpio *dag, 1152 u_int *irqp, uint32_t *modep) 1153 { 1154 u_int irq; 1155 uint32_t mode; 1156 1157 irq = dag->gpio_pin_num; 1158 if (irq >= BCM_GPIO_PINS || bcm_gpio_pin_is_ro(sc, irq)) 1159 return (EINVAL); 1160 1161 mode = dag->gpio_intr_mode; 1162 if (mode != GPIO_INTR_LEVEL_LOW && mode != GPIO_INTR_LEVEL_HIGH && 1163 mode != GPIO_INTR_EDGE_RISING && mode != GPIO_INTR_EDGE_FALLING && 1164 mode != GPIO_INTR_EDGE_BOTH) 1165 return (EINVAL); 1166 1167 *irqp = irq; 1168 if (modep != NULL) 1169 *modep = mode; 1170 return (0); 1171 } 1172 1173 static int 1174 bcm_gpio_pic_map(struct bcm_gpio_softc *sc, struct intr_map_data *data, 1175 u_int *irqp, uint32_t *modep) 1176 { 1177 1178 switch (data->type) { 1179 case INTR_MAP_DATA_FDT: 1180 return (bcm_gpio_pic_map_fdt(sc, 1181 (struct intr_map_data_fdt *)data, irqp, modep)); 1182 case INTR_MAP_DATA_GPIO: 1183 return (bcm_gpio_pic_map_gpio(sc, 1184 (struct intr_map_data_gpio *)data, irqp, modep)); 1185 default: 1186 return (ENOTSUP); 1187 } 1188 } 1189 1190 static int 1191 bcm_gpio_pic_map_intr(device_t dev, struct intr_map_data *data, 1192 struct intr_irqsrc **isrcp) 1193 { 1194 int error; 1195 u_int irq; 1196 struct bcm_gpio_softc *sc = device_get_softc(dev); 1197 1198 error = bcm_gpio_pic_map(sc, data, &irq, NULL); 1199 if (error == 0) 1200 *isrcp = &sc->sc_isrcs[irq].bgi_isrc; 1201 return (error); 1202 } 1203 1204 static void 1205 bcm_gpio_pic_post_filter(device_t dev, struct intr_irqsrc *isrc) 1206 { 1207 struct bcm_gpio_softc *sc = device_get_softc(dev); 1208 struct bcm_gpio_irqsrc *bgi = (struct bcm_gpio_irqsrc *)isrc; 1209 1210 if (bcm_gpio_isrc_is_level(bgi)) 1211 bcm_gpio_isrc_eoi(sc, bgi); 1212 } 1213 1214 static void 1215 bcm_gpio_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc) 1216 { 1217 1218 bcm_gpio_pic_enable_intr(dev, isrc); 1219 } 1220 1221 static void 1222 bcm_gpio_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc) 1223 { 1224 struct bcm_gpio_softc *sc = device_get_softc(dev); 1225 struct bcm_gpio_irqsrc *bgi = (struct bcm_gpio_irqsrc *)isrc; 1226 1227 bcm_gpio_isrc_mask(sc, bgi); 1228 if (bcm_gpio_isrc_is_level(bgi)) 1229 bcm_gpio_isrc_eoi(sc, bgi); 1230 } 1231 1232 static int 1233 bcm_gpio_pic_setup_intr(device_t dev, struct intr_irqsrc *isrc, 1234 struct resource *res, struct intr_map_data *data) 1235 { 1236 u_int irq; 1237 uint32_t mode; 1238 struct bcm_gpio_softc *sc; 1239 struct bcm_gpio_irqsrc *bgi; 1240 1241 if (data == NULL) 1242 return (ENOTSUP); 1243 1244 sc = device_get_softc(dev); 1245 bgi = (struct bcm_gpio_irqsrc *)isrc; 1246 1247 /* Get and check config for an interrupt. */ 1248 if (bcm_gpio_pic_map(sc, data, &irq, &mode) != 0 || bgi->bgi_irq != irq) 1249 return (EINVAL); 1250 1251 /* 1252 * If this is a setup for another handler, 1253 * only check that its configuration match. 1254 */ 1255 if (isrc->isrc_handlers != 0) 1256 return (bgi->bgi_mode == mode ? 0 : EINVAL); 1257 1258 bcm_gpio_pic_config_intr(sc, bgi, mode); 1259 return (0); 1260 } 1261 1262 static int 1263 bcm_gpio_pic_teardown_intr(device_t dev, struct intr_irqsrc *isrc, 1264 struct resource *res, struct intr_map_data *data) 1265 { 1266 struct bcm_gpio_softc *sc = device_get_softc(dev); 1267 struct bcm_gpio_irqsrc *bgi = (struct bcm_gpio_irqsrc *)isrc; 1268 1269 if (isrc->isrc_handlers == 0) 1270 bcm_gpio_pic_config_intr(sc, bgi, GPIO_INTR_CONFORM); 1271 return (0); 1272 } 1273 1274 #else 1275 static uint32_t 1276 bcm_gpio_intr_reg(struct bcm_gpio_softc *sc, unsigned int irq, uint32_t bank) 1277 { 1278 1279 if (irq > BCM_GPIO_PINS) 1280 return (0); 1281 if (sc->sc_irq_trigger[irq] == INTR_TRIGGER_LEVEL) { 1282 if (sc->sc_irq_polarity[irq] == INTR_POLARITY_LOW) 1283 return (BCM_GPIO_GPLEN(bank)); 1284 else if (sc->sc_irq_polarity[irq] == INTR_POLARITY_HIGH) 1285 return (BCM_GPIO_GPHEN(bank)); 1286 } else if (sc->sc_irq_trigger[irq] == INTR_TRIGGER_EDGE) { 1287 if (sc->sc_irq_polarity[irq] == INTR_POLARITY_LOW) 1288 return (BCM_GPIO_GPFEN(bank)); 1289 else if (sc->sc_irq_polarity[irq] == INTR_POLARITY_HIGH) 1290 return (BCM_GPIO_GPREN(bank)); 1291 } 1292 1293 return (0); 1294 } 1295 1296 static void 1297 bcm_gpio_mask_irq(void *source) 1298 { 1299 uint32_t bank, mask, reg; 1300 unsigned int irq; 1301 1302 irq = (unsigned int)source; 1303 if (irq > BCM_GPIO_PINS) 1304 return; 1305 if (bcm_gpio_pin_is_ro(bcm_gpio_sc, irq)) 1306 return; 1307 bank = BCM_GPIO_BANK(irq); 1308 mask = BCM_GPIO_MASK(irq); 1309 BCM_GPIO_LOCK(bcm_gpio_sc); 1310 reg = bcm_gpio_intr_reg(bcm_gpio_sc, irq, bank); 1311 if (reg != 0) 1312 BCM_GPIO_CLEAR_BITS(bcm_gpio_sc, reg, mask); 1313 BCM_GPIO_UNLOCK(bcm_gpio_sc); 1314 } 1315 1316 static void 1317 bcm_gpio_unmask_irq(void *source) 1318 { 1319 uint32_t bank, mask, reg; 1320 unsigned int irq; 1321 1322 irq = (unsigned int)source; 1323 if (irq > BCM_GPIO_PINS) 1324 return; 1325 if (bcm_gpio_pin_is_ro(bcm_gpio_sc, irq)) 1326 return; 1327 bank = BCM_GPIO_BANK(irq); 1328 mask = BCM_GPIO_MASK(irq); 1329 BCM_GPIO_LOCK(bcm_gpio_sc); 1330 reg = bcm_gpio_intr_reg(bcm_gpio_sc, irq, bank); 1331 if (reg != 0) 1332 BCM_GPIO_SET_BITS(bcm_gpio_sc, reg, mask); 1333 BCM_GPIO_UNLOCK(bcm_gpio_sc); 1334 } 1335 1336 static int 1337 bcm_gpio_activate_resource(device_t bus, device_t child, int type, int rid, 1338 struct resource *res) 1339 { 1340 int pin; 1341 1342 if (type != SYS_RES_IRQ) 1343 return (ENXIO); 1344 /* Unmask the interrupt. */ 1345 pin = rman_get_start(res); 1346 bcm_gpio_unmask_irq((void *)pin); 1347 1348 return (0); 1349 } 1350 1351 static int 1352 bcm_gpio_deactivate_resource(device_t bus, device_t child, int type, int rid, 1353 struct resource *res) 1354 { 1355 int pin; 1356 1357 if (type != SYS_RES_IRQ) 1358 return (ENXIO); 1359 /* Mask the interrupt. */ 1360 pin = rman_get_start(res); 1361 bcm_gpio_mask_irq((void *)pin); 1362 1363 return (0); 1364 } 1365 1366 static int 1367 bcm_gpio_config_intr(device_t dev, int irq, enum intr_trigger trig, 1368 enum intr_polarity pol) 1369 { 1370 int bank; 1371 struct bcm_gpio_softc *sc; 1372 uint32_t mask, oldreg, reg; 1373 1374 if (irq > BCM_GPIO_PINS) 1375 return (EINVAL); 1376 /* There is no standard trigger or polarity. */ 1377 if (trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM) 1378 return (EINVAL); 1379 sc = device_get_softc(dev); 1380 if (bcm_gpio_pin_is_ro(sc, irq)) 1381 return (EINVAL); 1382 bank = BCM_GPIO_BANK(irq); 1383 mask = BCM_GPIO_MASK(irq); 1384 BCM_GPIO_LOCK(sc); 1385 oldreg = bcm_gpio_intr_reg(sc, irq, bank); 1386 sc->sc_irq_trigger[irq] = trig; 1387 sc->sc_irq_polarity[irq] = pol; 1388 reg = bcm_gpio_intr_reg(sc, irq, bank); 1389 if (reg != 0) 1390 BCM_GPIO_SET_BITS(sc, reg, mask); 1391 if (reg != oldreg && oldreg != 0) 1392 BCM_GPIO_CLEAR_BITS(sc, oldreg, mask); 1393 BCM_GPIO_UNLOCK(sc); 1394 1395 return (0); 1396 } 1397 1398 static int 1399 bcm_gpio_setup_intr(device_t bus, device_t child, struct resource *ires, 1400 int flags, driver_filter_t *filt, driver_intr_t *handler, 1401 void *arg, void **cookiep) 1402 { 1403 struct bcm_gpio_softc *sc; 1404 struct intr_event *event; 1405 int pin, error; 1406 1407 sc = device_get_softc(bus); 1408 pin = rman_get_start(ires); 1409 if (pin > BCM_GPIO_PINS) 1410 panic("%s: bad pin %d", __func__, pin); 1411 event = sc->sc_events[pin]; 1412 if (event == NULL) { 1413 error = intr_event_create(&event, (void *)pin, 0, pin, 1414 bcm_gpio_mask_irq, bcm_gpio_unmask_irq, NULL, NULL, 1415 "gpio%d pin%d:", device_get_unit(bus), pin); 1416 if (error != 0) 1417 return (error); 1418 sc->sc_events[pin] = event; 1419 } 1420 intr_event_add_handler(event, device_get_nameunit(child), filt, 1421 handler, arg, intr_priority(flags), flags, cookiep); 1422 1423 return (0); 1424 } 1425 1426 static int 1427 bcm_gpio_teardown_intr(device_t dev, device_t child, struct resource *ires, 1428 void *cookie) 1429 { 1430 struct bcm_gpio_softc *sc; 1431 int pin, err; 1432 1433 sc = device_get_softc(dev); 1434 pin = rman_get_start(ires); 1435 if (pin > BCM_GPIO_PINS) 1436 panic("%s: bad pin %d", __func__, pin); 1437 if (sc->sc_events[pin] == NULL) 1438 panic("Trying to teardown unoccupied IRQ"); 1439 err = intr_event_remove_handler(cookie); 1440 if (!err) 1441 sc->sc_events[pin] = NULL; 1442 1443 return (err); 1444 } 1445 #endif 1446 1447 static phandle_t 1448 bcm_gpio_get_node(device_t bus, device_t dev) 1449 { 1450 1451 /* We only have one child, the GPIO bus, which needs our own node. */ 1452 return (ofw_bus_get_node(bus)); 1453 } 1454 1455 static device_method_t bcm_gpio_methods[] = { 1456 /* Device interface */ 1457 DEVMETHOD(device_probe, bcm_gpio_probe), 1458 DEVMETHOD(device_attach, bcm_gpio_attach), 1459 DEVMETHOD(device_detach, bcm_gpio_detach), 1460 1461 /* GPIO protocol */ 1462 DEVMETHOD(gpio_get_bus, bcm_gpio_get_bus), 1463 DEVMETHOD(gpio_pin_max, bcm_gpio_pin_max), 1464 DEVMETHOD(gpio_pin_getname, bcm_gpio_pin_getname), 1465 DEVMETHOD(gpio_pin_getflags, bcm_gpio_pin_getflags), 1466 DEVMETHOD(gpio_pin_getcaps, bcm_gpio_pin_getcaps), 1467 DEVMETHOD(gpio_pin_setflags, bcm_gpio_pin_setflags), 1468 DEVMETHOD(gpio_pin_get, bcm_gpio_pin_get), 1469 DEVMETHOD(gpio_pin_set, bcm_gpio_pin_set), 1470 DEVMETHOD(gpio_pin_toggle, bcm_gpio_pin_toggle), 1471 1472 #ifdef INTRNG 1473 /* Interrupt controller interface */ 1474 DEVMETHOD(pic_disable_intr, bcm_gpio_pic_disable_intr), 1475 DEVMETHOD(pic_enable_intr, bcm_gpio_pic_enable_intr), 1476 DEVMETHOD(pic_map_intr, bcm_gpio_pic_map_intr), 1477 DEVMETHOD(pic_post_filter, bcm_gpio_pic_post_filter), 1478 DEVMETHOD(pic_post_ithread, bcm_gpio_pic_post_ithread), 1479 DEVMETHOD(pic_pre_ithread, bcm_gpio_pic_pre_ithread), 1480 DEVMETHOD(pic_setup_intr, bcm_gpio_pic_setup_intr), 1481 DEVMETHOD(pic_teardown_intr, bcm_gpio_pic_teardown_intr), 1482 #else 1483 /* Bus interface */ 1484 DEVMETHOD(bus_activate_resource, bcm_gpio_activate_resource), 1485 DEVMETHOD(bus_deactivate_resource, bcm_gpio_deactivate_resource), 1486 DEVMETHOD(bus_config_intr, bcm_gpio_config_intr), 1487 DEVMETHOD(bus_setup_intr, bcm_gpio_setup_intr), 1488 DEVMETHOD(bus_teardown_intr, bcm_gpio_teardown_intr), 1489 #endif 1490 /* ofw_bus interface */ 1491 DEVMETHOD(ofw_bus_get_node, bcm_gpio_get_node), 1492 1493 DEVMETHOD_END 1494 }; 1495 1496 static devclass_t bcm_gpio_devclass; 1497 1498 static driver_t bcm_gpio_driver = { 1499 "gpio", 1500 bcm_gpio_methods, 1501 sizeof(struct bcm_gpio_softc), 1502 }; 1503 1504 DRIVER_MODULE(bcm_gpio, simplebus, bcm_gpio_driver, bcm_gpio_devclass, 0, 0); 1505