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/gpio/gpiobusvar.h> 62 #include <dev/ofw/openfirm.h> 63 #include <dev/ofw/ofw_bus.h> 64 #include <dev/ofw/ofw_bus_subr.h> 65 66 #include "gpio_if.h" 67 #include "ti_gpio_if.h" 68 #include "pic_if.h" 69 70 #if !defined(SOC_OMAP4) && !defined(SOC_TI_AM335X) 71 #error "Unknown SoC" 72 #endif 73 74 /* Register definitions */ 75 #define TI_GPIO_REVISION 0x0000 76 #define TI_GPIO_SYSCONFIG 0x0010 77 #define TI_GPIO_IRQSTATUS_RAW_0 0x0024 78 #define TI_GPIO_IRQSTATUS_RAW_1 0x0028 79 #define TI_GPIO_IRQSTATUS_0 0x002C /* writing a 0 has no effect */ 80 #define TI_GPIO_IRQSTATUS_1 0x0030 /* writing a 0 has no effect */ 81 #define TI_GPIO_IRQSTATUS_SET_0 0x0034 /* writing a 0 has no effect */ 82 #define TI_GPIO_IRQSTATUS_SET_1 0x0038 /* writing a 0 has no effect */ 83 #define TI_GPIO_IRQSTATUS_CLR_0 0x003C /* writing a 0 has no effect */ 84 #define TI_GPIO_IRQSTATUS_CLR_1 0x0040 /* writing a 0 has no effect */ 85 #define TI_GPIO_IRQWAKEN_0 0x0044 86 #define TI_GPIO_IRQWAKEN_1 0x0048 87 #define TI_GPIO_SYSSTATUS 0x0114 88 #define TI_GPIO_IRQSTATUS1 0x0118 89 #define TI_GPIO_IRQENABLE1 0x011C 90 #define TI_GPIO_WAKEUPENABLE 0x0120 91 #define TI_GPIO_IRQSTATUS2 0x0128 92 #define TI_GPIO_IRQENABLE2 0x012C 93 #define TI_GPIO_CTRL 0x0130 94 #define TI_GPIO_OE 0x0134 95 #define TI_GPIO_DATAIN 0x0138 96 #define TI_GPIO_DATAOUT 0x013C 97 #define TI_GPIO_LEVELDETECT0 0x0140 /* RW register */ 98 #define TI_GPIO_LEVELDETECT1 0x0144 /* RW register */ 99 #define TI_GPIO_RISINGDETECT 0x0148 /* RW register */ 100 #define TI_GPIO_FALLINGDETECT 0x014C /* RW register */ 101 #define TI_GPIO_DEBOUNCENABLE 0x0150 102 #define TI_GPIO_DEBOUNCINGTIME 0x0154 103 #define TI_GPIO_CLEARWKUPENA 0x0180 104 #define TI_GPIO_SETWKUENA 0x0184 105 #define TI_GPIO_CLEARDATAOUT 0x0190 106 #define TI_GPIO_SETDATAOUT 0x0194 107 108 /* Other SoC Specific definitions */ 109 #define OMAP4_FIRST_GPIO_BANK 1 110 #define OMAP4_INTR_PER_BANK 1 111 #define OMAP4_GPIO_REV 0x50600801 112 #define AM335X_FIRST_GPIO_BANK 0 113 #define AM335X_INTR_PER_BANK 2 114 #define AM335X_GPIO_REV 0x50600801 115 #define PINS_PER_BANK 32 116 #define TI_GPIO_MASK(p) (1U << ((p) % PINS_PER_BANK)) 117 118 static int ti_gpio_intr(void *arg); 119 static int ti_gpio_detach(device_t); 120 121 static int ti_gpio_pic_attach(struct ti_gpio_softc *sc); 122 static int ti_gpio_pic_detach(struct ti_gpio_softc *sc); 123 124 static u_int 125 ti_first_gpio_bank(void) 126 { 127 switch(ti_chip()) { 128 #ifdef SOC_OMAP4 129 case CHIP_OMAP_4: 130 return (OMAP4_FIRST_GPIO_BANK); 131 #endif 132 #ifdef SOC_TI_AM335X 133 case CHIP_AM335X: 134 return (AM335X_FIRST_GPIO_BANK); 135 #endif 136 } 137 return (0); 138 } 139 140 static uint32_t 141 ti_gpio_rev(void) 142 { 143 switch(ti_chip()) { 144 #ifdef SOC_OMAP4 145 case CHIP_OMAP_4: 146 return (OMAP4_GPIO_REV); 147 #endif 148 #ifdef SOC_TI_AM335X 149 case CHIP_AM335X: 150 return (AM335X_GPIO_REV); 151 #endif 152 } 153 return (0); 154 } 155 156 /** 157 * Macros for driver mutex locking 158 */ 159 #define TI_GPIO_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx) 160 #define TI_GPIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx) 161 #define TI_GPIO_LOCK_INIT(_sc) \ 162 mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \ 163 "ti_gpio", MTX_SPIN) 164 #define TI_GPIO_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx) 165 #define TI_GPIO_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED) 166 #define TI_GPIO_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED) 167 168 /** 169 * ti_gpio_read_4 - reads a 32-bit value from one of the GPIO registers 170 * @sc: GPIO device context 171 * @bank: The bank to read from 172 * @off: The offset of a register from the GPIO register address range 173 * 174 * 175 * RETURNS: 176 * 32-bit value read from the register. 177 */ 178 static inline uint32_t 179 ti_gpio_read_4(struct ti_gpio_softc *sc, bus_size_t off) 180 { 181 return (bus_read_4(sc->sc_mem_res, off)); 182 } 183 184 /** 185 * ti_gpio_write_4 - writes a 32-bit value to one of the GPIO registers 186 * @sc: GPIO device context 187 * @bank: The bank to write to 188 * @off: The offset of a register from the GPIO register address range 189 * @val: The value to write into the register 190 * 191 * RETURNS: 192 * nothing 193 */ 194 static inline void 195 ti_gpio_write_4(struct ti_gpio_softc *sc, bus_size_t off, 196 uint32_t val) 197 { 198 bus_write_4(sc->sc_mem_res, off, val); 199 } 200 201 static inline void 202 ti_gpio_intr_clr(struct ti_gpio_softc *sc, uint32_t mask) 203 { 204 205 /* We clear both set of registers. */ 206 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_CLR_0, mask); 207 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_CLR_1, mask); 208 } 209 210 static inline void 211 ti_gpio_intr_set(struct ti_gpio_softc *sc, uint32_t mask) 212 { 213 214 /* 215 * On OMAP4 we unmask only the MPU interrupt and on AM335x we 216 * also activate only the first interrupt. 217 */ 218 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_SET_0, mask); 219 } 220 221 static inline void 222 ti_gpio_intr_ack(struct ti_gpio_softc *sc, uint32_t mask) 223 { 224 225 /* 226 * Acknowledge the interrupt on both registers even if we use only 227 * the first one. 228 */ 229 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_0, mask); 230 ti_gpio_write_4(sc, TI_GPIO_IRQSTATUS_1, mask); 231 } 232 233 static inline uint32_t 234 ti_gpio_intr_status(struct ti_gpio_softc *sc) 235 { 236 uint32_t reg; 237 238 /* Get the status from both registers. */ 239 reg = ti_gpio_read_4(sc, TI_GPIO_IRQSTATUS_0); 240 reg |= ti_gpio_read_4(sc, TI_GPIO_IRQSTATUS_1); 241 242 return (reg); 243 } 244 245 static device_t 246 ti_gpio_get_bus(device_t dev) 247 { 248 struct ti_gpio_softc *sc; 249 250 sc = device_get_softc(dev); 251 252 return (sc->sc_busdev); 253 } 254 255 /** 256 * ti_gpio_pin_max - Returns the maximum number of GPIO pins 257 * @dev: gpio device handle 258 * @maxpin: pointer to a value that upon return will contain the maximum number 259 * of pins in the device. 260 * 261 * 262 * LOCKING: 263 * No locking required, returns static data. 264 * 265 * RETURNS: 266 * Returns 0 on success otherwise an error code 267 */ 268 static int 269 ti_gpio_pin_max(device_t dev, int *maxpin) 270 { 271 272 *maxpin = PINS_PER_BANK - 1; 273 274 return (0); 275 } 276 277 static int 278 ti_gpio_valid_pin(struct ti_gpio_softc *sc, int pin) 279 { 280 281 if (pin >= sc->sc_maxpin || sc->sc_mem_res == NULL) 282 return (EINVAL); 283 284 return (0); 285 } 286 287 /** 288 * ti_gpio_pin_getcaps - Gets the capabilities of a given pin 289 * @dev: gpio device handle 290 * @pin: the number of the pin 291 * @caps: pointer to a value that upon return will contain the capabilities 292 * 293 * Currently all pins have the same capability, notably: 294 * - GPIO_PIN_INPUT 295 * - GPIO_PIN_OUTPUT 296 * - GPIO_PIN_PULLUP 297 * - GPIO_PIN_PULLDOWN 298 * - GPIO_INTR_LEVEL_LOW 299 * - GPIO_INTR_LEVEL_HIGH 300 * - GPIO_INTR_EDGE_RISING 301 * - GPIO_INTR_EDGE_FALLING 302 * - GPIO_INTR_EDGE_BOTH 303 * 304 * LOCKING: 305 * No locking required, returns static data. 306 * 307 * RETURNS: 308 * Returns 0 on success otherwise an error code 309 */ 310 static int 311 ti_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps) 312 { 313 struct ti_gpio_softc *sc; 314 315 sc = device_get_softc(dev); 316 if (ti_gpio_valid_pin(sc, pin) != 0) 317 return (EINVAL); 318 319 *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_PULLUP | 320 GPIO_PIN_PULLDOWN | GPIO_INTR_LEVEL_LOW | GPIO_INTR_LEVEL_HIGH | 321 GPIO_INTR_EDGE_RISING | GPIO_INTR_EDGE_FALLING | 322 GPIO_INTR_EDGE_BOTH); 323 324 return (0); 325 } 326 327 /** 328 * ti_gpio_pin_getflags - Gets the current flags of a given pin 329 * @dev: gpio device handle 330 * @pin: the number of the pin 331 * @flags: upon return will contain the current flags of the pin 332 * 333 * Reads the current flags of a given pin, here we actually read the H/W 334 * registers to determine the flags, rather than storing the value in the 335 * setflags call. 336 * 337 * LOCKING: 338 * Internally locks the context 339 * 340 * RETURNS: 341 * Returns 0 on success otherwise an error code 342 */ 343 static int 344 ti_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags) 345 { 346 struct ti_gpio_softc *sc; 347 348 sc = device_get_softc(dev); 349 if (ti_gpio_valid_pin(sc, pin) != 0) 350 return (EINVAL); 351 352 /* Get the current pin state */ 353 TI_GPIO_LOCK(sc); 354 TI_GPIO_GET_FLAGS(dev, pin, flags); 355 TI_GPIO_UNLOCK(sc); 356 357 return (0); 358 } 359 360 /** 361 * ti_gpio_pin_getname - Gets the name of a given pin 362 * @dev: gpio device handle 363 * @pin: the number of the pin 364 * @name: buffer to put the name in 365 * 366 * The driver simply calls the pins gpio_n, where 'n' is obviously the number 367 * of the pin. 368 * 369 * LOCKING: 370 * No locking required, returns static data. 371 * 372 * RETURNS: 373 * Returns 0 on success otherwise an error code 374 */ 375 static int 376 ti_gpio_pin_getname(device_t dev, uint32_t pin, char *name) 377 { 378 struct ti_gpio_softc *sc; 379 380 sc = device_get_softc(dev); 381 if (ti_gpio_valid_pin(sc, pin) != 0) 382 return (EINVAL); 383 384 /* Set a very simple name */ 385 snprintf(name, GPIOMAXNAME, "gpio_%u", pin); 386 name[GPIOMAXNAME - 1] = '\0'; 387 388 return (0); 389 } 390 391 /** 392 * ti_gpio_pin_setflags - Sets the flags for a given pin 393 * @dev: gpio device handle 394 * @pin: the number of the pin 395 * @flags: the flags to set 396 * 397 * The flags of the pin correspond to things like input/output mode, pull-ups, 398 * pull-downs, etc. This driver doesn't support all flags, only the following: 399 * - GPIO_PIN_INPUT 400 * - GPIO_PIN_OUTPUT 401 * - GPIO_PIN_PULLUP 402 * - GPIO_PIN_PULLDOWN 403 * 404 * LOCKING: 405 * Internally locks the context 406 * 407 * RETURNS: 408 * Returns 0 on success otherwise an error code 409 */ 410 static int 411 ti_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) 412 { 413 struct ti_gpio_softc *sc; 414 uint32_t oe; 415 416 sc = device_get_softc(dev); 417 if (ti_gpio_valid_pin(sc, pin) != 0) 418 return (EINVAL); 419 420 /* Set the GPIO mode and state */ 421 TI_GPIO_LOCK(sc); 422 if (TI_GPIO_SET_FLAGS(dev, pin, flags) != 0) { 423 TI_GPIO_UNLOCK(sc); 424 return (EINVAL); 425 } 426 427 /* If configuring as an output set the "output enable" bit */ 428 oe = ti_gpio_read_4(sc, TI_GPIO_OE); 429 if (flags & GPIO_PIN_INPUT) 430 oe |= TI_GPIO_MASK(pin); 431 else 432 oe &= ~TI_GPIO_MASK(pin); 433 ti_gpio_write_4(sc, TI_GPIO_OE, oe); 434 TI_GPIO_UNLOCK(sc); 435 436 return (0); 437 } 438 439 /** 440 * ti_gpio_pin_set - Sets the current level on a GPIO pin 441 * @dev: gpio device handle 442 * @pin: the number of the pin 443 * @value: non-zero value will drive the pin high, otherwise the pin is 444 * driven low. 445 * 446 * 447 * LOCKING: 448 * Internally locks the context 449 * 450 * RETURNS: 451 * Returns 0 on success otherwise a error code 452 */ 453 static int 454 ti_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value) 455 { 456 struct ti_gpio_softc *sc; 457 uint32_t reg; 458 459 sc = device_get_softc(dev); 460 if (ti_gpio_valid_pin(sc, pin) != 0) 461 return (EINVAL); 462 463 TI_GPIO_LOCK(sc); 464 if (value == GPIO_PIN_LOW) 465 reg = TI_GPIO_CLEARDATAOUT; 466 else 467 reg = TI_GPIO_SETDATAOUT; 468 ti_gpio_write_4(sc, reg, TI_GPIO_MASK(pin)); 469 TI_GPIO_UNLOCK(sc); 470 471 return (0); 472 } 473 474 /** 475 * ti_gpio_pin_get - Gets the current level on a GPIO pin 476 * @dev: gpio device handle 477 * @pin: the number of the pin 478 * @value: pointer to a value that upond return will contain the pin value 479 * 480 * The pin must be configured as an input pin beforehand, otherwise this 481 * function will fail. 482 * 483 * LOCKING: 484 * Internally locks the context 485 * 486 * RETURNS: 487 * Returns 0 on success otherwise a error code 488 */ 489 static int 490 ti_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value) 491 { 492 struct ti_gpio_softc *sc; 493 uint32_t oe, reg, val; 494 495 sc = device_get_softc(dev); 496 if (ti_gpio_valid_pin(sc, pin) != 0) 497 return (EINVAL); 498 499 /* 500 * Return data from output latch when set as output and from the 501 * input register otherwise. 502 */ 503 TI_GPIO_LOCK(sc); 504 oe = ti_gpio_read_4(sc, TI_GPIO_OE); 505 if (oe & TI_GPIO_MASK(pin)) 506 reg = TI_GPIO_DATAIN; 507 else 508 reg = TI_GPIO_DATAOUT; 509 val = ti_gpio_read_4(sc, reg); 510 *value = (val & TI_GPIO_MASK(pin)) ? 1 : 0; 511 TI_GPIO_UNLOCK(sc); 512 513 return (0); 514 } 515 516 /** 517 * ti_gpio_pin_toggle - Toggles a given GPIO pin 518 * @dev: gpio device handle 519 * @pin: the number of the pin 520 * 521 * 522 * LOCKING: 523 * Internally locks the context 524 * 525 * RETURNS: 526 * Returns 0 on success otherwise a error code 527 */ 528 static int 529 ti_gpio_pin_toggle(device_t dev, uint32_t pin) 530 { 531 struct ti_gpio_softc *sc; 532 uint32_t reg, val; 533 534 sc = device_get_softc(dev); 535 if (ti_gpio_valid_pin(sc, pin) != 0) 536 return (EINVAL); 537 538 /* Toggle the pin */ 539 TI_GPIO_LOCK(sc); 540 val = ti_gpio_read_4(sc, TI_GPIO_DATAOUT); 541 if (val & TI_GPIO_MASK(pin)) 542 reg = TI_GPIO_CLEARDATAOUT; 543 else 544 reg = TI_GPIO_SETDATAOUT; 545 ti_gpio_write_4(sc, reg, TI_GPIO_MASK(pin)); 546 TI_GPIO_UNLOCK(sc); 547 548 return (0); 549 } 550 551 static int 552 ti_gpio_bank_init(device_t dev) 553 { 554 int pin; 555 struct ti_gpio_softc *sc; 556 uint32_t flags, reg_oe, reg_set, rev; 557 clk_ident_t clk; 558 559 sc = device_get_softc(dev); 560 561 /* Enable the interface and functional clocks for the module. */ 562 clk = ti_hwmods_get_clock(dev); 563 if (clk == INVALID_CLK_IDENT) { 564 device_printf(dev, "failed to get device id based on ti,hwmods\n"); 565 return (EINVAL); 566 } 567 568 sc->sc_bank = clk - GPIO1_CLK + ti_first_gpio_bank(); 569 ti_prcm_clk_enable(clk); 570 571 /* 572 * Read the revision number of the module. TI don't publish the 573 * actual revision numbers, so instead the values have been 574 * determined by experimentation. 575 */ 576 rev = ti_gpio_read_4(sc, TI_GPIO_REVISION); 577 578 /* Check the revision. */ 579 if (rev != ti_gpio_rev()) { 580 device_printf(dev, "Warning: could not determine the revision " 581 "of GPIO module (revision:0x%08x)\n", rev); 582 return (EINVAL); 583 } 584 585 /* Disable interrupts for all pins. */ 586 ti_gpio_intr_clr(sc, 0xffffffff); 587 588 /* Init OE register based on pads configuration. */ 589 reg_oe = 0xffffffff; 590 reg_set = 0; 591 for (pin = 0; pin < PINS_PER_BANK; pin++) { 592 TI_GPIO_GET_FLAGS(dev, pin, &flags); 593 if (flags & GPIO_PIN_OUTPUT) { 594 reg_oe &= ~(1UL << pin); 595 if (flags & GPIO_PIN_PULLUP) 596 reg_set |= (1UL << pin); 597 } 598 } 599 ti_gpio_write_4(sc, TI_GPIO_OE, reg_oe); 600 if (reg_set) 601 ti_gpio_write_4(sc, TI_GPIO_SETDATAOUT, reg_set); 602 603 return (0); 604 } 605 606 /** 607 * ti_gpio_attach - attach function for the driver 608 * @dev: gpio device handle 609 * 610 * Allocates and sets up the driver context for all GPIO banks. This function 611 * expects the memory ranges and IRQs to already be allocated to the driver. 612 * 613 * LOCKING: 614 * None 615 * 616 * RETURNS: 617 * Always returns 0 618 */ 619 static int 620 ti_gpio_attach(device_t dev) 621 { 622 struct ti_gpio_softc *sc; 623 int err; 624 625 sc = device_get_softc(dev); 626 sc->sc_dev = dev; 627 TI_GPIO_LOCK_INIT(sc); 628 ti_gpio_pin_max(dev, &sc->sc_maxpin); 629 sc->sc_maxpin++; 630 631 sc->sc_mem_rid = 0; 632 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 633 &sc->sc_mem_rid, RF_ACTIVE); 634 if (!sc->sc_mem_res) { 635 device_printf(dev, "Error: could not allocate mem resources\n"); 636 ti_gpio_detach(dev); 637 return (ENXIO); 638 } 639 640 sc->sc_irq_rid = 0; 641 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, 642 &sc->sc_irq_rid, RF_ACTIVE); 643 if (!sc->sc_irq_res) { 644 device_printf(dev, "Error: could not allocate irq resources\n"); 645 ti_gpio_detach(dev); 646 return (ENXIO); 647 } 648 649 /* 650 * Register our interrupt filter for each of the IRQ resources. 651 */ 652 if (bus_setup_intr(dev, sc->sc_irq_res, 653 INTR_TYPE_MISC | INTR_MPSAFE, ti_gpio_intr, NULL, sc, 654 &sc->sc_irq_hdl) != 0) { 655 device_printf(dev, 656 "WARNING: unable to register interrupt filter\n"); 657 ti_gpio_detach(dev); 658 return (ENXIO); 659 } 660 661 if (ti_gpio_pic_attach(sc) != 0) { 662 device_printf(dev, "WARNING: unable to attach PIC\n"); 663 ti_gpio_detach(dev); 664 return (ENXIO); 665 } 666 667 /* We need to go through each block and ensure the clocks are running and 668 * the module is enabled. It might be better to do this only when the 669 * pins are configured which would result in less power used if the GPIO 670 * pins weren't used ... 671 */ 672 if (sc->sc_mem_res != NULL) { 673 /* Initialize the GPIO module. */ 674 err = ti_gpio_bank_init(dev); 675 if (err != 0) { 676 ti_gpio_detach(dev); 677 return (err); 678 } 679 } 680 681 sc->sc_busdev = gpiobus_attach_bus(dev); 682 if (sc->sc_busdev == NULL) { 683 ti_gpio_detach(dev); 684 return (ENXIO); 685 } 686 687 return (0); 688 } 689 690 /** 691 * ti_gpio_detach - detach function for the driver 692 * @dev: scm device handle 693 * 694 * Allocates and sets up the driver context, this simply entails creating a 695 * bus mappings for the SCM register set. 696 * 697 * LOCKING: 698 * None 699 * 700 * RETURNS: 701 * Always returns 0 702 */ 703 static int 704 ti_gpio_detach(device_t dev) 705 { 706 struct ti_gpio_softc *sc = device_get_softc(dev); 707 708 KASSERT(mtx_initialized(&sc->sc_mtx), ("gpio mutex not initialized")); 709 710 /* Disable all interrupts */ 711 if (sc->sc_mem_res != NULL) 712 ti_gpio_intr_clr(sc, 0xffffffff); 713 gpiobus_detach_bus(dev); 714 if (sc->sc_isrcs != NULL) 715 ti_gpio_pic_detach(sc); 716 /* Release the memory and IRQ resources. */ 717 if (sc->sc_irq_hdl) { 718 bus_teardown_intr(dev, sc->sc_irq_res, 719 sc->sc_irq_hdl); 720 } 721 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid, 722 sc->sc_irq_res); 723 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid, 724 sc->sc_mem_res); 725 TI_GPIO_LOCK_DESTROY(sc); 726 727 return (0); 728 } 729 730 static inline void 731 ti_gpio_rwreg_modify(struct ti_gpio_softc *sc, uint32_t reg, uint32_t mask, 732 bool set_bits) 733 { 734 uint32_t value; 735 736 value = ti_gpio_read_4(sc, reg); 737 ti_gpio_write_4(sc, reg, set_bits ? value | mask : value & ~mask); 738 } 739 740 static inline void 741 ti_gpio_isrc_mask(struct ti_gpio_softc *sc, struct ti_gpio_irqsrc *tgi) 742 { 743 744 /* Writing a 0 has no effect. */ 745 ti_gpio_intr_clr(sc, tgi->tgi_mask); 746 } 747 748 static inline void 749 ti_gpio_isrc_unmask(struct ti_gpio_softc *sc, struct ti_gpio_irqsrc *tgi) 750 { 751 752 /* Writing a 0 has no effect. */ 753 ti_gpio_intr_set(sc, tgi->tgi_mask); 754 } 755 756 static inline void 757 ti_gpio_isrc_eoi(struct ti_gpio_softc *sc, struct ti_gpio_irqsrc *tgi) 758 { 759 760 /* Writing a 0 has no effect. */ 761 ti_gpio_intr_ack(sc, tgi->tgi_mask); 762 } 763 764 static inline bool 765 ti_gpio_isrc_is_level(struct ti_gpio_irqsrc *tgi) 766 { 767 768 return (tgi->tgi_mode == GPIO_INTR_LEVEL_LOW || 769 tgi->tgi_mode == GPIO_INTR_LEVEL_HIGH); 770 } 771 772 static int 773 ti_gpio_intr(void *arg) 774 { 775 u_int irq; 776 uint32_t reg; 777 struct ti_gpio_softc *sc; 778 struct trapframe *tf; 779 struct ti_gpio_irqsrc *tgi; 780 781 sc = (struct ti_gpio_softc *)arg; 782 tf = curthread->td_intr_frame; 783 784 reg = ti_gpio_intr_status(sc); 785 for (irq = 0; irq < sc->sc_maxpin; irq++) { 786 tgi = &sc->sc_isrcs[irq]; 787 if ((reg & tgi->tgi_mask) == 0) 788 continue; 789 if (!ti_gpio_isrc_is_level(tgi)) 790 ti_gpio_isrc_eoi(sc, tgi); 791 if (intr_isrc_dispatch(&tgi->tgi_isrc, tf) != 0) { 792 ti_gpio_isrc_mask(sc, tgi); 793 if (ti_gpio_isrc_is_level(tgi)) 794 ti_gpio_isrc_eoi(sc, tgi); 795 device_printf(sc->sc_dev, "Stray irq %u disabled\n", 796 irq); 797 } 798 } 799 return (FILTER_HANDLED); 800 } 801 802 static int 803 ti_gpio_pic_attach(struct ti_gpio_softc *sc) 804 { 805 int error; 806 uint32_t irq; 807 const char *name; 808 809 sc->sc_isrcs = malloc(sizeof(*sc->sc_isrcs) * sc->sc_maxpin, M_DEVBUF, 810 M_WAITOK | M_ZERO); 811 812 name = device_get_nameunit(sc->sc_dev); 813 for (irq = 0; irq < sc->sc_maxpin; irq++) { 814 sc->sc_isrcs[irq].tgi_irq = irq; 815 sc->sc_isrcs[irq].tgi_mask = TI_GPIO_MASK(irq); 816 sc->sc_isrcs[irq].tgi_mode = GPIO_INTR_CONFORM; 817 818 error = intr_isrc_register(&sc->sc_isrcs[irq].tgi_isrc, 819 sc->sc_dev, 0, "%s,%u", name, irq); 820 if (error != 0) 821 return (error); /* XXX deregister ISRCs */ 822 } 823 if (intr_pic_register(sc->sc_dev, 824 OF_xref_from_node(ofw_bus_get_node(sc->sc_dev))) == NULL) 825 return (ENXIO); 826 827 return (0); 828 } 829 830 static int 831 ti_gpio_pic_detach(struct ti_gpio_softc *sc) 832 { 833 834 /* 835 * There has not been established any procedure yet 836 * how to detach PIC from living system correctly. 837 */ 838 device_printf(sc->sc_dev, "%s: not implemented yet\n", __func__); 839 return (EBUSY); 840 } 841 842 static void 843 ti_gpio_pic_config_intr(struct ti_gpio_softc *sc, struct ti_gpio_irqsrc *tgi, 844 uint32_t mode) 845 { 846 847 TI_GPIO_LOCK(sc); 848 ti_gpio_rwreg_modify(sc, TI_GPIO_RISINGDETECT, tgi->tgi_mask, 849 mode == GPIO_INTR_EDGE_RISING || mode == GPIO_INTR_EDGE_BOTH); 850 ti_gpio_rwreg_modify(sc, TI_GPIO_FALLINGDETECT, tgi->tgi_mask, 851 mode == GPIO_INTR_EDGE_FALLING || mode == GPIO_INTR_EDGE_BOTH); 852 ti_gpio_rwreg_modify(sc, TI_GPIO_LEVELDETECT1, tgi->tgi_mask, 853 mode == GPIO_INTR_LEVEL_HIGH); 854 ti_gpio_rwreg_modify(sc, TI_GPIO_LEVELDETECT0, tgi->tgi_mask, 855 mode == GPIO_INTR_LEVEL_LOW); 856 tgi->tgi_mode = mode; 857 TI_GPIO_UNLOCK(sc); 858 } 859 860 static void 861 ti_gpio_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc) 862 { 863 struct ti_gpio_softc *sc = device_get_softc(dev); 864 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 865 866 ti_gpio_isrc_mask(sc, tgi); 867 } 868 869 static void 870 ti_gpio_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc) 871 { 872 struct ti_gpio_softc *sc = device_get_softc(dev); 873 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 874 875 arm_irq_memory_barrier(tgi->tgi_irq); 876 ti_gpio_isrc_unmask(sc, tgi); 877 } 878 879 static int 880 ti_gpio_pic_map_fdt(struct ti_gpio_softc *sc, struct intr_map_data_fdt *daf, 881 u_int *irqp, uint32_t *modep) 882 { 883 uint32_t mode; 884 885 /* 886 * The first cell is the interrupt number. 887 * The second cell is used to specify flags: 888 * bits[3:0] trigger type and level flags: 889 * 1 = low-to-high edge triggered. 890 * 2 = high-to-low edge triggered. 891 * 4 = active high level-sensitive. 892 * 8 = active low level-sensitive. 893 */ 894 if (daf->ncells != 2 || daf->cells[0] >= sc->sc_maxpin) 895 return (EINVAL); 896 897 /* Only reasonable modes are supported. */ 898 if (daf->cells[1] == 1) 899 mode = GPIO_INTR_EDGE_RISING; 900 else if (daf->cells[1] == 2) 901 mode = GPIO_INTR_EDGE_FALLING; 902 else if (daf->cells[1] == 3) 903 mode = GPIO_INTR_EDGE_BOTH; 904 else if (daf->cells[1] == 4) 905 mode = GPIO_INTR_LEVEL_HIGH; 906 else if (daf->cells[1] == 8) 907 mode = GPIO_INTR_LEVEL_LOW; 908 else 909 return (EINVAL); 910 911 *irqp = daf->cells[0]; 912 if (modep != NULL) 913 *modep = mode; 914 return (0); 915 } 916 917 static int 918 ti_gpio_pic_map_gpio(struct ti_gpio_softc *sc, struct intr_map_data_gpio *dag, 919 u_int *irqp, uint32_t *modep) 920 { 921 uint32_t mode; 922 923 if (dag->gpio_pin_num >= sc->sc_maxpin) 924 return (EINVAL); 925 926 mode = dag->gpio_intr_mode; 927 if (mode != GPIO_INTR_LEVEL_LOW && mode != GPIO_INTR_LEVEL_HIGH && 928 mode != GPIO_INTR_EDGE_RISING && mode != GPIO_INTR_EDGE_FALLING && 929 mode != GPIO_INTR_EDGE_BOTH) 930 return (EINVAL); 931 932 *irqp = dag->gpio_pin_num; 933 if (modep != NULL) 934 *modep = mode; 935 return (0); 936 } 937 938 static int 939 ti_gpio_pic_map(struct ti_gpio_softc *sc, struct intr_map_data *data, 940 u_int *irqp, uint32_t *modep) 941 { 942 943 switch (data->type) { 944 case INTR_MAP_DATA_FDT: 945 return (ti_gpio_pic_map_fdt(sc, 946 (struct intr_map_data_fdt *)data, irqp, modep)); 947 case INTR_MAP_DATA_GPIO: 948 return (ti_gpio_pic_map_gpio(sc, 949 (struct intr_map_data_gpio *)data, irqp, modep)); 950 default: 951 return (ENOTSUP); 952 } 953 } 954 955 static int 956 ti_gpio_pic_map_intr(device_t dev, struct intr_map_data *data, 957 struct intr_irqsrc **isrcp) 958 { 959 int error; 960 u_int irq; 961 struct ti_gpio_softc *sc = device_get_softc(dev); 962 963 error = ti_gpio_pic_map(sc, data, &irq, NULL); 964 if (error == 0) 965 *isrcp = &sc->sc_isrcs[irq].tgi_isrc; 966 return (error); 967 } 968 969 static void 970 ti_gpio_pic_post_filter(device_t dev, struct intr_irqsrc *isrc) 971 { 972 struct ti_gpio_softc *sc = device_get_softc(dev); 973 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 974 975 if (ti_gpio_isrc_is_level(tgi)) 976 ti_gpio_isrc_eoi(sc, tgi); 977 } 978 979 static void 980 ti_gpio_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc) 981 { 982 983 ti_gpio_pic_enable_intr(dev, isrc); 984 } 985 986 static void 987 ti_gpio_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc) 988 { 989 struct ti_gpio_softc *sc = device_get_softc(dev); 990 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 991 992 ti_gpio_isrc_mask(sc, tgi); 993 if (ti_gpio_isrc_is_level(tgi)) 994 ti_gpio_isrc_eoi(sc, tgi); 995 } 996 997 static int 998 ti_gpio_pic_setup_intr(device_t dev, struct intr_irqsrc *isrc, 999 struct resource *res, struct intr_map_data *data) 1000 { 1001 u_int irq; 1002 uint32_t mode; 1003 struct ti_gpio_softc *sc; 1004 struct ti_gpio_irqsrc *tgi; 1005 1006 if (data == NULL) 1007 return (ENOTSUP); 1008 1009 sc = device_get_softc(dev); 1010 tgi = (struct ti_gpio_irqsrc *)isrc; 1011 1012 /* Get and check config for an interrupt. */ 1013 if (ti_gpio_pic_map(sc, data, &irq, &mode) != 0 || tgi->tgi_irq != irq) 1014 return (EINVAL); 1015 1016 /* 1017 * If this is a setup for another handler, 1018 * only check that its configuration match. 1019 */ 1020 if (isrc->isrc_handlers != 0) 1021 return (tgi->tgi_mode == mode ? 0 : EINVAL); 1022 1023 ti_gpio_pic_config_intr(sc, tgi, mode); 1024 return (0); 1025 } 1026 1027 static int 1028 ti_gpio_pic_teardown_intr(device_t dev, struct intr_irqsrc *isrc, 1029 struct resource *res, struct intr_map_data *data) 1030 { 1031 struct ti_gpio_softc *sc = device_get_softc(dev); 1032 struct ti_gpio_irqsrc *tgi = (struct ti_gpio_irqsrc *)isrc; 1033 1034 if (isrc->isrc_handlers == 0) 1035 ti_gpio_pic_config_intr(sc, tgi, GPIO_INTR_CONFORM); 1036 return (0); 1037 } 1038 1039 static phandle_t 1040 ti_gpio_get_node(device_t bus, device_t dev) 1041 { 1042 1043 /* We only have one child, the GPIO bus, which needs our own node. */ 1044 return (ofw_bus_get_node(bus)); 1045 } 1046 1047 static device_method_t ti_gpio_methods[] = { 1048 DEVMETHOD(device_attach, ti_gpio_attach), 1049 DEVMETHOD(device_detach, ti_gpio_detach), 1050 1051 /* GPIO protocol */ 1052 DEVMETHOD(gpio_get_bus, ti_gpio_get_bus), 1053 DEVMETHOD(gpio_pin_max, ti_gpio_pin_max), 1054 DEVMETHOD(gpio_pin_getname, ti_gpio_pin_getname), 1055 DEVMETHOD(gpio_pin_getflags, ti_gpio_pin_getflags), 1056 DEVMETHOD(gpio_pin_getcaps, ti_gpio_pin_getcaps), 1057 DEVMETHOD(gpio_pin_setflags, ti_gpio_pin_setflags), 1058 DEVMETHOD(gpio_pin_get, ti_gpio_pin_get), 1059 DEVMETHOD(gpio_pin_set, ti_gpio_pin_set), 1060 DEVMETHOD(gpio_pin_toggle, ti_gpio_pin_toggle), 1061 1062 /* Interrupt controller interface */ 1063 DEVMETHOD(pic_disable_intr, ti_gpio_pic_disable_intr), 1064 DEVMETHOD(pic_enable_intr, ti_gpio_pic_enable_intr), 1065 DEVMETHOD(pic_map_intr, ti_gpio_pic_map_intr), 1066 DEVMETHOD(pic_setup_intr, ti_gpio_pic_setup_intr), 1067 DEVMETHOD(pic_teardown_intr, ti_gpio_pic_teardown_intr), 1068 DEVMETHOD(pic_post_filter, ti_gpio_pic_post_filter), 1069 DEVMETHOD(pic_post_ithread, ti_gpio_pic_post_ithread), 1070 DEVMETHOD(pic_pre_ithread, ti_gpio_pic_pre_ithread), 1071 1072 /* ofw_bus interface */ 1073 DEVMETHOD(ofw_bus_get_node, ti_gpio_get_node), 1074 1075 {0, 0}, 1076 }; 1077 1078 driver_t ti_gpio_driver = { 1079 "gpio", 1080 ti_gpio_methods, 1081 sizeof(struct ti_gpio_softc), 1082 }; 1083