2 * SMP support for SoCs with APMU
4 * Copyright (C) 2013 Magnus Damm
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/cpu_pm.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/of_address.h>
16 #include <linux/smp.h>
17 #include <linux/suspend.h>
18 #include <linux/threads.h>
19 #include <asm/cacheflush.h>
21 #include <asm/proc-fns.h>
22 #include <asm/smp_plat.h>
23 #include <asm/suspend.h>
31 #define WUPCR_OFFS 0x10
32 #define PSTR_OFFS 0x40
33 #define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
35 static int __maybe_unused apmu_power_on(void __iomem *p, int bit)
37 /* request power on */
38 writel_relaxed(BIT(bit), p + WUPCR_OFFS);
40 /* wait for APMU to finish */
41 while (readl_relaxed(p + WUPCR_OFFS) != 0)
47 static int apmu_power_off(void __iomem *p, int bit)
49 /* request Core Standby for next WFI */
50 writel_relaxed(3, p + CPUNCR_OFFS(bit));
54 static int __maybe_unused apmu_power_off_poll(void __iomem *p, int bit)
58 for (k = 0; k < 1000; k++) {
59 if (((readl_relaxed(p + PSTR_OFFS) >> (bit * 4)) & 0x03) == 3)
68 static int apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu))
70 void __iomem *p = apmu_cpus[cpu].iomem;
72 return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL;
75 static void apmu_init_cpu(struct resource *res, int cpu, int bit)
77 if ((cpu >= ARRAY_SIZE(apmu_cpus)) || apmu_cpus[cpu].iomem)
80 apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res));
81 apmu_cpus[cpu].bit = bit;
83 pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res);
87 struct resource iomem;
91 .iomem = DEFINE_RES_MEM(0xe6152000, 0x88),
92 .cpus = { 0, 1, 2, 3 },
95 .iomem = DEFINE_RES_MEM(0xe6151000, 0x88),
96 .cpus = { 0x100, 0x101, 0x102, 0x103 },
100 static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit))
107 for (k = 0; k < ARRAY_SIZE(apmu_config); k++) {
108 /* only enable the cluster that includes the boot CPU */
110 for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
111 id = apmu_config[k].cpus[bit];
113 if (id == cpu_logical_map(0))
120 for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
121 id = apmu_config[k].cpus[bit];
123 index = get_logical_index(id);
125 fn(&apmu_config[k].iomem, index, bit);
131 void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus)
133 /* install boot code shared by all CPUs */
134 shmobile_boot_fn = virt_to_phys(shmobile_smp_boot);
135 shmobile_boot_arg = MPIDR_HWID_BITMASK;
137 /* perform per-cpu setup */
138 apmu_parse_cfg(apmu_init_cpu);
142 int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
144 /* For this particular CPU register boot vector */
145 shmobile_smp_hook(cpu, virt_to_phys(shmobile_invalidate_start), 0);
147 return apmu_wrap(cpu, apmu_power_on);
151 #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
152 /* nicked from arch/arm/mach-exynos/hotplug.c */
153 static inline void cpu_enter_lowpower_a15(void)
158 " mrc p15, 0, %0, c1, c0, 0\n"
160 " mcr p15, 0, %0, c1, c0, 0\n"
171 " mrc p15, 0, %0, c1, c0, 1\n"
173 " mcr p15, 0, %0, c1, c0, 1\n"
182 void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu)
185 /* Select next sleep mode using the APMU */
186 apmu_wrap(cpu, apmu_power_off);
188 /* Do ARM specific CPU shutdown */
189 cpu_enter_lowpower_a15();
192 static inline void cpu_leave_lowpower(void)
196 asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
198 " mcr p15, 0, %0, c1, c0, 0\n"
199 " mrc p15, 0, %0, c1, c0, 1\n"
201 " mcr p15, 0, %0, c1, c0, 1\n"
203 : "Ir" (CR_C), "Ir" (0x40)
208 #if defined(CONFIG_HOTPLUG_CPU)
209 void shmobile_smp_apmu_cpu_die(unsigned int cpu)
211 /* For this particular CPU deregister boot vector */
212 shmobile_smp_hook(cpu, 0, 0);
214 /* Shutdown CPU core */
215 shmobile_smp_apmu_cpu_shutdown(cpu);
217 /* jump to shared mach-shmobile sleep / reset code */
218 shmobile_smp_sleep();
221 int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
223 return apmu_wrap(cpu, apmu_power_off_poll);
227 #if defined(CONFIG_SUSPEND)
228 static int shmobile_smp_apmu_do_suspend(unsigned long cpu)
230 shmobile_smp_hook(cpu, virt_to_phys(cpu_resume), 0);
231 shmobile_smp_apmu_cpu_shutdown(cpu);
232 cpu_do_idle(); /* WFI selects Core Standby */
236 static int shmobile_smp_apmu_enter_suspend(suspend_state_t state)
238 cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend);
239 cpu_leave_lowpower();
243 void __init shmobile_smp_apmu_suspend_init(void)
245 shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend;