firmware.c 6.1 KB

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