blob: 91fa7674f178565cc40f29dbaad8c26262172fa0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04002/*
3 * Copyright (C) 2011
4 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04005 */
6#include <common.h>
Tom Rinid97b4ce2012-08-14 14:33:02 -07007#include <config.h>
Simon Glass4d72caa2020-05-10 11:40:01 -06008#include <fdt_support.h>
9#include <image.h>
Sean Andersonab121792023-10-14 16:47:44 -040010#include <imx_container.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070012#include <spl.h>
Simon Schwarzdf163a52012-03-15 04:01:36 +000013#include <asm/io.h>
Sean Andersonbd9573c2023-11-04 16:37:46 -040014#include <mapmem.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040015#include <nand.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090016#include <linux/libfdt_env.h>
Lokesh Vutla8bd88772016-05-24 10:34:42 +053017#include <fdt.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040018
Peng Fan2c792292019-09-23 10:18:43 +080019uint32_t __weak spl_nand_get_uboot_raw_page(void)
20{
21 return CONFIG_SYS_NAND_U_BOOT_OFFS;
22}
23
Heiko Schocher0c3117b2014-10-31 08:31:00 +010024#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi25dabd72018-07-04 15:53:37 +020025static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060026 struct spl_boot_device *bootdev)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010027{
28 nand_init();
29
Stefan Roese443b3ce2019-03-26 13:04:00 +010030 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
Tom Rini4e590942022-11-12 17:36:51 -050031 CONFIG_SYS_NAND_U_BOOT_OFFS, CFG_SYS_NAND_U_BOOT_SIZE,
32 CFG_SYS_NAND_U_BOOT_DST);
Stefan Roese443b3ce2019-03-26 13:04:00 +010033
Peng Fan2c792292019-09-23 10:18:43 +080034 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Tom Rini4e590942022-11-12 17:36:51 -050035 CFG_SYS_NAND_U_BOOT_SIZE,
Sean Andersonbd9573c2023-11-04 16:37:46 -040036 map_sysmem(CFG_SYS_NAND_U_BOOT_DST,
37 CFG_SYS_NAND_U_BOOT_SIZE));
Simon Glass2a2ee2a2016-09-24 18:20:13 -060038 spl_set_header_raw_uboot(spl_image);
Heiko Schocher0c3117b2014-10-31 08:31:00 +010039 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020040
41 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010042}
43#else
Lokesh Vutla8bd88772016-05-24 10:34:42 +053044
Sean Anderson0ddfa862023-11-08 11:48:44 -050045__weak u32 nand_spl_adjust_offset(u32 sector, u32 offs)
46{
47 return offs;
48}
49
50static ulong spl_nand_read(struct spl_load_info *load, ulong offs, ulong size,
51 void *dst)
Lokesh Vutla8bd88772016-05-24 10:34:42 +053052{
Dario Binacchi84dd1902020-05-27 13:56:21 +020053 int err;
Tim Harvey39cb8502021-03-01 14:33:27 -080054 ulong sector;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053055
Weijie Gao4620e8a2022-05-20 11:24:04 +080056 debug("%s: offs %lx, size %lx, dst %p\n",
57 __func__, offs, size, dst);
58
Sean Anderson0ddfa862023-11-08 11:48:44 -050059 sector = *(int *)load->priv;
60 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
Weijie Gao4620e8a2022-05-20 11:24:04 +080061 err = nand_spl_load_image(offs, size, dst);
62 if (err)
63 return 0;
64
65 return size;
66}
67
Simon Glass2a2ee2a2016-09-24 18:20:13 -060068static int spl_nand_load_element(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010069 struct spl_boot_device *bootdev,
Simon Glassf3543e62022-09-06 20:26:52 -060070 int offset, struct legacy_img_hdr *header)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020071{
Sean Anderson38ef64e2023-11-04 16:37:44 -040072 int bl_len;
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020073 int err;
74
75 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
76 if (err)
77 return err;
78
Sean Anderson38ef64e2023-11-04 16:37:44 -040079 bl_len = nand_page_size();
Lokesh Vutla8bd88772016-05-24 10:34:42 +053080 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
81 image_get_magic(header) == FDT_MAGIC) {
82 struct spl_load_info load;
Marek Vasut7e0f2262016-04-29 00:44:54 +020083
Lokesh Vutla8bd88772016-05-24 10:34:42 +053084 debug("Found FIT\n");
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020085 load.priv = &offset;
Sean Anderson5271e352023-11-08 11:48:43 -050086 spl_set_bl_len(&load, bl_len);
Sean Anderson0ddfa862023-11-08 11:48:44 -050087 load.read = spl_nand_read;
Sean Anderson73c40fc2023-11-08 11:48:40 -050088 return spl_load_simple_fit(spl_image, &load, offset, header);
Sean Andersonab121792023-10-14 16:47:44 -040089 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
90 valid_container_hdr((void *)header)) {
Peng Fan00e180c2019-09-23 10:18:46 +080091 struct spl_load_info load;
92
Sean Anderson23fe0c02023-11-04 16:37:39 -040093 load.priv = &offset;
Sean Anderson5271e352023-11-08 11:48:43 -050094 spl_set_bl_len(&load, bl_len);
Sean Anderson0ddfa862023-11-08 11:48:44 -050095 load.read = spl_nand_read;
Sean Anderson73c40fc2023-11-08 11:48:40 -050096 return spl_load_imx_container(spl_image, &load, offset);
Weijie Gao4620e8a2022-05-20 11:24:04 +080097 } else if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT) &&
98 image_get_magic(header) == IH_MAGIC) {
99 struct spl_load_info load;
100
101 debug("Found legacy image\n");
Sean Anderson0ddfa862023-11-08 11:48:44 -0500102 load.priv = &offset;
Sean Anderson5271e352023-11-08 11:48:43 -0500103 spl_set_bl_len(&load, IS_ENABLED(CONFIG_SPL_LZMA) ? bl_len : 1);
Sean Anderson0ddfa862023-11-08 11:48:44 -0500104 load.read = spl_nand_read;
Roger Quadros06377c52022-09-29 13:11:28 +0300105 return spl_load_legacy_img(spl_image, bootdev, &load, offset, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530106 } else {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100107 err = spl_parse_image_header(spl_image, bootdev, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530108 if (err)
109 return err;
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600110 return nand_spl_load_image(offset, spl_image->size,
Sean Andersonbd9573c2023-11-04 16:37:46 -0400111 map_sysmem(spl_image->load_addr,
112 spl_image->size));
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530113 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200114}
115
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600116static int spl_nand_load_image(struct spl_image_info *spl_image,
117 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400118{
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200119 int err;
Simon Glassf3543e62022-09-06 20:26:52 -0600120 struct legacy_img_hdr *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +0000121 int *src __attribute__((unused));
122 int *dst __attribute__((unused));
123
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100124#ifdef CONFIG_SPL_NAND_SOFTECC
125 debug("spl: nand - using sw ecc\n");
126#else
Tom Rini8082fda2012-08-13 14:11:06 -0700127 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100128#endif
Tom Rini8082fda2012-08-13 14:11:06 -0700129 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400130
Marek Vasut04ce5422018-08-14 11:27:02 +0200131 header = spl_get_load_buffer(0, sizeof(*header));
132
Tom Rini71150072021-10-30 23:03:48 -0400133#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz379c19a2012-03-15 04:01:38 +0000134 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-03-15 04:01:36 +0000135 /*
136 * load parameter image
137 * load to temp position since nand_spl_load_image reads
138 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +0000139 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +0000140 * following sections like BSS
141 */
142 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
143 CONFIG_CMD_SPL_WRITE_SIZE,
Simon Glass98463902022-10-20 18:22:39 -0600144 (void *)CONFIG_TEXT_BASE);
Simon Schwarzdf163a52012-03-15 04:01:36 +0000145 /* copy to destintion */
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600146 for (dst = (int *)CONFIG_SPL_PAYLOAD_ARGS_ADDR,
147 src = (int *)CONFIG_TEXT_BASE;
148 src < (int *)(CONFIG_TEXT_BASE +
149 CONFIG_CMD_SPL_WRITE_SIZE);
150 src++, dst++) {
Simon Schwarzdf163a52012-03-15 04:01:36 +0000151 writel(readl(src), dst);
152 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400153
Simon Schwarzdf163a52012-03-15 04:01:36 +0000154 /* load linux */
155 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900156 sizeof(*header), (void *)header);
Pali Rohár2e0429b2022-01-14 14:31:38 +0100157 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200158 if (err)
159 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000160 if (header->ih_os == IH_OS_LINUX) {
161 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200162 err = nand_spl_load_image(
163 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600164 spl_image->size,
165 (void *)spl_image->load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000166 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200167 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000168 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700169 puts("The Expected Linux image was not "
170 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000171 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700172 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000173 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000174 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000175#endif
176#ifdef CONFIG_NAND_ENV_DST
Pali Rohár2e0429b2022-01-14 14:31:38 +0100177 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000178#ifdef CONFIG_ENV_OFFSET_REDUND
Pali Rohár2e0429b2022-01-14 14:31:38 +0100179 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000180#endif
181#endif
182 /* Load u-boot */
Pali Rohár2e0429b2022-01-14 14:31:38 +0100183 err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page(),
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600184 header);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200185#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
186#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
187 if (err)
Pali Rohár2e0429b2022-01-14 14:31:38 +0100188 err = spl_nand_load_element(spl_image, bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600189 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon80ef7002016-06-06 10:16:58 +0200190 header);
191#endif
192#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400193 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200194 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400195}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100196#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600197/* Use priorty 1 so that Ubi can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700198SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);