Home | History | Annotate | Line # | Download | only in dev
argpio.c revision 1.1
      1  1.1  gdamore /* $NetBSD: argpio.c,v 1.1 2006/07/07 22:03:19 gdamore Exp $ */
      2  1.1  gdamore 
      3  1.1  gdamore /*-
      4  1.1  gdamore  * Copyright (c) 2006 Garrett D'Amore
      5  1.1  gdamore  * All rights reserved.
      6  1.1  gdamore  *
      7  1.1  gdamore  * Written by Garrett D'Amore.
      8  1.1  gdamore  *
      9  1.1  gdamore  * Redistribution and use in source and binary forms, with or without
     10  1.1  gdamore  * modification, are permitted provided that the following conditions
     11  1.1  gdamore  * are met:
     12  1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     13  1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     14  1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     17  1.1  gdamore  * 3. The name of the author may not be used to endorse
     18  1.1  gdamore  *    or promote products derived from this software without specific
     19  1.1  gdamore  *    prior written permission.
     20  1.1  gdamore  *
     21  1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  1.1  gdamore  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  1.1  gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  1.1  gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  gdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.1  gdamore  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.1  gdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.1  gdamore  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.1  gdamore  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     31  1.1  gdamore  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  gdamore  */
     33  1.1  gdamore 
     34  1.1  gdamore #include <sys/cdefs.h>
     35  1.1  gdamore __KERNEL_RCSID(0, "$NetBSD: argpio.c,v 1.1 2006/07/07 22:03:19 gdamore Exp $");
     36  1.1  gdamore 
     37  1.1  gdamore #include <sys/param.h>
     38  1.1  gdamore #include <sys/conf.h>
     39  1.1  gdamore #include <sys/kernel.h>
     40  1.1  gdamore #include <sys/types.h>
     41  1.1  gdamore #include <sys/device.h>
     42  1.1  gdamore #include <sys/gpio.h>
     43  1.1  gdamore 
     44  1.1  gdamore #include <machine/bus.h>
     45  1.1  gdamore #include <machine/intr.h>
     46  1.1  gdamore 
     47  1.1  gdamore #include <mips/atheros/include/ar531xreg.h>
     48  1.1  gdamore #include <mips/atheros/include/ar531xvar.h>
     49  1.1  gdamore #include <mips/atheros/include/arbusvar.h>
     50  1.1  gdamore 
     51  1.1  gdamore #include <contrib/dev/ath/ah_soc.h>	/* this should really move */
     52  1.1  gdamore 
     53  1.1  gdamore #include <dev/gpio/gpiovar.h>
     54  1.1  gdamore #include <dev/sysmon/sysmonvar.h>
     55  1.1  gdamore #include <dev/sysmon/sysmon_taskq.h>
     56  1.1  gdamore 
     57  1.1  gdamore #include <mips/atheros/dev/argpioreg.h>
     58  1.1  gdamore 
     59  1.1  gdamore /*
     60  1.1  gdamore  * General Plan:
     61  1.1  gdamore  *
     62  1.1  gdamore  * Register GPIOs for all pins that are _not_ associated with the reset
     63  1.1  gdamore  * pin.  (Possibly also not the sytem LED.)
     64  1.1  gdamore  */
     65  1.1  gdamore 
     66  1.1  gdamore struct argpio_softc {
     67  1.1  gdamore 	struct device		sc_dev;
     68  1.1  gdamore 	struct gpio_chipset_tag	sc_gc;
     69  1.1  gdamore 	gpio_pin_t		sc_pins[ARGPIO_NPINS];
     70  1.1  gdamore 	int			sc_npins;
     71  1.1  gdamore 	bus_space_tag_t		sc_st;
     72  1.1  gdamore 	bus_space_handle_t	sc_sh;
     73  1.1  gdamore 	bus_size_t		sc_size;
     74  1.1  gdamore 	int			sc_caps;
     75  1.1  gdamore 	struct sysmon_pswitch	sc_resetbtn;
     76  1.1  gdamore 	void			*sc_ih;
     77  1.1  gdamore 	int			sc_rstpin;
     78  1.1  gdamore };
     79  1.1  gdamore 
     80  1.1  gdamore static int argpio_match(struct device *, struct cfdata *, void *);
     81  1.1  gdamore static void argpio_attach(struct device *, struct device *, void *);
     82  1.1  gdamore static int argpio_intr(void *);
     83  1.1  gdamore static void argpio_reset_pressed(void *);
     84  1.1  gdamore static void argpio_ctl(void *, int, int);
     85  1.1  gdamore static void argpio_write(void *, int, int);
     86  1.1  gdamore static int argpio_read(void *, int);
     87  1.1  gdamore 
     88  1.1  gdamore CFATTACH_DECL(argpio, sizeof (struct argpio_softc), argpio_match,
     89  1.1  gdamore     argpio_attach, NULL, NULL);
     90  1.1  gdamore 
     91  1.1  gdamore #define	INPUT(pin)	(1 << (pin))		/* input bit */
     92  1.1  gdamore #define	INTR(pin)	(1 << ((pin) + 8))	/* interrupt bit */
     93  1.1  gdamore #define	SERIAL(pin)	(1 << ((pin) + 16))	/* serial mux bit */
     94  1.1  gdamore 
     95  1.1  gdamore #define	GETREG(sc, o)		bus_space_read_4(sc->sc_st, sc->sc_sh, o)
     96  1.1  gdamore #define	PUTREG(sc, o, v)	bus_space_write_4(sc->sc_st, sc->sc_sh, o, v)
     97  1.1  gdamore #define	FLUSH(sc)		bus_space_barrier(sc->sc_st, sc->sc_sh, \
     98  1.1  gdamore 				0, 12, BUS_SPACE_BARRIER_SYNC)
     99  1.1  gdamore 
    100  1.1  gdamore int
    101  1.1  gdamore argpio_match(struct device *parent, struct cfdata *match, void *aux)
    102  1.1  gdamore {
    103  1.1  gdamore 	struct arbus_attach_args *aa = aux;
    104  1.1  gdamore 
    105  1.1  gdamore 	return ((strcmp(aa->aa_name, "argpio") == 0) ? 1 : 0);
    106  1.1  gdamore }
    107  1.1  gdamore 
    108  1.1  gdamore void
    109  1.1  gdamore argpio_attach(struct device *parent, struct device *self, void *aux)
    110  1.1  gdamore {
    111  1.1  gdamore 	struct argpio_softc *sc = (struct argpio_softc *)self;
    112  1.1  gdamore 	struct arbus_attach_args *aa = aux;
    113  1.1  gdamore 	struct gpiobus_attach_args gba;
    114  1.1  gdamore 	const struct ar531x_boarddata *board;
    115  1.1  gdamore 	int rstpin = -1, ledpin = -1, i;
    116  1.1  gdamore 	uint32_t reg;
    117  1.1  gdamore 
    118  1.1  gdamore 	sc->sc_st = aa->aa_bst;
    119  1.1  gdamore 	sc->sc_npins = ARGPIO_NPINS;
    120  1.1  gdamore 	sc->sc_size = aa->aa_size;
    121  1.1  gdamore 
    122  1.1  gdamore 	if (bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0,
    123  1.1  gdamore 		&sc->sc_sh) != 0) {
    124  1.1  gdamore 		printf(": unable to map registers!\n");
    125  1.1  gdamore 		return;
    126  1.1  gdamore 	}
    127  1.1  gdamore 
    128  1.1  gdamore 	sc->sc_gc.gp_cookie = sc;
    129  1.1  gdamore 	sc->sc_gc.gp_pin_read = argpio_read;
    130  1.1  gdamore 	sc->sc_gc.gp_pin_write = argpio_write;
    131  1.1  gdamore 	sc->sc_gc.gp_pin_ctl = argpio_ctl;
    132  1.1  gdamore 
    133  1.1  gdamore 	board = ar531x_board_info();
    134  1.1  gdamore 
    135  1.1  gdamore 	aprint_normal(": Atheros AR531X GPIO");
    136  1.1  gdamore 	if (board->config & BD_RSTFACTORY) {
    137  1.1  gdamore 		rstpin = board->resetConfigGpio;
    138  1.1  gdamore 		aprint_normal(", reset button pin %d", rstpin);
    139  1.1  gdamore 		sc->sc_rstpin = rstpin;
    140  1.1  gdamore 	}
    141  1.1  gdamore 	if (board->config & BD_SYSLED) {
    142  1.1  gdamore 		ledpin = board->sysLedGpio;
    143  1.1  gdamore 		aprint_normal(", system led pin %d", ledpin);
    144  1.1  gdamore 	}
    145  1.1  gdamore 	printf("\n");
    146  1.1  gdamore 
    147  1.1  gdamore 	if ((board->config & BD_RSTFACTORY) && (aa->aa_irq > -1)) {
    148  1.1  gdamore 		sc->sc_ih = arbus_intr_establish(aa->aa_irq, argpio_intr, sc);
    149  1.1  gdamore 		if (sc->sc_ih == NULL) {
    150  1.1  gdamore 			aprint_error("%s: couldn't establish interrupt\n",
    151  1.1  gdamore 			    sc->sc_dev.dv_xname);
    152  1.1  gdamore 		}
    153  1.1  gdamore 
    154  1.1  gdamore 	}
    155  1.1  gdamore 
    156  1.1  gdamore 	if (sc->sc_ih) {
    157  1.1  gdamore 		sysmon_task_queue_init();
    158  1.1  gdamore 
    159  1.1  gdamore 		sc->sc_resetbtn.smpsw_name = sc->sc_dev.dv_xname;
    160  1.1  gdamore 		sc->sc_resetbtn.smpsw_type = PSWITCH_TYPE_RESET;
    161  1.1  gdamore 		if (sysmon_pswitch_register(&sc->sc_resetbtn) != 0)
    162  1.1  gdamore 			printf("%s: unable to register reset button\n",
    163  1.1  gdamore 			    sc->sc_dev.dv_xname);
    164  1.1  gdamore 	}
    165  1.1  gdamore 
    166  1.1  gdamore 	reg = GETREG(sc, GPIO_CR);
    167  1.1  gdamore 
    168  1.1  gdamore 	for (i = 0; i < sc->sc_npins; i++) {
    169  1.1  gdamore 		gpio_pin_t	*pp;
    170  1.1  gdamore 
    171  1.1  gdamore 		pp = &sc->sc_pins[i];
    172  1.1  gdamore 
    173  1.1  gdamore 		if (i == rstpin) {
    174  1.1  gdamore 			/* configure as interrupt for reset */
    175  1.1  gdamore 			pp->pin_caps = GPIO_PIN_INPUT;
    176  1.1  gdamore 			reg &= ~SERIAL(i);
    177  1.1  gdamore 			reg |= INPUT(i);
    178  1.1  gdamore 			/* only if we were able to set up the handler, tho' */
    179  1.1  gdamore 			if (sc->sc_ih != NULL)
    180  1.1  gdamore 				reg |= INTR(i);
    181  1.1  gdamore 
    182  1.1  gdamore 		} else if (i == ledpin) {
    183  1.1  gdamore 			/* configure as output for LED */
    184  1.1  gdamore 			pp->pin_caps = GPIO_PIN_OUTPUT;
    185  1.1  gdamore 			reg &= ~SERIAL(i);
    186  1.1  gdamore 			reg &= ~INPUT(i);
    187  1.1  gdamore 			reg &= ~INTR(i);
    188  1.1  gdamore 
    189  1.1  gdamore 		} else {
    190  1.1  gdamore 			if (reg & SERIAL(i)) {
    191  1.1  gdamore 				/* pin multiplexed with serial bit */
    192  1.1  gdamore 				pp->pin_caps = 0;
    193  1.1  gdamore 			} else {
    194  1.1  gdamore 				pp->pin_caps = GPIO_PIN_INPUT |
    195  1.1  gdamore 				    GPIO_PIN_OUTPUT;
    196  1.1  gdamore 			}
    197  1.1  gdamore 		}
    198  1.1  gdamore 	}
    199  1.1  gdamore 
    200  1.1  gdamore 	PUTREG(sc, GPIO_CR, reg);
    201  1.1  gdamore 	FLUSH(sc);
    202  1.1  gdamore 
    203  1.1  gdamore 	gba.gba_gc = &sc->sc_gc;
    204  1.1  gdamore 	gba.gba_pins = sc->sc_pins;
    205  1.1  gdamore 	gba.gba_npins = sc->sc_npins;
    206  1.1  gdamore 	config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
    207  1.1  gdamore }
    208  1.1  gdamore 
    209  1.1  gdamore void
    210  1.1  gdamore argpio_ctl(void *arg, int pin, int flags)
    211  1.1  gdamore {
    212  1.1  gdamore 	struct argpio_softc	*sc = arg;
    213  1.1  gdamore 	uint32_t		reg;
    214  1.1  gdamore 
    215  1.1  gdamore 	reg = GETREG(sc, GPIO_CR);
    216  1.1  gdamore 	if (reg & (SERIAL(pin) | INTR(pin))) {
    217  1.1  gdamore 		printf("pin %d cannot be changed!\n", pin);
    218  1.1  gdamore 		/* don't allow changes to these bits */
    219  1.1  gdamore 		return;
    220  1.1  gdamore 	}
    221  1.1  gdamore 	if (flags & GPIO_PIN_INPUT) {
    222  1.1  gdamore 		reg |= INPUT(pin);
    223  1.1  gdamore 	} else {
    224  1.1  gdamore 		reg &= ~INPUT(pin);
    225  1.1  gdamore 	}
    226  1.1  gdamore 
    227  1.1  gdamore 	PUTREG(sc, GPIO_CR, reg);
    228  1.1  gdamore 	FLUSH(sc);
    229  1.1  gdamore }
    230  1.1  gdamore 
    231  1.1  gdamore void
    232  1.1  gdamore argpio_write(void *arg, int pin, int value)
    233  1.1  gdamore {
    234  1.1  gdamore 	struct argpio_softc	*sc = arg;
    235  1.1  gdamore 	uint32_t		reg;
    236  1.1  gdamore 
    237  1.1  gdamore 	reg = GETREG(sc, GPIO_DO);
    238  1.1  gdamore 	if (value)
    239  1.1  gdamore 		reg &= ~(1 << pin);
    240  1.1  gdamore 	else
    241  1.1  gdamore 		reg |= (1 << pin);
    242  1.1  gdamore 	PUTREG(sc, GPIO_DO, reg);
    243  1.1  gdamore 	FLUSH(sc);
    244  1.1  gdamore }
    245  1.1  gdamore 
    246  1.1  gdamore int
    247  1.1  gdamore argpio_read(void *arg, int pin)
    248  1.1  gdamore {
    249  1.1  gdamore 	struct argpio_softc	*sc = arg;
    250  1.1  gdamore 
    251  1.1  gdamore 	return ((GETREG(sc, GPIO_DI) & (1 << pin)) ?
    252  1.1  gdamore 	    GPIO_PIN_HIGH : GPIO_PIN_LOW);
    253  1.1  gdamore }
    254  1.1  gdamore 
    255  1.1  gdamore void
    256  1.1  gdamore argpio_reset_pressed(void *arg)
    257  1.1  gdamore {
    258  1.1  gdamore 	struct argpio_softc	*sc = arg;
    259  1.1  gdamore 	int			x;
    260  1.1  gdamore 
    261  1.1  gdamore 	sysmon_pswitch_event(&sc->sc_resetbtn, PSWITCH_EVENT_PRESSED);
    262  1.1  gdamore 
    263  1.1  gdamore 	/* reenable the interrupt */
    264  1.1  gdamore 	x = splhigh();
    265  1.1  gdamore 	PUTREG(sc, GPIO_CR,
    266  1.1  gdamore 	    GETREG(sc, GPIO_CR) | INTR(sc->sc_rstpin));
    267  1.1  gdamore 	splx(x);
    268  1.1  gdamore }
    269  1.1  gdamore 
    270  1.1  gdamore int
    271  1.1  gdamore argpio_intr(void  *arg)
    272  1.1  gdamore {
    273  1.1  gdamore 	struct argpio_softc	*sc = arg;
    274  1.1  gdamore 
    275  1.1  gdamore 	if (sc->sc_rstpin < 0)
    276  1.1  gdamore 		return 0;
    277  1.1  gdamore 
    278  1.1  gdamore 	/* this is an edge triggered interrupt, so disable it for now */
    279  1.1  gdamore 	PUTREG(sc, GPIO_CR, GETREG(sc, GPIO_CR) & ~INTR(sc->sc_rstpin));
    280  1.1  gdamore 
    281  1.1  gdamore 	/* no other interrupt on this, so we have to claim it */
    282  1.1  gdamore 	sysmon_task_queue_sched(0, argpio_reset_pressed, sc);
    283  1.1  gdamore 
    284  1.1  gdamore 	return 1;
    285  1.1  gdamore }
    286