16fba6e04STony Xie /* 26fba6e04STony Xie * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved. 36fba6e04STony Xie * 46fba6e04STony Xie * Redistribution and use in source and binary forms, with or without 56fba6e04STony Xie * modification, are permitted provided that the following conditions are met: 66fba6e04STony Xie * 76fba6e04STony Xie * Redistributions of source code must retain the above copyright notice, this 86fba6e04STony Xie * list of conditions and the following disclaimer. 96fba6e04STony Xie * 106fba6e04STony Xie * Redistributions in binary form must reproduce the above copyright notice, 116fba6e04STony Xie * this list of conditions and the following disclaimer in the documentation 126fba6e04STony Xie * and/or other materials provided with the distribution. 136fba6e04STony Xie * 146fba6e04STony Xie * Neither the name of ARM nor the names of its contributors may be used 156fba6e04STony Xie * to endorse or promote products derived from this software without specific 166fba6e04STony Xie * prior written permission. 176fba6e04STony Xie * 186fba6e04STony Xie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 196fba6e04STony Xie * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 206fba6e04STony Xie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 216fba6e04STony Xie * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 226fba6e04STony Xie * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 236fba6e04STony Xie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 246fba6e04STony Xie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 256fba6e04STony Xie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 266fba6e04STony Xie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 276fba6e04STony Xie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 286fba6e04STony Xie * POSSIBILITY OF SUCH DAMAGE. 296fba6e04STony Xie */ 306fba6e04STony Xie 316fba6e04STony Xie #include <arch_helpers.h> 326fba6e04STony Xie #include <assert.h> 336fba6e04STony Xie #include <console.h> 346fba6e04STony Xie #include <errno.h> 356fba6e04STony Xie #include <debug.h> 366fba6e04STony Xie #include <psci.h> 376fba6e04STony Xie #include <delay_timer.h> 386fba6e04STony Xie #include <platform_def.h> 396fba6e04STony Xie #include <plat_private.h> 406fba6e04STony Xie 416fba6e04STony Xie /* Macros to read the rk power domain state */ 426fba6e04STony Xie #define RK_CORE_PWR_STATE(state) \ 436fba6e04STony Xie ((state)->pwr_domain_state[MPIDR_AFFLVL0]) 446fba6e04STony Xie #define RK_CLUSTER_PWR_STATE(state) \ 456fba6e04STony Xie ((state)->pwr_domain_state[MPIDR_AFFLVL1]) 466fba6e04STony Xie #define RK_SYSTEM_PWR_STATE(state) \ 476fba6e04STony Xie ((state)->pwr_domain_state[PLAT_MAX_PWR_LVL]) 486fba6e04STony Xie 496fba6e04STony Xie static uintptr_t rockchip_sec_entrypoint; 506fba6e04STony Xie 516fba6e04STony Xie static struct rockchip_pm_ops_cb *rockchip_ops; 526fba6e04STony Xie 536fba6e04STony Xie static void plat_rockchip_sys_pwr_domain_resume(void) 546fba6e04STony Xie { 556fba6e04STony Xie if (rockchip_ops && rockchip_ops->sys_pwr_dm_resume) 566fba6e04STony Xie rockchip_ops->sys_pwr_dm_resume(); 576fba6e04STony Xie } 586fba6e04STony Xie 596fba6e04STony Xie static void plat_rockchip_cores_pwr_domain_resume(void) 606fba6e04STony Xie { 616fba6e04STony Xie if (rockchip_ops && rockchip_ops->cores_pwr_dm_resume) 626fba6e04STony Xie rockchip_ops->cores_pwr_dm_resume(); 636fba6e04STony Xie 646fba6e04STony Xie /* Program the gic per-cpu distributor or re-distributor interface */ 656fba6e04STony Xie plat_rockchip_gic_cpuif_enable(); 666fba6e04STony Xie } 676fba6e04STony Xie 686fba6e04STony Xie /******************************************************************************* 696fba6e04STony Xie * Rockchip standard platform handler called to check the validity of the power 706fba6e04STony Xie * state parameter. 716fba6e04STony Xie ******************************************************************************/ 726fba6e04STony Xie int rockchip_validate_power_state(unsigned int power_state, 736fba6e04STony Xie psci_power_state_t *req_state) 746fba6e04STony Xie { 756fba6e04STony Xie int pstate = psci_get_pstate_type(power_state); 766fba6e04STony Xie int pwr_lvl = psci_get_pstate_pwrlvl(power_state); 776fba6e04STony Xie int i; 786fba6e04STony Xie 796fba6e04STony Xie assert(req_state); 806fba6e04STony Xie 816fba6e04STony Xie if (pwr_lvl > PLAT_MAX_PWR_LVL) 826fba6e04STony Xie return PSCI_E_INVALID_PARAMS; 836fba6e04STony Xie 846fba6e04STony Xie /* Sanity check the requested state */ 856fba6e04STony Xie if (pstate == PSTATE_TYPE_STANDBY) { 866fba6e04STony Xie /* 876fba6e04STony Xie * It's probably to enter standby only on power level 0 886fba6e04STony Xie * ignore any other power level. 896fba6e04STony Xie */ 906fba6e04STony Xie if (pwr_lvl != MPIDR_AFFLVL0) 916fba6e04STony Xie return PSCI_E_INVALID_PARAMS; 926fba6e04STony Xie 936fba6e04STony Xie req_state->pwr_domain_state[MPIDR_AFFLVL0] = 946fba6e04STony Xie PLAT_MAX_RET_STATE; 956fba6e04STony Xie } else { 966fba6e04STony Xie for (i = MPIDR_AFFLVL0; i <= pwr_lvl; i++) 976fba6e04STony Xie req_state->pwr_domain_state[i] = 986fba6e04STony Xie PLAT_MAX_OFF_STATE; 996fba6e04STony Xie } 1006fba6e04STony Xie 1016fba6e04STony Xie /* We expect the 'state id' to be zero */ 1026fba6e04STony Xie if (psci_get_pstate_id(power_state)) 1036fba6e04STony Xie return PSCI_E_INVALID_PARAMS; 1046fba6e04STony Xie 1056fba6e04STony Xie return PSCI_E_SUCCESS; 1066fba6e04STony Xie } 1076fba6e04STony Xie 1086fba6e04STony Xie void rockchip_get_sys_suspend_power_state(psci_power_state_t *req_state) 1096fba6e04STony Xie { 1106fba6e04STony Xie int i; 1116fba6e04STony Xie 1126fba6e04STony Xie for (i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++) 1136fba6e04STony Xie req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE; 1146fba6e04STony Xie } 1156fba6e04STony Xie 1166fba6e04STony Xie /******************************************************************************* 1176fba6e04STony Xie * RockChip handler called when a CPU is about to enter standby. 1186fba6e04STony Xie ******************************************************************************/ 1196fba6e04STony Xie void rockchip_cpu_standby(plat_local_state_t cpu_state) 1206fba6e04STony Xie { 1216fba6e04STony Xie unsigned int scr; 1226fba6e04STony Xie 1236fba6e04STony Xie assert(cpu_state == PLAT_MAX_RET_STATE); 1246fba6e04STony Xie 1256fba6e04STony Xie scr = read_scr_el3(); 1266fba6e04STony Xie /* Enable PhysicalIRQ bit for NS world to wake the CPU */ 1276fba6e04STony Xie write_scr_el3(scr | SCR_IRQ_BIT); 1286fba6e04STony Xie isb(); 1296fba6e04STony Xie dsb(); 1306fba6e04STony Xie wfi(); 1316fba6e04STony Xie 1326fba6e04STony Xie /* 1336fba6e04STony Xie * Restore SCR to the original value, synchronisation of scr_el3 is 1346fba6e04STony Xie * done by eret while el3_exit to save some execution cycles. 1356fba6e04STony Xie */ 1366fba6e04STony Xie write_scr_el3(scr); 1376fba6e04STony Xie } 1386fba6e04STony Xie 1396fba6e04STony Xie /******************************************************************************* 1406fba6e04STony Xie * RockChip handler called when a power domain is about to be turned on. The 1416fba6e04STony Xie * mpidr determines the CPU to be turned on. 1426fba6e04STony Xie ******************************************************************************/ 1436fba6e04STony Xie int rockchip_pwr_domain_on(u_register_t mpidr) 1446fba6e04STony Xie { 1456fba6e04STony Xie if (rockchip_ops && rockchip_ops->cores_pwr_dm_on) 1466fba6e04STony Xie rockchip_ops->cores_pwr_dm_on(mpidr, rockchip_sec_entrypoint); 1476fba6e04STony Xie 1486fba6e04STony Xie return PSCI_E_SUCCESS; 1496fba6e04STony Xie } 1506fba6e04STony Xie 1516fba6e04STony Xie /******************************************************************************* 1526fba6e04STony Xie * RockChip handler called when a power domain is about to be turned off. The 1536fba6e04STony Xie * target_state encodes the power state that each level should transition to. 1546fba6e04STony Xie ******************************************************************************/ 1556fba6e04STony Xie void rockchip_pwr_domain_off(const psci_power_state_t *target_state) 1566fba6e04STony Xie { 1576fba6e04STony Xie assert(RK_CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE); 1586fba6e04STony Xie 1596fba6e04STony Xie plat_rockchip_gic_cpuif_disable(); 1606fba6e04STony Xie 1616fba6e04STony Xie if (RK_CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) 1626fba6e04STony Xie plat_cci_disable(); 1636fba6e04STony Xie if (rockchip_ops && rockchip_ops->cores_pwr_dm_off) 1646fba6e04STony Xie rockchip_ops->cores_pwr_dm_off(); 1656fba6e04STony Xie } 1666fba6e04STony Xie 1676fba6e04STony Xie /******************************************************************************* 1686fba6e04STony Xie * RockChip handler called when a power domain is about to be suspended. The 1696fba6e04STony Xie * target_state encodes the power state that each level should transition to. 1706fba6e04STony Xie ******************************************************************************/ 1716fba6e04STony Xie void rockchip_pwr_domain_suspend(const psci_power_state_t *target_state) 1726fba6e04STony Xie { 1736fba6e04STony Xie if (RK_CORE_PWR_STATE(target_state) == PLAT_MAX_RET_STATE) 1746fba6e04STony Xie return; 1756fba6e04STony Xie 1766fba6e04STony Xie assert(RK_CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE); 1776fba6e04STony Xie 1786fba6e04STony Xie if (RK_SYSTEM_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) { 1796fba6e04STony Xie if (rockchip_ops && rockchip_ops->sys_pwr_dm_suspend) 1806fba6e04STony Xie rockchip_ops->sys_pwr_dm_suspend(); 1816fba6e04STony Xie } else { 1826fba6e04STony Xie if (rockchip_ops && rockchip_ops->cores_pwr_dm_suspend) 1836fba6e04STony Xie rockchip_ops->cores_pwr_dm_suspend(); 1846fba6e04STony Xie } 1856fba6e04STony Xie 1866fba6e04STony Xie /* Prevent interrupts from spuriously waking up this cpu */ 1876fba6e04STony Xie plat_rockchip_gic_cpuif_disable(); 1886fba6e04STony Xie 1896fba6e04STony Xie /* Perform the common cluster specific operations */ 1906fba6e04STony Xie if (RK_CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) 1916fba6e04STony Xie plat_cci_disable(); 1926fba6e04STony Xie } 1936fba6e04STony Xie 1946fba6e04STony Xie /******************************************************************************* 1956fba6e04STony Xie * RockChip handler called when a power domain has just been powered on after 1966fba6e04STony Xie * being turned off earlier. The target_state encodes the low power state that 1976fba6e04STony Xie * each level has woken up from. 1986fba6e04STony Xie ******************************************************************************/ 1996fba6e04STony Xie void rockchip_pwr_domain_on_finish(const psci_power_state_t *target_state) 2006fba6e04STony Xie { 2016fba6e04STony Xie assert(RK_CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE); 2026fba6e04STony Xie 2036fba6e04STony Xie if (rockchip_ops && rockchip_ops->cores_pwr_dm_on_finish) 2046fba6e04STony Xie rockchip_ops->cores_pwr_dm_on_finish(); 2056fba6e04STony Xie 2066fba6e04STony Xie /* Perform the common cluster specific operations */ 2076fba6e04STony Xie if (RK_CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) { 2086fba6e04STony Xie /* Enable coherency if this cluster was off */ 2096fba6e04STony Xie plat_cci_enable(); 2106fba6e04STony Xie } 2116fba6e04STony Xie 2126fba6e04STony Xie /* Enable the gic cpu interface */ 2136fba6e04STony Xie plat_rockchip_gic_pcpu_init(); 2146fba6e04STony Xie 2156fba6e04STony Xie /* Program the gic per-cpu distributor or re-distributor interface */ 2166fba6e04STony Xie plat_rockchip_gic_cpuif_enable(); 2176fba6e04STony Xie } 2186fba6e04STony Xie 2196fba6e04STony Xie /******************************************************************************* 2206fba6e04STony Xie * RockChip handler called when a power domain has just been powered on after 2216fba6e04STony Xie * having been suspended earlier. The target_state encodes the low power state 2226fba6e04STony Xie * that each level has woken up from. 2236fba6e04STony Xie * TODO: At the moment we reuse the on finisher and reinitialize the secure 2246fba6e04STony Xie * context. Need to implement a separate suspend finisher. 2256fba6e04STony Xie ******************************************************************************/ 2266fba6e04STony Xie void rockchip_pwr_domain_suspend_finish(const psci_power_state_t *target_state) 2276fba6e04STony Xie { 2286fba6e04STony Xie /* Nothing to be done on waking up from retention from CPU level */ 2296fba6e04STony Xie if (RK_CORE_PWR_STATE(target_state) == PLAT_MAX_RET_STATE) 2306fba6e04STony Xie return; 2316fba6e04STony Xie 2326fba6e04STony Xie /* Perform system domain restore if woken up from system suspend */ 2336fba6e04STony Xie if (RK_SYSTEM_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) 2346fba6e04STony Xie plat_rockchip_sys_pwr_domain_resume(); 2356fba6e04STony Xie else 2366fba6e04STony Xie plat_rockchip_cores_pwr_domain_resume(); 2376fba6e04STony Xie 2386fba6e04STony Xie /* Perform the common cluster specific operations */ 2396fba6e04STony Xie if (RK_CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) { 2406fba6e04STony Xie /* Enable coherency if this cluster was off */ 2416fba6e04STony Xie plat_cci_enable(); 2426fba6e04STony Xie } 2436fba6e04STony Xie } 2446fba6e04STony Xie 2456fba6e04STony Xie /******************************************************************************* 2466fba6e04STony Xie * RockChip handlers to reboot the system 2476fba6e04STony Xie ******************************************************************************/ 2486fba6e04STony Xie static void __dead2 rockchip_system_reset(void) 2496fba6e04STony Xie { 2506fba6e04STony Xie assert(rockchip_ops && rockchip_ops->sys_gbl_soft_reset); 2516fba6e04STony Xie 2526fba6e04STony Xie rockchip_ops->sys_gbl_soft_reset(); 2536fba6e04STony Xie } 2546fba6e04STony Xie 2556fba6e04STony Xie /******************************************************************************* 256*86c253e4SCaesar Wang * RockChip handlers to power off the system 257*86c253e4SCaesar Wang ******************************************************************************/ 258*86c253e4SCaesar Wang static void __dead2 rockchip_system_poweroff(void) 259*86c253e4SCaesar Wang { 260*86c253e4SCaesar Wang assert(rockchip_ops && rockchip_ops->system_off); 261*86c253e4SCaesar Wang 262*86c253e4SCaesar Wang rockchip_ops->system_off(); 263*86c253e4SCaesar Wang } 264*86c253e4SCaesar Wang 265*86c253e4SCaesar Wang /******************************************************************************* 2666fba6e04STony Xie * Export the platform handlers via plat_rockchip_psci_pm_ops. The rockchip 2676fba6e04STony Xie * standard 2686fba6e04STony Xie * platform layer will take care of registering the handlers with PSCI. 2696fba6e04STony Xie ******************************************************************************/ 2706fba6e04STony Xie const plat_psci_ops_t plat_rockchip_psci_pm_ops = { 2716fba6e04STony Xie .cpu_standby = rockchip_cpu_standby, 2726fba6e04STony Xie .pwr_domain_on = rockchip_pwr_domain_on, 2736fba6e04STony Xie .pwr_domain_off = rockchip_pwr_domain_off, 2746fba6e04STony Xie .pwr_domain_suspend = rockchip_pwr_domain_suspend, 2756fba6e04STony Xie .pwr_domain_on_finish = rockchip_pwr_domain_on_finish, 2766fba6e04STony Xie .pwr_domain_suspend_finish = rockchip_pwr_domain_suspend_finish, 2776fba6e04STony Xie .system_reset = rockchip_system_reset, 278*86c253e4SCaesar Wang .system_off = rockchip_system_poweroff, 2796fba6e04STony Xie .validate_power_state = rockchip_validate_power_state, 2806fba6e04STony Xie .get_sys_suspend_power_state = rockchip_get_sys_suspend_power_state 2816fba6e04STony Xie }; 2826fba6e04STony Xie 2836fba6e04STony Xie int plat_setup_psci_ops(uintptr_t sec_entrypoint, 2846fba6e04STony Xie const plat_psci_ops_t **psci_ops) 2856fba6e04STony Xie { 2866fba6e04STony Xie *psci_ops = &plat_rockchip_psci_pm_ops; 2876fba6e04STony Xie rockchip_sec_entrypoint = sec_entrypoint; 2886fba6e04STony Xie return 0; 2896fba6e04STony Xie } 2906fba6e04STony Xie 2916fba6e04STony Xie void plat_setup_rockchip_pm_ops(struct rockchip_pm_ops_cb *ops) 2926fba6e04STony Xie { 2936fba6e04STony Xie rockchip_ops = ops; 2946fba6e04STony Xie } 295