blob: 48bff7ba57bb8d0c436e86a04027069408df4f3f [file] [log] [blame]
Antonio Nino Diaz272e8712018-09-18 01:36:00 +01001/*
Carlo Caionee5a30db2019-08-24 17:31:51 +01002 * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
Antonio Nino Diaz272e8712018-09-18 01:36:00 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <arch_helpers.h>
Carlo Caione41f0ed32019-09-03 12:38:58 +01008#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <common/debug.h>
10#include <drivers/arm/gicv2.h>
11#include <drivers/console.h>
Carlo Caione41f0ed32019-09-03 12:38:58 +010012#include <errno.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <lib/mmio.h>
14#include <lib/psci/psci.h>
15#include <plat/common/platform.h>
Carlo Caione41f0ed32019-09-03 12:38:58 +010016#include <platform_def.h>
Antonio Nino Diaz272e8712018-09-18 01:36:00 +010017
Carlo Caionee5a30db2019-08-24 17:31:51 +010018#include "aml_private.h"
Antonio Nino Diaz272e8712018-09-18 01:36:00 +010019
20#define SCPI_POWER_ON 0
21#define SCPI_POWER_RETENTION 1
22#define SCPI_POWER_OFF 3
23
24#define SCPI_SYSTEM_SHUTDOWN 0
25#define SCPI_SYSTEM_REBOOT 1
26
27static uintptr_t gxbb_sec_entrypoint;
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +010028static volatile uint32_t gxbb_cpu0_go;
Antonio Nino Diaz272e8712018-09-18 01:36:00 +010029
30static void gxbb_program_mailbox(u_register_t mpidr, uint64_t value)
31{
Carlo Caione1afdfb02019-08-24 18:47:06 +010032 unsigned int core = plat_calc_core_pos(mpidr);
Carlo Caione883e3ca2019-08-28 15:19:56 +010033 uintptr_t cpu_mailbox_addr = AML_PSCI_MAILBOX_BASE + (core << 4);
Antonio Nino Diaz272e8712018-09-18 01:36:00 +010034
35 mmio_write_64(cpu_mailbox_addr, value);
36 flush_dcache_range(cpu_mailbox_addr, sizeof(uint64_t));
37}
38
39static void __dead2 gxbb_system_reset(void)
40{
41 INFO("BL31: PSCI_SYSTEM_RESET\n");
42
Carlo Caione883e3ca2019-08-28 15:19:56 +010043 uint32_t status = mmio_read_32(AML_AO_RTI_STATUS_REG3);
Antonio Nino Diaz272e8712018-09-18 01:36:00 +010044
45 NOTICE("BL31: Reboot reason: 0x%x\n", status);
46
47 status &= 0xFFFF0FF0;
48
49 console_flush();
50
Carlo Caione883e3ca2019-08-28 15:19:56 +010051 mmio_write_32(AML_AO_RTI_STATUS_REG3, status);
Antonio Nino Diaz272e8712018-09-18 01:36:00 +010052
Carlo Caione7bb83022019-08-28 10:08:24 +010053 int ret = aml_scpi_sys_power_state(SCPI_SYSTEM_REBOOT);
Antonio Nino Diaz272e8712018-09-18 01:36:00 +010054
55 if (ret != 0) {
56 ERROR("BL31: PSCI_SYSTEM_RESET: SCP error: %u\n", ret);
57 panic();
58 }
59
60 wfi();
61
62 ERROR("BL31: PSCI_SYSTEM_RESET: Operation not handled\n");
63 panic();
64}
65
66static void __dead2 gxbb_system_off(void)
67{
68 INFO("BL31: PSCI_SYSTEM_OFF\n");
69
Carlo Caione7bb83022019-08-28 10:08:24 +010070 unsigned int ret = aml_scpi_sys_power_state(SCPI_SYSTEM_SHUTDOWN);
Antonio Nino Diaz272e8712018-09-18 01:36:00 +010071
72 if (ret != 0) {
73 ERROR("BL31: PSCI_SYSTEM_OFF: SCP error %u\n", ret);
74 panic();
75 }
76
77 gxbb_program_mailbox(read_mpidr_el1(), 0);
78
79 wfi();
80
81 ERROR("BL31: PSCI_SYSTEM_OFF: Operation not handled\n");
82 panic();
83}
84
85static int32_t gxbb_pwr_domain_on(u_register_t mpidr)
86{
Carlo Caione1afdfb02019-08-24 18:47:06 +010087 unsigned int core = plat_calc_core_pos(mpidr);
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +010088
89 /* CPU0 can't be turned OFF, emulate it with a WFE loop */
Carlo Caione1afdfb02019-08-24 18:47:06 +010090 if (core == AML_PRIMARY_CPU) {
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +010091 VERBOSE("BL31: Releasing CPU0 from wait loop...\n");
92
93 gxbb_cpu0_go = 1;
94 flush_dcache_range((uintptr_t)&gxbb_cpu0_go, sizeof(gxbb_cpu0_go));
95 dsb();
96 isb();
97
98 sev();
99
100 return PSCI_E_SUCCESS;
101 }
102
Antonio Nino Diaz272e8712018-09-18 01:36:00 +0100103 gxbb_program_mailbox(mpidr, gxbb_sec_entrypoint);
Carlo Caione7bb83022019-08-28 10:08:24 +0100104 aml_scpi_set_css_power_state(mpidr,
105 SCPI_POWER_ON, SCPI_POWER_ON, SCPI_POWER_ON);
Antonio Nino Diaz272e8712018-09-18 01:36:00 +0100106 dmbsy();
107 sev();
108
109 return PSCI_E_SUCCESS;
110}
111
112static void gxbb_pwr_domain_on_finish(const psci_power_state_t *target_state)
113{
Carlo Caione1afdfb02019-08-24 18:47:06 +0100114 unsigned int core = plat_calc_core_pos(read_mpidr_el1());
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100115
Antonio Nino Diaz272e8712018-09-18 01:36:00 +0100116 assert(target_state->pwr_domain_state[MPIDR_AFFLVL0] ==
117 PLAT_LOCAL_STATE_OFF);
118
Carlo Caione1afdfb02019-08-24 18:47:06 +0100119 if (core == AML_PRIMARY_CPU) {
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100120 gxbb_cpu0_go = 0;
121 flush_dcache_range((uintptr_t)&gxbb_cpu0_go, sizeof(gxbb_cpu0_go));
122 dsb();
123 isb();
124 }
125
Antonio Nino Diaz272e8712018-09-18 01:36:00 +0100126 gicv2_pcpu_distif_init();
127 gicv2_cpuif_enable();
128}
129
Antonio Nino Diazc76e6032018-10-05 20:42:42 +0100130static void gxbb_pwr_domain_off(const psci_power_state_t *target_state)
131{
132 u_register_t mpidr = read_mpidr_el1();
Carlo Caione1afdfb02019-08-24 18:47:06 +0100133 unsigned int core = plat_calc_core_pos(mpidr);
Carlo Caione883e3ca2019-08-28 15:19:56 +0100134 uintptr_t addr = AML_PSCI_MAILBOX_BASE + 8 + (core << 4);
Antonio Nino Diazc76e6032018-10-05 20:42:42 +0100135
136 mmio_write_32(addr, 0xFFFFFFFF);
137 flush_dcache_range(addr, sizeof(uint32_t));
138
139 gicv2_cpuif_disable();
140
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100141 /* CPU0 can't be turned OFF, emulate it with a WFE loop */
Carlo Caione1afdfb02019-08-24 18:47:06 +0100142 if (core == AML_PRIMARY_CPU)
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100143 return;
144
Carlo Caione7bb83022019-08-28 10:08:24 +0100145 aml_scpi_set_css_power_state(mpidr,
146 SCPI_POWER_OFF, SCPI_POWER_ON, SCPI_POWER_ON);
Antonio Nino Diazc76e6032018-10-05 20:42:42 +0100147}
148
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100149static void __dead2 gxbb_pwr_domain_pwr_down_wfi(const psci_power_state_t
150 *target_state)
151{
Carlo Caione1afdfb02019-08-24 18:47:06 +0100152 unsigned int core = plat_calc_core_pos(read_mpidr_el1());
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100153
154 /* CPU0 can't be turned OFF, emulate it with a WFE loop */
Carlo Caione1afdfb02019-08-24 18:47:06 +0100155 if (core == AML_PRIMARY_CPU) {
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100156 VERBOSE("BL31: CPU0 entering wait loop...\n");
157
158 while (gxbb_cpu0_go == 0)
159 wfe();
160
161 VERBOSE("BL31: CPU0 resumed.\n");
162
163 write_rmr_el3(RMR_EL3_RR_BIT | RMR_EL3_AA64_BIT);
164 }
165
166 dsbsy();
167
168 for (;;)
169 wfi();
170}
171
Antonio Nino Diaz272e8712018-09-18 01:36:00 +0100172/*******************************************************************************
173 * Platform handlers and setup function.
174 ******************************************************************************/
175static const plat_psci_ops_t gxbb_ops = {
176 .pwr_domain_on = gxbb_pwr_domain_on,
177 .pwr_domain_on_finish = gxbb_pwr_domain_on_finish,
Antonio Nino Diazc76e6032018-10-05 20:42:42 +0100178 .pwr_domain_off = gxbb_pwr_domain_off,
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100179 .pwr_domain_pwr_down_wfi = gxbb_pwr_domain_pwr_down_wfi,
Antonio Nino Diaz272e8712018-09-18 01:36:00 +0100180 .system_off = gxbb_system_off,
181 .system_reset = gxbb_system_reset,
182};
183
184int plat_setup_psci_ops(uintptr_t sec_entrypoint,
185 const plat_psci_ops_t **psci_ops)
186{
187 gxbb_sec_entrypoint = sec_entrypoint;
188 *psci_ops = &gxbb_ops;
Antonio Nino Diaz07d364d2018-10-10 23:50:35 +0100189 gxbb_cpu0_go = 0;
Antonio Nino Diaz272e8712018-09-18 01:36:00 +0100190 return 0;
191}