1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2012 Samsung Electronics.
4 // Kyungmin Park <kyungmin.park@samsung.com>
5 // Tomasz Figa <t.figa@samsung.com>
7 #include <linux/kernel.h>
9 #include <linux/init.h>
11 #include <linux/of_address.h>
13 #include <asm/cacheflush.h>
14 #include <asm/cputype.h>
15 #include <asm/firmware.h>
16 #include <asm/hardware/cache-l2x0.h>
17 #include <asm/suspend.h>
22 #define EXYNOS_BOOT_ADDR 0x8
23 #define EXYNOS_BOOT_FLAG 0xc
25 static void exynos_save_cp15(void)
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
)
34 static int exynos_do_idle(unsigned long mode
)
38 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
)
40 writel_relaxed(__pa_symbol(exynos_cpu_resume_ns
),
41 sysram_ns_base_addr
+ 0x24);
42 writel_relaxed(EXYNOS_AFTR_MAGIC
, sysram_ns_base_addr
+ 0x20);
43 if (soc_is_exynos3250()) {
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);
50 exynos_smc(SMC_CMD_CPU0AFTR
, 0, 0, 0);
52 case FW_DO_IDLE_SLEEP
:
53 exynos_smc(SMC_CMD_SLEEP
, 0, 0, 0);
58 static int exynos_cpu_boot(int cpu
)
61 * Exynos3250 doesn't need to send smc command for secondary CPU boot
62 * because Exynos3250 removes WFE in secure mode.
64 if (soc_is_exynos3250())
68 * The second parameter of SMC_CMD_CPU1BOOT command means CPU id.
70 exynos_smc(SMC_CMD_CPU1BOOT
, cpu
, 0, 0);
74 static int exynos_set_cpu_boot_addr(int cpu
, unsigned long boot_addr
)
76 void __iomem
*boot_reg
;
78 if (!sysram_ns_base_addr
)
81 boot_reg
= sysram_ns_base_addr
+ 0x1c;
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
88 if (soc_is_exynos4412())
91 writel_relaxed(boot_addr
, boot_reg
);
95 static int exynos_get_cpu_boot_addr(int cpu
, unsigned long *boot_addr
)
97 void __iomem
*boot_reg
;
99 if (!sysram_ns_base_addr
)
102 boot_reg
= sysram_ns_base_addr
+ 0x1c;
104 if (soc_is_exynos4412())
107 *boot_addr
= readl_relaxed(boot_reg
);
111 static int exynos_cpu_suspend(unsigned long arg
)
116 exynos_smc(SMC_CMD_SLEEP
, 0, 0, 0);
118 pr_info("Failed to suspend the system\n");
119 writel(0, sysram_ns_base_addr
+ EXYNOS_BOOT_FLAG
);
123 static int exynos_suspend(void)
125 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
)
128 writel(EXYNOS_SLEEP_MAGIC
, sysram_ns_base_addr
+ EXYNOS_BOOT_FLAG
);
129 writel(__pa_symbol(exynos_cpu_resume_ns
),
130 sysram_ns_base_addr
+ EXYNOS_BOOT_ADDR
);
132 return cpu_suspend(0, exynos_cpu_suspend
);
135 static int exynos_resume(void)
137 writel(0, sysram_ns_base_addr
+ EXYNOS_BOOT_FLAG
);
142 static const struct firmware_ops exynos_firmware_ops
= {
143 .do_idle
= IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND
) ? exynos_do_idle
: NULL
,
144 .set_cpu_boot_addr
= exynos_set_cpu_boot_addr
,
145 .get_cpu_boot_addr
= exynos_get_cpu_boot_addr
,
146 .cpu_boot
= exynos_cpu_boot
,
147 .suspend
= IS_ENABLED(CONFIG_PM_SLEEP
) ? exynos_suspend
: NULL
,
148 .resume
= IS_ENABLED(CONFIG_EXYNOS_CPU_SUSPEND
) ? exynos_resume
: NULL
,
151 static void exynos_l2_write_sec(unsigned long val
, unsigned reg
)
153 static int l2cache_enabled
;
157 if (val
& L2X0_CTRL_EN
) {
159 * Before the cache can be enabled, due to firmware
160 * design, SMC_CMD_L2X0INVALL must be called.
162 if (!l2cache_enabled
) {
163 exynos_smc(SMC_CMD_L2X0INVALL
, 0, 0, 0);
169 exynos_smc(SMC_CMD_L2X0CTRL
, val
, 0, 0);
172 case L2X0_DEBUG_CTRL
:
173 exynos_smc(SMC_CMD_L2X0DEBUG
, val
, 0, 0);
177 WARN_ONCE(1, "%s: ignoring write to reg 0x%x\n", __func__
, reg
);
181 static void exynos_l2_configure(const struct l2x0_regs
*regs
)
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);
188 bool __init
exynos_secure_firmware_available(void)
190 struct device_node
*nd
;
193 nd
= of_find_compatible_node(NULL
, NULL
,
194 "samsung,secure-firmware");
198 addr
= of_get_address(nd
, 0, NULL
, NULL
);
201 pr_err("%s: No address specified.\n", __func__
);
208 void __init
exynos_firmware_init(void)
210 if (!exynos_secure_firmware_available())
213 pr_info("Running under secure firmware.\n");
215 register_firmware_ops(&exynos_firmware_ops
);
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.
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
;
230 #define REG_CPU_STATE_ADDR (sysram_ns_base_addr + 0x28)
231 #define BOOT_MODE_MASK 0x1f
233 void exynos_set_boot_flag(unsigned int cpu
, unsigned int mode
)
237 tmp
= readl_relaxed(REG_CPU_STATE_ADDR
+ cpu
* 4);
239 if (mode
& BOOT_MODE_MASK
)
240 tmp
&= ~BOOT_MODE_MASK
;
243 writel_relaxed(tmp
, REG_CPU_STATE_ADDR
+ cpu
* 4);
246 void exynos_clear_boot_flag(unsigned int cpu
, unsigned int mode
)
250 tmp
= readl_relaxed(REG_CPU_STATE_ADDR
+ cpu
* 4);
252 writel_relaxed(tmp
, REG_CPU_STATE_ADDR
+ cpu
* 4);