Commit | Line | Data |
---|---|---|
a112de8c MD |
1 | /* |
2 | * SMP support for SoCs with APMU | |
3 | * | |
a8d2ff39 | 4 | * Copyright (C) 2014 Renesas Electronics Corporation |
a112de8c MD |
5 | * Copyright (C) 2013 Magnus Damm |
6 | * | |
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. | |
10 | */ | |
d6d757c9 | 11 | #include <linux/cpu_pm.h> |
a112de8c MD |
12 | #include <linux/delay.h> |
13 | #include <linux/init.h> | |
14 | #include <linux/io.h> | |
15 | #include <linux/ioport.h> | |
16 | #include <linux/of_address.h> | |
17 | #include <linux/smp.h> | |
d6d757c9 | 18 | #include <linux/suspend.h> |
784500be | 19 | #include <linux/threads.h> |
a112de8c MD |
20 | #include <asm/cacheflush.h> |
21 | #include <asm/cp15.h> | |
d6d757c9 | 22 | #include <asm/proc-fns.h> |
a112de8c | 23 | #include <asm/smp_plat.h> |
d6d757c9 | 24 | #include <asm/suspend.h> |
fd44aa5e | 25 | #include "common.h" |
a8d2ff39 | 26 | #include "platsmp-apmu.h" |
a112de8c MD |
27 | |
28 | static struct { | |
29 | void __iomem *iomem; | |
30 | int bit; | |
784500be | 31 | } apmu_cpus[NR_CPUS]; |
a112de8c MD |
32 | |
33 | #define WUPCR_OFFS 0x10 | |
34 | #define PSTR_OFFS 0x40 | |
35 | #define CPUNCR_OFFS(n) (0x100 + (0x10 * (n))) | |
36 | ||
784500be | 37 | static int __maybe_unused apmu_power_on(void __iomem *p, int bit) |
a112de8c MD |
38 | { |
39 | /* request power on */ | |
40 | writel_relaxed(BIT(bit), p + WUPCR_OFFS); | |
41 | ||
42 | /* wait for APMU to finish */ | |
43 | while (readl_relaxed(p + WUPCR_OFFS) != 0) | |
44 | ; | |
45 | ||
46 | return 0; | |
47 | } | |
48 | ||
151dd346 | 49 | static int __maybe_unused apmu_power_off(void __iomem *p, int bit) |
a112de8c MD |
50 | { |
51 | /* request Core Standby for next WFI */ | |
52 | writel_relaxed(3, p + CPUNCR_OFFS(bit)); | |
53 | return 0; | |
54 | } | |
55 | ||
784500be | 56 | static int __maybe_unused apmu_power_off_poll(void __iomem *p, int bit) |
a112de8c MD |
57 | { |
58 | int k; | |
59 | ||
60 | for (k = 0; k < 1000; k++) { | |
61 | if (((readl_relaxed(p + PSTR_OFFS) >> (bit * 4)) & 0x03) == 3) | |
62 | return 1; | |
63 | ||
64 | mdelay(1); | |
65 | } | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
151dd346 | 70 | static int __maybe_unused apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu)) |
a112de8c MD |
71 | { |
72 | void __iomem *p = apmu_cpus[cpu].iomem; | |
73 | ||
74 | return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL; | |
75 | } | |
76 | ||
77 | static void apmu_init_cpu(struct resource *res, int cpu, int bit) | |
78 | { | |
784500be | 79 | if ((cpu >= ARRAY_SIZE(apmu_cpus)) || apmu_cpus[cpu].iomem) |
a112de8c MD |
80 | return; |
81 | ||
82 | apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res)); | |
83 | apmu_cpus[cpu].bit = bit; | |
84 | ||
56ff8731 | 85 | pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res); |
a112de8c MD |
86 | } |
87 | ||
a8d2ff39 HN |
88 | static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit), |
89 | struct rcar_apmu_config *apmu_config, int num) | |
a112de8c | 90 | { |
6e67d2cc | 91 | int id; |
a112de8c MD |
92 | int k; |
93 | int bit, index; | |
ee490bcc | 94 | bool is_allowed; |
a112de8c | 95 | |
a8d2ff39 | 96 | for (k = 0; k < num; k++) { |
ee490bcc MD |
97 | /* only enable the cluster that includes the boot CPU */ |
98 | is_allowed = false; | |
99 | for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) { | |
100 | id = apmu_config[k].cpus[bit]; | |
101 | if (id >= 0) { | |
102 | if (id == cpu_logical_map(0)) | |
103 | is_allowed = true; | |
104 | } | |
105 | } | |
106 | if (!is_allowed) | |
107 | continue; | |
108 | ||
a112de8c MD |
109 | for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) { |
110 | id = apmu_config[k].cpus[bit]; | |
111 | if (id >= 0) { | |
112 | index = get_logical_index(id); | |
113 | if (index >= 0) | |
114 | fn(&apmu_config[k].iomem, index, bit); | |
115 | } | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
a8d2ff39 HN |
120 | void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus, |
121 | struct rcar_apmu_config *apmu_config, | |
122 | int num) | |
a112de8c MD |
123 | { |
124 | /* install boot code shared by all CPUs */ | |
125 | shmobile_boot_fn = virt_to_phys(shmobile_smp_boot); | |
a112de8c MD |
126 | |
127 | /* perform per-cpu setup */ | |
a8d2ff39 | 128 | apmu_parse_cfg(apmu_init_cpu, apmu_config, num); |
a112de8c MD |
129 | } |
130 | ||
784500be | 131 | #ifdef CONFIG_SMP |
a112de8c MD |
132 | int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle) |
133 | { | |
134 | /* For this particular CPU register boot vector */ | |
02b4e275 | 135 | shmobile_smp_hook(cpu, virt_to_phys(secondary_startup), 0); |
a112de8c MD |
136 | |
137 | return apmu_wrap(cpu, apmu_power_on); | |
138 | } | |
784500be | 139 | #endif |
a112de8c | 140 | |
d6d757c9 | 141 | #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND) |
a112de8c MD |
142 | /* nicked from arch/arm/mach-exynos/hotplug.c */ |
143 | static inline void cpu_enter_lowpower_a15(void) | |
144 | { | |
145 | unsigned int v; | |
146 | ||
147 | asm volatile( | |
148 | " mrc p15, 0, %0, c1, c0, 0\n" | |
149 | " bic %0, %0, %1\n" | |
150 | " mcr p15, 0, %0, c1, c0, 0\n" | |
151 | : "=&r" (v) | |
152 | : "Ir" (CR_C) | |
153 | : "cc"); | |
154 | ||
155 | flush_cache_louis(); | |
156 | ||
157 | asm volatile( | |
158 | /* | |
159 | * Turn off coherency | |
160 | */ | |
161 | " mrc p15, 0, %0, c1, c0, 1\n" | |
162 | " bic %0, %0, %1\n" | |
163 | " mcr p15, 0, %0, c1, c0, 1\n" | |
164 | : "=&r" (v) | |
165 | : "Ir" (0x40) | |
166 | : "cc"); | |
167 | ||
168 | isb(); | |
169 | dsb(); | |
170 | } | |
171 | ||
54245073 | 172 | static void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu) |
a112de8c | 173 | { |
a112de8c MD |
174 | |
175 | /* Select next sleep mode using the APMU */ | |
176 | apmu_wrap(cpu, apmu_power_off); | |
177 | ||
178 | /* Do ARM specific CPU shutdown */ | |
179 | cpu_enter_lowpower_a15(); | |
d6d757c9 | 180 | } |
181 | ||
182 | static inline void cpu_leave_lowpower(void) | |
183 | { | |
184 | unsigned int v; | |
185 | ||
186 | asm volatile("mrc p15, 0, %0, c1, c0, 0\n" | |
187 | " orr %0, %0, %1\n" | |
188 | " mcr p15, 0, %0, c1, c0, 0\n" | |
189 | " mrc p15, 0, %0, c1, c0, 1\n" | |
190 | " orr %0, %0, %2\n" | |
191 | " mcr p15, 0, %0, c1, c0, 1\n" | |
192 | : "=&r" (v) | |
193 | : "Ir" (CR_C), "Ir" (0x40) | |
194 | : "cc"); | |
195 | } | |
196 | #endif | |
197 | ||
198 | #if defined(CONFIG_HOTPLUG_CPU) | |
199 | void shmobile_smp_apmu_cpu_die(unsigned int cpu) | |
200 | { | |
201 | /* For this particular CPU deregister boot vector */ | |
202 | shmobile_smp_hook(cpu, 0, 0); | |
203 | ||
204 | /* Shutdown CPU core */ | |
205 | shmobile_smp_apmu_cpu_shutdown(cpu); | |
a112de8c MD |
206 | |
207 | /* jump to shared mach-shmobile sleep / reset code */ | |
208 | shmobile_smp_sleep(); | |
209 | } | |
210 | ||
211 | int shmobile_smp_apmu_cpu_kill(unsigned int cpu) | |
212 | { | |
213 | return apmu_wrap(cpu, apmu_power_off_poll); | |
214 | } | |
215 | #endif | |
d6d757c9 | 216 | |
217 | #if defined(CONFIG_SUSPEND) | |
218 | static int shmobile_smp_apmu_do_suspend(unsigned long cpu) | |
219 | { | |
220 | shmobile_smp_hook(cpu, virt_to_phys(cpu_resume), 0); | |
221 | shmobile_smp_apmu_cpu_shutdown(cpu); | |
222 | cpu_do_idle(); /* WFI selects Core Standby */ | |
223 | return 1; | |
224 | } | |
225 | ||
226 | static int shmobile_smp_apmu_enter_suspend(suspend_state_t state) | |
227 | { | |
228 | cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend); | |
229 | cpu_leave_lowpower(); | |
230 | return 0; | |
231 | } | |
232 | ||
0d77c9aa | 233 | void __init shmobile_smp_apmu_suspend_init(void) |
d6d757c9 | 234 | { |
235 | shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend; | |
236 | } | |
d6d757c9 | 237 | #endif |