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 <linux/slab.h>
18 #include <soc/bcm2835/raspberrypi-firmware.h>
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
25 static struct platform_device
*rpi_hwmon
;
28 struct mbox_client cl
;
29 struct mbox_chan
*chan
; /* The property channel. */
34 static DEFINE_MUTEX(transaction_lock
);
36 static void response_callback(struct mbox_client
*cl
, void *msg
)
38 struct rpi_firmware
*fw
= container_of(cl
, struct rpi_firmware
, cl
);
43 * Sends a request to the firmware through the BCM2835 mailbox driver,
44 * and synchronously waits for the reply.
47 rpi_firmware_transaction(struct rpi_firmware
*fw
, u32 chan
, u32 data
)
49 u32 message
= MBOX_MSG(chan
, data
);
54 mutex_lock(&transaction_lock
);
55 reinit_completion(&fw
->c
);
56 ret
= mbox_send_message(fw
->chan
, &message
);
58 wait_for_completion(&fw
->c
);
61 dev_err(fw
->cl
.dev
, "mbox_send_message returned %d\n", ret
);
63 mutex_unlock(&transaction_lock
);
69 * rpi_firmware_property_list - Submit firmware property list
70 * @fw: Pointer to firmware structure from rpi_firmware_get().
71 * @data: Buffer holding tags.
72 * @tag_size: Size of tags buffer.
74 * Submits a set of concatenated tags to the VPU firmware through the
75 * mailbox property interface.
77 * The buffer header and the ending tag are added by this function and
78 * don't need to be supplied, just the actual tags for your operation.
79 * See struct rpi_firmware_property_tag_header for the per-tag
82 int rpi_firmware_property_list(struct rpi_firmware
*fw
,
83 void *data
, size_t tag_size
)
85 size_t size
= tag_size
+ 12;
90 /* Packets are processed a dword at a time. */
94 buf
= dma_alloc_coherent(fw
->cl
.dev
, PAGE_ALIGN(size
), &bus_addr
,
99 /* The firmware will error out without parsing in this case. */
100 WARN_ON(size
>= 1024 * 1024);
103 buf
[1] = RPI_FIRMWARE_STATUS_REQUEST
;
104 memcpy(&buf
[2], data
, tag_size
);
105 buf
[size
/ 4 - 1] = RPI_FIRMWARE_PROPERTY_END
;
108 ret
= rpi_firmware_transaction(fw
, MBOX_CHAN_PROPERTY
, bus_addr
);
111 memcpy(data
, &buf
[2], tag_size
);
112 if (ret
== 0 && buf
[1] != RPI_FIRMWARE_STATUS_SUCCESS
) {
114 * The tag name here might not be the one causing the
115 * error, if there were multiple tags in the request.
116 * But single-tag is the most common, so go with it.
118 dev_err(fw
->cl
.dev
, "Request 0x%08x returned status 0x%08x\n",
123 dma_free_coherent(fw
->cl
.dev
, PAGE_ALIGN(size
), buf
, bus_addr
);
127 EXPORT_SYMBOL_GPL(rpi_firmware_property_list
);
130 * rpi_firmware_property - Submit single firmware property
131 * @fw: Pointer to firmware structure from rpi_firmware_get().
132 * @tag: One of enum_mbox_property_tag.
133 * @tag_data: Tag data buffer.
134 * @buf_size: Buffer size.
136 * Submits a single tag to the VPU firmware through the mailbox
137 * property interface.
139 * This is a convenience wrapper around
140 * rpi_firmware_property_list() to avoid some of the
141 * boilerplate in property calls.
143 int rpi_firmware_property(struct rpi_firmware
*fw
,
144 u32 tag
, void *tag_data
, size_t buf_size
)
146 struct rpi_firmware_property_tag_header
*header
;
149 /* Some mailboxes can use over 1k bytes. Rather than checking
150 * size and using stack or kmalloc depending on requirements,
151 * just use kmalloc. Mailboxes don't get called enough to worry
152 * too much about the time taken in the allocation.
154 void *data
= kmalloc(sizeof(*header
) + buf_size
, GFP_KERNEL
);
161 header
->buf_size
= buf_size
;
162 header
->req_resp_size
= 0;
163 memcpy(data
+ sizeof(*header
), tag_data
, buf_size
);
165 ret
= rpi_firmware_property_list(fw
, data
, buf_size
+ sizeof(*header
));
167 memcpy(tag_data
, data
+ sizeof(*header
), buf_size
);
173 EXPORT_SYMBOL_GPL(rpi_firmware_property
);
176 rpi_firmware_print_firmware_revision(struct rpi_firmware
*fw
)
179 int ret
= rpi_firmware_property(fw
,
180 RPI_FIRMWARE_GET_FIRMWARE_REVISION
,
181 &packet
, sizeof(packet
));
186 time64_to_tm(packet
, 0, &tm
);
189 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
190 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
191 tm
.tm_hour
, tm
.tm_min
);
196 rpi_register_hwmon_driver(struct device
*dev
, struct rpi_firmware
*fw
)
199 int ret
= rpi_firmware_property(fw
, RPI_FIRMWARE_GET_THROTTLED
,
200 &packet
, sizeof(packet
));
205 rpi_hwmon
= platform_device_register_data(dev
, "raspberrypi-hwmon",
209 static int rpi_firmware_probe(struct platform_device
*pdev
)
211 struct device
*dev
= &pdev
->dev
;
212 struct rpi_firmware
*fw
;
214 fw
= devm_kzalloc(dev
, sizeof(*fw
), GFP_KERNEL
);
219 fw
->cl
.rx_callback
= response_callback
;
220 fw
->cl
.tx_block
= true;
222 fw
->chan
= mbox_request_channel(&fw
->cl
, 0);
223 if (IS_ERR(fw
->chan
)) {
224 int ret
= PTR_ERR(fw
->chan
);
225 if (ret
!= -EPROBE_DEFER
)
226 dev_err(dev
, "Failed to get mbox channel: %d\n", ret
);
230 init_completion(&fw
->c
);
232 platform_set_drvdata(pdev
, fw
);
234 rpi_firmware_print_firmware_revision(fw
);
235 rpi_register_hwmon_driver(dev
, fw
);
240 static int rpi_firmware_remove(struct platform_device
*pdev
)
242 struct rpi_firmware
*fw
= platform_get_drvdata(pdev
);
244 platform_device_unregister(rpi_hwmon
);
246 mbox_free_channel(fw
->chan
);
252 * rpi_firmware_get - Get pointer to rpi_firmware structure.
253 * @firmware_node: Pointer to the firmware Device Tree node.
255 * Returns NULL is the firmware device is not ready.
257 struct rpi_firmware
*rpi_firmware_get(struct device_node
*firmware_node
)
259 struct platform_device
*pdev
= of_find_device_by_node(firmware_node
);
264 return platform_get_drvdata(pdev
);
266 EXPORT_SYMBOL_GPL(rpi_firmware_get
);
268 static const struct of_device_id rpi_firmware_of_match
[] = {
269 { .compatible
= "raspberrypi,bcm2835-firmware", },
272 MODULE_DEVICE_TABLE(of
, rpi_firmware_of_match
);
274 static struct platform_driver rpi_firmware_driver
= {
276 .name
= "raspberrypi-firmware",
277 .of_match_table
= rpi_firmware_of_match
,
279 .probe
= rpi_firmware_probe
,
280 .remove
= rpi_firmware_remove
,
282 module_platform_driver(rpi_firmware_driver
);
284 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
285 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
286 MODULE_LICENSE("GPL v2");