2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Benno Rice.
5 * Copyright (C) 2008 MARVELL INTERNATIONAL LTD.
6 * Copyright (c) 2017 Semihalf.
9 * Adapted and extended for Marvell SoCs by Semihalf.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * from: FreeBSD: //depot/projects/arm/src/sys/arm/xscale/pxa2x0/pxa2x0_gpio.c, rev 1
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/interrupt.h>
43 #include <sys/module.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
47 #include <sys/queue.h>
48 #include <sys/timetc.h>
49 #include <sys/callout.h>
51 #include <machine/bus.h>
52 #include <machine/intr.h>
54 #include <dev/gpio/gpiobusvar.h>
55 #include <dev/ofw/ofw_bus.h>
56 #include <dev/ofw/ofw_bus_subr.h>
58 #include <arm/mv/mvvar.h>
59 #include <arm/mv/mvreg.h>
63 #define GPIO_MAX_INTR_COUNT 8
64 #define GPIO_PINS_PER_REG 32
65 #define GPIO_GENERIC_CAP (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
66 GPIO_PIN_OPENDRAIN | GPIO_PIN_PUSHPULL | \
67 GPIO_PIN_TRISTATE | GPIO_PIN_PULLUP | \
68 GPIO_PIN_PULLDOWN | GPIO_PIN_INVIN | \
71 #define DEBOUNCE_CHECK_MS 1
72 #define DEBOUNCE_LO_HI_MS 2
73 #define DEBOUNCE_HI_LO_MS 2
74 #define DEBOUNCE_CHECK_TICKS ((hz / 1000) * DEBOUNCE_CHECK_MS)
76 struct mv_gpio_softc {
78 struct resource * mem_res;
80 struct resource * irq_res[GPIO_MAX_INTR_COUNT];
81 int irq_rid[GPIO_MAX_INTR_COUNT];
82 struct intr_event * gpio_events[MV_GPIO_MAX_NPINS];
83 void *ih_cookie[GPIO_MAX_INTR_COUNT];
85 bus_space_handle_t bsh;
87 uint8_t pin_num; /* number of GPIO pins */
88 uint8_t irq_num; /* number of real IRQs occupied by GPIO controller */
89 struct gpio_pin gpio_setup[MV_GPIO_MAX_NPINS];
91 /* Used for debouncing. */
92 uint32_t debounced_state_lo;
93 uint32_t debounced_state_hi;
94 struct callout **debounce_callouts;
95 int *debounce_counters;
98 struct mv_gpio_pindev {
103 static int mv_gpio_probe(device_t);
104 static int mv_gpio_attach(device_t);
105 static int mv_gpio_intr(device_t, void *);
107 static void mv_gpio_double_edge_init(device_t, int);
109 static int mv_gpio_debounce_setup(device_t, int);
110 static int mv_gpio_debounce_prepare(device_t, int);
111 static int mv_gpio_debounce_init(device_t, int);
112 static void mv_gpio_debounce_start(device_t, int);
113 static void mv_gpio_debounce(void *);
114 static void mv_gpio_debounced_state_set(device_t, int, uint8_t);
115 static uint32_t mv_gpio_debounced_state_get(device_t, int);
117 static void mv_gpio_exec_intr_handlers(device_t, uint32_t, int);
118 static void mv_gpio_intr_handler(device_t, int);
119 static uint32_t mv_gpio_reg_read(device_t, uint32_t);
120 static void mv_gpio_reg_write(device_t, uint32_t, uint32_t);
121 static void mv_gpio_reg_set(device_t, uint32_t, uint32_t);
122 static void mv_gpio_reg_clear(device_t, uint32_t, uint32_t);
124 static void mv_gpio_blink(device_t, uint32_t, uint8_t);
125 static void mv_gpio_polarity(device_t, uint32_t, uint8_t, uint8_t);
126 static void mv_gpio_level(device_t, uint32_t, uint8_t);
127 static void mv_gpio_edge(device_t, uint32_t, uint8_t);
128 static void mv_gpio_out_en(device_t, uint32_t, uint8_t);
129 static void mv_gpio_int_ack(struct mv_gpio_pindev *);
130 static void mv_gpio_value_set(device_t, uint32_t, uint8_t);
131 static uint32_t mv_gpio_value_get(device_t, uint32_t, uint8_t);
133 static void mv_gpio_intr_mask(struct mv_gpio_pindev *);
134 static void mv_gpio_intr_unmask(struct mv_gpio_pindev *);
136 void mv_gpio_finish_intrhandler(struct mv_gpio_pindev *);
137 int mv_gpio_setup_intrhandler(device_t, const char *,
138 driver_filter_t *, void (*)(void *), void *,
140 int mv_gpio_configure(device_t, uint32_t, uint32_t, uint32_t);
141 void mv_gpio_out(device_t, uint32_t, uint8_t, uint8_t);
142 uint8_t mv_gpio_in(device_t, uint32_t);
147 static device_t mv_gpio_get_bus(device_t);
148 static int mv_gpio_pin_max(device_t, int *);
149 static int mv_gpio_pin_getcaps(device_t, uint32_t, uint32_t *);
150 static int mv_gpio_pin_getflags(device_t, uint32_t, uint32_t *);
151 static int mv_gpio_pin_getname(device_t, uint32_t, char *);
152 static int mv_gpio_pin_setflags(device_t, uint32_t, uint32_t);
153 static int mv_gpio_pin_set(device_t, uint32_t, unsigned int);
154 static int mv_gpio_pin_get(device_t, uint32_t, unsigned int *);
155 static int mv_gpio_pin_toggle(device_t, uint32_t);
156 static int mv_gpio_map_gpios(device_t, phandle_t, phandle_t,
157 int, pcell_t *, uint32_t *, uint32_t *);
159 #define MV_GPIO_LOCK() mtx_lock_spin(&sc->mutex)
160 #define MV_GPIO_UNLOCK() mtx_unlock_spin(&sc->mutex)
161 #define MV_GPIO_ASSERT_LOCKED() mtx_assert(&sc->mutex, MA_OWNED)
163 static device_method_t mv_gpio_methods[] = {
164 DEVMETHOD(device_probe, mv_gpio_probe),
165 DEVMETHOD(device_attach, mv_gpio_attach),
168 DEVMETHOD(gpio_get_bus, mv_gpio_get_bus),
169 DEVMETHOD(gpio_pin_max, mv_gpio_pin_max),
170 DEVMETHOD(gpio_pin_getname, mv_gpio_pin_getname),
171 DEVMETHOD(gpio_pin_getflags, mv_gpio_pin_getflags),
172 DEVMETHOD(gpio_pin_getcaps, mv_gpio_pin_getcaps),
173 DEVMETHOD(gpio_pin_setflags, mv_gpio_pin_setflags),
174 DEVMETHOD(gpio_pin_get, mv_gpio_pin_get),
175 DEVMETHOD(gpio_pin_set, mv_gpio_pin_set),
176 DEVMETHOD(gpio_pin_toggle, mv_gpio_pin_toggle),
177 DEVMETHOD(gpio_map_gpios, mv_gpio_map_gpios),
182 static driver_t mv_gpio_driver = {
185 sizeof(struct mv_gpio_softc),
188 static devclass_t mv_gpio_devclass;
190 DRIVER_MODULE(mv_gpio, simplebus, mv_gpio_driver, mv_gpio_devclass, 0, 0);
192 struct ofw_compat_data gpio_controllers[] = {
193 { "mrvl,gpio", (uintptr_t)true },
194 { "marvell,orion-gpio", (uintptr_t)true },
199 mv_gpio_probe(device_t dev)
201 if (!ofw_bus_status_okay(dev))
204 if (ofw_bus_search_compatible(dev, gpio_controllers)->ocd_data == 0)
207 device_set_desc(dev, "Marvell Integrated GPIO Controller");
212 mv_gpio_attach(device_t dev)
215 struct mv_gpio_softc *sc;
217 pcell_t irq_cells = 0;
220 sc = (struct mv_gpio_softc *)device_get_softc(dev);
224 if (OF_getencprop(ofw_bus_get_node(dev), "pin-count", &pincnt,
225 sizeof(pcell_t)) >= 0 ||
226 OF_getencprop(ofw_bus_get_node(dev), "ngpios", &pincnt,
227 sizeof(pcell_t)) >= 0) {
228 sc->pin_num = MIN(pincnt, MV_GPIO_MAX_NPINS);
230 device_printf(dev, "%d pins available\n", sc->pin_num);
232 device_printf(dev, "ERROR: no pin-count or ngpios entry found!\n");
236 /* Assign generic capabilities to every gpio pin */
237 for(i = 0; i < sc->pin_num; i++)
238 sc->gpio_setup[i].gp_caps = GPIO_GENERIC_CAP;
240 /* Find root interrupt controller */
241 iparent = ofw_bus_find_iparent(ofw_bus_get_node(dev));
243 device_printf(dev, "No interrupt-parrent found. "
247 /* While at parent - store interrupt cells prop */
248 if (OF_searchencprop(OF_node_from_xref(iparent),
249 "#interrupt-cells", &irq_cells, sizeof(irq_cells)) == -1) {
250 device_printf(dev, "DTB: Missing #interrupt-cells "
251 "property in interrupt parent node\n");
256 size = OF_getproplen(ofw_bus_get_node(dev), "interrupts");
258 size = size / sizeof(pcell_t);
259 size = size / irq_cells;
261 device_printf(dev, "%d IRQs available\n", sc->irq_num);
263 device_printf(dev, "ERROR: no interrupts entry found!\n");
267 sc->debounce_callouts = (struct callout **)malloc(sc->pin_num *
268 sizeof(struct callout *), M_DEVBUF, M_WAITOK | M_ZERO);
269 if (sc->debounce_callouts == NULL)
272 sc->debounce_counters = (int *)malloc(sc->pin_num * sizeof(int),
274 if (sc->debounce_counters == NULL)
277 mtx_init(&sc->mutex, device_get_nameunit(dev), NULL, MTX_SPIN);
280 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
284 mtx_destroy(&sc->mutex);
285 device_printf(dev, "could not allocate memory window\n");
289 sc->bst = rman_get_bustag(sc->mem_res);
290 sc->bsh = rman_get_bushandle(sc->mem_res);
292 for (i = 0; i < sc->irq_num; i++) {
294 sc->irq_res[i] = bus_alloc_resource_any(dev, SYS_RES_IRQ,
295 &sc->irq_rid[i], RF_ACTIVE);
296 if (!sc->irq_res[i]) {
297 mtx_destroy(&sc->mutex);
299 "could not allocate gpio%d interrupt\n", i+1);
304 /* Disable all interrupts */
305 bus_space_write_4(sc->bst, sc->bsh, GPIO_INT_EDGE_MASK, 0);
306 bus_space_write_4(sc->bst, sc->bsh, GPIO_INT_LEV_MASK, 0);
308 for (i = 0; i < sc->irq_num; i++) {
309 if (bus_setup_intr(dev, sc->irq_res[i],
311 (driver_filter_t *)mv_gpio_intr, NULL,
312 sc, &sc->ih_cookie[i]) != 0) {
313 mtx_destroy(&sc->mutex);
314 bus_release_resource(dev, SYS_RES_IRQ,
315 sc->irq_rid[i], sc->irq_res[i]);
316 device_printf(dev, "could not set up intr %d\n", i);
321 /* Clear interrupt status. */
322 bus_space_write_4(sc->bst, sc->bsh, GPIO_INT_CAUSE, 0);
324 sc->sc_busdev = gpiobus_attach_bus(dev);
325 if (sc->sc_busdev == NULL) {
326 mtx_destroy(&sc->mutex);
327 bus_release_resource(dev, SYS_RES_IRQ,
328 sc->irq_rid[i], sc->irq_res[i]);
336 mv_gpio_intr(device_t dev, void *arg)
338 uint32_t int_cause, gpio_val;
339 struct mv_gpio_softc *sc;
340 sc = (struct mv_gpio_softc *)device_get_softc(dev);
345 * According to documentation, edge sensitive interrupts are asserted
346 * when unmasked GPIO_INT_CAUSE register bits are set.
348 int_cause = mv_gpio_reg_read(dev, GPIO_INT_CAUSE);
349 int_cause &= mv_gpio_reg_read(dev, GPIO_INT_EDGE_MASK);
352 * Level sensitive interrupts are asserted when unmasked GPIO_DATA_IN
353 * register bits are set.
355 gpio_val = mv_gpio_reg_read(dev, GPIO_DATA_IN);
356 gpio_val &= mv_gpio_reg_read(dev, GPIO_INT_LEV_MASK);
358 mv_gpio_exec_intr_handlers(dev, int_cause | gpio_val, 0);
362 return (FILTER_HANDLED);
366 * GPIO interrupt handling
370 mv_gpio_finish_intrhandler(struct mv_gpio_pindev *s)
372 /* When we acheive full interrupt support
373 * This function will be opposite to
374 * mv_gpio_setup_intrhandler
377 /* Now it exists only to remind that
378 * there should be place to free mv_gpio_pindev
379 * allocated by mv_gpio_setup_intrhandler
385 mv_gpio_setup_intrhandler(device_t dev, const char *name, driver_filter_t *filt,
386 void (*hand)(void *), void *arg, int pin, int flags, void **cookiep)
388 struct intr_event *event;
390 struct mv_gpio_pindev *s;
391 struct mv_gpio_softc *sc;
392 sc = (struct mv_gpio_softc *)device_get_softc(dev);
393 s = malloc(sizeof(struct mv_gpio_pindev), M_DEVBUF, M_NOWAIT | M_ZERO);
395 if (pin < 0 || pin >= sc->pin_num)
397 event = sc->gpio_events[pin];
400 if (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_DEBOUNCE) {
401 error = mv_gpio_debounce_init(dev, pin);
406 } else if (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_IRQ_DOUBLE_EDGE)
407 mv_gpio_double_edge_init(dev, pin);
409 error = intr_event_create(&event, (void *)s, 0, pin,
410 (void (*)(void *))mv_gpio_intr_mask,
411 (void (*)(void *))mv_gpio_intr_unmask,
412 (void (*)(void *))mv_gpio_int_ack,
417 sc->gpio_events[pin] = event;
420 intr_event_add_handler(event, name, filt, hand, arg,
421 intr_priority(flags), flags, cookiep);
426 mv_gpio_intr_mask(struct mv_gpio_pindev *s)
428 struct mv_gpio_softc *sc;
429 sc = (struct mv_gpio_softc *)device_get_softc(s->dev);
431 if (s->pin >= sc->pin_num)
436 if (sc->gpio_setup[s->pin].gp_flags & (MV_GPIO_IN_IRQ_EDGE |
437 MV_GPIO_IN_IRQ_DOUBLE_EDGE))
438 mv_gpio_edge(s->dev, s->pin, 0);
440 mv_gpio_level(s->dev, s->pin, 0);
443 * The interrupt has to be acknowledged before scheduling an interrupt
444 * thread. This way we allow for interrupt source to trigger again
445 * (which can happen with shared IRQs e.g. PCI) while processing the
456 mv_gpio_intr_unmask(struct mv_gpio_pindev *s)
458 struct mv_gpio_softc *sc;
459 sc = (struct mv_gpio_softc *)device_get_softc(s->dev);
461 if (s->pin >= sc->pin_num)
466 if (sc->gpio_setup[s->pin].gp_flags & (MV_GPIO_IN_IRQ_EDGE |
467 MV_GPIO_IN_IRQ_DOUBLE_EDGE))
468 mv_gpio_edge(s->dev, s->pin, 1);
470 mv_gpio_level(s->dev, s->pin, 1);
478 mv_gpio_exec_intr_handlers(device_t dev, uint32_t status, int high)
481 struct mv_gpio_softc *sc;
482 sc = (struct mv_gpio_softc *)device_get_softc(dev);
484 MV_GPIO_ASSERT_LOCKED();
487 while (status != 0) {
489 pin = (high ? (i + GPIO_PINS_PER_REG) : i);
490 if (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_DEBOUNCE)
491 mv_gpio_debounce_start(dev, pin);
492 else if (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_IRQ_DOUBLE_EDGE) {
493 mv_gpio_polarity(dev, pin, 0, 1);
494 mv_gpio_intr_handler(dev, pin);
496 mv_gpio_intr_handler(dev, pin);
504 mv_gpio_intr_handler(device_t dev, int pin)
507 struct intr_irqsrc isrc;
508 struct mv_gpio_softc *sc;
509 sc = (struct mv_gpio_softc *)device_get_softc(dev);
511 MV_GPIO_ASSERT_LOCKED();
514 isrc.isrc_filter = NULL;
516 isrc.isrc_event = sc->gpio_events[pin];
518 if (isrc.isrc_event == NULL ||
519 CK_SLIST_EMPTY(&isrc.isrc_event->ie_handlers))
522 intr_isrc_dispatch(&isrc, NULL);
527 mv_gpio_configure(device_t dev, uint32_t pin, uint32_t flags, uint32_t mask)
530 struct mv_gpio_softc *sc;
531 sc = (struct mv_gpio_softc *)device_get_softc(dev);
534 if (pin >= sc->pin_num)
537 /* check flags consistency */
538 if (((flags & mask) & (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)) ==
539 (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT))
542 if (mask & MV_GPIO_IN_DEBOUNCE) {
543 error = mv_gpio_debounce_prepare(dev, pin);
550 if ((mask & flags) & GPIO_PIN_INPUT)
551 mv_gpio_out_en(dev, pin, 0);
552 if ((mask & flags) & GPIO_PIN_OUTPUT) {
553 if ((flags & mask) & GPIO_PIN_OPENDRAIN)
554 mv_gpio_value_set(dev, pin, 0);
556 mv_gpio_value_set(dev, pin, 1);
557 mv_gpio_out_en(dev, pin, 1);
560 if (mask & MV_GPIO_OUT_BLINK)
561 mv_gpio_blink(dev, pin, flags & MV_GPIO_OUT_BLINK);
562 if (mask & MV_GPIO_IN_POL_LOW)
563 mv_gpio_polarity(dev, pin, flags & MV_GPIO_IN_POL_LOW, 0);
564 if (mask & MV_GPIO_IN_DEBOUNCE) {
565 error = mv_gpio_debounce_setup(dev, pin);
572 sc->gpio_setup[pin].gp_flags &= ~(mask);
573 sc->gpio_setup[pin].gp_flags |= (flags & mask);
581 mv_gpio_double_edge_init(device_t dev, int pin)
584 struct mv_gpio_softc *sc;
585 sc = (struct mv_gpio_softc *)device_get_softc(dev);
587 MV_GPIO_ASSERT_LOCKED();
589 raw_read = (mv_gpio_value_get(dev, pin, 1) ? 1 : 0);
592 mv_gpio_polarity(dev, pin, 1, 0);
594 mv_gpio_polarity(dev, pin, 0, 0);
598 mv_gpio_debounce_setup(device_t dev, int pin)
601 struct mv_gpio_softc *sc;
603 sc = (struct mv_gpio_softc *)device_get_softc(dev);
605 MV_GPIO_ASSERT_LOCKED();
607 c = sc->debounce_callouts[pin];
611 if (callout_active(c))
612 callout_deactivate(c);
620 mv_gpio_debounce_prepare(device_t dev, int pin)
623 struct mv_gpio_softc *sc;
625 sc = (struct mv_gpio_softc *)device_get_softc(dev);
627 c = sc->debounce_callouts[pin];
629 c = (struct callout *)malloc(sizeof(struct callout),
631 sc->debounce_callouts[pin] = c;
641 mv_gpio_debounce_init(device_t dev, int pin)
645 struct mv_gpio_softc *sc;
647 sc = (struct mv_gpio_softc *)device_get_softc(dev);
649 MV_GPIO_ASSERT_LOCKED();
651 cnt = &sc->debounce_counters[pin];
652 raw_read = (mv_gpio_value_get(dev, pin, 1) ? 1 : 0);
654 mv_gpio_polarity(dev, pin, 1, 0);
655 *cnt = DEBOUNCE_HI_LO_MS / DEBOUNCE_CHECK_MS;
657 mv_gpio_polarity(dev, pin, 0, 0);
658 *cnt = DEBOUNCE_LO_HI_MS / DEBOUNCE_CHECK_MS;
661 mv_gpio_debounced_state_set(dev, pin, raw_read);
667 mv_gpio_debounce_start(device_t dev, int pin)
670 struct mv_gpio_pindev s = {dev, pin};
671 struct mv_gpio_pindev *sd;
672 struct mv_gpio_softc *sc;
673 sc = (struct mv_gpio_softc *)device_get_softc(dev);
675 MV_GPIO_ASSERT_LOCKED();
677 c = sc->debounce_callouts[pin];
683 if (callout_pending(c) || callout_active(c)) {
688 sd = (struct mv_gpio_pindev *)malloc(sizeof(struct mv_gpio_pindev),
697 callout_reset(c, DEBOUNCE_CHECK_TICKS, mv_gpio_debounce, sd);
701 mv_gpio_debounce(void *arg)
703 uint8_t raw_read, last_state;
706 int *debounce_counter;
707 struct mv_gpio_softc *sc;
708 struct mv_gpio_pindev *s;
710 s = (struct mv_gpio_pindev *)arg;
713 sc = (struct mv_gpio_softc *)device_get_softc(dev);
717 raw_read = (mv_gpio_value_get(dev, pin, 1) ? 1 : 0);
718 last_state = (mv_gpio_debounced_state_get(dev, pin) ? 1 : 0);
719 debounce_counter = &sc->debounce_counters[pin];
721 if (raw_read == last_state) {
723 *debounce_counter = DEBOUNCE_HI_LO_MS /
726 *debounce_counter = DEBOUNCE_LO_HI_MS /
729 callout_reset(sc->debounce_callouts[pin],
730 DEBOUNCE_CHECK_TICKS, mv_gpio_debounce, arg);
732 *debounce_counter = *debounce_counter - 1;
733 if (*debounce_counter != 0)
734 callout_reset(sc->debounce_callouts[pin],
735 DEBOUNCE_CHECK_TICKS, mv_gpio_debounce, arg);
737 mv_gpio_debounced_state_set(dev, pin, raw_read);
740 *debounce_counter = DEBOUNCE_HI_LO_MS /
743 *debounce_counter = DEBOUNCE_LO_HI_MS /
746 if (((sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_POL_LOW) &&
748 (((sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_POL_LOW) == 0) &&
750 (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_IRQ_DOUBLE_EDGE))
751 mv_gpio_intr_handler(dev, pin);
753 /* Toggle polarity for next edge. */
754 mv_gpio_polarity(dev, pin, 0, 1);
757 callout_deactivate(sc->debounce_callouts[pin]);
765 mv_gpio_debounced_state_set(device_t dev, int pin, uint8_t new_state)
768 struct mv_gpio_softc *sc;
769 sc = (struct mv_gpio_softc *)device_get_softc(dev);
771 MV_GPIO_ASSERT_LOCKED();
773 if (pin >= GPIO_PINS_PER_REG) {
774 old_state = &sc->debounced_state_hi;
775 pin -= GPIO_PINS_PER_REG;
777 old_state = &sc->debounced_state_lo;
780 *old_state |= (1 << pin);
782 *old_state &= ~(1 << pin);
786 mv_gpio_debounced_state_get(device_t dev, int pin)
789 struct mv_gpio_softc *sc;
790 sc = (struct mv_gpio_softc *)device_get_softc(dev);
792 MV_GPIO_ASSERT_LOCKED();
794 if (pin >= GPIO_PINS_PER_REG) {
795 state = &sc->debounced_state_hi;
796 pin -= GPIO_PINS_PER_REG;
798 state = &sc->debounced_state_lo;
800 return (*state & (1 << pin));
804 mv_gpio_out(device_t dev, uint32_t pin, uint8_t val, uint8_t enable)
806 struct mv_gpio_softc *sc;
807 sc = (struct mv_gpio_softc *)device_get_softc(dev);
811 mv_gpio_value_set(dev, pin, val);
812 mv_gpio_out_en(dev, pin, enable);
818 mv_gpio_in(device_t dev, uint32_t pin)
821 struct mv_gpio_softc *sc;
822 sc = (struct mv_gpio_softc *)device_get_softc(dev);
824 MV_GPIO_ASSERT_LOCKED();
826 if (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_DEBOUNCE) {
827 if (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_POL_LOW)
828 state = (mv_gpio_debounced_state_get(dev, pin) ? 0 : 1);
830 state = (mv_gpio_debounced_state_get(dev, pin) ? 1 : 0);
831 } else if (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_IRQ_DOUBLE_EDGE) {
832 if (sc->gpio_setup[pin].gp_flags & MV_GPIO_IN_POL_LOW)
833 state = (mv_gpio_value_get(dev, pin, 1) ? 0 : 1);
835 state = (mv_gpio_value_get(dev, pin, 1) ? 1 : 0);
837 state = (mv_gpio_value_get(dev, pin, 0) ? 1 : 0);
843 mv_gpio_reg_read(device_t dev, uint32_t reg)
845 struct mv_gpio_softc *sc;
846 sc = (struct mv_gpio_softc *)device_get_softc(dev);
848 return (bus_space_read_4(sc->bst, sc->bsh, reg));
852 mv_gpio_reg_write(device_t dev, uint32_t reg, uint32_t val)
854 struct mv_gpio_softc *sc;
855 sc = (struct mv_gpio_softc *)device_get_softc(dev);
857 bus_space_write_4(sc->bst, sc->bsh, reg, val);
861 mv_gpio_reg_set(device_t dev, uint32_t reg, uint32_t pin)
865 reg_val = mv_gpio_reg_read(dev, reg);
866 reg_val |= GPIO(pin);
867 mv_gpio_reg_write(dev, reg, reg_val);
871 mv_gpio_reg_clear(device_t dev, uint32_t reg, uint32_t pin)
875 reg_val = mv_gpio_reg_read(dev, reg);
876 reg_val &= ~(GPIO(pin));
877 mv_gpio_reg_write(dev, reg, reg_val);
881 mv_gpio_out_en(device_t dev, uint32_t pin, uint8_t enable)
884 struct mv_gpio_softc *sc;
885 sc = (struct mv_gpio_softc *)device_get_softc(dev);
887 if (pin >= sc->pin_num)
890 reg = GPIO_DATA_OUT_EN_CTRL;
893 mv_gpio_reg_clear(dev, reg, pin);
895 mv_gpio_reg_set(dev, reg, pin);
899 mv_gpio_blink(device_t dev, uint32_t pin, uint8_t enable)
902 struct mv_gpio_softc *sc;
903 sc = (struct mv_gpio_softc *)device_get_softc(dev);
905 if (pin >= sc->pin_num)
911 mv_gpio_reg_set(dev, reg, pin);
913 mv_gpio_reg_clear(dev, reg, pin);
917 mv_gpio_polarity(device_t dev, uint32_t pin, uint8_t enable, uint8_t toggle)
919 uint32_t reg, reg_val;
920 struct mv_gpio_softc *sc;
921 sc = (struct mv_gpio_softc *)device_get_softc(dev);
923 if (pin >= sc->pin_num)
926 reg = GPIO_DATA_IN_POLAR;
929 reg_val = mv_gpio_reg_read(dev, reg) & GPIO(pin);
931 mv_gpio_reg_clear(dev, reg, pin);
933 mv_gpio_reg_set(dev, reg, pin);
935 mv_gpio_reg_set(dev, reg, pin);
937 mv_gpio_reg_clear(dev, reg, pin);
941 mv_gpio_level(device_t dev, uint32_t pin, uint8_t enable)
944 struct mv_gpio_softc *sc;
945 sc = (struct mv_gpio_softc *)device_get_softc(dev);
947 if (pin >= sc->pin_num)
950 reg = GPIO_INT_LEV_MASK;
953 mv_gpio_reg_set(dev, reg, pin);
955 mv_gpio_reg_clear(dev, reg, pin);
959 mv_gpio_edge(device_t dev, uint32_t pin, uint8_t enable)
962 struct mv_gpio_softc *sc;
963 sc = (struct mv_gpio_softc *)device_get_softc(dev);
965 if (pin >= sc->pin_num)
968 reg = GPIO_INT_EDGE_MASK;
971 mv_gpio_reg_set(dev, reg, pin);
973 mv_gpio_reg_clear(dev, reg, pin);
977 mv_gpio_int_ack(struct mv_gpio_pindev *s)
980 struct mv_gpio_softc *sc;
981 sc = (struct mv_gpio_softc *)device_get_softc(s->dev);
984 if (pin >= sc->pin_num)
987 reg = GPIO_INT_CAUSE;
989 mv_gpio_reg_clear(s->dev, reg, pin);
993 mv_gpio_value_get(device_t dev, uint32_t pin, uint8_t exclude_polar)
995 uint32_t reg, polar_reg, reg_val, polar_reg_val;
996 struct mv_gpio_softc *sc;
997 sc = (struct mv_gpio_softc *)device_get_softc(dev);
999 if (pin >= sc->pin_num)
1003 polar_reg = GPIO_DATA_IN_POLAR;
1005 reg_val = mv_gpio_reg_read(dev, reg);
1007 if (exclude_polar) {
1008 polar_reg_val = mv_gpio_reg_read(dev, polar_reg);
1009 return ((reg_val & GPIO(pin)) ^ (polar_reg_val & GPIO(pin)));
1011 return (reg_val & GPIO(pin));
1015 mv_gpio_value_set(device_t dev, uint32_t pin, uint8_t val)
1018 struct mv_gpio_softc *sc;
1019 sc = (struct mv_gpio_softc *)device_get_softc(dev);
1021 MV_GPIO_ASSERT_LOCKED();
1023 if (pin >= sc->pin_num)
1026 reg = GPIO_DATA_OUT;
1029 mv_gpio_reg_set(dev, reg, pin);
1031 mv_gpio_reg_clear(dev, reg, pin);
1035 * GPIO interface methods
1039 mv_gpio_pin_max(device_t dev, int *maxpin)
1041 struct mv_gpio_softc *sc;
1045 sc = device_get_softc(dev);
1046 *maxpin = sc->pin_num;
1052 mv_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
1054 struct mv_gpio_softc *sc = device_get_softc(dev);
1058 if (pin >= sc->pin_num)
1062 *caps = sc->gpio_setup[pin].gp_caps;
1069 mv_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
1071 struct mv_gpio_softc *sc = device_get_softc(dev);
1075 if (pin >= sc->pin_num)
1079 *flags = sc->gpio_setup[pin].gp_flags;
1086 mv_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
1088 struct mv_gpio_softc *sc = device_get_softc(dev);
1092 if (pin >= sc->pin_num)
1096 memcpy(name, sc->gpio_setup[pin].gp_name, GPIOMAXNAME);
1103 mv_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
1106 struct mv_gpio_softc *sc = device_get_softc(dev);
1107 if (pin >= sc->pin_num)
1110 /* Check for unwanted flags. */
1111 if ((flags & sc->gpio_setup[pin].gp_caps) != flags)
1114 ret = mv_gpio_configure(dev, pin, flags, ~0);
1120 mv_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
1122 struct mv_gpio_softc *sc = device_get_softc(dev);
1123 if (pin >= sc->pin_num)
1127 mv_gpio_value_set(dev, pin, value);
1134 mv_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
1136 struct mv_gpio_softc *sc = device_get_softc(dev);
1140 if (pin >= sc->pin_num)
1144 *value = mv_gpio_in(dev, pin);
1151 mv_gpio_pin_toggle(device_t dev, uint32_t pin)
1153 struct mv_gpio_softc *sc = device_get_softc(dev);
1155 if (pin >= sc->pin_num)
1159 value = mv_gpio_in(dev, pin);
1160 value = (~value) & 1;
1161 mv_gpio_value_set(dev, pin, value);
1168 mv_gpio_get_bus(device_t dev)
1170 struct mv_gpio_softc *sc = device_get_softc(dev);
1172 return (sc->sc_busdev);
1176 mv_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, int gcells,
1177 pcell_t *gpios, uint32_t *pin, uint32_t *flags)
1179 struct mv_gpio_softc *sc = device_get_softc(bus);
1181 if (gpios[0] >= sc->pin_num)
1186 mv_gpio_configure(bus, *pin, *flags, ~0);