Add a driver to talk to the Raspberry Pi firmware
Communicating with the Raspberry Pi firmware is currently handled by each driver calling into the mbox driver, however the device tree is structured such that they should be calling into a firmware driver. Add a driver for this node with an interface to communicate to the firmware via the mbox interface. There is a sysctl to get the firmware revision. This is a unix date so can be parsed with: root@generic:~ # date -j -f '%s' sysctl -n dev.bcm2835_firmware.0.revision Tue Nov 19 16:40:28 UTC 2019 Reviewed by: manu Sponsored by: Innovate UK Differential Revision: https://reviews.freebsd.org/D25572
This commit is contained in:
parent
b6734d8f4a
commit
201a1f34da
181
sys/arm/broadcom/bcm2835/bcm2835_firmware.c
Normal file
181
sys/arm/broadcom/bcm2835/bcm2835_firmware.c
Normal file
@ -0,0 +1,181 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 Andrew Turner
|
||||
*
|
||||
* This work was supported by Innovate UK project 105694, "Digital Security
|
||||
* by Design (DSbD) Technology Platform Prototype".
|
||||
*
|
||||
* 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 <sys/param.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/bus.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#include <dev/ofw/ofw_bus.h>
|
||||
#include <dev/ofw/ofw_bus_subr.h>
|
||||
|
||||
#include <arm/broadcom/bcm2835/bcm2835_firmware.h>
|
||||
#include <arm/broadcom/bcm2835/bcm2835_mbox.h>
|
||||
#include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
|
||||
#include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
|
||||
|
||||
struct bcm2835_firmware_softc {
|
||||
device_t sc_dev;
|
||||
phandle_t sc_mbox;
|
||||
};
|
||||
|
||||
static struct ofw_compat_data compat_data[] = {
|
||||
{"raspberrypi,bcm2835-firmware", 1},
|
||||
{NULL, 0}
|
||||
};
|
||||
|
||||
static int sysctl_bcm2835_firmware_get_revision(SYSCTL_HANDLER_ARGS);
|
||||
|
||||
static int
|
||||
bcm2835_firmware_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, "BCM2835 Firmware");
|
||||
return (BUS_PROBE_DEFAULT);
|
||||
}
|
||||
|
||||
static int
|
||||
bcm2835_firmware_attach(device_t dev)
|
||||
{
|
||||
struct bcm2835_firmware_softc *sc;
|
||||
struct sysctl_ctx_list *ctx;
|
||||
struct sysctl_oid *tree_node;
|
||||
struct sysctl_oid_list *tree;
|
||||
phandle_t node, mbox;
|
||||
int rv;
|
||||
|
||||
sc = device_get_softc(dev);
|
||||
sc->sc_dev = dev;
|
||||
|
||||
node = ofw_bus_get_node(dev);
|
||||
rv = OF_getencprop(node, "mboxes", &mbox, sizeof(mbox));
|
||||
if (rv <= 0) {
|
||||
device_printf(dev, "can't read mboxes property\n");
|
||||
return (ENXIO);
|
||||
}
|
||||
sc->sc_mbox = mbox;
|
||||
|
||||
OF_device_register_xref(OF_xref_from_node(node), dev);
|
||||
|
||||
ctx = device_get_sysctl_ctx(sc->sc_dev);
|
||||
tree_node = device_get_sysctl_tree(sc->sc_dev);
|
||||
tree = SYSCTL_CHILDREN(tree_node);
|
||||
SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "revision",
|
||||
CTLTYPE_UINT | CTLFLAG_RD, sc, sizeof(*sc),
|
||||
sysctl_bcm2835_firmware_get_revision, "IU",
|
||||
"Firmware revision");
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
bcm2835_firmware_property(device_t dev, uint32_t prop, void *data, size_t len)
|
||||
{
|
||||
struct {
|
||||
struct bcm2835_mbox_hdr hdr;
|
||||
struct bcm2835_mbox_tag_hdr tag_hdr;
|
||||
uint32_t data[];
|
||||
} *msg_hdr;
|
||||
size_t msg_len;
|
||||
int err;
|
||||
|
||||
/*
|
||||
* The message is processed in 32-bit chunks so must be a multiple
|
||||
* of 32-bits.
|
||||
*/
|
||||
if ((len & (sizeof(uint32_t) - 1)) != 0)
|
||||
return (EINVAL);
|
||||
|
||||
msg_len = sizeof(*msg_hdr) + len + sizeof(uint32_t);
|
||||
msg_hdr = malloc(sizeof(*msg_hdr) + msg_len + sizeof(uint32_t),
|
||||
M_DEVBUF, M_WAITOK | M_ZERO);
|
||||
|
||||
msg_hdr->hdr.buf_size = msg_len;
|
||||
msg_hdr->hdr.code = BCM2835_MBOX_CODE_REQ;
|
||||
msg_hdr->tag_hdr.tag = prop;
|
||||
msg_hdr->tag_hdr.val_buf_size = len;
|
||||
memcpy(msg_hdr->data, data, len);
|
||||
msg_hdr->data[len / sizeof(uint32_t)] = 0;
|
||||
|
||||
err = bcm2835_mbox_property(msg_hdr, msg_len);
|
||||
if (err == 0) {
|
||||
memcpy(data, msg_hdr->data, len);
|
||||
}
|
||||
|
||||
free(msg_hdr, M_DEVBUF);
|
||||
|
||||
return (err);
|
||||
}
|
||||
|
||||
static int
|
||||
sysctl_bcm2835_firmware_get_revision(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
struct bcm2835_firmware_softc *sc = arg1;
|
||||
uint32_t rev;
|
||||
int err;
|
||||
|
||||
if (bcm2835_firmware_property(sc->sc_dev,
|
||||
BCM2835_MBOX_TAG_FIRMWARE_REVISION, &rev, sizeof(rev)) != 0)
|
||||
return (ENXIO);
|
||||
|
||||
err = sysctl_handle_int(oidp, &rev, sizeof(rev), req);
|
||||
if (err || !req->newptr) /* error || read request */
|
||||
return (err);
|
||||
|
||||
/* write request */
|
||||
return (EINVAL);
|
||||
}
|
||||
|
||||
static device_method_t bcm2835_firmware_methods[] = {
|
||||
/* Device interface */
|
||||
DEVMETHOD(device_probe, bcm2835_firmware_probe),
|
||||
DEVMETHOD(device_attach, bcm2835_firmware_attach),
|
||||
|
||||
DEVMETHOD_END
|
||||
};
|
||||
|
||||
static devclass_t bcm2835_firmware_devclass;
|
||||
static driver_t bcm2835_firmware_driver = {
|
||||
"bcm2835_firmware",
|
||||
bcm2835_firmware_methods,
|
||||
sizeof(struct bcm2835_firmware_softc),
|
||||
};
|
||||
|
||||
DRIVER_MODULE(bcm2835_firmware, simplebus, bcm2835_firmware_driver,
|
||||
bcm2835_firmware_devclass, 0, 0);
|
38
sys/arm/broadcom/bcm2835/bcm2835_firmware.h
Normal file
38
sys/arm/broadcom/bcm2835/bcm2835_firmware.h
Normal file
@ -0,0 +1,38 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 2020 Andrew Turner
|
||||
*
|
||||
* This work was supported by Innovate UK project 105694, "Digital Security
|
||||
* by Design (DSbD) Technology Platform Prototype".
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#ifndef _BCM2835_FIRMWARE_H_
|
||||
#define _BCM2835_FIRMWARE_H_
|
||||
|
||||
int bcm2835_firmware_property(device_t, uint32_t, void *, size_t);
|
||||
|
||||
#endif
|
@ -58,6 +58,10 @@ struct bcm2835_mbox_tag_hdr {
|
||||
(tag_)->tag_hdr.val_len = sizeof((tag_)->body.req); \
|
||||
} while (0)
|
||||
|
||||
#define BCM2835_MBOX_TAG_FIRMWARE_REVISION 0x00000001
|
||||
|
||||
|
||||
|
||||
#define BCM2835_MBOX_POWER_ID_EMMC 0x00000000
|
||||
#define BCM2835_MBOX_POWER_ID_UART0 0x00000001
|
||||
#define BCM2835_MBOX_POWER_ID_UART1 0x00000002
|
||||
|
@ -5,6 +5,7 @@ arm/broadcom/bcm2835/bcm2835_cpufreq.c standard
|
||||
arm/broadcom/bcm2835/bcm2835_dma.c standard
|
||||
arm/broadcom/bcm2835/bcm2835_fb.c optional sc
|
||||
arm/broadcom/bcm2835/bcm2835_fbd.c optional vt
|
||||
arm/broadcom/bcm2835/bcm2835_firmware.c standard
|
||||
arm/broadcom/bcm2835/bcm2835_ft5406.c optional evdev bcm2835_ft5406
|
||||
arm/broadcom/bcm2835/bcm2835_gpio.c optional gpio
|
||||
arm/broadcom/bcm2835/bcm2835_intr.c standard
|
||||
|
@ -92,6 +92,7 @@ arm/broadcom/bcm2835/bcm2835_clkman.c optional soc_brcm_bcm2837 fdt | soc_brcm_
|
||||
arm/broadcom/bcm2835/bcm2835_cpufreq.c optional soc_brcm_bcm2837 fdt | soc_brcm_bcm2838 fdt
|
||||
arm/broadcom/bcm2835/bcm2835_dma.c optional soc_brcm_bcm2837 fdt | soc_brcm_bcm2838 fdt
|
||||
arm/broadcom/bcm2835/bcm2835_fbd.c optional vt soc_brcm_bcm2837 fdt | vt soc_brcm_bcm2838 fdt
|
||||
arm/broadcom/bcm2835/bcm2835_firmware.c optional soc_brcm_bcm2837 fdt | soc_brcm_bcm2838 fdt
|
||||
arm/broadcom/bcm2835/bcm2835_ft5406.c optional evdev bcm2835_ft5406 fdt
|
||||
arm/broadcom/bcm2835/bcm2835_gpio.c optional gpio soc_brcm_bcm2837 fdt | gpio soc_brcm_bcm2838 fdt
|
||||
arm/broadcom/bcm2835/bcm2835_intr.c optional soc_brcm_bcm2837 fdt | soc_brcm_bcm2838 fdt
|
||||
|
Loading…
Reference in New Issue
Block a user