1*91f16700Schasinglulu /* 2*91f16700Schasinglulu * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 3*91f16700Schasinglulu * 4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause 5*91f16700Schasinglulu */ 6*91f16700Schasinglulu 7*91f16700Schasinglulu #include <assert.h> 8*91f16700Schasinglulu 9*91f16700Schasinglulu #include <platform_def.h> 10*91f16700Schasinglulu 11*91f16700Schasinglulu #include <arch_helpers.h> 12*91f16700Schasinglulu #include <common/debug.h> 13*91f16700Schasinglulu #include <drivers/console.h> 14*91f16700Schasinglulu #include <lib/mmio.h> 15*91f16700Schasinglulu #include <lib/psci/psci.h> 16*91f16700Schasinglulu #include <plat/common/platform.h> 17*91f16700Schasinglulu 18*91f16700Schasinglulu #include <rpi_hw.h> 19*91f16700Schasinglulu 20*91f16700Schasinglulu #ifdef RPI_HAVE_GIC 21*91f16700Schasinglulu #include <drivers/arm/gicv2.h> 22*91f16700Schasinglulu #endif 23*91f16700Schasinglulu 24*91f16700Schasinglulu /* Make composite power state parameter till power level 0 */ 25*91f16700Schasinglulu #if PSCI_EXTENDED_STATE_ID 26*91f16700Schasinglulu 27*91f16700Schasinglulu #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ 28*91f16700Schasinglulu (((lvl0_state) << PSTATE_ID_SHIFT) | \ 29*91f16700Schasinglulu ((type) << PSTATE_TYPE_SHIFT)) 30*91f16700Schasinglulu 31*91f16700Schasinglulu #else 32*91f16700Schasinglulu 33*91f16700Schasinglulu #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ 34*91f16700Schasinglulu (((lvl0_state) << PSTATE_ID_SHIFT) | \ 35*91f16700Schasinglulu ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \ 36*91f16700Schasinglulu ((type) << PSTATE_TYPE_SHIFT)) 37*91f16700Schasinglulu 38*91f16700Schasinglulu #endif /* PSCI_EXTENDED_STATE_ID */ 39*91f16700Schasinglulu 40*91f16700Schasinglulu #define rpi3_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \ 41*91f16700Schasinglulu (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \ 42*91f16700Schasinglulu rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type)) 43*91f16700Schasinglulu 44*91f16700Schasinglulu /* 45*91f16700Schasinglulu * The table storing the valid idle power states. Ensure that the 46*91f16700Schasinglulu * array entries are populated in ascending order of state-id to 47*91f16700Schasinglulu * enable us to use binary search during power state validation. 48*91f16700Schasinglulu * The table must be terminated by a NULL entry. 49*91f16700Schasinglulu */ 50*91f16700Schasinglulu static const unsigned int rpi3_pm_idle_states[] = { 51*91f16700Schasinglulu /* State-id - 0x01 */ 52*91f16700Schasinglulu rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET, 53*91f16700Schasinglulu MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY), 54*91f16700Schasinglulu /* State-id - 0x02 */ 55*91f16700Schasinglulu rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF, 56*91f16700Schasinglulu MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN), 57*91f16700Schasinglulu /* State-id - 0x22 */ 58*91f16700Schasinglulu rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF, 59*91f16700Schasinglulu MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN), 60*91f16700Schasinglulu 0, 61*91f16700Schasinglulu }; 62*91f16700Schasinglulu 63*91f16700Schasinglulu /******************************************************************************* 64*91f16700Schasinglulu * Platform handler called to check the validity of the power state 65*91f16700Schasinglulu * parameter. The power state parameter has to be a composite power state. 66*91f16700Schasinglulu ******************************************************************************/ 67*91f16700Schasinglulu static int rpi3_validate_power_state(unsigned int power_state, 68*91f16700Schasinglulu psci_power_state_t *req_state) 69*91f16700Schasinglulu { 70*91f16700Schasinglulu unsigned int state_id; 71*91f16700Schasinglulu int i; 72*91f16700Schasinglulu 73*91f16700Schasinglulu assert(req_state != 0); 74*91f16700Schasinglulu 75*91f16700Schasinglulu /* 76*91f16700Schasinglulu * Currently we are using a linear search for finding the matching 77*91f16700Schasinglulu * entry in the idle power state array. This can be made a binary 78*91f16700Schasinglulu * search if the number of entries justify the additional complexity. 79*91f16700Schasinglulu */ 80*91f16700Schasinglulu for (i = 0; rpi3_pm_idle_states[i] != 0; i++) { 81*91f16700Schasinglulu if (power_state == rpi3_pm_idle_states[i]) { 82*91f16700Schasinglulu break; 83*91f16700Schasinglulu } 84*91f16700Schasinglulu } 85*91f16700Schasinglulu 86*91f16700Schasinglulu /* Return error if entry not found in the idle state array */ 87*91f16700Schasinglulu if (!rpi3_pm_idle_states[i]) { 88*91f16700Schasinglulu return PSCI_E_INVALID_PARAMS; 89*91f16700Schasinglulu } 90*91f16700Schasinglulu 91*91f16700Schasinglulu i = 0; 92*91f16700Schasinglulu state_id = psci_get_pstate_id(power_state); 93*91f16700Schasinglulu 94*91f16700Schasinglulu /* Parse the State ID and populate the state info parameter */ 95*91f16700Schasinglulu while (state_id) { 96*91f16700Schasinglulu req_state->pwr_domain_state[i++] = state_id & 97*91f16700Schasinglulu PLAT_LOCAL_PSTATE_MASK; 98*91f16700Schasinglulu state_id >>= PLAT_LOCAL_PSTATE_WIDTH; 99*91f16700Schasinglulu } 100*91f16700Schasinglulu 101*91f16700Schasinglulu return PSCI_E_SUCCESS; 102*91f16700Schasinglulu } 103*91f16700Schasinglulu 104*91f16700Schasinglulu /******************************************************************************* 105*91f16700Schasinglulu * Platform handler called when a CPU is about to enter standby. 106*91f16700Schasinglulu ******************************************************************************/ 107*91f16700Schasinglulu static void rpi3_cpu_standby(plat_local_state_t cpu_state) 108*91f16700Schasinglulu { 109*91f16700Schasinglulu assert(cpu_state == PLAT_LOCAL_STATE_RET); 110*91f16700Schasinglulu 111*91f16700Schasinglulu /* 112*91f16700Schasinglulu * Enter standby state. 113*91f16700Schasinglulu * dsb is good practice before using wfi to enter low power states 114*91f16700Schasinglulu */ 115*91f16700Schasinglulu dsb(); 116*91f16700Schasinglulu wfi(); 117*91f16700Schasinglulu } 118*91f16700Schasinglulu 119*91f16700Schasinglulu static void rpi3_pwr_domain_off(const psci_power_state_t *target_state) 120*91f16700Schasinglulu { 121*91f16700Schasinglulu #ifdef RPI_HAVE_GIC 122*91f16700Schasinglulu gicv2_cpuif_disable(); 123*91f16700Schasinglulu #endif 124*91f16700Schasinglulu } 125*91f16700Schasinglulu 126*91f16700Schasinglulu /******************************************************************************* 127*91f16700Schasinglulu * Platform handler called when a power domain is about to be turned on. The 128*91f16700Schasinglulu * mpidr determines the CPU to be turned on. 129*91f16700Schasinglulu ******************************************************************************/ 130*91f16700Schasinglulu static int rpi3_pwr_domain_on(u_register_t mpidr) 131*91f16700Schasinglulu { 132*91f16700Schasinglulu int rc = PSCI_E_SUCCESS; 133*91f16700Schasinglulu unsigned int pos = plat_core_pos_by_mpidr(mpidr); 134*91f16700Schasinglulu uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE; 135*91f16700Schasinglulu 136*91f16700Schasinglulu assert(pos < PLATFORM_CORE_COUNT); 137*91f16700Schasinglulu 138*91f16700Schasinglulu hold_base += pos * PLAT_RPI3_TM_HOLD_ENTRY_SIZE; 139*91f16700Schasinglulu 140*91f16700Schasinglulu mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_GO); 141*91f16700Schasinglulu /* No cache maintenance here, hold_base is mapped as device memory. */ 142*91f16700Schasinglulu 143*91f16700Schasinglulu /* Make sure that the write has completed */ 144*91f16700Schasinglulu dsb(); 145*91f16700Schasinglulu isb(); 146*91f16700Schasinglulu 147*91f16700Schasinglulu sev(); 148*91f16700Schasinglulu 149*91f16700Schasinglulu return rc; 150*91f16700Schasinglulu } 151*91f16700Schasinglulu 152*91f16700Schasinglulu /******************************************************************************* 153*91f16700Schasinglulu * Platform handler called when a power domain has just been powered on after 154*91f16700Schasinglulu * being turned off earlier. The target_state encodes the low power state that 155*91f16700Schasinglulu * each level has woken up from. 156*91f16700Schasinglulu ******************************************************************************/ 157*91f16700Schasinglulu static void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state) 158*91f16700Schasinglulu { 159*91f16700Schasinglulu assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] == 160*91f16700Schasinglulu PLAT_LOCAL_STATE_OFF); 161*91f16700Schasinglulu 162*91f16700Schasinglulu #ifdef RPI_HAVE_GIC 163*91f16700Schasinglulu gicv2_pcpu_distif_init(); 164*91f16700Schasinglulu gicv2_cpuif_enable(); 165*91f16700Schasinglulu #endif 166*91f16700Schasinglulu } 167*91f16700Schasinglulu 168*91f16700Schasinglulu static void __dead2 rpi3_pwr_down_wfi( 169*91f16700Schasinglulu const psci_power_state_t *target_state) 170*91f16700Schasinglulu { 171*91f16700Schasinglulu uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE; 172*91f16700Schasinglulu unsigned int pos = plat_my_core_pos(); 173*91f16700Schasinglulu 174*91f16700Schasinglulu if (pos == 0) { 175*91f16700Schasinglulu /* 176*91f16700Schasinglulu * The secondaries will always be in a wait 177*91f16700Schasinglulu * for warm boot on reset, but the BSP needs 178*91f16700Schasinglulu * to be able to distinguish between waiting 179*91f16700Schasinglulu * for warm boot (e.g. after psci_off, waiting 180*91f16700Schasinglulu * for psci_on) and a cold boot. 181*91f16700Schasinglulu */ 182*91f16700Schasinglulu mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_BSP_OFF); 183*91f16700Schasinglulu /* No cache maintenance here, we run with caches off already. */ 184*91f16700Schasinglulu dsb(); 185*91f16700Schasinglulu isb(); 186*91f16700Schasinglulu } 187*91f16700Schasinglulu 188*91f16700Schasinglulu write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT); 189*91f16700Schasinglulu 190*91f16700Schasinglulu while (1) { 191*91f16700Schasinglulu wfi(); 192*91f16700Schasinglulu } 193*91f16700Schasinglulu } 194*91f16700Schasinglulu 195*91f16700Schasinglulu /******************************************************************************* 196*91f16700Schasinglulu * Platform handlers for system reset and system off. 197*91f16700Schasinglulu ******************************************************************************/ 198*91f16700Schasinglulu 199*91f16700Schasinglulu /* 10 ticks (Watchdog timer = Timer clock / 16) */ 200*91f16700Schasinglulu #define RESET_TIMEOUT U(10) 201*91f16700Schasinglulu 202*91f16700Schasinglulu static void __dead2 rpi3_watchdog_reset(void) 203*91f16700Schasinglulu { 204*91f16700Schasinglulu uint32_t rstc; 205*91f16700Schasinglulu 206*91f16700Schasinglulu console_flush(); 207*91f16700Schasinglulu 208*91f16700Schasinglulu dsbsy(); 209*91f16700Schasinglulu isb(); 210*91f16700Schasinglulu 211*91f16700Schasinglulu mmio_write_32(RPI3_PM_BASE + RPI3_PM_WDOG_OFFSET, 212*91f16700Schasinglulu RPI3_PM_PASSWORD | RESET_TIMEOUT); 213*91f16700Schasinglulu 214*91f16700Schasinglulu rstc = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET); 215*91f16700Schasinglulu rstc &= ~RPI3_PM_RSTC_WRCFG_MASK; 216*91f16700Schasinglulu rstc |= RPI3_PM_PASSWORD | RPI3_PM_RSTC_WRCFG_FULL_RESET; 217*91f16700Schasinglulu mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET, rstc); 218*91f16700Schasinglulu 219*91f16700Schasinglulu for (;;) { 220*91f16700Schasinglulu wfi(); 221*91f16700Schasinglulu } 222*91f16700Schasinglulu } 223*91f16700Schasinglulu 224*91f16700Schasinglulu static void __dead2 rpi3_system_reset(void) 225*91f16700Schasinglulu { 226*91f16700Schasinglulu INFO("rpi3: PSCI_SYSTEM_RESET: Invoking watchdog reset\n"); 227*91f16700Schasinglulu 228*91f16700Schasinglulu rpi3_watchdog_reset(); 229*91f16700Schasinglulu } 230*91f16700Schasinglulu 231*91f16700Schasinglulu static void __dead2 rpi3_system_off(void) 232*91f16700Schasinglulu { 233*91f16700Schasinglulu uint32_t rsts; 234*91f16700Schasinglulu 235*91f16700Schasinglulu INFO("rpi3: PSCI_SYSTEM_OFF: Invoking watchdog reset\n"); 236*91f16700Schasinglulu 237*91f16700Schasinglulu /* 238*91f16700Schasinglulu * This function doesn't actually make the Raspberry Pi turn itself off, 239*91f16700Schasinglulu * the hardware doesn't allow it. It simply reboots it and the RSTS 240*91f16700Schasinglulu * value tells the bootcode.bin firmware not to continue the regular 241*91f16700Schasinglulu * bootflow and to stay in a low power mode. 242*91f16700Schasinglulu */ 243*91f16700Schasinglulu 244*91f16700Schasinglulu rsts = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET); 245*91f16700Schasinglulu rsts |= RPI3_PM_PASSWORD | RPI3_PM_RSTS_WRCFG_HALT; 246*91f16700Schasinglulu mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET, rsts); 247*91f16700Schasinglulu 248*91f16700Schasinglulu rpi3_watchdog_reset(); 249*91f16700Schasinglulu } 250*91f16700Schasinglulu 251*91f16700Schasinglulu /******************************************************************************* 252*91f16700Schasinglulu * Platform handlers and setup function. 253*91f16700Schasinglulu ******************************************************************************/ 254*91f16700Schasinglulu static const plat_psci_ops_t plat_rpi3_psci_pm_ops = { 255*91f16700Schasinglulu .cpu_standby = rpi3_cpu_standby, 256*91f16700Schasinglulu .pwr_domain_off = rpi3_pwr_domain_off, 257*91f16700Schasinglulu .pwr_domain_on = rpi3_pwr_domain_on, 258*91f16700Schasinglulu .pwr_domain_on_finish = rpi3_pwr_domain_on_finish, 259*91f16700Schasinglulu .pwr_domain_pwr_down_wfi = rpi3_pwr_down_wfi, 260*91f16700Schasinglulu .system_off = rpi3_system_off, 261*91f16700Schasinglulu .system_reset = rpi3_system_reset, 262*91f16700Schasinglulu .validate_power_state = rpi3_validate_power_state, 263*91f16700Schasinglulu }; 264*91f16700Schasinglulu 265*91f16700Schasinglulu int plat_setup_psci_ops(uintptr_t sec_entrypoint, 266*91f16700Schasinglulu const plat_psci_ops_t **psci_ops) 267*91f16700Schasinglulu { 268*91f16700Schasinglulu uintptr_t *entrypoint = (void *) PLAT_RPI3_TM_ENTRYPOINT; 269*91f16700Schasinglulu 270*91f16700Schasinglulu *entrypoint = sec_entrypoint; 271*91f16700Schasinglulu *psci_ops = &plat_rpi3_psci_pm_ops; 272*91f16700Schasinglulu 273*91f16700Schasinglulu return 0; 274*91f16700Schasinglulu } 275