xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_pm.c (revision 818e67324bbe5fb74191182bd6bbafd1e1baab58)
158032586SSamuel Holland /*
2*818e6732SSamuel Holland  * Copyright (c) 2017-2019, 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 
23523ab5beSClément Péron #define SUNXI_WDOG0_CTRL_REG		(SUNXI_R_WDOG_BASE + 0x0010)
24523ab5beSClément Péron #define SUNXI_WDOG0_CFG_REG		(SUNXI_R_WDOG_BASE + 0x0014)
25523ab5beSClé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();
68*818e6732SSamuel Holland 
69*818e6732SSamuel Holland 	udelay(1000);
70*818e6732SSamuel Holland 	ERROR("PSCI: Cannot turn off system, halting\n");
71*818e6732SSamuel Holland 	wfi();
72*818e6732SSamuel Holland 	panic();
7358032586SSamuel Holland }
7458032586SSamuel Holland 
7558032586SSamuel Holland static void __dead2 sunxi_system_reset(void)
7658032586SSamuel Holland {
7758032586SSamuel Holland 	/* Reset the whole system when the watchdog times out */
7858032586SSamuel Holland 	mmio_write_32(SUNXI_WDOG0_CFG_REG, 1);
7958032586SSamuel Holland 	/* Enable the watchdog with the shortest timeout (0.5 seconds) */
8058032586SSamuel Holland 	mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1);
8158032586SSamuel Holland 	/* Wait for twice the watchdog timeout before panicking */
8258032586SSamuel Holland 	mdelay(1000);
8358032586SSamuel Holland 
8458032586SSamuel Holland 	ERROR("PSCI: System reset failed\n");
8558032586SSamuel Holland 	wfi();
8658032586SSamuel Holland 	panic();
8758032586SSamuel Holland }
8858032586SSamuel Holland 
89560581ecSSamuel Holland static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint)
90560581ecSSamuel Holland {
91560581ecSSamuel Holland 	/* The non-secure entry point must be in DRAM */
92c520be4bSAndre Przywara 	if (ns_entrypoint >= SUNXI_DRAM_BASE)
93560581ecSSamuel Holland 		return PSCI_E_SUCCESS;
94560581ecSSamuel Holland 
95560581ecSSamuel Holland 	return PSCI_E_INVALID_ADDRESS;
96560581ecSSamuel Holland }
97560581ecSSamuel Holland 
9858032586SSamuel Holland static plat_psci_ops_t sunxi_psci_ops = {
99560581ecSSamuel Holland 	.pwr_domain_on			= sunxi_pwr_domain_on,
100560581ecSSamuel Holland 	.pwr_domain_off			= sunxi_pwr_domain_off,
1017db0c960SAndre Przywara 	.pwr_domain_pwr_down_wfi	= sunxi_pwr_down_wfi,
102560581ecSSamuel Holland 	.pwr_domain_on_finish		= sunxi_pwr_domain_on_finish,
10358032586SSamuel Holland 	.system_off			= sunxi_system_off,
10458032586SSamuel Holland 	.system_reset			= sunxi_system_reset,
105560581ecSSamuel Holland 	.validate_ns_entrypoint		= sunxi_validate_ns_entrypoint,
10658032586SSamuel Holland };
10758032586SSamuel Holland 
10858032586SSamuel Holland int plat_setup_psci_ops(uintptr_t sec_entrypoint,
10958032586SSamuel Holland 			const plat_psci_ops_t **psci_ops)
11058032586SSamuel Holland {
11158032586SSamuel Holland 	assert(psci_ops);
11258032586SSamuel Holland 
113560581ecSSamuel Holland 	for (int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) {
114560581ecSSamuel Holland 		mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu),
115560581ecSSamuel Holland 			      sec_entrypoint & 0xffffffff);
116560581ecSSamuel Holland 		mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu),
117560581ecSSamuel Holland 			      sec_entrypoint >> 32);
118560581ecSSamuel Holland 	}
119560581ecSSamuel Holland 
12058032586SSamuel Holland 	*psci_ops = &sunxi_psci_ops;
12158032586SSamuel Holland 
12258032586SSamuel Holland 	return 0;
12358032586SSamuel Holland }
124