Home | History | Annotate | Line # | Download | only in sunxi
sunxi_gpio.c revision 1.24
      1  1.24  jmcneill /* $NetBSD: sunxi_gpio.c,v 1.24 2019/05/27 23:26:20 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.24  jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_gpio.c,v 1.24 2019/05/27 23:26:20 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.12  jmcneill #include <sys/bitops.h>
     43  1.13  jmcneill #include <sys/lwp.h>
     44   1.1  jmcneill 
     45   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     46   1.5  jmcneill #include <dev/gpio/gpiovar.h>
     47   1.1  jmcneill 
     48   1.1  jmcneill #include <arm/sunxi/sunxi_gpio.h>
     49   1.1  jmcneill 
     50  1.15  jmcneill #define	SUNXI_GPIO_MAX_EINT_BANK	5
     51  1.12  jmcneill #define	SUNXI_GPIO_MAX_EINT		32
     52  1.12  jmcneill 
     53  1.24  jmcneill #define	SUNXI_GPIO_MAX_BANK		26
     54  1.24  jmcneill 
     55   1.4  jmcneill #define	SUNXI_GPIO_PORT(port)		(0x24 * (port))
     56   1.4  jmcneill #define SUNXI_GPIO_CFG(port, pin)	(SUNXI_GPIO_PORT(port) + 0x00 + (0x4 * ((pin) / 8)))
     57   1.1  jmcneill #define  SUNXI_GPIO_CFG_PINMASK(pin)	(0x7 << (((pin) % 8) * 4))
     58   1.1  jmcneill #define	SUNXI_GPIO_DATA(port)		(SUNXI_GPIO_PORT(port) + 0x10)
     59   1.1  jmcneill #define	SUNXI_GPIO_DRV(port, pin)	(SUNXI_GPIO_PORT(port) + 0x14 + (0x4 * ((pin) / 16)))
     60   1.4  jmcneill #define  SUNXI_GPIO_DRV_PINMASK(pin)	(0x3 << (((pin) % 16) * 2))
     61   1.1  jmcneill #define	SUNXI_GPIO_PULL(port, pin)	(SUNXI_GPIO_PORT(port) + 0x1c + (0x4 * ((pin) / 16)))
     62   1.2  jmcneill #define	 SUNXI_GPIO_PULL_DISABLE	0
     63   1.2  jmcneill #define	 SUNXI_GPIO_PULL_UP		1
     64   1.2  jmcneill #define	 SUNXI_GPIO_PULL_DOWN		2
     65   1.4  jmcneill #define  SUNXI_GPIO_PULL_PINMASK(pin)	(0x3 << (((pin) % 16) * 2))
     66  1.15  jmcneill #define	SUNXI_GPIO_INT_CFG(bank, eint)	(0x200 + (0x20 * (bank)) + (0x4 * ((eint) / 8)))
     67  1.12  jmcneill #define	 SUNXI_GPIO_INT_MODEMASK(eint)	(0xf << (((eint) % 8) * 4))
     68  1.12  jmcneill #define	  SUNXI_GPIO_INT_MODE_POS_EDGE		0x0
     69  1.12  jmcneill #define	  SUNXI_GPIO_INT_MODE_NEG_EDGE		0x1
     70  1.12  jmcneill #define	  SUNXI_GPIO_INT_MODE_HIGH_LEVEL	0x2
     71  1.12  jmcneill #define	  SUNXI_GPIO_INT_MODE_LOW_LEVEL		0x3
     72  1.12  jmcneill #define	  SUNXI_GPIO_INT_MODE_DOUBLE_EDGE	0x4
     73  1.15  jmcneill #define	SUNXI_GPIO_INT_CTL(bank)	(0x210 + 0x20 * (bank))
     74  1.15  jmcneill #define	SUNXI_GPIO_INT_STATUS(bank)	(0x214 + 0x20 * (bank))
     75  1.24  jmcneill #define	SUNXI_GPIO_GRP_CONFIG(bank)	(0x300 + 0x4 * (bank))
     76  1.24  jmcneill #define	 SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK	0xf
     77   1.1  jmcneill 
     78   1.1  jmcneill static const struct of_compat_data compat_data[] = {
     79  1.14  jmcneill #ifdef SOC_SUN4I_A10
     80  1.14  jmcneill 	{ "allwinner,sun4i-a10-pinctrl",	(uintptr_t)&sun4i_a10_padconf },
     81  1.14  jmcneill #endif
     82  1.11  jmcneill #ifdef SOC_SUN5I_A13
     83  1.11  jmcneill 	{ "allwinner,sun5i-a13-pinctrl",	(uintptr_t)&sun5i_a13_padconf },
     84  1.17  jmcneill 	{ "nextthing,gr8-pinctrl",		(uintptr_t)&sun5i_a13_padconf },
     85  1.11  jmcneill #endif
     86   1.1  jmcneill #ifdef SOC_SUN6I_A31
     87   1.1  jmcneill 	{ "allwinner,sun6i-a31-pinctrl",	(uintptr_t)&sun6i_a31_padconf },
     88   1.2  jmcneill 	{ "allwinner,sun6i-a31-r-pinctrl",	(uintptr_t)&sun6i_a31_r_padconf },
     89   1.1  jmcneill #endif
     90  1.14  jmcneill #ifdef SOC_SUN7I_A20
     91  1.14  jmcneill 	{ "allwinner,sun7i-a20-pinctrl",	(uintptr_t)&sun7i_a20_padconf },
     92  1.14  jmcneill #endif
     93   1.6  jmcneill #ifdef SOC_SUN8I_A83T
     94   1.6  jmcneill 	{ "allwinner,sun8i-a83t-pinctrl",	(uintptr_t)&sun8i_a83t_padconf },
     95   1.6  jmcneill 	{ "allwinner,sun8i-a83t-r-pinctrl",	(uintptr_t)&sun8i_a83t_r_padconf },
     96   1.6  jmcneill #endif
     97   1.1  jmcneill #ifdef SOC_SUN8I_H3
     98   1.1  jmcneill 	{ "allwinner,sun8i-h3-pinctrl",		(uintptr_t)&sun8i_h3_padconf },
     99   1.3  jmcneill 	{ "allwinner,sun8i-h3-r-pinctrl",	(uintptr_t)&sun8i_h3_r_padconf },
    100   1.1  jmcneill #endif
    101  1.15  jmcneill #ifdef SOC_SUN9I_A80
    102  1.15  jmcneill 	{ "allwinner,sun9i-a80-pinctrl",	(uintptr_t)&sun9i_a80_padconf },
    103  1.15  jmcneill 	{ "allwinner,sun9i-a80-r-pinctrl",	(uintptr_t)&sun9i_a80_r_padconf },
    104  1.15  jmcneill #endif
    105   1.9  jmcneill #ifdef SOC_SUN50I_A64
    106   1.9  jmcneill 	{ "allwinner,sun50i-a64-pinctrl",	(uintptr_t)&sun50i_a64_padconf },
    107   1.9  jmcneill 	{ "allwinner,sun50i-a64-r-pinctrl",	(uintptr_t)&sun50i_a64_r_padconf },
    108   1.9  jmcneill #endif
    109  1.16  jmcneill #ifdef SOC_SUN50I_H5
    110  1.16  jmcneill 	{ "allwinner,sun50i-h5-pinctrl",	(uintptr_t)&sun8i_h3_padconf },
    111  1.16  jmcneill 	{ "allwinner,sun50i-h5-r-pinctrl",	(uintptr_t)&sun8i_h3_r_padconf },
    112  1.16  jmcneill #endif
    113  1.19  jmcneill #ifdef SOC_SUN50I_H6
    114  1.19  jmcneill 	{ "allwinner,sun50i-h6-pinctrl",	(uintptr_t)&sun50i_h6_padconf },
    115  1.19  jmcneill 	{ "allwinner,sun50i-h6-r-pinctrl",	(uintptr_t)&sun50i_h6_r_padconf },
    116  1.19  jmcneill #endif
    117   1.1  jmcneill 	{ NULL }
    118   1.1  jmcneill };
    119   1.1  jmcneill 
    120  1.12  jmcneill struct sunxi_gpio_eint {
    121  1.12  jmcneill 	int (*eint_func)(void *);
    122  1.12  jmcneill 	void *eint_arg;
    123  1.12  jmcneill 	int eint_flags;
    124  1.15  jmcneill 	int eint_bank;
    125  1.12  jmcneill 	int eint_num;
    126  1.12  jmcneill };
    127  1.12  jmcneill 
    128   1.1  jmcneill struct sunxi_gpio_softc {
    129   1.1  jmcneill 	device_t sc_dev;
    130   1.1  jmcneill 	bus_space_tag_t sc_bst;
    131   1.1  jmcneill 	bus_space_handle_t sc_bsh;
    132  1.24  jmcneill 	int sc_phandle;
    133   1.1  jmcneill 	const struct sunxi_gpio_padconf *sc_padconf;
    134   1.5  jmcneill 	kmutex_t sc_lock;
    135   1.5  jmcneill 
    136   1.5  jmcneill 	struct gpio_chipset_tag sc_gp;
    137   1.5  jmcneill 	gpio_pin_t *sc_pins;
    138   1.5  jmcneill 	device_t sc_gpiodev;
    139  1.12  jmcneill 
    140  1.24  jmcneill 	struct fdtbus_regulator *sc_pin_supply[SUNXI_GPIO_MAX_BANK];
    141  1.24  jmcneill 
    142  1.15  jmcneill 	u_int sc_eint_bank_max;
    143  1.15  jmcneill 
    144  1.12  jmcneill 	void *sc_ih;
    145  1.15  jmcneill 	struct sunxi_gpio_eint sc_eint[SUNXI_GPIO_MAX_EINT_BANK][SUNXI_GPIO_MAX_EINT];
    146   1.1  jmcneill };
    147   1.1  jmcneill 
    148   1.1  jmcneill struct sunxi_gpio_pin {
    149   1.1  jmcneill 	struct sunxi_gpio_softc *pin_sc;
    150   1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    151   1.1  jmcneill 	int pin_flags;
    152   1.1  jmcneill 	bool pin_actlo;
    153   1.1  jmcneill };
    154   1.1  jmcneill 
    155   1.1  jmcneill #define GPIO_READ(sc, reg) 		\
    156   1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    157   1.1  jmcneill #define GPIO_WRITE(sc, reg, val) 	\
    158   1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    159   1.1  jmcneill 
    160   1.1  jmcneill static int	sunxi_gpio_match(device_t, cfdata_t, void *);
    161   1.1  jmcneill static void	sunxi_gpio_attach(device_t, device_t, void *);
    162   1.1  jmcneill 
    163   1.1  jmcneill CFATTACH_DECL_NEW(sunxi_gpio, sizeof(struct sunxi_gpio_softc),
    164   1.1  jmcneill 	sunxi_gpio_match, sunxi_gpio_attach, NULL, NULL);
    165   1.1  jmcneill 
    166   1.1  jmcneill static const struct sunxi_gpio_pins *
    167   1.1  jmcneill sunxi_gpio_lookup(struct sunxi_gpio_softc *sc, uint8_t port, uint8_t pin)
    168   1.1  jmcneill {
    169   1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    170   1.1  jmcneill 	u_int n;
    171   1.1  jmcneill 
    172   1.1  jmcneill 	for (n = 0; n < sc->sc_padconf->npins; n++) {
    173   1.1  jmcneill 		pin_def = &sc->sc_padconf->pins[n];
    174   1.1  jmcneill 		if (pin_def->port == port && pin_def->pin == pin)
    175   1.1  jmcneill 			return pin_def;
    176   1.1  jmcneill 	}
    177   1.1  jmcneill 
    178   1.1  jmcneill 	return NULL;
    179   1.1  jmcneill }
    180   1.1  jmcneill 
    181   1.2  jmcneill static const struct sunxi_gpio_pins *
    182   1.2  jmcneill sunxi_gpio_lookup_byname(struct sunxi_gpio_softc *sc, const char *name)
    183   1.2  jmcneill {
    184   1.2  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    185   1.2  jmcneill 	u_int n;
    186   1.2  jmcneill 
    187   1.2  jmcneill 	for (n = 0; n < sc->sc_padconf->npins; n++) {
    188   1.2  jmcneill 		pin_def = &sc->sc_padconf->pins[n];
    189   1.2  jmcneill 		if (strcmp(pin_def->name, name) == 0)
    190   1.2  jmcneill 			return pin_def;
    191   1.2  jmcneill 	}
    192   1.2  jmcneill 
    193   1.2  jmcneill 	return NULL;
    194   1.2  jmcneill }
    195   1.2  jmcneill 
    196   1.1  jmcneill static int
    197   1.1  jmcneill sunxi_gpio_setfunc(struct sunxi_gpio_softc *sc,
    198   1.1  jmcneill     const struct sunxi_gpio_pins *pin_def, const char *func)
    199   1.1  jmcneill {
    200   1.1  jmcneill 	uint32_t cfg;
    201   1.1  jmcneill 	u_int n;
    202   1.1  jmcneill 
    203   1.5  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    204   1.5  jmcneill 
    205   1.1  jmcneill 	const bus_size_t cfg_reg = SUNXI_GPIO_CFG(pin_def->port, pin_def->pin);
    206   1.1  jmcneill 	const uint32_t cfg_mask = SUNXI_GPIO_CFG_PINMASK(pin_def->pin);
    207   1.1  jmcneill 
    208   1.1  jmcneill 	for (n = 0; n < SUNXI_GPIO_MAXFUNC; n++) {
    209   1.1  jmcneill 		if (pin_def->functions[n] == NULL)
    210   1.1  jmcneill 			continue;
    211   1.1  jmcneill 		if (strcmp(pin_def->functions[n], func) == 0) {
    212   1.1  jmcneill 			cfg = GPIO_READ(sc, cfg_reg);
    213   1.1  jmcneill 			cfg &= ~cfg_mask;
    214   1.1  jmcneill 			cfg |= __SHIFTIN(n, cfg_mask);
    215   1.4  jmcneill #ifdef SUNXI_GPIO_DEBUG
    216   1.4  jmcneill 			device_printf(sc->sc_dev, "P%c%02d cfg %08x -> %08x\n",
    217   1.4  jmcneill 			    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, cfg_reg), cfg);
    218   1.4  jmcneill #endif
    219   1.1  jmcneill 			GPIO_WRITE(sc, cfg_reg, cfg);
    220   1.1  jmcneill 			return 0;
    221   1.1  jmcneill 		}
    222   1.1  jmcneill 	}
    223   1.1  jmcneill 
    224   1.1  jmcneill 	/* Function not found */
    225   1.1  jmcneill 	device_printf(sc->sc_dev, "function '%s' not supported on P%c%02d\n",
    226   1.1  jmcneill 	    func, pin_def->port + 'A', pin_def->pin);
    227   1.1  jmcneill 
    228   1.1  jmcneill 	return ENXIO;
    229   1.1  jmcneill }
    230   1.1  jmcneill 
    231   1.1  jmcneill static int
    232   1.2  jmcneill sunxi_gpio_setpull(struct sunxi_gpio_softc *sc,
    233   1.2  jmcneill     const struct sunxi_gpio_pins *pin_def, int flags)
    234   1.2  jmcneill {
    235   1.2  jmcneill 	uint32_t pull;
    236   1.2  jmcneill 
    237   1.5  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    238   1.5  jmcneill 
    239   1.2  jmcneill 	const bus_size_t pull_reg = SUNXI_GPIO_PULL(pin_def->port, pin_def->pin);
    240   1.2  jmcneill 	const uint32_t pull_mask = SUNXI_GPIO_PULL_PINMASK(pin_def->pin);
    241   1.2  jmcneill 
    242   1.2  jmcneill 	pull = GPIO_READ(sc, pull_reg);
    243   1.2  jmcneill 	pull &= ~pull_mask;
    244   1.2  jmcneill 	if (flags & GPIO_PIN_PULLUP)
    245   1.2  jmcneill 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_UP, pull_mask);
    246   1.2  jmcneill 	else if (flags & GPIO_PIN_PULLDOWN)
    247   1.2  jmcneill 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_DOWN, pull_mask);
    248   1.2  jmcneill 	else
    249   1.2  jmcneill 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_DISABLE, pull_mask);
    250   1.4  jmcneill #ifdef SUNXI_GPIO_DEBUG
    251   1.4  jmcneill 	device_printf(sc->sc_dev, "P%c%02d pull %08x -> %08x\n",
    252   1.4  jmcneill 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, pull_reg), pull);
    253   1.4  jmcneill #endif
    254   1.2  jmcneill 	GPIO_WRITE(sc, pull_reg, pull);
    255   1.2  jmcneill 
    256   1.2  jmcneill 	return 0;
    257   1.2  jmcneill }
    258   1.2  jmcneill 
    259   1.2  jmcneill static int
    260   1.2  jmcneill sunxi_gpio_setdrv(struct sunxi_gpio_softc *sc,
    261   1.2  jmcneill     const struct sunxi_gpio_pins *pin_def, int drive_strength)
    262   1.2  jmcneill {
    263   1.2  jmcneill 	uint32_t drv;
    264   1.2  jmcneill 
    265   1.5  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    266   1.5  jmcneill 
    267   1.2  jmcneill 	if (drive_strength < 10 || drive_strength > 40)
    268   1.2  jmcneill 		return EINVAL;
    269   1.2  jmcneill 
    270   1.2  jmcneill 	const bus_size_t drv_reg = SUNXI_GPIO_DRV(pin_def->port, pin_def->pin);
    271   1.2  jmcneill 	const uint32_t drv_mask = SUNXI_GPIO_DRV_PINMASK(pin_def->pin);
    272   1.2  jmcneill 
    273   1.2  jmcneill 	drv = GPIO_READ(sc, drv_reg);
    274   1.2  jmcneill 	drv &= ~drv_mask;
    275   1.2  jmcneill 	drv |= __SHIFTIN((drive_strength / 10) - 1, drv_mask);
    276   1.4  jmcneill #ifdef SUNXI_GPIO_DEBUG
    277   1.4  jmcneill 	device_printf(sc->sc_dev, "P%c%02d drv %08x -> %08x\n",
    278   1.4  jmcneill 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, drv_reg), drv);
    279   1.4  jmcneill #endif
    280   1.2  jmcneill 	GPIO_WRITE(sc, drv_reg, drv);
    281   1.2  jmcneill 
    282   1.2  jmcneill 	return 0;
    283   1.2  jmcneill }
    284   1.2  jmcneill 
    285   1.2  jmcneill static int
    286   1.1  jmcneill sunxi_gpio_ctl(struct sunxi_gpio_softc *sc, const struct sunxi_gpio_pins *pin_def,
    287   1.1  jmcneill     int flags)
    288   1.1  jmcneill {
    289   1.5  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    290   1.5  jmcneill 
    291   1.1  jmcneill 	if (flags & GPIO_PIN_INPUT)
    292   1.1  jmcneill 		return sunxi_gpio_setfunc(sc, pin_def, "gpio_in");
    293   1.1  jmcneill 	if (flags & GPIO_PIN_OUTPUT)
    294   1.1  jmcneill 		return sunxi_gpio_setfunc(sc, pin_def, "gpio_out");
    295   1.1  jmcneill 
    296   1.1  jmcneill 	return EINVAL;
    297   1.1  jmcneill }
    298   1.1  jmcneill 
    299   1.1  jmcneill static void *
    300   1.1  jmcneill sunxi_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    301   1.1  jmcneill {
    302   1.1  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    303   1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    304   1.1  jmcneill 	struct sunxi_gpio_pin *gpin;
    305   1.1  jmcneill 	const u_int *gpio = data;
    306   1.1  jmcneill 	int error;
    307   1.1  jmcneill 
    308   1.1  jmcneill 	if (len != 16)
    309   1.1  jmcneill 		return NULL;
    310   1.1  jmcneill 
    311   1.1  jmcneill 	const uint8_t port = be32toh(gpio[1]) & 0xff;
    312   1.1  jmcneill 	const uint8_t pin = be32toh(gpio[2]) & 0xff;
    313   1.1  jmcneill 	const bool actlo = be32toh(gpio[3]) & 1;
    314   1.1  jmcneill 
    315   1.1  jmcneill 	pin_def = sunxi_gpio_lookup(sc, port, pin);
    316   1.1  jmcneill 	if (pin_def == NULL)
    317   1.1  jmcneill 		return NULL;
    318   1.1  jmcneill 
    319   1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    320   1.1  jmcneill 	error = sunxi_gpio_ctl(sc, pin_def, flags);
    321   1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    322   1.5  jmcneill 
    323   1.1  jmcneill 	if (error != 0)
    324   1.1  jmcneill 		return NULL;
    325   1.1  jmcneill 
    326   1.1  jmcneill 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    327   1.1  jmcneill 	gpin->pin_sc = sc;
    328   1.1  jmcneill 	gpin->pin_def = pin_def;
    329   1.1  jmcneill 	gpin->pin_flags = flags;
    330   1.1  jmcneill 	gpin->pin_actlo = actlo;
    331   1.1  jmcneill 
    332   1.1  jmcneill 	return gpin;
    333   1.1  jmcneill }
    334   1.1  jmcneill 
    335   1.1  jmcneill static void
    336   1.1  jmcneill sunxi_gpio_release(device_t dev, void *priv)
    337   1.1  jmcneill {
    338  1.21  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    339   1.1  jmcneill 	struct sunxi_gpio_pin *pin = priv;
    340   1.1  jmcneill 
    341  1.21  jmcneill 	mutex_enter(&sc->sc_lock);
    342   1.1  jmcneill 	sunxi_gpio_ctl(pin->pin_sc, pin->pin_def, GPIO_PIN_INPUT);
    343  1.21  jmcneill 	mutex_exit(&sc->sc_lock);
    344   1.1  jmcneill 
    345   1.1  jmcneill 	kmem_free(pin, sizeof(*pin));
    346  1.18     skrll }
    347   1.1  jmcneill 
    348   1.1  jmcneill static int
    349   1.1  jmcneill sunxi_gpio_read(device_t dev, void *priv, bool raw)
    350   1.1  jmcneill {
    351   1.1  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    352   1.1  jmcneill 	struct sunxi_gpio_pin *pin = priv;
    353   1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def = pin->pin_def;
    354   1.1  jmcneill 	uint32_t data;
    355   1.1  jmcneill 	int val;
    356   1.1  jmcneill 
    357   1.1  jmcneill 	KASSERT(sc == pin->pin_sc);
    358   1.1  jmcneill 
    359   1.1  jmcneill 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    360   1.1  jmcneill 	const uint32_t data_mask = __BIT(pin_def->pin);
    361   1.1  jmcneill 
    362   1.5  jmcneill 	/* No lock required for reads */
    363   1.1  jmcneill 	data = GPIO_READ(sc, data_reg);
    364   1.1  jmcneill 	val = __SHIFTOUT(data, data_mask);
    365   1.1  jmcneill 	if (!raw && pin->pin_actlo)
    366   1.1  jmcneill 		val = !val;
    367   1.1  jmcneill 
    368   1.1  jmcneill #ifdef SUNXI_GPIO_DEBUG
    369   1.1  jmcneill 	device_printf(dev, "P%c%02d rd %08x (%d %d)\n",
    370   1.1  jmcneill 	    pin_def->port + 'A', pin_def->pin, data,
    371   1.1  jmcneill 	    __SHIFTOUT(val, data_mask), val);
    372   1.1  jmcneill #endif
    373   1.1  jmcneill 
    374   1.1  jmcneill 	return val;
    375   1.1  jmcneill }
    376   1.1  jmcneill 
    377   1.1  jmcneill static void
    378   1.1  jmcneill sunxi_gpio_write(device_t dev, void *priv, int val, bool raw)
    379   1.1  jmcneill {
    380   1.1  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    381   1.1  jmcneill 	struct sunxi_gpio_pin *pin = priv;
    382   1.1  jmcneill 	const struct sunxi_gpio_pins *pin_def = pin->pin_def;
    383   1.1  jmcneill 	uint32_t data;
    384   1.1  jmcneill 
    385   1.1  jmcneill 	KASSERT(sc == pin->pin_sc);
    386   1.1  jmcneill 
    387   1.1  jmcneill 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    388   1.1  jmcneill 	const uint32_t data_mask = __BIT(pin_def->pin);
    389   1.1  jmcneill 
    390   1.1  jmcneill 	if (!raw && pin->pin_actlo)
    391   1.1  jmcneill 		val = !val;
    392   1.1  jmcneill 
    393   1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    394   1.1  jmcneill 	data = GPIO_READ(sc, data_reg);
    395   1.1  jmcneill 	data &= ~data_mask;
    396   1.1  jmcneill 	data |= __SHIFTIN(val, data_mask);
    397   1.1  jmcneill #ifdef SUNXI_GPIO_DEBUG
    398   1.1  jmcneill 	device_printf(dev, "P%c%02d wr %08x -> %08x\n",
    399   1.4  jmcneill 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, data_reg), data);
    400   1.1  jmcneill #endif
    401   1.7  jmcneill 	GPIO_WRITE(sc, data_reg, data);
    402   1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    403   1.1  jmcneill }
    404   1.1  jmcneill 
    405   1.1  jmcneill static struct fdtbus_gpio_controller_func sunxi_gpio_funcs = {
    406   1.1  jmcneill 	.acquire = sunxi_gpio_acquire,
    407   1.1  jmcneill 	.release = sunxi_gpio_release,
    408   1.1  jmcneill 	.read = sunxi_gpio_read,
    409   1.1  jmcneill 	.write = sunxi_gpio_write,
    410   1.1  jmcneill };
    411   1.1  jmcneill 
    412  1.12  jmcneill static int
    413  1.12  jmcneill sunxi_gpio_intr(void *priv)
    414  1.12  jmcneill {
    415  1.12  jmcneill 	struct sunxi_gpio_softc * const sc = priv;
    416  1.12  jmcneill 	struct sunxi_gpio_eint *eint;
    417  1.12  jmcneill 	uint32_t status, bit;
    418  1.15  jmcneill 	u_int bank;
    419  1.12  jmcneill 	int ret = 0;
    420  1.12  jmcneill 
    421  1.15  jmcneill 	for (bank = 0; bank <= sc->sc_eint_bank_max; bank++) {
    422  1.15  jmcneill 		status = GPIO_READ(sc, SUNXI_GPIO_INT_STATUS(bank));
    423  1.15  jmcneill 		if (status == 0)
    424  1.15  jmcneill 			continue;
    425  1.15  jmcneill 		GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(bank), status);
    426  1.12  jmcneill 
    427  1.15  jmcneill 		while ((bit = ffs32(status)) != 0) {
    428  1.15  jmcneill 			status &= ~__BIT(bit - 1);
    429  1.15  jmcneill 			eint = &sc->sc_eint[bank][bit - 1];
    430  1.15  jmcneill 			if (eint->eint_func == NULL)
    431  1.15  jmcneill 				continue;
    432  1.15  jmcneill 			const bool mpsafe = (eint->eint_flags & FDT_INTR_MPSAFE) != 0;
    433  1.15  jmcneill 			if (!mpsafe)
    434  1.15  jmcneill 				KERNEL_LOCK(1, curlwp);
    435  1.15  jmcneill 			ret |= eint->eint_func(eint->eint_arg);
    436  1.15  jmcneill 			if (!mpsafe)
    437  1.15  jmcneill 				KERNEL_UNLOCK_ONE(curlwp);
    438  1.15  jmcneill 		}
    439  1.12  jmcneill 	}
    440  1.12  jmcneill 
    441  1.12  jmcneill 	return ret;
    442  1.12  jmcneill }
    443  1.12  jmcneill 
    444  1.12  jmcneill static void *
    445  1.12  jmcneill sunxi_gpio_establish(device_t dev, u_int *specifier, int ipl, int flags,
    446  1.12  jmcneill     int (*func)(void *), void *arg)
    447  1.12  jmcneill {
    448  1.12  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    449  1.12  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    450  1.12  jmcneill 	struct sunxi_gpio_eint *eint;
    451  1.12  jmcneill 	uint32_t val;
    452  1.12  jmcneill 	u_int mode;
    453  1.12  jmcneill 
    454  1.12  jmcneill 	if (ipl != IPL_VM) {
    455  1.12  jmcneill 		aprint_error_dev(dev, "%s: wrong IPL %d (expected %d)\n",
    456  1.12  jmcneill 		    __func__, ipl, IPL_VM);
    457  1.12  jmcneill 		return NULL;
    458  1.12  jmcneill 	}
    459  1.12  jmcneill 
    460  1.12  jmcneill 	/* 1st cell is the bank */
    461  1.12  jmcneill 	/* 2nd cell is the pin */
    462  1.12  jmcneill 	/* 3rd cell is flags */
    463  1.12  jmcneill 	const u_int port = be32toh(specifier[0]);
    464  1.12  jmcneill 	const u_int pin = be32toh(specifier[1]);
    465  1.12  jmcneill 	const u_int type = be32toh(specifier[2]) & 0xf;
    466  1.12  jmcneill 
    467  1.12  jmcneill 	switch (type) {
    468  1.23   thorpej 	case FDT_INTR_TYPE_POS_EDGE:
    469  1.12  jmcneill 		mode = SUNXI_GPIO_INT_MODE_POS_EDGE;
    470  1.12  jmcneill 		break;
    471  1.23   thorpej 	case FDT_INTR_TYPE_NEG_EDGE:
    472  1.12  jmcneill 		mode = SUNXI_GPIO_INT_MODE_NEG_EDGE;
    473  1.12  jmcneill 		break;
    474  1.23   thorpej 	case FDT_INTR_TYPE_DOUBLE_EDGE:
    475  1.12  jmcneill 		mode = SUNXI_GPIO_INT_MODE_DOUBLE_EDGE;
    476  1.12  jmcneill 		break;
    477  1.23   thorpej 	case FDT_INTR_TYPE_HIGH_LEVEL:
    478  1.12  jmcneill 		mode = SUNXI_GPIO_INT_MODE_HIGH_LEVEL;
    479  1.12  jmcneill 		break;
    480  1.23   thorpej 	case FDT_INTR_TYPE_LOW_LEVEL:
    481  1.12  jmcneill 		mode = SUNXI_GPIO_INT_MODE_LOW_LEVEL;
    482  1.12  jmcneill 		break;
    483  1.12  jmcneill 	default:
    484  1.12  jmcneill 		aprint_error_dev(dev, "%s: unsupported irq type 0x%x\n",
    485  1.12  jmcneill 		    __func__, type);
    486  1.12  jmcneill 		return NULL;
    487  1.12  jmcneill 	}
    488  1.12  jmcneill 
    489  1.12  jmcneill 	pin_def = sunxi_gpio_lookup(sc, port, pin);
    490  1.12  jmcneill 	if (pin_def == NULL)
    491  1.12  jmcneill 		return NULL;
    492  1.12  jmcneill 	if (pin_def->functions[pin_def->eint_func] == NULL ||
    493  1.20    bouyer 	    strcmp(pin_def->functions[pin_def->eint_func], "irq") != 0)
    494  1.12  jmcneill 		return NULL;
    495  1.12  jmcneill 
    496  1.12  jmcneill 	KASSERT(pin_def->eint_num < SUNXI_GPIO_MAX_EINT);
    497  1.12  jmcneill 
    498  1.12  jmcneill 	mutex_enter(&sc->sc_lock);
    499  1.12  jmcneill 
    500  1.15  jmcneill 	eint = &sc->sc_eint[pin_def->eint_bank][pin_def->eint_num];
    501  1.12  jmcneill 	if (eint->eint_func != NULL) {
    502  1.12  jmcneill 		mutex_exit(&sc->sc_lock);
    503  1.12  jmcneill 		return NULL;	/* in use */
    504  1.12  jmcneill 	}
    505  1.12  jmcneill 
    506  1.12  jmcneill 	/* Set function */
    507  1.20    bouyer 	if (sunxi_gpio_setfunc(sc, pin_def, "irq") != 0) {
    508  1.12  jmcneill 		mutex_exit(&sc->sc_lock);
    509  1.12  jmcneill 		return NULL;
    510  1.12  jmcneill 	}
    511  1.12  jmcneill 
    512  1.12  jmcneill 	eint->eint_func = func;
    513  1.12  jmcneill 	eint->eint_arg = arg;
    514  1.12  jmcneill 	eint->eint_flags = flags;
    515  1.15  jmcneill 	eint->eint_bank = pin_def->eint_bank;
    516  1.12  jmcneill 	eint->eint_num = pin_def->eint_num;
    517  1.12  jmcneill 
    518  1.12  jmcneill 	/* Configure eint mode */
    519  1.15  jmcneill 	val = GPIO_READ(sc, SUNXI_GPIO_INT_CFG(eint->eint_bank, eint->eint_num));
    520  1.12  jmcneill 	val &= ~SUNXI_GPIO_INT_MODEMASK(eint->eint_num);
    521  1.12  jmcneill 	val |= __SHIFTIN(mode, SUNXI_GPIO_INT_MODEMASK(eint->eint_num));
    522  1.15  jmcneill 	GPIO_WRITE(sc, SUNXI_GPIO_INT_CFG(eint->eint_bank, eint->eint_num), val);
    523  1.12  jmcneill 
    524  1.12  jmcneill 	/* Enable eint */
    525  1.15  jmcneill 	val = GPIO_READ(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank));
    526  1.12  jmcneill 	val |= __BIT(eint->eint_num);
    527  1.15  jmcneill 	GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank), val);
    528  1.12  jmcneill 
    529  1.12  jmcneill 	mutex_exit(&sc->sc_lock);
    530  1.12  jmcneill 
    531  1.12  jmcneill 	return eint;
    532  1.12  jmcneill }
    533  1.12  jmcneill 
    534  1.12  jmcneill static void
    535  1.12  jmcneill sunxi_gpio_disestablish(device_t dev, void *ih)
    536  1.12  jmcneill {
    537  1.12  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    538  1.12  jmcneill 	struct sunxi_gpio_eint * const eint = ih;
    539  1.12  jmcneill 	uint32_t val;
    540  1.12  jmcneill 
    541  1.12  jmcneill 	KASSERT(eint->eint_func != NULL);
    542  1.12  jmcneill 
    543  1.12  jmcneill 	mutex_enter(&sc->sc_lock);
    544  1.12  jmcneill 
    545  1.12  jmcneill 	/* Disable eint */
    546  1.15  jmcneill 	val = GPIO_READ(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank));
    547  1.12  jmcneill 	val &= ~__BIT(eint->eint_num);
    548  1.15  jmcneill 	GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank), val);
    549  1.15  jmcneill 	GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(eint->eint_bank), __BIT(eint->eint_num));
    550  1.12  jmcneill 
    551  1.12  jmcneill 	eint->eint_func = NULL;
    552  1.12  jmcneill 	eint->eint_arg = NULL;
    553  1.12  jmcneill 	eint->eint_flags = 0;
    554  1.12  jmcneill 
    555  1.12  jmcneill 	mutex_exit(&sc->sc_lock);
    556  1.12  jmcneill }
    557  1.12  jmcneill 
    558  1.12  jmcneill static bool
    559  1.12  jmcneill sunxi_gpio_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    560  1.12  jmcneill {
    561  1.12  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    562  1.12  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    563  1.12  jmcneill 
    564  1.12  jmcneill 	/* 1st cell is the bank */
    565  1.12  jmcneill 	/* 2nd cell is the pin */
    566  1.12  jmcneill 	/* 3rd cell is flags */
    567  1.12  jmcneill 	if (!specifier)
    568  1.12  jmcneill 		return false;
    569  1.12  jmcneill 	const u_int port = be32toh(specifier[0]);
    570  1.12  jmcneill 	const u_int pin = be32toh(specifier[1]);
    571  1.12  jmcneill 
    572  1.12  jmcneill 	pin_def = sunxi_gpio_lookup(sc, port, pin);
    573  1.12  jmcneill 	if (pin_def == NULL)
    574  1.12  jmcneill 		return false;
    575  1.12  jmcneill 
    576  1.12  jmcneill 	snprintf(buf, buflen, "GPIO %s", pin_def->name);
    577  1.12  jmcneill 
    578  1.12  jmcneill 	return true;
    579  1.12  jmcneill }
    580  1.12  jmcneill 
    581  1.12  jmcneill static struct fdtbus_interrupt_controller_func sunxi_gpio_intrfuncs = {
    582  1.12  jmcneill 	.establish = sunxi_gpio_establish,
    583  1.12  jmcneill 	.disestablish = sunxi_gpio_disestablish,
    584  1.12  jmcneill 	.intrstr = sunxi_gpio_intrstr,
    585  1.12  jmcneill };
    586  1.12  jmcneill 
    587  1.10  jmcneill static const char *
    588  1.10  jmcneill sunxi_pinctrl_parse_function(int phandle)
    589  1.10  jmcneill {
    590  1.10  jmcneill 	const char *function;
    591  1.10  jmcneill 
    592  1.22   thorpej 	function = fdtbus_pinctrl_parse_function(phandle);
    593  1.10  jmcneill 	if (function != NULL)
    594  1.10  jmcneill 		return function;
    595  1.10  jmcneill 
    596  1.10  jmcneill 	return fdtbus_get_string(phandle, "allwinner,function");
    597  1.10  jmcneill }
    598  1.10  jmcneill 
    599  1.10  jmcneill static const char *
    600  1.10  jmcneill sunxi_pinctrl_parse_pins(int phandle, int *pins_len)
    601  1.10  jmcneill {
    602  1.22   thorpej 	const char *pins;
    603  1.10  jmcneill 	int len;
    604  1.10  jmcneill 
    605  1.22   thorpej 	pins = fdtbus_pinctrl_parse_pins(phandle, pins_len);
    606  1.22   thorpej 	if (pins != NULL)
    607  1.22   thorpej 		return pins;
    608  1.10  jmcneill 
    609  1.10  jmcneill 	len = OF_getproplen(phandle, "allwinner,pins");
    610  1.10  jmcneill 	if (len > 0) {
    611  1.10  jmcneill 		*pins_len = len;
    612  1.24  jmcneill 		return fdtbus_get_prop(phandle, "allwinner,pins", pins_len);
    613  1.10  jmcneill 	}
    614  1.10  jmcneill 
    615  1.10  jmcneill 	return NULL;
    616  1.10  jmcneill }
    617  1.10  jmcneill 
    618  1.10  jmcneill static int
    619  1.10  jmcneill sunxi_pinctrl_parse_bias(int phandle)
    620  1.10  jmcneill {
    621  1.10  jmcneill 	u_int pull;
    622  1.22   thorpej 	int bias;
    623  1.22   thorpej 
    624  1.22   thorpej 	bias = fdtbus_pinctrl_parse_bias(phandle, NULL);
    625  1.22   thorpej 	if (bias != -1)
    626  1.22   thorpej 		return bias;
    627  1.10  jmcneill 
    628  1.22   thorpej 	if (of_getprop_uint32(phandle, "allwinner,pull", &pull) == 0) {
    629  1.10  jmcneill 		switch (pull) {
    630  1.10  jmcneill 		case 0:
    631  1.10  jmcneill 			bias = 0;
    632  1.10  jmcneill 			break;
    633  1.10  jmcneill 		case 1:
    634  1.10  jmcneill 			bias = GPIO_PIN_PULLUP;
    635  1.10  jmcneill 			break;
    636  1.10  jmcneill 		case 2:
    637  1.10  jmcneill 			bias = GPIO_PIN_PULLDOWN;
    638  1.10  jmcneill 			break;
    639  1.10  jmcneill 		}
    640  1.10  jmcneill 	}
    641  1.10  jmcneill 
    642  1.10  jmcneill 	return bias;
    643  1.10  jmcneill }
    644  1.10  jmcneill 
    645  1.10  jmcneill static int
    646  1.10  jmcneill sunxi_pinctrl_parse_drive_strength(int phandle)
    647  1.10  jmcneill {
    648  1.10  jmcneill 	int val;
    649  1.10  jmcneill 
    650  1.22   thorpej 	val = fdtbus_pinctrl_parse_drive_strength(phandle);
    651  1.22   thorpej 	if (val != -1)
    652  1.10  jmcneill 		return val;
    653  1.10  jmcneill 
    654  1.10  jmcneill 	if (of_getprop_uint32(phandle, "allwinner,drive", &val) == 0)
    655  1.10  jmcneill 		return (val + 1) * 10;
    656  1.10  jmcneill 
    657  1.10  jmcneill 	return -1;
    658  1.10  jmcneill }
    659  1.10  jmcneill 
    660  1.24  jmcneill static void
    661  1.24  jmcneill sunxi_pinctrl_enable_regulator(struct sunxi_gpio_softc *sc,
    662  1.24  jmcneill     const struct sunxi_gpio_pins *pin_def)
    663  1.24  jmcneill {
    664  1.24  jmcneill 	char supply_prop[16];
    665  1.24  jmcneill 	uint32_t val;
    666  1.24  jmcneill 	u_int uvol;
    667  1.24  jmcneill 	int error;
    668  1.24  jmcneill 
    669  1.24  jmcneill 	const char c = tolower(pin_def->name[1]);
    670  1.24  jmcneill 	if (c < 'a' || c > 'z')
    671  1.24  jmcneill 		return;
    672  1.24  jmcneill 	const int index = c - 'a';
    673  1.24  jmcneill 
    674  1.24  jmcneill 	if (sc->sc_pin_supply[index] != NULL) {
    675  1.24  jmcneill 		/* Already enabled */
    676  1.24  jmcneill 		return;
    677  1.24  jmcneill 	}
    678  1.24  jmcneill 
    679  1.24  jmcneill 	snprintf(supply_prop, sizeof(supply_prop), "vcc-p%c-supply", c);
    680  1.24  jmcneill 	sc->sc_pin_supply[index] = fdtbus_regulator_acquire(sc->sc_phandle, supply_prop);
    681  1.24  jmcneill 	if (sc->sc_pin_supply[index] == NULL)
    682  1.24  jmcneill 		return;
    683  1.24  jmcneill 
    684  1.24  jmcneill 	aprint_debug_dev(sc->sc_dev, "enable \"%s\"\n", supply_prop);
    685  1.24  jmcneill 	error = fdtbus_regulator_enable(sc->sc_pin_supply[index]);
    686  1.24  jmcneill 	if (error != 0)
    687  1.24  jmcneill 		aprint_error_dev(sc->sc_dev, "failed to enable %s: %d\n", supply_prop, error);
    688  1.24  jmcneill 
    689  1.24  jmcneill 	if (sc->sc_padconf->has_io_bias_config) {
    690  1.24  jmcneill 		error = fdtbus_regulator_get_voltage(sc->sc_pin_supply[index], &uvol);
    691  1.24  jmcneill 		if (error != 0) {
    692  1.24  jmcneill 			aprint_error_dev(sc->sc_dev, "failed to get %s voltage: %d\n",
    693  1.24  jmcneill 			    supply_prop, error);
    694  1.24  jmcneill 			uvol = 0;
    695  1.24  jmcneill 		}
    696  1.24  jmcneill 		if (uvol != 0) {
    697  1.24  jmcneill 			if (uvol <= 1800000)
    698  1.24  jmcneill 				val = 0x0;	/* 1.8V */
    699  1.24  jmcneill 			else if (uvol <= 2500000)
    700  1.24  jmcneill 				val = 0x6;	/* 2.5V */
    701  1.24  jmcneill 			else if (uvol <= 2800000)
    702  1.24  jmcneill 				val = 0x9;	/* 2.8V */
    703  1.24  jmcneill 			else if (uvol <= 3000000)
    704  1.24  jmcneill 				val = 0xa;	/* 3.0V */
    705  1.24  jmcneill 			else
    706  1.24  jmcneill 				val = 0xd;	/* 3.3V */
    707  1.24  jmcneill 
    708  1.24  jmcneill 			aprint_debug_dev(sc->sc_dev, "set io bias config for port %d to 0x%x\n",
    709  1.24  jmcneill 			    pin_def->port, val);
    710  1.24  jmcneill 			val = GPIO_READ(sc, SUNXI_GPIO_GRP_CONFIG(pin_def->port));
    711  1.24  jmcneill 			val &= ~SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK;
    712  1.24  jmcneill 			val |= __SHIFTIN(val, SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK);
    713  1.24  jmcneill 			GPIO_WRITE(sc, SUNXI_GPIO_GRP_CONFIG(pin_def->port), val);
    714  1.24  jmcneill 		}
    715  1.24  jmcneill 	}
    716  1.24  jmcneill }
    717  1.24  jmcneill 
    718   1.1  jmcneill static int
    719   1.2  jmcneill sunxi_pinctrl_set_config(device_t dev, const void *data, size_t len)
    720   1.2  jmcneill {
    721   1.2  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(dev);
    722   1.2  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    723  1.10  jmcneill 	int pins_len;
    724   1.2  jmcneill 
    725   1.2  jmcneill 	if (len != 4)
    726   1.2  jmcneill 		return -1;
    727   1.2  jmcneill 
    728   1.2  jmcneill 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
    729   1.2  jmcneill 
    730   1.2  jmcneill 	/*
    731   1.2  jmcneill 	 * Required: pins, function
    732  1.10  jmcneill 	 * Optional: bias, drive strength
    733   1.2  jmcneill 	 */
    734   1.2  jmcneill 
    735  1.10  jmcneill 	const char *function = sunxi_pinctrl_parse_function(phandle);
    736   1.2  jmcneill 	if (function == NULL)
    737   1.2  jmcneill 		return -1;
    738  1.10  jmcneill 	const char *pins = sunxi_pinctrl_parse_pins(phandle, &pins_len);
    739  1.10  jmcneill 	if (pins == NULL)
    740   1.2  jmcneill 		return -1;
    741  1.10  jmcneill 
    742  1.10  jmcneill 	const int bias = sunxi_pinctrl_parse_bias(phandle);
    743  1.10  jmcneill 	const int drive_strength = sunxi_pinctrl_parse_drive_strength(phandle);
    744   1.2  jmcneill 
    745   1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    746   1.5  jmcneill 
    747  1.10  jmcneill 	for (; pins_len > 0;
    748  1.10  jmcneill 	    pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
    749   1.2  jmcneill 		pin_def = sunxi_gpio_lookup_byname(sc, pins);
    750   1.2  jmcneill 		if (pin_def == NULL) {
    751   1.2  jmcneill 			aprint_error_dev(dev, "unknown pin name '%s'\n", pins);
    752   1.2  jmcneill 			continue;
    753   1.2  jmcneill 		}
    754   1.2  jmcneill 		if (sunxi_gpio_setfunc(sc, pin_def, function) != 0)
    755   1.2  jmcneill 			continue;
    756   1.2  jmcneill 
    757  1.10  jmcneill 		if (bias != -1)
    758  1.10  jmcneill 			sunxi_gpio_setpull(sc, pin_def, bias);
    759   1.2  jmcneill 
    760  1.10  jmcneill 		if (drive_strength != -1)
    761   1.2  jmcneill 			sunxi_gpio_setdrv(sc, pin_def, drive_strength);
    762  1.24  jmcneill 
    763  1.24  jmcneill 		sunxi_pinctrl_enable_regulator(sc, pin_def);
    764   1.2  jmcneill 	}
    765   1.2  jmcneill 
    766   1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    767   1.5  jmcneill 
    768   1.2  jmcneill 	return 0;
    769   1.2  jmcneill }
    770   1.2  jmcneill 
    771   1.2  jmcneill static struct fdtbus_pinctrl_controller_func sunxi_pinctrl_funcs = {
    772   1.2  jmcneill 	.set_config = sunxi_pinctrl_set_config,
    773   1.2  jmcneill };
    774   1.2  jmcneill 
    775   1.2  jmcneill static int
    776   1.5  jmcneill sunxi_gpio_pin_read(void *priv, int pin)
    777   1.5  jmcneill {
    778   1.5  jmcneill 	struct sunxi_gpio_softc * const sc = priv;
    779   1.5  jmcneill 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    780   1.5  jmcneill 	uint32_t data;
    781   1.5  jmcneill 	int val;
    782   1.5  jmcneill 
    783   1.5  jmcneill 	KASSERT(pin < sc->sc_padconf->npins);
    784   1.5  jmcneill 
    785   1.5  jmcneill 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    786   1.5  jmcneill 	const uint32_t data_mask = __BIT(pin_def->pin);
    787   1.5  jmcneill 
    788   1.5  jmcneill 	/* No lock required for reads */
    789   1.5  jmcneill 	data = GPIO_READ(sc, data_reg);
    790   1.5  jmcneill 	val = __SHIFTOUT(data, data_mask);
    791   1.5  jmcneill 
    792   1.5  jmcneill 	return val;
    793   1.5  jmcneill }
    794   1.5  jmcneill 
    795   1.5  jmcneill static void
    796   1.5  jmcneill sunxi_gpio_pin_write(void *priv, int pin, int val)
    797   1.5  jmcneill {
    798   1.5  jmcneill 	struct sunxi_gpio_softc * const sc = priv;
    799   1.5  jmcneill 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    800   1.5  jmcneill 	uint32_t data;
    801   1.5  jmcneill 
    802   1.5  jmcneill 	KASSERT(pin < sc->sc_padconf->npins);
    803   1.5  jmcneill 
    804   1.5  jmcneill 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    805   1.5  jmcneill 	const uint32_t data_mask = __BIT(pin_def->pin);
    806   1.5  jmcneill 
    807   1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    808   1.5  jmcneill 	data = GPIO_READ(sc, data_reg);
    809   1.5  jmcneill 	if (val)
    810   1.5  jmcneill 		data |= data_mask;
    811   1.5  jmcneill 	else
    812   1.5  jmcneill 		data &= ~data_mask;
    813   1.5  jmcneill 	GPIO_WRITE(sc, data_reg, data);
    814   1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    815   1.5  jmcneill }
    816   1.5  jmcneill 
    817   1.5  jmcneill static void
    818   1.5  jmcneill sunxi_gpio_pin_ctl(void *priv, int pin, int flags)
    819   1.5  jmcneill {
    820   1.5  jmcneill 	struct sunxi_gpio_softc * const sc = priv;
    821   1.5  jmcneill 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    822   1.5  jmcneill 
    823   1.5  jmcneill 	KASSERT(pin < sc->sc_padconf->npins);
    824   1.5  jmcneill 
    825   1.5  jmcneill 	mutex_enter(&sc->sc_lock);
    826   1.5  jmcneill 	sunxi_gpio_ctl(sc, pin_def, flags);
    827   1.5  jmcneill 	sunxi_gpio_setpull(sc, pin_def, flags);
    828   1.5  jmcneill 	mutex_exit(&sc->sc_lock);
    829   1.5  jmcneill }
    830   1.5  jmcneill 
    831   1.5  jmcneill static void
    832   1.5  jmcneill sunxi_gpio_attach_ports(struct sunxi_gpio_softc *sc)
    833   1.5  jmcneill {
    834   1.5  jmcneill 	const struct sunxi_gpio_pins *pin_def;
    835   1.5  jmcneill 	struct gpio_chipset_tag *gp = &sc->sc_gp;
    836   1.5  jmcneill 	struct gpiobus_attach_args gba;
    837   1.5  jmcneill 	u_int pin;
    838   1.5  jmcneill 
    839   1.5  jmcneill 	gp->gp_cookie = sc;
    840   1.5  jmcneill 	gp->gp_pin_read = sunxi_gpio_pin_read;
    841   1.5  jmcneill 	gp->gp_pin_write = sunxi_gpio_pin_write;
    842   1.5  jmcneill 	gp->gp_pin_ctl = sunxi_gpio_pin_ctl;
    843   1.5  jmcneill 
    844   1.5  jmcneill 	const u_int npins = sc->sc_padconf->npins;
    845   1.5  jmcneill 	sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) * npins, KM_SLEEP);
    846   1.5  jmcneill 
    847   1.5  jmcneill 	for (pin = 0; pin < sc->sc_padconf->npins; pin++) {
    848   1.5  jmcneill 		pin_def = &sc->sc_padconf->pins[pin];
    849   1.5  jmcneill 		sc->sc_pins[pin].pin_num = pin;
    850   1.5  jmcneill 		sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
    851   1.5  jmcneill 		    GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    852   1.5  jmcneill 		sc->sc_pins[pin].pin_state = sunxi_gpio_pin_read(sc, pin);
    853   1.5  jmcneill 		strlcpy(sc->sc_pins[pin].pin_defname, pin_def->name,
    854   1.5  jmcneill 		    sizeof(sc->sc_pins[pin].pin_defname));
    855   1.5  jmcneill 	}
    856   1.5  jmcneill 
    857   1.5  jmcneill 	memset(&gba, 0, sizeof(gba));
    858   1.5  jmcneill 	gba.gba_gc = gp;
    859   1.5  jmcneill 	gba.gba_pins = sc->sc_pins;
    860   1.5  jmcneill 	gba.gba_npins = npins;
    861   1.5  jmcneill 	sc->sc_gpiodev = config_found_ia(sc->sc_dev, "gpiobus", &gba, NULL);
    862   1.5  jmcneill }
    863   1.5  jmcneill 
    864   1.5  jmcneill static int
    865   1.1  jmcneill sunxi_gpio_match(device_t parent, cfdata_t cf, void *aux)
    866   1.1  jmcneill {
    867   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    868   1.1  jmcneill 
    869   1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    870   1.1  jmcneill }
    871   1.1  jmcneill 
    872   1.1  jmcneill static void
    873   1.1  jmcneill sunxi_gpio_attach(device_t parent, device_t self, void *aux)
    874   1.1  jmcneill {
    875   1.1  jmcneill 	struct sunxi_gpio_softc * const sc = device_private(self);
    876   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    877   1.1  jmcneill 	const int phandle = faa->faa_phandle;
    878  1.12  jmcneill 	char intrstr[128];
    879   1.8  jmcneill 	struct fdtbus_reset *rst;
    880   1.8  jmcneill 	struct clk *clk;
    881   1.1  jmcneill 	bus_addr_t addr;
    882   1.1  jmcneill 	bus_size_t size;
    883   1.2  jmcneill 	int child;
    884   1.1  jmcneill 
    885   1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    886   1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    887   1.1  jmcneill 		return;
    888   1.1  jmcneill 	}
    889   1.1  jmcneill 
    890   1.8  jmcneill 	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
    891   1.8  jmcneill 		if (clk_enable(clk) != 0) {
    892   1.8  jmcneill 			aprint_error(": couldn't enable clock\n");
    893   1.8  jmcneill 			return;
    894   1.8  jmcneill 		}
    895   1.8  jmcneill 
    896   1.8  jmcneill 	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
    897   1.8  jmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
    898   1.8  jmcneill 			aprint_error(": couldn't de-assert reset\n");
    899   1.8  jmcneill 			return;
    900   1.8  jmcneill 		}
    901   1.8  jmcneill 
    902   1.1  jmcneill 	sc->sc_dev = self;
    903  1.24  jmcneill 	sc->sc_phandle = phandle;
    904   1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    905   1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    906   1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    907   1.1  jmcneill 		return;
    908   1.1  jmcneill 	}
    909   1.5  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    910   1.1  jmcneill 	sc->sc_padconf = (void *)of_search_compatible(phandle, compat_data)->data;
    911   1.1  jmcneill 
    912   1.1  jmcneill 	aprint_naive("\n");
    913   1.1  jmcneill 	aprint_normal(": PIO\n");
    914   1.1  jmcneill 
    915   1.1  jmcneill 	fdtbus_register_gpio_controller(self, phandle, &sunxi_gpio_funcs);
    916   1.2  jmcneill 
    917   1.2  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    918  1.24  jmcneill 		bool is_valid =
    919  1.24  jmcneill 		    (of_hasprop(child, "function") && of_hasprop(child, "pins")) ||
    920  1.24  jmcneill 		    (of_hasprop(child, "allwinner,function") && of_hasprop(child, "allwinner,pins"));
    921  1.24  jmcneill 		if (!is_valid)
    922   1.2  jmcneill 			continue;
    923   1.2  jmcneill 		fdtbus_register_pinctrl_config(self, child, &sunxi_pinctrl_funcs);
    924   1.2  jmcneill 	}
    925   1.2  jmcneill 
    926   1.2  jmcneill 	fdtbus_pinctrl_configure();
    927   1.5  jmcneill 
    928   1.5  jmcneill 	sunxi_gpio_attach_ports(sc);
    929  1.12  jmcneill 
    930  1.12  jmcneill 	/* Disable all external interrupts */
    931  1.15  jmcneill 	for (int i = 0; i < sc->sc_padconf->npins; i++) {
    932  1.15  jmcneill 		const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[i];
    933  1.15  jmcneill 		if (pin_def->eint_func == 0)
    934  1.15  jmcneill 			continue;
    935  1.15  jmcneill 		GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(pin_def->eint_bank), __BIT(pin_def->eint_num));
    936  1.15  jmcneill 		GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(pin_def->eint_bank), __BIT(pin_def->eint_num));
    937  1.15  jmcneill 
    938  1.15  jmcneill 		if (sc->sc_eint_bank_max < pin_def->eint_bank)
    939  1.15  jmcneill 			sc->sc_eint_bank_max = pin_def->eint_bank;
    940  1.15  jmcneill 	}
    941  1.15  jmcneill 	KASSERT(sc->sc_eint_bank_max < SUNXI_GPIO_MAX_EINT_BANK);
    942  1.12  jmcneill 
    943  1.12  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    944  1.12  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    945  1.12  jmcneill 		return;
    946  1.12  jmcneill 	}
    947  1.12  jmcneill 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
    948  1.12  jmcneill 	    sunxi_gpio_intr, sc);
    949  1.12  jmcneill 	if (sc->sc_ih == NULL) {
    950  1.12  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    951  1.12  jmcneill 		    intrstr);
    952  1.12  jmcneill 		return;
    953  1.12  jmcneill 	}
    954  1.12  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    955  1.12  jmcneill 	fdtbus_register_interrupt_controller(self, phandle,
    956  1.12  jmcneill 	    &sunxi_gpio_intrfuncs);
    957   1.1  jmcneill }
    958