platsmp-apmu.c 6.7 KB

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