Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_gpio.c revision 1.7
      1  1.7  thorpej /*	$NetBSD: bcm2835_gpio.c,v 1.7 2018/05/19 14:02:10 thorpej Exp $	*/
      2  1.1   kardel 
      3  1.1   kardel /*-
      4  1.6    skrll  * Copyright (c) 2013, 2014, 2017 The NetBSD Foundation, Inc.
      5  1.1   kardel  * All rights reserved.
      6  1.1   kardel  *
      7  1.1   kardel  * This code is derived from software contributed to The NetBSD Foundation
      8  1.6    skrll  * by Jonathan A. Kollasch, Frank Kardel and Nick Hudson
      9  1.1   kardel  *
     10  1.1   kardel  * Redistribution and use in source and binary forms, with or without
     11  1.1   kardel  * modification, are permitted provided that the following conditions
     12  1.1   kardel  * are met:
     13  1.1   kardel  * 1. Redistributions of source code must retain the above copyright
     14  1.1   kardel  *    notice, this list of conditions and the following disclaimer.
     15  1.1   kardel  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   kardel  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   kardel  *    documentation and/or other materials provided with the distribution.
     18  1.1   kardel  *
     19  1.1   kardel  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  1.1   kardel  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   kardel  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   kardel  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     23  1.1   kardel  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     24  1.1   kardel  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     25  1.1   kardel  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     26  1.1   kardel  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27  1.1   kardel  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     28  1.1   kardel  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     29  1.1   kardel  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1   kardel  */
     31  1.1   kardel 
     32  1.1   kardel #include <sys/cdefs.h>
     33  1.7  thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_gpio.c,v 1.7 2018/05/19 14:02:10 thorpej Exp $");
     34  1.1   kardel 
     35  1.1   kardel /*
     36  1.1   kardel  * Driver for BCM2835 GPIO
     37  1.1   kardel  *
     38  1.1   kardel  * see: http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
     39  1.1   kardel  */
     40  1.1   kardel 
     41  1.1   kardel #include <sys/param.h>
     42  1.1   kardel #include <sys/device.h>
     43  1.1   kardel #include <sys/systm.h>
     44  1.1   kardel #include <sys/mutex.h>
     45  1.1   kardel #include <sys/bus.h>
     46  1.1   kardel #include <sys/intr.h>
     47  1.1   kardel #include <sys/kernel.h>
     48  1.6    skrll #include <sys/kmem.h>
     49  1.7  thorpej #include <sys/proc.h>
     50  1.1   kardel #include <sys/gpio.h>
     51  1.1   kardel 
     52  1.1   kardel #include <sys/bitops.h>
     53  1.1   kardel 
     54  1.1   kardel #include <arm/broadcom/bcm2835reg.h>
     55  1.1   kardel #include <arm/broadcom/bcm2835_gpioreg.h>
     56  1.6    skrll 
     57  1.6    skrll #include <dev/gpio/gpiovar.h>
     58  1.6    skrll #include <dev/fdt/fdtvar.h>
     59  1.1   kardel 
     60  1.1   kardel /* #define BCM2835_GPIO_DEBUG */
     61  1.1   kardel #ifdef BCM2835_GPIO_DEBUG
     62  1.1   kardel int bcm2835gpiodebug = 3;
     63  1.1   kardel #define DPRINTF(l, x)	do { if (l <= bcm2835gpiodebug) { printf x; } } while (0)
     64  1.1   kardel #else
     65  1.1   kardel #define DPRINTF(l, x)
     66  1.1   kardel #endif
     67  1.1   kardel 
     68  1.6    skrll #define	BCMGPIO_MAXPINS	54
     69  1.6    skrll 
     70  1.7  thorpej struct bcmgpio_eint {
     71  1.7  thorpej 	 int			(*eint_func)(void *);
     72  1.7  thorpej 	 void			*eint_arg;
     73  1.7  thorpej 	 int			eint_flags;
     74  1.7  thorpej 	 int			eint_bank;
     75  1.7  thorpej 	 int			eint_num;
     76  1.7  thorpej };
     77  1.7  thorpej 
     78  1.7  thorpej #define	BCMGPIO_INTR_POS_EDGE	0x01
     79  1.7  thorpej #define	BCMGPIO_INTR_NEG_EDGE	0x02
     80  1.7  thorpej #define	BCMGPIO_INTR_HIGH_LEVEL	0x04
     81  1.7  thorpej #define	BCMGPIO_INTR_LOW_LEVEL	0x08
     82  1.7  thorpej #define	BCMGPIO_INTR_MPSAFE	0x10
     83  1.7  thorpej 
     84  1.7  thorpej struct bcmgpio_softc;
     85  1.7  thorpej struct bcmgpio_bank {
     86  1.7  thorpej 	struct bcmgpio_softc	*sc_bcm;
     87  1.7  thorpej 	void			*sc_ih;
     88  1.7  thorpej 	struct bcmgpio_eint	sc_eint[32];
     89  1.7  thorpej 	int			sc_bankno;
     90  1.7  thorpej };
     91  1.7  thorpej #define	BCMGPIO_NBANKS	2
     92  1.7  thorpej 
     93  1.1   kardel struct bcmgpio_softc {
     94  1.1   kardel 	device_t		sc_dev;
     95  1.1   kardel 	bus_space_tag_t		sc_iot;
     96  1.1   kardel 	bus_space_handle_t	sc_ioh;
     97  1.1   kardel 	struct gpio_chipset_tag	sc_gpio_gc;
     98  1.6    skrll 
     99  1.6    skrll 	kmutex_t		sc_lock;
    100  1.6    skrll 	gpio_pin_t		sc_gpio_pins[BCMGPIO_MAXPINS];
    101  1.7  thorpej 
    102  1.7  thorpej 	/* For interrupt support. */
    103  1.7  thorpej 	struct bcmgpio_bank	sc_banks[BCMGPIO_NBANKS];
    104  1.6    skrll };
    105  1.6    skrll 
    106  1.6    skrll struct bcmgpio_pin {
    107  1.6    skrll 	int			pin_no;
    108  1.6    skrll 	u_int			pin_flags;
    109  1.6    skrll 	bool			pin_actlo;
    110  1.1   kardel };
    111  1.1   kardel 
    112  1.6    skrll 
    113  1.1   kardel static int	bcmgpio_match(device_t, cfdata_t, void *);
    114  1.1   kardel static void	bcmgpio_attach(device_t, device_t, void *);
    115  1.1   kardel 
    116  1.6    skrll static int	bcm2835gpio_gpio_pin_read(void *, int);
    117  1.6    skrll static void	bcm2835gpio_gpio_pin_write(void *, int, int);
    118  1.6    skrll static void	bcm2835gpio_gpio_pin_ctl(void *, int, int);
    119  1.6    skrll 
    120  1.7  thorpej static void *	bcmgpio_gpio_intr_establish(void *, int, int, int,
    121  1.7  thorpej 					    int (*)(void *), void *);
    122  1.7  thorpej static void	bcmgpio_gpio_intr_disestablish(void *, void *);
    123  1.7  thorpej static bool	bcmgpio_gpio_intrstr(void *, int, int, char *, size_t);
    124  1.7  thorpej 
    125  1.7  thorpej static int	bcmgpio_intr(void *);
    126  1.7  thorpej 
    127  1.6    skrll u_int		bcm283x_pin_getfunc(const struct bcmgpio_softc * const, u_int);
    128  1.6    skrll void		bcm283x_pin_setfunc(const struct bcmgpio_softc * const, u_int,
    129  1.6    skrll 		    u_int);
    130  1.6    skrll void		bcm283x_pin_setpull(const struct bcmgpio_softc * const, u_int,
    131  1.6    skrll 		    u_int);
    132  1.6    skrll 
    133  1.6    skrll static int 	bcm283x_pinctrl_set_config(device_t, const void *, size_t);
    134  1.6    skrll 
    135  1.6    skrll static void *	bcmgpio_fdt_acquire(device_t, const void *, size_t, int);
    136  1.6    skrll static void	bcmgpio_fdt_release(device_t, void *);
    137  1.6    skrll static int	bcmgpio_fdt_read(device_t, void *, bool);
    138  1.6    skrll static void	bcmgpio_fdt_write(device_t, void *, int, bool);
    139  1.6    skrll 
    140  1.6    skrll static struct fdtbus_gpio_controller_func bcmgpio_funcs = {
    141  1.6    skrll 	.acquire = bcmgpio_fdt_acquire,
    142  1.6    skrll 	.release = bcmgpio_fdt_release,
    143  1.6    skrll 	.read = bcmgpio_fdt_read,
    144  1.6    skrll 	.write = bcmgpio_fdt_write
    145  1.6    skrll };
    146  1.1   kardel 
    147  1.7  thorpej static void *	bcmgpio_fdt_intr_establish(device_t, u_int *, int, int,
    148  1.7  thorpej 		    int (*func)(void *), void *);
    149  1.7  thorpej static void	bcmgpio_fdt_intr_disestablish(device_t, void *);
    150  1.7  thorpej static bool	bcmgpio_fdt_intrstr(device_t, u_int *, char *, size_t);
    151  1.7  thorpej 
    152  1.7  thorpej static struct fdtbus_interrupt_controller_func bcmgpio_fdt_intrfuncs = {
    153  1.7  thorpej 	.establish = bcmgpio_fdt_intr_establish,
    154  1.7  thorpej 	.disestablish = bcmgpio_fdt_intr_disestablish,
    155  1.7  thorpej 	.intrstr = bcmgpio_fdt_intrstr,
    156  1.7  thorpej };
    157  1.7  thorpej 
    158  1.1   kardel CFATTACH_DECL_NEW(bcmgpio, sizeof(struct bcmgpio_softc),
    159  1.1   kardel     bcmgpio_match, bcmgpio_attach, NULL, NULL);
    160  1.1   kardel 
    161  1.6    skrll 
    162  1.6    skrll static struct fdtbus_pinctrl_controller_func bcm283x_pinctrl_funcs = {
    163  1.6    skrll 	.set_config = bcm283x_pinctrl_set_config,
    164  1.6    skrll };
    165  1.6    skrll 
    166  1.6    skrll static int
    167  1.6    skrll bcm283x_pinctrl_set_config(device_t dev, const void *data, size_t len)
    168  1.6    skrll {
    169  1.6    skrll 	struct bcmgpio_softc * const sc = device_private(dev);
    170  1.6    skrll 
    171  1.6    skrll 	if (len != 4)
    172  1.6    skrll 		return -1;
    173  1.6    skrll 
    174  1.6    skrll 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
    175  1.6    skrll 
    176  1.6    skrll 	/*
    177  1.6    skrll 	 * Required: brcm,pins
    178  1.6    skrll 	 * Optional: brcm,function, brcm,pull
    179  1.6    skrll 	 */
    180  1.6    skrll 
    181  1.6    skrll 	int pins_len;
    182  1.6    skrll 	const u_int *pins = fdtbus_get_prop(phandle, "brcm,pins", &pins_len);
    183  1.6    skrll 
    184  1.6    skrll 	if (pins == NULL)
    185  1.6    skrll 		return -1;
    186  1.6    skrll 
    187  1.6    skrll 	int pull_len = 0;
    188  1.6    skrll 	const u_int *pull = fdtbus_get_prop(phandle, "brcm,pull", &pull_len);
    189  1.6    skrll 
    190  1.6    skrll 	int func_len = 0;
    191  1.6    skrll 	const u_int *func = fdtbus_get_prop(phandle, "brcm,function", &func_len);
    192  1.6    skrll 
    193  1.6    skrll 	if (!pull && !func) {
    194  1.6    skrll 		aprint_error_dev(dev, "one of brcm,pull or brcm,funcion must "
    195  1.6    skrll 		    "be specified");
    196  1.6    skrll 		return -1;
    197  1.6    skrll 	}
    198  1.6    skrll 
    199  1.6    skrll 	const int npins = pins_len / 4;
    200  1.6    skrll 	const int npull = pull_len / 4;
    201  1.6    skrll 	const int nfunc = func_len / 4;
    202  1.6    skrll 
    203  1.6    skrll 	if (npull > 1 && npull != npins) {
    204  1.6    skrll 		aprint_error_dev(dev, "brcm,pull must have 1 or %d entries",
    205  1.6    skrll 		    npins);
    206  1.6    skrll 		return -1;
    207  1.6    skrll 	}
    208  1.6    skrll 	if (nfunc > 1 && nfunc != npins) {
    209  1.6    skrll 		aprint_error_dev(dev, "brcm,function must have 1 or %d entries",
    210  1.6    skrll 		    npins);
    211  1.6    skrll 		return -1;
    212  1.6    skrll 	}
    213  1.6    skrll 
    214  1.6    skrll 	mutex_enter(&sc->sc_lock);
    215  1.6    skrll 
    216  1.6    skrll 	for (int i = 0; i < npins; i++) {
    217  1.6    skrll 		const u_int pin = be32toh(pins[i]);
    218  1.6    skrll 
    219  1.6    skrll 		if (pin > BCMGPIO_MAXPINS)
    220  1.6    skrll 			continue;
    221  1.6    skrll 		if (pull) {
    222  1.6    skrll 			const int value = be32toh(pull[npull == 1 ? 0 : i]);
    223  1.6    skrll 			bcm283x_pin_setpull(sc, pin, value);
    224  1.6    skrll 		}
    225  1.6    skrll 		if (func) {
    226  1.6    skrll 			const int value = be32toh(func[nfunc == 1 ? 0 : i]);
    227  1.6    skrll 			bcm283x_pin_setfunc(sc, pin, value);
    228  1.6    skrll 		}
    229  1.6    skrll 	}
    230  1.6    skrll 
    231  1.6    skrll 	mutex_exit(&sc->sc_lock);
    232  1.6    skrll 
    233  1.6    skrll 	return 0;
    234  1.6    skrll }
    235  1.6    skrll 
    236  1.1   kardel static int
    237  1.1   kardel bcmgpio_match(device_t parent, cfdata_t cf, void *aux)
    238  1.1   kardel {
    239  1.6    skrll 	const char * const compatible[] = { "brcm,bcm2835-gpio", NULL };
    240  1.6    skrll 	struct fdt_attach_args * const faa = aux;
    241  1.1   kardel 
    242  1.6    skrll 	return of_match_compatible(faa->faa_phandle, compatible);
    243  1.1   kardel }
    244  1.1   kardel 
    245  1.1   kardel static void
    246  1.1   kardel bcmgpio_attach(device_t parent, device_t self, void *aux)
    247  1.1   kardel {
    248  1.1   kardel 	struct bcmgpio_softc * const sc = device_private(self);
    249  1.6    skrll 	struct fdt_attach_args * const faa = aux;
    250  1.3    skrll 	struct gpiobus_attach_args gba;
    251  1.6    skrll 	bus_addr_t addr;
    252  1.6    skrll 	bus_size_t size;
    253  1.1   kardel 	u_int func;
    254  1.3    skrll 	int error;
    255  1.6    skrll 	int pin;
    256  1.7  thorpej 	int bank;
    257  1.5    skrll 
    258  1.6    skrll 	const int phandle = faa->faa_phandle;
    259  1.6    skrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    260  1.6    skrll 		aprint_error(": couldn't get registers\n");
    261  1.1   kardel 		return;
    262  1.1   kardel 	}
    263  1.5    skrll 
    264  1.6    skrll 	sc->sc_dev = self;
    265  1.6    skrll 
    266  1.5    skrll 	aprint_naive("\n");
    267  1.6    skrll 	aprint_normal(": GPIO controller\n");
    268  1.1   kardel 
    269  1.6    skrll 	sc->sc_iot = faa->faa_bst;
    270  1.6    skrll 	error = bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh);
    271  1.3    skrll 	if (error) {
    272  1.6    skrll 		aprint_error_dev(self, "couldn't map registers\n");
    273  1.3    skrll 		return;
    274  1.3    skrll 	}
    275  1.3    skrll 
    276  1.6    skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    277  1.5    skrll 
    278  1.6    skrll 	for (pin = 0; pin < BCMGPIO_MAXPINS; pin++) {
    279  1.6    skrll 		sc->sc_gpio_pins[pin].pin_num = pin;
    280  1.1   kardel 		/*
    281  1.1   kardel 		 * find out pins still available for GPIO
    282  1.1   kardel 		 */
    283  1.6    skrll 		func = bcm283x_pin_getfunc(sc, pin);
    284  1.5    skrll 
    285  1.1   kardel 		if (func == BCM2835_GPIO_IN ||
    286  1.1   kardel 		    func == BCM2835_GPIO_OUT) {
    287  1.7  thorpej 			/* XXX TRISTATE?  Really? */
    288  1.6    skrll 			sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
    289  1.1   kardel 				GPIO_PIN_OUTPUT |
    290  1.1   kardel 				GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
    291  1.1   kardel 				GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    292  1.7  thorpej 			sc->sc_gpio_pins[pin].pin_intrcaps =
    293  1.7  thorpej 				GPIO_INTR_POS_EDGE |
    294  1.7  thorpej 				GPIO_INTR_NEG_EDGE |
    295  1.7  thorpej 				GPIO_INTR_DOUBLE_EDGE |
    296  1.7  thorpej 				GPIO_INTR_HIGH_LEVEL |
    297  1.7  thorpej 				GPIO_INTR_LOW_LEVEL |
    298  1.7  thorpej 				GPIO_INTR_MPSAFE;
    299  1.1   kardel 			/* read initial state */
    300  1.6    skrll 			sc->sc_gpio_pins[pin].pin_state =
    301  1.6    skrll 				bcm2835gpio_gpio_pin_read(sc, pin);
    302  1.1   kardel 			DPRINTF(1, ("%s: attach pin %d\n", device_xname(sc->sc_dev), pin));
    303  1.4    skrll 		} else {
    304  1.6    skrll 			sc->sc_gpio_pins[pin].pin_caps = 0;
    305  1.6    skrll 			sc->sc_gpio_pins[pin].pin_state = 0;
    306  1.1   kardel   			DPRINTF(1, ("%s: skip pin %d - func = 0x%x\n", device_xname(sc->sc_dev), pin, func));
    307  1.4    skrll 		}
    308  1.4    skrll 	}
    309  1.5    skrll 
    310  1.7  thorpej 	/* Initialize interrupts. */
    311  1.7  thorpej 	for (bank = 0; bank < BCMGPIO_NBANKS; bank++) {
    312  1.7  thorpej 		char intrstr[128];
    313  1.7  thorpej 
    314  1.7  thorpej 		if (!fdtbus_intr_str(phandle, bank, intrstr, sizeof(intrstr))) {
    315  1.7  thorpej 			aprint_error_dev(self, "failed to decode interrupt\n");
    316  1.7  thorpej 			continue;
    317  1.7  thorpej 		}
    318  1.5    skrll 
    319  1.7  thorpej 		sc->sc_banks[bank].sc_bankno = bank;
    320  1.7  thorpej 		sc->sc_banks[bank].sc_bcm = sc;
    321  1.7  thorpej 		sc->sc_banks[bank].sc_ih =
    322  1.7  thorpej 		    fdtbus_intr_establish(phandle, bank, IPL_VM,
    323  1.7  thorpej 		    			  FDT_INTR_MPSAFE,
    324  1.7  thorpej 					  bcmgpio_intr, &sc->sc_banks[bank]);
    325  1.7  thorpej 		if (sc->sc_banks[bank].sc_ih) {
    326  1.7  thorpej 			aprint_normal_dev(self,
    327  1.7  thorpej 			    "pins %d..%d interrupting on %s\n",
    328  1.7  thorpej 			    bank * 32,
    329  1.7  thorpej 			    MIN((bank * 32) + 31, BCMGPIO_MAXPINS),
    330  1.7  thorpej 			    intrstr);
    331  1.7  thorpej 		} else {
    332  1.7  thorpej 			aprint_normal_dev(self,
    333  1.7  thorpej 			    "failed to establish interrupt for pins %d..%d\n",
    334  1.7  thorpej 			    bank * 32,
    335  1.7  thorpej 			    MIN((bank * 32) + 31, BCMGPIO_MAXPINS));
    336  1.7  thorpej 		}
    337  1.6    skrll 	}
    338  1.6    skrll 
    339  1.6    skrll 	fdtbus_register_gpio_controller(self, faa->faa_phandle, &bcmgpio_funcs);
    340  1.1   kardel 
    341  1.6    skrll 	for (int child = OF_child(phandle); child; child = OF_peer(child)) {
    342  1.6    skrll 		if (!of_hasprop(child, "brcm,pins"))
    343  1.6    skrll 			continue;
    344  1.6    skrll 		fdtbus_register_pinctrl_config(self, child,
    345  1.6    skrll 		    &bcm283x_pinctrl_funcs);
    346  1.6    skrll 	}
    347  1.6    skrll 
    348  1.6    skrll 	fdtbus_pinctrl_configure();
    349  1.7  thorpej 
    350  1.7  thorpej 	fdtbus_register_interrupt_controller(self, phandle,
    351  1.7  thorpej 	    &bcmgpio_fdt_intrfuncs);
    352  1.7  thorpej 
    353  1.7  thorpej 	/* create controller tag */
    354  1.7  thorpej 	sc->sc_gpio_gc.gp_cookie = sc;
    355  1.7  thorpej 	sc->sc_gpio_gc.gp_pin_read = bcm2835gpio_gpio_pin_read;
    356  1.7  thorpej 	sc->sc_gpio_gc.gp_pin_write = bcm2835gpio_gpio_pin_write;
    357  1.7  thorpej 	sc->sc_gpio_gc.gp_pin_ctl = bcm2835gpio_gpio_pin_ctl;
    358  1.7  thorpej 	sc->sc_gpio_gc.gp_intr_establish = bcmgpio_gpio_intr_establish;
    359  1.7  thorpej 	sc->sc_gpio_gc.gp_intr_disestablish = bcmgpio_gpio_intr_disestablish;
    360  1.7  thorpej 	sc->sc_gpio_gc.gp_intr_str = bcmgpio_gpio_intrstr;
    361  1.7  thorpej 
    362  1.7  thorpej 	gba.gba_gc = &sc->sc_gpio_gc;
    363  1.7  thorpej 	gba.gba_pins = &sc->sc_gpio_pins[0];
    364  1.7  thorpej 	gba.gba_npins = BCMGPIO_MAXPINS;
    365  1.7  thorpej 	(void) config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    366  1.7  thorpej }
    367  1.7  thorpej 
    368  1.7  thorpej /* GPIO interrupt support functions */
    369  1.7  thorpej 
    370  1.7  thorpej static int
    371  1.7  thorpej bcmgpio_intr(void *arg)
    372  1.7  thorpej {
    373  1.7  thorpej 	struct bcmgpio_bank * const b = arg;
    374  1.7  thorpej 	struct bcmgpio_softc * const sc = b->sc_bcm;
    375  1.7  thorpej 	struct bcmgpio_eint *eint;
    376  1.7  thorpej 	uint32_t status, pending, bit;
    377  1.7  thorpej 	uint32_t clear_level;
    378  1.7  thorpej 	int (*func)(void *);
    379  1.7  thorpej 	int rv = 0;
    380  1.7  thorpej 
    381  1.7  thorpej 	for (;;) {
    382  1.7  thorpej 		status = pending = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    383  1.7  thorpej 		    BCM2835_GPIO_GPEDS(b->sc_bankno));
    384  1.7  thorpej 		if (status == 0)
    385  1.7  thorpej 			break;
    386  1.7  thorpej 
    387  1.7  thorpej 		/*
    388  1.7  thorpej 		 * This will clear the indicator for any pending
    389  1.7  thorpej 		 * edge-triggered pins, but level-triggered pins
    390  1.7  thorpej 		 * will still be indicated until the pin is
    391  1.7  thorpej 		 * de-asserted.  We'll have to clear level-triggered
    392  1.7  thorpej 		 * indicators below.
    393  1.7  thorpej 		 */
    394  1.7  thorpej 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    395  1.7  thorpej 		    BCM2835_GPIO_GPEDS(b->sc_bankno), status);
    396  1.7  thorpej 		clear_level = 0;
    397  1.7  thorpej 
    398  1.7  thorpej 		while ((bit = ffs32(pending)) != 0) {
    399  1.7  thorpej 			pending &= ~__BIT(bit - 1);
    400  1.7  thorpej 			eint = &b->sc_eint[bit - 1];
    401  1.7  thorpej 			if ((func = eint->eint_func) == NULL)
    402  1.7  thorpej 				continue;
    403  1.7  thorpej 			if (eint->eint_flags & (BCMGPIO_INTR_HIGH_LEVEL |
    404  1.7  thorpej 						BCMGPIO_INTR_LOW_LEVEL))
    405  1.7  thorpej 				clear_level |= __BIT(bit - 1);
    406  1.7  thorpej 			const bool mpsafe =
    407  1.7  thorpej 			    (eint->eint_flags & BCMGPIO_INTR_MPSAFE) != 0;
    408  1.7  thorpej 			if (!mpsafe)
    409  1.7  thorpej 				KERNEL_LOCK(1, curlwp);
    410  1.7  thorpej 			rv |= (*func)(eint->eint_arg);
    411  1.7  thorpej 			if (!mpsafe)
    412  1.7  thorpej 				KERNEL_UNLOCK_ONE(curlwp);
    413  1.7  thorpej 		}
    414  1.7  thorpej 
    415  1.7  thorpej 		/*
    416  1.7  thorpej 		 * Now that all of the handlers have been called,
    417  1.7  thorpej 		 * we can clear the indicators for any level-triggered
    418  1.7  thorpej 		 * pins.
    419  1.7  thorpej 		 */
    420  1.7  thorpej 		if (clear_level)
    421  1.7  thorpej 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    422  1.7  thorpej 			    BCM2835_GPIO_GPEDS(b->sc_bankno), clear_level);
    423  1.7  thorpej 	}
    424  1.7  thorpej 
    425  1.7  thorpej 	return (rv);
    426  1.7  thorpej }
    427  1.7  thorpej 
    428  1.7  thorpej static void *
    429  1.7  thorpej bmcgpio_intr_enable(struct bcmgpio_softc *sc, int (*func)(void *), void *arg,
    430  1.7  thorpej 		    int bank, int pin, int flags)
    431  1.7  thorpej {
    432  1.7  thorpej 	struct bcmgpio_eint *eint;
    433  1.7  thorpej 	uint32_t mask, enabled_ren, enabled_fen, enabled_hen, enabled_len;
    434  1.7  thorpej 	int has_edge = flags & (BCMGPIO_INTR_POS_EDGE|BCMGPIO_INTR_NEG_EDGE);
    435  1.7  thorpej 	int has_level = flags &
    436  1.7  thorpej 	    (BCMGPIO_INTR_HIGH_LEVEL|BCMGPIO_INTR_LOW_LEVEL);
    437  1.7  thorpej 
    438  1.7  thorpej 	if (bank < 0 || bank >= BCMGPIO_NBANKS)
    439  1.7  thorpej 		return NULL;
    440  1.7  thorpej 	if (pin < 0 || pin >= 32)
    441  1.7  thorpej 		return (NULL);
    442  1.7  thorpej 
    443  1.7  thorpej 	/* Must specify a mode. */
    444  1.7  thorpej 	if (!has_edge && !has_level)
    445  1.7  thorpej 		return (NULL);
    446  1.7  thorpej 
    447  1.7  thorpej 	/* Can't have HIGH and LOW together. */
    448  1.7  thorpej 	if (has_level == (BCMGPIO_INTR_HIGH_LEVEL|BCMGPIO_INTR_LOW_LEVEL))
    449  1.7  thorpej 		return (NULL);
    450  1.7  thorpej 
    451  1.7  thorpej 	/* Can't have EDGE and LEVEL together. */
    452  1.7  thorpej 	if (has_edge && has_level)
    453  1.7  thorpej 		return (NULL);
    454  1.7  thorpej 
    455  1.7  thorpej 	eint = &sc->sc_banks[bank].sc_eint[pin];
    456  1.7  thorpej 
    457  1.7  thorpej 	mask = __BIT(pin);
    458  1.7  thorpej 
    459  1.7  thorpej 	mutex_enter(&sc->sc_lock);
    460  1.7  thorpej 
    461  1.7  thorpej 	if (eint->eint_func != NULL) {
    462  1.7  thorpej 		mutex_exit(&sc->sc_lock);
    463  1.7  thorpej 		return (NULL);	/* in use */
    464  1.7  thorpej 	}
    465  1.7  thorpej 
    466  1.7  thorpej 	eint->eint_func = func;
    467  1.7  thorpej 	eint->eint_arg = arg;
    468  1.7  thorpej 	eint->eint_flags = flags;
    469  1.7  thorpej 	eint->eint_bank = bank;
    470  1.7  thorpej 	eint->eint_num = pin;
    471  1.7  thorpej 
    472  1.7  thorpej 	enabled_ren = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    473  1.7  thorpej 				       BCM2835_GPIO_GPREN(bank));
    474  1.7  thorpej 	enabled_fen = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    475  1.7  thorpej 				       BCM2835_GPIO_GPFEN(bank));
    476  1.7  thorpej 	enabled_hen = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    477  1.7  thorpej 				       BCM2835_GPIO_GPHEN(bank));
    478  1.7  thorpej 	enabled_len = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    479  1.7  thorpej 				       BCM2835_GPIO_GPLEN(bank));
    480  1.7  thorpej 
    481  1.7  thorpej 	enabled_ren &= ~mask;
    482  1.7  thorpej 	enabled_fen &= ~mask;
    483  1.7  thorpej 	enabled_hen &= ~mask;
    484  1.7  thorpej 	enabled_len &= ~mask;
    485  1.7  thorpej 
    486  1.7  thorpej 	if (flags & BCMGPIO_INTR_POS_EDGE)
    487  1.7  thorpej 		enabled_ren |= mask;
    488  1.7  thorpej 	if (flags & BCMGPIO_INTR_NEG_EDGE)
    489  1.7  thorpej 		enabled_fen |= mask;
    490  1.7  thorpej 	if (flags & BCMGPIO_INTR_HIGH_LEVEL)
    491  1.7  thorpej 		enabled_hen |= mask;
    492  1.7  thorpej 	if (flags & BCMGPIO_INTR_LOW_LEVEL)
    493  1.7  thorpej 		enabled_len |= mask;
    494  1.7  thorpej 
    495  1.7  thorpej 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    496  1.7  thorpej 			  BCM2835_GPIO_GPREN(bank), enabled_ren);
    497  1.7  thorpej 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    498  1.7  thorpej 			  BCM2835_GPIO_GPFEN(bank), enabled_fen);
    499  1.7  thorpej 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    500  1.7  thorpej 			  BCM2835_GPIO_GPHEN(bank), enabled_hen);
    501  1.7  thorpej 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    502  1.7  thorpej 			  BCM2835_GPIO_GPLEN(bank), enabled_len);
    503  1.7  thorpej 
    504  1.7  thorpej 	mutex_exit(&sc->sc_lock);
    505  1.7  thorpej 	return (eint);
    506  1.7  thorpej }
    507  1.7  thorpej 
    508  1.7  thorpej static void
    509  1.7  thorpej bcmgpio_intr_disable(struct bcmgpio_softc *sc, struct bcmgpio_eint *eint)
    510  1.7  thorpej {
    511  1.7  thorpej 	uint32_t mask, enabled_ren, enabled_fen, enabled_hen, enabled_len;
    512  1.7  thorpej 	int bank = eint->eint_bank;
    513  1.7  thorpej 
    514  1.7  thorpej 	mask = __BIT(eint->eint_num);
    515  1.7  thorpej 
    516  1.7  thorpej 	KASSERT(eint->eint_func != NULL);
    517  1.7  thorpej 
    518  1.7  thorpej 	mutex_enter(&sc->sc_lock);
    519  1.7  thorpej 
    520  1.7  thorpej 	enabled_ren = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    521  1.7  thorpej 				       BCM2835_GPIO_GPREN(bank));
    522  1.7  thorpej 	enabled_fen = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    523  1.7  thorpej 				       BCM2835_GPIO_GPFEN(bank));
    524  1.7  thorpej 	enabled_hen = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    525  1.7  thorpej 				       BCM2835_GPIO_GPHEN(bank));
    526  1.7  thorpej 	enabled_len = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    527  1.7  thorpej 				       BCM2835_GPIO_GPLEN(bank));
    528  1.7  thorpej 
    529  1.7  thorpej 	enabled_ren &= ~mask;
    530  1.7  thorpej 	enabled_fen &= ~mask;
    531  1.7  thorpej 	enabled_hen &= ~mask;
    532  1.7  thorpej 	enabled_len &= ~mask;
    533  1.7  thorpej 
    534  1.7  thorpej 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    535  1.7  thorpej 			  BCM2835_GPIO_GPREN(bank), enabled_ren);
    536  1.7  thorpej 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    537  1.7  thorpej 			  BCM2835_GPIO_GPFEN(bank), enabled_fen);
    538  1.7  thorpej 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    539  1.7  thorpej 			  BCM2835_GPIO_GPHEN(bank), enabled_hen);
    540  1.7  thorpej 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    541  1.7  thorpej 			  BCM2835_GPIO_GPLEN(bank), enabled_len);
    542  1.7  thorpej 
    543  1.7  thorpej 	eint->eint_func = NULL;
    544  1.7  thorpej 	eint->eint_arg = NULL;
    545  1.7  thorpej 	eint->eint_flags = 0;
    546  1.7  thorpej 
    547  1.7  thorpej 	mutex_exit(&sc->sc_lock);
    548  1.7  thorpej }
    549  1.7  thorpej 
    550  1.7  thorpej static void *
    551  1.7  thorpej bcmgpio_fdt_intr_establish(device_t dev, u_int *specifier, int ipl, int flags,
    552  1.7  thorpej     int (*func)(void *), void *arg)
    553  1.7  thorpej {
    554  1.7  thorpej 	struct bcmgpio_softc * const sc = device_private(dev);
    555  1.7  thorpej 	int eint_flags = (flags & FDT_INTR_MPSAFE) ? BCMGPIO_INTR_MPSAFE : 0;
    556  1.7  thorpej 
    557  1.7  thorpej 	if (ipl != IPL_VM) {
    558  1.7  thorpej 		aprint_error_dev(dev, "%s: wrong IPL %d (expected %d)\n",
    559  1.7  thorpej 		    __func__, ipl, IPL_VM);
    560  1.7  thorpej 		return (NULL);
    561  1.7  thorpej 	}
    562  1.7  thorpej 
    563  1.7  thorpej 	/* 1st cell is the bank */
    564  1.7  thorpej 	/* 2nd cell is the pin */
    565  1.7  thorpej 	/* 3rd cell is flags */
    566  1.7  thorpej 	const u_int bank = be32toh(specifier[0]);
    567  1.7  thorpej 	const u_int pin = be32toh(specifier[1]);
    568  1.7  thorpej 	const u_int type = be32toh(specifier[2]) & 0xf;
    569  1.7  thorpej 
    570  1.7  thorpej 	switch (type) {
    571  1.7  thorpej 	case 0x1:
    572  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_POS_EDGE;
    573  1.7  thorpej 		break;
    574  1.7  thorpej 	case 0x2:
    575  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_NEG_EDGE;
    576  1.7  thorpej 		break;
    577  1.7  thorpej 	case 0x3:
    578  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_POS_EDGE | BCMGPIO_INTR_NEG_EDGE;
    579  1.7  thorpej 		break;
    580  1.7  thorpej 	case 0x4:
    581  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_HIGH_LEVEL;
    582  1.7  thorpej 		break;
    583  1.7  thorpej 	case 0x8:
    584  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_LOW_LEVEL;
    585  1.7  thorpej 		break;
    586  1.7  thorpej 	default:
    587  1.7  thorpej 		aprint_error_dev(dev, "%s: unsupported irq type 0x%x\n",
    588  1.7  thorpej 		    __func__, type);
    589  1.7  thorpej 		return (NULL);
    590  1.7  thorpej 	}
    591  1.7  thorpej 
    592  1.7  thorpej 	return (bmcgpio_intr_enable(sc, func, arg, bank, pin, eint_flags));
    593  1.7  thorpej }
    594  1.7  thorpej 
    595  1.7  thorpej static void
    596  1.7  thorpej bcmgpio_fdt_intr_disestablish(device_t dev, void *ih)
    597  1.7  thorpej {
    598  1.7  thorpej 	struct bcmgpio_softc * const sc = device_private(dev);
    599  1.7  thorpej 	struct bcmgpio_eint * const eint = ih;
    600  1.7  thorpej 
    601  1.7  thorpej 	bcmgpio_intr_disable(sc, eint);
    602  1.7  thorpej }
    603  1.7  thorpej 
    604  1.7  thorpej static void *
    605  1.7  thorpej bcmgpio_gpio_intr_establish(void *vsc, int pin, int ipl, int irqmode,
    606  1.7  thorpej 			    int (*func)(void *), void *arg)
    607  1.7  thorpej {
    608  1.7  thorpej 	struct bcmgpio_softc * const sc = vsc;
    609  1.7  thorpej 	int eint_flags = (irqmode & GPIO_INTR_MPSAFE) ? BCMGPIO_INTR_MPSAFE : 0;
    610  1.7  thorpej 	int bank = pin / 32;
    611  1.7  thorpej 	int type = irqmode & GPIO_INTR_MODE_MASK;
    612  1.7  thorpej 
    613  1.7  thorpej 	pin %= 32;
    614  1.7  thorpej 
    615  1.7  thorpej 	if (ipl != IPL_VM) {
    616  1.7  thorpej 		aprint_error_dev(sc->sc_dev, "%s: wrong IPL %d (expected %d)\n",
    617  1.7  thorpej 		    __func__, ipl, IPL_VM);
    618  1.7  thorpej 		return (NULL);
    619  1.7  thorpej 	}
    620  1.7  thorpej 
    621  1.7  thorpej 	switch (type) {
    622  1.7  thorpej 	case GPIO_INTR_POS_EDGE:
    623  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_POS_EDGE;
    624  1.7  thorpej 		break;
    625  1.7  thorpej 	case GPIO_INTR_NEG_EDGE:
    626  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_NEG_EDGE;
    627  1.7  thorpej 		break;
    628  1.7  thorpej 	case GPIO_INTR_DOUBLE_EDGE:
    629  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_POS_EDGE | BCMGPIO_INTR_NEG_EDGE;
    630  1.7  thorpej 		break;
    631  1.7  thorpej 	case GPIO_INTR_HIGH_LEVEL:
    632  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_HIGH_LEVEL;
    633  1.7  thorpej 		break;
    634  1.7  thorpej 	case GPIO_INTR_LOW_LEVEL:
    635  1.7  thorpej 		eint_flags |= BCMGPIO_INTR_LOW_LEVEL;
    636  1.7  thorpej 		break;
    637  1.7  thorpej 	default:
    638  1.7  thorpej 		aprint_error_dev(sc->sc_dev, "%s: unsupported irq type 0x%x\n",
    639  1.7  thorpej 		    __func__, type);
    640  1.7  thorpej 		return (NULL);
    641  1.7  thorpej 	}
    642  1.7  thorpej 
    643  1.7  thorpej 	return (bmcgpio_intr_enable(sc, func, arg, bank, pin, eint_flags));
    644  1.7  thorpej }
    645  1.7  thorpej 
    646  1.7  thorpej static void
    647  1.7  thorpej bcmgpio_gpio_intr_disestablish(void *vsc, void *ih)
    648  1.7  thorpej {
    649  1.7  thorpej 	struct bcmgpio_softc * const sc = vsc;
    650  1.7  thorpej 	struct bcmgpio_eint * const eint = ih;
    651  1.7  thorpej 
    652  1.7  thorpej 	bcmgpio_intr_disable(sc, eint);
    653  1.7  thorpej }
    654  1.7  thorpej 
    655  1.7  thorpej static bool
    656  1.7  thorpej bcmgpio_gpio_intrstr(void *vsc, int pin, int irqmode, char *buf, size_t buflen)
    657  1.7  thorpej {
    658  1.7  thorpej 
    659  1.7  thorpej 	if (pin < 0 || pin >= BCMGPIO_MAXPINS)
    660  1.7  thorpej 		return (false);
    661  1.7  thorpej 
    662  1.7  thorpej 	snprintf(buf, buflen, "GPIO %d", pin);
    663  1.7  thorpej 
    664  1.7  thorpej 	return (true);
    665  1.7  thorpej }
    666  1.7  thorpej 
    667  1.7  thorpej static bool
    668  1.7  thorpej bcmgpio_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    669  1.7  thorpej {
    670  1.7  thorpej 
    671  1.7  thorpej 	/* 1st cell is the bank */
    672  1.7  thorpej 	/* 2nd cell is the pin */
    673  1.7  thorpej 	/* 3rd cell is flags */
    674  1.7  thorpej 	if (!specifier)
    675  1.7  thorpej 		return (false);
    676  1.7  thorpej 	const u_int bank = be32toh(specifier[0]);
    677  1.7  thorpej 	const u_int pin = be32toh(specifier[1]);
    678  1.7  thorpej 
    679  1.7  thorpej 	if (bank >= BCMGPIO_NBANKS)
    680  1.7  thorpej 		return (false);
    681  1.7  thorpej 	if (pin >= 32)
    682  1.7  thorpej 		return (false);
    683  1.7  thorpej 
    684  1.7  thorpej 	snprintf(buf, buflen, "GPIO %u", (bank * 32) + pin);
    685  1.7  thorpej 
    686  1.7  thorpej 	return (true);
    687  1.1   kardel }
    688  1.1   kardel 
    689  1.1   kardel /* GPIO support functions */
    690  1.1   kardel static int
    691  1.1   kardel bcm2835gpio_gpio_pin_read(void *arg, int pin)
    692  1.1   kardel {
    693  1.1   kardel 	struct bcmgpio_softc *sc = arg;
    694  1.1   kardel 	uint32_t val;
    695  1.1   kardel 	int res;
    696  1.1   kardel 
    697  1.1   kardel 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    698  1.6    skrll 		BCM2835_GPIO_GPLEV(pin / BCM2835_GPIO_GPLEV_PINS_PER_REGISTER));
    699  1.1   kardel 
    700  1.6    skrll 	res = val & (1 << (pin % BCM2835_GPIO_GPLEV_PINS_PER_REGISTER)) ?
    701  1.1   kardel 		GPIO_PIN_HIGH : GPIO_PIN_LOW;
    702  1.1   kardel 
    703  1.6    skrll 	DPRINTF(2, ("%s: gpio_read pin %d->%d\n", device_xname(sc->sc_dev),
    704  1.6    skrll 	    pin, (res == GPIO_PIN_HIGH)));
    705  1.5    skrll 
    706  1.1   kardel 	return res;
    707  1.1   kardel }
    708  1.1   kardel 
    709  1.1   kardel static void
    710  1.1   kardel bcm2835gpio_gpio_pin_write(void *arg, int pin, int value)
    711  1.1   kardel {
    712  1.1   kardel 	struct bcmgpio_softc *sc = arg;
    713  1.1   kardel 	bus_size_t reg;
    714  1.5    skrll 
    715  1.6    skrll 	if (value == GPIO_PIN_HIGH) {
    716  1.6    skrll 		reg = BCM2835_GPIO_GPSET(pin / BCM2835_GPIO_GPSET_PINS_PER_REGISTER);
    717  1.6    skrll 	} else {
    718  1.6    skrll 		reg = BCM2835_GPIO_GPCLR(pin / BCM2835_GPIO_GPCLR_PINS_PER_REGISTER);
    719  1.6    skrll 	}
    720  1.6    skrll 
    721  1.6    skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg,
    722  1.6    skrll 	    1 << (pin % BCM2835_GPIO_GPSET_PINS_PER_REGISTER));
    723  1.6    skrll 
    724  1.6    skrll 	DPRINTF(2, ("%s: gpio_write pin %d<-%d\n", device_xname(sc->sc_dev),
    725  1.6    skrll 	    pin, (value == GPIO_PIN_HIGH)));
    726  1.6    skrll }
    727  1.6    skrll 
    728  1.6    skrll 
    729  1.6    skrll void
    730  1.6    skrll bcm283x_pin_setfunc(const struct bcmgpio_softc * const sc, u_int pin,
    731  1.6    skrll     u_int func)
    732  1.6    skrll {
    733  1.6    skrll 	const u_int mask = (1 << BCM2835_GPIO_GPFSEL_BITS_PER_PIN) - 1;
    734  1.6    skrll 	const u_int regid = (pin / BCM2835_GPIO_GPFSEL_PINS_PER_REGISTER);
    735  1.6    skrll 	const u_int shift = (pin % BCM2835_GPIO_GPFSEL_PINS_PER_REGISTER) *
    736  1.6    skrll 	    BCM2835_GPIO_GPFSEL_BITS_PER_PIN;
    737  1.6    skrll 	uint32_t v;
    738  1.6    skrll 
    739  1.6    skrll 	KASSERT(mutex_owned(&sc->sc_lock));
    740  1.6    skrll 	KASSERT(func <= mask);
    741  1.6    skrll 
    742  1.6    skrll 	v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPFSEL(regid));
    743  1.6    skrll 
    744  1.6    skrll 	if (((v >> shift) & mask) == func) {
    745  1.1   kardel 		return;
    746  1.1   kardel 	}
    747  1.5    skrll 
    748  1.6    skrll 	DPRINTF(2, ("%s: gpio_write pin %d<-%d\n", device_xname(sc->sc_dev),
    749  1.6    skrll 	    pin, func));
    750  1.6    skrll 
    751  1.6    skrll 	v &= ~(mask << shift);
    752  1.6    skrll 	v |=  (func << shift);
    753  1.6    skrll 
    754  1.6    skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPFSEL(regid), v);
    755  1.6    skrll }
    756  1.6    skrll 
    757  1.6    skrll u_int
    758  1.6    skrll bcm283x_pin_getfunc(const struct bcmgpio_softc * const sc, u_int pin)
    759  1.6    skrll {
    760  1.6    skrll 	const u_int mask = (1 << BCM2835_GPIO_GPFSEL_BITS_PER_PIN) - 1;
    761  1.6    skrll 	const u_int regid = (pin / BCM2835_GPIO_GPFSEL_PINS_PER_REGISTER);
    762  1.6    skrll 	const u_int shift = (pin % BCM2835_GPIO_GPFSEL_PINS_PER_REGISTER) *
    763  1.6    skrll 	    BCM2835_GPIO_GPFSEL_BITS_PER_PIN;
    764  1.6    skrll 	uint32_t v;
    765  1.6    skrll 
    766  1.6    skrll 	v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPFSEL(regid));
    767  1.6    skrll 
    768  1.6    skrll 	return ((v >> shift) & mask);
    769  1.6    skrll }
    770  1.6    skrll 
    771  1.6    skrll void
    772  1.6    skrll bcm283x_pin_setpull(const struct bcmgpio_softc * const sc, u_int pin, u_int pud)
    773  1.6    skrll {
    774  1.6    skrll 
    775  1.6    skrll 	KASSERT(mutex_owned(&sc->sc_lock));
    776  1.6    skrll 
    777  1.6    skrll 	const u_int mask = 1 << (pin % BCM2835_GPIO_GPPUD_PINS_PER_REGISTER);
    778  1.6    skrll 	const u_int regid = (pin / BCM2835_GPIO_GPPUD_PINS_PER_REGISTER);
    779  1.5    skrll 
    780  1.6    skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUD, pud);
    781  1.6    skrll 	delay(1);
    782  1.6    skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUDCLK(regid), mask);
    783  1.6    skrll 	delay(1);
    784  1.6    skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUD, 0);
    785  1.6    skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUDCLK(regid), 0);
    786  1.1   kardel }
    787  1.1   kardel 
    788  1.6    skrll 
    789  1.1   kardel static void
    790  1.1   kardel bcm2835gpio_gpio_pin_ctl(void *arg, int pin, int flags)
    791  1.1   kardel {
    792  1.1   kardel 	struct bcmgpio_softc *sc = arg;
    793  1.1   kardel 	uint32_t cmd;
    794  1.5    skrll 
    795  1.6    skrll 	DPRINTF(2, ("%s: gpio_ctl pin %d flags 0x%x\n", device_xname(sc->sc_dev), pin, flags));
    796  1.1   kardel 
    797  1.6    skrll 	mutex_enter(&sc->sc_lock);
    798  1.1   kardel 	if (flags & (GPIO_PIN_OUTPUT|GPIO_PIN_INPUT)) {
    799  1.1   kardel 		if ((flags & GPIO_PIN_INPUT) || !(flags & GPIO_PIN_OUTPUT)) {
    800  1.1   kardel 			/* for safety INPUT will overide output */
    801  1.6    skrll 			bcm283x_pin_setfunc(sc, pin, BCM2835_GPIO_IN);
    802  1.4    skrll 		} else {
    803  1.6    skrll 			bcm283x_pin_setfunc(sc, pin, BCM2835_GPIO_OUT);
    804  1.1   kardel 		}
    805  1.1   kardel 	}
    806  1.1   kardel 
    807  1.1   kardel 	if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) {
    808  1.1   kardel 		cmd = (flags & GPIO_PIN_PULLUP) ?
    809  1.1   kardel 			BCM2835_GPIO_GPPUD_PULLUP : BCM2835_GPIO_GPPUD_PULLDOWN;
    810  1.1   kardel 	} else {
    811  1.1   kardel 		cmd = BCM2835_GPIO_GPPUD_PULLOFF;
    812  1.1   kardel 	}
    813  1.1   kardel 
    814  1.1   kardel 	/* set up control signal */
    815  1.6    skrll 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUD, cmd);
    816  1.1   kardel 	delay(1); /* wait 150 cycles */
    817  1.1   kardel 	/* set clock signal */
    818  1.1   kardel 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    819  1.6    skrll 	    BCM2835_GPIO_GPPUDCLK(pin / BCM2835_GPIO_GPLEV_PINS_PER_REGISTER),
    820  1.6    skrll 	    1 << (pin % BCM2835_GPIO_GPPUD_PINS_PER_REGISTER));
    821  1.1   kardel 	delay(1); /* wait 150 cycles */
    822  1.1   kardel 	/* reset control signal and clock */
    823  1.1   kardel 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    824  1.6    skrll 	    BCM2835_GPIO_GPPUD, BCM2835_GPIO_GPPUD_PULLOFF);
    825  1.1   kardel 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    826  1.6    skrll 	    BCM2835_GPIO_GPPUDCLK(pin / BCM2835_GPIO_GPLEV_PINS_PER_REGISTER),
    827  1.6    skrll 	    0);
    828  1.6    skrll 	mutex_exit(&sc->sc_lock);
    829  1.6    skrll }
    830  1.6    skrll 
    831  1.6    skrll static void *
    832  1.6    skrll bcmgpio_fdt_acquire(device_t dev, const void *data, size_t len, int flags)
    833  1.6    skrll {
    834  1.6    skrll 	struct bcmgpio_softc *sc = device_private(dev);
    835  1.6    skrll 	struct bcmgpio_pin *gpin;
    836  1.6    skrll 	const u_int *gpio = data;
    837  1.6    skrll 
    838  1.6    skrll 	if (len != 12)
    839  1.6    skrll 		return NULL;
    840  1.6    skrll 
    841  1.6    skrll 	const u_int pin = be32toh(gpio[1]);
    842  1.6    skrll 	const bool actlo = be32toh(gpio[2]) & 1;
    843  1.6    skrll 
    844  1.6    skrll 	if (pin >= BCMGPIO_MAXPINS)
    845  1.6    skrll 		return NULL;
    846  1.6    skrll 
    847  1.6    skrll 	gpin = kmem_alloc(sizeof(*gpin), KM_SLEEP);
    848  1.6    skrll 	gpin->pin_no = pin;
    849  1.6    skrll 	gpin->pin_flags = flags;
    850  1.6    skrll 	gpin->pin_actlo = actlo;
    851  1.6    skrll 
    852  1.6    skrll 	bcm2835gpio_gpio_pin_ctl(sc, gpin->pin_no, gpin->pin_flags);
    853  1.6    skrll 
    854  1.6    skrll 	return gpin;
    855  1.6    skrll }
    856  1.6    skrll 
    857  1.6    skrll static void
    858  1.6    skrll bcmgpio_fdt_release(device_t dev, void *priv)
    859  1.6    skrll {
    860  1.6    skrll 	struct bcmgpio_softc *sc = device_private(dev);
    861  1.6    skrll 	struct bcmgpio_pin *gpin = priv;
    862  1.6    skrll 
    863  1.6    skrll 	bcm2835gpio_gpio_pin_ctl(sc, gpin->pin_no, GPIO_PIN_INPUT);
    864  1.6    skrll 	kmem_free(gpin, sizeof(*gpin));
    865  1.6    skrll }
    866  1.6    skrll 
    867  1.6    skrll static int
    868  1.6    skrll bcmgpio_fdt_read(device_t dev, void *priv, bool raw)
    869  1.6    skrll {
    870  1.6    skrll 	struct bcmgpio_softc *sc = device_private(dev);
    871  1.6    skrll 	struct bcmgpio_pin *gpin = priv;
    872  1.6    skrll 	int val;
    873  1.6    skrll 
    874  1.6    skrll 	val = bcm2835gpio_gpio_pin_read(sc, gpin->pin_no);
    875  1.6    skrll 
    876  1.6    skrll 	if (!raw && gpin->pin_actlo)
    877  1.6    skrll 		val = !val;
    878  1.6    skrll 
    879  1.6    skrll 	return val;
    880  1.6    skrll }
    881  1.6    skrll 
    882  1.6    skrll static void
    883  1.6    skrll bcmgpio_fdt_write(device_t dev, void *priv, int val, bool raw)
    884  1.6    skrll {
    885  1.6    skrll 	struct bcmgpio_softc *sc = device_private(dev);
    886  1.6    skrll 	struct bcmgpio_pin *gpin = priv;
    887  1.6    skrll 
    888  1.6    skrll 	if (!raw && gpin->pin_actlo)
    889  1.6    skrll 		val = !val;
    890  1.6    skrll 
    891  1.6    skrll 	bcm2835gpio_gpio_pin_write(sc, gpin->pin_no, val);
    892  1.1   kardel }
    893