Home | History | Annotate | Line # | Download | only in xscale
pxa2x0_gpio.c revision 1.5.20.1
      1  1.5.20.1     ad /*	$NetBSD: pxa2x0_gpio.c,v 1.5.20.1 2007/01/12 01:00:42 ad 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.2  lukem 
     38       1.2  lukem #include <sys/cdefs.h>
     39  1.5.20.1     ad __KERNEL_RCSID(0, "$NetBSD: pxa2x0_gpio.c,v 1.5.20.1 2007/01/12 01:00:42 ad Exp $");
     40       1.1    scw 
     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.3    bsh #include <arm/xscale/pxa2x0cpu.h>
     52       1.1    scw #include <arm/xscale/pxa2x0reg.h>
     53       1.1    scw #include <arm/xscale/pxa2x0var.h>
     54       1.1    scw #include <arm/xscale/pxa2x0_gpio.h>
     55       1.1    scw 
     56       1.1    scw #include "locators.h"
     57       1.1    scw 
     58       1.1    scw struct gpio_irq_handler {
     59  1.5.20.1     ad 	struct gpio_irq_handler *gh_next;
     60       1.1    scw 	int (*gh_func)(void *);
     61       1.1    scw 	void *gh_arg;
     62       1.1    scw 	int gh_spl;
     63       1.1    scw 	u_int gh_gpio;
     64  1.5.20.1     ad 	int gh_level;
     65       1.1    scw };
     66       1.1    scw 
     67       1.1    scw struct pxagpio_softc {
     68       1.1    scw 	struct device sc_dev;
     69       1.1    scw 	bus_space_tag_t sc_bust;
     70       1.1    scw 	bus_space_handle_t sc_bush;
     71       1.3    bsh 	void *sc_irqcookie[4];
     72       1.3    bsh 	u_int32_t sc_mask[4];
     73       1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
     74       1.1    scw 	struct gpio_irq_handler *sc_handlers[GPIO_NPINS];
     75       1.1    scw #else
     76       1.1    scw 	struct gpio_irq_handler *sc_handlers[2];
     77       1.1    scw #endif
     78       1.1    scw };
     79       1.1    scw 
     80       1.1    scw static int	pxagpio_match(struct device *, struct cfdata *, void *);
     81       1.1    scw static void	pxagpio_attach(struct device *, struct device *, void *);
     82       1.1    scw 
     83       1.1    scw CFATTACH_DECL(pxagpio, sizeof(struct pxagpio_softc),
     84       1.1    scw     pxagpio_match, pxagpio_attach, NULL, NULL);
     85       1.1    scw 
     86       1.1    scw static struct pxagpio_softc *pxagpio_softc;
     87       1.1    scw static vaddr_t pxagpio_regs;
     88       1.1    scw #define GPIO_BOOTSTRAP_REG(reg)	\
     89       1.1    scw 	(*((volatile u_int32_t *)(pxagpio_regs + (reg))))
     90       1.1    scw 
     91       1.1    scw static int gpio_intr0(void *);
     92       1.1    scw static int gpio_intr1(void *);
     93       1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
     94       1.1    scw static int gpio_dispatch(struct pxagpio_softc *, int);
     95       1.1    scw static int gpio_intrN(void *);
     96       1.1    scw #endif
     97       1.1    scw 
     98       1.5  perry static inline u_int32_t
     99       1.1    scw pxagpio_reg_read(struct pxagpio_softc *sc, int reg)
    100       1.1    scw {
    101       1.1    scw 	if (__predict_true(sc != NULL))
    102       1.1    scw 		return (bus_space_read_4(sc->sc_bust, sc->sc_bush, reg));
    103       1.1    scw 	else
    104       1.1    scw 	if (pxagpio_regs)
    105       1.1    scw 		return (GPIO_BOOTSTRAP_REG(reg));
    106       1.1    scw 	panic("pxagpio_reg_read: not bootstrapped");
    107       1.1    scw }
    108       1.1    scw 
    109       1.5  perry static inline void
    110       1.1    scw pxagpio_reg_write(struct pxagpio_softc *sc, int reg, u_int32_t val)
    111       1.1    scw {
    112       1.1    scw 	if (__predict_true(sc != NULL))
    113       1.1    scw 		bus_space_write_4(sc->sc_bust, sc->sc_bush, reg, val);
    114       1.1    scw 	else
    115       1.1    scw 	if (pxagpio_regs)
    116       1.1    scw 		GPIO_BOOTSTRAP_REG(reg) = val;
    117       1.1    scw 	else
    118       1.1    scw 		panic("pxagpio_reg_write: not bootstrapped");
    119       1.1    scw 	return;
    120       1.1    scw }
    121       1.1    scw 
    122       1.1    scw static int
    123       1.1    scw pxagpio_match(struct device *parent, struct cfdata *cf, void *aux)
    124       1.1    scw {
    125       1.1    scw 	struct pxaip_attach_args *pxa = aux;
    126       1.1    scw 
    127       1.1    scw 	if (pxagpio_softc != NULL || pxa->pxa_addr != PXA2X0_GPIO_BASE)
    128       1.1    scw 		return (0);
    129       1.1    scw 
    130       1.1    scw 	pxa->pxa_size = PXA2X0_GPIO_SIZE;
    131       1.1    scw 
    132       1.1    scw 	return (1);
    133       1.1    scw }
    134       1.1    scw 
    135  1.5.20.1     ad static void
    136       1.1    scw pxagpio_attach(struct device *parent, struct device *self, void *aux)
    137       1.1    scw {
    138       1.1    scw 	struct pxagpio_softc *sc = (struct pxagpio_softc *)self;
    139       1.1    scw 	struct pxaip_attach_args *pxa = aux;
    140       1.1    scw 
    141       1.1    scw 	sc->sc_bust = pxa->pxa_iot;
    142       1.1    scw 
    143       1.1    scw 	aprint_normal(": GPIO Controller\n");
    144       1.1    scw 
    145       1.1    scw 	if (bus_space_map(sc->sc_bust, pxa->pxa_addr, pxa->pxa_size, 0,
    146       1.1    scw 	    &sc->sc_bush)) {
    147       1.1    scw 		aprint_error("%s: Can't map registers!\n", sc->sc_dev.dv_xname);
    148       1.1    scw 		return;
    149       1.1    scw 	}
    150       1.1    scw 
    151       1.3    bsh 	pxagpio_regs = (vaddr_t)bus_space_vaddr(sc->sc_bust, sc->sc_bush);
    152       1.3    bsh 
    153       1.1    scw 	memset(sc->sc_handlers, 0, sizeof(sc->sc_handlers));
    154       1.1    scw 
    155       1.1    scw 	/*
    156       1.1    scw 	 * Disable all GPIO interrupts
    157       1.1    scw 	 */
    158       1.1    scw 	pxagpio_reg_write(sc, GPIO_GRER0, 0);
    159       1.1    scw 	pxagpio_reg_write(sc, GPIO_GRER1, 0);
    160       1.1    scw 	pxagpio_reg_write(sc, GPIO_GRER2, 0);
    161       1.1    scw 	pxagpio_reg_write(sc, GPIO_GFER0, 0);
    162       1.1    scw 	pxagpio_reg_write(sc, GPIO_GFER1, 0);
    163       1.1    scw 	pxagpio_reg_write(sc, GPIO_GFER2, 0);
    164       1.1    scw 	pxagpio_reg_write(sc, GPIO_GEDR0, ~0);
    165       1.1    scw 	pxagpio_reg_write(sc, GPIO_GEDR1, ~0);
    166       1.1    scw 	pxagpio_reg_write(sc, GPIO_GEDR2, ~0);
    167       1.3    bsh #ifdef	CPU_XSCALE_PXA270
    168       1.3    bsh 	if (CPU_IS_PXA270) {
    169       1.3    bsh 		pxagpio_reg_write(sc, GPIO_GRER3, 0);
    170       1.3    bsh 		pxagpio_reg_write(sc, GPIO_GFER3, 0);
    171       1.3    bsh 		pxagpio_reg_write(sc, GPIO_GEDR3, ~0);
    172       1.3    bsh 	}
    173       1.3    bsh #endif
    174       1.1    scw 
    175       1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
    176       1.1    scw 	sc->sc_irqcookie[2] = pxa2x0_intr_establish(PXA2X0_INT_GPION, IPL_BIO,
    177       1.1    scw 	    gpio_intrN, sc);
    178       1.1    scw 	if (sc->sc_irqcookie[2] == NULL) {
    179       1.1    scw 		aprint_error("%s: failed to hook main GPIO interrupt\n",
    180       1.1    scw 		    sc->sc_dev.dv_xname);
    181       1.1    scw 		return;
    182       1.1    scw 	}
    183       1.1    scw #endif
    184       1.1    scw 
    185       1.1    scw 	sc->sc_irqcookie[0] = sc->sc_irqcookie[1] = NULL;
    186       1.1    scw 
    187       1.1    scw 	pxagpio_softc = sc;
    188       1.1    scw }
    189       1.1    scw 
    190       1.1    scw void
    191       1.1    scw pxa2x0_gpio_bootstrap(vaddr_t gpio_regs)
    192       1.1    scw {
    193       1.1    scw 
    194       1.1    scw 	pxagpio_regs = gpio_regs;
    195       1.1    scw }
    196       1.1    scw 
    197       1.1    scw void *
    198       1.1    scw pxa2x0_gpio_intr_establish(u_int gpio, int level, int spl, int (*func)(void *),
    199       1.1    scw     void *arg)
    200       1.1    scw {
    201       1.1    scw 	struct pxagpio_softc *sc = pxagpio_softc;
    202       1.1    scw 	struct gpio_irq_handler *gh;
    203       1.1    scw 	u_int32_t bit, reg;
    204       1.1    scw 
    205       1.1    scw #ifdef DEBUG
    206       1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
    207       1.1    scw 	if (gpio >= GPIO_NPINS)
    208       1.1    scw 		panic("pxa2x0_gpio_intr_establish: bad pin number: %d", gpio);
    209       1.1    scw #else
    210       1.1    scw 	if (gpio > 1)
    211       1.1    scw 		panic("pxa2x0_gpio_intr_establish: bad pin number: %d", gpio);
    212       1.1    scw #endif
    213       1.1    scw #endif
    214       1.1    scw 
    215       1.1    scw 	if (!GPIO_IS_GPIO_IN(pxa2x0_gpio_get_function(gpio)))
    216       1.1    scw 		panic("pxa2x0_gpio_intr_establish: Pin %d not GPIO_IN", gpio);
    217       1.1    scw 
    218       1.1    scw 	switch (level) {
    219       1.1    scw 	case IST_EDGE_FALLING:
    220       1.1    scw 	case IST_EDGE_RISING:
    221       1.1    scw 	case IST_EDGE_BOTH:
    222       1.1    scw 		break;
    223       1.1    scw 
    224       1.1    scw 	default:
    225       1.1    scw 		panic("pxa2x0_gpio_intr_establish: bad level: %d", level);
    226       1.1    scw 		break;
    227       1.1    scw 	}
    228       1.1    scw 
    229       1.1    scw 	if (sc->sc_handlers[gpio] != NULL)
    230       1.1    scw 		panic("pxa2x0_gpio_intr_establish: illegal shared interrupt");
    231       1.1    scw 
    232       1.1    scw 	MALLOC(gh, struct gpio_irq_handler *, sizeof(struct gpio_irq_handler),
    233       1.1    scw 	    M_DEVBUF, M_NOWAIT);
    234       1.1    scw 
    235       1.1    scw 	gh->gh_func = func;
    236       1.1    scw 	gh->gh_arg = arg;
    237       1.1    scw 	gh->gh_spl = spl;
    238       1.1    scw 	gh->gh_gpio = gpio;
    239  1.5.20.1     ad 	gh->gh_level = level;
    240  1.5.20.1     ad 	gh->gh_next = sc->sc_handlers[gpio];
    241       1.1    scw 	sc->sc_handlers[gpio] = gh;
    242       1.1    scw 
    243       1.1    scw 	if (gpio == 0) {
    244       1.1    scw 		KDASSERT(sc->sc_irqcookie[0] == NULL);
    245       1.1    scw 		sc->sc_irqcookie[0] = pxa2x0_intr_establish(PXA2X0_INT_GPIO0,
    246       1.1    scw 		    spl, gpio_intr0, sc);
    247       1.1    scw 		KDASSERT(sc->sc_irqcookie[0]);
    248       1.1    scw 	} else
    249       1.1    scw 	if (gpio == 1) {
    250       1.1    scw 		KDASSERT(sc->sc_irqcookie[1] == NULL);
    251       1.1    scw 		sc->sc_irqcookie[1] = pxa2x0_intr_establish(PXA2X0_INT_GPIO1,
    252       1.1    scw 		    spl, gpio_intr1, sc);
    253       1.1    scw 		KDASSERT(sc->sc_irqcookie[1]);
    254       1.1    scw 	}
    255       1.1    scw 
    256       1.1    scw 	bit = GPIO_BIT(gpio);
    257       1.1    scw 	sc->sc_mask[GPIO_BANK(gpio)] |= bit;
    258       1.1    scw 
    259       1.1    scw 	switch (level) {
    260       1.1    scw 	case IST_EDGE_FALLING:
    261       1.1    scw 		reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GFER0, gpio));
    262       1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GFER0, gpio), reg | bit);
    263       1.1    scw 		break;
    264       1.1    scw 
    265       1.1    scw 	case IST_EDGE_RISING:
    266       1.1    scw 		reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GRER0, gpio));
    267       1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GRER0, gpio), reg | bit);
    268       1.1    scw 		break;
    269       1.1    scw 
    270       1.1    scw 	case IST_EDGE_BOTH:
    271       1.1    scw 		reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GFER0, gpio));
    272       1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GFER0, gpio), reg | bit);
    273       1.1    scw 		reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GRER0, gpio));
    274       1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GRER0, gpio), reg | bit);
    275       1.1    scw 		break;
    276       1.1    scw 	}
    277       1.1    scw 
    278       1.1    scw 	return (gh);
    279       1.1    scw }
    280       1.1    scw 
    281       1.1    scw void
    282       1.1    scw pxa2x0_gpio_intr_disestablish(void *cookie)
    283       1.1    scw {
    284       1.1    scw 	struct pxagpio_softc *sc = pxagpio_softc;
    285       1.1    scw 	struct gpio_irq_handler *gh = cookie;
    286       1.1    scw 	u_int32_t bit, reg;
    287       1.1    scw 
    288       1.1    scw 	bit = GPIO_BIT(gh->gh_gpio);
    289       1.1    scw 
    290       1.1    scw 	reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GFER0, gh->gh_gpio));
    291       1.1    scw 	reg &= ~bit;
    292       1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GFER0, gh->gh_gpio), reg);
    293       1.1    scw 	reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GRER0, gh->gh_gpio));
    294       1.1    scw 	reg &= ~bit;
    295       1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GRER0, gh->gh_gpio), reg);
    296       1.1    scw 
    297       1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GEDR0, gh->gh_gpio), bit);
    298       1.1    scw 
    299       1.1    scw 	sc->sc_mask[GPIO_BANK(gh->gh_gpio)] &= ~bit;
    300       1.1    scw 	sc->sc_handlers[gh->gh_gpio] = NULL;
    301       1.1    scw 
    302       1.1    scw 	if (gh->gh_gpio == 0) {
    303       1.1    scw #if 0
    304       1.1    scw 		pxa2x0_intr_disestablish(sc->sc_irqcookie[0]);
    305       1.1    scw 		sc->sc_irqcookie[0] = NULL;
    306       1.1    scw #else
    307       1.1    scw 		panic("pxa2x0_gpio_intr_disestablish: can't unhook GPIO#0");
    308       1.1    scw #endif
    309       1.1    scw 	} else
    310       1.1    scw 	if (gh->gh_gpio == 1) {
    311       1.1    scw #if 0
    312       1.1    scw 		pxa2x0_intr_disestablish(sc->sc_irqcookie[1]);
    313       1.1    scw 		sc->sc_irqcookie[0] = NULL;
    314       1.1    scw #else
    315       1.1    scw 		panic("pxa2x0_gpio_intr_disestablish: can't unhook GPIO#0");
    316       1.1    scw #endif
    317       1.1    scw 	}
    318       1.1    scw 
    319       1.1    scw 	FREE(gh, M_DEVBUF);
    320       1.1    scw }
    321       1.1    scw 
    322       1.1    scw static int
    323       1.1    scw gpio_intr0(void *arg)
    324       1.1    scw {
    325       1.1    scw 	struct pxagpio_softc *sc = arg;
    326       1.1    scw 
    327       1.1    scw #ifdef DIAGNOSTIC
    328       1.1    scw 	if (sc->sc_handlers[0] == NULL) {
    329       1.1    scw 		printf("%s: stray GPIO#0 edge interrupt\n",
    330       1.1    scw 		    sc->sc_dev.dv_xname);
    331       1.1    scw 		return (0);
    332       1.1    scw 	}
    333       1.1    scw #endif
    334       1.1    scw 
    335       1.1    scw 	bus_space_write_4(sc->sc_bust, sc->sc_bush, GPIO_REG(GPIO_GEDR0, 0),
    336       1.1    scw 	    GPIO_BIT(0));
    337       1.1    scw 
    338       1.1    scw 	return ((sc->sc_handlers[0]->gh_func)(sc->sc_handlers[0]->gh_arg));
    339       1.1    scw }
    340       1.1    scw 
    341       1.1    scw static int
    342       1.1    scw gpio_intr1(void *arg)
    343       1.1    scw {
    344       1.1    scw 	struct pxagpio_softc *sc = arg;
    345       1.1    scw 
    346       1.1    scw #ifdef DIAGNOSTIC
    347       1.1    scw 	if (sc->sc_handlers[1] == NULL) {
    348       1.1    scw 		printf("%s: stray GPIO#1 edge interrupt\n",
    349       1.1    scw 		    sc->sc_dev.dv_xname);
    350       1.1    scw 		return (0);
    351       1.1    scw 	}
    352       1.1    scw #endif
    353       1.1    scw 
    354       1.1    scw 	bus_space_write_4(sc->sc_bust, sc->sc_bush, GPIO_REG(GPIO_GEDR0, 1),
    355       1.1    scw 	    GPIO_BIT(1));
    356       1.1    scw 
    357       1.1    scw 	return ((sc->sc_handlers[1]->gh_func)(sc->sc_handlers[1]->gh_arg));
    358       1.1    scw }
    359       1.1    scw 
    360       1.1    scw #ifdef PXAGPIO_HAS_GPION_INTRS
    361       1.1    scw static int
    362       1.1    scw gpio_dispatch(struct pxagpio_softc *sc, int gpio_base)
    363       1.1    scw {
    364       1.1    scw 	struct gpio_irq_handler **ghp, *gh;
    365  1.5.20.1     ad 	int i, s, nhandled, handled, pins;
    366       1.1    scw 	u_int32_t gedr, mask;
    367       1.1    scw 	int bank;
    368       1.1    scw 
    369       1.1    scw 	/* Fetch bitmap of pending interrupts on this GPIO bank */
    370       1.1    scw 	gedr = pxagpio_reg_read(sc, GPIO_REG(GPIO_GEDR0, gpio_base));
    371       1.1    scw 
    372       1.1    scw 	/* Don't handle GPIO 0/1 here */
    373       1.1    scw 	if (gpio_base == 0)
    374       1.1    scw 		gedr &= ~(GPIO_BIT(0) | GPIO_BIT(1));
    375       1.1    scw 
    376       1.1    scw 	/* Bail early if there are no pending interrupts in this bank */
    377       1.1    scw 	if (gedr == 0)
    378       1.1    scw 		return (0);
    379       1.1    scw 
    380       1.1    scw 	/* Acknowledge pending interrupts. */
    381       1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GEDR0, gpio_base), gedr);
    382       1.1    scw 
    383       1.1    scw 	bank = GPIO_BANK(gpio_base);
    384       1.1    scw 
    385       1.1    scw 	/*
    386       1.1    scw 	 * We're only interested in those for which we have a handler
    387       1.1    scw 	 * registered
    388       1.1    scw 	 */
    389       1.1    scw #ifdef DEBUG
    390       1.1    scw 	if ((gedr & sc->sc_mask[bank]) == 0) {
    391       1.1    scw 		printf("%s: stray GPIO interrupt. Bank %d, GEDR 0x%08x, mask 0x%08x\n",
    392       1.1    scw 		    sc->sc_dev.dv_xname, bank, gedr, sc->sc_mask[bank]);
    393       1.1    scw 		return (1);	/* XXX: Pretend we dealt with it */
    394       1.1    scw 	}
    395       1.1    scw #endif
    396       1.1    scw 
    397       1.1    scw 	gedr &= sc->sc_mask[bank];
    398       1.1    scw 	ghp = &sc->sc_handlers[gpio_base];
    399       1.3    bsh 	if (CPU_IS_PXA270)
    400       1.3    bsh 		pins = (gpio_base < 96) ? 32 : 25;
    401       1.3    bsh 	else
    402       1.3    bsh 		pins = (gpio_base < 64) ? 32 : 17;
    403       1.1    scw 	handled = 0;
    404       1.1    scw 
    405       1.1    scw 	for (i = 0, mask = 1; i < pins && gedr; i++, ghp++, mask <<= 1) {
    406       1.1    scw 		if ((gedr & mask) == 0)
    407       1.1    scw 			continue;
    408       1.1    scw 		gedr &= ~mask;
    409       1.1    scw 
    410       1.1    scw 		if ((gh = *ghp) == NULL) {
    411       1.1    scw 			printf("%s: unhandled GPIO interrupt. GPIO#%d\n",
    412       1.1    scw 			    sc->sc_dev.dv_xname, gpio_base + i);
    413       1.1    scw 			continue;
    414       1.1    scw 		}
    415       1.1    scw 
    416       1.1    scw 		s = _splraise(gh->gh_spl);
    417  1.5.20.1     ad 		do {
    418  1.5.20.1     ad 			nhandled = (gh->gh_func)(gh->gh_arg);
    419  1.5.20.1     ad 			handled |= nhandled;
    420  1.5.20.1     ad 			gh = gh->gh_next;
    421  1.5.20.1     ad 		} while (gh != NULL);
    422       1.1    scw 		splx(s);
    423       1.1    scw 	}
    424       1.1    scw 
    425       1.1    scw 	return (handled);
    426       1.1    scw }
    427       1.1    scw 
    428       1.1    scw static int
    429       1.1    scw gpio_intrN(void *arg)
    430       1.1    scw {
    431       1.1    scw 	struct pxagpio_softc *sc = arg;
    432       1.1    scw 	int handled;
    433       1.1    scw 
    434       1.1    scw 	handled = gpio_dispatch(sc, 0);
    435       1.1    scw 	handled |= gpio_dispatch(sc, 32);
    436       1.1    scw 	handled |= gpio_dispatch(sc, 64);
    437       1.3    bsh 	if (CPU_IS_PXA270)
    438       1.3    bsh 		handled |= gpio_dispatch(sc, 96);
    439       1.1    scw 	return (handled);
    440       1.1    scw }
    441       1.1    scw #endif	/* PXAGPIO_HAS_GPION_INTRS */
    442       1.1    scw 
    443       1.1    scw u_int
    444       1.1    scw pxa2x0_gpio_get_function(u_int gpio)
    445       1.1    scw {
    446       1.1    scw 	struct pxagpio_softc *sc = pxagpio_softc;
    447       1.1    scw 	u_int32_t rv, io;
    448       1.1    scw 
    449       1.1    scw 	KDASSERT(gpio < GPIO_NPINS);
    450       1.1    scw 
    451       1.1    scw 	rv = pxagpio_reg_read(sc, GPIO_FN_REG(gpio)) >> GPIO_FN_SHIFT(gpio);
    452       1.1    scw 	rv = GPIO_FN(rv);
    453       1.1    scw 
    454       1.1    scw 	io = pxagpio_reg_read(sc, GPIO_REG(GPIO_GPDR0, gpio));
    455       1.1    scw 	if (io & GPIO_BIT(gpio))
    456       1.1    scw 		rv |= GPIO_OUT;
    457       1.1    scw 
    458       1.1    scw 	io = pxagpio_reg_read(sc, GPIO_REG(GPIO_GPLR0, gpio));
    459       1.1    scw 	if (io & GPIO_BIT(gpio))
    460       1.1    scw 		rv |= GPIO_SET;
    461       1.1    scw 
    462       1.1    scw 	return (rv);
    463       1.1    scw }
    464       1.1    scw 
    465       1.1    scw u_int
    466       1.1    scw pxa2x0_gpio_set_function(u_int gpio, u_int fn)
    467       1.1    scw {
    468       1.1    scw 	struct pxagpio_softc *sc = pxagpio_softc;
    469       1.1    scw 	u_int32_t rv, bit;
    470       1.1    scw 	u_int oldfn;
    471       1.1    scw 
    472       1.1    scw 	KDASSERT(gpio < GPIO_NPINS);
    473       1.1    scw 
    474       1.1    scw 	oldfn = pxa2x0_gpio_get_function(gpio);
    475       1.1    scw 
    476       1.1    scw 	if (GPIO_FN(fn) == GPIO_FN(oldfn) &&
    477       1.1    scw 	    GPIO_FN_IS_OUT(fn) == GPIO_FN_IS_OUT(oldfn)) {
    478       1.1    scw 		/*
    479       1.1    scw 		 * The pin's function is not changing.
    480       1.1    scw 		 * For Alternate Functions and GPIO input, we can just
    481       1.1    scw 		 * return now.
    482       1.1    scw 		 * For GPIO output pins, check the initial state is
    483       1.1    scw 		 * the same.
    484       1.1    scw 		 *
    485       1.1    scw 		 * Return 'fn' instead of 'oldfn' so the caller can
    486       1.1    scw 		 * reliably detect that we didn't change anything.
    487       1.1    scw 		 * (The initial state might be different for non-
    488       1.1    scw 		 * GPIO output pins).
    489       1.1    scw 		 */
    490       1.1    scw 		if (!GPIO_IS_GPIO_OUT(fn) ||
    491       1.1    scw 		    GPIO_FN_IS_SET(fn) == GPIO_FN_IS_SET(oldfn))
    492       1.1    scw 			return (fn);
    493       1.1    scw 	}
    494       1.1    scw 
    495       1.1    scw 	/*
    496       1.1    scw 	 * See section 4.1.3.7 of the PXA2x0 Developer's Manual for
    497       1.1    scw 	 * the correct procedure for changing GPIO pin functions.
    498       1.1    scw 	 */
    499       1.1    scw 
    500       1.1    scw 	bit = GPIO_BIT(gpio);
    501       1.1    scw 
    502       1.1    scw 	/*
    503       1.1    scw 	 * 1. Configure the correct set/clear state of the pin
    504       1.1    scw 	 */
    505       1.1    scw 	if (GPIO_FN_IS_SET(fn))
    506       1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GPSR0, gpio), bit);
    507       1.1    scw 	else
    508       1.1    scw 		pxagpio_reg_write(sc, GPIO_REG(GPIO_GPCR0, gpio), bit);
    509       1.1    scw 
    510       1.1    scw 	/*
    511       1.1    scw 	 * 2. Configure the pin as an input or output as appropriate
    512       1.1    scw 	 */
    513       1.1    scw 	rv = pxagpio_reg_read(sc, GPIO_REG(GPIO_GPDR0, gpio)) & ~bit;
    514       1.1    scw 	if (GPIO_FN_IS_OUT(fn))
    515       1.1    scw 		rv |= bit;
    516       1.1    scw 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GPDR0, gpio), rv);
    517       1.1    scw 
    518       1.1    scw 	/*
    519       1.1    scw 	 * 3. Configure the pin's function
    520       1.1    scw 	 */
    521       1.1    scw 	bit = GPIO_FN_MASK << GPIO_FN_SHIFT(gpio);
    522       1.1    scw 	fn = GPIO_FN(fn) << GPIO_FN_SHIFT(gpio);
    523       1.1    scw 	rv = pxagpio_reg_read(sc, GPIO_FN_REG(gpio)) & ~bit;
    524       1.1    scw 	pxagpio_reg_write(sc, GPIO_FN_REG(gpio), rv | fn);
    525       1.1    scw 
    526       1.1    scw 	return (oldfn);
    527       1.1    scw }
    528  1.5.20.1     ad 
    529  1.5.20.1     ad /*
    530  1.5.20.1     ad  * Quick function to read pin value
    531  1.5.20.1     ad  */
    532  1.5.20.1     ad int
    533  1.5.20.1     ad pxa2x0_gpio_get_bit(u_int gpio)
    534  1.5.20.1     ad {
    535  1.5.20.1     ad 	struct pxagpio_softc *sc = pxagpio_softc;
    536  1.5.20.1     ad 	int bit;
    537  1.5.20.1     ad 
    538  1.5.20.1     ad 	bit = GPIO_BIT(gpio);
    539  1.5.20.1     ad 	if (pxagpio_reg_read(sc, GPIO_REG(GPIO_GPLR0, gpio)) & bit)
    540  1.5.20.1     ad 		return 1;
    541  1.5.20.1     ad 	else
    542  1.5.20.1     ad 		return 0;
    543  1.5.20.1     ad }
    544  1.5.20.1     ad 
    545  1.5.20.1     ad /*
    546  1.5.20.1     ad  * Quick function to set pin to 1
    547  1.5.20.1     ad  */
    548  1.5.20.1     ad void
    549  1.5.20.1     ad pxa2x0_gpio_set_bit(u_int gpio)
    550  1.5.20.1     ad {
    551  1.5.20.1     ad 	struct pxagpio_softc *sc = pxagpio_softc;
    552  1.5.20.1     ad 	int bit;
    553  1.5.20.1     ad 
    554  1.5.20.1     ad 	bit = GPIO_BIT(gpio);
    555  1.5.20.1     ad 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GPSR0, gpio), bit);
    556  1.5.20.1     ad }
    557  1.5.20.1     ad 
    558  1.5.20.1     ad /*
    559  1.5.20.1     ad  * Quick function to set pin to 0
    560  1.5.20.1     ad  */
    561  1.5.20.1     ad void
    562  1.5.20.1     ad pxa2x0_gpio_clear_bit(u_int gpio)
    563  1.5.20.1     ad {
    564  1.5.20.1     ad 	struct pxagpio_softc *sc = pxagpio_softc;
    565  1.5.20.1     ad 	int bit;
    566  1.5.20.1     ad 
    567  1.5.20.1     ad 	bit = GPIO_BIT(gpio);
    568  1.5.20.1     ad 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GPCR0, gpio), bit);
    569  1.5.20.1     ad }
    570  1.5.20.1     ad 
    571  1.5.20.1     ad /*
    572  1.5.20.1     ad  * Quick function to change pin direction
    573  1.5.20.1     ad  */
    574  1.5.20.1     ad void
    575  1.5.20.1     ad pxa2x0_gpio_set_dir(u_int gpio, int dir)
    576  1.5.20.1     ad {
    577  1.5.20.1     ad 	struct pxagpio_softc *sc = pxagpio_softc;
    578  1.5.20.1     ad 	int bit;
    579  1.5.20.1     ad 	u_int32_t reg;
    580  1.5.20.1     ad 
    581  1.5.20.1     ad 	bit = GPIO_BIT(gpio);
    582  1.5.20.1     ad 
    583  1.5.20.1     ad 	reg = pxagpio_reg_read(sc, GPIO_REG(GPIO_GPDR0, gpio)) & ~bit;
    584  1.5.20.1     ad 	if (GPIO_FN_IS_OUT(dir))
    585  1.5.20.1     ad 		reg |= bit;
    586  1.5.20.1     ad 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GPDR0, gpio), reg);
    587  1.5.20.1     ad }
    588  1.5.20.1     ad 
    589  1.5.20.1     ad /*
    590  1.5.20.1     ad  * Quick function to clear interrupt status on a pin
    591  1.5.20.1     ad  * GPIO pins may be toggle in an interrupt and we dont want
    592  1.5.20.1     ad  * extra spurious interrupts to occur.
    593  1.5.20.1     ad  * Suppose this causes a slight race if a key is pressed while
    594  1.5.20.1     ad  * the interrupt handler is running. (yes this is for the keyboard driver)
    595  1.5.20.1     ad  */
    596  1.5.20.1     ad void
    597  1.5.20.1     ad pxa2x0_gpio_clear_intr(u_int gpio)
    598  1.5.20.1     ad {
    599  1.5.20.1     ad 	struct pxagpio_softc *sc = pxagpio_softc;
    600  1.5.20.1     ad 	int bit;
    601  1.5.20.1     ad 
    602  1.5.20.1     ad 	bit = GPIO_BIT(gpio);
    603  1.5.20.1     ad 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GEDR0, gpio), bit);
    604  1.5.20.1     ad }
    605  1.5.20.1     ad 
    606  1.5.20.1     ad /*
    607  1.5.20.1     ad  * Quick function to mask (disable) a GPIO interrupt
    608  1.5.20.1     ad  */
    609  1.5.20.1     ad void
    610  1.5.20.1     ad pxa2x0_gpio_intr_mask(void *v)
    611  1.5.20.1     ad {
    612  1.5.20.1     ad 	struct gpio_irq_handler *gh = (struct gpio_irq_handler *)v;
    613  1.5.20.1     ad 
    614  1.5.20.1     ad 	pxa2x0_gpio_set_intr_level(gh->gh_gpio, IPL_NONE);
    615  1.5.20.1     ad }
    616  1.5.20.1     ad 
    617  1.5.20.1     ad /*
    618  1.5.20.1     ad  * Quick function to unmask (enable) a GPIO interrupt
    619  1.5.20.1     ad  */
    620  1.5.20.1     ad void
    621  1.5.20.1     ad pxa2x0_gpio_intr_unmask(void *v)
    622  1.5.20.1     ad {
    623  1.5.20.1     ad 	struct gpio_irq_handler *gh = (struct gpio_irq_handler *)v;
    624  1.5.20.1     ad 
    625  1.5.20.1     ad 	pxa2x0_gpio_set_intr_level(gh->gh_gpio, gh->gh_level);
    626  1.5.20.1     ad }
    627  1.5.20.1     ad 
    628  1.5.20.1     ad /*
    629  1.5.20.1     ad  * Configure the edge sensitivity of interrupt pins
    630  1.5.20.1     ad  */
    631  1.5.20.1     ad void
    632  1.5.20.1     ad pxa2x0_gpio_set_intr_level(u_int gpio, int level)
    633  1.5.20.1     ad {
    634  1.5.20.1     ad 	struct pxagpio_softc *sc = pxagpio_softc;
    635  1.5.20.1     ad 	u_int32_t bit;
    636  1.5.20.1     ad 	u_int32_t gfer;
    637  1.5.20.1     ad 	u_int32_t grer;
    638  1.5.20.1     ad 	int s;
    639  1.5.20.1     ad 
    640  1.5.20.1     ad 	s = splhigh();
    641  1.5.20.1     ad 
    642  1.5.20.1     ad 	bit = GPIO_BIT(gpio);
    643  1.5.20.1     ad 	gfer = pxagpio_reg_read(sc, GPIO_REG(GPIO_GFER0, gpio));
    644  1.5.20.1     ad 	grer = pxagpio_reg_read(sc, GPIO_REG(GPIO_GRER0, gpio));
    645  1.5.20.1     ad 
    646  1.5.20.1     ad 	switch (level) {
    647  1.5.20.1     ad 	case IST_NONE:
    648  1.5.20.1     ad 		gfer &= ~bit;
    649  1.5.20.1     ad 		grer &= ~bit;
    650  1.5.20.1     ad 		break;
    651  1.5.20.1     ad 	case IST_EDGE_FALLING:
    652  1.5.20.1     ad 		gfer |= bit;
    653  1.5.20.1     ad 		grer &= ~bit;
    654  1.5.20.1     ad 		break;
    655  1.5.20.1     ad 	case IST_EDGE_RISING:
    656  1.5.20.1     ad 		gfer &= ~bit;
    657  1.5.20.1     ad 		grer |= bit;
    658  1.5.20.1     ad 		break;
    659  1.5.20.1     ad 	case IST_EDGE_BOTH:
    660  1.5.20.1     ad 		gfer |= bit;
    661  1.5.20.1     ad 		grer |= bit;
    662  1.5.20.1     ad 		break;
    663  1.5.20.1     ad 	default:
    664  1.5.20.1     ad 		panic("pxa2x0_gpio_set_intr_level: bad level: %d", level);
    665  1.5.20.1     ad 		break;
    666  1.5.20.1     ad 	}
    667  1.5.20.1     ad 
    668  1.5.20.1     ad 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GFER0, gpio), gfer);
    669  1.5.20.1     ad 	pxagpio_reg_write(sc, GPIO_REG(GPIO_GRER0, gpio), grer);
    670  1.5.20.1     ad 
    671  1.5.20.1     ad 	splx(s);
    672  1.5.20.1     ad }
    673