blob: 625c8fdceabf369be60e721a04de3971d0a3e63a [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/*
3 * Defines interfaces for interacting wtih the Raspberry Pi firmware's
4 * property channel.
5 *
6 * Copyright © 2015 Broadcom
Eric Anholt4e3d6062015-02-26 10:08:06 +00007 */
8
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>
James Hughes91c6ada2018-11-16 14:39:07 +000014#include <linux/slab.h>
Nicolas Saenz Juliennefbbc5ff2020-05-05 18:13:15 +020015#include <linux/pci.h>
16#include <linux/delay.h>
Eric Anholt4e3d6062015-02-26 10:08:06 +000017#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
Nicolas Saenz Juliennefbbc5ff2020-05-05 18:13:15 +020024#define VL805_PCI_CONFIG_VERSION_OFFSET 0x50
25
Stefan Wahren70eea1b2018-05-25 21:24:36 +020026static struct platform_device *rpi_hwmon;
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +020027static struct platform_device *rpi_clk;
Stefan Wahren70eea1b2018-05-25 21:24:36 +020028
Eric Anholt4e3d6062015-02-26 10:08:06 +000029struct rpi_firmware {
30 struct mbox_client cl;
31 struct mbox_chan *chan; /* The property channel. */
32 struct completion c;
33 u32 enabled;
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) {
Stefan Wahren08291872018-10-06 13:31:56 +020060 if (wait_for_completion_timeout(&fw->c, HZ)) {
61 ret = 0;
62 } else {
63 ret = -ETIMEDOUT;
64 WARN_ONCE(1, "Firmware transaction timeout");
65 }
Eric Anholt4e3d6062015-02-26 10:08:06 +000066 } 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->cl.dev, PAGE_ALIGN(size), &bus_addr,
101 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->cl.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{
James Hughes91c6ada2018-11-16 14:39:07 +0000152 struct rpi_firmware_property_tag_header *header;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000153 int ret;
154
James Hughes91c6ada2018-11-16 14:39:07 +0000155 /* 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);
Kees Cooka1547e02018-06-29 11:44:50 -0700161
James Hughes91c6ada2018-11-16 14:39:07 +0000162 if (!data)
163 return -ENOMEM;
164
165 header = data;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000166 header->tag = tag;
167 header->buf_size = buf_size;
168 header->req_resp_size = 0;
James Hughes91c6ada2018-11-16 14:39:07 +0000169 memcpy(data + sizeof(*header), tag_data, buf_size);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000170
James Hughes91c6ada2018-11-16 14:39:07 +0000171 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);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000176
177 return ret;
178}
179EXPORT_SYMBOL_GPL(rpi_firmware_property);
180
181static void
182rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
183{
Andy Shevchenkoda785a82020-06-16 19:31:39 +0300184 time64_t date_and_time;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000185 u32 packet;
186 int ret = rpi_firmware_property(fw,
187 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
188 &packet, sizeof(packet));
189
Andy Shevchenko4a60f582020-04-15 20:00:45 +0300190 if (ret)
191 return;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000192
Andy Shevchenkoda785a82020-06-16 19:31:39 +0300193 /* This is not compatible with y2038 */
194 date_and_time = packet;
195 dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000196}
197
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200198static void
199rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
200{
201 u32 packet;
202 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
203 &packet, sizeof(packet));
204
205 if (ret)
206 return;
207
208 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
209 -1, NULL, 0);
210}
211
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +0200212static void rpi_register_clk_driver(struct device *dev)
213{
214 rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
215 -1, NULL, 0);
216}
217
Eric Anholt4e3d6062015-02-26 10:08:06 +0000218static int rpi_firmware_probe(struct platform_device *pdev)
219{
220 struct device *dev = &pdev->dev;
221 struct rpi_firmware *fw;
222
223 fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
224 if (!fw)
225 return -ENOMEM;
226
227 fw->cl.dev = dev;
228 fw->cl.rx_callback = response_callback;
229 fw->cl.tx_block = true;
230
231 fw->chan = mbox_request_channel(&fw->cl, 0);
232 if (IS_ERR(fw->chan)) {
233 int ret = PTR_ERR(fw->chan);
234 if (ret != -EPROBE_DEFER)
235 dev_err(dev, "Failed to get mbox channel: %d\n", ret);
236 return ret;
237 }
238
239 init_completion(&fw->c);
240
241 platform_set_drvdata(pdev, fw);
242
243 rpi_firmware_print_firmware_revision(fw);
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200244 rpi_register_hwmon_driver(dev, fw);
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +0200245 rpi_register_clk_driver(dev);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000246
247 return 0;
248}
249
Stefan Wahrenb80ec7c2018-12-07 19:21:11 +0100250static void rpi_firmware_shutdown(struct platform_device *pdev)
251{
252 struct rpi_firmware *fw = platform_get_drvdata(pdev);
253
254 if (!fw)
255 return;
256
257 rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
258}
259
Eric Anholt4e3d6062015-02-26 10:08:06 +0000260static int rpi_firmware_remove(struct platform_device *pdev)
261{
262 struct rpi_firmware *fw = platform_get_drvdata(pdev);
263
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200264 platform_device_unregister(rpi_hwmon);
265 rpi_hwmon = NULL;
Nicolas Saenz Julienne91f2cf42019-06-12 20:24:55 +0200266 platform_device_unregister(rpi_clk);
267 rpi_clk = NULL;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000268 mbox_free_channel(fw->chan);
269
270 return 0;
271}
272
273/**
274 * rpi_firmware_get - Get pointer to rpi_firmware structure.
275 * @firmware_node: Pointer to the firmware Device Tree node.
276 *
277 * Returns NULL is the firmware device is not ready.
278 */
279struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
280{
281 struct platform_device *pdev = of_find_device_by_node(firmware_node);
282
283 if (!pdev)
284 return NULL;
285
286 return platform_get_drvdata(pdev);
287}
288EXPORT_SYMBOL_GPL(rpi_firmware_get);
289
Nicolas Saenz Juliennefbbc5ff2020-05-05 18:13:15 +0200290/*
291 * The Raspberry Pi 4 gets its USB functionality from VL805, a PCIe chip that
292 * implements xHCI. After a PCI reset, VL805's firmware may either be loaded
293 * directly from an EEPROM or, if not present, by the SoC's co-processor,
294 * VideoCore. RPi4's VideoCore OS contains both the non public firmware load
295 * logic and the VL805 firmware blob. This function triggers the aforementioned
296 * process.
297 */
298int rpi_firmware_init_vl805(struct pci_dev *pdev)
299{
300 struct device_node *fw_np;
301 struct rpi_firmware *fw;
302 u32 dev_addr, version;
303 int ret;
304
305 fw_np = of_find_compatible_node(NULL, NULL,
306 "raspberrypi,bcm2835-firmware");
307 if (!fw_np)
308 return 0;
309
310 fw = rpi_firmware_get(fw_np);
311 of_node_put(fw_np);
312 if (!fw)
313 return -ENODEV;
314
315 /*
316 * Make sure we don't trigger a firmware load unnecessarily.
317 *
318 * If something went wrong with PCI, this whole exercise would be
319 * futile as VideoCore expects from us a configured PCI bus. Just take
320 * the faulty version (likely ~0) and let xHCI's registration fail
321 * further down the line.
322 */
323 pci_read_config_dword(pdev, VL805_PCI_CONFIG_VERSION_OFFSET, &version);
324 if (version)
325 goto exit;
326
327 dev_addr = pdev->bus->number << 20 | PCI_SLOT(pdev->devfn) << 15 |
328 PCI_FUNC(pdev->devfn) << 12;
329
330 ret = rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_XHCI_RESET,
331 &dev_addr, sizeof(dev_addr));
332 if (ret)
333 return ret;
334
335 /* Wait for vl805 to startup */
336 usleep_range(200, 1000);
337
338 pci_read_config_dword(pdev, VL805_PCI_CONFIG_VERSION_OFFSET,
339 &version);
340exit:
341 pci_info(pdev, "VL805 firmware version %08x\n", version);
342
343 return 0;
344}
345EXPORT_SYMBOL_GPL(rpi_firmware_init_vl805);
346
Eric Anholt4e3d6062015-02-26 10:08:06 +0000347static const struct of_device_id rpi_firmware_of_match[] = {
348 { .compatible = "raspberrypi,bcm2835-firmware", },
349 {},
350};
351MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
352
353static struct platform_driver rpi_firmware_driver = {
354 .driver = {
355 .name = "raspberrypi-firmware",
356 .of_match_table = rpi_firmware_of_match,
357 },
358 .probe = rpi_firmware_probe,
Stefan Wahrenb80ec7c2018-12-07 19:21:11 +0100359 .shutdown = rpi_firmware_shutdown,
Eric Anholt4e3d6062015-02-26 10:08:06 +0000360 .remove = rpi_firmware_remove,
361};
362module_platform_driver(rpi_firmware_driver);
363
364MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
365MODULE_DESCRIPTION("Raspberry Pi firmware driver");
366MODULE_LICENSE("GPL v2");