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/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 device_t sc_dev; 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 sc->sc_dev = dev; 86 87 node = ofw_bus_get_node(dev); 88 rv = OF_getencprop(node, "mboxes", &mbox, sizeof(mbox)); 89 if (rv <= 0) { 90 device_printf(dev, "can't read mboxes property\n"); 91 return (ENXIO); 92 } 93 sc->sc_mbox = mbox; 94 95 OF_device_register_xref(OF_xref_from_node(node), dev); 96 97 ctx = device_get_sysctl_ctx(sc->sc_dev); 98 tree_node = device_get_sysctl_tree(sc->sc_dev); 99 tree = SYSCTL_CHILDREN(tree_node); 100 SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "revision", 101 CTLTYPE_UINT | CTLFLAG_RD, sc, sizeof(*sc), 102 sysctl_bcm2835_firmware_get_revision, "IU", 103 "Firmware revision"); 104 return (0); 105 } 106 107 int 108 bcm2835_firmware_property(device_t dev, uint32_t prop, void *data, size_t len) 109 { 110 struct { 111 struct bcm2835_mbox_hdr hdr; 112 struct bcm2835_mbox_tag_hdr tag_hdr; 113 uint32_t data[]; 114 } *msg_hdr; 115 size_t msg_len; 116 int err; 117 118 /* 119 * The message is processed in 32-bit chunks so must be a multiple 120 * of 32-bits. 121 */ 122 if ((len & (sizeof(uint32_t) - 1)) != 0) 123 return (EINVAL); 124 125 msg_len = sizeof(*msg_hdr) + len + sizeof(uint32_t); 126 msg_hdr = malloc(sizeof(*msg_hdr) + msg_len + sizeof(uint32_t), 127 M_DEVBUF, M_WAITOK | M_ZERO); 128 129 msg_hdr->hdr.buf_size = msg_len; 130 msg_hdr->hdr.code = BCM2835_MBOX_CODE_REQ; 131 msg_hdr->tag_hdr.tag = prop; 132 msg_hdr->tag_hdr.val_buf_size = len; 133 memcpy(msg_hdr->data, data, len); 134 msg_hdr->data[len / sizeof(uint32_t)] = 0; 135 136 err = bcm2835_mbox_property(msg_hdr, msg_len); 137 if (err == 0) { 138 memcpy(data, msg_hdr->data, len); 139 } 140 141 free(msg_hdr, M_DEVBUF); 142 143 return (err); 144 } 145 146 static int 147 sysctl_bcm2835_firmware_get_revision(SYSCTL_HANDLER_ARGS) 148 { 149 struct bcm2835_firmware_softc *sc = arg1; 150 uint32_t rev; 151 int err; 152 153 if (bcm2835_firmware_property(sc->sc_dev, 154 BCM2835_MBOX_TAG_FIRMWARE_REVISION, &rev, sizeof(rev)) != 0) 155 return (ENXIO); 156 157 err = sysctl_handle_int(oidp, &rev, sizeof(rev), req); 158 if (err || !req->newptr) /* error || read request */ 159 return (err); 160 161 /* write request */ 162 return (EINVAL); 163 } 164 165 static device_method_t bcm2835_firmware_methods[] = { 166 /* Device interface */ 167 DEVMETHOD(device_probe, bcm2835_firmware_probe), 168 DEVMETHOD(device_attach, bcm2835_firmware_attach), 169 170 DEVMETHOD_END 171 }; 172 173 static devclass_t bcm2835_firmware_devclass; 174 static driver_t bcm2835_firmware_driver = { 175 "bcm2835_firmware", 176 bcm2835_firmware_methods, 177 sizeof(struct bcm2835_firmware_softc), 178 }; 179 180 DRIVER_MODULE(bcm2835_firmware, simplebus, bcm2835_firmware_driver, 181 bcm2835_firmware_devclass, 0, 0); 182