]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/firmware/raspberrypi.c
raspberrypi-firmware: Export the general transaction function.
[mirror_ubuntu-artful-kernel.git] / drivers / firmware / raspberrypi.c
CommitLineData
4e3d6065
EA
1/*
2 * Defines interfaces for interacting wtih the Raspberry Pi firmware's
3 * property channel.
4 *
5 * Copyright © 2015 Broadcom
6 *
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.
10 */
11
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>
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
24struct rpi_firmware {
25 struct mbox_client cl;
26 struct mbox_chan *chan; /* The property channel. */
27 struct completion c;
28 u32 enabled;
29};
30
874b518a
NT
31static struct platform_device *g_pdev;
32
4e3d6065
EA
33static DEFINE_MUTEX(transaction_lock);
34
35static void response_callback(struct mbox_client *cl, void *msg)
36{
37 struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
38 complete(&fw->c);
39}
40
41/*
42 * Sends a request to the firmware through the BCM2835 mailbox driver,
43 * and synchronously waits for the reply.
44 */
95cda0b9 45int
4e3d6065
EA
46rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
47{
48 u32 message = MBOX_MSG(chan, data);
49 int ret;
50
51 WARN_ON(data & 0xf);
52
53 mutex_lock(&transaction_lock);
54 reinit_completion(&fw->c);
55 ret = mbox_send_message(fw->chan, &message);
56 if (ret >= 0) {
57 wait_for_completion(&fw->c);
58 ret = 0;
59 } else {
60 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
61 }
62 mutex_unlock(&transaction_lock);
63
64 return ret;
65}
95cda0b9 66EXPORT_SYMBOL_GPL(rpi_firmware_transaction);
4e3d6065
EA
67
68/**
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.
73 *
74 * Submits a set of concatenated tags to the VPU firmware through the
75 * mailbox property interface.
76 *
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
80 * structure.
81 */
82int rpi_firmware_property_list(struct rpi_firmware *fw,
83 void *data, size_t tag_size)
84{
85 size_t size = tag_size + 12;
86 u32 *buf;
87 dma_addr_t bus_addr;
88 int ret;
89
90 /* Packets are processed a dword at a time. */
91 if (size & 3)
92 return -EINVAL;
93
94 buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
95 GFP_ATOMIC);
96 if (!buf)
97 return -ENOMEM;
98
99 /* The firmware will error out without parsing in this case. */
100 WARN_ON(size >= 1024 * 1024);
101
102 buf[0] = size;
103 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
104 memcpy(&buf[2], data, tag_size);
105 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
106 wmb();
107
108 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
109
110 rmb();
111 memcpy(data, &buf[2], tag_size);
112 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
113 /*
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.
117 */
118 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
119 buf[2], buf[1]);
120 ret = -EINVAL;
121 }
122
123 dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
124
125 return ret;
126}
127EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
128
129/**
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.
135 *
136 * Submits a single tag to the VPU firmware through the mailbox
137 * property interface.
138 *
139 * This is a convenience wrapper around
140 * rpi_firmware_property_list() to avoid some of the
141 * boilerplate in property calls.
142 */
143int rpi_firmware_property(struct rpi_firmware *fw,
144 u32 tag, void *tag_data, size_t buf_size)
145{
146 /* Single tags are very small (generally 8 bytes), so the
147 * stack should be safe.
148 */
149 u8 data[buf_size + sizeof(struct rpi_firmware_property_tag_header)];
150 struct rpi_firmware_property_tag_header *header =
151 (struct rpi_firmware_property_tag_header *)data;
152 int ret;
153
154 header->tag = tag;
155 header->buf_size = buf_size;
156 header->req_resp_size = 0;
157 memcpy(data + sizeof(struct rpi_firmware_property_tag_header),
158 tag_data, buf_size);
159
160 ret = rpi_firmware_property_list(fw, &data, sizeof(data));
161 memcpy(tag_data,
162 data + sizeof(struct rpi_firmware_property_tag_header),
163 buf_size);
164
165 return ret;
166}
167EXPORT_SYMBOL_GPL(rpi_firmware_property);
168
169static void
170rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
171{
172 u32 packet;
173 int ret = rpi_firmware_property(fw,
174 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
175 &packet, sizeof(packet));
176
177 if (ret == 0) {
178 struct tm tm;
179
180 time_to_tm(packet, 0, &tm);
181
182 dev_info(fw->cl.dev,
183 "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
184 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
185 tm.tm_hour, tm.tm_min);
186 }
187}
188
189static int rpi_firmware_probe(struct platform_device *pdev)
190{
191 struct device *dev = &pdev->dev;
192 struct rpi_firmware *fw;
193
194 fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
195 if (!fw)
196 return -ENOMEM;
197
198 fw->cl.dev = dev;
199 fw->cl.rx_callback = response_callback;
200 fw->cl.tx_block = true;
201
202 fw->chan = mbox_request_channel(&fw->cl, 0);
203 if (IS_ERR(fw->chan)) {
204 int ret = PTR_ERR(fw->chan);
205 if (ret != -EPROBE_DEFER)
206 dev_err(dev, "Failed to get mbox channel: %d\n", ret);
207 return ret;
208 }
209
210 init_completion(&fw->c);
211
212 platform_set_drvdata(pdev, fw);
874b518a 213 g_pdev = pdev;
4e3d6065
EA
214
215 rpi_firmware_print_firmware_revision(fw);
216
217 return 0;
218}
219
220static int rpi_firmware_remove(struct platform_device *pdev)
221{
222 struct rpi_firmware *fw = platform_get_drvdata(pdev);
223
224 mbox_free_channel(fw->chan);
874b518a 225 g_pdev = NULL;
4e3d6065
EA
226
227 return 0;
228}
229
230/**
231 * rpi_firmware_get - Get pointer to rpi_firmware structure.
232 * @firmware_node: Pointer to the firmware Device Tree node.
233 *
234 * Returns NULL is the firmware device is not ready.
235 */
236struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
237{
874b518a 238 struct platform_device *pdev = g_pdev;
4e3d6065
EA
239
240 if (!pdev)
241 return NULL;
242
243 return platform_get_drvdata(pdev);
244}
245EXPORT_SYMBOL_GPL(rpi_firmware_get);
246
247static const struct of_device_id rpi_firmware_of_match[] = {
248 { .compatible = "raspberrypi,bcm2835-firmware", },
249 {},
250};
251MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
252
253static struct platform_driver rpi_firmware_driver = {
254 .driver = {
255 .name = "raspberrypi-firmware",
256 .of_match_table = rpi_firmware_of_match,
257 },
258 .probe = rpi_firmware_probe,
259 .remove = rpi_firmware_remove,
260};
874b518a
NT
261
262static int __init rpi_firmware_init(void)
263{
264 return platform_driver_register(&rpi_firmware_driver);
265}
266subsys_initcall(rpi_firmware_init);
267
268static void __init rpi_firmware_exit(void)
269{
270 platform_driver_unregister(&rpi_firmware_driver);
271}
272module_exit(rpi_firmware_exit);
4e3d6065
EA
273
274MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
275MODULE_DESCRIPTION("Raspberry Pi firmware driver");
276MODULE_LICENSE("GPL v2");