blob: 07916bedbb91c607b780c4245a5268f970c50f51 [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>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040014#include <nand.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090015#include <linux/libfdt_env.h>
Lokesh Vutla8bd88772016-05-24 10:34:42 +053016#include <fdt.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040017
Peng Fan2c792292019-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 Schocher0c3117b2014-10-31 08:31:00 +010023#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi25dabd72018-07-04 15:53:37 +020024static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060025 struct spl_boot_device *bootdev)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010026{
27 nand_init();
28
Stefan Roese443b3ce2019-03-26 13:04:00 +010029 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
Tom Rini4e590942022-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 Roese443b3ce2019-03-26 13:04:00 +010032
Peng Fan2c792292019-09-23 10:18:43 +080033 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Tom Rini4e590942022-11-12 17:36:51 -050034 CFG_SYS_NAND_U_BOOT_SIZE,
35 (void *)CFG_SYS_NAND_U_BOOT_DST);
Simon Glass2a2ee2a2016-09-24 18:20:13 -060036 spl_set_header_raw_uboot(spl_image);
Heiko Schocher0c3117b2014-10-31 08:31:00 +010037 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020038
39 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010040}
41#else
Lokesh Vutla8bd88772016-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 Binacchi84dd1902020-05-27 13:56:21 +020046 int err;
Tim Harvey39cb8502021-03-01 14:33:27 -080047 ulong sector;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053048
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020049 sector = *(int *)load->priv;
Tim Harveyaa0032f2021-03-01 14:33:28 -080050 offs *= load->bl_len;
51 size *= load->bl_len;
Michael Trimarchicc980142022-05-15 11:35:33 +020052 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
Dario Binacchi84dd1902020-05-27 13:56:21 +020053 err = nand_spl_load_image(offs, size, dst);
54 if (err)
Lokesh Vutla8bd88772016-05-24 10:34:42 +053055 return 0;
Dario Binacchi84dd1902020-05-27 13:56:21 +020056
Tim Harveyaa0032f2021-03-01 14:33:28 -080057 return size / load->bl_len;
58}
59
Weijie Gao4620e8a2022-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 Harveyaa0032f2021-03-01 14:33:28 -080075struct mtd_info * __weak nand_get_mtd(void)
76{
77 return NULL;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053078}
79
Simon Glass2a2ee2a2016-09-24 18:20:13 -060080static int spl_nand_load_element(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010081 struct spl_boot_device *bootdev,
Simon Glassf3543e62022-09-06 20:26:52 -060082 int offset, struct legacy_img_hdr *header)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020083{
Tim Harveyaa0032f2021-03-01 14:33:28 -080084 struct mtd_info *mtd = nand_get_mtd();
85 int bl_len = mtd ? mtd->writesize : 1;
Nikita Kiryanov483ab3d2015-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 Vutla8bd88772016-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 Vasut7e0f2262016-04-29 00:44:54 +020095
Lokesh Vutla8bd88772016-05-24 10:34:42 +053096 debug("Found FIT\n");
97 load.dev = NULL;
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020098 load.priv = &offset;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053099 load.filename = NULL;
Tim Harveyaa0032f2021-03-01 14:33:28 -0800100 load.bl_len = bl_len;
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530101 load.read = spl_nand_fit_read;
Tim Harveyaa0032f2021-03-01 14:33:28 -0800102 return spl_load_simple_fit(spl_image, &load, offset / bl_len, header);
Sean Andersonab121792023-10-14 16:47:44 -0400103 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
104 valid_container_hdr((void *)header)) {
Peng Fan00e180c2019-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 Harveyaa0032f2021-03-01 14:33:28 -0800110 load.bl_len = bl_len;
Peng Fan00e180c2019-09-23 10:18:46 +0800111 load.read = spl_nand_fit_read;
Tim Harveyaa0032f2021-03-01 14:33:28 -0800112 return spl_load_imx_container(spl_image, &load, offset / bl_len);
Weijie Gao4620e8a2022-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 Quadros06377c52022-09-29 13:11:28 +0300124 return spl_load_legacy_img(spl_image, bootdev, &load, offset, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530125 } else {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100126 err = spl_parse_image_header(spl_image, bootdev, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530127 if (err)
128 return err;
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600129 return nand_spl_load_image(offset, spl_image->size,
130 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530131 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200132}
133
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600134static int spl_nand_load_image(struct spl_image_info *spl_image,
135 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400136{
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200137 int err;
Simon Glassf3543e62022-09-06 20:26:52 -0600138 struct legacy_img_hdr *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +0000139 int *src __attribute__((unused));
140 int *dst __attribute__((unused));
141
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100142#ifdef CONFIG_SPL_NAND_SOFTECC
143 debug("spl: nand - using sw ecc\n");
144#else
Tom Rini8082fda2012-08-13 14:11:06 -0700145 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100146#endif
Tom Rini8082fda2012-08-13 14:11:06 -0700147 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400148
Marek Vasut04ce5422018-08-14 11:27:02 +0200149 header = spl_get_load_buffer(0, sizeof(*header));
150
Tom Rini71150072021-10-30 23:03:48 -0400151#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz379c19a2012-03-15 04:01:38 +0000152 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-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 Vasutb6e95fd2012-03-31 07:47:17 +0000157 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-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 Glass98463902022-10-20 18:22:39 -0600162 (void *)CONFIG_TEXT_BASE);
Simon Schwarzdf163a52012-03-15 04:01:36 +0000163 /* copy to destintion */
Simon Glass9cbdc3a2023-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 Schwarzdf163a52012-03-15 04:01:36 +0000169 writel(readl(src), dst);
170 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400171
Simon Schwarzdf163a52012-03-15 04:01:36 +0000172 /* load linux */
173 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900174 sizeof(*header), (void *)header);
Pali Rohár2e0429b2022-01-14 14:31:38 +0100175 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200176 if (err)
177 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000178 if (header->ih_os == IH_OS_LINUX) {
179 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200180 err = nand_spl_load_image(
181 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600182 spl_image->size,
183 (void *)spl_image->load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000184 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200185 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000186 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700187 puts("The Expected Linux image was not "
188 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000189 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700190 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000191 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000192 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000193#endif
194#ifdef CONFIG_NAND_ENV_DST
Pali Rohár2e0429b2022-01-14 14:31:38 +0100195 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000196#ifdef CONFIG_ENV_OFFSET_REDUND
Pali Rohár2e0429b2022-01-14 14:31:38 +0100197 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000198#endif
199#endif
200 /* Load u-boot */
Pali Rohár2e0429b2022-01-14 14:31:38 +0100201 err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page(),
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600202 header);
Boris Brezillon80ef7002016-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ár2e0429b2022-01-14 14:31:38 +0100206 err = spl_nand_load_element(spl_image, bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600207 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon80ef7002016-06-06 10:16:58 +0200208 header);
209#endif
210#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400211 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200212 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400213}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100214#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600215/* Use priorty 1 so that Ubi can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700216SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);