platsmp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Carlo Caione <[email protected]>
  4. * Copyright (C) 2017 Martin Blumenstingl <[email protected]>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/init.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/regmap.h>
  12. #include <linux/reset.h>
  13. #include <linux/smp.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/cp15.h>
  17. #include <asm/smp_scu.h>
  18. #include <asm/smp_plat.h>
  19. #define MESON_SMP_SRAM_CPU_CTRL_REG (0x00)
  20. #define MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(c) (0x04 + ((c - 1) << 2))
  21. #define MESON_CPU_AO_RTI_PWR_A9_CNTL0 (0x00)
  22. #define MESON_CPU_AO_RTI_PWR_A9_CNTL1 (0x04)
  23. #define MESON_CPU_AO_RTI_PWR_A9_MEM_PD0 (0x14)
  24. #define MESON_CPU_PWR_A9_CNTL0_M(c) (0x03 << ((c * 2) + 16))
  25. #define MESON_CPU_PWR_A9_CNTL1_M(c) (0x03 << ((c + 1) << 1))
  26. #define MESON_CPU_PWR_A9_MEM_PD0_M(c) (0x0f << (32 - (c * 4)))
  27. #define MESON_CPU_PWR_A9_CNTL1_ST(c) (0x01 << (c + 16))
  28. static void __iomem *sram_base;
  29. static void __iomem *scu_base;
  30. static struct regmap *pmu;
  31. static struct reset_control *meson_smp_get_core_reset(int cpu)
  32. {
  33. struct device_node *np = of_get_cpu_node(cpu, 0);
  34. return of_reset_control_get_exclusive(np, NULL);
  35. }
  36. static void meson_smp_set_cpu_ctrl(int cpu, bool on_off)
  37. {
  38. u32 val = readl(sram_base + MESON_SMP_SRAM_CPU_CTRL_REG);
  39. if (on_off)
  40. val |= BIT(cpu);
  41. else
  42. val &= ~BIT(cpu);
  43. /* keep bit 0 always enabled */
  44. val |= BIT(0);
  45. writel(val, sram_base + MESON_SMP_SRAM_CPU_CTRL_REG);
  46. }
  47. static void __init meson_smp_prepare_cpus(const char *scu_compatible,
  48. const char *pmu_compatible,
  49. const char *sram_compatible)
  50. {
  51. static struct device_node *node;
  52. /* SMP SRAM */
  53. node = of_find_compatible_node(NULL, NULL, sram_compatible);
  54. if (!node) {
  55. pr_err("Missing SRAM node\n");
  56. return;
  57. }
  58. sram_base = of_iomap(node, 0);
  59. of_node_put(node);
  60. if (!sram_base) {
  61. pr_err("Couldn't map SRAM registers\n");
  62. return;
  63. }
  64. /* PMU */
  65. pmu = syscon_regmap_lookup_by_compatible(pmu_compatible);
  66. if (IS_ERR(pmu)) {
  67. pr_err("Couldn't map PMU registers\n");
  68. return;
  69. }
  70. /* SCU */
  71. node = of_find_compatible_node(NULL, NULL, scu_compatible);
  72. if (!node) {
  73. pr_err("Missing SCU node\n");
  74. return;
  75. }
  76. scu_base = of_iomap(node, 0);
  77. of_node_put(node);
  78. if (!scu_base) {
  79. pr_err("Couldn't map SCU registers\n");
  80. return;
  81. }
  82. scu_enable(scu_base);
  83. }
  84. static void __init meson8b_smp_prepare_cpus(unsigned int max_cpus)
  85. {
  86. meson_smp_prepare_cpus("arm,cortex-a5-scu", "amlogic,meson8b-pmu",
  87. "amlogic,meson8b-smp-sram");
  88. }
  89. static void __init meson8_smp_prepare_cpus(unsigned int max_cpus)
  90. {
  91. meson_smp_prepare_cpus("arm,cortex-a9-scu", "amlogic,meson8-pmu",
  92. "amlogic,meson8-smp-sram");
  93. }
  94. static void meson_smp_begin_secondary_boot(unsigned int cpu)
  95. {
  96. /*
  97. * Set the entry point before powering on the CPU through the SCU. This
  98. * is needed if the CPU is in "warm" state (= after rebooting the
  99. * system without power-cycling, or when taking the CPU offline and
  100. * then taking it online again.
  101. */
  102. writel(__pa_symbol(secondary_startup),
  103. sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu));
  104. /*
  105. * SCU Power on CPU (needs to be done before starting the CPU,
  106. * otherwise the secondary CPU will not start).
  107. */
  108. scu_cpu_power_enable(scu_base, cpu);
  109. }
  110. static int meson_smp_finalize_secondary_boot(unsigned int cpu)
  111. {
  112. unsigned long timeout;
  113. timeout = jiffies + (10 * HZ);
  114. while (readl(sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu))) {
  115. if (!time_before(jiffies, timeout)) {
  116. pr_err("Timeout while waiting for CPU%d status\n",
  117. cpu);
  118. return -ETIMEDOUT;
  119. }
  120. }
  121. writel(__pa_symbol(secondary_startup),
  122. sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu));
  123. meson_smp_set_cpu_ctrl(cpu, true);
  124. return 0;
  125. }
  126. static int meson8_smp_boot_secondary(unsigned int cpu,
  127. struct task_struct *idle)
  128. {
  129. struct reset_control *rstc;
  130. int ret;
  131. rstc = meson_smp_get_core_reset(cpu);
  132. if (IS_ERR(rstc)) {
  133. pr_err("Couldn't get the reset controller for CPU%d\n", cpu);
  134. return PTR_ERR(rstc);
  135. }
  136. meson_smp_begin_secondary_boot(cpu);
  137. /* Reset enable */
  138. ret = reset_control_assert(rstc);
  139. if (ret) {
  140. pr_err("Failed to assert CPU%d reset\n", cpu);
  141. goto out;
  142. }
  143. /* CPU power ON */
  144. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
  145. MESON_CPU_PWR_A9_CNTL1_M(cpu), 0);
  146. if (ret < 0) {
  147. pr_err("Couldn't wake up CPU%d\n", cpu);
  148. goto out;
  149. }
  150. udelay(10);
  151. /* Isolation disable */
  152. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
  153. 0);
  154. if (ret < 0) {
  155. pr_err("Error when disabling isolation of CPU%d\n", cpu);
  156. goto out;
  157. }
  158. /* Reset disable */
  159. ret = reset_control_deassert(rstc);
  160. if (ret) {
  161. pr_err("Failed to de-assert CPU%d reset\n", cpu);
  162. goto out;
  163. }
  164. ret = meson_smp_finalize_secondary_boot(cpu);
  165. if (ret)
  166. goto out;
  167. out:
  168. reset_control_put(rstc);
  169. return 0;
  170. }
  171. static int meson8b_smp_boot_secondary(unsigned int cpu,
  172. struct task_struct *idle)
  173. {
  174. struct reset_control *rstc;
  175. int ret;
  176. u32 val;
  177. rstc = meson_smp_get_core_reset(cpu);
  178. if (IS_ERR(rstc)) {
  179. pr_err("Couldn't get the reset controller for CPU%d\n", cpu);
  180. return PTR_ERR(rstc);
  181. }
  182. meson_smp_begin_secondary_boot(cpu);
  183. /* CPU power UP */
  184. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0,
  185. MESON_CPU_PWR_A9_CNTL0_M(cpu), 0);
  186. if (ret < 0) {
  187. pr_err("Couldn't power up CPU%d\n", cpu);
  188. goto out;
  189. }
  190. udelay(5);
  191. /* Reset enable */
  192. ret = reset_control_assert(rstc);
  193. if (ret) {
  194. pr_err("Failed to assert CPU%d reset\n", cpu);
  195. goto out;
  196. }
  197. /* Memory power UP */
  198. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_MEM_PD0,
  199. MESON_CPU_PWR_A9_MEM_PD0_M(cpu), 0);
  200. if (ret < 0) {
  201. pr_err("Couldn't power up the memory for CPU%d\n", cpu);
  202. goto out;
  203. }
  204. /* Wake up CPU */
  205. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
  206. MESON_CPU_PWR_A9_CNTL1_M(cpu), 0);
  207. if (ret < 0) {
  208. pr_err("Couldn't wake up CPU%d\n", cpu);
  209. goto out;
  210. }
  211. udelay(10);
  212. ret = regmap_read_poll_timeout(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1, val,
  213. val & MESON_CPU_PWR_A9_CNTL1_ST(cpu),
  214. 10, 10000);
  215. if (ret) {
  216. pr_err("Timeout while polling PMU for CPU%d status\n", cpu);
  217. goto out;
  218. }
  219. /* Isolation disable */
  220. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
  221. 0);
  222. if (ret < 0) {
  223. pr_err("Error when disabling isolation of CPU%d\n", cpu);
  224. goto out;
  225. }
  226. /* Reset disable */
  227. ret = reset_control_deassert(rstc);
  228. if (ret) {
  229. pr_err("Failed to de-assert CPU%d reset\n", cpu);
  230. goto out;
  231. }
  232. ret = meson_smp_finalize_secondary_boot(cpu);
  233. if (ret)
  234. goto out;
  235. out:
  236. reset_control_put(rstc);
  237. return 0;
  238. }
  239. #ifdef CONFIG_HOTPLUG_CPU
  240. static void meson8_smp_cpu_die(unsigned int cpu)
  241. {
  242. meson_smp_set_cpu_ctrl(cpu, false);
  243. v7_exit_coherency_flush(louis);
  244. scu_power_mode(scu_base, SCU_PM_POWEROFF);
  245. dsb();
  246. wfi();
  247. /* we should never get here */
  248. WARN_ON(1);
  249. }
  250. static int meson8_smp_cpu_kill(unsigned int cpu)
  251. {
  252. int ret, power_mode;
  253. unsigned long timeout;
  254. timeout = jiffies + (50 * HZ);
  255. do {
  256. power_mode = scu_get_cpu_power_mode(scu_base, cpu);
  257. if (power_mode == SCU_PM_POWEROFF)
  258. break;
  259. usleep_range(10000, 15000);
  260. } while (time_before(jiffies, timeout));
  261. if (power_mode != SCU_PM_POWEROFF) {
  262. pr_err("Error while waiting for SCU power-off on CPU%d\n",
  263. cpu);
  264. return -ETIMEDOUT;
  265. }
  266. msleep(30);
  267. /* Isolation enable */
  268. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
  269. 0x3);
  270. if (ret < 0) {
  271. pr_err("Error when enabling isolation for CPU%d\n", cpu);
  272. return ret;
  273. }
  274. udelay(10);
  275. /* CPU power OFF */
  276. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
  277. MESON_CPU_PWR_A9_CNTL1_M(cpu), 0x3);
  278. if (ret < 0) {
  279. pr_err("Couldn't change sleep status of CPU%d\n", cpu);
  280. return ret;
  281. }
  282. return 1;
  283. }
  284. static int meson8b_smp_cpu_kill(unsigned int cpu)
  285. {
  286. int ret, power_mode, count = 5000;
  287. do {
  288. power_mode = scu_get_cpu_power_mode(scu_base, cpu);
  289. if (power_mode == SCU_PM_POWEROFF)
  290. break;
  291. udelay(10);
  292. } while (++count);
  293. if (power_mode != SCU_PM_POWEROFF) {
  294. pr_err("Error while waiting for SCU power-off on CPU%d\n",
  295. cpu);
  296. return -ETIMEDOUT;
  297. }
  298. udelay(10);
  299. /* CPU power DOWN */
  300. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0,
  301. MESON_CPU_PWR_A9_CNTL0_M(cpu), 0x3);
  302. if (ret < 0) {
  303. pr_err("Couldn't power down CPU%d\n", cpu);
  304. return ret;
  305. }
  306. /* Isolation enable */
  307. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
  308. 0x3);
  309. if (ret < 0) {
  310. pr_err("Error when enabling isolation for CPU%d\n", cpu);
  311. return ret;
  312. }
  313. udelay(10);
  314. /* Sleep status */
  315. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
  316. MESON_CPU_PWR_A9_CNTL1_M(cpu), 0x3);
  317. if (ret < 0) {
  318. pr_err("Couldn't change sleep status of CPU%d\n", cpu);
  319. return ret;
  320. }
  321. /* Memory power DOWN */
  322. ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_MEM_PD0,
  323. MESON_CPU_PWR_A9_MEM_PD0_M(cpu), 0xf);
  324. if (ret < 0) {
  325. pr_err("Couldn't power down the memory of CPU%d\n", cpu);
  326. return ret;
  327. }
  328. return 1;
  329. }
  330. #endif
  331. static struct smp_operations meson8_smp_ops __initdata = {
  332. .smp_prepare_cpus = meson8_smp_prepare_cpus,
  333. .smp_boot_secondary = meson8_smp_boot_secondary,
  334. #ifdef CONFIG_HOTPLUG_CPU
  335. .cpu_die = meson8_smp_cpu_die,
  336. .cpu_kill = meson8_smp_cpu_kill,
  337. #endif
  338. };
  339. static struct smp_operations meson8b_smp_ops __initdata = {
  340. .smp_prepare_cpus = meson8b_smp_prepare_cpus,
  341. .smp_boot_secondary = meson8b_smp_boot_secondary,
  342. #ifdef CONFIG_HOTPLUG_CPU
  343. .cpu_die = meson8_smp_cpu_die,
  344. .cpu_kill = meson8b_smp_cpu_kill,
  345. #endif
  346. };
  347. CPU_METHOD_OF_DECLARE(meson8_smp, "amlogic,meson8-smp", &meson8_smp_ops);
  348. CPU_METHOD_OF_DECLARE(meson8b_smp, "amlogic,meson8b-smp", &meson8b_smp_ops);