1 // SPDX-License-Identifier: GPL-2.0-only
3 * Broadcom STB CPU SMP and hotplug support for ARM
5 * Copyright (C) 2013-2014 Broadcom Corporation
8 #include <linux/delay.h>
9 #include <linux/errno.h>
10 #include <linux/init.h>
12 #include <linux/jiffies.h>
14 #include <linux/of_address.h>
15 #include <linux/printk.h>
16 #include <linux/regmap.h>
17 #include <linux/smp.h>
18 #include <linux/mfd/syscon.h>
20 #include <asm/cacheflush.h>
22 #include <asm/mach-types.h>
23 #include <asm/smp_plat.h>
26 ZONE_MAN_CLKEN_MASK
= BIT(0),
27 ZONE_MAN_RESET_CNTL_MASK
= BIT(1),
28 ZONE_MAN_MEM_PWR_MASK
= BIT(4),
29 ZONE_RESERVED_1_MASK
= BIT(5),
30 ZONE_MAN_ISO_CNTL_MASK
= BIT(6),
31 ZONE_MANUAL_CONTROL_MASK
= BIT(7),
32 ZONE_PWR_DN_REQ_MASK
= BIT(9),
33 ZONE_PWR_UP_REQ_MASK
= BIT(10),
34 ZONE_BLK_RST_ASSERT_MASK
= BIT(12),
35 ZONE_PWR_OFF_STATE_MASK
= BIT(25),
36 ZONE_PWR_ON_STATE_MASK
= BIT(26),
37 ZONE_DPG_PWR_STATE_MASK
= BIT(28),
38 ZONE_MEM_PWR_STATE_MASK
= BIT(29),
39 ZONE_RESET_STATE_MASK
= BIT(31),
40 CPU0_PWR_ZONE_CTRL_REG
= 1,
41 CPU_RESET_CONFIG_REG
= 2,
44 static void __iomem
*cpubiuctrl_block
;
45 static void __iomem
*hif_cont_block
;
46 static u32 cpu0_pwr_zone_ctrl_reg
;
47 static u32 cpu_rst_cfg_reg
;
48 static u32 hif_cont_reg
;
50 #ifdef CONFIG_HOTPLUG_CPU
52 * We must quiesce a dying CPU before it can be killed by the boot CPU. Because
53 * one or more cache may be disabled, we must flush to ensure coherency. We
54 * cannot use traditional completion structures or spinlocks as they rely on
57 static DEFINE_PER_CPU_ALIGNED(int, per_cpu_sw_state
);
59 static int per_cpu_sw_state_rd(u32 cpu
)
61 sync_cache_r(SHIFT_PERCPU_PTR(&per_cpu_sw_state
, per_cpu_offset(cpu
)));
62 return per_cpu(per_cpu_sw_state
, cpu
);
65 static void per_cpu_sw_state_wr(u32 cpu
, int val
)
68 per_cpu(per_cpu_sw_state
, cpu
) = val
;
69 sync_cache_w(SHIFT_PERCPU_PTR(&per_cpu_sw_state
, per_cpu_offset(cpu
)));
72 static inline void per_cpu_sw_state_wr(u32 cpu
, int val
) { }
75 static void __iomem
*pwr_ctrl_get_base(u32 cpu
)
77 void __iomem
*base
= cpubiuctrl_block
+ cpu0_pwr_zone_ctrl_reg
;
78 base
+= (cpu_logical_map(cpu
) * 4);
82 static u32
pwr_ctrl_rd(u32 cpu
)
84 void __iomem
*base
= pwr_ctrl_get_base(cpu
);
85 return readl_relaxed(base
);
88 static void pwr_ctrl_set(unsigned int cpu
, u32 val
, u32 mask
)
90 void __iomem
*base
= pwr_ctrl_get_base(cpu
);
91 writel((readl(base
) & mask
) | val
, base
);
94 static void pwr_ctrl_clr(unsigned int cpu
, u32 val
, u32 mask
)
96 void __iomem
*base
= pwr_ctrl_get_base(cpu
);
97 writel((readl(base
) & mask
) & ~val
, base
);
100 #define POLL_TMOUT_MS 500
101 static int pwr_ctrl_wait_tmout(unsigned int cpu
, u32 set
, u32 mask
)
103 const unsigned long timeo
= jiffies
+ msecs_to_jiffies(POLL_TMOUT_MS
);
107 tmp
= pwr_ctrl_rd(cpu
) & mask
;
110 } while (time_before(jiffies
, timeo
));
112 tmp
= pwr_ctrl_rd(cpu
) & mask
;
119 static void cpu_rst_cfg_set(u32 cpu
, int set
)
122 val
= readl_relaxed(cpubiuctrl_block
+ cpu_rst_cfg_reg
);
124 val
|= BIT(cpu_logical_map(cpu
));
126 val
&= ~BIT(cpu_logical_map(cpu
));
127 writel_relaxed(val
, cpubiuctrl_block
+ cpu_rst_cfg_reg
);
130 static void cpu_set_boot_addr(u32 cpu
, unsigned long boot_addr
)
132 const int reg_ofs
= cpu_logical_map(cpu
) * 8;
133 writel_relaxed(0, hif_cont_block
+ hif_cont_reg
+ reg_ofs
);
134 writel_relaxed(boot_addr
, hif_cont_block
+ hif_cont_reg
+ 4 + reg_ofs
);
137 static void brcmstb_cpu_boot(u32 cpu
)
139 /* Mark this CPU as "up" */
140 per_cpu_sw_state_wr(cpu
, 1);
143 * Set the reset vector to point to the secondary_startup
146 cpu_set_boot_addr(cpu
, __pa_symbol(secondary_startup
));
149 cpu_rst_cfg_set(cpu
, 0);
152 static void brcmstb_cpu_power_on(u32 cpu
)
155 * The secondary cores power was cut, so we must go through
156 * power-on initialization.
158 pwr_ctrl_set(cpu
, ZONE_MAN_ISO_CNTL_MASK
, 0xffffff00);
159 pwr_ctrl_set(cpu
, ZONE_MANUAL_CONTROL_MASK
, -1);
160 pwr_ctrl_set(cpu
, ZONE_RESERVED_1_MASK
, -1);
162 pwr_ctrl_set(cpu
, ZONE_MAN_MEM_PWR_MASK
, -1);
164 if (pwr_ctrl_wait_tmout(cpu
, 1, ZONE_MEM_PWR_STATE_MASK
))
165 panic("ZONE_MEM_PWR_STATE_MASK set timeout");
167 pwr_ctrl_set(cpu
, ZONE_MAN_CLKEN_MASK
, -1);
169 if (pwr_ctrl_wait_tmout(cpu
, 1, ZONE_DPG_PWR_STATE_MASK
))
170 panic("ZONE_DPG_PWR_STATE_MASK set timeout");
172 pwr_ctrl_clr(cpu
, ZONE_MAN_ISO_CNTL_MASK
, -1);
173 pwr_ctrl_set(cpu
, ZONE_MAN_RESET_CNTL_MASK
, -1);
176 static int brcmstb_cpu_get_power_state(u32 cpu
)
178 int tmp
= pwr_ctrl_rd(cpu
);
179 return (tmp
& ZONE_RESET_STATE_MASK
) ? 0 : 1;
182 #ifdef CONFIG_HOTPLUG_CPU
184 static void brcmstb_cpu_die(u32 cpu
)
186 v7_exit_coherency_flush(all
);
188 per_cpu_sw_state_wr(cpu
, 0);
190 /* Sit and wait to die */
193 /* We should never get here... */
198 static int brcmstb_cpu_kill(u32 cpu
)
201 * Ordinarily, the hardware forbids power-down of CPU0 (which is good
202 * because it is the boot CPU), but this is not true when using BPCM
203 * manual mode. Consequently, we must avoid turning off CPU0 here to
204 * ensure that TI2C master reset will work.
207 pr_warn("SMP: refusing to power off CPU0\n");
211 while (per_cpu_sw_state_rd(cpu
))
214 pwr_ctrl_set(cpu
, ZONE_MANUAL_CONTROL_MASK
, -1);
215 pwr_ctrl_clr(cpu
, ZONE_MAN_RESET_CNTL_MASK
, -1);
216 pwr_ctrl_clr(cpu
, ZONE_MAN_CLKEN_MASK
, -1);
217 pwr_ctrl_set(cpu
, ZONE_MAN_ISO_CNTL_MASK
, -1);
218 pwr_ctrl_clr(cpu
, ZONE_MAN_MEM_PWR_MASK
, -1);
220 if (pwr_ctrl_wait_tmout(cpu
, 0, ZONE_MEM_PWR_STATE_MASK
))
221 panic("ZONE_MEM_PWR_STATE_MASK clear timeout");
223 pwr_ctrl_clr(cpu
, ZONE_RESERVED_1_MASK
, -1);
225 if (pwr_ctrl_wait_tmout(cpu
, 0, ZONE_DPG_PWR_STATE_MASK
))
226 panic("ZONE_DPG_PWR_STATE_MASK clear timeout");
228 /* Flush pipeline before resetting CPU */
231 /* Assert reset on the CPU */
232 cpu_rst_cfg_set(cpu
, 1);
237 #endif /* CONFIG_HOTPLUG_CPU */
239 static int __init
setup_hifcpubiuctrl_regs(struct device_node
*np
)
243 struct device_node
*syscon_np
= NULL
;
247 syscon_np
= of_parse_phandle(np
, name
, 0);
249 pr_err("can't find phandle %s\n", name
);
254 cpubiuctrl_block
= of_iomap(syscon_np
, 0);
255 if (!cpubiuctrl_block
) {
256 pr_err("iomap failed for cpubiuctrl_block\n");
261 rc
= of_property_read_u32_index(np
, name
, CPU0_PWR_ZONE_CTRL_REG
,
262 &cpu0_pwr_zone_ctrl_reg
);
264 pr_err("failed to read 1st entry from %s property (%d)\n", name
,
270 rc
= of_property_read_u32_index(np
, name
, CPU_RESET_CONFIG_REG
,
273 pr_err("failed to read 2nd entry from %s property (%d)\n", name
,
280 of_node_put(syscon_np
);
284 static int __init
setup_hifcont_regs(struct device_node
*np
)
288 struct device_node
*syscon_np
= NULL
;
290 name
= "syscon-cont";
292 syscon_np
= of_parse_phandle(np
, name
, 0);
294 pr_err("can't find phandle %s\n", name
);
299 hif_cont_block
= of_iomap(syscon_np
, 0);
300 if (!hif_cont_block
) {
301 pr_err("iomap failed for hif_cont_block\n");
306 /* Offset is at top of hif_cont_block */
310 of_node_put(syscon_np
);
314 static void __init
brcmstb_cpu_ctrl_setup(unsigned int max_cpus
)
317 struct device_node
*np
;
320 name
= "brcm,brcmstb-smpboot";
321 np
= of_find_compatible_node(NULL
, NULL
, name
);
323 pr_err("can't find compatible node %s\n", name
);
327 rc
= setup_hifcpubiuctrl_regs(np
);
331 rc
= setup_hifcont_regs(np
);
339 static int brcmstb_boot_secondary(unsigned int cpu
, struct task_struct
*idle
)
341 /* Missing the brcm,brcmstb-smpboot DT node? */
342 if (!cpubiuctrl_block
|| !hif_cont_block
)
345 /* Bring up power to the core if necessary */
346 if (brcmstb_cpu_get_power_state(cpu
) == 0)
347 brcmstb_cpu_power_on(cpu
);
349 brcmstb_cpu_boot(cpu
);
354 static const struct smp_operations brcmstb_smp_ops __initconst
= {
355 .smp_prepare_cpus
= brcmstb_cpu_ctrl_setup
,
356 .smp_boot_secondary
= brcmstb_boot_secondary
,
357 #ifdef CONFIG_HOTPLUG_CPU
358 .cpu_kill
= brcmstb_cpu_kill
,
359 .cpu_die
= brcmstb_cpu_die
,
363 CPU_METHOD_OF_DECLARE(brcmstb_smp
, "brcm,brahma-b15", &brcmstb_smp_ops
);