platsmp-apmu.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * SMP support for SoCs with APMU
  3. *
  4. * Copyright (C) 2014 Renesas Electronics Corporation
  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. */
  11. #include <linux/cpu_pm.h>
  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>
  18. #include <linux/suspend.h>
  19. #include <linux/threads.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/cp15.h>
  22. #include <asm/proc-fns.h>
  23. #include <asm/smp_plat.h>
  24. #include <asm/suspend.h>
  25. #include "common.h"
  26. #include "platsmp-apmu.h"
  27. #include "rcar-gen2.h"
  28. static struct {
  29. void __iomem *iomem;
  30. int bit;
  31. } apmu_cpus[NR_CPUS];
  32. #define WUPCR_OFFS 0x10 /* Wake Up Control Register */
  33. #define PSTR_OFFS 0x40 /* Power Status Register */
  34. #define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
  35. /* CPUn Power Status Control Register */
  36. #define DBGRCR_OFFS 0x180 /* Debug Resource Reset Control Reg. */
  37. /* Power Status Register */
  38. #define CPUNST(r, n) (((r) >> (n * 4)) & 3) /* CPUn Status Bit */
  39. #define CPUST_RUN 0 /* Run Mode */
  40. #define CPUST_STANDBY 3 /* CoreStandby Mode */
  41. /* Debug Resource Reset Control Register */
  42. #define DBGCPUREN BIT(24) /* CPU Other Reset Request Enable */
  43. #define DBGCPUNREN(n) BIT((n) + 20) /* CPUn Reset Request Enable */
  44. #define DBGCPUPREN BIT(19) /* CPU Peripheral Reset Req. Enable */
  45. static int __maybe_unused apmu_power_on(void __iomem *p, int bit)
  46. {
  47. /* request power on */
  48. writel_relaxed(BIT(bit), p + WUPCR_OFFS);
  49. /* wait for APMU to finish */
  50. while (readl_relaxed(p + WUPCR_OFFS) != 0)
  51. ;
  52. return 0;
  53. }
  54. static int __maybe_unused apmu_power_off(void __iomem *p, int bit)
  55. {
  56. /* request Core Standby for next WFI */
  57. writel_relaxed(3, p + CPUNCR_OFFS(bit));
  58. return 0;
  59. }
  60. static int __maybe_unused apmu_power_off_poll(void __iomem *p, int bit)
  61. {
  62. int k;
  63. for (k = 0; k < 1000; k++) {
  64. if (CPUNST(readl_relaxed(p + PSTR_OFFS), bit) == CPUST_STANDBY)
  65. return 1;
  66. mdelay(1);
  67. }
  68. return 0;
  69. }
  70. static int __maybe_unused apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu))
  71. {
  72. void __iomem *p = apmu_cpus[cpu].iomem;
  73. return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL;
  74. }
  75. #ifdef CONFIG_SMP
  76. static void apmu_init_cpu(struct resource *res, int cpu, int bit)
  77. {
  78. u32 x;
  79. if ((cpu >= ARRAY_SIZE(apmu_cpus)) || apmu_cpus[cpu].iomem)
  80. return;
  81. apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res));
  82. apmu_cpus[cpu].bit = bit;
  83. pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res);
  84. /* Setup for debug mode */
  85. x = readl(apmu_cpus[cpu].iomem + DBGRCR_OFFS);
  86. x |= DBGCPUREN | DBGCPUNREN(bit) | DBGCPUPREN;
  87. writel(x, apmu_cpus[cpu].iomem + DBGRCR_OFFS);
  88. }
  89. static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit),
  90. struct rcar_apmu_config *apmu_config, int num)
  91. {
  92. int id;
  93. int k;
  94. int bit, index;
  95. bool is_allowed;
  96. for (k = 0; k < num; k++) {
  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. for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
  109. id = apmu_config[k].cpus[bit];
  110. if (id >= 0) {
  111. index = get_logical_index(id);
  112. if (index >= 0)
  113. fn(&apmu_config[k].iomem, index, bit);
  114. }
  115. }
  116. }
  117. }
  118. static const struct of_device_id apmu_ids[] = {
  119. { .compatible = "renesas,apmu" },
  120. { /*sentinel*/ }
  121. };
  122. static void apmu_parse_dt(void (*fn)(struct resource *res, int cpu, int bit))
  123. {
  124. struct device_node *np_apmu, *np_cpu;
  125. struct resource res;
  126. int bit, index;
  127. u32 id;
  128. for_each_matching_node(np_apmu, apmu_ids) {
  129. /* only enable the cluster that includes the boot CPU */
  130. bool is_allowed = false;
  131. for (bit = 0; bit < CONFIG_NR_CPUS; bit++) {
  132. np_cpu = of_parse_phandle(np_apmu, "cpus", bit);
  133. if (np_cpu) {
  134. if (!of_property_read_u32(np_cpu, "reg", &id)) {
  135. if (id == cpu_logical_map(0)) {
  136. is_allowed = true;
  137. of_node_put(np_cpu);
  138. break;
  139. }
  140. }
  141. of_node_put(np_cpu);
  142. }
  143. }
  144. if (!is_allowed)
  145. continue;
  146. for (bit = 0; bit < CONFIG_NR_CPUS; bit++) {
  147. np_cpu = of_parse_phandle(np_apmu, "cpus", bit);
  148. if (np_cpu) {
  149. if (!of_property_read_u32(np_cpu, "reg", &id)) {
  150. index = get_logical_index(id);
  151. if ((index >= 0) &&
  152. !of_address_to_resource(np_apmu,
  153. 0, &res))
  154. fn(&res, index, bit);
  155. }
  156. of_node_put(np_cpu);
  157. }
  158. }
  159. }
  160. }
  161. static void __init shmobile_smp_apmu_setup_boot(void)
  162. {
  163. /* install boot code shared by all CPUs */
  164. shmobile_boot_fn = __pa_symbol(shmobile_smp_boot);
  165. shmobile_boot_fn_gen2 = shmobile_boot_fn;
  166. }
  167. void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus,
  168. struct rcar_apmu_config *apmu_config,
  169. int num)
  170. {
  171. shmobile_smp_apmu_setup_boot();
  172. apmu_parse_cfg(apmu_init_cpu, apmu_config, num);
  173. }
  174. int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
  175. {
  176. /* For this particular CPU register boot vector */
  177. shmobile_smp_hook(cpu, __pa_symbol(shmobile_boot_apmu), 0);
  178. return apmu_wrap(cpu, apmu_power_on);
  179. }
  180. static void __init shmobile_smp_apmu_prepare_cpus_dt(unsigned int max_cpus)
  181. {
  182. shmobile_smp_apmu_setup_boot();
  183. apmu_parse_dt(apmu_init_cpu);
  184. rcar_gen2_pm_init();
  185. }
  186. static struct smp_operations apmu_smp_ops __initdata = {
  187. .smp_prepare_cpus = shmobile_smp_apmu_prepare_cpus_dt,
  188. .smp_boot_secondary = shmobile_smp_apmu_boot_secondary,
  189. #ifdef CONFIG_HOTPLUG_CPU
  190. .cpu_can_disable = shmobile_smp_cpu_can_disable,
  191. .cpu_die = shmobile_smp_apmu_cpu_die,
  192. .cpu_kill = shmobile_smp_apmu_cpu_kill,
  193. #endif
  194. };
  195. CPU_METHOD_OF_DECLARE(shmobile_smp_apmu, "renesas,apmu", &apmu_smp_ops);
  196. #endif /* CONFIG_SMP */
  197. #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
  198. /* nicked from arch/arm/mach-exynos/hotplug.c */
  199. static inline void cpu_enter_lowpower_a15(void)
  200. {
  201. unsigned int v;
  202. asm volatile(
  203. " mrc p15, 0, %0, c1, c0, 0\n"
  204. " bic %0, %0, %1\n"
  205. " mcr p15, 0, %0, c1, c0, 0\n"
  206. : "=&r" (v)
  207. : "Ir" (CR_C)
  208. : "cc");
  209. flush_cache_louis();
  210. asm volatile(
  211. /*
  212. * Turn off coherency
  213. */
  214. " mrc p15, 0, %0, c1, c0, 1\n"
  215. " bic %0, %0, %1\n"
  216. " mcr p15, 0, %0, c1, c0, 1\n"
  217. : "=&r" (v)
  218. : "Ir" (0x40)
  219. : "cc");
  220. isb();
  221. dsb();
  222. }
  223. static void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu)
  224. {
  225. /* Select next sleep mode using the APMU */
  226. apmu_wrap(cpu, apmu_power_off);
  227. /* Do ARM specific CPU shutdown */
  228. cpu_enter_lowpower_a15();
  229. }
  230. static inline void cpu_leave_lowpower(void)
  231. {
  232. unsigned int v;
  233. asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
  234. " orr %0, %0, %1\n"
  235. " mcr p15, 0, %0, c1, c0, 0\n"
  236. " mrc p15, 0, %0, c1, c0, 1\n"
  237. " orr %0, %0, %2\n"
  238. " mcr p15, 0, %0, c1, c0, 1\n"
  239. : "=&r" (v)
  240. : "Ir" (CR_C), "Ir" (0x40)
  241. : "cc");
  242. }
  243. #endif
  244. #if defined(CONFIG_HOTPLUG_CPU)
  245. void shmobile_smp_apmu_cpu_die(unsigned int cpu)
  246. {
  247. /* For this particular CPU deregister boot vector */
  248. shmobile_smp_hook(cpu, 0, 0);
  249. /* Shutdown CPU core */
  250. shmobile_smp_apmu_cpu_shutdown(cpu);
  251. /* jump to shared mach-shmobile sleep / reset code */
  252. shmobile_smp_sleep();
  253. }
  254. int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
  255. {
  256. return apmu_wrap(cpu, apmu_power_off_poll);
  257. }
  258. #endif
  259. #if defined(CONFIG_SUSPEND)
  260. static int shmobile_smp_apmu_do_suspend(unsigned long cpu)
  261. {
  262. shmobile_smp_hook(cpu, __pa_symbol(cpu_resume), 0);
  263. shmobile_smp_apmu_cpu_shutdown(cpu);
  264. cpu_do_idle(); /* WFI selects Core Standby */
  265. return 1;
  266. }
  267. static int shmobile_smp_apmu_enter_suspend(suspend_state_t state)
  268. {
  269. cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend);
  270. cpu_leave_lowpower();
  271. return 0;
  272. }
  273. void __init shmobile_smp_apmu_suspend_init(void)
  274. {
  275. shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend;
  276. }
  277. #endif