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