xref: /rk3399_rockchip-uboot/common/spl/spl.c (revision fcdd83d4455871c49a93236c3f1cb964727f8c0c)
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 static int spl_common_init(bool setup_malloc)
225 {
226 	int ret;
227 
228 	debug("spl_early_init()\n");
229 
230 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
231 	if (setup_malloc) {
232 #ifdef CONFIG_MALLOC_F_ADDR
233 		gd->malloc_base = CONFIG_MALLOC_F_ADDR;
234 #endif
235 		gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
236 		gd->malloc_ptr = 0;
237 	}
238 #endif
239 	ret = bootstage_init(true);
240 	if (ret) {
241 		debug("%s: Failed to set up bootstage: ret=%d\n", __func__,
242 		      ret);
243 		return ret;
244 	}
245 	bootstage_mark_name(BOOTSTAGE_ID_START_SPL, "spl");
246 	if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
247 		ret = fdtdec_setup();
248 		if (ret) {
249 			debug("fdtdec_setup() returned error %d\n", ret);
250 			return ret;
251 		}
252 	}
253 	if (CONFIG_IS_ENABLED(DM)) {
254 		bootstage_start(BOOTSTATE_ID_ACCUM_DM_SPL, "dm_spl");
255 		/* With CONFIG_SPL_OF_PLATDATA, bring in all devices */
256 		ret = dm_init_and_scan(!CONFIG_IS_ENABLED(OF_PLATDATA));
257 		bootstage_accum(BOOTSTATE_ID_ACCUM_DM_SPL);
258 		if (ret) {
259 			debug("dm_init_and_scan() returned error %d\n", ret);
260 			return ret;
261 		}
262 	}
263 
264 	return 0;
265 }
266 
267 #if !defined(CONFIG_SPL_SKIP_RELOCATE) && !defined(CONFIG_TPL_BUILD)
268 static void spl_setup_relocate(void)
269 {
270 	gd->relocaddr = CONFIG_SPL_RELOC_TEXT_BASE;
271 	gd->new_gd = (gd_t *)gd;
272 	gd->start_addr_sp = gd->relocaddr;
273 	gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
274 
275 	gd->start_addr_sp -= gd->fdt_size;
276 	gd->new_fdt = (void *)gd->start_addr_sp;
277 	memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
278 	gd->fdt_blob = gd->new_fdt;
279 
280 	gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
281 }
282 #else
283 static void spl_setup_relocate(void)
284 {
285 
286 }
287 #endif
288 
289 void spl_set_bd(void)
290 {
291 	if (!gd->bd)
292 		gd->bd = &bdata;
293 }
294 
295 int spl_early_init(void)
296 {
297 	int ret;
298 
299 	ret = spl_common_init(true);
300 	if (ret)
301 		return ret;
302 	gd->flags |= GD_FLG_SPL_EARLY_INIT;
303 
304 	spl_setup_relocate();
305 
306 	return 0;
307 }
308 
309 int spl_init(void)
310 {
311 	int ret;
312 	bool setup_malloc = !(IS_ENABLED(CONFIG_SPL_STACK_R) &&
313 			IS_ENABLED(CONFIG_SPL_SYS_MALLOC_SIMPLE));
314 
315 	if (!(gd->flags & GD_FLG_SPL_EARLY_INIT)) {
316 		ret = spl_common_init(setup_malloc);
317 		if (ret)
318 			return ret;
319 	}
320 	gd->flags |= GD_FLG_SPL_INIT;
321 
322 	return 0;
323 }
324 
325 #ifndef BOOT_DEVICE_NONE
326 #define BOOT_DEVICE_NONE 0xdeadbeef
327 #endif
328 
329 __weak void board_boot_order(u32 *spl_boot_list)
330 {
331 	spl_boot_list[0] = spl_boot_device();
332 }
333 
334 static struct spl_image_loader *spl_ll_find_loader(uint boot_device)
335 {
336 	struct spl_image_loader *drv =
337 		ll_entry_start(struct spl_image_loader, spl_image_loader);
338 	const int n_ents =
339 		ll_entry_count(struct spl_image_loader, spl_image_loader);
340 	struct spl_image_loader *entry;
341 
342 	for (entry = drv; entry != drv + n_ents; entry++) {
343 		if (boot_device == entry->boot_device)
344 			return entry;
345 	}
346 
347 	/* Not found */
348 	return NULL;
349 }
350 
351 static int spl_load_image(struct spl_image_info *spl_image,
352 			  struct spl_image_loader *loader)
353 {
354 	struct spl_boot_device bootdev;
355 
356 	bootdev.boot_device = loader->boot_device;
357 	bootdev.boot_device_name = NULL;
358 
359 	return loader->load_image(spl_image, &bootdev);
360 }
361 
362 /**
363  * boot_from_devices() - Try loading an booting U-Boot from a list of devices
364  *
365  * @spl_image: Place to put the image details if successful
366  * @spl_boot_list: List of boot devices to try
367  * @count: Number of elements in spl_boot_list
368  * @return 0 if OK, -ve on error
369  */
370 static int boot_from_devices(struct spl_image_info *spl_image,
371 			     u32 spl_boot_list[], int count)
372 {
373 	int i;
374 
375 	for (i = 0; i < count && spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
376 		struct spl_image_loader *loader;
377 
378 		loader = spl_ll_find_loader(spl_boot_list[i]);
379 #if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
380 		if (loader)
381 			printf("Trying to boot from %s\n", loader->name);
382 		else
383 			puts("SPL: Unsupported Boot Device!\n");
384 #endif
385 		if (loader && !spl_load_image(spl_image, loader)) {
386 			spl_image->boot_device = spl_boot_list[i];
387 			return 0;
388 		}
389 	}
390 
391 	return -ENODEV;
392 }
393 
394 #if defined(CONFIG_DM) && !defined(CONFIG_SPL_SKIP_RELOCATE) && !defined(CONFIG_TPL_BUILD)
395 static int spl_initr_dm(void)
396 {
397 	int ret;
398 
399 	/* Save the pre-reloc driver model and start a new one */
400 	gd->dm_root_f = gd->dm_root;
401 	gd->dm_root = NULL;
402 	bootstage_start(BOOTSTATE_ID_ACCUM_DM_R, "dm_r");
403 	ret = dm_init_and_scan(false);
404 	bootstage_accum(BOOTSTATE_ID_ACCUM_DM_R);
405 	if (ret)
406 		return ret;
407 
408 #if defined(CONFIG_TIMER)
409 	gd->timer = NULL;
410 #endif
411 	serial_init();
412 
413 	return 0;
414 }
415 #else
416 static int spl_initr_dm(void)
417 {
418 	return 0;
419 }
420 #endif
421 
422 void board_init_r(gd_t *dummy1, ulong dummy2)
423 {
424 	u32 spl_boot_list[] = {
425 		BOOT_DEVICE_NONE,
426 		BOOT_DEVICE_NONE,
427 		BOOT_DEVICE_NONE,
428 		BOOT_DEVICE_NONE,
429 		BOOT_DEVICE_NONE,
430 	};
431 	struct spl_image_info spl_image;
432 
433 	debug(">>spl:board_init_r()\n");
434 
435 	spl_initr_dm();
436 
437 	spl_set_bd();
438 
439 #ifdef CONFIG_SPL_OS_BOOT
440 	dram_init_banksize();
441 #endif
442 
443 #if defined(CONFIG_SYS_SPL_MALLOC_START)
444 	mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
445 			CONFIG_SYS_SPL_MALLOC_SIZE);
446 	gd->flags |= GD_FLG_FULL_MALLOC_INIT;
447 #endif
448 	if (!(gd->flags & GD_FLG_SPL_INIT)) {
449 		if (spl_init())
450 			hang();
451 	}
452 #if !defined(CONFIG_PPC) && !defined(CONFIG_ARCH_MX6)
453 	/*
454 	 * timer_init() does not exist on PPC systems. The timer is initialized
455 	 * and enabled (decrementer) in interrupt_init() here.
456 	 */
457 	timer_init();
458 #endif
459 
460 #if CONFIG_IS_ENABLED(BOARD_INIT)
461 	spl_board_init();
462 #endif
463 
464 	memset(&spl_image, '\0', sizeof(spl_image));
465 
466 #if CONFIG_IS_ENABLED(ATF)
467 	/*
468 	 * Bl32 ep is optional, initial it as an invalid value.
469 	 * BL33 ep is mandatory, but initial it as a default value is better.
470 	 */
471 	spl_image.entry_point_bl32 = -1;
472 	spl_image.entry_point_bl33 = CONFIG_SYS_TEXT_BASE;
473 #endif
474 
475 #ifdef CONFIG_SYS_SPL_ARGS_ADDR
476 	spl_image.arg = (void *)CONFIG_SYS_SPL_ARGS_ADDR;
477 #endif
478 	spl_image.boot_device = BOOT_DEVICE_NONE;
479 	board_boot_order(spl_boot_list);
480 
481 	if (boot_from_devices(&spl_image, spl_boot_list,
482 			      ARRAY_SIZE(spl_boot_list))) {
483 		puts("SPL: failed to boot from all boot devices\n");
484 		hang();
485 	}
486 
487 	spl_perform_fixups(&spl_image);
488 
489 #ifdef CONFIG_CPU_V7M
490 	spl_image.entry_point |= 0x1;
491 #endif
492 	switch (spl_image.os) {
493 	case IH_OS_U_BOOT:
494 		debug("Jumping to U-Boot\n");
495 		break;
496 #if CONFIG_IS_ENABLED(ATF)
497 	case IH_OS_ARM_TRUSTED_FIRMWARE:
498 		printf("Jumping to U-Boot via ARM Trusted Firmware\n\n");
499 		spl_invoke_atf(&spl_image);
500 		break;
501 #endif
502 #if CONFIG_IS_ENABLED(OPTEE)
503 	case IH_OS_OP_TEE:
504 		debug("Jumping to U-Boot via OP-TEE\n");
505 		spl_optee_entry(NULL, NULL, (void *)spl_image.fdt_addr,
506 				(void *)spl_image.entry_point);
507 		break;
508 #endif
509 #ifdef CONFIG_SPL_OS_BOOT
510 	case IH_OS_LINUX:
511 		debug("Jumping to Linux\n");
512 		spl_fixup_fdt();
513 		spl_board_prepare_for_linux();
514 		jump_to_image_linux(&spl_image);
515 #endif
516 	default:
517 		debug("Unsupported OS image.. Jumping nevertheless..\n");
518 	}
519 #if CONFIG_VAL(SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
520 	debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
521 	      gd->malloc_ptr / 1024);
522 #endif
523 
524 	debug("loaded - jumping to U-Boot...\n");
525 #ifdef CONFIG_BOOTSTAGE_STASH
526 	int ret;
527 
528 	bootstage_mark_name(BOOTSTAGE_ID_END_SPL, "end_spl");
529 	ret = bootstage_stash((void *)CONFIG_BOOTSTAGE_STASH_ADDR,
530 			      CONFIG_BOOTSTAGE_STASH_SIZE);
531 	if (ret)
532 		debug("Failed to stash bootstage: err=%d\n", ret);
533 #endif
534 
535 	debug("loaded - jumping to U-Boot...\n");
536 	spl_board_prepare_for_boot();
537 	jump_to_image_no_args(&spl_image);
538 }
539 
540 /*
541  * This requires UART clocks to be enabled.  In order for this to work the
542  * caller must ensure that the gd pointer is valid.
543  */
544 void preloader_console_init(void)
545 {
546 	gd->baudrate = CONFIG_BAUDRATE;
547 
548 	serial_init();		/* serial communications setup */
549 
550 	gd->have_console = 1;
551 
552 	puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
553 			U_BOOT_TIME ")\n");
554 #ifdef CONFIG_SPL_DISPLAY_PRINT
555 	spl_display_print();
556 #endif
557 }
558 
559 /**
560  * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
561  *
562  * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
563  * for the main board_init_r() execution. This is typically because we need
564  * more stack space for things like the MMC sub-system.
565  *
566  * This function calculates the stack position, copies the global_data into
567  * place, sets the new gd (except for ARM, for which setting GD within a C
568  * function may not always work) and returns the new stack position. The
569  * caller is responsible for setting up the sp register and, in the case
570  * of ARM, setting up gd.
571  *
572  * All of this is done using the same layout and alignments as done in
573  * board_init_f_init_reserve() / board_init_f_alloc_reserve().
574  *
575  * @return new stack location, or 0 to use the same stack
576  */
577 ulong spl_relocate_stack_gd(void)
578 {
579 #ifdef CONFIG_SPL_STACK_R
580 	gd_t *new_gd;
581 	ulong ptr = CONFIG_SPL_STACK_R_ADDR;
582 
583 #if defined(CONFIG_SPL_SYS_MALLOC_SIMPLE) && CONFIG_VAL(SYS_MALLOC_F_LEN)
584 	if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
585 		ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
586 		gd->malloc_base = ptr;
587 		gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
588 		gd->malloc_ptr = 0;
589 	}
590 #endif
591 	/* Get stack position: use 8-byte alignment for ABI compliance */
592 	ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
593 	new_gd = (gd_t *)ptr;
594 	memcpy(new_gd, (void *)gd, sizeof(gd_t));
595 #if CONFIG_IS_ENABLED(DM)
596 	dm_fixup_for_gd_move(new_gd);
597 #endif
598 #if !defined(CONFIG_ARM)
599 	gd = new_gd;
600 #endif
601 	return ptr;
602 #else
603 	return 0;
604 #endif
605 }
606