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