2 * Defines interfaces for interacting wtih the Raspberry Pi firmware's
5 * Copyright © 2015 Broadcom
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/dma-mapping.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <soc/bcm2835/raspberrypi-firmware.h>
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
25 struct mbox_client cl
;
26 struct mbox_chan
*chan
; /* The property channel. */
31 static DEFINE_MUTEX(transaction_lock
);
33 static void response_callback(struct mbox_client
*cl
, void *msg
)
35 struct rpi_firmware
*fw
= container_of(cl
, struct rpi_firmware
, cl
);
40 * Sends a request to the firmware through the BCM2835 mailbox driver,
41 * and synchronously waits for the reply.
44 rpi_firmware_transaction(struct rpi_firmware
*fw
, u32 chan
, u32 data
)
46 u32 message
= MBOX_MSG(chan
, data
);
51 mutex_lock(&transaction_lock
);
52 reinit_completion(&fw
->c
);
53 ret
= mbox_send_message(fw
->chan
, &message
);
55 wait_for_completion(&fw
->c
);
58 dev_err(fw
->cl
.dev
, "mbox_send_message returned %d\n", ret
);
60 mutex_unlock(&transaction_lock
);
66 * rpi_firmware_property_list - Submit firmware property list
67 * @fw: Pointer to firmware structure from rpi_firmware_get().
68 * @data: Buffer holding tags.
69 * @tag_size: Size of tags buffer.
71 * Submits a set of concatenated tags to the VPU firmware through the
72 * mailbox property interface.
74 * The buffer header and the ending tag are added by this function and
75 * don't need to be supplied, just the actual tags for your operation.
76 * See struct rpi_firmware_property_tag_header for the per-tag
79 int rpi_firmware_property_list(struct rpi_firmware
*fw
,
80 void *data
, size_t tag_size
)
82 size_t size
= tag_size
+ 12;
87 /* Packets are processed a dword at a time. */
91 buf
= dma_alloc_coherent(fw
->cl
.dev
, PAGE_ALIGN(size
), &bus_addr
,
96 /* The firmware will error out without parsing in this case. */
97 WARN_ON(size
>= 1024 * 1024);
100 buf
[1] = RPI_FIRMWARE_STATUS_REQUEST
;
101 memcpy(&buf
[2], data
, tag_size
);
102 buf
[size
/ 4 - 1] = RPI_FIRMWARE_PROPERTY_END
;
105 ret
= rpi_firmware_transaction(fw
, MBOX_CHAN_PROPERTY
, bus_addr
);
108 memcpy(data
, &buf
[2], tag_size
);
109 if (ret
== 0 && buf
[1] != RPI_FIRMWARE_STATUS_SUCCESS
) {
111 * The tag name here might not be the one causing the
112 * error, if there were multiple tags in the request.
113 * But single-tag is the most common, so go with it.
115 dev_err(fw
->cl
.dev
, "Request 0x%08x returned status 0x%08x\n",
120 dma_free_coherent(fw
->cl
.dev
, PAGE_ALIGN(size
), buf
, bus_addr
);
124 EXPORT_SYMBOL_GPL(rpi_firmware_property_list
);
127 * rpi_firmware_property - Submit single firmware property
128 * @fw: Pointer to firmware structure from rpi_firmware_get().
129 * @tag: One of enum_mbox_property_tag.
130 * @tag_data: Tag data buffer.
131 * @buf_size: Buffer size.
133 * Submits a single tag to the VPU firmware through the mailbox
134 * property interface.
136 * This is a convenience wrapper around
137 * rpi_firmware_property_list() to avoid some of the
138 * boilerplate in property calls.
140 int rpi_firmware_property(struct rpi_firmware
*fw
,
141 u32 tag
, void *tag_data
, size_t buf_size
)
143 /* Single tags are very small (generally 8 bytes), so the
144 * stack should be safe.
146 u8 data
[buf_size
+ sizeof(struct rpi_firmware_property_tag_header
)];
147 struct rpi_firmware_property_tag_header
*header
=
148 (struct rpi_firmware_property_tag_header
*)data
;
152 header
->buf_size
= buf_size
;
153 header
->req_resp_size
= 0;
154 memcpy(data
+ sizeof(struct rpi_firmware_property_tag_header
),
157 ret
= rpi_firmware_property_list(fw
, &data
, sizeof(data
));
159 data
+ sizeof(struct rpi_firmware_property_tag_header
),
164 EXPORT_SYMBOL_GPL(rpi_firmware_property
);
167 rpi_firmware_print_firmware_revision(struct rpi_firmware
*fw
)
170 int ret
= rpi_firmware_property(fw
,
171 RPI_FIRMWARE_GET_FIRMWARE_REVISION
,
172 &packet
, sizeof(packet
));
177 time64_to_tm(packet
, 0, &tm
);
180 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
181 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
182 tm
.tm_hour
, tm
.tm_min
);
186 static int rpi_firmware_probe(struct platform_device
*pdev
)
188 struct device
*dev
= &pdev
->dev
;
189 struct rpi_firmware
*fw
;
191 fw
= devm_kzalloc(dev
, sizeof(*fw
), GFP_KERNEL
);
196 fw
->cl
.rx_callback
= response_callback
;
197 fw
->cl
.tx_block
= true;
199 fw
->chan
= mbox_request_channel(&fw
->cl
, 0);
200 if (IS_ERR(fw
->chan
)) {
201 int ret
= PTR_ERR(fw
->chan
);
202 if (ret
!= -EPROBE_DEFER
)
203 dev_err(dev
, "Failed to get mbox channel: %d\n", ret
);
207 init_completion(&fw
->c
);
209 platform_set_drvdata(pdev
, fw
);
211 rpi_firmware_print_firmware_revision(fw
);
216 static int rpi_firmware_remove(struct platform_device
*pdev
)
218 struct rpi_firmware
*fw
= platform_get_drvdata(pdev
);
220 mbox_free_channel(fw
->chan
);
226 * rpi_firmware_get - Get pointer to rpi_firmware structure.
227 * @firmware_node: Pointer to the firmware Device Tree node.
229 * Returns NULL is the firmware device is not ready.
231 struct rpi_firmware
*rpi_firmware_get(struct device_node
*firmware_node
)
233 struct platform_device
*pdev
= of_find_device_by_node(firmware_node
);
238 return platform_get_drvdata(pdev
);
240 EXPORT_SYMBOL_GPL(rpi_firmware_get
);
242 static const struct of_device_id rpi_firmware_of_match
[] = {
243 { .compatible
= "raspberrypi,bcm2835-firmware", },
246 MODULE_DEVICE_TABLE(of
, rpi_firmware_of_match
);
248 static struct platform_driver rpi_firmware_driver
= {
250 .name
= "raspberrypi-firmware",
251 .of_match_table
= rpi_firmware_of_match
,
253 .probe
= rpi_firmware_probe
,
254 .remove
= rpi_firmware_remove
,
256 module_platform_driver(rpi_firmware_driver
);
258 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
259 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
260 MODULE_LICENSE("GPL v2");