2 * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include "opt_platform.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
45 #include <machine/bus.h>
46 #include <machine/intr.h>
47 #include <machine/resource.h>
49 #include <dev/gpio/gpiobusvar.h>
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
56 #define GPIO_LOCK(_sc) mtx_lock(&(_sc)->mtx)
57 #define GPIO_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
58 #define GPIO_LOCK_INIT(_sc) mtx_init(&_sc->mtx, \
59 device_get_nameunit(_sc->dev), "tegra_gpio", MTX_DEF)
60 #define GPIO_LOCK_DESTROY(_sc) mtx_destroy(&_sc->mtx);
61 #define GPIO_ASSERT_LOCKED(_sc) mtx_assert(&_sc->mtx, MA_OWNED);
62 #define GPIO_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->mtx, MA_NOTOWNED);
64 #define WR4(_sc, _r, _v) bus_write_4((_sc)->mem_res, (_r), (_v))
65 #define RD4(_sc, _r) bus_read_4((_sc)->mem_res, (_r))
67 #define GPIO_BANK_OFFS 0x100 /* Bank offset */
68 #define GPIO_NUM_BANKS 8 /* Total number per bank */
69 #define GPIO_REGS_IN_BANK 4 /* Total registers in bank */
70 #define GPIO_PINS_IN_REG 8 /* Total pin in register */
72 #define GPIO_BANKNUM(n) ((n) / (GPIO_REGS_IN_BANK * GPIO_PINS_IN_REG))
73 #define GPIO_PORTNUM(n) (((n) / GPIO_PINS_IN_REG) % GPIO_REGS_IN_BANK)
74 #define GPIO_BIT(n) ((n) % GPIO_PINS_IN_REG)
76 #define GPIO_REGNUM(n) (GPIO_BANKNUM(n) * GPIO_BANK_OFFS + \
79 #define NGPIO ((GPIO_NUM_BANKS * GPIO_REGS_IN_BANK * GPIO_PINS_IN_REG) - 8)
81 /* Register offsets */
86 #define GPIO_INT_STA 0x40
87 #define GPIO_INT_ENB 0x50
88 #define GPIO_INT_LVL 0x60
89 #define GPIO_INT_LVL_DELTA (1 << 16)
90 #define GPIO_INT_LVL_EDGE (1 << 8)
91 #define GPIO_INT_LVL_HIGH (1 << 0)
92 #define GPIO_INT_LVL_MASK (GPIO_INT_LVL_DELTA | \
93 GPIO_INT_LVL_EDGE | GPIO_INT_LVL_HIGH)
94 #define GPIO_INT_CLR 0x70
95 #define GPIO_MSK_CNF 0x80
96 #define GPIO_MSK_OE 0x90
97 #define GPIO_MSK_OUT 0xA0
98 #define GPIO_MSK_INT_STA 0xC0
99 #define GPIO_MSK_INT_ENB 0xD0
100 #define GPIO_MSK_INT_LVL 0xE0
102 char *tegra_gpio_port_names[] = {
103 "A", "B", "C", "D", /* Bank 0 */
104 "E", "F", "G", "H", /* Bank 1 */
105 "I", "J", "K", "L", /* Bank 2 */
106 "M", "N", "O", "P", /* Bank 3 */
107 "Q", "R", "S", "T", /* Bank 4 */
108 "U", "V", "W", "X", /* Bank 5 */
109 "Y", "Z", "AA", "BB", /* Bank 6 */
110 "CC", "DD", "EE" /* Bank 7 */
113 struct tegra_gpio_irqsrc {
114 struct intr_irqsrc isrc;
119 struct tegra_gpio_softc;
120 struct tegra_gpio_irq_cookie {
121 struct tegra_gpio_softc *sc;
125 struct tegra_gpio_softc {
129 struct resource *mem_res;
130 struct resource *irq_res[GPIO_NUM_BANKS];
131 void *irq_ih[GPIO_NUM_BANKS];
132 struct tegra_gpio_irq_cookie irq_cookies[GPIO_NUM_BANKS];
134 struct gpio_pin gpio_pins[NGPIO];
135 struct tegra_gpio_irqsrc *isrcs;
138 static struct ofw_compat_data compat_data[] = {
139 {"nvidia,tegra124-gpio", 1},
140 {"nvidia,tegra210-gpio", 1},
144 /* --------------------------------------------------------------------------
150 gpio_write_masked(struct tegra_gpio_softc *sc, bus_size_t reg,
151 struct gpio_pin *pin, uint32_t val)
156 bit = GPIO_BIT(pin->gp_pin);
157 tmp = 0x100 << bit; /* mask */
158 tmp |= (val & 1) << bit; /* value */
159 bus_write_4(sc->mem_res, reg + GPIO_REGNUM(pin->gp_pin), tmp);
162 static inline uint32_t
163 gpio_read(struct tegra_gpio_softc *sc, bus_size_t reg, struct gpio_pin *pin)
168 bit = GPIO_BIT(pin->gp_pin);
169 val = bus_read_4(sc->mem_res, reg + GPIO_REGNUM(pin->gp_pin));
170 return (val >> bit) & 1;
174 tegra_gpio_pin_configure(struct tegra_gpio_softc *sc, struct gpio_pin *pin,
178 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) == 0)
181 /* Manage input/output */
182 pin->gp_flags &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
183 if (flags & GPIO_PIN_OUTPUT) {
184 pin->gp_flags |= GPIO_PIN_OUTPUT;
185 gpio_write_masked(sc, GPIO_MSK_OE, pin, 1);
187 pin->gp_flags |= GPIO_PIN_INPUT;
188 gpio_write_masked(sc, GPIO_MSK_OE, pin, 0);
193 tegra_gpio_get_bus(device_t dev)
195 struct tegra_gpio_softc *sc;
197 sc = device_get_softc(dev);
202 tegra_gpio_pin_max(device_t dev, int *maxpin)
210 tegra_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
212 struct tegra_gpio_softc *sc;
214 sc = device_get_softc(dev);
215 if (pin >= sc->gpio_npins)
219 *caps = sc->gpio_pins[pin].gp_caps;
226 tegra_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
228 struct tegra_gpio_softc *sc;
231 sc = device_get_softc(dev);
232 if (pin >= sc->gpio_npins)
236 cnf = gpio_read(sc, GPIO_CNF, &sc->gpio_pins[pin]);
241 *flags = sc->gpio_pins[pin].gp_flags;
248 tegra_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
250 struct tegra_gpio_softc *sc;
252 sc = device_get_softc(dev);
253 if (pin >= sc->gpio_npins)
257 memcpy(name, sc->gpio_pins[pin].gp_name, GPIOMAXNAME);
264 tegra_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
266 struct tegra_gpio_softc *sc;
269 sc = device_get_softc(dev);
270 if (pin >= sc->gpio_npins)
274 cnf = gpio_read(sc, GPIO_CNF, &sc->gpio_pins[pin]);
276 /* XXX - allow this for while ....
280 gpio_write_masked(sc, GPIO_MSK_CNF, &sc->gpio_pins[pin], 1);
282 tegra_gpio_pin_configure(sc, &sc->gpio_pins[pin], flags);
289 tegra_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
291 struct tegra_gpio_softc *sc;
293 sc = device_get_softc(dev);
294 if (pin >= sc->gpio_npins)
297 gpio_write_masked(sc, GPIO_MSK_OUT, &sc->gpio_pins[pin], value);
304 tegra_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
306 struct tegra_gpio_softc *sc;
308 sc = device_get_softc(dev);
309 if (pin >= sc->gpio_npins)
313 *val = gpio_read(sc, GPIO_IN, &sc->gpio_pins[pin]);
320 tegra_gpio_pin_toggle(device_t dev, uint32_t pin)
322 struct tegra_gpio_softc *sc;
324 sc = device_get_softc(dev);
325 if (pin >= sc->gpio_npins)
329 gpio_write_masked(sc, GPIO_MSK_OE, &sc->gpio_pins[pin],
330 gpio_read(sc, GPIO_IN, &sc->gpio_pins[pin]) ^ 1);
336 /* --------------------------------------------------------------------------
342 intr_write_masked(struct tegra_gpio_softc *sc, bus_addr_t reg,
343 struct tegra_gpio_irqsrc *tgi, uint32_t val)
348 bit = GPIO_BIT(tgi->irq);
349 tmp = 0x100 << bit; /* mask */
350 tmp |= (val & 1) << bit; /* value */
351 bus_write_4(sc->mem_res, reg + GPIO_REGNUM(tgi->irq), tmp);
355 intr_write_modify(struct tegra_gpio_softc *sc, bus_addr_t reg,
356 struct tegra_gpio_irqsrc *tgi, uint32_t val, uint32_t mask)
361 bit = GPIO_BIT(tgi->irq);
363 tmp = bus_read_4(sc->mem_res, reg + GPIO_REGNUM(tgi->irq));
364 tmp &= ~(mask << bit);
366 bus_write_4(sc->mem_res, reg + GPIO_REGNUM(tgi->irq), tmp);
371 tegra_gpio_isrc_mask(struct tegra_gpio_softc *sc,
372 struct tegra_gpio_irqsrc *tgi, uint32_t val)
375 intr_write_masked(sc, GPIO_MSK_INT_ENB, tgi, val);
379 tegra_gpio_isrc_eoi(struct tegra_gpio_softc *sc,
380 struct tegra_gpio_irqsrc *tgi)
383 intr_write_masked(sc, GPIO_INT_CLR, tgi, 1);
387 tegra_gpio_isrc_is_level(struct tegra_gpio_irqsrc *tgi)
390 return (tgi->cfgreg & GPIO_INT_LVL_EDGE);
394 tegra_gpio_intr(void *arg)
396 u_int irq, i, j, val, basepin;
397 struct tegra_gpio_softc *sc;
398 struct trapframe *tf;
399 struct tegra_gpio_irqsrc *tgi;
400 struct tegra_gpio_irq_cookie *cookie;
402 cookie = (struct tegra_gpio_irq_cookie *)arg;
404 tf = curthread->td_intr_frame;
406 for (i = 0; i < GPIO_REGS_IN_BANK; i++) {
407 basepin = cookie->bank_num * GPIO_REGS_IN_BANK *
408 GPIO_PINS_IN_REG + i * GPIO_PINS_IN_REG;
410 val = bus_read_4(sc->mem_res, GPIO_INT_STA +
411 GPIO_REGNUM(basepin));
412 val &= bus_read_4(sc->mem_res, GPIO_INT_ENB +
413 GPIO_REGNUM(basepin));
414 /* Interrupt handling */
415 for (j = 0; j < GPIO_PINS_IN_REG; j++) {
416 if ((val & (1 << j)) == 0)
419 tgi = &sc->isrcs[irq];
420 if (!tegra_gpio_isrc_is_level(tgi))
421 tegra_gpio_isrc_eoi(sc, tgi);
422 if (intr_isrc_dispatch(&tgi->isrc, tf) != 0) {
423 tegra_gpio_isrc_mask(sc, tgi, 0);
424 if (tegra_gpio_isrc_is_level(tgi))
425 tegra_gpio_isrc_eoi(sc, tgi);
426 device_printf(sc->dev,
427 "Stray irq %u disabled\n", irq);
432 return (FILTER_HANDLED);
436 tegra_gpio_pic_attach(struct tegra_gpio_softc *sc)
442 sc->isrcs = malloc(sizeof(*sc->isrcs) * sc->gpio_npins, M_DEVBUF,
445 name = device_get_nameunit(sc->dev);
446 for (irq = 0; irq < sc->gpio_npins; irq++) {
447 sc->isrcs[irq].irq = irq;
448 sc->isrcs[irq].cfgreg = 0;
449 error = intr_isrc_register(&sc->isrcs[irq].isrc,
450 sc->dev, 0, "%s,%u", name, irq);
452 return (error); /* XXX deregister ISRCs */
454 if (intr_pic_register(sc->dev,
455 OF_xref_from_node(ofw_bus_get_node(sc->dev))) == NULL)
462 tegra_gpio_pic_detach(struct tegra_gpio_softc *sc)
466 * There has not been established any procedure yet
467 * how to detach PIC from living system correctly.
469 device_printf(sc->dev, "%s: not implemented yet\n", __func__);
474 tegra_gpio_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
476 struct tegra_gpio_softc *sc;
477 struct tegra_gpio_irqsrc *tgi;
479 sc = device_get_softc(dev);
480 tgi = (struct tegra_gpio_irqsrc *)isrc;
481 tegra_gpio_isrc_mask(sc, tgi, 0);
485 tegra_gpio_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
487 struct tegra_gpio_softc *sc;
488 struct tegra_gpio_irqsrc *tgi;
490 sc = device_get_softc(dev);
491 tgi = (struct tegra_gpio_irqsrc *)isrc;
492 tegra_gpio_isrc_mask(sc, tgi, 1);
496 tegra_gpio_pic_map_fdt(struct tegra_gpio_softc *sc, u_int ncells,
497 pcell_t *cells, u_int *irqp, uint32_t *regp)
502 * The first cell is the interrupt number.
503 * The second cell is used to specify flags:
504 * bits[3:0] trigger type and level flags:
505 * 1 = low-to-high edge triggered.
506 * 2 = high-to-low edge triggered.
507 * 4 = active high level-sensitive.
508 * 8 = active low level-sensitive.
510 if (ncells != 2 || cells[0] >= sc->gpio_npins)
514 * All interrupt types could be set for an interrupt at one moment.
515 * At least, the combination of 'low-to-high' and 'high-to-low' edge
516 * triggered interrupt types can make a sense.
519 reg = GPIO_INT_LVL_EDGE | GPIO_INT_LVL_HIGH;
520 else if (cells[1] == 2)
521 reg = GPIO_INT_LVL_EDGE;
522 else if (cells[1] == 3)
523 reg = GPIO_INT_LVL_EDGE | GPIO_INT_LVL_DELTA;
524 else if (cells[1] == 4)
525 reg = GPIO_INT_LVL_HIGH;
526 else if (cells[1] == 8)
538 tegra_gpio_pic_map_gpio(struct tegra_gpio_softc *sc, u_int gpio_pin_num,
539 u_int gpio_pin_flags, u_int intr_mode, u_int *irqp, uint32_t *regp)
544 if (gpio_pin_num >= sc->gpio_npins)
547 case GPIO_INTR_CONFORM:
548 case GPIO_INTR_LEVEL_LOW:
551 case GPIO_INTR_LEVEL_HIGH:
552 reg = GPIO_INT_LVL_HIGH;
554 case GPIO_INTR_EDGE_RISING:
555 reg = GPIO_INT_LVL_EDGE | GPIO_INT_LVL_HIGH;
557 case GPIO_INTR_EDGE_FALLING:
558 reg = GPIO_INT_LVL_EDGE;
560 case GPIO_INTR_EDGE_BOTH:
561 reg = GPIO_INT_LVL_EDGE | GPIO_INT_LVL_DELTA;
566 *irqp = gpio_pin_num;
573 tegra_gpio_pic_map_intr(device_t dev, struct intr_map_data *data,
574 struct intr_irqsrc **isrcp)
578 struct tegra_gpio_softc *sc;
580 sc = device_get_softc(dev);
582 if (data->type == INTR_MAP_DATA_FDT) {
583 struct intr_map_data_fdt *daf;
585 daf = (struct intr_map_data_fdt *)data;
586 rv = tegra_gpio_pic_map_fdt(sc, daf->ncells, daf->cells, &irq,
588 } else if (data->type == INTR_MAP_DATA_GPIO) {
589 struct intr_map_data_gpio *dag;
591 dag = (struct intr_map_data_gpio *)data;
592 rv = tegra_gpio_pic_map_gpio(sc, dag->gpio_pin_num,
593 dag->gpio_pin_flags, dag->gpio_intr_mode, &irq, NULL);
598 *isrcp = &sc->isrcs[irq].isrc;
603 tegra_gpio_pic_post_filter(device_t dev, struct intr_irqsrc *isrc)
605 struct tegra_gpio_softc *sc;
606 struct tegra_gpio_irqsrc *tgi;
608 sc = device_get_softc(dev);
609 tgi = (struct tegra_gpio_irqsrc *)isrc;
610 if (tegra_gpio_isrc_is_level(tgi))
611 tegra_gpio_isrc_eoi(sc, tgi);
615 tegra_gpio_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
617 struct tegra_gpio_softc *sc;
618 struct tegra_gpio_irqsrc *tgi;
620 sc = device_get_softc(dev);
621 tgi = (struct tegra_gpio_irqsrc *)isrc;
622 tegra_gpio_isrc_mask(sc, tgi, 1);
626 tegra_gpio_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
628 struct tegra_gpio_softc *sc;
629 struct tegra_gpio_irqsrc *tgi;
631 sc = device_get_softc(dev);
632 tgi = (struct tegra_gpio_irqsrc *)isrc;
634 tegra_gpio_isrc_mask(sc, tgi, 0);
635 if (tegra_gpio_isrc_is_level(tgi))
636 tegra_gpio_isrc_eoi(sc, tgi);
640 tegra_gpio_pic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
641 struct resource *res, struct intr_map_data *data)
646 struct tegra_gpio_softc *sc;
647 struct tegra_gpio_irqsrc *tgi;
649 sc = device_get_softc(dev);
650 tgi = (struct tegra_gpio_irqsrc *)isrc;
655 /* Get and check config for an interrupt. */
656 if (data->type == INTR_MAP_DATA_FDT) {
657 struct intr_map_data_fdt *daf;
659 daf = (struct intr_map_data_fdt *)data;
660 rv = tegra_gpio_pic_map_fdt(sc, daf->ncells, daf->cells, &irq,
662 } else if (data->type == INTR_MAP_DATA_GPIO) {
663 struct intr_map_data_gpio *dag;
665 dag = (struct intr_map_data_gpio *)data;
666 rv = tegra_gpio_pic_map_gpio(sc, dag->gpio_pin_num,
667 dag->gpio_pin_flags, dag->gpio_intr_mode, &irq, &cfgreg);
674 * If this is a setup for another handler,
675 * only check that its configuration match.
677 if (isrc->isrc_handlers != 0)
678 return (tgi->cfgreg == cfgreg ? 0 : EINVAL);
680 tgi->cfgreg = cfgreg;
681 intr_write_modify(sc, GPIO_INT_LVL, tgi, cfgreg, GPIO_INT_LVL_MASK);
682 tegra_gpio_pic_enable_intr(dev, isrc);
688 tegra_gpio_pic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
689 struct resource *res, struct intr_map_data *data)
691 struct tegra_gpio_softc *sc;
692 struct tegra_gpio_irqsrc *tgi;
694 sc = device_get_softc(dev);
695 tgi = (struct tegra_gpio_irqsrc *)isrc;
697 if (isrc->isrc_handlers == 0)
698 tegra_gpio_isrc_mask(sc, tgi, 0);
703 tegra_gpio_probe(device_t dev)
706 if (!ofw_bus_status_okay(dev))
708 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
709 device_set_desc(dev, "Tegra GPIO Controller");
710 return (BUS_PROBE_DEFAULT);
716 /* --------------------------------------------------------------------------
722 tegra_gpio_detach(device_t dev)
724 struct tegra_gpio_softc *sc;
727 sc = device_get_softc(dev);
729 KASSERT(mtx_initialized(&sc->mtx), ("gpio mutex not initialized"));
731 for (i = 0; i < GPIO_NUM_BANKS; i++) {
732 if (sc->irq_ih[i] != NULL)
733 bus_teardown_intr(dev, sc->irq_res[i], sc->irq_ih[i]);
736 if (sc->isrcs != NULL)
737 tegra_gpio_pic_detach(sc);
739 gpiobus_detach_bus(dev);
741 for (i = 0; i < GPIO_NUM_BANKS; i++) {
742 if (sc->irq_res[i] != NULL)
743 bus_release_resource(dev, SYS_RES_IRQ, 0,
746 if (sc->mem_res != NULL)
747 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
748 GPIO_LOCK_DESTROY(sc);
754 tegra_gpio_attach(device_t dev)
756 struct tegra_gpio_softc *sc;
759 sc = device_get_softc(dev);
763 /* Allocate bus_space resources. */
765 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
767 if (sc->mem_res == NULL) {
768 device_printf(dev, "Cannot allocate memory resources\n");
769 tegra_gpio_detach(dev);
773 sc->gpio_npins = NGPIO;
774 for (i = 0; i < sc->gpio_npins; i++) {
775 sc->gpio_pins[i].gp_pin = i;
776 sc->gpio_pins[i].gp_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
777 GPIO_INTR_LEVEL_LOW | GPIO_INTR_LEVEL_HIGH |
778 GPIO_INTR_EDGE_RISING | GPIO_INTR_EDGE_FALLING |
780 snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME, "gpio_%s.%d",
781 tegra_gpio_port_names[ i / GPIO_PINS_IN_REG],
782 i % GPIO_PINS_IN_REG);
783 sc->gpio_pins[i].gp_flags =
784 gpio_read(sc, GPIO_OE, &sc->gpio_pins[i]) != 0 ?
785 GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
788 /* Init interrupt related registes. */
789 for (i = 0; i < sc->gpio_npins; i += GPIO_PINS_IN_REG) {
790 bus_write_4(sc->mem_res, GPIO_INT_ENB + GPIO_REGNUM(i), 0);
791 bus_write_4(sc->mem_res, GPIO_INT_STA + GPIO_REGNUM(i), 0xFF);
792 bus_write_4(sc->mem_res, GPIO_INT_CLR + GPIO_REGNUM(i), 0xFF);
795 /* Allocate interrupts. */
796 for (i = 0; i < GPIO_NUM_BANKS; i++) {
797 sc->irq_cookies[i].sc = sc;
798 sc->irq_cookies[i].bank_num = i;
800 sc->irq_res[i] = bus_alloc_resource_any(dev, SYS_RES_IRQ,
802 if (sc->irq_res[i] == NULL) {
803 device_printf(dev, "Cannot allocate IRQ resources\n");
804 tegra_gpio_detach(dev);
807 if ((bus_setup_intr(dev, sc->irq_res[i],
808 INTR_TYPE_MISC | INTR_MPSAFE, tegra_gpio_intr, NULL,
809 &sc->irq_cookies[i], &sc->irq_ih[i]))) {
811 "WARNING: unable to register interrupt handler\n");
812 tegra_gpio_detach(dev);
817 if (tegra_gpio_pic_attach(sc) != 0) {
818 device_printf(dev, "WARNING: unable to attach PIC\n");
819 tegra_gpio_detach(dev);
823 sc->busdev = gpiobus_attach_bus(dev);
824 if (sc->busdev == NULL) {
825 tegra_gpio_detach(dev);
829 return (bus_generic_attach(dev));
833 tegra_map_gpios(device_t dev, phandle_t pdev, phandle_t gparent,
834 int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags)
845 tegra_gpio_get_node(device_t bus, device_t dev)
848 /* We only have one child, the GPIO bus, which needs our own node. */
849 return (ofw_bus_get_node(bus));
852 static device_method_t tegra_gpio_methods[] = {
853 DEVMETHOD(device_probe, tegra_gpio_probe),
854 DEVMETHOD(device_attach, tegra_gpio_attach),
855 DEVMETHOD(device_detach, tegra_gpio_detach),
857 /* Interrupt controller interface */
858 DEVMETHOD(pic_disable_intr, tegra_gpio_pic_disable_intr),
859 DEVMETHOD(pic_enable_intr, tegra_gpio_pic_enable_intr),
860 DEVMETHOD(pic_map_intr, tegra_gpio_pic_map_intr),
861 DEVMETHOD(pic_setup_intr, tegra_gpio_pic_setup_intr),
862 DEVMETHOD(pic_teardown_intr, tegra_gpio_pic_teardown_intr),
863 DEVMETHOD(pic_post_filter, tegra_gpio_pic_post_filter),
864 DEVMETHOD(pic_post_ithread, tegra_gpio_pic_post_ithread),
865 DEVMETHOD(pic_pre_ithread, tegra_gpio_pic_pre_ithread),
868 DEVMETHOD(gpio_get_bus, tegra_gpio_get_bus),
869 DEVMETHOD(gpio_pin_max, tegra_gpio_pin_max),
870 DEVMETHOD(gpio_pin_getname, tegra_gpio_pin_getname),
871 DEVMETHOD(gpio_pin_getflags, tegra_gpio_pin_getflags),
872 DEVMETHOD(gpio_pin_getcaps, tegra_gpio_pin_getcaps),
873 DEVMETHOD(gpio_pin_setflags, tegra_gpio_pin_setflags),
874 DEVMETHOD(gpio_pin_get, tegra_gpio_pin_get),
875 DEVMETHOD(gpio_pin_set, tegra_gpio_pin_set),
876 DEVMETHOD(gpio_pin_toggle, tegra_gpio_pin_toggle),
877 DEVMETHOD(gpio_map_gpios, tegra_map_gpios),
879 /* ofw_bus interface */
880 DEVMETHOD(ofw_bus_get_node, tegra_gpio_get_node),
885 static devclass_t tegra_gpio_devclass;
886 static DEFINE_CLASS_0(gpio, tegra_gpio_driver, tegra_gpio_methods,
887 sizeof(struct tegra_gpio_softc));
888 EARLY_DRIVER_MODULE(tegra_gpio, simplebus, tegra_gpio_driver,
889 tegra_gpio_devclass, NULL, NULL, 70);