2 * Copyright (C) 2015 Carlo Caione <carlo@endlessm.com>
3 * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/delay.h>
18 #include <linux/init.h>
21 #include <linux/of_address.h>
22 #include <linux/regmap.h>
23 #include <linux/reset.h>
24 #include <linux/smp.h>
25 #include <linux/mfd/syscon.h>
27 #include <asm/cacheflush.h>
29 #include <asm/smp_scu.h>
30 #include <asm/smp_plat.h>
32 #define MESON_SMP_SRAM_CPU_CTRL_REG (0x00)
33 #define MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(c) (0x04 + ((c - 1) << 2))
35 #define MESON_CPU_AO_RTI_PWR_A9_CNTL0 (0x00)
36 #define MESON_CPU_AO_RTI_PWR_A9_CNTL1 (0x04)
37 #define MESON_CPU_AO_RTI_PWR_A9_MEM_PD0 (0x14)
39 #define MESON_CPU_PWR_A9_CNTL0_M(c) (0x03 << ((c * 2) + 16))
40 #define MESON_CPU_PWR_A9_CNTL1_M(c) (0x03 << ((c + 1) << 1))
41 #define MESON_CPU_PWR_A9_MEM_PD0_M(c) (0x0f << (32 - (c * 4)))
42 #define MESON_CPU_PWR_A9_CNTL1_ST(c) (0x01 << (c + 16))
44 static void __iomem *sram_base;
45 static void __iomem *scu_base;
46 static struct regmap *pmu;
48 static struct reset_control *meson_smp_get_core_reset(int cpu)
50 struct device_node *np = of_get_cpu_node(cpu, 0);
52 return of_reset_control_get_exclusive(np, NULL);
55 static void meson_smp_set_cpu_ctrl(int cpu, bool on_off)
57 u32 val = readl(sram_base + MESON_SMP_SRAM_CPU_CTRL_REG);
64 /* keep bit 0 always enabled */
67 writel(val, sram_base + MESON_SMP_SRAM_CPU_CTRL_REG);
70 static void __init meson_smp_prepare_cpus(const char *scu_compatible,
71 const char *pmu_compatible,
72 const char *sram_compatible)
74 static struct device_node *node;
77 node = of_find_compatible_node(NULL, NULL, sram_compatible);
79 pr_err("Missing SRAM node\n");
83 sram_base = of_iomap(node, 0);
85 pr_err("Couldn't map SRAM registers\n");
90 pmu = syscon_regmap_lookup_by_compatible(pmu_compatible);
92 pr_err("Couldn't map PMU registers\n");
97 node = of_find_compatible_node(NULL, NULL, scu_compatible);
99 pr_err("Missing SCU node\n");
103 scu_base = of_iomap(node, 0);
105 pr_err("Couldn't map SCU registers\n");
109 scu_enable(scu_base);
112 static void __init meson8b_smp_prepare_cpus(unsigned int max_cpus)
114 meson_smp_prepare_cpus("arm,cortex-a5-scu", "amlogic,meson8b-pmu",
115 "amlogic,meson8b-smp-sram");
118 static void __init meson8_smp_prepare_cpus(unsigned int max_cpus)
120 meson_smp_prepare_cpus("arm,cortex-a9-scu", "amlogic,meson8-pmu",
121 "amlogic,meson8-smp-sram");
124 static void meson_smp_begin_secondary_boot(unsigned int cpu)
127 * Set the entry point before powering on the CPU through the SCU. This
128 * is needed if the CPU is in "warm" state (= after rebooting the
129 * system without power-cycling, or when taking the CPU offline and
130 * then taking it online again.
132 writel(__pa_symbol(secondary_startup),
133 sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu));
136 * SCU Power on CPU (needs to be done before starting the CPU,
137 * otherwise the secondary CPU will not start).
139 scu_cpu_power_enable(scu_base, cpu);
142 static int meson_smp_finalize_secondary_boot(unsigned int cpu)
144 unsigned long timeout;
146 timeout = jiffies + (10 * HZ);
147 while (readl(sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu))) {
148 if (!time_before(jiffies, timeout)) {
149 pr_err("Timeout while waiting for CPU%d status\n",
155 writel(__pa_symbol(secondary_startup),
156 sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu));
158 meson_smp_set_cpu_ctrl(cpu, true);
163 static int meson8_smp_boot_secondary(unsigned int cpu,
164 struct task_struct *idle)
166 struct reset_control *rstc;
169 rstc = meson_smp_get_core_reset(cpu);
171 pr_err("Couldn't get the reset controller for CPU%d\n", cpu);
172 return PTR_ERR(rstc);
175 meson_smp_begin_secondary_boot(cpu);
178 ret = reset_control_assert(rstc);
180 pr_err("Failed to assert CPU%d reset\n", cpu);
185 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
186 MESON_CPU_PWR_A9_CNTL1_M(cpu), 0);
188 pr_err("Couldn't wake up CPU%d\n", cpu);
194 /* Isolation disable */
195 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
198 pr_err("Error when disabling isolation of CPU%d\n", cpu);
203 ret = reset_control_deassert(rstc);
205 pr_err("Failed to de-assert CPU%d reset\n", cpu);
209 ret = meson_smp_finalize_secondary_boot(cpu);
214 reset_control_put(rstc);
219 static int meson8b_smp_boot_secondary(unsigned int cpu,
220 struct task_struct *idle)
222 struct reset_control *rstc;
226 rstc = meson_smp_get_core_reset(cpu);
228 pr_err("Couldn't get the reset controller for CPU%d\n", cpu);
229 return PTR_ERR(rstc);
232 meson_smp_begin_secondary_boot(cpu);
235 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0,
236 MESON_CPU_PWR_A9_CNTL0_M(cpu), 0);
238 pr_err("Couldn't power up CPU%d\n", cpu);
245 ret = reset_control_assert(rstc);
247 pr_err("Failed to assert CPU%d reset\n", cpu);
251 /* Memory power UP */
252 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_MEM_PD0,
253 MESON_CPU_PWR_A9_MEM_PD0_M(cpu), 0);
255 pr_err("Couldn't power up the memory for CPU%d\n", cpu);
260 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
261 MESON_CPU_PWR_A9_CNTL1_M(cpu), 0);
263 pr_err("Couldn't wake up CPU%d\n", cpu);
269 ret = regmap_read_poll_timeout(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1, val,
270 val & MESON_CPU_PWR_A9_CNTL1_ST(cpu),
273 pr_err("Timeout while polling PMU for CPU%d status\n", cpu);
277 /* Isolation disable */
278 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
281 pr_err("Error when disabling isolation of CPU%d\n", cpu);
286 ret = reset_control_deassert(rstc);
288 pr_err("Failed to de-assert CPU%d reset\n", cpu);
292 ret = meson_smp_finalize_secondary_boot(cpu);
297 reset_control_put(rstc);
302 #ifdef CONFIG_HOTPLUG_CPU
303 static void meson8_smp_cpu_die(unsigned int cpu)
305 meson_smp_set_cpu_ctrl(cpu, false);
307 v7_exit_coherency_flush(louis);
309 scu_power_mode(scu_base, SCU_PM_POWEROFF);
314 /* we should never get here */
318 static int meson8_smp_cpu_kill(unsigned int cpu)
321 unsigned long timeout;
323 timeout = jiffies + (50 * HZ);
325 power_mode = scu_get_cpu_power_mode(scu_base, cpu);
327 if (power_mode == SCU_PM_POWEROFF)
330 usleep_range(10000, 15000);
331 } while (time_before(jiffies, timeout));
333 if (power_mode != SCU_PM_POWEROFF) {
334 pr_err("Error while waiting for SCU power-off on CPU%d\n",
341 /* Isolation enable */
342 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
345 pr_err("Error when enabling isolation for CPU%d\n", cpu);
352 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
353 MESON_CPU_PWR_A9_CNTL1_M(cpu), 0x3);
355 pr_err("Couldn't change sleep status of CPU%d\n", cpu);
362 static int meson8b_smp_cpu_kill(unsigned int cpu)
364 int ret, power_mode, count = 5000;
367 power_mode = scu_get_cpu_power_mode(scu_base, cpu);
369 if (power_mode == SCU_PM_POWEROFF)
375 if (power_mode != SCU_PM_POWEROFF) {
376 pr_err("Error while waiting for SCU power-off on CPU%d\n",
384 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0,
385 MESON_CPU_PWR_A9_CNTL0_M(cpu), 0x3);
387 pr_err("Couldn't power down CPU%d\n", cpu);
391 /* Isolation enable */
392 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
395 pr_err("Error when enabling isolation for CPU%d\n", cpu);
402 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
403 MESON_CPU_PWR_A9_CNTL1_M(cpu), 0x3);
405 pr_err("Couldn't change sleep status of CPU%d\n", cpu);
409 /* Memory power DOWN */
410 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_MEM_PD0,
411 MESON_CPU_PWR_A9_MEM_PD0_M(cpu), 0xf);
413 pr_err("Couldn't power down the memory of CPU%d\n", cpu);
421 static struct smp_operations meson8_smp_ops __initdata = {
422 .smp_prepare_cpus = meson8_smp_prepare_cpus,
423 .smp_boot_secondary = meson8_smp_boot_secondary,
424 #ifdef CONFIG_HOTPLUG_CPU
425 .cpu_die = meson8_smp_cpu_die,
426 .cpu_kill = meson8_smp_cpu_kill,
430 static struct smp_operations meson8b_smp_ops __initdata = {
431 .smp_prepare_cpus = meson8b_smp_prepare_cpus,
432 .smp_boot_secondary = meson8b_smp_boot_secondary,
433 #ifdef CONFIG_HOTPLUG_CPU
434 .cpu_die = meson8_smp_cpu_die,
435 .cpu_kill = meson8b_smp_cpu_kill,
439 CPU_METHOD_OF_DECLARE(meson8_smp, "amlogic,meson8-smp", &meson8_smp_ops);
440 CPU_METHOD_OF_DECLARE(meson8b_smp, "amlogic,meson8b-smp", &meson8b_smp_ops);