1 /*- 2 * Copyright (c) 2011 Ben Gray <ben.r.gray@gmail.com>. 3 * Copyright (c) 2014 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 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 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 /** 29 * Beware that the OMAP4 datasheet(s) lists GPIO banks 1-6, whereas the code 30 * here uses 0-5. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_platform.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bus.h> 41 42 #include <sys/kernel.h> 43 #include <sys/module.h> 44 #include <sys/proc.h> 45 #include <sys/rman.h> 46 #include <sys/lock.h> 47 #include <sys/mutex.h> 48 #include <sys/gpio.h> 49 #include <sys/interrupt.h> 50 51 #include <machine/bus.h> 52 #include <machine/intr.h> 53 #include <machine/resource.h> 54 55 #include <arm/ti/ti_cpuid.h> 56 #include <arm/ti/ti_gpio.h> 57 #include <arm/ti/ti_scm.h> 58 #include <arm/ti/ti_prcm.h> 59 #include <arm/ti/ti_hwmods.h> 60 61 #include <dev/fdt/fdt_common.h> 62 #include <dev/gpio/gpiobusvar.h> 63 #include <dev/ofw/openfirm.h> 64 #include <dev/ofw/ofw_bus.h> 65 #include <dev/ofw/ofw_bus_subr.h> 66 67 #include "gpio_if.h" 68 #include "ti_gpio_if.h" 69 #ifdef INTRNG 70 #include "pic_if.h" 71 #endif 72 73 #if !defined(SOC_OMAP4) && !defined(SOC_TI_AM335X) 74 #error "Unknown SoC" 75 #endif 76 77 /* Register definitions */ 78 #define TI_GPIO_REVISION 0x0000 79 #define TI_GPIO_SYSCONFIG 0x0010 80 #define TI_GPIO_IRQSTATUS_RAW_0 0x0024 81 #define TI_GPIO_IRQSTATUS_RAW_1 0x0028 82 #define TI_GPIO_IRQSTATUS_0 0x002C /* writing a 0 has no effect */ 83 #define TI_GPIO_IRQSTATUS_1 0x0030 /* writing a 0 has no effect */ 84 #define TI_GPIO_IRQSTATUS_SET_0 0x0034 /* writing a 0 has no effect */ 85 #define TI_GPIO_IRQSTATUS_SET_1 0x0038 /* writing a 0 has no effect */ 86 #define TI_GPIO_IRQSTATUS_CLR_0 0x003C /* writing a 0 has no effect */ 87 #define TI_GPIO_IRQSTATUS_CLR_1 0x0040 /* writing a 0 has no effect */ 88 #define TI_GPIO_IRQWAKEN_0 0x0044 89 #define TI_GPIO_IRQWAKEN_1 0x0048 90 #define TI_GPIO_SYSSTATUS 0x0114 91 #define TI_GPIO_IRQSTATUS1 0x0118 92 #define TI_GPIO_IRQENABLE1 0x011C 93 #define TI_GPIO_WAKEUPENABLE 0x0120 94 #define TI_GPIO_IRQSTATUS2 0x0128 95 #define TI_GPIO_IRQENABLE2 0x012C 96 #define TI_GPIO_CTRL 0x0130 97 #define TI_GPIO_OE 0x0134 98 #define TI_GPIO_DATAIN 0x0138 99 #define TI_GPIO_DATAOUT 0x013C 100 #define TI_GPIO_LEVELDETECT0 0x0140 /* RW register */ 101 #define TI_GPIO_LEVELDETECT1 0x0144 /* RW register */ 102 #define TI_GPIO_RISINGDETECT 0x0148 /* RW register */ 103 #define TI_GPIO_FALLINGDETECT 0x014C /* RW register */ 104 #define TI_GPIO_DEBOUNCENABLE 0x0150 105 #define TI_GPIO_DEBOUNCINGTIME 0x0154 106 #define TI_GPIO_CLEARWKUPENA 0x0180 107 #define TI_GPIO_SETWKUENA 0x0184 108 #define TI_GPIO_CLEARDATAOUT 0x0190 109 #define TI_GPIO_SETDATAOUT 0x0194 110 111 /* Other SoC Specific definitions */ 112 #define OMAP4_FIRST_GPIO_BANK 1 113 #define OMAP4_INTR_PER_BANK 1 114 #define OMAP4_GPIO_REV 0x50600801 115 #define AM335X_FIRST_GPIO_BANK 0 116 #define AM335X_INTR_PER_BANK 2 117 #define AM335X_GPIO_REV 0x50600801 118 #define PINS_PER_BANK 32 119 #define TI_GPIO_MASK(p) (1U << ((p) % PINS_PER_BANK)) 120 121 static int ti_gpio_intr(void *arg); 122 static int ti_gpio_detach(device_t); 123 124 #ifdef INTRNG 125 static int ti_gpio_pic_attach(struct ti_gpio_softc *sc); 126 static int ti_gpio_pic_detach(struct ti_gpio_softc *sc); 127 #endif 128 129 static u_int 130 ti_first_gpio_bank(void) 131 { 132 switch(ti_chip()) { 133 #ifdef SOC_OMAP4 134 case CHIP_OMAP_4: 135 return (OMAP4_FIRST_GPIO_BANK); 136 #endif 137 #ifdef SOC_TI_AM335X 138 case CHIP_AM335X: 139 return (AM335X_FIRST_GPIO_BANK); 140 #endif 141 } 142 return (0); 143 } 144 145 static uint32_t 146 ti_gpio_rev(void) 147 { 148 switch(ti_chip()) { 149 #ifdef SOC_OMAP4 150 case CHIP_OMAP_4: 151 return (OMAP4_GPIO_REV); 152 #endif 153 #ifdef SOC_TI_AM335X 154 case CHIP_AM335X: 155 return (AM335X_GPIO_REV); 156 #endif 157 } 158 return (0); 159 } 160 161 /** 162 * Macros for driver mutex locking 163 */ 164 #define TI_GPIO_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx) 165 #define TI_GPIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx) 166 #define TI_GPIO_LOCK_INIT(_sc) \ 167 mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \ 168 "ti_gpio", MTX_SPIN) 169 #define TI_GPIO_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx) 170 #define TI_GPIO_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED) 171 #define TI_GPIO_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED) 172 173 /** 174 * ti_gpio_read_4 - reads a 32-bit value from one of the GPIO registers 175 * @sc: GPIO device context 176 * @bank: The bank to read from 177 * @off: The offset of a register from the GPIO register address range 178 * 179 * 180 * RETURNS: 181 * 32-bit value read from the register. 182 */ 183 static inline uint32_t 184 ti_gpio_read_4(struct ti_gpio_softc *sc, bus_size_t off) 185 { 186 return (bus_read_4(sc->sc_mem_res, off)); 187 } 188 189 /** 190 * ti_gpio_write_4 - writes a 32-bit value to one of the GPIO registers 191 * @sc: GPIO device context 192 * @bank: The bank to write to 193 * @off: The offset of a register from the GPIO register address range 194 * @val: The value to write into the register 195 * 196 * RETURNS: 197 * nothing 198 */ 199 static inline void 200 ti_gpio_write_4(struct ti_gpio_softc *sc, bus_size_t off, 201 uint32_t val) 202 { 203 bus_write_4(sc->sc_mem_res, off, val); 204 } 205 206 static inline void 207 ti_gpio_intr_clr(struct ti_gpio_softc *sc, uint32_t mask) 208 { 209 210 /* We clear both set of registers. */ 211 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_CLR_0, mask); 212 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_CLR_1, mask); 213 } 214 215 static inline void 216 ti_gpio_intr_set(struct ti_gpio_softc *sc, uint32_t mask) 217 { 218 219 /* 220 * On OMAP4 we unmask only the MPU interrupt and on AM335x we 221 * also activate only the first interrupt. 222 */ 223 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_SET_0, mask); 224 } 225 226 static inline void 227 ti_gpio_intr_ack(struct ti_gpio_softc *sc, uint32_t mask) 228 { 229 230 /* 231 * Acknowledge the interrupt on both registers even if we use only 232 * the first one. 233 */ 234 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_0, mask); 235 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_1, mask); 236 } 237 238 static inline uint32_t 239 ti_gpio_intr_status(struct ti_gpio_softc *sc) 240 { 241 uint32_t reg; 242 243 /* Get the status from both registers. */ 244 reg = ti_gpio_read_4(sc, TI_GPIO_IRQSTATUS_0); 245 reg |= ti_gpio_read_4(sc, TI_GPIO_IRQSTATUS_1); 246 247 return (reg); 248 } 249 250 static device_t 251 ti_gpio_get_bus(device_t dev) 252 { 253 struct ti_gpio_softc *sc; 254 255 sc = device_get_softc(dev); 256 257 return (sc->sc_busdev); 258 } 259 260 /** 261 * ti_gpio_pin_max - Returns the maximum number of GPIO pins 262 * @dev: gpio device handle 263 * @maxpin: pointer to a value that upon return will contain the maximum number 264 * of pins in the device. 265 * 266 * 267 * LOCKING: 268 * No locking required, returns static data. 269 * 270 * RETURNS: 271 * Returns 0 on success otherwise an error code 272 */ 273 static int 274 ti_gpio_pin_max(device_t dev, int *maxpin) 275 { 276 277 *maxpin = PINS_PER_BANK - 1; 278 279 return (0); 280 } 281 282 static int 283 ti_gpio_valid_pin(struct ti_gpio_softc *sc, int pin) 284 { 285 286 if (pin >= sc->sc_maxpin || sc->sc_mem_res == NULL) 287 return (EINVAL); 288 289 return (0); 290 } 291 292 /** 293 * ti_gpio_pin_getcaps - Gets the capabilities of a given pin 294 * @dev: gpio device handle 295 * @pin: the number of the pin 296 * @caps: pointer to a value that upon return will contain the capabilities 297 * 298 * Currently all pins have the same capability, notably: 299 * - GPIO_PIN_INPUT 300 * - GPIO_PIN_OUTPUT 301 * - GPIO_PIN_PULLUP 302 * - GPIO_PIN_PULLDOWN 303 * - GPIO_INTR_LEVEL_LOW 304 * - GPIO_INTR_LEVEL_HIGH 305 * - GPIO_INTR_EDGE_RISING 306 * - GPIO_INTR_EDGE_FALLING 307 * - GPIO_INTR_EDGE_BOTH 308 * 309 * LOCKING: 310 * No locking required, returns static data. 311 * 312 * RETURNS: 313 * Returns 0 on success otherwise an error code 314 */ 315 static int 316 ti_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps) 317 { 318 struct ti_gpio_softc *sc; 319 320 sc = device_get_softc(dev); 321 if (ti_gpio_valid_pin(sc, pin) != 0) 322 return (EINVAL); 323 324 #ifdef INTRNG 325 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_PULLUP | 326 GPIO_PIN_PULLDOWN | GPIO_INTR_LEVEL_LOW | GPIO_INTR_LEVEL_HIGH | 327 GPIO_INTR_EDGE_RISING | GPIO_INTR_EDGE_FALLING | 328 GPIO_INTR_EDGE_BOTH); 329 #else 330 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_PULLUP | 331 GPIO_PIN_PULLDOWN); 332 #endif 333 334 return (0); 335 } 336 337 /** 338 * ti_gpio_pin_getflags - Gets the current flags of a given pin 339 * @dev: gpio device handle 340 * @pin: the number of the pin 341 * @flags: upon return will contain the current flags of the pin 342 * 343 * Reads the current flags of a given pin, here we actually read the H/W 344 * registers to determine the flags, rather than storing the value in the 345 * setflags call. 346 * 347 * LOCKING: 348 * Internally locks the context 349 * 350 * RETURNS: 351 * Returns 0 on success otherwise an error code 352 */ 353 static int 354 ti_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags) 355 { 356 struct ti_gpio_softc *sc; 357 358 sc = device_get_softc(dev); 359 if (ti_gpio_valid_pin(sc, pin) != 0) 360 return (EINVAL); 361 362 /* Get the current pin state */ 363 TI_GPIO_LOCK(sc); 364 TI_GPIO_GET_FLAGS(dev, pin, flags); 365 TI_GPIO_UNLOCK(sc); 366 367 return (0); 368 } 369 370 /** 371 * ti_gpio_pin_getname - Gets the name of a given pin 372 * @dev: gpio device handle 373 * @pin: the number of the pin 374 * @name: buffer to put the name in 375 * 376 * The driver simply calls the pins gpio_n, where 'n' is obviously the number 377 * of the pin. 378 * 379 * LOCKING: 380 * No locking required, returns static data. 381 * 382 * RETURNS: 383 * Returns 0 on success otherwise an error code 384 */ 385 static int 386 ti_gpio_pin_getname(device_t dev, uint32_t pin, char *name) 387 { 388 struct ti_gpio_softc *sc; 389 390 sc = device_get_softc(dev); 391 if (ti_gpio_valid_pin(sc, pin) != 0) 392 return (EINVAL); 393 394 /* Set a very simple name */ 395 snprintf(name, GPIOMAXNAME, "gpio_%u", pin); 396 name[GPIOMAXNAME - 1] = '\0'; 397 398 return (0); 399 } 400 401 /** 402 * ti_gpio_pin_setflags - Sets the flags for a given pin 403 * @dev: gpio device handle 404 * @pin: the number of the pin 405 * @flags: the flags to set 406 * 407 * The flags of the pin correspond to things like input/output mode, pull-ups, 408 * pull-downs, etc. This driver doesn't support all flags, only the following: 409 * - GPIO_PIN_INPUT 410 * - GPIO_PIN_OUTPUT 411 * - GPIO_PIN_PULLUP 412 * - GPIO_PIN_PULLDOWN 413 * 414 * LOCKING: 415 * Internally locks the context 416 * 417 * RETURNS: 418 * Returns 0 on success otherwise an error code 419 */ 420 static int 421 ti_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) 422 { 423 struct ti_gpio_softc *sc; 424 uint32_t oe; 425 426 sc = device_get_softc(dev); 427 if (ti_gpio_valid_pin(sc, pin) != 0) 428 return (EINVAL); 429 430 /* Set the GPIO mode and state */ 431 TI_GPIO_LOCK(sc); 432 if (TI_GPIO_SET_FLAGS(dev, pin, flags) != 0) { 433 TI_GPIO_UNLOCK(sc); 434 return (EINVAL); 435 } 436 437 /* If configuring as an output set the "output enable" bit */ 438 oe = ti_gpio_read_4(sc, TI_GPIO_OE); 439 if (flags & GPIO_PIN_INPUT) 440 oe |= TI_GPIO_MASK(pin); 441 else 442 oe &= ~TI_GPIO_MASK(pin); 443 ti_gpio_write_4(sc, TI_GPIO_OE, oe); 444 TI_GPIO_UNLOCK(sc); 445 446 return (0); 447 } 448 449 /** 450 * ti_gpio_pin_set - Sets the current level on a GPIO pin 451 * @dev: gpio device handle 452 * @pin: the number of the pin 453 * @value: non-zero value will drive the pin high, otherwise the pin is 454 * driven low. 455 * 456 * 457 * LOCKING: 458 * Internally locks the context 459 * 460 * RETURNS: 461 * Returns 0 on success otherwise a error code 462 */ 463 static int 464 ti_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value) 465 { 466 struct ti_gpio_softc *sc; 467 uint32_t reg; 468 469 sc = device_get_softc(dev); 470 if (ti_gpio_valid_pin(sc, pin) != 0) 471 return (EINVAL); 472 473 TI_GPIO_LOCK(sc); 474 if (value == GPIO_PIN_LOW) 475 reg = TI_GPIO_CLEARDATAOUT; 476 else 477 reg = TI_GPIO_SETDATAOUT; 478 ti_gpio_write_4(sc, reg, TI_GPIO_MASK(pin)); 479 TI_GPIO_UNLOCK(sc); 480 481 return (0); 482 } 483 484 /** 485 * ti_gpio_pin_get - Gets the current level on a GPIO pin 486 * @dev: gpio device handle 487 * @pin: the number of the pin 488 * @value: pointer to a value that upond return will contain the pin value 489 * 490 * The pin must be configured as an input pin beforehand, otherwise this 491 * function will fail. 492 * 493 * LOCKING: 494 * Internally locks the context 495 * 496 * RETURNS: 497 * Returns 0 on success otherwise a error code 498 */ 499 static int 500 ti_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value) 501 { 502 struct ti_gpio_softc *sc; 503 uint32_t oe, reg, val; 504 505 sc = device_get_softc(dev); 506 if (ti_gpio_valid_pin(sc, pin) != 0) 507 return (EINVAL); 508 509 /* 510 * Return data from output latch when set as output and from the 511 * input register otherwise. 512 */ 513 TI_GPIO_LOCK(sc); 514 oe = ti_gpio_read_4(sc, TI_GPIO_OE); 515 if (oe & TI_GPIO_MASK(pin)) 516 reg = TI_GPIO_DATAIN; 517 else 518 reg = TI_GPIO_DATAOUT; 519 val = ti_gpio_read_4(sc, reg); 520 *value = (val & TI_GPIO_MASK(pin)) ? 1 : 0; 521 TI_GPIO_UNLOCK(sc); 522 523 return (0); 524 } 525 526 /** 527 * ti_gpio_pin_toggle - Toggles a given GPIO pin 528 * @dev: gpio device handle 529 * @pin: the number of the pin 530 * 531 * 532 * LOCKING: 533 * Internally locks the context 534 * 535 * RETURNS: 536 * Returns 0 on success otherwise a error code 537 */ 538 static int 539 ti_gpio_pin_toggle(device_t dev, uint32_t pin) 540 { 541 struct ti_gpio_softc *sc; 542 uint32_t reg, val; 543 544 sc = device_get_softc(dev); 545 if (ti_gpio_valid_pin(sc, pin) != 0) 546 return (EINVAL); 547 548 /* Toggle the pin */ 549 TI_GPIO_LOCK(sc); 550 val = ti_gpio_read_4(sc, TI_GPIO_DATAOUT); 551 if (val & TI_GPIO_MASK(pin)) 552 reg = TI_GPIO_CLEARDATAOUT; 553 else 554 reg = TI_GPIO_SETDATAOUT; 555 ti_gpio_write_4(sc, reg, TI_GPIO_MASK(pin)); 556 TI_GPIO_UNLOCK(sc); 557 558 return (0); 559 } 560 561 #ifndef INTRNG 562 /** 563 * ti_gpio_intr - ISR for all GPIO modules 564 * @arg: the soft context pointer 565 * 566 * LOCKING: 567 * Internally locks the context 568 * 569 */ 570 static int 571 ti_gpio_intr(void *arg) 572 { 573 int bank_last, irq; 574 struct intr_event *event; 575 struct ti_gpio_softc *sc; 576 uint32_t reg; 577 578 sc = (struct ti_gpio_softc *)arg; 579 bank_last = -1; 580 reg = 0; /* squelch bogus gcc warning */ 581 reg = ti_gpio_intr_status(sc); 582 for (irq = 0; irq < sc->sc_maxpin; irq++) { 583 if ((reg & TI_GPIO_MASK(irq)) == 0) 584 continue; 585 event = sc->sc_events[irq]; 586 if (event != NULL && !TAILQ_EMPTY(&event->ie_handlers)) 587 intr_event_handle(event, NULL); 588 else 589 device_printf(sc->sc_dev, "Stray IRQ %d\n", irq); 590 /* Ack the IRQ Status bit. */ 591 ti_gpio_intr_ack(sc, TI_GPIO_MASK(irq)); 592 } 593 594 return (FILTER_HANDLED); 595 } 596 #endif 597 598 static int 599 ti_gpio_bank_init(device_t dev) 600 { 601 int pin; 602 struct ti_gpio_softc *sc; 603 uint32_t flags, reg_oe, reg_set, rev; 604 clk_ident_t clk; 605 606 sc = device_get_softc(dev); 607 608 /* Enable the interface and functional clocks for the module. */ 609 clk = ti_hwmods_get_clock(dev); 610 if (clk == INVALID_CLK_IDENT) { 611 device_printf(dev, "failed to get device id based on ti,hwmods\n"); 612 return (EINVAL); 613 } 614 615 sc->sc_bank = clk - GPIO1_CLK + ti_first_gpio_bank(); 616 ti_prcm_clk_enable(clk); 617 618 /* 619 * Read the revision number of the module. TI don't publish the 620 * actual revision numbers, so instead the values have been 621 * determined by experimentation. 622 */ 623 rev = ti_gpio_read_4(sc, TI_GPIO_REVISION); 624 625 /* Check the revision. */ 626 if (rev != ti_gpio_rev()) { 627 device_printf(dev, "Warning: could not determine the revision " 628 "of GPIO module (revision:0x%08x)\n", rev); 629 return (EINVAL); 630 } 631 632 /* Disable interrupts for all pins. */ 633 ti_gpio_intr_clr(sc, 0xffffffff); 634 635 /* Init OE register based on pads configuration. */ 636 reg_oe = 0xffffffff; 637 reg_set = 0; 638 for (pin = 0; pin < PINS_PER_BANK; pin++) { 639 TI_GPIO_GET_FLAGS(dev, pin, &flags); 640 if (flags & GPIO_PIN_OUTPUT) { 641 reg_oe &= ~(1UL << pin); 642 if (flags & GPIO_PIN_PULLUP) 643 reg_set |= (1UL << pin); 644 } 645 } 646 ti_gpio_write_4(sc, TI_GPIO_OE, reg_oe); 647 if (reg_set) 648 ti_gpio_write_4(sc, TI_GPIO_SETDATAOUT, reg_set); 649 650 return (0); 651 } 652 653 /** 654 * ti_gpio_attach - attach function for the driver 655 * @dev: gpio device handle 656 * 657 * Allocates and sets up the driver context for all GPIO banks. This function 658 * expects the memory ranges and IRQs to already be allocated to the driver. 659 * 660 * LOCKING: 661 * None 662 * 663 * RETURNS: 664 * Always returns 0 665 */ 666 static int 667 ti_gpio_attach(device_t dev) 668 { 669 struct ti_gpio_softc *sc; 670 #ifndef INTRNG 671 unsigned int i; 672 #endif 673 int err; 674 675 sc = device_get_softc(dev); 676 sc->sc_dev = dev; 677 TI_GPIO_LOCK_INIT(sc); 678 ti_gpio_pin_max(dev, &sc->sc_maxpin); 679 sc->sc_maxpin++; 680 681 sc->sc_mem_rid = 0; 682 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 683 &sc->sc_mem_rid, RF_ACTIVE); 684 if (!sc->sc_mem_res) { 685 device_printf(dev, "Error: could not allocate mem resources\n"); 686 ti_gpio_detach(dev); 687 return (ENXIO); 688 } 689 690 sc->sc_irq_rid = 0; 691 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 692 &sc->sc_irq_rid, RF_ACTIVE); 693 if (!sc->sc_irq_res) { 694 device_printf(dev, "Error: could not allocate irq resources\n"); 695 ti_gpio_detach(dev); 696 return (ENXIO); 697 } 698 699 /* 700 * Register our interrupt filter for each of the IRQ resources. 701 */ 702 if (bus_setup_intr(dev, sc->sc_irq_res, 703 INTR_TYPE_MISC | INTR_MPSAFE, ti_gpio_intr, NULL, sc, 704 &sc->sc_irq_hdl) != 0) { 705 device_printf(dev, 706 "WARNING: unable to register interrupt filter\n"); 707 ti_gpio_detach(dev); 708 return (ENXIO); 709 } 710 711 #ifdef INTRNG 712 if (ti_gpio_pic_attach(sc) != 0) { 713 device_printf(dev, "WARNING: unable to attach PIC\n"); 714 ti_gpio_detach(dev); 715 return (ENXIO); 716 } 717 #else 718 /* 719 * Initialize the interrupt settings. The default is active-low 720 * interrupts. 721 */ 722 sc->sc_irq_trigger = malloc( 723 sizeof(*sc->sc_irq_trigger) * sc->sc_maxpin, 724 M_DEVBUF, M_WAITOK | M_ZERO); 725 sc->sc_irq_polarity = malloc( 726 sizeof(*sc->sc_irq_polarity) * sc->sc_maxpin, 727 M_DEVBUF, M_WAITOK | M_ZERO); 728 for (i = 0; i < sc->sc_maxpin; i++) { 729 sc->sc_irq_trigger[i] = INTR_TRIGGER_LEVEL; 730 sc->sc_irq_polarity[i] = INTR_POLARITY_LOW; 731 } 732 733 sc->sc_events = malloc(sizeof(struct intr_event *) * sc->sc_maxpin, 734 M_DEVBUF, M_WAITOK | M_ZERO); 735 736 sc->sc_mask_args = malloc(sizeof(struct ti_gpio_mask_arg) * sc->sc_maxpin, 737 M_DEVBUF, M_WAITOK | M_ZERO); 738 #endif 739 /* We need to go through each block and ensure the clocks are running and 740 * the module is enabled. It might be better to do this only when the 741 * pins are configured which would result in less power used if the GPIO 742 * pins weren't used ... 743 */ 744 if (sc->sc_mem_res != NULL) { 745 /* Initialize the GPIO module. */ 746 err = ti_gpio_bank_init(dev); 747 if (err != 0) { 748 ti_gpio_detach(dev); 749 return (err); 750 } 751 } 752 753 sc->sc_busdev = gpiobus_attach_bus(dev); 754 if (sc->sc_busdev == NULL) { 755 ti_gpio_detach(dev); 756 return (ENXIO); 757 } 758 759 return (0); 760 } 761 762 /** 763 * ti_gpio_detach - detach function for the driver 764 * @dev: scm device handle 765 * 766 * Allocates and sets up the driver context, this simply entails creating a 767 * bus mappings for the SCM register set. 768 * 769 * LOCKING: 770 * None 771 * 772 * RETURNS: 773 * Always returns 0 774 */ 775 static int 776 ti_gpio_detach(device_t dev) 777 { 778 struct ti_gpio_softc *sc = device_get_softc(dev); 779 780 KASSERT(mtx_initialized(&sc->sc_mtx), ("gpio mutex not initialized")); 781 782 /* Disable all interrupts */ 783 if (sc->sc_mem_res != NULL) 784 ti_gpio_intr_clr(sc, 0xffffffff); 785 gpiobus_detach_bus(dev); 786 #ifdef INTRNG 787 if (sc->sc_isrcs != NULL) 788 ti_gpio_pic_detach(sc); 789 #else 790 if (sc->sc_events) 791 free(sc->sc_events, M_DEVBUF); 792 if (sc->sc_mask_args) 793 free(sc->sc_mask_args, M_DEVBUF); 794 if (sc->sc_irq_polarity) 795 free(sc->sc_irq_polarity, M_DEVBUF); 796 if (sc->sc_irq_trigger) 797 free(sc->sc_irq_trigger, M_DEVBUF); 798 #endif 799 /* Release the memory and IRQ resources. */ 800 if (sc->sc_irq_hdl) { 801 bus_teardown_intr(dev, sc->sc_irq_res, 802 sc->sc_irq_hdl); 803 } 804 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid, 805 sc->sc_irq_res); 806 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid, 807 sc->sc_mem_res); 808 TI_GPIO_LOCK_DESTROY(sc); 809 810 return (0); 811 } 812 813 #ifdef INTRNG 814 static inline void 815 ti_gpio_rwreg_modify(struct ti_gpio_softc *sc, uint32_t reg, uint32_t mask, 816 bool set_bits) 817 { 818 uint32_t value; 819 820 value = ti_gpio_read_4(sc, reg); 821 ti_gpio_write_4(sc, reg, set_bits ? value | mask : value & ~mask); 822 } 823 824 static inline void 825 ti_gpio_isrc_mask(struct ti_gpio_softc *sc, struct ti_gpio_irqsrc *tgi) 826 { 827 828 /* Writing a 0 has no effect. */ 829 ti_gpio_intr_clr(sc, tgi->tgi_mask); 830 } 831 832 static inline void 833 ti_gpio_isrc_unmask(struct ti_gpio_softc *sc, struct ti_gpio_irqsrc *tgi) 834 { 835 836 /* Writing a 0 has no effect. */ 837 ti_gpio_intr_set(sc, tgi->tgi_mask); 838 } 839 840 static inline void 841 ti_gpio_isrc_eoi(struct ti_gpio_softc *sc, struct ti_gpio_irqsrc *tgi) 842 { 843 844 /* Writing a 0 has no effect. */ 845 ti_gpio_intr_ack(sc, tgi->tgi_mask); 846 } 847 848 static inline bool 849 ti_gpio_isrc_is_level(struct ti_gpio_irqsrc *tgi) 850 { 851 852 return (tgi->tgi_mode == GPIO_INTR_LEVEL_LOW || 853 tgi->tgi_mode == GPIO_INTR_LEVEL_HIGH); 854 } 855 856 static int 857 ti_gpio_intr(void *arg) 858 { 859 u_int irq; 860 uint32_t reg; 861 struct ti_gpio_softc *sc; 862 struct trapframe *tf; 863 struct ti_gpio_irqsrc *tgi; 864 865 sc = (struct ti_gpio_softc *)arg; 866 tf = curthread->td_intr_frame; 867 868 reg = ti_gpio_intr_status(sc); 869 for (irq = 0; irq < sc->sc_maxpin; irq++) { 870 tgi = &sc->sc_isrcs[irq]; 871 if ((reg & tgi->tgi_mask) == 0) 872 continue; 873 if (!ti_gpio_isrc_is_level(tgi)) 874 ti_gpio_isrc_eoi(sc, tgi); 875 if (intr_isrc_dispatch(&tgi->tgi_isrc, tf) != 0) { 876 ti_gpio_isrc_mask(sc, tgi); 877 if (ti_gpio_isrc_is_level(tgi)) 878 ti_gpio_isrc_eoi(sc, tgi); 879 device_printf(sc->sc_dev, "Stray irq %u disabled\n", 880 irq); 881 } 882 } 883 return (FILTER_HANDLED); 884 } 885 886 static int 887 ti_gpio_pic_attach(struct ti_gpio_softc *sc) 888 { 889 int error; 890 uint32_t irq; 891 const char *name; 892 893 sc->sc_isrcs = malloc(sizeof(*sc->sc_isrcs) * sc->sc_maxpin, M_DEVBUF, 894 M_WAITOK | M_ZERO); 895 896 name = device_get_nameunit(sc->sc_dev); 897 for (irq = 0; irq < sc->sc_maxpin; irq++) { 898 sc->sc_isrcs[irq].tgi_irq = irq; 899 sc->sc_isrcs[irq].tgi_mask = TI_GPIO_MASK(irq); 900 sc->sc_isrcs[irq].tgi_mode = GPIO_INTR_CONFORM; 901 902 error = intr_isrc_register(&sc->sc_isrcs[irq].tgi_isrc, 903 sc->sc_dev, 0, "%s,%u", name, irq); 904 if (error != 0) 905 return (error); /* XXX deregister ISRCs */ 906 } 907 if (intr_pic_register(sc->sc_dev, 908 OF_xref_from_node(ofw_bus_get_node(sc->sc_dev))) == NULL) 909 return (ENXIO); 910 911 return (0); 912 } 913 914 static int 915 ti_gpio_pic_detach(struct ti_gpio_softc *sc) 916 { 917 918 /* 919 * There has not been established any procedure yet 920 * how to detach PIC from living system correctly. 921 */ 922 device_printf(sc->sc_dev, "%s: not implemented yet\n", __func__); 923 return (EBUSY); 924 } 925 926 static void 927 ti_gpio_pic_config_intr(struct ti_gpio_softc *sc, struct ti_gpio_irqsrc *tgi, 928 uint32_t mode) 929 { 930 931 TI_GPIO_LOCK(sc); 932 ti_gpio_rwreg_modify(sc, TI_GPIO_RISINGDETECT, tgi->tgi_mask, 933 mode == GPIO_INTR_EDGE_RISING || mode == GPIO_INTR_EDGE_BOTH); 934 ti_gpio_rwreg_modify(sc, TI_GPIO_FALLINGDETECT, tgi->tgi_mask, 935 mode == GPIO_INTR_EDGE_FALLING || mode == GPIO_INTR_EDGE_BOTH); 936 ti_gpio_rwreg_modify(sc, TI_GPIO_LEVELDETECT1, tgi->tgi_mask, 937 mode == GPIO_INTR_LEVEL_HIGH); 938 ti_gpio_rwreg_modify(sc, TI_GPIO_LEVELDETECT0, tgi->tgi_mask, 939 mode == GPIO_INTR_LEVEL_LOW); 940 tgi->tgi_mode = mode; 941 TI_GPIO_UNLOCK(sc); 942 } 943 944 static void 945 ti_gpio_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc) 946 { 947 struct ti_gpio_softc *sc = device_get_softc(dev); 948 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 949 950 ti_gpio_isrc_mask(sc, tgi); 951 } 952 953 static void 954 ti_gpio_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc) 955 { 956 struct ti_gpio_softc *sc = device_get_softc(dev); 957 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 958 959 arm_irq_memory_barrier(tgi->tgi_irq); 960 ti_gpio_isrc_unmask(sc, tgi); 961 } 962 963 static int 964 ti_gpio_pic_map_fdt(struct ti_gpio_softc *sc, struct intr_map_data_fdt *daf, 965 u_int *irqp, uint32_t *modep) 966 { 967 uint32_t mode; 968 969 /* 970 * The first cell is the interrupt number. 971 * The second cell is used to specify flags: 972 * bits[3:0] trigger type and level flags: 973 * 1 = low-to-high edge triggered. 974 * 2 = high-to-low edge triggered. 975 * 4 = active high level-sensitive. 976 * 8 = active low level-sensitive. 977 */ 978 if (daf->ncells != 2 || daf->cells[0] >= sc->sc_maxpin) 979 return (EINVAL); 980 981 /* Only reasonable modes are supported. */ 982 if (daf->cells[1] == 1) 983 mode = GPIO_INTR_EDGE_RISING; 984 else if (daf->cells[1] == 2) 985 mode = GPIO_INTR_EDGE_FALLING; 986 else if (daf->cells[1] == 3) 987 mode = GPIO_INTR_EDGE_BOTH; 988 else if (daf->cells[1] == 4) 989 mode = GPIO_INTR_LEVEL_HIGH; 990 else if (daf->cells[1] == 8) 991 mode = GPIO_INTR_LEVEL_LOW; 992 else 993 return (EINVAL); 994 995 *irqp = daf->cells[0]; 996 if (modep != NULL) 997 *modep = mode; 998 return (0); 999 } 1000 1001 static int 1002 ti_gpio_pic_map_gpio(struct ti_gpio_softc *sc, struct intr_map_data_gpio *dag, 1003 u_int *irqp, uint32_t *modep) 1004 { 1005 uint32_t mode; 1006 1007 if (dag->gpio_pin_num >= sc->sc_maxpin) 1008 return (EINVAL); 1009 1010 mode = dag->gpio_intr_mode; 1011 if (mode != GPIO_INTR_LEVEL_LOW && mode != GPIO_INTR_LEVEL_HIGH && 1012 mode != GPIO_INTR_EDGE_RISING && mode != GPIO_INTR_EDGE_FALLING && 1013 mode != GPIO_INTR_EDGE_BOTH) 1014 return (EINVAL); 1015 1016 *irqp = dag->gpio_pin_num; 1017 if (modep != NULL) 1018 *modep = mode; 1019 return (0); 1020 } 1021 1022 static int 1023 ti_gpio_pic_map(struct ti_gpio_softc *sc, struct intr_map_data *data, 1024 u_int *irqp, uint32_t *modep) 1025 { 1026 1027 switch (data->type) { 1028 case INTR_MAP_DATA_FDT: 1029 return (ti_gpio_pic_map_fdt(sc, 1030 (struct intr_map_data_fdt *)data, irqp, modep)); 1031 case INTR_MAP_DATA_GPIO: 1032 return (ti_gpio_pic_map_gpio(sc, 1033 (struct intr_map_data_gpio *)data, irqp, modep)); 1034 default: 1035 return (ENOTSUP); 1036 } 1037 } 1038 1039 static int 1040 ti_gpio_pic_map_intr(device_t dev, struct intr_map_data *data, 1041 struct intr_irqsrc **isrcp) 1042 { 1043 int error; 1044 u_int irq; 1045 struct ti_gpio_softc *sc = device_get_softc(dev); 1046 1047 error = ti_gpio_pic_map(sc, data, &irq, NULL); 1048 if (error == 0) 1049 *isrcp = &sc->sc_isrcs[irq].tgi_isrc; 1050 return (error); 1051 } 1052 1053 static void 1054 ti_gpio_pic_post_filter(device_t dev, struct intr_irqsrc *isrc) 1055 { 1056 struct ti_gpio_softc *sc = device_get_softc(dev); 1057 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 1058 1059 if (ti_gpio_isrc_is_level(tgi)) 1060 ti_gpio_isrc_eoi(sc, tgi); 1061 } 1062 1063 static void 1064 ti_gpio_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc) 1065 { 1066 1067 ti_gpio_pic_enable_intr(dev, isrc); 1068 } 1069 1070 static void 1071 ti_gpio_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc) 1072 { 1073 struct ti_gpio_softc *sc = device_get_softc(dev); 1074 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 1075 1076 ti_gpio_isrc_mask(sc, tgi); 1077 if (ti_gpio_isrc_is_level(tgi)) 1078 ti_gpio_isrc_eoi(sc, tgi); 1079 } 1080 1081 static int 1082 ti_gpio_pic_setup_intr(device_t dev, struct intr_irqsrc *isrc, 1083 struct resource *res, struct intr_map_data *data) 1084 { 1085 u_int irq; 1086 uint32_t mode; 1087 struct ti_gpio_softc *sc; 1088 struct ti_gpio_irqsrc *tgi; 1089 1090 if (data == NULL) 1091 return (ENOTSUP); 1092 1093 sc = device_get_softc(dev); 1094 tgi = (struct ti_gpio_irqsrc *)isrc; 1095 1096 /* Get and check config for an interrupt. */ 1097 if (ti_gpio_pic_map(sc, data, &irq, &mode) != 0 || tgi->tgi_irq != irq) 1098 return (EINVAL); 1099 1100 /* 1101 * If this is a setup for another handler, 1102 * only check that its configuration match. 1103 */ 1104 if (isrc->isrc_handlers != 0) 1105 return (tgi->tgi_mode == mode ? 0 : EINVAL); 1106 1107 ti_gpio_pic_config_intr(sc, tgi, mode); 1108 return (0); 1109 } 1110 1111 static int 1112 ti_gpio_pic_teardown_intr(device_t dev, struct intr_irqsrc *isrc, 1113 struct resource *res, struct intr_map_data *data) 1114 { 1115 struct ti_gpio_softc *sc = device_get_softc(dev); 1116 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 1117 1118 if (isrc->isrc_handlers == 0) 1119 ti_gpio_pic_config_intr(sc, tgi, GPIO_INTR_CONFORM); 1120 return (0); 1121 } 1122 1123 #else 1124 static uint32_t 1125 ti_gpio_intr_reg(struct ti_gpio_softc *sc, int irq) 1126 { 1127 1128 if (ti_gpio_valid_pin(sc, irq) != 0) 1129 return (0); 1130 1131 if (sc->sc_irq_trigger[irq] == INTR_TRIGGER_LEVEL) { 1132 if (sc->sc_irq_polarity[irq] == INTR_POLARITY_LOW) 1133 return (TI_GPIO_LEVELDETECT0); 1134 else if (sc->sc_irq_polarity[irq] == INTR_POLARITY_HIGH) 1135 return (TI_GPIO_LEVELDETECT1); 1136 } else if (sc->sc_irq_trigger[irq] == INTR_TRIGGER_EDGE) { 1137 if (sc->sc_irq_polarity[irq] == INTR_POLARITY_LOW) 1138 return (TI_GPIO_FALLINGDETECT); 1139 else if (sc->sc_irq_polarity[irq] == INTR_POLARITY_HIGH) 1140 return (TI_GPIO_RISINGDETECT); 1141 } 1142 1143 return (0); 1144 } 1145 1146 static void 1147 ti_gpio_mask_irq_internal(struct ti_gpio_softc *sc, int irq) 1148 { 1149 uint32_t reg, val; 1150 1151 if (ti_gpio_valid_pin(sc, irq) != 0) 1152 return; 1153 1154 TI_GPIO_LOCK(sc); 1155 ti_gpio_intr_clr(sc, TI_GPIO_MASK(irq)); 1156 reg = ti_gpio_intr_reg(sc, irq); 1157 if (reg != 0) { 1158 val = ti_gpio_read_4(sc, reg); 1159 val &= ~TI_GPIO_MASK(irq); 1160 ti_gpio_write_4(sc, reg, val); 1161 } 1162 TI_GPIO_UNLOCK(sc); 1163 } 1164 1165 static void 1166 ti_gpio_unmask_irq_internal(struct ti_gpio_softc *sc, int irq) 1167 { 1168 uint32_t reg, val; 1169 1170 if (ti_gpio_valid_pin(sc, irq) != 0) 1171 return; 1172 1173 TI_GPIO_LOCK(sc); 1174 reg = ti_gpio_intr_reg(sc, irq); 1175 if (reg != 0) { 1176 val = ti_gpio_read_4(sc, reg); 1177 val |= TI_GPIO_MASK(irq); 1178 ti_gpio_write_4(sc, reg, val); 1179 ti_gpio_intr_set(sc, TI_GPIO_MASK(irq)); 1180 } 1181 TI_GPIO_UNLOCK(sc); 1182 } 1183 1184 static void 1185 ti_gpio_mask_irq(void *source) 1186 { 1187 struct ti_gpio_mask_arg *arg = source; 1188 1189 ti_gpio_mask_irq_internal(arg->softc, arg->pin); 1190 } 1191 1192 static void 1193 ti_gpio_unmask_irq(void *source) 1194 { 1195 struct ti_gpio_mask_arg *arg = source; 1196 1197 ti_gpio_unmask_irq_internal(arg->softc, arg->pin); 1198 } 1199 1200 static int 1201 ti_gpio_activate_resource(device_t dev, device_t child, int type, int rid, 1202 struct resource *res) 1203 { 1204 struct ti_gpio_mask_arg mask_arg; 1205 1206 if (type != SYS_RES_IRQ) 1207 return (ENXIO); 1208 1209 /* Unmask the interrupt. */ 1210 mask_arg.pin = rman_get_start(res); 1211 mask_arg.softc = device_get_softc(dev); 1212 1213 ti_gpio_unmask_irq((void *)&mask_arg); 1214 1215 return (0); 1216 } 1217 1218 static int 1219 ti_gpio_deactivate_resource(device_t dev, device_t child, int type, int rid, 1220 struct resource *res) 1221 { 1222 int pin; 1223 1224 if (type != SYS_RES_IRQ) 1225 return (ENXIO); 1226 1227 /* Mask the interrupt. */ 1228 pin = rman_get_start(res); 1229 ti_gpio_mask_irq((void *)(uintptr_t)pin); 1230 1231 return (0); 1232 } 1233 1234 static int 1235 ti_gpio_config_intr(device_t dev, int irq, enum intr_trigger trig, 1236 enum intr_polarity pol) 1237 { 1238 struct ti_gpio_softc *sc; 1239 uint32_t oldreg, reg, val; 1240 1241 sc = device_get_softc(dev); 1242 if (ti_gpio_valid_pin(sc, irq) != 0) 1243 return (EINVAL); 1244 1245 /* There is no standard trigger or polarity. */ 1246 if (trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM) 1247 return (EINVAL); 1248 1249 TI_GPIO_LOCK(sc); 1250 /* 1251 * TRM recommends add the new event before remove the old one to 1252 * avoid losing interrupts. 1253 */ 1254 oldreg = ti_gpio_intr_reg(sc, irq); 1255 sc->sc_irq_trigger[irq] = trig; 1256 sc->sc_irq_polarity[irq] = pol; 1257 reg = ti_gpio_intr_reg(sc, irq); 1258 if (reg != 0) { 1259 /* Apply the new settings. */ 1260 val = ti_gpio_read_4(sc, reg); 1261 val |= TI_GPIO_MASK(irq); 1262 ti_gpio_write_4(sc, reg, val); 1263 } 1264 if (reg != oldreg && oldreg != 0) { 1265 /* Remove the old settings. */ 1266 val = ti_gpio_read_4(sc, oldreg); 1267 val &= ~TI_GPIO_MASK(irq); 1268 ti_gpio_write_4(sc, oldreg, val); 1269 } 1270 TI_GPIO_UNLOCK(sc); 1271 1272 return (0); 1273 } 1274 1275 static int 1276 ti_gpio_setup_intr(device_t dev, device_t child, struct resource *ires, 1277 int flags, driver_filter_t *filt, driver_intr_t *handler, 1278 void *arg, void **cookiep) 1279 { 1280 struct ti_gpio_softc *sc; 1281 struct intr_event *event; 1282 int pin, error; 1283 1284 sc = device_get_softc(dev); 1285 pin = rman_get_start(ires); 1286 if (ti_gpio_valid_pin(sc, pin) != 0) 1287 panic("%s: bad pin %d", __func__, pin); 1288 1289 event = sc->sc_events[pin]; 1290 if (event == NULL) { 1291 sc->sc_mask_args[pin].softc = sc; 1292 sc->sc_mask_args[pin].pin = pin; 1293 error = intr_event_create(&event, (void *)&sc->sc_mask_args[pin], 0, 1294 pin, ti_gpio_mask_irq, ti_gpio_unmask_irq, NULL, NULL, 1295 "gpio%d pin%d:", device_get_unit(dev), pin); 1296 if (error != 0) 1297 return (error); 1298 sc->sc_events[pin] = event; 1299 } 1300 intr_event_add_handler(event, device_get_nameunit(child), filt, 1301 handler, arg, intr_priority(flags), flags, cookiep); 1302 1303 return (0); 1304 } 1305 1306 static int 1307 ti_gpio_teardown_intr(device_t dev, device_t child, struct resource *ires, 1308 void *cookie) 1309 { 1310 struct ti_gpio_softc *sc; 1311 int pin, err; 1312 1313 sc = device_get_softc(dev); 1314 pin = rman_get_start(ires); 1315 if (ti_gpio_valid_pin(sc, pin) != 0) 1316 panic("%s: bad pin %d", __func__, pin); 1317 if (sc->sc_events[pin] == NULL) 1318 panic("Trying to teardown unoccupied IRQ"); 1319 err = intr_event_remove_handler(cookie); 1320 if (!err) 1321 sc->sc_events[pin] = NULL; 1322 1323 return (err); 1324 } 1325 #endif 1326 1327 static phandle_t 1328 ti_gpio_get_node(device_t bus, device_t dev) 1329 { 1330 1331 /* We only have one child, the GPIO bus, which needs our own node. */ 1332 return (ofw_bus_get_node(bus)); 1333 } 1334 1335 static device_method_t ti_gpio_methods[] = { 1336 DEVMETHOD(device_attach, ti_gpio_attach), 1337 DEVMETHOD(device_detach, ti_gpio_detach), 1338 1339 /* GPIO protocol */ 1340 DEVMETHOD(gpio_get_bus, ti_gpio_get_bus), 1341 DEVMETHOD(gpio_pin_max, ti_gpio_pin_max), 1342 DEVMETHOD(gpio_pin_getname, ti_gpio_pin_getname), 1343 DEVMETHOD(gpio_pin_getflags, ti_gpio_pin_getflags), 1344 DEVMETHOD(gpio_pin_getcaps, ti_gpio_pin_getcaps), 1345 DEVMETHOD(gpio_pin_setflags, ti_gpio_pin_setflags), 1346 DEVMETHOD(gpio_pin_get, ti_gpio_pin_get), 1347 DEVMETHOD(gpio_pin_set, ti_gpio_pin_set), 1348 DEVMETHOD(gpio_pin_toggle, ti_gpio_pin_toggle), 1349 1350 #ifdef INTRNG 1351 /* Interrupt controller interface */ 1352 DEVMETHOD(pic_disable_intr, ti_gpio_pic_disable_intr), 1353 DEVMETHOD(pic_enable_intr, ti_gpio_pic_enable_intr), 1354 DEVMETHOD(pic_map_intr, ti_gpio_pic_map_intr), 1355 DEVMETHOD(pic_setup_intr, ti_gpio_pic_setup_intr), 1356 DEVMETHOD(pic_teardown_intr, ti_gpio_pic_teardown_intr), 1357 DEVMETHOD(pic_post_filter, ti_gpio_pic_post_filter), 1358 DEVMETHOD(pic_post_ithread, ti_gpio_pic_post_ithread), 1359 DEVMETHOD(pic_pre_ithread, ti_gpio_pic_pre_ithread), 1360 #else 1361 /* Bus interface */ 1362 DEVMETHOD(bus_activate_resource, ti_gpio_activate_resource), 1363 DEVMETHOD(bus_deactivate_resource, ti_gpio_deactivate_resource), 1364 DEVMETHOD(bus_config_intr, ti_gpio_config_intr), 1365 DEVMETHOD(bus_setup_intr, ti_gpio_setup_intr), 1366 DEVMETHOD(bus_teardown_intr, ti_gpio_teardown_intr), 1367 #endif 1368 1369 /* ofw_bus interface */ 1370 DEVMETHOD(ofw_bus_get_node, ti_gpio_get_node), 1371 1372 {0, 0}, 1373 }; 1374 1375 driver_t ti_gpio_driver = { 1376 "gpio", 1377 ti_gpio_methods, 1378 sizeof(struct ti_gpio_softc), 1379 }; 1380