firmware.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2012 Samsung Electronics.
  4. // Kyungmin Park <kyungmin.park@samsung.com>
  5. // Tomasz Figa <t.figa@samsung.com>
  6. #include <linux/kernel.h>
  7. #include <linux/io.h>
  8. #include <linux/init.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <asm/cacheflush.h>
  12. #include <asm/cputype.h>
  13. #include <asm/firmware.h>
  14. #include <asm/hardware/cache-l2x0.h>
  15. #include <asm/suspend.h>
  16. #include "common.h"
  17. #include "smc.h"
  18. #define EXYNOS_BOOT_ADDR 0x8
  19. #define EXYNOS_BOOT_FLAG 0xc
  20. static void exynos_save_cp15(void)
  21. {
  22. /* Save Power control and Diagnostic registers */
  23. asm ("mrc p15, 0, %0, c15, c0, 0\n"
  24. "mrc p15, 0, %1, c15, c0, 1\n"
  25. : "=r" (cp15_save_power), "=r" (cp15_save_diag)
  26. : : "cc");
  27. }
  28. static int exynos_do_idle(unsigned long mode)
  29. {
  30. switch (mode) {
  31. case FW_DO_IDLE_AFTR:
  32. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  33. exynos_save_cp15();
  34. writel_relaxed(__pa_symbol(exynos_cpu_resume_ns),
  35. sysram_ns_base_addr + 0x24);
  36. writel_relaxed(EXYNOS_AFTR_MAGIC, sysram_ns_base_addr + 0x20);
  37. if (soc_is_exynos3250()) {
  38. flush_cache_all();
  39. exynos_smc(SMC_CMD_SAVE, OP_TYPE_CORE,
  40. SMC_POWERSTATE_IDLE, 0);
  41. exynos_smc(SMC_CMD_SHUTDOWN, OP_TYPE_CLUSTER,
  42. SMC_POWERSTATE_IDLE, 0);
  43. } else
  44. exynos_smc(SMC_CMD_CPU0AFTR, 0, 0, 0);
  45. break;
  46. case FW_DO_IDLE_SLEEP:
  47. exynos_smc(SMC_CMD_SLEEP, 0, 0, 0);
  48. }
  49. return 0;
  50. }
  51. static int exynos_cpu_boot(int cpu)
  52. {
  53. /*
  54. * Exynos3250 doesn't need to send smc command for secondary CPU boot
  55. * because Exynos3250 removes WFE in secure mode.
  56. */
  57. if (soc_is_exynos3250())
  58. return 0;
  59. /*
  60. * The second parameter of SMC_CMD_CPU1BOOT command means CPU id.
  61. */
  62. exynos_smc(SMC_CMD_CPU1BOOT, cpu, 0, 0);
  63. return 0;
  64. }
  65. static int exynos_set_cpu_boot_addr(int cpu, unsigned long boot_addr)
  66. {
  67. void __iomem *boot_reg;
  68. if (!sysram_ns_base_addr)
  69. return -ENODEV;
  70. boot_reg = sysram_ns_base_addr + 0x1c;
  71. /*
  72. * Almost all Exynos-series of SoCs that run in secure mode don't need
  73. * additional offset for every CPU, with Exynos4412 being the only
  74. * exception.
  75. */
  76. if (soc_is_exynos4412())
  77. boot_reg += 4 * cpu;
  78. writel_relaxed(boot_addr, boot_reg);
  79. return 0;
  80. }
  81. static int exynos_get_cpu_boot_addr(int cpu, unsigned long *boot_addr)
  82. {
  83. void __iomem *boot_reg;
  84. if (!sysram_ns_base_addr)
  85. return -ENODEV;
  86. boot_reg = sysram_ns_base_addr + 0x1c;
  87. if (soc_is_exynos4412())
  88. boot_reg += 4 * cpu;
  89. *boot_addr = readl_relaxed(boot_reg);
  90. return 0;
  91. }
  92. static int exynos_cpu_suspend(unsigned long arg)
  93. {
  94. flush_cache_all();
  95. outer_flush_all();
  96. exynos_smc(SMC_CMD_SLEEP, 0, 0, 0);
  97. pr_info("Failed to suspend the system\n");
  98. writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  99. return 1;
  100. }
  101. static int exynos_suspend(void)
  102. {
  103. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  104. exynos_save_cp15();
  105. writel(EXYNOS_SLEEP_MAGIC, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  106. writel(__pa_symbol(exynos_cpu_resume_ns),
  107. sysram_ns_base_addr + EXYNOS_BOOT_ADDR);
  108. return cpu_suspend(0, exynos_cpu_suspend);
  109. }
  110. static int exynos_resume(void)
  111. {
  112. writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  113. return 0;
  114. }
  115. static const struct firmware_ops exynos_firmware_ops = {
  116. .do_idle = IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND) ? exynos_do_idle : NULL,
  117. .set_cpu_boot_addr = exynos_set_cpu_boot_addr,
  118. .get_cpu_boot_addr = exynos_get_cpu_boot_addr,
  119. .cpu_boot = exynos_cpu_boot,
  120. .suspend = IS_ENABLED(CONFIG_PM_SLEEP) ? exynos_suspend : NULL,
  121. .resume = IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND) ? exynos_resume : NULL,
  122. };
  123. static void exynos_l2_write_sec(unsigned long val, unsigned reg)
  124. {
  125. static int l2cache_enabled;
  126. switch (reg) {
  127. case L2X0_CTRL:
  128. if (val & L2X0_CTRL_EN) {
  129. /*
  130. * Before the cache can be enabled, due to firmware
  131. * design, SMC_CMD_L2X0INVALL must be called.
  132. */
  133. if (!l2cache_enabled) {
  134. exynos_smc(SMC_CMD_L2X0INVALL, 0, 0, 0);
  135. l2cache_enabled = 1;
  136. }
  137. } else {
  138. l2cache_enabled = 0;
  139. }
  140. exynos_smc(SMC_CMD_L2X0CTRL, val, 0, 0);
  141. break;
  142. case L2X0_DEBUG_CTRL:
  143. exynos_smc(SMC_CMD_L2X0DEBUG, val, 0, 0);
  144. break;
  145. default:
  146. WARN_ONCE(1, "%s: ignoring write to reg 0x%x\n", __func__, reg);
  147. }
  148. }
  149. static void exynos_l2_configure(const struct l2x0_regs *regs)
  150. {
  151. exynos_smc(SMC_CMD_L2X0SETUP1, regs->tag_latency, regs->data_latency,
  152. regs->prefetch_ctrl);
  153. exynos_smc(SMC_CMD_L2X0SETUP2, regs->pwr_ctrl, regs->aux_ctrl, 0);
  154. }
  155. void __init exynos_firmware_init(void)
  156. {
  157. struct device_node *nd;
  158. const __be32 *addr;
  159. nd = of_find_compatible_node(NULL, NULL,
  160. "samsung,secure-firmware");
  161. if (!nd)
  162. return;
  163. addr = of_get_address(nd, 0, NULL, NULL);
  164. of_node_put(nd);
  165. if (!addr) {
  166. pr_err("%s: No address specified.\n", __func__);
  167. return;
  168. }
  169. pr_info("Running under secure firmware.\n");
  170. register_firmware_ops(&exynos_firmware_ops);
  171. /*
  172. * Exynos 4 SoCs (based on Cortex A9 and equipped with L2C-310),
  173. * running under secure firmware, require certain registers of L2
  174. * cache controller to be written in secure mode. Here .write_sec
  175. * callback is provided to perform necessary SMC calls.
  176. */
  177. if (IS_ENABLED(CONFIG_CACHE_L2X0) &&
  178. read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
  179. outer_cache.write_sec = exynos_l2_write_sec;
  180. outer_cache.configure = exynos_l2_configure;
  181. }
  182. }
  183. #define REG_CPU_STATE_ADDR (sysram_ns_base_addr + 0x28)
  184. #define BOOT_MODE_MASK 0x1f
  185. void exynos_set_boot_flag(unsigned int cpu, unsigned int mode)
  186. {
  187. unsigned int tmp;
  188. tmp = readl_relaxed(REG_CPU_STATE_ADDR + cpu * 4);
  189. if (mode & BOOT_MODE_MASK)
  190. tmp &= ~BOOT_MODE_MASK;
  191. tmp |= mode;
  192. writel_relaxed(tmp, REG_CPU_STATE_ADDR + cpu * 4);
  193. }
  194. void exynos_clear_boot_flag(unsigned int cpu, unsigned int mode)
  195. {
  196. unsigned int tmp;
  197. tmp = readl_relaxed(REG_CPU_STATE_ADDR + cpu * 4);
  198. tmp &= ~mode;
  199. writel_relaxed(tmp, REG_CPU_STATE_ADDR + cpu * 4);
  200. }