Home | History | Annotate | Line # | Download | only in fdt
pl061gpio_fdt.c revision 1.1.4.1
      1  1.1.4.1  christos /* $NetBSD: pl061gpio_fdt.c,v 1.1.4.1 2019/06/10 22:07:08 christos Exp $ */
      2      1.1  jakllsch 
      3      1.1  jakllsch /*
      4      1.1  jakllsch  * Copyright (c) 2018 Jonathan A. Kollasch
      5      1.1  jakllsch  * All rights reserved.
      6      1.1  jakllsch  *
      7      1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
      8      1.1  jakllsch  * modification, are permitted provided that the following conditions
      9      1.1  jakllsch  * are met:
     10      1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     15      1.1  jakllsch  *
     16      1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17      1.1  jakllsch  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  jakllsch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  jakllsch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     20      1.1  jakllsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21      1.1  jakllsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22      1.1  jakllsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23      1.1  jakllsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24      1.1  jakllsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25      1.1  jakllsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26      1.1  jakllsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27      1.1  jakllsch  */
     28      1.1  jakllsch 
     29      1.1  jakllsch #include <sys/cdefs.h>
     30  1.1.4.1  christos __KERNEL_RCSID(0, "$NetBSD: pl061gpio_fdt.c,v 1.1.4.1 2019/06/10 22:07:08 christos Exp $");
     31      1.1  jakllsch 
     32      1.1  jakllsch #include <sys/param.h>
     33      1.1  jakllsch #include <sys/bus.h>
     34      1.1  jakllsch #include <sys/device.h>
     35      1.1  jakllsch #include <sys/intr.h>
     36      1.1  jakllsch #include <sys/systm.h>
     37      1.1  jakllsch #include <sys/kernel.h>
     38      1.1  jakllsch #include <sys/kmem.h>
     39      1.1  jakllsch #include <sys/gpio.h>
     40      1.1  jakllsch 
     41      1.1  jakllsch #include <dev/gpio/gpiovar.h>
     42      1.1  jakllsch 
     43      1.1  jakllsch #include <dev/ic/pl061reg.h>
     44  1.1.4.1  christos #include <dev/ic/pl061var.h>
     45      1.1  jakllsch 
     46      1.1  jakllsch #include <dev/fdt/fdtvar.h>
     47      1.1  jakllsch 
     48  1.1.4.1  christos static int	plgpio_fdt_match(device_t, cfdata_t, void *);
     49  1.1.4.1  christos static void	plgpio_fdt_attach(device_t, device_t, void *);
     50      1.1  jakllsch 
     51  1.1.4.1  christos static void *	plgpio_fdt_acquire(device_t, const void *,
     52      1.1  jakllsch 		    size_t, int);
     53  1.1.4.1  christos static void	plgpio_fdt_release(device_t, void *);
     54  1.1.4.1  christos static int	plgpio_fdt_read(device_t, void *, bool);
     55  1.1.4.1  christos static void	plgpio_fdt_write(device_t, void *, int, bool);
     56  1.1.4.1  christos 
     57  1.1.4.1  christos struct fdtbus_gpio_controller_func plgpio_fdt_funcs = {
     58  1.1.4.1  christos 	.acquire = plgpio_fdt_acquire,
     59  1.1.4.1  christos 	.release = plgpio_fdt_release,
     60  1.1.4.1  christos 	.read = plgpio_fdt_read,
     61  1.1.4.1  christos 	.write = plgpio_fdt_write
     62      1.1  jakllsch };
     63      1.1  jakllsch 
     64  1.1.4.1  christos struct plgpio_fdt_pin {
     65  1.1.4.1  christos 	struct plgpio_softc *	pin_sc;
     66      1.1  jakllsch 	int			pin_no;
     67      1.1  jakllsch 	u_int			pin_flags;
     68      1.1  jakllsch 	bool			pin_actlo;
     69      1.1  jakllsch };
     70      1.1  jakllsch 
     71  1.1.4.1  christos CFATTACH_DECL_NEW(plgpio_fdt, sizeof(struct plgpio_softc),
     72  1.1.4.1  christos 	plgpio_fdt_match, plgpio_fdt_attach, NULL, NULL);
     73      1.1  jakllsch 
     74      1.1  jakllsch static int
     75  1.1.4.1  christos plgpio_fdt_match(device_t parent, cfdata_t cf, void *aux)
     76      1.1  jakllsch {
     77      1.1  jakllsch 	const char * const compatible[] = {
     78      1.1  jakllsch 		"arm,pl061",
     79      1.1  jakllsch 		NULL
     80      1.1  jakllsch 	};
     81      1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
     82      1.1  jakllsch 
     83      1.1  jakllsch 	return of_match_compatible(faa->faa_phandle, compatible);
     84      1.1  jakllsch }
     85      1.1  jakllsch 
     86      1.1  jakllsch static void
     87  1.1.4.1  christos plgpio_fdt_attach(device_t parent, device_t self, void *aux)
     88      1.1  jakllsch {
     89  1.1.4.1  christos 	struct plgpio_softc * const sc = device_private(self);
     90      1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
     91      1.1  jakllsch 	bus_addr_t addr;
     92      1.1  jakllsch 	bus_size_t size;
     93      1.1  jakllsch 	int error;
     94      1.1  jakllsch 
     95      1.1  jakllsch 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
     96      1.1  jakllsch 		aprint_error(": couldn't get registers\n");
     97      1.1  jakllsch 		return;
     98      1.1  jakllsch 	}
     99      1.1  jakllsch 
    100      1.1  jakllsch 	sc->sc_dev = self;
    101  1.1.4.1  christos 	sc->sc_bst = faa->faa_bst;
    102      1.1  jakllsch 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    103      1.1  jakllsch 	if (error) {
    104  1.1.4.1  christos 		aprint_error(": couldn't map %#"PRIx64": %d", (uint64_t)addr, error);
    105      1.1  jakllsch 		return;
    106      1.1  jakllsch 	}
    107      1.1  jakllsch 
    108      1.1  jakllsch 	aprint_naive("\n");
    109      1.1  jakllsch 	aprint_normal(": GPIO\n");
    110      1.1  jakllsch 
    111  1.1.4.1  christos 	plgpio_attach(sc);
    112      1.1  jakllsch 
    113      1.1  jakllsch 	fdtbus_register_gpio_controller(self, faa->faa_phandle,
    114  1.1.4.1  christos 	    &plgpio_fdt_funcs);
    115      1.1  jakllsch }
    116      1.1  jakllsch 
    117      1.1  jakllsch static void *
    118  1.1.4.1  christos plgpio_fdt_acquire(device_t dev, const void *data, size_t len, int flags)
    119      1.1  jakllsch {
    120  1.1.4.1  christos 	struct plgpio_softc * const sc = device_private(dev);
    121  1.1.4.1  christos 	struct plgpio_fdt_pin *gpin;
    122      1.1  jakllsch 	const u_int *gpio = data;
    123      1.1  jakllsch 
    124      1.1  jakllsch 	if (len != 12)
    125      1.1  jakllsch 		return NULL;
    126      1.1  jakllsch 
    127      1.1  jakllsch 	const u_int pin = be32toh(gpio[1]);
    128      1.1  jakllsch 	const bool actlo = be32toh(gpio[2]) & 1;
    129      1.1  jakllsch 
    130      1.1  jakllsch 	if (pin > 8)
    131      1.1  jakllsch 		return NULL;
    132      1.1  jakllsch 
    133  1.1.4.1  christos 	const uint32_t cnf = PLGPIO_READ(sc, PL061_GPIOAFSEL_REG);
    134      1.1  jakllsch 	if ((cnf & __BIT(pin)) != 0)
    135  1.1.4.1  christos 		PLGPIO_WRITE(sc, PL061_GPIOAFSEL_REG, cnf & ~__BIT(pin));
    136      1.1  jakllsch 
    137      1.1  jakllsch 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    138      1.1  jakllsch 	gpin->pin_sc = sc;
    139      1.1  jakllsch 	gpin->pin_no = pin;
    140      1.1  jakllsch 	gpin->pin_flags = flags;
    141      1.1  jakllsch 	gpin->pin_actlo = actlo;
    142      1.1  jakllsch 
    143  1.1.4.1  christos 	plgpio_pin_ctl(gpin->pin_sc, gpin->pin_no, gpin->pin_flags);
    144      1.1  jakllsch 
    145      1.1  jakllsch 	return gpin;
    146      1.1  jakllsch }
    147      1.1  jakllsch 
    148      1.1  jakllsch static void
    149  1.1.4.1  christos plgpio_fdt_release(device_t dev, void *priv)
    150      1.1  jakllsch {
    151  1.1.4.1  christos 	struct plgpio_fdt_pin * const gpin = priv;
    152      1.1  jakllsch 
    153  1.1.4.1  christos 	plgpio_pin_ctl(gpin->pin_sc, gpin->pin_no, GPIO_PIN_INPUT);
    154      1.1  jakllsch 	kmem_free(gpin, sizeof(*gpin));
    155      1.1  jakllsch }
    156      1.1  jakllsch 
    157      1.1  jakllsch static int
    158  1.1.4.1  christos plgpio_fdt_read(device_t dev, void *priv, bool raw)
    159      1.1  jakllsch {
    160  1.1.4.1  christos 	struct plgpio_fdt_pin * const gpin = priv;
    161      1.1  jakllsch 	int val;
    162      1.1  jakllsch 
    163  1.1.4.1  christos 	val = plgpio_pin_read(gpin->pin_sc, gpin->pin_no);
    164      1.1  jakllsch 
    165      1.1  jakllsch 	if (!raw && gpin->pin_actlo)
    166      1.1  jakllsch 		val = !val;
    167      1.1  jakllsch 
    168      1.1  jakllsch 	return val;
    169      1.1  jakllsch }
    170      1.1  jakllsch 
    171      1.1  jakllsch static void
    172  1.1.4.1  christos plgpio_fdt_write(device_t dev, void *priv, int val, bool raw)
    173      1.1  jakllsch {
    174  1.1.4.1  christos 	struct plgpio_fdt_pin * const gpin = priv;
    175      1.1  jakllsch 
    176      1.1  jakllsch 	if (!raw && gpin->pin_actlo)
    177      1.1  jakllsch 		val = !val;
    178      1.1  jakllsch 
    179  1.1.4.1  christos 	plgpio_pin_write(gpin->pin_sc, gpin->pin_no, val);
    180      1.1  jakllsch }
    181