xref: /rk3399_ARM-atf/plat/allwinner/common/sunxi_pm.c (revision 4ec1a2399cf6e182ba2828a40795912d20eca1ab)
1 /*
2  * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch_helpers.h>
8 #include <assert.h>
9 #include <debug.h>
10 #include <delay_timer.h>
11 #include <gicv2.h>
12 #include <mmio.h>
13 #include <platform.h>
14 #include <platform_def.h>
15 #include <psci.h>
16 #include <sunxi_cpucfg.h>
17 #include <sunxi_mmap.h>
18 #include <sunxi_private.h>
19 
20 #define SUNXI_WDOG0_CTRL_REG		(SUNXI_WDOG_BASE + 0x0010)
21 #define SUNXI_WDOG0_CFG_REG		(SUNXI_WDOG_BASE + 0x0014)
22 #define SUNXI_WDOG0_MODE_REG		(SUNXI_WDOG_BASE + 0x0018)
23 
24 #define mpidr_is_valid(mpidr) ( \
25 	MPIDR_AFFLVL3_VAL(mpidr) == 0 && \
26 	MPIDR_AFFLVL2_VAL(mpidr) == 0 && \
27 	MPIDR_AFFLVL1_VAL(mpidr) < PLATFORM_CLUSTER_COUNT && \
28 	MPIDR_AFFLVL0_VAL(mpidr) < PLATFORM_MAX_CPUS_PER_CLUSTER)
29 
30 static int sunxi_pwr_domain_on(u_register_t mpidr)
31 {
32 	if (mpidr_is_valid(mpidr) == 0)
33 		return PSCI_E_INTERN_FAIL;
34 
35 	sunxi_cpu_on(MPIDR_AFFLVL1_VAL(mpidr), MPIDR_AFFLVL0_VAL(mpidr));
36 
37 	return PSCI_E_SUCCESS;
38 }
39 
40 static void sunxi_pwr_domain_off(const psci_power_state_t *target_state)
41 {
42 	gicv2_cpuif_disable();
43 }
44 
45 static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state)
46 {
47 	gicv2_pcpu_distif_init();
48 	gicv2_cpuif_enable();
49 }
50 
51 static void __dead2 sunxi_system_off(void)
52 {
53 	/* Turn off all secondary CPUs */
54 	sunxi_disable_secondary_cpus(plat_my_core_pos());
55 
56 	sunxi_power_down();
57 }
58 
59 static void __dead2 sunxi_system_reset(void)
60 {
61 	/* Reset the whole system when the watchdog times out */
62 	mmio_write_32(SUNXI_WDOG0_CFG_REG, 1);
63 	/* Enable the watchdog with the shortest timeout (0.5 seconds) */
64 	mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1);
65 	/* Wait for twice the watchdog timeout before panicking */
66 	mdelay(1000);
67 
68 	ERROR("PSCI: System reset failed\n");
69 	wfi();
70 	panic();
71 }
72 
73 static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint)
74 {
75 	/* The non-secure entry point must be in DRAM */
76 	if (ns_entrypoint >= SUNXI_DRAM_BASE)
77 		return PSCI_E_SUCCESS;
78 
79 	return PSCI_E_INVALID_ADDRESS;
80 }
81 
82 static plat_psci_ops_t sunxi_psci_ops = {
83 	.pwr_domain_on			= sunxi_pwr_domain_on,
84 	.pwr_domain_off			= sunxi_pwr_domain_off,
85 	.pwr_domain_on_finish		= sunxi_pwr_domain_on_finish,
86 	.system_off			= sunxi_system_off,
87 	.system_reset			= sunxi_system_reset,
88 	.validate_ns_entrypoint		= sunxi_validate_ns_entrypoint,
89 };
90 
91 int plat_setup_psci_ops(uintptr_t sec_entrypoint,
92 			const plat_psci_ops_t **psci_ops)
93 {
94 	assert(psci_ops);
95 
96 	for (int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) {
97 		mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu),
98 			      sec_entrypoint & 0xffffffff);
99 		mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu),
100 			      sec_entrypoint >> 32);
101 	}
102 
103 	*psci_ops = &sunxi_psci_ops;
104 
105 	return 0;
106 }
107