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