Home | History | Annotate | Line # | Download | only in dev
augpio.c revision 1.3
      1  1.3  gdamore /* $NetBSD: augpio.c,v 1.3 2006/02/18 23:21:06 gdamore Exp $ */
      2  1.1  gdamore 
      3  1.1  gdamore /*-
      4  1.1  gdamore  * Copyright (c) 2006 Itronix Inc.
      5  1.1  gdamore  * All rights reserved.
      6  1.1  gdamore  *
      7  1.1  gdamore  * Written by Garrett D'Amore for Itronix Inc.
      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 Itronix Inc. 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 ITRONIX INC. ``AS IS'' AND
     22  1.1  gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  1.1  gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.1  gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  1.1  gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  1.1  gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.1  gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  1.1  gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.1  gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.1  gdamore  * POSSIBILITY OF SUCH DAMAGE.
     32  1.1  gdamore  */
     33  1.1  gdamore 
     34  1.1  gdamore #include <sys/cdefs.h>
     35  1.3  gdamore __KERNEL_RCSID(0, "$NetBSD: augpio.c,v 1.3 2006/02/18 23:21:06 gdamore Exp $");
     36  1.1  gdamore 
     37  1.1  gdamore #include <sys/types.h>
     38  1.1  gdamore #include <sys/param.h>
     39  1.1  gdamore #include <sys/systm.h>
     40  1.1  gdamore #include <sys/errno.h>
     41  1.1  gdamore #include <sys/device.h>
     42  1.1  gdamore #include <sys/gpio.h>
     43  1.1  gdamore #include <sys/kernel.h>
     44  1.1  gdamore 
     45  1.1  gdamore #include <dev/gpio/gpiovar.h>
     46  1.1  gdamore 
     47  1.1  gdamore #include <machine/bus.h>
     48  1.1  gdamore #include <machine/cpu.h>
     49  1.1  gdamore 
     50  1.1  gdamore #include <mips/alchemy/include/aubusvar.h>
     51  1.1  gdamore #include <mips/alchemy/include/aureg.h>
     52  1.1  gdamore #include <mips/alchemy/dev/augpioreg.h>
     53  1.1  gdamore 
     54  1.1  gdamore struct augpio_softc {
     55  1.1  gdamore 	struct device			sc_dev;
     56  1.1  gdamore 	struct gpio_chipset_tag		sc_gc;
     57  1.1  gdamore 	gpio_pin_t			sc_pins[AUGPIO_NPINS];
     58  1.1  gdamore 	int				sc_npins;
     59  1.3  gdamore 	bus_space_tag_t			sc_bst;
     60  1.3  gdamore 	bus_space_handle_t		sc_bsh;
     61  1.3  gdamore 	int				sc_caps;
     62  1.3  gdamore 	const char 			*sc_name;
     63  1.3  gdamore 	int				(*sc_getctl)(void *, int);
     64  1.1  gdamore };
     65  1.1  gdamore 
     66  1.3  gdamore static int augpio_read(void *, int);
     67  1.3  gdamore static void augpio_write(void *, int, int);
     68  1.3  gdamore static void augpio_ctl(void *, int, int);
     69  1.3  gdamore static int augpio_getctl(void *, int);
     70  1.3  gdamore 
     71  1.3  gdamore static int augpio2_read(void *, int);
     72  1.3  gdamore static void augpio2_write(void *, int, int);
     73  1.3  gdamore static void augpio2_ctl(void *, int, int);
     74  1.3  gdamore static int augpio2_getctl(void *, int);
     75  1.3  gdamore 
     76  1.1  gdamore static int augpio_match(struct device *, struct cfdata *, void *);
     77  1.1  gdamore static void augpio_attach(struct device *, struct device *, void *);
     78  1.1  gdamore 
     79  1.1  gdamore CFATTACH_DECL(augpio, sizeof(struct augpio_softc),
     80  1.1  gdamore     augpio_match, augpio_attach, NULL, NULL);
     81  1.1  gdamore 
     82  1.1  gdamore #define	GETREG(x)	\
     83  1.1  gdamore 	(*(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(x))
     84  1.1  gdamore #define	PUTREG(x, v)	\
     85  1.1  gdamore 	((*(volatile uint32_t *)MIPS_PHYS_TO_KSEG1(x)) = (v))
     86  1.1  gdamore 
     87  1.1  gdamore static int augpio_found = 0;
     88  1.1  gdamore 
     89  1.1  gdamore int
     90  1.1  gdamore augpio_match(struct device *parent, struct cfdata *match, void *aux)
     91  1.1  gdamore {
     92  1.1  gdamore 	struct aubus_attach_args *aa = (struct aubus_attach_args *)aux;
     93  1.1  gdamore 
     94  1.1  gdamore 	if (strcmp(aa->aa_name, "augpio") != 0)
     95  1.1  gdamore 		return 0;
     96  1.1  gdamore 
     97  1.1  gdamore 	if (augpio_found)
     98  1.1  gdamore 		return 0;
     99  1.1  gdamore 
    100  1.1  gdamore 	return 1;
    101  1.1  gdamore }
    102  1.1  gdamore 
    103  1.1  gdamore void
    104  1.1  gdamore augpio_attach(struct device *parent, struct device *self, void *aux)
    105  1.1  gdamore {
    106  1.1  gdamore 	int	pin;
    107  1.1  gdamore 
    108  1.1  gdamore 	struct augpio_softc *sc = (struct augpio_softc *)self;
    109  1.1  gdamore 	struct aubus_attach_args *aa = aux;
    110  1.1  gdamore 	struct gpiobus_attach_args gba;
    111  1.1  gdamore 
    112  1.3  gdamore 	sc->sc_bst = aa->aa_st;
    113  1.3  gdamore 	sc->sc_npins = aa->aa_addrs[1];
    114  1.3  gdamore 	sc->sc_gc.gp_cookie = sc;
    115  1.1  gdamore 
    116  1.3  gdamore 	if (aa->aa_addrs[0] == GPIO_BASE) {
    117  1.2  gdamore 
    118  1.3  gdamore 		if (bus_space_map(sc->sc_bst, aa->aa_addrs[0],
    119  1.3  gdamore 			AUGPIO_SIZE, 0, &sc->sc_bsh) != 0) {
    120  1.3  gdamore 			printf(": cannot map registers!\n");
    121  1.3  gdamore 			return;
    122  1.1  gdamore 		}
    123  1.3  gdamore 		sc->sc_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
    124  1.3  gdamore 		    GPIO_PIN_TRISTATE;
    125  1.3  gdamore 		sc->sc_gc.gp_pin_read = augpio_read;
    126  1.3  gdamore 		sc->sc_gc.gp_pin_write = augpio_write;
    127  1.3  gdamore 		sc->sc_gc.gp_pin_ctl = augpio_ctl;
    128  1.3  gdamore 		sc->sc_getctl = augpio_getctl;
    129  1.3  gdamore 		sc->sc_name = "primary block";
    130  1.1  gdamore 
    131  1.1  gdamore 	} else if (aa->aa_addrs[0] == GPIO2_BASE) {
    132  1.2  gdamore 		/*
    133  1.2  gdamore 		 * We rely on firmware (or platform init code) to initialize
    134  1.2  gdamore 		 * the GPIO2 block.  We can't do it ourselves, because
    135  1.2  gdamore 		 * resetting the GPIO2 block can have nasty effects (e.g.
    136  1.2  gdamore 		 * reset PCI bus...)
    137  1.2  gdamore 		 */
    138  1.3  gdamore 		if (bus_space_map(sc->sc_bst, aa->aa_addrs[0],
    139  1.3  gdamore 			AUGPIO2_SIZE, 0, &sc->sc_bsh) != 0) {
    140  1.3  gdamore 			printf(": cannot map registers!\n");
    141  1.3  gdamore 			return;
    142  1.1  gdamore 		}
    143  1.3  gdamore 		sc->sc_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
    144  1.3  gdamore 		sc->sc_gc.gp_pin_read = augpio2_read;
    145  1.3  gdamore 		sc->sc_gc.gp_pin_write = augpio2_write;
    146  1.3  gdamore 		sc->sc_gc.gp_pin_ctl = augpio2_ctl;
    147  1.3  gdamore 		sc->sc_getctl = augpio2_getctl;
    148  1.3  gdamore 		sc->sc_name = "secondary block";
    149  1.1  gdamore 
    150  1.1  gdamore 	} else {
    151  1.3  gdamore 		printf(": unidentified block\n");
    152  1.3  gdamore 		return;
    153  1.1  gdamore 	}
    154  1.1  gdamore 
    155  1.3  gdamore 	for (pin = 0; pin < sc->sc_npins; pin++) {
    156  1.3  gdamore 		gpio_pin_t	*pp = &sc->sc_pins[pin];
    157  1.3  gdamore 
    158  1.3  gdamore 		pp->pin_num = pin;
    159  1.3  gdamore 		pp->pin_caps = sc->sc_caps;
    160  1.3  gdamore 		pp->pin_flags = sc->sc_getctl(sc, pin);
    161  1.3  gdamore 		pp->pin_state = sc->sc_gc.gp_pin_read(sc, pin);
    162  1.3  gdamore 	}
    163  1.1  gdamore 
    164  1.1  gdamore 	gba.gba_gc = &sc->sc_gc;
    165  1.1  gdamore 	gba.gba_pins = sc->sc_pins;
    166  1.1  gdamore 	gba.gba_npins = sc->sc_npins;
    167  1.1  gdamore 
    168  1.3  gdamore 	printf(": Alchemy GPIO, %s\n", sc->sc_name);
    169  1.1  gdamore 	config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
    170  1.1  gdamore }
    171  1.1  gdamore 
    172  1.1  gdamore int
    173  1.3  gdamore augpio_read(void *arg, int pin)
    174  1.1  gdamore {
    175  1.3  gdamore 	struct augpio_softc	*sc = arg;
    176  1.1  gdamore 
    177  1.3  gdamore 	pin = 1 << pin;
    178  1.1  gdamore 
    179  1.3  gdamore 	if (bus_space_read_4(sc->sc_bst, sc->sc_bsh, AUGPIO_PINSTATERD) & pin)
    180  1.3  gdamore 		return GPIO_PIN_HIGH;
    181  1.1  gdamore 	else
    182  1.3  gdamore 		return GPIO_PIN_LOW;
    183  1.1  gdamore }
    184  1.1  gdamore 
    185  1.1  gdamore void
    186  1.3  gdamore augpio_write(void *arg, int pin, int value)
    187  1.1  gdamore {
    188  1.3  gdamore 	struct augpio_softc	*sc = arg;
    189  1.1  gdamore 
    190  1.1  gdamore 	pin = 1 << pin;
    191  1.3  gdamore 	bus_space_write_4(sc->sc_bst, sc->sc_bsh,
    192  1.3  gdamore 	    value ? AUGPIO_OUTPUTSET : AUGPIO_OUTPUTCLR, pin);
    193  1.1  gdamore }
    194  1.1  gdamore 
    195  1.1  gdamore void
    196  1.3  gdamore augpio_ctl(void *arg, int pin, int flags)
    197  1.1  gdamore {
    198  1.3  gdamore 	struct augpio_softc	*sc = arg;
    199  1.3  gdamore 	bus_addr_t		reg;
    200  1.1  gdamore 
    201  1.1  gdamore 	pin = 1 << pin;
    202  1.3  gdamore 
    203  1.3  gdamore 	if (flags & (GPIO_PIN_TRISTATE|GPIO_PIN_INPUT)) {
    204  1.3  gdamore 		reg = AUGPIO_TRIOUTCLR;
    205  1.3  gdamore 	} else if (flags & GPIO_PIN_OUTPUT) {
    206  1.3  gdamore 		uint32_t		out;
    207  1.3  gdamore 		out = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AUGPIO_OUTPUTRD);
    208  1.3  gdamore 		reg = pin & out ? AUGPIO_OUTPUTSET : AUGPIO_OUTPUTCLR;
    209  1.1  gdamore 	} else {
    210  1.3  gdamore 		return;
    211  1.1  gdamore 	}
    212  1.3  gdamore 
    213  1.3  gdamore 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, reg, pin);
    214  1.1  gdamore }
    215  1.1  gdamore 
    216  1.1  gdamore int
    217  1.3  gdamore augpio_getctl(void *arg, int pin)
    218  1.1  gdamore {
    219  1.3  gdamore 	struct augpio_softc	*sc = arg;
    220  1.1  gdamore 
    221  1.3  gdamore 	if (bus_space_read_4(sc->sc_bst, sc->sc_bsh, AUGPIO_TRIOUTRD) & pin)
    222  1.3  gdamore 		return GPIO_PIN_OUTPUT;
    223  1.3  gdamore 	else
    224  1.3  gdamore 		return GPIO_PIN_INPUT;
    225  1.1  gdamore }
    226  1.1  gdamore 
    227  1.1  gdamore int
    228  1.3  gdamore augpio2_read(void *arg, int pin)
    229  1.1  gdamore {
    230  1.3  gdamore 	struct augpio_softc	*sc = arg;
    231  1.1  gdamore 
    232  1.1  gdamore 	pin = 1 << pin;
    233  1.3  gdamore 
    234  1.3  gdamore 	if (bus_space_read_4(sc->sc_bst, sc->sc_bsh, AUGPIO2_PINSTATE) & pin)
    235  1.3  gdamore 		return GPIO_PIN_HIGH;
    236  1.3  gdamore 	else
    237  1.3  gdamore 		return GPIO_PIN_LOW;
    238  1.1  gdamore }
    239  1.1  gdamore 
    240  1.1  gdamore void
    241  1.3  gdamore augpio2_write(void *arg, int pin, int value)
    242  1.1  gdamore {
    243  1.3  gdamore 	struct augpio_softc	*sc = arg;
    244  1.1  gdamore 
    245  1.1  gdamore 	pin = 1 << pin;
    246  1.1  gdamore 
    247  1.1  gdamore 	if (value) {
    248  1.3  gdamore 		pin = pin | (pin << 16);
    249  1.1  gdamore 	} else {
    250  1.3  gdamore 		pin = (pin << 16);
    251  1.1  gdamore 	}
    252  1.3  gdamore 
    253  1.3  gdamore 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AUGPIO2_OUTPUT, pin);
    254  1.1  gdamore }
    255  1.1  gdamore 
    256  1.3  gdamore void
    257  1.3  gdamore augpio2_ctl(void *arg, int pin, int flags)
    258  1.1  gdamore {
    259  1.3  gdamore 	struct augpio_softc	*sc = arg;
    260  1.1  gdamore 	uint32_t		dir;
    261  1.1  gdamore 
    262  1.1  gdamore 	pin = 1 << pin;
    263  1.1  gdamore 
    264  1.3  gdamore 	dir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AUGPIO2_DIR);
    265  1.1  gdamore 
    266  1.1  gdamore 	if (flags & GPIO_PIN_INPUT) {
    267  1.1  gdamore 		dir |= pin;
    268  1.1  gdamore 	} else if (flags & GPIO_PIN_OUTPUT) {
    269  1.1  gdamore 		dir &= ~pin;
    270  1.1  gdamore 	}
    271  1.3  gdamore 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, AUGPIO2_DIR, dir);
    272  1.3  gdamore }
    273  1.1  gdamore 
    274  1.3  gdamore int
    275  1.3  gdamore augpio2_getctl(void *arg, int pin)
    276  1.3  gdamore {
    277  1.3  gdamore 	struct augpio_softc	*sc = arg;
    278  1.3  gdamore 	uint32_t		dir;
    279  1.3  gdamore 
    280  1.3  gdamore 	pin = 1 << pin;
    281  1.3  gdamore 
    282  1.3  gdamore 	dir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AUGPIO2_DIR);
    283  1.3  gdamore 	if (dir & (uint32_t)pin) {
    284  1.3  gdamore 		return GPIO_PIN_OUTPUT;
    285  1.3  gdamore 	} else {
    286  1.3  gdamore 		return GPIO_PIN_INPUT;
    287  1.3  gdamore 	}
    288  1.1  gdamore }
    289  1.3  gdamore 
    290