xref: /aosp_15_r20/external/arm-trusted-firmware/plat/hisilicon/hikey960/hikey960_pm.c (revision 54fd6939e177f8ff529b10183254802c76df6d08)
1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park  *
4*54fd6939SJiyong Park  * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park  */
6*54fd6939SJiyong Park 
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park 
9*54fd6939SJiyong Park #include <arch_helpers.h>
10*54fd6939SJiyong Park #include <common/debug.h>
11*54fd6939SJiyong Park #include <drivers/arm/cci.h>
12*54fd6939SJiyong Park #include <drivers/arm/gicv2.h>
13*54fd6939SJiyong Park #include <drivers/arm/pl011.h>
14*54fd6939SJiyong Park #include <drivers/arm/pl061_gpio.h>
15*54fd6939SJiyong Park #include <drivers/delay_timer.h>
16*54fd6939SJiyong Park #include <lib/mmio.h>
17*54fd6939SJiyong Park #include <lib/psci/psci.h>
18*54fd6939SJiyong Park 
19*54fd6939SJiyong Park #include <hi3660.h>
20*54fd6939SJiyong Park #include <hi3660_crg.h>
21*54fd6939SJiyong Park 
22*54fd6939SJiyong Park #include "drivers/pwrc/hisi_pwrc.h"
23*54fd6939SJiyong Park #include "hikey960_def.h"
24*54fd6939SJiyong Park #include "hikey960_private.h"
25*54fd6939SJiyong Park 
26*54fd6939SJiyong Park #define CORE_PWR_STATE(state) \
27*54fd6939SJiyong Park 	((state)->pwr_domain_state[MPIDR_AFFLVL0])
28*54fd6939SJiyong Park #define CLUSTER_PWR_STATE(state) \
29*54fd6939SJiyong Park 	((state)->pwr_domain_state[MPIDR_AFFLVL1])
30*54fd6939SJiyong Park #define SYSTEM_PWR_STATE(state) \
31*54fd6939SJiyong Park 	((state)->pwr_domain_state[PLAT_MAX_PWR_LVL])
32*54fd6939SJiyong Park 
33*54fd6939SJiyong Park #define DMAC_GLB_REG_SEC	0x694
34*54fd6939SJiyong Park #define AXI_CONF_BASE		0x820
35*54fd6939SJiyong Park 
36*54fd6939SJiyong Park static unsigned int uart_base;
37*54fd6939SJiyong Park static console_t console;
38*54fd6939SJiyong Park static uintptr_t hikey960_sec_entrypoint;
39*54fd6939SJiyong Park 
hikey960_pwr_domain_standby(plat_local_state_t cpu_state)40*54fd6939SJiyong Park static void hikey960_pwr_domain_standby(plat_local_state_t cpu_state)
41*54fd6939SJiyong Park {
42*54fd6939SJiyong Park 	unsigned long scr;
43*54fd6939SJiyong Park 
44*54fd6939SJiyong Park 	scr = read_scr_el3();
45*54fd6939SJiyong Park 
46*54fd6939SJiyong Park 	/* Enable Physical IRQ and FIQ to wake the CPU */
47*54fd6939SJiyong Park 	write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
48*54fd6939SJiyong Park 
49*54fd6939SJiyong Park 	/* Add barrier before CPU enter WFI state */
50*54fd6939SJiyong Park 	isb();
51*54fd6939SJiyong Park 	dsb();
52*54fd6939SJiyong Park 	wfi();
53*54fd6939SJiyong Park 
54*54fd6939SJiyong Park 	/*
55*54fd6939SJiyong Park 	 * Restore SCR to the original value, synchronisazion of
56*54fd6939SJiyong Park 	 * scr_el3 is done by eret while el3_exit to save some
57*54fd6939SJiyong Park 	 * execution cycles.
58*54fd6939SJiyong Park 	 */
59*54fd6939SJiyong Park 	write_scr_el3(scr);
60*54fd6939SJiyong Park }
61*54fd6939SJiyong Park 
hikey960_pwr_domain_on(u_register_t mpidr)62*54fd6939SJiyong Park static int hikey960_pwr_domain_on(u_register_t mpidr)
63*54fd6939SJiyong Park {
64*54fd6939SJiyong Park 	unsigned int core = mpidr & MPIDR_CPU_MASK;
65*54fd6939SJiyong Park 	unsigned int cluster =
66*54fd6939SJiyong Park 		(mpidr & MPIDR_CLUSTER_MASK) >> MPIDR_AFFINITY_BITS;
67*54fd6939SJiyong Park 	int cluster_stat = cluster_is_powered_on(cluster);
68*54fd6939SJiyong Park 
69*54fd6939SJiyong Park 	hisi_set_cpu_boot_flag(cluster, core);
70*54fd6939SJiyong Park 
71*54fd6939SJiyong Park 	mmio_write_32(CRG_REG_BASE + CRG_RVBAR(cluster, core),
72*54fd6939SJiyong Park 		      hikey960_sec_entrypoint >> 2);
73*54fd6939SJiyong Park 
74*54fd6939SJiyong Park 	if (cluster_stat)
75*54fd6939SJiyong Park 		hisi_powerup_core(cluster, core);
76*54fd6939SJiyong Park 	else
77*54fd6939SJiyong Park 		hisi_powerup_cluster(cluster, core);
78*54fd6939SJiyong Park 
79*54fd6939SJiyong Park 	return PSCI_E_SUCCESS;
80*54fd6939SJiyong Park }
81*54fd6939SJiyong Park 
82*54fd6939SJiyong Park static void
hikey960_pwr_domain_on_finish(const psci_power_state_t * target_state)83*54fd6939SJiyong Park hikey960_pwr_domain_on_finish(const psci_power_state_t *target_state)
84*54fd6939SJiyong Park {
85*54fd6939SJiyong Park 	if (CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE)
86*54fd6939SJiyong Park 		cci_enable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1()));
87*54fd6939SJiyong Park 
88*54fd6939SJiyong Park 	gicv2_pcpu_distif_init();
89*54fd6939SJiyong Park 	gicv2_cpuif_enable();
90*54fd6939SJiyong Park }
91*54fd6939SJiyong Park 
hikey960_pwr_domain_off(const psci_power_state_t * target_state)92*54fd6939SJiyong Park void hikey960_pwr_domain_off(const psci_power_state_t *target_state)
93*54fd6939SJiyong Park {
94*54fd6939SJiyong Park 	unsigned long mpidr = read_mpidr_el1();
95*54fd6939SJiyong Park 	unsigned int core = mpidr & MPIDR_CPU_MASK;
96*54fd6939SJiyong Park 	unsigned int cluster =
97*54fd6939SJiyong Park 		(mpidr & MPIDR_CLUSTER_MASK) >> MPIDR_AFFINITY_BITS;
98*54fd6939SJiyong Park 
99*54fd6939SJiyong Park 	clr_ex();
100*54fd6939SJiyong Park 	isb();
101*54fd6939SJiyong Park 	dsbsy();
102*54fd6939SJiyong Park 
103*54fd6939SJiyong Park 	gicv2_cpuif_disable();
104*54fd6939SJiyong Park 
105*54fd6939SJiyong Park 	hisi_clear_cpu_boot_flag(cluster, core);
106*54fd6939SJiyong Park 	hisi_powerdn_core(cluster, core);
107*54fd6939SJiyong Park 
108*54fd6939SJiyong Park 	/* check if any core is powered up */
109*54fd6939SJiyong Park 	if (hisi_test_cpu_down(cluster, core)) {
110*54fd6939SJiyong Park 
111*54fd6939SJiyong Park 		cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(read_mpidr_el1()));
112*54fd6939SJiyong Park 
113*54fd6939SJiyong Park 		isb();
114*54fd6939SJiyong Park 		dsbsy();
115*54fd6939SJiyong Park 
116*54fd6939SJiyong Park 		hisi_powerdn_cluster(cluster, core);
117*54fd6939SJiyong Park 	}
118*54fd6939SJiyong Park }
119*54fd6939SJiyong Park 
hikey960_system_off(void)120*54fd6939SJiyong Park static void __dead2 hikey960_system_off(void)
121*54fd6939SJiyong Park {
122*54fd6939SJiyong Park 	gpio_set_direction(176, GPIO_DIR_OUT);
123*54fd6939SJiyong Park 	gpio_set_value(176, GPIO_LEVEL_LOW);
124*54fd6939SJiyong Park 	panic();
125*54fd6939SJiyong Park }
126*54fd6939SJiyong Park 
hikey960_system_reset(void)127*54fd6939SJiyong Park static void __dead2 hikey960_system_reset(void)
128*54fd6939SJiyong Park {
129*54fd6939SJiyong Park 	dsb();
130*54fd6939SJiyong Park 	isb();
131*54fd6939SJiyong Park 	mdelay(2000);
132*54fd6939SJiyong Park 	mmio_write_32(SCTRL_SCPEREN1_REG,
133*54fd6939SJiyong Park 		      SCPEREN1_WAIT_DDR_SELFREFRESH_DONE_BYPASS);
134*54fd6939SJiyong Park 	mmio_write_32(SCTRL_SCSYSSTAT_REG, 0xdeadbeef);
135*54fd6939SJiyong Park 	panic();
136*54fd6939SJiyong Park }
137*54fd6939SJiyong Park 
hikey960_validate_power_state(unsigned int power_state,psci_power_state_t * req_state)138*54fd6939SJiyong Park int hikey960_validate_power_state(unsigned int power_state,
139*54fd6939SJiyong Park 			       psci_power_state_t *req_state)
140*54fd6939SJiyong Park {
141*54fd6939SJiyong Park 	unsigned int pstate = psci_get_pstate_type(power_state);
142*54fd6939SJiyong Park 	unsigned int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
143*54fd6939SJiyong Park 	int i;
144*54fd6939SJiyong Park 
145*54fd6939SJiyong Park 	assert(req_state);
146*54fd6939SJiyong Park 
147*54fd6939SJiyong Park 	if (pwr_lvl > PLAT_MAX_PWR_LVL)
148*54fd6939SJiyong Park 		return PSCI_E_INVALID_PARAMS;
149*54fd6939SJiyong Park 
150*54fd6939SJiyong Park 	/* Sanity check the requested state */
151*54fd6939SJiyong Park 	if (pstate == PSTATE_TYPE_STANDBY) {
152*54fd6939SJiyong Park 		/*
153*54fd6939SJiyong Park 		 * It's possible to enter standby only on power level 0
154*54fd6939SJiyong Park 		 * Ignore any other power level.
155*54fd6939SJiyong Park 		 */
156*54fd6939SJiyong Park 		if (pwr_lvl != MPIDR_AFFLVL0)
157*54fd6939SJiyong Park 			return PSCI_E_INVALID_PARAMS;
158*54fd6939SJiyong Park 
159*54fd6939SJiyong Park 		req_state->pwr_domain_state[MPIDR_AFFLVL0] =
160*54fd6939SJiyong Park 					PLAT_MAX_RET_STATE;
161*54fd6939SJiyong Park 	} else {
162*54fd6939SJiyong Park 		for (i = MPIDR_AFFLVL0; i <= pwr_lvl; i++)
163*54fd6939SJiyong Park 			req_state->pwr_domain_state[i] =
164*54fd6939SJiyong Park 					PLAT_MAX_OFF_STATE;
165*54fd6939SJiyong Park 	}
166*54fd6939SJiyong Park 
167*54fd6939SJiyong Park 	/*
168*54fd6939SJiyong Park 	 * We expect the 'state id' to be zero.
169*54fd6939SJiyong Park 	 */
170*54fd6939SJiyong Park 	if (psci_get_pstate_id(power_state))
171*54fd6939SJiyong Park 		return PSCI_E_INVALID_PARAMS;
172*54fd6939SJiyong Park 
173*54fd6939SJiyong Park 	return PSCI_E_SUCCESS;
174*54fd6939SJiyong Park }
175*54fd6939SJiyong Park 
hikey960_validate_ns_entrypoint(uintptr_t entrypoint)176*54fd6939SJiyong Park static int hikey960_validate_ns_entrypoint(uintptr_t entrypoint)
177*54fd6939SJiyong Park {
178*54fd6939SJiyong Park 	/*
179*54fd6939SJiyong Park 	 * Check if the non secure entrypoint lies within the non
180*54fd6939SJiyong Park 	 * secure DRAM.
181*54fd6939SJiyong Park 	 */
182*54fd6939SJiyong Park 	if ((entrypoint > DDR_BASE) && (entrypoint < (DDR_BASE + DDR_SIZE)))
183*54fd6939SJiyong Park 		return PSCI_E_SUCCESS;
184*54fd6939SJiyong Park 
185*54fd6939SJiyong Park 	return PSCI_E_INVALID_ADDRESS;
186*54fd6939SJiyong Park }
187*54fd6939SJiyong Park 
hikey960_pwr_domain_suspend(const psci_power_state_t * target_state)188*54fd6939SJiyong Park static void hikey960_pwr_domain_suspend(const psci_power_state_t *target_state)
189*54fd6939SJiyong Park {
190*54fd6939SJiyong Park 	u_register_t mpidr = read_mpidr_el1();
191*54fd6939SJiyong Park 	unsigned int core = mpidr & MPIDR_CPU_MASK;
192*54fd6939SJiyong Park 	unsigned int cluster =
193*54fd6939SJiyong Park 		(mpidr & MPIDR_CLUSTER_MASK) >> MPIDR_AFFINITY_BITS;
194*54fd6939SJiyong Park 
195*54fd6939SJiyong Park 	if (CORE_PWR_STATE(target_state) != PLAT_MAX_OFF_STATE)
196*54fd6939SJiyong Park 		return;
197*54fd6939SJiyong Park 
198*54fd6939SJiyong Park 	if (CORE_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) {
199*54fd6939SJiyong Park 		clr_ex();
200*54fd6939SJiyong Park 		isb();
201*54fd6939SJiyong Park 		dsbsy();
202*54fd6939SJiyong Park 
203*54fd6939SJiyong Park 		gicv2_cpuif_disable();
204*54fd6939SJiyong Park 
205*54fd6939SJiyong Park 		hisi_cpuidle_lock(cluster, core);
206*54fd6939SJiyong Park 		hisi_set_cpuidle_flag(cluster, core);
207*54fd6939SJiyong Park 		hisi_cpuidle_unlock(cluster, core);
208*54fd6939SJiyong Park 
209*54fd6939SJiyong Park 		mmio_write_32(CRG_REG_BASE + CRG_RVBAR(cluster, core),
210*54fd6939SJiyong Park 		      hikey960_sec_entrypoint >> 2);
211*54fd6939SJiyong Park 
212*54fd6939SJiyong Park 		hisi_enter_core_idle(cluster, core);
213*54fd6939SJiyong Park 	}
214*54fd6939SJiyong Park 
215*54fd6939SJiyong Park 	/* Perform the common cluster specific operations */
216*54fd6939SJiyong Park 	if (CLUSTER_PWR_STATE(target_state) == PLAT_MAX_OFF_STATE) {
217*54fd6939SJiyong Park 		hisi_cpuidle_lock(cluster, core);
218*54fd6939SJiyong Park 		hisi_disable_pdc(cluster);
219*54fd6939SJiyong Park 
220*54fd6939SJiyong Park 		/* check if any core is powered up */
221*54fd6939SJiyong Park 		if (hisi_test_pwrdn_allcores(cluster, core)) {
222*54fd6939SJiyong Park 
223*54fd6939SJiyong Park 			cci_disable_snoop_dvm_reqs(MPIDR_AFFLVL1_VAL(mpidr));
224*54fd6939SJiyong Park 
225*54fd6939SJiyong Park 			isb();
226*54fd6939SJiyong Park 			dsbsy();
227*54fd6939SJiyong Park 
228*54fd6939SJiyong Park 			/* mask the pdc wakeup irq, then
229*54fd6939SJiyong Park 			 * enable pdc to power down the core
230*54fd6939SJiyong Park 			 */
231*54fd6939SJiyong Park 			hisi_pdc_mask_cluster_wakeirq(cluster);
232*54fd6939SJiyong Park 			hisi_enable_pdc(cluster);
233*54fd6939SJiyong Park 
234*54fd6939SJiyong Park 			hisi_cpuidle_unlock(cluster, core);
235*54fd6939SJiyong Park 
236*54fd6939SJiyong Park 			/* check the SR flag bit to determine
237*54fd6939SJiyong Park 			 * CLUSTER_IDLE_IPC or AP_SR_IPC to send
238*54fd6939SJiyong Park 			 */
239*54fd6939SJiyong Park 			if (hisi_test_ap_suspend_flag())
240*54fd6939SJiyong Park 				hisi_enter_ap_suspend(cluster, core);
241*54fd6939SJiyong Park 			else
242*54fd6939SJiyong Park 				hisi_enter_cluster_idle(cluster, core);
243*54fd6939SJiyong Park 		} else {
244*54fd6939SJiyong Park 			/* enable pdc */
245*54fd6939SJiyong Park 			hisi_enable_pdc(cluster);
246*54fd6939SJiyong Park 			hisi_cpuidle_unlock(cluster, core);
247*54fd6939SJiyong Park 		}
248*54fd6939SJiyong Park 	}
249*54fd6939SJiyong Park }
250*54fd6939SJiyong Park 
hikey960_sr_dma_reinit(void)251*54fd6939SJiyong Park static void hikey960_sr_dma_reinit(void)
252*54fd6939SJiyong Park {
253*54fd6939SJiyong Park 	unsigned int ctr = 0;
254*54fd6939SJiyong Park 
255*54fd6939SJiyong Park 	mmio_write_32(DMAC_BASE + DMAC_GLB_REG_SEC, 0x3);
256*54fd6939SJiyong Park 
257*54fd6939SJiyong Park 	/* 1~15 channel is set non_secure */
258*54fd6939SJiyong Park 	for (ctr = 1; ctr <= 15; ctr++)
259*54fd6939SJiyong Park 		mmio_write_32(DMAC_BASE + AXI_CONF_BASE + ctr * (0x40),
260*54fd6939SJiyong Park 			      (1 << 6) | (1 << 18));
261*54fd6939SJiyong Park }
262*54fd6939SJiyong Park 
263*54fd6939SJiyong Park static void
hikey960_pwr_domain_suspend_finish(const psci_power_state_t * target_state)264*54fd6939SJiyong Park hikey960_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
265*54fd6939SJiyong Park {
266*54fd6939SJiyong Park 	unsigned long mpidr = read_mpidr_el1();
267*54fd6939SJiyong Park 	unsigned int core = mpidr & MPIDR_CPU_MASK;
268*54fd6939SJiyong Park 	unsigned int cluster =
269*54fd6939SJiyong Park 		(mpidr & MPIDR_CLUSTER_MASK) >> MPIDR_AFFINITY_BITS;
270*54fd6939SJiyong Park 
271*54fd6939SJiyong Park 	/* Nothing to be done on waking up from retention from CPU level */
272*54fd6939SJiyong Park 	if (CORE_PWR_STATE(target_state) != PLAT_MAX_OFF_STATE)
273*54fd6939SJiyong Park 		return;
274*54fd6939SJiyong Park 
275*54fd6939SJiyong Park 	hisi_cpuidle_lock(cluster, core);
276*54fd6939SJiyong Park 	hisi_clear_cpuidle_flag(cluster, core);
277*54fd6939SJiyong Park 	hisi_cpuidle_unlock(cluster, core);
278*54fd6939SJiyong Park 
279*54fd6939SJiyong Park 	if (hisi_test_ap_suspend_flag()) {
280*54fd6939SJiyong Park 		hikey960_sr_dma_reinit();
281*54fd6939SJiyong Park 		gicv2_cpuif_enable();
282*54fd6939SJiyong Park 		console_pl011_register(uart_base, PL011_UART_CLK_IN_HZ,
283*54fd6939SJiyong Park 				       PL011_BAUDRATE, &console);
284*54fd6939SJiyong Park 	}
285*54fd6939SJiyong Park 
286*54fd6939SJiyong Park 	hikey960_pwr_domain_on_finish(target_state);
287*54fd6939SJiyong Park }
288*54fd6939SJiyong Park 
hikey960_get_sys_suspend_power_state(psci_power_state_t * req_state)289*54fd6939SJiyong Park static void hikey960_get_sys_suspend_power_state(psci_power_state_t *req_state)
290*54fd6939SJiyong Park {
291*54fd6939SJiyong Park 	int i;
292*54fd6939SJiyong Park 
293*54fd6939SJiyong Park 	for (i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
294*54fd6939SJiyong Park 		req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
295*54fd6939SJiyong Park }
296*54fd6939SJiyong Park 
297*54fd6939SJiyong Park static const plat_psci_ops_t hikey960_psci_ops = {
298*54fd6939SJiyong Park 	.cpu_standby			= hikey960_pwr_domain_standby,
299*54fd6939SJiyong Park 	.pwr_domain_on			= hikey960_pwr_domain_on,
300*54fd6939SJiyong Park 	.pwr_domain_on_finish		= hikey960_pwr_domain_on_finish,
301*54fd6939SJiyong Park 	.pwr_domain_off			= hikey960_pwr_domain_off,
302*54fd6939SJiyong Park 	.pwr_domain_suspend		= hikey960_pwr_domain_suspend,
303*54fd6939SJiyong Park 	.pwr_domain_suspend_finish	= hikey960_pwr_domain_suspend_finish,
304*54fd6939SJiyong Park 	.system_off			= hikey960_system_off,
305*54fd6939SJiyong Park 	.system_reset			= hikey960_system_reset,
306*54fd6939SJiyong Park 	.validate_power_state		= hikey960_validate_power_state,
307*54fd6939SJiyong Park 	.validate_ns_entrypoint		= hikey960_validate_ns_entrypoint,
308*54fd6939SJiyong Park 	.get_sys_suspend_power_state	= hikey960_get_sys_suspend_power_state,
309*54fd6939SJiyong Park };
310*54fd6939SJiyong Park 
plat_setup_psci_ops(uintptr_t sec_entrypoint,const plat_psci_ops_t ** psci_ops)311*54fd6939SJiyong Park int plat_setup_psci_ops(uintptr_t sec_entrypoint,
312*54fd6939SJiyong Park 			const plat_psci_ops_t **psci_ops)
313*54fd6939SJiyong Park {
314*54fd6939SJiyong Park 	unsigned int id = 0;
315*54fd6939SJiyong Park 	int ret;
316*54fd6939SJiyong Park 
317*54fd6939SJiyong Park 	ret = hikey960_read_boardid(&id);
318*54fd6939SJiyong Park 	if (ret == 0) {
319*54fd6939SJiyong Park 		if (id == 5300U)
320*54fd6939SJiyong Park 			uart_base = PL011_UART5_BASE;
321*54fd6939SJiyong Park 		else
322*54fd6939SJiyong Park 			uart_base = PL011_UART6_BASE;
323*54fd6939SJiyong Park 	} else {
324*54fd6939SJiyong Park 		uart_base = PL011_UART6_BASE;
325*54fd6939SJiyong Park 	}
326*54fd6939SJiyong Park 
327*54fd6939SJiyong Park 	hikey960_sec_entrypoint = sec_entrypoint;
328*54fd6939SJiyong Park 
329*54fd6939SJiyong Park 	INFO("%s: sec_entrypoint=0x%lx\n", __func__,
330*54fd6939SJiyong Park 	     (unsigned long)hikey960_sec_entrypoint);
331*54fd6939SJiyong Park 
332*54fd6939SJiyong Park 	/*
333*54fd6939SJiyong Park 	 * Initialize PSCI ops struct
334*54fd6939SJiyong Park 	 */
335*54fd6939SJiyong Park 	*psci_ops = &hikey960_psci_ops;
336*54fd6939SJiyong Park 	return 0;
337*54fd6939SJiyong Park }
338