Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_gpio.c revision 1.6
      1 /*	$NetBSD: bcm2835_gpio.c,v 1.6 2017/12/10 21:38:26 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013, 2014, 2017 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jonathan A. Kollasch, Frank Kardel and Nick Hudson
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: bcm2835_gpio.c,v 1.6 2017/12/10 21:38:26 skrll Exp $");
     34 
     35 /*
     36  * Driver for BCM2835 GPIO
     37  *
     38  * see: http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/device.h>
     43 #include <sys/systm.h>
     44 #include <sys/mutex.h>
     45 #include <sys/bus.h>
     46 #include <sys/intr.h>
     47 #include <sys/kernel.h>
     48 #include <sys/kmem.h>
     49 #include <sys/gpio.h>
     50 
     51 #include <sys/bitops.h>
     52 
     53 #include <arm/broadcom/bcm2835reg.h>
     54 #include <arm/broadcom/bcm2835_gpioreg.h>
     55 
     56 #include <dev/gpio/gpiovar.h>
     57 #include <dev/fdt/fdtvar.h>
     58 
     59 /* #define BCM2835_GPIO_DEBUG */
     60 #ifdef BCM2835_GPIO_DEBUG
     61 int bcm2835gpiodebug = 3;
     62 #define DPRINTF(l, x)	do { if (l <= bcm2835gpiodebug) { printf x; } } while (0)
     63 #else
     64 #define DPRINTF(l, x)
     65 #endif
     66 
     67 #define	BCMGPIO_MAXPINS	54
     68 
     69 struct bcmgpio_softc {
     70 	device_t		sc_dev;
     71 	bus_space_tag_t		sc_iot;
     72 	bus_space_handle_t	sc_ioh;
     73 	struct gpio_chipset_tag	sc_gpio_gc;
     74 
     75 	kmutex_t		sc_lock;
     76 	gpio_pin_t		sc_gpio_pins[BCMGPIO_MAXPINS];
     77 };
     78 
     79 struct bcmgpio_pin {
     80 	int			pin_no;
     81 	u_int			pin_flags;
     82 	bool			pin_actlo;
     83 };
     84 
     85 
     86 static int	bcmgpio_match(device_t, cfdata_t, void *);
     87 static void	bcmgpio_attach(device_t, device_t, void *);
     88 
     89 static int	bcm2835gpio_gpio_pin_read(void *, int);
     90 static void	bcm2835gpio_gpio_pin_write(void *, int, int);
     91 static void	bcm2835gpio_gpio_pin_ctl(void *, int, int);
     92 
     93 u_int		bcm283x_pin_getfunc(const struct bcmgpio_softc * const, u_int);
     94 void		bcm283x_pin_setfunc(const struct bcmgpio_softc * const, u_int,
     95 		    u_int);
     96 void		bcm283x_pin_setpull(const struct bcmgpio_softc * const, u_int,
     97 		    u_int);
     98 
     99 static int 	bcm283x_pinctrl_set_config(device_t, const void *, size_t);
    100 
    101 static void *	bcmgpio_fdt_acquire(device_t, const void *, size_t, int);
    102 static void	bcmgpio_fdt_release(device_t, void *);
    103 static int	bcmgpio_fdt_read(device_t, void *, bool);
    104 static void	bcmgpio_fdt_write(device_t, void *, int, bool);
    105 
    106 static struct fdtbus_gpio_controller_func bcmgpio_funcs = {
    107 	.acquire = bcmgpio_fdt_acquire,
    108 	.release = bcmgpio_fdt_release,
    109 	.read = bcmgpio_fdt_read,
    110 	.write = bcmgpio_fdt_write
    111 };
    112 
    113 CFATTACH_DECL_NEW(bcmgpio, sizeof(struct bcmgpio_softc),
    114     bcmgpio_match, bcmgpio_attach, NULL, NULL);
    115 
    116 
    117 static struct fdtbus_pinctrl_controller_func bcm283x_pinctrl_funcs = {
    118 	.set_config = bcm283x_pinctrl_set_config,
    119 };
    120 
    121 static int
    122 bcm283x_pinctrl_set_config(device_t dev, const void *data, size_t len)
    123 {
    124 	struct bcmgpio_softc * const sc = device_private(dev);
    125 
    126 	if (len != 4)
    127 		return -1;
    128 
    129 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
    130 
    131 	/*
    132 	 * Required: brcm,pins
    133 	 * Optional: brcm,function, brcm,pull
    134 	 */
    135 
    136 	int pins_len;
    137 	const u_int *pins = fdtbus_get_prop(phandle, "brcm,pins", &pins_len);
    138 
    139 	if (pins == NULL)
    140 		return -1;
    141 
    142 	int pull_len = 0;
    143 	const u_int *pull = fdtbus_get_prop(phandle, "brcm,pull", &pull_len);
    144 
    145 	int func_len = 0;
    146 	const u_int *func = fdtbus_get_prop(phandle, "brcm,function", &func_len);
    147 
    148 	if (!pull && !func) {
    149 		aprint_error_dev(dev, "one of brcm,pull or brcm,funcion must "
    150 		    "be specified");
    151 		return -1;
    152 	}
    153 
    154 	const int npins = pins_len / 4;
    155 	const int npull = pull_len / 4;
    156 	const int nfunc = func_len / 4;
    157 
    158 	if (npull > 1 && npull != npins) {
    159 		aprint_error_dev(dev, "brcm,pull must have 1 or %d entries",
    160 		    npins);
    161 		return -1;
    162 	}
    163 	if (nfunc > 1 && nfunc != npins) {
    164 		aprint_error_dev(dev, "brcm,function must have 1 or %d entries",
    165 		    npins);
    166 		return -1;
    167 	}
    168 
    169 	mutex_enter(&sc->sc_lock);
    170 
    171 	for (int i = 0; i < npins; i++) {
    172 		const u_int pin = be32toh(pins[i]);
    173 
    174 		if (pin > BCMGPIO_MAXPINS)
    175 			continue;
    176 		if (pull) {
    177 			const int value = be32toh(pull[npull == 1 ? 0 : i]);
    178 			bcm283x_pin_setpull(sc, pin, value);
    179 		}
    180 		if (func) {
    181 			const int value = be32toh(func[nfunc == 1 ? 0 : i]);
    182 			bcm283x_pin_setfunc(sc, pin, value);
    183 		}
    184 	}
    185 
    186 	mutex_exit(&sc->sc_lock);
    187 
    188 	return 0;
    189 }
    190 
    191 static int
    192 bcmgpio_match(device_t parent, cfdata_t cf, void *aux)
    193 {
    194 	const char * const compatible[] = { "brcm,bcm2835-gpio", NULL };
    195 	struct fdt_attach_args * const faa = aux;
    196 
    197 	return of_match_compatible(faa->faa_phandle, compatible);
    198 }
    199 
    200 static void
    201 bcmgpio_attach(device_t parent, device_t self, void *aux)
    202 {
    203 	struct bcmgpio_softc * const sc = device_private(self);
    204 	struct fdt_attach_args * const faa = aux;
    205 	struct gpiobus_attach_args gba;
    206 	bus_addr_t addr;
    207 	bus_size_t size;
    208 	u_int func;
    209 	int error;
    210 	int pin;
    211 
    212 	const int phandle = faa->faa_phandle;
    213 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    214 		aprint_error(": couldn't get registers\n");
    215 		return;
    216 	}
    217 
    218 	sc->sc_dev = self;
    219 
    220 	aprint_naive("\n");
    221 	aprint_normal(": GPIO controller\n");
    222 
    223 	sc->sc_iot = faa->faa_bst;
    224 	error = bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh);
    225 	if (error) {
    226 		aprint_error_dev(self, "couldn't map registers\n");
    227 		return;
    228 	}
    229 
    230 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    231 
    232 	for (pin = 0; pin < BCMGPIO_MAXPINS; pin++) {
    233 		sc->sc_gpio_pins[pin].pin_num = pin;
    234 		/*
    235 		 * find out pins still available for GPIO
    236 		 */
    237 		func = bcm283x_pin_getfunc(sc, pin);
    238 
    239 		if (func == BCM2835_GPIO_IN ||
    240 		    func == BCM2835_GPIO_OUT) {
    241 			sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
    242 				GPIO_PIN_OUTPUT |
    243 				GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
    244 				GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    245 			/* read initial state */
    246 			sc->sc_gpio_pins[pin].pin_state =
    247 				bcm2835gpio_gpio_pin_read(sc, pin);
    248 			DPRINTF(1, ("%s: attach pin %d\n", device_xname(sc->sc_dev), pin));
    249 		} else {
    250 			sc->sc_gpio_pins[pin].pin_caps = 0;
    251 			sc->sc_gpio_pins[pin].pin_state = 0;
    252   			DPRINTF(1, ("%s: skip pin %d - func = 0x%x\n", device_xname(sc->sc_dev), pin, func));
    253 		}
    254 	}
    255 
    256 	/* create controller tag */
    257 	sc->sc_gpio_gc.gp_cookie = sc;
    258 	sc->sc_gpio_gc.gp_pin_read = bcm2835gpio_gpio_pin_read;
    259 	sc->sc_gpio_gc.gp_pin_write = bcm2835gpio_gpio_pin_write;
    260 	sc->sc_gpio_gc.gp_pin_ctl = bcm2835gpio_gpio_pin_ctl;
    261 
    262 	gba.gba_gc = &sc->sc_gpio_gc;
    263 	for (pin = 0; pin < BCMGPIO_MAXPINS;) {
    264 		const int npins = MIN(BCMGPIO_MAXPINS - pin, 32);
    265 		gba.gba_pins = &sc->sc_gpio_pins[pin];
    266 		gba.gba_npins = npins;
    267 		config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    268 		pin += npins;
    269 	}
    270 
    271 	fdtbus_register_gpio_controller(self, faa->faa_phandle, &bcmgpio_funcs);
    272 
    273 	for (int child = OF_child(phandle); child; child = OF_peer(child)) {
    274 		if (!of_hasprop(child, "brcm,pins"))
    275 			continue;
    276 		fdtbus_register_pinctrl_config(self, child,
    277 		    &bcm283x_pinctrl_funcs);
    278 	}
    279 
    280 	fdtbus_pinctrl_configure();
    281 }
    282 
    283 /* GPIO support functions */
    284 static int
    285 bcm2835gpio_gpio_pin_read(void *arg, int pin)
    286 {
    287 	struct bcmgpio_softc *sc = arg;
    288 	uint32_t val;
    289 	int res;
    290 
    291 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    292 		BCM2835_GPIO_GPLEV(pin / BCM2835_GPIO_GPLEV_PINS_PER_REGISTER));
    293 
    294 	res = val & (1 << (pin % BCM2835_GPIO_GPLEV_PINS_PER_REGISTER)) ?
    295 		GPIO_PIN_HIGH : GPIO_PIN_LOW;
    296 
    297 	DPRINTF(2, ("%s: gpio_read pin %d->%d\n", device_xname(sc->sc_dev),
    298 	    pin, (res == GPIO_PIN_HIGH)));
    299 
    300 	return res;
    301 }
    302 
    303 static void
    304 bcm2835gpio_gpio_pin_write(void *arg, int pin, int value)
    305 {
    306 	struct bcmgpio_softc *sc = arg;
    307 	bus_size_t reg;
    308 
    309 	if (value == GPIO_PIN_HIGH) {
    310 		reg = BCM2835_GPIO_GPSET(pin / BCM2835_GPIO_GPSET_PINS_PER_REGISTER);
    311 	} else {
    312 		reg = BCM2835_GPIO_GPCLR(pin / BCM2835_GPIO_GPCLR_PINS_PER_REGISTER);
    313 	}
    314 
    315 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg,
    316 	    1 << (pin % BCM2835_GPIO_GPSET_PINS_PER_REGISTER));
    317 
    318 	DPRINTF(2, ("%s: gpio_write pin %d<-%d\n", device_xname(sc->sc_dev),
    319 	    pin, (value == GPIO_PIN_HIGH)));
    320 }
    321 
    322 
    323 void
    324 bcm283x_pin_setfunc(const struct bcmgpio_softc * const sc, u_int pin,
    325     u_int func)
    326 {
    327 	const u_int mask = (1 << BCM2835_GPIO_GPFSEL_BITS_PER_PIN) - 1;
    328 	const u_int regid = (pin / BCM2835_GPIO_GPFSEL_PINS_PER_REGISTER);
    329 	const u_int shift = (pin % BCM2835_GPIO_GPFSEL_PINS_PER_REGISTER) *
    330 	    BCM2835_GPIO_GPFSEL_BITS_PER_PIN;
    331 	uint32_t v;
    332 
    333 	KASSERT(mutex_owned(&sc->sc_lock));
    334 	KASSERT(func <= mask);
    335 
    336 	v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPFSEL(regid));
    337 
    338 	if (((v >> shift) & mask) == func) {
    339 		return;
    340 	}
    341 
    342 	DPRINTF(2, ("%s: gpio_write pin %d<-%d\n", device_xname(sc->sc_dev),
    343 	    pin, func));
    344 
    345 	v &= ~(mask << shift);
    346 	v |=  (func << shift);
    347 
    348 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPFSEL(regid), v);
    349 }
    350 
    351 u_int
    352 bcm283x_pin_getfunc(const struct bcmgpio_softc * const sc, u_int pin)
    353 {
    354 	const u_int mask = (1 << BCM2835_GPIO_GPFSEL_BITS_PER_PIN) - 1;
    355 	const u_int regid = (pin / BCM2835_GPIO_GPFSEL_PINS_PER_REGISTER);
    356 	const u_int shift = (pin % BCM2835_GPIO_GPFSEL_PINS_PER_REGISTER) *
    357 	    BCM2835_GPIO_GPFSEL_BITS_PER_PIN;
    358 	uint32_t v;
    359 
    360 	v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPFSEL(regid));
    361 
    362 	return ((v >> shift) & mask);
    363 }
    364 
    365 void
    366 bcm283x_pin_setpull(const struct bcmgpio_softc * const sc, u_int pin, u_int pud)
    367 {
    368 
    369 	KASSERT(mutex_owned(&sc->sc_lock));
    370 
    371 	const u_int mask = 1 << (pin % BCM2835_GPIO_GPPUD_PINS_PER_REGISTER);
    372 	const u_int regid = (pin / BCM2835_GPIO_GPPUD_PINS_PER_REGISTER);
    373 
    374 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUD, pud);
    375 	delay(1);
    376 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUDCLK(regid), mask);
    377 	delay(1);
    378 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUD, 0);
    379 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUDCLK(regid), 0);
    380 }
    381 
    382 
    383 static void
    384 bcm2835gpio_gpio_pin_ctl(void *arg, int pin, int flags)
    385 {
    386 	struct bcmgpio_softc *sc = arg;
    387 	uint32_t cmd;
    388 
    389 	DPRINTF(2, ("%s: gpio_ctl pin %d flags 0x%x\n", device_xname(sc->sc_dev), pin, flags));
    390 
    391 	mutex_enter(&sc->sc_lock);
    392 	if (flags & (GPIO_PIN_OUTPUT|GPIO_PIN_INPUT)) {
    393 		if ((flags & GPIO_PIN_INPUT) || !(flags & GPIO_PIN_OUTPUT)) {
    394 			/* for safety INPUT will overide output */
    395 			bcm283x_pin_setfunc(sc, pin, BCM2835_GPIO_IN);
    396 		} else {
    397 			bcm283x_pin_setfunc(sc, pin, BCM2835_GPIO_OUT);
    398 		}
    399 	}
    400 
    401 	if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) {
    402 		cmd = (flags & GPIO_PIN_PULLUP) ?
    403 			BCM2835_GPIO_GPPUD_PULLUP : BCM2835_GPIO_GPPUD_PULLDOWN;
    404 	} else {
    405 		cmd = BCM2835_GPIO_GPPUD_PULLOFF;
    406 	}
    407 
    408 	/* set up control signal */
    409 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_GPIO_GPPUD, cmd);
    410 	delay(1); /* wait 150 cycles */
    411 	/* set clock signal */
    412 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    413 	    BCM2835_GPIO_GPPUDCLK(pin / BCM2835_GPIO_GPLEV_PINS_PER_REGISTER),
    414 	    1 << (pin % BCM2835_GPIO_GPPUD_PINS_PER_REGISTER));
    415 	delay(1); /* wait 150 cycles */
    416 	/* reset control signal and clock */
    417 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    418 	    BCM2835_GPIO_GPPUD, BCM2835_GPIO_GPPUD_PULLOFF);
    419 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    420 	    BCM2835_GPIO_GPPUDCLK(pin / BCM2835_GPIO_GPLEV_PINS_PER_REGISTER),
    421 	    0);
    422 	mutex_exit(&sc->sc_lock);
    423 }
    424 
    425 static void *
    426 bcmgpio_fdt_acquire(device_t dev, const void *data, size_t len, int flags)
    427 {
    428 	struct bcmgpio_softc *sc = device_private(dev);
    429 	struct bcmgpio_pin *gpin;
    430 	const u_int *gpio = data;
    431 
    432 	if (len != 12)
    433 		return NULL;
    434 
    435 	const u_int pin = be32toh(gpio[1]);
    436 	const bool actlo = be32toh(gpio[2]) & 1;
    437 
    438 	if (pin >= BCMGPIO_MAXPINS)
    439 		return NULL;
    440 
    441 	gpin = kmem_alloc(sizeof(*gpin), KM_SLEEP);
    442 	gpin->pin_no = pin;
    443 	gpin->pin_flags = flags;
    444 	gpin->pin_actlo = actlo;
    445 
    446 	bcm2835gpio_gpio_pin_ctl(sc, gpin->pin_no, gpin->pin_flags);
    447 
    448 	return gpin;
    449 }
    450 
    451 static void
    452 bcmgpio_fdt_release(device_t dev, void *priv)
    453 {
    454 	struct bcmgpio_softc *sc = device_private(dev);
    455 	struct bcmgpio_pin *gpin = priv;
    456 
    457 	bcm2835gpio_gpio_pin_ctl(sc, gpin->pin_no, GPIO_PIN_INPUT);
    458 	kmem_free(gpin, sizeof(*gpin));
    459 }
    460 
    461 static int
    462 bcmgpio_fdt_read(device_t dev, void *priv, bool raw)
    463 {
    464 	struct bcmgpio_softc *sc = device_private(dev);
    465 	struct bcmgpio_pin *gpin = priv;
    466 	int val;
    467 
    468 	val = bcm2835gpio_gpio_pin_read(sc, gpin->pin_no);
    469 
    470 	if (!raw && gpin->pin_actlo)
    471 		val = !val;
    472 
    473 	return val;
    474 }
    475 
    476 static void
    477 bcmgpio_fdt_write(device_t dev, void *priv, int val, bool raw)
    478 {
    479 	struct bcmgpio_softc *sc = device_private(dev);
    480 	struct bcmgpio_pin *gpin = priv;
    481 
    482 	if (!raw && gpin->pin_actlo)
    483 		val = !val;
    484 
    485 	bcm2835gpio_gpio_pin_write(sc, gpin->pin_no, val);
    486 }
    487