blob: 30841e2eab6f6eb06f34c6181cd647b01c68b557 [file] [log] [blame]
Samuel Holland321c0ab2017-08-12 04:07:39 -05001/*
Samuel Holland91bcab92021-01-24 06:37:29 -06002 * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved.
Samuel Holland321c0ab2017-08-12 04:07:39 -05003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Andre Przywara6d0b81b2018-09-28 00:43:32 +01007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Samuel Holland321c0ab2017-08-12 04:07:39 -05009#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/delay_timer.h>
14#include <lib/mmio.h>
15#include <lib/utils_def.h>
16#include <plat/common/platform.h>
17
Samuel Holland321c0ab2017-08-12 04:07:39 -050018#include <sunxi_cpucfg.h>
Andre Przywara6d0b81b2018-09-28 00:43:32 +010019#include <sunxi_mmap.h>
Andre Przywara456208a2018-10-14 12:02:02 +010020#include <sunxi_private.h>
Samuel Holland321c0ab2017-08-12 04:07:39 -050021
Mikhail Kalashnikov5cafd162023-03-27 18:36:14 +030022#ifndef SUNXI_C0_CPU_CTRL_REG
23#define SUNXI_C0_CPU_CTRL_REG(n) 0
24#define SUNXI_CPU_UNK_REG(n) 0
25#define SUNXI_CPU_CTRL_REG(n) 0
26#endif
27
Samuel Holland321c0ab2017-08-12 04:07:39 -050028static void sunxi_cpu_disable_power(unsigned int cluster, unsigned int core)
29{
30 if (mmio_read_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core)) == 0xff)
31 return;
32
Andre Przywara8501d092018-06-22 01:33:34 +010033 VERBOSE("PSCI: Disabling power to cluster %d core %d\n", cluster, core);
Samuel Holland321c0ab2017-08-12 04:07:39 -050034
35 mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xff);
36}
37
38static void sunxi_cpu_enable_power(unsigned int cluster, unsigned int core)
39{
40 if (mmio_read_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core)) == 0)
41 return;
42
Andre Przywara8501d092018-06-22 01:33:34 +010043 VERBOSE("PSCI: Enabling power to cluster %d core %d\n", cluster, core);
Samuel Holland321c0ab2017-08-12 04:07:39 -050044
45 /* Power enable sequence from original Allwinner sources */
46 mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xfe);
47 mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xf8);
48 mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xe0);
49 mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0x80);
50 mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0x00);
Icenowy Zheng25886842021-07-22 09:32:57 +080051 udelay(1);
Samuel Holland321c0ab2017-08-12 04:07:39 -050052}
53
Samuel Holland91bcab92021-01-24 06:37:29 -060054/* We can't turn ourself off like this, but it works for other cores. */
55static void sunxi_cpu_off(u_register_t mpidr)
Samuel Holland321c0ab2017-08-12 04:07:39 -050056{
Samuel Hollandc629daf2019-02-17 15:33:33 -060057 unsigned int cluster = MPIDR_AFFLVL1_VAL(mpidr);
58 unsigned int core = MPIDR_AFFLVL0_VAL(mpidr);
Andre Przywara6d0b81b2018-09-28 00:43:32 +010059
Andre Przywara8501d092018-06-22 01:33:34 +010060 VERBOSE("PSCI: Powering off cluster %d core %d\n", cluster, core);
Samuel Holland321c0ab2017-08-12 04:07:39 -050061
Mikhail Kalashnikov5cafd162023-03-27 18:36:14 +030062 if (sunxi_cpucfg_has_per_cluster_regs()) {
63 /* Deassert DBGPWRDUP */
64 mmio_clrbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core));
65 /* Activate the core output clamps, but not for core 0. */
66 if (core != 0) {
67 mmio_setbits_32(SUNXI_POWEROFF_GATING_REG(cluster),
68 BIT(core));
69 }
70 /* Assert CPU power-on reset */
71 mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
72 /* Remove power from the CPU */
73 sunxi_cpu_disable_power(cluster, core);
74 } else {
75 /* power down(?) debug core */
76 mmio_clrbits_32(SUNXI_C0_CPU_CTRL_REG(core), BIT(8));
77 /* ??? Activate the core output clamps, but not for core 0 */
78 if (core != 0) {
79 mmio_setbits_32(SUNXI_CPU_UNK_REG(core), BIT(1));
80 }
81 /* ??? Assert CPU power-on reset ??? */
82 mmio_clrbits_32(SUNXI_CPU_UNK_REG(core), BIT(0));
83 /* Remove power from the CPU */
84 sunxi_cpu_disable_power(cluster, core);
85 }
Samuel Holland91bcab92021-01-24 06:37:29 -060086}
Andre Przywara6d0b81b2018-09-28 00:43:32 +010087
Samuel Hollandc629daf2019-02-17 15:33:33 -060088void sunxi_cpu_on(u_register_t mpidr)
Samuel Holland321c0ab2017-08-12 04:07:39 -050089{
Samuel Hollandc629daf2019-02-17 15:33:33 -060090 unsigned int cluster = MPIDR_AFFLVL1_VAL(mpidr);
91 unsigned int core = MPIDR_AFFLVL0_VAL(mpidr);
92
Andre Przywara8501d092018-06-22 01:33:34 +010093 VERBOSE("PSCI: Powering on cluster %d core %d\n", cluster, core);
Samuel Holland321c0ab2017-08-12 04:07:39 -050094
Mikhail Kalashnikov5cafd162023-03-27 18:36:14 +030095 if (sunxi_cpucfg_has_per_cluster_regs()) {
96 /* Assert CPU core reset */
97 mmio_clrbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core));
98 /* Assert CPU power-on reset */
99 mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
100 /* Set CPU to start in AArch64 mode */
101 mmio_setbits_32(SUNXI_AA64nAA32_REG(cluster),
102 BIT(SUNXI_AA64nAA32_OFFSET + core));
103 /* Apply power to the CPU */
104 sunxi_cpu_enable_power(cluster, core);
105 /* Release the core output clamps */
106 mmio_clrbits_32(SUNXI_POWEROFF_GATING_REG(cluster), BIT(core));
107 /* Deassert CPU power-on reset */
108 mmio_setbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core));
109 /* Deassert CPU core reset */
110 mmio_setbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core));
111 /* Assert DBGPWRDUP */
112 mmio_setbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core));
113 } else {
114 /* Assert CPU core reset */
115 mmio_clrbits_32(SUNXI_C0_CPU_CTRL_REG(core), BIT(0));
116 /* ??? Assert CPU power-on reset ??? */
117 mmio_clrbits_32(SUNXI_CPU_UNK_REG(core), BIT(0));
118
119 /* Set CPU to start in AArch64 mode */
120 mmio_setbits_32(SUNXI_CPU_CTRL_REG(core), BIT(0));
121
122 /* Apply power to the CPU */
123 sunxi_cpu_enable_power(cluster, core);
124
125 /* ??? Release the core output clamps ??? */
126 mmio_clrbits_32(SUNXI_CPU_UNK_REG(core), BIT(1));
127 /* ??? Deassert CPU power-on reset ??? */
128 mmio_setbits_32(SUNXI_CPU_UNK_REG(core), BIT(0));
129 /* Deassert CPU core reset */
130 mmio_setbits_32(SUNXI_C0_CPU_CTRL_REG(core), BIT(0));
131 /* power up(?) debug core */
132 mmio_setbits_32(SUNXI_C0_CPU_CTRL_REG(core), BIT(8));
133 }
Samuel Holland321c0ab2017-08-12 04:07:39 -0500134}
135
Samuel Holland91bcab92021-01-24 06:37:29 -0600136void sunxi_cpu_power_off_others(void)
Samuel Holland321c0ab2017-08-12 04:07:39 -0500137{
Samuel Holland91bcab92021-01-24 06:37:29 -0600138 u_register_t self = read_mpidr();
Samuel Hollandc629daf2019-02-17 15:33:33 -0600139 unsigned int cluster;
140 unsigned int core;
141
142 for (cluster = 0; cluster < PLATFORM_CLUSTER_COUNT; ++cluster) {
143 for (core = 0; core < PLATFORM_MAX_CPUS_PER_CLUSTER; ++core) {
144 u_register_t mpidr = (cluster << MPIDR_AFF1_SHIFT) |
145 (core << MPIDR_AFF0_SHIFT) |
146 BIT(31);
Samuel Holland91bcab92021-01-24 06:37:29 -0600147 if (mpidr != self)
Samuel Hollandc629daf2019-02-17 15:33:33 -0600148 sunxi_cpu_off(mpidr);
149 }
Samuel Holland321c0ab2017-08-12 04:07:39 -0500150 }
151}