From nobody Tue Mar 31 19:56:57 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 4flf5q32pQz6WvrQ for ; Tue, 31 Mar 2026 19:57:03 +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 4flf5p6pQsz3NFk for ; Tue, 31 Mar 2026 19:57:02 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1774987023; 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=RpNiBK0OFMXEky3WtGFc+w2zEq2wyNtti7OW426G2NE=; b=nqq7XRHHi7pH2jNOPN5kkzAnZO0Bm7WjuJv1N/GuyOK75i/53I2PhoO0AHK6AuMOkuNrgG 28W5dWg9QK+tsSqQp5XL9VfcHbnJ+8Zh0/t5WeP+ssmXwFb3G2qDnVTlNFKW1JDdzeY2zC uJlbTrWPIjN/R1Pi+SF6Bm+jvcUcd3znpjx3bL9kSQ/l14KOAMoYPVZ3u/TIhSnWMqNSNQ uybPTBpekYbhrQCut68nE3xL8YS884c84OT6nPSWADIXQ1Lx09HXUbUkZmNRwSnY8KOU/O ASMcDJW5MR0xvI3rVwaNtzpubaWOcDiOdTgFnTLfrFJ1D1K8CC+ZtAwQQO3RDA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1774987023; a=rsa-sha256; cv=none; b=NirVasuamY+skAi9ng302e5Cs9/HZH1eIFRynwDpdF6XxIK27PWgM6ervgcNWVNFcbhzgs JPpRf9e7PnCTSG+YITblkm1HaUmOnJ/pv7smJ0EVT3OgaJFjxUmqcmZPGWpYygFymU8Qe3 YRA+Ubk3O3IdbUuHqHgesh6fpYjUeKK8PgKjkGsNlRUg4YFNKcyUdPhNrVg2kfpAyuzhKf dTo7sIVSrzXYwL1KB2mCrCZ0AmWDA6FSW5v0C//lFwIPO+R4EgwcqAUeob9pFWN2oIbxkF FUlR7yQFcnPZtUdq/7qf10+xgUGGa3opQVAcykLZq5rQJbF/j85nzEfmVZi9cA== 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=1774987023; 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=RpNiBK0OFMXEky3WtGFc+w2zEq2wyNtti7OW426G2NE=; b=W9cmm8BYXIFztkERo1rcY/aPRGsoyAeqBC0ZiYf5DBrpB2Xs6TnQvTPAuYwFZVi81KstgY 2yKHZLdClfUAAs21tJ252Fnyo0ZTIC3lacOuY0aN7fE5SoRgOdI/92Hf6cl1JJsUPFxugJ IINAcxJR4XgzpjECFRPM11MHmEu5x4wzy2FJDNXA/BtofPY6+o+QqT8xh9cO+7E4Zl27Gb vlQ6WwXdisWzVPDphw0RByt1nWtgSayYXfUWi7I0XciLeUXNuLqAIxVBn0NO5XWO+kyH9Y SQhv23gVkmwfFr2h6FsVTe2tUKMZUMm4M1BQzYF7kYtGLRTiuu+SFzRKdwN8dA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4flf5p5bNhzkCd for ; Tue, 31 Mar 2026 19:57:02 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 23772 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Tue, 31 Mar 2026 19:56:57 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Tetsuya Uemura From: Mitchell Horne Subject: git: 84c68dbb59df - main - 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/main X-Git-Reftype: branch X-Git-Commit: 84c68dbb59df81d5371f0d1eea888d30561d428d Auto-Submitted: auto-generated Date: Tue, 31 Mar 2026 19:56:57 +0000 Message-Id: <69cc2709.23772.7a69904e@gitrepo.freebsd.org> The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=84c68dbb59df81d5371f0d1eea888d30561d428d commit 84c68dbb59df81d5371f0d1eea888d30561d428d Author: Tetsuya Uemura AuthorDate: 2026-03-30 15:24:38 +0000 Commit: Mitchell Horne CommitDate: 2026-03-31 19:54:21 +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 --- 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 cb326a2229a4..2a607748db8d 100644 --- a/sys/conf/files.arm64 +++ b/sys/conf/files.arm64 @@ -629,6 +629,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 \