blob: dc45204fc0e68538752afadfeb0507a938217376 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070011#include <spl.h>
Simon Schwarzdf163a52012-03-15 04:01:36 +000012#include <asm/io.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040013#include <nand.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090014#include <linux/libfdt_env.h>
Lokesh Vutla8bd88772016-05-24 10:34:42 +053015#include <fdt.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040016
Peng Fan2c792292019-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 Schocher0c3117b2014-10-31 08:31:00 +010022#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi25dabd72018-07-04 15:53:37 +020023static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060024 struct spl_boot_device *bootdev)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010025{
26 nand_init();
27
Stefan Roese443b3ce2019-03-26 13:04:00 +010028 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
Tom Rini4e590942022-11-12 17:36:51 -050029 CONFIG_SYS_NAND_U_BOOT_OFFS, CFG_SYS_NAND_U_BOOT_SIZE,
30 CFG_SYS_NAND_U_BOOT_DST);
Stefan Roese443b3ce2019-03-26 13:04:00 +010031
Peng Fan2c792292019-09-23 10:18:43 +080032 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Tom Rini4e590942022-11-12 17:36:51 -050033 CFG_SYS_NAND_U_BOOT_SIZE,
34 (void *)CFG_SYS_NAND_U_BOOT_DST);
Simon Glass2a2ee2a2016-09-24 18:20:13 -060035 spl_set_header_raw_uboot(spl_image);
Heiko Schocher0c3117b2014-10-31 08:31:00 +010036 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020037
38 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010039}
40#else
Lokesh Vutla8bd88772016-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 Binacchi84dd1902020-05-27 13:56:21 +020045 int err;
Tim Harvey39cb8502021-03-01 14:33:27 -080046 ulong sector;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053047
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020048 sector = *(int *)load->priv;
Tim Harveyaa0032f2021-03-01 14:33:28 -080049 offs *= load->bl_len;
50 size *= load->bl_len;
Michael Trimarchicc980142022-05-15 11:35:33 +020051 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
Dario Binacchi84dd1902020-05-27 13:56:21 +020052 err = nand_spl_load_image(offs, size, dst);
53 if (err)
Lokesh Vutla8bd88772016-05-24 10:34:42 +053054 return 0;
Dario Binacchi84dd1902020-05-27 13:56:21 +020055
Tim Harveyaa0032f2021-03-01 14:33:28 -080056 return size / load->bl_len;
57}
58
Weijie Gao4620e8a2022-05-20 11:24:04 +080059static ulong spl_nand_legacy_read(struct spl_load_info *load, ulong offs,
60 ulong size, void *dst)
61{
62 int err;
63
64 debug("%s: offs %lx, size %lx, dst %p\n",
65 __func__, offs, size, dst);
66
67 err = nand_spl_load_image(offs, size, dst);
68 if (err)
69 return 0;
70
71 return size;
72}
73
Tim Harveyaa0032f2021-03-01 14:33:28 -080074struct mtd_info * __weak nand_get_mtd(void)
75{
76 return NULL;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053077}
78
Simon Glass2a2ee2a2016-09-24 18:20:13 -060079static int spl_nand_load_element(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010080 struct spl_boot_device *bootdev,
Simon Glassf3543e62022-09-06 20:26:52 -060081 int offset, struct legacy_img_hdr *header)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020082{
Tim Harveyaa0032f2021-03-01 14:33:28 -080083 struct mtd_info *mtd = nand_get_mtd();
84 int bl_len = mtd ? mtd->writesize : 1;
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020085 int err;
86
87 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
88 if (err)
89 return err;
90
Lokesh Vutla8bd88772016-05-24 10:34:42 +053091 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
92 image_get_magic(header) == FDT_MAGIC) {
93 struct spl_load_info load;
Marek Vasut7e0f2262016-04-29 00:44:54 +020094
Lokesh Vutla8bd88772016-05-24 10:34:42 +053095 debug("Found FIT\n");
96 load.dev = NULL;
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020097 load.priv = &offset;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053098 load.filename = NULL;
Tim Harveyaa0032f2021-03-01 14:33:28 -080099 load.bl_len = bl_len;
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530100 load.read = spl_nand_fit_read;
Tim Harveyaa0032f2021-03-01 14:33:28 -0800101 return spl_load_simple_fit(spl_image, &load, offset / bl_len, header);
Peng Fan00e180c2019-09-23 10:18:46 +0800102 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
103 struct spl_load_info load;
104
105 load.dev = NULL;
106 load.priv = NULL;
107 load.filename = NULL;
Tim Harveyaa0032f2021-03-01 14:33:28 -0800108 load.bl_len = bl_len;
Peng Fan00e180c2019-09-23 10:18:46 +0800109 load.read = spl_nand_fit_read;
Tim Harveyaa0032f2021-03-01 14:33:28 -0800110 return spl_load_imx_container(spl_image, &load, offset / bl_len);
Weijie Gao4620e8a2022-05-20 11:24:04 +0800111 } else if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT) &&
112 image_get_magic(header) == IH_MAGIC) {
113 struct spl_load_info load;
114
115 debug("Found legacy image\n");
116 load.dev = NULL;
117 load.priv = NULL;
118 load.filename = NULL;
119 load.bl_len = 1;
120 load.read = spl_nand_legacy_read;
121
Roger Quadros06377c52022-09-29 13:11:28 +0300122 return spl_load_legacy_img(spl_image, bootdev, &load, offset, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530123 } else {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100124 err = spl_parse_image_header(spl_image, bootdev, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530125 if (err)
126 return err;
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600127 return nand_spl_load_image(offset, spl_image->size,
128 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530129 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200130}
131
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600132static int spl_nand_load_image(struct spl_image_info *spl_image,
133 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400134{
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200135 int err;
Simon Glassf3543e62022-09-06 20:26:52 -0600136 struct legacy_img_hdr *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +0000137 int *src __attribute__((unused));
138 int *dst __attribute__((unused));
139
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100140#ifdef CONFIG_SPL_NAND_SOFTECC
141 debug("spl: nand - using sw ecc\n");
142#else
Tom Rini8082fda2012-08-13 14:11:06 -0700143 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100144#endif
Tom Rini8082fda2012-08-13 14:11:06 -0700145 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400146
Marek Vasut04ce5422018-08-14 11:27:02 +0200147 header = spl_get_load_buffer(0, sizeof(*header));
148
Tom Rini71150072021-10-30 23:03:48 -0400149#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz379c19a2012-03-15 04:01:38 +0000150 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-03-15 04:01:36 +0000151 /*
152 * load parameter image
153 * load to temp position since nand_spl_load_image reads
154 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +0000155 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +0000156 * following sections like BSS
157 */
158 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
159 CONFIG_CMD_SPL_WRITE_SIZE,
Simon Glass98463902022-10-20 18:22:39 -0600160 (void *)CONFIG_TEXT_BASE);
Simon Schwarzdf163a52012-03-15 04:01:36 +0000161 /* copy to destintion */
162 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
Simon Glass98463902022-10-20 18:22:39 -0600163 src = (int *)CONFIG_TEXT_BASE;
164 src < (int *)(CONFIG_TEXT_BASE +
Simon Schwarzdf163a52012-03-15 04:01:36 +0000165 CONFIG_CMD_SPL_WRITE_SIZE);
166 src++, dst++) {
167 writel(readl(src), dst);
168 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400169
Simon Schwarzdf163a52012-03-15 04:01:36 +0000170 /* load linux */
171 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900172 sizeof(*header), (void *)header);
Pali Rohár2e0429b2022-01-14 14:31:38 +0100173 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200174 if (err)
175 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000176 if (header->ih_os == IH_OS_LINUX) {
177 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200178 err = nand_spl_load_image(
179 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600180 spl_image->size,
181 (void *)spl_image->load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000182 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200183 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000184 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700185 puts("The Expected Linux image was not "
186 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000187 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700188 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000189 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000190 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000191#endif
192#ifdef CONFIG_NAND_ENV_DST
Pali Rohár2e0429b2022-01-14 14:31:38 +0100193 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000194#ifdef CONFIG_ENV_OFFSET_REDUND
Pali Rohár2e0429b2022-01-14 14:31:38 +0100195 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000196#endif
197#endif
198 /* Load u-boot */
Pali Rohár2e0429b2022-01-14 14:31:38 +0100199 err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page(),
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600200 header);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200201#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
202#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
203 if (err)
Pali Rohár2e0429b2022-01-14 14:31:38 +0100204 err = spl_nand_load_element(spl_image, bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600205 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon80ef7002016-06-06 10:16:58 +0200206 header);
207#endif
208#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400209 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200210 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400211}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100212#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600213/* Use priorty 1 so that Ubi can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700214SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);