xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_pm.c (revision 523ab5be1a84e9aa15fb62c3a15a6338b01d3961)
158032586SSamuel Holland /*
258032586SSamuel Holland  * Copyright (c) 2017-2018, 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>
1309d40e0eSAntonio Nino Diaz #include <drivers/arm/gicv2.h>
1409d40e0eSAntonio Nino Diaz #include <drivers/delay_timer.h>
1509d40e0eSAntonio Nino Diaz #include <lib/mmio.h>
1609d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h>
1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1809d40e0eSAntonio Nino Diaz 
19560581ecSSamuel Holland #include <sunxi_cpucfg.h>
204ec1a239SAndre Przywara #include <sunxi_mmap.h>
214ec1a239SAndre Przywara #include <sunxi_private.h>
2258032586SSamuel Holland 
23*523ab5beSClément Péron #define SUNXI_WDOG0_CTRL_REG		(SUNXI_R_WDOG_BASE + 0x0010)
24*523ab5beSClément Péron #define SUNXI_WDOG0_CFG_REG		(SUNXI_R_WDOG_BASE + 0x0014)
25*523ab5beSClément Péron #define SUNXI_WDOG0_MODE_REG		(SUNXI_R_WDOG_BASE + 0x0018)
2658032586SSamuel Holland 
27560581ecSSamuel Holland #define mpidr_is_valid(mpidr) ( \
28560581ecSSamuel Holland 	MPIDR_AFFLVL3_VAL(mpidr) == 0 && \
29560581ecSSamuel Holland 	MPIDR_AFFLVL2_VAL(mpidr) == 0 && \
30560581ecSSamuel Holland 	MPIDR_AFFLVL1_VAL(mpidr) < PLATFORM_CLUSTER_COUNT && \
31560581ecSSamuel Holland 	MPIDR_AFFLVL0_VAL(mpidr) < PLATFORM_MAX_CPUS_PER_CLUSTER)
32560581ecSSamuel Holland 
33560581ecSSamuel Holland static int sunxi_pwr_domain_on(u_register_t mpidr)
34560581ecSSamuel Holland {
35560581ecSSamuel Holland 	if (mpidr_is_valid(mpidr) == 0)
36560581ecSSamuel Holland 		return PSCI_E_INTERN_FAIL;
37560581ecSSamuel Holland 
385d4bd66dSSamuel Holland 	sunxi_cpu_on(mpidr);
39560581ecSSamuel Holland 
40560581ecSSamuel Holland 	return PSCI_E_SUCCESS;
41560581ecSSamuel Holland }
42560581ecSSamuel Holland 
43560581ecSSamuel Holland static void sunxi_pwr_domain_off(const psci_power_state_t *target_state)
44560581ecSSamuel Holland {
45560581ecSSamuel Holland 	gicv2_cpuif_disable();
46560581ecSSamuel Holland }
47560581ecSSamuel Holland 
487db0c960SAndre Przywara static void __dead2 sunxi_pwr_down_wfi(const psci_power_state_t *target_state)
497db0c960SAndre Przywara {
505d4bd66dSSamuel Holland 	sunxi_cpu_off(read_mpidr());
517db0c960SAndre Przywara 
527db0c960SAndre Przywara 	while (1)
537db0c960SAndre Przywara 		wfi();
547db0c960SAndre Przywara }
557db0c960SAndre Przywara 
56560581ecSSamuel Holland static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state)
57560581ecSSamuel Holland {
58560581ecSSamuel Holland 	gicv2_pcpu_distif_init();
59560581ecSSamuel Holland 	gicv2_cpuif_enable();
60560581ecSSamuel Holland }
61560581ecSSamuel Holland 
6258032586SSamuel Holland static void __dead2 sunxi_system_off(void)
6358032586SSamuel Holland {
64333d66cfSSamuel Holland 	/* Turn off all secondary CPUs */
655d4bd66dSSamuel Holland 	sunxi_disable_secondary_cpus(read_mpidr());
66333d66cfSSamuel Holland 
675069c1cfSIcenowy Zheng 	sunxi_power_down();
6858032586SSamuel Holland }
6958032586SSamuel Holland 
7058032586SSamuel Holland static void __dead2 sunxi_system_reset(void)
7158032586SSamuel Holland {
7258032586SSamuel Holland 	/* Reset the whole system when the watchdog times out */
7358032586SSamuel Holland 	mmio_write_32(SUNXI_WDOG0_CFG_REG, 1);
7458032586SSamuel Holland 	/* Enable the watchdog with the shortest timeout (0.5 seconds) */
7558032586SSamuel Holland 	mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1);
7658032586SSamuel Holland 	/* Wait for twice the watchdog timeout before panicking */
7758032586SSamuel Holland 	mdelay(1000);
7858032586SSamuel Holland 
7958032586SSamuel Holland 	ERROR("PSCI: System reset failed\n");
8058032586SSamuel Holland 	wfi();
8158032586SSamuel Holland 	panic();
8258032586SSamuel Holland }
8358032586SSamuel Holland 
84560581ecSSamuel Holland static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint)
85560581ecSSamuel Holland {
86560581ecSSamuel Holland 	/* The non-secure entry point must be in DRAM */
87c520be4bSAndre Przywara 	if (ns_entrypoint >= SUNXI_DRAM_BASE)
88560581ecSSamuel Holland 		return PSCI_E_SUCCESS;
89560581ecSSamuel Holland 
90560581ecSSamuel Holland 	return PSCI_E_INVALID_ADDRESS;
91560581ecSSamuel Holland }
92560581ecSSamuel Holland 
9358032586SSamuel Holland static plat_psci_ops_t sunxi_psci_ops = {
94560581ecSSamuel Holland 	.pwr_domain_on			= sunxi_pwr_domain_on,
95560581ecSSamuel Holland 	.pwr_domain_off			= sunxi_pwr_domain_off,
967db0c960SAndre Przywara 	.pwr_domain_pwr_down_wfi	= sunxi_pwr_down_wfi,
97560581ecSSamuel Holland 	.pwr_domain_on_finish		= sunxi_pwr_domain_on_finish,
9858032586SSamuel Holland 	.system_off			= sunxi_system_off,
9958032586SSamuel Holland 	.system_reset			= sunxi_system_reset,
100560581ecSSamuel Holland 	.validate_ns_entrypoint		= sunxi_validate_ns_entrypoint,
10158032586SSamuel Holland };
10258032586SSamuel Holland 
10358032586SSamuel Holland int plat_setup_psci_ops(uintptr_t sec_entrypoint,
10458032586SSamuel Holland 			const plat_psci_ops_t **psci_ops)
10558032586SSamuel Holland {
10658032586SSamuel Holland 	assert(psci_ops);
10758032586SSamuel Holland 
108560581ecSSamuel Holland 	for (int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) {
109560581ecSSamuel Holland 		mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu),
110560581ecSSamuel Holland 			      sec_entrypoint & 0xffffffff);
111560581ecSSamuel Holland 		mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu),
112560581ecSSamuel Holland 			      sec_entrypoint >> 32);
113560581ecSSamuel Holland 	}
114560581ecSSamuel Holland 
11558032586SSamuel Holland 	*psci_ops = &sunxi_psci_ops;
11658032586SSamuel Holland 
11758032586SSamuel Holland 	return 0;
11858032586SSamuel Holland }
119