blob: c8c47acfd34066c52d3cd3289c662aec33397067 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada836c55d2017-04-14 11:10:24 +09002/*
3 * Copyright (C) 2017 Masahiro Yamada <yamada.masahiro@socionext.com>
4 *
5 * Based on drivers/firmware/psci.c from Linux:
6 * Copyright (C) 2015 ARM Limited
Masahiro Yamada836c55d2017-04-14 11:10:24 +09007 */
8
9#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -060010#include <dm.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090011#include <dm/lists.h>
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020012#include <efi_loader.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090014#include <linux/arm-smccc.h>
15#include <linux/errno.h>
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090016#include <linux/printk.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090017#include <linux/psci.h>
18
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020019#define DRIVER_NAME "psci"
Masahiro Yamada836c55d2017-04-14 11:10:24 +090020
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020021#define PSCI_METHOD_HVC 1
22#define PSCI_METHOD_SMC 2
Masahiro Yamada836c55d2017-04-14 11:10:24 +090023
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020024int __efi_runtime_data psci_method;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090025
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020026unsigned long __efi_runtime invoke_psci_fn
27 (unsigned long function_id, unsigned long arg0,
28 unsigned long arg1, unsigned long arg2)
Masahiro Yamada836c55d2017-04-14 11:10:24 +090029{
30 struct arm_smccc_res res;
31
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020032 /*
33 * In the __efi_runtime we need to avoid the switch statement. In some
34 * cases the compiler creates lookup tables to implement switch. These
35 * tables are not correctly relocated when SetVirtualAddressMap is
36 * called.
37 */
38 if (psci_method == PSCI_METHOD_SMC)
39 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
40 else if (psci_method == PSCI_METHOD_HVC)
41 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
42 else
43 res.a0 = PSCI_RET_DISABLED;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090044 return res.a0;
45}
46
47static int psci_bind(struct udevice *dev)
48{
49 /* No SYSTEM_RESET support for PSCI 0.1 */
Simon Glass54cbcc82017-05-18 20:08:57 -060050 if (device_is_compatible(dev, "arm,psci-0.2") ||
51 device_is_compatible(dev, "arm,psci-1.0")) {
Masahiro Yamada836c55d2017-04-14 11:10:24 +090052 int ret;
53
54 /* bind psci-sysreset optionally */
55 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
56 NULL);
57 if (ret)
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090058 pr_debug("PSCI System Reset was not bound.\n");
Masahiro Yamada836c55d2017-04-14 11:10:24 +090059 }
60
61 return 0;
62}
63
64static int psci_probe(struct udevice *dev)
65{
66 DECLARE_GLOBAL_DATA_PTR;
67 const char *method;
68
Simon Glass7a494432017-05-17 17:18:09 -060069 method = fdt_stringlist_get(gd->fdt_blob, dev_of_offset(dev), "method",
70 0, NULL);
Masahiro Yamada836c55d2017-04-14 11:10:24 +090071 if (!method) {
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090072 pr_warn("missing \"method\" property\n");
Masahiro Yamada836c55d2017-04-14 11:10:24 +090073 return -ENXIO;
74 }
75
76 if (!strcmp("hvc", method)) {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020077 psci_method = PSCI_METHOD_HVC;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090078 } else if (!strcmp("smc", method)) {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020079 psci_method = PSCI_METHOD_SMC;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090080 } else {
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090081 pr_warn("invalid \"method\" property: %s\n", method);
Masahiro Yamada836c55d2017-04-14 11:10:24 +090082 return -EINVAL;
83 }
84
85 return 0;
86}
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020087
88/**
89 * void do_psci_probe() - probe PSCI firmware driver
90 *
91 * Ensure that psci_method is initialized.
92 */
93static void __maybe_unused do_psci_probe(void)
94{
95 struct udevice *dev;
96
97 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
98}
99
100#if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
101efi_status_t efi_reset_system_init(void)
102{
103 do_psci_probe();
104 return EFI_SUCCESS;
105}
106
107void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
108 efi_status_t reset_status,
109 unsigned long data_size,
110 void *reset_data)
111{
112 if (reset_type == EFI_RESET_COLD ||
113 reset_type == EFI_RESET_WARM ||
114 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
115 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
116 } else if (reset_type == EFI_RESET_SHUTDOWN) {
117 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
118 }
119 while (1)
120 ;
121}
122#endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
123
124#ifdef CONFIG_PSCI_RESET
125void reset_misc(void)
126{
127 do_psci_probe();
128 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
129}
130#endif /* CONFIG_PSCI_RESET */
131
132#ifdef CONFIG_CMD_POWEROFF
133int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
134{
135 do_psci_probe();
136
137 puts("poweroff ...\n");
138 udelay(50000); /* wait 50 ms */
139
140 disable_interrupts();
141 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
142 enable_interrupts();
143
144 log_err("Power off not supported on this platform\n");
145 return CMD_RET_FAILURE;
146}
147#endif
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900148
149static const struct udevice_id psci_of_match[] = {
150 { .compatible = "arm,psci" },
151 { .compatible = "arm,psci-0.2" },
152 { .compatible = "arm,psci-1.0" },
153 {},
154};
155
156U_BOOT_DRIVER(psci) = {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200157 .name = DRIVER_NAME,
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900158 .id = UCLASS_FIRMWARE,
159 .of_match = psci_of_match,
160 .bind = psci_bind,
161 .probe = psci_probe,
162};