Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Defines interfaces for interacting with the Raspberry Pi firmware's
  4 * property channel.
  5 *
  6 * Copyright © 2015 Broadcom
 
 
 
 
  7 */
  8
  9#include <linux/dma-mapping.h>
 10#include <linux/kref.h>
 11#include <linux/mailbox_client.h>
 12#include <linux/mailbox_controller.h>
 13#include <linux/module.h>
 14#include <linux/of.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>
 19
 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
 24
 25static struct platform_device *rpi_hwmon;
 26static struct platform_device *rpi_clk;
 27
 28struct rpi_firmware {
 29	struct mbox_client cl;
 30	struct mbox_chan *chan; /* The property channel. */
 31	struct completion c;
 32	u32 enabled;
 33
 34	struct kref consumers;
 35};
 36
 37static DEFINE_MUTEX(transaction_lock);
 38
 39static void response_callback(struct mbox_client *cl, void *msg)
 40{
 41	struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
 42	complete(&fw->c);
 43}
 44
 45/*
 46 * Sends a request to the firmware through the BCM2835 mailbox driver,
 47 * and synchronously waits for the reply.
 48 */
 49static int
 50rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
 51{
 52	u32 message = MBOX_MSG(chan, data);
 53	int ret;
 54
 55	WARN_ON(data & 0xf);
 56
 57	mutex_lock(&transaction_lock);
 58	reinit_completion(&fw->c);
 59	ret = mbox_send_message(fw->chan, &message);
 60	if (ret >= 0) {
 61		if (wait_for_completion_timeout(&fw->c, HZ)) {
 62			ret = 0;
 63		} else {
 64			ret = -ETIMEDOUT;
 65		}
 66	} else {
 67		dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
 68	}
 69	mutex_unlock(&transaction_lock);
 70
 71	return ret;
 72}
 73
 74/**
 75 * rpi_firmware_property_list - Submit firmware property list
 76 * @fw:		Pointer to firmware structure from rpi_firmware_get().
 77 * @data:	Buffer holding tags.
 78 * @tag_size:	Size of tags buffer.
 79 *
 80 * Submits a set of concatenated tags to the VPU firmware through the
 81 * mailbox property interface.
 82 *
 83 * The buffer header and the ending tag are added by this function and
 84 * don't need to be supplied, just the actual tags for your operation.
 85 * See struct rpi_firmware_property_tag_header for the per-tag
 86 * structure.
 87 */
 88int rpi_firmware_property_list(struct rpi_firmware *fw,
 89			       void *data, size_t tag_size)
 90{
 91	size_t size = tag_size + 12;
 92	u32 *buf;
 93	dma_addr_t bus_addr;
 94	int ret;
 95
 96	/* Packets are processed a dword at a time. */
 97	if (size & 3)
 98		return -EINVAL;
 99
100	buf = dma_alloc_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size),
101				 &bus_addr, GFP_ATOMIC);
102	if (!buf)
103		return -ENOMEM;
104
105	/* The firmware will error out without parsing in this case. */
106	WARN_ON(size >= 1024 * 1024);
107
108	buf[0] = size;
109	buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
110	memcpy(&buf[2], data, tag_size);
111	buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
112	wmb();
113
114	ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
115
116	rmb();
117	memcpy(data, &buf[2], tag_size);
118	if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
119		/*
120		 * The tag name here might not be the one causing the
121		 * error, if there were multiple tags in the request.
122		 * But single-tag is the most common, so go with it.
123		 */
124		dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
125			buf[2], buf[1]);
126		ret = -EINVAL;
127	} else if (ret == -ETIMEDOUT) {
128		WARN_ONCE(1, "Firmware transaction 0x%08x timeout", buf[2]);
129	}
130
131	dma_free_coherent(fw->chan->mbox->dev, PAGE_ALIGN(size), buf, bus_addr);
132
133	return ret;
134}
135EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
136
137/**
138 * rpi_firmware_property - Submit single firmware property
139 * @fw:		Pointer to firmware structure from rpi_firmware_get().
140 * @tag:	One of enum_mbox_property_tag.
141 * @tag_data:	Tag data buffer.
142 * @buf_size:	Buffer size.
143 *
144 * Submits a single tag to the VPU firmware through the mailbox
145 * property interface.
146 *
147 * This is a convenience wrapper around
148 * rpi_firmware_property_list() to avoid some of the
149 * boilerplate in property calls.
150 */
151int rpi_firmware_property(struct rpi_firmware *fw,
152			  u32 tag, void *tag_data, size_t buf_size)
153{
154	struct rpi_firmware_property_tag_header *header;
155	int ret;
156
157	/* Some mailboxes can use over 1k bytes. Rather than checking
158	 * size and using stack or kmalloc depending on requirements,
159	 * just use kmalloc. Mailboxes don't get called enough to worry
160	 * too much about the time taken in the allocation.
161	 */
162	void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
163
164	if (!data)
165		return -ENOMEM;
166
167	header = data;
168	header->tag = tag;
169	header->buf_size = buf_size;
170	header->req_resp_size = 0;
171	memcpy(data + sizeof(*header), tag_data, buf_size);
172
173	ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
174
175	memcpy(tag_data, data + sizeof(*header), buf_size);
176
177	kfree(data);
 
178
179	return ret;
180}
181EXPORT_SYMBOL_GPL(rpi_firmware_property);
182
183static void
184rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
185{
186	time64_t date_and_time;
187	u32 packet;
188	int ret = rpi_firmware_property(fw,
189					RPI_FIRMWARE_GET_FIRMWARE_REVISION,
190					&packet, sizeof(packet));
191
192	if (ret)
193		return;
194
195	/* This is not compatible with y2038 */
196	date_and_time = packet;
197	dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
198}
199
200static void
201rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
202{
203	u32 packet;
204	int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
205					&packet, sizeof(packet));
206
207	if (ret)
208		return;
209
210	rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
211						  -1, NULL, 0);
212}
213
214static void rpi_register_clk_driver(struct device *dev)
215{
216	struct device_node *firmware;
217
218	/*
219	 * Earlier DTs don't have a node for the firmware clocks but
220	 * rely on us creating a platform device by hand. If we do
221	 * have a node for the firmware clocks, just bail out here.
222	 */
223	firmware = of_get_compatible_child(dev->of_node,
224					   "raspberrypi,firmware-clocks");
225	if (firmware) {
226		of_node_put(firmware);
227		return;
228	}
229
230	rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
231						-1, NULL, 0);
232}
233
234unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
235{
236	struct rpi_firmware_clk_rate_request msg =
237		RPI_FIRMWARE_CLK_RATE_REQUEST(id);
238	int ret;
239
240	ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
241				    &msg, sizeof(msg));
242	if (ret)
243		/*
244		 * If our firmware doesn't support that operation, or fails, we
245		 * assume the maximum clock rate is absolute maximum we can
246		 * store over our type.
247		 */
248		 return UINT_MAX;
249
250	return le32_to_cpu(msg.rate);
251}
252EXPORT_SYMBOL_GPL(rpi_firmware_clk_get_max_rate);
253
254static void rpi_firmware_delete(struct kref *kref)
255{
256	struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
257					       consumers);
258
259	mbox_free_channel(fw->chan);
260	kfree(fw);
261}
262
263void rpi_firmware_put(struct rpi_firmware *fw)
264{
265	kref_put(&fw->consumers, rpi_firmware_delete);
266}
267EXPORT_SYMBOL_GPL(rpi_firmware_put);
268
269static void devm_rpi_firmware_put(void *data)
270{
271	struct rpi_firmware *fw = data;
272
273	rpi_firmware_put(fw);
274}
275
276static int rpi_firmware_probe(struct platform_device *pdev)
277{
278	struct device *dev = &pdev->dev;
279	struct rpi_firmware *fw;
280
281	/*
282	 * Memory will be freed by rpi_firmware_delete() once all users have
283	 * released their firmware handles. Don't use devm_kzalloc() here.
284	 */
285	fw = kzalloc(sizeof(*fw), GFP_KERNEL);
286	if (!fw)
287		return -ENOMEM;
288
289	fw->cl.dev = dev;
290	fw->cl.rx_callback = response_callback;
291	fw->cl.tx_block = true;
292
293	fw->chan = mbox_request_channel(&fw->cl, 0);
294	if (IS_ERR(fw->chan)) {
295		int ret = PTR_ERR(fw->chan);
296		kfree(fw);
297		return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
 
298	}
299
300	init_completion(&fw->c);
301	kref_init(&fw->consumers);
302
303	platform_set_drvdata(pdev, fw);
304
305	rpi_firmware_print_firmware_revision(fw);
306	rpi_register_hwmon_driver(dev, fw);
307	rpi_register_clk_driver(dev);
308
309	return 0;
310}
311
312static void rpi_firmware_shutdown(struct platform_device *pdev)
313{
314	struct rpi_firmware *fw = platform_get_drvdata(pdev);
315
316	if (!fw)
317		return;
318
319	rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
320}
321
322static void rpi_firmware_remove(struct platform_device *pdev)
323{
324	struct rpi_firmware *fw = platform_get_drvdata(pdev);
325
326	platform_device_unregister(rpi_hwmon);
327	rpi_hwmon = NULL;
328	platform_device_unregister(rpi_clk);
329	rpi_clk = NULL;
330
331	rpi_firmware_put(fw);
332}
333
334static const struct of_device_id rpi_firmware_of_match[] = {
335	{ .compatible = "raspberrypi,bcm2835-firmware", },
336	{},
337};
338MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
339
340struct device_node *rpi_firmware_find_node(void)
341{
342	return of_find_matching_node(NULL, rpi_firmware_of_match);
343}
344EXPORT_SYMBOL_GPL(rpi_firmware_find_node);
345
346/**
347 * rpi_firmware_get - Get pointer to rpi_firmware structure.
348 * @firmware_node:    Pointer to the firmware Device Tree node.
349 *
350 * The reference to rpi_firmware has to be released with rpi_firmware_put().
351 *
352 * Returns NULL is the firmware device is not ready.
353 */
354struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
355{
356	struct platform_device *pdev = of_find_device_by_node(firmware_node);
357	struct rpi_firmware *fw;
358
359	if (!pdev)
360		return NULL;
361
362	fw = platform_get_drvdata(pdev);
363	if (!fw)
364		goto err_put_device;
365
366	if (!kref_get_unless_zero(&fw->consumers))
367		goto err_put_device;
368
369	put_device(&pdev->dev);
370
371	return fw;
372
373err_put_device:
374	put_device(&pdev->dev);
375	return NULL;
376}
377EXPORT_SYMBOL_GPL(rpi_firmware_get);
378
379/**
380 * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
381 * @dev:              The firmware device structure
382 * @firmware_node:    Pointer to the firmware Device Tree node.
383 *
384 * Returns NULL is the firmware device is not ready.
385 */
386struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
387					   struct device_node *firmware_node)
388{
389	struct rpi_firmware *fw;
390
391	fw = rpi_firmware_get(firmware_node);
392	if (!fw)
393		return NULL;
394
395	if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
396		return NULL;
397
398	return fw;
399}
400EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
401
402static struct platform_driver rpi_firmware_driver = {
403	.driver = {
404		.name = "raspberrypi-firmware",
405		.of_match_table = rpi_firmware_of_match,
406	},
407	.probe		= rpi_firmware_probe,
408	.shutdown	= rpi_firmware_shutdown,
409	.remove		= rpi_firmware_remove,
410};
411module_platform_driver(rpi_firmware_driver);
412
413MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
414MODULE_DESCRIPTION("Raspberry Pi firmware driver");
415MODULE_LICENSE("GPL v2");
v4.10.11
 
  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
 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 * Sends a request to the firmware through the BCM2835 mailbox driver,
 41 * and synchronously waits for the reply.
 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 * 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.
 70 *
 71 * Submits a set of concatenated tags to the VPU firmware through the
 72 * mailbox property interface.
 73 *
 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
 77 * structure.
 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	/* Packets are processed a dword at a time. */
 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	/* The firmware will error out without parsing in this case. */
 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		 * 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.
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 * 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.
132 *
133 * Submits a single tag to the VPU firmware through the mailbox
134 * property interface.
135 *
136 * This is a convenience wrapper around
137 * rpi_firmware_property_list() to avoid some of the
138 * boilerplate in property calls.
139 */
140int rpi_firmware_property(struct rpi_firmware *fw,
141			  u32 tag, void *tag_data, size_t buf_size)
142{
143	/* Single tags are very small (generally 8 bytes), so the
144	 * stack should be safe.
 
 
 
 
 
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		time_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 * rpi_firmware_get - Get pointer to rpi_firmware structure.
227 * @firmware_node:    Pointer to the firmware Device Tree node.
228 *
 
 
229 * Returns NULL is the firmware device is not ready.
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");