xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_pm.c (revision 814dce8f96fdb82d095b0041a204ba4a272c0913)
158032586SSamuel Holland /*
2*814dce8fSSamuel Holland  * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
358032586SSamuel Holland  *
458032586SSamuel Holland  * SPDX-License-Identifier: BSD-3-Clause
558032586SSamuel Holland  */
658032586SSamuel Holland 
758032586SSamuel Holland #include <assert.h>
809d40e0eSAntonio Nino Diaz 
958032586SSamuel Holland #include <platform_def.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1209d40e0eSAntonio Nino Diaz #include <common/debug.h>
13e382c88eSSamuel Holland #include <drivers/arm/css/css_scpi.h>
1409d40e0eSAntonio Nino Diaz #include <drivers/arm/gicv2.h>
1509d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
1609d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1709d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
1809d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1909d40e0eSAntonio Nino Diaz 
20560581ecSSamuel Holland #include <sunxi_cpucfg.h>
21e382c88eSSamuel Holland #include <sunxi_def.h>
224ec1a239SAndre Przywara #include <sunxi_mmap.h>
234ec1a239SAndre Przywara #include <sunxi_private.h>
2458032586SSamuel Holland 
25523ab5beSClément Péron #define SUNXI_WDOG0_CTRL_REG		(SUNXI_R_WDOG_BASE + 0x0010)
26523ab5beSClément Péron #define SUNXI_WDOG0_CFG_REG		(SUNXI_R_WDOG_BASE + 0x0014)
27523ab5beSClément Péron #define SUNXI_WDOG0_MODE_REG		(SUNXI_R_WDOG_BASE + 0x0018)
2858032586SSamuel Holland 
29e382c88eSSamuel Holland #define CPU_PWR_LVL			MPIDR_AFFLVL0
30e382c88eSSamuel Holland #define CLUSTER_PWR_LVL			MPIDR_AFFLVL1
31e382c88eSSamuel Holland #define SYSTEM_PWR_LVL			MPIDR_AFFLVL2
32e382c88eSSamuel Holland 
33e382c88eSSamuel Holland #define CPU_PWR_STATE(state) \
34e382c88eSSamuel Holland 	((state)->pwr_domain_state[CPU_PWR_LVL])
35e382c88eSSamuel Holland #define CLUSTER_PWR_STATE(state) \
36e382c88eSSamuel Holland 	((state)->pwr_domain_state[CLUSTER_PWR_LVL])
37e382c88eSSamuel Holland #define SYSTEM_PWR_STATE(state) \
38e382c88eSSamuel Holland 	((state)->pwr_domain_state[SYSTEM_PWR_LVL])
39e382c88eSSamuel Holland 
40e382c88eSSamuel Holland /*
41e382c88eSSamuel Holland  * The addresses for the SCP exception vectors are defined in the or1k
42e382c88eSSamuel Holland  * architecture specification.
43e382c88eSSamuel Holland  */
44e382c88eSSamuel Holland #define OR1K_VEC_FIRST			0x01
45e382c88eSSamuel Holland #define OR1K_VEC_LAST			0x0e
46e382c88eSSamuel Holland #define OR1K_VEC_ADDR(n)		(0x100 * (n))
47e382c88eSSamuel Holland 
48e382c88eSSamuel Holland /*
49e382c88eSSamuel Holland  * This magic value is the little-endian representation of the or1k
50e382c88eSSamuel Holland  * instruction "l.mfspr r2, r0, 0x12", which is guaranteed to be the
51e382c88eSSamuel Holland  * first instruction in the SCP firmware.
52e382c88eSSamuel Holland  */
53e382c88eSSamuel Holland #define SCP_FIRMWARE_MAGIC		0xb4400012
54e382c88eSSamuel Holland 
55e382c88eSSamuel Holland static bool scpi_available;
56e382c88eSSamuel Holland 
57e382c88eSSamuel Holland static inline scpi_power_state_t scpi_map_state(plat_local_state_t psci_state)
58e382c88eSSamuel Holland {
59e382c88eSSamuel Holland 	if (is_local_state_run(psci_state))
60e382c88eSSamuel Holland 		return scpi_power_on;
61e382c88eSSamuel Holland 	if (is_local_state_retn(psci_state))
62e382c88eSSamuel Holland 		return scpi_power_retention;
63e382c88eSSamuel Holland 	return scpi_power_off;
64e382c88eSSamuel Holland }
65e382c88eSSamuel Holland 
66e382c88eSSamuel Holland static void sunxi_cpu_standby(plat_local_state_t cpu_state)
67e382c88eSSamuel Holland {
68e382c88eSSamuel Holland 	u_register_t scr = read_scr_el3();
69e382c88eSSamuel Holland 
70e382c88eSSamuel Holland 	assert(is_local_state_retn(cpu_state));
71e382c88eSSamuel Holland 
72e382c88eSSamuel Holland 	write_scr_el3(scr | SCR_IRQ_BIT);
73e382c88eSSamuel Holland 	wfi();
74e382c88eSSamuel Holland 	write_scr_el3(scr);
75e382c88eSSamuel Holland }
76560581ecSSamuel Holland 
77560581ecSSamuel Holland static int sunxi_pwr_domain_on(u_register_t mpidr)
78560581ecSSamuel Holland {
79e382c88eSSamuel Holland 	if (scpi_available) {
80e382c88eSSamuel Holland 		scpi_set_css_power_state(mpidr,
81e382c88eSSamuel Holland 					 scpi_power_on,
82e382c88eSSamuel Holland 					 scpi_power_on,
83e382c88eSSamuel Holland 					 scpi_power_on);
84e382c88eSSamuel Holland 	} else {
855d4bd66dSSamuel Holland 		sunxi_cpu_on(mpidr);
86e382c88eSSamuel Holland 	}
87560581ecSSamuel Holland 
88560581ecSSamuel Holland 	return PSCI_E_SUCCESS;
89560581ecSSamuel Holland }
90560581ecSSamuel Holland 
91560581ecSSamuel Holland static void sunxi_pwr_domain_off(const psci_power_state_t *target_state)
92560581ecSSamuel Holland {
93e382c88eSSamuel Holland 	plat_local_state_t cpu_pwr_state     = CPU_PWR_STATE(target_state);
94e382c88eSSamuel Holland 	plat_local_state_t cluster_pwr_state = CLUSTER_PWR_STATE(target_state);
95e382c88eSSamuel Holland 	plat_local_state_t system_pwr_state  = SYSTEM_PWR_STATE(target_state);
96e382c88eSSamuel Holland 
97e382c88eSSamuel Holland 	if (is_local_state_off(cpu_pwr_state))
98560581ecSSamuel Holland 		gicv2_cpuif_disable();
99e382c88eSSamuel Holland 
100e382c88eSSamuel Holland 	if (scpi_available) {
101e382c88eSSamuel Holland 		scpi_set_css_power_state(read_mpidr(),
102e382c88eSSamuel Holland 					 scpi_map_state(cpu_pwr_state),
103e382c88eSSamuel Holland 					 scpi_map_state(cluster_pwr_state),
104e382c88eSSamuel Holland 					 scpi_map_state(system_pwr_state));
105e382c88eSSamuel Holland 	}
106560581ecSSamuel Holland }
107560581ecSSamuel Holland 
1087db0c960SAndre Przywara static void __dead2 sunxi_pwr_down_wfi(const psci_power_state_t *target_state)
1097db0c960SAndre Przywara {
1105d4bd66dSSamuel Holland 	sunxi_cpu_off(read_mpidr());
1117db0c960SAndre Przywara 
1127db0c960SAndre Przywara 	while (1)
1137db0c960SAndre Przywara 		wfi();
1147db0c960SAndre Przywara }
1157db0c960SAndre Przywara 
116560581ecSSamuel Holland static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state)
117560581ecSSamuel Holland {
118e382c88eSSamuel Holland 	if (is_local_state_off(SYSTEM_PWR_STATE(target_state)))
119e382c88eSSamuel Holland 		gicv2_distif_init();
120e382c88eSSamuel Holland 	if (is_local_state_off(CPU_PWR_STATE(target_state))) {
121560581ecSSamuel Holland 		gicv2_pcpu_distif_init();
122560581ecSSamuel Holland 		gicv2_cpuif_enable();
123560581ecSSamuel Holland 	}
124e382c88eSSamuel Holland }
125560581ecSSamuel Holland 
12658032586SSamuel Holland static void __dead2 sunxi_system_off(void)
12758032586SSamuel Holland {
128e382c88eSSamuel Holland 	gicv2_cpuif_disable();
129e382c88eSSamuel Holland 
130e382c88eSSamuel Holland 	if (scpi_available) {
131e382c88eSSamuel Holland 		/* Send the power down request to the SCP */
132e382c88eSSamuel Holland 		uint32_t ret = scpi_sys_power_state(scpi_system_shutdown);
133e382c88eSSamuel Holland 
134e382c88eSSamuel Holland 		if (ret != SCP_OK)
135e382c88eSSamuel Holland 			ERROR("PSCI: SCPI %s failed: %d\n", "shutdown", ret);
136e382c88eSSamuel Holland 	}
137e382c88eSSamuel Holland 
138333d66cfSSamuel Holland 	/* Turn off all secondary CPUs */
1395d4bd66dSSamuel Holland 	sunxi_disable_secondary_cpus(read_mpidr());
140333d66cfSSamuel Holland 
1415069c1cfSIcenowy Zheng 	sunxi_power_down();
142818e6732SSamuel Holland 
143818e6732SSamuel Holland 	udelay(1000);
144818e6732SSamuel Holland 	ERROR("PSCI: Cannot turn off system, halting\n");
145818e6732SSamuel Holland 	wfi();
146818e6732SSamuel Holland 	panic();
14758032586SSamuel Holland }
14858032586SSamuel Holland 
14958032586SSamuel Holland static void __dead2 sunxi_system_reset(void)
15058032586SSamuel Holland {
151e382c88eSSamuel Holland 	gicv2_cpuif_disable();
152e382c88eSSamuel Holland 
153e382c88eSSamuel Holland 	if (scpi_available) {
154e382c88eSSamuel Holland 		/* Send the system reset request to the SCP */
155e382c88eSSamuel Holland 		uint32_t ret = scpi_sys_power_state(scpi_system_reboot);
156e382c88eSSamuel Holland 
157e382c88eSSamuel Holland 		if (ret != SCP_OK)
158e382c88eSSamuel Holland 			ERROR("PSCI: SCPI %s failed: %d\n", "reboot", ret);
159e382c88eSSamuel Holland 	}
160e382c88eSSamuel Holland 
16158032586SSamuel Holland 	/* Reset the whole system when the watchdog times out */
16258032586SSamuel Holland 	mmio_write_32(SUNXI_WDOG0_CFG_REG, 1);
16358032586SSamuel Holland 	/* Enable the watchdog with the shortest timeout (0.5 seconds) */
16458032586SSamuel Holland 	mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1);
16558032586SSamuel Holland 	/* Wait for twice the watchdog timeout before panicking */
16658032586SSamuel Holland 	mdelay(1000);
16758032586SSamuel Holland 
16858032586SSamuel Holland 	ERROR("PSCI: System reset failed\n");
16958032586SSamuel Holland 	wfi();
17058032586SSamuel Holland 	panic();
17158032586SSamuel Holland }
17258032586SSamuel Holland 
173e382c88eSSamuel Holland static int sunxi_validate_power_state(unsigned int power_state,
174e382c88eSSamuel Holland 				      psci_power_state_t *req_state)
175e382c88eSSamuel Holland {
176e382c88eSSamuel Holland 	unsigned int power_level = psci_get_pstate_pwrlvl(power_state);
177e382c88eSSamuel Holland 	unsigned int type = psci_get_pstate_type(power_state);
178e382c88eSSamuel Holland 
179e382c88eSSamuel Holland 	assert(req_state != NULL);
180e382c88eSSamuel Holland 
181e382c88eSSamuel Holland 	if (power_level > PLAT_MAX_PWR_LVL)
182e382c88eSSamuel Holland 		return PSCI_E_INVALID_PARAMS;
183e382c88eSSamuel Holland 
184e382c88eSSamuel Holland 	if (type == PSTATE_TYPE_STANDBY) {
185e382c88eSSamuel Holland 		/* Only one retention power state is supported. */
186e382c88eSSamuel Holland 		if (psci_get_pstate_id(power_state) > 0)
187e382c88eSSamuel Holland 			return PSCI_E_INVALID_PARAMS;
188e382c88eSSamuel Holland 		/* The SoC cannot be suspended without losing state */
189e382c88eSSamuel Holland 		if (power_level == SYSTEM_PWR_LVL)
190e382c88eSSamuel Holland 			return PSCI_E_INVALID_PARAMS;
191e382c88eSSamuel Holland 		for (unsigned int i = 0; i <= power_level; ++i)
192e382c88eSSamuel Holland 			req_state->pwr_domain_state[i] = PLAT_MAX_RET_STATE;
193e382c88eSSamuel Holland 	} else {
194e382c88eSSamuel Holland 		/* Only one off power state is supported. */
195e382c88eSSamuel Holland 		if (psci_get_pstate_id(power_state) > 0)
196e382c88eSSamuel Holland 			return PSCI_E_INVALID_PARAMS;
197e382c88eSSamuel Holland 		for (unsigned int i = 0; i <= power_level; ++i)
198e382c88eSSamuel Holland 			req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
199e382c88eSSamuel Holland 	}
200e382c88eSSamuel Holland 	/* Higher power domain levels should all remain running */
201e382c88eSSamuel Holland 	for (unsigned int i = power_level + 1; i <= PLAT_MAX_PWR_LVL; ++i)
202e382c88eSSamuel Holland 		req_state->pwr_domain_state[i] = PSCI_LOCAL_STATE_RUN;
203e382c88eSSamuel Holland 
204e382c88eSSamuel Holland 	return PSCI_E_SUCCESS;
205e382c88eSSamuel Holland }
206e382c88eSSamuel Holland 
207560581ecSSamuel Holland static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint)
208560581ecSSamuel Holland {
209560581ecSSamuel Holland 	/* The non-secure entry point must be in DRAM */
210*814dce8fSSamuel Holland 	if (ns_entrypoint < SUNXI_DRAM_BASE) {
211560581ecSSamuel Holland 		return PSCI_E_INVALID_ADDRESS;
212560581ecSSamuel Holland 	}
213560581ecSSamuel Holland 
214*814dce8fSSamuel Holland 	return PSCI_E_SUCCESS;
215*814dce8fSSamuel Holland }
216*814dce8fSSamuel Holland 
217e382c88eSSamuel Holland static void sunxi_get_sys_suspend_power_state(psci_power_state_t *req_state)
218e382c88eSSamuel Holland {
219e382c88eSSamuel Holland 	assert(req_state);
220e382c88eSSamuel Holland 
221e382c88eSSamuel Holland 	for (unsigned int i = 0; i <= PLAT_MAX_PWR_LVL; ++i)
222e382c88eSSamuel Holland 		req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
223e382c88eSSamuel Holland }
224e382c88eSSamuel Holland 
22558032586SSamuel Holland static plat_psci_ops_t sunxi_psci_ops = {
226e382c88eSSamuel Holland 	.cpu_standby			= sunxi_cpu_standby,
227560581ecSSamuel Holland 	.pwr_domain_on			= sunxi_pwr_domain_on,
228560581ecSSamuel Holland 	.pwr_domain_off			= sunxi_pwr_domain_off,
229560581ecSSamuel Holland 	.pwr_domain_on_finish		= sunxi_pwr_domain_on_finish,
23058032586SSamuel Holland 	.system_off			= sunxi_system_off,
23158032586SSamuel Holland 	.system_reset			= sunxi_system_reset,
232e382c88eSSamuel Holland 	.validate_power_state		= sunxi_validate_power_state,
233560581ecSSamuel Holland 	.validate_ns_entrypoint		= sunxi_validate_ns_entrypoint,
23458032586SSamuel Holland };
23558032586SSamuel Holland 
23658032586SSamuel Holland int plat_setup_psci_ops(uintptr_t sec_entrypoint,
23758032586SSamuel Holland 			const plat_psci_ops_t **psci_ops)
23858032586SSamuel Holland {
23958032586SSamuel Holland 	assert(psci_ops);
24058032586SSamuel Holland 
241e382c88eSSamuel Holland 	/* Program all CPU entry points. */
242e382c88eSSamuel Holland 	for (unsigned int cpu = 0; cpu < PLATFORM_CORE_COUNT; ++cpu) {
243560581ecSSamuel Holland 		mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu),
244560581ecSSamuel Holland 			      sec_entrypoint & 0xffffffff);
245560581ecSSamuel Holland 		mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu),
246560581ecSSamuel Holland 			      sec_entrypoint >> 32);
247560581ecSSamuel Holland 	}
248560581ecSSamuel Holland 
249e382c88eSSamuel Holland 	/* Check for a valid SCP firmware, and boot the SCP if found. */
250e382c88eSSamuel Holland 	if (mmio_read_32(SUNXI_SCP_BASE) == SCP_FIRMWARE_MAGIC) {
251e382c88eSSamuel Holland 		/* Program SCP exception vectors to the firmware entrypoint. */
252e382c88eSSamuel Holland 		for (unsigned int i = OR1K_VEC_FIRST; i <= OR1K_VEC_LAST; ++i) {
253e382c88eSSamuel Holland 			uint32_t vector = SUNXI_SRAM_A2_BASE + OR1K_VEC_ADDR(i);
254e382c88eSSamuel Holland 			uint32_t offset = SUNXI_SCP_BASE - vector;
255e382c88eSSamuel Holland 
256e382c88eSSamuel Holland 			mmio_write_32(vector, offset >> 2);
257e382c88eSSamuel Holland 			clean_dcache_range(vector, sizeof(uint32_t));
258e382c88eSSamuel Holland 		}
259e382c88eSSamuel Holland 		/* Take the SCP out of reset. */
260e382c88eSSamuel Holland 		mmio_setbits_32(SUNXI_R_CPUCFG_BASE, BIT(0));
261e382c88eSSamuel Holland 		/* Wait for the SCP firmware to boot. */
262e382c88eSSamuel Holland 		if (scpi_wait_ready() == 0)
263e382c88eSSamuel Holland 			scpi_available = true;
264e382c88eSSamuel Holland 	}
265e382c88eSSamuel Holland 
266e382c88eSSamuel Holland 	NOTICE("PSCI: System suspend is %s\n",
267e382c88eSSamuel Holland 	       scpi_available ? "available via SCPI" : "unavailable");
268e382c88eSSamuel Holland 	if (scpi_available) {
269e382c88eSSamuel Holland 		/* Suspend is only available via SCPI. */
270e382c88eSSamuel Holland 		sunxi_psci_ops.pwr_domain_suspend = sunxi_pwr_domain_off;
271e382c88eSSamuel Holland 		sunxi_psci_ops.pwr_domain_suspend_finish = sunxi_pwr_domain_on_finish;
272e382c88eSSamuel Holland 		sunxi_psci_ops.get_sys_suspend_power_state = sunxi_get_sys_suspend_power_state;
273e382c88eSSamuel Holland 	} else {
274e382c88eSSamuel Holland 		/* This is only needed when SCPI is unavailable. */
275e382c88eSSamuel Holland 		sunxi_psci_ops.pwr_domain_pwr_down_wfi = sunxi_pwr_down_wfi;
276e382c88eSSamuel Holland 	}
277e382c88eSSamuel Holland 
27858032586SSamuel Holland 	*psci_ops = &sunxi_psci_ops;
27958032586SSamuel Holland 
28058032586SSamuel Holland 	return 0;
28158032586SSamuel Holland }
282