blob: 3e2c07405003931c2bc30817f28bcf000140e917 [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>
Simon Schwarze21d2d82011-09-14 15:33:34 -040012
Heiko Schochercf000272014-10-31 08:31:00 +010013#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020014int spl_nand_load_image(void)
Simon Schwarze21d2d82011-09-14 15:33:34 -040015{
Heiko Schochercf000272014-10-31 08:31:00 +010016 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 Kiryanov33eefe42015-11-08 17:11:49 +020023
24 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010025}
26#else
Nikita Kiryanov08fb32e62015-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 Kiryanov33eefe42015-11-08 17:11:49 +020040int spl_nand_load_image(void)
Heiko Schochercf000272014-10-31 08:31:00 +010041{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020042 int err;
Simon Schwarze21d2d82011-09-14 15:33:34 -040043 struct image_header *header;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000044 int *src __attribute__((unused));
45 int *dst __attribute__((unused));
46
Tom Rini09ebf1a2012-08-13 14:11:06 -070047 debug("spl: nand - using hw ecc\n");
Tom Rini09ebf1a2012-08-13 14:11:06 -070048 nand_init();
Simon Schwarze21d2d82011-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 Schwarz6c3ddde2012-03-15 04:01:36 +000052#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz305bc4c2012-03-15 04:01:38 +000053 if (!spl_start_uboot()) {
Simon Schwarz6c3ddde2012-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 Vasut6d016762012-03-31 07:47:17 +000058 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-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 Schwarze21d2d82011-09-14 15:33:34 -040072
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000073 /* load linux */
74 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +090075 sizeof(*header), (void *)header);
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000076 spl_parse_image_header(header);
Simon Schwarz305bc4c2012-03-15 04:01:38 +000077 if (header->ih_os == IH_OS_LINUX) {
78 /* happy - was a linux */
Nikita Kiryanov33eefe42015-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 Schwarz305bc4c2012-03-15 04:01:38 +000083 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020084 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +000085 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -070086 puts("The Expected Linux image was not "
87 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +000088 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -070089 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +000090 }
91 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000092#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -040093#ifdef CONFIG_NAND_ENV_DST
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020094 spl_nand_load_element(CONFIG_ENV_OFFSET, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -040095#ifdef CONFIG_ENV_OFFSET_REDUND
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020096 spl_nand_load_element(CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -040097#endif
98#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +000099 /* Load u-boot */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200100 err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400101 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200102 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400103}
Heiko Schochercf000272014-10-31 08:31:00 +0100104#endif