2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
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
30 * Vybrid Family General-Purpose Input/Output (GPIO)
31 * Chapter 7, Vybrid Reference Manual, Rev. 5, 07/2013
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/malloc.h>
44 #include <sys/timeet.h>
45 #include <sys/timetc.h>
46 #include <sys/watchdog.h>
47 #include <sys/mutex.h>
50 #include <dev/gpio/gpiobusvar.h>
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <machine/bus.h>
56 #include <machine/cpu.h>
57 #include <machine/intr.h>
61 #include <arm/freescale/vybrid/vf_common.h>
62 #include <arm/freescale/vybrid/vf_port.h>
64 #define GPIO_PDOR(n) (0x00 + 0x40 * (n >> 5))
65 #define GPIO_PSOR(n) (0x04 + 0x40 * (n >> 5))
66 #define GPIO_PCOR(n) (0x08 + 0x40 * (n >> 5))
67 #define GPIO_PTOR(n) (0x0C + 0x40 * (n >> 5))
68 #define GPIO_PDIR(n) (0x10 + 0x40 * (n >> 5))
70 #define GPIO_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
71 #define GPIO_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
73 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
78 static device_t vf_gpio_get_bus(device_t);
79 static int vf_gpio_pin_max(device_t, int *);
80 static int vf_gpio_pin_getcaps(device_t, uint32_t, uint32_t *);
81 static int vf_gpio_pin_getname(device_t, uint32_t, char *);
82 static int vf_gpio_pin_getflags(device_t, uint32_t, uint32_t *);
83 static int vf_gpio_pin_setflags(device_t, uint32_t, uint32_t);
84 static int vf_gpio_pin_set(device_t, uint32_t, unsigned int);
85 static int vf_gpio_pin_get(device_t, uint32_t, unsigned int *);
86 static int vf_gpio_pin_toggle(device_t, uint32_t pin);
88 struct vf_gpio_softc {
89 struct resource *res[1];
91 bus_space_handle_t bsh;
96 struct gpio_pin gpio_pins[NGPIO];
99 struct vf_gpio_softc *gpio_sc;
101 static struct resource_spec vf_gpio_spec[] = {
102 { SYS_RES_MEMORY, 0, RF_ACTIVE },
107 vf_gpio_probe(device_t dev)
110 if (!ofw_bus_status_okay(dev))
113 if (!ofw_bus_is_compatible(dev, "fsl,mvf600-gpio"))
116 device_set_desc(dev, "Vybrid Family GPIO Unit");
117 return (BUS_PROBE_DEFAULT);
121 vf_gpio_attach(device_t dev)
123 struct vf_gpio_softc *sc;
126 sc = device_get_softc(dev);
127 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
129 if (bus_alloc_resources(dev, vf_gpio_spec, sc->res)) {
130 device_printf(dev, "could not allocate resources\n");
131 mtx_destroy(&sc->sc_mtx);
135 /* Memory interface */
136 sc->bst = rman_get_bustag(sc->res[0]);
137 sc->bsh = rman_get_bushandle(sc->res[0]);
141 sc->gpio_npins = NGPIO;
143 for (i = 0; i < sc->gpio_npins; i++) {
144 sc->gpio_pins[i].gp_pin = i;
145 sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
146 sc->gpio_pins[i].gp_flags =
147 (READ4(sc, GPIO_PDOR(i)) & (1 << (i % 32))) ?
148 GPIO_PIN_OUTPUT: GPIO_PIN_INPUT;
149 snprintf(sc->gpio_pins[i].gp_name, GPIOMAXNAME,
150 "vf_gpio%d.%d", device_get_unit(dev), i);
153 sc->sc_busdev = gpiobus_attach_bus(dev);
154 if (sc->sc_busdev == NULL) {
155 bus_release_resources(dev, vf_gpio_spec, sc->res);
156 mtx_destroy(&sc->sc_mtx);
164 vf_gpio_get_bus(device_t dev)
166 struct vf_gpio_softc *sc;
168 sc = device_get_softc(dev);
170 return (sc->sc_busdev);
174 vf_gpio_pin_max(device_t dev, int *maxpin)
182 vf_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
184 struct vf_gpio_softc *sc;
187 sc = device_get_softc(dev);
188 for (i = 0; i < sc->gpio_npins; i++) {
189 if (sc->gpio_pins[i].gp_pin == pin)
193 if (i >= sc->gpio_npins)
197 memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
204 vf_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
206 struct vf_gpio_softc *sc;
209 sc = device_get_softc(dev);
210 for (i = 0; i < sc->gpio_npins; i++) {
211 if (sc->gpio_pins[i].gp_pin == pin)
215 if (i >= sc->gpio_npins)
219 *caps = sc->gpio_pins[i].gp_caps;
226 vf_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
228 struct vf_gpio_softc *sc;
231 sc = device_get_softc(dev);
232 for (i = 0; i < sc->gpio_npins; i++) {
233 if (sc->gpio_pins[i].gp_pin == pin)
237 if (i >= sc->gpio_npins)
241 *flags = sc->gpio_pins[i].gp_flags;
248 vf_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
250 struct vf_gpio_softc *sc;
253 sc = device_get_softc(dev);
254 for (i = 0; i < sc->gpio_npins; i++) {
255 if (sc->gpio_pins[i].gp_pin == pin)
259 if (i >= sc->gpio_npins)
263 *val = (READ4(sc, GPIO_PDIR(i)) & (1 << (i % 32))) ? 1 : 0;
270 vf_gpio_pin_toggle(device_t dev, uint32_t pin)
272 struct vf_gpio_softc *sc;
275 sc = device_get_softc(dev);
276 for (i = 0; i < sc->gpio_npins; i++) {
277 if (sc->gpio_pins[i].gp_pin == pin)
281 if (i >= sc->gpio_npins)
285 WRITE4(sc, GPIO_PTOR(i), (1 << (i % 32)));
293 vf_gpio_pin_configure(struct vf_gpio_softc *sc, struct gpio_pin *pin,
300 * Manage input/output
302 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
303 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
304 if (flags & GPIO_PIN_OUTPUT) {
305 pin->gp_flags |= GPIO_PIN_OUTPUT;
308 pin->gp_flags |= GPIO_PIN_INPUT;
309 WRITE4(sc, GPIO_PCOR(pin->gp_pin),
310 (1 << (pin->gp_pin % 32)));
319 vf_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
321 struct vf_gpio_softc *sc;
324 sc = device_get_softc(dev);
325 for (i = 0; i < sc->gpio_npins; i++) {
326 if (sc->gpio_pins[i].gp_pin == pin)
330 if (i >= sc->gpio_npins)
333 vf_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
339 vf_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
341 struct vf_gpio_softc *sc;
344 sc = device_get_softc(dev);
345 for (i = 0; i < sc->gpio_npins; i++) {
346 if (sc->gpio_pins[i].gp_pin == pin)
350 if (i >= sc->gpio_npins)
355 WRITE4(sc, GPIO_PSOR(i), (1 << (i % 32)));
357 WRITE4(sc, GPIO_PCOR(i), (1 << (i % 32)));
363 static device_method_t vf_gpio_methods[] = {
364 DEVMETHOD(device_probe, vf_gpio_probe),
365 DEVMETHOD(device_attach, vf_gpio_attach),
368 DEVMETHOD(gpio_get_bus, vf_gpio_get_bus),
369 DEVMETHOD(gpio_pin_max, vf_gpio_pin_max),
370 DEVMETHOD(gpio_pin_getname, vf_gpio_pin_getname),
371 DEVMETHOD(gpio_pin_getcaps, vf_gpio_pin_getcaps),
372 DEVMETHOD(gpio_pin_getflags, vf_gpio_pin_getflags),
373 DEVMETHOD(gpio_pin_get, vf_gpio_pin_get),
374 DEVMETHOD(gpio_pin_toggle, vf_gpio_pin_toggle),
375 DEVMETHOD(gpio_pin_setflags, vf_gpio_pin_setflags),
376 DEVMETHOD(gpio_pin_set, vf_gpio_pin_set),
380 static driver_t vf_gpio_driver = {
383 sizeof(struct vf_gpio_softc),
386 static devclass_t vf_gpio_devclass;
388 DRIVER_MODULE(vf_gpio, simplebus, vf_gpio_driver, vf_gpio_devclass, 0, 0);