2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
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 Cavium Octeon
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>
51 #include <contrib/octeon-sdk/cvmx.h>
52 #include <contrib/octeon-sdk/cvmx-gpio.h>
53 #include <mips/cavium/octeon_irq.h>
55 #include <mips/cavium/octeon_gpiovar.h>
56 #include <dev/gpio/gpiobusvar.h>
60 #define DEFAULT_CAPS (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)
62 struct octeon_gpio_pin {
69 * on CAP100 GPIO 7 is "Factory defaults" button
72 static struct octeon_gpio_pin octeon_gpio_pins[] = {
73 { "F/D", 7, GPIO_PIN_INPUT},
80 static void octeon_gpio_pin_configure(struct octeon_gpio_softc *sc,
81 struct gpio_pin *pin, uint32_t flags);
86 static void octeon_gpio_identify(driver_t *, device_t);
87 static int octeon_gpio_probe(device_t dev);
88 static int octeon_gpio_attach(device_t dev);
89 static int octeon_gpio_detach(device_t dev);
90 static int octeon_gpio_filter(void *arg);
91 static void octeon_gpio_intr(void *arg);
96 static device_t octeon_gpio_get_bus(device_t);
97 static int octeon_gpio_pin_max(device_t dev, int *maxpin);
98 static int octeon_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps);
99 static int octeon_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t
101 static int octeon_gpio_pin_getname(device_t dev, uint32_t pin, char *name);
102 static int octeon_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags);
103 static int octeon_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value);
104 static int octeon_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val);
105 static int octeon_gpio_pin_toggle(device_t dev, uint32_t pin);
108 octeon_gpio_pin_configure(struct octeon_gpio_softc *sc, struct gpio_pin *pin,
112 cvmx_gpio_bit_cfgx_t gpio_cfgx;
114 mask = 1 << pin->gp_pin;
118 * Manage input/output
120 if (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
121 gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(pin->gp_pin));
122 pin->gp_flags &= ~(GPIO_PIN_INPUT|GPIO_PIN_OUTPUT);
123 if (flags & GPIO_PIN_OUTPUT) {
124 pin->gp_flags |= GPIO_PIN_OUTPUT;
125 gpio_cfgx.s.tx_oe = 1;
128 pin->gp_flags |= GPIO_PIN_INPUT;
129 gpio_cfgx.s.tx_oe = 0;
131 if (flags & GPIO_PIN_INVIN)
132 gpio_cfgx.s.rx_xor = 1;
134 gpio_cfgx.s.rx_xor = 0;
135 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(pin->gp_pin), gpio_cfgx.u64);
142 octeon_gpio_get_bus(device_t dev)
144 struct octeon_gpio_softc *sc;
146 sc = device_get_softc(dev);
152 octeon_gpio_pin_max(device_t dev, int *maxpin)
155 *maxpin = OCTEON_GPIO_PINS - 1;
160 octeon_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
162 struct octeon_gpio_softc *sc = device_get_softc(dev);
165 for (i = 0; i < sc->gpio_npins; i++) {
166 if (sc->gpio_pins[i].gp_pin == pin)
170 if (i >= sc->gpio_npins)
174 *caps = sc->gpio_pins[i].gp_caps;
181 octeon_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
183 struct octeon_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 *flags = sc->gpio_pins[i].gp_flags;
202 octeon_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
204 struct octeon_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 memcpy(name, sc->gpio_pins[i].gp_name, GPIOMAXNAME);
223 octeon_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
226 struct octeon_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)
236 octeon_gpio_pin_configure(sc, &sc->gpio_pins[i], flags);
242 octeon_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
244 struct octeon_gpio_softc *sc = device_get_softc(dev);
247 for (i = 0; i < sc->gpio_npins; i++) {
248 if (sc->gpio_pins[i].gp_pin == pin)
252 if (i >= sc->gpio_npins)
257 cvmx_gpio_set(1 << pin);
259 cvmx_gpio_clear(1 << pin);
266 octeon_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
268 struct octeon_gpio_softc *sc = device_get_softc(dev);
272 for (i = 0; i < sc->gpio_npins; i++) {
273 if (sc->gpio_pins[i].gp_pin == pin)
277 if (i >= sc->gpio_npins)
281 state = cvmx_gpio_read();
282 *val = (state & (1 << pin)) ? 1 : 0;
289 octeon_gpio_pin_toggle(device_t dev, uint32_t pin)
293 struct octeon_gpio_softc *sc = device_get_softc(dev);
295 for (i = 0; i < sc->gpio_npins; i++) {
296 if (sc->gpio_pins[i].gp_pin == pin)
300 if (i >= sc->gpio_npins)
305 * XXX: Need to check if read returns actual state of output
306 * pins or we need to keep this information by ourself
308 state = cvmx_gpio_read();
309 if (state & (1 << pin))
310 cvmx_gpio_clear(1 << pin);
312 cvmx_gpio_set(1 << pin);
319 octeon_gpio_filter(void *arg)
321 cvmx_gpio_bit_cfgx_t gpio_cfgx;
323 struct octeon_gpio_softc *sc = *cookie;
324 long int irq = (cookie - sc->gpio_intr_cookies);
326 if ((irq < 0) || (irq >= OCTEON_GPIO_IRQS))
327 return (FILTER_STRAY);
329 gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(irq));
330 /* Clear rising edge detector */
331 if (gpio_cfgx.s.int_type == OCTEON_GPIO_IRQ_EDGE)
332 cvmx_gpio_interrupt_clear(1 << irq);
333 /* disable interrupt */
334 gpio_cfgx.s.int_en = 0;
335 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(irq), gpio_cfgx.u64);
337 return (FILTER_SCHEDULE_THREAD);
341 octeon_gpio_intr(void *arg)
343 cvmx_gpio_bit_cfgx_t gpio_cfgx;
345 struct octeon_gpio_softc *sc = *cookie;
346 long int irq = (cookie - sc->gpio_intr_cookies);
348 if ((irq < 0) || (irq >= OCTEON_GPIO_IRQS)) {
349 printf("%s: invalid GPIO IRQ: %ld\n",
355 gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(irq));
356 /* disable interrupt */
357 gpio_cfgx.s.int_en = 1;
358 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(irq), gpio_cfgx.u64);
360 /* TODO: notify bus here or something */
361 printf("GPIO IRQ for pin %ld\n", irq);
366 octeon_gpio_identify(driver_t *drv, device_t parent)
369 BUS_ADD_CHILD(parent, 0, "gpio", 0);
373 octeon_gpio_probe(device_t dev)
376 device_set_desc(dev, "Cavium Octeon GPIO driver");
381 octeon_gpio_attach(device_t dev)
383 struct octeon_gpio_softc *sc = device_get_softc(dev);
384 struct octeon_gpio_pin *pinp;
385 cvmx_gpio_bit_cfgx_t gpio_cfgx;
389 KASSERT((device_get_unit(dev) == 0),
390 ("octeon_gpio: Only one gpio module supported"));
392 mtx_init(&sc->gpio_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
394 for ( i = 0; i < OCTEON_GPIO_IRQS; i++) {
395 if ((sc->gpio_irq_res[i] = bus_alloc_resource(dev,
396 SYS_RES_IRQ, &sc->gpio_irq_rid[i],
397 OCTEON_IRQ_GPIO0 + i, OCTEON_IRQ_GPIO0 + i, 1,
398 RF_SHAREABLE | RF_ACTIVE)) == NULL) {
399 device_printf(dev, "unable to allocate IRQ resource\n");
400 octeon_gpio_detach(dev);
404 sc->gpio_intr_cookies[i] = sc;
405 if ((bus_setup_intr(dev, sc->gpio_irq_res[i], INTR_TYPE_MISC,
406 octeon_gpio_filter, octeon_gpio_intr,
407 &(sc->gpio_intr_cookies[i]), &sc->gpio_ih[i]))) {
409 "WARNING: unable to register interrupt handler\n");
410 octeon_gpio_detach(dev);
416 /* Configure all pins as input */
417 /* disable interrupts for all pins */
418 pinp = octeon_gpio_pins;
421 strncpy(sc->gpio_pins[i].gp_name, pinp->name, GPIOMAXNAME);
422 sc->gpio_pins[i].gp_pin = pinp->pin;
423 sc->gpio_pins[i].gp_caps = DEFAULT_CAPS;
424 sc->gpio_pins[i].gp_flags = 0;
425 octeon_gpio_pin_configure(sc, &sc->gpio_pins[i], pinp->flags);
434 * Sample: how to enable edge-triggered interrupt
437 gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(7));
438 gpio_cfgx.s.int_en = 1;
439 gpio_cfgx.s.int_type = OCTEON_GPIO_IRQ_EDGE;
440 cvmx_write_csr(CVMX_GPIO_BIT_CFGX(7), gpio_cfgx.u64);
444 for (i = 0; i < 16; i++) {
445 gpio_cfgx.u64 = cvmx_read_csr(CVMX_GPIO_BIT_CFGX(i));
446 device_printf(dev, "[pin%d] output=%d, invinput=%d, intr=%d, intr_type=%s\n",
447 i, gpio_cfgx.s.tx_oe, gpio_cfgx.s.rx_xor,
448 gpio_cfgx.s.int_en, gpio_cfgx.s.int_type ? "rising edge" : "level");
451 sc->busdev = gpiobus_attach_bus(dev);
452 if (sc->busdev == NULL) {
453 octeon_gpio_detach(dev);
461 octeon_gpio_detach(device_t dev)
463 struct octeon_gpio_softc *sc = device_get_softc(dev);
466 KASSERT(mtx_initialized(&sc->gpio_mtx), ("gpio mutex not initialized"));
468 for ( i = 0; i < OCTEON_GPIO_IRQS; i++) {
470 bus_teardown_intr(dev, sc->gpio_irq_res[i],
472 if (sc->gpio_irq_res[i])
473 bus_release_resource(dev, SYS_RES_IRQ,
474 sc->gpio_irq_rid[i], sc->gpio_irq_res[i]);
476 gpiobus_detach_bus(dev);
477 mtx_destroy(&sc->gpio_mtx);
482 static device_method_t octeon_gpio_methods[] = {
483 DEVMETHOD(device_identify, octeon_gpio_identify),
484 DEVMETHOD(device_probe, octeon_gpio_probe),
485 DEVMETHOD(device_attach, octeon_gpio_attach),
486 DEVMETHOD(device_detach, octeon_gpio_detach),
489 DEVMETHOD(gpio_get_bus, octeon_gpio_get_bus),
490 DEVMETHOD(gpio_pin_max, octeon_gpio_pin_max),
491 DEVMETHOD(gpio_pin_getname, octeon_gpio_pin_getname),
492 DEVMETHOD(gpio_pin_getflags, octeon_gpio_pin_getflags),
493 DEVMETHOD(gpio_pin_getcaps, octeon_gpio_pin_getcaps),
494 DEVMETHOD(gpio_pin_setflags, octeon_gpio_pin_setflags),
495 DEVMETHOD(gpio_pin_get, octeon_gpio_pin_get),
496 DEVMETHOD(gpio_pin_set, octeon_gpio_pin_set),
497 DEVMETHOD(gpio_pin_toggle, octeon_gpio_pin_toggle),
501 static driver_t octeon_gpio_driver = {
504 sizeof(struct octeon_gpio_softc),
506 static devclass_t octeon_gpio_devclass;
508 DRIVER_MODULE(octeon_gpio, ciu, octeon_gpio_driver, octeon_gpio_devclass, 0, 0);