1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Masahiro Yamada <yamada.masahiro@socionext.com>
5 * Based on drivers/firmware/psci.c from Linux:
6 * Copyright (C) 2015 ARM Limited
15 #include <efi_loader.h>
16 #include <linux/delay.h>
17 #include <linux/libfdt.h>
18 #include <linux/arm-smccc.h>
19 #include <linux/errno.h>
20 #include <linux/printk.h>
21 #include <linux/psci.h>
23 #define DRIVER_NAME "psci"
25 #define PSCI_METHOD_HVC 1
26 #define PSCI_METHOD_SMC 2
28 #if CONFIG_IS_ENABLED(EFI_LOADER)
29 int __efi_runtime_data psci_method;
31 int psci_method __attribute__ ((section(".data")));
34 unsigned long __efi_runtime invoke_psci_fn
35 (unsigned long function_id, unsigned long arg0,
36 unsigned long arg1, unsigned long arg2)
38 struct arm_smccc_res res;
41 * In the __efi_runtime we need to avoid the switch statement. In some
42 * cases the compiler creates lookup tables to implement switch. These
43 * tables are not correctly relocated when SetVirtualAddressMap is
46 if (psci_method == PSCI_METHOD_SMC)
47 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
48 else if (psci_method == PSCI_METHOD_HVC)
49 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
51 res.a0 = PSCI_RET_DISABLED;
55 static int psci_bind(struct udevice *dev)
57 /* No SYSTEM_RESET support for PSCI 0.1 */
58 if (device_is_compatible(dev, "arm,psci-0.2") ||
59 device_is_compatible(dev, "arm,psci-1.0")) {
62 /* bind psci-sysreset optionally */
63 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
66 pr_debug("PSCI System Reset was not bound.\n");
72 static int psci_probe(struct udevice *dev)
76 method = ofnode_read_string(dev_ofnode(dev), "method");
78 pr_warn("missing \"method\" property\n");
82 if (!strcmp("hvc", method)) {
83 psci_method = PSCI_METHOD_HVC;
84 } else if (!strcmp("smc", method)) {
85 psci_method = PSCI_METHOD_SMC;
87 pr_warn("invalid \"method\" property: %s\n", method);
95 * void do_psci_probe() - probe PSCI firmware driver
97 * Ensure that psci_method is initialized.
99 static void __maybe_unused do_psci_probe(void)
103 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
106 #if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
107 efi_status_t efi_reset_system_init(void)
113 void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
114 efi_status_t reset_status,
115 unsigned long data_size,
118 if (reset_type == EFI_RESET_COLD ||
119 reset_type == EFI_RESET_WARM ||
120 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
121 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
122 } else if (reset_type == EFI_RESET_SHUTDOWN) {
123 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
128 #endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
130 #ifdef CONFIG_PSCI_RESET
131 void reset_misc(void)
134 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
136 #endif /* CONFIG_PSCI_RESET */
138 #ifdef CONFIG_CMD_POWEROFF
139 int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
143 puts("poweroff ...\n");
144 udelay(50000); /* wait 50 ms */
146 disable_interrupts();
147 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
150 log_err("Power off not supported on this platform\n");
151 return CMD_RET_FAILURE;
155 static const struct udevice_id psci_of_match[] = {
156 { .compatible = "arm,psci" },
157 { .compatible = "arm,psci-0.2" },
158 { .compatible = "arm,psci-1.0" },
162 U_BOOT_DRIVER(psci) = {
164 .id = UCLASS_FIRMWARE,
165 .of_match = psci_of_match,