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