From nobody Thu Apr 16 15:28:31 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4fxMNc280mz6ZrHk for ; Thu, 16 Apr 2026 15:28:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4fxMNb3Cvhz3Fqy for ; Thu, 16 Apr 2026 15:28:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1776353311; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=RS2hg+tuTdN8Y9dskW+d/p+EoIQv0CUxT15AOE9R1Dc=; b=lQlutxObrps1iWQlQ/kZRuotEzrF6jUN1VIPZIhCgr+/7fzjhZ8O+v8WIWTlPManGwZJ5o 2GGOAc34+UOm5Q3nrOw4PzgZl29qfNm5A+AeKybXaoteDAL8RL+lnug5vnIVB19xLvB9fE 3SSb7VOJYyInxT848liBhl0heOgEBo7Hk6SL/5d6w9I/IFoxHg+U1Pmy1Lij+TWu/Z5c9v t/l5jgig7+5q6nbGBrLlok/yRlyhcj+u+u7WgjEsPsTbgY4nhtjwfFmgDqGu7YB2XBMC0s h/BowZ4nj94/uARrjbGcHOXNHsGpCnu0nyviID4oM5+YAob3GRQcTiqI9ZSHbA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1776353311; a=rsa-sha256; cv=none; b=pvrMm6pIWBrXods8GzlD04AMAwMViJSIW5HvJscerRfwidcUCftWyZLRaF6hzB7xHQm4F2 cLglOG4QktUc74//kIHwgYkQWO6/7CFy12j1wQ2HwPn+5r6MkCcZqpkh2CS6yfeyGSwiEE Hcye9/V3k+2hVCQhwybmIVbcGsb16uXSsvCbYD3syI7LMkiffAFDhlvduHmJ1MfOE+20tp 20ytE9zE2MSJbgdIlcaCJvBTS5+xnpQjIz3siHTavtyicGLobRQz+Iw7YqHczjlcmSAGCO cdE9G+FvNn5wFgPAAW2LBfQ+umQyclvEK9MVJWtjHXIx+pn5dsvksRbQsg0JnA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1776353311; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=RS2hg+tuTdN8Y9dskW+d/p+EoIQv0CUxT15AOE9R1Dc=; b=eA40hcANLV72h1w9zLCG4gD4CT39fFlMtkb1vQx2gE4H/WNvzTATwXGJ/Lcs7GtggQ7DF5 mzC4arge+9q35e62anJq/fELJTKDo7vdr5hCq6C6MlH1R0DpWToltsTm6oL5k5mXdeB/0Z 6ujv/o+IPoo1k/2B8SLfTM28Wt7iQcOmplyw+1CWBO7WSgKoA4XRnAKG08RqhhiwE/cl+u ApiZ95Cx1U7MogA5LMBsw28Dawh8XQGVs9NOhtl9swbTQL1acRBv1NIQKtKU4y/9iXZPkj HYjK9/uX5LPZLSBLkKp199XvvzrVoo3kdzlq0HbkI9HJUpilZfUpw3PkjyGD/g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4fxMNb2gHWz1C0K for ; Thu, 16 Apr 2026 15:28:31 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1f92c by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 16 Apr 2026 15:28:31 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Cc: Tetsuya Uemura From: Mitchell Horne Subject: git: fb10574334ca - stable/15 - bcm2835_virtgpio: Add driver for virtual GPIO controller on some RPi models List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: fb10574334caedc02819f1bcbe0f13ea558689be Auto-Submitted: auto-generated Date: Thu, 16 Apr 2026 15:28:31 +0000 Message-Id: <69e1001f.1f92c.715a7656@gitrepo.freebsd.org> The branch stable/15 has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=fb10574334caedc02819f1bcbe0f13ea558689be commit fb10574334caedc02819f1bcbe0f13ea558689be Author: Tetsuya Uemura AuthorDate: 2026-03-30 15:24:38 +0000 Commit: Mitchell Horne CommitDate: 2026-04-16 15:25:07 +0000 bcm2835_virtgpio: Add driver for virtual GPIO controller on some RPi models This driver enables bcm2835-virtgpio GPIO controller found on RPi3B and some CM boards. On which, the ACT (green) LED is connected to this controller. It is essential for FreeBSD to have this driver to control this LED. It will be exposed via gpioled(4). Reviewed by: mhorne MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D51456 (cherry picked from commit 84c68dbb59df81d5371f0d1eea888d30561d428d) --- sys/arm/broadcom/bcm2835/bcm2835_firmware.h | 12 + sys/arm/broadcom/bcm2835/raspberrypi_virtgpio.c | 347 ++++++++++++++++++++++++ sys/conf/files.arm64 | 1 + 3 files changed, 360 insertions(+) diff --git a/sys/arm/broadcom/bcm2835/bcm2835_firmware.h b/sys/arm/broadcom/bcm2835/bcm2835_firmware.h index 8c77dac7d710..13116999d38c 100644 --- a/sys/arm/broadcom/bcm2835/bcm2835_firmware.h +++ b/sys/arm/broadcom/bcm2835/bcm2835_firmware.h @@ -195,6 +195,18 @@ union msg_set_gpio_config { } resp; }; +#define BCM2835_FIRMWARE_TAG_GET_GPIOVIRTBUF 0x00040010 +#define BCM2835_FIRMWARE_TAG_SET_GPIOVIRTBUF 0x00048020 + +union msg_gpiovirtbuf { + struct { + uint32_t addr; + } req; + struct { + uint32_t addr; + } resp; +}; + int bcm2835_firmware_property(device_t, uint32_t, void *, size_t); #endif diff --git a/sys/arm/broadcom/bcm2835/raspberrypi_virtgpio.c b/sys/arm/broadcom/bcm2835/raspberrypi_virtgpio.c new file mode 100644 index 000000000000..64ce6fda6306 --- /dev/null +++ b/sys/arm/broadcom/bcm2835/raspberrypi_virtgpio.c @@ -0,0 +1,347 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2025 Tetsuya Uemura + * + * 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. + */ + +/* + * This is a driver for bcm2835-virtgpio GPIO controller device found on some + * Raspberry Pi models (listed below but not limited to). On which, the green + * LED (ACT) is connected to this controller. With the help of this driver, a + * node corresponding to the green LED will be created under /dev/led, allowing + * us to control it. + * + * Applicable models (according to the FDTs of those models): + * Compute Module 2 (CM2) + * 3 Model B (not 3B+) + * Compute Module 3 (CM3) and possibly 3+ (CM3+) + * Compute Module 4 SODIMM (CM4S) + */ + +#include "opt_platform.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include + +#include "gpio_if.h" + +#define RPI_VIRT_GPIO_PINS 2 + +struct rpi_virt_gpio_softc { + device_t busdev; + device_t firmware; + struct mtx sc_mtx; + + void *vaddr; /* Virtual address. */ + vm_paddr_t paddr; /* Physical address. */ + + struct gpio_pin gpio_pins[RPI_VIRT_GPIO_PINS]; + uint32_t state[RPI_VIRT_GPIO_PINS]; +}; + +#define RPI_VIRT_GPIO_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx) +#define RPI_VIRT_GPIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx) + +static struct ofw_compat_data compat_data[] = { + {"brcm,bcm2835-virtgpio", 1}, + {NULL, 0} +}; + +static device_t +rpi_virt_gpio_get_bus(device_t dev) +{ + struct rpi_virt_gpio_softc *sc; + + sc = device_get_softc(dev); + + return (sc->busdev); +} + +static int +rpi_virt_gpio_pin_max(device_t dev, int *maxpin) +{ + *maxpin = RPI_VIRT_GPIO_PINS - 1; + + return (0); +} + +static int +rpi_virt_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps) +{ + if (pin >= RPI_VIRT_GPIO_PINS) + return (EINVAL); + + *caps = GPIO_PIN_OUTPUT; + + return (0); +} + +static int +rpi_virt_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags) +{ + if (pin >= RPI_VIRT_GPIO_PINS) + return (EINVAL); + + *flags = GPIO_PIN_OUTPUT; + + return (0); +} + +static int +rpi_virt_gpio_pin_set(device_t dev, uint32_t pin, uint32_t value) +{ + struct rpi_virt_gpio_softc *sc; + uint32_t *ptr; + uint16_t on, off; + + if (pin >= RPI_VIRT_GPIO_PINS) + return (EINVAL); + + sc = device_get_softc(dev); + + RPI_VIRT_GPIO_LOCK(sc); + on = (uint16_t)(sc->state[pin] >> 16); + off = (uint16_t)sc->state[pin]; + + if (bootverbose) + device_printf(dev, "on: %hu, off: %hu, now: %d -> %u\n", + on, off, on - off, value); + + if ((value > 0 && on - off != 0) || (value == 0 && on - off == 0)) { + RPI_VIRT_GPIO_UNLOCK(sc); + return (0); + } + + if (value > 0) + ++on; + else + ++off; + + sc->state[pin] = (on << 16 | off); + ptr = (uint32_t *)sc->vaddr; + ptr[pin] = sc->state[pin]; + RPI_VIRT_GPIO_UNLOCK(sc); + + return (0); +} + +static int +rpi_virt_gpio_pin_get(device_t dev, uint32_t pin, uint32_t *val) +{ + struct rpi_virt_gpio_softc *sc; + uint32_t *ptr, v; + + if (pin >= RPI_VIRT_GPIO_PINS) + return (EINVAL); + + sc = device_get_softc(dev); + + ptr = (uint32_t *)sc->vaddr; + RPI_VIRT_GPIO_LOCK(sc); + v = ptr[pin]; + RPI_VIRT_GPIO_UNLOCK(sc); + *val = ((uint16_t)(v >> 16) - (uint16_t)v) == 0 ? 0 : 1; + + return (0); +} + +static int +rpi_virt_gpio_pin_toggle(device_t dev, uint32_t pin) +{ + int rv; + unsigned int val; + + if (pin >= RPI_VIRT_GPIO_PINS) + return (EINVAL); + + rv = rpi_virt_gpio_pin_get(dev, pin, &val); + if (rv != 0) + return (rv); + + rv = rpi_virt_gpio_pin_set(dev, pin, val == 0 ? 1 : 0); + + return (rv); +} + +static int +rpi_virt_gpio_probe(device_t dev) +{ + device_t firmware; + phandle_t gpio; + union msg_gpiovirtbuf cfg; + int rv; + + if (ofw_bus_status_okay(dev) == 0) + return (ENXIO); + + if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) + return (ENXIO); + + gpio = ofw_bus_get_node(dev); + if (OF_hasprop(gpio, "gpio-controller") == 0) + return (ENXIO); + + /* Check whether the firmware is ready. */ + firmware = device_get_parent(dev); + rv = bcm2835_firmware_property(firmware, + BCM2835_FIRMWARE_TAG_GET_GPIOVIRTBUF, &cfg, sizeof(cfg)); + if (rv != 0) + return (ENXIO); + + device_set_desc(dev, "Raspberry Pi Virtual GPIO controller"); + + return (BUS_PROBE_DEFAULT); +} + +static int +rpi_virt_gpio_attach(device_t dev) +{ + struct rpi_virt_gpio_softc *sc; + union msg_gpiovirtbuf cfg; + int i, rv; + + sc = device_get_softc(dev); + sc->firmware = device_get_parent(dev); + mtx_init(&sc->sc_mtx, "Raspberry Pi virtgpio", NULL, MTX_SPIN); + + /* + * According to the Linux source at: + * https://github.com/raspberrypi/linux/blob/rpi-6.12.y/drivers/gpio/gpio-bcm-virt.c + * it first attempts to set the pre-allocated physical memory address + * in the firmware. If it is successfully acquired, access virtgpio via + * the virtual memory address mapped to that physical address. + * + * If the above fails, then as a fallback, attempts to obtain a + * physical memory address for accessing virtgpio from the firmware. + * And if obtained, link it to a virtual memory address and access + * virtgpio via it. + * + * An OpenWRT virtgpio driver I happened to see at first only + * implemented the fallback method. Then I implemented this method on + * FreeBSD and tested it with the 20240429 firmware, but it didn't + * work. + * + * At this point, I realised the first method in the source above. So I + * implemented this method on FreeBSD and tested it, and it worked. In + * my opinion, the second method was used until some time prior to + * 20240429, and then the firmware was modified and the first method + * was introduced. In my driver, only the first method exists. + */ + + /* Allocate a physical memory range for accessing virtgpio. */ + sc->vaddr = contigmalloc( + PAGE_SIZE, /* size */ + M_DEVBUF, M_ZERO, /* type, flags */ + 0, BCM2838_PERIPH_MAXADDR, /* low, high */ + PAGE_SIZE, 0); /* alignment, boundary */ + if (sc->vaddr == NULL) { + device_printf(dev, "Failed to allocate memory.\n"); + return ENOMEM; + } + sc->paddr = vtophys(sc->vaddr); + /* Mark it uncacheable. */ + pmap_change_attr((vm_offset_t)sc->vaddr, PAGE_SIZE, + VM_MEMATTR_UNCACHEABLE); + + if (bootverbose) + device_printf(dev, + "KVA alloc'd: virtual: %p, phys: %#jx\n", + sc->vaddr, (uintmax_t)sc->paddr); + + /* Set this address in firmware. */ + cfg.req.addr = (uint32_t)sc->paddr; + rv = bcm2835_firmware_property(sc->firmware, + BCM2835_FIRMWARE_TAG_SET_GPIOVIRTBUF, &cfg, sizeof(cfg)); + if (bootverbose) + device_printf(dev, "rv: %d, addr: 0x%x\n", rv, cfg.resp.addr); + if (rv != 0 || cfg.resp.addr != 0) + goto fail; + + /* Pins only support output. */ + for (i = 0; i < RPI_VIRT_GPIO_PINS; i++) { + sc->gpio_pins[i].gp_pin = i; + sc->gpio_pins[i].gp_caps = sc->gpio_pins[i].gp_flags + = GPIO_PIN_OUTPUT; + } + sc->busdev = gpiobus_add_bus(dev); + if (sc->busdev == NULL) + goto fail; + + bus_attach_children(dev); + return (0); + +fail: + /* Release resource if necessary. */ + free(sc->vaddr, M_DEVBUF); + mtx_destroy(&sc->sc_mtx); + + return (ENXIO); +} + +static int +rpi_virt_gpio_detach(device_t dev) +{ + return (EBUSY); +} + +static device_method_t rpi_virt_gpio_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, rpi_virt_gpio_probe), + DEVMETHOD(device_attach, rpi_virt_gpio_attach), + DEVMETHOD(device_detach, rpi_virt_gpio_detach), + + /* GPIO protocol */ + DEVMETHOD(gpio_get_bus, rpi_virt_gpio_get_bus), + DEVMETHOD(gpio_pin_max, rpi_virt_gpio_pin_max), + DEVMETHOD(gpio_pin_getcaps, rpi_virt_gpio_pin_getcaps), + DEVMETHOD(gpio_pin_getflags, rpi_virt_gpio_pin_getflags), + DEVMETHOD(gpio_pin_set, rpi_virt_gpio_pin_set), + DEVMETHOD(gpio_pin_get, rpi_virt_gpio_pin_get), + DEVMETHOD(gpio_pin_toggle, rpi_virt_gpio_pin_toggle), + + DEVMETHOD_END +}; + +static driver_t rpi_virt_gpio_driver = { + "gpio", + rpi_virt_gpio_methods, + sizeof(struct rpi_virt_gpio_softc), +}; + +EARLY_DRIVER_MODULE(rpi_virt_gpio, bcm2835_firmware, rpi_virt_gpio_driver, + 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE); diff --git a/sys/conf/files.arm64 b/sys/conf/files.arm64 index ab6d0c91c947..0ae122332e1f 100644 --- a/sys/conf/files.arm64 +++ b/sys/conf/files.arm64 @@ -590,6 +590,7 @@ arm/broadcom/bcm2835/bcm283x_dwc_fdt.c optional dwcotg fdt soc_brcm_bcm2837 | arm/broadcom/bcm2835/bcm2838_pci.c optional soc_brcm_bcm2838 fdt pci arm/broadcom/bcm2835/bcm2838_xhci.c optional soc_brcm_bcm2838 fdt pci xhci arm/broadcom/bcm2835/raspberrypi_gpio.c optional soc_brcm_bcm2837 gpio fdt | soc_brcm_bcm2838 gpio fdt +arm/broadcom/bcm2835/raspberrypi_virtgpio.c optional soc_brcm_bcm2837 gpio fdt | soc_brcm_bcm2838 gpio fdt contrib/vchiq/interface/compat/vchi_bsd.c optional vchiq soc_brcm_bcm2837 fdt \ compile-with "${NORMAL_C} -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000 -I$S/contrib/vchiq" contrib/vchiq/interface/vchiq_arm/vchiq_2835_arm.c optional vchiq soc_brcm_bcm2837 fdt \