Author: thompsa
Date: Sun Nov  7 20:33:39 2010
New Revision: 214946
URL: http://svn.freebsd.org/changeset/base/214946

Log:
  Hook up the five gpio pins on the Avila board to the gpio framework. There are
  actually 16 I/O lines but the other ones are used for system devices and
  interrupts.
  
  The IXP4XX platform can set interrupts on these pins for
  high/low/rising/falling/transitional but this is not implemented yet.
  
  The Cambria has the same interface but as all the pins are assigned to system
  functions the gpio header is toggled via a PLD on the i2c bus and is not
  supported by this commit.

Added:
  head/sys/arm/xscale/ixp425/avila_gpio.c   (contents, props changed)
Modified:
  head/sys/arm/conf/AVILA
  head/sys/arm/conf/AVILA.hints
  head/sys/arm/xscale/ixp425/files.avila
  head/sys/arm/xscale/ixp425/ixp425reg.h

Modified: head/sys/arm/conf/AVILA
==============================================================================
--- head/sys/arm/conf/AVILA     Sun Nov  7 20:33:36 2010        (r214945)
+++ head/sys/arm/conf/AVILA     Sun Nov  7 20:33:39 2010        (r214946)
@@ -87,6 +87,10 @@ device               ad7418          # AD7418 on I2C bus
 
 device         avila_led
 
+device         gpio
+device         gpioled
+device         avila_gpio      # GPIO pins on J8
+
 device         ata
 device         atadisk         # ATA disk drives
 device         avila_ata       # Gateworks CF/IDE support

Modified: head/sys/arm/conf/AVILA.hints
==============================================================================
--- head/sys/arm/conf/AVILA.hints       Sun Nov  7 20:33:36 2010        
(r214945)
+++ head/sys/arm/conf/AVILA.hints       Sun Nov  7 20:33:39 2010        
(r214946)
@@ -41,6 +41,9 @@ hint.ata_avila.0.at="ixp0"
 # Front Panel LED
 hint.led_avila.0.at="ixp0"
 
+# GPIO pins
+hint.gpio_avila.0.at="ixp0"
+
 # Analog Devices AD7418 temperature sensor
 hint.ad7418.0.at="iicbus0"
 hint.ad7418.0.addr=0x50

Added: head/sys/arm/xscale/ixp425/avila_gpio.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/arm/xscale/ixp425/avila_gpio.c     Sun Nov  7 20:33:39 2010        
(r214946)
@@ -0,0 +1,380 @@
+/*-
+ * Copyright (c) 2009, Oleksandr Tymoshenko <go...@freebsd.org>
+ * Copyright (c) 2009, Luiz Otavio O Souza.
+ * Copyright (c) 2010, Andrew Thompson <thom...@freebsd.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice unmodified, this list of conditions, and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * GPIO driver for Gateworks Avilia
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+
+#include <sys/kernel.h>
+#include <sys/module.h>
+#include <sys/rman.h>
+#include <sys/lock.h>
+#include <sys/mutex.h>
+#include <sys/gpio.h>
+
+#include <machine/bus.h>
+#include <machine/resource.h>
+#include <arm/xscale/ixp425/ixp425reg.h>
+#include <arm/xscale/ixp425/ixp425var.h>
+
+#include "gpio_if.h"
+
+#define GPIO_SET_BITS(sc, reg, bits)   \
+       GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, (reg)) | (bits))
+
+#define GPIO_CLEAR_BITS(sc, reg, bits) \
+       GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, (reg)) & ~(bits))
+
+#define GPIO_LOCK(_sc)         mtx_lock(&(_sc)->sc_mtx)
+#define GPIO_UNLOCK(_sc)       mtx_unlock(&(_sc)->sc_mtx)
+#define GPIO_LOCK_ASSERT(_sc)  mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
+
+struct avila_gpio_softc {
+       device_t                sc_dev;
+        struct mtx             sc_mtx;
+       bus_space_tag_t         sc_iot;
+       bus_space_handle_t      sc_gpio_ioh;
+       uint32_t                sc_valid;
+       struct gpio_pin         sc_pins[IXP4XX_GPIO_PINS];
+};
+
+struct avila_gpio_pin {
+       const char *name;
+       int pin;
+       int caps;
+};
+
+#define        GPIO_PIN_IO     (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)
+static struct avila_gpio_pin avila_gpio_pins[] = {
+       { "GPIO0", 0, GPIO_PIN_IO },
+       { "GPIO1", 1, GPIO_PIN_IO },
+       { "GPIO2", 2, GPIO_PIN_IO },
+       { "GPIO3", 3, GPIO_PIN_IO },
+       { "GPIO4", 4, GPIO_PIN_IO },
+       /*
+        * The following pins are connected to system devices and should not
+        * really be frobbed.
+        */
+#if 0
+       { "SER_ENA", 5, GPIO_PIN_IO },
+       { "I2C_SCL", 6, GPIO_PIN_IO },
+       { "I2C_SDA", 7, GPIO_PIN_IO },
+       { "PCI_INTD", 8, GPIO_PIN_IO },
+       { "PCI_INTC", 9, GPIO_PIN_IO },
+       { "PCI_INTB", 10, GPIO_PIN_IO },
+       { "PCI_INTA", 11, GPIO_PIN_IO },
+       { "ATA_INT", 12, GPIO_PIN_IO },
+       { "PCI_RST", 13, GPIO_PIN_IO },
+       { "PCI_CLK", 14, GPIO_PIN_OUTPUT },
+       { "EX_CLK", 15, GPIO_PIN_OUTPUT },
+#endif
+};
+#undef GPIO_PIN_IO
+
+/*
+ * Helpers
+ */
+static void avila_gpio_pin_configure(struct avila_gpio_softc *sc, 
+    struct gpio_pin *pin, uint32_t flags);
+static int  avila_gpio_pin_flags(struct avila_gpio_softc *sc, uint32_t pin);
+
+/*
+ * Driver stuff
+ */
+static int avila_gpio_probe(device_t dev);
+static int avila_gpio_attach(device_t dev);
+static int avila_gpio_detach(device_t dev);
+
+/*
+ * GPIO interface
+ */
+static int avila_gpio_pin_max(device_t dev, int *maxpin);
+static int avila_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
+static int avila_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
+    *flags);
+static int avila_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
+static int avila_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
+static int avila_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
+static int avila_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
+static int avila_gpio_pin_toggle(device_t dev, uint32_t pin);
+
+static int
+avila_gpio_pin_flags(struct avila_gpio_softc *sc, uint32_t pin)
+{
+       uint32_t v;
+
+       v = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR) & (1 << pin);
+
+       return (v ? GPIO_PIN_INPUT : GPIO_PIN_OUTPUT);
+}
+
+static void
+avila_gpio_pin_configure(struct avila_gpio_softc *sc, struct gpio_pin *pin,
+    unsigned int flags)
+{
+       uint32_t mask;
+
+       mask = 1 << pin->gp_pin;
+       GPIO_LOCK(sc);
+
+       /*
+        * Manage input/output
+        */
+       if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
+               pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
+               if (flags & GPIO_PIN_OUTPUT) {
+                       pin->gp_flags |= GPIO_PIN_OUTPUT;
+                       GPIO_CLEAR_BITS(sc, IXP425_GPIO_GPOER, mask);
+               }
+               else {
+                       pin->gp_flags |= GPIO_PIN_INPUT;
+                       GPIO_SET_BITS(sc, IXP425_GPIO_GPOER, mask);
+               }
+       }
+
+       GPIO_UNLOCK(sc);
+}
+
+static int
+avila_gpio_pin_max(device_t dev, int *maxpin)
+{
+
+       *maxpin = IXP4XX_GPIO_PINS - 1;
+       return (0);
+}
+
+static int
+avila_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
+{
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+
+       if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & (1 << pin)))
+               return (EINVAL);
+
+       GPIO_LOCK(sc);
+       *caps = sc->sc_pins[pin].gp_caps;
+       GPIO_UNLOCK(sc);
+
+       return (0);
+}
+
+static int
+avila_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
+{
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+
+       if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & (1 << pin)))
+               return (EINVAL);
+
+       GPIO_LOCK(sc);
+       /* refresh since we do not own all the pins */
+       sc->sc_pins[pin].gp_flags = avila_gpio_pin_flags(sc, pin);
+       *flags = sc->sc_pins[pin].gp_flags;
+       GPIO_UNLOCK(sc);
+
+       return (0);
+}
+
+static int
+avila_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
+{
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+
+       if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & (1 << pin)))
+               return (EINVAL);
+
+       GPIO_LOCK(sc);
+       memcpy(name, sc->sc_pins[pin].gp_name, GPIOMAXNAME);
+       GPIO_UNLOCK(sc);
+
+       return (0);
+}
+
+static int
+avila_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
+{
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+       uint32_t mask = 1 << pin;
+
+       if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & mask))
+               return (EINVAL);
+
+       /* Filter out unwanted flags */
+       if ((flags &= sc->sc_pins[pin].gp_caps) != flags)
+               return (EINVAL);
+
+       /* Can't mix input/output together */
+       if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) ==
+           (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT))
+               return (EINVAL);
+
+       avila_gpio_pin_configure(sc, &sc->sc_pins[pin], flags);
+       return (0);
+}
+
+static int
+avila_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
+{
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+       uint32_t mask = 1 << pin;
+
+       if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & mask))
+               return (EINVAL);
+
+       GPIO_LOCK(sc);
+       if (value)
+               GPIO_SET_BITS(sc, IXP425_GPIO_GPOUTR, mask);
+       else
+               GPIO_CLEAR_BITS(sc, IXP425_GPIO_GPOUTR, mask);
+       GPIO_UNLOCK(sc);
+
+       return (0);
+}
+
+static int
+avila_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
+{
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+
+       if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & (1 << pin)))
+               return (EINVAL);
+
+       GPIO_LOCK(sc);
+       *val = (GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR) & (1 << pin)) ? 1 : 0;
+       GPIO_UNLOCK(sc);
+
+       return (0);
+}
+
+static int
+avila_gpio_pin_toggle(device_t dev, uint32_t pin)
+{
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+       uint32_t mask = 1 << pin;
+       int res;
+
+       if (pin >= IXP4XX_GPIO_PINS || !(sc->sc_valid & mask))
+               return (EINVAL);
+
+       GPIO_LOCK(sc);
+       res = (GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR) & mask) ? 1 : 0;
+       if (res)
+               GPIO_CLEAR_BITS(sc, IXP425_GPIO_GPOUTR, mask);
+       else
+               GPIO_SET_BITS(sc, IXP425_GPIO_GPOUTR, mask);
+       GPIO_UNLOCK(sc);
+
+       return (0);
+}
+
+static int
+avila_gpio_probe(device_t dev)
+{
+
+       device_set_desc(dev, "Gateworks Avila GPIO driver");
+       return (0);
+}
+
+static int
+avila_gpio_attach(device_t dev)
+{
+#define        N(a)    (sizeof(a) / sizeof(a[0]))
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+       struct ixp425_softc *sa = device_get_softc(device_get_parent(dev));
+       int i;
+
+       sc->sc_dev = dev;
+       sc->sc_iot = sa->sc_iot;
+       sc->sc_gpio_ioh = sa->sc_gpio_ioh;
+
+       mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
+           MTX_DEF);
+
+       for (i = 0; i < N(avila_gpio_pins); i++) {
+               struct avila_gpio_pin *p = &avila_gpio_pins[i];
+
+               strncpy(sc->sc_pins[p->pin].gp_name, p->name, GPIOMAXNAME);
+               sc->sc_pins[p->pin].gp_pin = p->pin;
+               sc->sc_pins[p->pin].gp_caps = p->caps;
+               sc->sc_pins[p->pin].gp_flags = avila_gpio_pin_flags(sc, p->pin);
+               sc->sc_valid |= 1 << p->pin;
+       }
+
+       device_add_child(dev, "gpioc", device_get_unit(dev));
+       device_add_child(dev, "gpiobus", device_get_unit(dev));
+       return (bus_generic_attach(dev));
+#undef N
+}
+
+static int
+avila_gpio_detach(device_t dev)
+{
+       struct avila_gpio_softc *sc = device_get_softc(dev);
+
+       KASSERT(mtx_initialized(&sc->sc_mtx), ("gpio mutex not initialized"));
+
+       bus_generic_detach(dev);
+
+       mtx_destroy(&sc->sc_mtx);
+
+       return(0);
+}
+
+static device_method_t gpio_avila_methods[] = {
+       DEVMETHOD(device_probe, avila_gpio_probe),
+       DEVMETHOD(device_attach, avila_gpio_attach),
+       DEVMETHOD(device_detach, avila_gpio_detach),
+
+       /* GPIO protocol */
+       DEVMETHOD(gpio_pin_max, avila_gpio_pin_max),
+       DEVMETHOD(gpio_pin_getname, avila_gpio_pin_getname),
+       DEVMETHOD(gpio_pin_getflags, avila_gpio_pin_getflags),
+       DEVMETHOD(gpio_pin_getcaps, avila_gpio_pin_getcaps),
+       DEVMETHOD(gpio_pin_setflags, avila_gpio_pin_setflags),
+       DEVMETHOD(gpio_pin_get, avila_gpio_pin_get),
+       DEVMETHOD(gpio_pin_set, avila_gpio_pin_set),
+       DEVMETHOD(gpio_pin_toggle, avila_gpio_pin_toggle),
+       {0, 0},
+};
+
+static driver_t gpio_avila_driver = {
+       "gpio_avila",
+       gpio_avila_methods,
+       sizeof(struct avila_gpio_softc),
+};
+static devclass_t gpio_avila_devclass;
+
+DRIVER_MODULE(gpio_avila, ixp, gpio_avila_driver, gpio_avila_devclass, 0, 0);

Modified: head/sys/arm/xscale/ixp425/files.avila
==============================================================================
--- head/sys/arm/xscale/ixp425/files.avila      Sun Nov  7 20:33:36 2010        
(r214945)
+++ head/sys/arm/xscale/ixp425/files.avila      Sun Nov  7 20:33:39 2010        
(r214946)
@@ -2,7 +2,9 @@
 arm/xscale/ixp425/avila_machdep.c      standard
 arm/xscale/ixp425/avila_ata.c          optional avila_ata
 arm/xscale/ixp425/avila_led.c          optional avila_led
+arm/xscale/ixp425/avila_gpio.c         optional avila_gpio
 arm/xscale/ixp425/cambria_exp_space.c  standard
 arm/xscale/ixp425/cambria_fled.c       optional cambria_fled
 arm/xscale/ixp425/cambria_led.c                optional cambria_led
+arm/xscale/ixp425/cambria_gpio.c       optional cambria_gpio
 arm/xscale/ixp425/ixdp425_pci.c                optional pci

Modified: head/sys/arm/xscale/ixp425/ixp425reg.h
==============================================================================
--- head/sys/arm/xscale/ixp425/ixp425reg.h      Sun Nov  7 20:33:36 2010        
(r214945)
+++ head/sys/arm/xscale/ixp425/ixp425reg.h      Sun Nov  7 20:33:39 2010        
(r214946)
@@ -323,6 +323,8 @@
 #define        GPIO_TYPE(b,v)          ((v) << (((b) & 0x7) * 3))
 #define        GPIO_TYPE_REG(b)        
(((b)&8)?IXP425_GPIO_GPIT2R:IXP425_GPIO_GPIT1R)
 
+#define        IXP4XX_GPIO_PINS        16
+
 /*
  * Expansion Bus Configuration Space.
  */
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to