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/delay.h>
11 #include <linux/init.h>
13 #include <linux/ioport.h>
14 #include <linux/of_address.h>
15 #include <linux/smp.h>
16 #include <asm/cacheflush.h>
18 #include <asm/smp_plat.h>
19 #include <mach/common.h>
24 } apmu_cpus[CONFIG_NR_CPUS];
26 #define WUPCR_OFFS 0x10
27 #define PSTR_OFFS 0x40
28 #define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
30 static int apmu_power_on(void __iomem *p, int bit)
32 /* request power on */
33 writel_relaxed(BIT(bit), p + WUPCR_OFFS);
35 /* wait for APMU to finish */
36 while (readl_relaxed(p + WUPCR_OFFS) != 0)
42 static int apmu_power_off(void __iomem *p, int bit)
44 /* request Core Standby for next WFI */
45 writel_relaxed(3, p + CPUNCR_OFFS(bit));
49 static int apmu_power_off_poll(void __iomem *p, int bit)
53 for (k = 0; k < 1000; k++) {
54 if (((readl_relaxed(p + PSTR_OFFS) >> (bit * 4)) & 0x03) == 3)
63 static int apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu))
65 void __iomem *p = apmu_cpus[cpu].iomem;
67 return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL;
70 static void apmu_init_cpu(struct resource *res, int cpu, int bit)
72 if (apmu_cpus[cpu].iomem)
75 apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res));
76 apmu_cpus[cpu].bit = bit;
78 pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res);
82 struct resource iomem;
86 .iomem = DEFINE_RES_MEM(0xe6152000, 0x88),
87 .cpus = { 0, 1, 2, 3 },
90 .iomem = DEFINE_RES_MEM(0xe6151000, 0x88),
91 .cpus = { 0x100, 0x101, 0x102, 0x103 },
95 static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit))
102 for (k = 0; k < ARRAY_SIZE(apmu_config); k++) {
103 /* only enable the cluster that includes the boot CPU */
105 for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
106 id = apmu_config[k].cpus[bit];
108 if (id == cpu_logical_map(0))
115 for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
116 id = apmu_config[k].cpus[bit];
118 index = get_logical_index(id);
120 fn(&apmu_config[k].iomem, index, bit);
126 void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus)
128 /* install boot code shared by all CPUs */
129 shmobile_boot_fn = virt_to_phys(shmobile_smp_boot);
130 shmobile_boot_arg = MPIDR_HWID_BITMASK;
132 /* perform per-cpu setup */
133 apmu_parse_cfg(apmu_init_cpu);
136 int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
138 /* For this particular CPU register boot vector */
139 shmobile_smp_hook(cpu, virt_to_phys(shmobile_invalidate_start), 0);
141 return apmu_wrap(cpu, apmu_power_on);
144 #ifdef CONFIG_HOTPLUG_CPU
145 /* nicked from arch/arm/mach-exynos/hotplug.c */
146 static inline void cpu_enter_lowpower_a15(void)
151 " mrc p15, 0, %0, c1, c0, 0\n"
153 " mcr p15, 0, %0, c1, c0, 0\n"
164 " mrc p15, 0, %0, c1, c0, 1\n"
166 " mcr p15, 0, %0, c1, c0, 1\n"
175 void shmobile_smp_apmu_cpu_die(unsigned int cpu)
177 /* For this particular CPU deregister boot vector */
178 shmobile_smp_hook(cpu, 0, 0);
180 /* Select next sleep mode using the APMU */
181 apmu_wrap(cpu, apmu_power_off);
183 /* Do ARM specific CPU shutdown */
184 cpu_enter_lowpower_a15();
186 /* jump to shared mach-shmobile sleep / reset code */
187 shmobile_smp_sleep();
190 int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
192 return apmu_wrap(cpu, apmu_power_off_poll);