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