xref: /rk3399_ARM-atf/plat/rpi/common/rpi3_pm.c (revision 028c4e42d8f632d40081b88f66d0d05c7d7c9b23)
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 
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);
134af2a4877SAndre Przywara 	uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE;
1354f2b9848SAndre Przywara 
1364f2b9848SAndre Przywara 	assert(pos < PLATFORM_CORE_COUNT);
1374f2b9848SAndre Przywara 
138af2a4877SAndre Przywara 	hold_base += pos * PLAT_RPI3_TM_HOLD_ENTRY_SIZE;
139af2a4877SAndre Przywara 
140af2a4877SAndre Przywara 	mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_GO);
141af2a4877SAndre Przywara 	/* No cache maintenance here, hold_base is mapped as device memory. */
1424f2b9848SAndre Przywara 
1434f2b9848SAndre Przywara 	/* Make sure that the write has completed */
1444f2b9848SAndre Przywara 	dsb();
1454f2b9848SAndre Przywara 	isb();
1464f2b9848SAndre Przywara 
1474f2b9848SAndre Przywara 	sev();
1484f2b9848SAndre Przywara 
1494f2b9848SAndre Przywara 	return rc;
1504f2b9848SAndre Przywara }
1514f2b9848SAndre Przywara 
1524f2b9848SAndre Przywara /*******************************************************************************
1534f2b9848SAndre Przywara  * Platform handler called when a power domain has just been powered on after
1544f2b9848SAndre Przywara  * being turned off earlier. The target_state encodes the low power state that
1554f2b9848SAndre Przywara  * each level has woken up from.
1564f2b9848SAndre Przywara  ******************************************************************************/
1574f2b9848SAndre Przywara static void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state)
1584f2b9848SAndre Przywara {
1594f2b9848SAndre Przywara 	assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
1604f2b9848SAndre Przywara 					PLAT_LOCAL_STATE_OFF);
161e6fd00abSAndre Przywara 
162e6fd00abSAndre Przywara #ifdef RPI_HAVE_GIC
163e6fd00abSAndre Przywara 	gicv2_pcpu_distif_init();
164e6fd00abSAndre Przywara 	gicv2_cpuif_enable();
165e6fd00abSAndre Przywara #endif
1664f2b9848SAndre Przywara }
1674f2b9848SAndre Przywara 
1682e5f8443SAndrei Warkentin static void __dead2 rpi3_pwr_down_wfi(
1692e5f8443SAndrei Warkentin 		const psci_power_state_t *target_state)
1702e5f8443SAndrei Warkentin {
1712e5f8443SAndrei Warkentin 	uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE;
1722e5f8443SAndrei Warkentin 	unsigned int pos = plat_my_core_pos();
1732e5f8443SAndrei Warkentin 
1742e5f8443SAndrei Warkentin 	if (pos == 0) {
1752e5f8443SAndrei Warkentin 		/*
1762e5f8443SAndrei Warkentin 		 * The secondaries will always be in a wait
1772e5f8443SAndrei Warkentin 		 * for warm boot on reset, but the BSP needs
1782e5f8443SAndrei Warkentin 		 * to be able to distinguish between waiting
1792e5f8443SAndrei Warkentin 		 * for warm boot (e.g. after psci_off, waiting
1802e5f8443SAndrei Warkentin 		 * for psci_on) and a cold boot.
1812e5f8443SAndrei Warkentin 		 */
1822e5f8443SAndrei Warkentin 		mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_BSP_OFF);
1832e5f8443SAndrei Warkentin 		/* No cache maintenance here, we run with caches off already. */
1842e5f8443SAndrei Warkentin 		dsb();
1852e5f8443SAndrei Warkentin 		isb();
1862e5f8443SAndrei Warkentin 	}
1872e5f8443SAndrei Warkentin 
1882e5f8443SAndrei Warkentin 	write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT);
1892e5f8443SAndrei Warkentin 
190*028c4e42SBoyan Karatotev 	while (1) {
191*028c4e42SBoyan Karatotev 		wfi();
192*028c4e42SBoyan Karatotev 	}
1932e5f8443SAndrei Warkentin }
1942e5f8443SAndrei Warkentin 
1954f2b9848SAndre Przywara /*******************************************************************************
1964f2b9848SAndre Przywara  * Platform handlers for system reset and system off.
1974f2b9848SAndre Przywara  ******************************************************************************/
1984f2b9848SAndre Przywara 
1994f2b9848SAndre Przywara /* 10 ticks (Watchdog timer = Timer clock / 16) */
2004f2b9848SAndre Przywara #define RESET_TIMEOUT	U(10)
2014f2b9848SAndre Przywara 
2024f2b9848SAndre Przywara static void __dead2 rpi3_watchdog_reset(void)
2034f2b9848SAndre Przywara {
2044f2b9848SAndre Przywara 	uint32_t rstc;
2054f2b9848SAndre Przywara 
2064f2b9848SAndre Przywara 	console_flush();
2074f2b9848SAndre Przywara 
2084f2b9848SAndre Przywara 	dsbsy();
2094f2b9848SAndre Przywara 	isb();
2104f2b9848SAndre Przywara 
2114f2b9848SAndre Przywara 	mmio_write_32(RPI3_PM_BASE + RPI3_PM_WDOG_OFFSET,
2124f2b9848SAndre Przywara 		      RPI3_PM_PASSWORD | RESET_TIMEOUT);
2134f2b9848SAndre Przywara 
2144f2b9848SAndre Przywara 	rstc = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET);
2154f2b9848SAndre Przywara 	rstc &= ~RPI3_PM_RSTC_WRCFG_MASK;
2164f2b9848SAndre Przywara 	rstc |= RPI3_PM_PASSWORD | RPI3_PM_RSTC_WRCFG_FULL_RESET;
2174f2b9848SAndre Przywara 	mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET, rstc);
2184f2b9848SAndre Przywara 
2194f2b9848SAndre Przywara 	for (;;) {
2204f2b9848SAndre Przywara 		wfi();
2214f2b9848SAndre Przywara 	}
2224f2b9848SAndre Przywara }
2234f2b9848SAndre Przywara 
2244f2b9848SAndre Przywara static void __dead2 rpi3_system_reset(void)
2254f2b9848SAndre Przywara {
2264f2b9848SAndre Przywara 	INFO("rpi3: PSCI_SYSTEM_RESET: Invoking watchdog reset\n");
2274f2b9848SAndre Przywara 
2284f2b9848SAndre Przywara 	rpi3_watchdog_reset();
2294f2b9848SAndre Przywara }
2304f2b9848SAndre Przywara 
2314f2b9848SAndre Przywara static void __dead2 rpi3_system_off(void)
2324f2b9848SAndre Przywara {
2334f2b9848SAndre Przywara 	uint32_t rsts;
2344f2b9848SAndre Przywara 
2354f2b9848SAndre Przywara 	INFO("rpi3: PSCI_SYSTEM_OFF: Invoking watchdog reset\n");
2364f2b9848SAndre Przywara 
2374f2b9848SAndre Przywara 	/*
2384f2b9848SAndre Przywara 	 * This function doesn't actually make the Raspberry Pi turn itself off,
2394f2b9848SAndre Przywara 	 * the hardware doesn't allow it. It simply reboots it and the RSTS
2404f2b9848SAndre Przywara 	 * value tells the bootcode.bin firmware not to continue the regular
2414f2b9848SAndre Przywara 	 * bootflow and to stay in a low power mode.
2424f2b9848SAndre Przywara 	 */
2434f2b9848SAndre Przywara 
2444f2b9848SAndre Przywara 	rsts = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET);
2454f2b9848SAndre Przywara 	rsts |= RPI3_PM_PASSWORD | RPI3_PM_RSTS_WRCFG_HALT;
2464f2b9848SAndre Przywara 	mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET, rsts);
2474f2b9848SAndre Przywara 
2484f2b9848SAndre Przywara 	rpi3_watchdog_reset();
2494f2b9848SAndre Przywara }
2504f2b9848SAndre Przywara 
2514f2b9848SAndre Przywara /*******************************************************************************
2524f2b9848SAndre Przywara  * Platform handlers and setup function.
2534f2b9848SAndre Przywara  ******************************************************************************/
2544f2b9848SAndre Przywara static const plat_psci_ops_t plat_rpi3_psci_pm_ops = {
2554f2b9848SAndre Przywara 	.cpu_standby = rpi3_cpu_standby,
256e6fd00abSAndre Przywara 	.pwr_domain_off = rpi3_pwr_domain_off,
2574f2b9848SAndre Przywara 	.pwr_domain_on = rpi3_pwr_domain_on,
2584f2b9848SAndre Przywara 	.pwr_domain_on_finish = rpi3_pwr_domain_on_finish,
2592e5f8443SAndrei Warkentin 	.pwr_domain_pwr_down_wfi = rpi3_pwr_down_wfi,
2604f2b9848SAndre Przywara 	.system_off = rpi3_system_off,
2614f2b9848SAndre Przywara 	.system_reset = rpi3_system_reset,
2624f2b9848SAndre Przywara 	.validate_power_state = rpi3_validate_power_state,
2634f2b9848SAndre Przywara };
2644f2b9848SAndre Przywara 
2654f2b9848SAndre Przywara int plat_setup_psci_ops(uintptr_t sec_entrypoint,
2664f2b9848SAndre Przywara 			const plat_psci_ops_t **psci_ops)
2674f2b9848SAndre Przywara {
2684f2b9848SAndre Przywara 	uintptr_t *entrypoint = (void *) PLAT_RPI3_TM_ENTRYPOINT;
2694f2b9848SAndre Przywara 
2704f2b9848SAndre Przywara 	*entrypoint = sec_entrypoint;
2714f2b9848SAndre Przywara 	*psci_ops = &plat_rpi3_psci_pm_ops;
2724f2b9848SAndre Przywara 
2734f2b9848SAndre Przywara 	return 0;
2744f2b9848SAndre Przywara }
275