Krzysztof Kozlowski | 347863d | 2017-12-25 20:54:31 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // Copyright (C) 2012 Samsung Electronics. |
| 4 | // Kyungmin Park <kyungmin.park@samsung.com> |
| 5 | // Tomasz Figa <t.figa@samsung.com> |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/io.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/of.h> |
| 11 | #include <linux/of_address.h> |
| 12 | |
Tomasz Figa | 2b9d9c3 | 2014-09-24 01:24:39 +0900 | [diff] [blame] | 13 | #include <asm/cacheflush.h> |
| 14 | #include <asm/cputype.h> |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 15 | #include <asm/firmware.h> |
Tomasz Figa | 5445b64 | 2015-01-08 07:53:20 +0100 | [diff] [blame] | 16 | #include <asm/hardware/cache-l2x0.h> |
Tomasz Figa | 2b9d9c3 | 2014-09-24 01:24:39 +0900 | [diff] [blame] | 17 | #include <asm/suspend.h> |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 18 | |
Sachin Kamat | b3205de | 2014-05-13 07:13:44 +0900 | [diff] [blame] | 19 | #include "common.h" |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 20 | #include "smc.h" |
| 21 | |
Tomasz Figa | 2b9d9c3 | 2014-09-24 01:24:39 +0900 | [diff] [blame] | 22 | #define EXYNOS_BOOT_ADDR 0x8 |
| 23 | #define EXYNOS_BOOT_FLAG 0xc |
| 24 | |
Bartlomiej Zolnierkiewicz | a135e20 | 2014-09-25 17:59:41 +0900 | [diff] [blame] | 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 | |
Bartlomiej Zolnierkiewicz | 0b7778a | 2014-09-25 17:59:41 +0900 | [diff] [blame] | 34 | static int exynos_do_idle(unsigned long mode) |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 35 | { |
Bartlomiej Zolnierkiewicz | 0b7778a | 2014-09-25 17:59:41 +0900 | [diff] [blame] | 36 | switch (mode) { |
| 37 | case FW_DO_IDLE_AFTR: |
Bartlomiej Zolnierkiewicz | a135e20 | 2014-09-25 17:59:41 +0900 | [diff] [blame] | 38 | if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) |
| 39 | exynos_save_cp15(); |
Florian Fainelli | 64fc2a9 | 2017-01-15 03:59:29 +0100 | [diff] [blame] | 40 | writel_relaxed(__pa_symbol(exynos_cpu_resume_ns), |
Ben Dooks | 458ad21 | 2016-06-21 11:20:24 +0100 | [diff] [blame] | 41 | sysram_ns_base_addr + 0x24); |
| 42 | writel_relaxed(EXYNOS_AFTR_MAGIC, sysram_ns_base_addr + 0x20); |
Bartlomiej Zolnierkiewicz | 8936640 | 2015-03-27 02:35:48 +0900 | [diff] [blame] | 43 | if (soc_is_exynos3250()) { |
Bartlomiej Zolnierkiewicz | af99711 | 2015-03-18 14:09:57 +0100 | [diff] [blame] | 44 | flush_cache_all(); |
Bartlomiej Zolnierkiewicz | 8936640 | 2015-03-27 02:35:48 +0900 | [diff] [blame] | 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); |
Bartlomiej Zolnierkiewicz | 0b7778a | 2014-09-25 17:59:41 +0900 | [diff] [blame] | 51 | break; |
| 52 | case FW_DO_IDLE_SLEEP: |
| 53 | exynos_smc(SMC_CMD_SLEEP, 0, 0, 0); |
| 54 | } |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int exynos_cpu_boot(int cpu) |
| 59 | { |
Kyungmin Park | 989ff3f | 2014-05-09 06:19:18 +0900 | [diff] [blame] | 60 | /* |
Chanwoo Choi | 6457158 | 2014-05-26 04:12:32 +0900 | [diff] [blame] | 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 | /* |
Kyungmin Park | 989ff3f | 2014-05-09 06:19:18 +0900 | [diff] [blame] | 68 | * The second parameter of SMC_CMD_CPU1BOOT command means CPU id. |
Kyungmin Park | 989ff3f | 2014-05-09 06:19:18 +0900 | [diff] [blame] | 69 | */ |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 70 | exynos_smc(SMC_CMD_CPU1BOOT, cpu, 0, 0); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int exynos_set_cpu_boot_addr(int cpu, unsigned long boot_addr) |
| 75 | { |
Sachin Kamat | b3205de | 2014-05-13 07:13:44 +0900 | [diff] [blame] | 76 | void __iomem *boot_reg; |
| 77 | |
| 78 | if (!sysram_ns_base_addr) |
| 79 | return -ENODEV; |
| 80 | |
Olof Johansson | fe388fa | 2014-05-30 21:44:32 -0700 | [diff] [blame] | 81 | boot_reg = sysram_ns_base_addr + 0x1c; |
Kyungmin Park | 989ff3f | 2014-05-09 06:19:18 +0900 | [diff] [blame] | 82 | |
Sachin Kamat | 35e7564 | 2014-07-08 08:03:49 +0900 | [diff] [blame] | 83 | /* |
| 84 | * Almost all Exynos-series of SoCs that run in secure mode don't need |
| 85 | * additional offset for every CPU, with Exynos4412 being the only |
| 86 | * exception. |
| 87 | */ |
| 88 | if (soc_is_exynos4412()) |
| 89 | boot_reg += 4 * cpu; |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 90 | |
Ben Dooks | 458ad21 | 2016-06-21 11:20:24 +0100 | [diff] [blame] | 91 | writel_relaxed(boot_addr, boot_reg); |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Bartlomiej Zolnierkiewicz | 1225ad7 | 2015-03-18 14:09:56 +0100 | [diff] [blame] | 95 | static int exynos_get_cpu_boot_addr(int cpu, unsigned long *boot_addr) |
| 96 | { |
| 97 | void __iomem *boot_reg; |
| 98 | |
| 99 | if (!sysram_ns_base_addr) |
| 100 | return -ENODEV; |
| 101 | |
| 102 | boot_reg = sysram_ns_base_addr + 0x1c; |
| 103 | |
| 104 | if (soc_is_exynos4412()) |
| 105 | boot_reg += 4 * cpu; |
| 106 | |
Ben Dooks | 458ad21 | 2016-06-21 11:20:24 +0100 | [diff] [blame] | 107 | *boot_addr = readl_relaxed(boot_reg); |
Bartlomiej Zolnierkiewicz | 1225ad7 | 2015-03-18 14:09:56 +0100 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Tomasz Figa | 2b9d9c3 | 2014-09-24 01:24:39 +0900 | [diff] [blame] | 111 | static int exynos_cpu_suspend(unsigned long arg) |
| 112 | { |
| 113 | flush_cache_all(); |
| 114 | outer_flush_all(); |
| 115 | |
| 116 | exynos_smc(SMC_CMD_SLEEP, 0, 0, 0); |
| 117 | |
| 118 | pr_info("Failed to suspend the system\n"); |
| 119 | writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG); |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | static int exynos_suspend(void) |
| 124 | { |
Bartlomiej Zolnierkiewicz | a135e20 | 2014-09-25 17:59:41 +0900 | [diff] [blame] | 125 | if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) |
| 126 | exynos_save_cp15(); |
Tomasz Figa | 2b9d9c3 | 2014-09-24 01:24:39 +0900 | [diff] [blame] | 127 | |
| 128 | writel(EXYNOS_SLEEP_MAGIC, sysram_ns_base_addr + EXYNOS_BOOT_FLAG); |
Florian Fainelli | 64fc2a9 | 2017-01-15 03:59:29 +0100 | [diff] [blame] | 129 | writel(__pa_symbol(exynos_cpu_resume_ns), |
Tomasz Figa | 2b9d9c3 | 2014-09-24 01:24:39 +0900 | [diff] [blame] | 130 | sysram_ns_base_addr + EXYNOS_BOOT_ADDR); |
| 131 | |
| 132 | return cpu_suspend(0, exynos_cpu_suspend); |
| 133 | } |
| 134 | |
| 135 | static int exynos_resume(void) |
| 136 | { |
| 137 | writel(0, sysram_ns_base_addr + EXYNOS_BOOT_FLAG); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 142 | static const struct firmware_ops exynos_firmware_ops = { |
Arnd Bergmann | 03c1b76 | 2014-10-28 08:10:21 +0900 | [diff] [blame] | 143 | .do_idle = IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND) ? exynos_do_idle : NULL, |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 144 | .set_cpu_boot_addr = exynos_set_cpu_boot_addr, |
Bartlomiej Zolnierkiewicz | 1225ad7 | 2015-03-18 14:09:56 +0100 | [diff] [blame] | 145 | .get_cpu_boot_addr = exynos_get_cpu_boot_addr, |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 146 | .cpu_boot = exynos_cpu_boot, |
Arnd Bergmann | 03c1b76 | 2014-10-28 08:10:21 +0900 | [diff] [blame] | 147 | .suspend = IS_ENABLED(CONFIG_PM_SLEEP) ? exynos_suspend : NULL, |
| 148 | .resume = IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND) ? exynos_resume : NULL, |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 149 | }; |
| 150 | |
Tomasz Figa | 5445b64 | 2015-01-08 07:53:20 +0100 | [diff] [blame] | 151 | static void exynos_l2_write_sec(unsigned long val, unsigned reg) |
| 152 | { |
| 153 | static int l2cache_enabled; |
| 154 | |
| 155 | switch (reg) { |
| 156 | case L2X0_CTRL: |
| 157 | if (val & L2X0_CTRL_EN) { |
| 158 | /* |
| 159 | * Before the cache can be enabled, due to firmware |
| 160 | * design, SMC_CMD_L2X0INVALL must be called. |
| 161 | */ |
| 162 | if (!l2cache_enabled) { |
| 163 | exynos_smc(SMC_CMD_L2X0INVALL, 0, 0, 0); |
| 164 | l2cache_enabled = 1; |
| 165 | } |
| 166 | } else { |
| 167 | l2cache_enabled = 0; |
| 168 | } |
| 169 | exynos_smc(SMC_CMD_L2X0CTRL, val, 0, 0); |
| 170 | break; |
| 171 | |
| 172 | case L2X0_DEBUG_CTRL: |
| 173 | exynos_smc(SMC_CMD_L2X0DEBUG, val, 0, 0); |
| 174 | break; |
| 175 | |
| 176 | default: |
| 177 | WARN_ONCE(1, "%s: ignoring write to reg 0x%x\n", __func__, reg); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void exynos_l2_configure(const struct l2x0_regs *regs) |
| 182 | { |
| 183 | exynos_smc(SMC_CMD_L2X0SETUP1, regs->tag_latency, regs->data_latency, |
| 184 | regs->prefetch_ctrl); |
| 185 | exynos_smc(SMC_CMD_L2X0SETUP2, regs->pwr_ctrl, regs->aux_ctrl, 0); |
| 186 | } |
| 187 | |
Krzysztof Kozlowski | e0b35c1 | 2018-07-24 18:49:46 +0200 | [diff] [blame] | 188 | bool __init exynos_secure_firmware_available(void) |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 189 | { |
Tomasz Figa | 4ee1cc7 | 2013-06-15 09:13:25 +0900 | [diff] [blame] | 190 | struct device_node *nd; |
| 191 | const __be32 *addr; |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 192 | |
Tomasz Figa | 4ee1cc7 | 2013-06-15 09:13:25 +0900 | [diff] [blame] | 193 | nd = of_find_compatible_node(NULL, NULL, |
| 194 | "samsung,secure-firmware"); |
| 195 | if (!nd) |
Krzysztof Kozlowski | e0b35c1 | 2018-07-24 18:49:46 +0200 | [diff] [blame] | 196 | return false; |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 197 | |
Tomasz Figa | 4ee1cc7 | 2013-06-15 09:13:25 +0900 | [diff] [blame] | 198 | addr = of_get_address(nd, 0, NULL, NULL); |
Wen Yang | 629266b | 2019-03-05 19:33:54 +0800 | [diff] [blame] | 199 | of_node_put(nd); |
Tomasz Figa | 4ee1cc7 | 2013-06-15 09:13:25 +0900 | [diff] [blame] | 200 | if (!addr) { |
| 201 | pr_err("%s: No address specified.\n", __func__); |
Krzysztof Kozlowski | e0b35c1 | 2018-07-24 18:49:46 +0200 | [diff] [blame] | 202 | return false; |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 203 | } |
| 204 | |
Krzysztof Kozlowski | e0b35c1 | 2018-07-24 18:49:46 +0200 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
| 208 | void __init exynos_firmware_init(void) |
| 209 | { |
| 210 | if (!exynos_secure_firmware_available()) |
| 211 | return; |
| 212 | |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 213 | pr_info("Running under secure firmware.\n"); |
| 214 | |
| 215 | register_firmware_ops(&exynos_firmware_ops); |
Tomasz Figa | 5445b64 | 2015-01-08 07:53:20 +0100 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Exynos 4 SoCs (based on Cortex A9 and equipped with L2C-310), |
| 219 | * running under secure firmware, require certain registers of L2 |
| 220 | * cache controller to be written in secure mode. Here .write_sec |
| 221 | * callback is provided to perform necessary SMC calls. |
| 222 | */ |
| 223 | if (IS_ENABLED(CONFIG_CACHE_L2X0) && |
| 224 | read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) { |
| 225 | outer_cache.write_sec = exynos_l2_write_sec; |
| 226 | outer_cache.configure = exynos_l2_configure; |
| 227 | } |
Tomasz Figa | bca28f8 | 2012-12-11 13:58:43 +0900 | [diff] [blame] | 228 | } |
Bartlomiej Zolnierkiewicz | dc1b944 | 2015-03-27 02:32:56 +0900 | [diff] [blame] | 229 | |
| 230 | #define REG_CPU_STATE_ADDR (sysram_ns_base_addr + 0x28) |
| 231 | #define BOOT_MODE_MASK 0x1f |
| 232 | |
| 233 | void exynos_set_boot_flag(unsigned int cpu, unsigned int mode) |
| 234 | { |
| 235 | unsigned int tmp; |
| 236 | |
Ben Dooks | 458ad21 | 2016-06-21 11:20:24 +0100 | [diff] [blame] | 237 | tmp = readl_relaxed(REG_CPU_STATE_ADDR + cpu * 4); |
Bartlomiej Zolnierkiewicz | dc1b944 | 2015-03-27 02:32:56 +0900 | [diff] [blame] | 238 | |
| 239 | if (mode & BOOT_MODE_MASK) |
| 240 | tmp &= ~BOOT_MODE_MASK; |
| 241 | |
| 242 | tmp |= mode; |
Ben Dooks | 458ad21 | 2016-06-21 11:20:24 +0100 | [diff] [blame] | 243 | writel_relaxed(tmp, REG_CPU_STATE_ADDR + cpu * 4); |
Bartlomiej Zolnierkiewicz | dc1b944 | 2015-03-27 02:32:56 +0900 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void exynos_clear_boot_flag(unsigned int cpu, unsigned int mode) |
| 247 | { |
| 248 | unsigned int tmp; |
| 249 | |
Ben Dooks | 458ad21 | 2016-06-21 11:20:24 +0100 | [diff] [blame] | 250 | tmp = readl_relaxed(REG_CPU_STATE_ADDR + cpu * 4); |
Bartlomiej Zolnierkiewicz | dc1b944 | 2015-03-27 02:32:56 +0900 | [diff] [blame] | 251 | tmp &= ~mode; |
Ben Dooks | 458ad21 | 2016-06-21 11:20:24 +0100 | [diff] [blame] | 252 | writel_relaxed(tmp, REG_CPU_STATE_ADDR + cpu * 4); |
Bartlomiej Zolnierkiewicz | dc1b944 | 2015-03-27 02:32:56 +0900 | [diff] [blame] | 253 | } |