162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Created by: Nicolas Pitre, October 2012 462306a36Sopenharmony_ci * Copyright: (C) 2012-2013 Linaro Limited 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Some portions of this file were originally written by Achin Gupta 762306a36Sopenharmony_ci * Copyright: (C) 2012 ARM Limited 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <linux/delay.h> 1162306a36Sopenharmony_ci#include <linux/init.h> 1262306a36Sopenharmony_ci#include <linux/io.h> 1362306a36Sopenharmony_ci#include <linux/kernel.h> 1462306a36Sopenharmony_ci#include <linux/of_address.h> 1562306a36Sopenharmony_ci#include <linux/of_irq.h> 1662306a36Sopenharmony_ci#include <linux/errno.h> 1762306a36Sopenharmony_ci#include <linux/irqchip/arm-gic.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include <asm/mcpm.h> 2062306a36Sopenharmony_ci#include <asm/proc-fns.h> 2162306a36Sopenharmony_ci#include <asm/cacheflush.h> 2262306a36Sopenharmony_ci#include <asm/cputype.h> 2362306a36Sopenharmony_ci#include <asm/cp15.h> 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#include <linux/arm-cci.h> 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#include "spc.h" 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci/* SCC conf registers */ 3062306a36Sopenharmony_ci#define RESET_CTRL 0x018 3162306a36Sopenharmony_ci#define RESET_A15_NCORERESET(cpu) (1 << (2 + (cpu))) 3262306a36Sopenharmony_ci#define RESET_A7_NCORERESET(cpu) (1 << (16 + (cpu))) 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#define A15_CONF 0x400 3562306a36Sopenharmony_ci#define A7_CONF 0x500 3662306a36Sopenharmony_ci#define SYS_INFO 0x700 3762306a36Sopenharmony_ci#define SPC_BASE 0xb00 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_cistatic void __iomem *scc; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci#define TC2_CLUSTERS 2 4262306a36Sopenharmony_ci#define TC2_MAX_CPUS_PER_CLUSTER 3 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_cistatic unsigned int tc2_nr_cpus[TC2_CLUSTERS]; 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistatic int tc2_pm_cpu_powerup(unsigned int cpu, unsigned int cluster) 4762306a36Sopenharmony_ci{ 4862306a36Sopenharmony_ci pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster); 4962306a36Sopenharmony_ci if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster]) 5062306a36Sopenharmony_ci return -EINVAL; 5162306a36Sopenharmony_ci ve_spc_set_resume_addr(cluster, cpu, 5262306a36Sopenharmony_ci __pa_symbol(mcpm_entry_point)); 5362306a36Sopenharmony_ci ve_spc_cpu_wakeup_irq(cluster, cpu, true); 5462306a36Sopenharmony_ci return 0; 5562306a36Sopenharmony_ci} 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cistatic int tc2_pm_cluster_powerup(unsigned int cluster) 5862306a36Sopenharmony_ci{ 5962306a36Sopenharmony_ci pr_debug("%s: cluster %u\n", __func__, cluster); 6062306a36Sopenharmony_ci if (cluster >= TC2_CLUSTERS) 6162306a36Sopenharmony_ci return -EINVAL; 6262306a36Sopenharmony_ci ve_spc_powerdown(cluster, false); 6362306a36Sopenharmony_ci return 0; 6462306a36Sopenharmony_ci} 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistatic void tc2_pm_cpu_powerdown_prepare(unsigned int cpu, unsigned int cluster) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster); 6962306a36Sopenharmony_ci BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER); 7062306a36Sopenharmony_ci ve_spc_cpu_wakeup_irq(cluster, cpu, true); 7162306a36Sopenharmony_ci /* 7262306a36Sopenharmony_ci * If the CPU is committed to power down, make sure 7362306a36Sopenharmony_ci * the power controller will be in charge of waking it 7462306a36Sopenharmony_ci * up upon IRQ, ie IRQ lines are cut from GIC CPU IF 7562306a36Sopenharmony_ci * to the CPU by disabling the GIC CPU IF to prevent wfi 7662306a36Sopenharmony_ci * from completing execution behind power controller back 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_ci gic_cpu_if_down(0); 7962306a36Sopenharmony_ci} 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_cistatic void tc2_pm_cluster_powerdown_prepare(unsigned int cluster) 8262306a36Sopenharmony_ci{ 8362306a36Sopenharmony_ci pr_debug("%s: cluster %u\n", __func__, cluster); 8462306a36Sopenharmony_ci BUG_ON(cluster >= TC2_CLUSTERS); 8562306a36Sopenharmony_ci ve_spc_powerdown(cluster, true); 8662306a36Sopenharmony_ci ve_spc_global_wakeup_irq(true); 8762306a36Sopenharmony_ci} 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_cistatic void tc2_pm_cpu_cache_disable(void) 9062306a36Sopenharmony_ci{ 9162306a36Sopenharmony_ci v7_exit_coherency_flush(louis); 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_cistatic void tc2_pm_cluster_cache_disable(void) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) { 9762306a36Sopenharmony_ci /* 9862306a36Sopenharmony_ci * On the Cortex-A15 we need to disable 9962306a36Sopenharmony_ci * L2 prefetching before flushing the cache. 10062306a36Sopenharmony_ci */ 10162306a36Sopenharmony_ci asm volatile( 10262306a36Sopenharmony_ci "mcr p15, 1, %0, c15, c0, 3 \n\t" 10362306a36Sopenharmony_ci "isb \n\t" 10462306a36Sopenharmony_ci "dsb " 10562306a36Sopenharmony_ci : : "r" (0x400) ); 10662306a36Sopenharmony_ci } 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci v7_exit_coherency_flush(all); 10962306a36Sopenharmony_ci cci_disable_port_by_cpu(read_cpuid_mpidr()); 11062306a36Sopenharmony_ci} 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_cistatic int tc2_core_in_reset(unsigned int cpu, unsigned int cluster) 11362306a36Sopenharmony_ci{ 11462306a36Sopenharmony_ci u32 mask = cluster ? 11562306a36Sopenharmony_ci RESET_A7_NCORERESET(cpu) 11662306a36Sopenharmony_ci : RESET_A15_NCORERESET(cpu); 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci return !(readl_relaxed(scc + RESET_CTRL) & mask); 11962306a36Sopenharmony_ci} 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci#define POLL_MSEC 10 12262306a36Sopenharmony_ci#define TIMEOUT_MSEC 1000 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_cistatic int tc2_pm_wait_for_powerdown(unsigned int cpu, unsigned int cluster) 12562306a36Sopenharmony_ci{ 12662306a36Sopenharmony_ci unsigned tries; 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster); 12962306a36Sopenharmony_ci BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci for (tries = 0; tries < TIMEOUT_MSEC / POLL_MSEC; ++tries) { 13262306a36Sopenharmony_ci pr_debug("%s(cpu=%u, cluster=%u): RESET_CTRL = 0x%08X\n", 13362306a36Sopenharmony_ci __func__, cpu, cluster, 13462306a36Sopenharmony_ci readl_relaxed(scc + RESET_CTRL)); 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci /* 13762306a36Sopenharmony_ci * We need the CPU to reach WFI, but the power 13862306a36Sopenharmony_ci * controller may put the cluster in reset and 13962306a36Sopenharmony_ci * power it off as soon as that happens, before 14062306a36Sopenharmony_ci * we have a chance to see STANDBYWFI. 14162306a36Sopenharmony_ci * 14262306a36Sopenharmony_ci * So we need to check for both conditions: 14362306a36Sopenharmony_ci */ 14462306a36Sopenharmony_ci if (tc2_core_in_reset(cpu, cluster) || 14562306a36Sopenharmony_ci ve_spc_cpu_in_wfi(cpu, cluster)) 14662306a36Sopenharmony_ci return 0; /* success: the CPU is halted */ 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci /* Otherwise, wait and retry: */ 14962306a36Sopenharmony_ci msleep(POLL_MSEC); 15062306a36Sopenharmony_ci } 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ci return -ETIMEDOUT; /* timeout */ 15362306a36Sopenharmony_ci} 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_cistatic void tc2_pm_cpu_suspend_prepare(unsigned int cpu, unsigned int cluster) 15662306a36Sopenharmony_ci{ 15762306a36Sopenharmony_ci ve_spc_set_resume_addr(cluster, cpu, __pa_symbol(mcpm_entry_point)); 15862306a36Sopenharmony_ci} 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_cistatic void tc2_pm_cpu_is_up(unsigned int cpu, unsigned int cluster) 16162306a36Sopenharmony_ci{ 16262306a36Sopenharmony_ci pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster); 16362306a36Sopenharmony_ci BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER); 16462306a36Sopenharmony_ci ve_spc_cpu_wakeup_irq(cluster, cpu, false); 16562306a36Sopenharmony_ci ve_spc_set_resume_addr(cluster, cpu, 0); 16662306a36Sopenharmony_ci} 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_cistatic void tc2_pm_cluster_is_up(unsigned int cluster) 16962306a36Sopenharmony_ci{ 17062306a36Sopenharmony_ci pr_debug("%s: cluster %u\n", __func__, cluster); 17162306a36Sopenharmony_ci BUG_ON(cluster >= TC2_CLUSTERS); 17262306a36Sopenharmony_ci ve_spc_powerdown(cluster, false); 17362306a36Sopenharmony_ci ve_spc_global_wakeup_irq(false); 17462306a36Sopenharmony_ci} 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_cistatic const struct mcpm_platform_ops tc2_pm_power_ops = { 17762306a36Sopenharmony_ci .cpu_powerup = tc2_pm_cpu_powerup, 17862306a36Sopenharmony_ci .cluster_powerup = tc2_pm_cluster_powerup, 17962306a36Sopenharmony_ci .cpu_suspend_prepare = tc2_pm_cpu_suspend_prepare, 18062306a36Sopenharmony_ci .cpu_powerdown_prepare = tc2_pm_cpu_powerdown_prepare, 18162306a36Sopenharmony_ci .cluster_powerdown_prepare = tc2_pm_cluster_powerdown_prepare, 18262306a36Sopenharmony_ci .cpu_cache_disable = tc2_pm_cpu_cache_disable, 18362306a36Sopenharmony_ci .cluster_cache_disable = tc2_pm_cluster_cache_disable, 18462306a36Sopenharmony_ci .wait_for_powerdown = tc2_pm_wait_for_powerdown, 18562306a36Sopenharmony_ci .cpu_is_up = tc2_pm_cpu_is_up, 18662306a36Sopenharmony_ci .cluster_is_up = tc2_pm_cluster_is_up, 18762306a36Sopenharmony_ci}; 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci/* 19062306a36Sopenharmony_ci * Enable cluster-level coherency, in preparation for turning on the MMU. 19162306a36Sopenharmony_ci */ 19262306a36Sopenharmony_cistatic void __naked tc2_pm_power_up_setup(unsigned int affinity_level) 19362306a36Sopenharmony_ci{ 19462306a36Sopenharmony_ci asm volatile (" \n" 19562306a36Sopenharmony_ci" cmp r0, #1 \n" 19662306a36Sopenharmony_ci" bxne lr \n" 19762306a36Sopenharmony_ci" b cci_enable_port_for_self "); 19862306a36Sopenharmony_ci} 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_cistatic int __init tc2_pm_init(void) 20162306a36Sopenharmony_ci{ 20262306a36Sopenharmony_ci unsigned int mpidr, cpu, cluster; 20362306a36Sopenharmony_ci int ret, irq; 20462306a36Sopenharmony_ci u32 a15_cluster_id, a7_cluster_id, sys_info; 20562306a36Sopenharmony_ci struct device_node *np; 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci /* 20862306a36Sopenharmony_ci * The power management-related features are hidden behind 20962306a36Sopenharmony_ci * SCC registers. We need to extract runtime information like 21062306a36Sopenharmony_ci * cluster ids and number of CPUs really available in clusters. 21162306a36Sopenharmony_ci */ 21262306a36Sopenharmony_ci np = of_find_compatible_node(NULL, NULL, 21362306a36Sopenharmony_ci "arm,vexpress-scc,v2p-ca15_a7"); 21462306a36Sopenharmony_ci scc = of_iomap(np, 0); 21562306a36Sopenharmony_ci if (!scc) 21662306a36Sopenharmony_ci return -ENODEV; 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci a15_cluster_id = readl_relaxed(scc + A15_CONF) & 0xf; 21962306a36Sopenharmony_ci a7_cluster_id = readl_relaxed(scc + A7_CONF) & 0xf; 22062306a36Sopenharmony_ci if (a15_cluster_id >= TC2_CLUSTERS || a7_cluster_id >= TC2_CLUSTERS) 22162306a36Sopenharmony_ci return -EINVAL; 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci sys_info = readl_relaxed(scc + SYS_INFO); 22462306a36Sopenharmony_ci tc2_nr_cpus[a15_cluster_id] = (sys_info >> 16) & 0xf; 22562306a36Sopenharmony_ci tc2_nr_cpus[a7_cluster_id] = (sys_info >> 20) & 0xf; 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ci irq = irq_of_parse_and_map(np, 0); 22862306a36Sopenharmony_ci 22962306a36Sopenharmony_ci /* 23062306a36Sopenharmony_ci * A subset of the SCC registers is also used to communicate 23162306a36Sopenharmony_ci * with the SPC (power controller). We need to be able to 23262306a36Sopenharmony_ci * drive it very early in the boot process to power up 23362306a36Sopenharmony_ci * processors, so we initialize the SPC driver here. 23462306a36Sopenharmony_ci */ 23562306a36Sopenharmony_ci ret = ve_spc_init(scc + SPC_BASE, a15_cluster_id, irq); 23662306a36Sopenharmony_ci if (ret) 23762306a36Sopenharmony_ci return ret; 23862306a36Sopenharmony_ci 23962306a36Sopenharmony_ci if (!cci_probed()) 24062306a36Sopenharmony_ci return -ENODEV; 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_ci mpidr = read_cpuid_mpidr(); 24362306a36Sopenharmony_ci cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0); 24462306a36Sopenharmony_ci cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1); 24562306a36Sopenharmony_ci pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster); 24662306a36Sopenharmony_ci if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster]) { 24762306a36Sopenharmony_ci pr_err("%s: boot CPU is out of bound!\n", __func__); 24862306a36Sopenharmony_ci return -EINVAL; 24962306a36Sopenharmony_ci } 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci ret = mcpm_platform_register(&tc2_pm_power_ops); 25262306a36Sopenharmony_ci if (!ret) { 25362306a36Sopenharmony_ci mcpm_sync_init(tc2_pm_power_up_setup); 25462306a36Sopenharmony_ci /* test if we can (re)enable the CCI on our own */ 25562306a36Sopenharmony_ci BUG_ON(mcpm_loopback(tc2_pm_cluster_cache_disable) != 0); 25662306a36Sopenharmony_ci pr_info("TC2 power management initialized\n"); 25762306a36Sopenharmony_ci } 25862306a36Sopenharmony_ci return ret; 25962306a36Sopenharmony_ci} 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ciearly_initcall(tc2_pm_init); 262