blob: 1fcc89fa66036773baea3843a09f86f7db890b70 [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
45static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
46 ulong size, void *dst)
47{
Dario Binacchi84dd1902020-05-27 13:56:21 +020048 int err;
Tim Harvey39cb8502021-03-01 14:33:27 -080049 ulong sector;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053050
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020051 sector = *(int *)load->priv;
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
Sean Anderson73c40fc2023-11-08 11:48:40 -050057 return size;
Tim Harveyaa0032f2021-03-01 14:33:28 -080058}
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
Simon Glass2a2ee2a2016-09-24 18:20:13 -060075static int spl_nand_load_element(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010076 struct spl_boot_device *bootdev,
Simon Glassf3543e62022-09-06 20:26:52 -060077 int offset, struct legacy_img_hdr *header)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020078{
Sean Anderson38ef64e2023-11-04 16:37:44 -040079 int bl_len;
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020080 int err;
81
82 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
83 if (err)
84 return err;
85
Sean Anderson38ef64e2023-11-04 16:37:44 -040086 bl_len = nand_page_size();
Lokesh Vutla8bd88772016-05-24 10:34:42 +053087 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
88 image_get_magic(header) == FDT_MAGIC) {
89 struct spl_load_info load;
Marek Vasut7e0f2262016-04-29 00:44:54 +020090
Lokesh Vutla8bd88772016-05-24 10:34:42 +053091 debug("Found FIT\n");
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020092 load.priv = &offset;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053093 load.filename = NULL;
Tim Harveyaa0032f2021-03-01 14:33:28 -080094 load.bl_len = bl_len;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053095 load.read = spl_nand_fit_read;
Sean Anderson73c40fc2023-11-08 11:48:40 -050096 return spl_load_simple_fit(spl_image, &load, offset, header);
Sean Andersonab121792023-10-14 16:47:44 -040097 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) &&
98 valid_container_hdr((void *)header)) {
Peng Fan00e180c2019-09-23 10:18:46 +080099 struct spl_load_info load;
100
Sean Anderson23fe0c02023-11-04 16:37:39 -0400101 load.priv = &offset;
Peng Fan00e180c2019-09-23 10:18:46 +0800102 load.filename = NULL;
Tim Harveyaa0032f2021-03-01 14:33:28 -0800103 load.bl_len = bl_len;
Peng Fan00e180c2019-09-23 10:18:46 +0800104 load.read = spl_nand_fit_read;
Sean Anderson73c40fc2023-11-08 11:48:40 -0500105 return spl_load_imx_container(spl_image, &load, offset);
Weijie Gao4620e8a2022-05-20 11:24:04 +0800106 } else if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT) &&
107 image_get_magic(header) == IH_MAGIC) {
108 struct spl_load_info load;
109
110 debug("Found legacy image\n");
Weijie Gao4620e8a2022-05-20 11:24:04 +0800111 load.filename = NULL;
Sean Anderson38ef64e2023-11-04 16:37:44 -0400112 load.bl_len = IS_ENABLED(CONFIG_SPL_LZMA) ? bl_len : 1;
Weijie Gao4620e8a2022-05-20 11:24:04 +0800113 load.read = spl_nand_legacy_read;
114
Roger Quadros06377c52022-09-29 13:11:28 +0300115 return spl_load_legacy_img(spl_image, bootdev, &load, offset, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530116 } else {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100117 err = spl_parse_image_header(spl_image, bootdev, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530118 if (err)
119 return err;
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600120 return nand_spl_load_image(offset, spl_image->size,
Sean Andersonbd9573c2023-11-04 16:37:46 -0400121 map_sysmem(spl_image->load_addr,
122 spl_image->size));
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530123 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200124}
125
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600126static int spl_nand_load_image(struct spl_image_info *spl_image,
127 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400128{
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200129 int err;
Simon Glassf3543e62022-09-06 20:26:52 -0600130 struct legacy_img_hdr *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +0000131 int *src __attribute__((unused));
132 int *dst __attribute__((unused));
133
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100134#ifdef CONFIG_SPL_NAND_SOFTECC
135 debug("spl: nand - using sw ecc\n");
136#else
Tom Rini8082fda2012-08-13 14:11:06 -0700137 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100138#endif
Tom Rini8082fda2012-08-13 14:11:06 -0700139 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400140
Marek Vasut04ce5422018-08-14 11:27:02 +0200141 header = spl_get_load_buffer(0, sizeof(*header));
142
Tom Rini71150072021-10-30 23:03:48 -0400143#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz379c19a2012-03-15 04:01:38 +0000144 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-03-15 04:01:36 +0000145 /*
146 * load parameter image
147 * load to temp position since nand_spl_load_image reads
148 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +0000149 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +0000150 * following sections like BSS
151 */
152 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
153 CONFIG_CMD_SPL_WRITE_SIZE,
Simon Glass98463902022-10-20 18:22:39 -0600154 (void *)CONFIG_TEXT_BASE);
Simon Schwarzdf163a52012-03-15 04:01:36 +0000155 /* copy to destintion */
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600156 for (dst = (int *)CONFIG_SPL_PAYLOAD_ARGS_ADDR,
157 src = (int *)CONFIG_TEXT_BASE;
158 src < (int *)(CONFIG_TEXT_BASE +
159 CONFIG_CMD_SPL_WRITE_SIZE);
160 src++, dst++) {
Simon Schwarzdf163a52012-03-15 04:01:36 +0000161 writel(readl(src), dst);
162 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400163
Simon Schwarzdf163a52012-03-15 04:01:36 +0000164 /* load linux */
165 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900166 sizeof(*header), (void *)header);
Pali Rohár2e0429b2022-01-14 14:31:38 +0100167 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200168 if (err)
169 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000170 if (header->ih_os == IH_OS_LINUX) {
171 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200172 err = nand_spl_load_image(
173 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600174 spl_image->size,
175 (void *)spl_image->load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000176 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200177 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000178 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700179 puts("The Expected Linux image was not "
180 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000181 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700182 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000183 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000184 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000185#endif
186#ifdef CONFIG_NAND_ENV_DST
Pali Rohár2e0429b2022-01-14 14:31:38 +0100187 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000188#ifdef CONFIG_ENV_OFFSET_REDUND
Pali Rohár2e0429b2022-01-14 14:31:38 +0100189 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000190#endif
191#endif
192 /* Load u-boot */
Pali Rohár2e0429b2022-01-14 14:31:38 +0100193 err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page(),
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600194 header);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200195#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
196#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
197 if (err)
Pali Rohár2e0429b2022-01-14 14:31:38 +0100198 err = spl_nand_load_element(spl_image, bootdev,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600199 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon80ef7002016-06-06 10:16:58 +0200200 header);
201#endif
202#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400203 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200204 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400205}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100206#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600207/* Use priorty 1 so that Ubi can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700208SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);