blob: 07916bedbb91c607b780c4245a5268f970c50f51 [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>
Sean Anderson952ed672023-10-14 16:47:44 -040010#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Tom Rini28591df2012-08-13 12:03:19 -070012#include <spl.h>
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000013#include <asm/io.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040014#include <nand.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt_env.h>
Lokesh Vutla12c49222016-05-24 10:34:42 +053016#include <fdt.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040017
Peng Fan59a62732019-09-23 10:18:43 +080018uint32_t __weak spl_nand_get_uboot_raw_page(void)
19{
20 return CONFIG_SYS_NAND_U_BOOT_OFFS;
21}
22
Heiko Schochercf000272014-10-31 08:31:00 +010023#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi93e88022018-07-04 15:53:37 +020024static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glassee306792016-09-24 18:20:13 -060025 struct spl_boot_device *bootdev)
Simon Schwarze21d2d82011-09-14 15:33:34 -040026{
Heiko Schochercf000272014-10-31 08:31:00 +010027 nand_init();
28
Stefan Roese438b6cd2019-03-26 13:04:00 +010029 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
Tom Rinib4213492022-11-12 17:36:51 -050030 CONFIG_SYS_NAND_U_BOOT_OFFS, CFG_SYS_NAND_U_BOOT_SIZE,
31 CFG_SYS_NAND_U_BOOT_DST);
Stefan Roese438b6cd2019-03-26 13:04:00 +010032
Peng Fan59a62732019-09-23 10:18:43 +080033 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Tom Rinib4213492022-11-12 17:36:51 -050034 CFG_SYS_NAND_U_BOOT_SIZE,
35 (void *)CFG_SYS_NAND_U_BOOT_DST);
Simon Glassee306792016-09-24 18:20:13 -060036 spl_set_header_raw_uboot(spl_image);
Heiko Schochercf000272014-10-31 08:31:00 +010037 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020038
39 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010040}
41#else
Lokesh Vutla12c49222016-05-24 10:34:42 +053042
43static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
44 ulong size, void *dst)
45{
Dario Binacchifdcb9792020-05-27 13:56:21 +020046 int err;
Tim Harvey26205a32021-03-01 14:33:27 -080047 ulong sector;
Lokesh Vutla12c49222016-05-24 10:34:42 +053048
Dario Binacchia6d91ca2020-05-27 13:56:20 +020049 sector = *(int *)load->priv;
Tim Harveya2c63f02021-03-01 14:33:28 -080050 offs *= load->bl_len;
51 size *= load->bl_len;
Michael Trimarchi7396b7d2022-05-15 11:35:33 +020052 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
Dario Binacchifdcb9792020-05-27 13:56:21 +020053 err = nand_spl_load_image(offs, size, dst);
54 if (err)
Lokesh Vutla12c49222016-05-24 10:34:42 +053055 return 0;
Dario Binacchifdcb9792020-05-27 13:56:21 +020056
Tim Harveya2c63f02021-03-01 14:33:28 -080057 return size / load->bl_len;
Lokesh Vutla12c49222016-05-24 10:34:42 +053058}
59
developer1e885e02022-05-20 11:24:04 +080060static ulong spl_nand_legacy_read(struct spl_load_info *load, ulong offs,
61 ulong size, void *dst)
62{
63 int err;
64
65 debug("%s: offs %lx, size %lx, dst %p\n",
66 __func__, offs, size, dst);
67
68 err = nand_spl_load_image(offs, size, dst);
69 if (err)
70 return 0;
71
72 return size;
73}
74
Tim Harveya2c63f02021-03-01 14:33:28 -080075struct mtd_info * __weak nand_get_mtd(void)
76{
77 return NULL;
78}
79
Simon Glassee306792016-09-24 18:20:13 -060080static int spl_nand_load_element(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010081 struct spl_boot_device *bootdev,
Simon Glassbb7d3bb2022-09-06 20:26:52 -060082 int offset, struct legacy_img_hdr *header)
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020083{
Tim Harveya2c63f02021-03-01 14:33:28 -080084 struct mtd_info *mtd = nand_get_mtd();
85 int bl_len = mtd ? mtd->writesize : 1;
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020086 int err;
87
88 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
89 if (err)
90 return err;
91
Lokesh Vutla12c49222016-05-24 10:34:42 +053092 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
93 image_get_magic(header) == FDT_MAGIC) {
94 struct spl_load_info load;
Marek Vasut02266e22016-04-29 00:44:54 +020095
Lokesh Vutla12c49222016-05-24 10:34:42 +053096 debug("Found FIT\n");
97 load.dev = NULL;
Dario Binacchia6d91ca2020-05-27 13:56:20 +020098 load.priv = &offset;
Lokesh Vutla12c49222016-05-24 10:34:42 +053099 load.filename = NULL;
Tim Harveya2c63f02021-03-01 14:33:28 -0800100 load.bl_len = bl_len;
Lokesh Vutla12c49222016-05-24 10:34:42 +0530101 load.read = spl_nand_fit_read;
Tim Harveya2c63f02021-03-01 14:33:28 -0800102 return spl_load_simple_fit(spl_image, &load, offset / bl_len, header);
Sean Anderson952ed672023-10-14 16:47:44 -0400103 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
104 valid_container_hdr((void *)header)) {
Peng Fan519ce8d2019-09-23 10:18:46 +0800105 struct spl_load_info load;
106
107 load.dev = NULL;
108 load.priv = NULL;
109 load.filename = NULL;
Tim Harveya2c63f02021-03-01 14:33:28 -0800110 load.bl_len = bl_len;
Peng Fan519ce8d2019-09-23 10:18:46 +0800111 load.read = spl_nand_fit_read;
Tim Harveya2c63f02021-03-01 14:33:28 -0800112 return spl_load_imx_container(spl_image, &load, offset / bl_len);
developer1e885e02022-05-20 11:24:04 +0800113 } else if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT) &&
114 image_get_magic(header) == IH_MAGIC) {
115 struct spl_load_info load;
116
117 debug("Found legacy image\n");
118 load.dev = NULL;
119 load.priv = NULL;
120 load.filename = NULL;
121 load.bl_len = 1;
122 load.read = spl_nand_legacy_read;
123
Roger Quadros237f0182022-09-29 13:11:28 +0300124 return spl_load_legacy_img(spl_image, bootdev, &load, offset, header);
Lokesh Vutla12c49222016-05-24 10:34:42 +0530125 } else {
Pali Rohárdda8f882022-01-14 14:31:38 +0100126 err = spl_parse_image_header(spl_image, bootdev, header);
Lokesh Vutla12c49222016-05-24 10:34:42 +0530127 if (err)
128 return err;
Simon Glassee306792016-09-24 18:20:13 -0600129 return nand_spl_load_image(offset, spl_image->size,
130 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla12c49222016-05-24 10:34:42 +0530131 }
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +0200132}
133
Simon Glassee306792016-09-24 18:20:13 -0600134static int spl_nand_load_image(struct spl_image_info *spl_image,
135 struct spl_boot_device *bootdev)
Heiko Schochercf000272014-10-31 08:31:00 +0100136{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200137 int err;
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600138 struct legacy_img_hdr *header;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000139 int *src __attribute__((unused));
140 int *dst __attribute__((unused));
141
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +0100142#ifdef CONFIG_SPL_NAND_SOFTECC
143 debug("spl: nand - using sw ecc\n");
144#else
Tom Rini09ebf1a2012-08-13 14:11:06 -0700145 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +0100146#endif
Tom Rini09ebf1a2012-08-13 14:11:06 -0700147 nand_init();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400148
Marek Vasut1dda46f2018-08-14 11:27:02 +0200149 header = spl_get_load_buffer(0, sizeof(*header));
150
Tom Rinic2e78882021-10-30 23:03:48 -0400151#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000152 if (!spl_start_uboot()) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000153 /*
154 * load parameter image
155 * load to temp position since nand_spl_load_image reads
156 * a whole block which is typically larger than
Marek Vasut6d016762012-03-31 07:47:17 +0000157 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000158 * following sections like BSS
159 */
160 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
161 CONFIG_CMD_SPL_WRITE_SIZE,
Simon Glass72cc5382022-10-20 18:22:39 -0600162 (void *)CONFIG_TEXT_BASE);
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000163 /* copy to destintion */
Simon Glass259cdb42023-09-26 08:14:17 -0600164 for (dst = (int *)CONFIG_SPL_PAYLOAD_ARGS_ADDR,
165 src = (int *)CONFIG_TEXT_BASE;
166 src < (int *)(CONFIG_TEXT_BASE +
167 CONFIG_CMD_SPL_WRITE_SIZE);
168 src++, dst++) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000169 writel(readl(src), dst);
170 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400171
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000172 /* load linux */
173 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +0900174 sizeof(*header), (void *)header);
Pali Rohárdda8f882022-01-14 14:31:38 +0100175 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +0200176 if (err)
177 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000178 if (header->ih_os == IH_OS_LINUX) {
179 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200180 err = nand_spl_load_image(
181 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glassee306792016-09-24 18:20:13 -0600182 spl_image->size,
183 (void *)spl_image->load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000184 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200185 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000186 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -0700187 puts("The Expected Linux image was not "
188 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000189 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -0700190 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000191 }
192 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000193#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400194#ifdef CONFIG_NAND_ENV_DST
Pali Rohárdda8f882022-01-14 14:31:38 +0100195 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400196#ifdef CONFIG_ENV_OFFSET_REDUND
Pali Rohárdda8f882022-01-14 14:31:38 +0100197 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400198#endif
199#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000200 /* Load u-boot */
Pali Rohárdda8f882022-01-14 14:31:38 +0100201 err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page(),
Simon Glassee306792016-09-24 18:20:13 -0600202 header);
Boris Brezillon4f238e72016-06-06 10:16:58 +0200203#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
204#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
205 if (err)
Pali Rohárdda8f882022-01-14 14:31:38 +0100206 err = spl_nand_load_element(spl_image, bootdev,
Simon Glassee306792016-09-24 18:20:13 -0600207 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon4f238e72016-06-06 10:16:58 +0200208 header);
209#endif
210#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400211 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200212 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400213}
Heiko Schochercf000272014-10-31 08:31:00 +0100214#endif
Simon Glass6cf82c92016-09-24 18:20:02 -0600215/* Use priorty 1 so that Ubi can override this */
Simon Glass4fc1f252016-11-30 15:30:50 -0700216SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);