platsmp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2013 MundoReader S.L.
  3. * Author: Heiko Stuebner <heiko@sntech.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * 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/init.h>
  17. #include <linux/smp.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/regmap.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/reset.h>
  24. #include <linux/cpu.h>
  25. #include <asm/cacheflush.h>
  26. #include <asm/cp15.h>
  27. #include <asm/smp_scu.h>
  28. #include <asm/smp_plat.h>
  29. #include <asm/mach/map.h>
  30. #include "core.h"
  31. static void __iomem *scu_base_addr;
  32. static void __iomem *sram_base_addr;
  33. static int ncores;
  34. #define PMU_PWRDN_CON 0x08
  35. #define PMU_PWRDN_ST 0x0c
  36. #define PMU_PWRDN_SCU 4
  37. static struct regmap *pmu;
  38. static int pmu_power_domain_is_on(int pd)
  39. {
  40. u32 val;
  41. int ret;
  42. ret = regmap_read(pmu, PMU_PWRDN_ST, &val);
  43. if (ret < 0)
  44. return ret;
  45. return !(val & BIT(pd));
  46. }
  47. static struct reset_control *rockchip_get_core_reset(int cpu)
  48. {
  49. struct device *dev = get_cpu_device(cpu);
  50. struct device_node *np;
  51. /* The cpu device is only available after the initial core bringup */
  52. if (dev)
  53. np = dev->of_node;
  54. else
  55. np = of_get_cpu_node(cpu, 0);
  56. return of_reset_control_get(np, NULL);
  57. }
  58. static int pmu_set_power_domain(int pd, bool on)
  59. {
  60. u32 val = (on) ? 0 : BIT(pd);
  61. struct reset_control *rstc = rockchip_get_core_reset(pd);
  62. int ret;
  63. if (IS_ERR(rstc) && read_cpuid_part() != ARM_CPU_PART_CORTEX_A9) {
  64. pr_err("%s: could not get reset control for core %d\n",
  65. __func__, pd);
  66. return PTR_ERR(rstc);
  67. }
  68. /*
  69. * We need to soft reset the cpu when we turn off the cpu power domain,
  70. * or else the active processors might be stalled when the individual
  71. * processor is powered down.
  72. */
  73. if (!IS_ERR(rstc) && !on)
  74. reset_control_assert(rstc);
  75. ret = regmap_update_bits(pmu, PMU_PWRDN_CON, BIT(pd), val);
  76. if (ret < 0) {
  77. pr_err("%s: could not update power domain\n", __func__);
  78. return ret;
  79. }
  80. ret = -1;
  81. while (ret != on) {
  82. ret = pmu_power_domain_is_on(pd);
  83. if (ret < 0) {
  84. pr_err("%s: could not read power domain state\n",
  85. __func__);
  86. return ret;
  87. }
  88. }
  89. if (!IS_ERR(rstc)) {
  90. if (on)
  91. reset_control_deassert(rstc);
  92. reset_control_put(rstc);
  93. }
  94. return 0;
  95. }
  96. /*
  97. * Handling of CPU cores
  98. */
  99. static int rockchip_boot_secondary(unsigned int cpu, struct task_struct *idle)
  100. {
  101. int ret;
  102. if (!sram_base_addr || !pmu) {
  103. pr_err("%s: sram or pmu missing for cpu boot\n", __func__);
  104. return -ENXIO;
  105. }
  106. if (cpu >= ncores) {
  107. pr_err("%s: cpu %d outside maximum number of cpus %d\n",
  108. __func__, cpu, ncores);
  109. return -ENXIO;
  110. }
  111. /* start the core */
  112. ret = pmu_set_power_domain(0 + cpu, true);
  113. if (ret < 0)
  114. return ret;
  115. if (read_cpuid_part() != ARM_CPU_PART_CORTEX_A9) {
  116. /*
  117. * We communicate with the bootrom to active the cpus other
  118. * than cpu0, after a blob of initialize code, they will
  119. * stay at wfe state, once they are actived, they will check
  120. * the mailbox:
  121. * sram_base_addr + 4: 0xdeadbeaf
  122. * sram_base_addr + 8: start address for pc
  123. * The cpu0 need to wait the other cpus other than cpu0 entering
  124. * the wfe state.The wait time is affected by many aspects.
  125. * (e.g: cpu frequency, bootrom frequency, sram frequency, ...)
  126. */
  127. mdelay(1); /* ensure the cpus other than cpu0 to startup */
  128. writel(virt_to_phys(secondary_startup), sram_base_addr + 8);
  129. writel(0xDEADBEAF, sram_base_addr + 4);
  130. dsb_sev();
  131. }
  132. return 0;
  133. }
  134. /**
  135. * rockchip_smp_prepare_sram - populate necessary sram block
  136. * Starting cores execute the code residing at the start of the on-chip sram
  137. * after power-on. Therefore make sure, this sram region is reserved and
  138. * big enough. After this check, copy the trampoline code that directs the
  139. * core to the real startup code in ram into the sram-region.
  140. * @node: mmio-sram device node
  141. */
  142. static int __init rockchip_smp_prepare_sram(struct device_node *node)
  143. {
  144. unsigned int trampoline_sz = &rockchip_secondary_trampoline_end -
  145. &rockchip_secondary_trampoline;
  146. struct resource res;
  147. unsigned int rsize;
  148. int ret;
  149. ret = of_address_to_resource(node, 0, &res);
  150. if (ret < 0) {
  151. pr_err("%s: could not get address for node %s\n",
  152. __func__, node->full_name);
  153. return ret;
  154. }
  155. rsize = resource_size(&res);
  156. if (rsize < trampoline_sz) {
  157. pr_err("%s: reserved block with size 0x%x is to small for trampoline size 0x%x\n",
  158. __func__, rsize, trampoline_sz);
  159. return -EINVAL;
  160. }
  161. /* set the boot function for the sram code */
  162. rockchip_boot_fn = virt_to_phys(secondary_startup);
  163. /* copy the trampoline to sram, that runs during startup of the core */
  164. memcpy(sram_base_addr, &rockchip_secondary_trampoline, trampoline_sz);
  165. flush_cache_all();
  166. outer_clean_range(0, trampoline_sz);
  167. dsb_sev();
  168. return 0;
  169. }
  170. static const struct regmap_config rockchip_pmu_regmap_config = {
  171. .reg_bits = 32,
  172. .val_bits = 32,
  173. .reg_stride = 4,
  174. };
  175. static int __init rockchip_smp_prepare_pmu(void)
  176. {
  177. struct device_node *node;
  178. void __iomem *pmu_base;
  179. /*
  180. * This function is only called via smp_ops->smp_prepare_cpu().
  181. * That only happens if a "/cpus" device tree node exists
  182. * and has an "enable-method" property that selects the SMP
  183. * operations defined herein.
  184. */
  185. node = of_find_node_by_path("/cpus");
  186. pmu = syscon_regmap_lookup_by_phandle(node, "rockchip,pmu");
  187. of_node_put(node);
  188. if (!IS_ERR(pmu))
  189. return 0;
  190. pmu = syscon_regmap_lookup_by_compatible("rockchip,rk3066-pmu");
  191. if (!IS_ERR(pmu))
  192. return 0;
  193. /* fallback, create our own regmap for the pmu area */
  194. pmu = NULL;
  195. node = of_find_compatible_node(NULL, NULL, "rockchip,rk3066-pmu");
  196. if (!node) {
  197. pr_err("%s: could not find pmu dt node\n", __func__);
  198. return -ENODEV;
  199. }
  200. pmu_base = of_iomap(node, 0);
  201. if (!pmu_base) {
  202. pr_err("%s: could not map pmu registers\n", __func__);
  203. return -ENOMEM;
  204. }
  205. pmu = regmap_init_mmio(NULL, pmu_base, &rockchip_pmu_regmap_config);
  206. if (IS_ERR(pmu)) {
  207. int ret = PTR_ERR(pmu);
  208. iounmap(pmu_base);
  209. pmu = NULL;
  210. pr_err("%s: regmap init failed\n", __func__);
  211. return ret;
  212. }
  213. return 0;
  214. }
  215. static void __init rockchip_smp_prepare_cpus(unsigned int max_cpus)
  216. {
  217. struct device_node *node;
  218. unsigned int i;
  219. node = of_find_compatible_node(NULL, NULL, "rockchip,rk3066-smp-sram");
  220. if (!node) {
  221. pr_err("%s: could not find sram dt node\n", __func__);
  222. return;
  223. }
  224. sram_base_addr = of_iomap(node, 0);
  225. if (!sram_base_addr) {
  226. pr_err("%s: could not map sram registers\n", __func__);
  227. return;
  228. }
  229. if (rockchip_smp_prepare_pmu())
  230. return;
  231. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
  232. if (rockchip_smp_prepare_sram(node))
  233. return;
  234. /* enable the SCU power domain */
  235. pmu_set_power_domain(PMU_PWRDN_SCU, true);
  236. node = of_find_compatible_node(NULL, NULL, "arm,cortex-a9-scu");
  237. if (!node) {
  238. pr_err("%s: missing scu\n", __func__);
  239. return;
  240. }
  241. scu_base_addr = of_iomap(node, 0);
  242. if (!scu_base_addr) {
  243. pr_err("%s: could not map scu registers\n", __func__);
  244. return;
  245. }
  246. /*
  247. * While the number of cpus is gathered from dt, also get the
  248. * number of cores from the scu to verify this value when
  249. * booting the cores.
  250. */
  251. ncores = scu_get_core_count(scu_base_addr);
  252. pr_err("%s: ncores %d\n", __func__, ncores);
  253. scu_enable(scu_base_addr);
  254. } else {
  255. unsigned int l2ctlr;
  256. asm ("mrc p15, 1, %0, c9, c0, 2\n" : "=r" (l2ctlr));
  257. ncores = ((l2ctlr >> 24) & 0x3) + 1;
  258. }
  259. /* Make sure that all cores except the first are really off */
  260. for (i = 1; i < ncores; i++)
  261. pmu_set_power_domain(0 + i, false);
  262. }
  263. #ifdef CONFIG_HOTPLUG_CPU
  264. static int rockchip_cpu_kill(unsigned int cpu)
  265. {
  266. /*
  267. * We need a delay here to ensure that the dying CPU can finish
  268. * executing v7_coherency_exit() and reach the WFI/WFE state
  269. * prior to having the power domain disabled.
  270. */
  271. mdelay(1);
  272. pmu_set_power_domain(0 + cpu, false);
  273. return 1;
  274. }
  275. static void rockchip_cpu_die(unsigned int cpu)
  276. {
  277. v7_exit_coherency_flush(louis);
  278. while (1)
  279. cpu_do_idle();
  280. }
  281. #endif
  282. static struct smp_operations rockchip_smp_ops __initdata = {
  283. .smp_prepare_cpus = rockchip_smp_prepare_cpus,
  284. .smp_boot_secondary = rockchip_boot_secondary,
  285. #ifdef CONFIG_HOTPLUG_CPU
  286. .cpu_kill = rockchip_cpu_kill,
  287. .cpu_die = rockchip_cpu_die,
  288. #endif
  289. };
  290. CPU_METHOD_OF_DECLARE(rk3066_smp, "rockchip,rk3066-smp", &rockchip_smp_ops);