2 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3 * Copyright (c) 2009, Luiz Otavio O Souza.
4 * Copyright (c) 2010, Andrew Thompson <thompsa@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * GPIO driver for Gateworks Avilia
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <sys/mutex.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <arm/xscale/ixp425/ixp425reg.h>
51 #include <arm/xscale/ixp425/ixp425var.h>
52 #include <dev/gpio/gpiobusvar.h>
56 #define GPIO_SET_BITS(sc, reg, bits) \
57 GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, (reg)) | (bits))
59 #define GPIO_CLEAR_BITS(sc, reg, bits) \
60 GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, (reg)) & ~(bits))
62 struct avila_gpio_softc {
65 bus_space_tag_t sc_iot;
66 bus_space_handle_t sc_gpio_ioh;
68 struct gpio_pin sc_pins[IXP4XX_GPIO_PINS];
71 struct avila_gpio_pin {
77 #define GPIO_PIN_IO (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)
78 static struct avila_gpio_pin avila_gpio_pins[] = {
79 { "GPIO0", 0, GPIO_PIN_IO },
80 { "GPIO1", 1, GPIO_PIN_IO },
81 { "GPIO2", 2, GPIO_PIN_IO },
82 { "GPIO3", 3, GPIO_PIN_IO },
83 { "GPIO4", 4, GPIO_PIN_IO },
85 * The following pins are connected to system devices and should not
89 { "SER_ENA", 5, GPIO_PIN_IO },
90 { "I2C_SCL", 6, GPIO_PIN_IO },
91 { "I2C_SDA", 7, GPIO_PIN_IO },
92 { "PCI_INTD", 8, GPIO_PIN_IO },
93 { "PCI_INTC", 9, GPIO_PIN_IO },
94 { "PCI_INTB", 10, GPIO_PIN_IO },
95 { "PCI_INTA", 11, GPIO_PIN_IO },
96 { "ATA_INT", 12, GPIO_PIN_IO },
97 { "PCI_RST", 13, GPIO_PIN_IO },
98 { "PCI_CLK", 14, GPIO_PIN_OUTPUT },
99 { "EX_CLK", 15, GPIO_PIN_OUTPUT },
107 static void avila_gpio_pin_configure(struct avila_gpio_softc *sc,
108 struct gpio_pin *pin, uint32_t flags);
109 static int avila_gpio_pin_flags(struct avila_gpio_softc *sc, uint32_t pin);
114 static int avila_gpio_probe(device_t dev);
115 static int avila_gpio_attach(device_t dev);
116 static int avila_gpio_detach(device_t dev);
121 static device_t avila_gpio_get_bus(device_t);
122 static int avila_gpio_pin_max(device_t dev, int *maxpin);
123 static int avila_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
124 static int avila_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
126 static int avila_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
127 static int avila_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
128 static int avila_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
129 static int avila_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
130 static int avila_gpio_pin_toggle(device_t dev, uint32_t pin);
133 avila_gpio_pin_flags(struct avila_gpio_softc *sc, uint32_t pin)
137 v = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR) & (1 << pin);
139 return (v ? GPIO_PIN_INPUT : GPIO_PIN_OUTPUT);
143 avila_gpio_pin_configure(struct avila_gpio_softc *sc, struct gpio_pin *pin,
148 mask = 1 << pin->gp_pin;
151 * Manage input/output
153 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
155 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
156 if (flags & GPIO_PIN_OUTPUT) {
157 pin->gp_flags |= GPIO_PIN_OUTPUT;
158 GPIO_CLEAR_BITS(sc, IXP425_GPIO_GPOER, mask);
161 pin->gp_flags |= GPIO_PIN_INPUT;
162 GPIO_SET_BITS(sc, IXP425_GPIO_GPOER, mask);
164 IXP4XX_GPIO_UNLOCK();
169 avila_gpio_get_bus(device_t dev)
171 struct avila_gpio_softc *sc;
173 sc = device_get_softc(dev);
175 return (sc->sc_busdev);
179 avila_gpio_pin_max(device_t dev, int *maxpin)
182 *maxpin = IXP4XX_GPIO_PINS - 1;
187 avila_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
189 struct avila_gpio_softc *sc = device_get_softc(dev);
191 if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & (1 << pin)))
194 *caps = sc->sc_pins[pin].gp_caps;
199 avila_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
201 struct avila_gpio_softc *sc = device_get_softc(dev);
203 if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & (1 << pin)))
207 /* refresh since we do not own all the pins */
208 sc->sc_pins[pin].gp_flags = avila_gpio_pin_flags(sc, pin);
209 *flags = sc->sc_pins[pin].gp_flags;
210 IXP4XX_GPIO_UNLOCK();
216 avila_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
218 struct avila_gpio_softc *sc = device_get_softc(dev);
220 if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & (1 << pin)))
223 memcpy(name, sc->sc_pins[pin].gp_name, GPIOMAXNAME);
228 avila_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
230 struct avila_gpio_softc *sc = device_get_softc(dev);
231 uint32_t mask = 1 << pin;
233 if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & mask))
236 avila_gpio_pin_configure(sc, &sc->sc_pins[pin], flags);
242 avila_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
244 struct avila_gpio_softc *sc = device_get_softc(dev);
245 uint32_t mask = 1 << pin;
247 if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & mask))
252 GPIO_SET_BITS(sc, IXP425_GPIO_GPOUTR, mask);
254 GPIO_CLEAR_BITS(sc, IXP425_GPIO_GPOUTR, mask);
255 IXP4XX_GPIO_UNLOCK();
261 avila_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
263 struct avila_gpio_softc *sc = device_get_softc(dev);
265 if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & (1 << pin)))
269 *val = (GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR) & (1 << pin)) ? 1 : 0;
270 IXP4XX_GPIO_UNLOCK();
276 avila_gpio_pin_toggle(device_t dev, uint32_t pin)
278 struct avila_gpio_softc *sc = device_get_softc(dev);
279 uint32_t mask = 1 << pin;
282 if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & mask))
286 res = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR) & mask;
288 GPIO_CLEAR_BITS(sc, IXP425_GPIO_GPOUTR, mask);
290 GPIO_SET_BITS(sc, IXP425_GPIO_GPOUTR, mask);
291 IXP4XX_GPIO_UNLOCK();
297 avila_gpio_probe(device_t dev)
300 device_set_desc(dev, "Gateworks Avila GPIO driver");
305 avila_gpio_attach(device_t dev)
307 #define N(a) (sizeof(a) / sizeof(a[0]))
308 struct avila_gpio_softc *sc = device_get_softc(dev);
309 struct ixp425_softc *sa = device_get_softc(device_get_parent(dev));
313 sc->sc_iot = sa->sc_iot;
314 sc->sc_gpio_ioh = sa->sc_gpio_ioh;
316 for (i = 0; i < N(avila_gpio_pins); i++) {
317 struct avila_gpio_pin *p = &avila_gpio_pins[i];
319 strncpy(sc->sc_pins[p->pin].gp_name, p->name, GPIOMAXNAME);
320 sc->sc_pins[p->pin].gp_pin = p->pin;
321 sc->sc_pins[p->pin].gp_caps = p->caps;
322 sc->sc_pins[p->pin].gp_flags = avila_gpio_pin_flags(sc, p->pin);
323 sc->sc_valid |= 1 << p->pin;
326 sc->sc_busdev = gpiobus_attach_bus(dev);
327 if (sc->sc_busdev == NULL)
335 avila_gpio_detach(device_t dev)
338 gpiobus_detach_bus(dev);
343 static device_method_t gpio_avila_methods[] = {
344 DEVMETHOD(device_probe, avila_gpio_probe),
345 DEVMETHOD(device_attach, avila_gpio_attach),
346 DEVMETHOD(device_detach, avila_gpio_detach),
349 DEVMETHOD(gpio_get_bus, avila_gpio_get_bus),
350 DEVMETHOD(gpio_pin_max, avila_gpio_pin_max),
351 DEVMETHOD(gpio_pin_getname, avila_gpio_pin_getname),
352 DEVMETHOD(gpio_pin_getflags, avila_gpio_pin_getflags),
353 DEVMETHOD(gpio_pin_getcaps, avila_gpio_pin_getcaps),
354 DEVMETHOD(gpio_pin_setflags, avila_gpio_pin_setflags),
355 DEVMETHOD(gpio_pin_get, avila_gpio_pin_get),
356 DEVMETHOD(gpio_pin_set, avila_gpio_pin_set),
357 DEVMETHOD(gpio_pin_toggle, avila_gpio_pin_toggle),
361 static driver_t gpio_avila_driver = {
364 sizeof(struct avila_gpio_softc),
366 static devclass_t gpio_avila_devclass;
368 DRIVER_MODULE(gpio_avila, ixp, gpio_avila_driver, gpio_avila_devclass, 0, 0);
369 MODULE_VERSION(gpio_avila, 1);