2 * Copyright (c) 2013 Ganbold Tsagaankhuu <ganbold@freebsd.org>
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 static struct rk30_gpio_softc *rk30_gpio_sc = NULL;
91 typedef int (*gpios_phandler_t)(phandle_t, pcell_t *, int);
93 struct gpio_ctrl_entry {
95 gpios_phandler_t handler;
98 int rk30_gpios_prop_handle(phandle_t ctrl, pcell_t *gpios, int len);
99 static int rk30_gpio_init(void);
101 struct gpio_ctrl_entry gpio_controllers[] = {
102 { "rockchip,rk30xx-gpio", &rk30_gpios_prop_handle },
103 { "rockchip,rk30xx-gpio", &rk30_gpios_prop_handle },
104 { "rockchip,rk30xx-gpio", &rk30_gpios_prop_handle },
105 { "rockchip,rk30xx-gpio", &rk30_gpios_prop_handle },
109 #define RK30_GPIO_LOCK(_sc) mtx_lock(&_sc->sc_mtx)
110 #define RK30_GPIO_UNLOCK(_sc) mtx_unlock(&_sc->sc_mtx)
111 #define RK30_GPIO_LOCK_ASSERT(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED)
113 #define RK30_GPIO_SWPORT_DR 0x00
114 #define RK30_GPIO_SWPORT_DDR 0x04
115 #define RK30_GPIO_INTEN 0x30
116 #define RK30_GPIO_INTMASK 0x34
117 #define RK30_GPIO_INTTYPE_LEVEL 0x38
118 #define RK30_GPIO_INT_POLARITY 0x3c
119 #define RK30_GPIO_INT_STATUS 0x40
120 #define RK30_GPIO_INT_RAWSTATUS 0x44
121 #define RK30_GPIO_DEBOUNCE 0x48
122 #define RK30_GPIO_PORTS_EOI 0x4c
123 #define RK30_GPIO_EXT_PORT 0x50
124 #define RK30_GPIO_LS_SYNC 0x60
126 #define RK30_GPIO_WRITE(_sc, _off, _val) \
127 bus_space_write_4(_sc->sc_bst, _sc->sc_bsh, _off, _val)
128 #define RK30_GPIO_READ(_sc, _off) \
129 bus_space_read_4(_sc->sc_bst, _sc->sc_bsh, _off)
132 rk30_gpio_get_function(struct rk30_gpio_softc *sc, uint32_t pin)
134 uint32_t bank, func, offset;
140 func = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DDR);
147 rk30_gpio_func_flag(uint32_t nfunc)
151 case RK30_GPIO_INPUT:
152 return (GPIO_PIN_INPUT);
153 case RK30_GPIO_OUTPUT:
154 return (GPIO_PIN_OUTPUT);
160 rk30_gpio_set_function(struct rk30_gpio_softc *sc, uint32_t pin, uint32_t f)
162 uint32_t bank, data, offset;
164 /* Must be called with lock held. */
165 RK30_GPIO_LOCK_ASSERT(sc);
171 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DDR);
176 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DDR, data);
180 rk30_gpio_set_pud(struct rk30_gpio_softc *sc, uint32_t pin, uint32_t state)
186 /* Must be called with lock held. */
187 RK30_GPIO_LOCK_ASSERT(sc);
189 if (bank == 0 && pin < 12)
190 rk30_pmu_gpio_pud(pin, state);
192 rk30_grf_gpio_pud(bank, pin, state);
196 rk30_gpio_pin_configure(struct rk30_gpio_softc *sc, struct gpio_pin *pin,
203 * Manage input/output.
205 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
206 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
207 if (flags & GPIO_PIN_OUTPUT) {
208 pin->gp_flags |= GPIO_PIN_OUTPUT;
209 rk30_gpio_set_function(sc, pin->gp_pin,
212 pin->gp_flags |= GPIO_PIN_INPUT;
213 rk30_gpio_set_function(sc, pin->gp_pin,
218 /* Manage Pull-up/pull-down. */
219 pin->gp_flags &= ~(GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN);
220 if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) {
221 if (flags & GPIO_PIN_PULLUP) {
222 pin->gp_flags |= GPIO_PIN_PULLUP;
223 rk30_gpio_set_pud(sc, pin->gp_pin,
226 pin->gp_flags |= GPIO_PIN_PULLDOWN;
227 rk30_gpio_set_pud(sc, pin->gp_pin,
231 rk30_gpio_set_pud(sc, pin->gp_pin, RK30_GPIO_NONE);
233 RK30_GPIO_UNLOCK(sc);
237 rk30_gpio_pin_max(device_t dev, int *maxpin)
240 *maxpin = RK30_GPIO_PINS - 1;
245 rk30_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
247 struct rk30_gpio_softc *sc = device_get_softc(dev);
250 for (i = 0; i < sc->sc_gpio_npins; i++) {
251 if (sc->sc_gpio_pins[i].gp_pin == pin)
255 if (i >= sc->sc_gpio_npins)
259 *caps = sc->sc_gpio_pins[i].gp_caps;
260 RK30_GPIO_UNLOCK(sc);
266 rk30_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
268 struct rk30_gpio_softc *sc = device_get_softc(dev);
271 for (i = 0; i < sc->sc_gpio_npins; i++) {
272 if (sc->sc_gpio_pins[i].gp_pin == pin)
276 if (i >= sc->sc_gpio_npins)
280 *flags = sc->sc_gpio_pins[i].gp_flags;
281 RK30_GPIO_UNLOCK(sc);
287 rk30_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
289 struct rk30_gpio_softc *sc = device_get_softc(dev);
292 for (i = 0; i < sc->sc_gpio_npins; i++) {
293 if (sc->sc_gpio_pins[i].gp_pin == pin)
297 if (i >= sc->sc_gpio_npins)
301 memcpy(name, sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME);
302 RK30_GPIO_UNLOCK(sc);
308 rk30_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
310 struct rk30_gpio_softc *sc = device_get_softc(dev);
313 for (i = 0; i < sc->sc_gpio_npins; i++) {
314 if (sc->sc_gpio_pins[i].gp_pin == pin)
318 if (i >= sc->sc_gpio_npins)
321 rk30_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags);
327 rk30_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
329 struct rk30_gpio_softc *sc = device_get_softc(dev);
330 uint32_t bank, offset, data;
333 for (i = 0; i < sc->sc_gpio_npins; i++) {
334 if (sc->sc_gpio_pins[i].gp_pin == pin)
338 if (i >= sc->sc_gpio_npins)
346 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DDR);
348 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DDR, data);
350 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DR);
355 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DR, data);
356 RK30_GPIO_UNLOCK(sc);
362 rk30_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
364 struct rk30_gpio_softc *sc = device_get_softc(dev);
365 uint32_t bank, offset, reg_data;
368 for (i = 0; i < sc->sc_gpio_npins; i++) {
369 if (sc->sc_gpio_pins[i].gp_pin == pin)
373 if (i >= sc->sc_gpio_npins)
381 reg_data = RK30_GPIO_READ(sc, RK30_GPIO_EXT_PORT);
382 RK30_GPIO_UNLOCK(sc);
383 *val = (reg_data & offset) ? 1 : 0;
389 rk30_gpio_pin_toggle(device_t dev, uint32_t pin)
391 struct rk30_gpio_softc *sc = device_get_softc(dev);
392 uint32_t bank, data, offset;
395 for (i = 0; i < sc->sc_gpio_npins; i++) {
396 if (sc->sc_gpio_pins[i].gp_pin == pin)
400 if (i >= sc->sc_gpio_npins)
408 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DDR);
413 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DDR, data);
415 data = RK30_GPIO_READ(sc, RK30_GPIO_SWPORT_DR);
420 RK30_GPIO_WRITE(sc, RK30_GPIO_SWPORT_DR, data);
421 RK30_GPIO_UNLOCK(sc);
427 rk30_gpio_probe(device_t dev)
430 if (!ofw_bus_status_okay(dev))
433 if (!ofw_bus_is_compatible(dev, "rockchip,rk30xx-gpio"))
436 device_set_desc(dev, "Rockchip RK30XX GPIO controller");
437 return (BUS_PROBE_DEFAULT);
441 rk30_gpio_attach(device_t dev)
443 struct rk30_gpio_softc *sc = device_get_softc(dev);
453 mtx_init(&sc->sc_mtx, "rk30 gpio", "gpio", MTX_DEF);
456 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
458 if (!sc->sc_mem_res) {
459 device_printf(dev, "cannot allocate memory window\n");
463 sc->sc_bst = rman_get_bustag(sc->sc_mem_res);
464 sc->sc_bsh = rman_get_bushandle(sc->sc_mem_res);
467 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
469 if (!sc->sc_irq_res) {
470 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
471 device_printf(dev, "cannot allocate interrupt\n");
476 gpio = ofw_bus_get_node(sc->sc_dev);
478 if (!OF_hasprop(gpio, "gpio-controller"))
479 /* Node is not a GPIO controller. */
482 /* Initialize the software controlled pins. */
483 for (i = 0; i < RK30_GPIO_PINS; i++) {
484 snprintf(sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME,
486 func = rk30_gpio_get_function(sc, i);
487 sc->sc_gpio_pins[i].gp_pin = i;
488 sc->sc_gpio_pins[i].gp_caps = RK30_GPIO_DEFAULT_CAPS;
489 sc->sc_gpio_pins[i].gp_flags = rk30_gpio_func_flag(func);
491 sc->sc_gpio_npins = i;
493 device_add_child(dev, "gpioc", -1);
494 device_add_child(dev, "gpiobus", -1);
500 return (bus_generic_attach(dev));
504 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
506 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_mem_res);
511 rk30_gpio_detach(device_t dev)
517 static device_method_t rk30_gpio_methods[] = {
518 /* Device interface */
519 DEVMETHOD(device_probe, rk30_gpio_probe),
520 DEVMETHOD(device_attach, rk30_gpio_attach),
521 DEVMETHOD(device_detach, rk30_gpio_detach),
524 DEVMETHOD(gpio_pin_max, rk30_gpio_pin_max),
525 DEVMETHOD(gpio_pin_getname, rk30_gpio_pin_getname),
526 DEVMETHOD(gpio_pin_getflags, rk30_gpio_pin_getflags),
527 DEVMETHOD(gpio_pin_getcaps, rk30_gpio_pin_getcaps),
528 DEVMETHOD(gpio_pin_setflags, rk30_gpio_pin_setflags),
529 DEVMETHOD(gpio_pin_get, rk30_gpio_pin_get),
530 DEVMETHOD(gpio_pin_set, rk30_gpio_pin_set),
531 DEVMETHOD(gpio_pin_toggle, rk30_gpio_pin_toggle),
536 static devclass_t rk30_gpio_devclass;
538 static driver_t rk30_gpio_driver = {
541 sizeof(struct rk30_gpio_softc),
544 DRIVER_MODULE(rk30_gpio, simplebus, rk30_gpio_driver, rk30_gpio_devclass, 0, 0);
547 rk30_gpios_prop_handle(phandle_t ctrl, pcell_t *gpios, int len)
549 struct rk30_gpio_softc *sc;
551 int inc, t, tuples, tuple_size;
552 int dir, flags, pin, i;
553 u_long gpio_ctrl, size;
559 if (OF_getprop(ctrl, "#gpio-cells", &gpio_cells, sizeof(pcell_t)) < 0)
562 gpio_cells = fdt32_to_cpu(gpio_cells);
566 tuple_size = gpio_cells * sizeof(pcell_t) + sizeof(phandle_t);
567 tuples = len / tuple_size;
569 if (fdt_regsize(ctrl, &gpio_ctrl, &size))
573 * Skip controller reference, since controller's phandle is given
574 * explicitly (in a function argument).
576 inc = sizeof(ihandle_t) / sizeof(pcell_t);
578 for (t = 0; t < tuples; t++) {
579 pin = fdt32_to_cpu(gpios[0]);
580 dir = fdt32_to_cpu(gpios[1]);
581 flags = fdt32_to_cpu(gpios[2]);
583 for (i = 0; i < sc->sc_gpio_npins; i++) {
584 if (sc->sc_gpio_pins[i].gp_pin == pin)
587 if (i >= sc->sc_gpio_npins)
590 rk30_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags);
594 rk30_gpio_pin_set(sc->sc_dev, pin, RK30_GPIO_INPUT);
597 rk30_gpio_pin_set(sc->sc_dev, pin, RK30_GPIO_OUTPUT);
599 gpios += gpio_cells + inc;
605 #define MAX_PINS_PER_NODE 5
606 #define GPIOS_PROP_CELLS 4
611 phandle_t child, parent, root, ctrl;
612 pcell_t gpios[MAX_PINS_PER_NODE * GPIOS_PROP_CELLS];
613 struct gpio_ctrl_entry *e;
616 root = OF_finddevice("/");
620 /* Traverse through entire tree to find nodes with 'gpios' prop */
621 for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
623 /* Find a 'leaf'. Start the search from this node. */
624 while (OF_child(child)) {
626 child = OF_child(child);
628 if ((len = OF_getproplen(child, "gpios")) > 0) {
630 if (len > sizeof(gpios))
633 /* Get 'gpios' property. */
634 OF_getprop(child, "gpios", &gpios, len);
636 e = (struct gpio_ctrl_entry *)&gpio_controllers;
638 /* Find and call a handler. */
639 for (; e->compat; e++) {
641 * First cell of 'gpios' property should
642 * contain a ref. to a node defining GPIO
645 ctrl = OF_node_from_xref(fdt32_to_cpu(gpios[0]));
647 if (fdt_is_compatible(ctrl, e->compat))
648 /* Call a handler. */
649 if ((rv = e->handler(ctrl,
650 (pcell_t *)&gpios, len)))
655 if (OF_peer(child) == 0) {
656 /* No more siblings. */
658 parent = OF_parent(child);