hotplug.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013 Linaro Ltd.
  4. * Copyright (c) 2013 HiSilicon Limited.
  5. */
  6. #include <linux/cpu.h>
  7. #include <linux/delay.h>
  8. #include <linux/io.h>
  9. #include <linux/of_address.h>
  10. #include <asm/cacheflush.h>
  11. #include <asm/smp_plat.h>
  12. #include "core.h"
  13. /* Sysctrl registers in Hi3620 SoC */
  14. #define SCISOEN 0xc0
  15. #define SCISODIS 0xc4
  16. #define SCPERPWREN 0xd0
  17. #define SCPERPWRDIS 0xd4
  18. #define SCCPUCOREEN 0xf4
  19. #define SCCPUCOREDIS 0xf8
  20. #define SCPERCTRL0 0x200
  21. #define SCCPURSTEN 0x410
  22. #define SCCPURSTDIS 0x414
  23. /*
  24. * bit definition in SCISOEN/SCPERPWREN/...
  25. *
  26. * CPU2_ISO_CTRL (1 << 5)
  27. * CPU3_ISO_CTRL (1 << 6)
  28. * ...
  29. */
  30. #define CPU2_ISO_CTRL (1 << 5)
  31. /*
  32. * bit definition in SCPERCTRL0
  33. *
  34. * CPU0_WFI_MASK_CFG (1 << 28)
  35. * CPU1_WFI_MASK_CFG (1 << 29)
  36. * ...
  37. */
  38. #define CPU0_WFI_MASK_CFG (1 << 28)
  39. /*
  40. * bit definition in SCCPURSTEN/...
  41. *
  42. * CPU0_SRST_REQ_EN (1 << 0)
  43. * CPU1_SRST_REQ_EN (1 << 1)
  44. * ...
  45. */
  46. #define CPU0_HPM_SRST_REQ_EN (1 << 22)
  47. #define CPU0_DBG_SRST_REQ_EN (1 << 12)
  48. #define CPU0_NEON_SRST_REQ_EN (1 << 4)
  49. #define CPU0_SRST_REQ_EN (1 << 0)
  50. #define HIX5HD2_PERI_CRG20 0x50
  51. #define CRG20_CPU1_RESET (1 << 17)
  52. #define HIX5HD2_PERI_PMC0 0x1000
  53. #define PMC0_CPU1_WAIT_MTCOMS_ACK (1 << 8)
  54. #define PMC0_CPU1_PMC_ENABLE (1 << 7)
  55. #define PMC0_CPU1_POWERDOWN (1 << 3)
  56. #define HIP01_PERI9 0x50
  57. #define PERI9_CPU1_RESET (1 << 1)
  58. enum {
  59. HI3620_CTRL,
  60. ERROR_CTRL,
  61. };
  62. static void __iomem *ctrl_base;
  63. static int id;
  64. static void set_cpu_hi3620(int cpu, bool enable)
  65. {
  66. u32 val = 0;
  67. if (enable) {
  68. /* MTCMOS set */
  69. if ((cpu == 2) || (cpu == 3))
  70. writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
  71. ctrl_base + SCPERPWREN);
  72. udelay(100);
  73. /* Enable core */
  74. writel_relaxed(0x01 << cpu, ctrl_base + SCCPUCOREEN);
  75. /* unreset */
  76. val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
  77. | CPU0_SRST_REQ_EN;
  78. writel_relaxed(val << cpu, ctrl_base + SCCPURSTDIS);
  79. /* reset */
  80. val |= CPU0_HPM_SRST_REQ_EN;
  81. writel_relaxed(val << cpu, ctrl_base + SCCPURSTEN);
  82. /* ISO disable */
  83. if ((cpu == 2) || (cpu == 3))
  84. writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
  85. ctrl_base + SCISODIS);
  86. udelay(1);
  87. /* WFI Mask */
  88. val = readl_relaxed(ctrl_base + SCPERCTRL0);
  89. val &= ~(CPU0_WFI_MASK_CFG << cpu);
  90. writel_relaxed(val, ctrl_base + SCPERCTRL0);
  91. /* Unreset */
  92. val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
  93. | CPU0_SRST_REQ_EN | CPU0_HPM_SRST_REQ_EN;
  94. writel_relaxed(val << cpu, ctrl_base + SCCPURSTDIS);
  95. } else {
  96. /* wfi mask */
  97. val = readl_relaxed(ctrl_base + SCPERCTRL0);
  98. val |= (CPU0_WFI_MASK_CFG << cpu);
  99. writel_relaxed(val, ctrl_base + SCPERCTRL0);
  100. /* disable core*/
  101. writel_relaxed(0x01 << cpu, ctrl_base + SCCPUCOREDIS);
  102. if ((cpu == 2) || (cpu == 3)) {
  103. /* iso enable */
  104. writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
  105. ctrl_base + SCISOEN);
  106. udelay(1);
  107. }
  108. /* reset */
  109. val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
  110. | CPU0_SRST_REQ_EN | CPU0_HPM_SRST_REQ_EN;
  111. writel_relaxed(val << cpu, ctrl_base + SCCPURSTEN);
  112. if ((cpu == 2) || (cpu == 3)) {
  113. /* MTCMOS unset */
  114. writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
  115. ctrl_base + SCPERPWRDIS);
  116. udelay(100);
  117. }
  118. }
  119. }
  120. static int hi3xxx_hotplug_init(void)
  121. {
  122. struct device_node *node;
  123. node = of_find_compatible_node(NULL, NULL, "hisilicon,sysctrl");
  124. if (!node) {
  125. id = ERROR_CTRL;
  126. return -ENOENT;
  127. }
  128. ctrl_base = of_iomap(node, 0);
  129. of_node_put(node);
  130. if (!ctrl_base) {
  131. id = ERROR_CTRL;
  132. return -ENOMEM;
  133. }
  134. id = HI3620_CTRL;
  135. return 0;
  136. }
  137. void hi3xxx_set_cpu(int cpu, bool enable)
  138. {
  139. if (!ctrl_base) {
  140. if (hi3xxx_hotplug_init() < 0)
  141. return;
  142. }
  143. if (id == HI3620_CTRL)
  144. set_cpu_hi3620(cpu, enable);
  145. }
  146. static bool hix5hd2_hotplug_init(void)
  147. {
  148. struct device_node *np;
  149. np = of_find_compatible_node(NULL, NULL, "hisilicon,cpuctrl");
  150. if (!np)
  151. return false;
  152. ctrl_base = of_iomap(np, 0);
  153. of_node_put(np);
  154. if (!ctrl_base)
  155. return false;
  156. return true;
  157. }
  158. void hix5hd2_set_cpu(int cpu, bool enable)
  159. {
  160. u32 val = 0;
  161. if (!ctrl_base)
  162. if (!hix5hd2_hotplug_init())
  163. BUG();
  164. if (enable) {
  165. /* power on cpu1 */
  166. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_PMC0);
  167. val &= ~(PMC0_CPU1_WAIT_MTCOMS_ACK | PMC0_CPU1_POWERDOWN);
  168. val |= PMC0_CPU1_PMC_ENABLE;
  169. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_PMC0);
  170. /* unreset */
  171. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_CRG20);
  172. val &= ~CRG20_CPU1_RESET;
  173. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_CRG20);
  174. } else {
  175. /* power down cpu1 */
  176. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_PMC0);
  177. val |= PMC0_CPU1_PMC_ENABLE | PMC0_CPU1_POWERDOWN;
  178. val &= ~PMC0_CPU1_WAIT_MTCOMS_ACK;
  179. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_PMC0);
  180. /* reset */
  181. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_CRG20);
  182. val |= CRG20_CPU1_RESET;
  183. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_CRG20);
  184. }
  185. }
  186. void hip01_set_cpu(int cpu, bool enable)
  187. {
  188. unsigned int temp;
  189. struct device_node *np;
  190. if (!ctrl_base) {
  191. np = of_find_compatible_node(NULL, NULL, "hisilicon,hip01-sysctrl");
  192. BUG_ON(!np);
  193. ctrl_base = of_iomap(np, 0);
  194. of_node_put(np);
  195. BUG_ON(!ctrl_base);
  196. }
  197. if (enable) {
  198. /* reset on CPU1 */
  199. temp = readl_relaxed(ctrl_base + HIP01_PERI9);
  200. temp |= PERI9_CPU1_RESET;
  201. writel_relaxed(temp, ctrl_base + HIP01_PERI9);
  202. udelay(50);
  203. /* unreset on CPU1 */
  204. temp = readl_relaxed(ctrl_base + HIP01_PERI9);
  205. temp &= ~PERI9_CPU1_RESET;
  206. writel_relaxed(temp, ctrl_base + HIP01_PERI9);
  207. }
  208. }
  209. static inline void cpu_enter_lowpower(void)
  210. {
  211. unsigned int v;
  212. flush_cache_all();
  213. /*
  214. * Turn off coherency and L1 D-cache
  215. */
  216. asm volatile(
  217. " mrc p15, 0, %0, c1, c0, 1\n"
  218. " bic %0, %0, #0x40\n"
  219. " mcr p15, 0, %0, c1, c0, 1\n"
  220. " mrc p15, 0, %0, c1, c0, 0\n"
  221. " bic %0, %0, #0x04\n"
  222. " mcr p15, 0, %0, c1, c0, 0\n"
  223. : "=&r" (v)
  224. : "r" (0)
  225. : "cc");
  226. }
  227. #ifdef CONFIG_HOTPLUG_CPU
  228. void hi3xxx_cpu_die(unsigned int cpu)
  229. {
  230. cpu_enter_lowpower();
  231. hi3xxx_set_cpu_jump(cpu, phys_to_virt(0));
  232. cpu_do_idle();
  233. /* We should have never returned from idle */
  234. panic("cpu %d unexpectedly exit from shutdown\n", cpu);
  235. }
  236. int hi3xxx_cpu_kill(unsigned int cpu)
  237. {
  238. unsigned long timeout = jiffies + msecs_to_jiffies(50);
  239. while (hi3xxx_get_cpu_jump(cpu))
  240. if (time_after(jiffies, timeout))
  241. return 0;
  242. hi3xxx_set_cpu(cpu, false);
  243. return 1;
  244. }
  245. void hix5hd2_cpu_die(unsigned int cpu)
  246. {
  247. flush_cache_all();
  248. hix5hd2_set_cpu(cpu, false);
  249. }
  250. #endif