mcpm-exynos.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2014 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * arch/arm/mach-exynos/mcpm-exynos.c
  6. *
  7. * Based on arch/arm/mach-vexpress/dcscb.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/arm-cci.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/of_address.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/soc/samsung/exynos-regs-pmu.h>
  19. #include <asm/cputype.h>
  20. #include <asm/cp15.h>
  21. #include <asm/mcpm.h>
  22. #include <asm/smp_plat.h>
  23. #include "common.h"
  24. #define EXYNOS5420_CPUS_PER_CLUSTER 4
  25. #define EXYNOS5420_NR_CLUSTERS 2
  26. #define EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN BIT(9)
  27. #define EXYNOS5420_USE_ARM_CORE_DOWN_STATE BIT(29)
  28. #define EXYNOS5420_USE_L2_COMMON_UP_STATE BIT(30)
  29. static void __iomem *ns_sram_base_addr;
  30. /*
  31. * The common v7_exit_coherency_flush API could not be used because of the
  32. * Erratum 799270 workaround. This macro is the same as the common one (in
  33. * arch/arm/include/asm/cacheflush.h) except for the erratum handling.
  34. */
  35. #define exynos_v7_exit_coherency_flush(level) \
  36. asm volatile( \
  37. "stmfd sp!, {fp, ip}\n\t"\
  38. "mrc p15, 0, r0, c1, c0, 0 @ get SCTLR\n\t" \
  39. "bic r0, r0, #"__stringify(CR_C)"\n\t" \
  40. "mcr p15, 0, r0, c1, c0, 0 @ set SCTLR\n\t" \
  41. "isb\n\t"\
  42. "bl v7_flush_dcache_"__stringify(level)"\n\t" \
  43. "mrc p15, 0, r0, c1, c0, 1 @ get ACTLR\n\t" \
  44. "bic r0, r0, #(1 << 6) @ disable local coherency\n\t" \
  45. /* Dummy Load of a device register to avoid Erratum 799270 */ \
  46. "ldr r4, [%0]\n\t" \
  47. "and r4, r4, #0\n\t" \
  48. "orr r0, r0, r4\n\t" \
  49. "mcr p15, 0, r0, c1, c0, 1 @ set ACTLR\n\t" \
  50. "isb\n\t" \
  51. "dsb\n\t" \
  52. "ldmfd sp!, {fp, ip}" \
  53. : \
  54. : "Ir" (pmu_base_addr + S5P_INFORM0) \
  55. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", \
  56. "r9", "r10", "lr", "memory")
  57. static int exynos_cpu_powerup(unsigned int cpu, unsigned int cluster)
  58. {
  59. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  60. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  61. if (cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  62. cluster >= EXYNOS5420_NR_CLUSTERS)
  63. return -EINVAL;
  64. if (!exynos_cpu_power_state(cpunr)) {
  65. exynos_cpu_power_up(cpunr);
  66. /*
  67. * This assumes the cluster number of the big cores(Cortex A15)
  68. * is 0 and the Little cores(Cortex A7) is 1.
  69. * When the system was booted from the Little core,
  70. * they should be reset during power up cpu.
  71. */
  72. if (cluster &&
  73. cluster == MPIDR_AFFINITY_LEVEL(cpu_logical_map(0), 1)) {
  74. /*
  75. * Before we reset the Little cores, we should wait
  76. * the SPARE2 register is set to 1 because the init
  77. * codes of the iROM will set the register after
  78. * initialization.
  79. */
  80. while (!pmu_raw_readl(S5P_PMU_SPARE2))
  81. udelay(10);
  82. pmu_raw_writel(EXYNOS5420_KFC_CORE_RESET(cpu),
  83. EXYNOS_SWRESET);
  84. }
  85. }
  86. return 0;
  87. }
  88. static int exynos_cluster_powerup(unsigned int cluster)
  89. {
  90. pr_debug("%s: cluster %u\n", __func__, cluster);
  91. if (cluster >= EXYNOS5420_NR_CLUSTERS)
  92. return -EINVAL;
  93. exynos_cluster_power_up(cluster);
  94. return 0;
  95. }
  96. static void exynos_cpu_powerdown_prepare(unsigned int cpu, unsigned int cluster)
  97. {
  98. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  99. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  100. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  101. cluster >= EXYNOS5420_NR_CLUSTERS);
  102. exynos_cpu_power_down(cpunr);
  103. }
  104. static void exynos_cluster_powerdown_prepare(unsigned int cluster)
  105. {
  106. pr_debug("%s: cluster %u\n", __func__, cluster);
  107. BUG_ON(cluster >= EXYNOS5420_NR_CLUSTERS);
  108. exynos_cluster_power_down(cluster);
  109. }
  110. static void exynos_cpu_cache_disable(void)
  111. {
  112. /* Disable and flush the local CPU cache. */
  113. exynos_v7_exit_coherency_flush(louis);
  114. }
  115. static void exynos_cluster_cache_disable(void)
  116. {
  117. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
  118. /*
  119. * On the Cortex-A15 we need to disable
  120. * L2 prefetching before flushing the cache.
  121. */
  122. asm volatile(
  123. "mcr p15, 1, %0, c15, c0, 3\n\t"
  124. "isb\n\t"
  125. "dsb"
  126. : : "r" (0x400));
  127. }
  128. /* Flush all cache levels for this cluster. */
  129. exynos_v7_exit_coherency_flush(all);
  130. /*
  131. * Disable cluster-level coherency by masking
  132. * incoming snoops and DVM messages:
  133. */
  134. cci_disable_port_by_cpu(read_cpuid_mpidr());
  135. }
  136. static int exynos_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
  137. {
  138. unsigned int tries = 100;
  139. unsigned int cpunr = cpu + (cluster * EXYNOS5420_CPUS_PER_CLUSTER);
  140. pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
  141. BUG_ON(cpu >= EXYNOS5420_CPUS_PER_CLUSTER ||
  142. cluster >= EXYNOS5420_NR_CLUSTERS);
  143. /* Wait for the core state to be OFF */
  144. while (tries--) {
  145. if ((exynos_cpu_power_state(cpunr) == 0))
  146. return 0; /* success: the CPU is halted */
  147. /* Otherwise, wait and retry: */
  148. msleep(1);
  149. }
  150. return -ETIMEDOUT; /* timeout */
  151. }
  152. static void exynos_cpu_is_up(unsigned int cpu, unsigned int cluster)
  153. {
  154. /* especially when resuming: make sure power control is set */
  155. exynos_cpu_powerup(cpu, cluster);
  156. }
  157. static const struct mcpm_platform_ops exynos_power_ops = {
  158. .cpu_powerup = exynos_cpu_powerup,
  159. .cluster_powerup = exynos_cluster_powerup,
  160. .cpu_powerdown_prepare = exynos_cpu_powerdown_prepare,
  161. .cluster_powerdown_prepare = exynos_cluster_powerdown_prepare,
  162. .cpu_cache_disable = exynos_cpu_cache_disable,
  163. .cluster_cache_disable = exynos_cluster_cache_disable,
  164. .wait_for_powerdown = exynos_wait_for_powerdown,
  165. .cpu_is_up = exynos_cpu_is_up,
  166. };
  167. /*
  168. * Enable cluster-level coherency, in preparation for turning on the MMU.
  169. */
  170. static void __naked exynos_pm_power_up_setup(unsigned int affinity_level)
  171. {
  172. asm volatile ("\n"
  173. "cmp r0, #1\n"
  174. "bxne lr\n"
  175. "b cci_enable_port_for_self");
  176. }
  177. static const struct of_device_id exynos_dt_mcpm_match[] = {
  178. { .compatible = "samsung,exynos5420" },
  179. { .compatible = "samsung,exynos5800" },
  180. {},
  181. };
  182. static void exynos_mcpm_setup_entry_point(void)
  183. {
  184. /*
  185. * U-Boot SPL is hardcoded to jump to the start of ns_sram_base_addr
  186. * as part of secondary_cpu_start(). Let's redirect it to the
  187. * mcpm_entry_point(). This is done during both secondary boot-up as
  188. * well as system resume.
  189. */
  190. __raw_writel(0xe59f0000, ns_sram_base_addr); /* ldr r0, [pc, #0] */
  191. __raw_writel(0xe12fff10, ns_sram_base_addr + 4); /* bx r0 */
  192. __raw_writel(virt_to_phys(mcpm_entry_point), ns_sram_base_addr + 8);
  193. }
  194. static struct syscore_ops exynos_mcpm_syscore_ops = {
  195. .resume = exynos_mcpm_setup_entry_point,
  196. };
  197. static int __init exynos_mcpm_init(void)
  198. {
  199. struct device_node *node;
  200. unsigned int value, i;
  201. int ret;
  202. node = of_find_matching_node(NULL, exynos_dt_mcpm_match);
  203. if (!node)
  204. return -ENODEV;
  205. of_node_put(node);
  206. if (!cci_probed())
  207. return -ENODEV;
  208. node = of_find_compatible_node(NULL, NULL,
  209. "samsung,exynos4210-sysram-ns");
  210. if (!node)
  211. return -ENODEV;
  212. ns_sram_base_addr = of_iomap(node, 0);
  213. of_node_put(node);
  214. if (!ns_sram_base_addr) {
  215. pr_err("failed to map non-secure iRAM base address\n");
  216. return -ENOMEM;
  217. }
  218. /*
  219. * To increase the stability of KFC reset we need to program
  220. * the PMU SPARE3 register
  221. */
  222. pmu_raw_writel(EXYNOS5420_SWRESET_KFC_SEL, S5P_PMU_SPARE3);
  223. ret = mcpm_platform_register(&exynos_power_ops);
  224. if (!ret)
  225. ret = mcpm_sync_init(exynos_pm_power_up_setup);
  226. if (!ret)
  227. ret = mcpm_loopback(exynos_cluster_cache_disable); /* turn on the CCI */
  228. if (ret) {
  229. iounmap(ns_sram_base_addr);
  230. return ret;
  231. }
  232. mcpm_smp_set_ops();
  233. pr_info("Exynos MCPM support installed\n");
  234. /*
  235. * On Exynos5420/5800 for the A15 and A7 clusters:
  236. *
  237. * EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN ensures that all the cores
  238. * in a cluster are turned off before turning off the cluster L2.
  239. *
  240. * EXYNOS5420_USE_ARM_CORE_DOWN_STATE ensures that a cores is powered
  241. * off before waking it up.
  242. *
  243. * EXYNOS5420_USE_L2_COMMON_UP_STATE ensures that cluster L2 will be
  244. * turned on before the first man is powered up.
  245. */
  246. for (i = 0; i < EXYNOS5420_NR_CLUSTERS; i++) {
  247. value = pmu_raw_readl(EXYNOS_COMMON_OPTION(i));
  248. value |= EXYNOS5420_ENABLE_AUTOMATIC_CORE_DOWN |
  249. EXYNOS5420_USE_ARM_CORE_DOWN_STATE |
  250. EXYNOS5420_USE_L2_COMMON_UP_STATE;
  251. pmu_raw_writel(value, EXYNOS_COMMON_OPTION(i));
  252. }
  253. exynos_mcpm_setup_entry_point();
  254. register_syscore_ops(&exynos_mcpm_syscore_ops);
  255. return ret;
  256. }
  257. early_initcall(exynos_mcpm_init);