blob: 394f30fa886d798993f7cc999b23359e3d07d6e9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada573a3812017-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 Yamada573a3812017-04-14 11:10:24 +09007 */
8
9#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060010#include <dm.h>
Simon Glass36bf4462019-11-14 12:57:42 -070011#include <irq_func.h>
Masahiro Yamada573a3812017-04-14 11:10:24 +090012#include <dm/lists.h>
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020013#include <efi_loader.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Masahiro Yamada573a3812017-04-14 11:10:24 +090015#include <linux/arm-smccc.h>
16#include <linux/errno.h>
Masahiro Yamadaaf4e6d32017-11-29 15:04:40 +090017#include <linux/printk.h>
Masahiro Yamada573a3812017-04-14 11:10:24 +090018#include <linux/psci.h>
19
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020020#define DRIVER_NAME "psci"
Masahiro Yamada573a3812017-04-14 11:10:24 +090021
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020022#define PSCI_METHOD_HVC 1
23#define PSCI_METHOD_SMC 2
24
25int __efi_runtime_data psci_method;
26
27unsigned long __efi_runtime invoke_psci_fn
28 (unsigned long function_id, unsigned long arg0,
29 unsigned long arg1, unsigned long arg2)
Masahiro Yamada573a3812017-04-14 11:10:24 +090030{
31 struct arm_smccc_res res;
32
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020033 /*
34 * In the __efi_runtime we need to avoid the switch statement. In some
35 * cases the compiler creates lookup tables to implement switch. These
36 * tables are not correctly relocated when SetVirtualAddressMap is
37 * called.
38 */
39 if (psci_method == PSCI_METHOD_SMC)
40 arm_smccc_smc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
41 else if (psci_method == PSCI_METHOD_HVC)
42 arm_smccc_hvc(function_id, arg0, arg1, arg2, 0, 0, 0, 0, &res);
43 else
44 res.a0 = PSCI_RET_DISABLED;
Masahiro Yamada573a3812017-04-14 11:10:24 +090045 return res.a0;
46}
47
48static int psci_bind(struct udevice *dev)
49{
50 /* No SYSTEM_RESET support for PSCI 0.1 */
Simon Glass911f3ae2017-05-18 20:08:57 -060051 if (device_is_compatible(dev, "arm,psci-0.2") ||
52 device_is_compatible(dev, "arm,psci-1.0")) {
Masahiro Yamada573a3812017-04-14 11:10:24 +090053 int ret;
54
55 /* bind psci-sysreset optionally */
56 ret = device_bind_driver(dev, "psci-sysreset", "psci-sysreset",
57 NULL);
58 if (ret)
Masahiro Yamadaaf4e6d32017-11-29 15:04:40 +090059 pr_debug("PSCI System Reset was not bound.\n");
Masahiro Yamada573a3812017-04-14 11:10:24 +090060 }
61
62 return 0;
63}
64
65static int psci_probe(struct udevice *dev)
66{
67 DECLARE_GLOBAL_DATA_PTR;
68 const char *method;
69
Simon Glassda409cc2017-05-17 17:18:09 -060070 method = fdt_stringlist_get(gd->fdt_blob, dev_of_offset(dev), "method",
71 0, NULL);
Masahiro Yamada573a3812017-04-14 11:10:24 +090072 if (!method) {
Masahiro Yamadaaf4e6d32017-11-29 15:04:40 +090073 pr_warn("missing \"method\" property\n");
Masahiro Yamada573a3812017-04-14 11:10:24 +090074 return -ENXIO;
75 }
76
77 if (!strcmp("hvc", method)) {
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020078 psci_method = PSCI_METHOD_HVC;
Masahiro Yamada573a3812017-04-14 11:10:24 +090079 } else if (!strcmp("smc", method)) {
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020080 psci_method = PSCI_METHOD_SMC;
Masahiro Yamada573a3812017-04-14 11:10:24 +090081 } else {
Masahiro Yamadaaf4e6d32017-11-29 15:04:40 +090082 pr_warn("invalid \"method\" property: %s\n", method);
Masahiro Yamada573a3812017-04-14 11:10:24 +090083 return -EINVAL;
84 }
85
86 return 0;
87}
88
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +020089/**
90 * void do_psci_probe() - probe PSCI firmware driver
91 *
92 * Ensure that psci_method is initialized.
93 */
94static void __maybe_unused do_psci_probe(void)
95{
96 struct udevice *dev;
97
98 uclass_get_device_by_name(UCLASS_FIRMWARE, DRIVER_NAME, &dev);
99}
100
101#if IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET)
102efi_status_t efi_reset_system_init(void)
103{
104 do_psci_probe();
105 return EFI_SUCCESS;
106}
107
108void __efi_runtime EFIAPI efi_reset_system(enum efi_reset_type reset_type,
109 efi_status_t reset_status,
110 unsigned long data_size,
111 void *reset_data)
112{
113 if (reset_type == EFI_RESET_COLD ||
114 reset_type == EFI_RESET_WARM ||
115 reset_type == EFI_RESET_PLATFORM_SPECIFIC) {
116 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
117 } else if (reset_type == EFI_RESET_SHUTDOWN) {
118 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
119 }
120 while (1)
121 ;
122}
123#endif /* IS_ENABLED(CONFIG_EFI_LOADER) && IS_ENABLED(CONFIG_PSCI_RESET) */
124
125#ifdef CONFIG_PSCI_RESET
126void reset_misc(void)
127{
128 do_psci_probe();
129 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_RESET, 0, 0, 0);
130}
131#endif /* CONFIG_PSCI_RESET */
132
133#ifdef CONFIG_CMD_POWEROFF
134int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
135{
136 do_psci_probe();
137
138 puts("poweroff ...\n");
139 udelay(50000); /* wait 50 ms */
140
141 disable_interrupts();
142 invoke_psci_fn(PSCI_0_2_FN_SYSTEM_OFF, 0, 0, 0);
143 enable_interrupts();
144
145 log_err("Power off not supported on this platform\n");
146 return CMD_RET_FAILURE;
147}
148#endif
149
Masahiro Yamada573a3812017-04-14 11:10:24 +0900150static const struct udevice_id psci_of_match[] = {
151 { .compatible = "arm,psci" },
152 { .compatible = "arm,psci-0.2" },
153 { .compatible = "arm,psci-1.0" },
154 {},
155};
156
157U_BOOT_DRIVER(psci) = {
Heinrich Schuchardt81ea0082018-10-18 12:29:40 +0200158 .name = DRIVER_NAME,
Masahiro Yamada573a3812017-04-14 11:10:24 +0900159 .id = UCLASS_FIRMWARE,
160 .of_match = psci_of_match,
161 .bind = psci_bind,
162 .probe = psci_probe,
163};