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/mailbox_controller.h>
13 #include <linux/module.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>
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;
37 static DEFINE_MUTEX(transaction_lock);
39 static void response_callback(struct mbox_client *cl, void *msg)
41 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
46 * Sends a request to the firmware through the BCM2835 mailbox driver,
47 * and synchronously waits for the reply.
50 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
52 u32 message = MBOX_MSG(chan, data);
57 mutex_lock(&transaction_lock);
58 reinit_completion(&fw->c);
59 ret = mbox_send_message(fw->chan, &message);
61 if (wait_for_completion_timeout(&fw->c, HZ)) {
65 WARN_ONCE(1, "Firmware transaction timeout");
68 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
70 mutex_unlock(&transaction_lock);
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.
81 * Submits a set of concatenated tags to the VPU firmware through the
82 * mailbox property interface.
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
89 int rpi_firmware_property_list(struct rpi_firmware *fw,
90 void *data, size_t tag_size)
92 size_t size = tag_size + 12;
97 /* Packets are processed a dword at a time. */
101 buf = dma_alloc_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size),
102 &bus_addr, GFP_ATOMIC);
106 /* The firmware will error out without parsing in this case. */
107 WARN_ON(size >= 1024 * 1024);
110 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
111 memcpy(&buf[2], data, tag_size);
112 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
115 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
118 memcpy(data, &buf[2], tag_size);
119 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
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.
125 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
130 dma_free_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size), buf, bus_addr);
134 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
137 * rpi_firmware_property - Submit single firmware property
138 * @fw: Pointer to firmware structure from rpi_firmware_get().
139 * @tag: One of enum_mbox_property_tag.
140 * @tag_data: Tag data buffer.
141 * @buf_size: Buffer size.
143 * Submits a single tag to the VPU firmware through the mailbox
144 * property interface.
146 * This is a convenience wrapper around
147 * rpi_firmware_property_list() to avoid some of the
148 * boilerplate in property calls.
150 int rpi_firmware_property(struct rpi_firmware *fw,
151 u32 tag, void *tag_data, size_t buf_size)
153 struct rpi_firmware_property_tag_header *header;
156 /* Some mailboxes can use over 1k bytes. Rather than checking
157 * size and using stack or kmalloc depending on requirements,
158 * just use kmalloc. Mailboxes don't get called enough to worry
159 * too much about the time taken in the allocation.
161 void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
168 header->buf_size = buf_size;
169 header->req_resp_size = 0;
170 memcpy(data + sizeof(*header), tag_data, buf_size);
172 ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
174 memcpy(tag_data, data + sizeof(*header), buf_size);
180 EXPORT_SYMBOL_GPL(rpi_firmware_property);
183 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
185 time64_t date_and_time;
187 int ret = rpi_firmware_property(fw,
188 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
189 &packet, sizeof(packet));
194 /* This is not compatible with y2038 */
195 date_and_time = packet;
196 dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
200 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
203 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
204 &packet, sizeof(packet));
209 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
213 static void rpi_register_clk_driver(struct device *dev)
215 struct device_node *firmware;
218 * Earlier DTs don't have a node for the firmware clocks but
219 * rely on us creating a platform device by hand. If we do
220 * have a node for the firmware clocks, just bail out here.
222 firmware = of_get_compatible_child(dev->of_node,
223 "raspberrypi,firmware-clocks");
225 of_node_put(firmware);
229 rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
233 unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
235 struct rpi_firmware_clk_rate_request msg =
236 RPI_FIRMWARE_CLK_RATE_REQUEST(id);
239 ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
243 * If our firmware doesn't support that operation, or fails, we
244 * assume the maximum clock rate is absolute maximum we can
245 * store over our type.
249 return le32_to_cpu(msg.rate);
251 EXPORT_SYMBOL_GPL(rpi_firmware_clk_get_max_rate);
253 static void rpi_firmware_delete(struct kref *kref)
255 struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
258 mbox_free_channel(fw->chan);
262 void rpi_firmware_put(struct rpi_firmware *fw)
264 kref_put(&fw->consumers, rpi_firmware_delete);
266 EXPORT_SYMBOL_GPL(rpi_firmware_put);
268 static void devm_rpi_firmware_put(void *data)
270 struct rpi_firmware *fw = data;
272 rpi_firmware_put(fw);
275 static int rpi_firmware_probe(struct platform_device *pdev)
277 struct device *dev = &pdev->dev;
278 struct rpi_firmware *fw;
281 * Memory will be freed by rpi_firmware_delete() once all users have
282 * released their firmware handles. Don't use devm_kzalloc() here.
284 fw = kzalloc(sizeof(*fw), GFP_KERNEL);
289 fw->cl.rx_callback = response_callback;
290 fw->cl.tx_block = true;
292 fw->chan = mbox_request_channel(&fw->cl, 0);
293 if (IS_ERR(fw->chan)) {
294 int ret = PTR_ERR(fw->chan);
296 return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
299 init_completion(&fw->c);
300 kref_init(&fw->consumers);
302 platform_set_drvdata(pdev, fw);
304 rpi_firmware_print_firmware_revision(fw);
305 rpi_register_hwmon_driver(dev, fw);
306 rpi_register_clk_driver(dev);
311 static void rpi_firmware_shutdown(struct platform_device *pdev)
313 struct rpi_firmware *fw = platform_get_drvdata(pdev);
318 rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
321 static void rpi_firmware_remove(struct platform_device *pdev)
323 struct rpi_firmware *fw = platform_get_drvdata(pdev);
325 platform_device_unregister(rpi_hwmon);
327 platform_device_unregister(rpi_clk);
330 rpi_firmware_put(fw);
333 static const struct of_device_id rpi_firmware_of_match[] = {
334 { .compatible = "raspberrypi,bcm2835-firmware", },
337 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
339 struct device_node *rpi_firmware_find_node(void)
341 return of_find_matching_node(NULL, rpi_firmware_of_match);
343 EXPORT_SYMBOL_GPL(rpi_firmware_find_node);
346 * rpi_firmware_get - Get pointer to rpi_firmware structure.
347 * @firmware_node: Pointer to the firmware Device Tree node.
349 * The reference to rpi_firmware has to be released with rpi_firmware_put().
351 * Returns NULL is the firmware device is not ready.
353 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
355 struct platform_device *pdev = of_find_device_by_node(firmware_node);
356 struct rpi_firmware *fw;
361 fw = platform_get_drvdata(pdev);
365 if (!kref_get_unless_zero(&fw->consumers))
368 put_device(&pdev->dev);
373 put_device(&pdev->dev);
376 EXPORT_SYMBOL_GPL(rpi_firmware_get);
379 * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
380 * @dev: The firmware device structure
381 * @firmware_node: Pointer to the firmware Device Tree node.
383 * Returns NULL is the firmware device is not ready.
385 struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
386 struct device_node *firmware_node)
388 struct rpi_firmware *fw;
390 fw = rpi_firmware_get(firmware_node);
394 if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
399 EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
401 static struct platform_driver rpi_firmware_driver = {
403 .name = "raspberrypi-firmware",
404 .of_match_table = rpi_firmware_of_match,
406 .probe = rpi_firmware_probe,
407 .shutdown = rpi_firmware_shutdown,
408 .remove_new = rpi_firmware_remove,
410 module_platform_driver(rpi_firmware_driver);
413 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
414 MODULE_LICENSE("GPL v2");