1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013 Linaro Ltd.
4 * Copyright (c) 2013 HiSilicon Limited.
8 #include <linux/delay.h>
10 #include <linux/of_address.h>
11 #include <asm/cacheflush.h>
12 #include <asm/smp_plat.h>
15 /* Sysctrl registers in Hi3620 SoC */
18 #define SCPERPWREN 0xd0
19 #define SCPERPWRDIS 0xd4
20 #define SCCPUCOREEN 0xf4
21 #define SCCPUCOREDIS 0xf8
22 #define SCPERCTRL0 0x200
23 #define SCCPURSTEN 0x410
24 #define SCCPURSTDIS 0x414
27 * bit definition in SCISOEN/SCPERPWREN/...
29 * CPU2_ISO_CTRL (1 << 5)
30 * CPU3_ISO_CTRL (1 << 6)
33 #define CPU2_ISO_CTRL (1 << 5)
36 * bit definition in SCPERCTRL0
38 * CPU0_WFI_MASK_CFG (1 << 28)
39 * CPU1_WFI_MASK_CFG (1 << 29)
42 #define CPU0_WFI_MASK_CFG (1 << 28)
45 * bit definition in SCCPURSTEN/...
47 * CPU0_SRST_REQ_EN (1 << 0)
48 * CPU1_SRST_REQ_EN (1 << 1)
51 #define CPU0_HPM_SRST_REQ_EN (1 << 22)
52 #define CPU0_DBG_SRST_REQ_EN (1 << 12)
53 #define CPU0_NEON_SRST_REQ_EN (1 << 4)
54 #define CPU0_SRST_REQ_EN (1 << 0)
56 #define HIX5HD2_PERI_CRG20 0x50
57 #define CRG20_CPU1_RESET (1 << 17)
59 #define HIX5HD2_PERI_PMC0 0x1000
60 #define PMC0_CPU1_WAIT_MTCOMS_ACK (1 << 8)
61 #define PMC0_CPU1_PMC_ENABLE (1 << 7)
62 #define PMC0_CPU1_POWERDOWN (1 << 3)
64 #define HIP01_PERI9 0x50
65 #define PERI9_CPU1_RESET (1 << 1)
72 static void __iomem *ctrl_base;
75 static void set_cpu_hi3620(int cpu, bool enable)
81 if ((cpu == 2) || (cpu == 3))
82 writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
83 ctrl_base + SCPERPWREN);
87 writel_relaxed(0x01 << cpu, ctrl_base + SCCPUCOREEN);
90 val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
92 writel_relaxed(val << cpu, ctrl_base + SCCPURSTDIS);
94 val |= CPU0_HPM_SRST_REQ_EN;
95 writel_relaxed(val << cpu, ctrl_base + SCCPURSTEN);
98 if ((cpu == 2) || (cpu == 3))
99 writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
100 ctrl_base + SCISODIS);
104 val = readl_relaxed(ctrl_base + SCPERCTRL0);
105 val &= ~(CPU0_WFI_MASK_CFG << cpu);
106 writel_relaxed(val, ctrl_base + SCPERCTRL0);
109 val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
110 | CPU0_SRST_REQ_EN | CPU0_HPM_SRST_REQ_EN;
111 writel_relaxed(val << cpu, ctrl_base + SCCPURSTDIS);
114 val = readl_relaxed(ctrl_base + SCPERCTRL0);
115 val |= (CPU0_WFI_MASK_CFG << cpu);
116 writel_relaxed(val, ctrl_base + SCPERCTRL0);
119 writel_relaxed(0x01 << cpu, ctrl_base + SCCPUCOREDIS);
121 if ((cpu == 2) || (cpu == 3)) {
123 writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
124 ctrl_base + SCISOEN);
129 val = CPU0_DBG_SRST_REQ_EN | CPU0_NEON_SRST_REQ_EN
130 | CPU0_SRST_REQ_EN | CPU0_HPM_SRST_REQ_EN;
131 writel_relaxed(val << cpu, ctrl_base + SCCPURSTEN);
133 if ((cpu == 2) || (cpu == 3)) {
135 writel_relaxed(CPU2_ISO_CTRL << (cpu - 2),
136 ctrl_base + SCPERPWRDIS);
142 static int hi3xxx_hotplug_init(void)
144 struct device_node *node;
146 node = of_find_compatible_node(NULL, NULL, "hisilicon,sysctrl");
152 ctrl_base = of_iomap(node, 0);
163 void hi3xxx_set_cpu(int cpu, bool enable)
166 if (hi3xxx_hotplug_init() < 0)
170 if (id == HI3620_CTRL)
171 set_cpu_hi3620(cpu, enable);
174 static bool hix5hd2_hotplug_init(void)
176 struct device_node *np;
178 np = of_find_compatible_node(NULL, NULL, "hisilicon,cpuctrl");
182 ctrl_base = of_iomap(np, 0);
190 void hix5hd2_set_cpu(int cpu, bool enable)
195 if (!hix5hd2_hotplug_init())
200 val = readl_relaxed(ctrl_base + HIX5HD2_PERI_PMC0);
201 val &= ~(PMC0_CPU1_WAIT_MTCOMS_ACK | PMC0_CPU1_POWERDOWN);
202 val |= PMC0_CPU1_PMC_ENABLE;
203 writel_relaxed(val, ctrl_base + HIX5HD2_PERI_PMC0);
205 val = readl_relaxed(ctrl_base + HIX5HD2_PERI_CRG20);
206 val &= ~CRG20_CPU1_RESET;
207 writel_relaxed(val, ctrl_base + HIX5HD2_PERI_CRG20);
209 /* power down cpu1 */
210 val = readl_relaxed(ctrl_base + HIX5HD2_PERI_PMC0);
211 val |= PMC0_CPU1_PMC_ENABLE | PMC0_CPU1_POWERDOWN;
212 val &= ~PMC0_CPU1_WAIT_MTCOMS_ACK;
213 writel_relaxed(val, ctrl_base + HIX5HD2_PERI_PMC0);
216 val = readl_relaxed(ctrl_base + HIX5HD2_PERI_CRG20);
217 val |= CRG20_CPU1_RESET;
218 writel_relaxed(val, ctrl_base + HIX5HD2_PERI_CRG20);
222 void hip01_set_cpu(int cpu, bool enable)
225 struct device_node *np;
228 np = of_find_compatible_node(NULL, NULL, "hisilicon,hip01-sysctrl");
230 ctrl_base = of_iomap(np, 0);
237 temp = readl_relaxed(ctrl_base + HIP01_PERI9);
238 temp |= PERI9_CPU1_RESET;
239 writel_relaxed(temp, ctrl_base + HIP01_PERI9);
243 /* unreset on CPU1 */
244 temp = readl_relaxed(ctrl_base + HIP01_PERI9);
245 temp &= ~PERI9_CPU1_RESET;
246 writel_relaxed(temp, ctrl_base + HIP01_PERI9);
250 static inline void cpu_enter_lowpower(void)
257 * Turn off coherency and L1 D-cache
260 " mrc p15, 0, %0, c1, c0, 1\n"
261 " bic %0, %0, #0x40\n"
262 " mcr p15, 0, %0, c1, c0, 1\n"
263 " mrc p15, 0, %0, c1, c0, 0\n"
264 " bic %0, %0, #0x04\n"
265 " mcr p15, 0, %0, c1, c0, 0\n"
271 #ifdef CONFIG_HOTPLUG_CPU
272 void hi3xxx_cpu_die(unsigned int cpu)
274 cpu_enter_lowpower();
275 hi3xxx_set_cpu_jump(cpu, phys_to_virt(0));
278 /* We should have never returned from idle */
279 panic("cpu %d unexpectedly exit from shutdown\n", cpu);
282 int hi3xxx_cpu_kill(unsigned int cpu)
284 unsigned long timeout = jiffies + msecs_to_jiffies(50);
286 while (hi3xxx_get_cpu_jump(cpu))
287 if (time_after(jiffies, timeout))
289 hi3xxx_set_cpu(cpu, false);
293 void hix5hd2_cpu_die(unsigned int cpu)
296 hix5hd2_set_cpu(cpu, false);