1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015-2016 Freescale Semiconductor, Inc.
9 #include <asm/secure.h>
10 #include <asm/arch/imx-regs.h>
11 #include <linux/bitops.h>
15 #define GPC_CPU_PGC_SW_PDN_REQ 0xfc
16 #define GPC_CPU_PGC_SW_PUP_REQ 0xf0
17 #define GPC_PGC_C0 0x800
18 #define GPC_PGC_C1 0x840
20 #define BM_CPU_PGC_SW_PDN_PUP_REQ_CORE0_A7 0x1
21 #define BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7 0x2
23 /* below is for i.MX7D */
24 #define SRC_GPR1_MX7D 0x074
25 #define SRC_A7RCR0 0x004
26 #define SRC_A7RCR1 0x008
28 #define BP_SRC_A7RCR0_A7_CORE_RESET0 0
29 #define BP_SRC_A7RCR1_A7_CORE1_ENABLE 1
31 #define SNVS_LPCR 0x38
32 #define BP_SNVS_LPCR_DP_EN 0x20
33 #define BP_SNVS_LPCR_TOP 0x40
35 #define CCM_CCGR_SNVS 0x4250
37 #define CCM_ROOT_WDOG 0xbb80
38 #define CCM_CCGR_WDOG1 0x49c0
40 #define MPIDR_AFF0 GENMASK(7, 0)
42 #define IMX7D_PSCI_NR_CPUS 2
43 #if IMX7D_PSCI_NR_CPUS > CONFIG_ARMV7_PSCI_NR_CPUS
44 #error "invalid value for CONFIG_ARMV7_PSCI_NR_CPUS"
47 u8 psci_state[IMX7D_PSCI_NR_CPUS] __secure_data = {
48 PSCI_AFFINITY_LEVEL_ON,
49 PSCI_AFFINITY_LEVEL_OFF};
51 static inline void psci_set_state(int cpu, u8 state)
53 psci_state[cpu] = state;
58 static inline void imx_gpcv2_set_m_core_pgc(bool enable, u32 offset)
60 writel(enable, GPC_IPS_BASE_ADDR + offset);
63 __secure void imx_gpcv2_set_core_power(int cpu, bool pdn)
65 u32 reg = pdn ? GPC_CPU_PGC_SW_PUP_REQ : GPC_CPU_PGC_SW_PDN_REQ;
66 u32 pgc = cpu ? GPC_PGC_C1 : GPC_PGC_C0;
67 u32 pdn_pup_req = cpu ? BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7 :
68 BM_CPU_PGC_SW_PDN_PUP_REQ_CORE0_A7;
71 imx_gpcv2_set_m_core_pgc(true, pgc);
73 val = readl(GPC_IPS_BASE_ADDR + reg);
75 writel(val, GPC_IPS_BASE_ADDR + reg);
77 while ((readl(GPC_IPS_BASE_ADDR + reg) & pdn_pup_req) != 0)
80 imx_gpcv2_set_m_core_pgc(false, pgc);
83 __secure void imx_enable_cpu_ca7(int cpu, bool enable)
87 mask = 1 << (BP_SRC_A7RCR1_A7_CORE1_ENABLE + cpu - 1);
88 val = readl(SRC_BASE_ADDR + SRC_A7RCR1);
89 val = enable ? val | mask : val & ~mask;
90 writel(val, SRC_BASE_ADDR + SRC_A7RCR1);
93 __secure void psci_arch_cpu_entry(void)
95 u32 cpu = psci_get_cpu_id();
97 psci_set_state(cpu, PSCI_AFFINITY_LEVEL_ON);
100 __secure s32 psci_cpu_on(u32 __always_unused function_id, u32 mpidr, u32 ep,
103 u32 cpu = mpidr & MPIDR_AFF0;
105 if (mpidr & ~MPIDR_AFF0)
106 return ARM_PSCI_RET_INVAL;
108 if (cpu >= IMX7D_PSCI_NR_CPUS)
109 return ARM_PSCI_RET_INVAL;
111 if (psci_state[cpu] == PSCI_AFFINITY_LEVEL_ON)
112 return ARM_PSCI_RET_ALREADY_ON;
114 if (psci_state[cpu] == PSCI_AFFINITY_LEVEL_ON_PENDING)
115 return ARM_PSCI_RET_ON_PENDING;
117 psci_save(cpu, ep, context_id);
119 writel((u32)psci_cpu_entry, SRC_BASE_ADDR + cpu * 8 + SRC_GPR1_MX7D);
121 psci_set_state(cpu, PSCI_AFFINITY_LEVEL_ON_PENDING);
123 imx_gpcv2_set_core_power(cpu, true);
124 imx_enable_cpu_ca7(cpu, true);
126 return ARM_PSCI_RET_SUCCESS;
129 __secure s32 psci_cpu_off(void)
133 cpu = psci_get_cpu_id();
135 psci_cpu_off_common();
136 psci_set_state(cpu, PSCI_AFFINITY_LEVEL_OFF);
138 imx_enable_cpu_ca7(cpu, false);
139 imx_gpcv2_set_core_power(cpu, false);
140 writel(0, SRC_BASE_ADDR + cpu * 8 + SRC_GPR1_MX7D + 4);
146 __secure void psci_system_reset(void)
148 struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
150 /* make sure WDOG1 clock is enabled */
151 writel(0x1 << 28, CCM_BASE_ADDR + CCM_ROOT_WDOG);
152 writel(0x3, CCM_BASE_ADDR + CCM_CCGR_WDOG1);
153 writew(WCR_WDE, &wdog->wcr);
159 __secure void psci_system_off(void)
163 /* make sure SNVS clock is enabled */
164 writel(0x3, CCM_BASE_ADDR + CCM_CCGR_SNVS);
166 val = readl(SNVS_BASE_ADDR + SNVS_LPCR);
167 val |= BP_SNVS_LPCR_DP_EN | BP_SNVS_LPCR_TOP;
168 writel(val, SNVS_BASE_ADDR + SNVS_LPCR);
174 __secure u32 psci_version(void)
176 return ARM_PSCI_VER_1_0;
179 __secure s32 psci_cpu_suspend(u32 __always_unused function_id, u32 power_state,
180 u32 entry_point_address,
183 return ARM_PSCI_RET_INVAL;
186 __secure s32 psci_affinity_info(u32 __always_unused function_id,
188 u32 lowest_affinity_level)
190 u32 cpu = target_affinity & MPIDR_AFF0;
192 if (lowest_affinity_level > 0)
193 return ARM_PSCI_RET_INVAL;
195 if (target_affinity & ~MPIDR_AFF0)
196 return ARM_PSCI_RET_INVAL;
198 if (cpu >= IMX7D_PSCI_NR_CPUS)
199 return ARM_PSCI_RET_INVAL;
201 return psci_state[cpu];
204 __secure s32 psci_migrate_info_type(u32 function_id)
206 /* Trusted OS is either not present or does not require migration */
210 __secure s32 psci_features(u32 __always_unused function_id, u32 psci_fid)
213 case ARM_PSCI_0_2_FN_PSCI_VERSION:
214 case ARM_PSCI_0_2_FN_CPU_OFF:
215 case ARM_PSCI_0_2_FN_CPU_ON:
216 case ARM_PSCI_0_2_FN_AFFINITY_INFO:
217 case ARM_PSCI_0_2_FN_MIGRATE_INFO_TYPE:
218 case ARM_PSCI_0_2_FN_SYSTEM_OFF:
219 case ARM_PSCI_0_2_FN_SYSTEM_RESET:
220 case ARM_PSCI_1_0_FN_PSCI_FEATURES:
223 return ARM_PSCI_RET_NI;