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>
56 #include "rk30xx_grf.h"
57 #include "rk30xx_pmu.h"
60 * RK3188 has 4 banks of gpio.
62 * PA0 - PA7 | PB0 - PB7
63 * PC0 - PC7 | PD0 - PD7
66 #define RK30_GPIO_PINS 128
67 #define RK30_GPIO_DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | \
68 GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN)
70 #define RK30_GPIO_NONE 0
71 #define RK30_GPIO_PULLUP 1
72 #define RK30_GPIO_PULLDOWN 2
74 #define RK30_GPIO_INPUT 0
75 #define RK30_GPIO_OUTPUT 1
77 struct rk30_gpio_softc {
80 struct resource * sc_mem_res;
81 struct resource * sc_irq_res;
82 bus_space_tag_t sc_bst;
83 bus_space_handle_t sc_bsh;
86 struct gpio_pin sc_gpio_pins[RK30_GPIO_PINS];
89 #define RK30_GPIO_LOCK(_sc) mtx_lock(&_sc->sc_mtx)
90 #define RK30_GPIO_UNLOCK(_sc) mtx_unlock(&_sc->sc_mtx)
91 #define RK30_GPIO_LOCK_ASSERT(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED)
93 #define RK30_GPIO_SWPORT_DR 0x00
94 #define RK30_GPIO_SWPORT_DDR 0x04
95 #define RK30_GPIO_INTEN 0x30
96 #define RK30_GPIO_INTMASK 0x34
97 #define RK30_GPIO_INTTYPE_LEVEL 0x38
98 #define RK30_GPIO_INT_POLARITY 0x3c
99 #define RK30_GPIO_INT_STATUS 0x40
100 #define RK30_GPIO_INT_RAWSTATUS 0x44
101 #define RK30_GPIO_DEBOUNCE 0x48
102 #define RK30_GPIO_PORTS_EOI 0x4c
103 #define RK30_GPIO_EXT_PORT 0x50
104 #define RK30_GPIO_LS_SYNC 0x60
106 #define RK30_GPIO_WRITE(_sc, _off, _val) \
107 bus_space_write_4(_sc->sc_bst, _sc->sc_bsh, _off, _val)
108 #define RK30_GPIO_READ(_sc, _off) \
109 bus_space_read_4(_sc->sc_bst, _sc->sc_bsh, _off)
112 rk30_gpio_get_function(struct rk30_gpio_softc *sc, uint32_t pin)
114 uint32_t bank, func, offset;
120 func = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DDR);
127 rk30_gpio_func_flag(uint32_t nfunc)
131 case RK30_GPIO_INPUT:
132 return (GPIO_PIN_INPUT);
133 case RK30_GPIO_OUTPUT:
134 return (GPIO_PIN_OUTPUT);
140 rk30_gpio_set_function(struct rk30_gpio_softc *sc, uint32_t pin, uint32_t f)
142 uint32_t bank, data, offset;
144 /* Must be called with lock held. */
145 RK30_GPIO_LOCK_ASSERT(sc);
151 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DDR);
156 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DDR, data);
160 rk30_gpio_set_pud(struct rk30_gpio_softc *sc, uint32_t pin, uint32_t state)
166 /* Must be called with lock held. */
167 RK30_GPIO_LOCK_ASSERT(sc);
169 if (bank == 0 && pin < 12)
170 rk30_pmu_gpio_pud(pin, state);
172 rk30_grf_gpio_pud(bank, pin, state);
176 rk30_gpio_pin_configure(struct rk30_gpio_softc *sc, struct gpio_pin *pin,
183 * Manage input/output.
185 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
186 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
187 if (flags & GPIO_PIN_OUTPUT) {
188 pin->gp_flags |= GPIO_PIN_OUTPUT;
189 rk30_gpio_set_function(sc, pin->gp_pin,
192 pin->gp_flags |= GPIO_PIN_INPUT;
193 rk30_gpio_set_function(sc, pin->gp_pin,
198 /* Manage Pull-up/pull-down. */
199 pin->gp_flags &= ~(GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN);
200 if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) {
201 if (flags & GPIO_PIN_PULLUP) {
202 pin->gp_flags |= GPIO_PIN_PULLUP;
203 rk30_gpio_set_pud(sc, pin->gp_pin,
206 pin->gp_flags |= GPIO_PIN_PULLDOWN;
207 rk30_gpio_set_pud(sc, pin->gp_pin,
211 rk30_gpio_set_pud(sc, pin->gp_pin, RK30_GPIO_NONE);
213 RK30_GPIO_UNLOCK(sc);
217 rk30_gpio_pin_max(device_t dev, int *maxpin)
220 *maxpin = RK30_GPIO_PINS - 1;
225 rk30_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
227 struct rk30_gpio_softc *sc = device_get_softc(dev);
230 for (i = 0; i < sc->sc_gpio_npins; i++) {
231 if (sc->sc_gpio_pins[i].gp_pin == pin)
235 if (i >= sc->sc_gpio_npins)
239 *caps = sc->sc_gpio_pins[i].gp_caps;
240 RK30_GPIO_UNLOCK(sc);
246 rk30_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
248 struct rk30_gpio_softc *sc = device_get_softc(dev);
251 for (i = 0; i < sc->sc_gpio_npins; i++) {
252 if (sc->sc_gpio_pins[i].gp_pin == pin)
256 if (i >= sc->sc_gpio_npins)
260 *flags = sc->sc_gpio_pins[i].gp_flags;
261 RK30_GPIO_UNLOCK(sc);
267 rk30_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
269 struct rk30_gpio_softc *sc = device_get_softc(dev);
272 for (i = 0; i < sc->sc_gpio_npins; i++) {
273 if (sc->sc_gpio_pins[i].gp_pin == pin)
277 if (i >= sc->sc_gpio_npins)
281 memcpy(name, sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME);
282 RK30_GPIO_UNLOCK(sc);
288 rk30_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
290 struct rk30_gpio_softc *sc = device_get_softc(dev);
293 for (i = 0; i < sc->sc_gpio_npins; i++) {
294 if (sc->sc_gpio_pins[i].gp_pin == pin)
298 if (i >= sc->sc_gpio_npins)
301 /* Check for unwanted flags. */
302 if ((flags & sc->sc_gpio_pins[i].gp_caps) != flags)
305 /* Can't mix input/output together. */
306 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) ==
307 (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT))
310 /* Can't mix pull-up/pull-down together. */
311 if ((flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) ==
312 (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN))
315 rk30_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags);
321 rk30_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
323 struct rk30_gpio_softc *sc = device_get_softc(dev);
324 uint32_t bank, offset, data;
327 for (i = 0; i < sc->sc_gpio_npins; i++) {
328 if (sc->sc_gpio_pins[i].gp_pin == pin)
332 if (i >= sc->sc_gpio_npins)
340 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DDR);
342 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DDR, data);
344 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DR);
349 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DR, data);
350 RK30_GPIO_UNLOCK(sc);
356 rk30_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
358 struct rk30_gpio_softc *sc = device_get_softc(dev);
359 uint32_t bank, offset, reg_data;
362 for (i = 0; i < sc->sc_gpio_npins; i++) {
363 if (sc->sc_gpio_pins[i].gp_pin == pin)
367 if (i >= sc->sc_gpio_npins)
375 reg_data = RK30_GPIO_READ(sc, RK30_GPIO_EXT_PORT);
376 RK30_GPIO_UNLOCK(sc);
377 *val = (reg_data & offset) ? 1 : 0;
383 rk30_gpio_pin_toggle(device_t dev, uint32_t pin)
385 struct rk30_gpio_softc *sc = device_get_softc(dev);
386 uint32_t bank, data, offset;
389 for (i = 0; i < sc->sc_gpio_npins; i++) {
390 if (sc->sc_gpio_pins[i].gp_pin == pin)
394 if (i >= sc->sc_gpio_npins)
402 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DDR);
407 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DDR, data);
409 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DR);
414 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DR, data);
415 RK30_GPIO_UNLOCK(sc);
421 rk30_gpio_probe(device_t dev)
424 if (!ofw_bus_is_compatible(dev, "rockchip,rk30xx-gpio"))
427 device_set_desc(dev, "Rockchip RK30XX GPIO controller");
428 return (BUS_PROBE_DEFAULT);
432 rk30_gpio_attach(device_t dev)
434 struct rk30_gpio_softc *sc = device_get_softc(dev);
441 mtx_init(&sc->sc_mtx, "rk30 gpio", "gpio", MTX_DEF);
444 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
446 if (!sc->sc_mem_res) {
447 device_printf(dev, "cannot allocate memory window\n");
451 sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
452 sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
455 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
457 if (!sc->sc_irq_res) {
458 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
459 device_printf(dev, "cannot allocate interrupt\n");
464 gpio = ofw_bus_get_node(sc->sc_dev);
466 if (!OF_hasprop(gpio, "gpio-controller"))
467 /* Node is not a GPIO controller. */
470 /* Initialize the software controlled pins. */
471 for (i = 0; i < RK30_GPIO_PINS; i++) {
472 snprintf(sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME,
474 func = rk30_gpio_get_function(sc, i);
475 sc->sc_gpio_pins[i].gp_pin = i;
476 sc->sc_gpio_pins[i].gp_caps = RK30_GPIO_DEFAULT_CAPS;
477 sc->sc_gpio_pins[i].gp_flags = rk30_gpio_func_flag(func);
479 sc->sc_gpio_npins = i;
481 device_add_child(dev, "gpioc", device_get_unit(dev));
482 device_add_child(dev, "gpiobus", device_get_unit(dev));
483 return (bus_generic_attach(dev));
487 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
489 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
494 rk30_gpio_detach(device_t dev)
500 static device_method_t rk30_gpio_methods[] = {
501 /* Device interface */
502 DEVMETHOD(device_probe, rk30_gpio_probe),
503 DEVMETHOD(device_attach, rk30_gpio_attach),
504 DEVMETHOD(device_detach, rk30_gpio_detach),
507 DEVMETHOD(gpio_pin_max, rk30_gpio_pin_max),
508 DEVMETHOD(gpio_pin_getname, rk30_gpio_pin_getname),
509 DEVMETHOD(gpio_pin_getflags, rk30_gpio_pin_getflags),
510 DEVMETHOD(gpio_pin_getcaps, rk30_gpio_pin_getcaps),
511 DEVMETHOD(gpio_pin_setflags, rk30_gpio_pin_setflags),
512 DEVMETHOD(gpio_pin_get, rk30_gpio_pin_get),
513 DEVMETHOD(gpio_pin_set, rk30_gpio_pin_set),
514 DEVMETHOD(gpio_pin_toggle, rk30_gpio_pin_toggle),
519 static devclass_t rk30_gpio_devclass;
521 static driver_t rk30_gpio_driver = {
524 sizeof(struct rk30_gpio_softc),
527 DRIVER_MODULE(rk30_gpio, simplebus, rk30_gpio_driver, rk30_gpio_devclass, 0, 0);