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>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/reboot.h>
17 #include <linux/slab.h>
18 #include <soc/bcm2835/raspberrypi-firmware.h>
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
25 static struct platform_device *rpi_hwmon;
26 static struct platform_device *rpi_clk;
29 struct mbox_client cl;
30 struct mbox_chan *chan; /* The property channel. */
34 struct kref consumers;
38 static struct platform_device *g_pdev;
40 static DEFINE_MUTEX(transaction_lock);
42 static void response_callback(struct mbox_client *cl, void *msg)
44 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
49 * Sends a request to the firmware through the BCM2835 mailbox driver,
50 * and synchronously waits for the reply.
53 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
55 u32 message = MBOX_MSG(chan, data);
60 mutex_lock(&transaction_lock);
61 reinit_completion(&fw->c);
62 ret = mbox_send_message(fw->chan, &message);
64 if (wait_for_completion_timeout(&fw->c, HZ)) {
68 WARN_ONCE(1, "Firmware transaction timeout");
71 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
73 mutex_unlock(&transaction_lock);
79 * rpi_firmware_property_list - Submit firmware property list
80 * @fw: Pointer to firmware structure from rpi_firmware_get().
81 * @data: Buffer holding tags.
82 * @tag_size: Size of tags buffer.
84 * Submits a set of concatenated tags to the VPU firmware through the
85 * mailbox property interface.
87 * The buffer header and the ending tag are added by this function and
88 * don't need to be supplied, just the actual tags for your operation.
89 * See struct rpi_firmware_property_tag_header for the per-tag
92 int rpi_firmware_property_list(struct rpi_firmware *fw,
93 void *data, size_t tag_size)
95 size_t size = tag_size + 12;
100 /* Packets are processed a dword at a time. */
104 buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
109 /* The firmware will error out without parsing in this case. */
110 WARN_ON(size >= 1024 * 1024);
113 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
114 memcpy(&buf[2], data, tag_size);
115 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
118 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
121 memcpy(data, &buf[2], tag_size);
122 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
124 * The tag name here might not be the one causing the
125 * error, if there were multiple tags in the request.
126 * But single-tag is the most common, so go with it.
128 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
133 dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
137 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
140 * rpi_firmware_property - Submit single firmware property
141 * @fw: Pointer to firmware structure from rpi_firmware_get().
142 * @tag: One of enum_mbox_property_tag.
143 * @tag_data: Tag data buffer.
144 * @buf_size: Buffer size.
146 * Submits a single tag to the VPU firmware through the mailbox
147 * property interface.
149 * This is a convenience wrapper around
150 * rpi_firmware_property_list() to avoid some of the
151 * boilerplate in property calls.
153 int rpi_firmware_property(struct rpi_firmware *fw,
154 u32 tag, void *tag_data, size_t buf_size)
156 struct rpi_firmware_property_tag_header *header;
159 /* Some mailboxes can use over 1k bytes. Rather than checking
160 * size and using stack or kmalloc depending on requirements,
161 * just use kmalloc. Mailboxes don't get called enough to worry
162 * too much about the time taken in the allocation.
164 void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
171 header->buf_size = buf_size;
172 header->req_resp_size = 0;
173 memcpy(data + sizeof(*header), tag_data, buf_size);
175 ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
177 memcpy(tag_data, data + sizeof(*header), buf_size);
181 if ((tag == RPI_FIRMWARE_GET_THROTTLED) &&
182 memcmp(&fw->get_throttled, tag_data, sizeof(fw->get_throttled))) {
183 memcpy(&fw->get_throttled, tag_data, sizeof(fw->get_throttled));
184 sysfs_notify(&fw->cl.dev->kobj, NULL, "get_throttled");
189 EXPORT_SYMBOL_GPL(rpi_firmware_property);
191 static int rpi_firmware_notify_reboot(struct notifier_block *nb,
192 unsigned long action,
195 struct rpi_firmware *fw;
196 struct platform_device *pdev = g_pdev;
197 u32 reboot_flags = 0;
202 fw = platform_get_drvdata(pdev);
206 // The partition id is the first parameter followed by zero or
207 // more flags separated by spaces indicating the reason for the reboot.
209 // 'tryboot': Sets a one-shot flag which is cleared upon reboot and
210 // causes the tryboot.txt to be loaded instead of config.txt
211 // by the bootloader and the start.elf firmware.
213 // This is intended to allow automatic fallback to a known
214 // good image if an OS/FW upgrade fails.
216 // N.B. The firmware mechanism for storing reboot flags may vary
217 // on different Raspberry Pi models.
218 if (data && strstr(data, " tryboot"))
221 // The mailbox might have been called earlier, directly via vcmailbox
222 // so only overwrite if reboot flags are passed to the reboot command.
224 (void)rpi_firmware_property(fw, RPI_FIRMWARE_SET_REBOOT_FLAGS,
225 &reboot_flags, sizeof(reboot_flags));
227 (void)rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
232 static ssize_t get_throttled_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
235 struct rpi_firmware *fw = dev_get_drvdata(dev);
237 WARN_ONCE(1, "deprecated, use hwmon sysfs instead\n");
239 return sprintf(buf, "%x\n", fw->get_throttled);
242 static DEVICE_ATTR_RO(get_throttled);
244 static struct attribute *rpi_firmware_dev_attrs[] = {
245 &dev_attr_get_throttled.attr,
249 static const struct attribute_group rpi_firmware_dev_group = {
250 .attrs = rpi_firmware_dev_attrs,
254 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
256 time64_t date_and_time;
258 static const char * const variant_strs[] = {
265 const char *variant_str = "cmd unsupported";
267 int ret = rpi_firmware_property(fw,
268 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
269 &packet, sizeof(packet));
274 /* This is not compatible with y2038 */
275 date_and_time = packet;
277 ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_FIRMWARE_VARIANT,
278 &variant, sizeof(variant));
281 if (variant >= ARRAY_SIZE(variant_strs))
283 variant_str = variant_strs[variant];
287 "Attached to firmware from %ptT, variant %s\n",
288 &date_and_time, variant_str);
292 rpi_firmware_print_firmware_hash(struct rpi_firmware *fw)
295 int ret = rpi_firmware_property(fw,
296 RPI_FIRMWARE_GET_FIRMWARE_HASH,
303 "Firmware hash is %08x%08x%08x%08x%08x\n",
304 hash[0], hash[1], hash[2], hash[3], hash[4]);
308 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
311 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
312 &packet, sizeof(packet));
317 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
320 if (!IS_ERR_OR_NULL(rpi_hwmon)) {
321 if (devm_device_add_group(dev, &rpi_firmware_dev_group))
322 dev_err(dev, "Failed to create get_trottled attr\n");
326 static void rpi_register_clk_driver(struct device *dev)
328 struct device_node *firmware;
331 * Earlier DTs don't have a node for the firmware clocks but
332 * rely on us creating a platform device by hand. If we do
333 * have a node for the firmware clocks, just bail out here.
335 firmware = of_get_compatible_child(dev->of_node,
336 "raspberrypi,firmware-clocks");
338 of_node_put(firmware);
342 rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
346 unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
348 struct rpi_firmware_clk_rate_request msg =
349 RPI_FIRMWARE_CLK_RATE_REQUEST(id);
352 ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
356 * If our firmware doesn't support that operation, or fails, we
357 * assume the maximum clock rate is absolute maximum we can
358 * store over our type.
362 return le32_to_cpu(msg.rate);
364 EXPORT_SYMBOL_GPL(rpi_firmware_clk_get_max_rate);
366 static void rpi_firmware_delete(struct kref *kref)
368 struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
371 mbox_free_channel(fw->chan);
375 void rpi_firmware_put(struct rpi_firmware *fw)
377 kref_put(&fw->consumers, rpi_firmware_delete);
379 EXPORT_SYMBOL_GPL(rpi_firmware_put);
381 static void devm_rpi_firmware_put(void *data)
383 struct rpi_firmware *fw = data;
385 rpi_firmware_put(fw);
388 static int rpi_firmware_probe(struct platform_device *pdev)
390 struct device *dev = &pdev->dev;
391 struct rpi_firmware *fw;
394 * Memory will be freed by rpi_firmware_delete() once all users have
395 * released their firmware handles. Don't use devm_kzalloc() here.
397 fw = kzalloc(sizeof(*fw), GFP_KERNEL);
402 fw->cl.rx_callback = response_callback;
403 fw->cl.tx_block = true;
405 fw->chan = mbox_request_channel(&fw->cl, 0);
406 if (IS_ERR(fw->chan)) {
407 int ret = PTR_ERR(fw->chan);
409 return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
412 init_completion(&fw->c);
413 kref_init(&fw->consumers);
415 platform_set_drvdata(pdev, fw);
418 rpi_firmware_print_firmware_revision(fw);
419 rpi_firmware_print_firmware_hash(fw);
420 rpi_register_hwmon_driver(dev, fw);
421 rpi_register_clk_driver(dev);
426 static void rpi_firmware_shutdown(struct platform_device *pdev)
428 struct rpi_firmware *fw = platform_get_drvdata(pdev);
433 rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
436 static int rpi_firmware_remove(struct platform_device *pdev)
438 struct rpi_firmware *fw = platform_get_drvdata(pdev);
440 platform_device_unregister(rpi_hwmon);
442 platform_device_unregister(rpi_clk);
445 rpi_firmware_put(fw);
451 static const struct of_device_id rpi_firmware_of_match[] = {
452 { .compatible = "raspberrypi,bcm2835-firmware", },
455 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
457 struct device_node *rpi_firmware_find_node(void)
459 return of_find_matching_node(NULL, rpi_firmware_of_match);
461 EXPORT_SYMBOL_GPL(rpi_firmware_find_node);
464 * rpi_firmware_get - Get pointer to rpi_firmware structure.
465 * @firmware_node: Pointer to the firmware Device Tree node.
467 * The reference to rpi_firmware has to be released with rpi_firmware_put().
469 * Returns NULL is the firmware device is not ready.
471 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
473 struct platform_device *pdev = of_find_device_by_node(firmware_node);
474 struct rpi_firmware *fw;
479 fw = platform_get_drvdata(pdev);
483 if (!kref_get_unless_zero(&fw->consumers))
486 put_device(&pdev->dev);
491 put_device(&pdev->dev);
494 EXPORT_SYMBOL_GPL(rpi_firmware_get);
497 * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
498 * @firmware_node: Pointer to the firmware Device Tree node.
500 * Returns NULL is the firmware device is not ready.
502 struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
503 struct device_node *firmware_node)
505 struct rpi_firmware *fw;
507 fw = rpi_firmware_get(firmware_node);
511 if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
516 EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
518 static struct platform_driver rpi_firmware_driver = {
520 .name = "raspberrypi-firmware",
521 .of_match_table = rpi_firmware_of_match,
523 .probe = rpi_firmware_probe,
524 .shutdown = rpi_firmware_shutdown,
525 .remove = rpi_firmware_remove,
528 static struct notifier_block rpi_firmware_reboot_notifier = {
529 .notifier_call = rpi_firmware_notify_reboot,
532 static int __init rpi_firmware_init(void)
534 int ret = register_reboot_notifier(&rpi_firmware_reboot_notifier);
537 ret = platform_driver_register(&rpi_firmware_driver);
544 unregister_reboot_notifier(&rpi_firmware_reboot_notifier);
548 core_initcall(rpi_firmware_init);
550 static void __init rpi_firmware_exit(void)
552 platform_driver_unregister(&rpi_firmware_driver);
553 unregister_reboot_notifier(&rpi_firmware_reboot_notifier);
555 module_exit(rpi_firmware_exit);
557 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
558 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
559 MODULE_LICENSE("GPL v2");