Home | History | Annotate | Line # | Download | only in ic
pl061.c revision 1.1.2.3
      1  1.1.2.3  pgoyette /* $NetBSD: pl061.c,v 1.1.2.3 2018/11/26 01:52:31 pgoyette Exp $ */
      2  1.1.2.2  pgoyette 
      3  1.1.2.2  pgoyette /*
      4  1.1.2.2  pgoyette  * Copyright (c) 2018 Jonathan A. Kollasch
      5  1.1.2.2  pgoyette  * All rights reserved.
      6  1.1.2.2  pgoyette  *
      7  1.1.2.2  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.1.2.2  pgoyette  * modification, are permitted provided that the following conditions
      9  1.1.2.2  pgoyette  * are met:
     10  1.1.2.2  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.1.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.1.2.2  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.2.2  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.2.2  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.1.2.2  pgoyette  *
     16  1.1.2.2  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  1.1.2.2  pgoyette  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1.2.2  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1.2.2  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     20  1.1.2.2  pgoyette  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  1.1.2.2  pgoyette  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  1.1.2.2  pgoyette  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23  1.1.2.2  pgoyette  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  1.1.2.2  pgoyette  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  1.1.2.2  pgoyette  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26  1.1.2.2  pgoyette  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1.2.2  pgoyette  */
     28  1.1.2.2  pgoyette 
     29  1.1.2.2  pgoyette #include <sys/cdefs.h>
     30  1.1.2.3  pgoyette __KERNEL_RCSID(0, "$NetBSD: pl061.c,v 1.1.2.3 2018/11/26 01:52:31 pgoyette Exp $");
     31  1.1.2.2  pgoyette 
     32  1.1.2.2  pgoyette #include <sys/param.h>
     33  1.1.2.2  pgoyette #include <sys/bus.h>
     34  1.1.2.2  pgoyette #include <sys/device.h>
     35  1.1.2.2  pgoyette #include <sys/intr.h>
     36  1.1.2.2  pgoyette #include <sys/systm.h>
     37  1.1.2.2  pgoyette #include <sys/kernel.h>
     38  1.1.2.2  pgoyette #include <sys/kmem.h>
     39  1.1.2.2  pgoyette #include <sys/gpio.h>
     40  1.1.2.2  pgoyette 
     41  1.1.2.2  pgoyette #include <dev/gpio/gpiovar.h>
     42  1.1.2.2  pgoyette #include "gpio.h"
     43  1.1.2.2  pgoyette 
     44  1.1.2.2  pgoyette #include <dev/ic/pl061reg.h>
     45  1.1.2.2  pgoyette #include <dev/ic/pl061var.h>
     46  1.1.2.2  pgoyette 
     47  1.1.2.2  pgoyette void
     48  1.1.2.2  pgoyette plgpio_attach(struct plgpio_softc *sc)
     49  1.1.2.2  pgoyette {
     50  1.1.2.2  pgoyette 	struct gpiobus_attach_args gba;
     51  1.1.2.2  pgoyette 	u_int pin;
     52  1.1.2.2  pgoyette 
     53  1.1.2.2  pgoyette 	sc->sc_gc.gp_cookie = sc;
     54  1.1.2.2  pgoyette 	sc->sc_gc.gp_pin_read = plgpio_pin_read;
     55  1.1.2.2  pgoyette 	sc->sc_gc.gp_pin_write = plgpio_pin_write;
     56  1.1.2.2  pgoyette 	sc->sc_gc.gp_pin_ctl = plgpio_pin_ctl;
     57  1.1.2.2  pgoyette 
     58  1.1.2.3  pgoyette 	const uint32_t cnf = PLGPIO_READ(sc, PL061_GPIOAFSEL_REG) |
     59  1.1.2.3  pgoyette 	    sc->sc_reserved_mask;
     60  1.1.2.2  pgoyette 
     61  1.1.2.2  pgoyette 	for (pin = 0; pin < 8; pin++) {
     62  1.1.2.2  pgoyette 		sc->sc_pins[pin].pin_num = pin;
     63  1.1.2.2  pgoyette 		/* skip pins in hardware control mode */
     64  1.1.2.2  pgoyette 		if ((cnf & __BIT(pin)) != 0)
     65  1.1.2.2  pgoyette 			continue;
     66  1.1.2.2  pgoyette 		sc->sc_pins[pin].pin_caps =
     67  1.1.2.2  pgoyette 		    GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
     68  1.1.2.2  pgoyette 		    GPIO_PIN_TRISTATE;
     69  1.1.2.2  pgoyette 		sc->sc_pins[pin].pin_state =
     70  1.1.2.2  pgoyette 		    plgpio_pin_read(sc, pin);
     71  1.1.2.2  pgoyette 	}
     72  1.1.2.2  pgoyette 
     73  1.1.2.2  pgoyette 	memset(&gba, 0, sizeof(gba));
     74  1.1.2.2  pgoyette 	gba.gba_gc = &sc->sc_gc;
     75  1.1.2.2  pgoyette 	gba.gba_pins = sc->sc_pins;
     76  1.1.2.2  pgoyette 	gba.gba_npins = 8;
     77  1.1.2.2  pgoyette 
     78  1.1.2.2  pgoyette #if NGPIO > 0
     79  1.1.2.2  pgoyette 	(void)config_found_ia(sc->sc_dev, "gpiobus", &gba,
     80  1.1.2.2  pgoyette 	    gpiobus_print);
     81  1.1.2.2  pgoyette #endif
     82  1.1.2.2  pgoyette }
     83  1.1.2.2  pgoyette 
     84  1.1.2.2  pgoyette int
     85  1.1.2.2  pgoyette plgpio_pin_read(void *priv, int pin)
     86  1.1.2.2  pgoyette {
     87  1.1.2.2  pgoyette 	struct plgpio_softc * const sc = priv;
     88  1.1.2.2  pgoyette 
     89  1.1.2.2  pgoyette 	const uint32_t v = PLGPIO_READ(sc, PL061_GPIODATA_REG(1<<pin));
     90  1.1.2.2  pgoyette 
     91  1.1.2.2  pgoyette 	return (v >> pin) & 1;
     92  1.1.2.2  pgoyette }
     93  1.1.2.2  pgoyette 
     94  1.1.2.2  pgoyette void
     95  1.1.2.2  pgoyette plgpio_pin_write(void *priv, int pin, int val)
     96  1.1.2.2  pgoyette {
     97  1.1.2.2  pgoyette 	struct plgpio_softc * const sc = priv;
     98  1.1.2.2  pgoyette 
     99  1.1.2.2  pgoyette 	PLGPIO_WRITE(sc, PL061_GPIODATA_REG(1 << pin), val << pin);
    100  1.1.2.2  pgoyette }
    101  1.1.2.2  pgoyette 
    102  1.1.2.2  pgoyette void
    103  1.1.2.2  pgoyette plgpio_pin_ctl(void *priv, int pin, int flags)
    104  1.1.2.2  pgoyette {
    105  1.1.2.2  pgoyette 	struct plgpio_softc * const sc = priv;
    106  1.1.2.2  pgoyette 	uint32_t v;
    107  1.1.2.2  pgoyette 
    108  1.1.2.2  pgoyette 	if (flags & GPIO_PIN_INPUT) {
    109  1.1.2.2  pgoyette 		v = PLGPIO_READ(sc, PL061_GPIODIR_REG);
    110  1.1.2.2  pgoyette 		v &= ~(1 << pin);
    111  1.1.2.2  pgoyette 		PLGPIO_WRITE(sc, PL061_GPIODIR_REG, v);
    112  1.1.2.2  pgoyette 	} else if (flags & GPIO_PIN_OUTPUT) {
    113  1.1.2.2  pgoyette 		v = PLGPIO_READ(sc, PL061_GPIODIR_REG);
    114  1.1.2.2  pgoyette 		v |= (1 << pin);
    115  1.1.2.2  pgoyette 		PLGPIO_WRITE(sc, PL061_GPIODIR_REG, v);
    116  1.1.2.2  pgoyette 	}
    117  1.1.2.2  pgoyette }
    118