drivers: pinctrl: add BCM2712D0 EMMC pins
[platform/kernel/linux-rpi.git] / drivers / firmware / raspberrypi.c
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/module.h>
13 #include <linux/of.h>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/reboot.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
25 static struct platform_device *rpi_hwmon;
26 static struct platform_device *rpi_clk;
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         struct kref consumers;
35         u32 get_throttled;
36 };
37
38 static struct platform_device *g_pdev;
39
40 static DEFINE_MUTEX(transaction_lock);
41
42 static void response_callback(struct mbox_client *cl, void *msg)
43 {
44         struct rpi_firmware *fw = container_of(cl, struct rpi_firmware, cl);
45         complete(&fw->c);
46 }
47
48 /*
49  * Sends a request to the firmware through the BCM2835 mailbox driver,
50  * and synchronously waits for the reply.
51  */
52 static int
53 rpi_firmware_transaction(struct rpi_firmware *fw, u32 chan, u32 data)
54 {
55         u32 message = MBOX_MSG(chan, data);
56         int ret;
57
58         WARN_ON(data & 0xf);
59
60         mutex_lock(&transaction_lock);
61         reinit_completion(&fw->c);
62         ret = mbox_send_message(fw->chan, &message);
63         if (ret >= 0) {
64                 if (wait_for_completion_timeout(&fw->c, HZ)) {
65                         ret = 0;
66                 } else {
67                         ret = -ETIMEDOUT;
68                         WARN_ONCE(1, "Firmware transaction timeout");
69                 }
70         } else {
71                 dev_err(fw->cl.dev, "mbox_send_message returned %d\n", ret);
72         }
73         mutex_unlock(&transaction_lock);
74
75         return ret;
76 }
77
78 /**
79  * rpi_firmware_property_list - Submit firmware property list
80  * @fw:         Pointer to firmware structure from rpi_firmware_get().
81  * @data:       Buffer holding tags.
82  * @tag_size:   Size of tags buffer.
83  *
84  * Submits a set of concatenated tags to the VPU firmware through the
85  * mailbox property interface.
86  *
87  * The buffer header and the ending tag are added by this function and
88  * don't need to be supplied, just the actual tags for your operation.
89  * See struct rpi_firmware_property_tag_header for the per-tag
90  * structure.
91  */
92 int rpi_firmware_property_list(struct rpi_firmware *fw,
93                                void *data, size_t tag_size)
94 {
95         size_t size = tag_size + 12;
96         u32 *buf;
97         dma_addr_t bus_addr;
98         int ret;
99
100         /* Packets are processed a dword at a time. */
101         if (size & 3)
102                 return -EINVAL;
103
104         buf = dma_alloc_coherent(fw->cl.dev, PAGE_ALIGN(size), &bus_addr,
105                                  GFP_ATOMIC);
106         if (!buf)
107                 return -ENOMEM;
108
109         /* The firmware will error out without parsing in this case. */
110         WARN_ON(size >= 1024 * 1024);
111
112         buf[0] = size;
113         buf[1] = RPI_FIRMWARE_STATUS_REQUEST;
114         memcpy(&buf[2], data, tag_size);
115         buf[size / 4 - 1] = RPI_FIRMWARE_PROPERTY_END;
116         wmb();
117
118         ret = rpi_firmware_transaction(fw, MBOX_CHAN_PROPERTY, bus_addr);
119
120         rmb();
121         memcpy(data, &buf[2], tag_size);
122         if (ret == 0 && buf[1] != RPI_FIRMWARE_STATUS_SUCCESS) {
123                 /*
124                  * The tag name here might not be the one causing the
125                  * error, if there were multiple tags in the request.
126                  * But single-tag is the most common, so go with it.
127                  */
128                 dev_err(fw->cl.dev, "Request 0x%08x returned status 0x%08x\n",
129                         buf[2], buf[1]);
130                 ret = -EINVAL;
131         }
132
133         dma_free_coherent(fw->cl.dev, PAGE_ALIGN(size), buf, bus_addr);
134
135         return ret;
136 }
137 EXPORT_SYMBOL_GPL(rpi_firmware_property_list);
138
139 /**
140  * rpi_firmware_property - Submit single firmware property
141  * @fw:         Pointer to firmware structure from rpi_firmware_get().
142  * @tag:        One of enum_mbox_property_tag.
143  * @tag_data:   Tag data buffer.
144  * @buf_size:   Buffer size.
145  *
146  * Submits a single tag to the VPU firmware through the mailbox
147  * property interface.
148  *
149  * This is a convenience wrapper around
150  * rpi_firmware_property_list() to avoid some of the
151  * boilerplate in property calls.
152  */
153 int rpi_firmware_property(struct rpi_firmware *fw,
154                           u32 tag, void *tag_data, size_t buf_size)
155 {
156         struct rpi_firmware_property_tag_header *header;
157         int ret;
158
159         /* Some mailboxes can use over 1k bytes. Rather than checking
160          * size and using stack or kmalloc depending on requirements,
161          * just use kmalloc. Mailboxes don't get called enough to worry
162          * too much about the time taken in the allocation.
163          */
164         void *data = kmalloc(sizeof(*header) + buf_size, GFP_KERNEL);
165
166         if (!data)
167                 return -ENOMEM;
168
169         header = data;
170         header->tag = tag;
171         header->buf_size = buf_size;
172         header->req_resp_size = 0;
173         memcpy(data + sizeof(*header), tag_data, buf_size);
174
175         ret = rpi_firmware_property_list(fw, data, buf_size + sizeof(*header));
176
177         memcpy(tag_data, data + sizeof(*header), buf_size);
178
179         kfree(data);
180
181         if ((tag == RPI_FIRMWARE_GET_THROTTLED) &&
182              memcmp(&fw->get_throttled, tag_data, sizeof(fw->get_throttled))) {
183                 memcpy(&fw->get_throttled, tag_data, sizeof(fw->get_throttled));
184                 sysfs_notify(&fw->cl.dev->kobj, NULL, "get_throttled");
185         }
186
187         return ret;
188 }
189 EXPORT_SYMBOL_GPL(rpi_firmware_property);
190
191 static int rpi_firmware_notify_reboot(struct notifier_block *nb,
192                                       unsigned long action,
193                                       void *data)
194 {
195         struct rpi_firmware *fw;
196         struct platform_device *pdev = g_pdev;
197         u32 reboot_flags = 0;
198
199         if (!pdev)
200                 return 0;
201
202         fw = platform_get_drvdata(pdev);
203         if (!fw)
204                 return 0;
205
206         // The partition id is the first parameter followed by zero or
207         // more flags separated by spaces indicating the reason for the reboot.
208         //
209         // 'tryboot': Sets a one-shot flag which is cleared upon reboot and
210         //            causes the tryboot.txt to be loaded instead of config.txt
211         //            by the bootloader and the start.elf firmware.
212         //
213         //            This is intended to allow automatic fallback to a known
214         //            good image if an OS/FW upgrade fails.
215         //
216         // N.B. The firmware mechanism for storing reboot flags may vary
217         // on different Raspberry Pi models.
218         if (data && strstr(data, " tryboot"))
219                 reboot_flags |= 0x1;
220
221         // The mailbox might have been called earlier, directly via vcmailbox
222         // so only overwrite if reboot flags are passed to the reboot command.
223         if (reboot_flags)
224                 (void)rpi_firmware_property(fw, RPI_FIRMWARE_SET_REBOOT_FLAGS,
225                                 &reboot_flags, sizeof(reboot_flags));
226
227         (void)rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
228
229         return 0;
230 }
231
232 static ssize_t get_throttled_show(struct device *dev,
233                                   struct device_attribute *attr, char *buf)
234 {
235         struct rpi_firmware *fw = dev_get_drvdata(dev);
236
237         WARN_ONCE(1, "deprecated, use hwmon sysfs instead\n");
238
239         return sprintf(buf, "%x\n", fw->get_throttled);
240 }
241
242 static DEVICE_ATTR_RO(get_throttled);
243
244 static struct attribute *rpi_firmware_dev_attrs[] = {
245         &dev_attr_get_throttled.attr,
246         NULL,
247 };
248
249 static const struct attribute_group rpi_firmware_dev_group = {
250         .attrs = rpi_firmware_dev_attrs,
251 };
252
253 static void
254 rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
255 {
256         time64_t date_and_time;
257         u32 packet;
258         static const char * const variant_strs[] = {
259                 "unknown",
260                 "start",
261                 "start_x",
262                 "start_db",
263                 "start_cd",
264         };
265         const char *variant_str = "cmd unsupported";
266         u32 variant;
267         int ret = rpi_firmware_property(fw,
268                                         RPI_FIRMWARE_GET_FIRMWARE_REVISION,
269                                         &packet, sizeof(packet));
270
271         if (ret)
272                 return;
273
274         /* This is not compatible with y2038 */
275         date_and_time = packet;
276
277         ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_FIRMWARE_VARIANT,
278                                     &variant, sizeof(variant));
279
280         if (!ret) {
281                 if (variant >= ARRAY_SIZE(variant_strs))
282                         variant = 0;
283                 variant_str = variant_strs[variant];
284         }
285
286         dev_info(fw->cl.dev,
287                  "Attached to firmware from %ptT, variant %s\n",
288                  &date_and_time, variant_str);
289 }
290
291 static void
292 rpi_firmware_print_firmware_hash(struct rpi_firmware *fw)
293 {
294         u32 hash[5];
295         int ret = rpi_firmware_property(fw,
296                                         RPI_FIRMWARE_GET_FIRMWARE_HASH,
297                                         hash, sizeof(hash));
298
299         if (ret)
300                 return;
301
302         dev_info(fw->cl.dev,
303                  "Firmware hash is %08x%08x%08x%08x%08x\n",
304                  hash[0], hash[1], hash[2], hash[3], hash[4]);
305 }
306
307 static void
308 rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
309 {
310         u32 packet;
311         int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
312                                         &packet, sizeof(packet));
313
314         if (ret)
315                 return;
316
317         rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
318                                                   -1, NULL, 0);
319
320         if (!IS_ERR_OR_NULL(rpi_hwmon)) {
321                 if (devm_device_add_group(dev, &rpi_firmware_dev_group))
322                         dev_err(dev, "Failed to create get_trottled attr\n");
323         }
324 }
325
326 static void rpi_register_clk_driver(struct device *dev)
327 {
328         struct device_node *firmware;
329
330         /*
331          * Earlier DTs don't have a node for the firmware clocks but
332          * rely on us creating a platform device by hand. If we do
333          * have a node for the firmware clocks, just bail out here.
334          */
335         firmware = of_get_compatible_child(dev->of_node,
336                                            "raspberrypi,firmware-clocks");
337         if (firmware) {
338                 of_node_put(firmware);
339                 return;
340         }
341
342         rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
343                                                 -1, NULL, 0);
344 }
345
346 unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
347 {
348         struct rpi_firmware_clk_rate_request msg =
349                 RPI_FIRMWARE_CLK_RATE_REQUEST(id);
350         int ret;
351
352         ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
353                                     &msg, sizeof(msg));
354         if (ret)
355                 /*
356                  * If our firmware doesn't support that operation, or fails, we
357                  * assume the maximum clock rate is absolute maximum we can
358                  * store over our type.
359                  */
360                  return UINT_MAX;
361
362         return le32_to_cpu(msg.rate);
363 }
364 EXPORT_SYMBOL_GPL(rpi_firmware_clk_get_max_rate);
365
366 static void rpi_firmware_delete(struct kref *kref)
367 {
368         struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
369                                                consumers);
370
371         mbox_free_channel(fw->chan);
372         kfree(fw);
373 }
374
375 void rpi_firmware_put(struct rpi_firmware *fw)
376 {
377         kref_put(&fw->consumers, rpi_firmware_delete);
378 }
379 EXPORT_SYMBOL_GPL(rpi_firmware_put);
380
381 static void devm_rpi_firmware_put(void *data)
382 {
383         struct rpi_firmware *fw = data;
384
385         rpi_firmware_put(fw);
386 }
387
388 static int rpi_firmware_probe(struct platform_device *pdev)
389 {
390         struct device *dev = &pdev->dev;
391         struct rpi_firmware *fw;
392
393         /*
394          * Memory will be freed by rpi_firmware_delete() once all users have
395          * released their firmware handles. Don't use devm_kzalloc() here.
396          */
397         fw = kzalloc(sizeof(*fw), GFP_KERNEL);
398         if (!fw)
399                 return -ENOMEM;
400
401         fw->cl.dev = dev;
402         fw->cl.rx_callback = response_callback;
403         fw->cl.tx_block = true;
404
405         fw->chan = mbox_request_channel(&fw->cl, 0);
406         if (IS_ERR(fw->chan)) {
407                 int ret = PTR_ERR(fw->chan);
408                 kfree(fw);
409                 return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
410         }
411
412         init_completion(&fw->c);
413         kref_init(&fw->consumers);
414
415         platform_set_drvdata(pdev, fw);
416         g_pdev = pdev;
417
418         rpi_firmware_print_firmware_revision(fw);
419         rpi_firmware_print_firmware_hash(fw);
420         rpi_register_hwmon_driver(dev, fw);
421         rpi_register_clk_driver(dev);
422
423         return 0;
424 }
425
426 static void rpi_firmware_shutdown(struct platform_device *pdev)
427 {
428         struct rpi_firmware *fw = platform_get_drvdata(pdev);
429
430         if (!fw)
431                 return;
432
433         rpi_firmware_property(fw, RPI_FIRMWARE_NOTIFY_REBOOT, NULL, 0);
434 }
435
436 static int rpi_firmware_remove(struct platform_device *pdev)
437 {
438         struct rpi_firmware *fw = platform_get_drvdata(pdev);
439
440         platform_device_unregister(rpi_hwmon);
441         rpi_hwmon = NULL;
442         platform_device_unregister(rpi_clk);
443         rpi_clk = NULL;
444
445         rpi_firmware_put(fw);
446         g_pdev = NULL;
447
448         return 0;
449 }
450
451 static const struct of_device_id rpi_firmware_of_match[] = {
452         { .compatible = "raspberrypi,bcm2835-firmware", },
453         {},
454 };
455 MODULE_DEVICE_TABLE(of, rpi_firmware_of_match);
456
457 struct device_node *rpi_firmware_find_node(void)
458 {
459         return of_find_matching_node(NULL, rpi_firmware_of_match);
460 }
461 EXPORT_SYMBOL_GPL(rpi_firmware_find_node);
462
463 /**
464  * rpi_firmware_get - Get pointer to rpi_firmware structure.
465  * @firmware_node:    Pointer to the firmware Device Tree node.
466  *
467  * The reference to rpi_firmware has to be released with rpi_firmware_put().
468  *
469  * Returns NULL is the firmware device is not ready.
470  */
471 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
472 {
473         struct platform_device *pdev = of_find_device_by_node(firmware_node);
474         struct rpi_firmware *fw;
475
476         if (!pdev)
477                 return NULL;
478
479         fw = platform_get_drvdata(pdev);
480         if (!fw)
481                 goto err_put_device;
482
483         if (!kref_get_unless_zero(&fw->consumers))
484                 goto err_put_device;
485
486         put_device(&pdev->dev);
487
488         return fw;
489
490 err_put_device:
491         put_device(&pdev->dev);
492         return NULL;
493 }
494 EXPORT_SYMBOL_GPL(rpi_firmware_get);
495
496 /**
497  * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
498  * @firmware_node:    Pointer to the firmware Device Tree node.
499  *
500  * Returns NULL is the firmware device is not ready.
501  */
502 struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
503                                            struct device_node *firmware_node)
504 {
505         struct rpi_firmware *fw;
506
507         fw = rpi_firmware_get(firmware_node);
508         if (!fw)
509                 return NULL;
510
511         if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
512                 return NULL;
513
514         return fw;
515 }
516 EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
517
518 static struct platform_driver rpi_firmware_driver = {
519         .driver = {
520                 .name = "raspberrypi-firmware",
521                 .of_match_table = rpi_firmware_of_match,
522         },
523         .probe          = rpi_firmware_probe,
524         .shutdown       = rpi_firmware_shutdown,
525         .remove         = rpi_firmware_remove,
526 };
527
528 static struct notifier_block rpi_firmware_reboot_notifier = {
529         .notifier_call = rpi_firmware_notify_reboot,
530 };
531
532 static int __init rpi_firmware_init(void)
533 {
534         int ret = register_reboot_notifier(&rpi_firmware_reboot_notifier);
535         if (ret)
536                 goto out1;
537         ret = platform_driver_register(&rpi_firmware_driver);
538         if (ret)
539                 goto out2;
540
541         return 0;
542
543 out2:
544         unregister_reboot_notifier(&rpi_firmware_reboot_notifier);
545 out1:
546         return ret;
547 }
548 core_initcall(rpi_firmware_init);
549
550 static void __init rpi_firmware_exit(void)
551 {
552         platform_driver_unregister(&rpi_firmware_driver);
553         unregister_reboot_notifier(&rpi_firmware_reboot_notifier);
554 }
555 module_exit(rpi_firmware_exit);
556
557 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
558 MODULE_DESCRIPTION("Raspberry Pi firmware driver");
559 MODULE_LICENSE("GPL v2");