blob: 5f8a111a2f064808dadead4d2e30e1ae0edd354a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Schwarze21d2d82011-09-14 15:33:34 -04002/*
3 * Copyright (C) 2011
4 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
Simon Schwarze21d2d82011-09-14 15:33:34 -04005 */
6#include <common.h>
Tom Rini9ef51bd2012-08-14 14:33:02 -07007#include <config.h>
Tom Rini28591df2012-08-13 12:03:19 -07008#include <spl.h>
Simon Schwarz6c3ddde2012-03-15 04:01:36 +00009#include <asm/io.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040010#include <nand.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090011#include <linux/libfdt_env.h>
Lokesh Vutla12c49222016-05-24 10:34:42 +053012#include <fdt.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040013
Peng Fan59a62732019-09-23 10:18:43 +080014uint32_t __weak spl_nand_get_uboot_raw_page(void)
15{
16 return CONFIG_SYS_NAND_U_BOOT_OFFS;
17}
18
Heiko Schochercf000272014-10-31 08:31:00 +010019#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi93e88022018-07-04 15:53:37 +020020static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glassee306792016-09-24 18:20:13 -060021 struct spl_boot_device *bootdev)
Simon Schwarze21d2d82011-09-14 15:33:34 -040022{
Heiko Schochercf000272014-10-31 08:31:00 +010023 nand_init();
24
Stefan Roese438b6cd2019-03-26 13:04:00 +010025 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
26 CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
27 CONFIG_SYS_NAND_U_BOOT_DST);
28
Peng Fan59a62732019-09-23 10:18:43 +080029 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Heiko Schochercf000272014-10-31 08:31:00 +010030 CONFIG_SYS_NAND_U_BOOT_SIZE,
31 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
Simon Glassee306792016-09-24 18:20:13 -060032 spl_set_header_raw_uboot(spl_image);
Heiko Schochercf000272014-10-31 08:31:00 +010033 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020034
35 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010036}
37#else
Lokesh Vutla12c49222016-05-24 10:34:42 +053038
39static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
40 ulong size, void *dst)
41{
42 int ret;
43
44 ret = nand_spl_load_image(offs, size, dst);
45 if (!ret)
46 return size;
47 else
48 return 0;
49}
50
Simon Glassee306792016-09-24 18:20:13 -060051static int spl_nand_load_element(struct spl_image_info *spl_image,
52 int offset, struct image_header *header)
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020053{
54 int err;
55
56 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
57 if (err)
58 return err;
59
Lokesh Vutla12c49222016-05-24 10:34:42 +053060 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
61 image_get_magic(header) == FDT_MAGIC) {
62 struct spl_load_info load;
Marek Vasut02266e22016-04-29 00:44:54 +020063
Lokesh Vutla12c49222016-05-24 10:34:42 +053064 debug("Found FIT\n");
65 load.dev = NULL;
66 load.priv = NULL;
67 load.filename = NULL;
68 load.bl_len = 1;
69 load.read = spl_nand_fit_read;
Simon Glass43a734f2016-09-24 18:20:16 -060070 return spl_load_simple_fit(spl_image, &load, offset, header);
Peng Fan519ce8d2019-09-23 10:18:46 +080071 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
72 struct spl_load_info load;
73
74 load.dev = NULL;
75 load.priv = NULL;
76 load.filename = NULL;
77 load.bl_len = 1;
78 load.read = spl_nand_fit_read;
79 return spl_load_imx_container(spl_image, &load, offset);
Lokesh Vutla12c49222016-05-24 10:34:42 +053080 } else {
Simon Glassee306792016-09-24 18:20:13 -060081 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla12c49222016-05-24 10:34:42 +053082 if (err)
83 return err;
Simon Glassee306792016-09-24 18:20:13 -060084 return nand_spl_load_image(offset, spl_image->size,
85 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla12c49222016-05-24 10:34:42 +053086 }
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020087}
88
Simon Glassee306792016-09-24 18:20:13 -060089static int spl_nand_load_image(struct spl_image_info *spl_image,
90 struct spl_boot_device *bootdev)
Heiko Schochercf000272014-10-31 08:31:00 +010091{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020092 int err;
Simon Schwarze21d2d82011-09-14 15:33:34 -040093 struct image_header *header;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000094 int *src __attribute__((unused));
95 int *dst __attribute__((unused));
96
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010097#ifdef CONFIG_SPL_NAND_SOFTECC
98 debug("spl: nand - using sw ecc\n");
99#else
Tom Rini09ebf1a2012-08-13 14:11:06 -0700100 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +0100101#endif
Tom Rini09ebf1a2012-08-13 14:11:06 -0700102 nand_init();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400103
Marek Vasut1dda46f2018-08-14 11:27:02 +0200104 header = spl_get_load_buffer(0, sizeof(*header));
105
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000106#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000107 if (!spl_start_uboot()) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000108 /*
109 * load parameter image
110 * load to temp position since nand_spl_load_image reads
111 * a whole block which is typically larger than
Marek Vasut6d016762012-03-31 07:47:17 +0000112 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000113 * following sections like BSS
114 */
115 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
116 CONFIG_CMD_SPL_WRITE_SIZE,
117 (void *)CONFIG_SYS_TEXT_BASE);
118 /* copy to destintion */
119 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
120 src = (int *)CONFIG_SYS_TEXT_BASE;
121 src < (int *)(CONFIG_SYS_TEXT_BASE +
122 CONFIG_CMD_SPL_WRITE_SIZE);
123 src++, dst++) {
124 writel(readl(src), dst);
125 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400126
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000127 /* load linux */
128 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +0900129 sizeof(*header), (void *)header);
Simon Glassee306792016-09-24 18:20:13 -0600130 err = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +0200131 if (err)
132 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000133 if (header->ih_os == IH_OS_LINUX) {
134 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200135 err = nand_spl_load_image(
136 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glassee306792016-09-24 18:20:13 -0600137 spl_image->size,
138 (void *)spl_image->load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000139 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200140 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000141 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -0700142 puts("The Expected Linux image was not "
143 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000144 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -0700145 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000146 }
147 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000148#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400149#ifdef CONFIG_NAND_ENV_DST
Simon Glassee306792016-09-24 18:20:13 -0600150 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400151#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassee306792016-09-24 18:20:13 -0600152 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400153#endif
154#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000155 /* Load u-boot */
Peng Fan59a62732019-09-23 10:18:43 +0800156 err = spl_nand_load_element(spl_image, spl_nand_get_uboot_raw_page(),
Simon Glassee306792016-09-24 18:20:13 -0600157 header);
Boris Brezillon4f238e72016-06-06 10:16:58 +0200158#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
159#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
160 if (err)
Simon Glassee306792016-09-24 18:20:13 -0600161 err = spl_nand_load_element(spl_image,
162 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon4f238e72016-06-06 10:16:58 +0200163 header);
164#endif
165#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400166 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200167 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400168}
Heiko Schochercf000272014-10-31 08:31:00 +0100169#endif
Simon Glass6cf82c92016-09-24 18:20:02 -0600170/* Use priorty 1 so that Ubi can override this */
Simon Glass4fc1f252016-11-30 15:30:50 -0700171SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);