xref: /rk3399_rockchip-uboot/common/spl/spl.c (revision 96a4cf48b5789204d57e728b42e19bf62157cb65)
1 /*
2  * (C) Copyright 2010
3  * Texas Instruments, <www.ti.com>
4  *
5  * Aneesh V <aneesh@ti.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <spl.h>
13 #include <asm/sections.h>
14 #include <asm/u-boot.h>
15 #include <nand.h>
16 #include <fat.h>
17 #include <version.h>
18 #include <image.h>
19 #include <malloc.h>
20 #include <dm/root.h>
21 #include <linux/compiler.h>
22 #include <fdt_support.h>
23 
24 DECLARE_GLOBAL_DATA_PTR;
25 
26 #ifndef CONFIG_SYS_UBOOT_START
27 #define CONFIG_SYS_UBOOT_START	CONFIG_SYS_TEXT_BASE
28 #endif
29 #ifndef CONFIG_SYS_MONITOR_LEN
30 /* Unknown U-Boot size, let's assume it will not be more than 200 KB */
31 #define CONFIG_SYS_MONITOR_LEN	(200 * 1024)
32 #endif
33 
34 u32 *boot_params_ptr = NULL;
35 
36 /* Define board data structure */
37 static bd_t bdata __attribute__ ((section(".data")));
38 
39 /*
40  * Board-specific Platform code can reimplement show_boot_progress () if needed
41  */
42 __weak void show_boot_progress(int val) {}
43 
44 /*
45  * Default function to determine if u-boot or the OS should
46  * be started. This implementation always returns 1.
47  *
48  * Please implement your own board specific funcion to do this.
49  *
50  * RETURN
51  * 0 to not start u-boot
52  * positive if u-boot should start
53  */
54 #ifdef CONFIG_SPL_OS_BOOT
55 __weak int spl_start_uboot(void)
56 {
57 	puts("SPL: Please implement spl_start_uboot() for your board\n");
58 	puts("SPL: Direct Linux boot not active!\n");
59 	return 1;
60 }
61 
62 /* weak default platform specific function to initialize
63  * dram banks
64  */
65 __weak int dram_init_banksize(void)
66 {
67 	return 0;
68 }
69 
70 /*
71  * Weak default function for arch specific zImage check. Return zero
72  * and fill start and end address if image is recognized.
73  */
74 int __weak bootz_setup(ulong image, ulong *start, ulong *end)
75 {
76 	 return 1;
77 }
78 #endif
79 
80 /* Weak default function for arch/board-specific fixups to the spl_image_info */
81 void __weak spl_perform_fixups(struct spl_image_info *spl_image)
82 {
83 }
84 
85 void spl_fixup_fdt(void)
86 {
87 #if defined(CONFIG_SPL_OF_LIBFDT) && defined(CONFIG_SYS_SPL_ARGS_ADDR)
88 	void *fdt_blob = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
89 	int err;
90 
91 	err = fdt_check_header(fdt_blob);
92 	if (err < 0) {
93 		printf("fdt_root: %s\n", fdt_strerror(err));
94 		return;
95 	}
96 
97 	/* fixup the memory dt node */
98 	err = fdt_shrink_to_minimum(fdt_blob, 0);
99 	if (err == 0) {
100 		printf("spl: fdt_shrink_to_minimum err - %d\n", err);
101 		return;
102 	}
103 
104 	err = arch_fixup_fdt(fdt_blob);
105 	if (err) {
106 		printf("spl: arch_fixup_fdt err - %d\n", err);
107 		return;
108 	}
109 #endif
110 }
111 
112 /*
113  * Weak default function for board specific cleanup/preparation before
114  * Linux boot. Some boards/platforms might not need it, so just provide
115  * an empty stub here.
116  */
117 __weak void spl_board_prepare_for_linux(void)
118 {
119 	/* Nothing to do! */
120 }
121 
122 __weak void spl_board_prepare_for_boot(void)
123 {
124 	/* Nothing to do! */
125 }
126 
127 void spl_set_header_raw_uboot(struct spl_image_info *spl_image)
128 {
129 	spl_image->size = CONFIG_SYS_MONITOR_LEN;
130 	spl_image->entry_point = CONFIG_SYS_UBOOT_START;
131 	spl_image->load_addr = CONFIG_SYS_TEXT_BASE;
132 	spl_image->os = IH_OS_U_BOOT;
133 	spl_image->name = "U-Boot";
134 }
135 
136 int spl_parse_image_header(struct spl_image_info *spl_image,
137 			   const struct image_header *header)
138 {
139 	if (image_get_magic(header) == IH_MAGIC) {
140 #ifdef CONFIG_SPL_LEGACY_IMAGE_SUPPORT
141 		u32 header_size = sizeof(struct image_header);
142 
143 		if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
144 			/*
145 			 * On some system (e.g. powerpc), the load-address and
146 			 * entry-point is located at address 0. We can't load
147 			 * to 0-0x40. So skip header in this case.
148 			 */
149 			spl_image->load_addr = image_get_load(header);
150 			spl_image->entry_point = image_get_ep(header);
151 			spl_image->size = image_get_data_size(header);
152 		} else {
153 			spl_image->entry_point = image_get_load(header);
154 			/* Load including the header */
155 			spl_image->load_addr = spl_image->entry_point -
156 				header_size;
157 			spl_image->size = image_get_data_size(header) +
158 				header_size;
159 		}
160 		spl_image->os = image_get_os(header);
161 		spl_image->name = image_get_name(header);
162 		debug("spl: payload image: %.*s load addr: 0x%lx size: %d\n",
163 			IH_NMLEN, spl_image->name,
164 			spl_image->load_addr, spl_image->size);
165 #else
166 		/* LEGACY image not supported */
167 		debug("Legacy boot image support not enabled, proceeding to other boot methods\n");
168 		return -EINVAL;
169 #endif
170 	} else {
171 #ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
172 		/*
173 		 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
174 		 * code which loads images in SPL cannot guarantee that
175 		 * absolutely all read errors will be reported.
176 		 * An example is the LPC32XX MLC NAND driver, which
177 		 * will consider that a completely unreadable NAND block
178 		 * is bad, and thus should be skipped silently.
179 		 */
180 		panic("** no mkimage signature but raw image not supported");
181 #endif
182 
183 #ifdef CONFIG_SPL_OS_BOOT
184 		ulong start, end;
185 
186 		if (!bootz_setup((ulong)header, &start, &end)) {
187 			spl_image->name = "Linux";
188 			spl_image->os = IH_OS_LINUX;
189 			spl_image->load_addr = CONFIG_SYS_LOAD_ADDR;
190 			spl_image->entry_point = CONFIG_SYS_LOAD_ADDR;
191 			spl_image->size = end - start;
192 			debug("spl: payload zImage, load addr: 0x%lx size: %d\n",
193 			      spl_image->load_addr, spl_image->size);
194 			return 0;
195 		}
196 #endif
197 
198 #ifdef CONFIG_SPL_RAW_IMAGE_SUPPORT
199 		/* Signature not found - assume u-boot.bin */
200 		debug("mkimage signature not found - ih_magic = %x\n",
201 			header->ih_magic);
202 		spl_set_header_raw_uboot(spl_image);
203 #else
204 		/* RAW image not supported, proceed to other boot methods. */
205 		debug("Raw boot image support not enabled, proceeding to other boot methods\n");
206 		return -EINVAL;
207 #endif
208 	}
209 
210 	return 0;
211 }
212 
213 __weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
214 {
215 	typedef void __noreturn (*image_entry_noargs_t)(void);
216 
217 	image_entry_noargs_t image_entry =
218 		(image_entry_noargs_t)spl_image->entry_point;
219 
220 	debug("image entry point: 0x%lX\n", spl_image->entry_point);
221 	image_entry();
222 }
223 
224 /*
225  * 64-bit: No special operation.
226  *
227  * 32-bit: Initial gd->bd->bi_dram[] to active dcache attr of memory.
228  *	   Assuming 256MB is enough for SPL(MMU still maps 4GB size).
229  */
230 #ifndef CONFIG_SPL_SYS_DCACHE_OFF
231 static int spl_dcache_enable(void)
232 {
233 	bool free_bd = false;
234 
235 #ifndef CONFIG_ARM64
236 	if (!gd->bd) {
237 		gd->bd = calloc(1, sizeof(bd_t));
238 		if (!gd->bd) {
239 			debug("spl: no bd_t memory\n");
240 			return -ENOMEM;
241 		}
242 		gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
243 		gd->bd->bi_dram[0].size  = SZ_256M;
244 		free_bd = true;
245 	}
246 #endif
247 	/* TLB memory should be 64KB base align and 4KB end align */
248 	gd->arch.tlb_size = PGTABLE_SIZE;
249 	gd->arch.tlb_addr = (ulong)memalign(SZ_64K, ALIGN(PGTABLE_SIZE, SZ_4K));
250 	if (!gd->arch.tlb_addr) {
251 		debug("spl: no TLB memory\n");
252 		return -ENOMEM;
253 	}
254 
255 	dcache_enable();
256 	if (free_bd)
257 		free(gd->bd);
258 
259 	return 0;
260 }
261 #endif
262 
263 static int spl_common_init(bool setup_malloc)
264 {
265 	int ret;
266 
267 	debug("spl_early_init()\n");
268 
269 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
270 	if (setup_malloc) {
271 #ifdef CONFIG_MALLOC_F_ADDR
272 		gd->malloc_base = CONFIG_MALLOC_F_ADDR;
273 #endif
274 		gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
275 		gd->malloc_ptr = 0;
276 	}
277 #endif
278 
279 	/*
280 	 * setup D-cache as early as possible after malloc setup
281 	 * I-cache has been setup at early assembly code by default.
282 	 */
283 #ifndef CONFIG_SPL_SYS_DCACHE_OFF
284 	ret = spl_dcache_enable();
285 	if (ret) {
286 		debug("spl_dcache_enable() return error %d\n", ret);
287 		return ret;
288 	}
289 #endif
290 	ret = bootstage_init(true);
291 	if (ret) {
292 		debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
293 		      ret);
294 		return ret;
295 	}
296 	bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
297 	if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
298 		ret = fdtdec_setup();
299 		if (ret) {
300 			debug("fdtdec_setup() returned error %d\n", ret);
301 			return ret;
302 		}
303 	}
304 	if (CONFIG_IS_ENABLED(DM)) {
305 		bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
306 		/* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
307 		ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
308 		bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
309 		if (ret) {
310 			debug("dm_init_and_scan() returned error %d\n", ret);
311 			return ret;
312 		}
313 	}
314 
315 	return 0;
316 }
317 
318 #if !defined(CONFIG_SPL_SKIP_RELOCATE) && !defined(CONFIG_TPL_BUILD)
319 static void spl_setup_relocate(void)
320 {
321 	gd->relocaddr = CONFIG_SPL_RELOC_TEXT_BASE;
322 	gd->new_gd = (gd_t *)gd;
323 	gd->start_addr_sp = gd->relocaddr;
324 	gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
325 
326 	gd->start_addr_sp -= gd->fdt_size;
327 	gd->new_fdt = (void *)gd->start_addr_sp;
328 	memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
329 	gd->fdt_blob = gd->new_fdt;
330 
331 	gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
332 }
333 #else
334 static void spl_setup_relocate(void)
335 {
336 
337 }
338 #endif
339 
340 void spl_set_bd(void)
341 {
342 	if (!gd->bd)
343 		gd->bd = &bdata;
344 }
345 
346 int spl_early_init(void)
347 {
348 	int ret;
349 
350 	ret = spl_common_init(true);
351 	if (ret)
352 		return ret;
353 	gd->flags |= GD_FLG_SPL_EARLY_INIT;
354 
355 	spl_setup_relocate();
356 
357 	return 0;
358 }
359 
360 int spl_init(void)
361 {
362 	int ret;
363 	bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
364 			IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
365 
366 	if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
367 		ret = spl_common_init(setup_malloc);
368 		if (ret)
369 			return ret;
370 	}
371 	gd->flags |= GD_FLG_SPL_INIT;
372 
373 	return 0;
374 }
375 
376 #ifndef BOOT_DEVICE_NONE
377 #define BOOT_DEVICE_NONE 0xdeadbeef
378 #endif
379 
380 __weak void board_boot_order(u32 *spl_boot_list)
381 {
382 	spl_boot_list[0] = spl_boot_device();
383 }
384 
385 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
386 {
387 	struct spl_image_loader *drv =
388 		ll_entry_start(struct spl_image_loader, spl_image_loader);
389 	const int n_ents =
390 		ll_entry_count(struct spl_image_loader, spl_image_loader);
391 	struct spl_image_loader *entry;
392 
393 	for (entry = drv; entry != drv + n_ents; entry++) {
394 		if (boot_device == entry->boot_device)
395 			return entry;
396 	}
397 
398 	/* Not found */
399 	return NULL;
400 }
401 
402 static int spl_load_image(struct spl_image_info *spl_image,
403 			  struct spl_image_loader *loader)
404 {
405 	struct spl_boot_device bootdev;
406 
407 	bootdev.boot_device = loader->boot_device;
408 	bootdev.boot_device_name = NULL;
409 
410 	return loader->load_image(spl_image, &bootdev);
411 }
412 
413 /**
414  * boot_from_devices() - Try loading an booting U-Boot from a list of devices
415  *
416  * @spl_image: Place to put the image details if successful
417  * @spl_boot_list: List of boot devices to try
418  * @count: Number of elements in spl_boot_list
419  * @return 0 if OK, -ve on error
420  */
421 static int boot_from_devices(struct spl_image_info *spl_image,
422 			     u32 spl_boot_list[], int count)
423 {
424 	int i;
425 
426 	for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
427 		struct spl_image_loader *loader;
428 
429 		loader = spl_ll_find_loader(spl_boot_list[i]);
430 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
431 		if (loader)
432 			printf("Trying to boot from %s\n", loader->name);
433 		else
434 			puts("SPL: Unsupported Boot Device!\n");
435 #endif
436 		if (loader && !spl_load_image(spl_image, loader)) {
437 			spl_image->boot_device = spl_boot_list[i];
438 			return 0;
439 		}
440 	}
441 
442 	return -ENODEV;
443 }
444 
445 #if defined(CONFIG_DM) && !defined(CONFIG_SPL_SKIP_RELOCATE) && !defined(CONFIG_TPL_BUILD)
446 static int spl_initr_dm(void)
447 {
448 	int ret;
449 
450 	/* Save the pre-reloc driver model and start a new one */
451 	gd->dm_root_f = gd->dm_root;
452 	gd->dm_root = NULL;
453 	bootstage_start(BOOTSTATE_ID_ACCUM_DM_R, "dm_r");
454 	ret = dm_init_and_scan(false);
455 	bootstage_accum(BOOTSTATE_ID_ACCUM_DM_R);
456 	if (ret)
457 		return ret;
458 
459 #if defined(CONFIG_TIMER)
460 	gd->timer = NULL;
461 #endif
462 	serial_init();
463 
464 	return 0;
465 }
466 #else
467 static int spl_initr_dm(void)
468 {
469 	return 0;
470 }
471 #endif
472 
473 void board_init_r(gd_t *dummy1, ulong dummy2)
474 {
475 	u32 spl_boot_list[] = {
476 		BOOT_DEVICE_NONE,
477 		BOOT_DEVICE_NONE,
478 		BOOT_DEVICE_NONE,
479 		BOOT_DEVICE_NONE,
480 		BOOT_DEVICE_NONE,
481 	};
482 	struct spl_image_info spl_image;
483 
484 	debug(">>spl:board_init_r()\n");
485 
486 	spl_initr_dm();
487 
488 	spl_set_bd();
489 
490 #ifdef CONFIG_SPL_OS_BOOT
491 	dram_init_banksize();
492 #endif
493 
494 #if defined(CONFIG_SYS_SPL_MALLOC_START)
495 	mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
496 			CONFIG_SYS_SPL_MALLOC_SIZE);
497 	gd->flags |= GD_FLG_FULL_MALLOC_INIT;
498 #endif
499 	if (!(gd->flags & GD_FLG_SPL_INIT)) {
500 		if (spl_init())
501 			hang();
502 	}
503 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
504 	/*
505 	 * timer_init() does not exist on PPC systems. The timer is initialized
506 	 * and enabled (decrementer) in interrupt_init() here.
507 	 */
508 	timer_init();
509 #endif
510 
511 #if CONFIG_IS_ENABLED(BOARD_INIT)
512 	spl_board_init();
513 #endif
514 
515 	memset(&spl_image, '\0', sizeof(spl_image));
516 
517 #if CONFIG_IS_ENABLED(ATF)
518 	/*
519 	 * Bl32 ep is optional, initial it as an invalid value.
520 	 * BL33 ep is mandatory, but initial it as a default value is better.
521 	 */
522 	spl_image.entry_point_bl32 = -1;
523 	spl_image.entry_point_bl33 = CONFIG_SYS_TEXT_BASE;
524 #endif
525 
526 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
527 	spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
528 #endif
529 	spl_image.boot_device = BOOT_DEVICE_NONE;
530 	board_boot_order(spl_boot_list);
531 
532 	if (boot_from_devices(&spl_image, spl_boot_list,
533 			      ARRAY_SIZE(spl_boot_list))) {
534 		puts("SPL: failed to boot from all boot devices\n");
535 		hang();
536 	}
537 
538 	spl_perform_fixups(&spl_image);
539 
540 #ifdef CONFIG_CPU_V7M
541 	spl_image.entry_point |= 0x1;
542 #endif
543 	switch (spl_image.os) {
544 	case IH_OS_U_BOOT:
545 		debug("Jumping to U-Boot\n");
546 		break;
547 #if CONFIG_IS_ENABLED(ATF)
548 	case IH_OS_ARM_TRUSTED_FIRMWARE:
549 		printf("Jumping to U-Boot via ARM Trusted Firmware\n\n");
550 		spl_invoke_atf(&spl_image);
551 		break;
552 #endif
553 #if CONFIG_IS_ENABLED(OPTEE)
554 	case IH_OS_OP_TEE:
555 		debug("Jumping to U-Boot via OP-TEE\n");
556 		spl_optee_entry(NULL, NULL, (void *)spl_image.fdt_addr,
557 				(void *)spl_image.entry_point);
558 		break;
559 #endif
560 #ifdef CONFIG_SPL_OS_BOOT
561 	case IH_OS_LINUX:
562 		debug("Jumping to Linux\n");
563 		spl_fixup_fdt();
564 		spl_board_prepare_for_linux();
565 		jump_to_image_linux(&spl_image);
566 #endif
567 	default:
568 		debug("Unsupported OS image.. Jumping nevertheless..\n");
569 	}
570 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
571 	debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
572 	      gd->malloc_ptr / 1024);
573 #endif
574 
575 	debug("loaded - jumping to U-Boot...\n");
576 #ifdef CONFIG_BOOTSTAGE_STASH
577 	int ret;
578 
579 	bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
580 	ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
581 			      CONFIG_BOOTSTAGE_STASH_SIZE);
582 	if (ret)
583 		debug("Failed to stash bootstage: err=%d\n", ret);
584 #endif
585 
586 	debug("loaded - jumping to U-Boot...\n");
587 	spl_board_prepare_for_boot();
588 	jump_to_image_no_args(&spl_image);
589 }
590 
591 /*
592  * This requires UART clocks to be enabled.  In order for this to work the
593  * caller must ensure that the gd pointer is valid.
594  */
595 void preloader_console_init(void)
596 {
597 	gd->baudrate = CONFIG_BAUDRATE;
598 
599 	serial_init();		/* serial communications setup */
600 
601 	gd->have_console = 1;
602 
603 	puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
604 			U_BOOT_TIME ")\n");
605 #ifdef CONFIG_SPL_DISPLAY_PRINT
606 	spl_display_print();
607 #endif
608 }
609 
610 /**
611  * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
612  *
613  * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
614  * for the main board_init_r() execution. This is typically because we need
615  * more stack space for things like the MMC sub-system.
616  *
617  * This function calculates the stack position, copies the global_data into
618  * place, sets the new gd (except for ARM, for which setting GD within a C
619  * function may not always work) and returns the new stack position. The
620  * caller is responsible for setting up the sp register and, in the case
621  * of ARM, setting up gd.
622  *
623  * All of this is done using the same layout and alignments as done in
624  * board_init_f_init_reserve() / board_init_f_alloc_reserve().
625  *
626  * @return new stack location, or 0 to use the same stack
627  */
628 ulong spl_relocate_stack_gd(void)
629 {
630 #ifdef CONFIG_SPL_STACK_R
631 	gd_t *new_gd;
632 	ulong ptr = CONFIG_SPL_STACK_R_ADDR;
633 
634 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
635 	if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
636 		ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
637 		gd->malloc_base = ptr;
638 		gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
639 		gd->malloc_ptr = 0;
640 	}
641 #endif
642 	/* Get stack position: use 8-byte alignment for ABI compliance */
643 	ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
644 	new_gd = (gd_t *)ptr;
645 	memcpy(new_gd, (void *)gd, sizeof(gd_t));
646 #if CONFIG_IS_ENABLED(DM)
647 	dm_fixup_for_gd_move(new_gd);
648 #endif
649 #if !defined(CONFIG_ARM)
650 	gd = new_gd;
651 #endif
652 	return ptr;
653 #else
654 	return 0;
655 #endif
656 }
657