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