Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_gpio.c revision 1.2.6.1
      1  1.2.6.1     snj /*	$NetBSD: bcm2835_gpio.c,v 1.2.6.1 2016/02/26 22:52:53 snj Exp $	*/
      2      1.1  kardel 
      3      1.1  kardel /*-
      4      1.1  kardel  * Copyright (c) 2014 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.1  kardel  * by Frank Kardel.
      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.2.6.1     snj __KERNEL_RCSID(0, "$NetBSD: bcm2835_gpio.c,v 1.2.6.1 2016/02/26 22:52:53 snj 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 "gpio.h"
     42      1.1  kardel 
     43      1.1  kardel #include <sys/param.h>
     44      1.1  kardel #include <sys/device.h>
     45      1.1  kardel #include <sys/systm.h>
     46      1.1  kardel #include <sys/mutex.h>
     47      1.1  kardel #include <sys/bus.h>
     48      1.1  kardel #include <sys/intr.h>
     49      1.1  kardel #include <sys/kernel.h>
     50      1.1  kardel #include <sys/gpio.h>
     51      1.1  kardel #include <dev/gpio/gpiovar.h>
     52      1.1  kardel 
     53      1.1  kardel #include <sys/bitops.h>
     54      1.1  kardel 
     55      1.1  kardel #include <arm/broadcom/bcm_amba.h>
     56      1.1  kardel #include <arm/broadcom/bcm2835reg.h>
     57      1.1  kardel #include <arm/broadcom/bcm2835_gpioreg.h>
     58      1.1  kardel #include <arm/broadcom/bcm2835_gpio_subr.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.1  kardel struct bcmgpio_softc {
     69      1.1  kardel 	device_t		sc_dev;
     70      1.1  kardel 	bus_space_tag_t		sc_iot;
     71      1.1  kardel 	bus_space_handle_t	sc_ioh;
     72      1.1  kardel 	struct gpio_chipset_tag	sc_gpio_gc;
     73      1.1  kardel 	gpio_pin_t		sc_gpio_pins[32];
     74      1.1  kardel };
     75      1.1  kardel 
     76      1.1  kardel static int	bcmgpio_match(device_t, cfdata_t, void *);
     77      1.1  kardel static void	bcmgpio_attach(device_t, device_t, void *);
     78      1.1  kardel 
     79      1.1  kardel #if NGPIO > 0
     80      1.1  kardel static int      bcm2835gpio_gpio_pin_read(void *, int);
     81      1.1  kardel static void     bcm2835gpio_gpio_pin_write(void *, int, int);
     82      1.1  kardel static void     bcm2835gpio_gpio_pin_ctl(void *, int, int);
     83      1.1  kardel #endif
     84      1.1  kardel 
     85      1.1  kardel CFATTACH_DECL_NEW(bcmgpio, sizeof(struct bcmgpio_softc),
     86      1.1  kardel     bcmgpio_match, bcmgpio_attach, NULL, NULL);
     87      1.1  kardel 
     88      1.1  kardel static int
     89      1.1  kardel bcmgpio_match(device_t parent, cfdata_t cf, void *aux)
     90      1.1  kardel {
     91      1.1  kardel 	struct amba_attach_args * const aaa = aux;
     92      1.1  kardel 
     93      1.1  kardel 	if (strcmp(aaa->aaa_name, "bcmgpio") != 0)
     94      1.1  kardel 		return 0;
     95      1.1  kardel 
     96      1.1  kardel 	return 1;
     97      1.1  kardel }
     98      1.1  kardel 
     99      1.1  kardel static void
    100      1.1  kardel bcmgpio_attach(device_t parent, device_t self, void *aux)
    101      1.1  kardel {
    102      1.1  kardel 	struct bcmgpio_softc * const sc = device_private(self);
    103      1.1  kardel #if NGPIO > 0
    104  1.2.6.1     snj 	struct amba_attach_args *aaa = aux;
    105  1.2.6.1     snj 	struct gpiobus_attach_args gba;
    106      1.1  kardel 	int pin, minpin, maxpin;
    107      1.1  kardel 	u_int func;
    108  1.2.6.1     snj 	int error;
    109      1.1  kardel #endif
    110      1.1  kardel 
    111      1.1  kardel 	sc->sc_dev = self;
    112      1.1  kardel 
    113      1.2   skrll #if NGPIO > 0
    114      1.1  kardel 	if (device_unit(sc->sc_dev) > 1) {
    115      1.1  kardel 		aprint_naive(" NO GPIO\n");
    116      1.1  kardel 		aprint_normal(": NO GPIO\n");
    117      1.1  kardel 		return;
    118      1.1  kardel 	} else if (device_unit(sc->sc_dev) == 1) {
    119      1.1  kardel 		maxpin = 53;
    120      1.1  kardel 		minpin = 32;
    121      1.1  kardel 	} else {
    122      1.1  kardel 		maxpin = 31;
    123      1.1  kardel 		minpin = 0;
    124      1.1  kardel 	}
    125      1.1  kardel 
    126      1.1  kardel 	aprint_naive("\n");
    127      1.1  kardel 	aprint_normal(": GPIO [%d...%d]\n", minpin, maxpin);
    128      1.1  kardel 
    129  1.2.6.1     snj 	sc->sc_iot = aaa->aaa_iot;
    130  1.2.6.1     snj 	error = bus_space_map(sc->sc_iot, aaa->aaa_addr, aaa->aaa_size, 0,
    131  1.2.6.1     snj 	    &sc->sc_ioh);
    132  1.2.6.1     snj 	if (error) {
    133  1.2.6.1     snj 		aprint_error_dev(self,
    134  1.2.6.1     snj 		    "can't map registers for %s: %d\n", aaa->aaa_name, error);
    135  1.2.6.1     snj 		return;
    136  1.2.6.1     snj 	}
    137  1.2.6.1     snj 
    138      1.1  kardel 	for (pin = minpin; pin <= maxpin; pin++) {
    139      1.1  kardel 	        int epin = pin - minpin;
    140      1.1  kardel 
    141      1.1  kardel 	        sc->sc_gpio_pins[epin].pin_num = epin;
    142      1.1  kardel 		/*
    143      1.1  kardel 		 * find out pins still available for GPIO
    144      1.1  kardel 		 */
    145      1.1  kardel 		func = bcm2835gpio_function_read(pin);
    146      1.1  kardel 
    147      1.1  kardel 		if (func == BCM2835_GPIO_IN ||
    148      1.1  kardel 		    func == BCM2835_GPIO_OUT) {
    149      1.1  kardel 	                sc->sc_gpio_pins[epin].pin_caps = GPIO_PIN_INPUT |
    150      1.1  kardel 				GPIO_PIN_OUTPUT |
    151      1.1  kardel 				GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
    152      1.1  kardel 				GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    153      1.1  kardel 			/* read initial state */
    154      1.1  kardel 			sc->sc_gpio_pins[epin].pin_state =
    155      1.1  kardel 				bcm2835gpio_gpio_pin_read(sc, epin);
    156      1.1  kardel 			DPRINTF(1, ("%s: attach pin %d\n", device_xname(sc->sc_dev), pin));
    157      1.1  kardel                 } else {
    158      1.1  kardel 	                sc->sc_gpio_pins[epin].pin_caps = 0;
    159      1.1  kardel 			sc->sc_gpio_pins[epin].pin_state = 0;
    160      1.1  kardel   			DPRINTF(1, ("%s: skip pin %d - func = 0x%x\n", device_xname(sc->sc_dev), pin, func));
    161      1.1  kardel                 }
    162      1.1  kardel         }
    163      1.1  kardel 
    164      1.1  kardel 	/* create controller tag */
    165      1.1  kardel 	sc->sc_gpio_gc.gp_cookie = sc;
    166      1.1  kardel 	sc->sc_gpio_gc.gp_pin_read = bcm2835gpio_gpio_pin_read;
    167      1.1  kardel 	sc->sc_gpio_gc.gp_pin_write = bcm2835gpio_gpio_pin_write;
    168      1.1  kardel 	sc->sc_gpio_gc.gp_pin_ctl = bcm2835gpio_gpio_pin_ctl;
    169      1.1  kardel 
    170      1.1  kardel 	gba.gba_gc = &sc->sc_gpio_gc;
    171      1.1  kardel 	gba.gba_pins = sc->sc_gpio_pins;
    172      1.1  kardel 	gba.gba_npins = maxpin - minpin + 1;
    173      1.1  kardel 
    174      1.1  kardel 	config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    175      1.1  kardel #else
    176      1.1  kardel 	aprint_normal_dev(sc->sc_dev, "no GPIO configured in kernel");
    177      1.1  kardel #endif
    178      1.1  kardel }
    179      1.1  kardel 
    180      1.1  kardel #if NGPIO > 0
    181      1.1  kardel /* GPIO support functions */
    182      1.1  kardel static int
    183      1.1  kardel bcm2835gpio_gpio_pin_read(void *arg, int pin)
    184      1.1  kardel {
    185      1.1  kardel 	struct bcmgpio_softc *sc = arg;
    186      1.1  kardel 	int epin = pin + device_unit(sc->sc_dev) * 32;
    187      1.1  kardel 	uint32_t val;
    188      1.1  kardel 	int res;
    189      1.1  kardel 
    190      1.1  kardel 	if (device_unit(sc->sc_dev) > 1) {
    191      1.1  kardel 		return 0;
    192      1.1  kardel 	}
    193      1.1  kardel 
    194      1.1  kardel 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    195      1.1  kardel 		BCM2835_GPIO_GPLEV(epin / BCM2835_GPIO_GPLEV_PINS_PER_REGISTER));
    196      1.1  kardel 
    197      1.1  kardel 	res = val & (1 << (epin % BCM2835_GPIO_GPLEV_PINS_PER_REGISTER)) ?
    198      1.1  kardel 		GPIO_PIN_HIGH : GPIO_PIN_LOW;
    199      1.1  kardel 
    200      1.1  kardel 	DPRINTF(2, ("%s: gpio_read pin %d->%d\n", device_xname(sc->sc_dev), epin, (res == GPIO_PIN_HIGH)));
    201      1.1  kardel 
    202      1.1  kardel 	return res;
    203      1.1  kardel }
    204      1.1  kardel 
    205      1.1  kardel static void
    206      1.1  kardel bcm2835gpio_gpio_pin_write(void *arg, int pin, int value)
    207      1.1  kardel {
    208      1.1  kardel 	struct bcmgpio_softc *sc = arg;
    209      1.1  kardel 	int epin = pin + device_unit(sc->sc_dev) * 32;
    210      1.1  kardel 	bus_size_t reg;
    211      1.1  kardel 
    212      1.1  kardel 	if (device_unit(sc->sc_dev) > 1) {
    213      1.1  kardel 		return;
    214      1.1  kardel 	}
    215      1.1  kardel 
    216      1.1  kardel 	if (value == GPIO_PIN_HIGH) {
    217      1.1  kardel 		reg = BCM2835_GPIO_GPSET(epin / BCM2835_GPIO_GPSET_PINS_PER_REGISTER);
    218      1.1  kardel 	} else {
    219      1.1  kardel 		reg = BCM2835_GPIO_GPCLR(epin / BCM2835_GPIO_GPCLR_PINS_PER_REGISTER);
    220      1.1  kardel 	}
    221      1.1  kardel 
    222      1.1  kardel 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    223      1.1  kardel 		reg, 1 << (epin % BCM2835_GPIO_GPSET_PINS_PER_REGISTER));
    224      1.1  kardel 	DPRINTF(2, ("%s: gpio_write pin %d<-%d\n", device_xname(sc->sc_dev), epin, (value == GPIO_PIN_HIGH)));
    225      1.1  kardel }
    226      1.1  kardel 
    227      1.1  kardel static void
    228      1.1  kardel bcm2835gpio_gpio_pin_ctl(void *arg, int pin, int flags)
    229      1.1  kardel {
    230      1.1  kardel 	struct bcmgpio_softc *sc = arg;
    231      1.1  kardel 	uint32_t cmd;
    232      1.1  kardel 	int epin = pin + device_unit(sc->sc_dev) * 32;
    233      1.1  kardel 
    234      1.1  kardel 	if (device_unit(sc->sc_dev) > 1) {
    235      1.1  kardel 		return;
    236      1.1  kardel 	}
    237      1.1  kardel 
    238      1.1  kardel 	DPRINTF(2, ("%s: gpio_ctl pin %d flags 0x%x\n", device_xname(sc->sc_dev), epin, flags));
    239      1.1  kardel 
    240      1.1  kardel 	if (flags & (GPIO_PIN_OUTPUT|GPIO_PIN_INPUT)) {
    241      1.1  kardel 		if ((flags & GPIO_PIN_INPUT) || !(flags & GPIO_PIN_OUTPUT)) {
    242      1.1  kardel 			/* for safety INPUT will overide output */
    243      1.1  kardel 	                bcm2835gpio_function_select(epin, BCM2835_GPIO_IN);
    244      1.1  kardel                 } else {
    245      1.1  kardel 	                bcm2835gpio_function_select(epin, BCM2835_GPIO_OUT);
    246      1.1  kardel 		}
    247      1.1  kardel 	}
    248      1.1  kardel 
    249      1.1  kardel 	if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) {
    250      1.1  kardel 		cmd = (flags & GPIO_PIN_PULLUP) ?
    251      1.1  kardel 			BCM2835_GPIO_GPPUD_PULLUP : BCM2835_GPIO_GPPUD_PULLDOWN;
    252      1.1  kardel 	} else {
    253      1.1  kardel 		cmd = BCM2835_GPIO_GPPUD_PULLOFF;
    254      1.1  kardel 	}
    255      1.1  kardel 
    256      1.1  kardel 	/* set up control signal */
    257      1.1  kardel 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    258      1.1  kardel 		BCM2835_GPIO_GPPUD, cmd);
    259      1.1  kardel 	delay(1); /* wait 150 cycles */
    260      1.1  kardel 	/* set clock signal */
    261      1.1  kardel 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    262      1.1  kardel 		BCM2835_GPIO_GPPUDCLK(device_unit(sc->sc_dev)),
    263      1.1  kardel 		1 << (epin % BCM2835_GPIO_GPPUD_PINS_PER_REGISTER));
    264      1.1  kardel 	delay(1); /* wait 150 cycles */
    265      1.1  kardel 	/* reset control signal and clock */
    266      1.1  kardel 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    267      1.1  kardel 		BCM2835_GPIO_GPPUD, BCM2835_GPIO_GPPUD_PULLOFF);
    268      1.1  kardel 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    269      1.1  kardel 		BCM2835_GPIO_GPPUDCLK(device_unit(sc->sc_dev)),
    270      1.1  kardel 		0);
    271      1.1  kardel }
    272      1.1  kardel #endif /* NGPIO > 0 */
    273