blob: 79388ff326a828f89eea814edf66e7b03d7ac5a2 [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
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010047#ifdef CONFIG_SPL_NAND_SOFTECC
48 debug("spl: nand - using sw ecc\n");
49#else
Tom Rini09ebf1a2012-08-13 14:11:06 -070050 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010051#endif
Tom Rini09ebf1a2012-08-13 14:11:06 -070052 nand_init();
Simon Schwarze21d2d82011-09-14 15:33:34 -040053
54 /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
55 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000056#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz305bc4c2012-03-15 04:01:38 +000057 if (!spl_start_uboot()) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000058 /*
59 * load parameter image
60 * load to temp position since nand_spl_load_image reads
61 * a whole block which is typically larger than
Marek Vasut6d016762012-03-31 07:47:17 +000062 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000063 * following sections like BSS
64 */
65 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
66 CONFIG_CMD_SPL_WRITE_SIZE,
67 (void *)CONFIG_SYS_TEXT_BASE);
68 /* copy to destintion */
69 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
70 src = (int *)CONFIG_SYS_TEXT_BASE;
71 src < (int *)(CONFIG_SYS_TEXT_BASE +
72 CONFIG_CMD_SPL_WRITE_SIZE);
73 src++, dst++) {
74 writel(readl(src), dst);
75 }
Simon Schwarze21d2d82011-09-14 15:33:34 -040076
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000077 /* load linux */
78 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +090079 sizeof(*header), (void *)header);
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000080 spl_parse_image_header(header);
Simon Schwarz305bc4c2012-03-15 04:01:38 +000081 if (header->ih_os == IH_OS_LINUX) {
82 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020083 err = nand_spl_load_image(
84 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
85 spl_image.size,
86 (void *)spl_image.load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +000087 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020088 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +000089 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -070090 puts("The Expected Linux image was not "
91 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +000092 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -070093 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +000094 }
95 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000096#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -040097#ifdef CONFIG_NAND_ENV_DST
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020098 spl_nand_load_element(CONFIG_ENV_OFFSET, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -040099#ifdef CONFIG_ENV_OFFSET_REDUND
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +0200100 spl_nand_load_element(CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400101#endif
102#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000103 /* Load u-boot */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200104 err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400105 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200106 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400107}
Heiko Schochercf000272014-10-31 08:31:00 +0100108#endif