1333d66cfSSamuel Holland /* 2333d66cfSSamuel Holland * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. 3333d66cfSSamuel Holland * 4333d66cfSSamuel Holland * SPDX-License-Identifier: BSD-3-Clause 5333d66cfSSamuel Holland */ 6333d66cfSSamuel Holland 7*7db0c960SAndre Przywara #include <arch_helpers.h> 8*7db0c960SAndre Przywara #include <assert.h> 9*7db0c960SAndre Przywara #include <core_off_arisc.h> 10333d66cfSSamuel Holland #include <debug.h> 11*7db0c960SAndre Przywara #include <delay_timer.h> 12333d66cfSSamuel Holland #include <mmio.h> 13*7db0c960SAndre Przywara #include <platform.h> 14333d66cfSSamuel Holland #include <platform_def.h> 15333d66cfSSamuel Holland #include <sunxi_cpucfg.h> 16*7db0c960SAndre Przywara #include <sunxi_mmap.h> 174ec1a239SAndre Przywara #include <sunxi_private.h> 18333d66cfSSamuel Holland #include <utils_def.h> 19333d66cfSSamuel Holland 20333d66cfSSamuel Holland static void sunxi_cpu_disable_power(unsigned int cluster, unsigned int core) 21333d66cfSSamuel Holland { 22333d66cfSSamuel Holland if (mmio_read_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core)) == 0xff) 23333d66cfSSamuel Holland return; 24333d66cfSSamuel Holland 2527f9616fSAndre Przywara VERBOSE("PSCI: Disabling power to cluster %d core %d\n", cluster, core); 26333d66cfSSamuel Holland 27333d66cfSSamuel Holland mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xff); 28333d66cfSSamuel Holland } 29333d66cfSSamuel Holland 30333d66cfSSamuel Holland static void sunxi_cpu_enable_power(unsigned int cluster, unsigned int core) 31333d66cfSSamuel Holland { 32333d66cfSSamuel Holland if (mmio_read_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core)) == 0) 33333d66cfSSamuel Holland return; 34333d66cfSSamuel Holland 3527f9616fSAndre Przywara VERBOSE("PSCI: Enabling power to cluster %d core %d\n", cluster, core); 36333d66cfSSamuel Holland 37333d66cfSSamuel Holland /* Power enable sequence from original Allwinner sources */ 38333d66cfSSamuel Holland mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xfe); 39333d66cfSSamuel Holland mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xf8); 40333d66cfSSamuel Holland mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0xe0); 41333d66cfSSamuel Holland mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0x80); 42333d66cfSSamuel Holland mmio_write_32(SUNXI_CPU_POWER_CLAMP_REG(cluster, core), 0x00); 43333d66cfSSamuel Holland } 44333d66cfSSamuel Holland 45333d66cfSSamuel Holland void sunxi_cpu_off(unsigned int cluster, unsigned int core) 46333d66cfSSamuel Holland { 47*7db0c960SAndre Przywara int corenr = cluster * PLATFORM_MAX_CPUS_PER_CLUSTER + core; 48*7db0c960SAndre Przywara 4927f9616fSAndre Przywara VERBOSE("PSCI: Powering off cluster %d core %d\n", cluster, core); 50333d66cfSSamuel Holland 51333d66cfSSamuel Holland /* Deassert DBGPWRDUP */ 52333d66cfSSamuel Holland mmio_clrbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core)); 53*7db0c960SAndre Przywara 54*7db0c960SAndre Przywara /* We can't turn ourself off like this, but it works for other cores. */ 55*7db0c960SAndre Przywara if (plat_my_core_pos() != corenr) { 56*7db0c960SAndre Przywara /* Activate the core output clamps, but not for core 0. */ 57*7db0c960SAndre Przywara if (corenr != 0) 58*7db0c960SAndre Przywara mmio_setbits_32(SUNXI_POWEROFF_GATING_REG(cluster), 59*7db0c960SAndre Przywara BIT(core)); 60333d66cfSSamuel Holland /* Assert CPU power-on reset */ 61333d66cfSSamuel Holland mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core)); 62333d66cfSSamuel Holland /* Remove power from the CPU */ 63333d66cfSSamuel Holland sunxi_cpu_disable_power(cluster, core); 64*7db0c960SAndre Przywara 65*7db0c960SAndre Przywara return; 66*7db0c960SAndre Przywara } 67*7db0c960SAndre Przywara 68*7db0c960SAndre Przywara /* Simplifies assembly, all SoCs so far are single cluster anyway. */ 69*7db0c960SAndre Przywara assert(cluster == 0); 70*7db0c960SAndre Przywara 71*7db0c960SAndre Przywara /* 72*7db0c960SAndre Przywara * If we are supposed to turn ourself off, tell the arisc SCP 73*7db0c960SAndre Przywara * to do that work for us. The code expects the core mask to be 74*7db0c960SAndre Przywara * patched into the first instruction. 75*7db0c960SAndre Przywara */ 76*7db0c960SAndre Przywara sunxi_execute_arisc_code(arisc_core_off, sizeof(arisc_core_off), 77*7db0c960SAndre Przywara 0, BIT_32(core)); 78333d66cfSSamuel Holland } 79333d66cfSSamuel Holland 80333d66cfSSamuel Holland void sunxi_cpu_on(unsigned int cluster, unsigned int core) 81333d66cfSSamuel Holland { 8227f9616fSAndre Przywara VERBOSE("PSCI: Powering on cluster %d core %d\n", cluster, core); 83333d66cfSSamuel Holland 84333d66cfSSamuel Holland /* Assert CPU core reset */ 85333d66cfSSamuel Holland mmio_clrbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core)); 86333d66cfSSamuel Holland /* Assert CPU power-on reset */ 87333d66cfSSamuel Holland mmio_clrbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core)); 88333d66cfSSamuel Holland /* Set CPU to start in AArch64 mode */ 89333d66cfSSamuel Holland mmio_setbits_32(SUNXI_CPUCFG_CLS_CTRL_REG0(cluster), BIT(24 + core)); 90333d66cfSSamuel Holland /* Apply power to the CPU */ 91333d66cfSSamuel Holland sunxi_cpu_enable_power(cluster, core); 92333d66cfSSamuel Holland /* Release the core output clamps */ 93333d66cfSSamuel Holland mmio_clrbits_32(SUNXI_POWEROFF_GATING_REG(cluster), BIT(core)); 94333d66cfSSamuel Holland /* Deassert CPU power-on reset */ 95333d66cfSSamuel Holland mmio_setbits_32(SUNXI_POWERON_RST_REG(cluster), BIT(core)); 96333d66cfSSamuel Holland /* Deassert CPU core reset */ 97333d66cfSSamuel Holland mmio_setbits_32(SUNXI_CPUCFG_RST_CTRL_REG(cluster), BIT(core)); 98333d66cfSSamuel Holland /* Assert DBGPWRDUP */ 99333d66cfSSamuel Holland mmio_setbits_32(SUNXI_CPUCFG_DBG_REG0, BIT(core)); 100333d66cfSSamuel Holland } 101333d66cfSSamuel Holland 102333d66cfSSamuel Holland void sunxi_disable_secondary_cpus(unsigned int primary_cpu) 103333d66cfSSamuel Holland { 104333d66cfSSamuel Holland for (unsigned int cpu = 0; cpu < PLATFORM_CORE_COUNT; cpu += 1) { 105333d66cfSSamuel Holland if (cpu == primary_cpu) 106333d66cfSSamuel Holland continue; 107333d66cfSSamuel Holland sunxi_cpu_off(cpu / PLATFORM_MAX_CPUS_PER_CLUSTER, 108333d66cfSSamuel Holland cpu % PLATFORM_MAX_CPUS_PER_CLUSTER); 109333d66cfSSamuel Holland } 110333d66cfSSamuel Holland } 111