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