1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * arch/arm/mach-vexpress/tc2_pm.c - TC2 power management support
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Created by: Nicolas Pitre, October 2012
6*4882a593Smuzhiyun * Copyright: (C) 2012-2013 Linaro Limited
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Some portions of this file were originally written by Achin Gupta
9*4882a593Smuzhiyun * Copyright: (C) 2012 ARM Limited
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun #include <linux/io.h>
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/of_address.h>
17*4882a593Smuzhiyun #include <linux/of_irq.h>
18*4882a593Smuzhiyun #include <linux/errno.h>
19*4882a593Smuzhiyun #include <linux/irqchip/arm-gic.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <asm/mcpm.h>
22*4882a593Smuzhiyun #include <asm/proc-fns.h>
23*4882a593Smuzhiyun #include <asm/cacheflush.h>
24*4882a593Smuzhiyun #include <asm/cputype.h>
25*4882a593Smuzhiyun #include <asm/cp15.h>
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/arm-cci.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "spc.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* SCC conf registers */
32*4882a593Smuzhiyun #define RESET_CTRL 0x018
33*4882a593Smuzhiyun #define RESET_A15_NCORERESET(cpu) (1 << (2 + (cpu)))
34*4882a593Smuzhiyun #define RESET_A7_NCORERESET(cpu) (1 << (16 + (cpu)))
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #define A15_CONF 0x400
37*4882a593Smuzhiyun #define A7_CONF 0x500
38*4882a593Smuzhiyun #define SYS_INFO 0x700
39*4882a593Smuzhiyun #define SPC_BASE 0xb00
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun static void __iomem *scc;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #define TC2_CLUSTERS 2
44*4882a593Smuzhiyun #define TC2_MAX_CPUS_PER_CLUSTER 3
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun static unsigned int tc2_nr_cpus[TC2_CLUSTERS];
47*4882a593Smuzhiyun
tc2_pm_cpu_powerup(unsigned int cpu,unsigned int cluster)48*4882a593Smuzhiyun static int tc2_pm_cpu_powerup(unsigned int cpu, unsigned int cluster)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
51*4882a593Smuzhiyun if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster])
52*4882a593Smuzhiyun return -EINVAL;
53*4882a593Smuzhiyun ve_spc_set_resume_addr(cluster, cpu,
54*4882a593Smuzhiyun __pa_symbol(mcpm_entry_point));
55*4882a593Smuzhiyun ve_spc_cpu_wakeup_irq(cluster, cpu, true);
56*4882a593Smuzhiyun return 0;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
tc2_pm_cluster_powerup(unsigned int cluster)59*4882a593Smuzhiyun static int tc2_pm_cluster_powerup(unsigned int cluster)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun pr_debug("%s: cluster %u\n", __func__, cluster);
62*4882a593Smuzhiyun if (cluster >= TC2_CLUSTERS)
63*4882a593Smuzhiyun return -EINVAL;
64*4882a593Smuzhiyun ve_spc_powerdown(cluster, false);
65*4882a593Smuzhiyun return 0;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
tc2_pm_cpu_powerdown_prepare(unsigned int cpu,unsigned int cluster)68*4882a593Smuzhiyun static void tc2_pm_cpu_powerdown_prepare(unsigned int cpu, unsigned int cluster)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
71*4882a593Smuzhiyun BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
72*4882a593Smuzhiyun ve_spc_cpu_wakeup_irq(cluster, cpu, true);
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun * If the CPU is committed to power down, make sure
75*4882a593Smuzhiyun * the power controller will be in charge of waking it
76*4882a593Smuzhiyun * up upon IRQ, ie IRQ lines are cut from GIC CPU IF
77*4882a593Smuzhiyun * to the CPU by disabling the GIC CPU IF to prevent wfi
78*4882a593Smuzhiyun * from completing execution behind power controller back
79*4882a593Smuzhiyun */
80*4882a593Smuzhiyun gic_cpu_if_down(0);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
tc2_pm_cluster_powerdown_prepare(unsigned int cluster)83*4882a593Smuzhiyun static void tc2_pm_cluster_powerdown_prepare(unsigned int cluster)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun pr_debug("%s: cluster %u\n", __func__, cluster);
86*4882a593Smuzhiyun BUG_ON(cluster >= TC2_CLUSTERS);
87*4882a593Smuzhiyun ve_spc_powerdown(cluster, true);
88*4882a593Smuzhiyun ve_spc_global_wakeup_irq(true);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
tc2_pm_cpu_cache_disable(void)91*4882a593Smuzhiyun static void tc2_pm_cpu_cache_disable(void)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun v7_exit_coherency_flush(louis);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
tc2_pm_cluster_cache_disable(void)96*4882a593Smuzhiyun static void tc2_pm_cluster_cache_disable(void)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun * On the Cortex-A15 we need to disable
101*4882a593Smuzhiyun * L2 prefetching before flushing the cache.
102*4882a593Smuzhiyun */
103*4882a593Smuzhiyun asm volatile(
104*4882a593Smuzhiyun "mcr p15, 1, %0, c15, c0, 3 \n\t"
105*4882a593Smuzhiyun "isb \n\t"
106*4882a593Smuzhiyun "dsb "
107*4882a593Smuzhiyun : : "r" (0x400) );
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun v7_exit_coherency_flush(all);
111*4882a593Smuzhiyun cci_disable_port_by_cpu(read_cpuid_mpidr());
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
tc2_core_in_reset(unsigned int cpu,unsigned int cluster)114*4882a593Smuzhiyun static int tc2_core_in_reset(unsigned int cpu, unsigned int cluster)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun u32 mask = cluster ?
117*4882a593Smuzhiyun RESET_A7_NCORERESET(cpu)
118*4882a593Smuzhiyun : RESET_A15_NCORERESET(cpu);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun return !(readl_relaxed(scc + RESET_CTRL) & mask);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun #define POLL_MSEC 10
124*4882a593Smuzhiyun #define TIMEOUT_MSEC 1000
125*4882a593Smuzhiyun
tc2_pm_wait_for_powerdown(unsigned int cpu,unsigned int cluster)126*4882a593Smuzhiyun static int tc2_pm_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun unsigned tries;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
131*4882a593Smuzhiyun BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun for (tries = 0; tries < TIMEOUT_MSEC / POLL_MSEC; ++tries) {
134*4882a593Smuzhiyun pr_debug("%s(cpu=%u, cluster=%u): RESET_CTRL = 0x%08X\n",
135*4882a593Smuzhiyun __func__, cpu, cluster,
136*4882a593Smuzhiyun readl_relaxed(scc + RESET_CTRL));
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun * We need the CPU to reach WFI, but the power
140*4882a593Smuzhiyun * controller may put the cluster in reset and
141*4882a593Smuzhiyun * power it off as soon as that happens, before
142*4882a593Smuzhiyun * we have a chance to see STANDBYWFI.
143*4882a593Smuzhiyun *
144*4882a593Smuzhiyun * So we need to check for both conditions:
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun if (tc2_core_in_reset(cpu, cluster) ||
147*4882a593Smuzhiyun ve_spc_cpu_in_wfi(cpu, cluster))
148*4882a593Smuzhiyun return 0; /* success: the CPU is halted */
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* Otherwise, wait and retry: */
151*4882a593Smuzhiyun msleep(POLL_MSEC);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun return -ETIMEDOUT; /* timeout */
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
tc2_pm_cpu_suspend_prepare(unsigned int cpu,unsigned int cluster)157*4882a593Smuzhiyun static void tc2_pm_cpu_suspend_prepare(unsigned int cpu, unsigned int cluster)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun ve_spc_set_resume_addr(cluster, cpu, __pa_symbol(mcpm_entry_point));
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
tc2_pm_cpu_is_up(unsigned int cpu,unsigned int cluster)162*4882a593Smuzhiyun static void tc2_pm_cpu_is_up(unsigned int cpu, unsigned int cluster)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
165*4882a593Smuzhiyun BUG_ON(cluster >= TC2_CLUSTERS || cpu >= TC2_MAX_CPUS_PER_CLUSTER);
166*4882a593Smuzhiyun ve_spc_cpu_wakeup_irq(cluster, cpu, false);
167*4882a593Smuzhiyun ve_spc_set_resume_addr(cluster, cpu, 0);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
tc2_pm_cluster_is_up(unsigned int cluster)170*4882a593Smuzhiyun static void tc2_pm_cluster_is_up(unsigned int cluster)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun pr_debug("%s: cluster %u\n", __func__, cluster);
173*4882a593Smuzhiyun BUG_ON(cluster >= TC2_CLUSTERS);
174*4882a593Smuzhiyun ve_spc_powerdown(cluster, false);
175*4882a593Smuzhiyun ve_spc_global_wakeup_irq(false);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun static const struct mcpm_platform_ops tc2_pm_power_ops = {
179*4882a593Smuzhiyun .cpu_powerup = tc2_pm_cpu_powerup,
180*4882a593Smuzhiyun .cluster_powerup = tc2_pm_cluster_powerup,
181*4882a593Smuzhiyun .cpu_suspend_prepare = tc2_pm_cpu_suspend_prepare,
182*4882a593Smuzhiyun .cpu_powerdown_prepare = tc2_pm_cpu_powerdown_prepare,
183*4882a593Smuzhiyun .cluster_powerdown_prepare = tc2_pm_cluster_powerdown_prepare,
184*4882a593Smuzhiyun .cpu_cache_disable = tc2_pm_cpu_cache_disable,
185*4882a593Smuzhiyun .cluster_cache_disable = tc2_pm_cluster_cache_disable,
186*4882a593Smuzhiyun .wait_for_powerdown = tc2_pm_wait_for_powerdown,
187*4882a593Smuzhiyun .cpu_is_up = tc2_pm_cpu_is_up,
188*4882a593Smuzhiyun .cluster_is_up = tc2_pm_cluster_is_up,
189*4882a593Smuzhiyun };
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun * Enable cluster-level coherency, in preparation for turning on the MMU.
193*4882a593Smuzhiyun */
tc2_pm_power_up_setup(unsigned int affinity_level)194*4882a593Smuzhiyun static void __naked tc2_pm_power_up_setup(unsigned int affinity_level)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun asm volatile (" \n"
197*4882a593Smuzhiyun " cmp r0, #1 \n"
198*4882a593Smuzhiyun " bxne lr \n"
199*4882a593Smuzhiyun " b cci_enable_port_for_self ");
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
tc2_pm_init(void)202*4882a593Smuzhiyun static int __init tc2_pm_init(void)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun unsigned int mpidr, cpu, cluster;
205*4882a593Smuzhiyun int ret, irq;
206*4882a593Smuzhiyun u32 a15_cluster_id, a7_cluster_id, sys_info;
207*4882a593Smuzhiyun struct device_node *np;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /*
210*4882a593Smuzhiyun * The power management-related features are hidden behind
211*4882a593Smuzhiyun * SCC registers. We need to extract runtime information like
212*4882a593Smuzhiyun * cluster ids and number of CPUs really available in clusters.
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun np = of_find_compatible_node(NULL, NULL,
215*4882a593Smuzhiyun "arm,vexpress-scc,v2p-ca15_a7");
216*4882a593Smuzhiyun scc = of_iomap(np, 0);
217*4882a593Smuzhiyun if (!scc)
218*4882a593Smuzhiyun return -ENODEV;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun a15_cluster_id = readl_relaxed(scc + A15_CONF) & 0xf;
221*4882a593Smuzhiyun a7_cluster_id = readl_relaxed(scc + A7_CONF) & 0xf;
222*4882a593Smuzhiyun if (a15_cluster_id >= TC2_CLUSTERS || a7_cluster_id >= TC2_CLUSTERS)
223*4882a593Smuzhiyun return -EINVAL;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun sys_info = readl_relaxed(scc + SYS_INFO);
226*4882a593Smuzhiyun tc2_nr_cpus[a15_cluster_id] = (sys_info >> 16) & 0xf;
227*4882a593Smuzhiyun tc2_nr_cpus[a7_cluster_id] = (sys_info >> 20) & 0xf;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun irq = irq_of_parse_and_map(np, 0);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /*
232*4882a593Smuzhiyun * A subset of the SCC registers is also used to communicate
233*4882a593Smuzhiyun * with the SPC (power controller). We need to be able to
234*4882a593Smuzhiyun * drive it very early in the boot process to power up
235*4882a593Smuzhiyun * processors, so we initialize the SPC driver here.
236*4882a593Smuzhiyun */
237*4882a593Smuzhiyun ret = ve_spc_init(scc + SPC_BASE, a15_cluster_id, irq);
238*4882a593Smuzhiyun if (ret)
239*4882a593Smuzhiyun return ret;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun if (!cci_probed())
242*4882a593Smuzhiyun return -ENODEV;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun mpidr = read_cpuid_mpidr();
245*4882a593Smuzhiyun cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
246*4882a593Smuzhiyun cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
247*4882a593Smuzhiyun pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
248*4882a593Smuzhiyun if (cluster >= TC2_CLUSTERS || cpu >= tc2_nr_cpus[cluster]) {
249*4882a593Smuzhiyun pr_err("%s: boot CPU is out of bound!\n", __func__);
250*4882a593Smuzhiyun return -EINVAL;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun ret = mcpm_platform_register(&tc2_pm_power_ops);
254*4882a593Smuzhiyun if (!ret) {
255*4882a593Smuzhiyun mcpm_sync_init(tc2_pm_power_up_setup);
256*4882a593Smuzhiyun /* test if we can (re)enable the CCI on our own */
257*4882a593Smuzhiyun BUG_ON(mcpm_loopback(tc2_pm_cluster_cache_disable) != 0);
258*4882a593Smuzhiyun pr_info("TC2 power management initialized\n");
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun return ret;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun early_initcall(tc2_pm_init);
264