platsmp-brcmstb.c 8.7 KB

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