blob: 61be15d9df7dc2737ab4d1841356de4004e75836 [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>
Eric Anholt4e3d6062015-02-26 10:08:06 +000015#include <soc/bcm2835/raspberrypi-firmware.h>
16
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
21
Stefan Wahren70eea1b2018-05-25 21:24:36 +020022static struct platform_device *rpi_hwmon;
23
Eric Anholt4e3d6062015-02-26 10:08:06 +000024struct rpi_firmware {
25 struct mbox_client cl;
26 struct mbox_chan *chan; /* The property channel. */
27 struct completion c;
28 u32 enabled;
29};
30
31static DEFINE_MUTEX(transaction_lock);
32
33static void response_callback(struct mbox_client *cl, void *msg)
34{
35 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
36 complete(&fw->c);
37}
38
39/*
40 * Sends a request to the firmware through the BCM2835 mailbox driver,
41 * and synchronously waits for the reply.
42 */
43static int
44rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
45{
46 u32 message = MBOX_MSG(chan, data);
47 int ret;
48
49 WARN_ON(data & 0xf);
50
51 mutex_lock(&transaction_lock);
52 reinit_completion(&fw->c);
53 ret = mbox_send_message(fw->chan, &message);
54 if (ret >= 0) {
Stefan Wahren08291872018-10-06 13:31:56 +020055 if (wait_for_completion_timeout(&fw->c, HZ)) {
56 ret = 0;
57 } else {
58 ret = -ETIMEDOUT;
59 WARN_ONCE(1, "Firmware transaction timeout");
60 }
Eric Anholt4e3d6062015-02-26 10:08:06 +000061 } else {
62 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
63 }
64 mutex_unlock(&transaction_lock);
65
66 return ret;
67}
68
69/**
70 * rpi_firmware_property_list - Submit firmware property list
71 * @fw: Pointer to firmware structure from rpi_firmware_get().
72 * @data: Buffer holding tags.
73 * @tag_size: Size of tags buffer.
74 *
75 * Submits a set of concatenated tags to the VPU firmware through the
76 * mailbox property interface.
77 *
78 * The buffer header and the ending tag are added by this function and
79 * don't need to be supplied, just the actual tags for your operation.
80 * See struct rpi_firmware_property_tag_header for the per-tag
81 * structure.
82 */
83int rpi_firmware_property_list(struct rpi_firmware *fw,
84 void *data, size_t tag_size)
85{
86 size_t size = tag_size + 12;
87 u32 *buf;
88 dma_addr_t bus_addr;
89 int ret;
90
91 /* Packets are processed a dword at a time. */
92 if (size & 3)
93 return -EINVAL;
94
95 buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
96 GFP_ATOMIC);
97 if (!buf)
98 return -ENOMEM;
99
100 /* The firmware will error out without parsing in this case. */
101 WARN_ON(size >= 1024 * 1024);
102
103 buf[0] = size;
104 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
105 memcpy(&buf[2], data, tag_size);
106 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
107 wmb();
108
109 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
110
111 rmb();
112 memcpy(data, &buf[2], tag_size);
113 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
114 /*
115 * The tag name here might not be the one causing the
116 * error, if there were multiple tags in the request.
117 * But single-tag is the most common, so go with it.
118 */
119 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
120 buf[2], buf[1]);
121 ret = -EINVAL;
122 }
123
124 dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
125
126 return ret;
127}
128EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
129
130/**
131 * rpi_firmware_property - Submit single firmware property
132 * @fw: Pointer to firmware structure from rpi_firmware_get().
133 * @tag: One of enum_mbox_property_tag.
134 * @tag_data: Tag data buffer.
135 * @buf_size: Buffer size.
136 *
137 * Submits a single tag to the VPU firmware through the mailbox
138 * property interface.
139 *
140 * This is a convenience wrapper around
141 * rpi_firmware_property_list() to avoid some of the
142 * boilerplate in property calls.
143 */
144int rpi_firmware_property(struct rpi_firmware *fw,
145 u32 tag, void *tag_data, size_t buf_size)
146{
James Hughes91c6ada2018-11-16 14:39:07 +0000147 struct rpi_firmware_property_tag_header *header;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000148 int ret;
149
James Hughes91c6ada2018-11-16 14:39:07 +0000150 /* Some mailboxes can use over 1k bytes. Rather than checking
151 * size and using stack or kmalloc depending on requirements,
152 * just use kmalloc. Mailboxes don't get called enough to worry
153 * too much about the time taken in the allocation.
154 */
155 void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
Kees Cooka1547e02018-06-29 11:44:50 -0700156
James Hughes91c6ada2018-11-16 14:39:07 +0000157 if (!data)
158 return -ENOMEM;
159
160 header = data;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000161 header->tag = tag;
162 header->buf_size = buf_size;
163 header->req_resp_size = 0;
James Hughes91c6ada2018-11-16 14:39:07 +0000164 memcpy(data + sizeof(*header), tag_data, buf_size);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000165
James Hughes91c6ada2018-11-16 14:39:07 +0000166 ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
167
168 memcpy(tag_data, data + sizeof(*header), buf_size);
169
170 kfree(data);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000171
172 return ret;
173}
174EXPORT_SYMBOL_GPL(rpi_firmware_property);
175
176static void
177rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
178{
179 u32 packet;
180 int ret = rpi_firmware_property(fw,
181 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
182 &packet, sizeof(packet));
183
184 if (ret == 0) {
185 struct tm tm;
186
Arnd Bergmann9107ee62017-11-27 12:57:14 +0100187 time64_to_tm(packet, 0, &tm);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000188
189 dev_info(fw->cl.dev,
190 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
191 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
192 tm.tm_hour, tm.tm_min);
193 }
194}
195
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200196static void
197rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
198{
199 u32 packet;
200 int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
201 &packet, sizeof(packet));
202
203 if (ret)
204 return;
205
206 rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
207 -1, NULL, 0);
208}
209
Eric Anholt4e3d6062015-02-26 10:08:06 +0000210static int rpi_firmware_probe(struct platform_device *pdev)
211{
212 struct device *dev = &pdev->dev;
213 struct rpi_firmware *fw;
214
215 fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
216 if (!fw)
217 return -ENOMEM;
218
219 fw->cl.dev = dev;
220 fw->cl.rx_callback = response_callback;
221 fw->cl.tx_block = true;
222
223 fw->chan = mbox_request_channel(&fw->cl, 0);
224 if (IS_ERR(fw->chan)) {
225 int ret = PTR_ERR(fw->chan);
226 if (ret != -EPROBE_DEFER)
227 dev_err(dev, "Failed to get mbox channel: %d\n", ret);
228 return ret;
229 }
230
231 init_completion(&fw->c);
232
233 platform_set_drvdata(pdev, fw);
234
235 rpi_firmware_print_firmware_revision(fw);
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200236 rpi_register_hwmon_driver(dev, fw);
Eric Anholt4e3d6062015-02-26 10:08:06 +0000237
238 return 0;
239}
240
Stefan Wahrenb80ec7c2018-12-07 19:21:11 +0100241static void rpi_firmware_shutdown(struct platform_device *pdev)
242{
243 struct rpi_firmware *fw = platform_get_drvdata(pdev);
244
245 if (!fw)
246 return;
247
248 rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
249}
250
Eric Anholt4e3d6062015-02-26 10:08:06 +0000251static int rpi_firmware_remove(struct platform_device *pdev)
252{
253 struct rpi_firmware *fw = platform_get_drvdata(pdev);
254
Stefan Wahren70eea1b2018-05-25 21:24:36 +0200255 platform_device_unregister(rpi_hwmon);
256 rpi_hwmon = NULL;
Eric Anholt4e3d6062015-02-26 10:08:06 +0000257 mbox_free_channel(fw->chan);
258
259 return 0;
260}
261
262/**
263 * rpi_firmware_get - Get pointer to rpi_firmware structure.
264 * @firmware_node: Pointer to the firmware Device Tree node.
265 *
266 * Returns NULL is the firmware device is not ready.
267 */
268struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
269{
270 struct platform_device *pdev = of_find_device_by_node(firmware_node);
271
272 if (!pdev)
273 return NULL;
274
275 return platform_get_drvdata(pdev);
276}
277EXPORT_SYMBOL_GPL(rpi_firmware_get);
278
279static const struct of_device_id rpi_firmware_of_match[] = {
280 { .compatible = "raspberrypi,bcm2835-firmware", },
281 {},
282};
283MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
284
285static struct platform_driver rpi_firmware_driver = {
286 .driver = {
287 .name = "raspberrypi-firmware",
288 .of_match_table = rpi_firmware_of_match,
289 },
290 .probe = rpi_firmware_probe,
Stefan Wahrenb80ec7c2018-12-07 19:21:11 +0100291 .shutdown = rpi_firmware_shutdown,
Eric Anholt4e3d6062015-02-26 10:08:06 +0000292 .remove = rpi_firmware_remove,
293};
294module_platform_driver(rpi_firmware_driver);
295
296MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
297MODULE_DESCRIPTION("Raspberry Pi firmware driver");
298MODULE_LICENSE("GPL v2");