platsmp-brcmstb.c 8.7 KB

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