platsmp.c 9.1 KB

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