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);
86 pr_err("Couldn't map SRAM registers\n");
91 pmu = syscon_regmap_lookup_by_compatible(pmu_compatible);
93 pr_err("Couldn't map PMU registers\n");
98 node = of_find_compatible_node(NULL, NULL, scu_compatible);
100 pr_err("Missing SCU node\n");
104 scu_base = of_iomap(node, 0);
107 pr_err("Couldn't map SCU registers\n");
111 scu_enable(scu_base);
114 static void __init meson8b_smp_prepare_cpus(unsigned int max_cpus)
116 meson_smp_prepare_cpus("arm,cortex-a5-scu", "amlogic,meson8b-pmu",
117 "amlogic,meson8b-smp-sram");
120 static void __init meson8_smp_prepare_cpus(unsigned int max_cpus)
122 meson_smp_prepare_cpus("arm,cortex-a9-scu", "amlogic,meson8-pmu",
123 "amlogic,meson8-smp-sram");
126 static void meson_smp_begin_secondary_boot(unsigned int cpu)
129 * Set the entry point before powering on the CPU through the SCU. This
130 * is needed if the CPU is in "warm" state (= after rebooting the
131 * system without power-cycling, or when taking the CPU offline and
132 * then taking it online again.
134 writel(__pa_symbol(secondary_startup),
135 sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu));
138 * SCU Power on CPU (needs to be done before starting the CPU,
139 * otherwise the secondary CPU will not start).
141 scu_cpu_power_enable(scu_base, cpu);
144 static int meson_smp_finalize_secondary_boot(unsigned int cpu)
146 unsigned long timeout;
148 timeout = jiffies + (10 * HZ);
149 while (readl(sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu))) {
150 if (!time_before(jiffies, timeout)) {
151 pr_err("Timeout while waiting for CPU%d status\n",
157 writel(__pa_symbol(secondary_startup),
158 sram_base + MESON_SMP_SRAM_CPU_CTRL_ADDR_REG(cpu));
160 meson_smp_set_cpu_ctrl(cpu, true);
165 static int meson8_smp_boot_secondary(unsigned int cpu,
166 struct task_struct *idle)
168 struct reset_control *rstc;
171 rstc = meson_smp_get_core_reset(cpu);
173 pr_err("Couldn't get the reset controller for CPU%d\n", cpu);
174 return PTR_ERR(rstc);
177 meson_smp_begin_secondary_boot(cpu);
180 ret = reset_control_assert(rstc);
182 pr_err("Failed to assert CPU%d reset\n", cpu);
187 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
188 MESON_CPU_PWR_A9_CNTL1_M(cpu), 0);
190 pr_err("Couldn't wake up CPU%d\n", cpu);
196 /* Isolation disable */
197 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
200 pr_err("Error when disabling isolation of CPU%d\n", cpu);
205 ret = reset_control_deassert(rstc);
207 pr_err("Failed to de-assert CPU%d reset\n", cpu);
211 ret = meson_smp_finalize_secondary_boot(cpu);
216 reset_control_put(rstc);
221 static int meson8b_smp_boot_secondary(unsigned int cpu,
222 struct task_struct *idle)
224 struct reset_control *rstc;
228 rstc = meson_smp_get_core_reset(cpu);
230 pr_err("Couldn't get the reset controller for CPU%d\n", cpu);
231 return PTR_ERR(rstc);
234 meson_smp_begin_secondary_boot(cpu);
237 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0,
238 MESON_CPU_PWR_A9_CNTL0_M(cpu), 0);
240 pr_err("Couldn't power up CPU%d\n", cpu);
247 ret = reset_control_assert(rstc);
249 pr_err("Failed to assert CPU%d reset\n", cpu);
253 /* Memory power UP */
254 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_MEM_PD0,
255 MESON_CPU_PWR_A9_MEM_PD0_M(cpu), 0);
257 pr_err("Couldn't power up the memory for CPU%d\n", cpu);
262 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
263 MESON_CPU_PWR_A9_CNTL1_M(cpu), 0);
265 pr_err("Couldn't wake up CPU%d\n", cpu);
271 ret = regmap_read_poll_timeout(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1, val,
272 val & MESON_CPU_PWR_A9_CNTL1_ST(cpu),
275 pr_err("Timeout while polling PMU for CPU%d status\n", cpu);
279 /* Isolation disable */
280 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
283 pr_err("Error when disabling isolation of CPU%d\n", cpu);
288 ret = reset_control_deassert(rstc);
290 pr_err("Failed to de-assert CPU%d reset\n", cpu);
294 ret = meson_smp_finalize_secondary_boot(cpu);
299 reset_control_put(rstc);
304 #ifdef CONFIG_HOTPLUG_CPU
305 static void meson8_smp_cpu_die(unsigned int cpu)
307 meson_smp_set_cpu_ctrl(cpu, false);
309 v7_exit_coherency_flush(louis);
311 scu_power_mode(scu_base, SCU_PM_POWEROFF);
316 /* we should never get here */
320 static int meson8_smp_cpu_kill(unsigned int cpu)
323 unsigned long timeout;
325 timeout = jiffies + (50 * HZ);
327 power_mode = scu_get_cpu_power_mode(scu_base, cpu);
329 if (power_mode == SCU_PM_POWEROFF)
332 usleep_range(10000, 15000);
333 } while (time_before(jiffies, timeout));
335 if (power_mode != SCU_PM_POWEROFF) {
336 pr_err("Error while waiting for SCU power-off on CPU%d\n",
343 /* Isolation enable */
344 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
347 pr_err("Error when enabling isolation for CPU%d\n", cpu);
354 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
355 MESON_CPU_PWR_A9_CNTL1_M(cpu), 0x3);
357 pr_err("Couldn't change sleep status of CPU%d\n", cpu);
364 static int meson8b_smp_cpu_kill(unsigned int cpu)
366 int ret, power_mode, count = 5000;
369 power_mode = scu_get_cpu_power_mode(scu_base, cpu);
371 if (power_mode == SCU_PM_POWEROFF)
377 if (power_mode != SCU_PM_POWEROFF) {
378 pr_err("Error while waiting for SCU power-off on CPU%d\n",
386 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0,
387 MESON_CPU_PWR_A9_CNTL0_M(cpu), 0x3);
389 pr_err("Couldn't power down CPU%d\n", cpu);
393 /* Isolation enable */
394 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL0, BIT(cpu),
397 pr_err("Error when enabling isolation for CPU%d\n", cpu);
404 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_CNTL1,
405 MESON_CPU_PWR_A9_CNTL1_M(cpu), 0x3);
407 pr_err("Couldn't change sleep status of CPU%d\n", cpu);
411 /* Memory power DOWN */
412 ret = regmap_update_bits(pmu, MESON_CPU_AO_RTI_PWR_A9_MEM_PD0,
413 MESON_CPU_PWR_A9_MEM_PD0_M(cpu), 0xf);
415 pr_err("Couldn't power down the memory of CPU%d\n", cpu);
423 static struct smp_operations meson8_smp_ops __initdata = {
424 .smp_prepare_cpus = meson8_smp_prepare_cpus,
425 .smp_boot_secondary = meson8_smp_boot_secondary,
426 #ifdef CONFIG_HOTPLUG_CPU
427 .cpu_die = meson8_smp_cpu_die,
428 .cpu_kill = meson8_smp_cpu_kill,
432 static struct smp_operations meson8b_smp_ops __initdata = {
433 .smp_prepare_cpus = meson8b_smp_prepare_cpus,
434 .smp_boot_secondary = meson8b_smp_boot_secondary,
435 #ifdef CONFIG_HOTPLUG_CPU
436 .cpu_die = meson8_smp_cpu_die,
437 .cpu_kill = meson8b_smp_cpu_kill,
441 CPU_METHOD_OF_DECLARE(meson8_smp, "amlogic,meson8-smp", &meson8_smp_ops);
442 CPU_METHOD_OF_DECLARE(meson8b_smp, "amlogic,meson8b-smp", &meson8b_smp_ops);