xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_pm.c (revision 5069c1cfef76cc86e6ad15bdd75b9330c3181e76)
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 <arch_helpers.h>
858032586SSamuel Holland #include <assert.h>
958032586SSamuel Holland #include <debug.h>
1058032586SSamuel Holland #include <delay_timer.h>
11560581ecSSamuel Holland #include <gicv2.h>
1258032586SSamuel Holland #include <mmio.h>
1358032586SSamuel Holland #include <platform.h>
1458032586SSamuel Holland #include <platform_def.h>
1558032586SSamuel Holland #include <psci.h>
1658032586SSamuel Holland #include <sunxi_mmap.h>
17560581ecSSamuel Holland #include <sunxi_cpucfg.h>
1858032586SSamuel Holland 
1958032586SSamuel Holland #define SUNXI_WDOG0_CTRL_REG		(SUNXI_WDOG_BASE + 0x0010)
2058032586SSamuel Holland #define SUNXI_WDOG0_CFG_REG		(SUNXI_WDOG_BASE + 0x0014)
2158032586SSamuel Holland #define SUNXI_WDOG0_MODE_REG		(SUNXI_WDOG_BASE + 0x0018)
2258032586SSamuel Holland 
23333d66cfSSamuel Holland #include "sunxi_private.h"
24333d66cfSSamuel Holland 
25560581ecSSamuel Holland #define mpidr_is_valid(mpidr) ( \
26560581ecSSamuel Holland 	MPIDR_AFFLVL3_VAL(mpidr) == 0 && \
27560581ecSSamuel Holland 	MPIDR_AFFLVL2_VAL(mpidr) == 0 && \
28560581ecSSamuel Holland 	MPIDR_AFFLVL1_VAL(mpidr) < PLATFORM_CLUSTER_COUNT && \
29560581ecSSamuel Holland 	MPIDR_AFFLVL0_VAL(mpidr) < PLATFORM_MAX_CPUS_PER_CLUSTER)
30560581ecSSamuel Holland 
31560581ecSSamuel Holland static int sunxi_pwr_domain_on(u_register_t mpidr)
32560581ecSSamuel Holland {
33560581ecSSamuel Holland 	if (mpidr_is_valid(mpidr) == 0)
34560581ecSSamuel Holland 		return PSCI_E_INTERN_FAIL;
35560581ecSSamuel Holland 
36560581ecSSamuel Holland 	sunxi_cpu_on(MPIDR_AFFLVL1_VAL(mpidr), MPIDR_AFFLVL0_VAL(mpidr));
37560581ecSSamuel Holland 
38560581ecSSamuel Holland 	return PSCI_E_SUCCESS;
39560581ecSSamuel Holland }
40560581ecSSamuel Holland 
41560581ecSSamuel Holland static void sunxi_pwr_domain_off(const psci_power_state_t *target_state)
42560581ecSSamuel Holland {
43560581ecSSamuel Holland 	gicv2_cpuif_disable();
44560581ecSSamuel Holland }
45560581ecSSamuel Holland 
46560581ecSSamuel Holland static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state)
47560581ecSSamuel Holland {
48560581ecSSamuel Holland 	gicv2_pcpu_distif_init();
49560581ecSSamuel Holland 	gicv2_cpuif_enable();
50560581ecSSamuel Holland }
51560581ecSSamuel Holland 
5258032586SSamuel Holland static void __dead2 sunxi_system_off(void)
5358032586SSamuel Holland {
54333d66cfSSamuel Holland 	/* Turn off all secondary CPUs */
55333d66cfSSamuel Holland 	sunxi_disable_secondary_cpus(plat_my_core_pos());
56333d66cfSSamuel Holland 
57*5069c1cfSIcenowy Zheng 	sunxi_power_down();
5858032586SSamuel Holland }
5958032586SSamuel Holland 
6058032586SSamuel Holland static void __dead2 sunxi_system_reset(void)
6158032586SSamuel Holland {
6258032586SSamuel Holland 	/* Reset the whole system when the watchdog times out */
6358032586SSamuel Holland 	mmio_write_32(SUNXI_WDOG0_CFG_REG, 1);
6458032586SSamuel Holland 	/* Enable the watchdog with the shortest timeout (0.5 seconds) */
6558032586SSamuel Holland 	mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1);
6658032586SSamuel Holland 	/* Wait for twice the watchdog timeout before panicking */
6758032586SSamuel Holland 	mdelay(1000);
6858032586SSamuel Holland 
6958032586SSamuel Holland 	ERROR("PSCI: System reset failed\n");
7058032586SSamuel Holland 	wfi();
7158032586SSamuel Holland 	panic();
7258032586SSamuel Holland }
7358032586SSamuel Holland 
74560581ecSSamuel Holland static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint)
75560581ecSSamuel Holland {
76560581ecSSamuel Holland 	/* The non-secure entry point must be in DRAM */
77c520be4bSAndre Przywara 	if (ns_entrypoint >= SUNXI_DRAM_BASE)
78560581ecSSamuel Holland 		return PSCI_E_SUCCESS;
79560581ecSSamuel Holland 
80560581ecSSamuel Holland 	return PSCI_E_INVALID_ADDRESS;
81560581ecSSamuel Holland }
82560581ecSSamuel Holland 
8358032586SSamuel Holland static plat_psci_ops_t sunxi_psci_ops = {
84560581ecSSamuel Holland 	.pwr_domain_on			= sunxi_pwr_domain_on,
85560581ecSSamuel Holland 	.pwr_domain_off			= sunxi_pwr_domain_off,
86560581ecSSamuel Holland 	.pwr_domain_on_finish		= sunxi_pwr_domain_on_finish,
8758032586SSamuel Holland 	.system_off			= sunxi_system_off,
8858032586SSamuel Holland 	.system_reset			= sunxi_system_reset,
89560581ecSSamuel Holland 	.validate_ns_entrypoint		= sunxi_validate_ns_entrypoint,
9058032586SSamuel Holland };
9158032586SSamuel Holland 
9258032586SSamuel Holland int plat_setup_psci_ops(uintptr_t sec_entrypoint,
9358032586SSamuel Holland 			const plat_psci_ops_t **psci_ops)
9458032586SSamuel Holland {
9558032586SSamuel Holland 	assert(psci_ops);
9658032586SSamuel Holland 
97560581ecSSamuel Holland 	for (int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) {
98560581ecSSamuel Holland 		mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu),
99560581ecSSamuel Holland 			      sec_entrypoint & 0xffffffff);
100560581ecSSamuel Holland 		mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu),
101560581ecSSamuel Holland 			      sec_entrypoint >> 32);
102560581ecSSamuel Holland 	}
103560581ecSSamuel Holland 
10458032586SSamuel Holland 	*psci_ops = &sunxi_psci_ops;
10558032586SSamuel Holland 
10658032586SSamuel Holland 	return 0;
10758032586SSamuel Holland }
108