14f2b9848SAndre Przywara /* 24f2b9848SAndre Przywara * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 34f2b9848SAndre Przywara * 44f2b9848SAndre Przywara * SPDX-License-Identifier: BSD-3-Clause 54f2b9848SAndre Przywara */ 64f2b9848SAndre Przywara 74f2b9848SAndre Przywara #include <assert.h> 84f2b9848SAndre Przywara 94f2b9848SAndre Przywara #include <platform_def.h> 104f2b9848SAndre Przywara 114f2b9848SAndre Przywara #include <arch_helpers.h> 124f2b9848SAndre Przywara #include <common/debug.h> 134f2b9848SAndre Przywara #include <drivers/console.h> 144f2b9848SAndre Przywara #include <lib/mmio.h> 154f2b9848SAndre Przywara #include <lib/psci/psci.h> 164f2b9848SAndre Przywara #include <plat/common/platform.h> 174f2b9848SAndre Przywara 184f2b9848SAndre Przywara #include <rpi_hw.h> 194f2b9848SAndre Przywara 20e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC 21e6fd00abSAndre Przywara #include <drivers/arm/gicv2.h> 22e6fd00abSAndre Przywara #endif 23e6fd00abSAndre Przywara 244f2b9848SAndre Przywara /* Make composite power state parameter till power level 0 */ 254f2b9848SAndre Przywara #if PSCI_EXTENDED_STATE_ID 264f2b9848SAndre Przywara 274f2b9848SAndre Przywara #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ 284f2b9848SAndre Przywara (((lvl0_state) << PSTATE_ID_SHIFT) | \ 294f2b9848SAndre Przywara ((type) << PSTATE_TYPE_SHIFT)) 304f2b9848SAndre Przywara 314f2b9848SAndre Przywara #else 324f2b9848SAndre Przywara 334f2b9848SAndre Przywara #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \ 344f2b9848SAndre Przywara (((lvl0_state) << PSTATE_ID_SHIFT) | \ 354f2b9848SAndre Przywara ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \ 364f2b9848SAndre Przywara ((type) << PSTATE_TYPE_SHIFT)) 374f2b9848SAndre Przywara 384f2b9848SAndre Przywara #endif /* PSCI_EXTENDED_STATE_ID */ 394f2b9848SAndre Przywara 404f2b9848SAndre Przywara #define rpi3_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \ 414f2b9848SAndre Przywara (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \ 424f2b9848SAndre Przywara rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type)) 434f2b9848SAndre Przywara 444f2b9848SAndre Przywara /* 454f2b9848SAndre Przywara * The table storing the valid idle power states. Ensure that the 464f2b9848SAndre Przywara * array entries are populated in ascending order of state-id to 474f2b9848SAndre Przywara * enable us to use binary search during power state validation. 484f2b9848SAndre Przywara * The table must be terminated by a NULL entry. 494f2b9848SAndre Przywara */ 504f2b9848SAndre Przywara static const unsigned int rpi3_pm_idle_states[] = { 514f2b9848SAndre Przywara /* State-id - 0x01 */ 524f2b9848SAndre Przywara rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET, 534f2b9848SAndre Przywara MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY), 544f2b9848SAndre Przywara /* State-id - 0x02 */ 554f2b9848SAndre Przywara rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF, 564f2b9848SAndre Przywara MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN), 574f2b9848SAndre Przywara /* State-id - 0x22 */ 584f2b9848SAndre Przywara rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF, 594f2b9848SAndre Przywara MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN), 604f2b9848SAndre Przywara 0, 614f2b9848SAndre Przywara }; 624f2b9848SAndre Przywara 634f2b9848SAndre Przywara /******************************************************************************* 644f2b9848SAndre Przywara * Platform handler called to check the validity of the power state 654f2b9848SAndre Przywara * parameter. The power state parameter has to be a composite power state. 664f2b9848SAndre Przywara ******************************************************************************/ 674f2b9848SAndre Przywara static int rpi3_validate_power_state(unsigned int power_state, 684f2b9848SAndre Przywara psci_power_state_t *req_state) 694f2b9848SAndre Przywara { 704f2b9848SAndre Przywara unsigned int state_id; 714f2b9848SAndre Przywara int i; 724f2b9848SAndre Przywara 734f2b9848SAndre Przywara assert(req_state != 0); 744f2b9848SAndre Przywara 754f2b9848SAndre Przywara /* 764f2b9848SAndre Przywara * Currently we are using a linear search for finding the matching 774f2b9848SAndre Przywara * entry in the idle power state array. This can be made a binary 784f2b9848SAndre Przywara * search if the number of entries justify the additional complexity. 794f2b9848SAndre Przywara */ 804f2b9848SAndre Przywara for (i = 0; rpi3_pm_idle_states[i] != 0; i++) { 814f2b9848SAndre Przywara if (power_state == rpi3_pm_idle_states[i]) { 824f2b9848SAndre Przywara break; 834f2b9848SAndre Przywara } 844f2b9848SAndre Przywara } 854f2b9848SAndre Przywara 864f2b9848SAndre Przywara /* Return error if entry not found in the idle state array */ 874f2b9848SAndre Przywara if (!rpi3_pm_idle_states[i]) { 884f2b9848SAndre Przywara return PSCI_E_INVALID_PARAMS; 894f2b9848SAndre Przywara } 904f2b9848SAndre Przywara 914f2b9848SAndre Przywara i = 0; 924f2b9848SAndre Przywara state_id = psci_get_pstate_id(power_state); 934f2b9848SAndre Przywara 944f2b9848SAndre Przywara /* Parse the State ID and populate the state info parameter */ 954f2b9848SAndre Przywara while (state_id) { 964f2b9848SAndre Przywara req_state->pwr_domain_state[i++] = state_id & 974f2b9848SAndre Przywara PLAT_LOCAL_PSTATE_MASK; 984f2b9848SAndre Przywara state_id >>= PLAT_LOCAL_PSTATE_WIDTH; 994f2b9848SAndre Przywara } 1004f2b9848SAndre Przywara 1014f2b9848SAndre Przywara return PSCI_E_SUCCESS; 1024f2b9848SAndre Przywara } 1034f2b9848SAndre Przywara 1044f2b9848SAndre Przywara /******************************************************************************* 1054f2b9848SAndre Przywara * Platform handler called when a CPU is about to enter standby. 1064f2b9848SAndre Przywara ******************************************************************************/ 1074f2b9848SAndre Przywara static void rpi3_cpu_standby(plat_local_state_t cpu_state) 1084f2b9848SAndre Przywara { 1094f2b9848SAndre Przywara assert(cpu_state == PLAT_LOCAL_STATE_RET); 1104f2b9848SAndre Przywara 1114f2b9848SAndre Przywara /* 1124f2b9848SAndre Przywara * Enter standby state. 1134f2b9848SAndre Przywara * dsb is good practice before using wfi to enter low power states 1144f2b9848SAndre Przywara */ 1154f2b9848SAndre Przywara dsb(); 1164f2b9848SAndre Przywara wfi(); 1174f2b9848SAndre Przywara } 1184f2b9848SAndre Przywara 119e6fd00abSAndre Przywara static void rpi3_pwr_domain_off(const psci_power_state_t *target_state) 120e6fd00abSAndre Przywara { 121e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC 122e6fd00abSAndre Przywara gicv2_cpuif_disable(); 123e6fd00abSAndre Przywara #endif 124e6fd00abSAndre Przywara } 125e6fd00abSAndre Przywara 1269e78cb18SJan Kiszka void __dead2 plat_secondary_cold_boot_setup(void); 1279e78cb18SJan Kiszka 1289e78cb18SJan Kiszka static void __dead2 1299e78cb18SJan Kiszka rpi3_pwr_domain_pwr_down_wfi(const psci_power_state_t *target_state) 1309e78cb18SJan Kiszka { 1319e78cb18SJan Kiszka disable_mmu_el3(); 1329e78cb18SJan Kiszka plat_secondary_cold_boot_setup(); 1339e78cb18SJan Kiszka } 1349e78cb18SJan Kiszka 1354f2b9848SAndre Przywara /******************************************************************************* 1364f2b9848SAndre Przywara * Platform handler called when a power domain is about to be turned on. The 1374f2b9848SAndre Przywara * mpidr determines the CPU to be turned on. 1384f2b9848SAndre Przywara ******************************************************************************/ 1394f2b9848SAndre Przywara static int rpi3_pwr_domain_on(u_register_t mpidr) 1404f2b9848SAndre Przywara { 1414f2b9848SAndre Przywara int rc = PSCI_E_SUCCESS; 1424f2b9848SAndre Przywara unsigned int pos = plat_core_pos_by_mpidr(mpidr); 143af2a4877SAndre Przywara uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE; 1444f2b9848SAndre Przywara 1454f2b9848SAndre Przywara assert(pos < PLATFORM_CORE_COUNT); 1464f2b9848SAndre Przywara 147af2a4877SAndre Przywara hold_base += pos * PLAT_RPI3_TM_HOLD_ENTRY_SIZE; 148af2a4877SAndre Przywara 149af2a4877SAndre Przywara mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_GO); 150af2a4877SAndre Przywara /* No cache maintenance here, hold_base is mapped as device memory. */ 1514f2b9848SAndre Przywara 1524f2b9848SAndre Przywara /* Make sure that the write has completed */ 1534f2b9848SAndre Przywara dsb(); 1544f2b9848SAndre Przywara isb(); 1554f2b9848SAndre Przywara 1564f2b9848SAndre Przywara sev(); 1574f2b9848SAndre Przywara 1584f2b9848SAndre Przywara return rc; 1594f2b9848SAndre Przywara } 1604f2b9848SAndre Przywara 1614f2b9848SAndre Przywara /******************************************************************************* 1624f2b9848SAndre Przywara * Platform handler called when a power domain has just been powered on after 1634f2b9848SAndre Przywara * being turned off earlier. The target_state encodes the low power state that 1644f2b9848SAndre Przywara * each level has woken up from. 1654f2b9848SAndre Przywara ******************************************************************************/ 1664f2b9848SAndre Przywara static void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state) 1674f2b9848SAndre Przywara { 1684f2b9848SAndre Przywara assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] == 1694f2b9848SAndre Przywara PLAT_LOCAL_STATE_OFF); 170e6fd00abSAndre Przywara 171e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC 172e6fd00abSAndre Przywara gicv2_pcpu_distif_init(); 173e6fd00abSAndre Przywara gicv2_cpuif_enable(); 174e6fd00abSAndre Przywara #endif 1754f2b9848SAndre Przywara } 1764f2b9848SAndre Przywara 177*2e5f8443SAndrei Warkentin static void __dead2 rpi3_pwr_down_wfi( 178*2e5f8443SAndrei Warkentin const psci_power_state_t *target_state) 179*2e5f8443SAndrei Warkentin { 180*2e5f8443SAndrei Warkentin uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE; 181*2e5f8443SAndrei Warkentin unsigned int pos = plat_my_core_pos(); 182*2e5f8443SAndrei Warkentin 183*2e5f8443SAndrei Warkentin if (pos == 0) { 184*2e5f8443SAndrei Warkentin /* 185*2e5f8443SAndrei Warkentin * The secondaries will always be in a wait 186*2e5f8443SAndrei Warkentin * for warm boot on reset, but the BSP needs 187*2e5f8443SAndrei Warkentin * to be able to distinguish between waiting 188*2e5f8443SAndrei Warkentin * for warm boot (e.g. after psci_off, waiting 189*2e5f8443SAndrei Warkentin * for psci_on) and a cold boot. 190*2e5f8443SAndrei Warkentin */ 191*2e5f8443SAndrei Warkentin mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_BSP_OFF); 192*2e5f8443SAndrei Warkentin /* No cache maintenance here, we run with caches off already. */ 193*2e5f8443SAndrei Warkentin dsb(); 194*2e5f8443SAndrei Warkentin isb(); 195*2e5f8443SAndrei Warkentin } 196*2e5f8443SAndrei Warkentin 197*2e5f8443SAndrei Warkentin write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT); 198*2e5f8443SAndrei Warkentin 199*2e5f8443SAndrei Warkentin while (1) 200*2e5f8443SAndrei Warkentin ; 201*2e5f8443SAndrei Warkentin } 202*2e5f8443SAndrei Warkentin 2034f2b9848SAndre Przywara /******************************************************************************* 2044f2b9848SAndre Przywara * Platform handlers for system reset and system off. 2054f2b9848SAndre Przywara ******************************************************************************/ 2064f2b9848SAndre Przywara 2074f2b9848SAndre Przywara /* 10 ticks (Watchdog timer = Timer clock / 16) */ 2084f2b9848SAndre Przywara #define RESET_TIMEOUT U(10) 2094f2b9848SAndre Przywara 2104f2b9848SAndre Przywara static void __dead2 rpi3_watchdog_reset(void) 2114f2b9848SAndre Przywara { 2124f2b9848SAndre Przywara uint32_t rstc; 2134f2b9848SAndre Przywara 2144f2b9848SAndre Przywara console_flush(); 2154f2b9848SAndre Przywara 2164f2b9848SAndre Przywara dsbsy(); 2174f2b9848SAndre Przywara isb(); 2184f2b9848SAndre Przywara 2194f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_WDOG_OFFSET, 2204f2b9848SAndre Przywara RPI3_PM_PASSWORD | RESET_TIMEOUT); 2214f2b9848SAndre Przywara 2224f2b9848SAndre Przywara rstc = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET); 2234f2b9848SAndre Przywara rstc &= ~RPI3_PM_RSTC_WRCFG_MASK; 2244f2b9848SAndre Przywara rstc |= RPI3_PM_PASSWORD | RPI3_PM_RSTC_WRCFG_FULL_RESET; 2254f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET, rstc); 2264f2b9848SAndre Przywara 2274f2b9848SAndre Przywara for (;;) { 2284f2b9848SAndre Przywara wfi(); 2294f2b9848SAndre Przywara } 2304f2b9848SAndre Przywara } 2314f2b9848SAndre Przywara 2324f2b9848SAndre Przywara static void __dead2 rpi3_system_reset(void) 2334f2b9848SAndre Przywara { 2344f2b9848SAndre Przywara INFO("rpi3: PSCI_SYSTEM_RESET: Invoking watchdog reset\n"); 2354f2b9848SAndre Przywara 2364f2b9848SAndre Przywara rpi3_watchdog_reset(); 2374f2b9848SAndre Przywara } 2384f2b9848SAndre Przywara 2394f2b9848SAndre Przywara static void __dead2 rpi3_system_off(void) 2404f2b9848SAndre Przywara { 2414f2b9848SAndre Przywara uint32_t rsts; 2424f2b9848SAndre Przywara 2434f2b9848SAndre Przywara INFO("rpi3: PSCI_SYSTEM_OFF: Invoking watchdog reset\n"); 2444f2b9848SAndre Przywara 2454f2b9848SAndre Przywara /* 2464f2b9848SAndre Przywara * This function doesn't actually make the Raspberry Pi turn itself off, 2474f2b9848SAndre Przywara * the hardware doesn't allow it. It simply reboots it and the RSTS 2484f2b9848SAndre Przywara * value tells the bootcode.bin firmware not to continue the regular 2494f2b9848SAndre Przywara * bootflow and to stay in a low power mode. 2504f2b9848SAndre Przywara */ 2514f2b9848SAndre Przywara 2524f2b9848SAndre Przywara rsts = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET); 2534f2b9848SAndre Przywara rsts |= RPI3_PM_PASSWORD | RPI3_PM_RSTS_WRCFG_HALT; 2544f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET, rsts); 2554f2b9848SAndre Przywara 2564f2b9848SAndre Przywara rpi3_watchdog_reset(); 2574f2b9848SAndre Przywara } 2584f2b9848SAndre Przywara 2594f2b9848SAndre Przywara /******************************************************************************* 2604f2b9848SAndre Przywara * Platform handlers and setup function. 2614f2b9848SAndre Przywara ******************************************************************************/ 2624f2b9848SAndre Przywara static const plat_psci_ops_t plat_rpi3_psci_pm_ops = { 2634f2b9848SAndre Przywara .cpu_standby = rpi3_cpu_standby, 264e6fd00abSAndre Przywara .pwr_domain_off = rpi3_pwr_domain_off, 2659e78cb18SJan Kiszka .pwr_domain_pwr_down_wfi = rpi3_pwr_domain_pwr_down_wfi, 2664f2b9848SAndre Przywara .pwr_domain_on = rpi3_pwr_domain_on, 2674f2b9848SAndre Przywara .pwr_domain_on_finish = rpi3_pwr_domain_on_finish, 268*2e5f8443SAndrei Warkentin .pwr_domain_pwr_down_wfi = rpi3_pwr_down_wfi, 2694f2b9848SAndre Przywara .system_off = rpi3_system_off, 2704f2b9848SAndre Przywara .system_reset = rpi3_system_reset, 2714f2b9848SAndre Przywara .validate_power_state = rpi3_validate_power_state, 2724f2b9848SAndre Przywara }; 2734f2b9848SAndre Przywara 2744f2b9848SAndre Przywara int plat_setup_psci_ops(uintptr_t sec_entrypoint, 2754f2b9848SAndre Przywara const plat_psci_ops_t **psci_ops) 2764f2b9848SAndre Przywara { 2774f2b9848SAndre Przywara uintptr_t *entrypoint = (void *) PLAT_RPI3_TM_ENTRYPOINT; 2784f2b9848SAndre Przywara 2794f2b9848SAndre Przywara *entrypoint = sec_entrypoint; 2804f2b9848SAndre Przywara *psci_ops = &plat_rpi3_psci_pm_ops; 2814f2b9848SAndre Przywara 2824f2b9848SAndre Przywara return 0; 2834f2b9848SAndre Przywara } 284