firmware.c 6.1 KB

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