2 * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@gmail.com>
3 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
4 * Copyright (c) 2012 Luiz Otavio O Souza.
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, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
40 #include <sys/mutex.h>
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 #include <machine/cpufunc.h>
46 #include <machine/resource.h>
47 #include <machine/fdt.h>
48 #include <machine/intr.h>
50 #include <dev/fdt/fdt_common.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
57 * A10 have 9 banks of gpio.
59 * PA0 - PA17 | PB0 - PB23 | PC0 - PC24
60 * PD0 - PD27 | PE0 - PE31 | PF0 - PF5
61 * PG0 - PG9 | PH0 - PH27 | PI0 - PI12
64 #define A10_GPIO_PINS 288
65 #define A10_GPIO_DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
66 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN)
68 #define A10_GPIO_NONE 0
69 #define A10_GPIO_PULLUP 1
70 #define A10_GPIO_PULLDOWN 2
72 #define A10_GPIO_INPUT 0
73 #define A10_GPIO_OUTPUT 1
75 struct a10_gpio_softc {
78 struct resource * sc_mem_res;
79 struct resource * sc_irq_res;
80 bus_space_tag_t sc_bst;
81 bus_space_handle_t sc_bsh;
84 struct gpio_pin sc_gpio_pins[A10_GPIO_PINS];
87 #define A10_GPIO_LOCK(_sc) mtx_lock(&_sc->sc_mtx)
88 #define A10_GPIO_UNLOCK(_sc) mtx_unlock(&_sc->sc_mtx)
89 #define A10_GPIO_LOCK_ASSERT(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED)
91 #define A10_GPIO_GP_CFG(_bank, _pin) 0x00 + ((_bank) * 0x24) + ((_pin)<<2)
92 #define A10_GPIO_GP_DAT(_bank) 0x10 + ((_bank) * 0x24)
93 #define A10_GPIO_GP_DRV(_bank, _pin) 0x14 + ((_bank) * 0x24) + ((_pin)<<2)
94 #define A10_GPIO_GP_PUL(_bank, _pin) 0x1c + ((_bank) * 0x24) + ((_pin)<<2)
96 #define A10_GPIO_GP_INT_CFG0 0x200
97 #define A10_GPIO_GP_INT_CFG1 0x204
98 #define A10_GPIO_GP_INT_CFG2 0x208
99 #define A10_GPIO_GP_INT_CFG3 0x20c
101 #define A10_GPIO_GP_INT_CTL 0x210
102 #define A10_GPIO_GP_INT_STA 0x214
103 #define A10_GPIO_GP_INT_DEB 0x218
105 #define A10_GPIO_WRITE(_sc, _off, _val) \
106 bus_space_write_4(_sc->sc_bst, _sc->sc_bsh, _off, _val)
107 #define A10_GPIO_READ(_sc, _off) \
108 bus_space_read_4(_sc->sc_bst, _sc->sc_bsh, _off)
111 a10_gpio_get_function(struct a10_gpio_softc *sc, uint32_t pin)
113 uint32_t bank, func, offset;
116 pin = pin - 32 * bank;
118 offset = ((pin & 0x07) << 2);
121 func = (A10_GPIO_READ(sc, A10_GPIO_GP_CFG(bank, func)) >> offset) & 7;
128 a10_gpio_func_flag(uint32_t nfunc)
133 return (GPIO_PIN_INPUT);
134 case A10_GPIO_OUTPUT:
135 return (GPIO_PIN_OUTPUT);
141 a10_gpio_set_function(struct a10_gpio_softc *sc, uint32_t pin, uint32_t f)
143 uint32_t bank, func, data, offset;
145 /* Must be called with lock held. */
146 A10_GPIO_LOCK_ASSERT(sc);
149 pin = pin - 32 * bank;
151 offset = ((pin & 0x07) << 2);
153 data = A10_GPIO_READ(sc, A10_GPIO_GP_CFG(bank, func));
154 data &= ~(7 << offset);
155 data |= (f << offset);
156 A10_GPIO_WRITE(sc, A10_GPIO_GP_CFG(bank, func), data);
160 a10_gpio_set_pud(struct a10_gpio_softc *sc, uint32_t pin, uint32_t state)
162 uint32_t bank, offset, pull, val;
164 /* Must be called with lock held. */
165 A10_GPIO_LOCK_ASSERT(sc);
168 pin = pin - 32 * bank;
170 offset = ((pin & 0x0f) << 1);
172 val = A10_GPIO_READ(sc, A10_GPIO_GP_PUL(bank, pull));
173 val &= ~(0x03 << offset);
174 val |= (state << offset);
175 A10_GPIO_WRITE(sc, A10_GPIO_GP_PUL(bank, pull), val);
179 a10_gpio_pin_configure(struct a10_gpio_softc *sc, struct gpio_pin *pin,
186 * Manage input/output.
188 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
189 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
190 if (flags & GPIO_PIN_OUTPUT) {
191 pin->gp_flags |= GPIO_PIN_OUTPUT;
192 a10_gpio_set_function(sc, pin->gp_pin,
195 pin->gp_flags |= GPIO_PIN_INPUT;
196 a10_gpio_set_function(sc, pin->gp_pin,
201 /* Manage Pull-up/pull-down. */
202 pin->gp_flags &= ~(GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN);
203 if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) {
204 if (flags & GPIO_PIN_PULLUP) {
205 pin->gp_flags |= GPIO_PIN_PULLUP;
206 a10_gpio_set_pud(sc, pin->gp_pin, A10_GPIO_PULLUP);
208 pin->gp_flags |= GPIO_PIN_PULLDOWN;
209 a10_gpio_set_pud(sc, pin->gp_pin, A10_GPIO_PULLDOWN);
212 a10_gpio_set_pud(sc, pin->gp_pin, A10_GPIO_NONE);
218 a10_gpio_pin_max(device_t dev, int *maxpin)
221 *maxpin = A10_GPIO_PINS - 1;
226 a10_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
228 struct a10_gpio_softc *sc = device_get_softc(dev);
231 for (i = 0; i < sc->sc_gpio_npins; i++) {
232 if (sc->sc_gpio_pins[i].gp_pin == pin)
236 if (i >= sc->sc_gpio_npins)
240 *caps = sc->sc_gpio_pins[i].gp_caps;
247 a10_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
249 struct a10_gpio_softc *sc = device_get_softc(dev);
252 for (i = 0; i < sc->sc_gpio_npins; i++) {
253 if (sc->sc_gpio_pins[i].gp_pin == pin)
257 if (i >= sc->sc_gpio_npins)
261 *flags = sc->sc_gpio_pins[i].gp_flags;
268 a10_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
270 struct a10_gpio_softc *sc = device_get_softc(dev);
273 for (i = 0; i < sc->sc_gpio_npins; i++) {
274 if (sc->sc_gpio_pins[i].gp_pin == pin)
278 if (i >= sc->sc_gpio_npins)
282 memcpy(name, sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME);
289 a10_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
291 struct a10_gpio_softc *sc = device_get_softc(dev);
294 for (i = 0; i < sc->sc_gpio_npins; i++) {
295 if (sc->sc_gpio_pins[i].gp_pin == pin)
299 if (i >= sc->sc_gpio_npins)
302 /* Check for unwanted flags. */
303 if ((flags & sc->sc_gpio_pins[i].gp_caps) != flags)
306 /* Can't mix input/output together. */
307 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) ==
308 (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT))
311 /* Can't mix pull-up/pull-down together. */
312 if ((flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) ==
313 (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN))
316 a10_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags);
322 a10_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
324 struct a10_gpio_softc *sc = device_get_softc(dev);
325 uint32_t bank, offset, data;
328 for (i = 0; i < sc->sc_gpio_npins; i++) {
329 if (sc->sc_gpio_pins[i].gp_pin == pin)
333 if (i >= sc->sc_gpio_npins)
337 pin = pin - 32 * bank;
341 data = A10_GPIO_READ(sc, A10_GPIO_GP_DAT(bank));
343 data |= (1 << offset);
345 data &= ~(1 << offset);
346 A10_GPIO_WRITE(sc, A10_GPIO_GP_DAT(bank), data);
353 a10_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
355 struct a10_gpio_softc *sc = device_get_softc(dev);
356 uint32_t bank, offset, reg_data;
359 for (i = 0; i < sc->sc_gpio_npins; i++) {
360 if (sc->sc_gpio_pins[i].gp_pin == pin)
364 if (i >= sc->sc_gpio_npins)
368 pin = pin - 32 * bank;
372 reg_data = A10_GPIO_READ(sc, A10_GPIO_GP_DAT(bank));
374 *val = (reg_data & (1 << offset)) ? 1 : 0;
380 a10_gpio_pin_toggle(device_t dev, uint32_t pin)
382 struct a10_gpio_softc *sc = device_get_softc(dev);
383 uint32_t bank, data, offset;
386 for (i = 0; i < sc->sc_gpio_npins; i++) {
387 if (sc->sc_gpio_pins[i].gp_pin == pin)
391 if (i >= sc->sc_gpio_npins)
395 pin = pin - 32 * bank;
399 data = A10_GPIO_READ(sc, A10_GPIO_GP_DAT(bank));
400 if (data & (1 << offset))
401 data &= ~(1 << offset);
403 data |= (1 << offset);
404 A10_GPIO_WRITE(sc, A10_GPIO_GP_DAT(bank), data);
411 a10_gpio_probe(device_t dev)
413 if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-gpio"))
416 device_set_desc(dev, "Allwinner GPIO controller");
417 return (BUS_PROBE_DEFAULT);
421 a10_gpio_attach(device_t dev)
423 struct a10_gpio_softc *sc = device_get_softc(dev);
430 mtx_init(&sc->sc_mtx, "a10 gpio", "gpio", MTX_DEF);
433 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
435 if (!sc->sc_mem_res) {
436 device_printf(dev, "cannot allocate memory window\n");
440 sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
441 sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
444 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
446 if (!sc->sc_irq_res) {
447 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
448 device_printf(dev, "cannot allocate interrupt\n");
453 gpio = ofw_bus_get_node(sc->sc_dev);
455 if (!OF_hasprop(gpio, "gpio-controller"))
456 /* Node is not a GPIO controller. */
459 /* Initialize the software controlled pins. */
460 for (i = 0; i < A10_GPIO_PINS; i++) {
461 snprintf(sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME,
463 func = a10_gpio_get_function(sc, i);
464 sc->sc_gpio_pins[i].gp_pin = i;
465 sc->sc_gpio_pins[i].gp_caps = A10_GPIO_DEFAULT_CAPS;
466 sc->sc_gpio_pins[i].gp_flags = a10_gpio_func_flag(func);
468 sc->sc_gpio_npins = i;
470 device_add_child(dev, "gpioc", device_get_unit(dev));
471 device_add_child(dev, "gpiobus", device_get_unit(dev));
472 return (bus_generic_attach(dev));
476 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
478 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
483 a10_gpio_detach(device_t dev)
489 static device_method_t a10_gpio_methods[] = {
490 /* Device interface */
491 DEVMETHOD(device_probe, a10_gpio_probe),
492 DEVMETHOD(device_attach, a10_gpio_attach),
493 DEVMETHOD(device_detach, a10_gpio_detach),
496 DEVMETHOD(gpio_pin_max, a10_gpio_pin_max),
497 DEVMETHOD(gpio_pin_getname, a10_gpio_pin_getname),
498 DEVMETHOD(gpio_pin_getflags, a10_gpio_pin_getflags),
499 DEVMETHOD(gpio_pin_getcaps, a10_gpio_pin_getcaps),
500 DEVMETHOD(gpio_pin_setflags, a10_gpio_pin_setflags),
501 DEVMETHOD(gpio_pin_get, a10_gpio_pin_get),
502 DEVMETHOD(gpio_pin_set, a10_gpio_pin_set),
503 DEVMETHOD(gpio_pin_toggle, a10_gpio_pin_toggle),
508 static devclass_t a10_gpio_devclass;
510 static driver_t a10_gpio_driver = {
513 sizeof(struct a10_gpio_softc),
516 DRIVER_MODULE(a10_gpio, simplebus, a10_gpio_driver, a10_gpio_devclass, 0, 0);