blob: 7cf0d1b986000b3851cd06e57b349633f9a4bea1 [file] [log] [blame]
Simon Schwarze21d2d82011-09-14 15:33:34 -04001/*
2 * Copyright (C) 2011
3 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarze21d2d82011-09-14 15:33:34 -04006 */
7#include <common.h>
Tom Rini9ef51bd2012-08-14 14:33:02 -07008#include <config.h>
Tom Rini28591df2012-08-13 12:03:19 -07009#include <spl.h>
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000010#include <asm/io.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040011#include <nand.h>
Lokesh Vutla12c49222016-05-24 10:34:42 +053012#include <libfdt_env.h>
13#include <fdt.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040014
Heiko Schochercf000272014-10-31 08:31:00 +010015#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020016int spl_nand_load_image(void)
Simon Schwarze21d2d82011-09-14 15:33:34 -040017{
Heiko Schochercf000272014-10-31 08:31:00 +010018 nand_init();
19
20 nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
21 CONFIG_SYS_NAND_U_BOOT_SIZE,
22 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
23 spl_set_header_raw_uboot();
24 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020025
26 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010027}
28#else
Lokesh Vutla12c49222016-05-24 10:34:42 +053029
30static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
31 ulong size, void *dst)
32{
33 int ret;
34
35 ret = nand_spl_load_image(offs, size, dst);
36 if (!ret)
37 return size;
38 else
39 return 0;
40}
41
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020042static int spl_nand_load_element(int offset, struct image_header *header)
43{
44 int err;
45
46 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
47 if (err)
48 return err;
49
Lokesh Vutla12c49222016-05-24 10:34:42 +053050 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
51 image_get_magic(header) == FDT_MAGIC) {
52 struct spl_load_info load;
Marek Vasut02266e22016-04-29 00:44:54 +020053
Lokesh Vutla12c49222016-05-24 10:34:42 +053054 debug("Found FIT\n");
55 load.dev = NULL;
56 load.priv = NULL;
57 load.filename = NULL;
58 load.bl_len = 1;
59 load.read = spl_nand_fit_read;
60 return spl_load_simple_fit(&load, offset, header);
61 } else {
62 err = spl_parse_image_header(header);
63 if (err)
64 return err;
65 return nand_spl_load_image(offset, spl_image.size,
66 (void *)(ulong)spl_image.load_addr);
67 }
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020068}
69
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020070int spl_nand_load_image(void)
Heiko Schochercf000272014-10-31 08:31:00 +010071{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020072 int err;
Simon Schwarze21d2d82011-09-14 15:33:34 -040073 struct image_header *header;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000074 int *src __attribute__((unused));
75 int *dst __attribute__((unused));
76
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010077#ifdef CONFIG_SPL_NAND_SOFTECC
78 debug("spl: nand - using sw ecc\n");
79#else
Tom Rini09ebf1a2012-08-13 14:11:06 -070080 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010081#endif
Tom Rini09ebf1a2012-08-13 14:11:06 -070082 nand_init();
Simon Schwarze21d2d82011-09-14 15:33:34 -040083
84 /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
85 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000086#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz305bc4c2012-03-15 04:01:38 +000087 if (!spl_start_uboot()) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000088 /*
89 * load parameter image
90 * load to temp position since nand_spl_load_image reads
91 * a whole block which is typically larger than
Marek Vasut6d016762012-03-31 07:47:17 +000092 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000093 * following sections like BSS
94 */
95 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
96 CONFIG_CMD_SPL_WRITE_SIZE,
97 (void *)CONFIG_SYS_TEXT_BASE);
98 /* copy to destintion */
99 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
100 src = (int *)CONFIG_SYS_TEXT_BASE;
101 src < (int *)(CONFIG_SYS_TEXT_BASE +
102 CONFIG_CMD_SPL_WRITE_SIZE);
103 src++, dst++) {
104 writel(readl(src), dst);
105 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400106
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000107 /* load linux */
108 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +0900109 sizeof(*header), (void *)header);
Marek Vasut02266e22016-04-29 00:44:54 +0200110 err = spl_parse_image_header(header);
111 if (err)
112 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000113 if (header->ih_os == IH_OS_LINUX) {
114 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200115 err = nand_spl_load_image(
116 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
117 spl_image.size,
118 (void *)spl_image.load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000119 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200120 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000121 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -0700122 puts("The Expected Linux image was not "
123 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000124 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -0700125 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000126 }
127 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000128#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400129#ifdef CONFIG_NAND_ENV_DST
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +0200130 spl_nand_load_element(CONFIG_ENV_OFFSET, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400131#ifdef CONFIG_ENV_OFFSET_REDUND
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +0200132 spl_nand_load_element(CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400133#endif
134#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000135 /* Load u-boot */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200136 err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400137 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200138 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400139}
Heiko Schochercf000272014-10-31 08:31:00 +0100140#endif