blob: 456e1603c5e5f5cf716a600e2d0674948259d961 [file] [log] [blame]
Antonio Nino Diazae6779e2017-11-06 14:49:04 +00001/*
Mario Bălănicăcb759ff2023-12-06 21:36:25 +02002 * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
Antonio Nino Diazae6779e2017-11-06 14:49:04 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diazae6779e2017-11-06 14:49:04 +00007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Antonio Nino Diazae6779e2017-11-06 14:49:04 +00009#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <arch_helpers.h>
12#include <common/debug.h>
13#include <drivers/console.h>
14#include <lib/mmio.h>
15#include <lib/psci/psci.h>
16#include <plat/common/platform.h>
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000017
Andre Przywarabb6ef152019-07-09 11:44:14 +010018#include <rpi_hw.h>
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000019
Andre Przywara5724e732019-07-15 23:04:26 +010020#ifdef RPI_HAVE_GIC
21#include <drivers/arm/gicv2.h>
22#endif
23
Mario Bălănicăcb759ff2023-12-06 21:36:25 +020024/* Registers on top of RPI3_PM_BASE. */
25#define RPI3_PM_RSTC_OFFSET ULL(0x0000001C)
26#define RPI3_PM_RSTS_OFFSET ULL(0x00000020)
27#define RPI3_PM_WDOG_OFFSET ULL(0x00000024)
28/* Watchdog constants */
29#define RPI3_PM_PASSWORD U(0x5A000000)
30#define RPI3_PM_RSTC_WRCFG_MASK U(0x00000030)
31#define RPI3_PM_RSTC_WRCFG_FULL_RESET U(0x00000020)
32/*
33 * The RSTS register is used by the VideoCore firmware when booting the
34 * Raspberry Pi to know which partition to boot from. The partition value is
35 * formed by bits 0, 2, 4, 6, 8 and 10. Partition 63 is used by said firmware
36 * to indicate halt.
37 */
38#define RPI3_PM_RSTS_WRCFG_HALT U(0x00000555)
39
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000040/* Make composite power state parameter till power level 0 */
41#if PSCI_EXTENDED_STATE_ID
42
43#define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
44 (((lvl0_state) << PSTATE_ID_SHIFT) | \
45 ((type) << PSTATE_TYPE_SHIFT))
46
47#else
48
49#define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
50 (((lvl0_state) << PSTATE_ID_SHIFT) | \
51 ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
52 ((type) << PSTATE_TYPE_SHIFT))
53
54#endif /* PSCI_EXTENDED_STATE_ID */
55
56#define rpi3_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
57 (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \
58 rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
59
60/*
61 * The table storing the valid idle power states. Ensure that the
62 * array entries are populated in ascending order of state-id to
63 * enable us to use binary search during power state validation.
64 * The table must be terminated by a NULL entry.
65 */
66static const unsigned int rpi3_pm_idle_states[] = {
67 /* State-id - 0x01 */
68 rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET,
69 MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
70 /* State-id - 0x02 */
71 rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF,
72 MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
73 /* State-id - 0x22 */
74 rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF,
75 MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN),
76 0,
77};
78
79/*******************************************************************************
80 * Platform handler called to check the validity of the power state
81 * parameter. The power state parameter has to be a composite power state.
82 ******************************************************************************/
83static int rpi3_validate_power_state(unsigned int power_state,
84 psci_power_state_t *req_state)
85{
86 unsigned int state_id;
87 int i;
88
89 assert(req_state != 0);
90
91 /*
92 * Currently we are using a linear search for finding the matching
93 * entry in the idle power state array. This can be made a binary
94 * search if the number of entries justify the additional complexity.
95 */
96 for (i = 0; rpi3_pm_idle_states[i] != 0; i++) {
97 if (power_state == rpi3_pm_idle_states[i]) {
98 break;
99 }
100 }
101
102 /* Return error if entry not found in the idle state array */
103 if (!rpi3_pm_idle_states[i]) {
104 return PSCI_E_INVALID_PARAMS;
105 }
106
107 i = 0;
108 state_id = psci_get_pstate_id(power_state);
109
110 /* Parse the State ID and populate the state info parameter */
111 while (state_id) {
112 req_state->pwr_domain_state[i++] = state_id &
113 PLAT_LOCAL_PSTATE_MASK;
114 state_id >>= PLAT_LOCAL_PSTATE_WIDTH;
115 }
116
117 return PSCI_E_SUCCESS;
118}
119
120/*******************************************************************************
121 * Platform handler called when a CPU is about to enter standby.
122 ******************************************************************************/
123static void rpi3_cpu_standby(plat_local_state_t cpu_state)
124{
125 assert(cpu_state == PLAT_LOCAL_STATE_RET);
126
127 /*
128 * Enter standby state.
129 * dsb is good practice before using wfi to enter low power states
130 */
131 dsb();
132 wfi();
133}
134
Andre Przywara5724e732019-07-15 23:04:26 +0100135static void rpi3_pwr_domain_off(const psci_power_state_t *target_state)
136{
137#ifdef RPI_HAVE_GIC
138 gicv2_cpuif_disable();
139#endif
140}
141
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000142/*******************************************************************************
143 * Platform handler called when a power domain is about to be turned on. The
144 * mpidr determines the CPU to be turned on.
145 ******************************************************************************/
146static int rpi3_pwr_domain_on(u_register_t mpidr)
147{
148 int rc = PSCI_E_SUCCESS;
149 unsigned int pos = plat_core_pos_by_mpidr(mpidr);
Andre Przywara4d52c242020-03-21 11:22:13 +0000150 uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE;
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000151
152 assert(pos < PLATFORM_CORE_COUNT);
153
Andre Przywara4d52c242020-03-21 11:22:13 +0000154 hold_base += pos * PLAT_RPI3_TM_HOLD_ENTRY_SIZE;
155
156 mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_GO);
157 /* No cache maintenance here, hold_base is mapped as device memory. */
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000158
159 /* Make sure that the write has completed */
160 dsb();
161 isb();
162
163 sev();
164
165 return rc;
166}
167
168/*******************************************************************************
169 * Platform handler called when a power domain has just been powered on after
170 * being turned off earlier. The target_state encodes the low power state that
171 * each level has woken up from.
172 ******************************************************************************/
Florian La Rocheae929572019-01-28 20:39:51 +0100173static void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state)
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000174{
175 assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
176 PLAT_LOCAL_STATE_OFF);
Andre Przywara5724e732019-07-15 23:04:26 +0100177
178#ifdef RPI_HAVE_GIC
179 gicv2_pcpu_distif_init();
180 gicv2_cpuif_enable();
181#endif
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000182}
183
Andrei Warkentinc96d4302020-03-11 22:11:06 -0700184static void __dead2 rpi3_pwr_down_wfi(
185 const psci_power_state_t *target_state)
186{
187 uintptr_t hold_base = PLAT_RPI3_TM_HOLD_BASE;
188 unsigned int pos = plat_my_core_pos();
189
190 if (pos == 0) {
191 /*
192 * The secondaries will always be in a wait
193 * for warm boot on reset, but the BSP needs
194 * to be able to distinguish between waiting
195 * for warm boot (e.g. after psci_off, waiting
196 * for psci_on) and a cold boot.
197 */
198 mmio_write_64(hold_base, PLAT_RPI3_TM_HOLD_STATE_BSP_OFF);
199 /* No cache maintenance here, we run with caches off already. */
200 dsb();
201 isb();
202 }
203
204 write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT);
205
Boyan Karatotev43771f32022-10-05 13:41:56 +0100206 while (1) {
207 wfi();
208 }
Andrei Warkentinc96d4302020-03-11 22:11:06 -0700209}
210
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000211/*******************************************************************************
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100212 * Platform handlers for system reset and system off.
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000213 ******************************************************************************/
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000214
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100215/* 10 ticks (Watchdog timer = Timer clock / 16) */
216#define RESET_TIMEOUT U(10)
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000217
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100218static void __dead2 rpi3_watchdog_reset(void)
219{
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000220 uint32_t rstc;
221
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000222 console_flush();
223
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100224 dsbsy();
225 isb();
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000226
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100227 mmio_write_32(RPI3_PM_BASE + RPI3_PM_WDOG_OFFSET,
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000228 RPI3_PM_PASSWORD | RESET_TIMEOUT);
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100229
230 rstc = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET);
231 rstc &= ~RPI3_PM_RSTC_WRCFG_MASK;
232 rstc |= RPI3_PM_PASSWORD | RPI3_PM_RSTC_WRCFG_FULL_RESET;
233 mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET, rstc);
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000234
235 for (;;) {
236 wfi();
237 }
238}
239
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100240static void __dead2 rpi3_system_reset(void)
241{
242 INFO("rpi3: PSCI_SYSTEM_RESET: Invoking watchdog reset\n");
243
244 rpi3_watchdog_reset();
245}
246
247static void __dead2 rpi3_system_off(void)
248{
249 uint32_t rsts;
250
251 INFO("rpi3: PSCI_SYSTEM_OFF: Invoking watchdog reset\n");
252
253 /*
254 * This function doesn't actually make the Raspberry Pi turn itself off,
255 * the hardware doesn't allow it. It simply reboots it and the RSTS
256 * value tells the bootcode.bin firmware not to continue the regular
257 * bootflow and to stay in a low power mode.
258 */
259
260 rsts = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET);
261 rsts |= RPI3_PM_PASSWORD | RPI3_PM_RSTS_WRCFG_HALT;
262 mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET, rsts);
263
264 rpi3_watchdog_reset();
265}
266
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000267/*******************************************************************************
268 * Platform handlers and setup function.
269 ******************************************************************************/
270static const plat_psci_ops_t plat_rpi3_psci_pm_ops = {
271 .cpu_standby = rpi3_cpu_standby,
Andre Przywara5724e732019-07-15 23:04:26 +0100272 .pwr_domain_off = rpi3_pwr_domain_off,
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000273 .pwr_domain_on = rpi3_pwr_domain_on,
274 .pwr_domain_on_finish = rpi3_pwr_domain_on_finish,
Andrei Warkentinc96d4302020-03-11 22:11:06 -0700275 .pwr_domain_pwr_down_wfi = rpi3_pwr_down_wfi,
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100276 .system_off = rpi3_system_off,
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000277 .system_reset = rpi3_system_reset,
278 .validate_power_state = rpi3_validate_power_state,
279};
280
281int plat_setup_psci_ops(uintptr_t sec_entrypoint,
282 const plat_psci_ops_t **psci_ops)
283{
Antonio Nino Diazbc297332018-07-14 01:22:43 +0100284 uintptr_t *entrypoint = (void *) PLAT_RPI3_TM_ENTRYPOINT;
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000285
Antonio Nino Diazbc297332018-07-14 01:22:43 +0100286 *entrypoint = sec_entrypoint;
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000287 *psci_ops = &plat_rpi3_psci_pm_ops;
288
289 return 0;
290}