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