blob: 5376d5231fa2e6f1374ba6a68ba41798a3775f44 [file] [log] [blame]
Varun Wadekarb316e242015-05-19 16:48:04 +05301/*
Varun Wadekar6077dce2016-01-27 11:31:06 -08002 * Copyright (c) 2015-2016, 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/*
52 * The following platform setup functions are weakly defined. They
53 * provide typical implementations that will be overridden by a SoC.
54 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +053055#pragma weak tegra_soc_pwr_domain_suspend
56#pragma weak tegra_soc_pwr_domain_on
57#pragma weak tegra_soc_pwr_domain_off
58#pragma weak tegra_soc_pwr_domain_on_finish
Varun Wadekard22429d2016-03-18 14:35:28 -070059#pragma weak tegra_soc_pwr_domain_power_down_wfi
Varun Wadekar8b82fae2015-11-09 17:39:28 -080060#pragma weak tegra_soc_prepare_system_reset
Varun Wadekare5caeed2016-01-07 14:04:21 -080061#pragma weak tegra_soc_prepare_system_off
Varun Wadekarf2aa1be2016-06-07 12:00:06 -070062#pragma weak tegra_soc_get_target_pwr_state
Varun Wadekarb316e242015-05-19 16:48:04 +053063
Varun Wadekara78bb1b2015-08-07 10:03:00 +053064int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053065{
66 return PSCI_E_NOT_SUPPORTED;
67}
68
Varun Wadekara78bb1b2015-08-07 10:03:00 +053069int tegra_soc_pwr_domain_on(u_register_t mpidr)
Varun Wadekarb316e242015-05-19 16:48:04 +053070{
71 return PSCI_E_SUCCESS;
72}
73
Varun Wadekara78bb1b2015-08-07 10:03:00 +053074int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053075{
76 return PSCI_E_SUCCESS;
77}
78
Varun Wadekara78bb1b2015-08-07 10:03:00 +053079int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +053080{
81 return PSCI_E_SUCCESS;
82}
83
Varun Wadekard22429d2016-03-18 14:35:28 -070084int tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
85{
86 return PSCI_E_SUCCESS;
87}
88
Varun Wadekar8b82fae2015-11-09 17:39:28 -080089int tegra_soc_prepare_system_reset(void)
90{
91 return PSCI_E_SUCCESS;
92}
93
Varun Wadekare5caeed2016-01-07 14:04:21 -080094__dead2 void tegra_soc_prepare_system_off(void)
95{
96 ERROR("Tegra System Off: operation not handled.\n");
97 panic();
98}
99
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700100plat_local_state_t tegra_soc_get_target_pwr_state(unsigned int lvl,
101 const plat_local_state_t *states,
102 unsigned int ncpu)
103{
Varun Wadekar14eaede2016-09-01 14:51:59 -0700104 plat_local_state_t target = PLAT_MAX_OFF_STATE, temp;
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700105
106 assert(ncpu);
107
108 do {
109 temp = *states++;
Varun Wadekar14eaede2016-09-01 14:51:59 -0700110 if ((temp < target))
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700111 target = temp;
112 } while (--ncpu);
113
114 return target;
115}
116
Varun Wadekarb316e242015-05-19 16:48:04 +0530117/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530118 * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
119 * call to get the `power_state` parameter. This allows the platform to encode
120 * the appropriate State-ID field within the `power_state` parameter which can
121 * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
122******************************************************************************/
123void tegra_get_sys_suspend_power_state(psci_power_state_t *req_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530124{
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700125 /* all affinities use system suspend state id */
126 for (int i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
127 req_state->pwr_domain_state[i] = PSTATE_ID_SOC_POWERDN;
Varun Wadekarb316e242015-05-19 16:48:04 +0530128}
129
130/*******************************************************************************
131 * Handler called when an affinity instance is about to enter standby.
132 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530133void tegra_cpu_standby(plat_local_state_t cpu_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530134{
135 /*
136 * Enter standby state
137 * dsb is good practice before using wfi to enter low power states
138 */
139 dsb();
140 wfi();
141}
142
143/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530144 * Handler called when an affinity instance is about to be turned on. The
145 * level and mpidr determine the affinity instance.
146 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530147int tegra_pwr_domain_on(u_register_t mpidr)
Varun Wadekarb316e242015-05-19 16:48:04 +0530148{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530149 return tegra_soc_pwr_domain_on(mpidr);
Varun Wadekarb316e242015-05-19 16:48:04 +0530150}
151
152/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530153 * Handler called when a power domain is about to be turned off. The
154 * target_state encodes the power state that each level should transition to.
Varun Wadekarb316e242015-05-19 16:48:04 +0530155 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530156void tegra_pwr_domain_off(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530157{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530158 tegra_soc_pwr_domain_off(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530159}
160
161/*******************************************************************************
Varun Wadekard22429d2016-03-18 14:35:28 -0700162 * Handler called when a power domain is about to be suspended. The
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530163 * target_state encodes the power state that each level should transition to.
Varun Wadekarb316e242015-05-19 16:48:04 +0530164 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530165void tegra_pwr_domain_suspend(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530166{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530167 tegra_soc_pwr_domain_suspend(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530168
Varun Wadekara2c6be62016-08-01 22:16:21 -0700169 /* Disable console if we are entering deep sleep. */
170 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
171 PSTATE_ID_SOC_POWERDN)
172 console_uninit();
173
Varun Wadekarb316e242015-05-19 16:48:04 +0530174 /* disable GICC */
175 tegra_gic_cpuif_deactivate();
176}
177
178/*******************************************************************************
Varun Wadekard22429d2016-03-18 14:35:28 -0700179 * Handler called at the end of the power domain suspend sequence. The
180 * target_state encodes the power state that each level should transition to.
181 ******************************************************************************/
182__dead2 void tegra_pwr_domain_power_down_wfi(const psci_power_state_t
183 *target_state)
184{
185 /* call the chip's power down handler */
186 tegra_soc_pwr_domain_power_down_wfi(target_state);
187
188 /* enter power down state */
189 wfi();
190
191 /* we can never reach here */
192 ERROR("%s: operation not handled.\n", __func__);
193 panic();
194}
195
196/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530197 * Handler called when a power domain has just been powered on after
198 * being turned off earlier. The target_state encodes the low power state that
199 * each level has woken up from.
Varun Wadekarb316e242015-05-19 16:48:04 +0530200 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530201void tegra_pwr_domain_on_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530202{
203 plat_params_from_bl2_t *plat_params;
204
205 /*
Varun Wadekarb316e242015-05-19 16:48:04 +0530206 * Initialize the GIC cpu and distributor interfaces
207 */
Varun Wadekarb7b45752015-12-28 14:55:41 -0800208 plat_gic_setup();
Varun Wadekarb316e242015-05-19 16:48:04 +0530209
210 /*
211 * Check if we are exiting from deep sleep.
212 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530213 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
214 PSTATE_ID_SOC_POWERDN) {
Varun Wadekarb316e242015-05-19 16:48:04 +0530215
Varun Wadekara2c6be62016-08-01 22:16:21 -0700216 /* Initialize the runtime console */
Damon Duan777baa52016-11-07 19:37:50 +0800217 if (tegra_console_base != (uint64_t)0) {
218 console_init(tegra_console_base, TEGRA_BOOT_UART_CLK_IN_HZ,
219 TEGRA_CONSOLE_BAUDRATE);
220 }
Varun Wadekara2c6be62016-08-01 22:16:21 -0700221
Varun Wadekarb316e242015-05-19 16:48:04 +0530222 /*
Varun Wadekar6eec6d62016-03-03 13:28:10 -0800223 * Restore Memory Controller settings as it loses state
224 * during system suspend.
Varun Wadekarb316e242015-05-19 16:48:04 +0530225 */
Varun Wadekar6eec6d62016-03-03 13:28:10 -0800226 tegra_memctrl_restore_settings();
Varun Wadekarb316e242015-05-19 16:48:04 +0530227
228 /*
229 * Security configuration to allow DRAM/device access.
230 */
231 plat_params = bl31_get_plat_params();
Varun Wadekar6bb62462015-10-06 12:49:31 +0530232 tegra_memctrl_tzdram_setup(plat_params->tzdram_base,
Varun Wadekarb316e242015-05-19 16:48:04 +0530233 plat_params->tzdram_size);
Varun Wadekard5f578a2016-06-01 19:34:37 -0700234
235 /*
236 * Set up the TZRAM memory aperture to allow only secure world
237 * access
238 */
239 tegra_memctrl_tzram_setup(TEGRA_TZRAM_BASE, TEGRA_TZRAM_SIZE);
Varun Wadekarb316e242015-05-19 16:48:04 +0530240 }
241
242 /*
243 * Reset hardware settings.
244 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530245 tegra_soc_pwr_domain_on_finish(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530246}
247
248/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530249 * Handler called when a power domain has just been powered on after
250 * having been suspended earlier. The target_state encodes the low power state
251 * that each level has woken up from.
Varun Wadekarb316e242015-05-19 16:48:04 +0530252 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530253void tegra_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
Varun Wadekarb316e242015-05-19 16:48:04 +0530254{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530255 tegra_pwr_domain_on_finish(target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530256}
257
258/*******************************************************************************
259 * Handler called when the system wants to be powered off
260 ******************************************************************************/
261__dead2 void tegra_system_off(void)
262{
Varun Wadekare5caeed2016-01-07 14:04:21 -0800263 INFO("Powering down system...\n");
264
265 tegra_soc_prepare_system_off();
Varun Wadekarb316e242015-05-19 16:48:04 +0530266}
267
268/*******************************************************************************
269 * Handler called when the system wants to be restarted.
270 ******************************************************************************/
271__dead2 void tegra_system_reset(void)
272{
Varun Wadekare5caeed2016-01-07 14:04:21 -0800273 INFO("Restarting system...\n");
274
Varun Wadekar8b82fae2015-11-09 17:39:28 -0800275 /* per-SoC system reset handler */
276 tegra_soc_prepare_system_reset();
277
Varun Wadekarb316e242015-05-19 16:48:04 +0530278 /*
279 * Program the PMC in order to restart the system.
280 */
281 tegra_pmc_system_reset();
282}
283
284/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530285 * Handler called to check the validity of the power state parameter.
286 ******************************************************************************/
287int32_t tegra_validate_power_state(unsigned int power_state,
288 psci_power_state_t *req_state)
289{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530290 assert(req_state);
291
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530292 return tegra_soc_validate_power_state(power_state, req_state);
293}
294
295/*******************************************************************************
296 * Platform handler called to check the validity of the non secure entrypoint.
297 ******************************************************************************/
298int tegra_validate_ns_entrypoint(uintptr_t entrypoint)
299{
300 /*
301 * Check if the non secure entrypoint lies within the non
302 * secure DRAM.
303 */
304 if ((entrypoint >= TEGRA_DRAM_BASE) && (entrypoint <= TEGRA_DRAM_END))
305 return PSCI_E_SUCCESS;
306
307 return PSCI_E_INVALID_ADDRESS;
308}
309
310/*******************************************************************************
Varun Wadekarb316e242015-05-19 16:48:04 +0530311 * Export the platform handlers to enable psci to invoke them
312 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530313static const plat_psci_ops_t tegra_plat_psci_ops = {
314 .cpu_standby = tegra_cpu_standby,
315 .pwr_domain_on = tegra_pwr_domain_on,
316 .pwr_domain_off = tegra_pwr_domain_off,
317 .pwr_domain_suspend = tegra_pwr_domain_suspend,
318 .pwr_domain_on_finish = tegra_pwr_domain_on_finish,
319 .pwr_domain_suspend_finish = tegra_pwr_domain_suspend_finish,
Varun Wadekard22429d2016-03-18 14:35:28 -0700320 .pwr_domain_pwr_down_wfi = tegra_pwr_domain_power_down_wfi,
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530321 .system_off = tegra_system_off,
322 .system_reset = tegra_system_reset,
323 .validate_power_state = tegra_validate_power_state,
324 .validate_ns_entrypoint = tegra_validate_ns_entrypoint,
325 .get_sys_suspend_power_state = tegra_get_sys_suspend_power_state,
Varun Wadekarb316e242015-05-19 16:48:04 +0530326};
327
328/*******************************************************************************
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530329 * Export the platform specific power ops and initialize Power Controller
Varun Wadekarb316e242015-05-19 16:48:04 +0530330 ******************************************************************************/
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530331int plat_setup_psci_ops(uintptr_t sec_entrypoint,
332 const plat_psci_ops_t **psci_ops)
Varun Wadekarb316e242015-05-19 16:48:04 +0530333{
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530334 psci_power_state_t target_state = { { PSCI_LOCAL_STATE_RUN } };
335
336 /*
337 * Flush entrypoint variable to PoC since it will be
338 * accessed after a reset with the caches turned off.
339 */
340 tegra_sec_entry_point = sec_entrypoint;
341 flush_dcache_range((uint64_t)&tegra_sec_entry_point, sizeof(uint64_t));
342
Varun Wadekarb316e242015-05-19 16:48:04 +0530343 /*
344 * Reset hardware settings.
345 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530346 tegra_soc_pwr_domain_on_finish(&target_state);
Varun Wadekarb316e242015-05-19 16:48:04 +0530347
348 /*
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530349 * Initialize PSCI ops struct
Varun Wadekarb316e242015-05-19 16:48:04 +0530350 */
Varun Wadekara78bb1b2015-08-07 10:03:00 +0530351 *psci_ops = &tegra_plat_psci_ops;
Varun Wadekarb316e242015-05-19 16:48:04 +0530352
353 return 0;
354}
Varun Wadekar24975392016-05-05 14:13:30 -0700355
356/*******************************************************************************
357 * Platform handler to calculate the proper target power level at the
358 * specified affinity level
359 ******************************************************************************/
360plat_local_state_t plat_get_target_pwr_state(unsigned int lvl,
361 const plat_local_state_t *states,
362 unsigned int ncpu)
363{
Varun Wadekarf2aa1be2016-06-07 12:00:06 -0700364 return tegra_soc_get_target_pwr_state(lvl, states, ncpu);
Varun Wadekar24975392016-05-05 14:13:30 -0700365}