Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_gpio.c revision 1.2.4.2
      1  1.2.4.2  tls /*	$NetBSD: bcm2835_gpio.c,v 1.2.4.2 2014/08/10 06:53:51 tls Exp $	*/
      2  1.2.4.2  tls 
      3  1.2.4.2  tls /*-
      4  1.2.4.2  tls  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.2.4.2  tls  * All rights reserved.
      6  1.2.4.2  tls  *
      7  1.2.4.2  tls  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.4.2  tls  * by Frank Kardel.
      9  1.2.4.2  tls  *
     10  1.2.4.2  tls  * Redistribution and use in source and binary forms, with or without
     11  1.2.4.2  tls  * modification, are permitted provided that the following conditions
     12  1.2.4.2  tls  * are met:
     13  1.2.4.2  tls  * 1. Redistributions of source code must retain the above copyright
     14  1.2.4.2  tls  *    notice, this list of conditions and the following disclaimer.
     15  1.2.4.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.4.2  tls  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.4.2  tls  *    documentation and/or other materials provided with the distribution.
     18  1.2.4.2  tls  *
     19  1.2.4.2  tls  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  1.2.4.2  tls  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.4.2  tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.4.2  tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     23  1.2.4.2  tls  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     24  1.2.4.2  tls  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     25  1.2.4.2  tls  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     26  1.2.4.2  tls  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27  1.2.4.2  tls  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     28  1.2.4.2  tls  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     29  1.2.4.2  tls  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.2.4.2  tls  */
     31  1.2.4.2  tls 
     32  1.2.4.2  tls #include <sys/cdefs.h>
     33  1.2.4.2  tls __KERNEL_RCSID(0, "$NetBSD: bcm2835_gpio.c,v 1.2.4.2 2014/08/10 06:53:51 tls Exp $");
     34  1.2.4.2  tls 
     35  1.2.4.2  tls /*
     36  1.2.4.2  tls  * Driver for BCM2835 GPIO
     37  1.2.4.2  tls  *
     38  1.2.4.2  tls  * see: http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
     39  1.2.4.2  tls  */
     40  1.2.4.2  tls 
     41  1.2.4.2  tls #include "gpio.h"
     42  1.2.4.2  tls 
     43  1.2.4.2  tls #include <sys/param.h>
     44  1.2.4.2  tls #include <sys/device.h>
     45  1.2.4.2  tls #include <sys/systm.h>
     46  1.2.4.2  tls #include <sys/mutex.h>
     47  1.2.4.2  tls #include <sys/bus.h>
     48  1.2.4.2  tls #include <sys/intr.h>
     49  1.2.4.2  tls #include <sys/kernel.h>
     50  1.2.4.2  tls #include <sys/gpio.h>
     51  1.2.4.2  tls #include <dev/gpio/gpiovar.h>
     52  1.2.4.2  tls 
     53  1.2.4.2  tls #include <sys/bitops.h>
     54  1.2.4.2  tls 
     55  1.2.4.2  tls #include <arm/broadcom/bcm_amba.h>
     56  1.2.4.2  tls #include <arm/broadcom/bcm2835reg.h>
     57  1.2.4.2  tls #include <arm/broadcom/bcm2835_gpioreg.h>
     58  1.2.4.2  tls #include <arm/broadcom/bcm2835_gpio_subr.h>
     59  1.2.4.2  tls 
     60  1.2.4.2  tls /* #define BCM2835_GPIO_DEBUG */
     61  1.2.4.2  tls #ifdef BCM2835_GPIO_DEBUG
     62  1.2.4.2  tls int bcm2835gpiodebug = 3;
     63  1.2.4.2  tls #define DPRINTF(l, x)	do { if (l <= bcm2835gpiodebug) { printf x; } } while (0)
     64  1.2.4.2  tls #else
     65  1.2.4.2  tls #define DPRINTF(l, x)
     66  1.2.4.2  tls #endif
     67  1.2.4.2  tls 
     68  1.2.4.2  tls struct bcmgpio_softc {
     69  1.2.4.2  tls 	device_t		sc_dev;
     70  1.2.4.2  tls 	bus_space_tag_t		sc_iot;
     71  1.2.4.2  tls 	bus_space_handle_t	sc_ioh;
     72  1.2.4.2  tls 	struct gpio_chipset_tag	sc_gpio_gc;
     73  1.2.4.2  tls 	gpio_pin_t		sc_gpio_pins[32];
     74  1.2.4.2  tls };
     75  1.2.4.2  tls 
     76  1.2.4.2  tls static int	bcmgpio_match(device_t, cfdata_t, void *);
     77  1.2.4.2  tls static void	bcmgpio_attach(device_t, device_t, void *);
     78  1.2.4.2  tls 
     79  1.2.4.2  tls #if NGPIO > 0
     80  1.2.4.2  tls static int      bcm2835gpio_gpio_pin_read(void *, int);
     81  1.2.4.2  tls static void     bcm2835gpio_gpio_pin_write(void *, int, int);
     82  1.2.4.2  tls static void     bcm2835gpio_gpio_pin_ctl(void *, int, int);
     83  1.2.4.2  tls #endif
     84  1.2.4.2  tls 
     85  1.2.4.2  tls CFATTACH_DECL_NEW(bcmgpio, sizeof(struct bcmgpio_softc),
     86  1.2.4.2  tls     bcmgpio_match, bcmgpio_attach, NULL, NULL);
     87  1.2.4.2  tls 
     88  1.2.4.2  tls static int
     89  1.2.4.2  tls bcmgpio_match(device_t parent, cfdata_t cf, void *aux)
     90  1.2.4.2  tls {
     91  1.2.4.2  tls 	struct amba_attach_args * const aaa = aux;
     92  1.2.4.2  tls 
     93  1.2.4.2  tls 	if (strcmp(aaa->aaa_name, "bcmgpio") != 0)
     94  1.2.4.2  tls 		return 0;
     95  1.2.4.2  tls 
     96  1.2.4.2  tls 	return 1;
     97  1.2.4.2  tls }
     98  1.2.4.2  tls 
     99  1.2.4.2  tls static void
    100  1.2.4.2  tls bcmgpio_attach(device_t parent, device_t self, void *aux)
    101  1.2.4.2  tls {
    102  1.2.4.2  tls 	struct bcmgpio_softc * const sc = device_private(self);
    103  1.2.4.2  tls #if NGPIO > 0
    104  1.2.4.2  tls 	int pin, minpin, maxpin;
    105  1.2.4.2  tls 	u_int func;
    106  1.2.4.2  tls 	struct gpiobus_attach_args gba;
    107  1.2.4.2  tls #endif
    108  1.2.4.2  tls 
    109  1.2.4.2  tls 	sc->sc_dev = self;
    110  1.2.4.2  tls 
    111  1.2.4.2  tls #if NGPIO > 0
    112  1.2.4.2  tls 	if (device_unit(sc->sc_dev) > 1) {
    113  1.2.4.2  tls 		aprint_naive(" NO GPIO\n");
    114  1.2.4.2  tls 		aprint_normal(": NO GPIO\n");
    115  1.2.4.2  tls 		return;
    116  1.2.4.2  tls 	} else if (device_unit(sc->sc_dev) == 1) {
    117  1.2.4.2  tls 		maxpin = 53;
    118  1.2.4.2  tls 		minpin = 32;
    119  1.2.4.2  tls 	} else {
    120  1.2.4.2  tls 		maxpin = 31;
    121  1.2.4.2  tls 		minpin = 0;
    122  1.2.4.2  tls 	}
    123  1.2.4.2  tls 
    124  1.2.4.2  tls 	aprint_naive("\n");
    125  1.2.4.2  tls 	aprint_normal(": GPIO [%d...%d]\n", minpin, maxpin);
    126  1.2.4.2  tls 
    127  1.2.4.2  tls 	/* already mapped - nothing to gain from struct amba_attach_args */
    128  1.2.4.2  tls 	sc->sc_iot = &bcm2835_bs_tag;
    129  1.2.4.2  tls 	sc->sc_ioh = BCM2835_IOPHYSTOVIRT(BCM2835_GPIO_BASE);
    130  1.2.4.2  tls 
    131  1.2.4.2  tls 	for (pin = minpin; pin <= maxpin; pin++) {
    132  1.2.4.2  tls 	        int epin = pin - minpin;
    133  1.2.4.2  tls 
    134  1.2.4.2  tls 	        sc->sc_gpio_pins[epin].pin_num = epin;
    135  1.2.4.2  tls 		/*
    136  1.2.4.2  tls 		 * find out pins still available for GPIO
    137  1.2.4.2  tls 		 */
    138  1.2.4.2  tls 		func = bcm2835gpio_function_read(pin);
    139  1.2.4.2  tls 
    140  1.2.4.2  tls 		if (func == BCM2835_GPIO_IN ||
    141  1.2.4.2  tls 		    func == BCM2835_GPIO_OUT) {
    142  1.2.4.2  tls 	                sc->sc_gpio_pins[epin].pin_caps = GPIO_PIN_INPUT |
    143  1.2.4.2  tls 				GPIO_PIN_OUTPUT |
    144  1.2.4.2  tls 				GPIO_PIN_PUSHPULL | GPIO_PIN_TRISTATE |
    145  1.2.4.2  tls 				GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    146  1.2.4.2  tls 			/* read initial state */
    147  1.2.4.2  tls 			sc->sc_gpio_pins[epin].pin_state =
    148  1.2.4.2  tls 				bcm2835gpio_gpio_pin_read(sc, epin);
    149  1.2.4.2  tls 			DPRINTF(1, ("%s: attach pin %d\n", device_xname(sc->sc_dev), pin));
    150  1.2.4.2  tls                 } else {
    151  1.2.4.2  tls 	                sc->sc_gpio_pins[epin].pin_caps = 0;
    152  1.2.4.2  tls 			sc->sc_gpio_pins[epin].pin_state = 0;
    153  1.2.4.2  tls   			DPRINTF(1, ("%s: skip pin %d - func = 0x%x\n", device_xname(sc->sc_dev), pin, func));
    154  1.2.4.2  tls                 }
    155  1.2.4.2  tls         }
    156  1.2.4.2  tls 
    157  1.2.4.2  tls 	/* create controller tag */
    158  1.2.4.2  tls 	sc->sc_gpio_gc.gp_cookie = sc;
    159  1.2.4.2  tls 	sc->sc_gpio_gc.gp_pin_read = bcm2835gpio_gpio_pin_read;
    160  1.2.4.2  tls 	sc->sc_gpio_gc.gp_pin_write = bcm2835gpio_gpio_pin_write;
    161  1.2.4.2  tls 	sc->sc_gpio_gc.gp_pin_ctl = bcm2835gpio_gpio_pin_ctl;
    162  1.2.4.2  tls 
    163  1.2.4.2  tls 	gba.gba_gc = &sc->sc_gpio_gc;
    164  1.2.4.2  tls 	gba.gba_pins = sc->sc_gpio_pins;
    165  1.2.4.2  tls 	gba.gba_npins = maxpin - minpin + 1;
    166  1.2.4.2  tls 
    167  1.2.4.2  tls 	config_found_ia(self, "gpiobus", &gba, gpiobus_print);
    168  1.2.4.2  tls #else
    169  1.2.4.2  tls 	aprint_normal_dev(sc->sc_dev, "no GPIO configured in kernel");
    170  1.2.4.2  tls #endif
    171  1.2.4.2  tls }
    172  1.2.4.2  tls 
    173  1.2.4.2  tls #if NGPIO > 0
    174  1.2.4.2  tls /* GPIO support functions */
    175  1.2.4.2  tls static int
    176  1.2.4.2  tls bcm2835gpio_gpio_pin_read(void *arg, int pin)
    177  1.2.4.2  tls {
    178  1.2.4.2  tls 	struct bcmgpio_softc *sc = arg;
    179  1.2.4.2  tls 	int epin = pin + device_unit(sc->sc_dev) * 32;
    180  1.2.4.2  tls 	uint32_t val;
    181  1.2.4.2  tls 	int res;
    182  1.2.4.2  tls 
    183  1.2.4.2  tls 	if (device_unit(sc->sc_dev) > 1) {
    184  1.2.4.2  tls 		return 0;
    185  1.2.4.2  tls 	}
    186  1.2.4.2  tls 
    187  1.2.4.2  tls 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    188  1.2.4.2  tls 		BCM2835_GPIO_GPLEV(epin / BCM2835_GPIO_GPLEV_PINS_PER_REGISTER));
    189  1.2.4.2  tls 
    190  1.2.4.2  tls 	res = val & (1 << (epin % BCM2835_GPIO_GPLEV_PINS_PER_REGISTER)) ?
    191  1.2.4.2  tls 		GPIO_PIN_HIGH : GPIO_PIN_LOW;
    192  1.2.4.2  tls 
    193  1.2.4.2  tls 	DPRINTF(2, ("%s: gpio_read pin %d->%d\n", device_xname(sc->sc_dev), epin, (res == GPIO_PIN_HIGH)));
    194  1.2.4.2  tls 
    195  1.2.4.2  tls 	return res;
    196  1.2.4.2  tls }
    197  1.2.4.2  tls 
    198  1.2.4.2  tls static void
    199  1.2.4.2  tls bcm2835gpio_gpio_pin_write(void *arg, int pin, int value)
    200  1.2.4.2  tls {
    201  1.2.4.2  tls 	struct bcmgpio_softc *sc = arg;
    202  1.2.4.2  tls 	int epin = pin + device_unit(sc->sc_dev) * 32;
    203  1.2.4.2  tls 	bus_size_t reg;
    204  1.2.4.2  tls 
    205  1.2.4.2  tls 	if (device_unit(sc->sc_dev) > 1) {
    206  1.2.4.2  tls 		return;
    207  1.2.4.2  tls 	}
    208  1.2.4.2  tls 
    209  1.2.4.2  tls 	if (value == GPIO_PIN_HIGH) {
    210  1.2.4.2  tls 		reg = BCM2835_GPIO_GPSET(epin / BCM2835_GPIO_GPSET_PINS_PER_REGISTER);
    211  1.2.4.2  tls 	} else {
    212  1.2.4.2  tls 		reg = BCM2835_GPIO_GPCLR(epin / BCM2835_GPIO_GPCLR_PINS_PER_REGISTER);
    213  1.2.4.2  tls 	}
    214  1.2.4.2  tls 
    215  1.2.4.2  tls 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    216  1.2.4.2  tls 		reg, 1 << (epin % BCM2835_GPIO_GPSET_PINS_PER_REGISTER));
    217  1.2.4.2  tls 	DPRINTF(2, ("%s: gpio_write pin %d<-%d\n", device_xname(sc->sc_dev), epin, (value == GPIO_PIN_HIGH)));
    218  1.2.4.2  tls }
    219  1.2.4.2  tls 
    220  1.2.4.2  tls static void
    221  1.2.4.2  tls bcm2835gpio_gpio_pin_ctl(void *arg, int pin, int flags)
    222  1.2.4.2  tls {
    223  1.2.4.2  tls 	struct bcmgpio_softc *sc = arg;
    224  1.2.4.2  tls 	uint32_t cmd;
    225  1.2.4.2  tls 	int epin = pin + device_unit(sc->sc_dev) * 32;
    226  1.2.4.2  tls 
    227  1.2.4.2  tls 	if (device_unit(sc->sc_dev) > 1) {
    228  1.2.4.2  tls 		return;
    229  1.2.4.2  tls 	}
    230  1.2.4.2  tls 
    231  1.2.4.2  tls 	DPRINTF(2, ("%s: gpio_ctl pin %d flags 0x%x\n", device_xname(sc->sc_dev), epin, flags));
    232  1.2.4.2  tls 
    233  1.2.4.2  tls 	if (flags & (GPIO_PIN_OUTPUT|GPIO_PIN_INPUT)) {
    234  1.2.4.2  tls 		if ((flags & GPIO_PIN_INPUT) || !(flags & GPIO_PIN_OUTPUT)) {
    235  1.2.4.2  tls 			/* for safety INPUT will overide output */
    236  1.2.4.2  tls 	                bcm2835gpio_function_select(epin, BCM2835_GPIO_IN);
    237  1.2.4.2  tls                 } else {
    238  1.2.4.2  tls 	                bcm2835gpio_function_select(epin, BCM2835_GPIO_OUT);
    239  1.2.4.2  tls 		}
    240  1.2.4.2  tls 	}
    241  1.2.4.2  tls 
    242  1.2.4.2  tls 	if (flags & (GPIO_PIN_PULLUP|GPIO_PIN_PULLDOWN)) {
    243  1.2.4.2  tls 		cmd = (flags & GPIO_PIN_PULLUP) ?
    244  1.2.4.2  tls 			BCM2835_GPIO_GPPUD_PULLUP : BCM2835_GPIO_GPPUD_PULLDOWN;
    245  1.2.4.2  tls 	} else {
    246  1.2.4.2  tls 		cmd = BCM2835_GPIO_GPPUD_PULLOFF;
    247  1.2.4.2  tls 	}
    248  1.2.4.2  tls 
    249  1.2.4.2  tls 	/* set up control signal */
    250  1.2.4.2  tls 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    251  1.2.4.2  tls 		BCM2835_GPIO_GPPUD, cmd);
    252  1.2.4.2  tls 	delay(1); /* wait 150 cycles */
    253  1.2.4.2  tls 	/* set clock signal */
    254  1.2.4.2  tls 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    255  1.2.4.2  tls 		BCM2835_GPIO_GPPUDCLK(device_unit(sc->sc_dev)),
    256  1.2.4.2  tls 		1 << (epin % BCM2835_GPIO_GPPUD_PINS_PER_REGISTER));
    257  1.2.4.2  tls 	delay(1); /* wait 150 cycles */
    258  1.2.4.2  tls 	/* reset control signal and clock */
    259  1.2.4.2  tls 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    260  1.2.4.2  tls 		BCM2835_GPIO_GPPUD, BCM2835_GPIO_GPPUD_PULLOFF);
    261  1.2.4.2  tls 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    262  1.2.4.2  tls 		BCM2835_GPIO_GPPUDCLK(device_unit(sc->sc_dev)),
    263  1.2.4.2  tls 		0);
    264  1.2.4.2  tls }
    265  1.2.4.2  tls #endif /* NGPIO > 0 */
    266