blob: 9fdebb1171e55734e455ae1ea9754dff0854f3a3 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Defines interfaces for interacting wtih the Raspberry Pi firmware's
4 * property channel.
5 *
6 * Copyright © 2015 Broadcom
7 */
8
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>
14#include <linux/of_platform.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <soc/bcm2835/raspberrypi-firmware.h>
18
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
23
24static struct platform_device *rpi_hwmon;
25static struct platform_device *rpi_clk;
26
27struct rpi_firmware {
28 struct mbox_client cl;
29 struct mbox_chan *chan; /* The property channel. */
30 struct completion c;
31 u32 enabled;
32
33 struct kref consumers;
34};
35
36static DEFINE_MUTEX(transaction_lock);
37
38static void response_callback(struct mbox_client *cl, void *msg)
39{
40 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
41 complete(&fw->c);
42}
43
44/*
45 * Sends a request to the firmware through the BCM2835 mailbox driver,
46 * and synchronously waits for the reply.
47 */
48static int
49rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
50{
51 u32 message = MBOX_MSG(chan, data);
52 int ret;
53
54 WARN_ON(data & 0xf);
55
56 mutex_lock(&transaction_lock);
57 reinit_completion(&fw->c);
58 ret = mbox_send_message(fw->chan, &message);
59 if (ret >= 0) {
60 if (wait_for_completion_timeout(&fw->c, HZ)) {
61 ret = 0;
62 } else {
63 ret = -ETIMEDOUT;
64 WARN_ONCE(1, "Firmware transaction timeout");
65 }
66 } else {
67 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
68 }
69 mutex_unlock(&transaction_lock);
70
71 return ret;
72}
73
74/**
75 * rpi_firmware_property_list - Submit firmware property list
76 * @fw: Pointer to firmware structure from rpi_firmware_get().
77 * @data: Buffer holding tags.
78 * @tag_size: Size of tags buffer.
79 *
80 * Submits a set of concatenated tags to the VPU firmware through the
81 * mailbox property interface.
82 *
83 * The buffer header and the ending tag are added by this function and
84 * don't need to be supplied, just the actual tags for your operation.
85 * See struct rpi_firmware_property_tag_header for the per-tag
86 * structure.
87 */
88int rpi_firmware_property_list(struct rpi_firmware *fw,
89 void *data, size_t tag_size)
90{
91 size_t size = tag_size + 12;
92 u32 *buf;
93 dma_addr_t bus_addr;
94 int ret;
95
96 /* Packets are processed a dword at a time. */
97 if (size & 3)
98 return -EINVAL;
99
100 buf = dma_alloc_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size),
101 &bus_addr, GFP_ATOMIC);
102 if (!buf)
103 return -ENOMEM;
104
105 /* The firmware will error out without parsing in this case. */
106 WARN_ON(size >= 1024 * 1024);
107
108 buf[0] = size;
109 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
110 memcpy(&buf[2], data, tag_size);
111 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
112 wmb();
113
114 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
115
116 rmb();
117 memcpy(data, &buf[2], tag_size);
118 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
119 /*
120 * The tag name here might not be the one causing the
121 * error, if there were multiple tags in the request.
122 * But single-tag is the most common, so go with it.
123 */
124 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
125 buf[2], buf[1]);
126 ret = -EINVAL;
127 }
128
129 dma_free_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size), buf, bus_addr);
130
131 return ret;
132}
133EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
134
135/**
136 * rpi_firmware_property - Submit single firmware property
137 * @fw: Pointer to firmware structure from rpi_firmware_get().
138 * @tag: One of enum_mbox_property_tag.
139 * @tag_data: Tag data buffer.
140 * @buf_size: Buffer size.
141 *
142 * Submits a single tag to the VPU firmware through the mailbox
143 * property interface.
144 *
145 * This is a convenience wrapper around
146 * rpi_firmware_property_list() to avoid some of the
147 * boilerplate in property calls.
148 */
149int rpi_firmware_property(struct rpi_firmware *fw,
150 u32 tag, void *tag_data, size_t buf_size)
151{
152 struct rpi_firmware_property_tag_header *header;
153 int ret;
154
155 /* Some mailboxes can use over 1k bytes. Rather than checking
156 * size and using stack or kmalloc depending on requirements,
157 * just use kmalloc. Mailboxes don't get called enough to worry
158 * too much about the time taken in the allocation.
159 */
160 void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
161
162 if (!data)
163 return -ENOMEM;
164
165 header = data;
166 header->tag = tag;
167 header->buf_size = buf_size;
168 header->req_resp_size = 0;
169 memcpy(data + sizeof(*header), tag_data, buf_size);
170
171 ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
172
173 memcpy(tag_data, data + sizeof(*header), buf_size);
174
175 kfree(data);
176
177 return ret;
178}
179EXPORT_SYMBOL_GPL(rpi_firmware_property);
180
181static void
182rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
183{
184 u32 packet;
185 int ret = rpi_firmware_property(fw,
186 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
187 &packet, sizeof(packet));
188
189 if (ret == 0) {
190 struct tm tm;
191
192 time64_to_tm(packet, 0, &tm);
193
194 dev_info(fw->cl.dev,
195 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
196 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
197 tm.tm_hour, tm.tm_min);
198 }
199}
200
201static void
202rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
203{
204 u32 packet;
205 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
206 &packet, sizeof(packet));
207
208 if (ret)
209 return;
210
211 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
212 -1, NULL, 0);
213}
214
215static void rpi_register_clk_driver(struct device *dev)
216{
217 rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
218 -1, NULL, 0);
219}
220
221static void rpi_firmware_delete(struct kref *kref)
222{
223 struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
224 consumers);
225
226 mbox_free_channel(fw->chan);
227 kfree(fw);
228}
229
230void rpi_firmware_put(struct rpi_firmware *fw)
231{
232 kref_put(&fw->consumers, rpi_firmware_delete);
233}
234EXPORT_SYMBOL_GPL(rpi_firmware_put);
235
236static void devm_rpi_firmware_put(void *data)
237{
238 struct rpi_firmware *fw = data;
239
240 rpi_firmware_put(fw);
241}
242
243static int rpi_firmware_probe(struct platform_device *pdev)
244{
245 struct device *dev = &pdev->dev;
246 struct rpi_firmware *fw;
247
248 /*
249 * Memory will be freed by rpi_firmware_delete() once all users have
250 * released their firmware handles. Don't use devm_kzalloc() here.
251 */
252 fw = kzalloc(sizeof(*fw), GFP_KERNEL);
253 if (!fw)
254 return -ENOMEM;
255
256 fw->cl.dev = dev;
257 fw->cl.rx_callback = response_callback;
258 fw->cl.tx_block = true;
259
260 fw->chan = mbox_request_channel(&fw->cl, 0);
261 if (IS_ERR(fw->chan)) {
262 int ret = PTR_ERR(fw->chan);
263 if (ret != -EPROBE_DEFER)
264 dev_err(dev, "Failed to get mbox channel: %d\n", ret);
265 kfree(fw);
266 return ret;
267 }
268
269 init_completion(&fw->c);
270 kref_init(&fw->consumers);
271
272 platform_set_drvdata(pdev, fw);
273
274 rpi_firmware_print_firmware_revision(fw);
275 rpi_register_hwmon_driver(dev, fw);
276 rpi_register_clk_driver(dev);
277
278 return 0;
279}
280
281static void rpi_firmware_shutdown(struct platform_device *pdev)
282{
283 struct rpi_firmware *fw = platform_get_drvdata(pdev);
284
285 if (!fw)
286 return;
287
288 rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
289}
290
291static int rpi_firmware_remove(struct platform_device *pdev)
292{
293 struct rpi_firmware *fw = platform_get_drvdata(pdev);
294
295 platform_device_unregister(rpi_hwmon);
296 rpi_hwmon = NULL;
297 platform_device_unregister(rpi_clk);
298 rpi_clk = NULL;
299
300 rpi_firmware_put(fw);
301
302 return 0;
303}
304
305/**
306 * rpi_firmware_get - Get pointer to rpi_firmware structure.
307 * @firmware_node: Pointer to the firmware Device Tree node.
308 *
309 * The reference to rpi_firmware has to be released with rpi_firmware_put().
310 *
311 * Returns NULL is the firmware device is not ready.
312 */
313struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
314{
315 struct platform_device *pdev = of_find_device_by_node(firmware_node);
316 struct rpi_firmware *fw;
317
318 if (!pdev)
319 return NULL;
320
321 fw = platform_get_drvdata(pdev);
322 if (!fw)
323 return NULL;
324
325 if (!kref_get_unless_zero(&fw->consumers))
326 return NULL;
327
328 return fw;
329}
330EXPORT_SYMBOL_GPL(rpi_firmware_get);
331
332/**
333 * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
334 * @firmware_node: Pointer to the firmware Device Tree node.
335 *
336 * Returns NULL is the firmware device is not ready.
337 */
338struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
339 struct device_node *firmware_node)
340{
341 struct rpi_firmware *fw;
342
343 fw = rpi_firmware_get(firmware_node);
344 if (!fw)
345 return NULL;
346
347 if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
348 return NULL;
349
350 return fw;
351}
352EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
353
354static const struct of_device_id rpi_firmware_of_match[] = {
355 { .compatible = "raspberrypi,bcm2835-firmware", },
356 {},
357};
358MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
359
360static struct platform_driver rpi_firmware_driver = {
361 .driver = {
362 .name = "raspberrypi-firmware",
363 .of_match_table = rpi_firmware_of_match,
364 },
365 .probe = rpi_firmware_probe,
366 .shutdown = rpi_firmware_shutdown,
367 .remove = rpi_firmware_remove,
368};
369module_platform_driver(rpi_firmware_driver);
370
371MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
372MODULE_DESCRIPTION("Raspberry Pi firmware driver");
373MODULE_LICENSE("GPL v2");