xref: /netbsd-src/sys/arch/i386/pci/gscpcib.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: gscpcib.c,v 1.20 2021/08/07 16:18:55 thorpej Exp $	*/
277750350Sjmcneill /*	$OpenBSD: gscpcib.c,v 1.3 2004/10/05 19:02:33 grange Exp $	*/
377750350Sjmcneill /*
477750350Sjmcneill  * Copyright (c) 2004 Alexander Yurchenko <grange@openbsd.org>
577750350Sjmcneill  *
677750350Sjmcneill  * Permission to use, copy, modify, and distribute this software for any
777750350Sjmcneill  * purpose with or without fee is hereby granted, provided that the above
877750350Sjmcneill  * copyright notice and this permission notice appear in all copies.
977750350Sjmcneill  *
1077750350Sjmcneill  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1177750350Sjmcneill  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1277750350Sjmcneill  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1377750350Sjmcneill  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1477750350Sjmcneill  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1577750350Sjmcneill  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1677750350Sjmcneill  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1777750350Sjmcneill  */
1877750350Sjmcneill 
1977750350Sjmcneill /*
2077750350Sjmcneill  * Special driver for the National Semiconductor Geode SC1100 PCI-ISA bridge
2177750350Sjmcneill  * that attaches instead of pcib(4). In addition to the core pcib(4)
2277750350Sjmcneill  * functionality this driver provides support for the GPIO interface.
2377750350Sjmcneill  */
2477750350Sjmcneill 
25ceed9c72Slukem #include <sys/cdefs.h>
26*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: gscpcib.c,v 1.20 2021/08/07 16:18:55 thorpej Exp $");
27ceed9c72Slukem 
2877750350Sjmcneill #include <sys/param.h>
2977750350Sjmcneill #include <sys/systm.h>
3077750350Sjmcneill #include <sys/device.h>
3177750350Sjmcneill #include <sys/gpio.h>
3277750350Sjmcneill #include <sys/kernel.h>
3377750350Sjmcneill 
34f5b064eeSdyoung #include <sys/bus.h>
3577750350Sjmcneill 
3677750350Sjmcneill #include <dev/pci/pcireg.h>
3777750350Sjmcneill #include <dev/pci/pcivar.h>
3877750350Sjmcneill #include <dev/pci/pcidevs.h>
3977750350Sjmcneill 
4077750350Sjmcneill #include <dev/gpio/gpiovar.h>
4177750350Sjmcneill 
4277750350Sjmcneill #include <i386/pci/gscpcibreg.h>
437f90e762Smbalmer #include <arch/x86/pci/pcibvar.h>
4477750350Sjmcneill 
454d3e3d44Smbalmer #include "gpio.h"
464d3e3d44Smbalmer 
4777750350Sjmcneill struct gscpcib_softc {
487f90e762Smbalmer 	struct pcib_softc sc_pcib;
497f90e762Smbalmer 
508d9f58eaSdyoung 	bool sc_gpio_present;
51e02c33d4Sdyoung 	device_t sc_gpiobus;
528d9f58eaSdyoung 
5377750350Sjmcneill 	/* GPIO interface */
5477750350Sjmcneill 	bus_space_tag_t sc_gpio_iot;
5577750350Sjmcneill 	bus_space_handle_t sc_gpio_ioh;
5677750350Sjmcneill 	struct gpio_chipset_tag sc_gpio_gc;
5777750350Sjmcneill 	gpio_pin_t sc_gpio_pins[GSCGPIO_NPINS];
5877750350Sjmcneill };
5977750350Sjmcneill 
60ed38b748Sxtraeme int	gscpcib_match(device_t, cfdata_t, void *);
618d9f58eaSdyoung void	gscpcib_attach(device_t, device_t, void *);
628d9f58eaSdyoung int	gscpcib_detach(device_t, int);
63e02c33d4Sdyoung int	gscpcib_rescan(device_t, const char *, const int *);
64e02c33d4Sdyoung void	gscpcib_childdetached(device_t, device_t);
6577750350Sjmcneill 
6677750350Sjmcneill int	gscpcib_gpio_pin_read(void *, int);
6777750350Sjmcneill void	gscpcib_gpio_pin_write(void *, int, int);
6877750350Sjmcneill void	gscpcib_gpio_pin_ctl(void *, int, int);
6977750350Sjmcneill 
70e02c33d4Sdyoung CFATTACH_DECL3_NEW(gscpcib, sizeof(struct gscpcib_softc),
71e02c33d4Sdyoung 	gscpcib_match, gscpcib_attach, gscpcib_detach, NULL, gscpcib_rescan,
72e02c33d4Sdyoung 	gscpcib_childdetached, DVF_DETACH_SHUTDOWN);
7377750350Sjmcneill 
7477750350Sjmcneill extern struct cfdriver gscpcib_cd;
7577750350Sjmcneill 
76e02c33d4Sdyoung void
gscpcib_childdetached(device_t self,device_t child)77e02c33d4Sdyoung gscpcib_childdetached(device_t self, device_t child)
78e02c33d4Sdyoung {
79e02c33d4Sdyoung 	struct gscpcib_softc *sc = device_private(self);
80e02c33d4Sdyoung 
81e02c33d4Sdyoung 	if (sc->sc_gpiobus == child)
82e02c33d4Sdyoung 		sc->sc_gpiobus = NULL;
83e02c33d4Sdyoung 	else
84e02c33d4Sdyoung 		pcibchilddet(self, child);
85e02c33d4Sdyoung }
86e02c33d4Sdyoung 
87e02c33d4Sdyoung int
gscpcib_rescan(device_t self,const char * ifattr,const int * loc)88e02c33d4Sdyoung gscpcib_rescan(device_t self, const char *ifattr, const int *loc)
89e02c33d4Sdyoung {
9031a08a90Ssborrill #if NGPIO > 0
91e02c33d4Sdyoung 	struct gscpcib_softc *sc = device_private(self);
92e02c33d4Sdyoung 
93e02c33d4Sdyoung 	/* Attach GPIO framework */
94e02c33d4Sdyoung 	if (sc->sc_gpio_present && ifattr_match(ifattr, "gpiobus") &&
95e02c33d4Sdyoung 	    sc->sc_gpiobus == NULL) {
96e02c33d4Sdyoung 		struct gpiobus_attach_args gba;
97e02c33d4Sdyoung 
98e02c33d4Sdyoung 		gba.gba_gc = &sc->sc_gpio_gc;
99e02c33d4Sdyoung 		gba.gba_pins = sc->sc_gpio_pins;
100e02c33d4Sdyoung 		gba.gba_npins = GSCGPIO_NPINS;
101e02c33d4Sdyoung 
1022685996bSthorpej 		sc->sc_gpiobus = config_found(self, &gba, gpiobus_print,
103*c7fb772bSthorpej 		    CFARGS(.iattr = "gpiobus"));
104e02c33d4Sdyoung 		return 0;
105e02c33d4Sdyoung 	}
10631a08a90Ssborrill #endif
10731a08a90Ssborrill 
108e02c33d4Sdyoung 	return pcibrescan(self, ifattr, loc);
109e02c33d4Sdyoung }
110e02c33d4Sdyoung 
11177750350Sjmcneill int
gscpcib_match(device_t parent,cfdata_t match,void * aux)112463905bbScegger gscpcib_match(device_t parent, cfdata_t match, void *aux)
11377750350Sjmcneill {
11477750350Sjmcneill 	struct pci_attach_args *pa = aux;
11577750350Sjmcneill 
11677750350Sjmcneill 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
11777750350Sjmcneill 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
11877750350Sjmcneill 		return (0);
11977750350Sjmcneill 
12077750350Sjmcneill 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
12177750350Sjmcneill 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SC1100_ISA)
12277750350Sjmcneill 		return (2);	/* supersede pcib(4) */
12377750350Sjmcneill 
12477750350Sjmcneill 	return (0);
12577750350Sjmcneill }
12677750350Sjmcneill 
12777750350Sjmcneill void
gscpcib_attach(device_t parent,device_t self,void * aux)1288d9f58eaSdyoung gscpcib_attach(device_t parent, device_t self, void *aux)
12977750350Sjmcneill {
1308d9f58eaSdyoung 	struct gscpcib_softc *sc = device_private(self);
13177750350Sjmcneill 	struct pci_attach_args *pa = aux;
13277750350Sjmcneill 	pcireg_t gpiobase;
13377750350Sjmcneill 	int i;
13477750350Sjmcneill 
13577750350Sjmcneill 	/* Map GPIO I/O space */
13677750350Sjmcneill 	gpiobase = pci_conf_read(pa->pa_pc, pa->pa_tag, GSCGPIO_BASE);
13777750350Sjmcneill 	sc->sc_gpio_iot = pa->pa_iot;
13877750350Sjmcneill 	if (bus_space_map(sc->sc_gpio_iot, PCI_MAPREG_IO_ADDR(gpiobase),
13977750350Sjmcneill 	    GSCGPIO_SIZE, 0, &sc->sc_gpio_ioh)) {
14077750350Sjmcneill 		printf(": failed to map GPIO I/O space");
14177750350Sjmcneill 		goto corepcib;
14277750350Sjmcneill 	}
14377750350Sjmcneill 
14477750350Sjmcneill 	/* Initialize pins array */
14577750350Sjmcneill 	for (i = 0; i < GSCGPIO_NPINS; i++) {
14677750350Sjmcneill 		sc->sc_gpio_pins[i].pin_num = i;
14777750350Sjmcneill 		sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
14877750350Sjmcneill 		    GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
14977750350Sjmcneill 		    GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
15077750350Sjmcneill 		    GPIO_PIN_PULLUP;
15177750350Sjmcneill 
15277750350Sjmcneill 		/* safe defaults */
15377750350Sjmcneill 		sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
15477750350Sjmcneill 		sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
15577750350Sjmcneill 		gscpcib_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
15677750350Sjmcneill 		gscpcib_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
15777750350Sjmcneill 	}
15877750350Sjmcneill 
15977750350Sjmcneill 	/* Create controller tag */
16077750350Sjmcneill 	sc->sc_gpio_gc.gp_cookie = sc;
16177750350Sjmcneill 	sc->sc_gpio_gc.gp_pin_read = gscpcib_gpio_pin_read;
16277750350Sjmcneill 	sc->sc_gpio_gc.gp_pin_write = gscpcib_gpio_pin_write;
16377750350Sjmcneill 	sc->sc_gpio_gc.gp_pin_ctl = gscpcib_gpio_pin_ctl;
16477750350Sjmcneill 
1658d9f58eaSdyoung 	sc->sc_gpio_present = true;
16677750350Sjmcneill 
16777750350Sjmcneill corepcib:
16877750350Sjmcneill 	/* Provide core pcib(4) functionality */
16977750350Sjmcneill 	pcibattach(parent, self, aux);
17077750350Sjmcneill 
171e02c33d4Sdyoung 	gscpcib_rescan(self, "gpiobus", NULL);
1728d9f58eaSdyoung }
1738d9f58eaSdyoung 
1748d9f58eaSdyoung int
gscpcib_detach(device_t self,int flags)1758d9f58eaSdyoung gscpcib_detach(device_t self, int flags)
1768d9f58eaSdyoung {
1778d9f58eaSdyoung 	int rc;
1788d9f58eaSdyoung 	struct gscpcib_softc *sc = device_private(self);
1798d9f58eaSdyoung 
1808d9f58eaSdyoung 	if ((rc = config_detach_children(self, flags)) != 0)
1818d9f58eaSdyoung 		return rc;
1828d9f58eaSdyoung 
183e02c33d4Sdyoung 	if ((rc = pcibdetach(self, flags)) != 0)
184e02c33d4Sdyoung 		return rc;
185e02c33d4Sdyoung 
1868d9f58eaSdyoung 	if (sc->sc_gpio_present)
1878d9f58eaSdyoung 		bus_space_unmap(sc->sc_gpio_iot, sc->sc_gpio_ioh, GSCGPIO_SIZE);
1888d9f58eaSdyoung 
1898d9f58eaSdyoung 	return rc;
19077750350Sjmcneill }
19177750350Sjmcneill 
1925f1c88d7Sperry static inline void
gscpcib_gpio_pin_select(struct gscpcib_softc * sc,int pin)19377750350Sjmcneill gscpcib_gpio_pin_select(struct gscpcib_softc *sc, int pin)
19477750350Sjmcneill {
19577750350Sjmcneill 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GSCGPIO_SEL, pin);
19677750350Sjmcneill }
19777750350Sjmcneill 
19877750350Sjmcneill int
gscpcib_gpio_pin_read(void * arg,int pin)19977750350Sjmcneill gscpcib_gpio_pin_read(void *arg, int pin)
20077750350Sjmcneill {
20177750350Sjmcneill 	struct gscpcib_softc *sc = arg;
20277750350Sjmcneill 	int reg, shift;
20368da4482Sperry 	uint32_t data;
20477750350Sjmcneill 
20577750350Sjmcneill 	reg = (pin < 32 ? GSCGPIO_GPDI0 : GSCGPIO_GPDI1);
20677750350Sjmcneill 	shift = pin % 32;
20777750350Sjmcneill 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
20877750350Sjmcneill 
20977750350Sjmcneill 	return ((data >> shift) & 0x1);
21077750350Sjmcneill }
21177750350Sjmcneill 
21277750350Sjmcneill void
gscpcib_gpio_pin_write(void * arg,int pin,int value)21377750350Sjmcneill gscpcib_gpio_pin_write(void *arg, int pin, int value)
21477750350Sjmcneill {
21577750350Sjmcneill 	struct gscpcib_softc *sc = arg;
21677750350Sjmcneill 	int reg, shift;
21768da4482Sperry 	uint32_t data;
21877750350Sjmcneill 
21977750350Sjmcneill 	reg = (pin < 32 ? GSCGPIO_GPDO0 : GSCGPIO_GPDO1);
22077750350Sjmcneill 	shift = pin % 32;
22177750350Sjmcneill 	data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
22277750350Sjmcneill 	if (value == 0)
22377750350Sjmcneill 		data &= ~(1 << shift);
22477750350Sjmcneill 	else if (value == 1)
22577750350Sjmcneill 		data |= (1 << shift);
22677750350Sjmcneill 
22777750350Sjmcneill 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg, data);
22877750350Sjmcneill }
22977750350Sjmcneill 
23077750350Sjmcneill void
gscpcib_gpio_pin_ctl(void * arg,int pin,int flags)23177750350Sjmcneill gscpcib_gpio_pin_ctl(void *arg, int pin, int flags)
23277750350Sjmcneill {
23377750350Sjmcneill 	struct gscpcib_softc *sc = arg;
23468da4482Sperry 	uint32_t conf;
23577750350Sjmcneill 
23677750350Sjmcneill 	gscpcib_gpio_pin_select(sc, pin);
23777750350Sjmcneill 	conf = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
23877750350Sjmcneill 	    GSCGPIO_CONF);
23977750350Sjmcneill 
24077750350Sjmcneill 	conf &= ~(GSCGPIO_CONF_OUTPUTEN | GSCGPIO_CONF_PUSHPULL |
24177750350Sjmcneill 	    GSCGPIO_CONF_PULLUP);
24277750350Sjmcneill 	if ((flags & GPIO_PIN_TRISTATE) == 0)
24377750350Sjmcneill 		conf |= GSCGPIO_CONF_OUTPUTEN;
24477750350Sjmcneill 	if (flags & GPIO_PIN_PUSHPULL)
24577750350Sjmcneill 		conf |= GSCGPIO_CONF_PUSHPULL;
24677750350Sjmcneill 	if (flags & GPIO_PIN_PULLUP)
24777750350Sjmcneill 		conf |= GSCGPIO_CONF_PULLUP;
24877750350Sjmcneill 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
24977750350Sjmcneill 	    GSCGPIO_CONF, conf);
25077750350Sjmcneill }
251