hotplug.c 6.5 KB

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