blob: 97e1ac61b3fdaf760a4edb9fc79271f422b87b1e [file] [log] [blame]
Yann Gautier9d135e42018-07-16 19:36:06 +02001/*
Lionel Debievea7ad5882020-11-12 10:44:51 +01002 * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
Yann Gautier9d135e42018-07-16 19:36:06 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Yann Gautier9d135e42018-07-16 19:36:06 +02007#include <assert.h>
Yann Gautier9d135e42018-07-16 19:36:06 +02008#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <arch_helpers.h>
Etienne Carriere5a0f82f2020-06-08 20:25:08 +020011#include <bl32/sp_min/platform_sp_min.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/debug.h>
13#include <drivers/arm/gic_common.h>
14#include <drivers/arm/gicv2.h>
Yann Gautiera205a5c2021-08-30 15:06:54 +020015#include <drivers/clk.h>
Lionel Debievea7ad5882020-11-12 10:44:51 +010016#include <drivers/st/stm32mp_reset.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <dt-bindings/clock/stm32mp1-clks.h>
18#include <lib/mmio.h>
19#include <lib/psci/psci.h>
20#include <plat/common/platform.h>
21
Nicolas Toromanoffbb82b1b2022-02-09 12:26:31 +010022#include <platform_def.h>
23
Yann Gautierf9d40d52019-01-17 14:41:46 +010024static uintptr_t stm32_sec_entrypoint;
Yann Gautier9d135e42018-07-16 19:36:06 +020025static uint32_t cntfrq_core0;
26
Yann Gautier9d135e42018-07-16 19:36:06 +020027/*******************************************************************************
28 * STM32MP1 handler called when a CPU is about to enter standby.
29 * call by core 1 to enter in wfi
30 ******************************************************************************/
31static void stm32_cpu_standby(plat_local_state_t cpu_state)
32{
33 uint32_t interrupt = GIC_SPURIOUS_INTERRUPT;
34
35 assert(cpu_state == ARM_LOCAL_STATE_RET);
36
37 /*
38 * Enter standby state
39 * dsb is good practice before using wfi to enter low power states
40 */
Yann Gautierf9d40d52019-01-17 14:41:46 +010041 isb();
Yann Gautier9d135e42018-07-16 19:36:06 +020042 dsb();
43 while (interrupt == GIC_SPURIOUS_INTERRUPT) {
44 wfi();
45
Elyes Haouas2be03c02023-02-13 09:14:48 +010046 /* Acknowledge IT */
Yann Gautier9d135e42018-07-16 19:36:06 +020047 interrupt = gicv2_acknowledge_interrupt();
48 /* If Interrupt == 1022 it will be acknowledged by non secure */
49 if ((interrupt != PENDING_G1_INTID) &&
50 (interrupt != GIC_SPURIOUS_INTERRUPT)) {
51 gicv2_end_of_interrupt(interrupt);
52 }
53 }
54}
55
56/*******************************************************************************
57 * STM32MP1 handler called when a power domain is about to be turned on. The
58 * mpidr determines the CPU to be turned on.
Yann Gautierf9d40d52019-01-17 14:41:46 +010059 * call by core 0 to activate core 1
Yann Gautier9d135e42018-07-16 19:36:06 +020060 ******************************************************************************/
61static int stm32_pwr_domain_on(u_register_t mpidr)
62{
63 unsigned long current_cpu_mpidr = read_mpidr_el1();
Nicolas Toromanoffbb82b1b2022-02-09 12:26:31 +010064 uintptr_t bkpr_core1_addr =
Yann Gautier9d135e42018-07-16 19:36:06 +020065 tamp_bkpr(BOOT_API_CORE1_BRANCH_ADDRESS_TAMP_BCK_REG_IDX);
Nicolas Toromanoffbb82b1b2022-02-09 12:26:31 +010066 uintptr_t bkpr_core1_magic =
Yann Gautier9d135e42018-07-16 19:36:06 +020067 tamp_bkpr(BOOT_API_CORE1_MAGIC_NUMBER_TAMP_BCK_REG_IDX);
68
69 if (mpidr == current_cpu_mpidr) {
70 return PSCI_E_INVALID_PARAMS;
71 }
72
Etienne Carriere5a0f82f2020-06-08 20:25:08 +020073 /* Only one valid entry point */
74 if (stm32_sec_entrypoint != (uintptr_t)&sp_min_warm_entrypoint) {
Yann Gautier9d135e42018-07-16 19:36:06 +020075 return PSCI_E_INVALID_ADDRESS;
76 }
77
Yann Gautiera205a5c2021-08-30 15:06:54 +020078 clk_enable(RTCAPB);
Yann Gautier9d135e42018-07-16 19:36:06 +020079
80 cntfrq_core0 = read_cntfrq_el0();
81
82 /* Write entrypoint in backup RAM register */
83 mmio_write_32(bkpr_core1_addr, stm32_sec_entrypoint);
84
85 /* Write magic number in backup register */
86 mmio_write_32(bkpr_core1_magic, BOOT_API_A7_CORE1_MAGIC_NUMBER);
87
Yann Gautiera205a5c2021-08-30 15:06:54 +020088 clk_disable(RTCAPB);
Yann Gautier9d135e42018-07-16 19:36:06 +020089
90 /* Generate an IT to core 1 */
Florian Lugoud4e25032021-09-08 12:40:24 +020091 gicv2_raise_sgi(ARM_IRQ_SEC_SGI_0, false, STM32MP_SECONDARY_CPU);
Yann Gautier9d135e42018-07-16 19:36:06 +020092
93 return PSCI_E_SUCCESS;
94}
95
96/*******************************************************************************
97 * STM32MP1 handler called when a power domain is about to be turned off. The
98 * target_state encodes the power state that each level should transition to.
99 ******************************************************************************/
100static void stm32_pwr_domain_off(const psci_power_state_t *target_state)
101{
102 /* Nothing to do */
103}
104
105/*******************************************************************************
106 * STM32MP1 handler called when a power domain is about to be suspended. The
107 * target_state encodes the power state that each level should transition to.
108 ******************************************************************************/
109static void stm32_pwr_domain_suspend(const psci_power_state_t *target_state)
110{
111 /* Nothing to do, power domain is not disabled */
112}
113
114/*******************************************************************************
115 * STM32MP1 handler called when a power domain has just been powered on after
116 * being turned off earlier. The target_state encodes the low power state that
117 * each level has woken up from.
118 * call by core 1 just after wake up
119 ******************************************************************************/
120static void stm32_pwr_domain_on_finish(const psci_power_state_t *target_state)
121{
Yann Gautier2bbf1712019-08-06 17:28:23 +0200122 stm32mp_gic_pcpu_init();
Yann Gautier9d135e42018-07-16 19:36:06 +0200123
124 write_cntfrq_el0(cntfrq_core0);
125}
126
127/*******************************************************************************
128 * STM32MP1 handler called when a power domain has just been powered on after
129 * having been suspended earlier. The target_state encodes the low power state
130 * that each level has woken up from.
131 ******************************************************************************/
132static void stm32_pwr_domain_suspend_finish(const psci_power_state_t
133 *target_state)
134{
135 /* Nothing to do, power domain is not disabled */
136}
137
138static void __dead2 stm32_pwr_domain_pwr_down_wfi(const psci_power_state_t
139 *target_state)
140{
141 ERROR("stm32mpu1 Power Down WFI: operation not handled.\n");
142 panic();
143}
144
145static void __dead2 stm32_system_off(void)
146{
147 ERROR("stm32mpu1 System Off: operation not handled.\n");
148 panic();
149}
150
151static void __dead2 stm32_system_reset(void)
152{
Lionel Debievea7ad5882020-11-12 10:44:51 +0100153 stm32mp_system_reset();
Yann Gautier9d135e42018-07-16 19:36:06 +0200154}
155
156static int stm32_validate_power_state(unsigned int power_state,
157 psci_power_state_t *req_state)
158{
Yann Gautier79c9c082021-12-21 17:56:52 +0100159 if (psci_get_pstate_type(power_state) != 0U) {
Yann Gautier9d135e42018-07-16 19:36:06 +0200160 return PSCI_E_INVALID_PARAMS;
161 }
162
Yann Gautier79c9c082021-12-21 17:56:52 +0100163 if (psci_get_pstate_pwrlvl(power_state) != 0U) {
Yann Gautier9d135e42018-07-16 19:36:06 +0200164 return PSCI_E_INVALID_PARAMS;
165 }
166
Yann Gautier79c9c082021-12-21 17:56:52 +0100167 if (psci_get_pstate_id(power_state) != 0U) {
Yann Gautier9d135e42018-07-16 19:36:06 +0200168 return PSCI_E_INVALID_PARAMS;
169 }
170
171 req_state->pwr_domain_state[0] = ARM_LOCAL_STATE_RET;
172 req_state->pwr_domain_state[1] = ARM_LOCAL_STATE_RUN;
173
174 return PSCI_E_SUCCESS;
175}
176
177static int stm32_validate_ns_entrypoint(uintptr_t entrypoint)
178{
179 /* The non-secure entry point must be in DDR */
Yann Gautiera2e2a302019-02-14 11:13:39 +0100180 if (entrypoint < STM32MP_DDR_BASE) {
Yann Gautier9d135e42018-07-16 19:36:06 +0200181 return PSCI_E_INVALID_ADDRESS;
182 }
183
184 return PSCI_E_SUCCESS;
185}
186
187static int stm32_node_hw_state(u_register_t target_cpu,
188 unsigned int power_level)
189{
190 /*
191 * The format of 'power_level' is implementation-defined, but 0 must
192 * mean a CPU. Only allow level 0.
193 */
194 if (power_level != MPIDR_AFFLVL0) {
195 return PSCI_E_INVALID_PARAMS;
196 }
197
198 /*
199 * From psci view the CPU 0 is always ON,
200 * CPU 1 can be SUSPEND or RUNNING.
201 * Therefore do not manage POWER OFF state and always return HW_ON.
202 */
203
204 return (int)HW_ON;
205}
206
207/*******************************************************************************
208 * Export the platform handlers. The ARM Standard platform layer will take care
209 * of registering the handlers with PSCI.
210 ******************************************************************************/
211static const plat_psci_ops_t stm32_psci_ops = {
212 .cpu_standby = stm32_cpu_standby,
213 .pwr_domain_on = stm32_pwr_domain_on,
214 .pwr_domain_off = stm32_pwr_domain_off,
215 .pwr_domain_suspend = stm32_pwr_domain_suspend,
216 .pwr_domain_on_finish = stm32_pwr_domain_on_finish,
217 .pwr_domain_suspend_finish = stm32_pwr_domain_suspend_finish,
218 .pwr_domain_pwr_down_wfi = stm32_pwr_domain_pwr_down_wfi,
219 .system_off = stm32_system_off,
220 .system_reset = stm32_system_reset,
221 .validate_power_state = stm32_validate_power_state,
222 .validate_ns_entrypoint = stm32_validate_ns_entrypoint,
223 .get_node_hw_state = stm32_node_hw_state
224};
225
226/*******************************************************************************
227 * Export the platform specific power ops.
228 ******************************************************************************/
229int plat_setup_psci_ops(uintptr_t sec_entrypoint,
230 const plat_psci_ops_t **psci_ops)
231{
232 stm32_sec_entrypoint = sec_entrypoint;
233 *psci_ops = &stm32_psci_ops;
234
235 return 0;
236}