Blame view

drivers/firmware/raspberrypi.c 7.92 KB
502b431cd   Stefan Wahren   firmware: raspber...
1
  // SPDX-License-Identifier: GPL-2.0
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
2
3
4
5
6
  /*
   * Defines interfaces for interacting wtih the Raspberry Pi firmware's
   * property channel.
   *
   * Copyright © 2015 Broadcom
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
7
8
9
10
11
12
13
   */
  
  #include <linux/dma-mapping.h>
  #include <linux/mailbox_client.h>
  #include <linux/module.h>
  #include <linux/of_platform.h>
  #include <linux/platform_device.h>
91c6ada69   James Hughes   firmware: raspber...
14
  #include <linux/slab.h>
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
15
16
17
18
19
20
  #include <soc/bcm2835/raspberrypi-firmware.h>
  
  #define MBOX_MSG(chan, data28)		(((data28) & ~0xf) | ((chan) & 0xf))
  #define MBOX_CHAN(msg)			((msg) & 0xf)
  #define MBOX_DATA28(msg)		((msg) & ~0xf)
  #define MBOX_CHAN_PROPERTY		8
70eea1bbb   Stefan Wahren   firmware: raspber...
21
  static struct platform_device *rpi_hwmon;
91f2cf4a6   Nicolas Saenz Julienne   firmware: raspber...
22
  static struct platform_device *rpi_clk;
70eea1bbb   Stefan Wahren   firmware: raspber...
23

4e3d60656   Eric Anholt   ARM: bcm2835: Add...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  struct rpi_firmware {
  	struct mbox_client cl;
  	struct mbox_chan *chan; /* The property channel. */
  	struct completion c;
  	u32 enabled;
  };
  
  static DEFINE_MUTEX(transaction_lock);
  
  static void response_callback(struct mbox_client *cl, void *msg)
  {
  	struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
  	complete(&fw->c);
  }
  
  /*
   * Sends a request to the firmware through the BCM2835 mailbox driver,
   * and synchronously waits for the reply.
   */
  static int
  rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
  {
  	u32 message = MBOX_MSG(chan, data);
  	int ret;
  
  	WARN_ON(data & 0xf);
  
  	mutex_lock(&transaction_lock);
  	reinit_completion(&fw->c);
  	ret = mbox_send_message(fw->chan, &message);
  	if (ret >= 0) {
0829187b1   Stefan Wahren   firmware: raspber...
55
56
57
58
59
60
  		if (wait_for_completion_timeout(&fw->c, HZ)) {
  			ret = 0;
  		} else {
  			ret = -ETIMEDOUT;
  			WARN_ONCE(1, "Firmware transaction timeout");
  		}
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	} else {
  		dev_err(fw->cl.dev, "mbox_send_message returned %d
  ", ret);
  	}
  	mutex_unlock(&transaction_lock);
  
  	return ret;
  }
  
  /**
   * rpi_firmware_property_list - Submit firmware property list
   * @fw:		Pointer to firmware structure from rpi_firmware_get().
   * @data:	Buffer holding tags.
   * @tag_size:	Size of tags buffer.
   *
   * Submits a set of concatenated tags to the VPU firmware through the
   * mailbox property interface.
   *
   * The buffer header and the ending tag are added by this function and
   * don't need to be supplied, just the actual tags for your operation.
   * See struct rpi_firmware_property_tag_header for the per-tag
   * structure.
   */
  int rpi_firmware_property_list(struct rpi_firmware *fw,
  			       void *data, size_t tag_size)
  {
  	size_t size = tag_size + 12;
  	u32 *buf;
  	dma_addr_t bus_addr;
  	int ret;
  
  	/* Packets are processed a dword at a time. */
  	if (size & 3)
  		return -EINVAL;
  
  	buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
  				 GFP_ATOMIC);
  	if (!buf)
  		return -ENOMEM;
  
  	/* The firmware will error out without parsing in this case. */
  	WARN_ON(size >= 1024 * 1024);
  
  	buf[0] = size;
  	buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
  	memcpy(&buf[2], data, tag_size);
  	buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
  	wmb();
  
  	ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
  
  	rmb();
  	memcpy(data, &buf[2], tag_size);
  	if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
  		/*
  		 * The tag name here might not be the one causing the
  		 * error, if there were multiple tags in the request.
  		 * But single-tag is the most common, so go with it.
  		 */
  		dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x
  ",
  			buf[2], buf[1]);
  		ret = -EINVAL;
  	}
  
  	dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
  
  /**
   * rpi_firmware_property - Submit single firmware property
   * @fw:		Pointer to firmware structure from rpi_firmware_get().
   * @tag:	One of enum_mbox_property_tag.
   * @tag_data:	Tag data buffer.
   * @buf_size:	Buffer size.
   *
   * Submits a single tag to the VPU firmware through the mailbox
   * property interface.
   *
   * This is a convenience wrapper around
   * rpi_firmware_property_list() to avoid some of the
   * boilerplate in property calls.
   */
  int rpi_firmware_property(struct rpi_firmware *fw,
  			  u32 tag, void *tag_data, size_t buf_size)
  {
91c6ada69   James Hughes   firmware: raspber...
149
  	struct rpi_firmware_property_tag_header *header;
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
150
  	int ret;
91c6ada69   James Hughes   firmware: raspber...
151
152
153
154
155
156
  	/* Some mailboxes can use over 1k bytes. Rather than checking
  	 * size and using stack or kmalloc depending on requirements,
  	 * just use kmalloc. Mailboxes don't get called enough to worry
  	 * too much about the time taken in the allocation.
  	 */
  	void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
a1547e0bc   Kees Cook   firmware: raspber...
157

91c6ada69   James Hughes   firmware: raspber...
158
159
160
161
  	if (!data)
  		return -ENOMEM;
  
  	header = data;
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
162
163
164
  	header->tag = tag;
  	header->buf_size = buf_size;
  	header->req_resp_size = 0;
91c6ada69   James Hughes   firmware: raspber...
165
166
167
168
169
  	memcpy(data + sizeof(*header), tag_data, buf_size);
  
  	ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
  
  	memcpy(tag_data, data + sizeof(*header), buf_size);
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
170

91c6ada69   James Hughes   firmware: raspber...
171
  	kfree(data);
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
172
173
174
175
176
177
178
179
  
  	return ret;
  }
  EXPORT_SYMBOL_GPL(rpi_firmware_property);
  
  static void
  rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
  {
da785a877   Andy Shevchenko   ARM: bcm2835: Fix...
180
  	time64_t date_and_time;
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
181
182
183
184
  	u32 packet;
  	int ret = rpi_firmware_property(fw,
  					RPI_FIRMWARE_GET_FIRMWARE_REVISION,
  					&packet, sizeof(packet));
4a60f58ee   Andy Shevchenko   ARM: bcm2835: Swi...
185
186
  	if (ret)
  		return;
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
187

da785a877   Andy Shevchenko   ARM: bcm2835: Fix...
188
189
190
191
  	/* This is not compatible with y2038 */
  	date_and_time = packet;
  	dev_info(fw->cl.dev, "Attached to firmware from %ptT
  ", &date_and_time);
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
192
  }
70eea1bbb   Stefan Wahren   firmware: raspber...
193
194
195
196
197
198
199
200
201
202
203
204
205
  static void
  rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
  {
  	u32 packet;
  	int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
  					&packet, sizeof(packet));
  
  	if (ret)
  		return;
  
  	rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
  						  -1, NULL, 0);
  }
91f2cf4a6   Nicolas Saenz Julienne   firmware: raspber...
206
207
  static void rpi_register_clk_driver(struct device *dev)
  {
511aba099   Maxime Ripard   firmware: rpi: On...
208
209
210
211
212
213
214
215
216
217
218
219
220
  	struct device_node *firmware;
  
  	/*
  	 * Earlier DTs don't have a node for the firmware clocks but
  	 * rely on us creating a platform device by hand. If we do
  	 * have a node for the firmware clocks, just bail out here.
  	 */
  	firmware = of_get_compatible_child(dev->of_node,
  					   "raspberrypi,firmware-clocks");
  	if (firmware) {
  		of_node_put(firmware);
  		return;
  	}
91f2cf4a6   Nicolas Saenz Julienne   firmware: raspber...
221
222
223
  	rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
  						-1, NULL, 0);
  }
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  static int rpi_firmware_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
  	struct rpi_firmware *fw;
  
  	fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
  	if (!fw)
  		return -ENOMEM;
  
  	fw->cl.dev = dev;
  	fw->cl.rx_callback = response_callback;
  	fw->cl.tx_block = true;
  
  	fw->chan = mbox_request_channel(&fw->cl, 0);
  	if (IS_ERR(fw->chan)) {
  		int ret = PTR_ERR(fw->chan);
  		if (ret != -EPROBE_DEFER)
  			dev_err(dev, "Failed to get mbox channel: %d
  ", ret);
  		return ret;
  	}
  
  	init_completion(&fw->c);
  
  	platform_set_drvdata(pdev, fw);
  
  	rpi_firmware_print_firmware_revision(fw);
70eea1bbb   Stefan Wahren   firmware: raspber...
251
  	rpi_register_hwmon_driver(dev, fw);
91f2cf4a6   Nicolas Saenz Julienne   firmware: raspber...
252
  	rpi_register_clk_driver(dev);
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
253
254
255
  
  	return 0;
  }
b80ec7c0e   Stefan Wahren   firmware: raspber...
256
257
258
259
260
261
262
263
264
  static void rpi_firmware_shutdown(struct platform_device *pdev)
  {
  	struct rpi_firmware *fw = platform_get_drvdata(pdev);
  
  	if (!fw)
  		return;
  
  	rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
  }
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
265
266
267
  static int rpi_firmware_remove(struct platform_device *pdev)
  {
  	struct rpi_firmware *fw = platform_get_drvdata(pdev);
70eea1bbb   Stefan Wahren   firmware: raspber...
268
269
  	platform_device_unregister(rpi_hwmon);
  	rpi_hwmon = NULL;
91f2cf4a6   Nicolas Saenz Julienne   firmware: raspber...
270
271
  	platform_device_unregister(rpi_clk);
  	rpi_clk = NULL;
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  	mbox_free_channel(fw->chan);
  
  	return 0;
  }
  
  /**
   * rpi_firmware_get - Get pointer to rpi_firmware structure.
   * @firmware_node:    Pointer to the firmware Device Tree node.
   *
   * Returns NULL is the firmware device is not ready.
   */
  struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
  {
  	struct platform_device *pdev = of_find_device_by_node(firmware_node);
  
  	if (!pdev)
  		return NULL;
  
  	return platform_get_drvdata(pdev);
  }
  EXPORT_SYMBOL_GPL(rpi_firmware_get);
  
  static const struct of_device_id rpi_firmware_of_match[] = {
  	{ .compatible = "raspberrypi,bcm2835-firmware", },
  	{},
  };
  MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
  
  static struct platform_driver rpi_firmware_driver = {
  	.driver = {
  		.name = "raspberrypi-firmware",
  		.of_match_table = rpi_firmware_of_match,
  	},
  	.probe		= rpi_firmware_probe,
b80ec7c0e   Stefan Wahren   firmware: raspber...
306
  	.shutdown	= rpi_firmware_shutdown,
4e3d60656   Eric Anholt   ARM: bcm2835: Add...
307
308
309
310
311
312
313
  	.remove		= rpi_firmware_remove,
  };
  module_platform_driver(rpi_firmware_driver);
  
  MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  MODULE_DESCRIPTION("Raspberry Pi firmware driver");
  MODULE_LICENSE("GPL v2");