blob: 2c06810d3a70e5af46dc8adc06dea0d66d19114f [file] [log] [blame]
Magnus Damma112de82013-08-29 08:21:58 +09001/*
2 * SMP support for SoCs with APMU
3 *
4 * Copyright (C) 2013 Magnus Damm
5 *
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.
9 */
keita kobayashid6d757c2014-05-29 16:24:27 +090010#include <linux/cpu_pm.h>
Magnus Damma112de82013-08-29 08:21:58 +090011#include <linux/delay.h>
12#include <linux/init.h>
13#include <linux/io.h>
14#include <linux/ioport.h>
15#include <linux/of_address.h>
16#include <linux/smp.h>
keita kobayashid6d757c2014-05-29 16:24:27 +090017#include <linux/suspend.h>
Magnus Damm784500b2014-06-06 16:20:18 +090018#include <linux/threads.h>
Magnus Damma112de82013-08-29 08:21:58 +090019#include <asm/cacheflush.h>
20#include <asm/cp15.h>
keita kobayashid6d757c2014-05-29 16:24:27 +090021#include <asm/proc-fns.h>
Magnus Damma112de82013-08-29 08:21:58 +090022#include <asm/smp_plat.h>
keita kobayashid6d757c2014-05-29 16:24:27 +090023#include <asm/suspend.h>
Magnus Dammfd44aa52014-06-17 16:47:37 +090024#include "common.h"
Magnus Damma112de82013-08-29 08:21:58 +090025
26static struct {
27 void __iomem *iomem;
28 int bit;
Magnus Damm784500b2014-06-06 16:20:18 +090029} apmu_cpus[NR_CPUS];
Magnus Damma112de82013-08-29 08:21:58 +090030
31#define WUPCR_OFFS 0x10
32#define PSTR_OFFS 0x40
33#define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
34
Magnus Damm784500b2014-06-06 16:20:18 +090035static int __maybe_unused apmu_power_on(void __iomem *p, int bit)
Magnus Damma112de82013-08-29 08:21:58 +090036{
37 /* request power on */
38 writel_relaxed(BIT(bit), p + WUPCR_OFFS);
39
40 /* wait for APMU to finish */
41 while (readl_relaxed(p + WUPCR_OFFS) != 0)
42 ;
43
44 return 0;
45}
46
47static int apmu_power_off(void __iomem *p, int bit)
48{
49 /* request Core Standby for next WFI */
50 writel_relaxed(3, p + CPUNCR_OFFS(bit));
51 return 0;
52}
53
Magnus Damm784500b2014-06-06 16:20:18 +090054static int __maybe_unused apmu_power_off_poll(void __iomem *p, int bit)
Magnus Damma112de82013-08-29 08:21:58 +090055{
56 int k;
57
58 for (k = 0; k < 1000; k++) {
59 if (((readl_relaxed(p + PSTR_OFFS) >> (bit * 4)) & 0x03) == 3)
60 return 1;
61
62 mdelay(1);
63 }
64
65 return 0;
66}
67
68static int apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu))
69{
70 void __iomem *p = apmu_cpus[cpu].iomem;
71
72 return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL;
73}
74
75static void apmu_init_cpu(struct resource *res, int cpu, int bit)
76{
Magnus Damm784500b2014-06-06 16:20:18 +090077 if ((cpu >= ARRAY_SIZE(apmu_cpus)) || apmu_cpus[cpu].iomem)
Magnus Damma112de82013-08-29 08:21:58 +090078 return;
79
80 apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res));
81 apmu_cpus[cpu].bit = bit;
82
Laurent Pinchart56ff8732014-03-04 19:11:15 +010083 pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res);
Magnus Damma112de82013-08-29 08:21:58 +090084}
85
86static struct {
87 struct resource iomem;
88 int cpus[4];
89} apmu_config[] = {
90 {
91 .iomem = DEFINE_RES_MEM(0xe6152000, 0x88),
92 .cpus = { 0, 1, 2, 3 },
Magnus Damm43651b12013-09-15 00:29:16 +090093 },
94 {
95 .iomem = DEFINE_RES_MEM(0xe6151000, 0x88),
96 .cpus = { 0x100, 0x101, 0x102, 0x103 },
Magnus Damma112de82013-08-29 08:21:58 +090097 }
98};
99
100static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit))
101{
102 u32 id;
103 int k;
104 int bit, index;
Magnus Dammee490bc2013-09-15 00:29:07 +0900105 bool is_allowed;
Magnus Damma112de82013-08-29 08:21:58 +0900106
107 for (k = 0; k < ARRAY_SIZE(apmu_config); k++) {
Magnus Dammee490bc2013-09-15 00:29:07 +0900108 /* only enable the cluster that includes the boot CPU */
109 is_allowed = false;
110 for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
111 id = apmu_config[k].cpus[bit];
112 if (id >= 0) {
113 if (id == cpu_logical_map(0))
114 is_allowed = true;
115 }
116 }
117 if (!is_allowed)
118 continue;
119
Magnus Damma112de82013-08-29 08:21:58 +0900120 for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
121 id = apmu_config[k].cpus[bit];
122 if (id >= 0) {
123 index = get_logical_index(id);
124 if (index >= 0)
125 fn(&apmu_config[k].iomem, index, bit);
126 }
127 }
128 }
129}
130
131void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus)
132{
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;
136
137 /* perform per-cpu setup */
138 apmu_parse_cfg(apmu_init_cpu);
139}
140
Magnus Damm784500b2014-06-06 16:20:18 +0900141#ifdef CONFIG_SMP
Magnus Damma112de82013-08-29 08:21:58 +0900142int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
143{
144 /* For this particular CPU register boot vector */
145 shmobile_smp_hook(cpu, virt_to_phys(shmobile_invalidate_start), 0);
146
147 return apmu_wrap(cpu, apmu_power_on);
148}
Magnus Damm784500b2014-06-06 16:20:18 +0900149#endif
Magnus Damma112de82013-08-29 08:21:58 +0900150
keita kobayashid6d757c2014-05-29 16:24:27 +0900151#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
Magnus Damma112de82013-08-29 08:21:58 +0900152/* nicked from arch/arm/mach-exynos/hotplug.c */
153static inline void cpu_enter_lowpower_a15(void)
154{
155 unsigned int v;
156
157 asm volatile(
158 " mrc p15, 0, %0, c1, c0, 0\n"
159 " bic %0, %0, %1\n"
160 " mcr p15, 0, %0, c1, c0, 0\n"
161 : "=&r" (v)
162 : "Ir" (CR_C)
163 : "cc");
164
165 flush_cache_louis();
166
167 asm volatile(
168 /*
169 * Turn off coherency
170 */
171 " mrc p15, 0, %0, c1, c0, 1\n"
172 " bic %0, %0, %1\n"
173 " mcr p15, 0, %0, c1, c0, 1\n"
174 : "=&r" (v)
175 : "Ir" (0x40)
176 : "cc");
177
178 isb();
179 dsb();
180}
181
keita kobayashid6d757c2014-05-29 16:24:27 +0900182void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu)
Magnus Damma112de82013-08-29 08:21:58 +0900183{
Magnus Damma112de82013-08-29 08:21:58 +0900184
185 /* Select next sleep mode using the APMU */
186 apmu_wrap(cpu, apmu_power_off);
187
188 /* Do ARM specific CPU shutdown */
189 cpu_enter_lowpower_a15();
keita kobayashid6d757c2014-05-29 16:24:27 +0900190}
191
192static inline void cpu_leave_lowpower(void)
193{
194 unsigned int v;
195
196 asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
197 " orr %0, %0, %1\n"
198 " mcr p15, 0, %0, c1, c0, 0\n"
199 " mrc p15, 0, %0, c1, c0, 1\n"
200 " orr %0, %0, %2\n"
201 " mcr p15, 0, %0, c1, c0, 1\n"
202 : "=&r" (v)
203 : "Ir" (CR_C), "Ir" (0x40)
204 : "cc");
205}
206#endif
207
208#if defined(CONFIG_HOTPLUG_CPU)
209void shmobile_smp_apmu_cpu_die(unsigned int cpu)
210{
211 /* For this particular CPU deregister boot vector */
212 shmobile_smp_hook(cpu, 0, 0);
213
214 /* Shutdown CPU core */
215 shmobile_smp_apmu_cpu_shutdown(cpu);
Magnus Damma112de82013-08-29 08:21:58 +0900216
217 /* jump to shared mach-shmobile sleep / reset code */
218 shmobile_smp_sleep();
219}
220
221int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
222{
223 return apmu_wrap(cpu, apmu_power_off_poll);
224}
225#endif
keita kobayashid6d757c2014-05-29 16:24:27 +0900226
227#if defined(CONFIG_SUSPEND)
228static int shmobile_smp_apmu_do_suspend(unsigned long cpu)
229{
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 */
233 return 1;
234}
235
236static int shmobile_smp_apmu_enter_suspend(suspend_state_t state)
237{
238 cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend);
239 cpu_leave_lowpower();
240 return 0;
241}
242
Magnus Damm0d77c9a2014-06-06 16:20:46 +0900243void __init shmobile_smp_apmu_suspend_init(void)
keita kobayashid6d757c2014-05-29 16:24:27 +0900244{
245 shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend;
246}
keita kobayashid6d757c2014-05-29 16:24:27 +0900247#endif