blob: 572abfa536f06dfe85a5d216f91ce9a13b659892 [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 Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Simon Glass11c89f32017-05-17 17:18:03 -060011#include <dm.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070012#include <irq_func.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090013#include <dm/lists.h>
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020014#include <efi_loader.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090016#include <linux/arm-smccc.h>
17#include <linux/errno.h>
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090018#include <linux/printk.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090019#include <linux/psci.h>
20
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020021#define DRIVER_NAME "psci"
Masahiro Yamada836c55d2017-04-14 11:10:24 +090022
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020023#define PSCI_METHOD_HVC 1
24#define PSCI_METHOD_SMC 2
Masahiro Yamada836c55d2017-04-14 11:10:24 +090025
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020026int __efi_runtime_data psci_method;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090027
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020028unsigned long __efi_runtime invoke_psci_fn
29 (unsigned long function_id, unsigned long arg0,
30 unsigned long arg1, unsigned long arg2)
Masahiro Yamada836c55d2017-04-14 11:10:24 +090031{
32 struct arm_smccc_res res;
33
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020034 /*
35 * In the __efi_runtime we need to avoid the switch statement. In some
36 * cases the compiler creates lookup tables to implement switch. These
37 * tables are not correctly relocated when SetVirtualAddressMap is
38 * called.
39 */
40 if (psci_method == PSCI_METHOD_SMC)
41 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
42 else if (psci_method == PSCI_METHOD_HVC)
43 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
44 else
45 res.a0 = PSCI_RET_DISABLED;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090046 return res.a0;
47}
48
49static int psci_bind(struct udevice *dev)
50{
51 /* No SYSTEM_RESET support for PSCI 0.1 */
Simon Glass54cbcc82017-05-18 20:08:57 -060052 if (device_is_compatible(dev, "arm,psci-0.2") ||
53 device_is_compatible(dev, "arm,psci-1.0")) {
Masahiro Yamada836c55d2017-04-14 11:10:24 +090054 int ret;
55
56 /* bind psci-sysreset optionally */
57 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
58 NULL);
59 if (ret)
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090060 pr_debug("PSCI System Reset was not bound.\n");
Masahiro Yamada836c55d2017-04-14 11:10:24 +090061 }
62
63 return 0;
64}
65
66static int psci_probe(struct udevice *dev)
67{
68 DECLARE_GLOBAL_DATA_PTR;
69 const char *method;
70
Simon Glass7a494432017-05-17 17:18:09 -060071 method = fdt_stringlist_get(gd->fdt_blob, dev_of_offset(dev), "method",
72 0, NULL);
Masahiro Yamada836c55d2017-04-14 11:10:24 +090073 if (!method) {
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090074 pr_warn("missing \"method\" property\n");
Masahiro Yamada836c55d2017-04-14 11:10:24 +090075 return -ENXIO;
76 }
77
78 if (!strcmp("hvc", method)) {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020079 psci_method = PSCI_METHOD_HVC;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090080 } else if (!strcmp("smc", method)) {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020081 psci_method = PSCI_METHOD_SMC;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090082 } else {
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090083 pr_warn("invalid \"method\" property: %s\n", method);
Masahiro Yamada836c55d2017-04-14 11:10:24 +090084 return -EINVAL;
85 }
86
87 return 0;
88}
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020089
90/**
91 * void do_psci_probe() - probe PSCI firmware driver
92 *
93 * Ensure that psci_method is initialized.
94 */
95static void __maybe_unused do_psci_probe(void)
96{
97 struct udevice *dev;
98
99 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
100}
101
102#if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
103efi_status_t efi_reset_system_init(void)
104{
105 do_psci_probe();
106 return EFI_SUCCESS;
107}
108
109void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
110 efi_status_t reset_status,
111 unsigned long data_size,
112 void *reset_data)
113{
114 if (reset_type == EFI_RESET_COLD ||
115 reset_type == EFI_RESET_WARM ||
116 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
117 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
118 } else if (reset_type == EFI_RESET_SHUTDOWN) {
119 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
120 }
121 while (1)
122 ;
123}
124#endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
125
126#ifdef CONFIG_PSCI_RESET
127void reset_misc(void)
128{
129 do_psci_probe();
130 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
131}
132#endif /* CONFIG_PSCI_RESET */
133
134#ifdef CONFIG_CMD_POWEROFF
Simon Glassed38aef2020-05-10 11:40:03 -0600135int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200136{
137 do_psci_probe();
138
139 puts("poweroff ...\n");
140 udelay(50000); /* wait 50 ms */
141
142 disable_interrupts();
143 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
144 enable_interrupts();
145
146 log_err("Power off not supported on this platform\n");
147 return CMD_RET_FAILURE;
148}
149#endif
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900150
151static const struct udevice_id psci_of_match[] = {
152 { .compatible = "arm,psci" },
153 { .compatible = "arm,psci-0.2" },
154 { .compatible = "arm,psci-1.0" },
155 {},
156};
157
158U_BOOT_DRIVER(psci) = {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200159 .name = DRIVER_NAME,
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900160 .id = UCLASS_FIRMWARE,
161 .of_match = psci_of_match,
162 .bind = psci_bind,
163 .probe = psci_probe,
164};