1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Carlo Caione <carlo@endlessm.com>
4 * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
7 #include <linux/delay.h>
8 #include <linux/init.h>
11 #include <linux/of_address.h>
12 #include <linux/regmap.h>
13 #include <linux/reset.h>
14 #include <linux/smp.h>
15 #include <linux/mfd/syscon.h>
17 #include <asm/cacheflush.h>
19 #include <asm/smp_scu.h>
20 #include <asm/smp_plat.h>
22 #define MESON_SMP_SRAM_CPU_CTRL_REG (0x00)
23 #define MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(c) (0x04 + ((c - 1) << 2))
25 #define MESON_CPU_AO_RTI_PWR_A9_CNTL0 (0x00)
26 #define MESON_CPU_AO_RTI_PWR_A9_CNTL1 (0x04)
27 #define MESON_CPU_AO_RTI_PWR_A9_MEM_PD0 (0x14)
29 #define MESON_CPU_PWR_A9_CNTL0_M(c) (0x03 << ((c * 2) + 16))
30 #define MESON_CPU_PWR_A9_CNTL1_M(c) (0x03 << ((c + 1) << 1))
31 #define MESON_CPU_PWR_A9_MEM_PD0_M(c) (0x0f << (32 - (c * 4)))
32 #define MESON_CPU_PWR_A9_CNTL1_ST(c) (0x01 << (c + 16))
34 static void __iomem
*sram_base
;
35 static void __iomem
*scu_base
;
36 static struct regmap
*pmu
;
38 static struct reset_control
*meson_smp_get_core_reset(int cpu
)
40 struct device_node
*np
= of_get_cpu_node(cpu
, 0);
42 return of_reset_control_get_exclusive(np
, NULL
);
45 static void meson_smp_set_cpu_ctrl(int cpu
, bool on_off
)
47 u32 val
= readl(sram_base
+ MESON_SMP_SRAM_CPU_CTRL_REG
);
54 /* keep bit 0 always enabled */
57 writel(val
, sram_base
+ MESON_SMP_SRAM_CPU_CTRL_REG
);
60 static void __init
meson_smp_prepare_cpus(const char *scu_compatible
,
61 const char *pmu_compatible
,
62 const char *sram_compatible
)
64 static struct device_node
*node
;
67 node
= of_find_compatible_node(NULL
, NULL
, sram_compatible
);
69 pr_err("Missing SRAM node\n");
73 sram_base
= of_iomap(node
, 0);
75 pr_err("Couldn't map SRAM registers\n");
80 pmu
= syscon_regmap_lookup_by_compatible(pmu_compatible
);
82 pr_err("Couldn't map PMU registers\n");
87 node
= of_find_compatible_node(NULL
, NULL
, scu_compatible
);
89 pr_err("Missing SCU node\n");
93 scu_base
= of_iomap(node
, 0);
95 pr_err("Couldn't map SCU registers\n");
102 static void __init
meson8b_smp_prepare_cpus(unsigned int max_cpus
)
104 meson_smp_prepare_cpus("arm,cortex-a5-scu", "amlogic,meson8b-pmu",
105 "amlogic,meson8b-smp-sram");
108 static void __init
meson8_smp_prepare_cpus(unsigned int max_cpus
)
110 meson_smp_prepare_cpus("arm,cortex-a9-scu", "amlogic,meson8-pmu",
111 "amlogic,meson8-smp-sram");
114 static void meson_smp_begin_secondary_boot(unsigned int cpu
)
117 * Set the entry point before powering on the CPU through the SCU. This
118 * is needed if the CPU is in "warm" state (= after rebooting the
119 * system without power-cycling, or when taking the CPU offline and
120 * then taking it online again.
122 writel(__pa_symbol(secondary_startup
),
123 sram_base
+ MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu
));
126 * SCU Power on CPU (needs to be done before starting the CPU,
127 * otherwise the secondary CPU will not start).
129 scu_cpu_power_enable(scu_base
, cpu
);
132 static int meson_smp_finalize_secondary_boot(unsigned int cpu
)
134 unsigned long timeout
;
136 timeout
= jiffies
+ (10 * HZ
);
137 while (readl(sram_base
+ MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu
))) {
138 if (!time_before(jiffies
, timeout
)) {
139 pr_err("Timeout while waiting for CPU%d status\n",
145 writel(__pa_symbol(secondary_startup
),
146 sram_base
+ MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu
));
148 meson_smp_set_cpu_ctrl(cpu
, true);
153 static int meson8_smp_boot_secondary(unsigned int cpu
,
154 struct task_struct
*idle
)
156 struct reset_control
*rstc
;
159 rstc
= meson_smp_get_core_reset(cpu
);
161 pr_err("Couldn't get the reset controller for CPU%d\n", cpu
);
162 return PTR_ERR(rstc
);
165 meson_smp_begin_secondary_boot(cpu
);
168 ret
= reset_control_assert(rstc
);
170 pr_err("Failed to assert CPU%d reset\n", cpu
);
175 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL1
,
176 MESON_CPU_PWR_A9_CNTL1_M(cpu
), 0);
178 pr_err("Couldn't wake up CPU%d\n", cpu
);
184 /* Isolation disable */
185 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL0
, BIT(cpu
),
188 pr_err("Error when disabling isolation of CPU%d\n", cpu
);
193 ret
= reset_control_deassert(rstc
);
195 pr_err("Failed to de-assert CPU%d reset\n", cpu
);
199 ret
= meson_smp_finalize_secondary_boot(cpu
);
204 reset_control_put(rstc
);
209 static int meson8b_smp_boot_secondary(unsigned int cpu
,
210 struct task_struct
*idle
)
212 struct reset_control
*rstc
;
216 rstc
= meson_smp_get_core_reset(cpu
);
218 pr_err("Couldn't get the reset controller for CPU%d\n", cpu
);
219 return PTR_ERR(rstc
);
222 meson_smp_begin_secondary_boot(cpu
);
225 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL0
,
226 MESON_CPU_PWR_A9_CNTL0_M(cpu
), 0);
228 pr_err("Couldn't power up CPU%d\n", cpu
);
235 ret
= reset_control_assert(rstc
);
237 pr_err("Failed to assert CPU%d reset\n", cpu
);
241 /* Memory power UP */
242 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_MEM_PD0
,
243 MESON_CPU_PWR_A9_MEM_PD0_M(cpu
), 0);
245 pr_err("Couldn't power up the memory for CPU%d\n", cpu
);
250 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL1
,
251 MESON_CPU_PWR_A9_CNTL1_M(cpu
), 0);
253 pr_err("Couldn't wake up CPU%d\n", cpu
);
259 ret
= regmap_read_poll_timeout(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL1
, val
,
260 val
& MESON_CPU_PWR_A9_CNTL1_ST(cpu
),
263 pr_err("Timeout while polling PMU for CPU%d status\n", cpu
);
267 /* Isolation disable */
268 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL0
, BIT(cpu
),
271 pr_err("Error when disabling isolation of CPU%d\n", cpu
);
276 ret
= reset_control_deassert(rstc
);
278 pr_err("Failed to de-assert CPU%d reset\n", cpu
);
282 ret
= meson_smp_finalize_secondary_boot(cpu
);
287 reset_control_put(rstc
);
292 #ifdef CONFIG_HOTPLUG_CPU
293 static void meson8_smp_cpu_die(unsigned int cpu
)
295 meson_smp_set_cpu_ctrl(cpu
, false);
297 v7_exit_coherency_flush(louis
);
299 scu_power_mode(scu_base
, SCU_PM_POWEROFF
);
304 /* we should never get here */
308 static int meson8_smp_cpu_kill(unsigned int cpu
)
311 unsigned long timeout
;
313 timeout
= jiffies
+ (50 * HZ
);
315 power_mode
= scu_get_cpu_power_mode(scu_base
, cpu
);
317 if (power_mode
== SCU_PM_POWEROFF
)
320 usleep_range(10000, 15000);
321 } while (time_before(jiffies
, timeout
));
323 if (power_mode
!= SCU_PM_POWEROFF
) {
324 pr_err("Error while waiting for SCU power-off on CPU%d\n",
331 /* Isolation enable */
332 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL0
, BIT(cpu
),
335 pr_err("Error when enabling isolation for CPU%d\n", cpu
);
342 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL1
,
343 MESON_CPU_PWR_A9_CNTL1_M(cpu
), 0x3);
345 pr_err("Couldn't change sleep status of CPU%d\n", cpu
);
352 static int meson8b_smp_cpu_kill(unsigned int cpu
)
354 int ret
, power_mode
, count
= 5000;
357 power_mode
= scu_get_cpu_power_mode(scu_base
, cpu
);
359 if (power_mode
== SCU_PM_POWEROFF
)
365 if (power_mode
!= SCU_PM_POWEROFF
) {
366 pr_err("Error while waiting for SCU power-off on CPU%d\n",
374 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL0
,
375 MESON_CPU_PWR_A9_CNTL0_M(cpu
), 0x3);
377 pr_err("Couldn't power down CPU%d\n", cpu
);
381 /* Isolation enable */
382 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL0
, BIT(cpu
),
385 pr_err("Error when enabling isolation for CPU%d\n", cpu
);
392 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_CNTL1
,
393 MESON_CPU_PWR_A9_CNTL1_M(cpu
), 0x3);
395 pr_err("Couldn't change sleep status of CPU%d\n", cpu
);
399 /* Memory power DOWN */
400 ret
= regmap_update_bits(pmu
, MESON_CPU_AO_RTI_PWR_A9_MEM_PD0
,
401 MESON_CPU_PWR_A9_MEM_PD0_M(cpu
), 0xf);
403 pr_err("Couldn't power down the memory of CPU%d\n", cpu
);
411 static struct smp_operations meson8_smp_ops __initdata
= {
412 .smp_prepare_cpus
= meson8_smp_prepare_cpus
,
413 .smp_boot_secondary
= meson8_smp_boot_secondary
,
414 #ifdef CONFIG_HOTPLUG_CPU
415 .cpu_die
= meson8_smp_cpu_die
,
416 .cpu_kill
= meson8_smp_cpu_kill
,
420 static struct smp_operations meson8b_smp_ops __initdata
= {
421 .smp_prepare_cpus
= meson8b_smp_prepare_cpus
,
422 .smp_boot_secondary
= meson8b_smp_boot_secondary
,
423 #ifdef CONFIG_HOTPLUG_CPU
424 .cpu_die
= meson8_smp_cpu_die
,
425 .cpu_kill
= meson8b_smp_cpu_kill
,
429 CPU_METHOD_OF_DECLARE(meson8_smp
, "amlogic,meson8-smp", &meson8_smp_ops
);
430 CPU_METHOD_OF_DECLARE(meson8b_smp
, "amlogic,meson8b-smp", &meson8b_smp_ops
);