Home | History | Annotate | Line # | Download | only in pci
gscpcib.c revision 1.5.18.3
      1  1.5.18.3  yamt /* $NetBSD: gscpcib.c,v 1.5.18.3 2006/12/30 20:46:11 yamt Exp $ */
      2  1.5.18.2  yamt /*	$OpenBSD: gscpcib.c,v 1.3 2004/10/05 19:02:33 grange Exp $	*/
      3  1.5.18.2  yamt /*
      4  1.5.18.2  yamt  * Copyright (c) 2004 Alexander Yurchenko <grange (at) openbsd.org>
      5  1.5.18.2  yamt  *
      6  1.5.18.2  yamt  * Permission to use, copy, modify, and distribute this software for any
      7  1.5.18.2  yamt  * purpose with or without fee is hereby granted, provided that the above
      8  1.5.18.2  yamt  * copyright notice and this permission notice appear in all copies.
      9  1.5.18.2  yamt  *
     10  1.5.18.2  yamt  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.5.18.2  yamt  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.5.18.2  yamt  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.5.18.2  yamt  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.5.18.2  yamt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  1.5.18.2  yamt  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  1.5.18.2  yamt  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.5.18.2  yamt  */
     18  1.5.18.2  yamt 
     19  1.5.18.2  yamt /*
     20  1.5.18.2  yamt  * Special driver for the National Semiconductor Geode SC1100 PCI-ISA bridge
     21  1.5.18.2  yamt  * that attaches instead of pcib(4). In addition to the core pcib(4)
     22  1.5.18.2  yamt  * functionality this driver provides support for the GPIO interface.
     23  1.5.18.2  yamt  */
     24  1.5.18.2  yamt 
     25  1.5.18.2  yamt #include <sys/param.h>
     26  1.5.18.2  yamt #include <sys/systm.h>
     27  1.5.18.2  yamt #include <sys/device.h>
     28  1.5.18.2  yamt #include <sys/gpio.h>
     29  1.5.18.2  yamt #include <sys/kernel.h>
     30  1.5.18.2  yamt 
     31  1.5.18.2  yamt #include <machine/bus.h>
     32  1.5.18.2  yamt 
     33  1.5.18.2  yamt #include <dev/pci/pcireg.h>
     34  1.5.18.2  yamt #include <dev/pci/pcivar.h>
     35  1.5.18.2  yamt #include <dev/pci/pcidevs.h>
     36  1.5.18.2  yamt 
     37  1.5.18.2  yamt #include <dev/gpio/gpiovar.h>
     38  1.5.18.2  yamt 
     39  1.5.18.2  yamt #include <i386/pci/gscpcibreg.h>
     40  1.5.18.2  yamt 
     41  1.5.18.2  yamt struct gscpcib_softc {
     42  1.5.18.2  yamt 	struct device sc_dev;
     43  1.5.18.2  yamt 
     44  1.5.18.2  yamt 	/* GPIO interface */
     45  1.5.18.2  yamt 	bus_space_tag_t sc_gpio_iot;
     46  1.5.18.2  yamt 	bus_space_handle_t sc_gpio_ioh;
     47  1.5.18.2  yamt 	struct gpio_chipset_tag sc_gpio_gc;
     48  1.5.18.2  yamt 	gpio_pin_t sc_gpio_pins[GSCGPIO_NPINS];
     49  1.5.18.2  yamt };
     50  1.5.18.2  yamt 
     51  1.5.18.2  yamt int	gscpcib_match(struct device *, struct cfdata *, void *);
     52  1.5.18.2  yamt void	gscpcib_attach(struct device *, struct device *, void *);
     53  1.5.18.2  yamt 
     54  1.5.18.2  yamt int	gscpcib_gpio_pin_read(void *, int);
     55  1.5.18.2  yamt void	gscpcib_gpio_pin_write(void *, int, int);
     56  1.5.18.2  yamt void	gscpcib_gpio_pin_ctl(void *, int, int);
     57  1.5.18.2  yamt 
     58  1.5.18.2  yamt /* arch/i386/pci/pcib.c */
     59  1.5.18.2  yamt void    pcibattach(struct device *, struct device *, void *);
     60  1.5.18.2  yamt 
     61  1.5.18.2  yamt CFATTACH_DECL(gscpcib, sizeof(struct gscpcib_softc),
     62  1.5.18.2  yamt 	gscpcib_match, gscpcib_attach, NULL, NULL);
     63  1.5.18.2  yamt 
     64  1.5.18.2  yamt extern struct cfdriver gscpcib_cd;
     65  1.5.18.2  yamt 
     66  1.5.18.2  yamt int
     67  1.5.18.3  yamt gscpcib_match(struct device *parent, struct cfdata *match,
     68  1.5.18.3  yamt     void *aux)
     69  1.5.18.2  yamt {
     70  1.5.18.2  yamt 	struct pci_attach_args *pa = aux;
     71  1.5.18.2  yamt 
     72  1.5.18.2  yamt 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
     73  1.5.18.2  yamt 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
     74  1.5.18.2  yamt 		return (0);
     75  1.5.18.2  yamt 
     76  1.5.18.2  yamt 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
     77  1.5.18.2  yamt 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SC1100_ISA)
     78  1.5.18.2  yamt 		return (2);	/* supersede pcib(4) */
     79  1.5.18.2  yamt 
     80  1.5.18.2  yamt 	return (0);
     81  1.5.18.2  yamt }
     82  1.5.18.2  yamt 
     83  1.5.18.2  yamt void
     84  1.5.18.2  yamt gscpcib_attach(struct device *parent, struct device *self, void *aux)
     85  1.5.18.2  yamt {
     86  1.5.18.2  yamt 	struct gscpcib_softc *sc = (struct gscpcib_softc *)self;
     87  1.5.18.2  yamt 	struct pci_attach_args *pa = aux;
     88  1.5.18.2  yamt 	struct gpiobus_attach_args gba;
     89  1.5.18.2  yamt 	pcireg_t gpiobase;
     90  1.5.18.2  yamt 	int i;
     91  1.5.18.2  yamt 	int gpio_present = 0;
     92  1.5.18.2  yamt 
     93  1.5.18.2  yamt 	/* Map GPIO I/O space */
     94  1.5.18.2  yamt 	gpiobase = pci_conf_read(pa->pa_pc, pa->pa_tag, GSCGPIO_BASE);
     95  1.5.18.2  yamt 	sc->sc_gpio_iot = pa->pa_iot;
     96  1.5.18.2  yamt 	if (bus_space_map(sc->sc_gpio_iot, PCI_MAPREG_IO_ADDR(gpiobase),
     97  1.5.18.2  yamt 	    GSCGPIO_SIZE, 0, &sc->sc_gpio_ioh)) {
     98  1.5.18.2  yamt 		printf(": failed to map GPIO I/O space");
     99  1.5.18.2  yamt 		goto corepcib;
    100  1.5.18.2  yamt 	}
    101  1.5.18.2  yamt 
    102  1.5.18.2  yamt 	/* Initialize pins array */
    103  1.5.18.2  yamt 	for (i = 0; i < GSCGPIO_NPINS; i++) {
    104  1.5.18.2  yamt 		sc->sc_gpio_pins[i].pin_num = i;
    105  1.5.18.2  yamt 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
    106  1.5.18.2  yamt 		    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
    107  1.5.18.2  yamt 		    GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
    108  1.5.18.2  yamt 		    GPIO_PIN_PULLUP;
    109  1.5.18.2  yamt 
    110  1.5.18.2  yamt 		/* safe defaults */
    111  1.5.18.2  yamt 		sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
    112  1.5.18.2  yamt 		sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
    113  1.5.18.2  yamt 		gscpcib_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
    114  1.5.18.2  yamt 		gscpcib_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
    115  1.5.18.2  yamt 	}
    116  1.5.18.2  yamt 
    117  1.5.18.2  yamt 	/* Create controller tag */
    118  1.5.18.2  yamt 	sc->sc_gpio_gc.gp_cookie = sc;
    119  1.5.18.2  yamt 	sc->sc_gpio_gc.gp_pin_read = gscpcib_gpio_pin_read;
    120  1.5.18.2  yamt 	sc->sc_gpio_gc.gp_pin_write = gscpcib_gpio_pin_write;
    121  1.5.18.2  yamt 	sc->sc_gpio_gc.gp_pin_ctl = gscpcib_gpio_pin_ctl;
    122  1.5.18.2  yamt 
    123  1.5.18.2  yamt 	gba.gba_gc = &sc->sc_gpio_gc;
    124  1.5.18.2  yamt 	gba.gba_pins = sc->sc_gpio_pins;
    125  1.5.18.2  yamt 	gba.gba_npins = GSCGPIO_NPINS;
    126  1.5.18.2  yamt 
    127  1.5.18.2  yamt 	gpio_present = 1;
    128  1.5.18.2  yamt 
    129  1.5.18.2  yamt corepcib:
    130  1.5.18.2  yamt 	/* Provide core pcib(4) functionality */
    131  1.5.18.2  yamt 	pcibattach(parent, self, aux);
    132  1.5.18.2  yamt 
    133  1.5.18.2  yamt 	/* Attach GPIO framework */
    134  1.5.18.2  yamt 	if (gpio_present)
    135  1.5.18.2  yamt 		config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
    136  1.5.18.2  yamt }
    137  1.5.18.2  yamt 
    138  1.5.18.2  yamt static inline void
    139  1.5.18.2  yamt gscpcib_gpio_pin_select(struct gscpcib_softc *sc, int pin)
    140  1.5.18.2  yamt {
    141  1.5.18.2  yamt 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GSCGPIO_SEL, pin);
    142  1.5.18.2  yamt }
    143  1.5.18.2  yamt 
    144  1.5.18.2  yamt int
    145  1.5.18.2  yamt gscpcib_gpio_pin_read(void *arg, int pin)
    146  1.5.18.2  yamt {
    147  1.5.18.2  yamt 	struct gscpcib_softc *sc = arg;
    148  1.5.18.2  yamt 	int reg, shift;
    149  1.5.18.2  yamt 	uint32_t data;
    150  1.5.18.2  yamt 
    151  1.5.18.2  yamt 	reg = (pin < 32 ? GSCGPIO_GPDI0 : GSCGPIO_GPDI1);
    152  1.5.18.2  yamt 	shift = pin % 32;
    153  1.5.18.2  yamt 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
    154  1.5.18.2  yamt 
    155  1.5.18.2  yamt 	return ((data >> shift) & 0x1);
    156  1.5.18.2  yamt }
    157  1.5.18.2  yamt 
    158  1.5.18.2  yamt void
    159  1.5.18.2  yamt gscpcib_gpio_pin_write(void *arg, int pin, int value)
    160  1.5.18.2  yamt {
    161  1.5.18.2  yamt 	struct gscpcib_softc *sc = arg;
    162  1.5.18.2  yamt 	int reg, shift;
    163  1.5.18.2  yamt 	uint32_t data;
    164  1.5.18.2  yamt 
    165  1.5.18.2  yamt 	reg = (pin < 32 ? GSCGPIO_GPDO0 : GSCGPIO_GPDO1);
    166  1.5.18.2  yamt 	shift = pin % 32;
    167  1.5.18.2  yamt 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
    168  1.5.18.2  yamt 	if (value == 0)
    169  1.5.18.2  yamt 		data &= ~(1 << shift);
    170  1.5.18.2  yamt 	else if (value == 1)
    171  1.5.18.2  yamt 		data |= (1 << shift);
    172  1.5.18.2  yamt 
    173  1.5.18.2  yamt 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg, data);
    174  1.5.18.2  yamt }
    175  1.5.18.2  yamt 
    176  1.5.18.2  yamt void
    177  1.5.18.2  yamt gscpcib_gpio_pin_ctl(void *arg, int pin, int flags)
    178  1.5.18.2  yamt {
    179  1.5.18.2  yamt 	struct gscpcib_softc *sc = arg;
    180  1.5.18.2  yamt 	uint32_t conf;
    181  1.5.18.2  yamt 
    182  1.5.18.2  yamt 	gscpcib_gpio_pin_select(sc, pin);
    183  1.5.18.2  yamt 	conf = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    184  1.5.18.2  yamt 	    GSCGPIO_CONF);
    185  1.5.18.2  yamt 
    186  1.5.18.2  yamt 	conf &= ~(GSCGPIO_CONF_OUTPUTEN | GSCGPIO_CONF_PUSHPULL |
    187  1.5.18.2  yamt 	    GSCGPIO_CONF_PULLUP);
    188  1.5.18.2  yamt 	if ((flags & GPIO_PIN_TRISTATE) == 0)
    189  1.5.18.2  yamt 		conf |= GSCGPIO_CONF_OUTPUTEN;
    190  1.5.18.2  yamt 	if (flags & GPIO_PIN_PUSHPULL)
    191  1.5.18.2  yamt 		conf |= GSCGPIO_CONF_PUSHPULL;
    192  1.5.18.2  yamt 	if (flags & GPIO_PIN_PULLUP)
    193  1.5.18.2  yamt 		conf |= GSCGPIO_CONF_PULLUP;
    194  1.5.18.2  yamt 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
    195  1.5.18.2  yamt 	    GSCGPIO_CONF, conf);
    196  1.5.18.2  yamt }
    197