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 20*e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC 21*e6fd00abSAndre Przywara #include <drivers/arm/gicv2.h> 22*e6fd00abSAndre Przywara #endif 23*e6fd00abSAndre 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 119*e6fd00abSAndre Przywara static void rpi3_pwr_domain_off(const psci_power_state_t *target_state) 120*e6fd00abSAndre Przywara { 121*e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC 122*e6fd00abSAndre Przywara gicv2_cpuif_disable(); 123*e6fd00abSAndre Przywara #endif 124*e6fd00abSAndre Przywara } 125*e6fd00abSAndre Przywara 1264f2b9848SAndre Przywara /******************************************************************************* 1274f2b9848SAndre Przywara * Platform handler called when a power domain is about to be turned on. The 1284f2b9848SAndre Przywara * mpidr determines the CPU to be turned on. 1294f2b9848SAndre Przywara ******************************************************************************/ 1304f2b9848SAndre Przywara static int rpi3_pwr_domain_on(u_register_t mpidr) 1314f2b9848SAndre Przywara { 1324f2b9848SAndre Przywara int rc = PSCI_E_SUCCESS; 1334f2b9848SAndre Przywara unsigned int pos = plat_core_pos_by_mpidr(mpidr); 1344f2b9848SAndre Przywara uint64_t *hold_base = (uint64_t *)PLAT_RPI3_TM_HOLD_BASE; 1354f2b9848SAndre Przywara 1364f2b9848SAndre Przywara assert(pos < PLATFORM_CORE_COUNT); 1374f2b9848SAndre Przywara 1384f2b9848SAndre Przywara hold_base[pos] = PLAT_RPI3_TM_HOLD_STATE_GO; 1394f2b9848SAndre Przywara 1404f2b9848SAndre Przywara /* Make sure that the write has completed */ 1414f2b9848SAndre Przywara dsb(); 1424f2b9848SAndre Przywara isb(); 1434f2b9848SAndre Przywara 1444f2b9848SAndre Przywara sev(); 1454f2b9848SAndre Przywara 1464f2b9848SAndre Przywara return rc; 1474f2b9848SAndre Przywara } 1484f2b9848SAndre Przywara 1494f2b9848SAndre Przywara /******************************************************************************* 1504f2b9848SAndre Przywara * Platform handler called when a power domain has just been powered on after 1514f2b9848SAndre Przywara * being turned off earlier. The target_state encodes the low power state that 1524f2b9848SAndre Przywara * each level has woken up from. 1534f2b9848SAndre Przywara ******************************************************************************/ 1544f2b9848SAndre Przywara static void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state) 1554f2b9848SAndre Przywara { 1564f2b9848SAndre Przywara assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] == 1574f2b9848SAndre Przywara PLAT_LOCAL_STATE_OFF); 158*e6fd00abSAndre Przywara 159*e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC 160*e6fd00abSAndre Przywara gicv2_pcpu_distif_init(); 161*e6fd00abSAndre Przywara gicv2_cpuif_enable(); 162*e6fd00abSAndre Przywara #endif 1634f2b9848SAndre Przywara } 1644f2b9848SAndre Przywara 1654f2b9848SAndre Przywara /******************************************************************************* 1664f2b9848SAndre Przywara * Platform handlers for system reset and system off. 1674f2b9848SAndre Przywara ******************************************************************************/ 1684f2b9848SAndre Przywara 1694f2b9848SAndre Przywara /* 10 ticks (Watchdog timer = Timer clock / 16) */ 1704f2b9848SAndre Przywara #define RESET_TIMEOUT U(10) 1714f2b9848SAndre Przywara 1724f2b9848SAndre Przywara static void __dead2 rpi3_watchdog_reset(void) 1734f2b9848SAndre Przywara { 1744f2b9848SAndre Przywara uint32_t rstc; 1754f2b9848SAndre Przywara 1764f2b9848SAndre Przywara console_flush(); 1774f2b9848SAndre Przywara 1784f2b9848SAndre Przywara dsbsy(); 1794f2b9848SAndre Przywara isb(); 1804f2b9848SAndre Przywara 1814f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_WDOG_OFFSET, 1824f2b9848SAndre Przywara RPI3_PM_PASSWORD | RESET_TIMEOUT); 1834f2b9848SAndre Przywara 1844f2b9848SAndre Przywara rstc = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET); 1854f2b9848SAndre Przywara rstc &= ~RPI3_PM_RSTC_WRCFG_MASK; 1864f2b9848SAndre Przywara rstc |= RPI3_PM_PASSWORD | RPI3_PM_RSTC_WRCFG_FULL_RESET; 1874f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET, rstc); 1884f2b9848SAndre Przywara 1894f2b9848SAndre Przywara for (;;) { 1904f2b9848SAndre Przywara wfi(); 1914f2b9848SAndre Przywara } 1924f2b9848SAndre Przywara } 1934f2b9848SAndre Przywara 1944f2b9848SAndre Przywara static void __dead2 rpi3_system_reset(void) 1954f2b9848SAndre Przywara { 1964f2b9848SAndre Przywara INFO("rpi3: PSCI_SYSTEM_RESET: Invoking watchdog reset\n"); 1974f2b9848SAndre Przywara 1984f2b9848SAndre Przywara rpi3_watchdog_reset(); 1994f2b9848SAndre Przywara } 2004f2b9848SAndre Przywara 2014f2b9848SAndre Przywara static void __dead2 rpi3_system_off(void) 2024f2b9848SAndre Przywara { 2034f2b9848SAndre Przywara uint32_t rsts; 2044f2b9848SAndre Przywara 2054f2b9848SAndre Przywara INFO("rpi3: PSCI_SYSTEM_OFF: Invoking watchdog reset\n"); 2064f2b9848SAndre Przywara 2074f2b9848SAndre Przywara /* 2084f2b9848SAndre Przywara * This function doesn't actually make the Raspberry Pi turn itself off, 2094f2b9848SAndre Przywara * the hardware doesn't allow it. It simply reboots it and the RSTS 2104f2b9848SAndre Przywara * value tells the bootcode.bin firmware not to continue the regular 2114f2b9848SAndre Przywara * bootflow and to stay in a low power mode. 2124f2b9848SAndre Przywara */ 2134f2b9848SAndre Przywara 2144f2b9848SAndre Przywara rsts = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET); 2154f2b9848SAndre Przywara rsts |= RPI3_PM_PASSWORD | RPI3_PM_RSTS_WRCFG_HALT; 2164f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET, rsts); 2174f2b9848SAndre Przywara 2184f2b9848SAndre Przywara rpi3_watchdog_reset(); 2194f2b9848SAndre Przywara } 2204f2b9848SAndre Przywara 2214f2b9848SAndre Przywara /******************************************************************************* 2224f2b9848SAndre Przywara * Platform handlers and setup function. 2234f2b9848SAndre Przywara ******************************************************************************/ 2244f2b9848SAndre Przywara static const plat_psci_ops_t plat_rpi3_psci_pm_ops = { 2254f2b9848SAndre Przywara .cpu_standby = rpi3_cpu_standby, 226*e6fd00abSAndre Przywara .pwr_domain_off = rpi3_pwr_domain_off, 2274f2b9848SAndre Przywara .pwr_domain_on = rpi3_pwr_domain_on, 2284f2b9848SAndre Przywara .pwr_domain_on_finish = rpi3_pwr_domain_on_finish, 2294f2b9848SAndre Przywara .system_off = rpi3_system_off, 2304f2b9848SAndre Przywara .system_reset = rpi3_system_reset, 2314f2b9848SAndre Przywara .validate_power_state = rpi3_validate_power_state, 2324f2b9848SAndre Przywara }; 2334f2b9848SAndre Przywara 2344f2b9848SAndre Przywara int plat_setup_psci_ops(uintptr_t sec_entrypoint, 2354f2b9848SAndre Przywara const plat_psci_ops_t **psci_ops) 2364f2b9848SAndre Przywara { 2374f2b9848SAndre Przywara uintptr_t *entrypoint = (void *) PLAT_RPI3_TM_ENTRYPOINT; 2384f2b9848SAndre Przywara 2394f2b9848SAndre Przywara *entrypoint = sec_entrypoint; 2404f2b9848SAndre Przywara *psci_ops = &plat_rpi3_psci_pm_ops; 2414f2b9848SAndre Przywara 2424f2b9848SAndre Przywara return 0; 2434f2b9848SAndre Przywara } 244