firmware.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/suspend.h>
  19. #include <mach/map.h>
  20. #include "common.h"
  21. #include "smc.h"
  22. #define EXYNOS_SLEEP_MAGIC 0x00000bad
  23. #define EXYNOS_AFTR_MAGIC 0xfcba0d10
  24. #define EXYNOS_BOOT_ADDR 0x8
  25. #define EXYNOS_BOOT_FLAG 0xc
  26. static void exynos_save_cp15(void)
  27. {
  28. /* Save Power control and Diagnostic registers */
  29. asm ("mrc p15, 0, %0, c15, c0, 0\n"
  30. "mrc p15, 0, %1, c15, c0, 1\n"
  31. : "=r" (cp15_save_power), "=r" (cp15_save_diag)
  32. : : "cc");
  33. }
  34. static int exynos_do_idle(unsigned long mode)
  35. {
  36. switch (mode) {
  37. case FW_DO_IDLE_AFTR:
  38. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  39. exynos_save_cp15();
  40. __raw_writel(virt_to_phys(exynos_cpu_resume_ns),
  41. sysram_ns_base_addr + 0x24);
  42. __raw_writel(EXYNOS_AFTR_MAGIC, sysram_ns_base_addr + 0x20);
  43. exynos_smc(SMC_CMD_CPU0AFTR, 0, 0, 0);
  44. break;
  45. case FW_DO_IDLE_SLEEP:
  46. exynos_smc(SMC_CMD_SLEEP, 0, 0, 0);
  47. }
  48. return 0;
  49. }
  50. static int exynos_cpu_boot(int cpu)
  51. {
  52. /*
  53. * Exynos3250 doesn't need to send smc command for secondary CPU boot
  54. * because Exynos3250 removes WFE in secure mode.
  55. */
  56. if (soc_is_exynos3250())
  57. return 0;
  58. /*
  59. * The second parameter of SMC_CMD_CPU1BOOT command means CPU id.
  60. * But, Exynos4212 has only one secondary CPU so second parameter
  61. * isn't used for informing secure firmware about CPU id.
  62. */
  63. if (soc_is_exynos4212())
  64. cpu = 0;
  65. exynos_smc(SMC_CMD_CPU1BOOT, cpu, 0, 0);
  66. return 0;
  67. }
  68. static int exynos_set_cpu_boot_addr(int cpu, unsigned long boot_addr)
  69. {
  70. void __iomem *boot_reg;
  71. if (!sysram_ns_base_addr)
  72. return -ENODEV;
  73. boot_reg = sysram_ns_base_addr + 0x1c;
  74. /*
  75. * Almost all Exynos-series of SoCs that run in secure mode don't need
  76. * additional offset for every CPU, with Exynos4412 being the only
  77. * exception.
  78. */
  79. if (soc_is_exynos4412())
  80. boot_reg += 4 * cpu;
  81. __raw_writel(boot_addr, boot_reg);
  82. return 0;
  83. }
  84. static int exynos_cpu_suspend(unsigned long arg)
  85. {
  86. flush_cache_all();
  87. outer_flush_all();
  88. exynos_smc(SMC_CMD_SLEEP, 0, 0, 0);
  89. pr_info("Failed to suspend the system\n");
  90. writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  91. return 1;
  92. }
  93. static int exynos_suspend(void)
  94. {
  95. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  96. exynos_save_cp15();
  97. writel(EXYNOS_SLEEP_MAGIC, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  98. writel(virt_to_phys(exynos_cpu_resume_ns),
  99. sysram_ns_base_addr + EXYNOS_BOOT_ADDR);
  100. return cpu_suspend(0, exynos_cpu_suspend);
  101. }
  102. static int exynos_resume(void)
  103. {
  104. writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG);
  105. return 0;
  106. }
  107. static const struct firmware_ops exynos_firmware_ops = {
  108. .do_idle = IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND) ? exynos_do_idle : NULL,
  109. .set_cpu_boot_addr = exynos_set_cpu_boot_addr,
  110. .cpu_boot = exynos_cpu_boot,
  111. .suspend = IS_ENABLED(CONFIG_PM_SLEEP) ? exynos_suspend : NULL,
  112. .resume = IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND) ? exynos_resume : NULL,
  113. };
  114. void __init exynos_firmware_init(void)
  115. {
  116. struct device_node *nd;
  117. const __be32 *addr;
  118. nd = of_find_compatible_node(NULL, NULL,
  119. "samsung,secure-firmware");
  120. if (!nd)
  121. return;
  122. addr = of_get_address(nd, 0, NULL, NULL);
  123. if (!addr) {
  124. pr_err("%s: No address specified.\n", __func__);
  125. return;
  126. }
  127. pr_info("Running under secure firmware.\n");
  128. register_firmware_ops(&exynos_firmware_ops);
  129. }