xref: /rk3399_ARM-atf/plat/nvidia/tegra/soc/t210/plat_psci_handlers.c (revision c948f77136c42a92d0bb660543a3600c36dcf7f1)
1 /*
2  * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <cortex_a57.h>
9 #include <arch_helpers.h>
10 #include <common/debug.h>
11 #include <drivers/delay_timer.h>
12 #include <lib/mmio.h>
13 #include <lib/psci/psci.h>
14 #include <plat/common/platform.h>
15 
16 #include <bpmp.h>
17 #include <flowctrl.h>
18 #include <pmc.h>
19 #include <platform_def.h>
20 #include <security_engine.h>
21 #include <tegra_def.h>
22 #include <tegra_private.h>
23 #include <tegra_platform.h>
24 
25 /*
26  * Register used to clear CPU reset signals. Each CPU has two reset
27  * signals: CPU reset (3:0) and Core reset (19:16).
28  */
29 #define CPU_CMPLX_RESET_CLR		0x454
30 #define CPU_CORE_RESET_MASK		0x10001
31 
32 /* Clock and Reset controller registers for system clock's settings */
33 #define SCLK_RATE			0x30
34 #define SCLK_BURST_POLICY		0x28
35 #define SCLK_BURST_POLICY_DEFAULT	0x10000000
36 
37 static int cpu_powergate_mask[PLATFORM_MAX_CPUS_PER_CLUSTER];
38 
39 int32_t tegra_soc_validate_power_state(unsigned int power_state,
40 					psci_power_state_t *req_state)
41 {
42 	int state_id = psci_get_pstate_id(power_state);
43 
44 	/* Sanity check the requested state id */
45 	switch (state_id) {
46 	case PSTATE_ID_CORE_POWERDN:
47 		/*
48 		 * Core powerdown request only for afflvl 0
49 		 */
50 		req_state->pwr_domain_state[MPIDR_AFFLVL0] = state_id & 0xff;
51 
52 		break;
53 
54 	case PSTATE_ID_CLUSTER_IDLE:
55 	case PSTATE_ID_CLUSTER_POWERDN:
56 		/*
57 		 * Cluster powerdown/idle request only for afflvl 1
58 		 */
59 		req_state->pwr_domain_state[MPIDR_AFFLVL1] = state_id;
60 		req_state->pwr_domain_state[MPIDR_AFFLVL0] = PSTATE_ID_CORE_POWERDN;
61 
62 		break;
63 
64 	case PSTATE_ID_SOC_POWERDN:
65 		/*
66 		 * System powerdown request only for afflvl 2
67 		 */
68 		for (uint32_t i = MPIDR_AFFLVL0; i < PLAT_MAX_PWR_LVL; i++)
69 			req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
70 
71 		req_state->pwr_domain_state[PLAT_MAX_PWR_LVL] =
72 			PLAT_SYS_SUSPEND_STATE_ID;
73 
74 		break;
75 
76 	default:
77 		ERROR("%s: unsupported state id (%d)\n", __func__, state_id);
78 		return PSCI_E_INVALID_PARAMS;
79 	}
80 
81 	return PSCI_E_SUCCESS;
82 }
83 
84 /*******************************************************************************
85  * Platform handler to calculate the proper target power level at the
86  * specified affinity level
87  ******************************************************************************/
88 plat_local_state_t tegra_soc_get_target_pwr_state(unsigned int lvl,
89 					     const plat_local_state_t *states,
90 					     unsigned int ncpu)
91 {
92 	plat_local_state_t target = PSCI_LOCAL_STATE_RUN;
93 	int cpu = plat_my_core_pos();
94 	int core_pos = read_mpidr() & MPIDR_CPU_MASK;
95 	uint32_t bpmp_reply, data[3];
96 	int ret;
97 
98 	/* get the power state at this level */
99 	if (lvl == MPIDR_AFFLVL1)
100 		target = *(states + core_pos);
101 	if (lvl == MPIDR_AFFLVL2)
102 		target = *(states + cpu);
103 
104 	if ((lvl == MPIDR_AFFLVL1) && (target == PSTATE_ID_CLUSTER_IDLE)) {
105 
106 		/* initialize the bpmp interface */
107 		(void)tegra_bpmp_init();
108 
109 		/* Cluster idle */
110 		data[0] = (uint32_t)cpu;
111 		data[1] = TEGRA_PM_CC6;
112 		data[2] = TEGRA_PM_SC1;
113 		ret = tegra_bpmp_send_receive_atomic(MRQ_DO_IDLE,
114 				(void *)&data, (int)sizeof(data),
115 				(void *)&bpmp_reply, (int)sizeof(bpmp_reply));
116 
117 		/* check if cluster idle entry is allowed */
118 		if ((ret != 0L) || (bpmp_reply != BPMP_CCx_ALLOWED)) {
119 
120 			/* Cluster idle not allowed */
121 			target = PSCI_LOCAL_STATE_RUN;
122 		}
123 
124 	} else if ((lvl == MPIDR_AFFLVL1) && (target == PSTATE_ID_CLUSTER_POWERDN)) {
125 
126 		/* initialize the bpmp interface */
127 		(void)tegra_bpmp_init();
128 
129 		/* Cluster power-down */
130 		data[0] = (uint32_t)cpu;
131 		data[1] = TEGRA_PM_CC7;
132 		data[2] = TEGRA_PM_SC1;
133 		ret = tegra_bpmp_send_receive_atomic(MRQ_DO_IDLE,
134 				(void *)&data, (int)sizeof(data),
135 				(void *)&bpmp_reply, (int)sizeof(bpmp_reply));
136 
137 		/* check if cluster power down is allowed */
138 		if ((ret != 0L) || (bpmp_reply != BPMP_CCx_ALLOWED)) {
139 
140 			/* Cluster power down not allowed */
141 			target = PSCI_LOCAL_STATE_RUN;
142 		}
143 
144 	} else if (((lvl == MPIDR_AFFLVL2) || (lvl == MPIDR_AFFLVL1)) &&
145 	    (target == PSTATE_ID_SOC_POWERDN)) {
146 
147 		/* System Suspend */
148 		target = PSTATE_ID_SOC_POWERDN;
149 
150 	} else {
151 		; /* do nothing */
152 	}
153 
154 	return target;
155 }
156 
157 int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
158 {
159 	u_register_t mpidr = read_mpidr();
160 	const plat_local_state_t *pwr_domain_state =
161 		target_state->pwr_domain_state;
162 	unsigned int stateid_afflvl2 = pwr_domain_state[MPIDR_AFFLVL2];
163 	unsigned int stateid_afflvl1 = pwr_domain_state[MPIDR_AFFLVL1];
164 	unsigned int stateid_afflvl0 = pwr_domain_state[MPIDR_AFFLVL0];
165 	int ret = PSCI_E_SUCCESS;
166 
167 	if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
168 
169 		assert((stateid_afflvl0 == PLAT_MAX_OFF_STATE) ||
170 			(stateid_afflvl0 == PSTATE_ID_SOC_POWERDN));
171 		assert((stateid_afflvl1 == PLAT_MAX_OFF_STATE) ||
172 			(stateid_afflvl1 == PSTATE_ID_SOC_POWERDN));
173 
174 		if (tegra_chipid_is_t210_b01()) {
175 
176 			/* Suspend se/se2 and pka1 */
177 			if (tegra_se_suspend() != 0) {
178 				ret = PSCI_E_INTERN_FAIL;
179 			}
180 
181 			/* Save tzram contents */
182 			if (tegra_se_save_tzram() != 0) {
183 				ret = PSCI_E_INTERN_FAIL;
184 			}
185 		}
186 
187 		/* enter system suspend */
188 		if (ret == PSCI_E_SUCCESS) {
189 			tegra_fc_soc_powerdn(mpidr);
190 		}
191 
192 	} else if (stateid_afflvl1 == PSTATE_ID_CLUSTER_IDLE) {
193 
194 		assert(stateid_afflvl0 == PSTATE_ID_CORE_POWERDN);
195 
196 		/* Prepare for cluster idle */
197 		tegra_fc_cluster_idle(mpidr);
198 
199 	} else if (stateid_afflvl1 == PSTATE_ID_CLUSTER_POWERDN) {
200 
201 		assert(stateid_afflvl0 == PSTATE_ID_CORE_POWERDN);
202 
203 		/* Prepare for cluster powerdn */
204 		tegra_fc_cluster_powerdn(mpidr);
205 
206 	} else if (stateid_afflvl0 == PSTATE_ID_CORE_POWERDN) {
207 
208 		/* Prepare for cpu powerdn */
209 		tegra_fc_cpu_powerdn(mpidr);
210 
211 	} else {
212 		ERROR("%s: Unknown state id (%d, %d, %d)\n", __func__,
213 			stateid_afflvl2, stateid_afflvl1, stateid_afflvl0);
214 		ret = PSCI_E_NOT_SUPPORTED;
215 	}
216 
217 	return ret;
218 }
219 
220 int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
221 {
222 	const plat_params_from_bl2_t *plat_params = bl31_get_plat_params();
223 	uint32_t val;
224 
225 	/* platform parameter passed by the previous bootloader */
226 	if (plat_params->l2_ecc_parity_prot_dis != 1) {
227 		/* Enable ECC Parity Protection for Cortex-A57 CPUs */
228 		val = read_l2ctlr_el1();
229 		val |= (uint64_t)CORTEX_A57_L2_ECC_PARITY_PROTECTION_BIT;
230 		write_l2ctlr_el1(val);
231 	}
232 
233 	/*
234 	 * Check if we are exiting from SOC_POWERDN.
235 	 */
236 	if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
237 			PLAT_SYS_SUSPEND_STATE_ID) {
238 
239 		/*
240 		 * Security engine resume
241 		 */
242 		if (tegra_chipid_is_t210_b01()) {
243 			tegra_se_resume();
244 		}
245 
246 		/*
247 		 * Lock scratch registers which hold the CPU vectors
248 		 */
249 		tegra_pmc_lock_cpu_vectors();
250 
251 		/*
252 		 * Enable WRAP to INCR burst type conversions for
253 		 * incoming requests on the AXI slave ports.
254 		 */
255 		val = mmio_read_32(TEGRA_MSELECT_BASE + MSELECT_CONFIG);
256 		val &= ~ENABLE_UNSUP_TX_ERRORS;
257 		val |= ENABLE_WRAP_TO_INCR_BURSTS;
258 		mmio_write_32(TEGRA_MSELECT_BASE + MSELECT_CONFIG, val);
259 
260 		/*
261 		 * Restore Boot and Power Management Processor (BPMP) reset
262 		 * address and reset it.
263 		 */
264 		tegra_fc_reset_bpmp();
265 	}
266 
267 	/*
268 	 * T210 has a dedicated ARMv7 boot and power mgmt processor, BPMP. It's
269 	 * used for power management and boot purposes. Inform the BPMP that
270 	 * we have completed the cluster power up.
271 	 */
272 	tegra_fc_lock_active_cluster();
273 
274 	return PSCI_E_SUCCESS;
275 }
276 
277 int tegra_soc_pwr_domain_on(u_register_t mpidr)
278 {
279 	int cpu = mpidr & MPIDR_CPU_MASK;
280 	uint32_t mask = CPU_CORE_RESET_MASK << cpu;
281 
282 	/* Deassert CPU reset signals */
283 	mmio_write_32(TEGRA_CAR_RESET_BASE + CPU_CMPLX_RESET_CLR, mask);
284 
285 	/* Turn on CPU using flow controller or PMC */
286 	if (cpu_powergate_mask[cpu] == 0) {
287 		tegra_pmc_cpu_on(cpu);
288 		cpu_powergate_mask[cpu] = 1;
289 	} else {
290 		tegra_fc_cpu_on(cpu);
291 	}
292 
293 	return PSCI_E_SUCCESS;
294 }
295 
296 int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
297 {
298 	tegra_fc_cpu_off(read_mpidr() & MPIDR_CPU_MASK);
299 	return PSCI_E_SUCCESS;
300 }
301 
302 int tegra_soc_prepare_system_reset(void)
303 {
304 	/*
305 	 * Set System Clock (SCLK) to POR default so that the clock source
306 	 * for the PMC APB clock would not be changed due to system reset.
307 	 */
308 	mmio_write_32((uintptr_t)TEGRA_CAR_RESET_BASE + SCLK_BURST_POLICY,
309 		SCLK_BURST_POLICY_DEFAULT);
310 	mmio_write_32((uintptr_t)TEGRA_CAR_RESET_BASE + SCLK_RATE, 0);
311 
312 	/* Wait 1 ms to make sure clock source/device logic is stabilized. */
313 	mdelay(1);
314 
315 	return PSCI_E_SUCCESS;
316 }
317