14f2b9848SAndre Przywara /*
27a9cdf58SMario Bălănică * Copyright (c) 2015-2024, 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
247a9cdf58SMario Bălănică /* Registers on top of RPI3_PM_BASE. */
257a9cdf58SMario Bălănică #define RPI3_PM_RSTC_OFFSET ULL(0x0000001C)
267a9cdf58SMario Bălănică #define RPI3_PM_RSTS_OFFSET ULL(0x00000020)
277a9cdf58SMario Bălănică #define RPI3_PM_WDOG_OFFSET ULL(0x00000024)
287a9cdf58SMario Bălănică /* Watchdog constants */
297a9cdf58SMario Bălănică #define RPI3_PM_PASSWORD U(0x5A000000)
307a9cdf58SMario Bălănică #define RPI3_PM_RSTC_WRCFG_MASK U(0x00000030)
317a9cdf58SMario Bălănică #define RPI3_PM_RSTC_WRCFG_FULL_RESET U(0x00000020)
327a9cdf58SMario Bălănică /*
337a9cdf58SMario Bălănică * The RSTS register is used by the VideoCore firmware when booting the
347a9cdf58SMario Bălănică * Raspberry Pi to know which partition to boot from. The partition value is
357a9cdf58SMario Bălănică * formed by bits 0, 2, 4, 6, 8 and 10. Partition 63 is used by said firmware
367a9cdf58SMario Bălănică * to indicate halt.
377a9cdf58SMario Bălănică */
387a9cdf58SMario Bălănică #define RPI3_PM_RSTS_WRCFG_HALT U(0x00000555)
397a9cdf58SMario Bălănică
404f2b9848SAndre Przywara /* Make composite power state parameter till power level 0 */
414f2b9848SAndre Przywara #if PSCI_EXTENDED_STATE_ID
424f2b9848SAndre Przywara
434f2b9848SAndre Przywara #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
444f2b9848SAndre Przywara (((lvl0_state) << PSTATE_ID_SHIFT) | \
454f2b9848SAndre Przywara ((type) << PSTATE_TYPE_SHIFT))
464f2b9848SAndre Przywara
474f2b9848SAndre Przywara #else
484f2b9848SAndre Przywara
494f2b9848SAndre Przywara #define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
504f2b9848SAndre Przywara (((lvl0_state) << PSTATE_ID_SHIFT) | \
514f2b9848SAndre Przywara ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
524f2b9848SAndre Przywara ((type) << PSTATE_TYPE_SHIFT))
534f2b9848SAndre Przywara
544f2b9848SAndre Przywara #endif /* PSCI_EXTENDED_STATE_ID */
554f2b9848SAndre Przywara
564f2b9848SAndre Przywara #define rpi3_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
574f2b9848SAndre Przywara (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \
584f2b9848SAndre Przywara rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
594f2b9848SAndre Przywara
604f2b9848SAndre Przywara /*
614f2b9848SAndre Przywara * The table storing the valid idle power states. Ensure that the
624f2b9848SAndre Przywara * array entries are populated in ascending order of state-id to
634f2b9848SAndre Przywara * enable us to use binary search during power state validation.
644f2b9848SAndre Przywara * The table must be terminated by a NULL entry.
654f2b9848SAndre Przywara */
664f2b9848SAndre Przywara static const unsigned int rpi3_pm_idle_states[] = {
674f2b9848SAndre Przywara /* State-id - 0x01 */
684f2b9848SAndre Przywara rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET,
694f2b9848SAndre Przywara MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
704f2b9848SAndre Przywara /* State-id - 0x02 */
714f2b9848SAndre Przywara rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF,
724f2b9848SAndre Przywara MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
734f2b9848SAndre Przywara /* State-id - 0x22 */
744f2b9848SAndre Przywara rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF,
754f2b9848SAndre Przywara MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN),
764f2b9848SAndre Przywara 0,
774f2b9848SAndre Przywara };
784f2b9848SAndre Przywara
794f2b9848SAndre Przywara /*******************************************************************************
804f2b9848SAndre Przywara * Platform handler called to check the validity of the power state
814f2b9848SAndre Przywara * parameter. The power state parameter has to be a composite power state.
824f2b9848SAndre Przywara ******************************************************************************/
rpi3_validate_power_state(unsigned int power_state,psci_power_state_t * req_state)834f2b9848SAndre Przywara static int rpi3_validate_power_state(unsigned int power_state,
844f2b9848SAndre Przywara psci_power_state_t *req_state)
854f2b9848SAndre Przywara {
864f2b9848SAndre Przywara unsigned int state_id;
874f2b9848SAndre Przywara int i;
884f2b9848SAndre Przywara
894f2b9848SAndre Przywara assert(req_state != 0);
904f2b9848SAndre Przywara
914f2b9848SAndre Przywara /*
924f2b9848SAndre Przywara * Currently we are using a linear search for finding the matching
934f2b9848SAndre Przywara * entry in the idle power state array. This can be made a binary
944f2b9848SAndre Przywara * search if the number of entries justify the additional complexity.
954f2b9848SAndre Przywara */
964f2b9848SAndre Przywara for (i = 0; rpi3_pm_idle_states[i] != 0; i++) {
974f2b9848SAndre Przywara if (power_state == rpi3_pm_idle_states[i]) {
984f2b9848SAndre Przywara break;
994f2b9848SAndre Przywara }
1004f2b9848SAndre Przywara }
1014f2b9848SAndre Przywara
1024f2b9848SAndre Przywara /* Return error if entry not found in the idle state array */
1034f2b9848SAndre Przywara if (!rpi3_pm_idle_states[i]) {
1044f2b9848SAndre Przywara return PSCI_E_INVALID_PARAMS;
1054f2b9848SAndre Przywara }
1064f2b9848SAndre Przywara
1074f2b9848SAndre Przywara i = 0;
1084f2b9848SAndre Przywara state_id = psci_get_pstate_id(power_state);
1094f2b9848SAndre Przywara
1104f2b9848SAndre Przywara /* Parse the State ID and populate the state info parameter */
1114f2b9848SAndre Przywara while (state_id) {
1124f2b9848SAndre Przywara req_state->pwr_domain_state[i++] = state_id &
1134f2b9848SAndre Przywara PLAT_LOCAL_PSTATE_MASK;
1144f2b9848SAndre Przywara state_id >>= PLAT_LOCAL_PSTATE_WIDTH;
1154f2b9848SAndre Przywara }
1164f2b9848SAndre Przywara
1174f2b9848SAndre Przywara return PSCI_E_SUCCESS;
1184f2b9848SAndre Przywara }
1194f2b9848SAndre Przywara
1204f2b9848SAndre Przywara /*******************************************************************************
1214f2b9848SAndre Przywara * Platform handler called when a CPU is about to enter standby.
1224f2b9848SAndre Przywara ******************************************************************************/
rpi3_cpu_standby(plat_local_state_t cpu_state)1234f2b9848SAndre Przywara static void rpi3_cpu_standby(plat_local_state_t cpu_state)
1244f2b9848SAndre Przywara {
1254f2b9848SAndre Przywara assert(cpu_state == PLAT_LOCAL_STATE_RET);
1264f2b9848SAndre Przywara
1274f2b9848SAndre Przywara /*
1284f2b9848SAndre Przywara * Enter standby state.
1294f2b9848SAndre Przywara * dsb is good practice before using wfi to enter low power states
1304f2b9848SAndre Przywara */
1314f2b9848SAndre Przywara dsb();
1324f2b9848SAndre Przywara wfi();
1334f2b9848SAndre Przywara }
1344f2b9848SAndre Przywara
rpi3_pwr_domain_off(const psci_power_state_t * target_state)135e6fd00abSAndre Przywara static void rpi3_pwr_domain_off(const psci_power_state_t *target_state)
136e6fd00abSAndre Przywara {
137e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC
138e6fd00abSAndre Przywara gicv2_cpuif_disable();
139e6fd00abSAndre Przywara #endif
140e6fd00abSAndre Przywara }
141e6fd00abSAndre Przywara
1424f2b9848SAndre Przywara /*******************************************************************************
1434f2b9848SAndre Przywara * Platform handler called when a power domain is about to be turned on. The
1444f2b9848SAndre Przywara * mpidr determines the CPU to be turned on.
1454f2b9848SAndre Przywara ******************************************************************************/
rpi3_pwr_domain_on(u_register_t mpidr)1464f2b9848SAndre Przywara static int rpi3_pwr_domain_on(u_register_t mpidr)
1474f2b9848SAndre Przywara {
1484f2b9848SAndre Przywara int rc = PSCI_E_SUCCESS;
1494f2b9848SAndre Przywara unsigned int pos = plat_core_pos_by_mpidr(mpidr);
150af2a4877SAndre Przywara uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE;
1514f2b9848SAndre Przywara
1524f2b9848SAndre Przywara assert(pos < PLATFORM_CORE_COUNT);
1534f2b9848SAndre Przywara
154af2a4877SAndre Przywara hold_base += pos * PLAT_RPI3_TM_HOLD_ENTRY_SIZE;
155af2a4877SAndre Przywara
156af2a4877SAndre Przywara mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_GO);
157af2a4877SAndre Przywara /* No cache maintenance here, hold_base is mapped as device memory. */
1584f2b9848SAndre Przywara
1594f2b9848SAndre Przywara /* Make sure that the write has completed */
1604f2b9848SAndre Przywara dsb();
1614f2b9848SAndre Przywara isb();
1624f2b9848SAndre Przywara
1634f2b9848SAndre Przywara sev();
1644f2b9848SAndre Przywara
1654f2b9848SAndre Przywara return rc;
1664f2b9848SAndre Przywara }
1674f2b9848SAndre Przywara
1684f2b9848SAndre Przywara /*******************************************************************************
1694f2b9848SAndre Przywara * Platform handler called when a power domain has just been powered on after
1704f2b9848SAndre Przywara * being turned off earlier. The target_state encodes the low power state that
1714f2b9848SAndre Przywara * each level has woken up from.
1724f2b9848SAndre Przywara ******************************************************************************/
rpi3_pwr_domain_on_finish(const psci_power_state_t * target_state)1734f2b9848SAndre Przywara static void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state)
1744f2b9848SAndre Przywara {
1754f2b9848SAndre Przywara assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
1764f2b9848SAndre Przywara PLAT_LOCAL_STATE_OFF);
177e6fd00abSAndre Przywara
178e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC
179e6fd00abSAndre Przywara gicv2_pcpu_distif_init();
180e6fd00abSAndre Przywara gicv2_cpuif_enable();
181e6fd00abSAndre Przywara #endif
1824f2b9848SAndre Przywara }
1834f2b9848SAndre Przywara
rpi3_pwr_down_wfi(const psci_power_state_t * target_state)1842e5f8443SAndrei Warkentin static void __dead2 rpi3_pwr_down_wfi(
1852e5f8443SAndrei Warkentin const psci_power_state_t *target_state)
1862e5f8443SAndrei Warkentin {
1872e5f8443SAndrei Warkentin uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE;
1882e5f8443SAndrei Warkentin unsigned int pos = plat_my_core_pos();
1892e5f8443SAndrei Warkentin
1902e5f8443SAndrei Warkentin if (pos == 0) {
1912e5f8443SAndrei Warkentin /*
1922e5f8443SAndrei Warkentin * The secondaries will always be in a wait
1932e5f8443SAndrei Warkentin * for warm boot on reset, but the BSP needs
1942e5f8443SAndrei Warkentin * to be able to distinguish between waiting
1952e5f8443SAndrei Warkentin * for warm boot (e.g. after psci_off, waiting
1962e5f8443SAndrei Warkentin * for psci_on) and a cold boot.
1972e5f8443SAndrei Warkentin */
1982e5f8443SAndrei Warkentin mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_BSP_OFF);
1992e5f8443SAndrei Warkentin /* No cache maintenance here, we run with caches off already. */
2002e5f8443SAndrei Warkentin dsb();
2012e5f8443SAndrei Warkentin isb();
2022e5f8443SAndrei Warkentin }
2032e5f8443SAndrei Warkentin
2042e5f8443SAndrei Warkentin write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT);
2052e5f8443SAndrei Warkentin
206028c4e42SBoyan Karatotev while (1) {
207028c4e42SBoyan Karatotev wfi();
208028c4e42SBoyan Karatotev }
2092e5f8443SAndrei Warkentin }
2102e5f8443SAndrei Warkentin
2114f2b9848SAndre Przywara /*******************************************************************************
2124f2b9848SAndre Przywara * Platform handlers for system reset and system off.
2134f2b9848SAndre Przywara ******************************************************************************/
2144f2b9848SAndre Przywara
2154f2b9848SAndre Przywara /* 10 ticks (Watchdog timer = Timer clock / 16) */
2164f2b9848SAndre Przywara #define RESET_TIMEOUT U(10)
2174f2b9848SAndre Przywara
rpi3_watchdog_reset(void)2184f2b9848SAndre Przywara static void __dead2 rpi3_watchdog_reset(void)
2194f2b9848SAndre Przywara {
2204f2b9848SAndre Przywara uint32_t rstc;
2214f2b9848SAndre Przywara
2224f2b9848SAndre Przywara console_flush();
2234f2b9848SAndre Przywara
2244f2b9848SAndre Przywara dsbsy();
2254f2b9848SAndre Przywara isb();
2264f2b9848SAndre Przywara
2274f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_WDOG_OFFSET,
2284f2b9848SAndre Przywara RPI3_PM_PASSWORD | RESET_TIMEOUT);
2294f2b9848SAndre Przywara
2304f2b9848SAndre Przywara rstc = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET);
2314f2b9848SAndre Przywara rstc &= ~RPI3_PM_RSTC_WRCFG_MASK;
2324f2b9848SAndre Przywara rstc |= RPI3_PM_PASSWORD | RPI3_PM_RSTC_WRCFG_FULL_RESET;
2334f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET, rstc);
2344f2b9848SAndre Przywara
2354f2b9848SAndre Przywara for (;;) {
2364f2b9848SAndre Przywara wfi();
2374f2b9848SAndre Przywara }
2384f2b9848SAndre Przywara }
2394f2b9848SAndre Przywara
rpi3_system_reset(void)2404f2b9848SAndre Przywara static void __dead2 rpi3_system_reset(void)
2414f2b9848SAndre Przywara {
2424f2b9848SAndre Przywara INFO("rpi3: PSCI_SYSTEM_RESET: Invoking watchdog reset\n");
2434f2b9848SAndre Przywara
2444f2b9848SAndre Przywara rpi3_watchdog_reset();
2454f2b9848SAndre Przywara }
2464f2b9848SAndre Przywara
rpi3_system_off(void)2474f2b9848SAndre Przywara static void __dead2 rpi3_system_off(void)
2484f2b9848SAndre Przywara {
2494f2b9848SAndre Przywara uint32_t rsts;
2504f2b9848SAndre Przywara
2514f2b9848SAndre Przywara INFO("rpi3: PSCI_SYSTEM_OFF: Invoking watchdog reset\n");
2524f2b9848SAndre Przywara
2534f2b9848SAndre Przywara /*
2544f2b9848SAndre Przywara * This function doesn't actually make the Raspberry Pi turn itself off,
2554f2b9848SAndre Przywara * the hardware doesn't allow it. It simply reboots it and the RSTS
2564f2b9848SAndre Przywara * value tells the bootcode.bin firmware not to continue the regular
2574f2b9848SAndre Przywara * bootflow and to stay in a low power mode.
2584f2b9848SAndre Przywara */
2594f2b9848SAndre Przywara
2604f2b9848SAndre Przywara rsts = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET);
2614f2b9848SAndre Przywara rsts |= RPI3_PM_PASSWORD | RPI3_PM_RSTS_WRCFG_HALT;
2624f2b9848SAndre Przywara mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET, rsts);
2634f2b9848SAndre Przywara
2644f2b9848SAndre Przywara rpi3_watchdog_reset();
2654f2b9848SAndre Przywara }
2664f2b9848SAndre Przywara
2674f2b9848SAndre Przywara /*******************************************************************************
2684f2b9848SAndre Przywara * Platform handlers and setup function.
2694f2b9848SAndre Przywara ******************************************************************************/
2704f2b9848SAndre Przywara static const plat_psci_ops_t plat_rpi3_psci_pm_ops = {
2714f2b9848SAndre Przywara .cpu_standby = rpi3_cpu_standby,
272e6fd00abSAndre Przywara .pwr_domain_off = rpi3_pwr_domain_off,
2734f2b9848SAndre Przywara .pwr_domain_on = rpi3_pwr_domain_on,
2744f2b9848SAndre Przywara .pwr_domain_on_finish = rpi3_pwr_domain_on_finish,
275*db5fe4f4SBoyan Karatotev .pwr_domain_pwr_down = rpi3_pwr_down_wfi,
2764f2b9848SAndre Przywara .system_off = rpi3_system_off,
2774f2b9848SAndre Przywara .system_reset = rpi3_system_reset,
2784f2b9848SAndre Przywara .validate_power_state = rpi3_validate_power_state,
2794f2b9848SAndre Przywara };
2804f2b9848SAndre Przywara
plat_setup_psci_ops(uintptr_t sec_entrypoint,const plat_psci_ops_t ** psci_ops)2814f2b9848SAndre Przywara int plat_setup_psci_ops(uintptr_t sec_entrypoint,
2824f2b9848SAndre Przywara const plat_psci_ops_t **psci_ops)
2834f2b9848SAndre Przywara {
2844f2b9848SAndre Przywara uintptr_t *entrypoint = (void *) PLAT_RPI3_TM_ENTRYPOINT;
2854f2b9848SAndre Przywara
2864f2b9848SAndre Przywara *entrypoint = sec_entrypoint;
2874f2b9848SAndre Przywara *psci_ops = &plat_rpi3_psci_pm_ops;
2884f2b9848SAndre Przywara
2894f2b9848SAndre Przywara return 0;
2904f2b9848SAndre Przywara }
291