hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/firmware/raspberrypi.c
....@@ -1,15 +1,13 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * Defines interfaces for interacting wtih the Raspberry Pi firmware's
34 * property channel.
45 *
56 * 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.
107 */
118
129 #include <linux/dma-mapping.h>
10
+#include <linux/kref.h>
1311 #include <linux/mailbox_client.h>
1412 #include <linux/module.h>
1513 #include <linux/of_platform.h>
....@@ -23,12 +21,15 @@
2321 #define MBOX_CHAN_PROPERTY 8
2422
2523 static struct platform_device *rpi_hwmon;
24
+static struct platform_device *rpi_clk;
2625
2726 struct rpi_firmware {
2827 struct mbox_client cl;
2928 struct mbox_chan *chan; /* The property channel. */
3029 struct completion c;
3130 u32 enabled;
31
+
32
+ struct kref consumers;
3233 };
3334
3435 static DEFINE_MUTEX(transaction_lock);
....@@ -55,8 +56,12 @@
5556 reinit_completion(&fw->c);
5657 ret = mbox_send_message(fw->chan, &message);
5758 if (ret >= 0) {
58
- wait_for_completion(&fw->c);
59
- ret = 0;
59
+ if (wait_for_completion_timeout(&fw->c, HZ)) {
60
+ ret = 0;
61
+ } else {
62
+ ret = -ETIMEDOUT;
63
+ WARN_ONCE(1, "Firmware transaction timeout");
64
+ }
6065 } else {
6166 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
6267 }
....@@ -175,21 +180,18 @@
175180 static void
176181 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
177182 {
183
+ time64_t date_and_time;
178184 u32 packet;
179185 int ret = rpi_firmware_property(fw,
180186 RPI_FIRMWARE_GET_FIRMWARE_REVISION,
181187 &packet, sizeof(packet));
182188
183
- if (ret == 0) {
184
- struct tm tm;
189
+ if (ret)
190
+ return;
185191
186
- time64_to_tm(packet, 0, &tm);
187
-
188
- dev_info(fw->cl.dev,
189
- "Attached to firmware from %04ld-%02d-%02d %02d:%02d\n",
190
- tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
191
- tm.tm_hour, tm.tm_min);
192
- }
192
+ /* This is not compatible with y2038 */
193
+ date_and_time = packet;
194
+ dev_info(fw->cl.dev, "Attached to firmware from %ptT\n", &date_and_time);
193195 }
194196
195197 static void
....@@ -206,12 +208,58 @@
206208 -1, NULL, 0);
207209 }
208210
211
+static void rpi_register_clk_driver(struct device *dev)
212
+{
213
+ struct device_node *firmware;
214
+
215
+ /*
216
+ * Earlier DTs don't have a node for the firmware clocks but
217
+ * rely on us creating a platform device by hand. If we do
218
+ * have a node for the firmware clocks, just bail out here.
219
+ */
220
+ firmware = of_get_compatible_child(dev->of_node,
221
+ "raspberrypi,firmware-clocks");
222
+ if (firmware) {
223
+ of_node_put(firmware);
224
+ return;
225
+ }
226
+
227
+ rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
228
+ -1, NULL, 0);
229
+}
230
+
231
+static void rpi_firmware_delete(struct kref *kref)
232
+{
233
+ struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
234
+ consumers);
235
+
236
+ mbox_free_channel(fw->chan);
237
+ kfree(fw);
238
+}
239
+
240
+void rpi_firmware_put(struct rpi_firmware *fw)
241
+{
242
+ kref_put(&fw->consumers, rpi_firmware_delete);
243
+}
244
+EXPORT_SYMBOL_GPL(rpi_firmware_put);
245
+
246
+static void devm_rpi_firmware_put(void *data)
247
+{
248
+ struct rpi_firmware *fw = data;
249
+
250
+ rpi_firmware_put(fw);
251
+}
252
+
209253 static int rpi_firmware_probe(struct platform_device *pdev)
210254 {
211255 struct device *dev = &pdev->dev;
212256 struct rpi_firmware *fw;
213257
214
- fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
258
+ /*
259
+ * Memory will be freed by rpi_firmware_delete() once all users have
260
+ * released their firmware handles. Don't use devm_kzalloc() here.
261
+ */
262
+ fw = kzalloc(sizeof(*fw), GFP_KERNEL);
215263 if (!fw)
216264 return -ENOMEM;
217265
....@@ -224,17 +272,30 @@
224272 int ret = PTR_ERR(fw->chan);
225273 if (ret != -EPROBE_DEFER)
226274 dev_err(dev, "Failed to get mbox channel: %d\n", ret);
275
+ kfree(fw);
227276 return ret;
228277 }
229278
230279 init_completion(&fw->c);
280
+ kref_init(&fw->consumers);
231281
232282 platform_set_drvdata(pdev, fw);
233283
234284 rpi_firmware_print_firmware_revision(fw);
235285 rpi_register_hwmon_driver(dev, fw);
286
+ rpi_register_clk_driver(dev);
236287
237288 return 0;
289
+}
290
+
291
+static void rpi_firmware_shutdown(struct platform_device *pdev)
292
+{
293
+ struct rpi_firmware *fw = platform_get_drvdata(pdev);
294
+
295
+ if (!fw)
296
+ return;
297
+
298
+ rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
238299 }
239300
240301 static int rpi_firmware_remove(struct platform_device *pdev)
....@@ -243,7 +304,10 @@
243304
244305 platform_device_unregister(rpi_hwmon);
245306 rpi_hwmon = NULL;
246
- mbox_free_channel(fw->chan);
307
+ platform_device_unregister(rpi_clk);
308
+ rpi_clk = NULL;
309
+
310
+ rpi_firmware_put(fw);
247311
248312 return 0;
249313 }
....@@ -252,18 +316,56 @@
252316 * rpi_firmware_get - Get pointer to rpi_firmware structure.
253317 * @firmware_node: Pointer to the firmware Device Tree node.
254318 *
319
+ * The reference to rpi_firmware has to be released with rpi_firmware_put().
320
+ *
255321 * Returns NULL is the firmware device is not ready.
256322 */
257323 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
258324 {
259325 struct platform_device *pdev = of_find_device_by_node(firmware_node);
326
+ struct rpi_firmware *fw;
260327
261328 if (!pdev)
262329 return NULL;
263330
264
- return platform_get_drvdata(pdev);
331
+ fw = platform_get_drvdata(pdev);
332
+ if (!fw)
333
+ goto err_put_device;
334
+
335
+ if (!kref_get_unless_zero(&fw->consumers))
336
+ goto err_put_device;
337
+
338
+ put_device(&pdev->dev);
339
+
340
+ return fw;
341
+
342
+err_put_device:
343
+ put_device(&pdev->dev);
344
+ return NULL;
265345 }
266346 EXPORT_SYMBOL_GPL(rpi_firmware_get);
347
+
348
+/**
349
+ * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
350
+ * @firmware_node: Pointer to the firmware Device Tree node.
351
+ *
352
+ * Returns NULL is the firmware device is not ready.
353
+ */
354
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
355
+ struct device_node *firmware_node)
356
+{
357
+ struct rpi_firmware *fw;
358
+
359
+ fw = rpi_firmware_get(firmware_node);
360
+ if (!fw)
361
+ return NULL;
362
+
363
+ if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
364
+ return NULL;
365
+
366
+ return fw;
367
+}
368
+EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
267369
268370 static const struct of_device_id rpi_firmware_of_match[] = {
269371 { .compatible = "raspberrypi,bcm2835-firmware", },
....@@ -277,6 +379,7 @@
277379 .of_match_table = rpi_firmware_of_match,
278380 },
279381 .probe = rpi_firmware_probe,
382
+ .shutdown = rpi_firmware_shutdown,
280383 .remove = rpi_firmware_remove,
281384 };
282385 module_platform_driver(rpi_firmware_driver);