1
2
3
4
5
6
7
8
9
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;
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
41
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) {
55 wait_for_completion(&fw->c);
56 ret = 0;
57 } else {
58 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
59 }
60 mutex_unlock(&transaction_lock);
61
62 return ret;
63}
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79int rpi_firmware_property_list(struct rpi_firmware *fw,
80 void *data, size_t tag_size)
81{
82 size_t size = tag_size + 12;
83 u32 *buf;
84 dma_addr_t bus_addr;
85 int ret;
86
87
88 if (size & 3)
89 return -EINVAL;
90
91 buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
92 GFP_ATOMIC);
93 if (!buf)
94 return -ENOMEM;
95
96
97 WARN_ON(size >= 1024 * 1024);
98
99 buf[0] = size;
100 buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
101 memcpy(&buf[2], data, tag_size);
102 buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
103 wmb();
104
105 ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
106
107 rmb();
108 memcpy(data, &buf[2], tag_size);
109 if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
110
111
112
113
114
115 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
116 buf[2], buf[1]);
117 ret = -EINVAL;
118 }
119
120 dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
121
122 return ret;
123}
124EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140int rpi_firmware_property(struct rpi_firmware *fw,
141 u32 tag, void *tag_data, size_t buf_size)
142{
143
144
145
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;
149 int ret;
150
151 header->tag = tag;
152 header->buf_size = buf_size;
153 header->req_resp_size = 0;
154 memcpy(data + sizeof(struct rpi_firmware_property_tag_header),
155 tag_data, buf_size);
156
157 ret = rpi_firmware_property_list(fw, &data, sizeof(data));
158 memcpy(tag_data,
159 data + sizeof(struct rpi_firmware_property_tag_header),
160 buf_size);
161
162 return ret;
163}
164EXPORT_SYMBOL_GPL(rpi_firmware_property);
165
166static void
167rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
168{
169 u32 packet;
170 int ret = rpi_firmware_property(fw,
171 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
172 &packet, sizeof(packet));
173
174 if (ret == 0) {
175 struct tm tm;
176
177 time64_to_tm(packet, 0, &tm);
178
179 dev_info(fw->cl.dev,
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);
183 }
184}
185
186static int rpi_firmware_probe(struct platform_device *pdev)
187{
188 struct device *dev = &pdev->dev;
189 struct rpi_firmware *fw;
190
191 fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
192 if (!fw)
193 return -ENOMEM;
194
195 fw->cl.dev = dev;
196 fw->cl.rx_callback = response_callback;
197 fw->cl.tx_block = true;
198
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);
204 return ret;
205 }
206
207 init_completion(&fw->c);
208
209 platform_set_drvdata(pdev, fw);
210
211 rpi_firmware_print_firmware_revision(fw);
212
213 return 0;
214}
215
216static int rpi_firmware_remove(struct platform_device *pdev)
217{
218 struct rpi_firmware *fw = platform_get_drvdata(pdev);
219
220 mbox_free_channel(fw->chan);
221
222 return 0;
223}
224
225
226
227
228
229
230
231struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
232{
233 struct platform_device *pdev = of_find_device_by_node(firmware_node);
234
235 if (!pdev)
236 return NULL;
237
238 return platform_get_drvdata(pdev);
239}
240EXPORT_SYMBOL_GPL(rpi_firmware_get);
241
242static const struct of_device_id rpi_firmware_of_match[] = {
243 { .compatible = "raspberrypi,bcm2835-firmware", },
244 {},
245};
246MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
247
248static struct platform_driver rpi_firmware_driver = {
249 .driver = {
250 .name = "raspberrypi-firmware",
251 .of_match_table = rpi_firmware_of_match,
252 },
253 .probe = rpi_firmware_probe,
254 .remove = rpi_firmware_remove,
255};
256module_platform_driver(rpi_firmware_driver);
257
258MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
259MODULE_DESCRIPTION("Raspberry Pi firmware driver");
260MODULE_LICENSE("GPL v2");
261