firmware.c 6.0 KB

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