blob: 8c2d070c449515a05a5931013f0cd51d6869cf1b [file] [log] [blame]
Antonio Nino Diazae6779e2017-11-06 14:49:04 +00001/*
Antonio Nino Diaz6942f052018-07-14 02:15:51 +01002 * Copyright (c) 2015-2018, 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
Antonio Nino Diazae6779e2017-11-06 14:49:04 +000024/* Make composite power state parameter till power level 0 */
25#if PSCI_EXTENDED_STATE_ID
26
27#define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
28 (((lvl0_state) << PSTATE_ID_SHIFT) | \
29 ((type) << PSTATE_TYPE_SHIFT))
30
31#else
32
33#define rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type) \
34 (((lvl0_state) << PSTATE_ID_SHIFT) | \
35 ((pwr_lvl) << PSTATE_PWR_LVL_SHIFT) | \
36 ((type) << PSTATE_TYPE_SHIFT))
37
38#endif /* PSCI_EXTENDED_STATE_ID */
39
40#define rpi3_make_pwrstate_lvl1(lvl1_state, lvl0_state, pwr_lvl, type) \
41 (((lvl1_state) << PLAT_LOCAL_PSTATE_WIDTH) | \
42 rpi3_make_pwrstate_lvl0(lvl0_state, pwr_lvl, type))
43
44/*
45 * The table storing the valid idle power states. Ensure that the
46 * array entries are populated in ascending order of state-id to
47 * enable us to use binary search during power state validation.
48 * The table must be terminated by a NULL entry.
49 */
50static const unsigned int rpi3_pm_idle_states[] = {
51 /* State-id - 0x01 */
52 rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_RET,
53 MPIDR_AFFLVL0, PSTATE_TYPE_STANDBY),
54 /* State-id - 0x02 */
55 rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_RUN, PLAT_LOCAL_STATE_OFF,
56 MPIDR_AFFLVL0, PSTATE_TYPE_POWERDOWN),
57 /* State-id - 0x22 */
58 rpi3_make_pwrstate_lvl1(PLAT_LOCAL_STATE_OFF, PLAT_LOCAL_STATE_OFF,
59 MPIDR_AFFLVL1, PSTATE_TYPE_POWERDOWN),
60 0,
61};
62
63/*******************************************************************************
64 * Platform handler called to check the validity of the power state
65 * parameter. The power state parameter has to be a composite power state.
66 ******************************************************************************/
67static int rpi3_validate_power_state(unsigned int power_state,
68 psci_power_state_t *req_state)
69{
70 unsigned int state_id;
71 int i;
72
73 assert(req_state != 0);
74
75 /*
76 * Currently we are using a linear search for finding the matching
77 * entry in the idle power state array. This can be made a binary
78 * search if the number of entries justify the additional complexity.
79 */
80 for (i = 0; rpi3_pm_idle_states[i] != 0; i++) {
81 if (power_state == rpi3_pm_idle_states[i]) {
82 break;
83 }
84 }
85
86 /* Return error if entry not found in the idle state array */
87 if (!rpi3_pm_idle_states[i]) {
88 return PSCI_E_INVALID_PARAMS;
89 }
90
91 i = 0;
92 state_id = psci_get_pstate_id(power_state);
93
94 /* Parse the State ID and populate the state info parameter */
95 while (state_id) {
96 req_state->pwr_domain_state[i++] = state_id &
97 PLAT_LOCAL_PSTATE_MASK;
98 state_id >>= PLAT_LOCAL_PSTATE_WIDTH;
99 }
100
101 return PSCI_E_SUCCESS;
102}
103
104/*******************************************************************************
105 * Platform handler called when a CPU is about to enter standby.
106 ******************************************************************************/
107static void rpi3_cpu_standby(plat_local_state_t cpu_state)
108{
109 assert(cpu_state == PLAT_LOCAL_STATE_RET);
110
111 /*
112 * Enter standby state.
113 * dsb is good practice before using wfi to enter low power states
114 */
115 dsb();
116 wfi();
117}
118
Andre Przywara5724e732019-07-15 23:04:26 +0100119static void rpi3_pwr_domain_off(const psci_power_state_t *target_state)
120{
121#ifdef RPI_HAVE_GIC
122 gicv2_cpuif_disable();
123#endif
124}
125
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000126/*******************************************************************************
127 * Platform handler called when a power domain is about to be turned on. The
128 * mpidr determines the CPU to be turned on.
129 ******************************************************************************/
130static int rpi3_pwr_domain_on(u_register_t mpidr)
131{
132 int rc = PSCI_E_SUCCESS;
133 unsigned int pos = plat_core_pos_by_mpidr(mpidr);
134 uint64_t *hold_base = (uint64_t *)PLAT_RPI3_TM_HOLD_BASE;
135
136 assert(pos < PLATFORM_CORE_COUNT);
137
138 hold_base[pos] = PLAT_RPI3_TM_HOLD_STATE_GO;
139
140 /* Make sure that the write has completed */
141 dsb();
142 isb();
143
144 sev();
145
146 return rc;
147}
148
149/*******************************************************************************
150 * Platform handler called when a power domain has just been powered on after
151 * being turned off earlier. The target_state encodes the low power state that
152 * each level has woken up from.
153 ******************************************************************************/
Florian La Rocheae929572019-01-28 20:39:51 +0100154static void rpi3_pwr_domain_on_finish(const psci_power_state_t *target_state)
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000155{
156 assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
157 PLAT_LOCAL_STATE_OFF);
Andre Przywara5724e732019-07-15 23:04:26 +0100158
159#ifdef RPI_HAVE_GIC
160 gicv2_pcpu_distif_init();
161 gicv2_cpuif_enable();
162#endif
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000163}
164
165/*******************************************************************************
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100166 * Platform handlers for system reset and system off.
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000167 ******************************************************************************/
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000168
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100169/* 10 ticks (Watchdog timer = Timer clock / 16) */
170#define RESET_TIMEOUT U(10)
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000171
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100172static void __dead2 rpi3_watchdog_reset(void)
173{
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000174 uint32_t rstc;
175
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000176 console_flush();
177
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100178 dsbsy();
179 isb();
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000180
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100181 mmio_write_32(RPI3_PM_BASE + RPI3_PM_WDOG_OFFSET,
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000182 RPI3_PM_PASSWORD | RESET_TIMEOUT);
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100183
184 rstc = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET);
185 rstc &= ~RPI3_PM_RSTC_WRCFG_MASK;
186 rstc |= RPI3_PM_PASSWORD | RPI3_PM_RSTC_WRCFG_FULL_RESET;
187 mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTC_OFFSET, rstc);
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000188
189 for (;;) {
190 wfi();
191 }
192}
193
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100194static void __dead2 rpi3_system_reset(void)
195{
196 INFO("rpi3: PSCI_SYSTEM_RESET: Invoking watchdog reset\n");
197
198 rpi3_watchdog_reset();
199}
200
201static void __dead2 rpi3_system_off(void)
202{
203 uint32_t rsts;
204
205 INFO("rpi3: PSCI_SYSTEM_OFF: Invoking watchdog reset\n");
206
207 /*
208 * This function doesn't actually make the Raspberry Pi turn itself off,
209 * the hardware doesn't allow it. It simply reboots it and the RSTS
210 * value tells the bootcode.bin firmware not to continue the regular
211 * bootflow and to stay in a low power mode.
212 */
213
214 rsts = mmio_read_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET);
215 rsts |= RPI3_PM_PASSWORD | RPI3_PM_RSTS_WRCFG_HALT;
216 mmio_write_32(RPI3_PM_BASE + RPI3_PM_RSTS_OFFSET, rsts);
217
218 rpi3_watchdog_reset();
219}
220
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000221/*******************************************************************************
222 * Platform handlers and setup function.
223 ******************************************************************************/
224static const plat_psci_ops_t plat_rpi3_psci_pm_ops = {
225 .cpu_standby = rpi3_cpu_standby,
Andre Przywara5724e732019-07-15 23:04:26 +0100226 .pwr_domain_off = rpi3_pwr_domain_off,
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000227 .pwr_domain_on = rpi3_pwr_domain_on,
228 .pwr_domain_on_finish = rpi3_pwr_domain_on_finish,
Antonio Nino Diaz6942f052018-07-14 02:15:51 +0100229 .system_off = rpi3_system_off,
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000230 .system_reset = rpi3_system_reset,
231 .validate_power_state = rpi3_validate_power_state,
232};
233
234int plat_setup_psci_ops(uintptr_t sec_entrypoint,
235 const plat_psci_ops_t **psci_ops)
236{
Antonio Nino Diazbc297332018-07-14 01:22:43 +0100237 uintptr_t *entrypoint = (void *) PLAT_RPI3_TM_ENTRYPOINT;
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000238
Antonio Nino Diazbc297332018-07-14 01:22:43 +0100239 *entrypoint = sec_entrypoint;
Antonio Nino Diazae6779e2017-11-06 14:49:04 +0000240 *psci_ops = &plat_rpi3_psci_pm_ops;
241
242 return 0;
243}