2 * Copyright 2016 Stanislav Galabov
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$");
30 #include "opt_platform.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <sys/resource.h>
44 #include <machine/bus.h>
45 #include <machine/intr.h>
47 #include <mips/mediatek/mtk_soc.h>
49 #include <dev/gpio/gpiobusvar.h>
51 #include <dev/fdt/fdt_common.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <gnu/dts/include/dt-bindings/interrupt-controller/irq.h>
60 #define MTK_GPIO_PINS 32
76 struct mtk_gpio_pin_irqsrc {
77 struct intr_irqsrc isrc;
84 enum intr_trigger intr_trigger;
85 enum intr_polarity intr_polarity;
86 char pin_name[GPIOMAXNAME];
87 struct mtk_gpio_pin_irqsrc pin_irqsrc;
90 struct mtk_gpio_softc {
93 struct resource *res[2];
95 struct mtk_gpio_pin pins[MTK_GPIO_PINS];
98 uint8_t regs[GPIO_PIOMAX];
103 #define PIC_INTR_ISRC(sc, irq) (&(sc)->pins[(irq)].pin_irqsrc.isrc)
105 static struct resource_spec mtk_gpio_spec[] = {
106 { SYS_RES_MEMORY, 0, RF_ACTIVE },
107 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
111 static int mtk_gpio_probe(device_t dev);
112 static int mtk_gpio_attach(device_t dev);
113 static int mtk_gpio_detach(device_t dev);
114 static int mtk_gpio_intr(void *arg);
116 #define MTK_GPIO_LOCK(sc) mtx_lock_spin(&(sc)->mtx)
117 #define MTK_GPIO_UNLOCK(sc) mtx_unlock_spin(&(sc)->mtx)
118 #define MTK_GPIO_LOCK_INIT(sc) \
119 mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev), \
120 "mtk_gpio", MTX_SPIN)
121 #define MTK_GPIO_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx)
123 #define MTK_WRITE_4(sc, reg, val) \
124 bus_write_4((sc)->res[0], (sc)->regs[(reg)], (val))
125 #define MTK_READ_4(sc, reg) \
126 bus_read_4((sc)->res[0], (sc)->regs[(reg)])
128 static struct ofw_compat_data compat_data[] = {
129 { "ralink,rt2880-gpio", 1 },
130 { "ralink,rt3050-gpio", 1 },
131 { "ralink,rt3352-gpio", 1 },
132 { "ralink,rt3883-gpio", 1 },
133 { "ralink,rt5350-gpio", 1 },
134 { "ralink,mt7620a-gpio", 1 },
139 mtk_gpio_probe(device_t dev)
143 if (!ofw_bus_status_okay(dev))
146 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
149 node = ofw_bus_get_node(dev);
150 if (!OF_hasprop(node, "gpio-controller"))
153 device_set_desc(dev, "MTK GPIO Controller (v1)");
155 return (BUS_PROBE_DEFAULT);
159 mtk_pic_register_isrcs(struct mtk_gpio_softc *sc)
163 struct intr_irqsrc *isrc;
166 name = device_get_nameunit(sc->dev);
167 for (irq = 0; irq < sc->num_pins; irq++) {
168 sc->pins[irq].pin_irqsrc.irq = irq;
169 isrc = PIC_INTR_ISRC(sc, irq);
170 error = intr_isrc_register(isrc, sc->dev, 0, "%s", name);
172 /* XXX call intr_isrc_deregister */
173 device_printf(sc->dev, "%s failed", __func__);
182 mtk_gpio_pin_set_direction(struct mtk_gpio_softc *sc, uint32_t pin,
185 uint32_t regval, mask = (1u << pin);
187 if (!(sc->pins[pin].pin_caps & dir))
190 regval = MTK_READ_4(sc, GPIO_PIODIR);
191 if (dir == GPIO_PIN_INPUT)
195 MTK_WRITE_4(sc, GPIO_PIODIR, regval);
197 sc->pins[pin].pin_flags &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
198 sc->pins[pin].pin_flags |= dir;
204 mtk_gpio_pin_set_invert(struct mtk_gpio_softc *sc, uint32_t pin, uint32_t val)
206 uint32_t regval, mask = (1u << pin);
208 regval = MTK_READ_4(sc, GPIO_PIOPOL);
213 MTK_WRITE_4(sc, GPIO_PIOPOL, regval);
214 sc->pins[pin].pin_flags &= ~(GPIO_PIN_INVIN | GPIO_PIN_INVOUT);
215 sc->pins[pin].pin_flags |= val;
221 mtk_gpio_pin_probe(struct mtk_gpio_softc *sc, uint32_t pin)
223 uint32_t mask = (1u << pin);
226 /* Clear cached gpio config */
227 sc->pins[pin].pin_flags = 0;
229 val = MTK_READ_4(sc, GPIO_PIORENA) |
230 MTK_READ_4(sc, GPIO_PIOFENA);
232 /* Pin is in interrupt mode */
233 sc->pins[pin].intr_trigger = INTR_TRIGGER_EDGE;
234 val = MTK_READ_4(sc, GPIO_PIORENA);
236 sc->pins[pin].intr_polarity = INTR_POLARITY_HIGH;
238 sc->pins[pin].intr_polarity = INTR_POLARITY_LOW;
241 val = MTK_READ_4(sc, GPIO_PIODIR);
243 sc->pins[pin].pin_flags |= GPIO_PIN_OUTPUT;
245 sc->pins[pin].pin_flags |= GPIO_PIN_INPUT;
247 val = MTK_READ_4(sc, GPIO_PIOPOL);
249 if (sc->pins[pin].pin_flags & GPIO_PIN_INPUT) {
250 sc->pins[pin].pin_flags |= GPIO_PIN_INVIN;
252 sc->pins[pin].pin_flags |= GPIO_PIN_INVOUT;
258 mtk_gpio_attach(device_t dev)
260 struct mtk_gpio_softc *sc;
262 uint32_t i, num_pins;
264 sc = device_get_softc(dev);
267 if (bus_alloc_resources(dev, mtk_gpio_spec, sc->res)) {
268 device_printf(dev, "could not allocate resources for device\n");
272 MTK_GPIO_LOCK_INIT(sc);
274 node = ofw_bus_get_node(dev);
276 if (OF_hasprop(node, "clocks"))
277 mtk_soc_start_clock(dev);
278 if (OF_hasprop(node, "resets"))
279 mtk_soc_reset_device(dev);
281 if (OF_getprop(node, "ralink,register-map", sc->regs,
283 device_printf(dev, "Failed to read register map\n");
287 if (OF_hasprop(node, "ralink,num-gpios") && (OF_getencprop(node,
288 "ralink,num-gpios", &num_pins, sizeof(num_pins)) >= 0))
289 sc->num_pins = num_pins;
291 sc->num_pins = MTK_GPIO_PINS;
293 for (i = 0; i < num_pins; i++) {
294 sc->pins[i].pin_caps |= GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
295 GPIO_PIN_INVIN | GPIO_PIN_INVOUT;
296 sc->pins[i].intr_polarity = INTR_POLARITY_HIGH;
297 sc->pins[i].intr_trigger = INTR_TRIGGER_EDGE;
299 snprintf(sc->pins[i].pin_name, GPIOMAXNAME - 1, "gpio%c%d",
300 device_get_unit(dev) + 'a', i);
301 sc->pins[i].pin_name[GPIOMAXNAME - 1] = '\0';
303 mtk_gpio_pin_probe(sc, i);
306 if (mtk_pic_register_isrcs(sc) != 0) {
307 device_printf(dev, "could not register PIC ISRCs\n");
311 if (intr_pic_register(dev, OF_xref_from_node(node)) != 0) {
312 device_printf(dev, "could not register PIC\n");
316 if (bus_setup_intr(dev, sc->res[1], INTR_TYPE_MISC | INTR_MPSAFE,
317 mtk_gpio_intr, NULL, sc, &sc->intrhand) != 0)
320 sc->busdev = gpiobus_attach_bus(dev);
321 if (sc->busdev == NULL)
326 intr_pic_deregister(dev, OF_xref_from_node(node));
328 if(sc->intrhand != NULL)
329 bus_teardown_intr(dev, sc->res[1], sc->intrhand);
330 bus_release_resources(dev, mtk_gpio_spec, sc->res);
331 MTK_GPIO_LOCK_DESTROY(sc);
336 mtk_gpio_detach(device_t dev)
338 struct mtk_gpio_softc *sc = device_get_softc(dev);
341 node = ofw_bus_get_node(dev);
342 intr_pic_deregister(dev, OF_xref_from_node(node));
343 if (sc->intrhand != NULL)
344 bus_teardown_intr(dev, sc->res[1], sc->intrhand);
345 bus_release_resources(dev, mtk_gpio_spec, sc->res);
346 MTK_GPIO_LOCK_DESTROY(sc);
351 mtk_gpio_get_bus(device_t dev)
353 struct mtk_gpio_softc *sc = device_get_softc(dev);
359 mtk_gpio_pin_max(device_t dev, int *maxpin)
361 struct mtk_gpio_softc *sc = device_get_softc(dev);
363 *maxpin = sc->num_pins - 1;
369 mtk_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
371 struct mtk_gpio_softc *sc = device_get_softc(dev);
373 if (pin >= sc->num_pins)
377 *caps = sc->pins[pin].pin_caps;
384 mtk_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
386 struct mtk_gpio_softc *sc = device_get_softc(dev);
388 if (pin >= sc->num_pins)
392 *flags = sc->pins[pin].pin_flags;
399 mtk_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
401 struct mtk_gpio_softc *sc = device_get_softc(dev);
403 if (pin >= sc->num_pins)
406 strncpy(name, sc->pins[pin].pin_name, GPIOMAXNAME - 1);
407 name[GPIOMAXNAME - 1] = '\0';
413 mtk_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
415 struct mtk_gpio_softc *sc;
418 sc = device_get_softc(dev);
420 if (pin >= sc->num_pins)
424 retval = mtk_gpio_pin_set_direction(sc, pin,
425 flags & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT));
427 retval = mtk_gpio_pin_set_invert(sc, pin,
428 flags & (GPIO_PIN_INVIN | GPIO_PIN_INVOUT));
435 mtk_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
437 struct mtk_gpio_softc *sc;
440 sc = device_get_softc(dev);
443 if (pin >= sc->num_pins)
447 if(!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) {
453 MTK_WRITE_4(sc, GPIO_PIOSET, (1u << pin));
455 MTK_WRITE_4(sc, GPIO_PIORESET, (1u << pin));
463 mtk_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
465 struct mtk_gpio_softc *sc;
469 sc = device_get_softc(dev);
472 if (pin >= sc->num_pins)
476 if(!(sc->pins[pin].pin_flags & GPIO_PIN_INPUT)) {
480 data = MTK_READ_4(sc, GPIO_PIODATA);
481 *val = (data & (1u << pin)) ? 1 : 0;
489 mtk_gpio_pin_toggle(device_t dev, uint32_t pin)
491 struct mtk_gpio_softc *sc;
494 if (pin >= sc->num_pins)
497 sc = device_get_softc(dev);
501 if (!(sc->pins[pin].pin_flags & GPIO_PIN_OUTPUT)) {
505 MTK_WRITE_4(sc, GPIO_PIOTOG, (1u << pin));
514 mtk_gpio_pic_map_intr(device_t dev, struct intr_map_data *data,
515 struct intr_irqsrc **isrcp)
517 struct mtk_gpio_softc *sc;
519 sc = device_get_softc(dev);
521 if (data == NULL || data->type != INTR_MAP_DATA_FDT ||
522 data->fdt.ncells != 1 || data->fdt.cells[0] >= sc->num_pins)
525 *isrcp = PIC_INTR_ISRC(sc, data->fdt.cells[0]);
530 mtk_gpio_pic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
532 struct mtk_gpio_softc *sc;
533 struct mtk_gpio_pin_irqsrc *pisrc;
534 uint32_t pin, mask, val;
536 sc = device_get_softc(dev);
538 pisrc = (struct mtk_gpio_pin_irqsrc *)isrc;
544 if (sc->pins[pin].intr_polarity == INTR_POLARITY_LOW) {
545 val = MTK_READ_4(sc, GPIO_PIORENA) & ~mask;
546 MTK_WRITE_4(sc, GPIO_PIORENA, val);
547 val = MTK_READ_4(sc, GPIO_PIOFENA) | mask;
548 MTK_WRITE_4(sc, GPIO_PIOFENA, val);
550 val = MTK_READ_4(sc, GPIO_PIOFENA) & ~mask;
551 MTK_WRITE_4(sc, GPIO_PIOFENA, val);
552 val = MTK_READ_4(sc, GPIO_PIORENA) | mask;
553 MTK_WRITE_4(sc, GPIO_PIORENA, val);
560 mtk_gpio_pic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
562 struct mtk_gpio_softc *sc;
563 struct mtk_gpio_pin_irqsrc *pisrc;
564 uint32_t pin, mask, val;
566 sc = device_get_softc(dev);
568 pisrc = (struct mtk_gpio_pin_irqsrc *)isrc;
574 val = MTK_READ_4(sc, GPIO_PIORENA) & ~mask;
575 MTK_WRITE_4(sc, GPIO_PIORENA, val);
576 val = MTK_READ_4(sc, GPIO_PIOFENA) & ~mask;
577 MTK_WRITE_4(sc, GPIO_PIOFENA, val);
583 mtk_gpio_pic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
586 mtk_gpio_pic_disable_intr(dev, isrc);
590 mtk_gpio_pic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
593 mtk_gpio_pic_enable_intr(dev, isrc);
597 mtk_gpio_pic_post_filter(device_t dev, struct intr_irqsrc *isrc)
599 struct mtk_gpio_softc *sc;
600 struct mtk_gpio_pin_irqsrc *pisrc;
602 pisrc = (struct mtk_gpio_pin_irqsrc *)isrc;
603 sc = device_get_softc(dev);
605 MTK_WRITE_4(sc, GPIO_PIOINT, 1u << pisrc->irq);
610 mtk_gpio_intr(void *arg)
612 struct mtk_gpio_softc *sc;
613 uint32_t i, interrupts;
616 interrupts = MTK_READ_4(sc, GPIO_PIOINT);
618 for (i = 0; interrupts != 0; i++, interrupts >>= 1) {
619 if ((interrupts & 0x1) == 0)
621 if (intr_isrc_dispatch(PIC_INTR_ISRC(sc, i),
622 curthread->td_intr_frame) != 0) {
623 device_printf(sc->dev, "spurious interrupt %d\n", i);
627 return (FILTER_HANDLED);
631 mtk_gpio_get_node(device_t bus, device_t dev)
634 /* We only have one child, the GPIO bus, which needs our own node. */
635 return (ofw_bus_get_node(bus));
638 static device_method_t mtk_gpio_methods[] = {
639 /* Device interface */
640 DEVMETHOD(device_probe, mtk_gpio_probe),
641 DEVMETHOD(device_attach, mtk_gpio_attach),
642 DEVMETHOD(device_detach, mtk_gpio_detach),
645 DEVMETHOD(gpio_get_bus, mtk_gpio_get_bus),
646 DEVMETHOD(gpio_pin_max, mtk_gpio_pin_max),
647 DEVMETHOD(gpio_pin_getname, mtk_gpio_pin_getname),
648 DEVMETHOD(gpio_pin_getflags, mtk_gpio_pin_getflags),
649 DEVMETHOD(gpio_pin_getcaps, mtk_gpio_pin_getcaps),
650 DEVMETHOD(gpio_pin_setflags, mtk_gpio_pin_setflags),
651 DEVMETHOD(gpio_pin_get, mtk_gpio_pin_get),
652 DEVMETHOD(gpio_pin_set, mtk_gpio_pin_set),
653 DEVMETHOD(gpio_pin_toggle, mtk_gpio_pin_toggle),
655 /* Interrupt controller interface */
656 DEVMETHOD(pic_disable_intr, mtk_gpio_pic_disable_intr),
657 DEVMETHOD(pic_enable_intr, mtk_gpio_pic_enable_intr),
658 DEVMETHOD(pic_map_intr, mtk_gpio_pic_map_intr),
659 DEVMETHOD(pic_post_filter, mtk_gpio_pic_post_filter),
660 DEVMETHOD(pic_post_ithread, mtk_gpio_pic_post_ithread),
661 DEVMETHOD(pic_pre_ithread, mtk_gpio_pic_pre_ithread),
663 /* ofw_bus interface */
664 DEVMETHOD(ofw_bus_get_node, mtk_gpio_get_node),
669 static driver_t mtk_gpio_driver = {
672 sizeof(struct mtk_gpio_softc),
675 static devclass_t mtk_gpio_devclass;
677 EARLY_DRIVER_MODULE(mtk_gpio_v1, simplebus, mtk_gpio_driver,
678 mtk_gpio_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);