firmware.c 6.0 KB

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