blob: 86021ba94ff422371626209b38db400faa78849f [file] [log] [blame]
Varun Wadekarb316e242015-05-19 16:48:04 +05301/*
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -08002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Varun Wadekarb316e242015-05-19 16:48:04 +05303 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Varun Wadekarb316e242015-05-19 16:48:04 +05305 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <bl_common.h>
Isla Mitchelle3631462017-07-14 10:46:32 +010010#include <console.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053011#include <context.h>
12#include <context_mgmt.h>
13#include <debug.h>
14#include <memctrl.h>
15#include <mmio.h>
16#include <platform.h>
17#include <platform_def.h>
18#include <pmc.h>
19#include <psci.h>
20#include <tegra_def.h>
21#include <tegra_private.h>
22
23extern uint64_t tegra_bl31_phys_base;
Varun Wadekara78bb1b2015-08-07 10:03:00 +053024extern uint64_t tegra_sec_entry_point;
Varun Wadekara2c6be62016-08-01 22:16:21 -070025extern uint64_t tegra_console_base;
Varun Wadekarb316e242015-05-19 16:48:04 +053026
27/*
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -080028 * tegra_fake_system_suspend acts as a boolean var controlling whether
29 * we are going to take fake system suspend code or normal system suspend code
30 * path. This variable is set inside the sip call handlers,when the kernel
31 * requests a SIP call to set the suspend debug flags.
32 */
33uint8_t tegra_fake_system_suspend;
34
35/*
Varun Wadekarb316e242015-05-19 16:48:04 +053036 * The following platform setup functions are weakly defined. They
37 * provide typical implementations that will be overridden by a SoC.
38 */
Varun Wadekar99782e82017-07-05 17:44:12 -070039#pragma weak tegra_soc_pwr_domain_suspend_pwrdown_early
Varun Wadekara78bb1b2015-08-07 10:03:00 +053040#pragma weak tegra_soc_pwr_domain_suspend
41#pragma weak tegra_soc_pwr_domain_on
42#pragma weak tegra_soc_pwr_domain_off
43#pragma weak tegra_soc_pwr_domain_on_finish
Varun Wadekard22429d2016-03-18 14:35:28 -070044#pragma weak tegra_soc_pwr_domain_power_down_wfi
Varun Wadekar8b82fae2015-11-09 17:39:28 -080045#pragma weak tegra_soc_prepare_system_reset
Varun Wadekare5caeed2016-01-07 14:04:21 -080046#pragma weak tegra_soc_prepare_system_off
Varun Wadekarf2aa1be2016-06-07 12:00:06 -070047#pragma weak tegra_soc_get_target_pwr_state
Varun Wadekarb316e242015-05-19 16:48:04 +053048
Varun Wadekar99782e82017-07-05 17:44:12 -070049int tegra_soc_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
50{
51 return PSCI_E_NOT_SUPPORTED;
52}
53
Varun Wadekara78bb1b2015-08-07 10:03:00 +053054int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053055{
56 return PSCI_E_NOT_SUPPORTED;
57}
58
Varun Wadekara78bb1b2015-08-07 10:03:00 +053059int tegra_soc_pwr_domain_on(u_register_t mpidr)
Varun Wadekarb316e242015-05-19 16:48:04 +053060{
61 return PSCI_E_SUCCESS;
62}
63
Varun Wadekara78bb1b2015-08-07 10:03:00 +053064int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053065{
66 return PSCI_E_SUCCESS;
67}
68
Varun Wadekara78bb1b2015-08-07 10:03:00 +053069int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053070{
71 return PSCI_E_SUCCESS;
72}
73
Varun Wadekard22429d2016-03-18 14:35:28 -070074int tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
75{
76 return PSCI_E_SUCCESS;
77}
78
Varun Wadekar8b82fae2015-11-09 17:39:28 -080079int tegra_soc_prepare_system_reset(void)
80{
81 return PSCI_E_SUCCESS;
82}
83
Varun Wadekare5caeed2016-01-07 14:04:21 -080084__dead2 void tegra_soc_prepare_system_off(void)
85{
86 ERROR("Tegra System Off: operation not handled.\n");
87 panic();
88}
89
Varun Wadekarf2aa1be2016-06-07 12:00:06 -070090plat_local_state_t tegra_soc_get_target_pwr_state(unsigned int lvl,
91 const plat_local_state_t *states,
92 unsigned int ncpu)
93{
Varun Wadekar14eaede2016-09-01 14:51:59 -070094 plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
Varun Wadekarf2aa1be2016-06-07 12:00:06 -070095
96 assert(ncpu);
97
98 do {
99 temp = *states++;
Varun Wadekar14eaede2016-09-01 14:51:59 -0700100 if ((temp < target))
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700101 target = temp;
102 } while (--ncpu);
103
104 return target;
105}
106
Varun Wadekarb316e242015-05-19 16:48:04 +0530107/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530108 * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
109 * call to get the `power_state` parameter. This allows the platform to encode
110 * the appropriate State-ID field within the `power_state` parameter which can
111 * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
112******************************************************************************/
113void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530114{
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700115 /* all affinities use system suspend state id */
Varun Wadekar66231d12017-06-07 09:57:42 -0700116 for (uint32_t i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700117 req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
Varun Wadekarb316e242015-05-19 16:48:04 +0530118}
119
120/*******************************************************************************
121 * Handler called when an affinity instance is about to enter standby.
122 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530123void tegra_cpu_standby(plat_local_state_t cpu_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530124{
125 /*
126 * Enter standby state
127 * dsb is good practice before using wfi to enter low power states
128 */
129 dsb();
130 wfi();
131}
132
133/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530134 * Handler called when an affinity instance is about to be turned on. The
135 * level and mpidr determine the affinity instance.
136 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530137int tegra_pwr_domain_on(u_register_t mpidr)
Varun Wadekarb316e242015-05-19 16:48:04 +0530138{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530139 return tegra_soc_pwr_domain_on(mpidr);
Varun Wadekarb316e242015-05-19 16:48:04 +0530140}
141
142/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530143 * Handler called when a power domain is about to be turned off. The
144 * target_state encodes the power state that each level should transition to.
Varun Wadekarb316e242015-05-19 16:48:04 +0530145 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530146void tegra_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530147{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530148 tegra_soc_pwr_domain_off(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530149}
150
151/*******************************************************************************
Varun Wadekard22429d2016-03-18 14:35:28 -0700152 * Handler called when a power domain is about to be suspended. The
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530153 * target_state encodes the power state that each level should transition to.
Varun Wadekar99782e82017-07-05 17:44:12 -0700154 * This handler is called with SMP and data cache enabled, when
155 * HW_ASSISTED_COHERENCY = 0
156 ******************************************************************************/
157void tegra_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
158{
159 tegra_soc_pwr_domain_suspend_pwrdown_early(target_state);
160}
161
162/*******************************************************************************
163 * Handler called when a power domain is about to be suspended. The
164 * target_state encodes the power state that each level should transition to.
Varun Wadekarb316e242015-05-19 16:48:04 +0530165 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530166void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530167{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530168 tegra_soc_pwr_domain_suspend(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530169
Varun Wadekara2c6be62016-08-01 22:16:21 -0700170 /* Disable console if we are entering deep sleep. */
171 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
172 PSTATE_ID_SOC_POWERDN)
173 console_uninit();
174
Varun Wadekarb316e242015-05-19 16:48:04 +0530175 /* disable GICC */
176 tegra_gic_cpuif_deactivate();
177}
178
179/*******************************************************************************
Varun Wadekard22429d2016-03-18 14:35:28 -0700180 * Handler called at the end of the power domain suspend sequence. The
181 * target_state encodes the power state that each level should transition to.
182 ******************************************************************************/
183__dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
184 *target_state)
185{
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -0800186 uint8_t pwr_state = target_state->pwr_domain_state[PLAT_MAX_PWR_LVL];
187 uint64_t rmr_el3 = 0;
188
Varun Wadekard22429d2016-03-18 14:35:28 -0700189 /* call the chip's power down handler */
190 tegra_soc_pwr_domain_power_down_wfi(target_state);
191
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -0800192 /*
193 * If we are in fake system suspend mode, ensure we start doing
194 * procedures that help in looping back towards system suspend exit
195 * instead of calling WFI by requesting a warm reset.
196 * Else, just call WFI to enter low power state.
197 */
198 if ((tegra_fake_system_suspend != 0U) &&
199 (pwr_state == (uint8_t)PSTATE_ID_SOC_POWERDN)) {
200
201 /* warm reboot */
202 rmr_el3 = read_rmr_el3();
203 write_rmr_el3(rmr_el3 | RMR_WARM_RESET_CPU);
204
205 } else {
206 /* enter power down state */
207 wfi();
208 }
Varun Wadekard22429d2016-03-18 14:35:28 -0700209
210 /* we can never reach here */
Varun Wadekard22429d2016-03-18 14:35:28 -0700211 panic();
212}
213
214/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530215 * Handler called when a power domain has just been powered on after
216 * being turned off earlier. The target_state encodes the low power state that
217 * each level has woken up from.
Varun Wadekarb316e242015-05-19 16:48:04 +0530218 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530219void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530220{
221 plat_params_from_bl2_t *plat_params;
222
223 /*
Varun Wadekarb316e242015-05-19 16:48:04 +0530224 * Initialize the GIC cpu and distributor interfaces
225 */
Varun Wadekarb7b45752015-12-28 14:55:41 -0800226 plat_gic_setup();
Varun Wadekarb316e242015-05-19 16:48:04 +0530227
228 /*
229 * Check if we are exiting from deep sleep.
230 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530231 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
232 PSTATE_ID_SOC_POWERDN) {
Varun Wadekarb316e242015-05-19 16:48:04 +0530233
Varun Wadekara2c6be62016-08-01 22:16:21 -0700234 /* Initialize the runtime console */
Damon Duan777baa52016-11-07 19:37:50 +0800235 if (tegra_console_base != (uint64_t)0) {
236 console_init(tegra_console_base, TEGRA_BOOT_UART_CLK_IN_HZ,
237 TEGRA_CONSOLE_BAUDRATE);
238 }
Varun Wadekara2c6be62016-08-01 22:16:21 -0700239
Varun Wadekarb316e242015-05-19 16:48:04 +0530240 /*
Varun Wadekar6eec6d62016-03-03 13:28:10 -0800241 * Restore Memory Controller settings as it loses state
242 * during system suspend.
Varun Wadekarb316e242015-05-19 16:48:04 +0530243 */
Varun Wadekar6eec6d62016-03-03 13:28:10 -0800244 tegra_memctrl_restore_settings();
Varun Wadekarb316e242015-05-19 16:48:04 +0530245
246 /*
247 * Security configuration to allow DRAM/device access.
248 */
249 plat_params = bl31_get_plat_params();
Varun Wadekar6bb62462015-10-06 12:49:31 +0530250 tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
Varun Wadekarb316e242015-05-19 16:48:04 +0530251 plat_params->tzdram_size);
Varun Wadekard5f578a2016-06-01 19:34:37 -0700252
253 /*
254 * Set up the TZRAM memory aperture to allow only secure world
255 * access
256 */
257 tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
Varun Wadekarb316e242015-05-19 16:48:04 +0530258 }
259
260 /*
261 * Reset hardware settings.
262 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530263 tegra_soc_pwr_domain_on_finish(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530264}
265
266/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530267 * Handler called when a power domain has just been powered on after
268 * having been suspended earlier. The target_state encodes the low power state
269 * that each level has woken up from.
Varun Wadekarb316e242015-05-19 16:48:04 +0530270 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530271void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530272{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530273 tegra_pwr_domain_on_finish(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530274}
275
276/*******************************************************************************
277 * Handler called when the system wants to be powered off
278 ******************************************************************************/
279__dead2 void tegra_system_off(void)
280{
Varun Wadekare5caeed2016-01-07 14:04:21 -0800281 INFO("Powering down system...\n");
282
283 tegra_soc_prepare_system_off();
Varun Wadekarb316e242015-05-19 16:48:04 +0530284}
285
286/*******************************************************************************
287 * Handler called when the system wants to be restarted.
288 ******************************************************************************/
289__dead2 void tegra_system_reset(void)
290{
Varun Wadekare5caeed2016-01-07 14:04:21 -0800291 INFO("Restarting system...\n");
292
Varun Wadekar8b82fae2015-11-09 17:39:28 -0800293 /* per-SoC system reset handler */
294 tegra_soc_prepare_system_reset();
295
Varun Wadekarb316e242015-05-19 16:48:04 +0530296 /*
297 * Program the PMC in order to restart the system.
298 */
299 tegra_pmc_system_reset();
300}
301
302/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530303 * Handler called to check the validity of the power state parameter.
304 ******************************************************************************/
305int32_t tegra_validate_power_state(unsigned int power_state,
306 psci_power_state_t *req_state)
307{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530308 assert(req_state);
309
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530310 return tegra_soc_validate_power_state(power_state, req_state);
311}
312
313/*******************************************************************************
314 * Platform handler called to check the validity of the non secure entrypoint.
315 ******************************************************************************/
316int tegra_validate_ns_entrypoint(uintptr_t entrypoint)
317{
318 /*
319 * Check if the non secure entrypoint lies within the non
320 * secure DRAM.
321 */
322 if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END))
323 return PSCI_E_SUCCESS;
324
325 return PSCI_E_INVALID_ADDRESS;
326}
327
328/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530329 * Export the platform handlers to enable psci to invoke them
330 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530331static const plat_psci_ops_t tegra_plat_psci_ops = {
332 .cpu_standby = tegra_cpu_standby,
333 .pwr_domain_on = tegra_pwr_domain_on,
334 .pwr_domain_off = tegra_pwr_domain_off,
Varun Wadekar99782e82017-07-05 17:44:12 -0700335 .pwr_domain_suspend_pwrdown_early = tegra_pwr_domain_suspend_pwrdown_early,
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530336 .pwr_domain_suspend = tegra_pwr_domain_suspend,
337 .pwr_domain_on_finish = tegra_pwr_domain_on_finish,
338 .pwr_domain_suspend_finish = tegra_pwr_domain_suspend_finish,
Varun Wadekard22429d2016-03-18 14:35:28 -0700339 .pwr_domain_pwr_down_wfi = tegra_pwr_domain_power_down_wfi,
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530340 .system_off = tegra_system_off,
341 .system_reset = tegra_system_reset,
342 .validate_power_state = tegra_validate_power_state,
343 .validate_ns_entrypoint = tegra_validate_ns_entrypoint,
344 .get_sys_suspend_power_state = tegra_get_sys_suspend_power_state,
Varun Wadekarb316e242015-05-19 16:48:04 +0530345};
346
347/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530348 * Export the platform specific power ops and initialize Power Controller
Varun Wadekarb316e242015-05-19 16:48:04 +0530349 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530350int plat_setup_psci_ops(uintptr_t sec_entrypoint,
351 const plat_psci_ops_t **psci_ops)
Varun Wadekarb316e242015-05-19 16:48:04 +0530352{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530353 psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };
354
355 /*
356 * Flush entrypoint variable to PoC since it will be
357 * accessed after a reset with the caches turned off.
358 */
359 tegra_sec_entry_point = sec_entrypoint;
360 flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));
361
Varun Wadekarb316e242015-05-19 16:48:04 +0530362 /*
363 * Reset hardware settings.
364 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530365 tegra_soc_pwr_domain_on_finish(&target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530366
367 /*
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530368 * Initialize PSCI ops struct
Varun Wadekarb316e242015-05-19 16:48:04 +0530369 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530370 *psci_ops = &tegra_plat_psci_ops;
Varun Wadekarb316e242015-05-19 16:48:04 +0530371
372 return 0;
373}
Varun Wadekar24975392016-05-05 14:13:30 -0700374
375/*******************************************************************************
376 * Platform handler to calculate the proper target power level at the
377 * specified affinity level
378 ******************************************************************************/
379plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
380 const plat_local_state_t *states,
381 unsigned int ncpu)
382{
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700383 return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
Varun Wadekar24975392016-05-05 14:13:30 -0700384}