blob: e0d66d74f54de842167ecd0d33d6ac11a86cdad0 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090014#include <dm/lists.h>
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020015#include <efi_loader.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090018#include <linux/arm-smccc.h>
19#include <linux/errno.h>
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090020#include <linux/printk.h>
Masahiro Yamada836c55d2017-04-14 11:10:24 +090021#include <linux/psci.h>
22
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020023#define DRIVER_NAME "psci"
Masahiro Yamada836c55d2017-04-14 11:10:24 +090024
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020025#define PSCI_METHOD_HVC 1
26#define PSCI_METHOD_SMC 2
Masahiro Yamada836c55d2017-04-14 11:10:24 +090027
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020028int __efi_runtime_data psci_method;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090029
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020030unsigned long __efi_runtime invoke_psci_fn
31 (unsigned long function_id, unsigned long arg0,
32 unsigned long arg1, unsigned long arg2)
Masahiro Yamada836c55d2017-04-14 11:10:24 +090033{
34 struct arm_smccc_res res;
35
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020036 /*
37 * In the __efi_runtime we need to avoid the switch statement. In some
38 * cases the compiler creates lookup tables to implement switch. These
39 * tables are not correctly relocated when SetVirtualAddressMap is
40 * called.
41 */
42 if (psci_method == PSCI_METHOD_SMC)
43 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
44 else if (psci_method == PSCI_METHOD_HVC)
45 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
46 else
47 res.a0 = PSCI_RET_DISABLED;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090048 return res.a0;
49}
50
51static int psci_bind(struct udevice *dev)
52{
53 /* No SYSTEM_RESET support for PSCI 0.1 */
Simon Glass54cbcc82017-05-18 20:08:57 -060054 if (device_is_compatible(dev, "arm,psci-0.2") ||
55 device_is_compatible(dev, "arm,psci-1.0")) {
Masahiro Yamada836c55d2017-04-14 11:10:24 +090056 int ret;
57
58 /* bind psci-sysreset optionally */
59 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
60 NULL);
61 if (ret)
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090062 pr_debug("PSCI System Reset was not bound.\n");
Masahiro Yamada836c55d2017-04-14 11:10:24 +090063 }
64
65 return 0;
66}
67
68static int psci_probe(struct udevice *dev)
69{
70 DECLARE_GLOBAL_DATA_PTR;
71 const char *method;
72
Simon Glass7a494432017-05-17 17:18:09 -060073 method = fdt_stringlist_get(gd->fdt_blob, dev_of_offset(dev), "method",
74 0, NULL);
Masahiro Yamada836c55d2017-04-14 11:10:24 +090075 if (!method) {
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090076 pr_warn("missing \"method\" property\n");
Masahiro Yamada836c55d2017-04-14 11:10:24 +090077 return -ENXIO;
78 }
79
80 if (!strcmp("hvc", method)) {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020081 psci_method = PSCI_METHOD_HVC;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090082 } else if (!strcmp("smc", method)) {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020083 psci_method = PSCI_METHOD_SMC;
Masahiro Yamada836c55d2017-04-14 11:10:24 +090084 } else {
Masahiro Yamada8fa57ed2017-11-29 15:04:40 +090085 pr_warn("invalid \"method\" property: %s\n", method);
Masahiro Yamada836c55d2017-04-14 11:10:24 +090086 return -EINVAL;
87 }
88
89 return 0;
90}
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +020091
92/**
93 * void do_psci_probe() - probe PSCI firmware driver
94 *
95 * Ensure that psci_method is initialized.
96 */
97static void __maybe_unused do_psci_probe(void)
98{
99 struct udevice *dev;
100
101 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
102}
103
104#if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
105efi_status_t efi_reset_system_init(void)
106{
107 do_psci_probe();
108 return EFI_SUCCESS;
109}
110
111void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
112 efi_status_t reset_status,
113 unsigned long data_size,
114 void *reset_data)
115{
116 if (reset_type == EFI_RESET_COLD ||
117 reset_type == EFI_RESET_WARM ||
118 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
119 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
120 } else if (reset_type == EFI_RESET_SHUTDOWN) {
121 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
122 }
123 while (1)
124 ;
125}
126#endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
127
128#ifdef CONFIG_PSCI_RESET
129void reset_misc(void)
130{
131 do_psci_probe();
132 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
133}
134#endif /* CONFIG_PSCI_RESET */
135
136#ifdef CONFIG_CMD_POWEROFF
Simon Glassed38aef2020-05-10 11:40:03 -0600137int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200138{
139 do_psci_probe();
140
141 puts("poweroff ...\n");
142 udelay(50000); /* wait 50 ms */
143
144 disable_interrupts();
145 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
146 enable_interrupts();
147
148 log_err("Power off not supported on this platform\n");
149 return CMD_RET_FAILURE;
150}
151#endif
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900152
153static const struct udevice_id psci_of_match[] = {
154 { .compatible = "arm,psci" },
155 { .compatible = "arm,psci-0.2" },
156 { .compatible = "arm,psci-1.0" },
157 {},
158};
159
160U_BOOT_DRIVER(psci) = {
Heinrich Schuchardt26f09d02018-10-18 12:29:40 +0200161 .name = DRIVER_NAME,
Masahiro Yamada836c55d2017-04-14 11:10:24 +0900162 .id = UCLASS_FIRMWARE,
163 .of_match = psci_of_match,
164 .bind = psci_bind,
165 .probe = psci_probe,
166};