blob: d1abda6e4ce1f555defbf3463bcda9b767b13d00 [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 Glass2a2ee2a2016-09-24 18:20:13 -060016int spl_nand_load_image(struct spl_image_info *spl_image,
17 struct spl_boot_device *bootdev)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010018{
19 nand_init();
20
21 nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
22 CONFIG_SYS_NAND_U_BOOT_SIZE,
23 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
Simon Glass2a2ee2a2016-09-24 18:20:13 -060024 spl_set_header_raw_uboot(spl_image);
Heiko Schocher0c3117b2014-10-31 08:31:00 +010025 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020026
27 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010028}
29#else
Lokesh Vutla8bd88772016-05-24 10:34:42 +053030
31static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
32 ulong size, void *dst)
33{
34 int ret;
35
36 ret = nand_spl_load_image(offs, size, dst);
37 if (!ret)
38 return size;
39 else
40 return 0;
41}
42
Simon Glass2a2ee2a2016-09-24 18:20:13 -060043static int spl_nand_load_element(struct spl_image_info *spl_image,
44 int offset, struct image_header *header)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020045{
46 int err;
47
48 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
49 if (err)
50 return err;
51
Lokesh Vutla8bd88772016-05-24 10:34:42 +053052 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
53 image_get_magic(header) == FDT_MAGIC) {
54 struct spl_load_info load;
Marek Vasut7e0f2262016-04-29 00:44:54 +020055
Lokesh Vutla8bd88772016-05-24 10:34:42 +053056 debug("Found FIT\n");
57 load.dev = NULL;
58 load.priv = NULL;
59 load.filename = NULL;
60 load.bl_len = 1;
61 load.read = spl_nand_fit_read;
Simon Glassf4d7d852016-09-24 18:20:16 -060062 return spl_load_simple_fit(spl_image, &load, offset, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053063 } else {
Simon Glass2a2ee2a2016-09-24 18:20:13 -060064 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053065 if (err)
66 return err;
Simon Glass2a2ee2a2016-09-24 18:20:13 -060067 return nand_spl_load_image(offset, spl_image->size,
68 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053069 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020070}
71
Simon Glass2a2ee2a2016-09-24 18:20:13 -060072static int spl_nand_load_image(struct spl_image_info *spl_image,
73 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040074{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020075 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040076 struct image_header *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +000077 int *src __attribute__((unused));
78 int *dst __attribute__((unused));
79
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010080#ifdef CONFIG_SPL_NAND_SOFTECC
81 debug("spl: nand - using sw ecc\n");
82#else
Tom Rini8082fda2012-08-13 14:11:06 -070083 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010084#endif
Tom Rini8082fda2012-08-13 14:11:06 -070085 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040086
87 /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
88 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
Simon Schwarzdf163a52012-03-15 04:01:36 +000089#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz379c19a2012-03-15 04:01:38 +000090 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-03-15 04:01:36 +000091 /*
92 * load parameter image
93 * load to temp position since nand_spl_load_image reads
94 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +000095 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +000096 * following sections like BSS
97 */
98 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
99 CONFIG_CMD_SPL_WRITE_SIZE,
100 (void *)CONFIG_SYS_TEXT_BASE);
101 /* copy to destintion */
102 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
103 src = (int *)CONFIG_SYS_TEXT_BASE;
104 src < (int *)(CONFIG_SYS_TEXT_BASE +
105 CONFIG_CMD_SPL_WRITE_SIZE);
106 src++, dst++) {
107 writel(readl(src), dst);
108 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400109
Simon Schwarzdf163a52012-03-15 04:01:36 +0000110 /* load linux */
111 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900112 sizeof(*header), (void *)header);
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600113 err = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200114 if (err)
115 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000116 if (header->ih_os == IH_OS_LINUX) {
117 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200118 err = nand_spl_load_image(
119 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600120 spl_image->size,
121 (void *)spl_image->load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000122 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200123 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000124 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700125 puts("The Expected Linux image was not "
126 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000127 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700128 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000129 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000130 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000131#endif
132#ifdef CONFIG_NAND_ENV_DST
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600133 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000134#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600135 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000136#endif
137#endif
138 /* Load u-boot */
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600139 err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS,
140 header);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200141#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
142#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
143 if (err)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600144 err = spl_nand_load_element(spl_image,
145 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon80ef7002016-06-06 10:16:58 +0200146 header);
147#endif
148#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400149 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200150 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400151}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100152#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600153/* Use priorty 1 so that Ubi can override this */
154SPL_LOAD_IMAGE_METHOD(1, BOOT_DEVICE_NAND, spl_nand_load_image);