xref: /rk3399_rockchip-uboot/arch/powerpc/cpu/mpc85xx/cpu.c (revision 98ffa19053f2d10578a227de4e441698226fde0a)
1 /*
2  * Copyright 2004,2007-2011 Freescale Semiconductor, Inc.
3  * (C) Copyright 2002, 2003 Motorola Inc.
4  * Xianghua Xiao (X.Xiao@motorola.com)
5  *
6  * (C) Copyright 2000
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27 
28 #include <config.h>
29 #include <common.h>
30 #include <watchdog.h>
31 #include <command.h>
32 #include <fsl_esdhc.h>
33 #include <asm/cache.h>
34 #include <asm/io.h>
35 #include <asm/mmu.h>
36 #include <asm/fsl_ifc.h>
37 #include <asm/fsl_law.h>
38 #include <asm/fsl_lbc.h>
39 #include <post.h>
40 #include <asm/processor.h>
41 #include <asm/fsl_ddr_sdram.h>
42 
43 DECLARE_GLOBAL_DATA_PTR;
44 
45 /*
46  * Default board reset function
47  */
48 static void
49 __board_reset(void)
50 {
51 	/* Do nothing */
52 }
53 void board_reset(void) __attribute__((weak, alias("__board_reset")));
54 
55 int checkcpu (void)
56 {
57 	sys_info_t sysinfo;
58 	uint pvr, svr;
59 	uint ver;
60 	uint major, minor;
61 	struct cpu_type *cpu;
62 	char buf1[32], buf2[32];
63 #if (defined(CONFIG_DDR_CLK_FREQ) || \
64 	defined(CONFIG_FSL_CORENET)) && !defined(CONFIG_SYS_FSL_QORIQ_CHASSIS2)
65 	volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
66 #endif /* CONFIG_FSL_CORENET */
67 
68 	/*
69 	 * Cornet platforms use ddr sync bit in RCW to indicate sync vs async
70 	 * mode. Previous platform use ddr ratio to do the same. This
71 	 * information is only for display here.
72 	 */
73 #ifdef CONFIG_FSL_CORENET
74 #ifdef CONFIG_SYS_FSL_QORIQ_CHASSIS2
75 	u32 ddr_sync = 0;	/* only async mode is supported */
76 #else
77 	u32 ddr_sync = ((gur->rcwsr[5]) & FSL_CORENET_RCWSR5_DDR_SYNC)
78 		>> FSL_CORENET_RCWSR5_DDR_SYNC_SHIFT;
79 #endif /* CONFIG_SYS_FSL_QORIQ_CHASSIS2 */
80 #else	/* CONFIG_FSL_CORENET */
81 #ifdef CONFIG_DDR_CLK_FREQ
82 	u32 ddr_ratio = ((gur->porpllsr) & MPC85xx_PORPLLSR_DDR_RATIO)
83 		>> MPC85xx_PORPLLSR_DDR_RATIO_SHIFT;
84 #else
85 	u32 ddr_ratio = 0;
86 #endif /* CONFIG_DDR_CLK_FREQ */
87 #endif /* CONFIG_FSL_CORENET */
88 
89 	unsigned int i, core, nr_cores = cpu_numcores();
90 	u32 mask = cpu_mask();
91 
92 	svr = get_svr();
93 	major = SVR_MAJ(svr);
94 	minor = SVR_MIN(svr);
95 
96 	if (cpu_numcores() > 1) {
97 #ifndef CONFIG_MP
98 		puts("Unicore software on multiprocessor system!!\n"
99 		     "To enable mutlticore build define CONFIG_MP\n");
100 #endif
101 		volatile ccsr_pic_t *pic = (void *)(CONFIG_SYS_MPC8xxx_PIC_ADDR);
102 		printf("CPU%d:  ", pic->whoami);
103 	} else {
104 		puts("CPU:   ");
105 	}
106 
107 	cpu = gd->cpu;
108 
109 	puts(cpu->name);
110 	if (IS_E_PROCESSOR(svr))
111 		puts("E");
112 
113 	printf(", Version: %d.%d, (0x%08x)\n", major, minor, svr);
114 
115 	pvr = get_pvr();
116 	ver = PVR_VER(pvr);
117 	major = PVR_MAJ(pvr);
118 	minor = PVR_MIN(pvr);
119 
120 	printf("Core:  ");
121 	switch(ver) {
122 	case PVR_VER_E500_V1:
123 	case PVR_VER_E500_V2:
124 		puts("E500");
125 		break;
126 	case PVR_VER_E500MC:
127 		puts("E500MC");
128 		break;
129 	case PVR_VER_E5500:
130 		puts("E5500");
131 		break;
132 	case PVR_VER_E6500:
133 		puts("E6500");
134 		break;
135 	default:
136 		puts("Unknown");
137 		break;
138 	}
139 
140 	printf(", Version: %d.%d, (0x%08x)\n", major, minor, pvr);
141 
142 	if (nr_cores > CONFIG_MAX_CPUS) {
143 		panic("\nUnexpected number of cores: %d, max is %d\n",
144 			nr_cores, CONFIG_MAX_CPUS);
145 	}
146 
147 	get_sys_info(&sysinfo);
148 
149 	puts("Clock Configuration:");
150 	for_each_cpu(i, core, nr_cores, mask) {
151 		if (!(i & 3))
152 			printf ("\n       ");
153 		printf("CPU%d:%-4s MHz, ", core,
154 			strmhz(buf1, sysinfo.freqProcessor[core]));
155 	}
156 	printf("\n       CCB:%-4s MHz,\n", strmhz(buf1, sysinfo.freqSystemBus));
157 
158 #ifdef CONFIG_FSL_CORENET
159 	if (ddr_sync == 1) {
160 		printf("       DDR:%-4s MHz (%s MT/s data rate) "
161 			"(Synchronous), ",
162 			strmhz(buf1, sysinfo.freqDDRBus/2),
163 			strmhz(buf2, sysinfo.freqDDRBus));
164 	} else {
165 		printf("       DDR:%-4s MHz (%s MT/s data rate) "
166 			"(Asynchronous), ",
167 			strmhz(buf1, sysinfo.freqDDRBus/2),
168 			strmhz(buf2, sysinfo.freqDDRBus));
169 	}
170 #else
171 	switch (ddr_ratio) {
172 	case 0x0:
173 		printf("       DDR:%-4s MHz (%s MT/s data rate), ",
174 			strmhz(buf1, sysinfo.freqDDRBus/2),
175 			strmhz(buf2, sysinfo.freqDDRBus));
176 		break;
177 	case 0x7:
178 		printf("       DDR:%-4s MHz (%s MT/s data rate) "
179 			"(Synchronous), ",
180 			strmhz(buf1, sysinfo.freqDDRBus/2),
181 			strmhz(buf2, sysinfo.freqDDRBus));
182 		break;
183 	default:
184 		printf("       DDR:%-4s MHz (%s MT/s data rate) "
185 			"(Asynchronous), ",
186 			strmhz(buf1, sysinfo.freqDDRBus/2),
187 			strmhz(buf2, sysinfo.freqDDRBus));
188 		break;
189 	}
190 #endif
191 
192 #if defined(CONFIG_FSL_LBC)
193 	if (sysinfo.freqLocalBus > LCRR_CLKDIV) {
194 		printf("LBC:%-4s MHz\n", strmhz(buf1, sysinfo.freqLocalBus));
195 	} else {
196 		printf("LBC: unknown (LCRR[CLKDIV] = 0x%02lx)\n",
197 		       sysinfo.freqLocalBus);
198 	}
199 #endif
200 
201 #if defined(CONFIG_FSL_IFC)
202 	printf("IFC:%-4s MHz\n", strmhz(buf1, sysinfo.freqLocalBus));
203 #endif
204 
205 #ifdef CONFIG_CPM2
206 	printf("CPM:   %s MHz\n", strmhz(buf1, sysinfo.freqSystemBus));
207 #endif
208 
209 #ifdef CONFIG_QE
210 	printf("       QE:%-4s MHz\n", strmhz(buf1, sysinfo.freqQE));
211 #endif
212 
213 #ifdef CONFIG_SYS_DPAA_FMAN
214 	for (i = 0; i < CONFIG_SYS_NUM_FMAN; i++) {
215 		printf("       FMAN%d: %s MHz\n", i + 1,
216 			strmhz(buf1, sysinfo.freqFMan[i]));
217 	}
218 #endif
219 
220 #ifdef CONFIG_SYS_DPAA_PME
221 	printf("       PME:   %s MHz\n", strmhz(buf1, sysinfo.freqPME));
222 #endif
223 
224 	puts("L1:    D-cache 32 kB enabled\n       I-cache 32 kB enabled\n");
225 
226 	return 0;
227 }
228 
229 
230 /* ------------------------------------------------------------------------- */
231 
232 int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
233 {
234 /* Everything after the first generation of PQ3 parts has RSTCR */
235 #if defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || \
236     defined(CONFIG_MPC8555) || defined(CONFIG_MPC8560)
237 	unsigned long val, msr;
238 
239 	/*
240 	 * Initiate hard reset in debug control register DBCR0
241 	 * Make sure MSR[DE] = 1.  This only resets the core.
242 	 */
243 	msr = mfmsr ();
244 	msr |= MSR_DE;
245 	mtmsr (msr);
246 
247 	val = mfspr(DBCR0);
248 	val |= 0x70000000;
249 	mtspr(DBCR0,val);
250 #else
251 	volatile ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
252 
253 	/* Attempt board-specific reset */
254 	board_reset();
255 
256 	/* Next try asserting HRESET_REQ */
257 	out_be32(&gur->rstcr, 0x2);
258 	udelay(100);
259 #endif
260 
261 	return 1;
262 }
263 
264 
265 /*
266  * Get timebase clock frequency
267  */
268 #ifndef CONFIG_SYS_FSL_TBCLK_DIV
269 #define CONFIG_SYS_FSL_TBCLK_DIV 8
270 #endif
271 unsigned long get_tbclk (void)
272 {
273 	unsigned long tbclk_div = CONFIG_SYS_FSL_TBCLK_DIV;
274 
275 	return (gd->bus_clk + (tbclk_div >> 1)) / tbclk_div;
276 }
277 
278 
279 #if defined(CONFIG_WATCHDOG)
280 void
281 watchdog_reset(void)
282 {
283 	int re_enable = disable_interrupts();
284 	reset_85xx_watchdog();
285 	if (re_enable) enable_interrupts();
286 }
287 
288 void
289 reset_85xx_watchdog(void)
290 {
291 	/*
292 	 * Clear TSR(WIS) bit by writing 1
293 	 */
294 	mtspr(SPRN_TSR, TSR_WIS);
295 }
296 #endif	/* CONFIG_WATCHDOG */
297 
298 /*
299  * Initializes on-chip MMC controllers.
300  * to override, implement board_mmc_init()
301  */
302 int cpu_mmc_init(bd_t *bis)
303 {
304 #ifdef CONFIG_FSL_ESDHC
305 	return fsl_esdhc_mmc_init(bis);
306 #else
307 	return 0;
308 #endif
309 }
310 
311 /*
312  * Print out the state of various machine registers.
313  * Currently prints out LAWs, BR0/OR0 for LBC, CSPR/CSOR/Timing
314  * parameters for IFC and TLBs
315  */
316 void mpc85xx_reginfo(void)
317 {
318 	print_tlbcam();
319 	print_laws();
320 #if defined(CONFIG_FSL_LBC)
321 	print_lbc_regs();
322 #endif
323 #ifdef CONFIG_FSL_IFC
324 	print_ifc_regs();
325 #endif
326 
327 }
328 
329 /* Common ddr init for non-corenet fsl 85xx platforms */
330 #ifndef CONFIG_FSL_CORENET
331 #if defined(CONFIG_SYS_RAMBOOT) && !defined(CONFIG_SYS_INIT_L2_ADDR)
332 phys_size_t initdram(int board_type)
333 {
334 #if defined(CONFIG_SPD_EEPROM) || defined(CONFIG_DDR_SPD)
335 	return fsl_ddr_sdram_size();
336 #else
337 	return CONFIG_SYS_SDRAM_SIZE * 1024 * 1024;
338 #endif
339 }
340 #else /* CONFIG_SYS_RAMBOOT */
341 phys_size_t initdram(int board_type)
342 {
343 	phys_size_t dram_size = 0;
344 
345 #if defined(CONFIG_SYS_FSL_ERRATUM_DDR_MSYNC_IN)
346 	{
347 		ccsr_gur_t *gur = (void *)(CONFIG_SYS_MPC85xx_GUTS_ADDR);
348 		unsigned int x = 10;
349 		unsigned int i;
350 
351 		/*
352 		 * Work around to stabilize DDR DLL
353 		 */
354 		out_be32(&gur->ddrdllcr, 0x81000000);
355 		asm("sync;isync;msync");
356 		udelay(200);
357 		while (in_be32(&gur->ddrdllcr) != 0x81000100) {
358 			setbits_be32(&gur->devdisr, 0x00010000);
359 			for (i = 0; i < x; i++)
360 				;
361 			clrbits_be32(&gur->devdisr, 0x00010000);
362 			x++;
363 		}
364 	}
365 #endif
366 
367 #if	defined(CONFIG_SPD_EEPROM)	|| \
368 	defined(CONFIG_DDR_SPD)		|| \
369 	defined(CONFIG_SYS_DDR_RAW_TIMING)
370 	dram_size = fsl_ddr_sdram();
371 #else
372 	dram_size = fixed_sdram();
373 #endif
374 	dram_size = setup_ddr_tlbs(dram_size / 0x100000);
375 	dram_size *= 0x100000;
376 
377 #if defined(CONFIG_DDR_ECC) && !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
378 	/*
379 	 * Initialize and enable DDR ECC.
380 	 */
381 	ddr_enable_ecc(dram_size);
382 #endif
383 
384 #if defined(CONFIG_FSL_LBC)
385 	/* Some boards also have sdram on the lbc */
386 	lbc_sdram_init();
387 #endif
388 
389 	debug("DDR: ");
390 	return dram_size;
391 }
392 #endif /* CONFIG_SYS_RAMBOOT */
393 #endif
394 
395 #if CONFIG_POST & CONFIG_SYS_POST_MEMORY
396 
397 /* Board-specific functions defined in each board's ddr.c */
398 void fsl_ddr_get_spd(generic_spd_eeprom_t *ctrl_dimms_spd,
399 	unsigned int ctrl_num);
400 void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
401 		       phys_addr_t *rpn);
402 unsigned int
403 	setup_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg);
404 
405 void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg);
406 
407 static void dump_spd_ddr_reg(void)
408 {
409 	int i, j, k, m;
410 	u8 *p_8;
411 	u32 *p_32;
412 	ccsr_ddr_t *ddr[CONFIG_NUM_DDR_CONTROLLERS];
413 	generic_spd_eeprom_t
414 		spd[CONFIG_NUM_DDR_CONTROLLERS][CONFIG_DIMM_SLOTS_PER_CTLR];
415 
416 	for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
417 		fsl_ddr_get_spd(spd[i], i);
418 
419 	puts("SPD data of all dimms (zero vaule is omitted)...\n");
420 	puts("Byte (hex)  ");
421 	k = 1;
422 	for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
423 		for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++)
424 			printf("Dimm%d ", k++);
425 	}
426 	puts("\n");
427 	for (k = 0; k < sizeof(generic_spd_eeprom_t); k++) {
428 		m = 0;
429 		printf("%3d (0x%02x)  ", k, k);
430 		for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
431 			for (j = 0; j < CONFIG_DIMM_SLOTS_PER_CTLR; j++) {
432 				p_8 = (u8 *) &spd[i][j];
433 				if (p_8[k]) {
434 					printf("0x%02x  ", p_8[k]);
435 					m++;
436 				} else
437 					puts("      ");
438 			}
439 		}
440 		if (m)
441 			puts("\n");
442 		else
443 			puts("\r");
444 	}
445 
446 	for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
447 		switch (i) {
448 		case 0:
449 			ddr[i] = (void *)CONFIG_SYS_MPC85xx_DDR_ADDR;
450 			break;
451 #if defined(CONFIG_SYS_MPC85xx_DDR2_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 1)
452 		case 1:
453 			ddr[i] = (void *)CONFIG_SYS_MPC85xx_DDR2_ADDR;
454 			break;
455 #endif
456 #if defined(CONFIG_SYS_MPC85xx_DDR3_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 2)
457 		case 2:
458 			ddr[i] = (void *)CONFIG_SYS_MPC85xx_DDR3_ADDR;
459 			break;
460 #endif
461 #if defined(CONFIG_SYS_MPC85xx_DDR4_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 3)
462 		case 3:
463 			ddr[i] = (void *)CONFIG_SYS_MPC85xx_DDR4_ADDR;
464 			break;
465 #endif
466 		default:
467 			printf("%s unexpected controller number = %u\n",
468 				__func__, i);
469 			return;
470 		}
471 	}
472 	printf("DDR registers dump for all controllers "
473 		"(zero vaule is omitted)...\n");
474 	puts("Offset (hex)   ");
475 	for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++)
476 		printf("     Base + 0x%04x", (u32)ddr[i] & 0xFFFF);
477 	puts("\n");
478 	for (k = 0; k < sizeof(ccsr_ddr_t)/4; k++) {
479 		m = 0;
480 		printf("%6d (0x%04x)", k * 4, k * 4);
481 		for (i = 0; i < CONFIG_NUM_DDR_CONTROLLERS; i++) {
482 			p_32 = (u32 *) ddr[i];
483 			if (p_32[k]) {
484 				printf("        0x%08x", p_32[k]);
485 				m++;
486 			} else
487 				puts("                  ");
488 		}
489 		if (m)
490 			puts("\n");
491 		else
492 			puts("\r");
493 	}
494 	puts("\n");
495 }
496 
497 /* invalid the TLBs for DDR and setup new ones to cover p_addr */
498 static int reset_tlb(phys_addr_t p_addr, u32 size, phys_addr_t *phys_offset)
499 {
500 	u32 vstart = CONFIG_SYS_DDR_SDRAM_BASE;
501 	unsigned long epn;
502 	u32 tsize, valid, ptr;
503 	int ddr_esel;
504 
505 	clear_ddr_tlbs_phys(p_addr, size>>20);
506 
507 	/* Setup new tlb to cover the physical address */
508 	setup_ddr_tlbs_phys(p_addr, size>>20);
509 
510 	ptr = vstart;
511 	ddr_esel = find_tlb_idx((void *)ptr, 1);
512 	if (ddr_esel != -1) {
513 		read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, phys_offset);
514 	} else {
515 		printf("TLB error in function %s\n", __func__);
516 		return -1;
517 	}
518 
519 	return 0;
520 }
521 
522 /*
523  * slide the testing window up to test another area
524  * for 32_bit system, the maximum testable memory is limited to
525  * CONFIG_MAX_MEM_MAPPED
526  */
527 int arch_memory_test_advance(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
528 {
529 	phys_addr_t test_cap, p_addr;
530 	phys_size_t p_size = min(gd->ram_size, CONFIG_MAX_MEM_MAPPED);
531 
532 #if !defined(CONFIG_PHYS_64BIT) || \
533     !defined(CONFIG_SYS_INIT_RAM_ADDR_PHYS) || \
534 	(CONFIG_SYS_INIT_RAM_ADDR_PHYS < 0x100000000ull)
535 		test_cap = p_size;
536 #else
537 		test_cap = gd->ram_size;
538 #endif
539 	p_addr = (*vstart) + (*size) + (*phys_offset);
540 	if (p_addr < test_cap - 1) {
541 		p_size = min(test_cap - p_addr, CONFIG_MAX_MEM_MAPPED);
542 		if (reset_tlb(p_addr, p_size, phys_offset) == -1)
543 			return -1;
544 		*vstart = CONFIG_SYS_DDR_SDRAM_BASE;
545 		*size = (u32) p_size;
546 		printf("Testing 0x%08llx - 0x%08llx\n",
547 			(u64)(*vstart) + (*phys_offset),
548 			(u64)(*vstart) + (*phys_offset) + (*size) - 1);
549 	} else
550 		return 1;
551 
552 	return 0;
553 }
554 
555 /* initialization for testing area */
556 int arch_memory_test_prepare(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
557 {
558 	phys_size_t p_size = min(gd->ram_size, CONFIG_MAX_MEM_MAPPED);
559 
560 	*vstart = CONFIG_SYS_DDR_SDRAM_BASE;
561 	*size = (u32) p_size;	/* CONFIG_MAX_MEM_MAPPED < 4G */
562 	*phys_offset = 0;
563 
564 #if !defined(CONFIG_PHYS_64BIT) || \
565     !defined(CONFIG_SYS_INIT_RAM_ADDR_PHYS) || \
566 	(CONFIG_SYS_INIT_RAM_ADDR_PHYS < 0x100000000ull)
567 		if (gd->ram_size > CONFIG_MAX_MEM_MAPPED) {
568 			puts("Cannot test more than ");
569 			print_size(CONFIG_MAX_MEM_MAPPED,
570 				" without proper 36BIT support.\n");
571 		}
572 #endif
573 	printf("Testing 0x%08llx - 0x%08llx\n",
574 		(u64)(*vstart) + (*phys_offset),
575 		(u64)(*vstart) + (*phys_offset) + (*size) - 1);
576 
577 	return 0;
578 }
579 
580 /* invalid TLBs for DDR and remap as normal after testing */
581 int arch_memory_test_cleanup(u32 *vstart, u32 *size, phys_addr_t *phys_offset)
582 {
583 	unsigned long epn;
584 	u32 tsize, valid, ptr;
585 	phys_addr_t rpn = 0;
586 	int ddr_esel;
587 
588 	/* disable the TLBs for this testing */
589 	ptr = *vstart;
590 
591 	while (ptr < (*vstart) + (*size)) {
592 		ddr_esel = find_tlb_idx((void *)ptr, 1);
593 		if (ddr_esel != -1) {
594 			read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, &rpn);
595 			disable_tlb(ddr_esel);
596 		}
597 		ptr += TSIZE_TO_BYTES(tsize);
598 	}
599 
600 	puts("Remap DDR ");
601 	setup_ddr_tlbs(gd->ram_size>>20);
602 	puts("\n");
603 
604 	return 0;
605 }
606 
607 void arch_memory_failure_handle(void)
608 {
609 	dump_spd_ddr_reg();
610 }
611 #endif
612