mcpm-exynos.c 7.5 KB

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