blob: 969e319de06a5457a70b7a1093533f68f0b96ede [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese33d34642012-08-27 12:50:59 +02002/*
3 * Copyright (C) 2012 Stefan Roese <sr@denx.de>
Stefan Roese33d34642012-08-27 12:50:59 +02004 */
5
6#include <common.h>
7#include <spl.h>
8
York Sun6ecd8202018-06-26 10:10:03 -07009#ifdef CONFIG_SPL_LOAD_FIT
10static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
11 ulong count, void *buf)
12{
13 debug("%s: sector %lx, count %lx, buf %p\n",
14 __func__, sector, count, buf);
15 memcpy(buf, (void *)sector, count);
16
17 return count;
18}
19#endif
20
Simon Glass2a2ee2a2016-09-24 18:20:13 -060021static int spl_nor_load_image(struct spl_image_info *spl_image,
22 struct spl_boot_device *bootdev)
Stefan Roese33d34642012-08-27 12:50:59 +020023{
Marek Vasut7e0f2262016-04-29 00:44:54 +020024 int ret;
York Sun6ecd8202018-06-26 10:10:03 -070025 __maybe_unused const struct image_header *header;
26 __maybe_unused struct spl_load_info load;
27
Stefan Roese33d34642012-08-27 12:50:59 +020028 /*
29 * Loading of the payload to SDRAM is done with skipping of
30 * the mkimage header in this SPL NOR driver
31 */
Simon Glass2a2ee2a2016-09-24 18:20:13 -060032 spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
Stefan Roese33d34642012-08-27 12:50:59 +020033
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090034#ifdef CONFIG_SPL_OS_BOOT
35 if (!spl_start_uboot()) {
Stefan Roese33d34642012-08-27 12:50:59 +020036 /*
37 * Load Linux from its location in NOR flash to its defined
38 * location in SDRAM
39 */
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090040 header = (const struct image_header *)CONFIG_SYS_OS_BASE;
York Sun6ecd8202018-06-26 10:10:03 -070041#ifdef CONFIG_SPL_LOAD_FIT
42 if (image_get_magic(header) == FDT_MAGIC) {
43 debug("Found FIT\n");
44 load.bl_len = 1;
45 load.read = spl_nor_load_read;
Stefan Roese33d34642012-08-27 12:50:59 +020046
York Sun6ecd8202018-06-26 10:10:03 -070047 ret = spl_load_simple_fit(spl_image, &load,
48 CONFIG_SYS_OS_BASE,
49 (void *)header);
50
51 return ret;
52 }
53#endif
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090054 if (image_get_os(header) == IH_OS_LINUX) {
55 /* happy - was a Linux */
Stefan Roese33d34642012-08-27 12:50:59 +020056
Simon Glass2a2ee2a2016-09-24 18:20:13 -060057 ret = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020058 if (ret)
59 return ret;
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090060
Simon Glass2a2ee2a2016-09-24 18:20:13 -060061 memcpy((void *)spl_image->load_addr,
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090062 (void *)(CONFIG_SYS_OS_BASE +
63 sizeof(struct image_header)),
Simon Glass2a2ee2a2016-09-24 18:20:13 -060064 spl_image->size);
York Sun14acea02018-06-26 10:10:04 -070065#ifdef CONFIG_SYS_FDT_BASE
Vikas Manocha5bf52502017-04-07 15:38:13 -070066 spl_image->arg = (void *)CONFIG_SYS_FDT_BASE;
York Sun14acea02018-06-26 10:10:04 -070067#endif
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090068
Nikita Kiryanov36afd452015-11-08 17:11:49 +020069 return 0;
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090070 } else {
71 puts("The Expected Linux image was not found.\n"
72 "Please check your NOR configuration.\n"
73 "Trying to start u-boot now...\n");
74 }
Stefan Roese33d34642012-08-27 12:50:59 +020075 }
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090076#endif
77
78 /*
79 * Load real U-Boot from its location in NOR flash to its
80 * defined location in SDRAM
81 */
York Sun6ecd8202018-06-26 10:10:03 -070082#ifdef CONFIG_SPL_LOAD_FIT
83 header = (const struct image_header *)CONFIG_SYS_UBOOT_BASE;
84 if (image_get_magic(header) == FDT_MAGIC) {
85 debug("Found FIT format U-Boot\n");
86 load.bl_len = 1;
87 load.read = spl_nor_load_read;
88 ret = spl_load_simple_fit(spl_image, &load,
89 CONFIG_SYS_UBOOT_BASE,
90 (void *)header);
91
92 return ret;
93 }
94#endif
Simon Glass2a2ee2a2016-09-24 18:20:13 -060095 ret = spl_parse_image_header(spl_image,
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090096 (const struct image_header *)CONFIG_SYS_UBOOT_BASE);
Marek Vasut7e0f2262016-04-29 00:44:54 +020097 if (ret)
98 return ret;
Masahiro Yamada9f9d8702015-01-08 19:23:35 +090099
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600100 memcpy((void *)(unsigned long)spl_image->load_addr,
Masahiro Yamada9f9d8702015-01-08 19:23:35 +0900101 (void *)(CONFIG_SYS_UBOOT_BASE + sizeof(struct image_header)),
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600102 spl_image->size);
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200103
104 return 0;
Stefan Roese33d34642012-08-27 12:50:59 +0200105}
Simon Glassebc4ef62016-11-30 15:30:50 -0700106SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);