Home | History | Annotate | Line # | Download | only in xscale
pxa2x0_gpio.c revision 1.1.2.1
      1  1.1.2.1  skrll /*	$NetBSD: pxa2x0_gpio.c,v 1.1.2.1 2004/08/03 10:32:58 skrll Exp $	*/
      2      1.1    scw 
      3      1.1    scw /*
      4      1.1    scw  * Copyright 2003 Wasabi Systems, Inc.
      5      1.1    scw  * All rights reserved.
      6      1.1    scw  *
      7      1.1    scw  * Written by Steve C. Woodford for Wasabi Systems, Inc.
      8      1.1    scw  *
      9      1.1    scw  * Redistribution and use in source and binary forms, with or without
     10      1.1    scw  * modification, are permitted provided that the following conditions
     11      1.1    scw  * are met:
     12      1.1    scw  * 1. Redistributions of source code must retain the above copyright
     13      1.1    scw  *    notice, this list of conditions and the following disclaimer.
     14      1.1    scw  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1    scw  *    notice, this list of conditions and the following disclaimer in the
     16      1.1    scw  *    documentation and/or other materials provided with the distribution.
     17      1.1    scw  * 3. All advertising materials mentioning features or use of this software
     18      1.1    scw  *    must display the following acknowledgement:
     19      1.1    scw  *      This product includes software developed for the NetBSD Project by
     20      1.1    scw  *      Wasabi Systems, Inc.
     21      1.1    scw  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22      1.1    scw  *    or promote products derived from this software without specific prior
     23      1.1    scw  *    written permission.
     24      1.1    scw  *
     25      1.1    scw  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26      1.1    scw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27      1.1    scw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28      1.1    scw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29      1.1    scw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30      1.1    scw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31      1.1    scw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32      1.1    scw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33      1.1    scw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34      1.1    scw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35      1.1    scw  * POSSIBILITY OF SUCH DAMAGE.
     36      1.1    scw  */
     37      1.1    scw 
     38  1.1.2.1  skrll #include <sys/cdefs.h>
     39  1.1.2.1  skrll __KERNEL_RCSID(0, "$NetBSD: pxa2x0_gpio.c,v 1.1.2.1 2004/08/03 10:32:58 skrll Exp $");
     40  1.1.2.1  skrll 
     41      1.1    scw #include "opt_pxa2x0_gpio.h"
     42      1.1    scw 
     43      1.1    scw #include <sys/param.h>
     44      1.1    scw #include <sys/systm.h>
     45      1.1    scw #include <sys/device.h>
     46      1.1    scw #include <sys/malloc.h>
     47      1.1    scw 
     48      1.1    scw #include <machine/intr.h>
     49      1.1    scw #include <machine/bus.h>
     50      1.1    scw 
     51      1.1    scw #include <arm/xscale/pxa2x0reg.h>
     52      1.1    scw #include <arm/xscale/pxa2x0var.h>
     53      1.1    scw #include <arm/xscale/pxa2x0_gpio.h>
     54      1.1    scw 
     55      1.1    scw #include "locators.h"
     56      1.1    scw 
     57      1.1    scw struct gpio_irq_handler {
     58      1.1    scw 	int (*gh_func)(void *);
     59      1.1    scw 	void *gh_arg;
     60      1.1    scw 	int gh_spl;
     61      1.1    scw 	u_int gh_gpio;
     62      1.1    scw };
     63      1.1    scw 
     64      1.1    scw struct pxagpio_softc {
     65      1.1    scw 	struct device sc_dev;
     66      1.1    scw 	bus_space_tag_t sc_bust;
     67      1.1    scw 	bus_space_handle_t sc_bush;
     68      1.1    scw 	void *sc_irqcookie[3];
     69      1.1    scw 	u_int32_t sc_mask[3];
     70      1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
     71      1.1    scw 	struct gpio_irq_handler *sc_handlers[GPIO_NPINS];
     72      1.1    scw #else
     73      1.1    scw 	struct gpio_irq_handler *sc_handlers[2];
     74      1.1    scw #endif
     75      1.1    scw };
     76      1.1    scw 
     77      1.1    scw static int	pxagpio_match(struct device *, struct cfdata *, void *);
     78      1.1    scw static void	pxagpio_attach(struct device *, struct device *, void *);
     79      1.1    scw 
     80      1.1    scw CFATTACH_DECL(pxagpio, sizeof(struct pxagpio_softc),
     81      1.1    scw     pxagpio_match, pxagpio_attach, NULL, NULL);
     82      1.1    scw 
     83      1.1    scw static struct pxagpio_softc *pxagpio_softc;
     84      1.1    scw static vaddr_t pxagpio_regs;
     85      1.1    scw #define GPIO_BOOTSTRAP_REG(reg)	\
     86      1.1    scw 	(*((volatile u_int32_t *)(pxagpio_regs + (reg))))
     87      1.1    scw 
     88      1.1    scw static int gpio_intr0(void *);
     89      1.1    scw static int gpio_intr1(void *);
     90      1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
     91      1.1    scw static int gpio_dispatch(struct pxagpio_softc *, int);
     92      1.1    scw static int gpio_intrN(void *);
     93      1.1    scw #endif
     94      1.1    scw 
     95      1.1    scw static __inline u_int32_t
     96      1.1    scw pxagpio_reg_read(struct pxagpio_softc *sc, int reg)
     97      1.1    scw {
     98      1.1    scw 	if (__predict_true(sc != NULL))
     99      1.1    scw 		return (bus_space_read_4(sc->sc_bust, sc->sc_bush, reg));
    100      1.1    scw 	else
    101      1.1    scw 	if (pxagpio_regs)
    102      1.1    scw 		return (GPIO_BOOTSTRAP_REG(reg));
    103      1.1    scw 	panic("pxagpio_reg_read: not bootstrapped");
    104      1.1    scw }
    105      1.1    scw 
    106      1.1    scw static __inline void
    107      1.1    scw pxagpio_reg_write(struct pxagpio_softc *sc, int reg, u_int32_t val)
    108      1.1    scw {
    109      1.1    scw 	if (__predict_true(sc != NULL))
    110      1.1    scw 		bus_space_write_4(sc->sc_bust, sc->sc_bush, reg, val);
    111      1.1    scw 	else
    112      1.1    scw 	if (pxagpio_regs)
    113      1.1    scw 		GPIO_BOOTSTRAP_REG(reg) = val;
    114      1.1    scw 	else
    115      1.1    scw 		panic("pxagpio_reg_write: not bootstrapped");
    116      1.1    scw 	return;
    117      1.1    scw }
    118      1.1    scw 
    119      1.1    scw static int
    120      1.1    scw pxagpio_match(struct device *parent, struct cfdata *cf, void *aux)
    121      1.1    scw {
    122      1.1    scw 	struct pxaip_attach_args *pxa = aux;
    123      1.1    scw 
    124      1.1    scw 	if (pxagpio_softc != NULL || pxa->pxa_addr != PXA2X0_GPIO_BASE)
    125      1.1    scw 		return (0);
    126      1.1    scw 
    127      1.1    scw 	pxa->pxa_size = PXA2X0_GPIO_SIZE;
    128      1.1    scw 
    129      1.1    scw 	return (1);
    130      1.1    scw }
    131      1.1    scw 
    132      1.1    scw void
    133      1.1    scw pxagpio_attach(struct device *parent, struct device *self, void *aux)
    134      1.1    scw {
    135      1.1    scw 	struct pxagpio_softc *sc = (struct pxagpio_softc *)self;
    136      1.1    scw 	struct pxaip_attach_args *pxa = aux;
    137      1.1    scw 
    138      1.1    scw 	sc->sc_bust = pxa->pxa_iot;
    139      1.1    scw 
    140      1.1    scw 	aprint_normal(": GPIO Controller\n");
    141      1.1    scw 
    142      1.1    scw 	if (bus_space_map(sc->sc_bust, pxa->pxa_addr, pxa->pxa_size, 0,
    143      1.1    scw 	    &sc->sc_bush)) {
    144      1.1    scw 		aprint_error("%s: Can't map registers!\n", sc->sc_dev.dv_xname);
    145      1.1    scw 		return;
    146      1.1    scw 	}
    147      1.1    scw 
    148      1.1    scw 	memset(sc->sc_handlers, 0, sizeof(sc->sc_handlers));
    149      1.1    scw 
    150      1.1    scw 	/*
    151      1.1    scw 	 * Disable all GPIO interrupts
    152      1.1    scw 	 */
    153      1.1    scw 	pxagpio_reg_write(sc, GPIO_GRER0, 0);
    154      1.1    scw 	pxagpio_reg_write(sc, GPIO_GRER1, 0);
    155      1.1    scw 	pxagpio_reg_write(sc, GPIO_GRER2, 0);
    156      1.1    scw 	pxagpio_reg_write(sc, GPIO_GFER0, 0);
    157      1.1    scw 	pxagpio_reg_write(sc, GPIO_GFER1, 0);
    158      1.1    scw 	pxagpio_reg_write(sc, GPIO_GFER2, 0);
    159      1.1    scw 	pxagpio_reg_write(sc, GPIO_GEDR0, ~0);
    160      1.1    scw 	pxagpio_reg_write(sc, GPIO_GEDR1, ~0);
    161      1.1    scw 	pxagpio_reg_write(sc, GPIO_GEDR2, ~0);
    162      1.1    scw 
    163      1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
    164      1.1    scw 	sc->sc_irqcookie[2] = pxa2x0_intr_establish(PXA2X0_INT_GPION, IPL_BIO,
    165      1.1    scw 	    gpio_intrN, sc);
    166      1.1    scw 	if (sc->sc_irqcookie[2] == NULL) {
    167      1.1    scw 		aprint_error("%s: failed to hook main GPIO interrupt\n",
    168      1.1    scw 		    sc->sc_dev.dv_xname);
    169      1.1    scw 		return;
    170      1.1    scw 	}
    171      1.1    scw #endif
    172      1.1    scw 
    173      1.1    scw 	sc->sc_irqcookie[0] = sc->sc_irqcookie[1] = NULL;
    174      1.1    scw 
    175      1.1    scw 	pxagpio_softc = sc;
    176      1.1    scw }
    177      1.1    scw 
    178      1.1    scw void
    179      1.1    scw pxa2x0_gpio_bootstrap(vaddr_t gpio_regs)
    180      1.1    scw {
    181      1.1    scw 
    182      1.1    scw 	pxagpio_regs = gpio_regs;
    183      1.1    scw }
    184      1.1    scw 
    185      1.1    scw void *
    186      1.1    scw pxa2x0_gpio_intr_establish(u_int gpio, int level, int spl, int (*func)(void *),
    187      1.1    scw     void *arg)
    188      1.1    scw {
    189      1.1    scw 	struct pxagpio_softc *sc = pxagpio_softc;
    190      1.1    scw 	struct gpio_irq_handler *gh;
    191      1.1    scw 	u_int32_t bit, reg;
    192      1.1    scw 
    193      1.1    scw #ifdef DEBUG
    194      1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
    195      1.1    scw 	if (gpio >= GPIO_NPINS)
    196      1.1    scw 		panic("pxa2x0_gpio_intr_establish: bad pin number: %d", gpio);
    197      1.1    scw #else
    198      1.1    scw 	if (gpio > 1)
    199      1.1    scw 		panic("pxa2x0_gpio_intr_establish: bad pin number: %d", gpio);
    200      1.1    scw #endif
    201      1.1    scw #endif
    202      1.1    scw 
    203      1.1    scw 	if (!GPIO_IS_GPIO_IN(pxa2x0_gpio_get_function(gpio)))
    204      1.1    scw 		panic("pxa2x0_gpio_intr_establish: Pin %d not GPIO_IN", gpio);
    205      1.1    scw 
    206      1.1    scw 	switch (level) {
    207      1.1    scw 	case IST_EDGE_FALLING:
    208      1.1    scw 	case IST_EDGE_RISING:
    209      1.1    scw 	case IST_EDGE_BOTH:
    210      1.1    scw 		break;
    211      1.1    scw 
    212      1.1    scw 	default:
    213      1.1    scw 		panic("pxa2x0_gpio_intr_establish: bad level: %d", level);
    214      1.1    scw 		break;
    215      1.1    scw 	}
    216      1.1    scw 
    217      1.1    scw 	if (sc->sc_handlers[gpio] != NULL)
    218      1.1    scw 		panic("pxa2x0_gpio_intr_establish: illegal shared interrupt");
    219      1.1    scw 
    220      1.1    scw 	MALLOC(gh, struct gpio_irq_handler *, sizeof(struct gpio_irq_handler),
    221      1.1    scw 	    M_DEVBUF, M_NOWAIT);
    222      1.1    scw 
    223      1.1    scw 	gh->gh_func = func;
    224      1.1    scw 	gh->gh_arg = arg;
    225      1.1    scw 	gh->gh_spl = spl;
    226      1.1    scw 	gh->gh_gpio = gpio;
    227      1.1    scw 	sc->sc_handlers[gpio] = gh;
    228      1.1    scw 
    229      1.1    scw 	if (gpio == 0) {
    230      1.1    scw 		KDASSERT(sc->sc_irqcookie[0] == NULL);
    231      1.1    scw 		sc->sc_irqcookie[0] = pxa2x0_intr_establish(PXA2X0_INT_GPIO0,
    232      1.1    scw 		    spl, gpio_intr0, sc);
    233      1.1    scw 		KDASSERT(sc->sc_irqcookie[0]);
    234      1.1    scw 	} else
    235      1.1    scw 	if (gpio == 1) {
    236      1.1    scw 		KDASSERT(sc->sc_irqcookie[1] == NULL);
    237      1.1    scw 		sc->sc_irqcookie[1] = pxa2x0_intr_establish(PXA2X0_INT_GPIO1,
    238      1.1    scw 		    spl, gpio_intr1, sc);
    239      1.1    scw 		KDASSERT(sc->sc_irqcookie[1]);
    240      1.1    scw 	}
    241      1.1    scw 
    242      1.1    scw 	bit = GPIO_BIT(gpio);
    243      1.1    scw 	sc->sc_mask[GPIO_BANK(gpio)] |= bit;
    244      1.1    scw 
    245      1.1    scw 	switch (level) {
    246      1.1    scw 	case IST_EDGE_FALLING:
    247      1.1    scw 		reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GFER0, gpio));
    248      1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GFER0, gpio), reg | bit);
    249      1.1    scw 		break;
    250      1.1    scw 
    251      1.1    scw 	case IST_EDGE_RISING:
    252      1.1    scw 		reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GRER0, gpio));
    253      1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GRER0, gpio), reg | bit);
    254      1.1    scw 		break;
    255      1.1    scw 
    256      1.1    scw 	case IST_EDGE_BOTH:
    257      1.1    scw 		reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GFER0, gpio));
    258      1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GFER0, gpio), reg | bit);
    259      1.1    scw 		reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GRER0, gpio));
    260      1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GRER0, gpio), reg | bit);
    261      1.1    scw 		break;
    262      1.1    scw 	}
    263      1.1    scw 
    264      1.1    scw 	return (gh);
    265      1.1    scw }
    266      1.1    scw 
    267      1.1    scw void
    268      1.1    scw pxa2x0_gpio_intr_disestablish(void *cookie)
    269      1.1    scw {
    270      1.1    scw 	struct pxagpio_softc *sc = pxagpio_softc;
    271      1.1    scw 	struct gpio_irq_handler *gh = cookie;
    272      1.1    scw 	u_int32_t bit, reg;
    273      1.1    scw 
    274      1.1    scw 	bit = GPIO_BIT(gh->gh_gpio);
    275      1.1    scw 
    276      1.1    scw 	reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GFER0, gh->gh_gpio));
    277      1.1    scw 	reg &= ~bit;
    278      1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GFER0, gh->gh_gpio), reg);
    279      1.1    scw 	reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GRER0, gh->gh_gpio));
    280      1.1    scw 	reg &= ~bit;
    281      1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GRER0, gh->gh_gpio), reg);
    282      1.1    scw 
    283      1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GEDR0, gh->gh_gpio), bit);
    284      1.1    scw 
    285      1.1    scw 	sc->sc_mask[GPIO_BANK(gh->gh_gpio)] &= ~bit;
    286      1.1    scw 	sc->sc_handlers[gh->gh_gpio] = NULL;
    287      1.1    scw 
    288      1.1    scw 	if (gh->gh_gpio == 0) {
    289      1.1    scw #if 0
    290      1.1    scw 		pxa2x0_intr_disestablish(sc->sc_irqcookie[0]);
    291      1.1    scw 		sc->sc_irqcookie[0] = NULL;
    292      1.1    scw #else
    293      1.1    scw 		panic("pxa2x0_gpio_intr_disestablish: can't unhook GPIO#0");
    294      1.1    scw #endif
    295      1.1    scw 	} else
    296      1.1    scw 	if (gh->gh_gpio == 1) {
    297      1.1    scw #if 0
    298      1.1    scw 		pxa2x0_intr_disestablish(sc->sc_irqcookie[1]);
    299      1.1    scw 		sc->sc_irqcookie[0] = NULL;
    300      1.1    scw #else
    301      1.1    scw 		panic("pxa2x0_gpio_intr_disestablish: can't unhook GPIO#0");
    302      1.1    scw #endif
    303      1.1    scw 	}
    304      1.1    scw 
    305      1.1    scw 	FREE(gh, M_DEVBUF);
    306      1.1    scw }
    307      1.1    scw 
    308      1.1    scw static int
    309      1.1    scw gpio_intr0(void *arg)
    310      1.1    scw {
    311      1.1    scw 	struct pxagpio_softc *sc = arg;
    312      1.1    scw 
    313      1.1    scw #ifdef DIAGNOSTIC
    314      1.1    scw 	if (sc->sc_handlers[0] == NULL) {
    315      1.1    scw 		printf("%s: stray GPIO#0 edge interrupt\n",
    316      1.1    scw 		    sc->sc_dev.dv_xname);
    317      1.1    scw 		return (0);
    318      1.1    scw 	}
    319      1.1    scw #endif
    320      1.1    scw 
    321      1.1    scw 	bus_space_write_4(sc->sc_bust, sc->sc_bush, GPIO_REG(GPIO_GEDR0, 0),
    322      1.1    scw 	    GPIO_BIT(0));
    323      1.1    scw 
    324      1.1    scw 	return ((sc->sc_handlers[0]->gh_func)(sc->sc_handlers[0]->gh_arg));
    325      1.1    scw }
    326      1.1    scw 
    327      1.1    scw static int
    328      1.1    scw gpio_intr1(void *arg)
    329      1.1    scw {
    330      1.1    scw 	struct pxagpio_softc *sc = arg;
    331      1.1    scw 
    332      1.1    scw #ifdef DIAGNOSTIC
    333      1.1    scw 	if (sc->sc_handlers[1] == NULL) {
    334      1.1    scw 		printf("%s: stray GPIO#1 edge interrupt\n",
    335      1.1    scw 		    sc->sc_dev.dv_xname);
    336      1.1    scw 		return (0);
    337      1.1    scw 	}
    338      1.1    scw #endif
    339      1.1    scw 
    340      1.1    scw 	bus_space_write_4(sc->sc_bust, sc->sc_bush, GPIO_REG(GPIO_GEDR0, 1),
    341      1.1    scw 	    GPIO_BIT(1));
    342      1.1    scw 
    343      1.1    scw 	return ((sc->sc_handlers[1]->gh_func)(sc->sc_handlers[1]->gh_arg));
    344      1.1    scw }
    345      1.1    scw 
    346      1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
    347      1.1    scw static int
    348      1.1    scw gpio_dispatch(struct pxagpio_softc *sc, int gpio_base)
    349      1.1    scw {
    350      1.1    scw 	struct gpio_irq_handler **ghp, *gh;
    351      1.1    scw 	int i, s, handled, pins;
    352      1.1    scw 	u_int32_t gedr, mask;
    353      1.1    scw 	int bank;
    354      1.1    scw 
    355      1.1    scw 	/* Fetch bitmap of pending interrupts on this GPIO bank */
    356      1.1    scw 	gedr = pxagpio_reg_read(sc, GPIO_REG(GPIO_GEDR0, gpio_base));
    357      1.1    scw 
    358      1.1    scw 	/* Don't handle GPIO 0/1 here */
    359      1.1    scw 	if (gpio_base == 0)
    360      1.1    scw 		gedr &= ~(GPIO_BIT(0) | GPIO_BIT(1));
    361      1.1    scw 
    362      1.1    scw 	/* Bail early if there are no pending interrupts in this bank */
    363      1.1    scw 	if (gedr == 0)
    364      1.1    scw 		return (0);
    365      1.1    scw 
    366      1.1    scw 	/* Acknowledge pending interrupts. */
    367      1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GEDR0, gpio_base), gedr);
    368      1.1    scw 
    369      1.1    scw 	bank = GPIO_BANK(gpio_base);
    370      1.1    scw 
    371      1.1    scw 	/*
    372      1.1    scw 	 * We're only interested in those for which we have a handler
    373      1.1    scw 	 * registered
    374      1.1    scw 	 */
    375      1.1    scw #ifdef DEBUG
    376      1.1    scw 	if ((gedr & sc->sc_mask[bank]) == 0) {
    377      1.1    scw 		printf("%s: stray GPIO interrupt. Bank %d, GEDR 0x%08x, mask 0x%08x\n",
    378      1.1    scw 		    sc->sc_dev.dv_xname, bank, gedr, sc->sc_mask[bank]);
    379      1.1    scw 		return (1);	/* XXX: Pretend we dealt with it */
    380      1.1    scw 	}
    381      1.1    scw #endif
    382      1.1    scw 
    383      1.1    scw 	gedr &= sc->sc_mask[bank];
    384      1.1    scw 	ghp = &sc->sc_handlers[gpio_base];
    385      1.1    scw 	pins = (gpio_base < 64) ? 32 : 17;
    386      1.1    scw 	handled = 0;
    387      1.1    scw 
    388      1.1    scw 	for (i = 0, mask = 1; i < pins && gedr; i++, ghp++, mask <<= 1) {
    389      1.1    scw 		if ((gedr & mask) == 0)
    390      1.1    scw 			continue;
    391      1.1    scw 		gedr &= ~mask;
    392      1.1    scw 
    393      1.1    scw 		if ((gh = *ghp) == NULL) {
    394      1.1    scw 			printf("%s: unhandled GPIO interrupt. GPIO#%d\n",
    395      1.1    scw 			    sc->sc_dev.dv_xname, gpio_base + i);
    396      1.1    scw 			continue;
    397      1.1    scw 		}
    398      1.1    scw 
    399      1.1    scw 		s = _splraise(gh->gh_spl);
    400      1.1    scw 		handled |= (gh->gh_func)(gh->gh_arg);
    401      1.1    scw 		splx(s);
    402      1.1    scw 	}
    403      1.1    scw 
    404      1.1    scw 	return (handled);
    405      1.1    scw }
    406      1.1    scw 
    407      1.1    scw static int
    408      1.1    scw gpio_intrN(void *arg)
    409      1.1    scw {
    410      1.1    scw 	struct pxagpio_softc *sc = arg;
    411      1.1    scw 	int handled;
    412      1.1    scw 
    413      1.1    scw 	handled = gpio_dispatch(sc, 0);
    414      1.1    scw 	handled |= gpio_dispatch(sc, 32);
    415      1.1    scw 	handled |= gpio_dispatch(sc, 64);
    416      1.1    scw 
    417      1.1    scw 	return (handled);
    418      1.1    scw }
    419      1.1    scw #endif	/* PXAGPIO_HAS_GPION_INTRS */
    420      1.1    scw 
    421      1.1    scw u_int
    422      1.1    scw pxa2x0_gpio_get_function(u_int gpio)
    423      1.1    scw {
    424      1.1    scw 	struct pxagpio_softc *sc = pxagpio_softc;
    425      1.1    scw 	u_int32_t rv, io;
    426      1.1    scw 
    427      1.1    scw 	KDASSERT(gpio < GPIO_NPINS);
    428      1.1    scw 
    429      1.1    scw 	rv = pxagpio_reg_read(sc, GPIO_FN_REG(gpio)) >> GPIO_FN_SHIFT(gpio);
    430      1.1    scw 	rv = GPIO_FN(rv);
    431      1.1    scw 
    432      1.1    scw 	io = pxagpio_reg_read(sc, GPIO_REG(GPIO_GPDR0, gpio));
    433      1.1    scw 	if (io & GPIO_BIT(gpio))
    434      1.1    scw 		rv |= GPIO_OUT;
    435      1.1    scw 
    436      1.1    scw 	io = pxagpio_reg_read(sc, GPIO_REG(GPIO_GPLR0, gpio));
    437      1.1    scw 	if (io & GPIO_BIT(gpio))
    438      1.1    scw 		rv |= GPIO_SET;
    439      1.1    scw 
    440      1.1    scw 	return (rv);
    441      1.1    scw }
    442      1.1    scw 
    443      1.1    scw u_int
    444      1.1    scw pxa2x0_gpio_set_function(u_int gpio, u_int fn)
    445      1.1    scw {
    446      1.1    scw 	struct pxagpio_softc *sc = pxagpio_softc;
    447      1.1    scw 	u_int32_t rv, bit;
    448      1.1    scw 	u_int oldfn;
    449      1.1    scw 
    450      1.1    scw 	KDASSERT(gpio < GPIO_NPINS);
    451      1.1    scw 
    452      1.1    scw 	oldfn = pxa2x0_gpio_get_function(gpio);
    453      1.1    scw 
    454      1.1    scw 	if (GPIO_FN(fn) == GPIO_FN(oldfn) &&
    455      1.1    scw 	    GPIO_FN_IS_OUT(fn) == GPIO_FN_IS_OUT(oldfn)) {
    456      1.1    scw 		/*
    457      1.1    scw 		 * The pin's function is not changing.
    458      1.1    scw 		 * For Alternate Functions and GPIO input, we can just
    459      1.1    scw 		 * return now.
    460      1.1    scw 		 * For GPIO output pins, check the initial state is
    461      1.1    scw 		 * the same.
    462      1.1    scw 		 *
    463      1.1    scw 		 * Return 'fn' instead of 'oldfn' so the caller can
    464      1.1    scw 		 * reliably detect that we didn't change anything.
    465      1.1    scw 		 * (The initial state might be different for non-
    466      1.1    scw 		 * GPIO output pins).
    467      1.1    scw 		 */
    468      1.1    scw 		if (!GPIO_IS_GPIO_OUT(fn) ||
    469      1.1    scw 		    GPIO_FN_IS_SET(fn) == GPIO_FN_IS_SET(oldfn))
    470      1.1    scw 			return (fn);
    471      1.1    scw 	}
    472      1.1    scw 
    473      1.1    scw 	/*
    474      1.1    scw 	 * See section 4.1.3.7 of the PXA2x0 Developer's Manual for
    475      1.1    scw 	 * the correct procedure for changing GPIO pin functions.
    476      1.1    scw 	 */
    477      1.1    scw 
    478      1.1    scw 	bit = GPIO_BIT(gpio);
    479      1.1    scw 
    480      1.1    scw 	/*
    481      1.1    scw 	 * 1. Configure the correct set/clear state of the pin
    482      1.1    scw 	 */
    483      1.1    scw 	if (GPIO_FN_IS_SET(fn))
    484      1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GPSR0, gpio), bit);
    485      1.1    scw 	else
    486      1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GPCR0, gpio), bit);
    487      1.1    scw 
    488      1.1    scw 	/*
    489      1.1    scw 	 * 2. Configure the pin as an input or output as appropriate
    490      1.1    scw 	 */
    491      1.1    scw 	rv = pxagpio_reg_read(sc, GPIO_REG(GPIO_GPDR0, gpio)) & ~bit;
    492      1.1    scw 	if (GPIO_FN_IS_OUT(fn))
    493      1.1    scw 		rv |= bit;
    494      1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GPDR0, gpio), rv);
    495      1.1    scw 
    496      1.1    scw 	/*
    497      1.1    scw 	 * 3. Configure the pin's function
    498      1.1    scw 	 */
    499      1.1    scw 	bit = GPIO_FN_MASK << GPIO_FN_SHIFT(gpio);
    500      1.1    scw 	fn = GPIO_FN(fn) << GPIO_FN_SHIFT(gpio);
    501      1.1    scw 	rv = pxagpio_reg_read(sc, GPIO_FN_REG(gpio)) & ~bit;
    502      1.1    scw 	pxagpio_reg_write(sc, GPIO_FN_REG(gpio), rv | fn);
    503      1.1    scw 
    504      1.1    scw 	return (oldfn);
    505      1.1    scw }
    506