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