2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5 * Copyright (c) 2009, Luiz Otavio O Souza.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * GPIO driver for AR71xx
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
52 #include <mips/atheros/ar71xxreg.h>
53 #include <mips/atheros/ar71xx_setup.h>
54 #include <mips/atheros/ar71xx_cpudef.h>
55 #include <mips/atheros/ar71xx_gpiovar.h>
56 #include <dev/gpio/gpiobusvar.h>
57 #include <mips/atheros/ar933xreg.h>
58 #include <mips/atheros/ar934xreg.h>
59 #include <mips/atheros/qca953xreg.h>
60 #include <mips/atheros/qca955xreg.h>
64 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
69 static void ar71xx_gpio_function_enable(struct ar71xx_gpio_softc *sc,
71 static void ar71xx_gpio_function_disable(struct ar71xx_gpio_softc *sc,
73 static void ar71xx_gpio_pin_configure(struct ar71xx_gpio_softc *sc,
74 struct gpio_pin *pin, uint32_t flags);
79 static int ar71xx_gpio_probe(device_t dev);
80 static int ar71xx_gpio_attach(device_t dev);
81 static int ar71xx_gpio_detach(device_t dev);
82 static int ar71xx_gpio_filter(void *arg);
83 static void ar71xx_gpio_intr(void *arg);
88 static device_t ar71xx_gpio_get_bus(device_t);
89 static int ar71xx_gpio_pin_max(device_t dev, int *maxpin);
90 static int ar71xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
91 static int ar71xx_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
93 static int ar71xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
94 static int ar71xx_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
95 static int ar71xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
96 static int ar71xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
97 static int ar71xx_gpio_pin_toggle(device_t dev, uint32_t pin);
100 * Enable/disable the GPIO function control space.
102 * This is primarily for the AR71xx, which has SPI CS1/CS2, UART, SLIC, I2S
103 * as GPIO pin options.
106 ar71xx_gpio_function_enable(struct ar71xx_gpio_softc *sc, uint32_t mask)
110 * XXX TODO: refactor this out into a per-chipset method.
112 if (ar71xx_soc == AR71XX_SOC_AR9341 ||
113 ar71xx_soc == AR71XX_SOC_AR9342 ||
114 ar71xx_soc == AR71XX_SOC_AR9344 ||
115 ar71xx_soc == AR71XX_SOC_QCA9533 ||
116 ar71xx_soc == AR71XX_SOC_QCA9533_V2 ||
117 ar71xx_soc == AR71XX_SOC_QCA9556 ||
118 ar71xx_soc == AR71XX_SOC_QCA9558)
119 GPIO_SET_BITS(sc, AR934X_GPIO_REG_FUNC, mask);
121 GPIO_SET_BITS(sc, AR71XX_GPIO_FUNCTION, mask);
125 ar71xx_gpio_function_disable(struct ar71xx_gpio_softc *sc, uint32_t mask)
129 * XXX TODO: refactor this out into a per-chipset method.
131 if (ar71xx_soc == AR71XX_SOC_AR9341 ||
132 ar71xx_soc == AR71XX_SOC_AR9342 ||
133 ar71xx_soc == AR71XX_SOC_AR9344 ||
134 ar71xx_soc == AR71XX_SOC_QCA9533 ||
135 ar71xx_soc == AR71XX_SOC_QCA9533_V2 ||
136 ar71xx_soc == AR71XX_SOC_QCA9556 ||
137 ar71xx_soc == AR71XX_SOC_QCA9558)
138 GPIO_CLEAR_BITS(sc, AR934X_GPIO_REG_FUNC, mask);
140 GPIO_CLEAR_BITS(sc, AR71XX_GPIO_FUNCTION, mask);
144 * On most platforms, GPIO_OE is a bitmap where the bit set
145 * means "enable output."
147 * On AR934x and QCA953x, it's the opposite - the bit set means
151 ar71xx_gpio_oe_is_high(void)
153 switch (ar71xx_soc) {
154 case AR71XX_SOC_AR9344:
155 case AR71XX_SOC_QCA9533:
156 case AR71XX_SOC_QCA9533_V2:
164 ar71xx_gpio_oe_set_output(struct ar71xx_gpio_softc *sc, int b)
170 if (ar71xx_gpio_oe_is_high())
171 GPIO_SET_BITS(sc, AR71XX_GPIO_OE, mask);
173 GPIO_CLEAR_BITS(sc, AR71XX_GPIO_OE, mask);
177 ar71xx_gpio_oe_set_input(struct ar71xx_gpio_softc *sc, int b)
183 if (ar71xx_gpio_oe_is_high())
184 GPIO_CLEAR_BITS(sc, AR71XX_GPIO_OE, mask);
186 GPIO_SET_BITS(sc, AR71XX_GPIO_OE, mask);
190 ar71xx_gpio_pin_configure(struct ar71xx_gpio_softc *sc, struct gpio_pin *pin,
195 * Manage input/output
197 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
198 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
199 if (flags & GPIO_PIN_OUTPUT) {
200 pin->gp_flags |= GPIO_PIN_OUTPUT;
201 ar71xx_gpio_oe_set_output(sc, pin->gp_pin);
203 pin->gp_flags |= GPIO_PIN_INPUT;
204 ar71xx_gpio_oe_set_input(sc, pin->gp_pin);
210 ar71xx_gpio_get_bus(device_t dev)
212 struct ar71xx_gpio_softc *sc;
214 sc = device_get_softc(dev);
220 ar71xx_gpio_pin_max(device_t dev, int *maxpin)
223 switch (ar71xx_soc) {
224 case AR71XX_SOC_AR9130:
225 case AR71XX_SOC_AR9132:
226 *maxpin = AR91XX_GPIO_PINS - 1;
228 case AR71XX_SOC_AR7240:
229 case AR71XX_SOC_AR7241:
230 case AR71XX_SOC_AR7242:
231 *maxpin = AR724X_GPIO_PINS - 1;
233 case AR71XX_SOC_AR9330:
234 case AR71XX_SOC_AR9331:
235 *maxpin = AR933X_GPIO_COUNT - 1;
237 case AR71XX_SOC_AR9341:
238 case AR71XX_SOC_AR9342:
239 case AR71XX_SOC_AR9344:
240 *maxpin = AR934X_GPIO_COUNT - 1;
242 case AR71XX_SOC_QCA9533:
243 case AR71XX_SOC_QCA9533_V2:
244 *maxpin = QCA953X_GPIO_COUNT - 1;
246 case AR71XX_SOC_QCA9556:
247 case AR71XX_SOC_QCA9558:
248 *maxpin = QCA955X_GPIO_COUNT - 1;
251 *maxpin = AR71XX_GPIO_PINS - 1;
257 ar71xx_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
259 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
262 for (i = 0; i < sc->gpio_npins; i++) {
263 if (sc->gpio_pins[i].gp_pin == pin)
267 if (i >= sc->gpio_npins)
271 *caps = sc->gpio_pins[i].gp_caps;
278 ar71xx_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
280 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
283 for (i = 0; i < sc->gpio_npins; i++) {
284 if (sc->gpio_pins[i].gp_pin == pin)
288 if (i >= sc->gpio_npins)
292 *flags = sc->gpio_pins[i].gp_flags;
299 ar71xx_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
301 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
304 for (i = 0; i < sc->gpio_npins; i++) {
305 if (sc->gpio_pins[i].gp_pin == pin)
309 if (i >= sc->gpio_npins)
313 memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
320 ar71xx_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
323 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
325 for (i = 0; i < sc->gpio_npins; i++) {
326 if (sc->gpio_pins[i].gp_pin == pin)
330 if (i >= sc->gpio_npins)
333 ar71xx_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
339 ar71xx_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
341 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
344 for (i = 0; i < sc->gpio_npins; i++) {
345 if (sc->gpio_pins[i].gp_pin == pin)
349 if (i >= sc->gpio_npins)
353 GPIO_WRITE(sc, AR71XX_GPIO_SET, (1 << pin));
355 GPIO_WRITE(sc, AR71XX_GPIO_CLEAR, (1 << pin));
361 ar71xx_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
363 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
366 for (i = 0; i < sc->gpio_npins; i++) {
367 if (sc->gpio_pins[i].gp_pin == pin)
371 if (i >= sc->gpio_npins)
374 *val = (GPIO_READ(sc, AR71XX_GPIO_IN) & (1 << pin)) ? 1 : 0;
380 ar71xx_gpio_pin_toggle(device_t dev, uint32_t pin)
383 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
385 for (i = 0; i < sc->gpio_npins; i++) {
386 if (sc->gpio_pins[i].gp_pin == pin)
390 if (i >= sc->gpio_npins)
393 res = (GPIO_READ(sc, AR71XX_GPIO_IN) & (1 << pin)) ? 1 : 0;
395 GPIO_WRITE(sc, AR71XX_GPIO_CLEAR, (1 << pin));
397 GPIO_WRITE(sc, AR71XX_GPIO_SET, (1 << pin));
403 ar71xx_gpio_filter(void *arg)
406 /* TODO: something useful */
407 return (FILTER_STRAY);
413 ar71xx_gpio_intr(void *arg)
415 struct ar71xx_gpio_softc *sc = arg;
417 /* TODO: something useful */
422 ar71xx_gpio_probe(device_t dev)
425 device_set_desc(dev, "Atheros AR71XX GPIO driver");
430 ar71xx_gpio_attach(device_t dev)
432 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
437 KASSERT((device_get_unit(dev) == 0),
438 ("ar71xx_gpio: Only one gpio module supported"));
440 mtx_init(&sc->gpio_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
442 /* Map control/status registers. */
443 sc->gpio_mem_rid = 0;
444 sc->gpio_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
445 &sc->gpio_mem_rid, RF_ACTIVE);
447 if (sc->gpio_mem_res == NULL) {
448 device_printf(dev, "couldn't map memory\n");
449 ar71xx_gpio_detach(dev);
453 if ((sc->gpio_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
454 &sc->gpio_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
455 device_printf(dev, "unable to allocate IRQ resource\n");
456 ar71xx_gpio_detach(dev);
460 if ((bus_setup_intr(dev, sc->gpio_irq_res, INTR_TYPE_MISC,
461 ar71xx_gpio_filter, ar71xx_gpio_intr, sc, &sc->gpio_ih))) {
463 "WARNING: unable to register interrupt handler\n");
464 ar71xx_gpio_detach(dev);
470 /* Enable function bits that are required */
471 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
472 "function_set", &mask) == 0) {
473 device_printf(dev, "function_set: 0x%x\n", mask);
474 ar71xx_gpio_function_enable(sc, mask);
476 /* Disable function bits that are required */
477 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
478 "function_clear", &mask) == 0) {
479 device_printf(dev, "function_clear: 0x%x\n", mask);
480 ar71xx_gpio_function_disable(sc, mask);
483 /* Disable interrupts for all pins. */
484 GPIO_WRITE(sc, AR71XX_GPIO_INT_MASK, 0);
486 /* Initialise all pins specified in the mask, up to the pin count */
487 (void) ar71xx_gpio_pin_max(dev, &maxpin);
488 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
489 "pinmask", &mask) != 0)
491 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
492 "pinon", &pinon) != 0)
494 device_printf(dev, "gpio pinmask=0x%x\n", mask);
495 for (j = 0; j <= maxpin; j++) {
496 if ((mask & (1 << j)) == 0)
500 /* Iniatilize the GPIO pins, keep the loader settings. */
501 oe = GPIO_READ(sc, AR71XX_GPIO_OE);
503 * For AR934x and QCA953x, the meaning of oe is inverted;
504 * so flip it the right way around so we can parse the GPIO
507 if (!ar71xx_gpio_oe_is_high())
510 sc->gpio_pins = malloc(sizeof(*sc->gpio_pins) * sc->gpio_npins,
511 M_DEVBUF, M_WAITOK | M_ZERO);
512 for (i = 0, j = 0; j <= maxpin; j++) {
513 if ((mask & (1 << j)) == 0)
515 snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME,
517 sc->gpio_pins[i].gp_pin = j;
518 sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
520 sc->gpio_pins[i].gp_flags = GPIO_PIN_OUTPUT;
522 sc->gpio_pins[i].gp_flags = GPIO_PIN_INPUT;
526 /* Turn on the hinted pins. */
527 for (i = 0; i < sc->gpio_npins; i++) {
528 j = sc->gpio_pins[i].gp_pin;
529 if ((pinon & (1 << j)) != 0) {
530 ar71xx_gpio_pin_setflags(dev, j, GPIO_PIN_OUTPUT);
531 ar71xx_gpio_pin_set(dev, j, 1);
536 * Search through the function hints, in case there's some
537 * overrides such as LNA control.
539 * hint.gpio.X.func.<pin>.gpiofunc=<func value>
540 * hint.gpio.X.func.<pin>.gpiomode=1 (for output, default low)
542 for (i = 0; i <= maxpin; i++) {
544 int gpiofunc, gpiomode;
546 snprintf(buf, 32, "func.%d.gpiofunc", i);
547 if (resource_int_value(device_get_name(dev),
548 device_get_unit(dev),
552 /* Get the mode too */
553 snprintf(buf, 32, "func.%d.gpiomode", i);
554 if (resource_int_value(device_get_name(dev),
555 device_get_unit(dev),
560 /* We only handle mode=1 for now */
564 device_printf(dev, "%s: GPIO %d: func=%d, mode=%d\n",
570 /* Set pin value = 0, so it stays low by default */
571 oe = GPIO_READ(sc, AR71XX_GPIO_OUT);
573 GPIO_WRITE(sc, AR71XX_GPIO_OUT, oe);
576 ar71xx_gpio_oe_set_output(sc, i);
578 /* Finally: Set the output config */
579 ar71xx_gpio_ouput_configure(i, gpiofunc);
582 sc->busdev = gpiobus_attach_bus(dev);
583 if (sc->busdev == NULL) {
584 ar71xx_gpio_detach(dev);
592 ar71xx_gpio_detach(device_t dev)
594 struct ar71xx_gpio_softc *sc = device_get_softc(dev);
596 KASSERT(mtx_initialized(&sc->gpio_mtx), ("gpio mutex not initialized"));
598 gpiobus_detach_bus(dev);
600 bus_teardown_intr(dev, sc->gpio_irq_res, sc->gpio_ih);
601 if (sc->gpio_irq_res)
602 bus_release_resource(dev, SYS_RES_IRQ, sc->gpio_irq_rid,
604 if (sc->gpio_mem_res)
605 bus_release_resource(dev, SYS_RES_MEMORY, sc->gpio_mem_rid,
608 free(sc->gpio_pins, M_DEVBUF);
609 mtx_destroy(&sc->gpio_mtx);
614 static device_method_t ar71xx_gpio_methods[] = {
615 DEVMETHOD(device_probe, ar71xx_gpio_probe),
616 DEVMETHOD(device_attach, ar71xx_gpio_attach),
617 DEVMETHOD(device_detach, ar71xx_gpio_detach),
620 DEVMETHOD(gpio_get_bus, ar71xx_gpio_get_bus),
621 DEVMETHOD(gpio_pin_max, ar71xx_gpio_pin_max),
622 DEVMETHOD(gpio_pin_getname, ar71xx_gpio_pin_getname),
623 DEVMETHOD(gpio_pin_getflags, ar71xx_gpio_pin_getflags),
624 DEVMETHOD(gpio_pin_getcaps, ar71xx_gpio_pin_getcaps),
625 DEVMETHOD(gpio_pin_setflags, ar71xx_gpio_pin_setflags),
626 DEVMETHOD(gpio_pin_get, ar71xx_gpio_pin_get),
627 DEVMETHOD(gpio_pin_set, ar71xx_gpio_pin_set),
628 DEVMETHOD(gpio_pin_toggle, ar71xx_gpio_pin_toggle),
632 static driver_t ar71xx_gpio_driver = {
635 sizeof(struct ar71xx_gpio_softc),
637 static devclass_t ar71xx_gpio_devclass;
639 DRIVER_MODULE(ar71xx_gpio, apb, ar71xx_gpio_driver, ar71xx_gpio_devclass, 0, 0);