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