firmware.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2012 Samsung Electronics.
  4. // Kyungmin Park <[email protected]>
  5. // Tomasz Figa <[email protected]>
  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. * On Exynos5 devices the call is ignored by trustzone firmware.
  58. */
  59. if (!soc_is_exynos4210() && !soc_is_exynos4412())
  60. return 0;
  61. /*
  62. * The second parameter of SMC_CMD_CPU1BOOT command means CPU id.
  63. */
  64. exynos_smc(SMC_CMD_CPU1BOOT, cpu, 0, 0);
  65. return 0;
  66. }
  67. static int exynos_set_cpu_boot_addr(int cpu, unsigned long boot_addr)
  68. {
  69. void __iomem *boot_reg;
  70. if (!sysram_ns_base_addr)
  71. return -ENODEV;
  72. boot_reg = sysram_ns_base_addr + 0x1c;
  73. /*
  74. * Almost all Exynos-series of SoCs that run in secure mode don't need
  75. * additional offset for every CPU, with Exynos4412 being the only
  76. * exception.
  77. */
  78. if (soc_is_exynos4412())
  79. boot_reg += 4 * cpu;
  80. writel_relaxed(boot_addr, boot_reg);
  81. return 0;
  82. }
  83. static int exynos_get_cpu_boot_addr(int cpu, unsigned long *boot_addr)
  84. {
  85. void __iomem *boot_reg;
  86. if (!sysram_ns_base_addr)
  87. return -ENODEV;
  88. boot_reg = sysram_ns_base_addr + 0x1c;
  89. if (soc_is_exynos4412())
  90. boot_reg += 4 * cpu;
  91. *boot_addr = readl_relaxed(boot_reg);
  92. return 0;
  93. }
  94. static int exynos_cpu_suspend(unsigned long arg)
  95. {
  96. flush_cache_all();
  97. outer_flush_all();
  98. exynos_smc(SMC_CMD_SLEEP, 0, 0, 0);
  99. pr_info("Failed to suspend the system\n");
  100. writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  101. return 1;
  102. }
  103. static int exynos_suspend(void)
  104. {
  105. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  106. exynos_save_cp15();
  107. writel(EXYNOS_SLEEP_MAGIC, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  108. writel(__pa_symbol(exynos_cpu_resume_ns),
  109. sysram_ns_base_addr + EXYNOS_BOOT_ADDR);
  110. return cpu_suspend(0, exynos_cpu_suspend);
  111. }
  112. static int exynos_resume(void)
  113. {
  114. writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  115. return 0;
  116. }
  117. static const struct firmware_ops exynos_firmware_ops = {
  118. .do_idle = IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND) ? exynos_do_idle : NULL,
  119. .set_cpu_boot_addr = exynos_set_cpu_boot_addr,
  120. .get_cpu_boot_addr = exynos_get_cpu_boot_addr,
  121. .cpu_boot = exynos_cpu_boot,
  122. .suspend = IS_ENABLED(CONFIG_PM_SLEEP) ? exynos_suspend : NULL,
  123. .resume = IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND) ? exynos_resume : NULL,
  124. };
  125. static void exynos_l2_write_sec(unsigned long val, unsigned reg)
  126. {
  127. static int l2cache_enabled;
  128. switch (reg) {
  129. case L2X0_CTRL:
  130. if (val & L2X0_CTRL_EN) {
  131. /*
  132. * Before the cache can be enabled, due to firmware
  133. * design, SMC_CMD_L2X0INVALL must be called.
  134. */
  135. if (!l2cache_enabled) {
  136. exynos_smc(SMC_CMD_L2X0INVALL, 0, 0, 0);
  137. l2cache_enabled = 1;
  138. }
  139. } else {
  140. l2cache_enabled = 0;
  141. }
  142. exynos_smc(SMC_CMD_L2X0CTRL, val, 0, 0);
  143. break;
  144. case L2X0_DEBUG_CTRL:
  145. exynos_smc(SMC_CMD_L2X0DEBUG, val, 0, 0);
  146. break;
  147. default:
  148. WARN_ONCE(1, "%s: ignoring write to reg 0x%x\n", __func__, reg);
  149. }
  150. }
  151. static void exynos_l2_configure(const struct l2x0_regs *regs)
  152. {
  153. exynos_smc(SMC_CMD_L2X0SETUP1, regs->tag_latency, regs->data_latency,
  154. regs->prefetch_ctrl);
  155. exynos_smc(SMC_CMD_L2X0SETUP2, regs->pwr_ctrl, regs->aux_ctrl, 0);
  156. }
  157. bool __init exynos_secure_firmware_available(void)
  158. {
  159. struct device_node *nd;
  160. const __be32 *addr;
  161. nd = of_find_compatible_node(NULL, NULL,
  162. "samsung,secure-firmware");
  163. if (!nd)
  164. return false;
  165. addr = of_get_address(nd, 0, NULL, NULL);
  166. of_node_put(nd);
  167. if (!addr) {
  168. pr_err("%s: No address specified.\n", __func__);
  169. return false;
  170. }
  171. return true;
  172. }
  173. void __init exynos_firmware_init(void)
  174. {
  175. if (!exynos_secure_firmware_available())
  176. return;
  177. pr_info("Running under secure firmware.\n");
  178. register_firmware_ops(&exynos_firmware_ops);
  179. /*
  180. * Exynos 4 SoCs (based on Cortex A9 and equipped with L2C-310),
  181. * running under secure firmware, require certain registers of L2
  182. * cache controller to be written in secure mode. Here .write_sec
  183. * callback is provided to perform necessary SMC calls.
  184. */
  185. if (IS_ENABLED(CONFIG_CACHE_L2X0) &&
  186. read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
  187. outer_cache.write_sec = exynos_l2_write_sec;
  188. outer_cache.configure = exynos_l2_configure;
  189. }
  190. }
  191. #define REG_CPU_STATE_ADDR (sysram_ns_base_addr + 0x28)
  192. #define BOOT_MODE_MASK 0x1f
  193. void exynos_set_boot_flag(unsigned int cpu, unsigned int mode)
  194. {
  195. unsigned int tmp;
  196. tmp = readl_relaxed(REG_CPU_STATE_ADDR + cpu * 4);
  197. if (mode & BOOT_MODE_MASK)
  198. tmp &= ~BOOT_MODE_MASK;
  199. tmp |= mode;
  200. writel_relaxed(tmp, REG_CPU_STATE_ADDR + cpu * 4);
  201. }
  202. void exynos_clear_boot_flag(unsigned int cpu, unsigned int mode)
  203. {
  204. unsigned int tmp;
  205. tmp = readl_relaxed(REG_CPU_STATE_ADDR + cpu * 4);
  206. tmp &= ~mode;
  207. writel_relaxed(tmp, REG_CPU_STATE_ADDR + cpu * 4);
  208. }