hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
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,51 @@
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
+
209246 static int rpi_firmware_probe(struct platform_device *pdev)
210247 {
211248 struct device *dev = &pdev->dev;
212249 struct rpi_firmware *fw;
213250
214
- fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
251
+ /*
252
+ * Memory will be freed by rpi_firmware_delete() once all users have
253
+ * released their firmware handles. Don't use devm_kzalloc() here.
254
+ */
255
+ fw = kzalloc(sizeof(*fw), GFP_KERNEL);
215256 if (!fw)
216257 return -ENOMEM;
217258
....@@ -228,13 +269,25 @@
228269 }
229270
230271 init_completion(&fw->c);
272
+ kref_init(&fw->consumers);
231273
232274 platform_set_drvdata(pdev, fw);
233275
234276 rpi_firmware_print_firmware_revision(fw);
235277 rpi_register_hwmon_driver(dev, fw);
278
+ rpi_register_clk_driver(dev);
236279
237280 return 0;
281
+}
282
+
283
+static void rpi_firmware_shutdown(struct platform_device *pdev)
284
+{
285
+ struct rpi_firmware *fw = platform_get_drvdata(pdev);
286
+
287
+ if (!fw)
288
+ return;
289
+
290
+ rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
238291 }
239292
240293 static int rpi_firmware_remove(struct platform_device *pdev)
....@@ -243,7 +296,10 @@
243296
244297 platform_device_unregister(rpi_hwmon);
245298 rpi_hwmon = NULL;
246
- mbox_free_channel(fw->chan);
299
+ platform_device_unregister(rpi_clk);
300
+ rpi_clk = NULL;
301
+
302
+ rpi_firmware_put(fw);
247303
248304 return 0;
249305 }
....@@ -252,16 +308,32 @@
252308 * rpi_firmware_get - Get pointer to rpi_firmware structure.
253309 * @firmware_node: Pointer to the firmware Device Tree node.
254310 *
311
+ * The reference to rpi_firmware has to be released with rpi_firmware_put().
312
+ *
255313 * Returns NULL is the firmware device is not ready.
256314 */
257315 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
258316 {
259317 struct platform_device *pdev = of_find_device_by_node(firmware_node);
318
+ struct rpi_firmware *fw;
260319
261320 if (!pdev)
262321 return NULL;
263322
264
- return platform_get_drvdata(pdev);
323
+ fw = platform_get_drvdata(pdev);
324
+ if (!fw)
325
+ goto err_put_device;
326
+
327
+ if (!kref_get_unless_zero(&fw->consumers))
328
+ goto err_put_device;
329
+
330
+ put_device(&pdev->dev);
331
+
332
+ return fw;
333
+
334
+err_put_device:
335
+ put_device(&pdev->dev);
336
+ return NULL;
265337 }
266338 EXPORT_SYMBOL_GPL(rpi_firmware_get);
267339
....@@ -277,6 +349,7 @@
277349 .of_match_table = rpi_firmware_of_match,
278350 },
279351 .probe = rpi_firmware_probe,
352
+ .shutdown = rpi_firmware_shutdown,
280353 .remove = rpi_firmware_remove,
281354 };
282355 module_platform_driver(rpi_firmware_driver);