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>
58 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | \
59 GPIO_PIN_INVOUT | GPIO_PIN_REPORT )
61 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_INVIN | \
68 static void rt305x_gpio_pin_configure(struct rt305x_gpio_softc *sc,
69 struct gpio_pin *pin, uint32_t flags);
74 static int rt305x_gpio_probe(device_t dev);
75 static int rt305x_gpio_attach(device_t dev);
76 static int rt305x_gpio_detach(device_t dev);
77 static int rt305x_gpio_intr(void *arg);
79 int rt305x_get_int_mask (device_t);
80 void rt305x_set_int_mask (device_t, uint32_t);
81 int rt305x_get_int_status(device_t);
82 void rt305x_set_int_status(device_t, uint32_t);
87 static int rt305x_gpio_pin_max(device_t dev, int *maxpin);
88 static int rt305x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
89 static int rt305x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
91 static int rt305x_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
92 static int rt305x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
93 static int rt305x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
94 static int rt305x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
95 static int rt305x_gpio_pin_toggle(device_t dev, uint32_t pin);
98 rt305x_gpio_pin_configure(struct rt305x_gpio_softc *sc, struct gpio_pin *pin,
104 * Manage input/output
106 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
107 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
108 if (flags & GPIO_PIN_OUTPUT) {
109 pin->gp_flags |= GPIO_PIN_OUTPUT;
110 GPIO_BIT_SET(sc, pin->gp_pin, DIR);
113 pin->gp_flags |= GPIO_PIN_INPUT;
114 GPIO_BIT_CLR(sc, pin->gp_pin, DIR);
118 if (flags & GPIO_PIN_INVOUT) {
119 pin->gp_flags |= GPIO_PIN_INVOUT;
120 GPIO_BIT_SET(sc, pin->gp_pin, POL);
123 pin->gp_flags &= ~GPIO_PIN_INVOUT;
124 GPIO_BIT_CLR(sc, pin->gp_pin, POL);
127 if (flags & GPIO_PIN_INVIN) {
128 pin->gp_flags |= GPIO_PIN_INVIN;
129 GPIO_BIT_SET(sc, pin->gp_pin, POL);
132 pin->gp_flags &= ~GPIO_PIN_INVIN;
133 GPIO_BIT_CLR(sc, pin->gp_pin, POL);
137 /* Enable interrupt bits for rising/falling transitions */
138 if (flags & GPIO_PIN_REPORT) {
139 pin->gp_flags |= GPIO_PIN_REPORT;
140 GPIO_BIT_SET(sc, pin->gp_pin, RENA);
141 GPIO_BIT_SET(sc, pin->gp_pin, FENA);
142 device_printf(sc->dev, "Will report interrupt on pin %d\n",
147 pin->gp_flags &= ~GPIO_PIN_REPORT;
148 GPIO_BIT_CLR(sc, pin->gp_pin, RENA);
149 GPIO_BIT_CLR(sc, pin->gp_pin, FENA);
152 /* Disable generating interrupts for now */
153 GPIO_BIT_CLR(sc, pin->gp_pin, RENA);
154 GPIO_BIT_CLR(sc, pin->gp_pin, FENA);
161 rt305x_gpio_pin_max(device_t dev, int *maxpin)
169 rt305x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
171 struct rt305x_gpio_softc *sc = device_get_softc(dev);
174 for (i = 0; i < sc->gpio_npins; i++) {
175 if (sc->gpio_pins[i].gp_pin == pin)
179 if (i >= sc->gpio_npins)
183 *caps = sc->gpio_pins[i].gp_caps;
190 rt305x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
192 struct rt305x_gpio_softc *sc = device_get_softc(dev);
195 for (i = 0; i < sc->gpio_npins; i++) {
196 if (sc->gpio_pins[i].gp_pin == pin)
200 if (i >= sc->gpio_npins)
204 *flags = sc->gpio_pins[i].gp_flags;
211 rt305x_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
213 struct rt305x_gpio_softc *sc = device_get_softc(dev);
216 for (i = 0; i < sc->gpio_npins; i++) {
217 if (sc->gpio_pins[i].gp_pin == pin)
221 if (i >= sc->gpio_npins)
225 memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
232 rt305x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
235 struct rt305x_gpio_softc *sc = device_get_softc(dev);
237 for (i = 0; i < sc->gpio_npins; i++) {
238 if (sc->gpio_pins[i].gp_pin == pin)
242 if (i >= sc->gpio_npins)
245 rt305x_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
251 rt305x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
253 struct rt305x_gpio_softc *sc = device_get_softc(dev);
256 for (i = 0; i < sc->gpio_npins; i++) {
257 if (sc->gpio_pins[i].gp_pin == pin)
261 if (i >= sc->gpio_npins)
266 if (value) GPIO_BIT_SET(sc, i, DATA);
267 else GPIO_BIT_CLR(sc, i, DATA);
274 rt305x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
276 struct rt305x_gpio_softc *sc = device_get_softc(dev);
279 for (i = 0; i < sc->gpio_npins; i++) {
280 if (sc->gpio_pins[i].gp_pin == pin)
284 if (i >= sc->gpio_npins)
288 *val = GPIO_BIT_GET(sc, i, DATA);
295 rt305x_gpio_pin_toggle(device_t dev, uint32_t pin)
298 struct rt305x_gpio_softc *sc = device_get_softc(dev);
300 for (i = 0; i < sc->gpio_npins; i++) {
301 if (sc->gpio_pins[i].gp_pin == pin)
305 if (i >= sc->gpio_npins)
309 GPIO_BIT_SET(sc, i, TOG);
316 rt305x_gpio_intr(void *arg)
318 struct rt305x_gpio_softc *sc = arg;
322 uint64_t input, value;
329 /* Read all reported pins */
330 input = GPIO_READ_ALL(sc, INT);
331 /* Clear int status */
332 GPIO_WRITE_ALL(sc, INT, input);
333 /* Clear report for OUTs */
334 input &= ~GPIO_READ_ALL(sc, DIR);
335 value = input & GPIO_READ_ALL(sc, DATA);
337 if (!input) goto intr_done;
340 /* if reset_gpio and this pin is input */
341 if (sc->reset_gpio >= 0 && (input & (1 << sc->reset_gpio))) {
342 /* get reset_gpio pin value */
343 reset_pin = (value & (1 << sc->reset_gpio))?1:0;
344 if ( sc->reset_gpio_last != reset_pin ) {
346 * if now reset is high, check how long
347 * and do reset if less than 2 seconds
350 (time_uptime - sc->reset_gpio_ontime) < 2 )
353 sc->reset_gpio_last = reset_pin;
354 sc->reset_gpio_ontime = time_uptime;
358 for ( i = 0; i < NGPIO; i ++ )
360 /* Next if output pin */
361 if ( !(( input >> i) & 1) ) continue;
363 if ( (((value & input) >> i) & 1) != sc->gpio_pins[i].gp_last )
365 /* !system=GPIO subsystem=pin7 type=PIN_HIGH period=3 */
366 snprintf(notify , sizeof(notify ), "period=%d",
367 (uint32_t)time_uptime - sc->gpio_pins[i].gp_time);
368 snprintf(pinname, sizeof(pinname), "pin%02d", i);
369 devctl_notify("GPIO", pinname,
370 (((value & input) >> i) & 1)?"PIN_HIGH":"PIN_LOW",
372 printf("GPIO[%s] %s %s\n", pinname,
373 (((value & input) >> i) & 1)?"PIN_HIGH":"PIN_LOW",
375 sc->gpio_pins[i].gp_last = ((value & input) >> i) & 1;
376 sc->gpio_pins[i].gp_time = time_uptime;
383 return (FILTER_HANDLED);
387 rt305x_gpio_probe(device_t dev)
389 device_set_desc(dev, "RT305X GPIO driver");
394 rt305x_gpio_init(device_t dev)
396 uint64_t avl = ~0ULL;
397 uint32_t gmode = rt305x_sysctl_get(SYSCTL_GPIOMODE);
398 if (!(gmode & SYSCTL_GPIOMODE_RGMII_GPIO_MODE))
399 avl &= ~RGMII_GPIO_MODE_MASK;
400 if (!(gmode & SYSCTL_GPIOMODE_SDRAM_GPIO_MODE))
401 avl &= ~SDRAM_GPIO_MODE_MASK;
402 if (!(gmode & SYSCTL_GPIOMODE_MDIO_GPIO_MODE))
403 avl &= ~MDIO_GPIO_MODE_MASK;
404 if (!(gmode & SYSCTL_GPIOMODE_JTAG_GPIO_MODE))
405 avl &= ~JTAG_GPIO_MODE_MASK;
406 if (!(gmode & SYSCTL_GPIOMODE_UARTL_GPIO_MODE))
407 avl &= ~UARTL_GPIO_MODE_MASK;
408 if (!(gmode & SYSCTL_GPIOMODE_SPI_GPIO_MODE))
409 avl &= ~SPI_GPIO_MODE_MASK;
410 if (!(gmode & SYSCTL_GPIOMODE_I2C_GPIO_MODE))
411 avl &= ~I2C_GPIO_MODE_MASK;
412 if ((gmode & SYSCTL_GPIOMODE_UARTF_SHARE_MODE_GPIO) !=
413 SYSCTL_GPIOMODE_UARTF_SHARE_MODE_GPIO)
414 avl &= ~I2C_GPIO_MODE_MASK;
415 /* D-Link DAP-1350 Board have
421 * 00000001 10000000 01111111 11111110
427 #define DAP1350_RESET_GPIO 10
430 rt305x_gpio_attach(device_t dev)
432 struct rt305x_gpio_softc *sc = device_get_softc(dev);
434 uint64_t avlpins = 0;
435 sc->reset_gpio = DAP1350_RESET_GPIO;
437 KASSERT((device_get_unit(dev) == 0),
438 ("rt305x_gpio_gpio: Only one gpio module supported"));
440 mtx_init(&sc->gpio_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
442 /* Map control/status registers. */
443 sc->gpio_mem_rid = 0;
444 sc->gpio_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
445 &sc->gpio_mem_rid, RF_ACTIVE);
447 if (sc->gpio_mem_res == NULL) {
448 device_printf(dev, "couldn't map memory\n");
450 rt305x_gpio_detach(dev);
454 if ((sc->gpio_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
455 &sc->gpio_irq_rid, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
456 device_printf(dev, "unable to allocate IRQ resource\n");
460 if ((bus_setup_intr(dev, sc->gpio_irq_res, INTR_TYPE_MISC,
461 /* rt305x_gpio_filter, */
462 rt305x_gpio_intr, NULL, sc, &sc->gpio_ih))) {
464 "WARNING: unable to register interrupt handler\n");
469 avlpins = rt305x_gpio_init(dev);
471 /* Configure all pins as input */
472 /* disable interrupts for all pins */
475 sc->gpio_npins = NGPIO;
476 resource_int_value(device_get_name(dev), device_get_unit(dev),
477 "pins", &sc->gpio_npins);
479 for (i = 0; i < sc->gpio_npins; i++) {
480 sc->gpio_pins[i].gp_pin = i;
481 sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
482 sc->gpio_pins[i].gp_flags = 0;
485 /* Setup reset pin interrupt */
486 if (TUNABLE_INT_FETCH("reset_gpio", &sc->reset_gpio)) {
487 device_printf(dev, "\tHinted reset_gpio %d\n", sc->reset_gpio);
490 if (sc->reset_gpio != -1) {
491 rt305x_gpio_pin_setflags(dev, sc->reset_gpio,
492 GPIO_PIN_INPUT|GPIO_PIN_INVOUT|
493 GPIO_PIN_INVOUT|GPIO_PIN_REPORT);
494 device_printf(dev, "\tUse reset_gpio %d\n", sc->reset_gpio);
497 if (sc->reset_gpio != -1) {
498 rt305x_gpio_pin_setflags(dev, sc->reset_gpio,
499 GPIO_PIN_INPUT|GPIO_PIN_INVOUT);
500 device_printf(dev, "\tUse reset_gpio %d\n", sc->reset_gpio);
504 device_add_child(dev, "gpioc", -1);
505 device_add_child(dev, "gpiobus", -1);
507 return (bus_generic_attach(dev));
511 rt305x_gpio_detach(device_t dev)
513 struct rt305x_gpio_softc *sc = device_get_softc(dev);
515 KASSERT(mtx_initialized(&sc->gpio_mtx), ("gpio mutex not initialized"));
517 bus_generic_detach(dev);
519 if (sc->gpio_mem_res)
520 bus_release_resource(dev, SYS_RES_MEMORY, sc->gpio_mem_rid,
523 mtx_destroy(&sc->gpio_mtx);
529 static struct resource *
530 rt305x_gpio_alloc_resource(device_t bus, device_t child, int type, int *rid,
531 u_long start, u_long end, u_long count, u_int flags)
533 struct obio_softc *sc = device_get_softc(bus);
542 printf("%s: unknown resource type %d\n", __func__, type);
546 rv = rman_reserve_resource(rm, start, end, count, flags, child);
548 printf("%s: could not reserve resource\n", __func__);
552 rman_set_rid(rv, *rid);
558 rt305x_gpio_activate_resource(device_t bus, device_t child, int type, int rid,
562 return (rman_activate_resource(r));
566 rt305x_gpio_deactivate_resource(device_t bus, device_t child, int type, int rid,
570 return (rman_deactivate_resource(r));
574 rt305x_gpio_release_resource(device_t dev, device_t child, int type,
575 int rid, struct resource *r)
577 rman_release_resource(r);
582 static device_method_t rt305x_gpio_methods[] = {
583 DEVMETHOD(device_probe, rt305x_gpio_probe),
584 DEVMETHOD(device_attach, rt305x_gpio_attach),
585 DEVMETHOD(device_detach, rt305x_gpio_detach),
588 DEVMETHOD(gpio_pin_max, rt305x_gpio_pin_max),
589 DEVMETHOD(gpio_pin_getname, rt305x_gpio_pin_getname),
590 DEVMETHOD(gpio_pin_getflags, rt305x_gpio_pin_getflags),
591 DEVMETHOD(gpio_pin_getcaps, rt305x_gpio_pin_getcaps),
592 DEVMETHOD(gpio_pin_setflags, rt305x_gpio_pin_setflags),
593 DEVMETHOD(gpio_pin_get, rt305x_gpio_pin_get),
594 DEVMETHOD(gpio_pin_set, rt305x_gpio_pin_set),
595 DEVMETHOD(gpio_pin_toggle, rt305x_gpio_pin_toggle),
599 static driver_t rt305x_gpio_driver = {
602 sizeof(struct rt305x_gpio_softc),
604 static devclass_t rt305x_gpio_devclass;
606 DRIVER_MODULE(rt305x_gpio, obio, rt305x_gpio_driver,
607 rt305x_gpio_devclass, 0, 0);