sunxi_pm.c 2.92 KB
Newer Older
1
/*
2
 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
8

9
#include <platform_def.h>
10
11
12
13
14
15
16
17
18

#include <arch_helpers.h>
#include <common/debug.h>
#include <drivers/arm/gicv2.h>
#include <drivers/delay_timer.h>
#include <lib/mmio.h>
#include <lib/psci/psci.h>
#include <plat/common/platform.h>

19
#include <sunxi_cpucfg.h>
20
21
#include <sunxi_mmap.h>
#include <sunxi_private.h>
22

23
24
25
#define SUNXI_WDOG0_CTRL_REG		(SUNXI_R_WDOG_BASE + 0x0010)
#define SUNXI_WDOG0_CFG_REG		(SUNXI_R_WDOG_BASE + 0x0014)
#define SUNXI_WDOG0_MODE_REG		(SUNXI_R_WDOG_BASE + 0x0018)
26

27
28
29
30
31
32
33
34
35
36
37
#define mpidr_is_valid(mpidr) ( \
	MPIDR_AFFLVL3_VAL(mpidr) == 0 && \
	MPIDR_AFFLVL2_VAL(mpidr) == 0 && \
	MPIDR_AFFLVL1_VAL(mpidr) < PLATFORM_CLUSTER_COUNT && \
	MPIDR_AFFLVL0_VAL(mpidr) < PLATFORM_MAX_CPUS_PER_CLUSTER)

static int sunxi_pwr_domain_on(u_register_t mpidr)
{
	if (mpidr_is_valid(mpidr) == 0)
		return PSCI_E_INTERN_FAIL;

38
	sunxi_cpu_on(mpidr);
39
40
41
42
43
44
45
46
47

	return PSCI_E_SUCCESS;
}

static void sunxi_pwr_domain_off(const psci_power_state_t *target_state)
{
	gicv2_cpuif_disable();
}

48
49
static void __dead2 sunxi_pwr_down_wfi(const psci_power_state_t *target_state)
{
50
	sunxi_cpu_off(read_mpidr());
51
52
53
54
55

	while (1)
		wfi();
}

56
57
58
59
60
61
static void sunxi_pwr_domain_on_finish(const psci_power_state_t *target_state)
{
	gicv2_pcpu_distif_init();
	gicv2_cpuif_enable();
}

62
63
static void __dead2 sunxi_system_off(void)
{
64
	/* Turn off all secondary CPUs */
65
	sunxi_disable_secondary_cpus(read_mpidr());
66

67
	sunxi_power_down();
68
69
70
71
72

	udelay(1000);
	ERROR("PSCI: Cannot turn off system, halting\n");
	wfi();
	panic();
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
}

static void __dead2 sunxi_system_reset(void)
{
	/* Reset the whole system when the watchdog times out */
	mmio_write_32(SUNXI_WDOG0_CFG_REG, 1);
	/* Enable the watchdog with the shortest timeout (0.5 seconds) */
	mmio_write_32(SUNXI_WDOG0_MODE_REG, (0 << 4) | 1);
	/* Wait for twice the watchdog timeout before panicking */
	mdelay(1000);

	ERROR("PSCI: System reset failed\n");
	wfi();
	panic();
}

89
90
91
static int sunxi_validate_ns_entrypoint(uintptr_t ns_entrypoint)
{
	/* The non-secure entry point must be in DRAM */
92
	if (ns_entrypoint >= SUNXI_DRAM_BASE)
93
94
95
96
97
		return PSCI_E_SUCCESS;

	return PSCI_E_INVALID_ADDRESS;
}

98
static plat_psci_ops_t sunxi_psci_ops = {
99
100
	.pwr_domain_on			= sunxi_pwr_domain_on,
	.pwr_domain_off			= sunxi_pwr_domain_off,
101
	.pwr_domain_pwr_down_wfi	= sunxi_pwr_down_wfi,
102
	.pwr_domain_on_finish		= sunxi_pwr_domain_on_finish,
103
104
	.system_off			= sunxi_system_off,
	.system_reset			= sunxi_system_reset,
105
	.validate_ns_entrypoint		= sunxi_validate_ns_entrypoint,
106
107
108
109
110
111
112
};

int plat_setup_psci_ops(uintptr_t sec_entrypoint,
			const plat_psci_ops_t **psci_ops)
{
	assert(psci_ops);

113
114
115
116
117
118
119
	for (int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) {
		mmio_write_32(SUNXI_CPUCFG_RVBAR_LO_REG(cpu),
			      sec_entrypoint & 0xffffffff);
		mmio_write_32(SUNXI_CPUCFG_RVBAR_HI_REG(cpu),
			      sec_entrypoint >> 32);
	}

120
121
122
123
	*psci_ops = &sunxi_psci_ops;

	return 0;
}