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

#include <assert.h>
#include <errno.h>
9

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

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

20
#include "aml_private.h"
21
22
23
24
25
26
27
28
29

#define SCPI_POWER_ON		0
#define SCPI_POWER_RETENTION	1
#define SCPI_POWER_OFF		3

#define SCPI_SYSTEM_SHUTDOWN	0
#define SCPI_SYSTEM_REBOOT	1

static uintptr_t gxbb_sec_entrypoint;
30
static volatile uint32_t gxbb_cpu0_go;
31
32
33

static void gxbb_program_mailbox(u_register_t mpidr, uint64_t value)
{
34
	unsigned int core = plat_calc_core_pos(mpidr);
35
	uintptr_t cpu_mailbox_addr = AML_PSCI_MAILBOX_BASE + (core << 4);
36
37
38
39
40
41
42
43
44

	mmio_write_64(cpu_mailbox_addr, value);
	flush_dcache_range(cpu_mailbox_addr, sizeof(uint64_t));
}

static void __dead2 gxbb_system_reset(void)
{
	INFO("BL31: PSCI_SYSTEM_RESET\n");

45
	uint32_t status = mmio_read_32(AML_AO_RTI_STATUS_REG3);
46
47
48
49
50
51
52

	NOTICE("BL31: Reboot reason: 0x%x\n", status);

	status &= 0xFFFF0FF0;

	console_flush();

53
	mmio_write_32(AML_AO_RTI_STATUS_REG3, status);
54

55
	int ret = aml_scpi_sys_power_state(SCPI_SYSTEM_REBOOT);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

	if (ret != 0) {
		ERROR("BL31: PSCI_SYSTEM_RESET: SCP error: %u\n", ret);
		panic();
	}

	wfi();

	ERROR("BL31: PSCI_SYSTEM_RESET: Operation not handled\n");
	panic();
}

static void __dead2 gxbb_system_off(void)
{
	INFO("BL31: PSCI_SYSTEM_OFF\n");

72
	unsigned int ret = aml_scpi_sys_power_state(SCPI_SYSTEM_SHUTDOWN);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

	if (ret != 0) {
		ERROR("BL31: PSCI_SYSTEM_OFF: SCP error %u\n", ret);
		panic();
	}

	gxbb_program_mailbox(read_mpidr_el1(), 0);

	wfi();

	ERROR("BL31: PSCI_SYSTEM_OFF: Operation not handled\n");
	panic();
}

static int32_t gxbb_pwr_domain_on(u_register_t mpidr)
{
89
	unsigned int core = plat_calc_core_pos(mpidr);
90
91

	/* CPU0 can't be turned OFF, emulate it with a WFE loop */
92
	if (core == AML_PRIMARY_CPU) {
93
94
95
96
97
98
99
100
101
102
103
104
		VERBOSE("BL31: Releasing CPU0 from wait loop...\n");

		gxbb_cpu0_go = 1;
		flush_dcache_range((uintptr_t)&gxbb_cpu0_go, sizeof(gxbb_cpu0_go));
		dsb();
		isb();

		sev();

		return PSCI_E_SUCCESS;
	}

105
	gxbb_program_mailbox(mpidr, gxbb_sec_entrypoint);
106
107
	aml_scpi_set_css_power_state(mpidr,
				     SCPI_POWER_ON, SCPI_POWER_ON, SCPI_POWER_ON);
108
109
110
111
112
113
114
115
	dmbsy();
	sev();

	return PSCI_E_SUCCESS;
}

static void gxbb_pwr_domain_on_finish(const psci_power_state_t *target_state)
{
116
	unsigned int core = plat_calc_core_pos(read_mpidr_el1());
117

118
119
120
	assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
					PLAT_LOCAL_STATE_OFF);

121
	if (core == AML_PRIMARY_CPU) {
122
123
124
125
126
127
		gxbb_cpu0_go = 0;
		flush_dcache_range((uintptr_t)&gxbb_cpu0_go, sizeof(gxbb_cpu0_go));
		dsb();
		isb();
	}

128
129
130
131
	gicv2_pcpu_distif_init();
	gicv2_cpuif_enable();
}

132
133
134
static void gxbb_pwr_domain_off(const psci_power_state_t *target_state)
{
	u_register_t mpidr = read_mpidr_el1();
135
	unsigned int core = plat_calc_core_pos(mpidr);
136
	uintptr_t addr = AML_PSCI_MAILBOX_BASE + 8 + (core << 4);
137
138
139
140
141
142

	mmio_write_32(addr, 0xFFFFFFFF);
	flush_dcache_range(addr, sizeof(uint32_t));

	gicv2_cpuif_disable();

143
	/* CPU0 can't be turned OFF, emulate it with a WFE loop */
144
	if (core == AML_PRIMARY_CPU)
145
146
		return;

147
148
	aml_scpi_set_css_power_state(mpidr,
				     SCPI_POWER_OFF, SCPI_POWER_ON, SCPI_POWER_ON);
149
150
}

151
152
153
static void __dead2 gxbb_pwr_domain_pwr_down_wfi(const psci_power_state_t
						 *target_state)
{
154
	unsigned int core = plat_calc_core_pos(read_mpidr_el1());
155
156

	/* CPU0 can't be turned OFF, emulate it with a WFE loop */
157
	if (core == AML_PRIMARY_CPU) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
		VERBOSE("BL31: CPU0 entering wait loop...\n");

		while (gxbb_cpu0_go == 0)
			wfe();

		VERBOSE("BL31: CPU0 resumed.\n");

		write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT);
	}

	dsbsy();

	for (;;)
		wfi();
}

174
175
176
177
178
179
/*******************************************************************************
 * Platform handlers and setup function.
 ******************************************************************************/
static const plat_psci_ops_t gxbb_ops = {
	.pwr_domain_on			= gxbb_pwr_domain_on,
	.pwr_domain_on_finish		= gxbb_pwr_domain_on_finish,
180
	.pwr_domain_off			= gxbb_pwr_domain_off,
181
	.pwr_domain_pwr_down_wfi	= gxbb_pwr_domain_pwr_down_wfi,
182
183
184
185
186
187
188
189
190
	.system_off			= gxbb_system_off,
	.system_reset			= gxbb_system_reset,
};

int plat_setup_psci_ops(uintptr_t sec_entrypoint,
			const plat_psci_ops_t **psci_ops)
{
	gxbb_sec_entrypoint = sec_entrypoint;
	*psci_ops = &gxbb_ops;
191
	gxbb_cpu0_go = 0;
192
193
	return 0;
}