blob: caaf135f9ad55c473737918e0d3d68db9a30d729 [file] [log] [blame]
Aneesh Vbcae7212011-07-21 09:10:21 -04001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * Aneesh V <aneesh@ti.com>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Aneesh Vbcae7212011-07-21 09:10:21 -04008 */
9#include <common.h>
Simon Glass11516512014-11-10 17:16:46 -070010#include <dm.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070011#include <spl.h>
Aneesh Vbcae7212011-07-21 09:10:21 -040012#include <asm/u-boot.h>
Simon Schwarzbb085b82011-09-14 15:29:26 -040013#include <nand.h>
Aneesh V8cf686e2011-07-21 09:10:27 -040014#include <fat.h>
Simon Glassefb21722011-10-10 08:55:19 +000015#include <version.h>
Aneesh V8cf686e2011-07-21 09:10:27 -040016#include <i2c.h>
17#include <image.h>
Aneesh V2d01dd92011-10-21 12:29:34 -040018#include <malloc.h>
Simon Glass11516512014-11-10 17:16:46 -070019#include <dm/root.h>
Andreas Müller761ca312012-01-04 15:26:24 +000020#include <linux/compiler.h>
Aneesh Vbcae7212011-07-21 09:10:21 -040021
22DECLARE_GLOBAL_DATA_PTR;
23
Stefan Roese3c6f8a02012-08-28 10:50:59 +020024#ifndef CONFIG_SYS_UBOOT_START
25#define CONFIG_SYS_UBOOT_START CONFIG_SYS_TEXT_BASE
26#endif
Stefano Babicae83d882012-08-23 12:46:16 +020027#ifndef CONFIG_SYS_MONITOR_LEN
Andreas Bießmanne76caa62014-10-25 02:54:55 +020028/* Unknown U-Boot size, let's assume it will not be more than 200 KB */
Stefano Babicae83d882012-08-23 12:46:16 +020029#define CONFIG_SYS_MONITOR_LEN (200 * 1024)
30#endif
31
Tom Rini47f7bca2012-08-13 12:03:19 -070032u32 *boot_params_ptr = NULL;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040033struct spl_image_info spl_image;
34
Tom Rini6507f132012-08-22 15:31:05 -070035/* Define board data structure */
Aneesh Vbcae7212011-07-21 09:10:21 -040036static bd_t bdata __attribute__ ((section(".data")));
37
Simon Schwarz379c19a2012-03-15 04:01:38 +000038/*
39 * Default function to determine if u-boot or the OS should
40 * be started. This implementation always returns 1.
41 *
42 * Please implement your own board specific funcion to do this.
43 *
44 * RETURN
45 * 0 to not start u-boot
46 * positive if u-boot should start
47 */
48#ifdef CONFIG_SPL_OS_BOOT
49__weak int spl_start_uboot(void)
50{
Tom Rini026b2fe2012-08-14 12:06:43 -070051 puts("SPL: Please implement spl_start_uboot() for your board\n");
52 puts("SPL: Direct Linux boot not active!\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +000053 return 1;
54}
55#endif
56
Stefan Roeseea8256f2012-08-23 08:34:21 +020057/*
58 * Weak default function for board specific cleanup/preparation before
59 * Linux boot. Some boards/platforms might not need it, so just provide
60 * an empty stub here.
61 */
62__weak void spl_board_prepare_for_linux(void)
63{
64 /* Nothing to do! */
65}
66
Michal Simek3a3b9142016-05-10 07:54:20 +020067__weak void spl_board_prepare_for_boot(void)
68{
69 /* Nothing to do! */
70}
71
Heiko Schocher0c3117b2014-10-31 08:31:00 +010072void spl_set_header_raw_uboot(void)
73{
74 spl_image.size = CONFIG_SYS_MONITOR_LEN;
75 spl_image.entry_point = CONFIG_SYS_UBOOT_START;
76 spl_image.load_addr = CONFIG_SYS_TEXT_BASE;
77 spl_image.os = IH_OS_U_BOOT;
78 spl_image.name = "U-Boot";
79}
80
Marek Vasut7e0f2262016-04-29 00:44:54 +020081int spl_parse_image_header(const struct image_header *header)
Aneesh V8cf686e2011-07-21 09:10:27 -040082{
83 u32 header_size = sizeof(struct image_header);
84
Stefan Roese77552b02012-08-27 12:50:57 +020085 if (image_get_magic(header) == IH_MAGIC) {
Stefan Roese022b4972012-08-27 12:50:58 +020086 if (spl_image.flags & SPL_COPY_PAYLOAD_ONLY) {
87 /*
88 * On some system (e.g. powerpc), the load-address and
89 * entry-point is located at address 0. We can't load
90 * to 0-0x40. So skip header in this case.
91 */
92 spl_image.load_addr = image_get_load(header);
93 spl_image.entry_point = image_get_ep(header);
94 spl_image.size = image_get_data_size(header);
95 } else {
96 spl_image.entry_point = image_get_load(header);
97 /* Load including the header */
98 spl_image.load_addr = spl_image.entry_point -
99 header_size;
100 spl_image.size = image_get_data_size(header) +
101 header_size;
102 }
Stefan Roese77552b02012-08-27 12:50:57 +0200103 spl_image.os = image_get_os(header);
104 spl_image.name = image_get_name(header);
Taras Kondratiuk62cf11c2013-07-16 14:45:01 +0300105 debug("spl: payload image: %.*s load addr: 0x%x size: %d\n",
Vasili Galka5d69a5d2014-08-26 13:45:48 +0300106 (int)sizeof(spl_image.name), spl_image.name,
Taras Kondratiuk62cf11c2013-07-16 14:45:01 +0300107 spl_image.load_addr, spl_image.size);
Aneesh V8cf686e2011-07-21 09:10:27 -0400108 } else {
Albert ARIBAUD \(3ADEV\)8c80eb32015-03-31 11:40:50 +0200109#ifdef CONFIG_SPL_PANIC_ON_RAW_IMAGE
110 /*
111 * CONFIG_SPL_PANIC_ON_RAW_IMAGE is defined when the
112 * code which loads images in SPL cannot guarantee that
113 * absolutely all read errors will be reported.
114 * An example is the LPC32XX MLC NAND driver, which
115 * will consider that a completely unreadable NAND block
116 * is bad, and thus should be skipped silently.
117 */
118 panic("** no mkimage signature but raw image not supported");
Marek Vasute0727512016-04-29 00:44:55 +0200119#elif defined(CONFIG_SPL_ABORT_ON_RAW_IMAGE)
120 /* Signature not found, proceed to other boot methods. */
121 return -EINVAL;
Albert ARIBAUD \(3ADEV\)8c80eb32015-03-31 11:40:50 +0200122#else
Aneesh V8cf686e2011-07-21 09:10:27 -0400123 /* Signature not found - assume u-boot.bin */
Tom Rini026b2fe2012-08-14 12:06:43 -0700124 debug("mkimage signature not found - ih_magic = %x\n",
Aneesh V8cf686e2011-07-21 09:10:27 -0400125 header->ih_magic);
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100126 spl_set_header_raw_uboot();
Albert ARIBAUD \(3ADEV\)8c80eb32015-03-31 11:40:50 +0200127#endif
Aneesh V8cf686e2011-07-21 09:10:27 -0400128 }
Marek Vasut7e0f2262016-04-29 00:44:54 +0200129 return 0;
Aneesh V8cf686e2011-07-21 09:10:27 -0400130}
131
Allen Martina759f1e2012-10-19 21:08:22 +0000132__weak void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image)
Aneesh V8cf686e2011-07-21 09:10:27 -0400133{
SRICHARAN R4a0eb752013-04-24 00:41:24 +0000134 typedef void __noreturn (*image_entry_noargs_t)(void);
135
Aneesh V8cf686e2011-07-21 09:10:27 -0400136 image_entry_noargs_t image_entry =
Scott Woodb2d5ac52015-03-24 13:25:02 -0700137 (image_entry_noargs_t)(unsigned long)spl_image->entry_point;
Aneesh V8cf686e2011-07-21 09:10:27 -0400138
Allen Martina759f1e2012-10-19 21:08:22 +0000139 debug("image entry point: 0x%X\n", spl_image->entry_point);
SRICHARAN R4a0eb752013-04-24 00:41:24 +0000140 image_entry();
Aneesh V8cf686e2011-07-21 09:10:27 -0400141}
142
Pavel Machekc57b9532012-08-30 22:42:11 +0200143#ifdef CONFIG_SPL_RAM_DEVICE
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200144static int spl_ram_load_image(void)
Pavel Machekc57b9532012-08-30 22:42:11 +0200145{
146 const struct image_header *header;
147
148 /*
149 * Get the header. It will point to an address defined by handoff
150 * which will tell where the image located inside the flash. For
151 * now, it will temporary fixed to address pointed by U-Boot.
152 */
153 header = (struct image_header *)
154 (CONFIG_SYS_TEXT_BASE - sizeof(struct image_header));
155
156 spl_parse_image_header(header);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200157
158 return 0;
Pavel Machekc57b9532012-08-30 22:42:11 +0200159}
160#endif
161
Simon Glass070d00b2015-06-23 15:39:10 -0600162int spl_init(void)
Aneesh Vbcae7212011-07-21 09:10:21 -0400163{
Simon Glassf3d46bd2015-02-27 22:06:42 -0700164 int ret;
165
Simon Glass070d00b2015-06-23 15:39:10 -0600166 debug("spl_init()\n");
167#if defined(CONFIG_SYS_MALLOC_F_LEN)
Simon Glass293f16b2015-02-27 22:06:37 -0700168 gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
Simon Glassfb4f5e72014-11-10 17:16:45 -0700169 gd->malloc_ptr = 0;
Tom Rini24dafad2012-08-13 14:13:07 -0700170#endif
Masahiro Yamada0f925822015-08-12 07:31:55 +0900171 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
Simon Glassf3d46bd2015-02-27 22:06:42 -0700172 ret = fdtdec_setup();
173 if (ret) {
174 debug("fdtdec_setup() returned error %d\n", ret);
Simon Glass070d00b2015-06-23 15:39:10 -0600175 return ret;
Simon Glassf3d46bd2015-02-27 22:06:42 -0700176 }
177 }
178 if (IS_ENABLED(CONFIG_SPL_DM)) {
179 ret = dm_init_and_scan(true);
180 if (ret) {
181 debug("dm_init_and_scan() returned error %d\n", ret);
Simon Glass070d00b2015-06-23 15:39:10 -0600182 return ret;
Simon Glassf3d46bd2015-02-27 22:06:42 -0700183 }
184 }
Simon Glass070d00b2015-06-23 15:39:10 -0600185 gd->flags |= GD_FLG_SPL_INIT;
Aneesh V2d01dd92011-10-21 12:29:34 -0400186
Simon Glass070d00b2015-06-23 15:39:10 -0600187 return 0;
188}
189
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200190#ifndef BOOT_DEVICE_NONE
191#define BOOT_DEVICE_NONE 0xdeadbeef
192#endif
193
194static u32 spl_boot_list[] = {
195 BOOT_DEVICE_NONE,
196 BOOT_DEVICE_NONE,
197 BOOT_DEVICE_NONE,
198 BOOT_DEVICE_NONE,
199 BOOT_DEVICE_NONE,
200};
201
202__weak void board_boot_order(u32 *spl_boot_list)
203{
204 spl_boot_list[0] = spl_boot_device();
205}
206
Nikita Kiryanov310c8462015-11-08 17:11:52 +0200207#ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
208__weak void spl_board_announce_boot_device(void) { }
209#endif
210
211#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
212struct boot_device_name {
213 u32 boot_dev;
214 const char *name;
215};
216
217struct boot_device_name boot_name_table[] = {
218#ifdef CONFIG_SPL_RAM_DEVICE
219 { BOOT_DEVICE_RAM, "RAM" },
220#endif
221#ifdef CONFIG_SPL_MMC_SUPPORT
Hans de Goede8f10b5c2016-03-20 14:17:10 +0100222 { BOOT_DEVICE_MMC1, "MMC1" },
223 { BOOT_DEVICE_MMC2, "MMC2" },
224 { BOOT_DEVICE_MMC2_2, "MMC2_2" },
Nikita Kiryanov310c8462015-11-08 17:11:52 +0200225#endif
226#ifdef CONFIG_SPL_NAND_SUPPORT
227 { BOOT_DEVICE_NAND, "NAND" },
228#endif
229#ifdef CONFIG_SPL_ONENAND_SUPPORT
230 { BOOT_DEVICE_ONENAND, "OneNAND" },
231#endif
232#ifdef CONFIG_SPL_NOR_SUPPORT
233 { BOOT_DEVICE_NOR, "NOR" },
234#endif
235#ifdef CONFIG_SPL_YMODEM_SUPPORT
236 { BOOT_DEVICE_UART, "UART" },
237#endif
238#ifdef CONFIG_SPL_SPI_SUPPORT
239 { BOOT_DEVICE_SPI, "SPI" },
240#endif
241#ifdef CONFIG_SPL_ETH_SUPPORT
242#ifdef CONFIG_SPL_ETH_DEVICE
243 { BOOT_DEVICE_CPGMAC, "eth device" },
244#else
245 { BOOT_DEVICE_CPGMAC, "net" },
246#endif
247#endif
248#ifdef CONFIG_SPL_USBETH_SUPPORT
249 { BOOT_DEVICE_USBETH, "USB eth" },
250#endif
251#ifdef CONFIG_SPL_USB_SUPPORT
252 { BOOT_DEVICE_USB, "USB" },
253#endif
254#ifdef CONFIG_SPL_SATA_SUPPORT
255 { BOOT_DEVICE_SATA, "SATA" },
256#endif
257 /* Keep this entry last */
258 { BOOT_DEVICE_NONE, "unknown boot device" },
259};
260
261static void announce_boot_device(u32 boot_device)
262{
263 int i;
264
265 puts("Trying to boot from ");
266
267#ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
268 if (boot_device == BOOT_DEVICE_BOARD) {
269 spl_board_announce_boot_device();
270 puts("\n");
271 return;
272 }
273#endif
274 for (i = 0; i < ARRAY_SIZE(boot_name_table) - 1; i++) {
275 if (boot_name_table[i].boot_dev == boot_device)
276 break;
277 }
278
279 printf("%s\n", boot_name_table[i].name);
280}
281#else
282static inline void announce_boot_device(u32 boot_device) { }
283#endif
284
Nikita Kiryanov5211b872015-11-08 17:11:50 +0200285static int spl_load_image(u32 boot_device)
286{
287 switch (boot_device) {
288#ifdef CONFIG_SPL_RAM_DEVICE
289 case BOOT_DEVICE_RAM:
290 return spl_ram_load_image();
291#endif
292#ifdef CONFIG_SPL_MMC_SUPPORT
293 case BOOT_DEVICE_MMC1:
294 case BOOT_DEVICE_MMC2:
295 case BOOT_DEVICE_MMC2_2:
Nikita Kiryanova1e56cf2015-11-08 17:11:54 +0200296 return spl_mmc_load_image(boot_device);
Nikita Kiryanov5211b872015-11-08 17:11:50 +0200297#endif
298#ifdef CONFIG_SPL_NAND_SUPPORT
299 case BOOT_DEVICE_NAND:
300 return spl_nand_load_image();
301#endif
302#ifdef CONFIG_SPL_ONENAND_SUPPORT
303 case BOOT_DEVICE_ONENAND:
304 return spl_onenand_load_image();
305#endif
306#ifdef CONFIG_SPL_NOR_SUPPORT
307 case BOOT_DEVICE_NOR:
308 return spl_nor_load_image();
309#endif
310#ifdef CONFIG_SPL_YMODEM_SUPPORT
311 case BOOT_DEVICE_UART:
312 return spl_ymodem_load_image();
313#endif
314#ifdef CONFIG_SPL_SPI_SUPPORT
315 case BOOT_DEVICE_SPI:
316 return spl_spi_load_image();
317#endif
318#ifdef CONFIG_SPL_ETH_SUPPORT
319 case BOOT_DEVICE_CPGMAC:
320#ifdef CONFIG_SPL_ETH_DEVICE
321 return spl_net_load_image(CONFIG_SPL_ETH_DEVICE);
322#else
323 return spl_net_load_image(NULL);
324#endif
325#endif
326#ifdef CONFIG_SPL_USBETH_SUPPORT
327 case BOOT_DEVICE_USBETH:
328 return spl_net_load_image("usb_ether");
329#endif
330#ifdef CONFIG_SPL_USB_SUPPORT
331 case BOOT_DEVICE_USB:
332 return spl_usb_load_image();
333#endif
334#ifdef CONFIG_SPL_SATA_SUPPORT
335 case BOOT_DEVICE_SATA:
336 return spl_sata_load_image();
337#endif
338#ifdef CONFIG_SPL_BOARD_LOAD_IMAGE
339 case BOOT_DEVICE_BOARD:
340 return spl_board_load_image();
341#endif
342 default:
343#if defined(CONFIG_SPL_SERIAL_SUPPORT) && defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
344 puts("SPL: Unsupported Boot Device!\n");
345#endif
346 return -ENODEV;
347 }
348
349 return -EINVAL;
350}
351
Simon Glass070d00b2015-06-23 15:39:10 -0600352void board_init_r(gd_t *dummy1, ulong dummy2)
353{
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200354 int i;
Simon Glass070d00b2015-06-23 15:39:10 -0600355
356 debug(">>spl:board_init_r()\n");
357
358#if defined(CONFIG_SYS_SPL_MALLOC_START)
359 mem_malloc_init(CONFIG_SYS_SPL_MALLOC_START,
360 CONFIG_SYS_SPL_MALLOC_SIZE);
361 gd->flags |= GD_FLG_FULL_MALLOC_INIT;
362#endif
363 if (!(gd->flags & GD_FLG_SPL_INIT)) {
364 if (spl_init())
365 hang();
366 }
Stefan Roeseea8256f2012-08-23 08:34:21 +0200367#ifndef CONFIG_PPC
368 /*
369 * timer_init() does not exist on PPC systems. The timer is initialized
370 * and enabled (decrementer) in interrupt_init() here.
371 */
Ilya Yanok4063c772012-09-17 10:26:26 +0000372 timer_init();
Stefan Roeseea8256f2012-08-23 08:34:21 +0200373#endif
Ilya Yanok4063c772012-09-17 10:26:26 +0000374
Tom Riniee08a822011-11-23 05:13:06 +0000375#ifdef CONFIG_SPL_BOARD_INIT
376 spl_board_init();
377#endif
378
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200379 board_boot_order(spl_boot_list);
380 for (i = 0; i < ARRAY_SIZE(spl_boot_list) &&
381 spl_boot_list[i] != BOOT_DEVICE_NONE; i++) {
Nikita Kiryanov310c8462015-11-08 17:11:52 +0200382 announce_boot_device(spl_boot_list[i]);
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200383 if (!spl_load_image(spl_boot_list[i]))
384 break;
385 }
386
387 if (i == ARRAY_SIZE(spl_boot_list) ||
388 spl_boot_list[i] == BOOT_DEVICE_NONE) {
389 puts("SPL: failed to boot from all boot devices\n");
Aneesh V8cf686e2011-07-21 09:10:27 -0400390 hang();
Nikita Kiryanovf101e4b2015-11-08 17:11:51 +0200391 }
Aneesh V8cf686e2011-07-21 09:10:27 -0400392
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400393 switch (spl_image.os) {
Aneesh V8cf686e2011-07-21 09:10:27 -0400394 case IH_OS_U_BOOT:
395 debug("Jumping to U-Boot\n");
Aneesh V8cf686e2011-07-21 09:10:27 -0400396 break;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000397#ifdef CONFIG_SPL_OS_BOOT
398 case IH_OS_LINUX:
399 debug("Jumping to Linux\n");
400 spl_board_prepare_for_linux();
401 jump_to_image_linux((void *)CONFIG_SYS_SPL_ARGS_ADDR);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000402#endif
Aneesh V8cf686e2011-07-21 09:10:27 -0400403 default:
Tom Rini42120982012-08-27 14:58:28 -0700404 debug("Unsupported OS image.. Jumping nevertheless..\n");
Aneesh V8cf686e2011-07-21 09:10:27 -0400405 }
Simon Glassfb4f5e72014-11-10 17:16:45 -0700406#if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SYS_SPL_MALLOC_SIZE)
407 debug("SPL malloc() used %#lx bytes (%ld KB)\n", gd->malloc_ptr,
408 gd->malloc_ptr / 1024);
409#endif
410
Simon Glassaea3d402015-06-23 15:39:11 -0600411 debug("loaded - jumping to U-Boot...");
Michal Simek3a3b9142016-05-10 07:54:20 +0200412 spl_board_prepare_for_boot();
Allen Martina759f1e2012-10-19 21:08:22 +0000413 jump_to_image_no_args(&spl_image);
Aneesh Vbcae7212011-07-21 09:10:21 -0400414}
415
Tom Rini6507f132012-08-22 15:31:05 -0700416/*
417 * This requires UART clocks to be enabled. In order for this to work the
418 * caller must ensure that the gd pointer is valid.
419 */
Aneesh Vbcae7212011-07-21 09:10:21 -0400420void preloader_console_init(void)
421{
Aneesh Vbcae7212011-07-21 09:10:21 -0400422 gd->bd = &bdata;
Aneesh Vbcae7212011-07-21 09:10:21 -0400423 gd->baudrate = CONFIG_BAUDRATE;
424
Aneesh Vbcae7212011-07-21 09:10:21 -0400425 serial_init(); /* serial communications setup */
426
Ilya Yanokb88befa2011-11-01 13:16:03 +0000427 gd->have_console = 1;
428
Tom Rini026b2fe2012-08-14 12:06:43 -0700429 puts("\nU-Boot SPL " PLAIN_VERSION " (" U_BOOT_DATE " - " \
430 U_BOOT_TIME ")\n");
Tom Rini861a86f2012-08-13 11:37:56 -0700431#ifdef CONFIG_SPL_DISPLAY_PRINT
432 spl_display_print();
433#endif
Tom Rinicc3f7052011-10-04 04:59:23 +0000434}
Simon Glassdb910352015-03-03 08:03:00 -0700435
436/**
437 * spl_relocate_stack_gd() - Relocate stack ready for board_init_r() execution
438 *
439 * Sometimes board_init_f() runs with a stack in SRAM but we want to use SDRAM
440 * for the main board_init_r() execution. This is typically because we need
441 * more stack space for things like the MMC sub-system.
442 *
443 * This function calculates the stack position, copies the global_data into
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100444 * place, sets the new gd (except for ARM, for which setting GD within a C
445 * function may not always work) and returns the new stack position. The
446 * caller is responsible for setting up the sp register and, in the case
447 * of ARM, setting up gd.
448 *
449 * All of this is done using the same layout and alignments as done in
450 * board_init_f_init_reserve() / board_init_f_alloc_reserve().
Simon Glassdb910352015-03-03 08:03:00 -0700451 *
452 * @return new stack location, or 0 to use the same stack
453 */
454ulong spl_relocate_stack_gd(void)
455{
456#ifdef CONFIG_SPL_STACK_R
457 gd_t *new_gd;
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100458 ulong ptr = CONFIG_SPL_STACK_R_ADDR;
Simon Glassdb910352015-03-03 08:03:00 -0700459
Hans de Goededcfcb8d2015-09-13 15:04:17 +0200460#ifdef CONFIG_SPL_SYS_MALLOC_SIMPLE
461 if (CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN) {
462 if (!(gd->flags & GD_FLG_SPL_INIT))
Sjoerd Simons4363de62015-12-04 23:27:35 +0100463 panic_str("spl_init must be called before heap reloc");
Hans de Goededcfcb8d2015-09-13 15:04:17 +0200464
465 ptr -= CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
466 gd->malloc_base = ptr;
467 gd->malloc_limit = CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN;
468 gd->malloc_ptr = 0;
469 }
470#endif
Albert ARIBAUDadc421e2015-11-25 17:56:33 +0100471 /* Get stack position: use 8-byte alignment for ABI compliance */
472 ptr = CONFIG_SPL_STACK_R_ADDR - roundup(sizeof(gd_t),16);
473 new_gd = (gd_t *)ptr;
474 memcpy(new_gd, (void *)gd, sizeof(gd_t));
475#if !defined(CONFIG_ARM)
476 gd = new_gd;
477#endif
Simon Glassdb910352015-03-03 08:03:00 -0700478 return ptr;
479#else
480 return 0;
481#endif
482}