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