2 * Copyright (c) 2010, Andrew Thompson <thompsa@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * GPIO driver for Gateworks Cambria
32 * The Cambria PLD does not set the i2c ack bit after each write, if we used the
33 * regular iicbus interface it would abort the xfer after the address byte
34 * times out and not write our latch. To get around this we grab the iicbus and
35 * then do our own bit banging. This is a comprimise to changing all the iicbb
36 * device methods to allow a flag to be passed down and is similir to how Linux
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/module.h>
52 #include <sys/mutex.h>
55 #include <arm/xscale/ixp425/ixp425reg.h>
56 #include <arm/xscale/ixp425/ixp425var.h>
57 #include <arm/xscale/ixp425/ixdp425reg.h>
59 #include <dev/iicbus/iiconf.h>
60 #include <dev/iicbus/iicbus.h>
65 #define IIC_M_WR 0 /* write operation */
66 #define PLD_ADDR 0xac /* slave address */
70 #define GPIO_CONF_CLR(sc, reg, mask) \
71 GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, reg) &~ (mask))
72 #define GPIO_CONF_SET(sc, reg, mask) \
73 GPIO_CONF_WRITE_4(sc, reg, GPIO_CONF_READ_4(sc, reg) | (mask))
75 #define GPIO_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
76 #define GPIO_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
77 #define GPIO_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
80 struct cambria_gpio_softc {
82 bus_space_tag_t sc_iot;
83 bus_space_handle_t sc_gpio_ioh;
85 struct gpio_pin sc_pins[GPIO_PINS];
89 struct cambria_gpio_pin {
95 extern struct ixp425_softc *ixp425_softc;
97 static struct cambria_gpio_pin cambria_gpio_pins[GPIO_PINS] = {
98 { "GPIO0", 0, GPIO_PIN_OUTPUT },
99 { "GPIO1", 1, GPIO_PIN_OUTPUT },
100 { "GPIO2", 2, GPIO_PIN_OUTPUT },
101 { "GPIO3", 3, GPIO_PIN_OUTPUT },
102 { "GPIO4", 4, GPIO_PIN_OUTPUT },
108 static int cambria_gpio_read(struct cambria_gpio_softc *, uint32_t, unsigned int *);
109 static int cambria_gpio_write(struct cambria_gpio_softc *);
114 static int cambria_gpio_probe(device_t dev);
115 static int cambria_gpio_attach(device_t dev);
116 static int cambria_gpio_detach(device_t dev);
121 static int cambria_gpio_pin_max(device_t dev, int *maxpin);
122 static int cambria_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
123 static int cambria_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
125 static int cambria_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
126 static int cambria_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
127 static int cambria_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
128 static int cambria_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
129 static int cambria_gpio_pin_toggle(device_t dev, uint32_t pin);
132 i2c_getsda(struct cambria_gpio_softc *sc)
137 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
139 reg = GPIO_CONF_READ_4(sc, IXP425_GPIO_GPINR);
140 IXP4XX_GPIO_UNLOCK();
141 return (reg & GPIO_I2C_SDA_BIT);
145 i2c_setsda(struct cambria_gpio_softc *sc, int val)
149 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SDA_BIT);
151 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
153 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SDA_BIT);
154 IXP4XX_GPIO_UNLOCK();
159 i2c_setscl(struct cambria_gpio_softc *sc, int val)
163 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOUTR, GPIO_I2C_SCL_BIT);
165 GPIO_CONF_SET(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
167 GPIO_CONF_CLR(sc, IXP425_GPIO_GPOER, GPIO_I2C_SCL_BIT);
168 IXP4XX_GPIO_UNLOCK();
173 i2c_sendstart(struct cambria_gpio_softc *sc)
182 i2c_sendstop(struct cambria_gpio_softc *sc)
191 i2c_sendbyte(struct cambria_gpio_softc *sc, u_char data)
195 for (i=7; i>=0; i--) {
196 i2c_setsda(sc, data & (1<<i));
206 i2c_readbyte(struct cambria_gpio_softc *sc)
209 unsigned char data=0;
222 cambria_gpio_read(struct cambria_gpio_softc *sc, uint32_t pin, unsigned int *val)
224 device_t dev = sc->sc_dev;
227 error = iicbus_request_bus(device_get_parent(dev), dev,
233 i2c_sendbyte(sc, PLD_ADDR | LSB);
234 *val = (i2c_readbyte(sc) & (1 << pin)) != 0;
237 iicbus_release_bus(device_get_parent(dev), dev);
243 cambria_gpio_write(struct cambria_gpio_softc *sc)
245 device_t dev = sc->sc_dev;
248 error = iicbus_request_bus(device_get_parent(dev), dev,
254 i2c_sendbyte(sc, PLD_ADDR & ~LSB);
255 i2c_sendbyte(sc, sc->sc_latch);
258 iicbus_release_bus(device_get_parent(dev), dev);
264 cambria_gpio_pin_max(device_t dev, int *maxpin)
267 *maxpin = GPIO_PINS - 1;
272 cambria_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
274 struct cambria_gpio_softc *sc = device_get_softc(dev);
276 if (pin >= GPIO_PINS)
279 *caps = sc->sc_pins[pin].gp_caps;
284 cambria_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
286 struct cambria_gpio_softc *sc = device_get_softc(dev);
288 if (pin >= GPIO_PINS)
291 *flags = sc->sc_pins[pin].gp_flags;
296 cambria_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
298 struct cambria_gpio_softc *sc = device_get_softc(dev);
300 if (pin >= GPIO_PINS)
303 memcpy(name, sc->sc_pins[pin].gp_name, GPIOMAXNAME);
308 cambria_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
310 struct cambria_gpio_softc *sc = device_get_softc(dev);
313 if (pin >= GPIO_PINS)
316 /* Filter out unwanted flags */
317 if ((flags &= sc->sc_pins[pin].gp_caps) != flags)
320 /* Can't mix input/output together */
321 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) ==
322 (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT))
326 sc->sc_pins[pin].gp_flags = flags;
328 sc->sc_latch |= (1 << pin);
329 error = cambria_gpio_write(sc);
336 cambria_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
338 struct cambria_gpio_softc *sc = device_get_softc(dev);
341 if (pin >= GPIO_PINS || sc->sc_pins[pin].gp_flags != GPIO_PIN_OUTPUT)
346 sc->sc_latch |= (1 << pin);
348 sc->sc_latch &= ~(1 << pin);
349 error = cambria_gpio_write(sc);
356 cambria_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
358 struct cambria_gpio_softc *sc = device_get_softc(dev);
361 if (pin >= GPIO_PINS)
365 if (sc->sc_pins[pin].gp_flags == GPIO_PIN_OUTPUT)
366 *val = (sc->sc_latch & (1 << pin)) ? 1 : 0;
368 error = cambria_gpio_read(sc, pin, val);
375 cambria_gpio_pin_toggle(device_t dev, uint32_t pin)
377 struct cambria_gpio_softc *sc = device_get_softc(dev);
380 if (pin >= GPIO_PINS || sc->sc_pins[pin].gp_flags != GPIO_PIN_OUTPUT)
384 sc->sc_latch ^= (1 << pin);
385 error = cambria_gpio_write(sc);
392 cambria_gpio_probe(device_t dev)
395 device_set_desc(dev, "Gateworks Cambria GPIO driver");
400 cambria_gpio_attach(device_t dev)
402 struct cambria_gpio_softc *sc = device_get_softc(dev);
406 sc->sc_iot = ixp425_softc->sc_iot;
407 sc->sc_gpio_ioh = ixp425_softc->sc_gpio_ioh;
409 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
412 for (pin = 0; pin < GPIO_PINS; pin++) {
413 struct cambria_gpio_pin *p = &cambria_gpio_pins[pin];
415 strncpy(sc->sc_pins[pin].gp_name, p->name, GPIOMAXNAME);
416 sc->sc_pins[pin].gp_pin = pin;
417 sc->sc_pins[pin].gp_caps = GPIO_PIN_INPUT|GPIO_PIN_OUTPUT;
418 sc->sc_pins[pin].gp_flags = 0;
419 cambria_gpio_pin_setflags(dev, pin, p->flags);
422 device_add_child(dev, "gpioc", device_get_unit(dev));
423 device_add_child(dev, "gpiobus", device_get_unit(dev));
424 return (bus_generic_attach(dev));
428 cambria_gpio_detach(device_t dev)
430 struct cambria_gpio_softc *sc = device_get_softc(dev);
432 KASSERT(mtx_initialized(&sc->sc_mtx), ("gpio mutex not initialized"));
434 bus_generic_detach(dev);
436 mtx_destroy(&sc->sc_mtx);
441 static device_method_t cambria_gpio_methods[] = {
442 DEVMETHOD(device_probe, cambria_gpio_probe),
443 DEVMETHOD(device_attach, cambria_gpio_attach),
444 DEVMETHOD(device_detach, cambria_gpio_detach),
447 DEVMETHOD(gpio_pin_max, cambria_gpio_pin_max),
448 DEVMETHOD(gpio_pin_getname, cambria_gpio_pin_getname),
449 DEVMETHOD(gpio_pin_getflags, cambria_gpio_pin_getflags),
450 DEVMETHOD(gpio_pin_getcaps, cambria_gpio_pin_getcaps),
451 DEVMETHOD(gpio_pin_setflags, cambria_gpio_pin_setflags),
452 DEVMETHOD(gpio_pin_get, cambria_gpio_pin_get),
453 DEVMETHOD(gpio_pin_set, cambria_gpio_pin_set),
454 DEVMETHOD(gpio_pin_toggle, cambria_gpio_pin_toggle),
458 static driver_t cambria_gpio_driver = {
460 cambria_gpio_methods,
461 sizeof(struct cambria_gpio_softc),
463 static devclass_t cambria_gpio_devclass;
464 extern devclass_t gpiobus_devclass, gpioc_devclass;
465 extern driver_t gpiobus_driver, gpioc_driver;
467 DRIVER_MODULE(gpio_cambria, iicbus, cambria_gpio_driver, cambria_gpio_devclass, 0, 0);
468 DRIVER_MODULE(gpiobus, gpio_cambria, gpiobus_driver, gpiobus_devclass, 0, 0);
469 DRIVER_MODULE(gpioc, gpio_cambria, gpioc_driver, gpioc_devclass, 0, 0);
470 MODULE_VERSION(gpio_cambria, 1);
471 MODULE_DEPEND(gpio_cambria, iicbus, 1, 1, 1);