blob: 8dc39e2179c4886288d31fcc33223a5e99a4214d [file] [log] [blame]
Hadi Asyrafi616da772019-06-27 11:34:03 +08001/*
Jit Loon Lim86f6fb32023-05-17 12:26:11 +08002 * Copyright (c) 2019-2023, ARM Limited and Contributors. All rights reserved.
Jit Loon Limb24dddf2023-05-17 12:26:11 +08003 * Copyright (c) 2019-2023, Intel Corporation. All rights reserved.
Sieu Mun Tangebca5152024-08-26 07:59:10 +08004 * Copyright (c) 2024, Altera Corporation. All rights reserved.
Hadi Asyrafi616da772019-06-27 11:34:03 +08005 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9#include <arch_helpers.h>
10#include <common/debug.h>
Jit Loon Limb24dddf2023-05-17 12:26:11 +080011
12#ifndef GICV3_SUPPORT_GIC600
Hadi Asyrafi616da772019-06-27 11:34:03 +080013#include <drivers/arm/gicv2.h>
Jit Loon Lim86f6fb32023-05-17 12:26:11 +080014#else
15#include <drivers/arm/gicv3.h>
16#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +080017#include <lib/mmio.h>
18#include <lib/psci/psci.h>
19#include <plat/common/platform.h>
Sieu Mun Tang82254ee2024-10-22 00:52:09 +080020#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
21#include "agilex5_cache.h"
22#endif
Sieu Mun Tang9dd2c182024-10-22 01:00:45 +080023#include "ccu/ncore_ccu.h"
Hadi Asyrafi6f8a2b22019-10-23 18:34:14 +080024#include "socfpga_mailbox.h"
Hadi Asyrafi4d9f3952019-10-23 17:35:32 +080025#include "socfpga_plat_def.h"
Sieu Mun Tang9dd2c182024-10-22 01:00:45 +080026#include "socfpga_private.h"
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +080027#include "socfpga_reset_manager.h"
Sieu Mun Tangdbcc2cf2022-03-07 12:13:04 +080028#include "socfpga_sip_svc.h"
Jit Loon Lim86f6fb32023-05-17 12:26:11 +080029#include "socfpga_system_manager.h"
Hadi Asyrafi616da772019-06-27 11:34:03 +080030
Jit Loon Limb24dddf2023-05-17 12:26:11 +080031#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
32void socfpga_wakeup_secondary_cpu(unsigned int cpu_id);
33extern void plat_secondary_cold_boot_setup(void);
34#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +080035
Hadi Asyrafi616da772019-06-27 11:34:03 +080036/*******************************************************************************
37 * plat handler called when a CPU is about to enter standby.
38 ******************************************************************************/
39void socfpga_cpu_standby(plat_local_state_t cpu_state)
40{
41 /*
42 * Enter standby state
43 * dsb is good practice before using wfi to enter low power states
44 */
45 VERBOSE("%s: cpu_state: 0x%x\n", __func__, cpu_state);
46 dsb();
47 wfi();
48}
49
50/*******************************************************************************
51 * plat handler called when a power domain is about to be turned on. The
52 * mpidr determines the CPU to be turned on.
53 ******************************************************************************/
54int socfpga_pwr_domain_on(u_register_t mpidr)
55{
56 unsigned int cpu_id = plat_core_pos_by_mpidr(mpidr);
Jit Loon Limb24dddf2023-05-17 12:26:11 +080057#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
58 /* TODO: Add in CPU FUSE from SDM */
59#else
Jit Loon Lim44c61fc2023-03-02 13:38:53 +080060 uint32_t psci_boot = 0x00;
Hadi Asyrafi616da772019-06-27 11:34:03 +080061
62 VERBOSE("%s: mpidr: 0x%lx\n", __func__, mpidr);
Jit Loon Limb24dddf2023-05-17 12:26:11 +080063#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +080064
65 if (cpu_id == -1)
66 return PSCI_E_INTERN_FAIL;
67
Jit Loon Limb24dddf2023-05-17 12:26:11 +080068#if PLATFORM_MODEL != PLAT_SOCFPGA_AGILEX5
Jit Loon Lim44c61fc2023-03-02 13:38:53 +080069 if (cpu_id == 0x00) {
70 psci_boot = mmio_read_32(SOCFPGA_SYSMGR(BOOT_SCRATCH_COLD_8));
Jit Loon Lim9da76202023-06-10 00:04:49 +080071 psci_boot |= 0x80000; /* bit 19 */
Jit Loon Lim44c61fc2023-03-02 13:38:53 +080072 mmio_write_32(SOCFPGA_SYSMGR(BOOT_SCRATCH_COLD_8), psci_boot);
73 }
74
Hadi Asyrafia2edf0e2019-10-22 13:39:14 +080075 mmio_write_64(PLAT_CPUID_RELEASE, cpu_id);
Jit Loon Limb24dddf2023-05-17 12:26:11 +080076#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +080077
78 /* release core reset */
Jit Loon Limb24dddf2023-05-17 12:26:11 +080079#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
80 bl31_plat_set_secondary_cpu_entrypoint(cpu_id);
81#else
Hadi Asyrafi67cb0ea2019-12-23 13:25:33 +080082 mmio_setbits_32(SOCFPGA_RSTMGR(MPUMODRST), 1 << cpu_id);
Jit Loon Limb24dddf2023-05-17 12:26:11 +080083 mmio_write_64(PLAT_CPUID_RELEASE, cpu_id);
84#endif
85
Hadi Asyrafi616da772019-06-27 11:34:03 +080086 return PSCI_E_SUCCESS;
87}
88
89/*******************************************************************************
90 * plat handler called when a power domain is about to be turned off. The
91 * target_state encodes the power state that each level should transition to.
92 ******************************************************************************/
93void socfpga_pwr_domain_off(const psci_power_state_t *target_state)
94{
Hadi Asyrafi616da772019-06-27 11:34:03 +080095 for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++)
96 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
97 __func__, i, target_state->pwr_domain_state[i]);
98
Hadi Asyrafi91071fc2019-09-12 15:14:01 +080099 /* Prevent interrupts from spuriously waking up this cpu */
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800100#ifdef GICV3_SUPPORT_GIC600
101 gicv3_cpuif_disable(plat_my_core_pos());
102#else
Hadi Asyrafi91071fc2019-09-12 15:14:01 +0800103 gicv2_cpuif_disable();
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800104#endif
105
Hadi Asyrafi616da772019-06-27 11:34:03 +0800106}
107
108/*******************************************************************************
109 * plat handler called when a power domain is about to be suspended. The
110 * target_state encodes the power state that each level should transition to.
111 ******************************************************************************/
112void socfpga_pwr_domain_suspend(const psci_power_state_t *target_state)
113{
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800114#if PLATFORM_MODEL != PLAT_SOCFPGA_AGILEX5
Hadi Asyrafi616da772019-06-27 11:34:03 +0800115 unsigned int cpu_id = plat_my_core_pos();
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800116#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +0800117
118 for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++)
119 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
120 __func__, i, target_state->pwr_domain_state[i]);
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800121
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800122#if PLATFORM_MODEL != PLAT_SOCFPGA_AGILEX5
Hadi Asyrafi616da772019-06-27 11:34:03 +0800123 /* assert core reset */
Hadi Asyrafi67cb0ea2019-12-23 13:25:33 +0800124 mmio_setbits_32(SOCFPGA_RSTMGR(MPUMODRST), 1 << cpu_id);
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800125#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +0800126}
127
128/*******************************************************************************
129 * plat handler called when a power domain has just been powered on after
130 * being turned off earlier. The target_state encodes the low power state that
131 * each level has woken up from.
132 ******************************************************************************/
133void socfpga_pwr_domain_on_finish(const psci_power_state_t *target_state)
134{
135 for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++)
136 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
137 __func__, i, target_state->pwr_domain_state[i]);
138
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800139 /* Enable the gic cpu interface */
140#ifdef GICV3_SUPPORT_GIC600
141 gicv3_rdistif_init(plat_my_core_pos());
142 gicv3_cpuif_enable(plat_my_core_pos());
143#else
Hadi Asyrafi616da772019-06-27 11:34:03 +0800144 /* Program the gic per-cpu distributor or re-distributor interface */
145 gicv2_pcpu_distif_init();
146 gicv2_set_pe_target_mask(plat_my_core_pos());
147
148 /* Enable the gic cpu interface */
149 gicv2_cpuif_enable();
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800150#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +0800151}
152
153/*******************************************************************************
154 * plat handler called when a power domain has just been powered on after
155 * having been suspended earlier. The target_state encodes the low power state
156 * that each level has woken up from.
157 * TODO: At the moment we reuse the on finisher and reinitialize the secure
158 * context. Need to implement a separate suspend finisher.
159 ******************************************************************************/
160void socfpga_pwr_domain_suspend_finish(const psci_power_state_t *target_state)
161{
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800162#if PLATFORM_MODEL != PLAT_SOCFPGA_AGILEX5
Hadi Asyrafi616da772019-06-27 11:34:03 +0800163 unsigned int cpu_id = plat_my_core_pos();
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800164#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +0800165
166 for (size_t i = 0; i <= PLAT_MAX_PWR_LVL; i++)
167 VERBOSE("%s: target_state->pwr_domain_state[%lu]=%x\n",
168 __func__, i, target_state->pwr_domain_state[i]);
169
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800170#if PLATFORM_MODEL != PLAT_SOCFPGA_AGILEX5
Hadi Asyrafi616da772019-06-27 11:34:03 +0800171 /* release core reset */
Hadi Asyrafi67cb0ea2019-12-23 13:25:33 +0800172 mmio_clrbits_32(SOCFPGA_RSTMGR(MPUMODRST), 1 << cpu_id);
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800173#endif
Hadi Asyrafi616da772019-06-27 11:34:03 +0800174}
175
176/*******************************************************************************
177 * plat handlers to shutdown/reboot the system
178 ******************************************************************************/
179static void __dead2 socfpga_system_off(void)
180{
181 wfi();
182 ERROR("System Off: operation not handled.\n");
183 panic();
184}
185
Hadi Asyrafi593c4c52019-12-17 19:22:17 +0800186extern uint64_t intel_rsu_update_address;
187
Hadi Asyrafi616da772019-06-27 11:34:03 +0800188static void __dead2 socfpga_system_reset(void)
189{
Abdul Halim, Muhammad Hadi Asyrafid84bfef2020-02-25 16:28:10 +0800190 uint32_t addr_buf[2];
191
Sieu Mun Tangebca5152024-08-26 07:59:10 +0800192 memcpy_s(addr_buf, sizeof(intel_rsu_update_address),
193 &intel_rsu_update_address, sizeof(intel_rsu_update_address));
194
Jit Loon Lim86f6fb32023-05-17 12:26:11 +0800195 if (intel_rsu_update_address) {
Abdul Halim, Muhammad Hadi Asyrafid84bfef2020-02-25 16:28:10 +0800196 mailbox_rsu_update(addr_buf);
Jit Loon Lim86f6fb32023-05-17 12:26:11 +0800197 } else {
Sieu Mun Tang9dd2c182024-10-22 01:00:45 +0800198#if CACHE_FLUSH
199 /* ATF Flush and Invalidate Cache */
200 dcsw_op_all(DCCISW);
201 invalidate_cache_low_el();
202#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
203 flush_l3_dcache();
204#endif
205#endif
Hadi Asyrafi593c4c52019-12-17 19:22:17 +0800206 mailbox_reset_cold();
Jit Loon Lim86f6fb32023-05-17 12:26:11 +0800207 }
Hadi Asyrafi616da772019-06-27 11:34:03 +0800208
209 while (1)
210 wfi();
211}
212
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800213static int socfpga_system_reset2(int is_vendor, int reset_type,
214 u_register_t cookie)
215{
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800216#if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX5
217 mailbox_reset_warm(reset_type);
218#else
Sieu Mun Tangdbcc2cf2022-03-07 12:13:04 +0800219 if (cold_reset_for_ecc_dbe()) {
220 mailbox_reset_cold();
221 }
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800222#endif
223
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800224 /* disable cpuif */
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800225#ifdef GICV3_SUPPORT_GIC600
226 gicv3_cpuif_disable(plat_my_core_pos());
227#else
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800228 gicv2_cpuif_disable();
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800229#endif
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800230
231 /* Store magic number */
232 mmio_write_32(L2_RESET_DONE_REG, L2_RESET_DONE_STATUS);
233
234 /* Increase timeout */
Hadi Asyrafi67cb0ea2019-12-23 13:25:33 +0800235 mmio_write_32(SOCFPGA_RSTMGR(HDSKTIMEOUT), 0xffffff);
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800236
237 /* Enable handshakes */
Hadi Asyrafi67cb0ea2019-12-23 13:25:33 +0800238 mmio_setbits_32(SOCFPGA_RSTMGR(HDSKEN), RSTMGR_HDSKEN_SET);
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800239
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800240#if PLATFORM_MODEL != PLAT_SOCFPGA_AGILEX5
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800241 /* Reset L2 module */
Hadi Asyrafi67cb0ea2019-12-23 13:25:33 +0800242 mmio_setbits_32(SOCFPGA_RSTMGR(COLDMODRST), 0x100);
Jit Loon Limb24dddf2023-05-17 12:26:11 +0800243#endif
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800244
245 while (1)
246 wfi();
247
248 /* Should not reach here */
249 return 0;
250}
251
Hadi Asyrafi616da772019-06-27 11:34:03 +0800252int socfpga_validate_power_state(unsigned int power_state,
253 psci_power_state_t *req_state)
254{
255 VERBOSE("%s: power_state: 0x%x\n", __func__, power_state);
256
257 return PSCI_E_SUCCESS;
258}
259
260int socfpga_validate_ns_entrypoint(unsigned long ns_entrypoint)
261{
262 VERBOSE("%s: ns_entrypoint: 0x%lx\n", __func__, ns_entrypoint);
263 return PSCI_E_SUCCESS;
264}
265
266void socfpga_get_sys_suspend_power_state(psci_power_state_t *req_state)
267{
268 req_state->pwr_domain_state[PSCI_CPU_PWR_LVL] = PLAT_MAX_OFF_STATE;
269 req_state->pwr_domain_state[1] = PLAT_MAX_OFF_STATE;
270}
271
272/*******************************************************************************
273 * Export the platform handlers via plat_arm_psci_pm_ops. The ARM Standard
274 * platform layer will take care of registering the handlers with PSCI.
275 ******************************************************************************/
276const plat_psci_ops_t socfpga_psci_pm_ops = {
277 .cpu_standby = socfpga_cpu_standby,
278 .pwr_domain_on = socfpga_pwr_domain_on,
279 .pwr_domain_off = socfpga_pwr_domain_off,
280 .pwr_domain_suspend = socfpga_pwr_domain_suspend,
281 .pwr_domain_on_finish = socfpga_pwr_domain_on_finish,
282 .pwr_domain_suspend_finish = socfpga_pwr_domain_suspend_finish,
283 .system_off = socfpga_system_off,
284 .system_reset = socfpga_system_reset,
Hadi Asyrafi5fae68f2019-10-22 14:23:57 +0800285 .system_reset2 = socfpga_system_reset2,
Hadi Asyrafi616da772019-06-27 11:34:03 +0800286 .validate_power_state = socfpga_validate_power_state,
287 .validate_ns_entrypoint = socfpga_validate_ns_entrypoint,
288 .get_sys_suspend_power_state = socfpga_get_sys_suspend_power_state
289};
290
291/*******************************************************************************
292 * Export the platform specific power ops.
293 ******************************************************************************/
294int plat_setup_psci_ops(uintptr_t sec_entrypoint,
295 const struct plat_psci_ops **psci_ops)
296{
297 /* Save warm boot entrypoint.*/
Hadi Asyrafia2edf0e2019-10-22 13:39:14 +0800298 mmio_write_64(PLAT_SEC_ENTRY, sec_entrypoint);
Hadi Asyrafi616da772019-06-27 11:34:03 +0800299 *psci_ops = &socfpga_psci_pm_ops;
Hadi Asyrafia2edf0e2019-10-22 13:39:14 +0800300
Hadi Asyrafi616da772019-06-27 11:34:03 +0800301 return 0;
302}