firmware.c 5.0 KB

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