1 // SPDX-License-Identifier: GPL-2.0
3 * Defines interfaces for interacting wtih the Raspberry Pi firmware's
6 * Copyright © 2015 Broadcom
9 #include <linux/dma-mapping.h>
10 #include <linux/mailbox_client.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <soc/bcm2835/raspberrypi-firmware.h>
17 #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
18 #define MBOX_CHAN(msg) ((msg) & 0xf)
19 #define MBOX_DATA28(msg) ((msg) & ~0xf)
20 #define MBOX_CHAN_PROPERTY 8
22 static struct platform_device
*rpi_hwmon
;
23 static struct platform_device
*rpi_clk
;
26 struct mbox_client cl
;
27 struct mbox_chan
*chan
; /* The property channel. */
32 static DEFINE_MUTEX(transaction_lock
);
34 static void response_callback(struct mbox_client
*cl
, void *msg
)
36 struct rpi_firmware
*fw
= container_of(cl
, struct rpi_firmware
, cl
);
41 * Sends a request to the firmware through the BCM2835 mailbox driver,
42 * and synchronously waits for the reply.
45 rpi_firmware_transaction(struct rpi_firmware
*fw
, u32 chan
, u32 data
)
47 u32 message
= MBOX_MSG(chan
, data
);
52 mutex_lock(&transaction_lock
);
53 reinit_completion(&fw
->c
);
54 ret
= mbox_send_message(fw
->chan
, &message
);
56 if (wait_for_completion_timeout(&fw
->c
, HZ
)) {
60 WARN_ONCE(1, "Firmware transaction timeout");
63 dev_err(fw
->cl
.dev
, "mbox_send_message returned %d\n", ret
);
65 mutex_unlock(&transaction_lock
);
71 * rpi_firmware_property_list - Submit firmware property list
72 * @fw: Pointer to firmware structure from rpi_firmware_get().
73 * @data: Buffer holding tags.
74 * @tag_size: Size of tags buffer.
76 * Submits a set of concatenated tags to the VPU firmware through the
77 * mailbox property interface.
79 * The buffer header and the ending tag are added by this function and
80 * don't need to be supplied, just the actual tags for your operation.
81 * See struct rpi_firmware_property_tag_header for the per-tag
84 int rpi_firmware_property_list(struct rpi_firmware
*fw
,
85 void *data
, size_t tag_size
)
87 size_t size
= tag_size
+ 12;
92 /* Packets are processed a dword at a time. */
96 buf
= dma_alloc_coherent(fw
->cl
.dev
, PAGE_ALIGN(size
), &bus_addr
,
101 /* The firmware will error out without parsing in this case. */
102 WARN_ON(size
>= 1024 * 1024);
105 buf
[1] = RPI_FIRMWARE_STATUS_REQUEST
;
106 memcpy(&buf
[2], data
, tag_size
);
107 buf
[size
/ 4 - 1] = RPI_FIRMWARE_PROPERTY_END
;
110 ret
= rpi_firmware_transaction(fw
, MBOX_CHAN_PROPERTY
, bus_addr
);
113 memcpy(data
, &buf
[2], tag_size
);
114 if (ret
== 0 && buf
[1] != RPI_FIRMWARE_STATUS_SUCCESS
) {
116 * The tag name here might not be the one causing the
117 * error, if there were multiple tags in the request.
118 * But single-tag is the most common, so go with it.
120 dev_err(fw
->cl
.dev
, "Request 0x%08x returned status 0x%08x\n",
125 dma_free_coherent(fw
->cl
.dev
, PAGE_ALIGN(size
), buf
, bus_addr
);
129 EXPORT_SYMBOL_GPL(rpi_firmware_property_list
);
132 * rpi_firmware_property - Submit single firmware property
133 * @fw: Pointer to firmware structure from rpi_firmware_get().
134 * @tag: One of enum_mbox_property_tag.
135 * @tag_data: Tag data buffer.
136 * @buf_size: Buffer size.
138 * Submits a single tag to the VPU firmware through the mailbox
139 * property interface.
141 * This is a convenience wrapper around
142 * rpi_firmware_property_list() to avoid some of the
143 * boilerplate in property calls.
145 int rpi_firmware_property(struct rpi_firmware
*fw
,
146 u32 tag
, void *tag_data
, size_t buf_size
)
148 struct rpi_firmware_property_tag_header
*header
;
151 /* Some mailboxes can use over 1k bytes. Rather than checking
152 * size and using stack or kmalloc depending on requirements,
153 * just use kmalloc. Mailboxes don't get called enough to worry
154 * too much about the time taken in the allocation.
156 void *data
= kmalloc(sizeof(*header
) + buf_size
, GFP_KERNEL
);
163 header
->buf_size
= buf_size
;
164 header
->req_resp_size
= 0;
165 memcpy(data
+ sizeof(*header
), tag_data
, buf_size
);
167 ret
= rpi_firmware_property_list(fw
, data
, buf_size
+ sizeof(*header
));
169 memcpy(tag_data
, data
+ sizeof(*header
), buf_size
);
175 EXPORT_SYMBOL_GPL(rpi_firmware_property
);
178 rpi_firmware_print_firmware_revision(struct rpi_firmware
*fw
)
181 int ret
= rpi_firmware_property(fw
,
182 RPI_FIRMWARE_GET_FIRMWARE_REVISION
,
183 &packet
, sizeof(packet
));
188 time64_to_tm(packet
, 0, &tm
);
191 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
192 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
193 tm
.tm_hour
, tm
.tm_min
);
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 rpi_clk
= platform_device_register_data(dev
, "raspberrypi-clk",
217 static int rpi_firmware_probe(struct platform_device
*pdev
)
219 struct device
*dev
= &pdev
->dev
;
220 struct rpi_firmware
*fw
;
222 fw
= devm_kzalloc(dev
, sizeof(*fw
), GFP_KERNEL
);
227 fw
->cl
.rx_callback
= response_callback
;
228 fw
->cl
.tx_block
= true;
230 fw
->chan
= mbox_request_channel(&fw
->cl
, 0);
231 if (IS_ERR(fw
->chan
)) {
232 int ret
= PTR_ERR(fw
->chan
);
233 if (ret
!= -EPROBE_DEFER
)
234 dev_err(dev
, "Failed to get mbox channel: %d\n", ret
);
238 init_completion(&fw
->c
);
240 platform_set_drvdata(pdev
, fw
);
242 rpi_firmware_print_firmware_revision(fw
);
243 rpi_register_hwmon_driver(dev
, fw
);
244 rpi_register_clk_driver(dev
);
249 static void rpi_firmware_shutdown(struct platform_device
*pdev
)
251 struct rpi_firmware
*fw
= platform_get_drvdata(pdev
);
256 rpi_firmware_property(fw
, RPI_FIRMWARE_NOTIFY_REBOOT
, NULL
, 0);
259 static int rpi_firmware_remove(struct platform_device
*pdev
)
261 struct rpi_firmware
*fw
= platform_get_drvdata(pdev
);
263 platform_device_unregister(rpi_hwmon
);
265 platform_device_unregister(rpi_clk
);
267 mbox_free_channel(fw
->chan
);
273 * rpi_firmware_get - Get pointer to rpi_firmware structure.
274 * @firmware_node: Pointer to the firmware Device Tree node.
276 * Returns NULL is the firmware device is not ready.
278 struct rpi_firmware
*rpi_firmware_get(struct device_node
*firmware_node
)
280 struct platform_device
*pdev
= of_find_device_by_node(firmware_node
);
285 return platform_get_drvdata(pdev
);
287 EXPORT_SYMBOL_GPL(rpi_firmware_get
);
289 static const struct of_device_id rpi_firmware_of_match
[] = {
290 { .compatible
= "raspberrypi,bcm2835-firmware", },
293 MODULE_DEVICE_TABLE(of
, rpi_firmware_of_match
);
295 static struct platform_driver rpi_firmware_driver
= {
297 .name
= "raspberrypi-firmware",
298 .of_match_table
= rpi_firmware_of_match
,
300 .probe
= rpi_firmware_probe
,
301 .shutdown
= rpi_firmware_shutdown
,
302 .remove
= rpi_firmware_remove
,
304 module_platform_driver(rpi_firmware_driver
);
306 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
307 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
308 MODULE_LICENSE("GPL v2");