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/slab.h>
16 #include <soc/bcm2835/raspberrypi-firmware.h>
18 #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
19 #define MBOX_CHAN(msg) ((msg) & 0xf)
20 #define MBOX_DATA28(msg) ((msg) & ~0xf)
21 #define MBOX_CHAN_PROPERTY 8
23 static struct platform_device *rpi_hwmon;
24 static struct platform_device *rpi_clk;
27 struct mbox_client cl;
28 struct mbox_chan *chan; /* The property channel. */
32 struct kref consumers;
35 static DEFINE_MUTEX(transaction_lock);
37 static void response_callback(struct mbox_client *cl, void *msg)
39 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
44 * Sends a request to the firmware through the BCM2835 mailbox driver,
45 * and synchronously waits for the reply.
48 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
50 u32 message = MBOX_MSG(chan, data);
55 mutex_lock(&transaction_lock);
56 reinit_completion(&fw->c);
57 ret = mbox_send_message(fw->chan, &message);
59 if (wait_for_completion_timeout(&fw->c, HZ)) {
63 WARN_ONCE(1, "Firmware transaction timeout");
66 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
68 mutex_unlock(&transaction_lock);
74 * rpi_firmware_property_list - Submit firmware property list
75 * @fw: Pointer to firmware structure from rpi_firmware_get().
76 * @data: Buffer holding tags.
77 * @tag_size: Size of tags buffer.
79 * Submits a set of concatenated tags to the VPU firmware through the
80 * mailbox property interface.
82 * The buffer header and the ending tag are added by this function and
83 * don't need to be supplied, just the actual tags for your operation.
84 * See struct rpi_firmware_property_tag_header for the per-tag
87 int rpi_firmware_property_list(struct rpi_firmware *fw,
88 void *data, size_t tag_size)
90 size_t size = tag_size + 12;
95 /* Packets are processed a dword at a time. */
99 buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
104 /* The firmware will error out without parsing in this case. */
105 WARN_ON(size >= 1024 * 1024);
108 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
109 memcpy(&buf[2], data, tag_size);
110 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
113 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
116 memcpy(data, &buf[2], tag_size);
117 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
119 * The tag name here might not be the one causing the
120 * error, if there were multiple tags in the request.
121 * But single-tag is the most common, so go with it.
123 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
128 dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
132 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
135 * rpi_firmware_property - Submit single firmware property
136 * @fw: Pointer to firmware structure from rpi_firmware_get().
137 * @tag: One of enum_mbox_property_tag.
138 * @tag_data: Tag data buffer.
139 * @buf_size: Buffer size.
141 * Submits a single tag to the VPU firmware through the mailbox
142 * property interface.
144 * This is a convenience wrapper around
145 * rpi_firmware_property_list() to avoid some of the
146 * boilerplate in property calls.
148 int rpi_firmware_property(struct rpi_firmware *fw,
149 u32 tag, void *tag_data, size_t buf_size)
151 struct rpi_firmware_property_tag_header *header;
154 /* Some mailboxes can use over 1k bytes. Rather than checking
155 * size and using stack or kmalloc depending on requirements,
156 * just use kmalloc. Mailboxes don't get called enough to worry
157 * too much about the time taken in the allocation.
159 void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
166 header->buf_size = buf_size;
167 header->req_resp_size = 0;
168 memcpy(data + sizeof(*header), tag_data, buf_size);
170 ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
172 memcpy(tag_data, data + sizeof(*header), buf_size);
178 EXPORT_SYMBOL_GPL(rpi_firmware_property);
181 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
183 time64_t date_and_time;
185 int ret = rpi_firmware_property(fw,
186 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
187 &packet, sizeof(packet));
192 /* This is not compatible with y2038 */
193 date_and_time = packet;
194 dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
198 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
201 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
202 &packet, sizeof(packet));
207 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
211 static void rpi_register_clk_driver(struct device *dev)
213 struct device_node *firmware;
216 * Earlier DTs don't have a node for the firmware clocks but
217 * rely on us creating a platform device by hand. If we do
218 * have a node for the firmware clocks, just bail out here.
220 firmware = of_get_compatible_child(dev->of_node,
221 "raspberrypi,firmware-clocks");
223 of_node_put(firmware);
227 rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
231 static void rpi_firmware_delete(struct kref *kref)
233 struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
236 mbox_free_channel(fw->chan);
240 void rpi_firmware_put(struct rpi_firmware *fw)
242 kref_put(&fw->consumers, rpi_firmware_delete);
244 EXPORT_SYMBOL_GPL(rpi_firmware_put);
246 static void devm_rpi_firmware_put(void *data)
248 struct rpi_firmware *fw = data;
250 rpi_firmware_put(fw);
253 static int rpi_firmware_probe(struct platform_device *pdev)
255 struct device *dev = &pdev->dev;
256 struct rpi_firmware *fw;
259 * Memory will be freed by rpi_firmware_delete() once all users have
260 * released their firmware handles. Don't use devm_kzalloc() here.
262 fw = kzalloc(sizeof(*fw), GFP_KERNEL);
267 fw->cl.rx_callback = response_callback;
268 fw->cl.tx_block = true;
270 fw->chan = mbox_request_channel(&fw->cl, 0);
271 if (IS_ERR(fw->chan)) {
272 int ret = PTR_ERR(fw->chan);
273 if (ret != -EPROBE_DEFER)
274 dev_err(dev, "Failed to get mbox channel: %d\n", ret);
278 init_completion(&fw->c);
279 kref_init(&fw->consumers);
281 platform_set_drvdata(pdev, fw);
283 rpi_firmware_print_firmware_revision(fw);
284 rpi_register_hwmon_driver(dev, fw);
285 rpi_register_clk_driver(dev);
290 static void rpi_firmware_shutdown(struct platform_device *pdev)
292 struct rpi_firmware *fw = platform_get_drvdata(pdev);
297 rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
300 static int rpi_firmware_remove(struct platform_device *pdev)
302 struct rpi_firmware *fw = platform_get_drvdata(pdev);
304 platform_device_unregister(rpi_hwmon);
306 platform_device_unregister(rpi_clk);
309 rpi_firmware_put(fw);
315 * rpi_firmware_get - Get pointer to rpi_firmware structure.
316 * @firmware_node: Pointer to the firmware Device Tree node.
318 * The reference to rpi_firmware has to be released with rpi_firmware_put().
320 * Returns NULL is the firmware device is not ready.
322 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
324 struct platform_device *pdev = of_find_device_by_node(firmware_node);
325 struct rpi_firmware *fw;
330 fw = platform_get_drvdata(pdev);
334 if (!kref_get_unless_zero(&fw->consumers))
337 put_device(&pdev->dev);
342 put_device(&pdev->dev);
345 EXPORT_SYMBOL_GPL(rpi_firmware_get);
348 * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
349 * @firmware_node: Pointer to the firmware Device Tree node.
351 * Returns NULL is the firmware device is not ready.
353 struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
354 struct device_node *firmware_node)
356 struct rpi_firmware *fw;
358 fw = rpi_firmware_get(firmware_node);
362 if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
367 EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
369 static const struct of_device_id rpi_firmware_of_match[] = {
370 { .compatible = "raspberrypi,bcm2835-firmware", },
373 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
375 static struct platform_driver rpi_firmware_driver = {
377 .name = "raspberrypi-firmware",
378 .of_match_table = rpi_firmware_of_match,
380 .probe = rpi_firmware_probe,
381 .shutdown = rpi_firmware_shutdown,
382 .remove = rpi_firmware_remove,
384 module_platform_driver(rpi_firmware_driver);
386 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
387 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
388 MODULE_LICENSE("GPL v2");