blob: ed758e518ca8adf8d8d29ebdb068c65f79f95698 [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>
Lokesh Vutla8bd88772016-05-24 10:34:42 +053012#include <libfdt_env.h>
13#include <fdt.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040014
Heiko Schocher0c3117b2014-10-31 08:31:00 +010015#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Simon Glassecdfd692016-09-24 18:19:57 -060016int spl_nand_load_image(struct spl_boot_device *bootdev)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010017{
18 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);
Simon Glassd95ceb92016-09-24 18:19:52 -060023 spl_set_header_raw_uboot(&spl_image);
Heiko Schocher0c3117b2014-10-31 08:31:00 +010024 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020025
26 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010027}
28#else
Lokesh Vutla8bd88772016-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 Kiryanov483ab3d2015-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 Vutla8bd88772016-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 Vasut7e0f2262016-04-29 00:44:54 +020053
Lokesh Vutla8bd88772016-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 {
Simon Glass71316c12016-09-24 18:19:53 -060062 err = spl_parse_image_header(&spl_image, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053063 if (err)
64 return err;
65 return nand_spl_load_image(offset, spl_image.size,
66 (void *)(ulong)spl_image.load_addr);
67 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020068}
69
Simon Glassd5c2b112016-09-24 18:20:02 -060070static int spl_nand_load_image(struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040071{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020072 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040073 struct image_header *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +000074 int *src __attribute__((unused));
75 int *dst __attribute__((unused));
76
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010077#ifdef CONFIG_SPL_NAND_SOFTECC
78 debug("spl: nand - using sw ecc\n");
79#else
Tom Rini8082fda2012-08-13 14:11:06 -070080 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010081#endif
Tom Rini8082fda2012-08-13 14:11:06 -070082 nand_init();
Simon Schwarz9ea5c6e2011-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 Schwarzdf163a52012-03-15 04:01:36 +000086#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz379c19a2012-03-15 04:01:38 +000087 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-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 Vasutb6e95fd2012-03-31 07:47:17 +000092 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-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 Schwarz9ea5c6e2011-09-14 15:33:34 -0400106
Simon Schwarzdf163a52012-03-15 04:01:36 +0000107 /* load linux */
108 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900109 sizeof(*header), (void *)header);
Simon Glass71316c12016-09-24 18:19:53 -0600110 err = spl_parse_image_header(&spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200111 if (err)
112 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000113 if (header->ih_os == IH_OS_LINUX) {
114 /* happy - was a linux */
Nikita Kiryanov36afd452015-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 Schwarz379c19a2012-03-15 04:01:38 +0000119 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200120 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000121 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700122 puts("The Expected Linux image was not "
123 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000124 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700125 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000126 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000127 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000128#endif
129#ifdef CONFIG_NAND_ENV_DST
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200130 spl_nand_load_element(CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000131#ifdef CONFIG_ENV_OFFSET_REDUND
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200132 spl_nand_load_element(CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000133#endif
134#endif
135 /* Load u-boot */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200136 err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS, header);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200137#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
138#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
139 if (err)
140 err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
141 header);
142#endif
143#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400144 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200145 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400146}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100147#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600148/* Use priorty 1 so that Ubi can override this */
149SPL_LOAD_IMAGE_METHOD(1, BOOT_DEVICE_NAND, spl_nand_load_image);