blob: 6dfb6c905ab30c6aa7147b0da641104f7af571f1 [file] [log] [blame]
developer2189d3a2020-04-17 17:14:23 +08001/*
2 * Copyright (c) 2020, MediaTek Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/* common headers */
developer0b0c04d2020-06-16 11:48:36 +08008#include <assert.h>
9
developerbd31bf02020-07-02 12:10:13 +080010#include <arch_helpers.h>
11#include <common/debug.h>
12#include <drivers/gpio.h>
developer2189d3a2020-04-17 17:14:23 +080013#include <lib/psci/psci.h>
14
developer0b0c04d2020-06-16 11:48:36 +080015/* platform specific headers */
16#include <mt_gic_v3.h>
developer4d055072020-08-25 22:31:14 +080017#include <mtk_ptp3_common.h>
developer0b0c04d2020-06-16 11:48:36 +080018#include <mtspmc.h>
19#include <plat/common/platform.h>
20#include <plat_mtk_lpm.h>
developerbd31bf02020-07-02 12:10:13 +080021#include <plat_params.h>
developer0b0c04d2020-06-16 11:48:36 +080022#include <plat_pm.h>
developerea0c4732020-08-12 16:32:10 +080023#include <pmic.h>
developerbeab2d72020-10-14 20:14:37 +080024#include <rtc.h>
developer0b0c04d2020-06-16 11:48:36 +080025
26/*
27 * Cluster state request:
28 * [0] : The CPU requires cluster power down
29 * [1] : The CPU requires cluster power on
30 */
31#define coordinate_cluster(onoff) write_clusterpwrdn_el1(onoff)
32#define coordinate_cluster_pwron() coordinate_cluster(1)
33#define coordinate_cluster_pwroff() coordinate_cluster(0)
34
35/* platform secure entry point */
36static uintptr_t secure_entrypoint;
37/* per-CPU power state */
38static unsigned int plat_power_state[PLATFORM_CORE_COUNT];
39
40/* platform CPU power domain - ops */
41static const struct mt_lpm_tz *plat_mt_pm;
42
43#define plat_mt_pm_invoke(_name, _cpu, _state) ({ \
44 int ret = -1; \
45 if (plat_mt_pm != NULL && plat_mt_pm->_name != NULL) { \
46 ret = plat_mt_pm->_name(_cpu, _state); \
47 } \
48 ret; })
49
50#define plat_mt_pm_invoke_no_check(_name, _cpu, _state) ({ \
51 if (plat_mt_pm != NULL && plat_mt_pm->_name != NULL) { \
52 (void) plat_mt_pm->_name(_cpu, _state); \
53 } \
54 })
55
56/*
57 * Common MTK_platform operations to power on/off a
58 * CPU in response to a CPU_ON, CPU_OFF or CPU_SUSPEND request.
59 */
60
61static void plat_cpu_pwrdwn_common(unsigned int cpu,
62 const psci_power_state_t *state, unsigned int req_pstate)
63{
64 assert(cpu == plat_my_core_pos());
65
66 plat_mt_pm_invoke_no_check(pwr_cpu_dwn, cpu, state);
67
68 if ((psci_get_pstate_pwrlvl(req_pstate) >= MTK_AFFLVL_CLUSTER) ||
69 (req_pstate == 0U)) { /* hotplug off */
70 coordinate_cluster_pwroff();
71 }
72
73 /* Prevent interrupts from spuriously waking up this CPU */
74 mt_gic_rdistif_save();
75 gicv3_cpuif_disable(cpu);
76 gicv3_rdistif_off(cpu);
developer4d055072020-08-25 22:31:14 +080077 /* PTP3 config */
78 ptp3_deinit(cpu);
developer0b0c04d2020-06-16 11:48:36 +080079}
80
81static void plat_cpu_pwron_common(unsigned int cpu,
82 const psci_power_state_t *state, unsigned int req_pstate)
83{
84 assert(cpu == plat_my_core_pos());
85
86 plat_mt_pm_invoke_no_check(pwr_cpu_on, cpu, state);
87
88 coordinate_cluster_pwron();
89
developer0b0c04d2020-06-16 11:48:36 +080090 /*
91 * If mcusys does power down before then restore
92 * all CPUs' GIC Redistributors
93 */
94 if (IS_MCUSYS_OFF_STATE(state)) {
95 mt_gic_rdistif_restore_all();
96 } else {
developera6d504d2021-01-04 00:00:12 +080097 gicv3_rdistif_on(cpu);
98 gicv3_cpuif_enable(cpu);
99 mt_gic_rdistif_init();
developer0b0c04d2020-06-16 11:48:36 +0800100 mt_gic_rdistif_restore();
101 }
developer4d055072020-08-25 22:31:14 +0800102
103 /* PTP3 config */
104 ptp3_init(cpu);
developer0b0c04d2020-06-16 11:48:36 +0800105}
106
107/*
108 * Common MTK_platform operations to power on/off a
109 * cluster in response to a CPU_ON, CPU_OFF or CPU_SUSPEND request.
110 */
111
112static void plat_cluster_pwrdwn_common(unsigned int cpu,
113 const psci_power_state_t *state, unsigned int req_pstate)
114{
115 assert(cpu == plat_my_core_pos());
116
117 if (plat_mt_pm_invoke(pwr_cluster_dwn, cpu, state) != 0) {
118 coordinate_cluster_pwron();
119
120 /* TODO: return on fail.
121 * Add a 'return' here before adding any code following
122 * the if-block.
123 */
124 }
125}
developer2189d3a2020-04-17 17:14:23 +0800126
developer0b0c04d2020-06-16 11:48:36 +0800127static void plat_cluster_pwron_common(unsigned int cpu,
128 const psci_power_state_t *state, unsigned int req_pstate)
129{
130 assert(cpu == plat_my_core_pos());
131
132 if (plat_mt_pm_invoke(pwr_cluster_on, cpu, state) != 0) {
133 /* TODO: return on fail.
134 * Add a 'return' here before adding any code following
135 * the if-block.
136 */
137 }
138}
139
140/*
141 * Common MTK_platform operations to power on/off a
142 * mcusys in response to a CPU_ON, CPU_OFF or CPU_SUSPEND request.
143 */
144
145static void plat_mcusys_pwrdwn_common(unsigned int cpu,
146 const psci_power_state_t *state, unsigned int req_pstate)
147{
148 assert(cpu == plat_my_core_pos());
149
150 if (plat_mt_pm_invoke(pwr_mcusys_dwn, cpu, state) != 0) {
151 return; /* return on fail */
152 }
153
154 mt_gic_distif_save();
155 gic_sgi_save_all();
156}
157
158static void plat_mcusys_pwron_common(unsigned int cpu,
159 const psci_power_state_t *state, unsigned int req_pstate)
160{
161 assert(cpu == plat_my_core_pos());
162
163 if (plat_mt_pm_invoke(pwr_mcusys_on, cpu, state) != 0) {
164 return; /* return on fail */
165 }
166
167 mt_gic_init();
168 mt_gic_distif_restore();
169 gic_sgi_restore_all();
170
171 plat_mt_pm_invoke_no_check(pwr_mcusys_on_finished, cpu, state);
172}
173
174/*
175 * plat_psci_ops implementation
176 */
177
178static void plat_cpu_standby(plat_local_state_t cpu_state)
179{
180 uint64_t scr;
181
182 scr = read_scr_el3();
183 write_scr_el3(scr | SCR_IRQ_BIT | SCR_FIQ_BIT);
184
185 isb();
186 dsb();
187 wfi();
188
189 write_scr_el3(scr);
190}
191
192static int plat_power_domain_on(u_register_t mpidr)
193{
194 unsigned int cpu = (unsigned int)plat_core_pos_by_mpidr(mpidr);
195 unsigned int cluster = 0U;
196
197 if (cpu >= PLATFORM_CORE_COUNT) {
198 return PSCI_E_INVALID_PARAMS;
199 }
200
201 if (!spm_get_cluster_powerstate(cluster)) {
202 spm_poweron_cluster(cluster);
203 }
204
205 /* init CPU reset arch as AARCH64 */
206 mcucfg_init_archstate(cluster, cpu, true);
207 mcucfg_set_bootaddr(cluster, cpu, secure_entrypoint);
208 spm_poweron_cpu(cluster, cpu);
209
210 return PSCI_E_SUCCESS;
211}
212
213static void plat_power_domain_on_finish(const psci_power_state_t *state)
214{
215 unsigned long mpidr = read_mpidr_el1();
216 unsigned int cpu = (unsigned int)plat_core_pos_by_mpidr(mpidr);
217
218 assert(cpu < PLATFORM_CORE_COUNT);
219
220 /* Allow IRQs to wakeup this core in IDLE flow */
221 mcucfg_enable_gic_wakeup(0U, cpu);
222
223 if (IS_CLUSTER_OFF_STATE(state)) {
224 plat_cluster_pwron_common(cpu, state, 0U);
225 }
226
227 plat_cpu_pwron_common(cpu, state, 0U);
228}
229
230static void plat_power_domain_off(const psci_power_state_t *state)
231{
232 unsigned long mpidr = read_mpidr_el1();
233 unsigned int cpu = (unsigned int)plat_core_pos_by_mpidr(mpidr);
234
235 assert(cpu < PLATFORM_CORE_COUNT);
236
237 plat_cpu_pwrdwn_common(cpu, state, 0U);
238 spm_poweroff_cpu(0U, cpu);
239
240 /* prevent unintended IRQs from waking up the hot-unplugged core */
241 mcucfg_disable_gic_wakeup(0U, cpu);
242
243 if (IS_CLUSTER_OFF_STATE(state)) {
244 plat_cluster_pwrdwn_common(cpu, state, 0U);
245 }
246}
247
248static void plat_power_domain_suspend(const psci_power_state_t *state)
249{
250 unsigned int cpu = plat_my_core_pos();
251
252 assert(cpu < PLATFORM_CORE_COUNT);
253
254 plat_mt_pm_invoke_no_check(pwr_prompt, cpu, state);
255
256 /* Perform the common CPU specific operations */
257 plat_cpu_pwrdwn_common(cpu, state, plat_power_state[cpu]);
258
259 if (IS_CLUSTER_OFF_STATE(state)) {
260 /* Perform the common cluster specific operations */
261 plat_cluster_pwrdwn_common(cpu, state, plat_power_state[cpu]);
262 }
263
264 if (IS_MCUSYS_OFF_STATE(state)) {
265 /* Perform the common mcusys specific operations */
266 plat_mcusys_pwrdwn_common(cpu, state, plat_power_state[cpu]);
267 }
268}
269
270static void plat_power_domain_suspend_finish(const psci_power_state_t *state)
271{
272 unsigned int cpu = plat_my_core_pos();
273
274 assert(cpu < PLATFORM_CORE_COUNT);
275
276 if (IS_MCUSYS_OFF_STATE(state)) {
277 /* Perform the common mcusys specific operations */
278 plat_mcusys_pwron_common(cpu, state, plat_power_state[cpu]);
279 }
280
281 if (IS_CLUSTER_OFF_STATE(state)) {
282 /* Perform the common cluster specific operations */
283 plat_cluster_pwron_common(cpu, state, plat_power_state[cpu]);
284 }
285
286 /* Perform the common CPU specific operations */
287 plat_cpu_pwron_common(cpu, state, plat_power_state[cpu]);
288
289 plat_mt_pm_invoke_no_check(pwr_reflect, cpu, state);
290}
291
292static int plat_validate_power_state(unsigned int power_state,
293 psci_power_state_t *req_state)
294{
295 unsigned int pstate = psci_get_pstate_type(power_state);
296 unsigned int aff_lvl = psci_get_pstate_pwrlvl(power_state);
297 unsigned int cpu = plat_my_core_pos();
298
developer0b0c04d2020-06-16 11:48:36 +0800299 if (pstate == PSTATE_TYPE_STANDBY) {
300 req_state->pwr_domain_state[0] = PLAT_MAX_RET_STATE;
301 } else {
302 unsigned int i;
303 unsigned int pstate_id = psci_get_pstate_id(power_state);
304 plat_local_state_t s = MTK_LOCAL_STATE_OFF;
305
306 /* Use pstate_id to be power domain state */
307 if (pstate_id > s) {
308 s = (plat_local_state_t)pstate_id;
309 }
310
311 for (i = 0U; i <= aff_lvl; i++) {
312 req_state->pwr_domain_state[i] = s;
313 }
314 }
315
316 plat_power_state[cpu] = power_state;
317 return PSCI_E_SUCCESS;
318}
319
320static void plat_get_sys_suspend_power_state(psci_power_state_t *req_state)
321{
322 unsigned int lv;
323 unsigned int cpu = plat_my_core_pos();
324
325 for (lv = PSCI_CPU_PWR_LVL; lv <= PLAT_MAX_PWR_LVL; lv++) {
326 req_state->pwr_domain_state[lv] = PLAT_MAX_OFF_STATE;
327 }
328
329 plat_power_state[cpu] =
330 psci_make_powerstate(
331 MT_PLAT_PWR_STATE_SYSTEM_SUSPEND,
332 PSTATE_TYPE_POWERDOWN, PLAT_MAX_PWR_LVL);
333
334 flush_dcache_range((uintptr_t)
335 &plat_power_state[cpu],
336 sizeof(plat_power_state[cpu]));
337}
338
developerea0c4732020-08-12 16:32:10 +0800339static void __dead2 plat_mtk_system_off(void)
340{
341 INFO("MTK System Off\n");
342
developerbeab2d72020-10-14 20:14:37 +0800343 rtc_power_off_sequence();
developerea0c4732020-08-12 16:32:10 +0800344 pmic_power_off();
345
346 wfi();
347 ERROR("MTK System Off: operation not handled.\n");
348 panic();
349}
350
developerbd31bf02020-07-02 12:10:13 +0800351static void __dead2 plat_mtk_system_reset(void)
352{
353 struct bl_aux_gpio_info *gpio_reset = plat_get_mtk_gpio_reset();
354
355 INFO("MTK System Reset\n");
356
357 gpio_set_value(gpio_reset->index, gpio_reset->polarity);
358
359 wfi();
360 ERROR("MTK System Reset: operation not handled.\n");
361 panic();
362}
developer2189d3a2020-04-17 17:14:23 +0800363
developer0b0c04d2020-06-16 11:48:36 +0800364static const plat_psci_ops_t plat_psci_ops = {
365 .system_reset = plat_mtk_system_reset,
366 .cpu_standby = plat_cpu_standby,
367 .pwr_domain_on = plat_power_domain_on,
368 .pwr_domain_on_finish = plat_power_domain_on_finish,
369 .pwr_domain_off = plat_power_domain_off,
370 .pwr_domain_suspend = plat_power_domain_suspend,
371 .pwr_domain_suspend_finish = plat_power_domain_suspend_finish,
developerea0c4732020-08-12 16:32:10 +0800372 .system_off = plat_mtk_system_off,
developer0b0c04d2020-06-16 11:48:36 +0800373 .validate_power_state = plat_validate_power_state,
374 .get_sys_suspend_power_state = plat_get_sys_suspend_power_state
developer2189d3a2020-04-17 17:14:23 +0800375};
376
377int plat_setup_psci_ops(uintptr_t sec_entrypoint,
378 const plat_psci_ops_t **psci_ops)
379{
developer0b0c04d2020-06-16 11:48:36 +0800380 *psci_ops = &plat_psci_ops;
381 secure_entrypoint = sec_entrypoint;
382
383 /*
384 * init the warm reset config for boot CPU
385 * reset arch as AARCH64
386 * reset addr as function bl31_warm_entrypoint()
387 */
388 mcucfg_init_archstate(0U, 0U, true);
389 mcucfg_set_bootaddr(0U, 0U, secure_entrypoint);
390
391 spmc_init();
392 plat_mt_pm = mt_plat_cpu_pm_init();
developer2189d3a2020-04-17 17:14:23 +0800393
394 return 0;
395}