xref: /rk3399_rockchip-uboot/arch/powerpc/cpu/mpc85xx/mp.c (revision 3f0997b3255c1498ac92453aa3a7a1cc95914dfd)
1 /*
2  * Copyright 2008-2011 Freescale Semiconductor, Inc.
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 
23 #include <common.h>
24 #include <asm/processor.h>
25 #include <ioports.h>
26 #include <lmb.h>
27 #include <asm/io.h>
28 #include <asm/mmu.h>
29 #include <asm/fsl_law.h>
30 #include <asm/fsl_ddr_sdram.h>
31 #include "mp.h"
32 
33 DECLARE_GLOBAL_DATA_PTR;
34 u32 fsl_ddr_get_intl3r(void);
35 
36 u32 get_my_id()
37 {
38 	return mfspr(SPRN_PIR);
39 }
40 
41 /*
42  * Determine if U-Boot should keep secondary cores in reset, or let them out
43  * of reset and hold them in a spinloop
44  */
45 int hold_cores_in_reset(int verbose)
46 {
47 	const char *s = getenv("mp_holdoff");
48 
49 	/* Default to no, overriden by 'y', 'yes', 'Y', 'Yes', or '1' */
50 	if (s && (*s == 'y' || *s == 'Y' || *s == '1')) {
51 		if (verbose) {
52 			puts("Secondary cores are being held in reset.\n");
53 			puts("See 'mp_holdoff' environment variable\n");
54 		}
55 
56 		return 1;
57 	}
58 
59 	return 0;
60 }
61 
62 int cpu_reset(int nr)
63 {
64 	volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
65 	out_be32(&pic->pir, 1 << nr);
66 	/* the dummy read works around an errata on early 85xx MP PICs */
67 	(void)in_be32(&pic->pir);
68 	out_be32(&pic->pir, 0x0);
69 
70 	return 0;
71 }
72 
73 int cpu_status(int nr)
74 {
75 	u32 *table, id = get_my_id();
76 
77 	if (hold_cores_in_reset(1))
78 		return 0;
79 
80 	if (nr == id) {
81 		table = (u32 *)get_spin_virt_addr();
82 		printf("table base @ 0x%p\n", table);
83 	} else {
84 		table = (u32 *)get_spin_virt_addr() + nr * NUM_BOOT_ENTRY;
85 		printf("Running on cpu %d\n", id);
86 		printf("\n");
87 		printf("table @ 0x%p\n", table);
88 		printf("   addr - 0x%08x\n", table[BOOT_ENTRY_ADDR_LOWER]);
89 		printf("   r3   - 0x%08x\n", table[BOOT_ENTRY_R3_LOWER]);
90 		printf("   pir  - 0x%08x\n", table[BOOT_ENTRY_PIR]);
91 	}
92 
93 	return 0;
94 }
95 
96 #ifdef CONFIG_FSL_CORENET
97 int cpu_disable(int nr)
98 {
99 	volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
100 
101 	setbits_be32(&gur->coredisrl, 1 << nr);
102 
103 	return 0;
104 }
105 
106 int is_core_disabled(int nr) {
107 	ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
108 	u32 coredisrl = in_be32(&gur->coredisrl);
109 
110 	return (coredisrl & (1 << nr));
111 }
112 #else
113 int cpu_disable(int nr)
114 {
115 	volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
116 
117 	switch (nr) {
118 	case 0:
119 		setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_CPU0);
120 		break;
121 	case 1:
122 		setbits_be32(&gur->devdisr, MPC85xx_DEVDISR_CPU1);
123 		break;
124 	default:
125 		printf("Invalid cpu number for disable %d\n", nr);
126 		return 1;
127 	}
128 
129 	return 0;
130 }
131 
132 int is_core_disabled(int nr) {
133 	ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
134 	u32 devdisr = in_be32(&gur->devdisr);
135 
136 	switch (nr) {
137 	case 0:
138 		return (devdisr & MPC85xx_DEVDISR_CPU0);
139 	case 1:
140 		return (devdisr & MPC85xx_DEVDISR_CPU1);
141 	default:
142 		printf("Invalid cpu number for disable %d\n", nr);
143 	}
144 
145 	return 0;
146 }
147 #endif
148 
149 static u8 boot_entry_map[4] = {
150 	0,
151 	BOOT_ENTRY_PIR,
152 	BOOT_ENTRY_R3_LOWER,
153 };
154 
155 int cpu_release(int nr, int argc, char * const argv[])
156 {
157 	u32 i, val, *table = (u32 *)get_spin_virt_addr() + nr * NUM_BOOT_ENTRY;
158 	u64 boot_addr;
159 
160 	if (hold_cores_in_reset(1))
161 		return 0;
162 
163 	if (nr == get_my_id()) {
164 		printf("Invalid to release the boot core.\n\n");
165 		return 1;
166 	}
167 
168 	if (argc != 4) {
169 		printf("Invalid number of arguments to release.\n\n");
170 		return 1;
171 	}
172 
173 	boot_addr = simple_strtoull(argv[0], NULL, 16);
174 
175 	/* handle pir, r3 */
176 	for (i = 1; i < 3; i++) {
177 		if (argv[i][0] != '-') {
178 			u8 entry = boot_entry_map[i];
179 			val = simple_strtoul(argv[i], NULL, 16);
180 			table[entry] = val;
181 		}
182 	}
183 
184 	table[BOOT_ENTRY_ADDR_UPPER] = (u32)(boot_addr >> 32);
185 
186 	/* ensure all table updates complete before final address write */
187 	eieio();
188 
189 	table[BOOT_ENTRY_ADDR_LOWER] = (u32)(boot_addr & 0xffffffff);
190 
191 	return 0;
192 }
193 
194 u32 determine_mp_bootpg(unsigned int *pagesize)
195 {
196 	u32 bootpg;
197 #ifdef CONFIG_SYS_FSL_ERRATUM_A004468
198 	u32 svr = get_svr();
199 	u32 granule_size, check;
200 	struct law_entry e;
201 #endif
202 
203 	/* if we have 4G or more of memory, put the boot page at 4Gb-4k */
204 	if ((u64)gd->ram_size > 0xfffff000)
205 		bootpg = 0xfffff000;
206 	else
207 		bootpg = gd->ram_size - 4096;
208 	if (pagesize)
209 		*pagesize = 4096;
210 
211 #ifdef CONFIG_SYS_FSL_ERRATUM_A004468
212 /*
213  * Erratum A004468 has two parts. The 3-way interleaving applies to T4240,
214  * to be fixed in rev 2.0. The 2-way interleaving applies to many SoCs. But
215  * the way boot page chosen in u-boot avoids hitting this erratum. So only
216  * thw workaround for 3-way interleaving is needed.
217  *
218  * To make sure boot page translation works with 3-Way DDR interleaving
219  * enforce a check for the following constrains
220  * 8K granule size requires BRSIZE=8K and
221  *    bootpg >> log2(BRSIZE) %3 == 1
222  * 4K and 1K granule size requires BRSIZE=4K and
223  *    bootpg >> log2(BRSIZE) %3 == 0
224  */
225 	if (SVR_SOC_VER(svr) == SVR_T4240 && SVR_MAJ(svr) < 2) {
226 		e = find_law(bootpg);
227 		switch (e.trgt_id) {
228 		case LAW_TRGT_IF_DDR_INTLV_123:
229 			granule_size = fsl_ddr_get_intl3r() & 0x1f;
230 			if (granule_size == FSL_DDR_3WAY_8KB_INTERLEAVING) {
231 				if (pagesize)
232 					*pagesize = 8192;
233 				bootpg &= 0xffffe000;	/* align to 8KB */
234 				check = bootpg >> 13;
235 				while ((check % 3) != 1)
236 					check--;
237 				bootpg = check << 13;
238 				debug("Boot page (8K) at 0x%08x\n", bootpg);
239 				break;
240 			} else {
241 				bootpg &= 0xfffff000;	/* align to 4KB */
242 				check = bootpg >> 12;
243 				while ((check % 3) != 0)
244 					check--;
245 				bootpg = check << 12;
246 				debug("Boot page (4K) at 0x%08x\n", bootpg);
247 			}
248 				break;
249 		default:
250 			break;
251 		}
252 	}
253 #endif /* CONFIG_SYS_FSL_ERRATUM_A004468 */
254 
255 	return bootpg;
256 }
257 
258 ulong get_spin_phys_addr(void)
259 {
260 	extern ulong __secondary_start_page;
261 	extern ulong __spin_table;
262 
263 	return (determine_mp_bootpg() +
264 		(ulong)&__spin_table - (ulong)&__secondary_start_page);
265 }
266 
267 ulong get_spin_virt_addr(void)
268 {
269 	extern ulong __secondary_start_page;
270 	extern ulong __spin_table;
271 
272 	return (CONFIG_BPTR_VIRT_ADDR +
273 		(ulong)&__spin_table - (ulong)&__secondary_start_page);
274 }
275 
276 #ifdef CONFIG_FSL_CORENET
277 static void plat_mp_up(unsigned long bootpg, unsigned int pagesize)
278 {
279 	u32 cpu_up_mask, whoami, brsize = LAW_SIZE_4K;
280 	u32 *table = (u32 *)get_spin_virt_addr();
281 	volatile ccsr_gur_t *gur;
282 	volatile ccsr_local_t *ccm;
283 	volatile ccsr_rcpm_t *rcpm;
284 	volatile ccsr_pic_t *pic;
285 	int timeout = 10;
286 	u32 mask = cpu_mask();
287 	struct law_entry e;
288 
289 	gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
290 	ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
291 	rcpm = (void *)(CONFIG_SYS_FSL_CORENET_RCPM_ADDR);
292 	pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
293 
294 	whoami = in_be32(&pic->whoami);
295 	cpu_up_mask = 1 << whoami;
296 	out_be32(&ccm->bstrl, bootpg);
297 
298 	e = find_law(bootpg);
299 	/* pagesize is only 4K or 8K */
300 	if (pagesize == 8192)
301 		brsize = LAW_SIZE_8K;
302 	out_be32(&ccm->bstrar, LAW_EN | e.trgt_id << 20 | brsize);
303 	debug("BRSIZE is 0x%x\n", brsize);
304 
305 	/* readback to sync write */
306 	in_be32(&ccm->bstrar);
307 
308 	/* disable time base at the platform */
309 	out_be32(&rcpm->ctbenrl, cpu_up_mask);
310 
311 	out_be32(&gur->brrl, mask);
312 
313 	/* wait for everyone */
314 	while (timeout) {
315 		unsigned int i, cpu, nr_cpus = cpu_numcores();
316 
317 		for_each_cpu(i, cpu, nr_cpus, mask) {
318 			if (table[cpu * NUM_BOOT_ENTRY + BOOT_ENTRY_ADDR_LOWER])
319 				cpu_up_mask |= (1 << cpu);
320 		}
321 
322 		if ((cpu_up_mask & mask) == mask)
323 			break;
324 
325 		udelay(100);
326 		timeout--;
327 	}
328 
329 	if (timeout == 0)
330 		printf("CPU up timeout. CPU up mask is %x should be %x\n",
331 			cpu_up_mask, mask);
332 
333 	/* enable time base at the platform */
334 	out_be32(&rcpm->ctbenrl, 0);
335 
336 	/* readback to sync write */
337 	in_be32(&rcpm->ctbenrl);
338 
339 	mtspr(SPRN_TBWU, 0);
340 	mtspr(SPRN_TBWL, 0);
341 
342 	out_be32(&rcpm->ctbenrl, mask);
343 
344 #ifdef CONFIG_MPC8xxx_DISABLE_BPTR
345 	/*
346 	 * Disabling Boot Page Translation allows the memory region 0xfffff000
347 	 * to 0xffffffff to be used normally.  Leaving Boot Page Translation
348 	 * enabled remaps 0xfffff000 to SDRAM which makes that memory region
349 	 * unusable for normal operation but it does allow OSes to easily
350 	 * reset a processor core to put it back into U-Boot's spinloop.
351 	 */
352 	clrbits_be32(&ccm->bstrar, LAW_EN);
353 #endif
354 }
355 #else
356 static void plat_mp_up(unsigned long bootpg, unsigned int pagesize)
357 {
358 	u32 up, cpu_up_mask, whoami;
359 	u32 *table = (u32 *)get_spin_virt_addr();
360 	volatile u32 bpcr;
361 	volatile ccsr_local_ecm_t *ecm = (void *)(CONFIG_SYS_MPC85xx_ECM_ADDR);
362 	volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
363 	volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
364 	u32 devdisr;
365 	int timeout = 10;
366 
367 	whoami = in_be32(&pic->whoami);
368 	out_be32(&ecm->bptr, 0x80000000 | (bootpg >> 12));
369 
370 	/* disable time base at the platform */
371 	devdisr = in_be32(&gur->devdisr);
372 	if (whoami)
373 		devdisr |= MPC85xx_DEVDISR_TB0;
374 	else
375 		devdisr |= MPC85xx_DEVDISR_TB1;
376 	out_be32(&gur->devdisr, devdisr);
377 
378 	/* release the hounds */
379 	up = ((1 << cpu_numcores()) - 1);
380 	bpcr = in_be32(&ecm->eebpcr);
381 	bpcr |= (up << 24);
382 	out_be32(&ecm->eebpcr, bpcr);
383 	asm("sync; isync; msync");
384 
385 	cpu_up_mask = 1 << whoami;
386 	/* wait for everyone */
387 	while (timeout) {
388 		int i;
389 		for (i = 0; i < cpu_numcores(); i++) {
390 			if (table[i * NUM_BOOT_ENTRY + BOOT_ENTRY_ADDR_LOWER])
391 				cpu_up_mask |= (1 << i);
392 		};
393 
394 		if ((cpu_up_mask & up) == up)
395 			break;
396 
397 		udelay(100);
398 		timeout--;
399 	}
400 
401 	if (timeout == 0)
402 		printf("CPU up timeout. CPU up mask is %x should be %x\n",
403 			cpu_up_mask, up);
404 
405 	/* enable time base at the platform */
406 	if (whoami)
407 		devdisr |= MPC85xx_DEVDISR_TB1;
408 	else
409 		devdisr |= MPC85xx_DEVDISR_TB0;
410 	out_be32(&gur->devdisr, devdisr);
411 
412 	/* readback to sync write */
413 	in_be32(&gur->devdisr);
414 
415 	mtspr(SPRN_TBWU, 0);
416 	mtspr(SPRN_TBWL, 0);
417 
418 	devdisr &= ~(MPC85xx_DEVDISR_TB0 | MPC85xx_DEVDISR_TB1);
419 	out_be32(&gur->devdisr, devdisr);
420 
421 #ifdef CONFIG_MPC8xxx_DISABLE_BPTR
422 	/*
423 	 * Disabling Boot Page Translation allows the memory region 0xfffff000
424 	 * to 0xffffffff to be used normally.  Leaving Boot Page Translation
425 	 * enabled remaps 0xfffff000 to SDRAM which makes that memory region
426 	 * unusable for normal operation but it does allow OSes to easily
427 	 * reset a processor core to put it back into U-Boot's spinloop.
428 	 */
429 	clrbits_be32(&ecm->bptr, 0x80000000);
430 #endif
431 }
432 #endif
433 
434 void cpu_mp_lmb_reserve(struct lmb *lmb)
435 {
436 	u32 bootpg = determine_mp_bootpg(NULL);
437 
438 	lmb_reserve(lmb, bootpg, 4096);
439 }
440 
441 void setup_mp(void)
442 {
443 	extern ulong __secondary_start_page;
444 	extern ulong __bootpg_addr;
445 
446 	ulong fixup = (ulong)&__secondary_start_page;
447 	u32 bootpg, bootpg_map, pagesize;
448 
449 	bootpg = determine_mp_bootpg(&pagesize);
450 
451 	/*
452 	 * pagesize is only 4K or 8K
453 	 * we only use the last 4K of boot page
454 	 * bootpg_map saves the address for the boot page
455 	 * 8K is used for the workaround of 3-way DDR interleaving
456 	 */
457 
458 	bootpg_map = bootpg;
459 
460 	if (pagesize == 8192)
461 		bootpg += 4096;	/* use 2nd half */
462 
463 	/* Some OSes expect secondary cores to be held in reset */
464 	if (hold_cores_in_reset(0))
465 		return;
466 
467 	/* Store the bootpg's SDRAM address for use by secondary CPU cores */
468 	__bootpg_addr = bootpg;
469 
470 	/* look for the tlb covering the reset page, there better be one */
471 	int i = find_tlb_idx((void *)CONFIG_BPTR_VIRT_ADDR, 1);
472 
473 	/* we found a match */
474 	if (i != -1) {
475 		/* map reset page to bootpg so we can copy code there */
476 		disable_tlb(i);
477 
478 		set_tlb(1, CONFIG_BPTR_VIRT_ADDR, bootpg, /* tlb, epn, rpn */
479 			MAS3_SX|MAS3_SW|MAS3_SR, MAS2_I|MAS2_G, /* perms, wimge */
480 			0, i, BOOKE_PAGESZ_4K, 1); /* ts, esel, tsize, iprot */
481 
482 		memcpy((void *)CONFIG_BPTR_VIRT_ADDR, (void *)fixup, 4096);
483 
484 		plat_mp_up(bootpg_map, pagesize);
485 	} else {
486 		puts("WARNING: No reset page TLB. "
487 			"Skipping secondary core setup\n");
488 	}
489 }
490