348df893b7ae74345d62ee63b6a471917a37e75f
[oweals/u-boot.git] / arch / arm / lib / psci-dt.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2016 NXP Semiconductor, Inc.
4  */
5
6 #include <common.h>
7 #include <linux/libfdt.h>
8 #include <fdt_support.h>
9 #include <linux/sizes.h>
10 #include <linux/kernel.h>
11 #include <asm/psci.h>
12 #ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
13 #include <asm/armv8/sec_firmware.h>
14 #endif
15
16 int fdt_psci(void *fdt)
17 {
18 #if defined(CONFIG_ARMV7_PSCI) || defined(CONFIG_ARMV8_PSCI) || \
19         defined(CONFIG_SEC_FIRMWARE_ARMV8_PSCI)
20         int nodeoff;
21         unsigned int psci_ver = 0;
22         int tmp;
23
24         nodeoff = fdt_path_offset(fdt, "/cpus");
25         if (nodeoff < 0) {
26                 printf("couldn't find /cpus\n");
27                 return nodeoff;
28         }
29
30         /* add 'enable-method = "psci"' to each cpu node */
31         for (tmp = fdt_first_subnode(fdt, nodeoff);
32              tmp >= 0;
33              tmp = fdt_next_subnode(fdt, tmp)) {
34                 const struct fdt_property *prop;
35                 int len;
36
37                 prop = fdt_get_property(fdt, tmp, "device_type", &len);
38                 if (!prop)
39                         continue;
40                 if (len < 4)
41                         continue;
42                 if (strcmp(prop->data, "cpu"))
43                         continue;
44
45                 /*
46                  * Not checking rv here, our approach is to skip over errors in
47                  * individual cpu nodes, hopefully some of the nodes are
48                  * processed correctly and those will boot
49                  */
50                 fdt_setprop_string(fdt, tmp, "enable-method", "psci");
51         }
52
53         nodeoff = fdt_path_offset(fdt, "/psci");
54         if (nodeoff >= 0)
55                 goto init_psci_node;
56
57         nodeoff = fdt_path_offset(fdt, "/");
58         if (nodeoff < 0)
59                 return nodeoff;
60
61         nodeoff = fdt_add_subnode(fdt, nodeoff, "psci");
62         if (nodeoff < 0)
63                 return nodeoff;
64
65 init_psci_node:
66 #ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
67         psci_ver = sec_firmware_support_psci_version();
68 #elif defined(CONFIG_ARMV7_PSCI_1_0) || defined(CONFIG_ARMV8_PSCI)
69         psci_ver = ARM_PSCI_VER_1_0;
70 #elif defined(CONFIG_ARMV7_PSCI_0_2)
71         psci_ver = ARM_PSCI_VER_0_2;
72 #endif
73         if (psci_ver >= ARM_PSCI_VER_1_0) {
74                 tmp = fdt_setprop_string(fdt, nodeoff,
75                                 "compatible", "arm,psci-1.0");
76                 if (tmp)
77                         return tmp;
78         }
79
80         if (psci_ver >= ARM_PSCI_VER_0_2) {
81                 tmp = fdt_appendprop_string(fdt, nodeoff,
82                                 "compatible", "arm,psci-0.2");
83                 if (tmp)
84                         return tmp;
85         }
86
87 #ifndef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
88         /*
89          * The Secure firmware framework isn't able to support PSCI version 0.1.
90          */
91         if (psci_ver < ARM_PSCI_VER_0_2) {
92                 tmp = fdt_appendprop_string(fdt, nodeoff,
93                                 "compatible", "arm,psci");
94                 if (tmp)
95                         return tmp;
96                 tmp = fdt_setprop_u32(fdt, nodeoff, "cpu_suspend",
97                                 ARM_PSCI_FN_CPU_SUSPEND);
98                 if (tmp)
99                         return tmp;
100                 tmp = fdt_setprop_u32(fdt, nodeoff, "cpu_off",
101                                 ARM_PSCI_FN_CPU_OFF);
102                 if (tmp)
103                         return tmp;
104                 tmp = fdt_setprop_u32(fdt, nodeoff, "cpu_on",
105                                 ARM_PSCI_FN_CPU_ON);
106                 if (tmp)
107                         return tmp;
108                 tmp = fdt_setprop_u32(fdt, nodeoff, "migrate",
109                                 ARM_PSCI_FN_MIGRATE);
110                 if (tmp)
111                         return tmp;
112         }
113 #endif
114
115         tmp = fdt_setprop_string(fdt, nodeoff, "method", "smc");
116         if (tmp)
117                 return tmp;
118
119 #endif
120         return 0;
121 }