blob: bbd95469870f29ee07edfbf860349d598991ed3f [file] [log] [blame]
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04001/*
2 * Copyright (C) 2011
3 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04006 */
7#include <common.h>
Tom Rinid97b4ce2012-08-14 14:33:02 -07008#include <config.h>
Tom Rini47f7bca2012-08-13 12:03:19 -07009#include <spl.h>
Simon Schwarzdf163a52012-03-15 04:01:36 +000010#include <asm/io.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040011#include <nand.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040012
Heiko Schocher0c3117b2014-10-31 08:31:00 +010013#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Nikita Kiryanov36afd452015-11-08 17:11:49 +020014int spl_nand_load_image(void)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010015{
16 nand_init();
17
18 nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
19 CONFIG_SYS_NAND_U_BOOT_SIZE,
20 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
21 spl_set_header_raw_uboot();
22 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020023
24 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010025}
26#else
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020027static int spl_nand_load_element(int offset, struct image_header *header)
28{
29 int err;
30
31 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
32 if (err)
33 return err;
34
Marek Vasut7e0f2262016-04-29 00:44:54 +020035 err = spl_parse_image_header(header);
36 if (err)
37 return err;
38
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020039 return nand_spl_load_image(offset, spl_image.size,
40 (void *)(unsigned long)spl_image.load_addr);
41}
42
Nikita Kiryanov36afd452015-11-08 17:11:49 +020043int spl_nand_load_image(void)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040044{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020045 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040046 struct image_header *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +000047 int *src __attribute__((unused));
48 int *dst __attribute__((unused));
49
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010050#ifdef CONFIG_SPL_NAND_SOFTECC
51 debug("spl: nand - using sw ecc\n");
52#else
Tom Rini8082fda2012-08-13 14:11:06 -070053 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010054#endif
Tom Rini8082fda2012-08-13 14:11:06 -070055 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040056
57 /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
58 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
Simon Schwarzdf163a52012-03-15 04:01:36 +000059#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz379c19a2012-03-15 04:01:38 +000060 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-03-15 04:01:36 +000061 /*
62 * load parameter image
63 * load to temp position since nand_spl_load_image reads
64 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +000065 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +000066 * following sections like BSS
67 */
68 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
69 CONFIG_CMD_SPL_WRITE_SIZE,
70 (void *)CONFIG_SYS_TEXT_BASE);
71 /* copy to destintion */
72 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
73 src = (int *)CONFIG_SYS_TEXT_BASE;
74 src < (int *)(CONFIG_SYS_TEXT_BASE +
75 CONFIG_CMD_SPL_WRITE_SIZE);
76 src++, dst++) {
77 writel(readl(src), dst);
78 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040079
Simon Schwarzdf163a52012-03-15 04:01:36 +000080 /* load linux */
81 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +090082 sizeof(*header), (void *)header);
Marek Vasut7e0f2262016-04-29 00:44:54 +020083 err = spl_parse_image_header(header);
84 if (err)
85 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +000086 if (header->ih_os == IH_OS_LINUX) {
87 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +020088 err = nand_spl_load_image(
89 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
90 spl_image.size,
91 (void *)spl_image.load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +000092 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020093 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +000094 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -070095 puts("The Expected Linux image was not "
96 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +000097 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -070098 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +000099 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000100 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000101#endif
102#ifdef CONFIG_NAND_ENV_DST
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200103 spl_nand_load_element(CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000104#ifdef CONFIG_ENV_OFFSET_REDUND
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200105 spl_nand_load_element(CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000106#endif
107#endif
108 /* Load u-boot */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200109 err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS, header);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400110 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200111 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400112}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100113#endif