firmware.c 5.7 KB

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