blob: 911884f7e28b174c271c6724c863520afe859868 [file] [log] [blame]
Magnus Damma112de82013-08-29 08:21:58 +09001/*
2 * SMP support for SoCs with APMU
3 *
Hisashi Nakamuraa8d2ff32014-10-24 17:33:08 +09004 * Copyright (C) 2014 Renesas Electronics Corporation
Magnus Damma112de82013-08-29 08:21:58 +09005 * 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 */
keita kobayashid6d757c2014-05-29 16:24:27 +090011#include <linux/cpu_pm.h>
Magnus Damma112de82013-08-29 08:21:58 +090012#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>
keita kobayashid6d757c2014-05-29 16:24:27 +090018#include <linux/suspend.h>
Magnus Damm784500b2014-06-06 16:20:18 +090019#include <linux/threads.h>
Magnus Damma112de82013-08-29 08:21:58 +090020#include <asm/cacheflush.h>
21#include <asm/cp15.h>
keita kobayashid6d757c2014-05-29 16:24:27 +090022#include <asm/proc-fns.h>
Magnus Damma112de82013-08-29 08:21:58 +090023#include <asm/smp_plat.h>
keita kobayashid6d757c2014-05-29 16:24:27 +090024#include <asm/suspend.h>
Magnus Dammfd44aa52014-06-17 16:47:37 +090025#include "common.h"
Hisashi Nakamuraa8d2ff32014-10-24 17:33:08 +090026#include "platsmp-apmu.h"
Magnus Damma112de82013-08-29 08:21:58 +090027
28static struct {
29 void __iomem *iomem;
30 int bit;
Magnus Damm784500b2014-06-06 16:20:18 +090031} apmu_cpus[NR_CPUS];
Magnus Damma112de82013-08-29 08:21:58 +090032
33#define WUPCR_OFFS 0x10
34#define PSTR_OFFS 0x40
35#define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
36
Magnus Damm784500b2014-06-06 16:20:18 +090037static int __maybe_unused apmu_power_on(void __iomem *p, int bit)
Magnus Damma112de82013-08-29 08:21:58 +090038{
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
Wolfram Sang151dd342015-07-10 22:48:16 +020049static int __maybe_unused apmu_power_off(void __iomem *p, int bit)
Magnus Damma112de82013-08-29 08:21:58 +090050{
51 /* request Core Standby for next WFI */
52 writel_relaxed(3, p + CPUNCR_OFFS(bit));
53 return 0;
54}
55
Magnus Damm784500b2014-06-06 16:20:18 +090056static int __maybe_unused apmu_power_off_poll(void __iomem *p, int bit)
Magnus Damma112de82013-08-29 08:21:58 +090057{
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
Wolfram Sang151dd342015-07-10 22:48:16 +020070static int __maybe_unused apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu))
Magnus Damma112de82013-08-29 08:21:58 +090071{
72 void __iomem *p = apmu_cpus[cpu].iomem;
73
74 return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL;
75}
76
77static void apmu_init_cpu(struct resource *res, int cpu, int bit)
78{
Magnus Damm784500b2014-06-06 16:20:18 +090079 if ((cpu >= ARRAY_SIZE(apmu_cpus)) || apmu_cpus[cpu].iomem)
Magnus Damma112de82013-08-29 08:21:58 +090080 return;
81
82 apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res));
83 apmu_cpus[cpu].bit = bit;
84
Laurent Pinchart56ff8732014-03-04 19:11:15 +010085 pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res);
Magnus Damma112de82013-08-29 08:21:58 +090086}
87
Hisashi Nakamuraa8d2ff32014-10-24 17:33:08 +090088static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit),
89 struct rcar_apmu_config *apmu_config, int num)
Magnus Damma112de82013-08-29 08:21:58 +090090{
Andrzej Hajda6e67d2c2015-09-21 15:33:45 +020091 int id;
Magnus Damma112de82013-08-29 08:21:58 +090092 int k;
93 int bit, index;
Magnus Dammee490bc2013-09-15 00:29:07 +090094 bool is_allowed;
Magnus Damma112de82013-08-29 08:21:58 +090095
Hisashi Nakamuraa8d2ff32014-10-24 17:33:08 +090096 for (k = 0; k < num; k++) {
Magnus Dammee490bc2013-09-15 00:29:07 +090097 /* 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
Magnus Damma112de82013-08-29 08:21:58 +0900109 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
Hisashi Nakamuraa8d2ff32014-10-24 17:33:08 +0900120void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus,
121 struct rcar_apmu_config *apmu_config,
122 int num)
Magnus Damma112de82013-08-29 08:21:58 +0900123{
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;
127
128 /* perform per-cpu setup */
Hisashi Nakamuraa8d2ff32014-10-24 17:33:08 +0900129 apmu_parse_cfg(apmu_init_cpu, apmu_config, num);
Magnus Damma112de82013-08-29 08:21:58 +0900130}
131
Magnus Damm784500b2014-06-06 16:20:18 +0900132#ifdef CONFIG_SMP
Magnus Damma112de82013-08-29 08:21:58 +0900133int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
134{
135 /* For this particular CPU register boot vector */
Russell King02b4e272015-05-19 17:06:44 +0100136 shmobile_smp_hook(cpu, virt_to_phys(secondary_startup), 0);
Magnus Damma112de82013-08-29 08:21:58 +0900137
138 return apmu_wrap(cpu, apmu_power_on);
139}
Magnus Damm784500b2014-06-06 16:20:18 +0900140#endif
Magnus Damma112de82013-08-29 08:21:58 +0900141
keita kobayashid6d757c2014-05-29 16:24:27 +0900142#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
Magnus Damma112de82013-08-29 08:21:58 +0900143/* nicked from arch/arm/mach-exynos/hotplug.c */
144static inline void cpu_enter_lowpower_a15(void)
145{
146 unsigned int v;
147
148 asm volatile(
149 " mrc p15, 0, %0, c1, c0, 0\n"
150 " bic %0, %0, %1\n"
151 " mcr p15, 0, %0, c1, c0, 0\n"
152 : "=&r" (v)
153 : "Ir" (CR_C)
154 : "cc");
155
156 flush_cache_louis();
157
158 asm volatile(
159 /*
160 * Turn off coherency
161 */
162 " mrc p15, 0, %0, c1, c0, 1\n"
163 " bic %0, %0, %1\n"
164 " mcr p15, 0, %0, c1, c0, 1\n"
165 : "=&r" (v)
166 : "Ir" (0x40)
167 : "cc");
168
169 isb();
170 dsb();
171}
172
Geert Uytterhoeven54245072015-09-30 17:50:18 +0200173static void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu)
Magnus Damma112de82013-08-29 08:21:58 +0900174{
Magnus Damma112de82013-08-29 08:21:58 +0900175
176 /* Select next sleep mode using the APMU */
177 apmu_wrap(cpu, apmu_power_off);
178
179 /* Do ARM specific CPU shutdown */
180 cpu_enter_lowpower_a15();
keita kobayashid6d757c2014-05-29 16:24:27 +0900181}
182
183static inline void cpu_leave_lowpower(void)
184{
185 unsigned int v;
186
187 asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
188 " orr %0, %0, %1\n"
189 " mcr p15, 0, %0, c1, c0, 0\n"
190 " mrc p15, 0, %0, c1, c0, 1\n"
191 " orr %0, %0, %2\n"
192 " mcr p15, 0, %0, c1, c0, 1\n"
193 : "=&r" (v)
194 : "Ir" (CR_C), "Ir" (0x40)
195 : "cc");
196}
197#endif
198
199#if defined(CONFIG_HOTPLUG_CPU)
200void shmobile_smp_apmu_cpu_die(unsigned int cpu)
201{
202 /* For this particular CPU deregister boot vector */
203 shmobile_smp_hook(cpu, 0, 0);
204
205 /* Shutdown CPU core */
206 shmobile_smp_apmu_cpu_shutdown(cpu);
Magnus Damma112de82013-08-29 08:21:58 +0900207
208 /* jump to shared mach-shmobile sleep / reset code */
209 shmobile_smp_sleep();
210}
211
212int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
213{
214 return apmu_wrap(cpu, apmu_power_off_poll);
215}
216#endif
keita kobayashid6d757c2014-05-29 16:24:27 +0900217
218#if defined(CONFIG_SUSPEND)
219static int shmobile_smp_apmu_do_suspend(unsigned long cpu)
220{
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 */
224 return 1;
225}
226
227static int shmobile_smp_apmu_enter_suspend(suspend_state_t state)
228{
229 cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend);
230 cpu_leave_lowpower();
231 return 0;
232}
233
Magnus Damm0d77c9a2014-06-06 16:20:46 +0900234void __init shmobile_smp_apmu_suspend_init(void)
keita kobayashid6d757c2014-05-29 16:24:27 +0900235{
236 shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend;
237}
keita kobayashid6d757c2014-05-29 16:24:27 +0900238#endif