arm/PSCI: Fixed the backward compatiblity issue
[platform/kernel/u-boot.git] / arch / arm / lib / psci-dt.c
1 /*
2  * Copyright 2016 NXP Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <libfdt.h>
9 #include <fdt_support.h>
10 #include <linux/sizes.h>
11 #include <linux/kernel.h>
12 #include <asm/psci.h>
13 #ifdef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
14 #include <asm/armv8/sec_firmware.h>
15 #endif
16
17 int fdt_psci(void *fdt)
18 {
19 #if defined(CONFIG_ARMV8_PSCI) || defined(CONFIG_ARMV7_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 #endif
69         switch (psci_ver) {
70         case ARM_PSCI_VER_1_0:
71                 tmp = fdt_setprop_string(fdt, nodeoff,
72                                 "compatible", "arm,psci-1.0");
73                 if (tmp)
74                         return tmp;
75         case ARM_PSCI_VER_0_2:
76                 tmp = fdt_appendprop_string(fdt, nodeoff,
77                                 "compatible", "arm,psci-0.2");
78                 if (tmp)
79                         return tmp;
80         default:
81         /*
82          * The Secure firmware framework isn't able to support PSCI version 0.1.
83          */
84 #ifndef CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
85                 tmp = fdt_appendprop_string(fdt, nodeoff,
86                                 "compatible", "arm,psci");
87                 if (tmp)
88                         return tmp;
89                 tmp = fdt_setprop_u32(fdt, nodeoff, "cpu_suspend",
90                                 ARM_PSCI_FN_CPU_SUSPEND);
91                 if (tmp)
92                         return tmp;
93                 tmp = fdt_setprop_u32(fdt, nodeoff, "cpu_off",
94                                 ARM_PSCI_FN_CPU_OFF);
95                 if (tmp)
96                         return tmp;
97                 tmp = fdt_setprop_u32(fdt, nodeoff, "cpu_on",
98                                 ARM_PSCI_FN_CPU_ON);
99                 if (tmp)
100                         return tmp;
101                 tmp = fdt_setprop_u32(fdt, nodeoff, "migrate",
102                                 ARM_PSCI_FN_MIGRATE);
103                 if (tmp)
104                         return tmp;
105 #endif
106                 break;
107         }
108
109         tmp = fdt_setprop_string(fdt, nodeoff, "method", "smc");
110         if (tmp)
111                 return tmp;
112
113 #endif
114         return 0;
115 }