Home | History | Annotate | Line # | Download | only in sunxi
sunxi_gpio.c revision 1.8
      1  1.8  jmcneill /* $NetBSD: sunxi_gpio.c,v 1.8 2017/07/08 11:12:24 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include "opt_soc.h"
     30  1.1  jmcneill 
     31  1.1  jmcneill #include <sys/cdefs.h>
     32  1.8  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_gpio.c,v 1.8 2017/07/08 11:12:24 jmcneill Exp $");
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <sys/param.h>
     35  1.1  jmcneill #include <sys/bus.h>
     36  1.1  jmcneill #include <sys/device.h>
     37  1.1  jmcneill #include <sys/intr.h>
     38  1.1  jmcneill #include <sys/systm.h>
     39  1.1  jmcneill #include <sys/mutex.h>
     40  1.1  jmcneill #include <sys/kmem.h>
     41  1.1  jmcneill #include <sys/gpio.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     44  1.5  jmcneill #include <dev/gpio/gpiovar.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <arm/sunxi/sunxi_gpio.h>
     47  1.1  jmcneill 
     48  1.4  jmcneill #define	SUNXI_GPIO_PORT(port)		(0x24 * (port))
     49  1.4  jmcneill #define SUNXI_GPIO_CFG(port, pin)	(SUNXI_GPIO_PORT(port) + 0x00 + (0x4 * ((pin) / 8)))
     50  1.1  jmcneill #define  SUNXI_GPIO_CFG_PINMASK(pin)	(0x7 << (((pin) % 8) * 4))
     51  1.1  jmcneill #define	SUNXI_GPIO_DATA(port)		(SUNXI_GPIO_PORT(port) + 0x10)
     52  1.1  jmcneill #define	SUNXI_GPIO_DRV(port, pin)	(SUNXI_GPIO_PORT(port) + 0x14 + (0x4 * ((pin) / 16)))
     53  1.4  jmcneill #define  SUNXI_GPIO_DRV_PINMASK(pin)	(0x3 << (((pin) % 16) * 2))
     54  1.1  jmcneill #define	SUNXI_GPIO_PULL(port, pin)	(SUNXI_GPIO_PORT(port) + 0x1c + (0x4 * ((pin) / 16)))
     55  1.2  jmcneill #define	 SUNXI_GPIO_PULL_DISABLE	0
     56  1.2  jmcneill #define	 SUNXI_GPIO_PULL_UP		1
     57  1.2  jmcneill #define	 SUNXI_GPIO_PULL_DOWN		2
     58  1.4  jmcneill #define  SUNXI_GPIO_PULL_PINMASK(pin)	(0x3 << (((pin) % 16) * 2))
     59  1.1  jmcneill 
     60  1.1  jmcneill static const struct of_compat_data compat_data[] = {
     61  1.1  jmcneill #ifdef SOC_SUN6I_A31
     62  1.1  jmcneill 	{ "allwinner,sun6i-a31-pinctrl",	(uintptr_t)&sun6i_a31_padconf },
     63  1.2  jmcneill 	{ "allwinner,sun6i-a31-r-pinctrl",	(uintptr_t)&sun6i_a31_r_padconf },
     64  1.1  jmcneill #endif
     65  1.6  jmcneill #ifdef SOC_SUN8I_A83T
     66  1.6  jmcneill 	{ "allwinner,sun8i-a83t-pinctrl",	(uintptr_t)&sun8i_a83t_padconf },
     67  1.6  jmcneill 	{ "allwinner,sun8i-a83t-r-pinctrl",	(uintptr_t)&sun8i_a83t_r_padconf },
     68  1.6  jmcneill #endif
     69  1.1  jmcneill #ifdef SOC_SUN8I_H3
     70  1.1  jmcneill 	{ "allwinner,sun8i-h3-pinctrl",		(uintptr_t)&sun8i_h3_padconf },
     71  1.3  jmcneill 	{ "allwinner,sun8i-h3-r-pinctrl",	(uintptr_t)&sun8i_h3_r_padconf },
     72  1.1  jmcneill #endif
     73  1.1  jmcneill 	{ NULL }
     74  1.1  jmcneill };
     75  1.1  jmcneill 
     76  1.1  jmcneill struct sunxi_gpio_softc {
     77  1.1  jmcneill 	device_t sc_dev;
     78  1.1  jmcneill 	bus_space_tag_t sc_bst;
     79  1.1  jmcneill 	bus_space_handle_t sc_bsh;
     80  1.1  jmcneill 	const struct sunxi_gpio_padconf *sc_padconf;
     81  1.5  jmcneill 	kmutex_t sc_lock;
     82  1.5  jmcneill 
     83  1.5  jmcneill 	struct gpio_chipset_tag sc_gp;
     84  1.5  jmcneill 	gpio_pin_t *sc_pins;
     85  1.5  jmcneill 	device_t sc_gpiodev;
     86  1.1  jmcneill };
     87  1.1  jmcneill 
     88  1.1  jmcneill struct sunxi_gpio_pin {
     89  1.1  jmcneill 	struct sunxi_gpio_softc *pin_sc;
     90  1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def;
     91  1.1  jmcneill 	int pin_flags;
     92  1.1  jmcneill 	bool pin_actlo;
     93  1.1  jmcneill };
     94  1.1  jmcneill 
     95  1.1  jmcneill #define GPIO_READ(sc, reg) 		\
     96  1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     97  1.1  jmcneill #define GPIO_WRITE(sc, reg, val) 	\
     98  1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     99  1.1  jmcneill 
    100  1.1  jmcneill static int	sunxi_gpio_match(device_t, cfdata_t, void *);
    101  1.1  jmcneill static void	sunxi_gpio_attach(device_t, device_t, void *);
    102  1.1  jmcneill 
    103  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_gpio, sizeof(struct sunxi_gpio_softc),
    104  1.1  jmcneill 	sunxi_gpio_match, sunxi_gpio_attach, NULL, NULL);
    105  1.1  jmcneill 
    106  1.1  jmcneill static const struct sunxi_gpio_pins *
    107  1.1  jmcneill sunxi_gpio_lookup(struct sunxi_gpio_softc *sc, uint8_t port, uint8_t pin)
    108  1.1  jmcneill {
    109  1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    110  1.1  jmcneill 	u_int n;
    111  1.1  jmcneill 
    112  1.1  jmcneill 	for (n = 0; n < sc->sc_padconf->npins; n++) {
    113  1.1  jmcneill 		pin_def = &sc->sc_padconf->pins[n];
    114  1.1  jmcneill 		if (pin_def->port == port && pin_def->pin == pin)
    115  1.1  jmcneill 			return pin_def;
    116  1.1  jmcneill 	}
    117  1.1  jmcneill 
    118  1.1  jmcneill 	return NULL;
    119  1.1  jmcneill }
    120  1.1  jmcneill 
    121  1.2  jmcneill static const struct sunxi_gpio_pins *
    122  1.2  jmcneill sunxi_gpio_lookup_byname(struct sunxi_gpio_softc *sc, const char *name)
    123  1.2  jmcneill {
    124  1.2  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    125  1.2  jmcneill 	u_int n;
    126  1.2  jmcneill 
    127  1.2  jmcneill 	for (n = 0; n < sc->sc_padconf->npins; n++) {
    128  1.2  jmcneill 		pin_def = &sc->sc_padconf->pins[n];
    129  1.2  jmcneill 		if (strcmp(pin_def->name, name) == 0)
    130  1.2  jmcneill 			return pin_def;
    131  1.2  jmcneill 	}
    132  1.2  jmcneill 
    133  1.2  jmcneill 	return NULL;
    134  1.2  jmcneill }
    135  1.2  jmcneill 
    136  1.1  jmcneill static int
    137  1.1  jmcneill sunxi_gpio_setfunc(struct sunxi_gpio_softc *sc,
    138  1.1  jmcneill     const struct sunxi_gpio_pins *pin_def, const char *func)
    139  1.1  jmcneill {
    140  1.1  jmcneill 	uint32_t cfg;
    141  1.1  jmcneill 	u_int n;
    142  1.1  jmcneill 
    143  1.5  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    144  1.5  jmcneill 
    145  1.1  jmcneill 	const bus_size_t cfg_reg = SUNXI_GPIO_CFG(pin_def->port, pin_def->pin);
    146  1.1  jmcneill 	const uint32_t cfg_mask = SUNXI_GPIO_CFG_PINMASK(pin_def->pin);
    147  1.1  jmcneill 
    148  1.1  jmcneill 	for (n = 0; n < SUNXI_GPIO_MAXFUNC; n++) {
    149  1.1  jmcneill 		if (pin_def->functions[n] == NULL)
    150  1.1  jmcneill 			continue;
    151  1.1  jmcneill 		if (strcmp(pin_def->functions[n], func) == 0) {
    152  1.1  jmcneill 			cfg = GPIO_READ(sc, cfg_reg);
    153  1.1  jmcneill 			cfg &= ~cfg_mask;
    154  1.1  jmcneill 			cfg |= __SHIFTIN(n, cfg_mask);
    155  1.4  jmcneill #ifdef SUNXI_GPIO_DEBUG
    156  1.4  jmcneill 			device_printf(sc->sc_dev, "P%c%02d cfg %08x -> %08x\n",
    157  1.4  jmcneill 			    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, cfg_reg), cfg);
    158  1.4  jmcneill #endif
    159  1.1  jmcneill 			GPIO_WRITE(sc, cfg_reg, cfg);
    160  1.1  jmcneill 			return 0;
    161  1.1  jmcneill 		}
    162  1.1  jmcneill 	}
    163  1.1  jmcneill 
    164  1.1  jmcneill 	/* Function not found */
    165  1.1  jmcneill 	device_printf(sc->sc_dev, "function '%s' not supported on P%c%02d\n",
    166  1.1  jmcneill 	    func, pin_def->port + 'A', pin_def->pin);
    167  1.1  jmcneill 
    168  1.1  jmcneill 	return ENXIO;
    169  1.1  jmcneill }
    170  1.1  jmcneill 
    171  1.1  jmcneill static int
    172  1.2  jmcneill sunxi_gpio_setpull(struct sunxi_gpio_softc *sc,
    173  1.2  jmcneill     const struct sunxi_gpio_pins *pin_def, int flags)
    174  1.2  jmcneill {
    175  1.2  jmcneill 	uint32_t pull;
    176  1.2  jmcneill 
    177  1.5  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    178  1.5  jmcneill 
    179  1.2  jmcneill 	const bus_size_t pull_reg = SUNXI_GPIO_PULL(pin_def->port, pin_def->pin);
    180  1.2  jmcneill 	const uint32_t pull_mask = SUNXI_GPIO_PULL_PINMASK(pin_def->pin);
    181  1.2  jmcneill 
    182  1.2  jmcneill 	pull = GPIO_READ(sc, pull_reg);
    183  1.2  jmcneill 	pull &= ~pull_mask;
    184  1.2  jmcneill 	if (flags & GPIO_PIN_PULLUP)
    185  1.2  jmcneill 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_UP, pull_mask);
    186  1.2  jmcneill 	else if (flags & GPIO_PIN_PULLDOWN)
    187  1.2  jmcneill 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_DOWN, pull_mask);
    188  1.2  jmcneill 	else
    189  1.2  jmcneill 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_DISABLE, pull_mask);
    190  1.4  jmcneill #ifdef SUNXI_GPIO_DEBUG
    191  1.4  jmcneill 	device_printf(sc->sc_dev, "P%c%02d pull %08x -> %08x\n",
    192  1.4  jmcneill 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, pull_reg), pull);
    193  1.4  jmcneill #endif
    194  1.2  jmcneill 	GPIO_WRITE(sc, pull_reg, pull);
    195  1.2  jmcneill 
    196  1.2  jmcneill 	return 0;
    197  1.2  jmcneill }
    198  1.2  jmcneill 
    199  1.2  jmcneill static int
    200  1.2  jmcneill sunxi_gpio_setdrv(struct sunxi_gpio_softc *sc,
    201  1.2  jmcneill     const struct sunxi_gpio_pins *pin_def, int drive_strength)
    202  1.2  jmcneill {
    203  1.2  jmcneill 	uint32_t drv;
    204  1.2  jmcneill 
    205  1.5  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    206  1.5  jmcneill 
    207  1.2  jmcneill 	if (drive_strength < 10 || drive_strength > 40)
    208  1.2  jmcneill 		return EINVAL;
    209  1.2  jmcneill 
    210  1.2  jmcneill 	const bus_size_t drv_reg = SUNXI_GPIO_DRV(pin_def->port, pin_def->pin);
    211  1.2  jmcneill 	const uint32_t drv_mask = SUNXI_GPIO_DRV_PINMASK(pin_def->pin);
    212  1.2  jmcneill 
    213  1.2  jmcneill 	drv = GPIO_READ(sc, drv_reg);
    214  1.2  jmcneill 	drv &= ~drv_mask;
    215  1.2  jmcneill 	drv |= __SHIFTIN((drive_strength / 10) - 1, drv_mask);
    216  1.4  jmcneill #ifdef SUNXI_GPIO_DEBUG
    217  1.4  jmcneill 	device_printf(sc->sc_dev, "P%c%02d drv %08x -> %08x\n",
    218  1.4  jmcneill 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, drv_reg), drv);
    219  1.4  jmcneill #endif
    220  1.2  jmcneill 	GPIO_WRITE(sc, drv_reg, drv);
    221  1.2  jmcneill 
    222  1.2  jmcneill 	return 0;
    223  1.2  jmcneill }
    224  1.2  jmcneill 
    225  1.2  jmcneill static int
    226  1.1  jmcneill sunxi_gpio_ctl(struct sunxi_gpio_softc *sc, const struct sunxi_gpio_pins *pin_def,
    227  1.1  jmcneill     int flags)
    228  1.1  jmcneill {
    229  1.5  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    230  1.5  jmcneill 
    231  1.1  jmcneill 	if (flags & GPIO_PIN_INPUT)
    232  1.1  jmcneill 		return sunxi_gpio_setfunc(sc, pin_def, "gpio_in");
    233  1.1  jmcneill 	if (flags & GPIO_PIN_OUTPUT)
    234  1.1  jmcneill 		return sunxi_gpio_setfunc(sc, pin_def, "gpio_out");
    235  1.1  jmcneill 
    236  1.1  jmcneill 	return EINVAL;
    237  1.1  jmcneill }
    238  1.1  jmcneill 
    239  1.1  jmcneill static void *
    240  1.1  jmcneill sunxi_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    241  1.1  jmcneill {
    242  1.1  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    243  1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    244  1.1  jmcneill 	struct sunxi_gpio_pin *gpin;
    245  1.1  jmcneill 	const u_int *gpio = data;
    246  1.1  jmcneill 	int error;
    247  1.1  jmcneill 
    248  1.1  jmcneill 	if (len != 16)
    249  1.1  jmcneill 		return NULL;
    250  1.1  jmcneill 
    251  1.1  jmcneill 	const uint8_t port = be32toh(gpio[1]) & 0xff;
    252  1.1  jmcneill 	const uint8_t pin = be32toh(gpio[2]) & 0xff;
    253  1.1  jmcneill 	const bool actlo = be32toh(gpio[3]) & 1;
    254  1.1  jmcneill 
    255  1.1  jmcneill 	pin_def = sunxi_gpio_lookup(sc, port, pin);
    256  1.1  jmcneill 	if (pin_def == NULL)
    257  1.1  jmcneill 		return NULL;
    258  1.1  jmcneill 
    259  1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    260  1.1  jmcneill 	error = sunxi_gpio_ctl(sc, pin_def, flags);
    261  1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    262  1.5  jmcneill 
    263  1.1  jmcneill 	if (error != 0)
    264  1.1  jmcneill 		return NULL;
    265  1.1  jmcneill 
    266  1.1  jmcneill 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    267  1.1  jmcneill 	gpin->pin_sc = sc;
    268  1.1  jmcneill 	gpin->pin_def = pin_def;
    269  1.1  jmcneill 	gpin->pin_flags = flags;
    270  1.1  jmcneill 	gpin->pin_actlo = actlo;
    271  1.1  jmcneill 
    272  1.1  jmcneill 	return gpin;
    273  1.1  jmcneill }
    274  1.1  jmcneill 
    275  1.1  jmcneill static void
    276  1.1  jmcneill sunxi_gpio_release(device_t dev, void *priv)
    277  1.1  jmcneill {
    278  1.1  jmcneill 	struct sunxi_gpio_pin *pin = priv;
    279  1.1  jmcneill 
    280  1.1  jmcneill 	sunxi_gpio_ctl(pin->pin_sc, pin->pin_def, GPIO_PIN_INPUT);
    281  1.1  jmcneill 
    282  1.1  jmcneill 	kmem_free(pin, sizeof(*pin));
    283  1.1  jmcneill }
    284  1.1  jmcneill 
    285  1.1  jmcneill static int
    286  1.1  jmcneill sunxi_gpio_read(device_t dev, void *priv, bool raw)
    287  1.1  jmcneill {
    288  1.1  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    289  1.1  jmcneill 	struct sunxi_gpio_pin *pin = priv;
    290  1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def = pin->pin_def;
    291  1.1  jmcneill 	uint32_t data;
    292  1.1  jmcneill 	int val;
    293  1.1  jmcneill 
    294  1.1  jmcneill 	KASSERT(sc == pin->pin_sc);
    295  1.1  jmcneill 
    296  1.1  jmcneill 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    297  1.1  jmcneill 	const uint32_t data_mask = __BIT(pin_def->pin);
    298  1.1  jmcneill 
    299  1.5  jmcneill 	/* No lock required for reads */
    300  1.1  jmcneill 	data = GPIO_READ(sc, data_reg);
    301  1.1  jmcneill 	val = __SHIFTOUT(data, data_mask);
    302  1.1  jmcneill 	if (!raw && pin->pin_actlo)
    303  1.1  jmcneill 		val = !val;
    304  1.1  jmcneill 
    305  1.1  jmcneill #ifdef SUNXI_GPIO_DEBUG
    306  1.1  jmcneill 	device_printf(dev, "P%c%02d rd %08x (%d %d)\n",
    307  1.1  jmcneill 	    pin_def->port + 'A', pin_def->pin, data,
    308  1.1  jmcneill 	    __SHIFTOUT(val, data_mask), val);
    309  1.1  jmcneill #endif
    310  1.1  jmcneill 
    311  1.1  jmcneill 	return val;
    312  1.1  jmcneill }
    313  1.1  jmcneill 
    314  1.1  jmcneill static void
    315  1.1  jmcneill sunxi_gpio_write(device_t dev, void *priv, int val, bool raw)
    316  1.1  jmcneill {
    317  1.1  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    318  1.1  jmcneill 	struct sunxi_gpio_pin *pin = priv;
    319  1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def = pin->pin_def;
    320  1.1  jmcneill 	uint32_t data;
    321  1.1  jmcneill 
    322  1.1  jmcneill 	KASSERT(sc == pin->pin_sc);
    323  1.1  jmcneill 
    324  1.1  jmcneill 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    325  1.1  jmcneill 	const uint32_t data_mask = __BIT(pin_def->pin);
    326  1.1  jmcneill 
    327  1.1  jmcneill 	if (!raw && pin->pin_actlo)
    328  1.1  jmcneill 		val = !val;
    329  1.1  jmcneill 
    330  1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    331  1.1  jmcneill 	data = GPIO_READ(sc, data_reg);
    332  1.1  jmcneill 	data &= ~data_mask;
    333  1.1  jmcneill 	data |= __SHIFTIN(val, data_mask);
    334  1.1  jmcneill #ifdef SUNXI_GPIO_DEBUG
    335  1.1  jmcneill 	device_printf(dev, "P%c%02d wr %08x -> %08x\n",
    336  1.4  jmcneill 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, data_reg), data);
    337  1.1  jmcneill #endif
    338  1.7  jmcneill 	GPIO_WRITE(sc, data_reg, data);
    339  1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    340  1.1  jmcneill }
    341  1.1  jmcneill 
    342  1.1  jmcneill static struct fdtbus_gpio_controller_func sunxi_gpio_funcs = {
    343  1.1  jmcneill 	.acquire = sunxi_gpio_acquire,
    344  1.1  jmcneill 	.release = sunxi_gpio_release,
    345  1.1  jmcneill 	.read = sunxi_gpio_read,
    346  1.1  jmcneill 	.write = sunxi_gpio_write,
    347  1.1  jmcneill };
    348  1.1  jmcneill 
    349  1.1  jmcneill static int
    350  1.2  jmcneill sunxi_pinctrl_set_config(device_t dev, const void *data, size_t len)
    351  1.2  jmcneill {
    352  1.2  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    353  1.2  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    354  1.2  jmcneill 	u_int drive_strength;
    355  1.2  jmcneill 
    356  1.2  jmcneill 	if (len != 4)
    357  1.2  jmcneill 		return -1;
    358  1.2  jmcneill 
    359  1.2  jmcneill 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
    360  1.2  jmcneill 
    361  1.2  jmcneill 	/*
    362  1.2  jmcneill 	 * Required: pins, function
    363  1.2  jmcneill 	 * Optional: bias-disable, bias-pull-up, bias-pull-down, drive-strength
    364  1.2  jmcneill 	 */
    365  1.2  jmcneill 
    366  1.2  jmcneill 	const char *function = fdtbus_get_string(phandle, "function");
    367  1.2  jmcneill 	if (function == NULL)
    368  1.2  jmcneill 		return -1;
    369  1.2  jmcneill 	int pins_len = OF_getproplen(phandle, "pins");
    370  1.2  jmcneill 	if (pins_len <= 0)
    371  1.2  jmcneill 		return -1;
    372  1.2  jmcneill 	const char *pins = fdtbus_get_string(phandle, "pins");
    373  1.2  jmcneill 
    374  1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    375  1.5  jmcneill 
    376  1.2  jmcneill 	for (pins = fdtbus_get_string(phandle, "pins");
    377  1.2  jmcneill 	     pins_len > 0;
    378  1.2  jmcneill 	     pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
    379  1.2  jmcneill 		pin_def = sunxi_gpio_lookup_byname(sc, pins);
    380  1.2  jmcneill 		if (pin_def == NULL) {
    381  1.2  jmcneill 			aprint_error_dev(dev, "unknown pin name '%s'\n", pins);
    382  1.2  jmcneill 			continue;
    383  1.2  jmcneill 		}
    384  1.2  jmcneill 		if (sunxi_gpio_setfunc(sc, pin_def, function) != 0)
    385  1.2  jmcneill 			continue;
    386  1.2  jmcneill 
    387  1.2  jmcneill 		if (of_hasprop(phandle, "bias-disable"))
    388  1.2  jmcneill 			sunxi_gpio_setpull(sc, pin_def, 0);
    389  1.2  jmcneill 		else if (of_hasprop(phandle, "bias-pull-up"))
    390  1.2  jmcneill 			sunxi_gpio_setpull(sc, pin_def, GPIO_PIN_PULLUP);
    391  1.2  jmcneill 		else if (of_hasprop(phandle, "bias-pull-down"))
    392  1.2  jmcneill 			sunxi_gpio_setpull(sc, pin_def, GPIO_PIN_PULLDOWN);
    393  1.2  jmcneill 
    394  1.2  jmcneill 		if (of_getprop_uint32(phandle, "drive-strength", &drive_strength) == 0)
    395  1.2  jmcneill 			sunxi_gpio_setdrv(sc, pin_def, drive_strength);
    396  1.2  jmcneill 	}
    397  1.2  jmcneill 
    398  1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    399  1.5  jmcneill 
    400  1.2  jmcneill 	return 0;
    401  1.2  jmcneill }
    402  1.2  jmcneill 
    403  1.2  jmcneill static struct fdtbus_pinctrl_controller_func sunxi_pinctrl_funcs = {
    404  1.2  jmcneill 	.set_config = sunxi_pinctrl_set_config,
    405  1.2  jmcneill };
    406  1.2  jmcneill 
    407  1.2  jmcneill static int
    408  1.5  jmcneill sunxi_gpio_pin_read(void *priv, int pin)
    409  1.5  jmcneill {
    410  1.5  jmcneill 	struct sunxi_gpio_softc * const sc = priv;
    411  1.5  jmcneill 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    412  1.5  jmcneill 	uint32_t data;
    413  1.5  jmcneill 	int val;
    414  1.5  jmcneill 
    415  1.5  jmcneill 	KASSERT(pin < sc->sc_padconf->npins);
    416  1.5  jmcneill 
    417  1.5  jmcneill 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    418  1.5  jmcneill 	const uint32_t data_mask = __BIT(pin_def->pin);
    419  1.5  jmcneill 
    420  1.5  jmcneill 	/* No lock required for reads */
    421  1.5  jmcneill 	data = GPIO_READ(sc, data_reg);
    422  1.5  jmcneill 	val = __SHIFTOUT(data, data_mask);
    423  1.5  jmcneill 
    424  1.5  jmcneill 	return val;
    425  1.5  jmcneill }
    426  1.5  jmcneill 
    427  1.5  jmcneill static void
    428  1.5  jmcneill sunxi_gpio_pin_write(void *priv, int pin, int val)
    429  1.5  jmcneill {
    430  1.5  jmcneill 	struct sunxi_gpio_softc * const sc = priv;
    431  1.5  jmcneill 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    432  1.5  jmcneill 	uint32_t data;
    433  1.5  jmcneill 
    434  1.5  jmcneill 	KASSERT(pin < sc->sc_padconf->npins);
    435  1.5  jmcneill 
    436  1.5  jmcneill 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    437  1.5  jmcneill 	const uint32_t data_mask = __BIT(pin_def->pin);
    438  1.5  jmcneill 
    439  1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    440  1.5  jmcneill 	data = GPIO_READ(sc, data_reg);
    441  1.5  jmcneill 	if (val)
    442  1.5  jmcneill 		data |= data_mask;
    443  1.5  jmcneill 	else
    444  1.5  jmcneill 		data &= ~data_mask;
    445  1.5  jmcneill 	GPIO_WRITE(sc, data_reg, data);
    446  1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    447  1.5  jmcneill }
    448  1.5  jmcneill 
    449  1.5  jmcneill static void
    450  1.5  jmcneill sunxi_gpio_pin_ctl(void *priv, int pin, int flags)
    451  1.5  jmcneill {
    452  1.5  jmcneill 	struct sunxi_gpio_softc * const sc = priv;
    453  1.5  jmcneill 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    454  1.5  jmcneill 
    455  1.5  jmcneill 	KASSERT(pin < sc->sc_padconf->npins);
    456  1.5  jmcneill 
    457  1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    458  1.5  jmcneill 	sunxi_gpio_ctl(sc, pin_def, flags);
    459  1.5  jmcneill 	sunxi_gpio_setpull(sc, pin_def, flags);
    460  1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    461  1.5  jmcneill }
    462  1.5  jmcneill 
    463  1.5  jmcneill static void
    464  1.5  jmcneill sunxi_gpio_attach_ports(struct sunxi_gpio_softc *sc)
    465  1.5  jmcneill {
    466  1.5  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    467  1.5  jmcneill 	struct gpio_chipset_tag *gp = &sc->sc_gp;
    468  1.5  jmcneill 	struct gpiobus_attach_args gba;
    469  1.5  jmcneill 	u_int pin;
    470  1.5  jmcneill 
    471  1.5  jmcneill 	gp->gp_cookie = sc;
    472  1.5  jmcneill 	gp->gp_pin_read = sunxi_gpio_pin_read;
    473  1.5  jmcneill 	gp->gp_pin_write = sunxi_gpio_pin_write;
    474  1.5  jmcneill 	gp->gp_pin_ctl = sunxi_gpio_pin_ctl;
    475  1.5  jmcneill 
    476  1.5  jmcneill 	const u_int npins = sc->sc_padconf->npins;
    477  1.5  jmcneill 	sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) * npins, KM_SLEEP);
    478  1.5  jmcneill 
    479  1.5  jmcneill 	for (pin = 0; pin < sc->sc_padconf->npins; pin++) {
    480  1.5  jmcneill 		pin_def = &sc->sc_padconf->pins[pin];
    481  1.5  jmcneill 		sc->sc_pins[pin].pin_num = pin;
    482  1.5  jmcneill 		sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
    483  1.5  jmcneill 		    GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    484  1.5  jmcneill 		sc->sc_pins[pin].pin_state = sunxi_gpio_pin_read(sc, pin);
    485  1.5  jmcneill 		strlcpy(sc->sc_pins[pin].pin_defname, pin_def->name,
    486  1.5  jmcneill 		    sizeof(sc->sc_pins[pin].pin_defname));
    487  1.5  jmcneill 	}
    488  1.5  jmcneill 
    489  1.5  jmcneill 	memset(&gba, 0, sizeof(gba));
    490  1.5  jmcneill 	gba.gba_gc = gp;
    491  1.5  jmcneill 	gba.gba_pins = sc->sc_pins;
    492  1.5  jmcneill 	gba.gba_npins = npins;
    493  1.5  jmcneill 	sc->sc_gpiodev = config_found_ia(sc->sc_dev, "gpiobus", &gba, NULL);
    494  1.5  jmcneill }
    495  1.5  jmcneill 
    496  1.5  jmcneill static int
    497  1.1  jmcneill sunxi_gpio_match(device_t parent, cfdata_t cf, void *aux)
    498  1.1  jmcneill {
    499  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    500  1.1  jmcneill 
    501  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    502  1.1  jmcneill }
    503  1.1  jmcneill 
    504  1.1  jmcneill static void
    505  1.1  jmcneill sunxi_gpio_attach(device_t parent, device_t self, void *aux)
    506  1.1  jmcneill {
    507  1.1  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(self);
    508  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    509  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    510  1.8  jmcneill 	struct fdtbus_reset *rst;
    511  1.8  jmcneill 	struct clk *clk;
    512  1.1  jmcneill 	bus_addr_t addr;
    513  1.1  jmcneill 	bus_size_t size;
    514  1.2  jmcneill 	int child;
    515  1.1  jmcneill 
    516  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    517  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    518  1.1  jmcneill 		return;
    519  1.1  jmcneill 	}
    520  1.1  jmcneill 
    521  1.8  jmcneill 	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
    522  1.8  jmcneill 		if (clk_enable(clk) != 0) {
    523  1.8  jmcneill 			aprint_error(": couldn't enable clock\n");
    524  1.8  jmcneill 			return;
    525  1.8  jmcneill 		}
    526  1.8  jmcneill 
    527  1.8  jmcneill 	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
    528  1.8  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    529  1.8  jmcneill 			aprint_error(": couldn't de-assert reset\n");
    530  1.8  jmcneill 			return;
    531  1.8  jmcneill 		}
    532  1.8  jmcneill 
    533  1.1  jmcneill 	sc->sc_dev = self;
    534  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    535  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    536  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    537  1.1  jmcneill 		return;
    538  1.1  jmcneill 	}
    539  1.5  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    540  1.1  jmcneill 	sc->sc_padconf = (void *)of_search_compatible(phandle, compat_data)->data;
    541  1.1  jmcneill 
    542  1.1  jmcneill 	aprint_naive("\n");
    543  1.1  jmcneill 	aprint_normal(": PIO\n");
    544  1.1  jmcneill 
    545  1.1  jmcneill 	fdtbus_register_gpio_controller(self, phandle, &sunxi_gpio_funcs);
    546  1.2  jmcneill 
    547  1.2  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    548  1.2  jmcneill 		if (!of_hasprop(child, "function") || !of_hasprop(child, "pins"))
    549  1.2  jmcneill 			continue;
    550  1.2  jmcneill 		fdtbus_register_pinctrl_config(self, child, &sunxi_pinctrl_funcs);
    551  1.2  jmcneill 	}
    552  1.2  jmcneill 
    553  1.2  jmcneill 	fdtbus_pinctrl_configure();
    554  1.5  jmcneill 
    555  1.5  jmcneill 	sunxi_gpio_attach_ports(sc);
    556  1.1  jmcneill }
    557