Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 1 | /* |
Samuel Holland | 91bcab9 | 2021-01-24 06:37:29 -0600 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved. |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Andre Przywara | 6d0b81b | 2018-09-28 00:43:32 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 9 | #include <platform_def.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 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 Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 18 | #include <sunxi_cpucfg.h> |
Andre Przywara | 6d0b81b | 2018-09-28 00:43:32 +0100 | [diff] [blame] | 19 | #include <sunxi_mmap.h> |
Andre Przywara | 456208a | 2018-10-14 12:02:02 +0100 | [diff] [blame] | 20 | #include <sunxi_private.h> |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 21 | |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 22 | static void sunxi_cpu_disable_power(unsigned int cluster, unsigned int core) |
| 23 | { |
| 24 | if (mmio_read_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core)) == 0xff) |
| 25 | return; |
| 26 | |
Andre Przywara | 8501d09 | 2018-06-22 01:33:34 +0100 | [diff] [blame] | 27 | VERBOSE("PSCI: Disabling power to cluster %d core %d\n", cluster, core); |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 28 | |
| 29 | mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xff); |
| 30 | } |
| 31 | |
| 32 | static void sunxi_cpu_enable_power(unsigned int cluster, unsigned int core) |
| 33 | { |
| 34 | if (mmio_read_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core)) == 0) |
| 35 | return; |
| 36 | |
Andre Przywara | 8501d09 | 2018-06-22 01:33:34 +0100 | [diff] [blame] | 37 | VERBOSE("PSCI: Enabling power to cluster %d core %d\n", cluster, core); |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 38 | |
| 39 | /* Power enable sequence from original Allwinner sources */ |
| 40 | mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xfe); |
| 41 | mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xf8); |
| 42 | mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xe0); |
| 43 | mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0x80); |
| 44 | mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0x00); |
Icenowy Zheng | 2588684 | 2021-07-22 09:32:57 +0800 | [diff] [blame] | 45 | udelay(1); |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Samuel Holland | 91bcab9 | 2021-01-24 06:37:29 -0600 | [diff] [blame] | 48 | /* We can't turn ourself off like this, but it works for other cores. */ |
| 49 | static void sunxi_cpu_off(u_register_t mpidr) |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 50 | { |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 51 | unsigned int cluster = MPIDR_AFFLVL1_VAL(mpidr); |
| 52 | unsigned int core = MPIDR_AFFLVL0_VAL(mpidr); |
Andre Przywara | 6d0b81b | 2018-09-28 00:43:32 +0100 | [diff] [blame] | 53 | |
Andre Przywara | 8501d09 | 2018-06-22 01:33:34 +0100 | [diff] [blame] | 54 | VERBOSE("PSCI: Powering off cluster %d core %d\n", cluster, core); |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 55 | |
| 56 | /* Deassert DBGPWRDUP */ |
| 57 | mmio_clrbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core)); |
Samuel Holland | 91bcab9 | 2021-01-24 06:37:29 -0600 | [diff] [blame] | 58 | /* Activate the core output clamps, but not for core 0. */ |
| 59 | if (core != 0) |
| 60 | mmio_setbits_32(SUNXI_POWEROFF_GATING_REG(cluster), BIT(core)); |
| 61 | /* Assert CPU power-on reset */ |
| 62 | mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core)); |
| 63 | /* Remove power from the CPU */ |
| 64 | sunxi_cpu_disable_power(cluster, core); |
| 65 | } |
Andre Przywara | 6d0b81b | 2018-09-28 00:43:32 +0100 | [diff] [blame] | 66 | |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 67 | void sunxi_cpu_on(u_register_t mpidr) |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 68 | { |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 69 | unsigned int cluster = MPIDR_AFFLVL1_VAL(mpidr); |
| 70 | unsigned int core = MPIDR_AFFLVL0_VAL(mpidr); |
| 71 | |
Andre Przywara | 8501d09 | 2018-06-22 01:33:34 +0100 | [diff] [blame] | 72 | VERBOSE("PSCI: Powering on cluster %d core %d\n", cluster, core); |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 73 | |
| 74 | /* Assert CPU core reset */ |
| 75 | mmio_clrbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core)); |
| 76 | /* Assert CPU power-on reset */ |
| 77 | mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core)); |
| 78 | /* Set CPU to start in AArch64 mode */ |
Icenowy Zheng | 6f515d0 | 2021-07-22 09:35:19 +0800 | [diff] [blame] | 79 | mmio_setbits_32(SUNXI_AA64nAA32_REG(cluster), |
| 80 | BIT(SUNXI_AA64nAA32_OFFSET + core)); |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 81 | /* Apply power to the CPU */ |
| 82 | sunxi_cpu_enable_power(cluster, core); |
| 83 | /* Release the core output clamps */ |
| 84 | mmio_clrbits_32(SUNXI_POWEROFF_GATING_REG(cluster), BIT(core)); |
| 85 | /* Deassert CPU power-on reset */ |
| 86 | mmio_setbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core)); |
| 87 | /* Deassert CPU core reset */ |
| 88 | mmio_setbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core)); |
| 89 | /* Assert DBGPWRDUP */ |
| 90 | mmio_setbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core)); |
| 91 | } |
| 92 | |
Samuel Holland | 91bcab9 | 2021-01-24 06:37:29 -0600 | [diff] [blame] | 93 | void sunxi_cpu_power_off_others(void) |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 94 | { |
Samuel Holland | 91bcab9 | 2021-01-24 06:37:29 -0600 | [diff] [blame] | 95 | u_register_t self = read_mpidr(); |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 96 | unsigned int cluster; |
| 97 | unsigned int core; |
| 98 | |
| 99 | for (cluster = 0; cluster < PLATFORM_CLUSTER_COUNT; ++cluster) { |
| 100 | for (core = 0; core < PLATFORM_MAX_CPUS_PER_CLUSTER; ++core) { |
| 101 | u_register_t mpidr = (cluster << MPIDR_AFF1_SHIFT) | |
| 102 | (core << MPIDR_AFF0_SHIFT) | |
| 103 | BIT(31); |
Samuel Holland | 91bcab9 | 2021-01-24 06:37:29 -0600 | [diff] [blame] | 104 | if (mpidr != self) |
Samuel Holland | c629daf | 2019-02-17 15:33:33 -0600 | [diff] [blame] | 105 | sunxi_cpu_off(mpidr); |
| 106 | } |
Samuel Holland | 321c0ab | 2017-08-12 04:07:39 -0500 | [diff] [blame] | 107 | } |
| 108 | } |