blob: d13a5245974fedb0f3dd0b639f9645d8284be8e2 [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>
Simon Glass2dc9c342020-05-10 11:40:01 -06008#include <fdt_support.h>
9#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Tom Rini28591df2012-08-13 12:03:19 -070011#include <spl.h>
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000012#include <asm/io.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040013#include <nand.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090014#include <linux/libfdt_env.h>
Lokesh Vutla12c49222016-05-24 10:34:42 +053015#include <fdt.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040016
Peng Fan59a62732019-09-23 10:18:43 +080017uint32_t __weak spl_nand_get_uboot_raw_page(void)
18{
19 return CONFIG_SYS_NAND_U_BOOT_OFFS;
20}
21
Heiko Schochercf000272014-10-31 08:31:00 +010022#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi93e88022018-07-04 15:53:37 +020023static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glassee306792016-09-24 18:20:13 -060024 struct spl_boot_device *bootdev)
Simon Schwarze21d2d82011-09-14 15:33:34 -040025{
Heiko Schochercf000272014-10-31 08:31:00 +010026 nand_init();
27
Stefan Roese438b6cd2019-03-26 13:04:00 +010028 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
29 CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
30 CONFIG_SYS_NAND_U_BOOT_DST);
31
Peng Fan59a62732019-09-23 10:18:43 +080032 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Heiko Schochercf000272014-10-31 08:31:00 +010033 CONFIG_SYS_NAND_U_BOOT_SIZE,
34 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
Simon Glassee306792016-09-24 18:20:13 -060035 spl_set_header_raw_uboot(spl_image);
Heiko Schochercf000272014-10-31 08:31:00 +010036 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020037
38 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010039}
40#else
Lokesh Vutla12c49222016-05-24 10:34:42 +053041
42static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
43 ulong size, void *dst)
44{
Dario Binacchia6d91ca2020-05-27 13:56:20 +020045 ulong sector;
Dario Binacchifdcb9792020-05-27 13:56:21 +020046 int err;
Lokesh Vutla12c49222016-05-24 10:34:42 +053047
Dario Binacchia6d91ca2020-05-27 13:56:20 +020048 sector = *(int *)load->priv;
49 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
Dario Binacchifdcb9792020-05-27 13:56:21 +020050 err = nand_spl_load_image(offs, size, dst);
51 if (err)
Lokesh Vutla12c49222016-05-24 10:34:42 +053052 return 0;
Dario Binacchifdcb9792020-05-27 13:56:21 +020053
54 return size;
Lokesh Vutla12c49222016-05-24 10:34:42 +053055}
56
Simon Glassee306792016-09-24 18:20:13 -060057static int spl_nand_load_element(struct spl_image_info *spl_image,
58 int offset, struct image_header *header)
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020059{
60 int err;
61
62 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
63 if (err)
64 return err;
65
Lokesh Vutla12c49222016-05-24 10:34:42 +053066 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
67 image_get_magic(header) == FDT_MAGIC) {
68 struct spl_load_info load;
Marek Vasut02266e22016-04-29 00:44:54 +020069
Lokesh Vutla12c49222016-05-24 10:34:42 +053070 debug("Found FIT\n");
71 load.dev = NULL;
Dario Binacchia6d91ca2020-05-27 13:56:20 +020072 load.priv = &offset;
Lokesh Vutla12c49222016-05-24 10:34:42 +053073 load.filename = NULL;
74 load.bl_len = 1;
75 load.read = spl_nand_fit_read;
Simon Glass43a734f2016-09-24 18:20:16 -060076 return spl_load_simple_fit(spl_image, &load, offset, header);
Peng Fan519ce8d2019-09-23 10:18:46 +080077 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
78 struct spl_load_info load;
79
80 load.dev = NULL;
81 load.priv = NULL;
82 load.filename = NULL;
83 load.bl_len = 1;
84 load.read = spl_nand_fit_read;
85 return spl_load_imx_container(spl_image, &load, offset);
Lokesh Vutla12c49222016-05-24 10:34:42 +053086 } else {
Simon Glassee306792016-09-24 18:20:13 -060087 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla12c49222016-05-24 10:34:42 +053088 if (err)
89 return err;
Simon Glassee306792016-09-24 18:20:13 -060090 return nand_spl_load_image(offset, spl_image->size,
91 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla12c49222016-05-24 10:34:42 +053092 }
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020093}
94
Simon Glassee306792016-09-24 18:20:13 -060095static int spl_nand_load_image(struct spl_image_info *spl_image,
96 struct spl_boot_device *bootdev)
Heiko Schochercf000272014-10-31 08:31:00 +010097{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020098 int err;
Simon Schwarze21d2d82011-09-14 15:33:34 -040099 struct image_header *header;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000100 int *src __attribute__((unused));
101 int *dst __attribute__((unused));
102
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +0100103#ifdef CONFIG_SPL_NAND_SOFTECC
104 debug("spl: nand - using sw ecc\n");
105#else
Tom Rini09ebf1a2012-08-13 14:11:06 -0700106 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +0100107#endif
Tom Rini09ebf1a2012-08-13 14:11:06 -0700108 nand_init();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400109
Marek Vasut1dda46f2018-08-14 11:27:02 +0200110 header = spl_get_load_buffer(0, sizeof(*header));
111
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000112#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000113 if (!spl_start_uboot()) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000114 /*
115 * load parameter image
116 * load to temp position since nand_spl_load_image reads
117 * a whole block which is typically larger than
Marek Vasut6d016762012-03-31 07:47:17 +0000118 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000119 * following sections like BSS
120 */
121 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
122 CONFIG_CMD_SPL_WRITE_SIZE,
123 (void *)CONFIG_SYS_TEXT_BASE);
124 /* copy to destintion */
125 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
126 src = (int *)CONFIG_SYS_TEXT_BASE;
127 src < (int *)(CONFIG_SYS_TEXT_BASE +
128 CONFIG_CMD_SPL_WRITE_SIZE);
129 src++, dst++) {
130 writel(readl(src), dst);
131 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400132
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000133 /* load linux */
134 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +0900135 sizeof(*header), (void *)header);
Simon Glassee306792016-09-24 18:20:13 -0600136 err = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +0200137 if (err)
138 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000139 if (header->ih_os == IH_OS_LINUX) {
140 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200141 err = nand_spl_load_image(
142 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glassee306792016-09-24 18:20:13 -0600143 spl_image->size,
144 (void *)spl_image->load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000145 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200146 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000147 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -0700148 puts("The Expected Linux image was not "
149 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000150 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -0700151 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000152 }
153 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000154#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400155#ifdef CONFIG_NAND_ENV_DST
Simon Glassee306792016-09-24 18:20:13 -0600156 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400157#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassee306792016-09-24 18:20:13 -0600158 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400159#endif
160#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000161 /* Load u-boot */
Peng Fan59a62732019-09-23 10:18:43 +0800162 err = spl_nand_load_element(spl_image, spl_nand_get_uboot_raw_page(),
Simon Glassee306792016-09-24 18:20:13 -0600163 header);
Boris Brezillon4f238e72016-06-06 10:16:58 +0200164#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
165#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
166 if (err)
Simon Glassee306792016-09-24 18:20:13 -0600167 err = spl_nand_load_element(spl_image,
168 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon4f238e72016-06-06 10:16:58 +0200169 header);
170#endif
171#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400172 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200173 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400174}
Heiko Schochercf000272014-10-31 08:31:00 +0100175#endif
Simon Glass6cf82c92016-09-24 18:20:02 -0600176/* Use priorty 1 so that Ubi can override this */
Simon Glass4fc1f252016-11-30 15:30:50 -0700177SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);