blob: 3e2c07405003931c2bc30817f28bcf000140e917 [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
35 spl_parse_image_header(header);
36 return nand_spl_load_image(offset, spl_image.size,
37 (void *)(unsigned long)spl_image.load_addr);
38}
39
Nikita Kiryanov36afd452015-11-08 17:11:49 +020040int spl_nand_load_image(void)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040041{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020042 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040043 struct image_header *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +000044 int *src __attribute__((unused));
45 int *dst __attribute__((unused));
46
Tom Rini8082fda2012-08-13 14:11:06 -070047 debug("spl: nand - using hw ecc\n");
Tom Rini8082fda2012-08-13 14:11:06 -070048 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040049
50 /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
51 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
Simon Schwarzdf163a52012-03-15 04:01:36 +000052#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz379c19a2012-03-15 04:01:38 +000053 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-03-15 04:01:36 +000054 /*
55 * load parameter image
56 * load to temp position since nand_spl_load_image reads
57 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +000058 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +000059 * following sections like BSS
60 */
61 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
62 CONFIG_CMD_SPL_WRITE_SIZE,
63 (void *)CONFIG_SYS_TEXT_BASE);
64 /* copy to destintion */
65 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
66 src = (int *)CONFIG_SYS_TEXT_BASE;
67 src < (int *)(CONFIG_SYS_TEXT_BASE +
68 CONFIG_CMD_SPL_WRITE_SIZE);
69 src++, dst++) {
70 writel(readl(src), dst);
71 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040072
Simon Schwarzdf163a52012-03-15 04:01:36 +000073 /* load linux */
74 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +090075 sizeof(*header), (void *)header);
Simon Schwarzdf163a52012-03-15 04:01:36 +000076 spl_parse_image_header(header);
Simon Schwarz379c19a2012-03-15 04:01:38 +000077 if (header->ih_os == IH_OS_LINUX) {
78 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +020079 err = nand_spl_load_image(
80 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
81 spl_image.size,
82 (void *)spl_image.load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +000083 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020084 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +000085 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -070086 puts("The Expected Linux image was not "
87 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +000088 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -070089 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +000090 }
Simon Schwarzdf163a52012-03-15 04:01:36 +000091 }
Simon Schwarz379c19a2012-03-15 04:01:38 +000092#endif
93#ifdef CONFIG_NAND_ENV_DST
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020094 spl_nand_load_element(CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +000095#ifdef CONFIG_ENV_OFFSET_REDUND
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020096 spl_nand_load_element(CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +000097#endif
98#endif
99 /* Load u-boot */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200100 err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS, header);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400101 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200102 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400103}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100104#endif