Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
 * Copyright (c) 2012-2015 Luiz Otavio O Souza <loos@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_platform.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/gpio.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/sx.h>
#include <sys/proc.h>

#include <dev/gpio/gpiobusvar.h>
#include <dev/ofw/ofw_bus.h>

#include <arm/broadcom/bcm2835/bcm2835_firmware.h>

#include "gpio_if.h"

#define	RPI_FW_GPIO_PINS		8
#define	RPI_FW_GPIO_BASE		128
#define	RPI_FW_GPIO_DEFAULT_CAPS	(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT)

struct rpi_fw_gpio_softc {
	device_t		sc_busdev;
	device_t		sc_firmware;
	struct sx		sc_sx;
	struct gpio_pin		sc_gpio_pins[RPI_FW_GPIO_PINS];
	uint8_t			sc_gpio_state;
};

#define	RPI_FW_GPIO_LOCK(_sc)	sx_xlock(&(_sc)->sc_sx)
#define	RPI_FW_GPIO_UNLOCK(_sc)	sx_xunlock(&(_sc)->sc_sx)

static struct ofw_compat_data compat_data[] = {
	{"raspberrypi,firmware-gpio",	1},
	{NULL,				0}
};

static int
rpi_fw_gpio_pin_configure(struct rpi_fw_gpio_softc *sc, struct gpio_pin *pin,
    unsigned int flags)
{
	union msg_get_gpio_config old_cfg;
	union msg_set_gpio_config new_cfg;
	int rv;

	bzero(&old_cfg, sizeof(old_cfg));
	bzero(&new_cfg, sizeof(new_cfg));
	old_cfg.req.gpio = RPI_FW_GPIO_BASE + pin->gp_pin;

	RPI_FW_GPIO_LOCK(sc);
	rv = bcm2835_firmware_property(sc->sc_firmware,
	    BCM2835_FIRMWARE_TAG_GET_GPIO_CONFIG, &old_cfg, sizeof(old_cfg));
	if (rv == 0 && old_cfg.resp.gpio != 0)
		rv = EIO;
	if (rv != 0)
		goto fail;

	new_cfg.req.gpio = RPI_FW_GPIO_BASE + pin->gp_pin;
	if (flags & GPIO_PIN_INPUT) {
		new_cfg.req.dir = BCM2835_FIRMWARE_GPIO_IN;
		new_cfg.req.state = 0;
		pin->gp_flags = GPIO_PIN_INPUT;
	} else if (flags & GPIO_PIN_OUTPUT) {
		new_cfg.req.dir = BCM2835_FIRMWARE_GPIO_OUT;
		if (flags & (GPIO_PIN_PRESET_HIGH | GPIO_PIN_PRESET_LOW)) {
			if (flags & GPIO_PIN_PRESET_HIGH) {
				new_cfg.req.state = 1;
				sc->sc_gpio_state |= (1 << pin->gp_pin);
			} else {
				new_cfg.req.state = 0;
				sc->sc_gpio_state &= ~(1 << pin->gp_pin);
			}
		} else {
			if ((sc->sc_gpio_state & (1 << pin->gp_pin)) != 0) {
				new_cfg.req.state = 1;
			} else {
				new_cfg.req.state = 0;
			}
		}
		pin->gp_flags = GPIO_PIN_OUTPUT;
	} else {
		new_cfg.req.dir = old_cfg.resp.dir;
		/* Use the old state to decide high/low */
		if ((sc->sc_gpio_state & (1 << pin->gp_pin)) != 0)
			new_cfg.req.state = 1;
		else
			new_cfg.req.state = 0;
	}
	new_cfg.req.pol = old_cfg.resp.pol;
	new_cfg.req.term_en = 0;
	new_cfg.req.term_pull_up = 0;

	rv = bcm2835_firmware_property(sc->sc_firmware,
	    BCM2835_FIRMWARE_TAG_SET_GPIO_CONFIG, &new_cfg, sizeof(new_cfg));

fail:
	RPI_FW_GPIO_UNLOCK(sc);

	return (rv);
}

static device_t
rpi_fw_gpio_get_bus(device_t dev)
{
	struct rpi_fw_gpio_softc *sc;

	sc = device_get_softc(dev);

	return (sc->sc_busdev);
}

static int
rpi_fw_gpio_pin_max(device_t dev, int *maxpin)
{

	*maxpin = RPI_FW_GPIO_PINS - 1;
	return (0);
}

static int
rpi_fw_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
{
	struct rpi_fw_gpio_softc *sc;
	int i;

	sc = device_get_softc(dev);
	for (i = 0; i < RPI_FW_GPIO_PINS; i++) {
		if (sc->sc_gpio_pins[i].gp_pin == pin)
			break;
	}

	if (i >= RPI_FW_GPIO_PINS)
		return (EINVAL);

	*caps = RPI_FW_GPIO_DEFAULT_CAPS;
	return (0);
}

static int
rpi_fw_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
{
	struct rpi_fw_gpio_softc *sc = device_get_softc(dev);
	int i;

	for (i = 0; i < RPI_FW_GPIO_PINS; i++) {
		if (sc->sc_gpio_pins[i].gp_pin == pin)
			break;
	}

	if (i >= RPI_FW_GPIO_PINS)
		return (EINVAL);

	RPI_FW_GPIO_LOCK(sc);
	*flags = sc->sc_gpio_pins[i].gp_flags;
	RPI_FW_GPIO_UNLOCK(sc);

	return (0);
}

static int
rpi_fw_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
{
	struct rpi_fw_gpio_softc *sc;
	int i;

	sc = device_get_softc(dev);
	for (i = 0; i < RPI_FW_GPIO_PINS; i++) {
		if (sc->sc_gpio_pins[i].gp_pin == pin)
			break;
	}

	if (i >= RPI_FW_GPIO_PINS)
		return (EINVAL);

	RPI_FW_GPIO_LOCK(sc);
	memcpy(name, sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME);
	RPI_FW_GPIO_UNLOCK(sc);

	return (0);
}

static int
rpi_fw_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
{
	struct rpi_fw_gpio_softc *sc;
	int i;

	sc = device_get_softc(dev);
	for (i = 0; i < RPI_FW_GPIO_PINS; i++) {
		if (sc->sc_gpio_pins[i].gp_pin == pin)
			break;
	}

	if (i >= RPI_FW_GPIO_PINS)
		return (EINVAL);

	return (rpi_fw_gpio_pin_configure(sc, &sc->sc_gpio_pins[i], flags));
}

static int
rpi_fw_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
{
	struct rpi_fw_gpio_softc *sc;
	union msg_set_gpio_state state;
	int i, rv;

	sc = device_get_softc(dev);
	for (i = 0; i < RPI_FW_GPIO_PINS; i++) {
		if (sc->sc_gpio_pins[i].gp_pin == pin)
			break;
	}
	if (i >= RPI_FW_GPIO_PINS)
		return (EINVAL);

	state.req.gpio = RPI_FW_GPIO_BASE + pin;
	state.req.state = value;

	RPI_FW_GPIO_LOCK(sc);
	rv = bcm2835_firmware_property(sc->sc_firmware,
	    BCM2835_FIRMWARE_TAG_SET_GPIO_STATE, &state, sizeof(state));
	/* The firmware sets gpio to 0 on success */
	if (rv == 0 && state.resp.gpio != 0)
		rv = EINVAL;
	if (rv == 0) {
		sc->sc_gpio_pins[i].gp_flags &= ~(GPIO_PIN_PRESET_HIGH |
		    GPIO_PIN_PRESET_LOW);
		if (value)
			sc->sc_gpio_state |= (1 << i);
		else
			sc->sc_gpio_state &= ~(1 << i);
	}
	RPI_FW_GPIO_UNLOCK(sc);

	return (rv);
}

static int
rpi_fw_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
{
	struct rpi_fw_gpio_softc *sc;
	union msg_get_gpio_state state;
	int i, rv;

	sc = device_get_softc(dev);
	for (i = 0; i < RPI_FW_GPIO_PINS; i++) {
		if (sc->sc_gpio_pins[i].gp_pin == pin)
			break;
	}
	if (i >= RPI_FW_GPIO_PINS)
		return (EINVAL);

	bzero(&state, sizeof(state));
	state.req.gpio = RPI_FW_GPIO_BASE + pin;

	RPI_FW_GPIO_LOCK(sc);
	rv = bcm2835_firmware_property(sc->sc_firmware,
	    BCM2835_FIRMWARE_TAG_GET_GPIO_STATE, &state, sizeof(state));
	RPI_FW_GPIO_UNLOCK(sc);

	/* The firmware sets gpio to 0 on success */
	if (rv == 0 && state.resp.gpio != 0)
		rv = EINVAL;
	if (rv == 0)
		*val = !state.resp.state;

	return (rv);
}

static int
rpi_fw_gpio_pin_toggle(device_t dev, uint32_t pin)
{
	struct rpi_fw_gpio_softc *sc;
	union msg_get_gpio_state old_state;
	union msg_set_gpio_state new_state;
	int i, rv;

	sc = device_get_softc(dev);
	for (i = 0; i < RPI_FW_GPIO_PINS; i++) {
		if (sc->sc_gpio_pins[i].gp_pin == pin)
			break;
	}
	if (i >= RPI_FW_GPIO_PINS)
		return (EINVAL);

	bzero(&old_state, sizeof(old_state));
	bzero(&new_state, sizeof(new_state));

	old_state.req.gpio = RPI_FW_GPIO_BASE + pin;
	new_state.req.gpio = RPI_FW_GPIO_BASE + pin;

	RPI_FW_GPIO_LOCK(sc);
	rv = bcm2835_firmware_property(sc->sc_firmware,
	    BCM2835_FIRMWARE_TAG_GET_GPIO_STATE, &old_state, sizeof(old_state));
	/* The firmware sets gpio to 0 on success */
	if (rv == 0 && old_state.resp.gpio == 0) {
		/* Set the new state to invert the GPIO */
		new_state.req.state = !old_state.resp.state;
		rv = bcm2835_firmware_property(sc->sc_firmware,
		    BCM2835_FIRMWARE_TAG_SET_GPIO_STATE, &new_state,
		    sizeof(new_state));
	}
	if (rv == 0 && (old_state.resp.gpio != 0 || new_state.resp.gpio != 0))
		rv = EINVAL;
	RPI_FW_GPIO_UNLOCK(sc);

	return (rv);
}

static int
rpi_fw_gpio_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
		return (ENXIO);

	device_set_desc(dev, "Raspberry Pi Firmware GPIO controller");
	return (BUS_PROBE_DEFAULT);
}

static int
rpi_fw_gpio_attach(device_t dev)
{
	union msg_get_gpio_config cfg;
	struct rpi_fw_gpio_softc *sc;
	char *names;
	phandle_t gpio;
	int i, nelems, elm_pos, rv;

	sc = device_get_softc(dev);
	sc->sc_firmware = device_get_parent(dev);
	sx_init(&sc->sc_sx, "Raspberry Pi firmware gpio");
	/* Find our node. */
	gpio = ofw_bus_get_node(dev);
	if (!OF_hasprop(gpio, "gpio-controller"))
		/* This is not a GPIO controller. */
		goto fail;

	nelems = OF_getprop_alloc(gpio, "gpio-line-names", (void **)&names);
	if (nelems <= 0)
		names = NULL;
	elm_pos = 0;
	for (i = 0; i < RPI_FW_GPIO_PINS; i++) {
		/* Set the current pin name */
		if (names != NULL && elm_pos < nelems &&
		    names[elm_pos] != '\0') {
			snprintf(sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME,
			    "%s", names + elm_pos);
			/* Find the next pin name */
			elm_pos += strlen(names + elm_pos) + 1;
		} else {
			snprintf(sc->sc_gpio_pins[i].gp_name, GPIOMAXNAME,
			    "pin %d", i);
		}

		sc->sc_gpio_pins[i].gp_pin = i;
		sc->sc_gpio_pins[i].gp_caps = RPI_FW_GPIO_DEFAULT_CAPS;

		bzero(&cfg, sizeof(cfg));
		cfg.req.gpio = RPI_FW_GPIO_BASE + i;
		rv = bcm2835_firmware_property(sc->sc_firmware,
		    BCM2835_FIRMWARE_TAG_GET_GPIO_CONFIG, &cfg, sizeof(cfg));
		if (rv == 0 && cfg.resp.gpio == 0) {
			if (cfg.resp.dir == BCM2835_FIRMWARE_GPIO_IN)
				sc->sc_gpio_pins[i].gp_flags = GPIO_PIN_INPUT;
			else
				sc->sc_gpio_pins[i].gp_flags = GPIO_PIN_OUTPUT;
		} else {
			sc->sc_gpio_pins[i].gp_flags = GPIO_PIN_INPUT;
		}
	}
	free(names, M_OFWPROP);
	sc->sc_busdev = gpiobus_attach_bus(dev);
	if (sc->sc_busdev == NULL)
		goto fail;

	return (0);

fail:
	sx_destroy(&sc->sc_sx);

	return (ENXIO);
}

static int
rpi_fw_gpio_detach(device_t dev)
{

	return (EBUSY);
}

static device_method_t rpi_fw_gpio_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		rpi_fw_gpio_probe),
	DEVMETHOD(device_attach,	rpi_fw_gpio_attach),
	DEVMETHOD(device_detach,	rpi_fw_gpio_detach),

	/* GPIO protocol */
	DEVMETHOD(gpio_get_bus,		rpi_fw_gpio_get_bus),
	DEVMETHOD(gpio_pin_max,		rpi_fw_gpio_pin_max),
	DEVMETHOD(gpio_pin_getname,	rpi_fw_gpio_pin_getname),
	DEVMETHOD(gpio_pin_getflags,	rpi_fw_gpio_pin_getflags),
	DEVMETHOD(gpio_pin_getcaps,	rpi_fw_gpio_pin_getcaps),
	DEVMETHOD(gpio_pin_setflags,	rpi_fw_gpio_pin_setflags),
	DEVMETHOD(gpio_pin_get,		rpi_fw_gpio_pin_get),
	DEVMETHOD(gpio_pin_set,		rpi_fw_gpio_pin_set),
	DEVMETHOD(gpio_pin_toggle,	rpi_fw_gpio_pin_toggle),

	DEVMETHOD_END
};

static devclass_t rpi_fw_gpio_devclass;

static driver_t rpi_fw_gpio_driver = {
	"gpio",
	rpi_fw_gpio_methods,
	sizeof(struct rpi_fw_gpio_softc),
};

EARLY_DRIVER_MODULE(rpi_fw_gpio, bcm2835_firmware, rpi_fw_gpio_driver,
    rpi_fw_gpio_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);