hotplug.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. enum {
  61. HI3620_CTRL,
  62. ERROR_CTRL,
  63. };
  64. static void __iomem *ctrl_base;
  65. static int id;
  66. static void set_cpu_hi3620(int cpu, bool enable)
  67. {
  68. u32 val = 0;
  69. if (enable) {
  70. /* MTCMOS set */
  71. if ((cpu == 2) || (cpu == 3))
  72. writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
  73. ctrl_base + SCPERPWREN);
  74. udelay(100);
  75. /* Enable core */
  76. writel_relaxed(0x01 << cpu, ctrl_base + SCCPUCOREEN);
  77. /* unreset */
  78. val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
  79. | CPU0_SRST_REQ_EN;
  80. writel_relaxed(val << cpu, ctrl_base + SCCPURSTDIS);
  81. /* reset */
  82. val |= CPU0_HPM_SRST_REQ_EN;
  83. writel_relaxed(val << cpu, ctrl_base + SCCPURSTEN);
  84. /* ISO disable */
  85. if ((cpu == 2) || (cpu == 3))
  86. writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
  87. ctrl_base + SCISODIS);
  88. udelay(1);
  89. /* WFI Mask */
  90. val = readl_relaxed(ctrl_base + SCPERCTRL0);
  91. val &= ~(CPU0_WFI_MASK_CFG << cpu);
  92. writel_relaxed(val, ctrl_base + SCPERCTRL0);
  93. /* Unreset */
  94. val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
  95. | CPU0_SRST_REQ_EN | CPU0_HPM_SRST_REQ_EN;
  96. writel_relaxed(val << cpu, ctrl_base + SCCPURSTDIS);
  97. } else {
  98. /* wfi mask */
  99. val = readl_relaxed(ctrl_base + SCPERCTRL0);
  100. val |= (CPU0_WFI_MASK_CFG << cpu);
  101. writel_relaxed(val, ctrl_base + SCPERCTRL0);
  102. /* disable core*/
  103. writel_relaxed(0x01 << cpu, ctrl_base + SCCPUCOREDIS);
  104. if ((cpu == 2) || (cpu == 3)) {
  105. /* iso enable */
  106. writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
  107. ctrl_base + SCISOEN);
  108. udelay(1);
  109. }
  110. /* reset */
  111. val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
  112. | CPU0_SRST_REQ_EN | CPU0_HPM_SRST_REQ_EN;
  113. writel_relaxed(val << cpu, ctrl_base + SCCPURSTEN);
  114. if ((cpu == 2) || (cpu == 3)) {
  115. /* MTCMOS unset */
  116. writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
  117. ctrl_base + SCPERPWRDIS);
  118. udelay(100);
  119. }
  120. }
  121. }
  122. static int hi3xxx_hotplug_init(void)
  123. {
  124. struct device_node *node;
  125. node = of_find_compatible_node(NULL, NULL, "hisilicon,sysctrl");
  126. if (node) {
  127. ctrl_base = of_iomap(node, 0);
  128. id = HI3620_CTRL;
  129. return 0;
  130. }
  131. id = ERROR_CTRL;
  132. return -ENOENT;
  133. }
  134. void hi3xxx_set_cpu(int cpu, bool enable)
  135. {
  136. if (!ctrl_base) {
  137. if (hi3xxx_hotplug_init() < 0)
  138. return;
  139. }
  140. if (id == HI3620_CTRL)
  141. set_cpu_hi3620(cpu, enable);
  142. }
  143. static bool hix5hd2_hotplug_init(void)
  144. {
  145. struct device_node *np;
  146. np = of_find_compatible_node(NULL, NULL, "hisilicon,cpuctrl");
  147. if (np) {
  148. ctrl_base = of_iomap(np, 0);
  149. return true;
  150. }
  151. return false;
  152. }
  153. void hix5hd2_set_cpu(int cpu, bool enable)
  154. {
  155. u32 val = 0;
  156. if (!ctrl_base)
  157. if (!hix5hd2_hotplug_init())
  158. BUG();
  159. if (enable) {
  160. /* power on cpu1 */
  161. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_PMC0);
  162. val &= ~(PMC0_CPU1_WAIT_MTCOMS_ACK | PMC0_CPU1_POWERDOWN);
  163. val |= PMC0_CPU1_PMC_ENABLE;
  164. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_PMC0);
  165. /* unreset */
  166. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_CRG20);
  167. val &= ~CRG20_CPU1_RESET;
  168. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_CRG20);
  169. } else {
  170. /* power down cpu1 */
  171. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_PMC0);
  172. val |= PMC0_CPU1_PMC_ENABLE | PMC0_CPU1_POWERDOWN;
  173. val &= ~PMC0_CPU1_WAIT_MTCOMS_ACK;
  174. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_PMC0);
  175. /* reset */
  176. val = readl_relaxed(ctrl_base + HIX5HD2_PERI_CRG20);
  177. val |= CRG20_CPU1_RESET;
  178. writel_relaxed(val, ctrl_base + HIX5HD2_PERI_CRG20);
  179. }
  180. }
  181. static inline void cpu_enter_lowpower(void)
  182. {
  183. unsigned int v;
  184. flush_cache_all();
  185. /*
  186. * Turn off coherency and L1 D-cache
  187. */
  188. asm volatile(
  189. " mrc p15, 0, %0, c1, c0, 1\n"
  190. " bic %0, %0, #0x40\n"
  191. " mcr p15, 0, %0, c1, c0, 1\n"
  192. " mrc p15, 0, %0, c1, c0, 0\n"
  193. " bic %0, %0, #0x04\n"
  194. " mcr p15, 0, %0, c1, c0, 0\n"
  195. : "=&r" (v)
  196. : "r" (0)
  197. : "cc");
  198. }
  199. #ifdef CONFIG_HOTPLUG_CPU
  200. void hi3xxx_cpu_die(unsigned int cpu)
  201. {
  202. cpu_enter_lowpower();
  203. hi3xxx_set_cpu_jump(cpu, phys_to_virt(0));
  204. cpu_do_idle();
  205. /* We should have never returned from idle */
  206. panic("cpu %d unexpectedly exit from shutdown\n", cpu);
  207. }
  208. int hi3xxx_cpu_kill(unsigned int cpu)
  209. {
  210. unsigned long timeout = jiffies + msecs_to_jiffies(50);
  211. while (hi3xxx_get_cpu_jump(cpu))
  212. if (time_after(jiffies, timeout))
  213. return 0;
  214. hi3xxx_set_cpu(cpu, false);
  215. return 1;
  216. }
  217. void hix5hd2_cpu_die(unsigned int cpu)
  218. {
  219. flush_cache_all();
  220. hix5hd2_set_cpu(cpu, false);
  221. }
  222. #endif