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