2 * Copyright (c) 2010-2011, Aleksandr Rybalko <ray@ddteam.net>
3 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
4 * Copyright (c) 2009, 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 unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * GPIO driver for RT305X SoC.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
45 #include <sys/mutex.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <mips/rt305x/rt305xreg.h>
51 #include <mips/rt305x/rt305x_gpio.h>
52 #include <mips/rt305x/rt305x_gpiovar.h>
53 #include <mips/rt305x/rt305x_sysctlvar.h>
54 #include <dev/gpio/gpiobusvar.h>
59 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | \
60 GPIO_PIN_INVOUT | GPIO_PIN_REPORT )
62 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | \
69 static void rt305x_gpio_pin_configure(struct rt305x_gpio_softc *sc,
70 struct gpio_pin *pin, uint32_t flags);
75 static int rt305x_gpio_probe(device_t dev);
76 static int rt305x_gpio_attach(device_t dev);
77 static int rt305x_gpio_detach(device_t dev);
78 static int rt305x_gpio_intr(void *arg);
80 int rt305x_get_int_mask (device_t);
81 void rt305x_set_int_mask (device_t, uint32_t);
82 int rt305x_get_int_status(device_t);
83 void rt305x_set_int_status(device_t, uint32_t);
88 static device_t rt305x_gpio_get_bus(device_t);
89 static int rt305x_gpio_pin_max(device_t dev, int *maxpin);
90 static int rt305x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
91 static int rt305x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
93 static int rt305x_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
94 static int rt305x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
95 static int rt305x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
96 static int rt305x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
97 static int rt305x_gpio_pin_toggle(device_t dev, uint32_t pin);
100 rt305x_gpio_pin_configure(struct rt305x_gpio_softc *sc, struct gpio_pin *pin,
106 * Manage input/output
108 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
109 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
110 if (flags & GPIO_PIN_OUTPUT) {
111 pin->gp_flags |= GPIO_PIN_OUTPUT;
112 GPIO_BIT_SET(sc, pin->gp_pin, DIR);
115 pin->gp_flags |= GPIO_PIN_INPUT;
116 GPIO_BIT_CLR(sc, pin->gp_pin, DIR);
120 if (flags & GPIO_PIN_INVOUT) {
121 pin->gp_flags |= GPIO_PIN_INVOUT;
122 GPIO_BIT_SET(sc, pin->gp_pin, POL);
125 pin->gp_flags &= ~GPIO_PIN_INVOUT;
126 GPIO_BIT_CLR(sc, pin->gp_pin, POL);
129 if (flags & GPIO_PIN_INVIN) {
130 pin->gp_flags |= GPIO_PIN_INVIN;
131 GPIO_BIT_SET(sc, pin->gp_pin, POL);
134 pin->gp_flags &= ~GPIO_PIN_INVIN;
135 GPIO_BIT_CLR(sc, pin->gp_pin, POL);
139 /* Enable interrupt bits for rising/falling transitions */
140 if (flags & GPIO_PIN_REPORT) {
141 pin->gp_flags |= GPIO_PIN_REPORT;
142 GPIO_BIT_SET(sc, pin->gp_pin, RENA);
143 GPIO_BIT_SET(sc, pin->gp_pin, FENA);
144 device_printf(sc->dev, "Will report interrupt on pin %d\n",
149 pin->gp_flags &= ~GPIO_PIN_REPORT;
150 GPIO_BIT_CLR(sc, pin->gp_pin, RENA);
151 GPIO_BIT_CLR(sc, pin->gp_pin, FENA);
154 /* Disable generating interrupts for now */
155 GPIO_BIT_CLR(sc, pin->gp_pin, RENA);
156 GPIO_BIT_CLR(sc, pin->gp_pin, FENA);
163 rt305x_gpio_get_bus(device_t dev)
165 struct rt305x_gpio_softc *sc;
167 sc = device_get_softc(dev);
173 rt305x_gpio_pin_max(device_t dev, int *maxpin)
181 rt305x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
183 struct rt305x_gpio_softc *sc = device_get_softc(dev);
186 for (i = 0; i < sc->gpio_npins; i++) {
187 if (sc->gpio_pins[i].gp_pin == pin)
191 if (i >= sc->gpio_npins)
195 *caps = sc->gpio_pins[i].gp_caps;
202 rt305x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
204 struct rt305x_gpio_softc *sc = device_get_softc(dev);
207 for (i = 0; i < sc->gpio_npins; i++) {
208 if (sc->gpio_pins[i].gp_pin == pin)
212 if (i >= sc->gpio_npins)
216 *flags = sc->gpio_pins[i].gp_flags;
223 rt305x_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
225 struct rt305x_gpio_softc *sc = device_get_softc(dev);
228 for (i = 0; i < sc->gpio_npins; i++) {
229 if (sc->gpio_pins[i].gp_pin == pin)
233 if (i >= sc->gpio_npins)
237 memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
244 rt305x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
247 struct rt305x_gpio_softc *sc = device_get_softc(dev);
249 for (i = 0; i < sc->gpio_npins; i++) {
250 if (sc->gpio_pins[i].gp_pin == pin)
254 if (i >= sc->gpio_npins)
257 rt305x_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
263 rt305x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
265 struct rt305x_gpio_softc *sc = device_get_softc(dev);
268 for (i = 0; i < sc->gpio_npins; i++) {
269 if (sc->gpio_pins[i].gp_pin == pin)
273 if (i >= sc->gpio_npins)
278 if (value) GPIO_BIT_SET(sc, i, DATA);
279 else GPIO_BIT_CLR(sc, i, DATA);
286 rt305x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
288 struct rt305x_gpio_softc *sc = device_get_softc(dev);
291 for (i = 0; i < sc->gpio_npins; i++) {
292 if (sc->gpio_pins[i].gp_pin == pin)
296 if (i >= sc->gpio_npins)
300 *val = GPIO_BIT_GET(sc, i, DATA);
307 rt305x_gpio_pin_toggle(device_t dev, uint32_t pin)
310 struct rt305x_gpio_softc *sc = device_get_softc(dev);
312 for (i = 0; i < sc->gpio_npins; i++) {
313 if (sc->gpio_pins[i].gp_pin == pin)
317 if (i >= sc->gpio_npins)
321 GPIO_BIT_SET(sc, i, TOG);
328 rt305x_gpio_intr(void *arg)
330 struct rt305x_gpio_softc *sc = arg;
334 uint64_t input, value;
341 /* Read all reported pins */
342 input = GPIO_READ_ALL(sc, INT);
343 /* Clear int status */
344 GPIO_WRITE_ALL(sc, INT, input);
345 /* Clear report for OUTs */
346 input &= ~GPIO_READ_ALL(sc, DIR);
347 value = input & GPIO_READ_ALL(sc, DATA);
349 if (!input) goto intr_done;
352 /* if reset_gpio and this pin is input */
353 if (sc->reset_gpio >= 0 && (input & (1 << sc->reset_gpio))) {
354 /* get reset_gpio pin value */
355 reset_pin = (value & (1 << sc->reset_gpio))?1:0;
356 if ( sc->reset_gpio_last != reset_pin ) {
358 * if now reset is high, check how long
359 * and do reset if less than 2 seconds
362 (time_uptime - sc->reset_gpio_ontime) < 2 )
365 sc->reset_gpio_last = reset_pin;
366 sc->reset_gpio_ontime = time_uptime;
370 for ( i = 0; i < NGPIO; i ++ )
372 /* Next if output pin */
373 if ( !(( input >> i) & 1) ) continue;
375 if ( (((value & input) >> i) & 1) != sc->gpio_pins[i].gp_last )
377 /* !system=GPIO subsystem=pin7 type=PIN_HIGH period=3 */
378 snprintf(notify , sizeof(notify ), "period=%d",
379 (uint32_t)time_uptime - sc->gpio_pins[i].gp_time);
380 snprintf(pinname, sizeof(pinname), "pin%02d", i);
381 devctl_notify("GPIO", pinname,
382 (((value & input) >> i) & 1)?"PIN_HIGH":"PIN_LOW",
384 printf("GPIO[%s] %s %s\n", pinname,
385 (((value & input) >> i) & 1)?"PIN_HIGH":"PIN_LOW",
387 sc->gpio_pins[i].gp_last = ((value & input) >> i) & 1;
388 sc->gpio_pins[i].gp_time = time_uptime;
395 return (FILTER_HANDLED);
399 rt305x_gpio_probe(device_t dev)
401 device_set_desc(dev, "RT305X GPIO driver");
406 rt305x_gpio_init(device_t dev)
408 uint64_t avl = ~0ULL;
409 uint32_t gmode = rt305x_sysctl_get(SYSCTL_GPIOMODE);
410 if (!(gmode & SYSCTL_GPIOMODE_RGMII_GPIO_MODE))
411 avl &= ~RGMII_GPIO_MODE_MASK;
412 if (!(gmode & SYSCTL_GPIOMODE_SDRAM_GPIO_MODE))
413 avl &= ~SDRAM_GPIO_MODE_MASK;
414 if (!(gmode & SYSCTL_GPIOMODE_MDIO_GPIO_MODE))
415 avl &= ~MDIO_GPIO_MODE_MASK;
416 if (!(gmode & SYSCTL_GPIOMODE_JTAG_GPIO_MODE))
417 avl &= ~JTAG_GPIO_MODE_MASK;
418 if (!(gmode & SYSCTL_GPIOMODE_UARTL_GPIO_MODE))
419 avl &= ~UARTL_GPIO_MODE_MASK;
420 if (!(gmode & SYSCTL_GPIOMODE_SPI_GPIO_MODE))
421 avl &= ~SPI_GPIO_MODE_MASK;
422 if (!(gmode & SYSCTL_GPIOMODE_I2C_GPIO_MODE))
423 avl &= ~I2C_GPIO_MODE_MASK;
424 if ((gmode & SYSCTL_GPIOMODE_UARTF_SHARE_MODE_GPIO) !=
425 SYSCTL_GPIOMODE_UARTF_SHARE_MODE_GPIO)
426 avl &= ~I2C_GPIO_MODE_MASK;
427 /* D-Link DAP-1350 Board have
433 * 00000001 10000000 01111111 11111110
439 #define DAP1350_RESET_GPIO 10
442 rt305x_gpio_attach(device_t dev)
444 struct rt305x_gpio_softc *sc = device_get_softc(dev);
446 uint64_t avlpins = 0;
447 sc->reset_gpio = DAP1350_RESET_GPIO;
449 KASSERT((device_get_unit(dev) == 0),
450 ("rt305x_gpio_gpio: Only one gpio module supported"));
452 mtx_init(&sc->gpio_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
454 /* Map control/status registers. */
455 sc->gpio_mem_rid = 0;
456 sc->gpio_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
457 &sc->gpio_mem_rid, RF_ACTIVE);
459 if (sc->gpio_mem_res == NULL) {
460 device_printf(dev, "couldn't map memory\n");
461 rt305x_gpio_detach(dev);
465 if ((sc->gpio_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
466 &sc->gpio_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
467 device_printf(dev, "unable to allocate IRQ resource\n");
468 rt305x_gpio_detach(dev);
472 if ((bus_setup_intr(dev, sc->gpio_irq_res, INTR_TYPE_MISC,
473 /* rt305x_gpio_filter, */
474 rt305x_gpio_intr, NULL, sc, &sc->gpio_ih))) {
476 "WARNING: unable to register interrupt handler\n");
477 rt305x_gpio_detach(dev);
482 avlpins = rt305x_gpio_init(dev);
484 /* Configure all pins as input */
485 /* disable interrupts for all pins */
488 sc->gpio_npins = NGPIO;
489 resource_int_value(device_get_name(dev), device_get_unit(dev),
490 "pins", &sc->gpio_npins);
492 for (i = 0; i < sc->gpio_npins; i++) {
493 sc->gpio_pins[i].gp_pin = i;
494 sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
495 sc->gpio_pins[i].gp_flags = 0;
498 /* Setup reset pin interrupt */
499 if (TUNABLE_INT_FETCH("reset_gpio", &sc->reset_gpio)) {
500 device_printf(dev, "\tHinted reset_gpio %d\n", sc->reset_gpio);
503 if (sc->reset_gpio != -1) {
504 rt305x_gpio_pin_setflags(dev, sc->reset_gpio,
505 GPIO_PIN_INPUT|GPIO_PIN_INVOUT|
506 GPIO_PIN_INVOUT|GPIO_PIN_REPORT);
507 device_printf(dev, "\tUse reset_gpio %d\n", sc->reset_gpio);
510 if (sc->reset_gpio != -1) {
511 rt305x_gpio_pin_setflags(dev, sc->reset_gpio,
512 GPIO_PIN_INPUT|GPIO_PIN_INVOUT);
513 device_printf(dev, "\tUse reset_gpio %d\n", sc->reset_gpio);
516 sc->busdev = gpiobus_attach_bus(dev);
517 if (sc->busdev == NULL) {
518 rt305x_gpio_detach(dev);
526 rt305x_gpio_detach(device_t dev)
528 struct rt305x_gpio_softc *sc = device_get_softc(dev);
530 KASSERT(mtx_initialized(&sc->gpio_mtx), ("gpio mutex not initialized"));
532 gpiobus_detach_bus(dev);
534 bus_teardown_intr(dev, sc->gpio_irq_res, sc->gpio_ih);
535 if (sc->gpio_irq_res)
536 bus_release_resource(dev, SYS_RES_IRQ, sc->gpio_irq_rid,
538 if (sc->gpio_mem_res)
539 bus_release_resource(dev, SYS_RES_MEMORY, sc->gpio_mem_rid,
541 mtx_destroy(&sc->gpio_mtx);
547 static struct resource *
548 rt305x_gpio_alloc_resource(device_t bus, device_t child, int type, int *rid,
549 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
551 struct obio_softc *sc = device_get_softc(bus);
560 printf("%s: unknown resource type %d\n", __func__, type);
564 rv = rman_reserve_resource(rm, start, end, count, flags, child);
566 printf("%s: could not reserve resource\n", __func__);
570 rman_set_rid(rv, *rid);
576 rt305x_gpio_activate_resource(device_t bus, device_t child, int type, int rid,
580 return (rman_activate_resource(r));
584 rt305x_gpio_deactivate_resource(device_t bus, device_t child, int type, int rid,
588 return (rman_deactivate_resource(r));
592 rt305x_gpio_release_resource(device_t dev, device_t child, int type,
593 int rid, struct resource *r)
595 rman_release_resource(r);
600 static device_method_t rt305x_gpio_methods[] = {
601 DEVMETHOD(device_probe, rt305x_gpio_probe),
602 DEVMETHOD(device_attach, rt305x_gpio_attach),
603 DEVMETHOD(device_detach, rt305x_gpio_detach),
606 DEVMETHOD(gpio_get_bus, rt305x_gpio_get_bus),
607 DEVMETHOD(gpio_pin_max, rt305x_gpio_pin_max),
608 DEVMETHOD(gpio_pin_getname, rt305x_gpio_pin_getname),
609 DEVMETHOD(gpio_pin_getflags, rt305x_gpio_pin_getflags),
610 DEVMETHOD(gpio_pin_getcaps, rt305x_gpio_pin_getcaps),
611 DEVMETHOD(gpio_pin_setflags, rt305x_gpio_pin_setflags),
612 DEVMETHOD(gpio_pin_get, rt305x_gpio_pin_get),
613 DEVMETHOD(gpio_pin_set, rt305x_gpio_pin_set),
614 DEVMETHOD(gpio_pin_toggle, rt305x_gpio_pin_toggle),
618 static driver_t rt305x_gpio_driver = {
621 sizeof(struct rt305x_gpio_softc),
623 static devclass_t rt305x_gpio_devclass;
625 DRIVER_MODULE(rt305x_gpio, obio, rt305x_gpio_driver,
626 rt305x_gpio_devclass, 0, 0);