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