platsmp-brcmstb.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Broadcom STB CPU SMP and hotplug support for ARM
  4. *
  5. * Copyright (C) 2013-2014 Broadcom Corporation
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/printk.h>
  15. #include <linux/regmap.h>
  16. #include <linux/smp.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/cp15.h>
  20. #include <asm/mach-types.h>
  21. #include <asm/smp_plat.h>
  22. enum {
  23. ZONE_MAN_CLKEN_MASK = BIT(0),
  24. ZONE_MAN_RESET_CNTL_MASK = BIT(1),
  25. ZONE_MAN_MEM_PWR_MASK = BIT(4),
  26. ZONE_RESERVED_1_MASK = BIT(5),
  27. ZONE_MAN_ISO_CNTL_MASK = BIT(6),
  28. ZONE_MANUAL_CONTROL_MASK = BIT(7),
  29. ZONE_PWR_DN_REQ_MASK = BIT(9),
  30. ZONE_PWR_UP_REQ_MASK = BIT(10),
  31. ZONE_BLK_RST_ASSERT_MASK = BIT(12),
  32. ZONE_PWR_OFF_STATE_MASK = BIT(25),
  33. ZONE_PWR_ON_STATE_MASK = BIT(26),
  34. ZONE_DPG_PWR_STATE_MASK = BIT(28),
  35. ZONE_MEM_PWR_STATE_MASK = BIT(29),
  36. ZONE_RESET_STATE_MASK = BIT(31),
  37. CPU0_PWR_ZONE_CTRL_REG = 1,
  38. CPU_RESET_CONFIG_REG = 2,
  39. };
  40. static void __iomem *cpubiuctrl_block;
  41. static void __iomem *hif_cont_block;
  42. static u32 cpu0_pwr_zone_ctrl_reg;
  43. static u32 cpu_rst_cfg_reg;
  44. static u32 hif_cont_reg;
  45. #ifdef CONFIG_HOTPLUG_CPU
  46. /*
  47. * We must quiesce a dying CPU before it can be killed by the boot CPU. Because
  48. * one or more cache may be disabled, we must flush to ensure coherency. We
  49. * cannot use traditional completion structures or spinlocks as they rely on
  50. * coherency.
  51. */
  52. static DEFINE_PER_CPU_ALIGNED(int, per_cpu_sw_state);
  53. static int per_cpu_sw_state_rd(u32 cpu)
  54. {
  55. sync_cache_r(SHIFT_PERCPU_PTR(&per_cpu_sw_state, per_cpu_offset(cpu)));
  56. return per_cpu(per_cpu_sw_state, cpu);
  57. }
  58. static void per_cpu_sw_state_wr(u32 cpu, int val)
  59. {
  60. dmb();
  61. per_cpu(per_cpu_sw_state, cpu) = val;
  62. sync_cache_w(SHIFT_PERCPU_PTR(&per_cpu_sw_state, per_cpu_offset(cpu)));
  63. }
  64. #else
  65. static inline void per_cpu_sw_state_wr(u32 cpu, int val) { }
  66. #endif
  67. static void __iomem *pwr_ctrl_get_base(u32 cpu)
  68. {
  69. void __iomem *base = cpubiuctrl_block + cpu0_pwr_zone_ctrl_reg;
  70. base += (cpu_logical_map(cpu) * 4);
  71. return base;
  72. }
  73. static u32 pwr_ctrl_rd(u32 cpu)
  74. {
  75. void __iomem *base = pwr_ctrl_get_base(cpu);
  76. return readl_relaxed(base);
  77. }
  78. static void pwr_ctrl_set(unsigned int cpu, u32 val, u32 mask)
  79. {
  80. void __iomem *base = pwr_ctrl_get_base(cpu);
  81. writel((readl(base) & mask) | val, base);
  82. }
  83. static void pwr_ctrl_clr(unsigned int cpu, u32 val, u32 mask)
  84. {
  85. void __iomem *base = pwr_ctrl_get_base(cpu);
  86. writel((readl(base) & mask) & ~val, base);
  87. }
  88. #define POLL_TMOUT_MS 500
  89. static int pwr_ctrl_wait_tmout(unsigned int cpu, u32 set, u32 mask)
  90. {
  91. const unsigned long timeo = jiffies + msecs_to_jiffies(POLL_TMOUT_MS);
  92. u32 tmp;
  93. do {
  94. tmp = pwr_ctrl_rd(cpu) & mask;
  95. if (!set == !tmp)
  96. return 0;
  97. } while (time_before(jiffies, timeo));
  98. tmp = pwr_ctrl_rd(cpu) & mask;
  99. if (!set == !tmp)
  100. return 0;
  101. return -ETIMEDOUT;
  102. }
  103. static void cpu_rst_cfg_set(u32 cpu, int set)
  104. {
  105. u32 val;
  106. val = readl_relaxed(cpubiuctrl_block + cpu_rst_cfg_reg);
  107. if (set)
  108. val |= BIT(cpu_logical_map(cpu));
  109. else
  110. val &= ~BIT(cpu_logical_map(cpu));
  111. writel_relaxed(val, cpubiuctrl_block + cpu_rst_cfg_reg);
  112. }
  113. static void cpu_set_boot_addr(u32 cpu, unsigned long boot_addr)
  114. {
  115. const int reg_ofs = cpu_logical_map(cpu) * 8;
  116. writel_relaxed(0, hif_cont_block + hif_cont_reg + reg_ofs);
  117. writel_relaxed(boot_addr, hif_cont_block + hif_cont_reg + 4 + reg_ofs);
  118. }
  119. static void brcmstb_cpu_boot(u32 cpu)
  120. {
  121. /* Mark this CPU as "up" */
  122. per_cpu_sw_state_wr(cpu, 1);
  123. /*
  124. * Set the reset vector to point to the secondary_startup
  125. * routine
  126. */
  127. cpu_set_boot_addr(cpu, __pa_symbol(secondary_startup));
  128. /* Unhalt the cpu */
  129. cpu_rst_cfg_set(cpu, 0);
  130. }
  131. static void brcmstb_cpu_power_on(u32 cpu)
  132. {
  133. /*
  134. * The secondary cores power was cut, so we must go through
  135. * power-on initialization.
  136. */
  137. pwr_ctrl_set(cpu, ZONE_MAN_ISO_CNTL_MASK, 0xffffff00);
  138. pwr_ctrl_set(cpu, ZONE_MANUAL_CONTROL_MASK, -1);
  139. pwr_ctrl_set(cpu, ZONE_RESERVED_1_MASK, -1);
  140. pwr_ctrl_set(cpu, ZONE_MAN_MEM_PWR_MASK, -1);
  141. if (pwr_ctrl_wait_tmout(cpu, 1, ZONE_MEM_PWR_STATE_MASK))
  142. panic("ZONE_MEM_PWR_STATE_MASK set timeout");
  143. pwr_ctrl_set(cpu, ZONE_MAN_CLKEN_MASK, -1);
  144. if (pwr_ctrl_wait_tmout(cpu, 1, ZONE_DPG_PWR_STATE_MASK))
  145. panic("ZONE_DPG_PWR_STATE_MASK set timeout");
  146. pwr_ctrl_clr(cpu, ZONE_MAN_ISO_CNTL_MASK, -1);
  147. pwr_ctrl_set(cpu, ZONE_MAN_RESET_CNTL_MASK, -1);
  148. }
  149. static int brcmstb_cpu_get_power_state(u32 cpu)
  150. {
  151. int tmp = pwr_ctrl_rd(cpu);
  152. return (tmp & ZONE_RESET_STATE_MASK) ? 0 : 1;
  153. }
  154. #ifdef CONFIG_HOTPLUG_CPU
  155. static void brcmstb_cpu_die(u32 cpu)
  156. {
  157. v7_exit_coherency_flush(all);
  158. per_cpu_sw_state_wr(cpu, 0);
  159. /* Sit and wait to die */
  160. wfi();
  161. /* We should never get here... */
  162. while (1)
  163. ;
  164. }
  165. static int brcmstb_cpu_kill(u32 cpu)
  166. {
  167. /*
  168. * Ordinarily, the hardware forbids power-down of CPU0 (which is good
  169. * because it is the boot CPU), but this is not true when using BPCM
  170. * manual mode. Consequently, we must avoid turning off CPU0 here to
  171. * ensure that TI2C master reset will work.
  172. */
  173. if (cpu == 0) {
  174. pr_warn("SMP: refusing to power off CPU0\n");
  175. return 1;
  176. }
  177. while (per_cpu_sw_state_rd(cpu))
  178. ;
  179. pwr_ctrl_set(cpu, ZONE_MANUAL_CONTROL_MASK, -1);
  180. pwr_ctrl_clr(cpu, ZONE_MAN_RESET_CNTL_MASK, -1);
  181. pwr_ctrl_clr(cpu, ZONE_MAN_CLKEN_MASK, -1);
  182. pwr_ctrl_set(cpu, ZONE_MAN_ISO_CNTL_MASK, -1);
  183. pwr_ctrl_clr(cpu, ZONE_MAN_MEM_PWR_MASK, -1);
  184. if (pwr_ctrl_wait_tmout(cpu, 0, ZONE_MEM_PWR_STATE_MASK))
  185. panic("ZONE_MEM_PWR_STATE_MASK clear timeout");
  186. pwr_ctrl_clr(cpu, ZONE_RESERVED_1_MASK, -1);
  187. if (pwr_ctrl_wait_tmout(cpu, 0, ZONE_DPG_PWR_STATE_MASK))
  188. panic("ZONE_DPG_PWR_STATE_MASK clear timeout");
  189. /* Flush pipeline before resetting CPU */
  190. mb();
  191. /* Assert reset on the CPU */
  192. cpu_rst_cfg_set(cpu, 1);
  193. return 1;
  194. }
  195. #endif /* CONFIG_HOTPLUG_CPU */
  196. static int __init setup_hifcpubiuctrl_regs(struct device_node *np)
  197. {
  198. int rc = 0;
  199. char *name;
  200. struct device_node *syscon_np = NULL;
  201. name = "syscon-cpu";
  202. syscon_np = of_parse_phandle(np, name, 0);
  203. if (!syscon_np) {
  204. pr_err("can't find phandle %s\n", name);
  205. rc = -EINVAL;
  206. goto cleanup;
  207. }
  208. cpubiuctrl_block = of_iomap(syscon_np, 0);
  209. if (!cpubiuctrl_block) {
  210. pr_err("iomap failed for cpubiuctrl_block\n");
  211. rc = -EINVAL;
  212. goto cleanup;
  213. }
  214. rc = of_property_read_u32_index(np, name, CPU0_PWR_ZONE_CTRL_REG,
  215. &cpu0_pwr_zone_ctrl_reg);
  216. if (rc) {
  217. pr_err("failed to read 1st entry from %s property (%d)\n", name,
  218. rc);
  219. rc = -EINVAL;
  220. goto cleanup;
  221. }
  222. rc = of_property_read_u32_index(np, name, CPU_RESET_CONFIG_REG,
  223. &cpu_rst_cfg_reg);
  224. if (rc) {
  225. pr_err("failed to read 2nd entry from %s property (%d)\n", name,
  226. rc);
  227. rc = -EINVAL;
  228. goto cleanup;
  229. }
  230. cleanup:
  231. of_node_put(syscon_np);
  232. return rc;
  233. }
  234. static int __init setup_hifcont_regs(struct device_node *np)
  235. {
  236. int rc = 0;
  237. char *name;
  238. struct device_node *syscon_np = NULL;
  239. name = "syscon-cont";
  240. syscon_np = of_parse_phandle(np, name, 0);
  241. if (!syscon_np) {
  242. pr_err("can't find phandle %s\n", name);
  243. rc = -EINVAL;
  244. goto cleanup;
  245. }
  246. hif_cont_block = of_iomap(syscon_np, 0);
  247. if (!hif_cont_block) {
  248. pr_err("iomap failed for hif_cont_block\n");
  249. rc = -EINVAL;
  250. goto cleanup;
  251. }
  252. /* Offset is at top of hif_cont_block */
  253. hif_cont_reg = 0;
  254. cleanup:
  255. of_node_put(syscon_np);
  256. return rc;
  257. }
  258. static void __init brcmstb_cpu_ctrl_setup(unsigned int max_cpus)
  259. {
  260. int rc;
  261. struct device_node *np;
  262. char *name;
  263. name = "brcm,brcmstb-smpboot";
  264. np = of_find_compatible_node(NULL, NULL, name);
  265. if (!np) {
  266. pr_err("can't find compatible node %s\n", name);
  267. return;
  268. }
  269. rc = setup_hifcpubiuctrl_regs(np);
  270. if (rc)
  271. goto out_put_node;
  272. rc = setup_hifcont_regs(np);
  273. if (rc)
  274. goto out_put_node;
  275. out_put_node:
  276. of_node_put(np);
  277. }
  278. static int brcmstb_boot_secondary(unsigned int cpu, struct task_struct *idle)
  279. {
  280. /* Missing the brcm,brcmstb-smpboot DT node? */
  281. if (!cpubiuctrl_block || !hif_cont_block)
  282. return -ENODEV;
  283. /* Bring up power to the core if necessary */
  284. if (brcmstb_cpu_get_power_state(cpu) == 0)
  285. brcmstb_cpu_power_on(cpu);
  286. brcmstb_cpu_boot(cpu);
  287. return 0;
  288. }
  289. static const struct smp_operations brcmstb_smp_ops __initconst = {
  290. .smp_prepare_cpus = brcmstb_cpu_ctrl_setup,
  291. .smp_boot_secondary = brcmstb_boot_secondary,
  292. #ifdef CONFIG_HOTPLUG_CPU
  293. .cpu_kill = brcmstb_cpu_kill,
  294. .cpu_die = brcmstb_cpu_die,
  295. #endif
  296. };
  297. CPU_METHOD_OF_DECLARE(brcmstb_smp, "brcm,brahma-b15", &brcmstb_smp_ops);