platsmp-apmu.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. }
  166. void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus,
  167. struct rcar_apmu_config *apmu_config,
  168. int num)
  169. {
  170. shmobile_smp_apmu_setup_boot();
  171. apmu_parse_cfg(apmu_init_cpu, apmu_config, num);
  172. }
  173. int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
  174. {
  175. /* For this particular CPU register boot vector */
  176. shmobile_smp_hook(cpu, __pa_symbol(secondary_startup), 0);
  177. return apmu_wrap(cpu, apmu_power_on);
  178. }
  179. static void __init shmobile_smp_apmu_prepare_cpus_dt(unsigned int max_cpus)
  180. {
  181. shmobile_smp_apmu_setup_boot();
  182. apmu_parse_dt(apmu_init_cpu);
  183. rcar_gen2_pm_init();
  184. }
  185. static struct smp_operations apmu_smp_ops __initdata = {
  186. .smp_prepare_cpus = shmobile_smp_apmu_prepare_cpus_dt,
  187. .smp_boot_secondary = shmobile_smp_apmu_boot_secondary,
  188. #ifdef CONFIG_HOTPLUG_CPU
  189. .cpu_can_disable = shmobile_smp_cpu_can_disable,
  190. .cpu_die = shmobile_smp_apmu_cpu_die,
  191. .cpu_kill = shmobile_smp_apmu_cpu_kill,
  192. #endif
  193. };
  194. CPU_METHOD_OF_DECLARE(shmobile_smp_apmu, "renesas,apmu", &apmu_smp_ops);
  195. #endif /* CONFIG_SMP */
  196. #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
  197. /* nicked from arch/arm/mach-exynos/hotplug.c */
  198. static inline void cpu_enter_lowpower_a15(void)
  199. {
  200. unsigned int v;
  201. asm volatile(
  202. " mrc p15, 0, %0, c1, c0, 0\n"
  203. " bic %0, %0, %1\n"
  204. " mcr p15, 0, %0, c1, c0, 0\n"
  205. : "=&r" (v)
  206. : "Ir" (CR_C)
  207. : "cc");
  208. flush_cache_louis();
  209. asm volatile(
  210. /*
  211. * Turn off coherency
  212. */
  213. " mrc p15, 0, %0, c1, c0, 1\n"
  214. " bic %0, %0, %1\n"
  215. " mcr p15, 0, %0, c1, c0, 1\n"
  216. : "=&r" (v)
  217. : "Ir" (0x40)
  218. : "cc");
  219. isb();
  220. dsb();
  221. }
  222. static void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu)
  223. {
  224. /* Select next sleep mode using the APMU */
  225. apmu_wrap(cpu, apmu_power_off);
  226. /* Do ARM specific CPU shutdown */
  227. cpu_enter_lowpower_a15();
  228. }
  229. static inline void cpu_leave_lowpower(void)
  230. {
  231. unsigned int v;
  232. asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
  233. " orr %0, %0, %1\n"
  234. " mcr p15, 0, %0, c1, c0, 0\n"
  235. " mrc p15, 0, %0, c1, c0, 1\n"
  236. " orr %0, %0, %2\n"
  237. " mcr p15, 0, %0, c1, c0, 1\n"
  238. : "=&r" (v)
  239. : "Ir" (CR_C), "Ir" (0x40)
  240. : "cc");
  241. }
  242. #endif
  243. #if defined(CONFIG_HOTPLUG_CPU)
  244. void shmobile_smp_apmu_cpu_die(unsigned int cpu)
  245. {
  246. /* For this particular CPU deregister boot vector */
  247. shmobile_smp_hook(cpu, 0, 0);
  248. /* Shutdown CPU core */
  249. shmobile_smp_apmu_cpu_shutdown(cpu);
  250. /* jump to shared mach-shmobile sleep / reset code */
  251. shmobile_smp_sleep();
  252. }
  253. int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
  254. {
  255. return apmu_wrap(cpu, apmu_power_off_poll);
  256. }
  257. #endif
  258. #if defined(CONFIG_SUSPEND)
  259. static int shmobile_smp_apmu_do_suspend(unsigned long cpu)
  260. {
  261. shmobile_smp_hook(cpu, __pa_symbol(cpu_resume), 0);
  262. shmobile_smp_apmu_cpu_shutdown(cpu);
  263. cpu_do_idle(); /* WFI selects Core Standby */
  264. return 1;
  265. }
  266. static int shmobile_smp_apmu_enter_suspend(suspend_state_t state)
  267. {
  268. cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend);
  269. cpu_leave_lowpower();
  270. return 0;
  271. }
  272. void __init shmobile_smp_apmu_suspend_init(void)
  273. {
  274. shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend;
  275. }
  276. #endif