hotplug.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. ctrl_base = of_iomap(node, 0);
  130. id = HI3620_CTRL;
  131. return 0;
  132. }
  133. id = ERROR_CTRL;
  134. return -ENOENT;
  135. }
  136. void hi3xxx_set_cpu(int cpu, bool enable)
  137. {
  138. if (!ctrl_base) {
  139. if (hi3xxx_hotplug_init() < 0)
  140. return;
  141. }
  142. if (id == HI3620_CTRL)
  143. set_cpu_hi3620(cpu, enable);
  144. }
  145. static bool hix5hd2_hotplug_init(void)
  146. {
  147. struct device_node *np;
  148. np = of_find_compatible_node(NULL, NULL, "hisilicon,cpuctrl");
  149. if (np) {
  150. ctrl_base = of_iomap(np, 0);
  151. return true;
  152. }
  153. return false;
  154. }
  155. void hix5hd2_set_cpu(int cpu, bool enable)
  156. {
  157. u32 val = 0;
  158. if (!ctrl_base)
  159. if (!hix5hd2_hotplug_init())
  160. BUG();
  161. if (enable) {
  162. /* power on cpu1 */
  163. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_PMC0);
  164. val &= ~(PMC0_CPU1_WAIT_MTCOMS_ACK | PMC0_CPU1_POWERDOWN);
  165. val |= PMC0_CPU1_PMC_ENABLE;
  166. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_PMC0);
  167. /* unreset */
  168. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_CRG20);
  169. val &= ~CRG20_CPU1_RESET;
  170. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_CRG20);
  171. } else {
  172. /* power down cpu1 */
  173. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_PMC0);
  174. val |= PMC0_CPU1_PMC_ENABLE | PMC0_CPU1_POWERDOWN;
  175. val &= ~PMC0_CPU1_WAIT_MTCOMS_ACK;
  176. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_PMC0);
  177. /* reset */
  178. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_CRG20);
  179. val |= CRG20_CPU1_RESET;
  180. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_CRG20);
  181. }
  182. }
  183. void hip01_set_cpu(int cpu, bool enable)
  184. {
  185. unsigned int temp;
  186. struct device_node *np;
  187. if (!ctrl_base) {
  188. np = of_find_compatible_node(NULL, NULL, "hisilicon,hip01-sysctrl");
  189. if (np)
  190. ctrl_base = of_iomap(np, 0);
  191. else
  192. BUG();
  193. }
  194. if (enable) {
  195. /* reset on CPU1 */
  196. temp = readl_relaxed(ctrl_base + HIP01_PERI9);
  197. temp |= PERI9_CPU1_RESET;
  198. writel_relaxed(temp, ctrl_base + HIP01_PERI9);
  199. udelay(50);
  200. /* unreset on CPU1 */
  201. temp = readl_relaxed(ctrl_base + HIP01_PERI9);
  202. temp &= ~PERI9_CPU1_RESET;
  203. writel_relaxed(temp, ctrl_base + HIP01_PERI9);
  204. }
  205. }
  206. static inline void cpu_enter_lowpower(void)
  207. {
  208. unsigned int v;
  209. flush_cache_all();
  210. /*
  211. * Turn off coherency and L1 D-cache
  212. */
  213. asm volatile(
  214. " mrc p15, 0, %0, c1, c0, 1\n"
  215. " bic %0, %0, #0x40\n"
  216. " mcr p15, 0, %0, c1, c0, 1\n"
  217. " mrc p15, 0, %0, c1, c0, 0\n"
  218. " bic %0, %0, #0x04\n"
  219. " mcr p15, 0, %0, c1, c0, 0\n"
  220. : "=&r" (v)
  221. : "r" (0)
  222. : "cc");
  223. }
  224. #ifdef CONFIG_HOTPLUG_CPU
  225. void hi3xxx_cpu_die(unsigned int cpu)
  226. {
  227. cpu_enter_lowpower();
  228. hi3xxx_set_cpu_jump(cpu, phys_to_virt(0));
  229. cpu_do_idle();
  230. /* We should have never returned from idle */
  231. panic("cpu %d unexpectedly exit from shutdown\n", cpu);
  232. }
  233. int hi3xxx_cpu_kill(unsigned int cpu)
  234. {
  235. unsigned long timeout = jiffies + msecs_to_jiffies(50);
  236. while (hi3xxx_get_cpu_jump(cpu))
  237. if (time_after(jiffies, timeout))
  238. return 0;
  239. hi3xxx_set_cpu(cpu, false);
  240. return 1;
  241. }
  242. void hix5hd2_cpu_die(unsigned int cpu)
  243. {
  244. flush_cache_all();
  245. hix5hd2_set_cpu(cpu, false);
  246. }
  247. #endif