summaryrefslogtreecommitdiff
path: root/drivers/platform/sprd/platsmp.c
blob: dcccde1aa4be056470f0480a2e508f7e23ec4cf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* linux/arch/arm/mach-sc8830/platsmp.c
 *
 * Copyright (c) 2010-2012 Spreadtrum Co., Ltd.
 *		http://www.spreadtrum.com
 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * Cloned from linux/arch/arm/mach-vexpress/platsmp.c
 *
 *  Copyright (C) 2002 ARM Ltd.
 *  All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/jiffies.h>
#include <linux/smp.h>
#include <linux/io.h>

#include <asm/cacheflush.h>
#include <asm/smp_plat.h>
#include <linux/irqchip/arm-gic.h>
#include <asm/mach-types.h>
#include <asm/smp_scu.h>
#include <asm/unified.h>

#include <soc/sprd/hardware.h>
#include <soc/sprd/sci_glb_regs.h>
#include <soc/sprd/sci.h>
#include <asm/atomic.h>

extern void sci_secondary_startup(void);

#if (defined CONFIG_ARCH_SC8825)
static int __cpuinit boot_secondary_cpus(int cpu_id, u32 paddr)
{
	if (cpu_id != 1)
		return -1;
	writel(paddr,CPU_JUMP_VADDR);
	writel(1 << (cpu_id * 4),HOLDING_PEN_VADDR);
	return 0;
}

#elif (defined CONFIG_ARCH_SCX35)

#define CA7_CORE1_PD (BIT(8)|BIT(9)|BIT(10))
#define CA7_CORE2_PD (BIT(12)|BIT(13)|BIT(14))
#define CA7_CORE3_PD (BIT(16)|BIT(17)|BIT(18))
int scxx30_all_nonboot_cpus_died(void)
{
	u32 val;
	u32 core1_pd , core2_pd , core3_pd;

	val = sci_glb_read(REG_PMU_APB_PWR_STATUS0_DBG, -1UL);

	core1_pd = val & CA7_CORE1_PD;
	core2_pd = val & CA7_CORE2_PD;
	core3_pd = val & CA7_CORE3_PD;

	if(core1_pd && core2_pd && core3_pd){
		pr_debug("*** %s, REG_PMU_APB_PWR_STATUS0_DBG:0x%x ***\n", __func__, val);
		return 1;
	}
	else
		return 0;
}

int poweron_cpus(int cpu)
{
	u32 poweron, val;

	if (cpu == 1)
		poweron = REG_PMU_APB_PD_CA7_C1_CFG;
	else if (cpu == 2)
		poweron = REG_PMU_APB_PD_CA7_C2_CFG;
	else if (cpu == 3)
		poweron = REG_PMU_APB_PD_CA7_C3_CFG;
	else
		return -1;


	val = sci_glb_read(REG_AP_AHB_CA7_RST_SET, -1UL);
	val |= (1 << cpu);
	sci_glb_write(REG_AP_AHB_CA7_RST_SET, val, -1UL);

	val = BITS_PD_CA7_C3_PWR_ON_DLY(4) | BITS_PD_CA7_C3_PWR_ON_SEQ_DLY(4) | BITS_PD_CA7_C3_ISO_ON_DLY(4);
	sci_glb_write(poweron, val, -1UL);

	val = (BIT_PD_CA7_C3_AUTO_SHUTDOWN_EN | __raw_readl(poweron)) &~(BIT_PD_CA7_C3_FORCE_SHUTDOWN);
	sci_glb_write(poweron, val, -1UL);
	dmb();
	udelay(1000);

	while((__raw_readl(poweron)&BIT_PD_CA7_C3_FORCE_SHUTDOWN) ||
		!(__raw_readl(REG_AP_AHB_CA7_RST_SET)&(1 << cpu)) ){
		pr_debug("??? %s set regs failed ???\n", __func__ );
		writel((__raw_readl(REG_AP_AHB_CA7_RST_SET) | (1 << cpu)), REG_AP_AHB_CA7_RST_SET);
		val = (BIT_PD_CA7_C3_AUTO_SHUTDOWN_EN | __raw_readl(poweron)) &~(BIT_PD_CA7_C3_FORCE_SHUTDOWN);
		writel(val,poweron);
		dmb();
		udelay(500);
	}
	writel((__raw_readl(REG_AP_AHB_CA7_RST_SET) & ~(1 << cpu)), REG_AP_AHB_CA7_RST_SET);
	return 0;
}

int powerdown_cpus(int cpu)
{
	u32 poweron, val, i = 0;
	if (cpu == 1)
		poweron = REG_PMU_APB_PD_CA7_C1_CFG;
	else if (cpu == 2)
		poweron = REG_PMU_APB_PD_CA7_C2_CFG;
	else if (cpu == 3)
		poweron = REG_PMU_APB_PD_CA7_C3_CFG;
	else
		return -1;

	val = (BIT_PD_CA7_C3_FORCE_SHUTDOWN | __raw_readl(poweron)) &~(BIT_PD_CA7_C3_AUTO_SHUTDOWN_EN);
	writel(val, poweron);


	while (i < 20) {
		//check power down?
		if (((__raw_readl(REG_PMU_APB_PWR_STATUS0_DBG) >> (4 * (cpu_logical_map(cpu) + 1))) & 0x0f) == 0x07) {
			break;
		}
		udelay(60);
		i++;
	}
	printk("powerdown_cpus i=%d !!\n", i);
	BUG_ON(i >= 20);
	udelay(60);

	return 0;
}

static int __cpuinit boot_secondary_cpus(int cpu_id, u32 paddr)
{
	if (cpu_id < 1 || cpu_id > 3)
		return -1;

	writel(paddr,(CPU_JUMP_VADDR + (cpu_id << 2)));
	writel(readl(HOLDING_PEN_VADDR) | (1 << cpu_id),HOLDING_PEN_VADDR);
	poweron_cpus(cpu_id);

	return 0;
}

#endif

/*
 * control for which core is the next to come out of the secondary
 * boot "holding pen"
 */
extern volatile int pen_release;

/*
 * Write pen_release in a way that is guaranteed to be visible to all
 * observers, irrespective of whether they're taking part in coherency
 * or not.  This is necessary for the hotplug code to work reliably.
 */
static void __cpuinit write_pen_release(int val)
{
	pen_release = val;
	smp_wmb();
	__cpuc_flush_dcache_area((void *)&pen_release, sizeof(pen_release));
	outer_clean_range(__pa(&pen_release), __pa(&pen_release + 1));
}

static DEFINE_SPINLOCK(boot_lock);


unsigned int  sprd_boot_magnum =	0xbadf1a90;

#define SPRD_UP_FLAG0	(0x63507530)
#define SPRD_UP_FLAG1	(0x63507531)
#define SPRD_UP_FLAG2	(0x63507532)
#define SPRD_UP_FLAG3	(0x63507533)

unsigned int g_sprd_boot_flag_1 = 0;
unsigned int g_sprd_boot_flag_2 = 0;
char * ga_boot_flag1[4] =
	{
		"bf10",
		"bf11",
		"bf12",
		"bf13"
	};
char * ga_boot_flag2[4] =
	{
		"bf20",
		"bf21",
		"bf22",
		"bf23"
	};

unsigned int g_sprd_up_flag[4] =
	{
		SPRD_UP_FLAG0,
		SPRD_UP_FLAG1,
		SPRD_UP_FLAG2,
		SPRD_UP_FLAG3
	};

atomic_t boot_lock_cnt  = ATOMIC_INIT(0);
atomic_t boot_unlock_cnt = ATOMIC_INIT(0);

void __cpuinit sprd_secondary_init(unsigned int cpu)
{
	/*
	 * if any interrupts are already enabled for the primary
	 * core (e.g. timer irq), then they will not have been enabled
	 * for us: do so
	 */
	//gic_secondary_init(0);

	/*
	 * let the primary processor know we're out of the
	 * pen, then head off into the C entry point
	 */

	write_pen_release(g_sprd_up_flag[cpu]);

	/*
	 * Synchronise with the boot thread.
	 */
	spin_lock(&boot_lock);
	spin_unlock(&boot_lock);
	atomic_inc(&boot_unlock_cnt);
	g_sprd_boot_flag_1 = 0;
	g_sprd_boot_flag_2 = 0;

}



int __cpuinit sprd_boot_secondary(unsigned int cpu, struct task_struct *idle)
{
	unsigned long timeout;
	int ret;
	unsigned int val = sprd_boot_magnum;

	/*
	 * Set synchronisation state between this boot processor
	 * and the secondary one
	 */
	spin_lock(&boot_lock);
	atomic_inc(&boot_lock_cnt);

	/*
	 * The secondary processor is waiting to be released from
	 * the holding pen - release it, then wait for it to flag
	 * that it has been released by resetting pen_release.
	 *
	 */

	memcpy(&g_sprd_boot_flag_1,ga_boot_flag1[cpu],4);

	val |= (cpu_logical_map(cpu) & 0x0000000f);

	write_pen_release((int)val);

	memcpy(&g_sprd_boot_flag_2,ga_boot_flag2[cpu],4);

	ret = boot_secondary_cpus(cpu, virt_to_phys(sci_secondary_startup));
	if (ret < 0)
		pr_warn("SMP: boot_secondary(%u) error\n", cpu);

	dsb_sev();


	timeout = jiffies + (1 * HZ);

	while (time_before(jiffies, timeout)) {
		smp_rmb();
		if (pen_release == g_sprd_up_flag[cpu])
			break;

		udelay(10);
		dsb_sev();
	}

	spin_unlock(&boot_lock);

	/*
	 * now the secondary core is starting up let it run its
	 * calibrations, then wait for it to finish
	 */
	if(pen_release != g_sprd_up_flag[cpu]){
		printk("*** %s, pen_release:%x ***\n ", __func__, pen_release);
		printk("*** %s, REG_PMU_APB_PWR_STATUS0_DBG:0x%x \n", __func__, sci_glb_read(REG_PMU_APB_PWR_STATUS0_DBG, -1UL));
		printk("*** %s, REG_AP_AHB_CA7_RST_SET:0x%x \n", __func__, sci_glb_read(REG_AP_AHB_CA7_RST_SET, -1UL));
		printk("*** %s, REG_AP_AHB_CA7_STANDBY_STATUS:0x%x \n", __func__, sci_glb_read(REG_AP_AHB_CA7_STANDBY_STATUS, -1UL));
#ifndef CONFIG_ARCH_SCX35L
		printk("*** %s, REG_AON_APB_MPLL_CFG:0x%x \n", __func__, sci_glb_read(REG_AON_APB_MPLL_CFG, -1UL));
#endif
	}

	return pen_release !=  g_sprd_up_flag[cpu] ? -ENOSYS : 0;
}

#ifdef CONFIG_HAVE_ARM_SCU
static unsigned int __get_core_num(void)
{
	void __iomem *scu_base = (void __iomem *)(SPRD_CORE_BASE);
	return (scu_base ? scu_get_core_count(scu_base) : 1);
}
#else
static unsigned int __get_core_num(void)
{
	return 4;
}
#endif

static unsigned int sci_get_core_num(void)
{
	return __get_core_num();
}

/*
 * Initialise the CPU possible map early - this describes the CPUs
 * which may be present or become present in the system.
 */
void __init sprd_smp_init_cpus(void)
{
	unsigned int i, ncores;

	ncores = sci_get_core_num();

	/* sanity check */
	if (ncores > nr_cpu_ids) {
		pr_warn("SMP: %u cores greater than maximum (%u), clipping\n",
			ncores, nr_cpu_ids);
		ncores = nr_cpu_ids;
	}

	for (i = 0; i < ncores; i++)
		set_cpu_possible(i, true);

}
#ifdef CONFIG_FIX_V7TAGRAM_BUG
unsigned long physical_from_idle = 0;
#endif
void __init sprd_smp_prepare_cpus(unsigned int max_cpus)
{
#ifdef CONFIG_HAVE_ARM_SCU
	scu_enable((void __iomem *)(SPRD_CORE_BASE));
#endif
#ifdef CONFIG_FIX_V7TAGRAM_BUG
	extern unsigned long other_cpu_fix_phys;
	extern void fix_tag_ram_bug(void);
	extern void other_cpu_fix(void);

	if(!other_cpu_fix_phys)
		other_cpu_fix_phys = virt_to_phys(other_cpu_fix);
	local_irq_disable();
	fix_tag_ram_bug();
	local_irq_enable();
#endif
}

extern int sprd_cpu_kill(unsigned int cpu);
extern void sprd_cpu_die(unsigned int cpu);
extern int sprd_cpu_disable(unsigned int cpu);

struct smp_operations sprd_smp_ops __initdata = {
	.smp_init_cpus      = sprd_smp_init_cpus,
	.smp_prepare_cpus   = sprd_smp_prepare_cpus,
	.smp_secondary_init = sprd_secondary_init,
	.smp_boot_secondary = sprd_boot_secondary,
#ifdef CONFIG_HOTPLUG_CPU
	.cpu_kill		= sprd_cpu_kill,
	.cpu_disable	= sprd_cpu_disable,
	.cpu_die        = sprd_cpu_die,
#endif
};