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