blob: d6329268954dab0cdef29c9eb42f72d6e93db5a6 [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 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <arch_helpers.h>
32#include <assert.h>
33#include <bl_common.h>
34#include <context.h>
35#include <context_mgmt.h>
Varun Wadekara2c6be62016-08-01 22:16:21 -070036#include <console.h>
Varun Wadekarb316e242015-05-19 16:48:04 +053037#include <debug.h>
38#include <memctrl.h>
39#include <mmio.h>
40#include <platform.h>
41#include <platform_def.h>
42#include <pmc.h>
43#include <psci.h>
44#include <tegra_def.h>
45#include <tegra_private.h>
46
47extern uint64_t tegra_bl31_phys_base;
Varun Wadekara78bb1b2015-08-07 10:03:00 +053048extern uint64_t tegra_sec_entry_point;
Varun Wadekara2c6be62016-08-01 22:16:21 -070049extern uint64_t tegra_console_base;
Varun Wadekarb316e242015-05-19 16:48:04 +053050
51/*
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -080052 * tegra_fake_system_suspend acts as a boolean var controlling whether
53 * we are going to take fake system suspend code or normal system suspend code
54 * path. This variable is set inside the sip call handlers,when the kernel
55 * requests a SIP call to set the suspend debug flags.
56 */
57uint8_t tegra_fake_system_suspend;
58
59/*
Varun Wadekarb316e242015-05-19 16:48:04 +053060 * The following platform setup functions are weakly defined. They
61 * provide typical implementations that will be overridden by a SoC.
62 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +053063#pragma weak tegra_soc_pwr_domain_suspend
64#pragma weak tegra_soc_pwr_domain_on
65#pragma weak tegra_soc_pwr_domain_off
66#pragma weak tegra_soc_pwr_domain_on_finish
Varun Wadekard22429d2016-03-18 14:35:28 -070067#pragma weak tegra_soc_pwr_domain_power_down_wfi
Varun Wadekar8b82fae2015-11-09 17:39:28 -080068#pragma weak tegra_soc_prepare_system_reset
Varun Wadekare5caeed2016-01-07 14:04:21 -080069#pragma weak tegra_soc_prepare_system_off
Varun Wadekarf2aa1be2016-06-07 12:00:06 -070070#pragma weak tegra_soc_get_target_pwr_state
Varun Wadekarb316e242015-05-19 16:48:04 +053071
Varun Wadekara78bb1b2015-08-07 10:03:00 +053072int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053073{
74 return PSCI_E_NOT_SUPPORTED;
75}
76
Varun Wadekara78bb1b2015-08-07 10:03:00 +053077int tegra_soc_pwr_domain_on(u_register_t mpidr)
Varun Wadekarb316e242015-05-19 16:48:04 +053078{
79 return PSCI_E_SUCCESS;
80}
81
Varun Wadekara78bb1b2015-08-07 10:03:00 +053082int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053083{
84 return PSCI_E_SUCCESS;
85}
86
Varun Wadekara78bb1b2015-08-07 10:03:00 +053087int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053088{
89 return PSCI_E_SUCCESS;
90}
91
Varun Wadekard22429d2016-03-18 14:35:28 -070092int tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
93{
94 return PSCI_E_SUCCESS;
95}
96
Varun Wadekar8b82fae2015-11-09 17:39:28 -080097int tegra_soc_prepare_system_reset(void)
98{
99 return PSCI_E_SUCCESS;
100}
101
Varun Wadekare5caeed2016-01-07 14:04:21 -0800102__dead2 void tegra_soc_prepare_system_off(void)
103{
104 ERROR("Tegra System Off: operation not handled.\n");
105 panic();
106}
107
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700108plat_local_state_t tegra_soc_get_target_pwr_state(unsigned int lvl,
109 const plat_local_state_t *states,
110 unsigned int ncpu)
111{
Varun Wadekar14eaede2016-09-01 14:51:59 -0700112 plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700113
114 assert(ncpu);
115
116 do {
117 temp = *states++;
Varun Wadekar14eaede2016-09-01 14:51:59 -0700118 if ((temp < target))
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700119 target = temp;
120 } while (--ncpu);
121
122 return target;
123}
124
Varun Wadekarb316e242015-05-19 16:48:04 +0530125/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530126 * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
127 * call to get the `power_state` parameter. This allows the platform to encode
128 * the appropriate State-ID field within the `power_state` parameter which can
129 * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
130******************************************************************************/
131void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530132{
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700133 /* all affinities use system suspend state id */
134 for (int i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
135 req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
Varun Wadekarb316e242015-05-19 16:48:04 +0530136}
137
138/*******************************************************************************
139 * Handler called when an affinity instance is about to enter standby.
140 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530141void tegra_cpu_standby(plat_local_state_t cpu_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530142{
143 /*
144 * Enter standby state
145 * dsb is good practice before using wfi to enter low power states
146 */
147 dsb();
148 wfi();
149}
150
151/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530152 * Handler called when an affinity instance is about to be turned on. The
153 * level and mpidr determine the affinity instance.
154 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530155int tegra_pwr_domain_on(u_register_t mpidr)
Varun Wadekarb316e242015-05-19 16:48:04 +0530156{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530157 return tegra_soc_pwr_domain_on(mpidr);
Varun Wadekarb316e242015-05-19 16:48:04 +0530158}
159
160/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530161 * Handler called when a power domain is about to be turned off. The
162 * target_state encodes the power state that each level should transition to.
Varun Wadekarb316e242015-05-19 16:48:04 +0530163 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530164void tegra_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530165{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530166 tegra_soc_pwr_domain_off(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530167}
168
169/*******************************************************************************
Varun Wadekard22429d2016-03-18 14:35:28 -0700170 * Handler called when a power domain is about to be suspended. The
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530171 * target_state encodes the power state that each level should transition to.
Varun Wadekarb316e242015-05-19 16:48:04 +0530172 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530173void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530174{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530175 tegra_soc_pwr_domain_suspend(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530176
Varun Wadekara2c6be62016-08-01 22:16:21 -0700177 /* Disable console if we are entering deep sleep. */
178 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
179 PSTATE_ID_SOC_POWERDN)
180 console_uninit();
181
Varun Wadekarb316e242015-05-19 16:48:04 +0530182 /* disable GICC */
183 tegra_gic_cpuif_deactivate();
184}
185
186/*******************************************************************************
Varun Wadekard22429d2016-03-18 14:35:28 -0700187 * Handler called at the end of the power domain suspend sequence. The
188 * target_state encodes the power state that each level should transition to.
189 ******************************************************************************/
190__dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
191 *target_state)
192{
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -0800193 uint8_t pwr_state = target_state->pwr_domain_state[PLAT_MAX_PWR_LVL];
194 uint64_t rmr_el3 = 0;
195
Varun Wadekard22429d2016-03-18 14:35:28 -0700196 /* call the chip's power down handler */
197 tegra_soc_pwr_domain_power_down_wfi(target_state);
198
Vignesh Radhakrishnanb4a72942017-03-03 10:58:05 -0800199 /*
200 * If we are in fake system suspend mode, ensure we start doing
201 * procedures that help in looping back towards system suspend exit
202 * instead of calling WFI by requesting a warm reset.
203 * Else, just call WFI to enter low power state.
204 */
205 if ((tegra_fake_system_suspend != 0U) &&
206 (pwr_state == (uint8_t)PSTATE_ID_SOC_POWERDN)) {
207
208 /* warm reboot */
209 rmr_el3 = read_rmr_el3();
210 write_rmr_el3(rmr_el3 | RMR_WARM_RESET_CPU);
211
212 } else {
213 /* enter power down state */
214 wfi();
215 }
Varun Wadekard22429d2016-03-18 14:35:28 -0700216
217 /* we can never reach here */
Varun Wadekard22429d2016-03-18 14:35:28 -0700218 panic();
219}
220
221/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530222 * Handler called when a power domain has just been powered on after
223 * being turned off earlier. The target_state encodes the low power state that
224 * each level has woken up from.
Varun Wadekarb316e242015-05-19 16:48:04 +0530225 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530226void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530227{
228 plat_params_from_bl2_t *plat_params;
229
230 /*
Varun Wadekarb316e242015-05-19 16:48:04 +0530231 * Initialize the GIC cpu and distributor interfaces
232 */
Varun Wadekarb7b45752015-12-28 14:55:41 -0800233 plat_gic_setup();
Varun Wadekarb316e242015-05-19 16:48:04 +0530234
235 /*
236 * Check if we are exiting from deep sleep.
237 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530238 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
239 PSTATE_ID_SOC_POWERDN) {
Varun Wadekarb316e242015-05-19 16:48:04 +0530240
Varun Wadekara2c6be62016-08-01 22:16:21 -0700241 /* Initialize the runtime console */
Damon Duan777baa52016-11-07 19:37:50 +0800242 if (tegra_console_base != (uint64_t)0) {
243 console_init(tegra_console_base, TEGRA_BOOT_UART_CLK_IN_HZ,
244 TEGRA_CONSOLE_BAUDRATE);
245 }
Varun Wadekara2c6be62016-08-01 22:16:21 -0700246
Varun Wadekarb316e242015-05-19 16:48:04 +0530247 /*
Varun Wadekar6eec6d62016-03-03 13:28:10 -0800248 * Restore Memory Controller settings as it loses state
249 * during system suspend.
Varun Wadekarb316e242015-05-19 16:48:04 +0530250 */
Varun Wadekar6eec6d62016-03-03 13:28:10 -0800251 tegra_memctrl_restore_settings();
Varun Wadekarb316e242015-05-19 16:48:04 +0530252
253 /*
254 * Security configuration to allow DRAM/device access.
255 */
256 plat_params = bl31_get_plat_params();
Varun Wadekar6bb62462015-10-06 12:49:31 +0530257 tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
Varun Wadekarb316e242015-05-19 16:48:04 +0530258 plat_params->tzdram_size);
Varun Wadekard5f578a2016-06-01 19:34:37 -0700259
260 /*
261 * Set up the TZRAM memory aperture to allow only secure world
262 * access
263 */
264 tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
Varun Wadekarb316e242015-05-19 16:48:04 +0530265 }
266
267 /*
268 * Reset hardware settings.
269 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530270 tegra_soc_pwr_domain_on_finish(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530271}
272
273/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530274 * Handler called when a power domain has just been powered on after
275 * having been suspended earlier. The target_state encodes the low power state
276 * that each level has woken up from.
Varun Wadekarb316e242015-05-19 16:48:04 +0530277 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530278void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530279{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530280 tegra_pwr_domain_on_finish(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530281}
282
283/*******************************************************************************
284 * Handler called when the system wants to be powered off
285 ******************************************************************************/
286__dead2 void tegra_system_off(void)
287{
Varun Wadekare5caeed2016-01-07 14:04:21 -0800288 INFO("Powering down system...\n");
289
290 tegra_soc_prepare_system_off();
Varun Wadekarb316e242015-05-19 16:48:04 +0530291}
292
293/*******************************************************************************
294 * Handler called when the system wants to be restarted.
295 ******************************************************************************/
296__dead2 void tegra_system_reset(void)
297{
Varun Wadekare5caeed2016-01-07 14:04:21 -0800298 INFO("Restarting system...\n");
299
Varun Wadekar8b82fae2015-11-09 17:39:28 -0800300 /* per-SoC system reset handler */
301 tegra_soc_prepare_system_reset();
302
Varun Wadekarb316e242015-05-19 16:48:04 +0530303 /*
304 * Program the PMC in order to restart the system.
305 */
306 tegra_pmc_system_reset();
307}
308
309/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530310 * Handler called to check the validity of the power state parameter.
311 ******************************************************************************/
312int32_t tegra_validate_power_state(unsigned int power_state,
313 psci_power_state_t *req_state)
314{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530315 assert(req_state);
316
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530317 return tegra_soc_validate_power_state(power_state, req_state);
318}
319
320/*******************************************************************************
321 * Platform handler called to check the validity of the non secure entrypoint.
322 ******************************************************************************/
323int tegra_validate_ns_entrypoint(uintptr_t entrypoint)
324{
325 /*
326 * Check if the non secure entrypoint lies within the non
327 * secure DRAM.
328 */
329 if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END))
330 return PSCI_E_SUCCESS;
331
332 return PSCI_E_INVALID_ADDRESS;
333}
334
335/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530336 * Export the platform handlers to enable psci to invoke them
337 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530338static const plat_psci_ops_t tegra_plat_psci_ops = {
339 .cpu_standby = tegra_cpu_standby,
340 .pwr_domain_on = tegra_pwr_domain_on,
341 .pwr_domain_off = tegra_pwr_domain_off,
342 .pwr_domain_suspend = tegra_pwr_domain_suspend,
343 .pwr_domain_on_finish = tegra_pwr_domain_on_finish,
344 .pwr_domain_suspend_finish = tegra_pwr_domain_suspend_finish,
Varun Wadekard22429d2016-03-18 14:35:28 -0700345 .pwr_domain_pwr_down_wfi = tegra_pwr_domain_power_down_wfi,
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530346 .system_off = tegra_system_off,
347 .system_reset = tegra_system_reset,
348 .validate_power_state = tegra_validate_power_state,
349 .validate_ns_entrypoint = tegra_validate_ns_entrypoint,
350 .get_sys_suspend_power_state = tegra_get_sys_suspend_power_state,
Varun Wadekarb316e242015-05-19 16:48:04 +0530351};
352
353/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530354 * Export the platform specific power ops and initialize Power Controller
Varun Wadekarb316e242015-05-19 16:48:04 +0530355 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530356int plat_setup_psci_ops(uintptr_t sec_entrypoint,
357 const plat_psci_ops_t **psci_ops)
Varun Wadekarb316e242015-05-19 16:48:04 +0530358{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530359 psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };
360
361 /*
362 * Flush entrypoint variable to PoC since it will be
363 * accessed after a reset with the caches turned off.
364 */
365 tegra_sec_entry_point = sec_entrypoint;
366 flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));
367
Varun Wadekarb316e242015-05-19 16:48:04 +0530368 /*
369 * Reset hardware settings.
370 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530371 tegra_soc_pwr_domain_on_finish(&target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530372
373 /*
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530374 * Initialize PSCI ops struct
Varun Wadekarb316e242015-05-19 16:48:04 +0530375 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530376 *psci_ops = &tegra_plat_psci_ops;
Varun Wadekarb316e242015-05-19 16:48:04 +0530377
378 return 0;
379}
Varun Wadekar24975392016-05-05 14:13:30 -0700380
381/*******************************************************************************
382 * Platform handler to calculate the proper target power level at the
383 * specified affinity level
384 ******************************************************************************/
385plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
386 const plat_local_state_t *states,
387 unsigned int ncpu)
388{
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700389 return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
Varun Wadekar24975392016-05-05 14:13:30 -0700390}