xref: /linux/drivers/firmware/raspberrypi.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Defines interfaces for interacting with the Raspberry Pi firmware's
4  * property channel.
5  *
6  * Copyright © 2015 Broadcom
7  */
8 
9 #include <linux/dma-mapping.h>
10 #include <linux/kref.h>
11 #include <linux/mailbox_client.h>
12 #include <linux/mailbox_controller.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <soc/bcm2835/raspberrypi-firmware.h>
19 
20 #define MBOX_MSG(chan, data28)		(((data28) & ~0xf) | ((chan) & 0xf))
21 #define MBOX_CHAN(msg)			((msg) & 0xf)
22 #define MBOX_DATA28(msg)		((msg) & ~0xf)
23 #define MBOX_CHAN_PROPERTY		8
24 
25 static struct platform_device *rpi_hwmon;
26 static struct platform_device *rpi_clk;
27 static struct platform_device *rpi_rtc;
28 
29 struct rpi_firmware {
30 	struct mbox_client cl;
31 	struct mbox_chan *chan; /* The property channel. */
32 	struct completion c;
33 	u32 enabled;
34 
35 	struct kref consumers;
36 };
37 
38 static DEFINE_MUTEX(transaction_lock);
39 
40 static void response_callback(struct mbox_client *cl, void *msg)
41 {
42 	struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
43 	complete(&fw->c);
44 }
45 
46 /*
47  * Sends a request to the firmware through the BCM2835 mailbox driver,
48  * and synchronously waits for the reply.
49  */
50 static int
51 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
52 {
53 	u32 message = MBOX_MSG(chan, data);
54 	int ret;
55 
56 	WARN_ON(data & 0xf);
57 
58 	mutex_lock(&transaction_lock);
59 	reinit_completion(&fw->c);
60 	ret = mbox_send_message(fw->chan, &message);
61 	if (ret >= 0) {
62 		if (wait_for_completion_timeout(&fw->c, HZ)) {
63 			ret = 0;
64 		} else {
65 			ret = -ETIMEDOUT;
66 		}
67 	} else {
68 		dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
69 	}
70 	mutex_unlock(&transaction_lock);
71 
72 	return ret;
73 }
74 
75 /**
76  * rpi_firmware_property_list - Submit firmware property list
77  * @fw:		Pointer to firmware structure from rpi_firmware_get().
78  * @data:	Buffer holding tags.
79  * @tag_size:	Size of tags buffer.
80  *
81  * Submits a set of concatenated tags to the VPU firmware through the
82  * mailbox property interface.
83  *
84  * The buffer header and the ending tag are added by this function and
85  * don't need to be supplied, just the actual tags for your operation.
86  * See struct rpi_firmware_property_tag_header for the per-tag
87  * structure.
88  */
89 int rpi_firmware_property_list(struct rpi_firmware *fw,
90 			       void *data, size_t tag_size)
91 {
92 	size_t size = tag_size + 12;
93 	u32 *buf;
94 	dma_addr_t bus_addr;
95 	int ret;
96 
97 	/* Packets are processed a dword at a time. */
98 	if (size & 3)
99 		return -EINVAL;
100 
101 	buf = dma_alloc_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size),
102 				 &bus_addr, GFP_ATOMIC);
103 	if (!buf)
104 		return -ENOMEM;
105 
106 	/* The firmware will error out without parsing in this case. */
107 	WARN_ON(size >= 1024 * 1024);
108 
109 	buf[0] = size;
110 	buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
111 	memcpy(&buf[2], data, tag_size);
112 	buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
113 	wmb();
114 
115 	ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
116 
117 	rmb();
118 	memcpy(data, &buf[2], tag_size);
119 	if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
120 		/*
121 		 * The tag name here might not be the one causing the
122 		 * error, if there were multiple tags in the request.
123 		 * But single-tag is the most common, so go with it.
124 		 */
125 		dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
126 			buf[2], buf[1]);
127 		ret = -EINVAL;
128 	} else if (ret == -ETIMEDOUT) {
129 		WARN_ONCE(1, "Firmware transaction 0x%08x timeout", buf[2]);
130 	}
131 
132 	dma_free_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size), buf, bus_addr);
133 
134 	return ret;
135 }
136 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
137 
138 /**
139  * rpi_firmware_property - Submit single firmware property
140  * @fw:		Pointer to firmware structure from rpi_firmware_get().
141  * @tag:	One of enum_mbox_property_tag.
142  * @tag_data:	Tag data buffer.
143  * @buf_size:	Buffer size.
144  *
145  * Submits a single tag to the VPU firmware through the mailbox
146  * property interface.
147  *
148  * This is a convenience wrapper around
149  * rpi_firmware_property_list() to avoid some of the
150  * boilerplate in property calls.
151  */
152 int rpi_firmware_property(struct rpi_firmware *fw,
153 			  u32 tag, void *tag_data, size_t buf_size)
154 {
155 	struct rpi_firmware_property_tag_header *header;
156 	int ret;
157 
158 	/* Some mailboxes can use over 1k bytes. Rather than checking
159 	 * size and using stack or kmalloc depending on requirements,
160 	 * just use kmalloc. Mailboxes don't get called enough to worry
161 	 * too much about the time taken in the allocation.
162 	 */
163 	void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
164 
165 	if (!data)
166 		return -ENOMEM;
167 
168 	header = data;
169 	header->tag = tag;
170 	header->buf_size = buf_size;
171 	header->req_resp_size = 0;
172 	memcpy(data + sizeof(*header), tag_data, buf_size);
173 
174 	ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
175 
176 	memcpy(tag_data, data + sizeof(*header), buf_size);
177 
178 	kfree(data);
179 
180 	return ret;
181 }
182 EXPORT_SYMBOL_GPL(rpi_firmware_property);
183 
184 static void
185 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
186 {
187 	time64_t date_and_time;
188 	u32 packet;
189 	int ret = rpi_firmware_property(fw,
190 					RPI_FIRMWARE_GET_FIRMWARE_REVISION,
191 					&packet, sizeof(packet));
192 
193 	if (ret)
194 		return;
195 
196 	/* This is not compatible with y2038 */
197 	date_and_time = packet;
198 	dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
199 }
200 
201 static void
202 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
203 {
204 	u32 packet;
205 	int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
206 					&packet, sizeof(packet));
207 
208 	if (ret)
209 		return;
210 
211 	rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
212 						  -1, NULL, 0);
213 }
214 
215 static void rpi_register_clk_driver(struct device *dev)
216 {
217 	struct device_node *firmware;
218 
219 	/*
220 	 * Earlier DTs don't have a node for the firmware clocks but
221 	 * rely on us creating a platform device by hand. If we do
222 	 * have a node for the firmware clocks, just bail out here.
223 	 */
224 	firmware = of_get_compatible_child(dev->of_node,
225 					   "raspberrypi,firmware-clocks");
226 	if (firmware) {
227 		of_node_put(firmware);
228 		return;
229 	}
230 
231 	rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
232 						-1, NULL, 0);
233 }
234 
235 static void rpi_register_rtc_driver(struct device *dev)
236 {
237 	struct platform_device *pdev;
238 
239 	/*
240 	 * Check if our model of Raspberry Pi board is from the 5th gen
241 	 * as the RTC is only present on that model.
242 	 */
243 	if (!of_machine_is_compatible("brcm,bcm2712"))
244 		return;
245 
246 	pdev = platform_device_register_data(dev, "raspberrypi-rtc",
247 					     -1, NULL, 0);
248 
249 	if (IS_ERR(pdev))
250 		return;
251 
252 	rpi_rtc = pdev;
253 }
254 
255 unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
256 {
257 	struct rpi_firmware_clk_rate_request msg =
258 		RPI_FIRMWARE_CLK_RATE_REQUEST(id);
259 	int ret;
260 
261 	ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
262 				    &msg, sizeof(msg));
263 	if (ret)
264 		/*
265 		 * If our firmware doesn't support that operation, or fails, we
266 		 * assume the maximum clock rate is absolute maximum we can
267 		 * store over our type.
268 		 */
269 		 return UINT_MAX;
270 
271 	return le32_to_cpu(msg.rate);
272 }
273 EXPORT_SYMBOL_GPL(rpi_firmware_clk_get_max_rate);
274 
275 static void rpi_firmware_delete(struct kref *kref)
276 {
277 	struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
278 					       consumers);
279 
280 	mbox_free_channel(fw->chan);
281 	kfree(fw);
282 }
283 
284 void rpi_firmware_put(struct rpi_firmware *fw)
285 {
286 	kref_put(&fw->consumers, rpi_firmware_delete);
287 }
288 EXPORT_SYMBOL_GPL(rpi_firmware_put);
289 
290 static void devm_rpi_firmware_put(void *data)
291 {
292 	struct rpi_firmware *fw = data;
293 
294 	rpi_firmware_put(fw);
295 }
296 
297 static int rpi_firmware_probe(struct platform_device *pdev)
298 {
299 	struct device *dev = &pdev->dev;
300 	struct rpi_firmware *fw;
301 
302 	/*
303 	 * Memory will be freed by rpi_firmware_delete() once all users have
304 	 * released their firmware handles. Don't use devm_kzalloc() here.
305 	 */
306 	fw = kzalloc_obj(*fw);
307 	if (!fw)
308 		return -ENOMEM;
309 
310 	fw->cl.dev = dev;
311 	fw->cl.rx_callback = response_callback;
312 	fw->cl.tx_block = true;
313 
314 	fw->chan = mbox_request_channel(&fw->cl, 0);
315 	if (IS_ERR(fw->chan)) {
316 		int ret = PTR_ERR(fw->chan);
317 		kfree(fw);
318 		return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
319 	}
320 
321 	init_completion(&fw->c);
322 	kref_init(&fw->consumers);
323 
324 	platform_set_drvdata(pdev, fw);
325 
326 	rpi_firmware_print_firmware_revision(fw);
327 	rpi_register_hwmon_driver(dev, fw);
328 	rpi_register_clk_driver(dev);
329 	rpi_register_rtc_driver(dev);
330 
331 	return 0;
332 }
333 
334 static void rpi_firmware_shutdown(struct platform_device *pdev)
335 {
336 	struct rpi_firmware *fw = platform_get_drvdata(pdev);
337 
338 	if (!fw)
339 		return;
340 
341 	rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
342 }
343 
344 static void rpi_firmware_remove(struct platform_device *pdev)
345 {
346 	struct rpi_firmware *fw = platform_get_drvdata(pdev);
347 
348 	platform_device_unregister(rpi_hwmon);
349 	rpi_hwmon = NULL;
350 	platform_device_unregister(rpi_clk);
351 	rpi_clk = NULL;
352 	platform_device_unregister(rpi_rtc);
353 	rpi_rtc = NULL;
354 
355 	rpi_firmware_put(fw);
356 }
357 
358 static const struct of_device_id rpi_firmware_of_match[] = {
359 	{ .compatible = "raspberrypi,bcm2835-firmware", },
360 	{},
361 };
362 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
363 
364 struct device_node *rpi_firmware_find_node(void)
365 {
366 	return of_find_matching_node(NULL, rpi_firmware_of_match);
367 }
368 EXPORT_SYMBOL_GPL(rpi_firmware_find_node);
369 
370 /**
371  * rpi_firmware_get - Get pointer to rpi_firmware structure.
372  * @firmware_node:    Pointer to the firmware Device Tree node.
373  *
374  * The reference to rpi_firmware has to be released with rpi_firmware_put().
375  *
376  * Returns NULL is the firmware device is not ready.
377  */
378 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
379 {
380 	struct platform_device *pdev = of_find_device_by_node(firmware_node);
381 	struct rpi_firmware *fw;
382 
383 	if (!pdev)
384 		return NULL;
385 
386 	fw = platform_get_drvdata(pdev);
387 	if (!fw)
388 		goto err_put_device;
389 
390 	if (!kref_get_unless_zero(&fw->consumers))
391 		goto err_put_device;
392 
393 	put_device(&pdev->dev);
394 
395 	return fw;
396 
397 err_put_device:
398 	put_device(&pdev->dev);
399 	return NULL;
400 }
401 EXPORT_SYMBOL_GPL(rpi_firmware_get);
402 
403 /**
404  * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
405  * @dev:              The firmware device structure
406  * @firmware_node:    Pointer to the firmware Device Tree node.
407  *
408  * Returns NULL is the firmware device is not ready.
409  */
410 struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
411 					   struct device_node *firmware_node)
412 {
413 	struct rpi_firmware *fw;
414 
415 	fw = rpi_firmware_get(firmware_node);
416 	if (!fw)
417 		return NULL;
418 
419 	if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
420 		return NULL;
421 
422 	return fw;
423 }
424 EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
425 
426 static struct platform_driver rpi_firmware_driver = {
427 	.driver = {
428 		.name = "raspberrypi-firmware",
429 		.of_match_table = rpi_firmware_of_match,
430 	},
431 	.probe		= rpi_firmware_probe,
432 	.shutdown	= rpi_firmware_shutdown,
433 	.remove		= rpi_firmware_remove,
434 };
435 module_platform_driver(rpi_firmware_driver);
436 
437 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
438 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
439 MODULE_LICENSE("GPL v2");
440