xref: /arm-trusted-firmware/plat/marvell/armada/a8k/common/plat_pm.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (C) 2018 Marvell International Ltd.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier:     BSD-3-Clause
5*91f16700Schasinglulu  * https://spdx.org/licenses
6*91f16700Schasinglulu  */
7*91f16700Schasinglulu 
8*91f16700Schasinglulu #include <assert.h>
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <common/debug.h>
11*91f16700Schasinglulu #include <drivers/arm/gicv2.h>
12*91f16700Schasinglulu #include <drivers/console.h>
13*91f16700Schasinglulu #include <drivers/delay_timer.h>
14*91f16700Schasinglulu #include <drivers/marvell/cache_llc.h>
15*91f16700Schasinglulu #include <lib/bakery_lock.h>
16*91f16700Schasinglulu #include <lib/mmio.h>
17*91f16700Schasinglulu #include <plat/common/platform.h>
18*91f16700Schasinglulu 
19*91f16700Schasinglulu #include <armada_common.h>
20*91f16700Schasinglulu #include <marvell_pm.h>
21*91f16700Schasinglulu #if MSS_SUPPORT
22*91f16700Schasinglulu #include <mss_pm_ipc.h>
23*91f16700Schasinglulu #endif
24*91f16700Schasinglulu #include <plat_marvell.h>
25*91f16700Schasinglulu #include <plat_pm_trace.h>
26*91f16700Schasinglulu 
27*91f16700Schasinglulu #define MVEBU_PRIVATE_UID_REG		0x30
28*91f16700Schasinglulu #define MVEBU_RFU_GLOBL_SW_RST		0x84
29*91f16700Schasinglulu #define MVEBU_CCU_RVBAR(cpu)		(MVEBU_REGS_BASE + 0x640 + (cpu * 4))
30*91f16700Schasinglulu #define MVEBU_CCU_CPU_UN_RESET(cpu)	(MVEBU_REGS_BASE + 0x650 + (cpu * 4))
31*91f16700Schasinglulu 
32*91f16700Schasinglulu #define MPIDR_CPU_GET(mpidr)		((mpidr) & MPIDR_CPU_MASK)
33*91f16700Schasinglulu #define MPIDR_CLUSTER_GET(mpidr)	MPIDR_AFFLVL1_VAL((mpidr))
34*91f16700Schasinglulu 
35*91f16700Schasinglulu #define MVEBU_GPIO_MASK(index)		(1 << (index % 32))
36*91f16700Schasinglulu #define MVEBU_MPP_MASK(index)		(0xF << (4 * (index % 8)))
37*91f16700Schasinglulu #define MVEBU_GPIO_VALUE(index, value)	(value << (index % 32))
38*91f16700Schasinglulu 
39*91f16700Schasinglulu #define MVEBU_USER_CMD_0_REG		(MVEBU_DRAM_MAC_BASE + 0x20)
40*91f16700Schasinglulu #define MVEBU_USER_CMD_CH0_OFFSET	28
41*91f16700Schasinglulu #define MVEBU_USER_CMD_CH0_MASK		(1 << MVEBU_USER_CMD_CH0_OFFSET)
42*91f16700Schasinglulu #define MVEBU_USER_CMD_CH0_EN		(1 << MVEBU_USER_CMD_CH0_OFFSET)
43*91f16700Schasinglulu #define MVEBU_USER_CMD_CS_OFFSET	24
44*91f16700Schasinglulu #define MVEBU_USER_CMD_CS_MASK		(0xF << MVEBU_USER_CMD_CS_OFFSET)
45*91f16700Schasinglulu #define MVEBU_USER_CMD_CS_ALL		(0xF << MVEBU_USER_CMD_CS_OFFSET)
46*91f16700Schasinglulu #define MVEBU_USER_CMD_SR_OFFSET	6
47*91f16700Schasinglulu #define MVEBU_USER_CMD_SR_MASK		(0x3 << MVEBU_USER_CMD_SR_OFFSET)
48*91f16700Schasinglulu #define MVEBU_USER_CMD_SR_ENTER		(0x1 << MVEBU_USER_CMD_SR_OFFSET)
49*91f16700Schasinglulu #define MVEBU_MC_PWR_CTRL_REG		(MVEBU_DRAM_MAC_BASE + 0x54)
50*91f16700Schasinglulu #define MVEBU_MC_AC_ON_DLY_OFFSET	8
51*91f16700Schasinglulu #define MVEBU_MC_AC_ON_DLY_MASK		(0xF << MVEBU_MC_AC_ON_DLY_OFFSET)
52*91f16700Schasinglulu #define MVEBU_MC_AC_ON_DLY_DEF_VAR	(8 << MVEBU_MC_AC_ON_DLY_OFFSET)
53*91f16700Schasinglulu #define MVEBU_MC_AC_OFF_DLY_OFFSET	4
54*91f16700Schasinglulu #define MVEBU_MC_AC_OFF_DLY_MASK	(0xF << MVEBU_MC_AC_OFF_DLY_OFFSET)
55*91f16700Schasinglulu #define MVEBU_MC_AC_OFF_DLY_DEF_VAR	(0xC << MVEBU_MC_AC_OFF_DLY_OFFSET)
56*91f16700Schasinglulu #define MVEBU_MC_PHY_AUTO_OFF_OFFSET	0
57*91f16700Schasinglulu #define MVEBU_MC_PHY_AUTO_OFF_MASK	(1 << MVEBU_MC_PHY_AUTO_OFF_OFFSET)
58*91f16700Schasinglulu #define MVEBU_MC_PHY_AUTO_OFF_EN	(1 << MVEBU_MC_PHY_AUTO_OFF_OFFSET)
59*91f16700Schasinglulu 
60*91f16700Schasinglulu /* this lock synchronize AP multiple cores execution with MSS */
61*91f16700Schasinglulu DEFINE_BAKERY_LOCK(pm_sys_lock);
62*91f16700Schasinglulu 
63*91f16700Schasinglulu /* Weak definitions may be overridden in specific board */
64*91f16700Schasinglulu #pragma weak plat_marvell_get_pm_cfg
65*91f16700Schasinglulu 
66*91f16700Schasinglulu /* AP806 CPU power down /power up definitions */
67*91f16700Schasinglulu enum CPU_ID {
68*91f16700Schasinglulu 	CPU0,
69*91f16700Schasinglulu 	CPU1,
70*91f16700Schasinglulu 	CPU2,
71*91f16700Schasinglulu 	CPU3
72*91f16700Schasinglulu };
73*91f16700Schasinglulu 
74*91f16700Schasinglulu #define REG_WR_VALIDATE_TIMEOUT		(2000)
75*91f16700Schasinglulu 
76*91f16700Schasinglulu #define FEATURE_DISABLE_STATUS_REG			\
77*91f16700Schasinglulu 			(MVEBU_REGS_BASE + 0x6F8230)
78*91f16700Schasinglulu #define FEATURE_DISABLE_STATUS_CPU_CLUSTER_OFFSET	4
79*91f16700Schasinglulu #define FEATURE_DISABLE_STATUS_CPU_CLUSTER_MASK		\
80*91f16700Schasinglulu 			(0x1 << FEATURE_DISABLE_STATUS_CPU_CLUSTER_OFFSET)
81*91f16700Schasinglulu 
82*91f16700Schasinglulu #ifdef MVEBU_SOC_AP807
83*91f16700Schasinglulu 	#define PWRC_CPUN_CR_PWR_DN_RQ_OFFSET		1
84*91f16700Schasinglulu 	#define PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET	0
85*91f16700Schasinglulu #else
86*91f16700Schasinglulu 	#define PWRC_CPUN_CR_PWR_DN_RQ_OFFSET		0
87*91f16700Schasinglulu 	#define PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET	31
88*91f16700Schasinglulu #endif
89*91f16700Schasinglulu 
90*91f16700Schasinglulu #define PWRC_CPUN_CR_REG(cpu_id)		\
91*91f16700Schasinglulu 			(MVEBU_REGS_BASE + 0x680000 + (cpu_id * 0x10))
92*91f16700Schasinglulu #define PWRC_CPUN_CR_PWR_DN_RQ_MASK		\
93*91f16700Schasinglulu 			(0x1 << PWRC_CPUN_CR_PWR_DN_RQ_OFFSET)
94*91f16700Schasinglulu #define PWRC_CPUN_CR_ISO_ENABLE_OFFSET		16
95*91f16700Schasinglulu #define PWRC_CPUN_CR_ISO_ENABLE_MASK		\
96*91f16700Schasinglulu 			(0x1 << PWRC_CPUN_CR_ISO_ENABLE_OFFSET)
97*91f16700Schasinglulu #define PWRC_CPUN_CR_LDO_BYPASS_RDY_MASK	\
98*91f16700Schasinglulu 			(0x1U << PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET)
99*91f16700Schasinglulu 
100*91f16700Schasinglulu #define CCU_B_PRCRN_REG(cpu_id)			\
101*91f16700Schasinglulu 			(MVEBU_REGS_BASE + 0x1A50 + \
102*91f16700Schasinglulu 			((cpu_id / 2) * (0x400)) + ((cpu_id % 2) * 4))
103*91f16700Schasinglulu #define CCU_B_PRCRN_CPUPORESET_STATIC_OFFSET	0
104*91f16700Schasinglulu #define CCU_B_PRCRN_CPUPORESET_STATIC_MASK	\
105*91f16700Schasinglulu 			(0x1 << CCU_B_PRCRN_CPUPORESET_STATIC_OFFSET)
106*91f16700Schasinglulu 
107*91f16700Schasinglulu /* power switch fingers */
108*91f16700Schasinglulu #define AP807_PWRC_LDO_CR0_REG			\
109*91f16700Schasinglulu 			(MVEBU_REGS_BASE + 0x680000 + 0x100)
110*91f16700Schasinglulu #define AP807_PWRC_LDO_CR0_OFFSET		16
111*91f16700Schasinglulu #define AP807_PWRC_LDO_CR0_MASK			\
112*91f16700Schasinglulu 			(0xff << AP807_PWRC_LDO_CR0_OFFSET)
113*91f16700Schasinglulu #define AP807_PWRC_LDO_CR0_VAL			0xfc
114*91f16700Schasinglulu 
115*91f16700Schasinglulu /*
116*91f16700Schasinglulu  * Power down CPU:
117*91f16700Schasinglulu  * Used to reduce power consumption, and avoid SoC unnecessary temperature rise.
118*91f16700Schasinglulu  */
119*91f16700Schasinglulu static int plat_marvell_cpu_powerdown(int cpu_id)
120*91f16700Schasinglulu {
121*91f16700Schasinglulu 	uint32_t	reg_val;
122*91f16700Schasinglulu 	int		exit_loop = REG_WR_VALIDATE_TIMEOUT;
123*91f16700Schasinglulu 
124*91f16700Schasinglulu 	INFO("Powering down CPU%d\n", cpu_id);
125*91f16700Schasinglulu 
126*91f16700Schasinglulu 	/* 1. Isolation enable */
127*91f16700Schasinglulu 	reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
128*91f16700Schasinglulu 	reg_val |= 0x1 << PWRC_CPUN_CR_ISO_ENABLE_OFFSET;
129*91f16700Schasinglulu 	mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
130*91f16700Schasinglulu 
131*91f16700Schasinglulu 	/* 2. Read and check Isolation enabled - verify bit set to 1 */
132*91f16700Schasinglulu 	do {
133*91f16700Schasinglulu 		reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
134*91f16700Schasinglulu 		exit_loop--;
135*91f16700Schasinglulu 	} while (!(reg_val & (0x1 << PWRC_CPUN_CR_ISO_ENABLE_OFFSET)) &&
136*91f16700Schasinglulu 		 exit_loop > 0);
137*91f16700Schasinglulu 
138*91f16700Schasinglulu 	/* 3. Switch off CPU power */
139*91f16700Schasinglulu 	reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
140*91f16700Schasinglulu 	reg_val &= ~PWRC_CPUN_CR_PWR_DN_RQ_MASK;
141*91f16700Schasinglulu 	mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
142*91f16700Schasinglulu 
143*91f16700Schasinglulu 	/* 4. Read and check Switch Off - verify bit set to 0 */
144*91f16700Schasinglulu 	exit_loop = REG_WR_VALIDATE_TIMEOUT;
145*91f16700Schasinglulu 	do {
146*91f16700Schasinglulu 		reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
147*91f16700Schasinglulu 		exit_loop--;
148*91f16700Schasinglulu 	} while (reg_val & PWRC_CPUN_CR_PWR_DN_RQ_MASK && exit_loop > 0);
149*91f16700Schasinglulu 
150*91f16700Schasinglulu 	if (exit_loop <= 0)
151*91f16700Schasinglulu 		goto cpu_poweroff_error;
152*91f16700Schasinglulu 
153*91f16700Schasinglulu 	/* 5. De-Assert power ready */
154*91f16700Schasinglulu 	reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
155*91f16700Schasinglulu 	reg_val &= ~PWRC_CPUN_CR_LDO_BYPASS_RDY_MASK;
156*91f16700Schasinglulu 	mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
157*91f16700Schasinglulu 
158*91f16700Schasinglulu 	/* 6. Assert CPU POR reset */
159*91f16700Schasinglulu 	reg_val = mmio_read_32(CCU_B_PRCRN_REG(cpu_id));
160*91f16700Schasinglulu 	reg_val &= ~CCU_B_PRCRN_CPUPORESET_STATIC_MASK;
161*91f16700Schasinglulu 	mmio_write_32(CCU_B_PRCRN_REG(cpu_id), reg_val);
162*91f16700Schasinglulu 
163*91f16700Schasinglulu 	/* 7. Read and poll on Validate the CPU is out of reset */
164*91f16700Schasinglulu 	exit_loop = REG_WR_VALIDATE_TIMEOUT;
165*91f16700Schasinglulu 	do {
166*91f16700Schasinglulu 		reg_val = mmio_read_32(CCU_B_PRCRN_REG(cpu_id));
167*91f16700Schasinglulu 		exit_loop--;
168*91f16700Schasinglulu 	} while (reg_val & CCU_B_PRCRN_CPUPORESET_STATIC_MASK && exit_loop > 0);
169*91f16700Schasinglulu 
170*91f16700Schasinglulu 	if (exit_loop <= 0)
171*91f16700Schasinglulu 		goto cpu_poweroff_error;
172*91f16700Schasinglulu 
173*91f16700Schasinglulu 	INFO("Successfully powered down CPU%d\n", cpu_id);
174*91f16700Schasinglulu 
175*91f16700Schasinglulu 	return 0;
176*91f16700Schasinglulu 
177*91f16700Schasinglulu cpu_poweroff_error:
178*91f16700Schasinglulu 	ERROR("ERROR: Can't power down CPU%d\n", cpu_id);
179*91f16700Schasinglulu 	return -1;
180*91f16700Schasinglulu }
181*91f16700Schasinglulu 
182*91f16700Schasinglulu /*
183*91f16700Schasinglulu  * Power down CPUs 1-3 at early boot stage,
184*91f16700Schasinglulu  * to reduce power consumption and SoC temperature.
185*91f16700Schasinglulu  * This is triggered by BLE prior to DDR initialization.
186*91f16700Schasinglulu  *
187*91f16700Schasinglulu  * Note:
188*91f16700Schasinglulu  * All CPUs will be powered up by plat_marvell_cpu_powerup on Linux boot stage,
189*91f16700Schasinglulu  * which is triggered by PSCI ops (pwr_domain_on).
190*91f16700Schasinglulu  */
191*91f16700Schasinglulu int plat_marvell_early_cpu_powerdown(void)
192*91f16700Schasinglulu {
193*91f16700Schasinglulu 	uint32_t cpu_cluster_status =
194*91f16700Schasinglulu 		mmio_read_32(FEATURE_DISABLE_STATUS_REG) &
195*91f16700Schasinglulu 			     FEATURE_DISABLE_STATUS_CPU_CLUSTER_MASK;
196*91f16700Schasinglulu 	/* if cpu_cluster_status bit is set,
197*91f16700Schasinglulu 	 * that means we have only single cluster
198*91f16700Schasinglulu 	 */
199*91f16700Schasinglulu 	int cluster_count = cpu_cluster_status ? 1 : 2;
200*91f16700Schasinglulu 
201*91f16700Schasinglulu 	INFO("Powering off unused CPUs\n");
202*91f16700Schasinglulu 
203*91f16700Schasinglulu 	/* CPU1 is in AP806 cluster-0, which always exists, so power it down */
204*91f16700Schasinglulu 	if (plat_marvell_cpu_powerdown(CPU1) == -1)
205*91f16700Schasinglulu 		return -1;
206*91f16700Schasinglulu 
207*91f16700Schasinglulu 	/*
208*91f16700Schasinglulu 	 * CPU2-3 are in AP806 2nd cluster (cluster-1),
209*91f16700Schasinglulu 	 * which doesn't exists in dual-core systems.
210*91f16700Schasinglulu 	 * so need to check if we have dual-core (single cluster)
211*91f16700Schasinglulu 	 * or quad-code (2 clusters)
212*91f16700Schasinglulu 	 */
213*91f16700Schasinglulu 	if (cluster_count == 2) {
214*91f16700Schasinglulu 		/* CPU2-3 are part of 2nd cluster */
215*91f16700Schasinglulu 		if (plat_marvell_cpu_powerdown(CPU2) == -1)
216*91f16700Schasinglulu 			return -1;
217*91f16700Schasinglulu 		if (plat_marvell_cpu_powerdown(CPU3) == -1)
218*91f16700Schasinglulu 			return -1;
219*91f16700Schasinglulu 	}
220*91f16700Schasinglulu 
221*91f16700Schasinglulu 	return 0;
222*91f16700Schasinglulu }
223*91f16700Schasinglulu 
224*91f16700Schasinglulu /*
225*91f16700Schasinglulu  * Power up CPU - part of Linux boot stage
226*91f16700Schasinglulu  */
227*91f16700Schasinglulu static int plat_marvell_cpu_powerup(u_register_t mpidr)
228*91f16700Schasinglulu {
229*91f16700Schasinglulu 	uint32_t	reg_val;
230*91f16700Schasinglulu 	int	cpu_id = MPIDR_CPU_GET(mpidr),
231*91f16700Schasinglulu 		cluster = MPIDR_CLUSTER_GET(mpidr);
232*91f16700Schasinglulu 	int	exit_loop = REG_WR_VALIDATE_TIMEOUT;
233*91f16700Schasinglulu 
234*91f16700Schasinglulu 	/* calculate absolute CPU ID */
235*91f16700Schasinglulu 	cpu_id = cluster * PLAT_MARVELL_CLUSTER_CORE_COUNT + cpu_id;
236*91f16700Schasinglulu 
237*91f16700Schasinglulu 	INFO("Powering on CPU%d\n", cpu_id);
238*91f16700Schasinglulu 
239*91f16700Schasinglulu #ifdef MVEBU_SOC_AP807
240*91f16700Schasinglulu 	/* Activate 2 power switch fingers */
241*91f16700Schasinglulu 	reg_val = mmio_read_32(AP807_PWRC_LDO_CR0_REG);
242*91f16700Schasinglulu 	reg_val &= ~(AP807_PWRC_LDO_CR0_MASK);
243*91f16700Schasinglulu 	reg_val |= (AP807_PWRC_LDO_CR0_VAL << AP807_PWRC_LDO_CR0_OFFSET);
244*91f16700Schasinglulu 	mmio_write_32(AP807_PWRC_LDO_CR0_REG, reg_val);
245*91f16700Schasinglulu 	udelay(100);
246*91f16700Schasinglulu #endif
247*91f16700Schasinglulu 
248*91f16700Schasinglulu 	/* 1. Switch CPU power ON */
249*91f16700Schasinglulu 	reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
250*91f16700Schasinglulu 	reg_val |= 0x1 << PWRC_CPUN_CR_PWR_DN_RQ_OFFSET;
251*91f16700Schasinglulu 	mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
252*91f16700Schasinglulu 
253*91f16700Schasinglulu 	/* 2. Wait for CPU on, up to 100 uSec: */
254*91f16700Schasinglulu 	udelay(100);
255*91f16700Schasinglulu 
256*91f16700Schasinglulu 	/* 3. Assert power ready */
257*91f16700Schasinglulu 	reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
258*91f16700Schasinglulu 	reg_val |= 0x1U << PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET;
259*91f16700Schasinglulu 	mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
260*91f16700Schasinglulu 
261*91f16700Schasinglulu 	/* 4. Read & Validate power ready
262*91f16700Schasinglulu 	 * used in order to generate 16 Host CPU cycles
263*91f16700Schasinglulu 	 */
264*91f16700Schasinglulu 	do {
265*91f16700Schasinglulu 		reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
266*91f16700Schasinglulu 		exit_loop--;
267*91f16700Schasinglulu 	} while (!(reg_val & (0x1U << PWRC_CPUN_CR_LDO_BYPASS_RDY_OFFSET)) &&
268*91f16700Schasinglulu 		 exit_loop > 0);
269*91f16700Schasinglulu 
270*91f16700Schasinglulu 	if (exit_loop <= 0)
271*91f16700Schasinglulu 		goto cpu_poweron_error;
272*91f16700Schasinglulu 
273*91f16700Schasinglulu 	/* 5. Isolation disable */
274*91f16700Schasinglulu 	reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
275*91f16700Schasinglulu 	reg_val &= ~PWRC_CPUN_CR_ISO_ENABLE_MASK;
276*91f16700Schasinglulu 	mmio_write_32(PWRC_CPUN_CR_REG(cpu_id), reg_val);
277*91f16700Schasinglulu 
278*91f16700Schasinglulu 	/* 6. Read and check Isolation enabled - verify bit set to 1 */
279*91f16700Schasinglulu 	exit_loop = REG_WR_VALIDATE_TIMEOUT;
280*91f16700Schasinglulu 	do {
281*91f16700Schasinglulu 		reg_val = mmio_read_32(PWRC_CPUN_CR_REG(cpu_id));
282*91f16700Schasinglulu 		exit_loop--;
283*91f16700Schasinglulu 	} while ((reg_val & (0x1 << PWRC_CPUN_CR_ISO_ENABLE_OFFSET)) &&
284*91f16700Schasinglulu 		 exit_loop > 0);
285*91f16700Schasinglulu 
286*91f16700Schasinglulu 	/* 7. De Assert CPU POR reset & Core reset */
287*91f16700Schasinglulu 	reg_val = mmio_read_32(CCU_B_PRCRN_REG(cpu_id));
288*91f16700Schasinglulu 	reg_val |= 0x1 << CCU_B_PRCRN_CPUPORESET_STATIC_OFFSET;
289*91f16700Schasinglulu 	mmio_write_32(CCU_B_PRCRN_REG(cpu_id), reg_val);
290*91f16700Schasinglulu 
291*91f16700Schasinglulu 	/* 8. Read & Validate CPU POR reset */
292*91f16700Schasinglulu 	exit_loop = REG_WR_VALIDATE_TIMEOUT;
293*91f16700Schasinglulu 	do {
294*91f16700Schasinglulu 		reg_val = mmio_read_32(CCU_B_PRCRN_REG(cpu_id));
295*91f16700Schasinglulu 		exit_loop--;
296*91f16700Schasinglulu 	} while (!(reg_val & (0x1 << CCU_B_PRCRN_CPUPORESET_STATIC_OFFSET)) &&
297*91f16700Schasinglulu 		 exit_loop > 0);
298*91f16700Schasinglulu 
299*91f16700Schasinglulu 	if (exit_loop <= 0)
300*91f16700Schasinglulu 		goto cpu_poweron_error;
301*91f16700Schasinglulu 
302*91f16700Schasinglulu 	INFO("Successfully powered on CPU%d\n", cpu_id);
303*91f16700Schasinglulu 
304*91f16700Schasinglulu 	return 0;
305*91f16700Schasinglulu 
306*91f16700Schasinglulu cpu_poweron_error:
307*91f16700Schasinglulu 	ERROR("ERROR: Can't power up CPU%d\n", cpu_id);
308*91f16700Schasinglulu 	return -1;
309*91f16700Schasinglulu }
310*91f16700Schasinglulu 
311*91f16700Schasinglulu static int plat_marvell_cpu_on(u_register_t mpidr)
312*91f16700Schasinglulu {
313*91f16700Schasinglulu 	int cpu_id;
314*91f16700Schasinglulu 	int cluster;
315*91f16700Schasinglulu 
316*91f16700Schasinglulu 	/* Set barierr */
317*91f16700Schasinglulu 	dsbsy();
318*91f16700Schasinglulu 
319*91f16700Schasinglulu 	/* Get cpu number - use CPU ID */
320*91f16700Schasinglulu 	cpu_id =  MPIDR_CPU_GET(mpidr);
321*91f16700Schasinglulu 
322*91f16700Schasinglulu 	/* Get cluster number - use affinity level 1 */
323*91f16700Schasinglulu 	cluster = MPIDR_CLUSTER_GET(mpidr);
324*91f16700Schasinglulu 
325*91f16700Schasinglulu 	/* Set CPU private UID */
326*91f16700Schasinglulu 	mmio_write_32(MVEBU_REGS_BASE + MVEBU_PRIVATE_UID_REG, cluster + 0x4);
327*91f16700Schasinglulu 
328*91f16700Schasinglulu 	/* Set the cpu start address to BL1 entry point (align to 0x10000) */
329*91f16700Schasinglulu 	mmio_write_32(MVEBU_CCU_RVBAR(cpu_id),
330*91f16700Schasinglulu 		      PLAT_MARVELL_CPU_ENTRY_ADDR >> 16);
331*91f16700Schasinglulu 
332*91f16700Schasinglulu 	/* Get the cpu out of reset */
333*91f16700Schasinglulu 	mmio_write_32(MVEBU_CCU_CPU_UN_RESET(cpu_id), 0x10001);
334*91f16700Schasinglulu 
335*91f16700Schasinglulu 	return 0;
336*91f16700Schasinglulu }
337*91f16700Schasinglulu 
338*91f16700Schasinglulu /*****************************************************************************
339*91f16700Schasinglulu  * A8K handler called to check the validity of the power state
340*91f16700Schasinglulu  * parameter.
341*91f16700Schasinglulu  *****************************************************************************
342*91f16700Schasinglulu  */
343*91f16700Schasinglulu static int a8k_validate_power_state(unsigned int power_state,
344*91f16700Schasinglulu 			    psci_power_state_t *req_state)
345*91f16700Schasinglulu {
346*91f16700Schasinglulu 	int pstate = psci_get_pstate_type(power_state);
347*91f16700Schasinglulu 	int pwr_lvl = psci_get_pstate_pwrlvl(power_state);
348*91f16700Schasinglulu 	int i;
349*91f16700Schasinglulu 
350*91f16700Schasinglulu 	if (pwr_lvl > PLAT_MAX_PWR_LVL)
351*91f16700Schasinglulu 		return PSCI_E_INVALID_PARAMS;
352*91f16700Schasinglulu 
353*91f16700Schasinglulu 	/* Sanity check the requested state */
354*91f16700Schasinglulu 	if (pstate == PSTATE_TYPE_STANDBY) {
355*91f16700Schasinglulu 		/*
356*91f16700Schasinglulu 		 * It's possible to enter standby only on power level 0
357*91f16700Schasinglulu 		 * Ignore any other power level.
358*91f16700Schasinglulu 		 */
359*91f16700Schasinglulu 		if (pwr_lvl != MARVELL_PWR_LVL0)
360*91f16700Schasinglulu 			return PSCI_E_INVALID_PARAMS;
361*91f16700Schasinglulu 
362*91f16700Schasinglulu 		req_state->pwr_domain_state[MARVELL_PWR_LVL0] =
363*91f16700Schasinglulu 					MARVELL_LOCAL_STATE_RET;
364*91f16700Schasinglulu 	} else {
365*91f16700Schasinglulu 		for (i = MARVELL_PWR_LVL0; i <= pwr_lvl; i++)
366*91f16700Schasinglulu 			req_state->pwr_domain_state[i] =
367*91f16700Schasinglulu 					MARVELL_LOCAL_STATE_OFF;
368*91f16700Schasinglulu 	}
369*91f16700Schasinglulu 
370*91f16700Schasinglulu 	/*
371*91f16700Schasinglulu 	 * We expect the 'state id' to be zero.
372*91f16700Schasinglulu 	 */
373*91f16700Schasinglulu 	if (psci_get_pstate_id(power_state))
374*91f16700Schasinglulu 		return PSCI_E_INVALID_PARAMS;
375*91f16700Schasinglulu 
376*91f16700Schasinglulu 	return PSCI_E_SUCCESS;
377*91f16700Schasinglulu }
378*91f16700Schasinglulu 
379*91f16700Schasinglulu /*****************************************************************************
380*91f16700Schasinglulu  * A8K handler called when a CPU is about to enter standby.
381*91f16700Schasinglulu  *****************************************************************************
382*91f16700Schasinglulu  */
383*91f16700Schasinglulu static void a8k_cpu_standby(plat_local_state_t cpu_state)
384*91f16700Schasinglulu {
385*91f16700Schasinglulu 	if (!is_pm_fw_running()) {
386*91f16700Schasinglulu 		ERROR("%s: needs to be implemented\n", __func__);
387*91f16700Schasinglulu 		panic();
388*91f16700Schasinglulu 	}
389*91f16700Schasinglulu }
390*91f16700Schasinglulu 
391*91f16700Schasinglulu /*****************************************************************************
392*91f16700Schasinglulu  * A8K handler called when a power domain is about to be turned on. The
393*91f16700Schasinglulu  * mpidr determines the CPU to be turned on.
394*91f16700Schasinglulu  *****************************************************************************
395*91f16700Schasinglulu  */
396*91f16700Schasinglulu static int a8k_pwr_domain_on(u_register_t mpidr)
397*91f16700Schasinglulu {
398*91f16700Schasinglulu 	/* Power up CPU (CPUs 1-3 are powered off at start of BLE) */
399*91f16700Schasinglulu 	plat_marvell_cpu_powerup(mpidr);
400*91f16700Schasinglulu 
401*91f16700Schasinglulu #if MSS_SUPPORT
402*91f16700Schasinglulu 	if (is_pm_fw_running()) {
403*91f16700Schasinglulu 		unsigned int target =
404*91f16700Schasinglulu 				((mpidr & 0xFF) + (((mpidr >> 8) & 0xFF) * 2));
405*91f16700Schasinglulu 
406*91f16700Schasinglulu 		/*
407*91f16700Schasinglulu 		 * pm system synchronization - used to synchronize
408*91f16700Schasinglulu 		 * multiple core access to MSS
409*91f16700Schasinglulu 		 */
410*91f16700Schasinglulu 		bakery_lock_get(&pm_sys_lock);
411*91f16700Schasinglulu 
412*91f16700Schasinglulu 		/* send CPU ON IPC Message to MSS */
413*91f16700Schasinglulu 		mss_pm_ipc_msg_send(target, PM_IPC_MSG_CPU_ON, 0);
414*91f16700Schasinglulu 
415*91f16700Schasinglulu 		/* trigger IPC message to MSS */
416*91f16700Schasinglulu 		mss_pm_ipc_msg_trigger();
417*91f16700Schasinglulu 
418*91f16700Schasinglulu 		/* pm system synchronization */
419*91f16700Schasinglulu 		bakery_lock_release(&pm_sys_lock);
420*91f16700Schasinglulu 
421*91f16700Schasinglulu 		/* trace message */
422*91f16700Schasinglulu 		PM_TRACE(TRACE_PWR_DOMAIN_ON | target);
423*91f16700Schasinglulu 	} else
424*91f16700Schasinglulu #endif
425*91f16700Schasinglulu 	{
426*91f16700Schasinglulu 		/* proprietary CPU ON execution flow */
427*91f16700Schasinglulu 		plat_marvell_cpu_on(mpidr);
428*91f16700Schasinglulu 	}
429*91f16700Schasinglulu 	return 0;
430*91f16700Schasinglulu }
431*91f16700Schasinglulu 
432*91f16700Schasinglulu /*****************************************************************************
433*91f16700Schasinglulu  * A8K handler called to validate the entry point.
434*91f16700Schasinglulu  *****************************************************************************
435*91f16700Schasinglulu  */
436*91f16700Schasinglulu static int a8k_validate_ns_entrypoint(uintptr_t entrypoint)
437*91f16700Schasinglulu {
438*91f16700Schasinglulu 	return PSCI_E_SUCCESS;
439*91f16700Schasinglulu }
440*91f16700Schasinglulu 
441*91f16700Schasinglulu /*****************************************************************************
442*91f16700Schasinglulu  * A8K handler called when a power domain is about to be turned off. The
443*91f16700Schasinglulu  * target_state encodes the power state that each level should transition to.
444*91f16700Schasinglulu  *****************************************************************************
445*91f16700Schasinglulu  */
446*91f16700Schasinglulu static void a8k_pwr_domain_off(const psci_power_state_t *target_state)
447*91f16700Schasinglulu {
448*91f16700Schasinglulu #if MSS_SUPPORT
449*91f16700Schasinglulu 	if (is_pm_fw_running()) {
450*91f16700Schasinglulu 		unsigned int idx = plat_my_core_pos();
451*91f16700Schasinglulu 
452*91f16700Schasinglulu 		/* Prevent interrupts from spuriously waking up this cpu */
453*91f16700Schasinglulu 		gicv2_cpuif_disable();
454*91f16700Schasinglulu 
455*91f16700Schasinglulu 		/* pm system synchronization - used to synchronize multiple
456*91f16700Schasinglulu 		 * core access to MSS
457*91f16700Schasinglulu 		 */
458*91f16700Schasinglulu 		bakery_lock_get(&pm_sys_lock);
459*91f16700Schasinglulu 
460*91f16700Schasinglulu 		/* send CPU OFF IPC Message to MSS */
461*91f16700Schasinglulu 		mss_pm_ipc_msg_send(idx, PM_IPC_MSG_CPU_OFF, target_state);
462*91f16700Schasinglulu 
463*91f16700Schasinglulu 		/* trigger IPC message to MSS */
464*91f16700Schasinglulu 		mss_pm_ipc_msg_trigger();
465*91f16700Schasinglulu 
466*91f16700Schasinglulu 		/* pm system synchronization */
467*91f16700Schasinglulu 		bakery_lock_release(&pm_sys_lock);
468*91f16700Schasinglulu 
469*91f16700Schasinglulu 		/* trace message */
470*91f16700Schasinglulu 		PM_TRACE(TRACE_PWR_DOMAIN_OFF);
471*91f16700Schasinglulu 	} else {
472*91f16700Schasinglulu 		INFO("%s: is not supported without SCP\n", __func__);
473*91f16700Schasinglulu 	}
474*91f16700Schasinglulu #endif
475*91f16700Schasinglulu }
476*91f16700Schasinglulu 
477*91f16700Schasinglulu /* Get PM config to power off the SoC */
478*91f16700Schasinglulu void *plat_marvell_get_pm_cfg(void)
479*91f16700Schasinglulu {
480*91f16700Schasinglulu 	return NULL;
481*91f16700Schasinglulu }
482*91f16700Schasinglulu 
483*91f16700Schasinglulu /*
484*91f16700Schasinglulu  * This function should be called on restore from
485*91f16700Schasinglulu  * "suspend to RAM" state when the execution flow
486*91f16700Schasinglulu  * has to bypass BootROM image to RAM copy and speed up
487*91f16700Schasinglulu  * the system recovery
488*91f16700Schasinglulu  *
489*91f16700Schasinglulu  */
490*91f16700Schasinglulu static void plat_marvell_exit_bootrom(void)
491*91f16700Schasinglulu {
492*91f16700Schasinglulu 	marvell_exit_bootrom(PLAT_MARVELL_TRUSTED_ROM_BASE);
493*91f16700Schasinglulu }
494*91f16700Schasinglulu 
495*91f16700Schasinglulu /*
496*91f16700Schasinglulu  * Prepare for the power off of the system via GPIO
497*91f16700Schasinglulu  */
498*91f16700Schasinglulu static void plat_marvell_power_off_gpio(struct power_off_method *pm_cfg,
499*91f16700Schasinglulu 					register_t *gpio_addr,
500*91f16700Schasinglulu 					register_t *gpio_data)
501*91f16700Schasinglulu {
502*91f16700Schasinglulu 	unsigned int gpio;
503*91f16700Schasinglulu 	unsigned int idx;
504*91f16700Schasinglulu 	unsigned int shift;
505*91f16700Schasinglulu 	unsigned int reg;
506*91f16700Schasinglulu 	unsigned int addr;
507*91f16700Schasinglulu 	gpio_info_t *info;
508*91f16700Schasinglulu 	unsigned int tog_bits;
509*91f16700Schasinglulu 
510*91f16700Schasinglulu 	assert((pm_cfg->cfg.gpio.pin_count < PMIC_GPIO_MAX_NUMBER) &&
511*91f16700Schasinglulu 	       (pm_cfg->cfg.gpio.step_count < PMIC_GPIO_MAX_TOGGLE_STEP));
512*91f16700Schasinglulu 
513*91f16700Schasinglulu 	/* Prepare GPIOs for PMIC */
514*91f16700Schasinglulu 	for (gpio = 0; gpio < pm_cfg->cfg.gpio.pin_count; gpio++) {
515*91f16700Schasinglulu 		info = &pm_cfg->cfg.gpio.info[gpio];
516*91f16700Schasinglulu 		/* Set PMIC GPIO to output mode */
517*91f16700Schasinglulu 		reg = mmio_read_32(MVEBU_CP_GPIO_DATA_OUT_EN(
518*91f16700Schasinglulu 				   info->cp_index, info->gpio_index));
519*91f16700Schasinglulu 		mmio_write_32(MVEBU_CP_GPIO_DATA_OUT_EN(
520*91f16700Schasinglulu 			      info->cp_index, info->gpio_index),
521*91f16700Schasinglulu 			      reg & ~MVEBU_GPIO_MASK(info->gpio_index));
522*91f16700Schasinglulu 
523*91f16700Schasinglulu 		/* Set the appropriate MPP to GPIO mode */
524*91f16700Schasinglulu 		reg = mmio_read_32(MVEBU_PM_MPP_REGS(info->cp_index,
525*91f16700Schasinglulu 						     info->gpio_index));
526*91f16700Schasinglulu 		mmio_write_32(MVEBU_PM_MPP_REGS(info->cp_index,
527*91f16700Schasinglulu 						info->gpio_index),
528*91f16700Schasinglulu 			reg & ~MVEBU_MPP_MASK(info->gpio_index));
529*91f16700Schasinglulu 	}
530*91f16700Schasinglulu 
531*91f16700Schasinglulu 	/* Wait for MPP & GPIO pre-configurations done */
532*91f16700Schasinglulu 	mdelay(pm_cfg->cfg.gpio.delay_ms);
533*91f16700Schasinglulu 
534*91f16700Schasinglulu 	/* Toggle the GPIO values, and leave final step to be triggered
535*91f16700Schasinglulu 	 * after  DDR self-refresh is enabled
536*91f16700Schasinglulu 	 */
537*91f16700Schasinglulu 	for (idx = 0; idx < pm_cfg->cfg.gpio.step_count; idx++) {
538*91f16700Schasinglulu 		tog_bits = pm_cfg->cfg.gpio.seq[idx];
539*91f16700Schasinglulu 
540*91f16700Schasinglulu 		/* The GPIOs must be within same GPIO register,
541*91f16700Schasinglulu 		 * thus could get the original value by first GPIO
542*91f16700Schasinglulu 		 */
543*91f16700Schasinglulu 		info = &pm_cfg->cfg.gpio.info[0];
544*91f16700Schasinglulu 		reg = mmio_read_32(MVEBU_CP_GPIO_DATA_OUT(
545*91f16700Schasinglulu 				   info->cp_index, info->gpio_index));
546*91f16700Schasinglulu 		addr = MVEBU_CP_GPIO_DATA_OUT(info->cp_index, info->gpio_index);
547*91f16700Schasinglulu 
548*91f16700Schasinglulu 		for (gpio = 0; gpio < pm_cfg->cfg.gpio.pin_count; gpio++) {
549*91f16700Schasinglulu 			shift = pm_cfg->cfg.gpio.info[gpio].gpio_index % 32;
550*91f16700Schasinglulu 			if (GPIO_LOW == (tog_bits & (1 << gpio)))
551*91f16700Schasinglulu 				reg &= ~(1 << shift);
552*91f16700Schasinglulu 			else
553*91f16700Schasinglulu 				reg |= (1 << shift);
554*91f16700Schasinglulu 		}
555*91f16700Schasinglulu 
556*91f16700Schasinglulu 		/* Set the GPIO register, for last step just store
557*91f16700Schasinglulu 		 * register address and values to system registers
558*91f16700Schasinglulu 		 */
559*91f16700Schasinglulu 		if (idx < pm_cfg->cfg.gpio.step_count - 1) {
560*91f16700Schasinglulu 			mmio_write_32(MVEBU_CP_GPIO_DATA_OUT(
561*91f16700Schasinglulu 				      info->cp_index, info->gpio_index), reg);
562*91f16700Schasinglulu 			mdelay(pm_cfg->cfg.gpio.delay_ms);
563*91f16700Schasinglulu 		} else {
564*91f16700Schasinglulu 			/* Save GPIO register and address values for
565*91f16700Schasinglulu 			 * finishing the power down operation later
566*91f16700Schasinglulu 			 */
567*91f16700Schasinglulu 			*gpio_addr = addr;
568*91f16700Schasinglulu 			*gpio_data = reg;
569*91f16700Schasinglulu 		}
570*91f16700Schasinglulu 	}
571*91f16700Schasinglulu }
572*91f16700Schasinglulu 
573*91f16700Schasinglulu /*
574*91f16700Schasinglulu  * Prepare for the power off of the system
575*91f16700Schasinglulu  */
576*91f16700Schasinglulu static void plat_marvell_power_off_prepare(struct power_off_method *pm_cfg,
577*91f16700Schasinglulu 					   register_t *addr, register_t *data)
578*91f16700Schasinglulu {
579*91f16700Schasinglulu 	switch (pm_cfg->type) {
580*91f16700Schasinglulu 	case PMIC_GPIO:
581*91f16700Schasinglulu 		plat_marvell_power_off_gpio(pm_cfg, addr, data);
582*91f16700Schasinglulu 		break;
583*91f16700Schasinglulu 	default:
584*91f16700Schasinglulu 		break;
585*91f16700Schasinglulu 	}
586*91f16700Schasinglulu }
587*91f16700Schasinglulu 
588*91f16700Schasinglulu /*****************************************************************************
589*91f16700Schasinglulu  * A8K handler called when a power domain is about to be suspended. The
590*91f16700Schasinglulu  * target_state encodes the power state that each level should transition to.
591*91f16700Schasinglulu  *****************************************************************************
592*91f16700Schasinglulu  */
593*91f16700Schasinglulu static void a8k_pwr_domain_suspend(const psci_power_state_t *target_state)
594*91f16700Schasinglulu {
595*91f16700Schasinglulu #if MSS_SUPPORT
596*91f16700Schasinglulu 	if (is_pm_fw_running()) {
597*91f16700Schasinglulu 		unsigned int idx;
598*91f16700Schasinglulu 
599*91f16700Schasinglulu 		/* Prevent interrupts from spuriously waking up this cpu */
600*91f16700Schasinglulu 		gicv2_cpuif_disable();
601*91f16700Schasinglulu 
602*91f16700Schasinglulu 		idx = plat_my_core_pos();
603*91f16700Schasinglulu 
604*91f16700Schasinglulu 		/* pm system synchronization - used to synchronize multiple
605*91f16700Schasinglulu 		 * core access to MSS
606*91f16700Schasinglulu 		 */
607*91f16700Schasinglulu 		bakery_lock_get(&pm_sys_lock);
608*91f16700Schasinglulu 
609*91f16700Schasinglulu 		/* send CPU Suspend IPC Message to MSS */
610*91f16700Schasinglulu 		mss_pm_ipc_msg_send(idx, PM_IPC_MSG_CPU_SUSPEND, target_state);
611*91f16700Schasinglulu 
612*91f16700Schasinglulu 		/* trigger IPC message to MSS */
613*91f16700Schasinglulu 		mss_pm_ipc_msg_trigger();
614*91f16700Schasinglulu 
615*91f16700Schasinglulu 		/* pm system synchronization */
616*91f16700Schasinglulu 		bakery_lock_release(&pm_sys_lock);
617*91f16700Schasinglulu 
618*91f16700Schasinglulu 		/* trace message */
619*91f16700Schasinglulu 		PM_TRACE(TRACE_PWR_DOMAIN_SUSPEND);
620*91f16700Schasinglulu 	} else
621*91f16700Schasinglulu #endif
622*91f16700Schasinglulu 	{
623*91f16700Schasinglulu 		uintptr_t *mailbox = (void *)PLAT_MARVELL_MAILBOX_BASE;
624*91f16700Schasinglulu 
625*91f16700Schasinglulu 		INFO("Suspending to RAM\n");
626*91f16700Schasinglulu 
627*91f16700Schasinglulu 		marvell_console_runtime_end();
628*91f16700Schasinglulu 
629*91f16700Schasinglulu 		/* Prevent interrupts from spuriously waking up this cpu */
630*91f16700Schasinglulu 		gicv2_cpuif_disable();
631*91f16700Schasinglulu 
632*91f16700Schasinglulu 		mailbox[MBOX_IDX_SUSPEND_MAGIC] = MVEBU_MAILBOX_SUSPEND_STATE;
633*91f16700Schasinglulu 		mailbox[MBOX_IDX_ROM_EXIT_ADDR] = (uintptr_t)&plat_marvell_exit_bootrom;
634*91f16700Schasinglulu 
635*91f16700Schasinglulu #if PLAT_MARVELL_SHARED_RAM_CACHED
636*91f16700Schasinglulu 		flush_dcache_range(PLAT_MARVELL_MAILBOX_BASE +
637*91f16700Schasinglulu 		MBOX_IDX_SUSPEND_MAGIC * sizeof(uintptr_t),
638*91f16700Schasinglulu 		2 * sizeof(uintptr_t));
639*91f16700Schasinglulu #endif
640*91f16700Schasinglulu 		/* Flush and disable LLC before going off-power */
641*91f16700Schasinglulu 		llc_disable(0);
642*91f16700Schasinglulu 
643*91f16700Schasinglulu 		isb();
644*91f16700Schasinglulu 		/*
645*91f16700Schasinglulu 		 * Do not halt here!
646*91f16700Schasinglulu 		 * The function must return for allowing the caller function
647*91f16700Schasinglulu 		 * psci_power_up_finish() to do the proper context saving and
648*91f16700Schasinglulu 		 * to release the CPU lock.
649*91f16700Schasinglulu 		 */
650*91f16700Schasinglulu 	}
651*91f16700Schasinglulu }
652*91f16700Schasinglulu 
653*91f16700Schasinglulu /*****************************************************************************
654*91f16700Schasinglulu  * A8K handler called when a power domain has just been powered on after
655*91f16700Schasinglulu  * being turned off earlier. The target_state encodes the low power state that
656*91f16700Schasinglulu  * each level has woken up from.
657*91f16700Schasinglulu  *****************************************************************************
658*91f16700Schasinglulu  */
659*91f16700Schasinglulu static void a8k_pwr_domain_on_finish(const psci_power_state_t *target_state)
660*91f16700Schasinglulu {
661*91f16700Schasinglulu 	/* arch specific configuration */
662*91f16700Schasinglulu 	marvell_psci_arch_init(0);
663*91f16700Schasinglulu 
664*91f16700Schasinglulu 	/* Interrupt initialization */
665*91f16700Schasinglulu 	gicv2_pcpu_distif_init();
666*91f16700Schasinglulu 	gicv2_cpuif_enable();
667*91f16700Schasinglulu 
668*91f16700Schasinglulu 	if (is_pm_fw_running()) {
669*91f16700Schasinglulu 		/* trace message */
670*91f16700Schasinglulu 		PM_TRACE(TRACE_PWR_DOMAIN_ON_FINISH);
671*91f16700Schasinglulu 	}
672*91f16700Schasinglulu }
673*91f16700Schasinglulu 
674*91f16700Schasinglulu /*****************************************************************************
675*91f16700Schasinglulu  * A8K handler called when a power domain has just been powered on after
676*91f16700Schasinglulu  * having been suspended earlier. The target_state encodes the low power state
677*91f16700Schasinglulu  * that each level has woken up from.
678*91f16700Schasinglulu  * TODO: At the moment we reuse the on finisher and reinitialize the secure
679*91f16700Schasinglulu  * context. Need to implement a separate suspend finisher.
680*91f16700Schasinglulu  *****************************************************************************
681*91f16700Schasinglulu  */
682*91f16700Schasinglulu static void a8k_pwr_domain_suspend_finish(
683*91f16700Schasinglulu 					const psci_power_state_t *target_state)
684*91f16700Schasinglulu {
685*91f16700Schasinglulu 	if (is_pm_fw_running()) {
686*91f16700Schasinglulu 		/* arch specific configuration */
687*91f16700Schasinglulu 		marvell_psci_arch_init(0);
688*91f16700Schasinglulu 
689*91f16700Schasinglulu 		/* Interrupt initialization */
690*91f16700Schasinglulu 		gicv2_cpuif_enable();
691*91f16700Schasinglulu 
692*91f16700Schasinglulu 		/* trace message */
693*91f16700Schasinglulu 		PM_TRACE(TRACE_PWR_DOMAIN_SUSPEND_FINISH);
694*91f16700Schasinglulu 	} else {
695*91f16700Schasinglulu 		uintptr_t *mailbox = (void *)PLAT_MARVELL_MAILBOX_BASE;
696*91f16700Schasinglulu 
697*91f16700Schasinglulu 		/* Only primary CPU requres platform init */
698*91f16700Schasinglulu 		if (!plat_my_core_pos()) {
699*91f16700Schasinglulu 			/* Initialize the console to provide
700*91f16700Schasinglulu 			 * early debug support
701*91f16700Schasinglulu 			 */
702*91f16700Schasinglulu 			marvell_console_runtime_init();
703*91f16700Schasinglulu 
704*91f16700Schasinglulu 			bl31_plat_arch_setup();
705*91f16700Schasinglulu 			marvell_bl31_platform_setup();
706*91f16700Schasinglulu 			/*
707*91f16700Schasinglulu 			 * Remove suspend to RAM marker from the mailbox
708*91f16700Schasinglulu 			 * for treating a regular reset as a cold boot
709*91f16700Schasinglulu 			 */
710*91f16700Schasinglulu 			mailbox[MBOX_IDX_SUSPEND_MAGIC] = 0;
711*91f16700Schasinglulu 			mailbox[MBOX_IDX_ROM_EXIT_ADDR] = 0;
712*91f16700Schasinglulu #if PLAT_MARVELL_SHARED_RAM_CACHED
713*91f16700Schasinglulu 			flush_dcache_range(PLAT_MARVELL_MAILBOX_BASE +
714*91f16700Schasinglulu 			MBOX_IDX_SUSPEND_MAGIC * sizeof(uintptr_t),
715*91f16700Schasinglulu 			2 * sizeof(uintptr_t));
716*91f16700Schasinglulu #endif
717*91f16700Schasinglulu 		}
718*91f16700Schasinglulu 	}
719*91f16700Schasinglulu }
720*91f16700Schasinglulu 
721*91f16700Schasinglulu /*****************************************************************************
722*91f16700Schasinglulu  * This handler is called by the PSCI implementation during the `SYSTEM_SUSPEND`
723*91f16700Schasinglulu  * call to get the `power_state` parameter. This allows the platform to encode
724*91f16700Schasinglulu  * the appropriate State-ID field within the `power_state` parameter which can
725*91f16700Schasinglulu  * be utilized in `pwr_domain_suspend()` to suspend to system affinity level.
726*91f16700Schasinglulu  *****************************************************************************
727*91f16700Schasinglulu  */
728*91f16700Schasinglulu static void a8k_get_sys_suspend_power_state(psci_power_state_t *req_state)
729*91f16700Schasinglulu {
730*91f16700Schasinglulu 	/* lower affinities use PLAT_MAX_OFF_STATE */
731*91f16700Schasinglulu 	for (int i = MPIDR_AFFLVL0; i <= PLAT_MAX_PWR_LVL; i++)
732*91f16700Schasinglulu 		req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
733*91f16700Schasinglulu }
734*91f16700Schasinglulu 
735*91f16700Schasinglulu static void
736*91f16700Schasinglulu __dead2 a8k_pwr_domain_pwr_down_wfi(const psci_power_state_t *target_state)
737*91f16700Schasinglulu {
738*91f16700Schasinglulu 	struct power_off_method *pm_cfg;
739*91f16700Schasinglulu 	unsigned int srcmd;
740*91f16700Schasinglulu 	unsigned int sdram_reg;
741*91f16700Schasinglulu 	register_t gpio_data = 0, gpio_addr = 0;
742*91f16700Schasinglulu 
743*91f16700Schasinglulu 	if (is_pm_fw_running()) {
744*91f16700Schasinglulu 		psci_power_down_wfi();
745*91f16700Schasinglulu 		panic();
746*91f16700Schasinglulu 	}
747*91f16700Schasinglulu 
748*91f16700Schasinglulu 	pm_cfg = (struct power_off_method *)plat_marvell_get_pm_cfg();
749*91f16700Schasinglulu 
750*91f16700Schasinglulu 	/* Prepare for power off */
751*91f16700Schasinglulu 	plat_marvell_power_off_prepare(pm_cfg, &gpio_addr, &gpio_data);
752*91f16700Schasinglulu 
753*91f16700Schasinglulu 	/* First step to enable DDR self-refresh
754*91f16700Schasinglulu 	 * to keep the data during suspend
755*91f16700Schasinglulu 	 */
756*91f16700Schasinglulu 	mmio_write_32(MVEBU_MC_PWR_CTRL_REG, 0x8C1);
757*91f16700Schasinglulu 
758*91f16700Schasinglulu 	/* Save DDR self-refresh second step register
759*91f16700Schasinglulu 	 * and value to be issued later
760*91f16700Schasinglulu 	 */
761*91f16700Schasinglulu 	sdram_reg = MVEBU_USER_CMD_0_REG;
762*91f16700Schasinglulu 	srcmd = mmio_read_32(sdram_reg);
763*91f16700Schasinglulu 	srcmd &= ~(MVEBU_USER_CMD_CH0_MASK | MVEBU_USER_CMD_CS_MASK |
764*91f16700Schasinglulu 		 MVEBU_USER_CMD_SR_MASK);
765*91f16700Schasinglulu 	srcmd |= (MVEBU_USER_CMD_CH0_EN | MVEBU_USER_CMD_CS_ALL |
766*91f16700Schasinglulu 		 MVEBU_USER_CMD_SR_ENTER);
767*91f16700Schasinglulu 
768*91f16700Schasinglulu 	/*
769*91f16700Schasinglulu 	 * Wait for DRAM is done using registers access only.
770*91f16700Schasinglulu 	 * At this stage any access to DRAM (procedure call) will
771*91f16700Schasinglulu 	 * release it from the self-refresh mode
772*91f16700Schasinglulu 	 */
773*91f16700Schasinglulu 	__asm__ volatile (
774*91f16700Schasinglulu 		/* Align to a cache line */
775*91f16700Schasinglulu 		"	.balign 64\n\t"
776*91f16700Schasinglulu 
777*91f16700Schasinglulu 		/* Enter self refresh */
778*91f16700Schasinglulu 		"	str %[srcmd], [%[sdram_reg]]\n\t"
779*91f16700Schasinglulu 
780*91f16700Schasinglulu 		/*
781*91f16700Schasinglulu 		 * Wait 100 cycles for DDR to enter self refresh, by
782*91f16700Schasinglulu 		 * doing 50 times two instructions.
783*91f16700Schasinglulu 		 */
784*91f16700Schasinglulu 		"	mov x1, #50\n\t"
785*91f16700Schasinglulu 		"1:	subs x1, x1, #1\n\t"
786*91f16700Schasinglulu 		"	bne 1b\n\t"
787*91f16700Schasinglulu 
788*91f16700Schasinglulu 		/* Issue the command to trigger the SoC power off */
789*91f16700Schasinglulu 		"	str	%[gpio_data], [%[gpio_addr]]\n\t"
790*91f16700Schasinglulu 
791*91f16700Schasinglulu 		/* Trap the processor */
792*91f16700Schasinglulu 		"	b .\n\t"
793*91f16700Schasinglulu 		: : [srcmd] "r" (srcmd), [sdram_reg] "r" (sdram_reg),
794*91f16700Schasinglulu 		    [gpio_addr] "r" (gpio_addr),  [gpio_data] "r" (gpio_data)
795*91f16700Schasinglulu 		: "x1");
796*91f16700Schasinglulu 
797*91f16700Schasinglulu 	panic();
798*91f16700Schasinglulu }
799*91f16700Schasinglulu 
800*91f16700Schasinglulu /*****************************************************************************
801*91f16700Schasinglulu  * A8K handlers to shutdown/reboot the system
802*91f16700Schasinglulu  *****************************************************************************
803*91f16700Schasinglulu  */
804*91f16700Schasinglulu 
805*91f16700Schasinglulu /* Set a weak stub for platforms that don't configure system power off */
806*91f16700Schasinglulu #pragma weak system_power_off
807*91f16700Schasinglulu int system_power_off(void)
808*91f16700Schasinglulu {
809*91f16700Schasinglulu 	return 0;
810*91f16700Schasinglulu }
811*91f16700Schasinglulu 
812*91f16700Schasinglulu static void __dead2 a8k_system_off(void)
813*91f16700Schasinglulu {
814*91f16700Schasinglulu 	/* Call the platform specific system power off function */
815*91f16700Schasinglulu 	system_power_off();
816*91f16700Schasinglulu 
817*91f16700Schasinglulu 	/* board doesn't have a system off implementation */
818*91f16700Schasinglulu 	ERROR("%s:  needs to be implemented\n", __func__);
819*91f16700Schasinglulu 	panic();
820*91f16700Schasinglulu }
821*91f16700Schasinglulu 
822*91f16700Schasinglulu void plat_marvell_system_reset(void)
823*91f16700Schasinglulu {
824*91f16700Schasinglulu 	mmio_write_32(MVEBU_RFU_BASE + MVEBU_RFU_GLOBL_SW_RST, 0x0);
825*91f16700Schasinglulu }
826*91f16700Schasinglulu 
827*91f16700Schasinglulu static void __dead2 a8k_system_reset(void)
828*91f16700Schasinglulu {
829*91f16700Schasinglulu 	plat_marvell_system_reset();
830*91f16700Schasinglulu 
831*91f16700Schasinglulu 	/* we shouldn't get to this point */
832*91f16700Schasinglulu 	panic();
833*91f16700Schasinglulu }
834*91f16700Schasinglulu 
835*91f16700Schasinglulu /*****************************************************************************
836*91f16700Schasinglulu  * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
837*91f16700Schasinglulu  * platform layer will take care of registering the handlers with PSCI.
838*91f16700Schasinglulu  *****************************************************************************
839*91f16700Schasinglulu  */
840*91f16700Schasinglulu const plat_psci_ops_t plat_arm_psci_pm_ops = {
841*91f16700Schasinglulu 	.cpu_standby = a8k_cpu_standby,
842*91f16700Schasinglulu 	.pwr_domain_on = a8k_pwr_domain_on,
843*91f16700Schasinglulu 	.pwr_domain_off = a8k_pwr_domain_off,
844*91f16700Schasinglulu 	.pwr_domain_suspend = a8k_pwr_domain_suspend,
845*91f16700Schasinglulu 	.pwr_domain_on_finish = a8k_pwr_domain_on_finish,
846*91f16700Schasinglulu 	.get_sys_suspend_power_state = a8k_get_sys_suspend_power_state,
847*91f16700Schasinglulu 	.pwr_domain_suspend_finish = a8k_pwr_domain_suspend_finish,
848*91f16700Schasinglulu 	.pwr_domain_pwr_down_wfi = a8k_pwr_domain_pwr_down_wfi,
849*91f16700Schasinglulu 	.system_off = a8k_system_off,
850*91f16700Schasinglulu 	.system_reset = a8k_system_reset,
851*91f16700Schasinglulu 	.validate_power_state = a8k_validate_power_state,
852*91f16700Schasinglulu 	.validate_ns_entrypoint = a8k_validate_ns_entrypoint
853*91f16700Schasinglulu };
854