2 * Copyright (c) 2013 MundoReader S.L.
3 * Author: Heiko Stuebner <heiko@sntech.de>
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,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/smp.h>
21 #include <linux/of_address.h>
22 #include <linux/regmap.h>
23 #include <linux/mfd/syscon.h>
25 #include <linux/reset.h>
26 #include <linux/cpu.h>
27 #include <asm/cacheflush.h>
29 #include <asm/smp_scu.h>
30 #include <asm/smp_plat.h>
31 #include <asm/mach/map.h>
35 static void __iomem *scu_base_addr;
36 static void __iomem *sram_base_addr;
39 #define PMU_PWRDN_CON 0x08
40 #define PMU_PWRDN_ST 0x0c
42 #define PMU_PWRDN_SCU 4
44 static struct regmap *pmu;
45 static int has_pmu = true;
47 static int pmu_power_domain_is_on(int pd)
52 ret = regmap_read(pmu, PMU_PWRDN_ST, &val);
56 return !(val & BIT(pd));
59 static struct reset_control *rockchip_get_core_reset(int cpu)
61 struct device *dev = get_cpu_device(cpu);
62 struct device_node *np;
64 /* The cpu device is only available after the initial core bringup */
68 np = of_get_cpu_node(cpu, NULL);
70 return of_reset_control_get_exclusive(np, NULL);
73 static int pmu_set_power_domain(int pd, bool on)
75 u32 val = (on) ? 0 : BIT(pd);
76 struct reset_control *rstc = rockchip_get_core_reset(pd);
79 if (IS_ERR(rstc) && read_cpuid_part() != ARM_CPU_PART_CORTEX_A9) {
80 pr_err("%s: could not get reset control for core %d\n",
86 * We need to soft reset the cpu when we turn off the cpu power domain,
87 * or else the active processors might be stalled when the individual
88 * processor is powered down.
90 if (!IS_ERR(rstc) && !on)
91 reset_control_assert(rstc);
94 ret = regmap_update_bits(pmu, PMU_PWRDN_CON, BIT(pd), val);
96 pr_err("%s: could not update power domain\n",
103 ret = pmu_power_domain_is_on(pd);
105 pr_err("%s: could not read power domain state\n",
114 reset_control_deassert(rstc);
115 reset_control_put(rstc);
122 * Handling of CPU cores
125 static int rockchip_boot_secondary(unsigned int cpu, struct task_struct *idle)
129 if (!sram_base_addr || (has_pmu && !pmu)) {
130 pr_err("%s: sram or pmu missing for cpu boot\n", __func__);
135 pr_err("%s: cpu %d outside maximum number of cpus %d\n",
136 __func__, cpu, ncores);
141 ret = pmu_set_power_domain(0 + cpu, true);
145 if (read_cpuid_part() != ARM_CPU_PART_CORTEX_A9) {
147 * We communicate with the bootrom to active the cpus other
148 * than cpu0, after a blob of initialize code, they will
149 * stay at wfe state, once they are actived, they will check
151 * sram_base_addr + 4: 0xdeadbeaf
152 * sram_base_addr + 8: start address for pc
153 * The cpu0 need to wait the other cpus other than cpu0 entering
154 * the wfe state.The wait time is affected by many aspects.
155 * (e.g: cpu frequency, bootrom frequency, sram frequency, ...)
157 mdelay(1); /* ensure the cpus other than cpu0 to startup */
159 writel(__pa_symbol(secondary_startup), sram_base_addr + 8);
160 writel(0xDEADBEAF, sram_base_addr + 4);
168 * rockchip_smp_prepare_sram - populate necessary sram block
169 * Starting cores execute the code residing at the start of the on-chip sram
170 * after power-on. Therefore make sure, this sram region is reserved and
171 * big enough. After this check, copy the trampoline code that directs the
172 * core to the real startup code in ram into the sram-region.
173 * @node: mmio-sram device node
175 static int __init rockchip_smp_prepare_sram(struct device_node *node)
177 unsigned int trampoline_sz = &rockchip_secondary_trampoline_end -
178 &rockchip_secondary_trampoline;
183 ret = of_address_to_resource(node, 0, &res);
185 pr_err("%s: could not get address for node %pOF\n",
190 rsize = resource_size(&res);
191 if (rsize < trampoline_sz) {
192 pr_err("%s: reserved block with size 0x%x is to small for trampoline size 0x%x\n",
193 __func__, rsize, trampoline_sz);
197 /* set the boot function for the sram code */
198 rockchip_boot_fn = __pa_symbol(secondary_startup);
200 /* copy the trampoline to sram, that runs during startup of the core */
201 memcpy(sram_base_addr, &rockchip_secondary_trampoline, trampoline_sz);
203 outer_clean_range(0, trampoline_sz);
210 static const struct regmap_config rockchip_pmu_regmap_config = {
211 .name = "rockchip-pmu",
217 static int __init rockchip_smp_prepare_pmu(void)
219 struct device_node *node;
220 void __iomem *pmu_base;
223 * This function is only called via smp_ops->smp_prepare_cpu().
224 * That only happens if a "/cpus" device tree node exists
225 * and has an "enable-method" property that selects the SMP
226 * operations defined herein.
228 node = of_find_node_by_path("/cpus");
230 pmu = syscon_regmap_lookup_by_phandle(node, "rockchip,pmu");
235 pmu = syscon_regmap_lookup_by_compatible("rockchip,rk3066-pmu");
239 /* fallback, create our own regmap for the pmu area */
241 node = of_find_compatible_node(NULL, NULL, "rockchip,rk3066-pmu");
243 pr_err("%s: could not find pmu dt node\n", __func__);
247 pmu_base = of_iomap(node, 0);
249 pr_err("%s: could not map pmu registers\n", __func__);
253 pmu = regmap_init_mmio(NULL, pmu_base, &rockchip_pmu_regmap_config);
255 int ret = PTR_ERR(pmu);
259 pr_err("%s: regmap init failed\n", __func__);
266 static void __init rockchip_smp_prepare_cpus(unsigned int max_cpus)
268 struct device_node *node;
271 node = of_find_compatible_node(NULL, NULL, "rockchip,rk3066-smp-sram");
273 pr_err("%s: could not find sram dt node\n", __func__);
277 sram_base_addr = of_iomap(node, 0);
278 if (!sram_base_addr) {
279 pr_err("%s: could not map sram registers\n", __func__);
283 if (has_pmu && rockchip_smp_prepare_pmu())
286 if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
287 if (rockchip_smp_prepare_sram(node))
290 /* enable the SCU power domain */
291 pmu_set_power_domain(PMU_PWRDN_SCU, true);
293 node = of_find_compatible_node(NULL, NULL, "arm,cortex-a9-scu");
295 pr_err("%s: missing scu\n", __func__);
299 scu_base_addr = of_iomap(node, 0);
300 if (!scu_base_addr) {
301 pr_err("%s: could not map scu registers\n", __func__);
306 * While the number of cpus is gathered from dt, also get the
307 * number of cores from the scu to verify this value when
310 ncores = scu_get_core_count(scu_base_addr);
311 pr_err("%s: ncores %d\n", __func__, ncores);
313 scu_enable(scu_base_addr);
317 asm ("mrc p15, 1, %0, c9, c0, 2\n" : "=r" (l2ctlr));
318 ncores = ((l2ctlr >> 24) & 0x3) + 1;
321 /* Make sure that all cores except the first are really off */
322 for (i = 1; i < ncores; i++)
323 pmu_set_power_domain(0 + i, false);
326 static void __init rk3036_smp_prepare_cpus(unsigned int max_cpus)
330 rockchip_smp_prepare_cpus(max_cpus);
333 #ifdef CONFIG_HOTPLUG_CPU
334 static int rockchip_cpu_kill(unsigned int cpu)
337 * We need a delay here to ensure that the dying CPU can finish
338 * executing v7_coherency_exit() and reach the WFI/WFE state
339 * prior to having the power domain disabled.
343 pmu_set_power_domain(0 + cpu, false);
347 static void rockchip_cpu_die(unsigned int cpu)
349 v7_exit_coherency_flush(louis);
355 static const struct smp_operations rk3036_smp_ops __initconst = {
356 .smp_prepare_cpus = rk3036_smp_prepare_cpus,
357 .smp_boot_secondary = rockchip_boot_secondary,
358 #ifdef CONFIG_HOTPLUG_CPU
359 .cpu_kill = rockchip_cpu_kill,
360 .cpu_die = rockchip_cpu_die,
364 static const struct smp_operations rockchip_smp_ops __initconst = {
365 .smp_prepare_cpus = rockchip_smp_prepare_cpus,
366 .smp_boot_secondary = rockchip_boot_secondary,
367 #ifdef CONFIG_HOTPLUG_CPU
368 .cpu_kill = rockchip_cpu_kill,
369 .cpu_die = rockchip_cpu_die,
373 CPU_METHOD_OF_DECLARE(rk3036_smp, "rockchip,rk3036-smp", &rk3036_smp_ops);
374 CPU_METHOD_OF_DECLARE(rk3066_smp, "rockchip,rk3066-smp", &rockchip_smp_ops);