gscpcib.c revision 1.2 1 1.2 drochner /* $NetBSD: gscpcib.c,v 1.2 2005/10/11 15:59:16 drochner 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.1 jmcneill #include <sys/param.h>
26 1.1 jmcneill #include <sys/systm.h>
27 1.1 jmcneill #include <sys/device.h>
28 1.1 jmcneill #include <sys/gpio.h>
29 1.1 jmcneill #include <sys/kernel.h>
30 1.1 jmcneill
31 1.1 jmcneill #include <machine/bus.h>
32 1.1 jmcneill
33 1.1 jmcneill #include <dev/pci/pcireg.h>
34 1.1 jmcneill #include <dev/pci/pcivar.h>
35 1.1 jmcneill #include <dev/pci/pcidevs.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <dev/gpio/gpiovar.h>
38 1.1 jmcneill
39 1.1 jmcneill #include <i386/pci/gscpcibreg.h>
40 1.1 jmcneill
41 1.1 jmcneill struct gscpcib_softc {
42 1.1 jmcneill struct device sc_dev;
43 1.1 jmcneill
44 1.1 jmcneill /* GPIO interface */
45 1.1 jmcneill bus_space_tag_t sc_gpio_iot;
46 1.1 jmcneill bus_space_handle_t sc_gpio_ioh;
47 1.1 jmcneill struct gpio_chipset_tag sc_gpio_gc;
48 1.1 jmcneill gpio_pin_t sc_gpio_pins[GSCGPIO_NPINS];
49 1.1 jmcneill };
50 1.1 jmcneill
51 1.1 jmcneill int gscpcib_match(struct device *, struct cfdata *, void *);
52 1.1 jmcneill void gscpcib_attach(struct device *, struct device *, void *);
53 1.1 jmcneill
54 1.1 jmcneill int gscpcib_gpio_pin_read(void *, int);
55 1.1 jmcneill void gscpcib_gpio_pin_write(void *, int, int);
56 1.1 jmcneill void gscpcib_gpio_pin_ctl(void *, int, int);
57 1.1 jmcneill
58 1.1 jmcneill /* arch/i386/pci/pcib.c */
59 1.1 jmcneill void pcibattach(struct device *, struct device *, void *);
60 1.1 jmcneill
61 1.1 jmcneill CFATTACH_DECL(gscpcib, sizeof(struct gscpcib_softc),
62 1.1 jmcneill gscpcib_match, gscpcib_attach, NULL, NULL);
63 1.1 jmcneill
64 1.1 jmcneill extern struct cfdriver gscpcib_cd;
65 1.1 jmcneill
66 1.1 jmcneill int
67 1.1 jmcneill gscpcib_match(struct device *parent, struct cfdata *match, void *aux)
68 1.1 jmcneill {
69 1.1 jmcneill struct pci_attach_args *pa = aux;
70 1.1 jmcneill
71 1.1 jmcneill if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
72 1.1 jmcneill PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA)
73 1.1 jmcneill return (0);
74 1.1 jmcneill
75 1.1 jmcneill if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
76 1.1 jmcneill PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NS_SC1100_ISA)
77 1.1 jmcneill return (2); /* supersede pcib(4) */
78 1.1 jmcneill
79 1.1 jmcneill return (0);
80 1.1 jmcneill }
81 1.1 jmcneill
82 1.1 jmcneill void
83 1.1 jmcneill gscpcib_attach(struct device *parent, struct device *self, void *aux)
84 1.1 jmcneill {
85 1.1 jmcneill #ifndef SMALL_KERNEL
86 1.1 jmcneill struct gscpcib_softc *sc = (struct gscpcib_softc *)self;
87 1.1 jmcneill struct pci_attach_args *pa = aux;
88 1.1 jmcneill struct gpiobus_attach_args gba;
89 1.1 jmcneill pcireg_t gpiobase;
90 1.1 jmcneill int i;
91 1.1 jmcneill int gpio_present = 0;
92 1.1 jmcneill
93 1.1 jmcneill /* Map GPIO I/O space */
94 1.1 jmcneill gpiobase = pci_conf_read(pa->pa_pc, pa->pa_tag, GSCGPIO_BASE);
95 1.1 jmcneill sc->sc_gpio_iot = pa->pa_iot;
96 1.1 jmcneill if (bus_space_map(sc->sc_gpio_iot, PCI_MAPREG_IO_ADDR(gpiobase),
97 1.1 jmcneill GSCGPIO_SIZE, 0, &sc->sc_gpio_ioh)) {
98 1.1 jmcneill printf(": failed to map GPIO I/O space");
99 1.1 jmcneill goto corepcib;
100 1.1 jmcneill }
101 1.1 jmcneill
102 1.1 jmcneill /* Initialize pins array */
103 1.1 jmcneill for (i = 0; i < GSCGPIO_NPINS; i++) {
104 1.1 jmcneill sc->sc_gpio_pins[i].pin_num = i;
105 1.1 jmcneill sc->sc_gpio_pins[i].pin_caps = GPIO_PIN_INPUT |
106 1.1 jmcneill GPIO_PIN_OUTPUT | GPIO_PIN_OPENDRAIN |
107 1.1 jmcneill GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
108 1.1 jmcneill GPIO_PIN_PULLUP;
109 1.1 jmcneill
110 1.1 jmcneill /* safe defaults */
111 1.1 jmcneill sc->sc_gpio_pins[i].pin_flags = GPIO_PIN_TRISTATE;
112 1.1 jmcneill sc->sc_gpio_pins[i].pin_state = GPIO_PIN_LOW;
113 1.1 jmcneill gscpcib_gpio_pin_ctl(sc, i, sc->sc_gpio_pins[i].pin_flags);
114 1.1 jmcneill gscpcib_gpio_pin_write(sc, i, sc->sc_gpio_pins[i].pin_state);
115 1.1 jmcneill }
116 1.1 jmcneill
117 1.1 jmcneill /* Create controller tag */
118 1.1 jmcneill sc->sc_gpio_gc.gp_cookie = sc;
119 1.1 jmcneill sc->sc_gpio_gc.gp_pin_read = gscpcib_gpio_pin_read;
120 1.1 jmcneill sc->sc_gpio_gc.gp_pin_write = gscpcib_gpio_pin_write;
121 1.1 jmcneill sc->sc_gpio_gc.gp_pin_ctl = gscpcib_gpio_pin_ctl;
122 1.1 jmcneill
123 1.1 jmcneill gba.gba_gc = &sc->sc_gpio_gc;
124 1.1 jmcneill gba.gba_pins = sc->sc_gpio_pins;
125 1.1 jmcneill gba.gba_npins = GSCGPIO_NPINS;
126 1.1 jmcneill
127 1.1 jmcneill gpio_present = 1;
128 1.1 jmcneill
129 1.1 jmcneill corepcib:
130 1.1 jmcneill #endif /* !SMALL_KERNEL */
131 1.1 jmcneill /* Provide core pcib(4) functionality */
132 1.1 jmcneill pcibattach(parent, self, aux);
133 1.1 jmcneill
134 1.1 jmcneill #ifndef SMALL_KERNEL
135 1.1 jmcneill /* Attach GPIO framework */
136 1.1 jmcneill if (gpio_present)
137 1.2 drochner config_found_ia(&sc->sc_dev, "gpiobus", &gba, gpiobus_print);
138 1.1 jmcneill #endif /* !SMALL_KERNEL */
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.1 jmcneill #ifndef SMALL_KERNEL
142 1.1 jmcneill static __inline void
143 1.1 jmcneill gscpcib_gpio_pin_select(struct gscpcib_softc *sc, int pin)
144 1.1 jmcneill {
145 1.1 jmcneill bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, GSCGPIO_SEL, pin);
146 1.1 jmcneill }
147 1.1 jmcneill
148 1.1 jmcneill int
149 1.1 jmcneill gscpcib_gpio_pin_read(void *arg, int pin)
150 1.1 jmcneill {
151 1.1 jmcneill struct gscpcib_softc *sc = arg;
152 1.1 jmcneill int reg, shift;
153 1.1 jmcneill u_int32_t data;
154 1.1 jmcneill
155 1.1 jmcneill reg = (pin < 32 ? GSCGPIO_GPDI0 : GSCGPIO_GPDI1);
156 1.1 jmcneill shift = pin % 32;
157 1.1 jmcneill data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
158 1.1 jmcneill
159 1.1 jmcneill return ((data >> shift) & 0x1);
160 1.1 jmcneill }
161 1.1 jmcneill
162 1.1 jmcneill void
163 1.1 jmcneill gscpcib_gpio_pin_write(void *arg, int pin, int value)
164 1.1 jmcneill {
165 1.1 jmcneill struct gscpcib_softc *sc = arg;
166 1.1 jmcneill int reg, shift;
167 1.1 jmcneill u_int32_t data;
168 1.1 jmcneill
169 1.1 jmcneill reg = (pin < 32 ? GSCGPIO_GPDO0 : GSCGPIO_GPDO1);
170 1.1 jmcneill shift = pin % 32;
171 1.1 jmcneill data = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg);
172 1.1 jmcneill if (value == 0)
173 1.1 jmcneill data &= ~(1 << shift);
174 1.1 jmcneill else if (value == 1)
175 1.1 jmcneill data |= (1 << shift);
176 1.1 jmcneill
177 1.1 jmcneill bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, reg, data);
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.1 jmcneill void
181 1.1 jmcneill gscpcib_gpio_pin_ctl(void *arg, int pin, int flags)
182 1.1 jmcneill {
183 1.1 jmcneill struct gscpcib_softc *sc = arg;
184 1.1 jmcneill u_int32_t conf;
185 1.1 jmcneill
186 1.1 jmcneill gscpcib_gpio_pin_select(sc, pin);
187 1.1 jmcneill conf = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
188 1.1 jmcneill GSCGPIO_CONF);
189 1.1 jmcneill
190 1.1 jmcneill conf &= ~(GSCGPIO_CONF_OUTPUTEN | GSCGPIO_CONF_PUSHPULL |
191 1.1 jmcneill GSCGPIO_CONF_PULLUP);
192 1.1 jmcneill if ((flags & GPIO_PIN_TRISTATE) == 0)
193 1.1 jmcneill conf |= GSCGPIO_CONF_OUTPUTEN;
194 1.1 jmcneill if (flags & GPIO_PIN_PUSHPULL)
195 1.1 jmcneill conf |= GSCGPIO_CONF_PUSHPULL;
196 1.1 jmcneill if (flags & GPIO_PIN_PULLUP)
197 1.1 jmcneill conf |= GSCGPIO_CONF_PULLUP;
198 1.1 jmcneill bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh,
199 1.1 jmcneill GSCGPIO_CONF, conf);
200 1.1 jmcneill }
201 1.1 jmcneill #endif /* !SMALL_KERNEL */
202