1 // SPDX-License-Identifier: GPL-2.0
3 * Defines interfaces for interacting with the Raspberry Pi firmware's
6 * Copyright © 2015 Broadcom
9 #include <linux/dma-mapping.h>
10 #include <linux/kref.h>
11 #include <linux/mailbox_client.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/reboot.h>
16 #include <linux/slab.h>
17 #include <soc/bcm2835/raspberrypi-firmware.h>
19 #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
20 #define MBOX_CHAN(msg) ((msg) & 0xf)
21 #define MBOX_DATA28(msg) ((msg) & ~0xf)
22 #define MBOX_CHAN_PROPERTY 8
24 static struct platform_device *rpi_hwmon;
25 static struct platform_device *rpi_clk;
28 struct mbox_client cl;
29 struct mbox_chan *chan; /* The property channel. */
33 struct kref consumers;
37 static struct platform_device *g_pdev;
39 static DEFINE_MUTEX(transaction_lock);
41 static void response_callback(struct mbox_client *cl, void *msg)
43 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
48 * Sends a request to the firmware through the BCM2835 mailbox driver,
49 * and synchronously waits for the reply.
52 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
54 u32 message = MBOX_MSG(chan, data);
59 mutex_lock(&transaction_lock);
60 reinit_completion(&fw->c);
61 ret = mbox_send_message(fw->chan, &message);
63 if (wait_for_completion_timeout(&fw->c, HZ)) {
67 WARN_ONCE(1, "Firmware transaction timeout");
70 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
72 mutex_unlock(&transaction_lock);
78 * rpi_firmware_property_list - Submit firmware property list
79 * @fw: Pointer to firmware structure from rpi_firmware_get().
80 * @data: Buffer holding tags.
81 * @tag_size: Size of tags buffer.
83 * Submits a set of concatenated tags to the VPU firmware through the
84 * mailbox property interface.
86 * The buffer header and the ending tag are added by this function and
87 * don't need to be supplied, just the actual tags for your operation.
88 * See struct rpi_firmware_property_tag_header for the per-tag
91 int rpi_firmware_property_list(struct rpi_firmware *fw,
92 void *data, size_t tag_size)
94 size_t size = tag_size + 12;
99 /* Packets are processed a dword at a time. */
103 buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
108 /* The firmware will error out without parsing in this case. */
109 WARN_ON(size >= 1024 * 1024);
112 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
113 memcpy(&buf[2], data, tag_size);
114 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
117 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
120 memcpy(data, &buf[2], tag_size);
121 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
123 * The tag name here might not be the one causing the
124 * error, if there were multiple tags in the request.
125 * But single-tag is the most common, so go with it.
127 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
132 dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
136 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
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.
145 * Submits a single tag to the VPU firmware through the mailbox
146 * property interface.
148 * This is a convenience wrapper around
149 * rpi_firmware_property_list() to avoid some of the
150 * boilerplate in property calls.
152 int rpi_firmware_property(struct rpi_firmware *fw,
153 u32 tag, void *tag_data, size_t buf_size)
155 struct rpi_firmware_property_tag_header *header;
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.
163 void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
170 header->buf_size = buf_size;
171 header->req_resp_size = 0;
172 memcpy(data + sizeof(*header), tag_data, buf_size);
174 ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
176 memcpy(tag_data, data + sizeof(*header), buf_size);
180 if ((tag == RPI_FIRMWARE_GET_THROTTLED) &&
181 memcmp(&fw->get_throttled, tag_data, sizeof(fw->get_throttled))) {
182 memcpy(&fw->get_throttled, tag_data, sizeof(fw->get_throttled));
183 sysfs_notify(&fw->cl.dev->kobj, NULL, "get_throttled");
188 EXPORT_SYMBOL_GPL(rpi_firmware_property);
190 static int rpi_firmware_notify_reboot(struct notifier_block *nb,
191 unsigned long action,
194 struct rpi_firmware *fw;
195 struct platform_device *pdev = g_pdev;
196 u32 reboot_flags = 0;
201 fw = platform_get_drvdata(pdev);
205 // The partition id is the first parameter followed by zero or
206 // more flags separated by spaces indicating the reason for the reboot.
208 // 'tryboot': Sets a one-shot flag which is cleared upon reboot and
209 // causes the tryboot.txt to be loaded instead of config.txt
210 // by the bootloader and the start.elf firmware.
212 // This is intended to allow automatic fallback to a known
213 // good image if an OS/FW upgrade fails.
215 // N.B. The firmware mechanism for storing reboot flags may vary
216 // on different Raspberry Pi models.
217 if (data && strstr(data, " tryboot"))
220 // The mailbox might have been called earlier, directly via vcmailbox
221 // so only overwrite if reboot flags are passed to the reboot command.
223 (void)rpi_firmware_property(fw, RPI_FIRMWARE_SET_REBOOT_FLAGS,
224 &reboot_flags, sizeof(reboot_flags));
226 (void)rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
231 static ssize_t get_throttled_show(struct device *dev,
232 struct device_attribute *attr, char *buf)
234 struct rpi_firmware *fw = dev_get_drvdata(dev);
236 WARN_ONCE(1, "deprecated, use hwmon sysfs instead\n");
238 return sprintf(buf, "%x\n", fw->get_throttled);
241 static DEVICE_ATTR_RO(get_throttled);
243 static struct attribute *rpi_firmware_dev_attrs[] = {
244 &dev_attr_get_throttled.attr,
248 static const struct attribute_group rpi_firmware_dev_group = {
249 .attrs = rpi_firmware_dev_attrs,
253 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
255 time64_t date_and_time;
257 static const char * const variant_strs[] = {
264 const char *variant_str = "cmd unsupported";
266 int ret = rpi_firmware_property(fw,
267 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
268 &packet, sizeof(packet));
273 /* This is not compatible with y2038 */
274 date_and_time = packet;
276 ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_FIRMWARE_VARIANT,
277 &variant, sizeof(variant));
280 if (variant >= ARRAY_SIZE(variant_strs))
282 variant_str = variant_strs[variant];
286 "Attached to firmware from %ptT, variant %s\n",
287 &date_and_time, variant_str);
291 rpi_firmware_print_firmware_hash(struct rpi_firmware *fw)
294 int ret = rpi_firmware_property(fw,
295 RPI_FIRMWARE_GET_FIRMWARE_HASH,
302 "Firmware hash is %08x%08x%08x%08x%08x\n",
303 hash[0], hash[1], hash[2], hash[3], hash[4]);
307 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
310 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
311 &packet, sizeof(packet));
316 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
319 if (!IS_ERR_OR_NULL(rpi_hwmon)) {
320 if (devm_device_add_group(dev, &rpi_firmware_dev_group))
321 dev_err(dev, "Failed to create get_trottled attr\n");
325 static void rpi_register_clk_driver(struct device *dev)
327 struct device_node *firmware;
330 * Earlier DTs don't have a node for the firmware clocks but
331 * rely on us creating a platform device by hand. If we do
332 * have a node for the firmware clocks, just bail out here.
334 firmware = of_get_compatible_child(dev->of_node,
335 "raspberrypi,firmware-clocks");
337 of_node_put(firmware);
341 rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
345 static void rpi_firmware_delete(struct kref *kref)
347 struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
350 mbox_free_channel(fw->chan);
354 void rpi_firmware_put(struct rpi_firmware *fw)
356 kref_put(&fw->consumers, rpi_firmware_delete);
358 EXPORT_SYMBOL_GPL(rpi_firmware_put);
360 static void devm_rpi_firmware_put(void *data)
362 struct rpi_firmware *fw = data;
364 rpi_firmware_put(fw);
367 static int rpi_firmware_probe(struct platform_device *pdev)
369 struct device *dev = &pdev->dev;
370 struct rpi_firmware *fw;
373 * Memory will be freed by rpi_firmware_delete() once all users have
374 * released their firmware handles. Don't use devm_kzalloc() here.
376 fw = kzalloc(sizeof(*fw), GFP_KERNEL);
381 fw->cl.rx_callback = response_callback;
382 fw->cl.tx_block = true;
384 fw->chan = mbox_request_channel(&fw->cl, 0);
385 if (IS_ERR(fw->chan)) {
386 int ret = PTR_ERR(fw->chan);
387 if (ret != -EPROBE_DEFER)
388 dev_err(dev, "Failed to get mbox channel: %d\n", ret);
392 init_completion(&fw->c);
393 kref_init(&fw->consumers);
395 platform_set_drvdata(pdev, fw);
398 rpi_firmware_print_firmware_revision(fw);
399 rpi_firmware_print_firmware_hash(fw);
400 rpi_register_hwmon_driver(dev, fw);
401 rpi_register_clk_driver(dev);
406 static void rpi_firmware_shutdown(struct platform_device *pdev)
408 struct rpi_firmware *fw = platform_get_drvdata(pdev);
413 rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
416 static int rpi_firmware_remove(struct platform_device *pdev)
418 struct rpi_firmware *fw = platform_get_drvdata(pdev);
420 platform_device_unregister(rpi_hwmon);
422 platform_device_unregister(rpi_clk);
425 rpi_firmware_put(fw);
432 * rpi_firmware_get - Get pointer to rpi_firmware structure.
433 * @firmware_node: Pointer to the firmware Device Tree node.
435 * The reference to rpi_firmware has to be released with rpi_firmware_put().
437 * Returns NULL is the firmware device is not ready.
439 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
441 struct platform_device *pdev = of_find_device_by_node(firmware_node);
442 struct rpi_firmware *fw;
447 fw = platform_get_drvdata(pdev);
451 if (!kref_get_unless_zero(&fw->consumers))
454 put_device(&pdev->dev);
459 put_device(&pdev->dev);
462 EXPORT_SYMBOL_GPL(rpi_firmware_get);
465 * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
466 * @firmware_node: Pointer to the firmware Device Tree node.
468 * Returns NULL is the firmware device is not ready.
470 struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
471 struct device_node *firmware_node)
473 struct rpi_firmware *fw;
475 fw = rpi_firmware_get(firmware_node);
479 if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
484 EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
486 static const struct of_device_id rpi_firmware_of_match[] = {
487 { .compatible = "raspberrypi,bcm2835-firmware", },
490 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
492 static struct platform_driver rpi_firmware_driver = {
494 .name = "raspberrypi-firmware",
495 .of_match_table = rpi_firmware_of_match,
497 .probe = rpi_firmware_probe,
498 .shutdown = rpi_firmware_shutdown,
499 .remove = rpi_firmware_remove,
502 static struct notifier_block rpi_firmware_reboot_notifier = {
503 .notifier_call = rpi_firmware_notify_reboot,
506 static int __init rpi_firmware_init(void)
508 int ret = register_reboot_notifier(&rpi_firmware_reboot_notifier);
511 ret = platform_driver_register(&rpi_firmware_driver);
518 unregister_reboot_notifier(&rpi_firmware_reboot_notifier);
522 core_initcall(rpi_firmware_init);
524 static void __init rpi_firmware_exit(void)
526 platform_driver_unregister(&rpi_firmware_driver);
527 unregister_reboot_notifier(&rpi_firmware_reboot_notifier);
529 module_exit(rpi_firmware_exit);
531 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
532 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
533 MODULE_LICENSE("GPL v2");