2 * SMP support for SoCs with APMU
4 * Copyright (C) 2014 Renesas Electronics Corporation
5 * Copyright (C) 2013 Magnus Damm
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/cpu_pm.h>
12 #include <linux/delay.h>
13 #include <linux/init.h>
15 #include <linux/ioport.h>
16 #include <linux/of_address.h>
17 #include <linux/smp.h>
18 #include <linux/suspend.h>
19 #include <linux/threads.h>
20 #include <asm/cacheflush.h>
22 #include <asm/proc-fns.h>
23 #include <asm/smp_plat.h>
24 #include <asm/suspend.h>
26 #include "platsmp-apmu.h"
33 #define WUPCR_OFFS 0x10
34 #define PSTR_OFFS 0x40
35 #define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
37 static int __maybe_unused
apmu_power_on(void __iomem
*p
, int bit
)
39 /* request power on */
40 writel_relaxed(BIT(bit
), p
+ WUPCR_OFFS
);
42 /* wait for APMU to finish */
43 while (readl_relaxed(p
+ WUPCR_OFFS
) != 0)
49 static int __maybe_unused
apmu_power_off(void __iomem
*p
, int bit
)
51 /* request Core Standby for next WFI */
52 writel_relaxed(3, p
+ CPUNCR_OFFS(bit
));
56 static int __maybe_unused
apmu_power_off_poll(void __iomem
*p
, int bit
)
60 for (k
= 0; k
< 1000; k
++) {
61 if (((readl_relaxed(p
+ PSTR_OFFS
) >> (bit
* 4)) & 0x03) == 3)
70 static int __maybe_unused
apmu_wrap(int cpu
, int (*fn
)(void __iomem
*p
, int cpu
))
72 void __iomem
*p
= apmu_cpus
[cpu
].iomem
;
74 return p
? fn(p
, apmu_cpus
[cpu
].bit
) : -EINVAL
;
77 static void apmu_init_cpu(struct resource
*res
, int cpu
, int bit
)
79 if ((cpu
>= ARRAY_SIZE(apmu_cpus
)) || apmu_cpus
[cpu
].iomem
)
82 apmu_cpus
[cpu
].iomem
= ioremap_nocache(res
->start
, resource_size(res
));
83 apmu_cpus
[cpu
].bit
= bit
;
85 pr_debug("apmu ioremap %d %d %pr\n", cpu
, bit
, res
);
88 static void apmu_parse_cfg(void (*fn
)(struct resource
*res
, int cpu
, int bit
),
89 struct rcar_apmu_config
*apmu_config
, int num
)
96 for (k
= 0; k
< num
; k
++) {
97 /* only enable the cluster that includes the boot CPU */
99 for (bit
= 0; bit
< ARRAY_SIZE(apmu_config
[k
].cpus
); bit
++) {
100 id
= apmu_config
[k
].cpus
[bit
];
102 if (id
== cpu_logical_map(0))
109 for (bit
= 0; bit
< ARRAY_SIZE(apmu_config
[k
].cpus
); bit
++) {
110 id
= apmu_config
[k
].cpus
[bit
];
112 index
= get_logical_index(id
);
114 fn(&apmu_config
[k
].iomem
, index
, bit
);
120 void __init
shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus
,
121 struct rcar_apmu_config
*apmu_config
,
124 /* install boot code shared by all CPUs */
125 shmobile_boot_fn
= virt_to_phys(shmobile_smp_boot
);
126 shmobile_boot_arg
= MPIDR_HWID_BITMASK
;
128 /* perform per-cpu setup */
129 apmu_parse_cfg(apmu_init_cpu
, apmu_config
, num
);
133 int shmobile_smp_apmu_boot_secondary(unsigned int cpu
, struct task_struct
*idle
)
135 /* For this particular CPU register boot vector */
136 shmobile_smp_hook(cpu
, virt_to_phys(secondary_startup
), 0);
138 return apmu_wrap(cpu
, apmu_power_on
);
142 #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
143 /* nicked from arch/arm/mach-exynos/hotplug.c */
144 static inline void cpu_enter_lowpower_a15(void)
149 " mrc p15, 0, %0, c1, c0, 0\n"
151 " mcr p15, 0, %0, c1, c0, 0\n"
162 " mrc p15, 0, %0, c1, c0, 1\n"
164 " mcr p15, 0, %0, c1, c0, 1\n"
173 void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu
)
176 /* Select next sleep mode using the APMU */
177 apmu_wrap(cpu
, apmu_power_off
);
179 /* Do ARM specific CPU shutdown */
180 cpu_enter_lowpower_a15();
183 static inline void cpu_leave_lowpower(void)
187 asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
189 " mcr p15, 0, %0, c1, c0, 0\n"
190 " mrc p15, 0, %0, c1, c0, 1\n"
192 " mcr p15, 0, %0, c1, c0, 1\n"
194 : "Ir" (CR_C
), "Ir" (0x40)
199 #if defined(CONFIG_HOTPLUG_CPU)
200 void shmobile_smp_apmu_cpu_die(unsigned int cpu
)
202 /* For this particular CPU deregister boot vector */
203 shmobile_smp_hook(cpu
, 0, 0);
205 /* Shutdown CPU core */
206 shmobile_smp_apmu_cpu_shutdown(cpu
);
208 /* jump to shared mach-shmobile sleep / reset code */
209 shmobile_smp_sleep();
212 int shmobile_smp_apmu_cpu_kill(unsigned int cpu
)
214 return apmu_wrap(cpu
, apmu_power_off_poll
);
218 #if defined(CONFIG_SUSPEND)
219 static int shmobile_smp_apmu_do_suspend(unsigned long cpu
)
221 shmobile_smp_hook(cpu
, virt_to_phys(cpu_resume
), 0);
222 shmobile_smp_apmu_cpu_shutdown(cpu
);
223 cpu_do_idle(); /* WFI selects Core Standby */
227 static int shmobile_smp_apmu_enter_suspend(suspend_state_t state
)
229 cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend
);
230 cpu_leave_lowpower();
234 void __init
shmobile_smp_apmu_suspend_init(void)
236 shmobile_suspend_ops
.enter
= shmobile_smp_apmu_enter_suspend
;