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 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/sysctl.h> 38 39 #include <dev/fdt/simplebus.h> 40 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include <arm/broadcom/bcm2835/bcm2835_firmware.h> 45 #include <arm/broadcom/bcm2835/bcm2835_mbox.h> 46 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h> 47 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h> 48 49 struct bcm2835_firmware_softc { 50 struct simplebus_softc sc; 51 phandle_t sc_mbox; 52 }; 53 54 static struct ofw_compat_data compat_data[] = { 55 {"raspberrypi,bcm2835-firmware", 1}, 56 {NULL, 0} 57 }; 58 59 static int sysctl_bcm2835_firmware_get_revision(SYSCTL_HANDLER_ARGS); 60 61 static int 62 bcm2835_firmware_probe(device_t dev) 63 { 64 if (!ofw_bus_status_okay(dev)) 65 return (ENXIO); 66 67 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 68 return (ENXIO); 69 70 device_set_desc(dev, "BCM2835 Firmware"); 71 return (BUS_PROBE_DEFAULT); 72 } 73 74 static int 75 bcm2835_firmware_attach(device_t dev) 76 { 77 struct bcm2835_firmware_softc *sc; 78 struct sysctl_ctx_list *ctx; 79 struct sysctl_oid *tree_node; 80 struct sysctl_oid_list *tree; 81 phandle_t node, mbox; 82 int rv; 83 84 sc = device_get_softc(dev); 85 86 node = ofw_bus_get_node(dev); 87 rv = OF_getencprop(node, "mboxes", &mbox, sizeof(mbox)); 88 if (rv <= 0) { 89 device_printf(dev, "can't read mboxes property\n"); 90 return (ENXIO); 91 } 92 sc->sc_mbox = mbox; 93 94 OF_device_register_xref(OF_xref_from_node(node), dev); 95 96 ctx = device_get_sysctl_ctx(dev); 97 tree_node = device_get_sysctl_tree(dev); 98 tree = SYSCTL_CHILDREN(tree_node); 99 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "revision", 100 CTLTYPE_UINT | CTLFLAG_RD, sc, sizeof(*sc), 101 sysctl_bcm2835_firmware_get_revision, "IU", 102 "Firmware revision"); 103 104 /* The firmwaare doesn't have a ranges property */ 105 sc->sc.flags |= SB_FLAG_NO_RANGES; 106 return (simplebus_attach(dev)); 107 } 108 109 int 110 bcm2835_firmware_property(device_t dev, uint32_t prop, void *data, size_t len) 111 { 112 struct { 113 struct bcm2835_mbox_hdr hdr; 114 struct bcm2835_mbox_tag_hdr tag_hdr; 115 uint32_t data[]; 116 } *msg_hdr; 117 size_t msg_len; 118 int err; 119 120 /* 121 * The message is processed in 32-bit chunks so must be a multiple 122 * of 32-bits. 123 */ 124 if ((len & (sizeof(uint32_t) - 1)) != 0) 125 return (EINVAL); 126 127 msg_len = sizeof(*msg_hdr) + len + sizeof(uint32_t); 128 msg_hdr = malloc(sizeof(*msg_hdr) + msg_len + sizeof(uint32_t), 129 M_DEVBUF, M_WAITOK | M_ZERO); 130 131 msg_hdr->hdr.buf_size = msg_len; 132 msg_hdr->hdr.code = BCM2835_MBOX_CODE_REQ; 133 msg_hdr->tag_hdr.tag = prop; 134 msg_hdr->tag_hdr.val_buf_size = len; 135 memcpy(msg_hdr->data, data, len); 136 msg_hdr->data[len / sizeof(uint32_t)] = 0; 137 138 err = bcm2835_mbox_property(msg_hdr, msg_len); 139 if (err == 0) { 140 memcpy(data, msg_hdr->data, len); 141 } 142 143 free(msg_hdr, M_DEVBUF); 144 145 return (err); 146 } 147 148 static int 149 sysctl_bcm2835_firmware_get_revision(SYSCTL_HANDLER_ARGS) 150 { 151 struct bcm2835_firmware_softc *sc = arg1; 152 uint32_t rev; 153 int err; 154 155 if (bcm2835_firmware_property(sc->sc.dev, 156 BCM2835_MBOX_TAG_FIRMWARE_REVISION, &rev, sizeof(rev)) != 0) 157 return (ENXIO); 158 159 err = sysctl_handle_int(oidp, &rev, sizeof(rev), req); 160 if (err || !req->newptr) /* error || read request */ 161 return (err); 162 163 /* write request */ 164 return (EINVAL); 165 } 166 167 static device_method_t bcm2835_firmware_methods[] = { 168 /* Device interface */ 169 DEVMETHOD(device_probe, bcm2835_firmware_probe), 170 DEVMETHOD(device_attach, bcm2835_firmware_attach), 171 172 DEVMETHOD_END 173 }; 174 175 DEFINE_CLASS_1(bcm2835_firmware, bcm2835_firmware_driver, 176 bcm2835_firmware_methods, sizeof(struct bcm2835_firmware_softc), 177 simplebus_driver); 178 179 EARLY_DRIVER_MODULE(bcm2835_firmware, simplebus, bcm2835_firmware_driver, 0, 0, 180 BUS_PASS_BUS + BUS_PASS_ORDER_LATE); 181 MODULE_DEPEND(bcm2835_firmware, mbox, 1, 1, 1); 182