1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Defines interfaces for interacting wtih the Raspberry Pi firmware's
4  * property channel.
5  *
6  * Copyright © 2015 Broadcom
7  */
8 
9 #include <linux/dma-mapping.h>
10 #include <linux/mailbox_client.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <soc/bcm2835/raspberrypi-firmware.h>
16 
17 #define MBOX_MSG(chan, data28)		(((data28) & ~0xf) | ((chan) & 0xf))
18 #define MBOX_CHAN(msg)			((msg) & 0xf)
19 #define MBOX_DATA28(msg)		((msg) & ~0xf)
20 #define MBOX_CHAN_PROPERTY		8
21 
22 static struct platform_device *rpi_hwmon;
23 static struct platform_device *rpi_clk;
24 
25 struct rpi_firmware {
26 	struct mbox_client cl;
27 	struct mbox_chan *chan; /* The property channel. */
28 	struct completion c;
29 	u32 enabled;
30 };
31 
32 static DEFINE_MUTEX(transaction_lock);
33 
34 static void response_callback(struct mbox_client *cl, void *msg)
35 {
36 	struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
37 	complete(&fw->c);
38 }
39 
40 /*
41  * Sends a request to the firmware through the BCM2835 mailbox driver,
42  * and synchronously waits for the reply.
43  */
44 static int
45 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
46 {
47 	u32 message = MBOX_MSG(chan, data);
48 	int ret;
49 
50 	WARN_ON(data & 0xf);
51 
52 	mutex_lock(&transaction_lock);
53 	reinit_completion(&fw->c);
54 	ret = mbox_send_message(fw->chan, &message);
55 	if (ret >= 0) {
56 		if (wait_for_completion_timeout(&fw->c, HZ)) {
57 			ret = 0;
58 		} else {
59 			ret = -ETIMEDOUT;
60 			WARN_ONCE(1, "Firmware transaction timeout");
61 		}
62 	} else {
63 		dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
64 	}
65 	mutex_unlock(&transaction_lock);
66 
67 	return ret;
68 }
69 
70 /**
71  * rpi_firmware_property_list - Submit firmware property list
72  * @fw:		Pointer to firmware structure from rpi_firmware_get().
73  * @data:	Buffer holding tags.
74  * @tag_size:	Size of tags buffer.
75  *
76  * Submits a set of concatenated tags to the VPU firmware through the
77  * mailbox property interface.
78  *
79  * The buffer header and the ending tag are added by this function and
80  * don't need to be supplied, just the actual tags for your operation.
81  * See struct rpi_firmware_property_tag_header for the per-tag
82  * structure.
83  */
84 int rpi_firmware_property_list(struct rpi_firmware *fw,
85 			       void *data, size_t tag_size)
86 {
87 	size_t size = tag_size + 12;
88 	u32 *buf;
89 	dma_addr_t bus_addr;
90 	int ret;
91 
92 	/* Packets are processed a dword at a time. */
93 	if (size & 3)
94 		return -EINVAL;
95 
96 	buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
97 				 GFP_ATOMIC);
98 	if (!buf)
99 		return -ENOMEM;
100 
101 	/* The firmware will error out without parsing in this case. */
102 	WARN_ON(size >= 1024 * 1024);
103 
104 	buf[0] = size;
105 	buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
106 	memcpy(&buf[2], data, tag_size);
107 	buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
108 	wmb();
109 
110 	ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
111 
112 	rmb();
113 	memcpy(data, &buf[2], tag_size);
114 	if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
115 		/*
116 		 * The tag name here might not be the one causing the
117 		 * error, if there were multiple tags in the request.
118 		 * But single-tag is the most common, so go with it.
119 		 */
120 		dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
121 			buf[2], buf[1]);
122 		ret = -EINVAL;
123 	}
124 
125 	dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
126 
127 	return ret;
128 }
129 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
130 
131 /**
132  * rpi_firmware_property - Submit single firmware property
133  * @fw:		Pointer to firmware structure from rpi_firmware_get().
134  * @tag:	One of enum_mbox_property_tag.
135  * @tag_data:	Tag data buffer.
136  * @buf_size:	Buffer size.
137  *
138  * Submits a single tag to the VPU firmware through the mailbox
139  * property interface.
140  *
141  * This is a convenience wrapper around
142  * rpi_firmware_property_list() to avoid some of the
143  * boilerplate in property calls.
144  */
145 int rpi_firmware_property(struct rpi_firmware *fw,
146 			  u32 tag, void *tag_data, size_t buf_size)
147 {
148 	struct rpi_firmware_property_tag_header *header;
149 	int ret;
150 
151 	/* Some mailboxes can use over 1k bytes. Rather than checking
152 	 * size and using stack or kmalloc depending on requirements,
153 	 * just use kmalloc. Mailboxes don't get called enough to worry
154 	 * too much about the time taken in the allocation.
155 	 */
156 	void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
157 
158 	if (!data)
159 		return -ENOMEM;
160 
161 	header = data;
162 	header->tag = tag;
163 	header->buf_size = buf_size;
164 	header->req_resp_size = 0;
165 	memcpy(data + sizeof(*header), tag_data, buf_size);
166 
167 	ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
168 
169 	memcpy(tag_data, data + sizeof(*header), buf_size);
170 
171 	kfree(data);
172 
173 	return ret;
174 }
175 EXPORT_SYMBOL_GPL(rpi_firmware_property);
176 
177 static void
178 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
179 {
180 	time64_t date_and_time;
181 	u32 packet;
182 	int ret = rpi_firmware_property(fw,
183 					RPI_FIRMWARE_GET_FIRMWARE_REVISION,
184 					&packet, sizeof(packet));
185 
186 	if (ret)
187 		return;
188 
189 	/* This is not compatible with y2038 */
190 	date_and_time = packet;
191 	dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
192 }
193 
194 static void
195 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
196 {
197 	u32 packet;
198 	int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
199 					&packet, sizeof(packet));
200 
201 	if (ret)
202 		return;
203 
204 	rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
205 						  -1, NULL, 0);
206 }
207 
208 static void rpi_register_clk_driver(struct device *dev)
209 {
210 	struct device_node *firmware;
211 
212 	/*
213 	 * Earlier DTs don't have a node for the firmware clocks but
214 	 * rely on us creating a platform device by hand. If we do
215 	 * have a node for the firmware clocks, just bail out here.
216 	 */
217 	firmware = of_get_compatible_child(dev->of_node,
218 					   "raspberrypi,firmware-clocks");
219 	if (firmware) {
220 		of_node_put(firmware);
221 		return;
222 	}
223 
224 	rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
225 						-1, NULL, 0);
226 }
227 
228 static int rpi_firmware_probe(struct platform_device *pdev)
229 {
230 	struct device *dev = &pdev->dev;
231 	struct rpi_firmware *fw;
232 
233 	fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
234 	if (!fw)
235 		return -ENOMEM;
236 
237 	fw->cl.dev = dev;
238 	fw->cl.rx_callback = response_callback;
239 	fw->cl.tx_block = true;
240 
241 	fw->chan = mbox_request_channel(&fw->cl, 0);
242 	if (IS_ERR(fw->chan)) {
243 		int ret = PTR_ERR(fw->chan);
244 		if (ret != -EPROBE_DEFER)
245 			dev_err(dev, "Failed to get mbox channel: %d\n", ret);
246 		return ret;
247 	}
248 
249 	init_completion(&fw->c);
250 
251 	platform_set_drvdata(pdev, fw);
252 
253 	rpi_firmware_print_firmware_revision(fw);
254 	rpi_register_hwmon_driver(dev, fw);
255 	rpi_register_clk_driver(dev);
256 
257 	return 0;
258 }
259 
260 static void rpi_firmware_shutdown(struct platform_device *pdev)
261 {
262 	struct rpi_firmware *fw = platform_get_drvdata(pdev);
263 
264 	if (!fw)
265 		return;
266 
267 	rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
268 }
269 
270 static int rpi_firmware_remove(struct platform_device *pdev)
271 {
272 	struct rpi_firmware *fw = platform_get_drvdata(pdev);
273 
274 	platform_device_unregister(rpi_hwmon);
275 	rpi_hwmon = NULL;
276 	platform_device_unregister(rpi_clk);
277 	rpi_clk = NULL;
278 	mbox_free_channel(fw->chan);
279 
280 	return 0;
281 }
282 
283 /**
284  * rpi_firmware_get - Get pointer to rpi_firmware structure.
285  * @firmware_node:    Pointer to the firmware Device Tree node.
286  *
287  * Returns NULL is the firmware device is not ready.
288  */
289 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
290 {
291 	struct platform_device *pdev = of_find_device_by_node(firmware_node);
292 
293 	if (!pdev)
294 		return NULL;
295 
296 	return platform_get_drvdata(pdev);
297 }
298 EXPORT_SYMBOL_GPL(rpi_firmware_get);
299 
300 static const struct of_device_id rpi_firmware_of_match[] = {
301 	{ .compatible = "raspberrypi,bcm2835-firmware", },
302 	{},
303 };
304 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
305 
306 static struct platform_driver rpi_firmware_driver = {
307 	.driver = {
308 		.name = "raspberrypi-firmware",
309 		.of_match_table = rpi_firmware_of_match,
310 	},
311 	.probe		= rpi_firmware_probe,
312 	.shutdown	= rpi_firmware_shutdown,
313 	.remove		= rpi_firmware_remove,
314 };
315 module_platform_driver(rpi_firmware_driver);
316 
317 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
318 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
319 MODULE_LICENSE("GPL v2");
320