blob: ce44983b5f782da2083e03ea928fb2f7cb720bf4 [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
Varun Wadekarb316e242015-05-19 16:48:04 +05307#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <platform_def.h>
10
11#include <arch_helpers.h>
12#include <common/bl_common.h>
13#include <common/debug.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053014#include <context.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <drivers/console.h>
16#include <lib/el3_runtime/context_mgmt.h>
17#include <lib/mmio.h>
18#include <lib/psci/psci.h>
19#include <plat/common/platform.h>
20
Varun Wadekarb316e242015-05-19 16:48:04 +053021#include <memctrl.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053022#include <pmc.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053023#include <tegra_def.h>
Harvey Hsieh9e083c72017-04-10 16:20:32 +080024#include <tegra_platform.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053025#include <tegra_private.h>
26
27extern uint64_t tegra_bl31_phys_base;
Varun Wadekara78bb1b2015-08-07 10:03:00 +053028extern uint64_t tegra_sec_entry_point;
Varun Wadekara2c6be62016-08-01 22:16:21 -070029extern uint64_t tegra_console_base;
Varun Wadekarb316e242015-05-19 16:48:04 +053030
31/*
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -080032 * tegra_fake_system_suspend acts as a boolean var controlling whether
33 * we are going to take fake system suspend code or normal system suspend code
34 * path. This variable is set inside the sip call handlers,when the kernel
35 * requests a SIP call to set the suspend debug flags.
36 */
37uint8_t tegra_fake_system_suspend;
38
39/*
Varun Wadekarb316e242015-05-19 16:48:04 +053040 * The following platform setup functions are weakly defined. They
41 * provide typical implementations that will be overridden by a SoC.
42 */
Varun Wadekar99782e82017-07-05 17:44:12 -070043#pragma weak tegra_soc_pwr_domain_suspend_pwrdown_early
Varun Wadekara78bb1b2015-08-07 10:03:00 +053044#pragma weak tegra_soc_pwr_domain_suspend
45#pragma weak tegra_soc_pwr_domain_on
46#pragma weak tegra_soc_pwr_domain_off
47#pragma weak tegra_soc_pwr_domain_on_finish
Varun Wadekard22429d2016-03-18 14:35:28 -070048#pragma weak tegra_soc_pwr_domain_power_down_wfi
Varun Wadekar8b82fae2015-11-09 17:39:28 -080049#pragma weak tegra_soc_prepare_system_reset
Varun Wadekare5caeed2016-01-07 14:04:21 -080050#pragma weak tegra_soc_prepare_system_off
Varun Wadekarf2aa1be2016-06-07 12:00:06 -070051#pragma weak tegra_soc_get_target_pwr_state
Varun Wadekarb316e242015-05-19 16:48:04 +053052
Varun Wadekar99782e82017-07-05 17:44:12 -070053int tegra_soc_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
54{
55 return PSCI_E_NOT_SUPPORTED;
56}
57
Varun Wadekara78bb1b2015-08-07 10:03:00 +053058int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053059{
60 return PSCI_E_NOT_SUPPORTED;
61}
62
Varun Wadekara78bb1b2015-08-07 10:03:00 +053063int tegra_soc_pwr_domain_on(u_register_t mpidr)
Varun Wadekarb316e242015-05-19 16:48:04 +053064{
65 return PSCI_E_SUCCESS;
66}
67
Varun Wadekara78bb1b2015-08-07 10:03:00 +053068int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053069{
70 return PSCI_E_SUCCESS;
71}
72
Varun Wadekara78bb1b2015-08-07 10:03:00 +053073int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053074{
75 return PSCI_E_SUCCESS;
76}
77
Varun Wadekard22429d2016-03-18 14:35:28 -070078int tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
79{
80 return PSCI_E_SUCCESS;
81}
82
Varun Wadekar8b82fae2015-11-09 17:39:28 -080083int tegra_soc_prepare_system_reset(void)
84{
85 return PSCI_E_SUCCESS;
86}
87
Varun Wadekare5caeed2016-01-07 14:04:21 -080088__dead2 void tegra_soc_prepare_system_off(void)
89{
90 ERROR("Tegra System Off: operation not handled.\n");
91 panic();
92}
93
Varun Wadekarf2aa1be2016-06-07 12:00:06 -070094plat_local_state_t tegra_soc_get_target_pwr_state(unsigned int lvl,
95 const plat_local_state_t *states,
96 unsigned int ncpu)
97{
Varun Wadekar14eaede2016-09-01 14:51:59 -070098 plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
Varun Wadekarf2aa1be2016-06-07 12:00:06 -070099
100 assert(ncpu);
101
102 do {
103 temp = *states++;
Varun Wadekar14eaede2016-09-01 14:51:59 -0700104 if ((temp < target))
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700105 target = temp;
106 } while (--ncpu);
107
108 return target;
109}
110
Varun Wadekarb316e242015-05-19 16:48:04 +0530111/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530112 * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
113 * call to get the `power_state` parameter. This allows the platform to encode
114 * the appropriate State-ID field within the `power_state` parameter which can
115 * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
116******************************************************************************/
117void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530118{
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700119 /* all affinities use system suspend state id */
Varun Wadekar66231d12017-06-07 09:57:42 -0700120 for (uint32_t i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700121 req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
Varun Wadekarb316e242015-05-19 16:48:04 +0530122}
123
124/*******************************************************************************
125 * Handler called when an affinity instance is about to enter standby.
126 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530127void tegra_cpu_standby(plat_local_state_t cpu_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530128{
129 /*
130 * Enter standby state
131 * dsb is good practice before using wfi to enter low power states
132 */
133 dsb();
134 wfi();
135}
136
137/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530138 * Handler called when an affinity instance is about to be turned on. The
139 * level and mpidr determine the affinity instance.
140 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530141int tegra_pwr_domain_on(u_register_t mpidr)
Varun Wadekarb316e242015-05-19 16:48:04 +0530142{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530143 return tegra_soc_pwr_domain_on(mpidr);
Varun Wadekarb316e242015-05-19 16:48:04 +0530144}
145
146/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530147 * Handler called when a power domain is about to be turned off. The
148 * target_state encodes the power state that each level should transition to.
Varun Wadekarb316e242015-05-19 16:48:04 +0530149 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530150void tegra_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530151{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530152 tegra_soc_pwr_domain_off(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530153}
154
155/*******************************************************************************
Varun Wadekard22429d2016-03-18 14:35:28 -0700156 * Handler called when a power domain is about to be suspended. The
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530157 * target_state encodes the power state that each level should transition to.
Varun Wadekar99782e82017-07-05 17:44:12 -0700158 * This handler is called with SMP and data cache enabled, when
159 * HW_ASSISTED_COHERENCY = 0
160 ******************************************************************************/
161void tegra_pwr_domain_suspend_pwrdown_early(const psci_power_state_t *target_state)
162{
163 tegra_soc_pwr_domain_suspend_pwrdown_early(target_state);
164}
165
166/*******************************************************************************
167 * Handler called when a power domain is about to be suspended. The
168 * target_state encodes the power state that each level should transition to.
Varun Wadekarb316e242015-05-19 16:48:04 +0530169 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530170void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530171{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530172 tegra_soc_pwr_domain_suspend(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530173
Varun Wadekara2c6be62016-08-01 22:16:21 -0700174 /* Disable console if we are entering deep sleep. */
175 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
176 PSTATE_ID_SOC_POWERDN)
177 console_uninit();
178
Varun Wadekarb316e242015-05-19 16:48:04 +0530179 /* disable GICC */
180 tegra_gic_cpuif_deactivate();
181}
182
183/*******************************************************************************
Varun Wadekard22429d2016-03-18 14:35:28 -0700184 * Handler called at the end of the power domain suspend sequence. The
185 * target_state encodes the power state that each level should transition to.
186 ******************************************************************************/
187__dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
188 *target_state)
189{
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -0800190 uint8_t pwr_state = target_state->pwr_domain_state[PLAT_MAX_PWR_LVL];
191 uint64_t rmr_el3 = 0;
192
Varun Wadekard22429d2016-03-18 14:35:28 -0700193 /* call the chip's power down handler */
194 tegra_soc_pwr_domain_power_down_wfi(target_state);
195
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -0800196 /*
197 * If we are in fake system suspend mode, ensure we start doing
198 * procedures that help in looping back towards system suspend exit
199 * instead of calling WFI by requesting a warm reset.
200 * Else, just call WFI to enter low power state.
201 */
202 if ((tegra_fake_system_suspend != 0U) &&
203 (pwr_state == (uint8_t)PSTATE_ID_SOC_POWERDN)) {
204
205 /* warm reboot */
206 rmr_el3 = read_rmr_el3();
207 write_rmr_el3(rmr_el3 | RMR_WARM_RESET_CPU);
208
209 } else {
210 /* enter power down state */
211 wfi();
212 }
Varun Wadekard22429d2016-03-18 14:35:28 -0700213
214 /* we can never reach here */
Varun Wadekard22429d2016-03-18 14:35:28 -0700215 panic();
216}
217
218/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530219 * Handler called when a power domain has just been powered on after
220 * being turned off earlier. The target_state encodes the low power state that
221 * each level has woken up from.
Varun Wadekarb316e242015-05-19 16:48:04 +0530222 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530223void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530224{
225 plat_params_from_bl2_t *plat_params;
Harvey Hsieh9e083c72017-04-10 16:20:32 +0800226 uint32_t console_clock;
Varun Wadekarb316e242015-05-19 16:48:04 +0530227
228 /*
Varun Wadekarb316e242015-05-19 16:48:04 +0530229 * Initialize the GIC cpu and distributor interfaces
230 */
Varun Wadekarb7b45752015-12-28 14:55:41 -0800231 plat_gic_setup();
Varun Wadekarb316e242015-05-19 16:48:04 +0530232
233 /*
234 * Check if we are exiting from deep sleep.
235 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530236 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
237 PSTATE_ID_SOC_POWERDN) {
Varun Wadekarb316e242015-05-19 16:48:04 +0530238
Harvey Hsieh9e083c72017-04-10 16:20:32 +0800239 /*
240 * Reference clock used by the FPGAs is a lot slower.
241 */
242 if (tegra_platform_is_fpga() == 1U) {
243 console_clock = TEGRA_BOOT_UART_CLK_13_MHZ;
244 } else {
245 console_clock = TEGRA_BOOT_UART_CLK_408_MHZ;
246 }
247
Varun Wadekara2c6be62016-08-01 22:16:21 -0700248 /* Initialize the runtime console */
Damon Duan777baa52016-11-07 19:37:50 +0800249 if (tegra_console_base != (uint64_t)0) {
Harvey Hsieh9e083c72017-04-10 16:20:32 +0800250 console_init(tegra_console_base, console_clock,
251 TEGRA_CONSOLE_BAUDRATE);
Damon Duan777baa52016-11-07 19:37:50 +0800252 }
Varun Wadekara2c6be62016-08-01 22:16:21 -0700253
Varun Wadekarb316e242015-05-19 16:48:04 +0530254 /*
Varun Wadekar6eec6d62016-03-03 13:28:10 -0800255 * Restore Memory Controller settings as it loses state
256 * during system suspend.
Varun Wadekarb316e242015-05-19 16:48:04 +0530257 */
Varun Wadekar6eec6d62016-03-03 13:28:10 -0800258 tegra_memctrl_restore_settings();
Varun Wadekarb316e242015-05-19 16:48:04 +0530259
260 /*
261 * Security configuration to allow DRAM/device access.
262 */
263 plat_params = bl31_get_plat_params();
Varun Wadekar6bb62462015-10-06 12:49:31 +0530264 tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
Varun Wadekarb316e242015-05-19 16:48:04 +0530265 plat_params->tzdram_size);
Varun Wadekard5f578a2016-06-01 19:34:37 -0700266
267 /*
268 * Set up the TZRAM memory aperture to allow only secure world
269 * access
270 */
271 tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
Varun Wadekarb316e242015-05-19 16:48:04 +0530272 }
273
274 /*
275 * Reset hardware settings.
276 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530277 tegra_soc_pwr_domain_on_finish(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530278}
279
280/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530281 * Handler called when a power domain has just been powered on after
282 * having been suspended earlier. The target_state encodes the low power state
283 * that each level has woken up from.
Varun Wadekarb316e242015-05-19 16:48:04 +0530284 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530285void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530286{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530287 tegra_pwr_domain_on_finish(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530288}
289
290/*******************************************************************************
291 * Handler called when the system wants to be powered off
292 ******************************************************************************/
293__dead2 void tegra_system_off(void)
294{
Varun Wadekare5caeed2016-01-07 14:04:21 -0800295 INFO("Powering down system...\n");
296
297 tegra_soc_prepare_system_off();
Varun Wadekarb316e242015-05-19 16:48:04 +0530298}
299
300/*******************************************************************************
301 * Handler called when the system wants to be restarted.
302 ******************************************************************************/
303__dead2 void tegra_system_reset(void)
304{
Varun Wadekare5caeed2016-01-07 14:04:21 -0800305 INFO("Restarting system...\n");
306
Varun Wadekar8b82fae2015-11-09 17:39:28 -0800307 /* per-SoC system reset handler */
308 tegra_soc_prepare_system_reset();
309
Varun Wadekarb316e242015-05-19 16:48:04 +0530310 /*
311 * Program the PMC in order to restart the system.
312 */
313 tegra_pmc_system_reset();
314}
315
316/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530317 * Handler called to check the validity of the power state parameter.
318 ******************************************************************************/
319int32_t tegra_validate_power_state(unsigned int power_state,
320 psci_power_state_t *req_state)
321{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530322 assert(req_state);
323
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530324 return tegra_soc_validate_power_state(power_state, req_state);
325}
326
327/*******************************************************************************
328 * Platform handler called to check the validity of the non secure entrypoint.
329 ******************************************************************************/
330int tegra_validate_ns_entrypoint(uintptr_t entrypoint)
331{
332 /*
333 * Check if the non secure entrypoint lies within the non
334 * secure DRAM.
335 */
336 if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END))
337 return PSCI_E_SUCCESS;
338
339 return PSCI_E_INVALID_ADDRESS;
340}
341
342/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530343 * Export the platform handlers to enable psci to invoke them
344 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530345static const plat_psci_ops_t tegra_plat_psci_ops = {
346 .cpu_standby = tegra_cpu_standby,
347 .pwr_domain_on = tegra_pwr_domain_on,
348 .pwr_domain_off = tegra_pwr_domain_off,
Varun Wadekar99782e82017-07-05 17:44:12 -0700349 .pwr_domain_suspend_pwrdown_early = tegra_pwr_domain_suspend_pwrdown_early,
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530350 .pwr_domain_suspend = tegra_pwr_domain_suspend,
351 .pwr_domain_on_finish = tegra_pwr_domain_on_finish,
352 .pwr_domain_suspend_finish = tegra_pwr_domain_suspend_finish,
Varun Wadekard22429d2016-03-18 14:35:28 -0700353 .pwr_domain_pwr_down_wfi = tegra_pwr_domain_power_down_wfi,
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530354 .system_off = tegra_system_off,
355 .system_reset = tegra_system_reset,
356 .validate_power_state = tegra_validate_power_state,
357 .validate_ns_entrypoint = tegra_validate_ns_entrypoint,
358 .get_sys_suspend_power_state = tegra_get_sys_suspend_power_state,
Varun Wadekarb316e242015-05-19 16:48:04 +0530359};
360
361/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530362 * Export the platform specific power ops and initialize Power Controller
Varun Wadekarb316e242015-05-19 16:48:04 +0530363 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530364int plat_setup_psci_ops(uintptr_t sec_entrypoint,
365 const plat_psci_ops_t **psci_ops)
Varun Wadekarb316e242015-05-19 16:48:04 +0530366{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530367 psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };
368
369 /*
370 * Flush entrypoint variable to PoC since it will be
371 * accessed after a reset with the caches turned off.
372 */
373 tegra_sec_entry_point = sec_entrypoint;
374 flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));
375
Varun Wadekarb316e242015-05-19 16:48:04 +0530376 /*
377 * Reset hardware settings.
378 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530379 tegra_soc_pwr_domain_on_finish(&target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530380
381 /*
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530382 * Initialize PSCI ops struct
Varun Wadekarb316e242015-05-19 16:48:04 +0530383 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530384 *psci_ops = &tegra_plat_psci_ops;
Varun Wadekarb316e242015-05-19 16:48:04 +0530385
386 return 0;
387}
Varun Wadekar24975392016-05-05 14:13:30 -0700388
389/*******************************************************************************
390 * Platform handler to calculate the proper target power level at the
391 * specified affinity level
392 ******************************************************************************/
393plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
394 const plat_local_state_t *states,
395 unsigned int ncpu)
396{
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700397 return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
Varun Wadekar24975392016-05-05 14:13:30 -0700398}