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