brcm2708: update 4.1 patches
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0077-ARM-bcm2835-Add-the-Raspberry-Pi-firmware-driver.patch
1 From dc305fc3389b68e35f7ef905884073eb947e52f3 Mon Sep 17 00:00:00 2001
2 From: Eric Anholt <eric@anholt.net>
3 Date: Thu, 4 Jun 2015 13:11:46 -0700
4 Subject: [PATCH 077/148] ARM: bcm2835: Add the Raspberry Pi firmware driver
5
6 This gives us a function for making mailbox property channel requests
7 of the firmware, which is most notable in that it will let us get and
8 set clock rates.
9
10 Signed-off-by: Eric Anholt <eric@anholt.net>
11 ---
12 drivers/firmware/Kconfig | 7 +
13 drivers/firmware/Makefile | 1 +
14 drivers/firmware/raspberrypi.c | 260 +++++++++++++++++++++++++++++
15 include/soc/bcm2835/raspberrypi-firmware.h | 115 +++++++++++++
16 4 files changed, 383 insertions(+)
17 create mode 100644 drivers/firmware/raspberrypi.c
18 create mode 100644 include/soc/bcm2835/raspberrypi-firmware.h
19
20 --- a/drivers/firmware/Kconfig
21 +++ b/drivers/firmware/Kconfig
22 @@ -136,6 +136,13 @@ config QCOM_SCM
23 bool
24 depends on ARM || ARM64
25
26 +config RASPBERRYPI_FIRMWARE
27 + tristate "Raspberry Pi Firmware Driver"
28 + depends on BCM2835_MBOX
29 + help
30 + This option enables support for communicating with the firmware on the
31 + Raspberry Pi.
32 +
33 source "drivers/firmware/google/Kconfig"
34 source "drivers/firmware/efi/Kconfig"
35
36 --- a/drivers/firmware/Makefile
37 +++ b/drivers/firmware/Makefile
38 @@ -13,6 +13,7 @@ obj-$(CONFIG_ISCSI_IBFT) += iscsi_ibft.o
39 obj-$(CONFIG_FIRMWARE_MEMMAP) += memmap.o
40 obj-$(CONFIG_QCOM_SCM) += qcom_scm.o
41 CFLAGS_qcom_scm.o :=$(call as-instr,.arch_extension sec,-DREQUIRES_SEC=1)
42 +obj-$(CONFIG_RASPBERRYPI_FIRMWARE) += raspberrypi.o
43
44 obj-$(CONFIG_GOOGLE_FIRMWARE) += google/
45 obj-$(CONFIG_EFI) += efi/
46 --- /dev/null
47 +++ b/drivers/firmware/raspberrypi.c
48 @@ -0,0 +1,260 @@
49 +/*
50 + * Defines interfaces for interacting wtih the Raspberry Pi firmware's
51 + * property channel.
52 + *
53 + * Copyright © 2015 Broadcom
54 + *
55 + * This program is free software; you can redistribute it and/or modify
56 + * it under the terms of the GNU General Public License version 2 as
57 + * published by the Free Software Foundation.
58 + */
59 +
60 +#include <linux/dma-mapping.h>
61 +#include <linux/mailbox_client.h>
62 +#include <linux/module.h>
63 +#include <linux/of_platform.h>
64 +#include <linux/platform_device.h>
65 +#include <soc/bcm2835/raspberrypi-firmware.h>
66 +
67 +#define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
68 +#define MBOX_CHAN(msg) ((msg) & 0xf)
69 +#define MBOX_DATA28(msg) ((msg) & ~0xf)
70 +#define MBOX_CHAN_PROPERTY 8
71 +
72 +struct rpi_firmware {
73 + struct mbox_client cl;
74 + struct mbox_chan *chan; /* The property channel. */
75 + struct completion c;
76 + u32 enabled;
77 +};
78 +
79 +static DEFINE_MUTEX(transaction_lock);
80 +
81 +static void response_callback(struct mbox_client *cl, void *msg)
82 +{
83 + struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
84 + complete(&fw->c);
85 +}
86 +
87 +/*
88 + * Sends a request to the firmware through the BCM2835 mailbox driver,
89 + * and synchronously waits for the reply.
90 + */
91 +static int
92 +rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
93 +{
94 + u32 message = MBOX_MSG(chan, data);
95 + int ret;
96 +
97 + WARN_ON(data & 0xf);
98 +
99 + mutex_lock(&transaction_lock);
100 + reinit_completion(&fw->c);
101 + ret = mbox_send_message(fw->chan, &message);
102 + if (ret >= 0) {
103 + wait_for_completion(&fw->c);
104 + ret = 0;
105 + } else {
106 + dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
107 + }
108 + mutex_unlock(&transaction_lock);
109 +
110 + return ret;
111 +}
112 +
113 +/**
114 + * rpi_firmware_property_list - Submit firmware property list
115 + * @fw: Pointer to firmware structure from rpi_firmware_get().
116 + * @data: Buffer holding tags.
117 + * @tag_size: Size of tags buffer.
118 + *
119 + * Submits a set of concatenated tags to the VPU firmware through the
120 + * mailbox property interface.
121 + *
122 + * The buffer header and the ending tag are added by this function and
123 + * don't need to be supplied, just the actual tags for your operation.
124 + * See struct rpi_firmware_property_tag_header for the per-tag
125 + * structure.
126 + */
127 +int rpi_firmware_property_list(struct rpi_firmware *fw,
128 + void *data, size_t tag_size)
129 +{
130 + size_t size = tag_size + 12;
131 + u32 *buf;
132 + dma_addr_t bus_addr;
133 + int ret;
134 +
135 + /* Packets are processed a dword at a time. */
136 + if (size & 3)
137 + return -EINVAL;
138 +
139 + buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
140 + GFP_ATOMIC);
141 + if (!buf)
142 + return -ENOMEM;
143 +
144 + /* The firmware will error out without parsing in this case. */
145 + WARN_ON(size >= 1024 * 1024);
146 +
147 + buf[0] = size;
148 + buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
149 + memcpy(&buf[2], data, tag_size);
150 + buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
151 + wmb();
152 +
153 + ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
154 +
155 + rmb();
156 + memcpy(data, &buf[2], tag_size);
157 + if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
158 + /*
159 + * The tag name here might not be the one causing the
160 + * error, if there were multiple tags in the request.
161 + * But single-tag is the most common, so go with it.
162 + */
163 + dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
164 + buf[2], buf[1]);
165 + ret = -EINVAL;
166 + }
167 +
168 + dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
169 +
170 + return ret;
171 +}
172 +EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
173 +
174 +/**
175 + * rpi_firmware_property - Submit single firmware property
176 + * @fw: Pointer to firmware structure from rpi_firmware_get().
177 + * @tag: One of enum_mbox_property_tag.
178 + * @tag_data: Tag data buffer.
179 + * @buf_size: Buffer size.
180 + *
181 + * Submits a single tag to the VPU firmware through the mailbox
182 + * property interface.
183 + *
184 + * This is a convenience wrapper around
185 + * rpi_firmware_property_list() to avoid some of the
186 + * boilerplate in property calls.
187 + */
188 +int rpi_firmware_property(struct rpi_firmware *fw,
189 + u32 tag, void *tag_data, size_t buf_size)
190 +{
191 + /* Single tags are very small (generally 8 bytes), so the
192 + * stack should be safe.
193 + */
194 + u8 data[buf_size + sizeof(struct rpi_firmware_property_tag_header)];
195 + struct rpi_firmware_property_tag_header *header =
196 + (struct rpi_firmware_property_tag_header *)data;
197 + int ret;
198 +
199 + header->tag = tag;
200 + header->buf_size = buf_size;
201 + header->req_resp_size = 0;
202 + memcpy(data + sizeof(struct rpi_firmware_property_tag_header),
203 + tag_data, buf_size);
204 +
205 + ret = rpi_firmware_property_list(fw, &data, sizeof(data));
206 + memcpy(tag_data,
207 + data + sizeof(struct rpi_firmware_property_tag_header),
208 + buf_size);
209 +
210 + return ret;
211 +}
212 +EXPORT_SYMBOL_GPL(rpi_firmware_property);
213 +
214 +static void
215 +rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
216 +{
217 + u32 packet;
218 + int ret = rpi_firmware_property(fw,
219 + RPI_FIRMWARE_GET_FIRMWARE_REVISION,
220 + &packet, sizeof(packet));
221 +
222 + if (ret == 0) {
223 + struct tm tm;
224 +
225 + time_to_tm(packet, 0, &tm);
226 +
227 + dev_info(fw->cl.dev,
228 + "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
229 + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
230 + tm.tm_hour, tm.tm_min);
231 + }
232 +}
233 +
234 +static int rpi_firmware_probe(struct platform_device *pdev)
235 +{
236 + struct device *dev = &pdev->dev;
237 + struct rpi_firmware *fw;
238 +
239 + fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
240 + if (!fw)
241 + return -ENOMEM;
242 +
243 + fw->cl.dev = dev;
244 + fw->cl.rx_callback = response_callback;
245 + fw->cl.tx_block = true;
246 +
247 + fw->chan = mbox_request_channel(&fw->cl, 0);
248 + if (IS_ERR(fw->chan)) {
249 + int ret = PTR_ERR(fw->chan);
250 + if (ret != -EPROBE_DEFER)
251 + dev_err(dev, "Failed to get mbox channel: %d\n", ret);
252 + return ret;
253 + }
254 +
255 + init_completion(&fw->c);
256 +
257 + platform_set_drvdata(pdev, fw);
258 +
259 + rpi_firmware_print_firmware_revision(fw);
260 +
261 + return 0;
262 +}
263 +
264 +static int rpi_firmware_remove(struct platform_device *pdev)
265 +{
266 + struct rpi_firmware *fw = platform_get_drvdata(pdev);
267 +
268 + 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 + */
279 +struct 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 +}
288 +EXPORT_SYMBOL_GPL(rpi_firmware_get);
289 +
290 +static const struct of_device_id rpi_firmware_of_match[] = {
291 + { .compatible = "raspberrypi,bcm2835-firmware", },
292 + {},
293 +};
294 +MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
295 +
296 +static struct platform_driver rpi_firmware_driver = {
297 + .driver = {
298 + .name = "raspberrypi-firmware",
299 + .of_match_table = rpi_firmware_of_match,
300 + },
301 + .probe = rpi_firmware_probe,
302 + .remove = rpi_firmware_remove,
303 +};
304 +module_platform_driver(rpi_firmware_driver);
305 +
306 +MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
307 +MODULE_DESCRIPTION("Raspberry Pi firmware driver");
308 +MODULE_LICENSE("GPL v2");
309 --- /dev/null
310 +++ b/include/soc/bcm2835/raspberrypi-firmware.h
311 @@ -0,0 +1,115 @@
312 +/*
313 + * Copyright © 2015 Broadcom
314 + *
315 + * This program is free software; you can redistribute it and/or modify
316 + * it under the terms of the GNU General Public License version 2 as
317 + * published by the Free Software Foundation.
318 + */
319 +
320 +#include <linux/types.h>
321 +#include <linux/of_device.h>
322 +
323 +struct rpi_firmware;
324 +
325 +enum rpi_firmware_property_status {
326 + RPI_FIRMWARE_STATUS_REQUEST = 0,
327 + RPI_FIRMWARE_STATUS_SUCCESS = 0x80000000,
328 + RPI_FIRMWARE_STATUS_ERROR = 0x80000001,
329 +};
330 +
331 +/**
332 + * struct rpi_firmware_property_tag_header - Firmware property tag header
333 + * @tag: One of enum_mbox_property_tag.
334 + * @buf_size: The number of bytes in the value buffer following this
335 + * struct.
336 + * @req_resp_size: On submit, the length of the request (though it doesn't
337 + * appear to be currently used by the firmware). On return,
338 + * the length of the response (always 4 byte aligned), with
339 + * the low bit set.
340 + */
341 +struct rpi_firmware_property_tag_header {
342 + u32 tag;
343 + u32 buf_size;
344 + u32 req_resp_size;
345 +};
346 +
347 +enum rpi_firmware_property_tag {
348 + RPI_FIRMWARE_PROPERTY_END = 0,
349 + RPI_FIRMWARE_GET_FIRMWARE_REVISION = 0x00000001,
350 +
351 + RPI_FIRMWARE_SET_CURSOR_INFO = 0x00008010,
352 + RPI_FIRMWARE_SET_CURSOR_STATE = 0x00008011,
353 +
354 + RPI_FIRMWARE_GET_BOARD_MODEL = 0x00010001,
355 + RPI_FIRMWARE_GET_BOARD_REVISION = 0x00010002,
356 + RPI_FIRMWARE_GET_BOARD_MAC_ADDRESS = 0x00010003,
357 + RPI_FIRMWARE_GET_BOARD_SERIAL = 0x00010004,
358 + RPI_FIRMWARE_GET_ARM_MEMORY = 0x00010005,
359 + RPI_FIRMWARE_GET_VC_MEMORY = 0x00010006,
360 + RPI_FIRMWARE_GET_CLOCKS = 0x00010007,
361 + RPI_FIRMWARE_GET_POWER_STATE = 0x00020001,
362 + RPI_FIRMWARE_GET_TIMING = 0x00020002,
363 + RPI_FIRMWARE_SET_POWER_STATE = 0x00028001,
364 + RPI_FIRMWARE_GET_CLOCK_STATE = 0x00030001,
365 + RPI_FIRMWARE_GET_CLOCK_RATE = 0x00030002,
366 + RPI_FIRMWARE_GET_VOLTAGE = 0x00030003,
367 + RPI_FIRMWARE_GET_MAX_CLOCK_RATE = 0x00030004,
368 + RPI_FIRMWARE_GET_MAX_VOLTAGE = 0x00030005,
369 + RPI_FIRMWARE_GET_TEMPERATURE = 0x00030006,
370 + RPI_FIRMWARE_GET_MIN_CLOCK_RATE = 0x00030007,
371 + RPI_FIRMWARE_GET_MIN_VOLTAGE = 0x00030008,
372 + RPI_FIRMWARE_GET_TURBO = 0x00030009,
373 + RPI_FIRMWARE_GET_MAX_TEMPERATURE = 0x0003000a,
374 + RPI_FIRMWARE_ALLOCATE_MEMORY = 0x0003000c,
375 + RPI_FIRMWARE_LOCK_MEMORY = 0x0003000d,
376 + RPI_FIRMWARE_UNLOCK_MEMORY = 0x0003000e,
377 + RPI_FIRMWARE_RELEASE_MEMORY = 0x0003000f,
378 + RPI_FIRMWARE_EXECUTE_CODE = 0x00030010,
379 + RPI_FIRMWARE_EXECUTE_QPU = 0x00030011,
380 + RPI_FIRMWARE_SET_ENABLE_QPU = 0x00030012,
381 + RPI_FIRMWARE_GET_DISPMANX_RESOURCE_MEM_HANDLE = 0x00030014,
382 + RPI_FIRMWARE_GET_EDID_BLOCK = 0x00030020,
383 + RPI_FIRMWARE_SET_CLOCK_STATE = 0x00038001,
384 + RPI_FIRMWARE_SET_CLOCK_RATE = 0x00038002,
385 + RPI_FIRMWARE_SET_VOLTAGE = 0x00038003,
386 + RPI_FIRMWARE_SET_TURBO = 0x00038009,
387 +
388 + /* Dispmanx TAGS */
389 + RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE = 0x00040001,
390 + RPI_FIRMWARE_FRAMEBUFFER_BLANK = 0x00040002,
391 + RPI_FIRMWARE_FRAMEBUFFER_GET_PHYSICAL_WIDTH_HEIGHT = 0x00040003,
392 + RPI_FIRMWARE_FRAMEBUFFER_GET_VIRTUAL_WIDTH_HEIGHT = 0x00040004,
393 + RPI_FIRMWARE_FRAMEBUFFER_GET_DEPTH = 0x00040005,
394 + RPI_FIRMWARE_FRAMEBUFFER_GET_PIXEL_ORDER = 0x00040006,
395 + RPI_FIRMWARE_FRAMEBUFFER_GET_ALPHA_MODE = 0x00040007,
396 + RPI_FIRMWARE_FRAMEBUFFER_GET_PITCH = 0x00040008,
397 + RPI_FIRMWARE_FRAMEBUFFER_GET_VIRTUAL_OFFSET = 0x00040009,
398 + RPI_FIRMWARE_FRAMEBUFFER_GET_OVERSCAN = 0x0004000a,
399 + RPI_FIRMWARE_FRAMEBUFFER_GET_PALETTE = 0x0004000b,
400 + RPI_FIRMWARE_FRAMEBUFFER_RELEASE = 0x00048001,
401 + RPI_FIRMWARE_FRAMEBUFFER_TEST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
402 + RPI_FIRMWARE_FRAMEBUFFER_TEST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,
403 + RPI_FIRMWARE_FRAMEBUFFER_TEST_DEPTH = 0x00044005,
404 + RPI_FIRMWARE_FRAMEBUFFER_TEST_PIXEL_ORDER = 0x00044006,
405 + RPI_FIRMWARE_FRAMEBUFFER_TEST_ALPHA_MODE = 0x00044007,
406 + RPI_FIRMWARE_FRAMEBUFFER_TEST_VIRTUAL_OFFSET = 0x00044009,
407 + RPI_FIRMWARE_FRAMEBUFFER_TEST_OVERSCAN = 0x0004400a,
408 + RPI_FIRMWARE_FRAMEBUFFER_TEST_PALETTE = 0x0004400b,
409 + RPI_FIRMWARE_FRAMEBUFFER_SET_PHYSICAL_WIDTH_HEIGHT = 0x00048003,
410 + RPI_FIRMWARE_FRAMEBUFFER_SET_VIRTUAL_WIDTH_HEIGHT = 0x00048004,
411 + RPI_FIRMWARE_FRAMEBUFFER_SET_DEPTH = 0x00048005,
412 + RPI_FIRMWARE_FRAMEBUFFER_SET_PIXEL_ORDER = 0x00048006,
413 + RPI_FIRMWARE_FRAMEBUFFER_SET_ALPHA_MODE = 0x00048007,
414 + RPI_FIRMWARE_FRAMEBUFFER_SET_VIRTUAL_OFFSET = 0x00048009,
415 + RPI_FIRMWARE_FRAMEBUFFER_SET_OVERSCAN = 0x0004800a,
416 + RPI_FIRMWARE_FRAMEBUFFER_SET_PALETTE = 0x0004800b,
417 +
418 + RPI_FIRMWARE_GET_COMMAND_LINE = 0x00050001,
419 + RPI_FIRMWARE_GET_DMA_CHANNELS = 0x00060001,
420 +};
421 +
422 +int rpi_firmware_property(struct rpi_firmware *fw,
423 + u32 tag, void *data, size_t len);
424 +int rpi_firmware_property_list(struct rpi_firmware *fw,
425 + void *data, size_t tag_size);
426 +struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);