blob: ac34876a97f8b046bc177437118b9c94db580cc7 [file] [log] [blame]
Stefan Wahren502b431c2018-10-22 22:26:24 +02001// SPDX-License-Identifier: GPL-2.0
Eric Anholt4e3d6062015-02-26 10:08:06 +00002/*
Wang Qing60aa8782020-11-07 16:19:39 +08003 * Defines interfaces for interacting with the Raspberry Pi firmware's
Eric Anholt4e3d6062015-02-26 10:08:06 +00004 * property channel.
5 *
6 * Copyright © 2015 Broadcom
Eric Anholt4e3d6062015-02-26 10:08:06 +00007 */
8
9#include <linux/dma-mapping.h>
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +010010#include <linux/kref.h>
Eric Anholt4e3d6062015-02-26 10:08:06 +000011#include <linux/mailbox_client.h>
Laurent Pinchartdf518a02024-03-26 21:58:06 +020012#include <linux/mailbox_controller.h>
Eric Anholt4e3d6062015-02-26 10:08:06 +000013#include <linux/module.h>
Rob Herring5b457592023-08-03 16:42:57 -060014#include <linux/of.h>
Eric Anholt4e3d6062015-02-26 10:08:06 +000015#include <linux/of_platform.h>
16#include <linux/platform_device.h>
James Hughes91c6ada2018-11-16 14:39:07 +000017#include <linux/slab.h>
Eric Anholt4e3d6062015-02-26 10:08:06 +000018#include <soc/bcm2835/raspberrypi-firmware.h>
19
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
24
Stefan Wahren70eea1b2018-05-25 21:24:36 +020025static struct platform_device *rpi_hwmon;
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +020026static struct platform_device *rpi_clk;
Stefan Wahren70eea1b2018-05-25 21:24:36 +020027
Eric Anholt4e3d6062015-02-26 10:08:06 +000028struct rpi_firmware {
29 struct mbox_client cl;
30 struct mbox_chan *chan; /* The property channel. */
31 struct completion c;
32 u32 enabled;
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +010033
34 struct kref consumers;
Eric Anholt4e3d6062015-02-26 10:08:06 +000035};
36
37static DEFINE_MUTEX(transaction_lock);
38
39static void response_callback(struct mbox_client *cl, void *msg)
40{
41 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
42 complete(&fw->c);
43}
44
45/*
46 * Sends a request to the firmware through the BCM2835 mailbox driver,
47 * and synchronously waits for the reply.
48 */
49static int
50rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
51{
52 u32 message = MBOX_MSG(chan, data);
53 int ret;
54
55 WARN_ON(data & 0xf);
56
57 mutex_lock(&transaction_lock);
58 reinit_completion(&fw->c);
59 ret = mbox_send_message(fw->chan, &message);
60 if (ret >= 0) {
Stefan Wahren08291872018-10-06 13:31:56 +020061 if (wait_for_completion_timeout(&fw->c, HZ)) {
62 ret = 0;
63 } else {
64 ret = -ETIMEDOUT;
65 WARN_ONCE(1, "Firmware transaction timeout");
66 }
Eric Anholt4e3d6062015-02-26 10:08:06 +000067 } else {
68 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
69 }
70 mutex_unlock(&transaction_lock);
71
72 return ret;
73}
74
75/**
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.
80 *
81 * Submits a set of concatenated tags to the VPU firmware through the
82 * mailbox property interface.
83 *
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
87 * structure.
88 */
89int rpi_firmware_property_list(struct rpi_firmware *fw,
90 void *data, size_t tag_size)
91{
92 size_t size = tag_size + 12;
93 u32 *buf;
94 dma_addr_t bus_addr;
95 int ret;
96
97 /* Packets are processed a dword at a time. */
98 if (size & 3)
99 return -EINVAL;
100
Laurent Pinchartdf518a02024-03-26 21:58:06 +0200101 buf = dma_alloc_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size),
102 &bus_addr, GFP_ATOMIC);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000103 if (!buf)
104 return -ENOMEM;
105
106 /* The firmware will error out without parsing in this case. */
107 WARN_ON(size >= 1024 * 1024);
108
109 buf[0] = size;
110 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
111 memcpy(&buf[2], data, tag_size);
112 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
113 wmb();
114
115 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
116
117 rmb();
118 memcpy(data, &buf[2], tag_size);
119 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
120 /*
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.
124 */
125 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
126 buf[2], buf[1]);
127 ret = -EINVAL;
128 }
129
Laurent Pinchartdf518a02024-03-26 21:58:06 +0200130 dma_free_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size), buf, bus_addr);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000131
132 return ret;
133}
134EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
135
136/**
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.
142 *
143 * Submits a single tag to the VPU firmware through the mailbox
144 * property interface.
145 *
146 * This is a convenience wrapper around
147 * rpi_firmware_property_list() to avoid some of the
148 * boilerplate in property calls.
149 */
150int rpi_firmware_property(struct rpi_firmware *fw,
151 u32 tag, void *tag_data, size_t buf_size)
152{
James Hughes91c6ada2018-11-16 14:39:07 +0000153 struct rpi_firmware_property_tag_header *header;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000154 int ret;
155
James Hughes91c6ada2018-11-16 14:39:07 +0000156 /* 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.
160 */
161 void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
Kees Cooka1547e02018-06-29 11:44:50 -0700162
James Hughes91c6ada2018-11-16 14:39:07 +0000163 if (!data)
164 return -ENOMEM;
165
166 header = data;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000167 header->tag = tag;
168 header->buf_size = buf_size;
169 header->req_resp_size = 0;
James Hughes91c6ada2018-11-16 14:39:07 +0000170 memcpy(data + sizeof(*header), tag_data, buf_size);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000171
James Hughes91c6ada2018-11-16 14:39:07 +0000172 ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
173
174 memcpy(tag_data, data + sizeof(*header), buf_size);
175
176 kfree(data);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000177
178 return ret;
179}
180EXPORT_SYMBOL_GPL(rpi_firmware_property);
181
182static void
183rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
184{
Andy Shevchenkoda785a82020-06-16 19:31:39 +0300185 time64_t date_and_time;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000186 u32 packet;
187 int ret = rpi_firmware_property(fw,
188 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
189 &packet, sizeof(packet));
190
Andy Shevchenko4a60f582020-04-15 20:00:45 +0300191 if (ret)
192 return;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000193
Andy Shevchenkoda785a82020-06-16 19:31:39 +0300194 /* 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);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000197}
198
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200199static void
200rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
201{
202 u32 packet;
203 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
204 &packet, sizeof(packet));
205
206 if (ret)
207 return;
208
209 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
210 -1, NULL, 0);
211}
212
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +0200213static void rpi_register_clk_driver(struct device *dev)
214{
Maxime Ripard511aba02020-06-15 10:40:43 +0200215 struct device_node *firmware;
216
217 /*
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.
221 */
222 firmware = of_get_compatible_child(dev->of_node,
223 "raspberrypi,firmware-clocks");
224 if (firmware) {
225 of_node_put(firmware);
226 return;
227 }
228
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +0200229 rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
230 -1, NULL, 0);
231}
232
Maxime Ripard40c31952022-10-27 14:52:43 +0200233unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
234{
235 struct rpi_firmware_clk_rate_request msg =
236 RPI_FIRMWARE_CLK_RATE_REQUEST(id);
237 int ret;
238
239 ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
240 &msg, sizeof(msg));
241 if (ret)
242 /*
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.
246 */
247 return UINT_MAX;
248
249 return le32_to_cpu(msg.rate);
250}
251EXPORT_SYMBOL_GPL(rpi_firmware_clk_get_max_rate);
252
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100253static void rpi_firmware_delete(struct kref *kref)
254{
255 struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
256 consumers);
257
258 mbox_free_channel(fw->chan);
259 kfree(fw);
260}
261
262void rpi_firmware_put(struct rpi_firmware *fw)
263{
264 kref_put(&fw->consumers, rpi_firmware_delete);
265}
266EXPORT_SYMBOL_GPL(rpi_firmware_put);
267
Nicolas Saenz Juliennef6632042021-01-18 13:32:35 +0100268static void devm_rpi_firmware_put(void *data)
269{
270 struct rpi_firmware *fw = data;
271
272 rpi_firmware_put(fw);
273}
274
Eric Anholt4e3d6062015-02-26 10:08:06 +0000275static int rpi_firmware_probe(struct platform_device *pdev)
276{
277 struct device *dev = &pdev->dev;
278 struct rpi_firmware *fw;
279
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100280 /*
281 * Memory will be freed by rpi_firmware_delete() once all users have
282 * released their firmware handles. Don't use devm_kzalloc() here.
283 */
284 fw = kzalloc(sizeof(*fw), GFP_KERNEL);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000285 if (!fw)
286 return -ENOMEM;
287
288 fw->cl.dev = dev;
289 fw->cl.rx_callback = response_callback;
290 fw->cl.tx_block = true;
291
292 fw->chan = mbox_request_channel(&fw->cl, 0);
293 if (IS_ERR(fw->chan)) {
294 int ret = PTR_ERR(fw->chan);
Yang Yingliang7b511612022-11-17 15:06:36 +0800295 kfree(fw);
Linus Torvaldsba54ff12022-12-16 03:49:24 -0800296 return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
Eric Anholt4e3d6062015-02-26 10:08:06 +0000297 }
298
299 init_completion(&fw->c);
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100300 kref_init(&fw->consumers);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000301
302 platform_set_drvdata(pdev, fw);
303
304 rpi_firmware_print_firmware_revision(fw);
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200305 rpi_register_hwmon_driver(dev, fw);
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +0200306 rpi_register_clk_driver(dev);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000307
308 return 0;
309}
310
Stefan Wahrenb80ec7c2018-12-07 19:21:11 +0100311static void rpi_firmware_shutdown(struct platform_device *pdev)
312{
313 struct rpi_firmware *fw = platform_get_drvdata(pdev);
314
315 if (!fw)
316 return;
317
318 rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
319}
320
Uwe Kleine-Königffc3c922023-12-27 17:26:31 +0100321static void rpi_firmware_remove(struct platform_device *pdev)
Eric Anholt4e3d6062015-02-26 10:08:06 +0000322{
323 struct rpi_firmware *fw = platform_get_drvdata(pdev);
324
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200325 platform_device_unregister(rpi_hwmon);
326 rpi_hwmon = NULL;
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +0200327 platform_device_unregister(rpi_clk);
328 rpi_clk = NULL;
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100329
330 rpi_firmware_put(fw);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000331}
332
Maxime Ripardbc638972022-10-27 14:52:41 +0200333static const struct of_device_id rpi_firmware_of_match[] = {
334 { .compatible = "raspberrypi,bcm2835-firmware", },
335 {},
336};
337MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
338
339struct device_node *rpi_firmware_find_node(void)
340{
341 return of_find_matching_node(NULL, rpi_firmware_of_match);
342}
343EXPORT_SYMBOL_GPL(rpi_firmware_find_node);
344
Eric Anholt4e3d6062015-02-26 10:08:06 +0000345/**
346 * rpi_firmware_get - Get pointer to rpi_firmware structure.
347 * @firmware_node: Pointer to the firmware Device Tree node.
348 *
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100349 * The reference to rpi_firmware has to be released with rpi_firmware_put().
350 *
Eric Anholt4e3d6062015-02-26 10:08:06 +0000351 * Returns NULL is the firmware device is not ready.
352 */
353struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
354{
355 struct platform_device *pdev = of_find_device_by_node(firmware_node);
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100356 struct rpi_firmware *fw;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000357
358 if (!pdev)
359 return NULL;
360
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100361 fw = platform_get_drvdata(pdev);
362 if (!fw)
Christophe JAILLET09cbd1d2021-08-06 08:46:11 +0200363 goto err_put_device;
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100364
365 if (!kref_get_unless_zero(&fw->consumers))
Christophe JAILLET09cbd1d2021-08-06 08:46:11 +0200366 goto err_put_device;
367
368 put_device(&pdev->dev);
Nicolas Saenz Julienne1e7c5732021-01-18 13:32:34 +0100369
370 return fw;
Christophe JAILLET09cbd1d2021-08-06 08:46:11 +0200371
372err_put_device:
373 put_device(&pdev->dev);
374 return NULL;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000375}
376EXPORT_SYMBOL_GPL(rpi_firmware_get);
377
Nicolas Saenz Juliennef6632042021-01-18 13:32:35 +0100378/**
379 * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
Kieran Binghama21ecc52023-09-25 16:18:11 +0100380 * @dev: The firmware device structure
Nicolas Saenz Juliennef6632042021-01-18 13:32:35 +0100381 * @firmware_node: Pointer to the firmware Device Tree node.
382 *
383 * Returns NULL is the firmware device is not ready.
384 */
385struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
386 struct device_node *firmware_node)
387{
388 struct rpi_firmware *fw;
389
390 fw = rpi_firmware_get(firmware_node);
391 if (!fw)
392 return NULL;
393
394 if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
395 return NULL;
396
397 return fw;
398}
399EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
400
Eric Anholt4e3d6062015-02-26 10:08:06 +0000401static struct platform_driver rpi_firmware_driver = {
402 .driver = {
403 .name = "raspberrypi-firmware",
404 .of_match_table = rpi_firmware_of_match,
405 },
406 .probe = rpi_firmware_probe,
Stefan Wahrenb80ec7c2018-12-07 19:21:11 +0100407 .shutdown = rpi_firmware_shutdown,
Uwe Kleine-Königffc3c922023-12-27 17:26:31 +0100408 .remove_new = rpi_firmware_remove,
Eric Anholt4e3d6062015-02-26 10:08:06 +0000409};
410module_platform_driver(rpi_firmware_driver);
411
412MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
413MODULE_DESCRIPTION("Raspberry Pi firmware driver");
414MODULE_LICENSE("GPL v2");