Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 |
| 4 | * Author: Chen-Yu Tsai <wens@csie.org> |
| 5 | * |
| 6 | * Based on assembly code by Marc Zyngier <marc.zyngier@arm.com>, |
| 7 | * which was based on code by Carl van Schaik <carl@ok-labs.com>. |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 8 | */ |
| 9 | #include <config.h> |
| 10 | #include <common.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 11 | #include <asm/cache.h> |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 12 | |
| 13 | #include <asm/arch/cpu.h> |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 14 | #include <asm/armv7.h> |
| 15 | #include <asm/gic.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <asm/psci.h> |
Chen-Yu Tsai | 7ca1450 | 2016-06-19 12:38:41 +0800 | [diff] [blame] | 18 | #include <asm/secure.h> |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 19 | #include <asm/system.h> |
| 20 | |
| 21 | #include <linux/bitops.h> |
| 22 | |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 23 | #define __irq __attribute__ ((interrupt ("IRQ"))) |
| 24 | |
| 25 | #define GICD_BASE (SUNXI_GIC400_BASE + GIC_DIST_OFFSET) |
| 26 | #define GICC_BASE (SUNXI_GIC400_BASE + GIC_CPU_OFFSET_A15) |
| 27 | |
Chen-Yu Tsai | b1a1fda | 2017-03-01 11:03:15 +0800 | [diff] [blame] | 28 | /* |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 29 | * Offsets into the CPUCFG block applicable to most SUNXIs. |
| 30 | */ |
| 31 | #define SUNXI_CPU_RST(cpu) (0x40 + (cpu) * 0x40 + 0x0) |
| 32 | #define SUNXI_CPU_STATUS(cpu) (0x40 + (cpu) * 0x40 + 0x8) |
| 33 | #define SUNXI_GEN_CTRL (0x184) |
| 34 | #define SUNXI_PRIV0 (0x1a4) |
| 35 | #define SUN7I_CPU1_PWR_CLAMP (0x1b0) |
| 36 | #define SUN7I_CPU1_PWROFF (0x1b4) |
| 37 | #define SUNXI_DBG_CTRL1 (0x1e4) |
| 38 | |
| 39 | /* |
Chen-Yu Tsai | b1a1fda | 2017-03-01 11:03:15 +0800 | [diff] [blame] | 40 | * R40 is different from other single cluster SoCs. |
| 41 | * |
| 42 | * The power clamps are located in the unused space after the per-core |
| 43 | * reset controls for core 3. The secondary core entry address register |
| 44 | * is in the SRAM controller address range. |
| 45 | */ |
| 46 | #define SUN8I_R40_PWROFF (0x110) |
| 47 | #define SUN8I_R40_PWR_CLAMP(cpu) (0x120 + (cpu) * 0x4) |
| 48 | #define SUN8I_R40_SRAMC_SOFT_ENTRY_REG0 (0xbc) |
| 49 | |
Sam Edwards | 21e27f0 | 2023-10-11 19:47:56 -0600 | [diff] [blame^] | 50 | /* |
| 51 | * R528 is also different, as it has both cores powered up (but held in reset |
| 52 | * state) after the SoC is reset. Like the R40, it uses a "soft" entry point |
| 53 | * address register, but unlike the R40, it uses a newer "CPUX" block to manage |
| 54 | * CPU state, rather than the older CPUCFG system. |
| 55 | */ |
| 56 | #define SUN8I_R528_SOFT_ENTRY (0x1c8) |
| 57 | #define SUN8I_R528_C0_RST_CTRL (0x0000) |
| 58 | #define SUN8I_R528_C0_CTRL_REG0 (0x0010) |
| 59 | #define SUN8I_R528_C0_CPU_STATUS (0x0080) |
| 60 | |
| 61 | #define SUN8I_R528_C0_STATUS_STANDBYWFI (16) |
| 62 | |
| 63 | /* Only newer cores have this additional IP block. */ |
| 64 | #ifndef SUNXI_R_CPUCFG_BASE |
| 65 | #define SUNXI_R_CPUCFG_BASE 0 |
| 66 | #endif |
| 67 | |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 68 | static void __secure cp15_write_cntp_tval(u32 tval) |
| 69 | { |
| 70 | asm volatile ("mcr p15, 0, %0, c14, c2, 0" : : "r" (tval)); |
| 71 | } |
| 72 | |
| 73 | static void __secure cp15_write_cntp_ctl(u32 val) |
| 74 | { |
| 75 | asm volatile ("mcr p15, 0, %0, c14, c2, 1" : : "r" (val)); |
| 76 | } |
| 77 | |
| 78 | static u32 __secure cp15_read_cntp_ctl(void) |
| 79 | { |
| 80 | u32 val; |
| 81 | |
| 82 | asm volatile ("mrc p15, 0, %0, c14, c2, 1" : "=r" (val)); |
| 83 | |
| 84 | return val; |
| 85 | } |
| 86 | |
Peng Fan | e7c5939 | 2022-04-13 17:47:22 +0800 | [diff] [blame] | 87 | #define ONE_MS (CONFIG_COUNTER_FREQUENCY / 1000) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 88 | |
| 89 | static void __secure __mdelay(u32 ms) |
| 90 | { |
| 91 | u32 reg = ONE_MS * ms; |
| 92 | |
| 93 | cp15_write_cntp_tval(reg); |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 94 | isb(); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 95 | cp15_write_cntp_ctl(3); |
| 96 | |
| 97 | do { |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 98 | isb(); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 99 | reg = cp15_read_cntp_ctl(); |
| 100 | } while (!(reg & BIT(2))); |
| 101 | |
| 102 | cp15_write_cntp_ctl(0); |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 103 | isb(); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 104 | } |
| 105 | |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 106 | static void __secure clamp_release(u32 *clamp) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 107 | { |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 108 | u32 tmp = 0x1ff; |
| 109 | do { |
| 110 | tmp >>= 1; |
| 111 | writel(tmp, clamp); |
| 112 | } while (tmp); |
| 113 | |
| 114 | __mdelay(10); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 115 | } |
| 116 | |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 117 | static void __secure clamp_set(u32 *clamp) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 118 | { |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 119 | writel(0xff, clamp); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 120 | } |
| 121 | |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 122 | static void __secure sunxi_cpu_set_entry(int __always_unused cpu, void *entry) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 123 | { |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 124 | if (IS_ENABLED(CONFIG_MACH_SUN8I_R40)) { |
| 125 | writel((u32)entry, |
| 126 | SUNXI_SRAMC_BASE + SUN8I_R40_SRAMC_SOFT_ENTRY_REG0); |
Sam Edwards | 21e27f0 | 2023-10-11 19:47:56 -0600 | [diff] [blame^] | 127 | } else if (IS_ENABLED(CONFIG_MACH_SUN8I_R528)) { |
| 128 | writel((u32)entry, |
| 129 | SUNXI_R_CPUCFG_BASE + SUN8I_R528_SOFT_ENTRY); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 130 | } else { |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 131 | writel((u32)entry, SUNXI_CPUCFG_BASE + SUNXI_PRIV0); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 135 | static void __secure sunxi_cpu_set_power(int cpu, bool on) |
Chen-Yu Tsai | c4a8760 | 2017-06-07 15:11:49 +0800 | [diff] [blame] | 136 | { |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 137 | u32 *clamp = NULL; |
| 138 | u32 *pwroff; |
Chen-Yu Tsai | c4a8760 | 2017-06-07 15:11:49 +0800 | [diff] [blame] | 139 | |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 140 | /* sun7i (A20) is different from other single cluster SoCs */ |
| 141 | if (IS_ENABLED(CONFIG_MACH_SUN7I)) { |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 142 | clamp = (void *)SUNXI_CPUCFG_BASE + SUN7I_CPU1_PWR_CLAMP; |
| 143 | pwroff = (void *)SUNXI_CPUCFG_BASE + SUN7I_CPU1_PWROFF; |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 144 | cpu = 0; |
| 145 | } else if (IS_ENABLED(CONFIG_MACH_SUN8I_R40)) { |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 146 | clamp = (void *)SUNXI_CPUCFG_BASE + SUN8I_R40_PWR_CLAMP(cpu); |
| 147 | pwroff = (void *)SUNXI_CPUCFG_BASE + SUN8I_R40_PWROFF; |
Sam Edwards | 21e27f0 | 2023-10-11 19:47:56 -0600 | [diff] [blame^] | 148 | } else if (IS_ENABLED(CONFIG_MACH_SUN8I_R528)) { |
| 149 | /* R528 leaves both cores powered up, manages them via reset */ |
| 150 | return; |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 151 | } else { |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 152 | if (IS_ENABLED(CONFIG_MACH_SUN6I) || |
| 153 | IS_ENABLED(CONFIG_MACH_SUN8I_H3)) |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 154 | clamp = (void *)SUNXI_PRCM_BASE + 0x140 + cpu * 0x4; |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 155 | |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 156 | pwroff = (void *)SUNXI_PRCM_BASE + 0x100; |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 157 | } |
Chen-Yu Tsai | b1a1fda | 2017-03-01 11:03:15 +0800 | [diff] [blame] | 158 | |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 159 | if (on) { |
| 160 | /* Release power clamp */ |
| 161 | if (clamp) |
| 162 | clamp_release(clamp); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 163 | |
Sam Edwards | cbb505c | 2023-10-11 19:47:53 -0600 | [diff] [blame] | 164 | /* Clear power gating */ |
| 165 | clrbits_le32(pwroff, BIT(cpu)); |
| 166 | } else { |
| 167 | /* Set power gating */ |
| 168 | setbits_le32(pwroff, BIT(cpu)); |
| 169 | |
| 170 | /* Activate power clamp */ |
| 171 | if (clamp) |
| 172 | clamp_set(clamp); |
| 173 | } |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 174 | } |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 175 | |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 176 | static void __secure sunxi_cpu_set_reset(int cpu, bool reset) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 177 | { |
Sam Edwards | 21e27f0 | 2023-10-11 19:47:56 -0600 | [diff] [blame^] | 178 | if (IS_ENABLED(CONFIG_MACH_SUN8I_R528)) { |
| 179 | if (reset) |
| 180 | clrbits_le32(SUNXI_CPUCFG_BASE + SUN8I_R528_C0_RST_CTRL, |
| 181 | BIT(cpu)); |
| 182 | else |
| 183 | setbits_le32(SUNXI_CPUCFG_BASE + SUN8I_R528_C0_RST_CTRL, |
| 184 | BIT(cpu)); |
| 185 | |
| 186 | return; |
| 187 | } |
| 188 | |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 189 | writel(reset ? 0b00 : 0b11, SUNXI_CPUCFG_BASE + SUNXI_CPU_RST(cpu)); |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void __secure sunxi_cpu_set_locking(int cpu, bool lock) |
| 193 | { |
Sam Edwards | 21e27f0 | 2023-10-11 19:47:56 -0600 | [diff] [blame^] | 194 | if (IS_ENABLED(CONFIG_MACH_SUN8I_R528)) { |
| 195 | /* Not required on R528 */ |
| 196 | return; |
| 197 | } |
| 198 | |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 199 | if (lock) |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 200 | clrbits_le32(SUNXI_CPUCFG_BASE + SUNXI_DBG_CTRL1, BIT(cpu)); |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 201 | else |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 202 | setbits_le32(SUNXI_CPUCFG_BASE + SUNXI_DBG_CTRL1, BIT(cpu)); |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static bool __secure sunxi_cpu_poll_wfi(int cpu) |
| 206 | { |
Sam Edwards | 21e27f0 | 2023-10-11 19:47:56 -0600 | [diff] [blame^] | 207 | if (IS_ENABLED(CONFIG_MACH_SUN8I_R528)) { |
| 208 | return !!(readl(SUNXI_CPUCFG_BASE + SUN8I_R528_C0_CPU_STATUS) & |
| 209 | BIT(SUN8I_R528_C0_STATUS_STANDBYWFI + cpu)); |
| 210 | } |
| 211 | |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 212 | return !!(readl(SUNXI_CPUCFG_BASE + SUNXI_CPU_STATUS(cpu)) & BIT(2)); |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static void __secure sunxi_cpu_invalidate_cache(int cpu) |
| 216 | { |
Sam Edwards | 21e27f0 | 2023-10-11 19:47:56 -0600 | [diff] [blame^] | 217 | if (IS_ENABLED(CONFIG_MACH_SUN8I_R528)) { |
| 218 | clrbits_le32(SUNXI_CPUCFG_BASE + SUN8I_R528_C0_CTRL_REG0, |
| 219 | BIT(cpu)); |
| 220 | return; |
| 221 | } |
| 222 | |
Sam Edwards | 28c6899 | 2023-10-11 19:47:55 -0600 | [diff] [blame] | 223 | clrbits_le32(SUNXI_CPUCFG_BASE + SUNXI_GEN_CTRL, BIT(cpu)); |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static void __secure sunxi_cpu_power_off(u32 cpuid) |
| 227 | { |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 228 | u32 cpu = cpuid & 0x3; |
| 229 | |
| 230 | /* Wait for the core to enter WFI */ |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 231 | while (!sunxi_cpu_poll_wfi(cpu)) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 232 | __mdelay(1); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 233 | |
| 234 | /* Assert reset on target CPU */ |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 235 | sunxi_cpu_set_reset(cpu, true); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 236 | |
| 237 | /* Lock CPU (Disable external debug access) */ |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 238 | sunxi_cpu_set_locking(cpu, true); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 239 | |
| 240 | /* Power down CPU */ |
| 241 | sunxi_cpu_set_power(cpuid, false); |
| 242 | |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 243 | /* Unlock CPU (Reenable external debug access) */ |
| 244 | sunxi_cpu_set_locking(cpu, false); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static u32 __secure cp15_read_scr(void) |
| 248 | { |
| 249 | u32 scr; |
| 250 | |
| 251 | asm volatile ("mrc p15, 0, %0, c1, c1, 0" : "=r" (scr)); |
| 252 | |
| 253 | return scr; |
| 254 | } |
| 255 | |
| 256 | static void __secure cp15_write_scr(u32 scr) |
| 257 | { |
| 258 | asm volatile ("mcr p15, 0, %0, c1, c1, 0" : : "r" (scr)); |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 259 | isb(); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Although this is an FIQ handler, the FIQ is processed in monitor mode, |
| 264 | * which means there's no FIQ banked registers. This is the same as IRQ |
| 265 | * mode, so use the IRQ attribute to ask the compiler to handler entry |
| 266 | * and return. |
| 267 | */ |
| 268 | void __secure __irq psci_fiq_enter(void) |
| 269 | { |
| 270 | u32 scr, reg, cpu; |
| 271 | |
| 272 | /* Switch to secure mode */ |
| 273 | scr = cp15_read_scr(); |
| 274 | cp15_write_scr(scr & ~BIT(0)); |
| 275 | |
| 276 | /* Validate reason based on IAR and acknowledge */ |
| 277 | reg = readl(GICC_BASE + GICC_IAR); |
| 278 | |
| 279 | /* Skip spurious interrupts 1022 and 1023 */ |
| 280 | if (reg == 1023 || reg == 1022) |
| 281 | goto out; |
| 282 | |
| 283 | /* End of interrupt */ |
| 284 | writel(reg, GICC_BASE + GICC_EOIR); |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 285 | dsb(); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 286 | |
| 287 | /* Get CPU number */ |
| 288 | cpu = (reg >> 10) & 0x7; |
| 289 | |
| 290 | /* Power off the CPU */ |
| 291 | sunxi_cpu_power_off(cpu); |
| 292 | |
| 293 | out: |
| 294 | /* Restore security level */ |
| 295 | cp15_write_scr(scr); |
| 296 | } |
| 297 | |
Patrick Delaunay | 93b114c | 2018-04-16 10:15:11 +0200 | [diff] [blame] | 298 | int __secure psci_cpu_on(u32 __always_unused unused, u32 mpidr, u32 pc, |
| 299 | u32 context_id) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 300 | { |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 301 | u32 cpu = (mpidr & 0x3); |
| 302 | |
Patrick Delaunay | 93b114c | 2018-04-16 10:15:11 +0200 | [diff] [blame] | 303 | /* store target PC and context id */ |
| 304 | psci_save(cpu, pc, context_id); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 305 | |
| 306 | /* Set secondary core power on PC */ |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 307 | sunxi_cpu_set_entry(cpu, &psci_cpu_entry); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 308 | |
| 309 | /* Assert reset on target CPU */ |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 310 | sunxi_cpu_set_reset(cpu, true); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 311 | |
| 312 | /* Invalidate L1 cache */ |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 313 | sunxi_cpu_invalidate_cache(cpu); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 314 | |
| 315 | /* Lock CPU (Disable external debug access) */ |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 316 | sunxi_cpu_set_locking(cpu, true); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 317 | |
| 318 | /* Power up target CPU */ |
| 319 | sunxi_cpu_set_power(cpu, true); |
| 320 | |
| 321 | /* De-assert reset on target CPU */ |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 322 | sunxi_cpu_set_reset(cpu, false); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 323 | |
Sam Edwards | 1f684b5 | 2023-10-11 19:47:54 -0600 | [diff] [blame] | 324 | /* Unlock CPU (Reenable external debug access) */ |
| 325 | sunxi_cpu_set_locking(cpu, false); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 326 | |
| 327 | return ARM_PSCI_RET_SUCCESS; |
| 328 | } |
| 329 | |
Patrick Delaunay | 9c59d86 | 2019-07-22 14:19:20 +0200 | [diff] [blame] | 330 | s32 __secure psci_cpu_off(void) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 331 | { |
| 332 | psci_cpu_off_common(); |
| 333 | |
| 334 | /* Ask CPU0 via SGI15 to pull the rug... */ |
| 335 | writel(BIT(16) | 15, GICD_BASE + GICD_SGIR); |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 336 | dsb(); |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 337 | |
| 338 | /* Wait to be turned off */ |
| 339 | while (1) |
| 340 | wfi(); |
| 341 | } |
| 342 | |
Chen-Yu Tsai | 766c22e | 2016-06-19 12:38:32 +0800 | [diff] [blame] | 343 | void __secure psci_arch_init(void) |
Chen-Yu Tsai | 60e0b18 | 2016-06-07 10:54:34 +0800 | [diff] [blame] | 344 | { |
| 345 | u32 reg; |
| 346 | |
| 347 | /* SGI15 as Group-0 */ |
| 348 | clrbits_le32(GICD_BASE + GICD_IGROUPRn, BIT(15)); |
| 349 | |
| 350 | /* Set SGI15 priority to 0 */ |
| 351 | writeb(0, GICD_BASE + GICD_IPRIORITYRn + 15); |
| 352 | |
| 353 | /* Be cool with non-secure */ |
| 354 | writel(0xff, GICC_BASE + GICC_PMR); |
| 355 | |
| 356 | /* Switch FIQEn on */ |
| 357 | setbits_le32(GICC_BASE + GICC_CTLR, BIT(3)); |
| 358 | |
| 359 | reg = cp15_read_scr(); |
| 360 | reg |= BIT(2); /* Enable FIQ in monitor mode */ |
| 361 | reg &= ~BIT(0); /* Secure mode */ |
| 362 | cp15_write_scr(reg); |
| 363 | } |