1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020 Andrew Turner 5 * 6 * This work was supported by Innovate UK project 105694, "Digital Security 7 * by Design (DSbD) Technology Platform Prototype". 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/bus.h> 37 #include <sys/kernel.h> 38 #include <sys/module.h> 39 #include <sys/sysctl.h> 40 41 #include <dev/fdt/simplebus.h> 42 43 #include <dev/ofw/ofw_bus.h> 44 #include <dev/ofw/ofw_bus_subr.h> 45 46 #include <arm/broadcom/bcm2835/bcm2835_firmware.h> 47 #include <arm/broadcom/bcm2835/bcm2835_mbox.h> 48 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h> 49 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h> 50 51 struct bcm2835_firmware_softc { 52 struct simplebus_softc sc; 53 phandle_t sc_mbox; 54 }; 55 56 static struct ofw_compat_data compat_data[] = { 57 {"raspberrypi,bcm2835-firmware", 1}, 58 {NULL, 0} 59 }; 60 61 static int sysctl_bcm2835_firmware_get_revision(SYSCTL_HANDLER_ARGS); 62 63 static int 64 bcm2835_firmware_probe(device_t dev) 65 { 66 if (!ofw_bus_status_okay(dev)) 67 return (ENXIO); 68 69 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 70 return (ENXIO); 71 72 device_set_desc(dev, "BCM2835 Firmware"); 73 return (BUS_PROBE_DEFAULT); 74 } 75 76 static int 77 bcm2835_firmware_attach(device_t dev) 78 { 79 struct bcm2835_firmware_softc *sc; 80 struct sysctl_ctx_list *ctx; 81 struct sysctl_oid *tree_node; 82 struct sysctl_oid_list *tree; 83 phandle_t node, mbox; 84 int rv; 85 86 sc = device_get_softc(dev); 87 88 node = ofw_bus_get_node(dev); 89 rv = OF_getencprop(node, "mboxes", &mbox, sizeof(mbox)); 90 if (rv <= 0) { 91 device_printf(dev, "can't read mboxes property\n"); 92 return (ENXIO); 93 } 94 sc->sc_mbox = mbox; 95 96 OF_device_register_xref(OF_xref_from_node(node), dev); 97 98 ctx = device_get_sysctl_ctx(dev); 99 tree_node = device_get_sysctl_tree(dev); 100 tree = SYSCTL_CHILDREN(tree_node); 101 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "revision", 102 CTLTYPE_UINT | CTLFLAG_RD, sc, sizeof(*sc), 103 sysctl_bcm2835_firmware_get_revision, "IU", 104 "Firmware revision"); 105 106 /* The firmwaare doesn't have a ranges property */ 107 sc->sc.flags |= SB_FLAG_NO_RANGES; 108 return (simplebus_attach(dev)); 109 } 110 111 int 112 bcm2835_firmware_property(device_t dev, uint32_t prop, void *data, size_t len) 113 { 114 struct { 115 struct bcm2835_mbox_hdr hdr; 116 struct bcm2835_mbox_tag_hdr tag_hdr; 117 uint32_t data[]; 118 } *msg_hdr; 119 size_t msg_len; 120 int err; 121 122 /* 123 * The message is processed in 32-bit chunks so must be a multiple 124 * of 32-bits. 125 */ 126 if ((len & (sizeof(uint32_t) - 1)) != 0) 127 return (EINVAL); 128 129 msg_len = sizeof(*msg_hdr) + len + sizeof(uint32_t); 130 msg_hdr = malloc(sizeof(*msg_hdr) + msg_len + sizeof(uint32_t), 131 M_DEVBUF, M_WAITOK | M_ZERO); 132 133 msg_hdr->hdr.buf_size = msg_len; 134 msg_hdr->hdr.code = BCM2835_MBOX_CODE_REQ; 135 msg_hdr->tag_hdr.tag = prop; 136 msg_hdr->tag_hdr.val_buf_size = len; 137 memcpy(msg_hdr->data, data, len); 138 msg_hdr->data[len / sizeof(uint32_t)] = 0; 139 140 err = bcm2835_mbox_property(msg_hdr, msg_len); 141 if (err == 0) { 142 memcpy(data, msg_hdr->data, len); 143 } 144 145 free(msg_hdr, M_DEVBUF); 146 147 return (err); 148 } 149 150 static int 151 sysctl_bcm2835_firmware_get_revision(SYSCTL_HANDLER_ARGS) 152 { 153 struct bcm2835_firmware_softc *sc = arg1; 154 uint32_t rev; 155 int err; 156 157 if (bcm2835_firmware_property(sc->sc.dev, 158 BCM2835_MBOX_TAG_FIRMWARE_REVISION, &rev, sizeof(rev)) != 0) 159 return (ENXIO); 160 161 err = sysctl_handle_int(oidp, &rev, sizeof(rev), req); 162 if (err || !req->newptr) /* error || read request */ 163 return (err); 164 165 /* write request */ 166 return (EINVAL); 167 } 168 169 static device_method_t bcm2835_firmware_methods[] = { 170 /* Device interface */ 171 DEVMETHOD(device_probe, bcm2835_firmware_probe), 172 DEVMETHOD(device_attach, bcm2835_firmware_attach), 173 174 DEVMETHOD_END 175 }; 176 177 DEFINE_CLASS_1(bcm2835_firmware, bcm2835_firmware_driver, 178 bcm2835_firmware_methods, sizeof(struct bcm2835_firmware_softc), 179 simplebus_driver); 180 181 EARLY_DRIVER_MODULE(bcm2835_firmware, simplebus, bcm2835_firmware_driver, 0, 0, 182 BUS_PASS_BUS + BUS_PASS_ORDER_LATE); 183 MODULE_DEPEND(bcm2835_firmware, mbox, 1, 1, 1); 184