blob: 3b0a15242389e43d303d1338f9491e4c30dac3a4 [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>
Sean Anderson11f83462023-11-08 11:48:51 -050013#include <spl_load.h>
Simon Schwarzdf163a52012-03-15 04:01:36 +000014#include <asm/io.h>
Sean Andersonbd9573c2023-11-04 16:37:46 -040015#include <mapmem.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040016#include <nand.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090017#include <linux/libfdt_env.h>
Lokesh Vutla8bd88772016-05-24 10:34:42 +053018#include <fdt.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040019
Peng Fan2c792292019-09-23 10:18:43 +080020uint32_t __weak spl_nand_get_uboot_raw_page(void)
21{
22 return CONFIG_SYS_NAND_U_BOOT_OFFS;
23}
24
Heiko Schocher0c3117b2014-10-31 08:31:00 +010025#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi25dabd72018-07-04 15:53:37 +020026static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060027 struct spl_boot_device *bootdev)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010028{
29 nand_init();
30
Stefan Roese443b3ce2019-03-26 13:04:00 +010031 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
Tom Rini4e590942022-11-12 17:36:51 -050032 CONFIG_SYS_NAND_U_BOOT_OFFS, CFG_SYS_NAND_U_BOOT_SIZE,
33 CFG_SYS_NAND_U_BOOT_DST);
Stefan Roese443b3ce2019-03-26 13:04:00 +010034
Peng Fan2c792292019-09-23 10:18:43 +080035 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Tom Rini4e590942022-11-12 17:36:51 -050036 CFG_SYS_NAND_U_BOOT_SIZE,
Sean Andersonbd9573c2023-11-04 16:37:46 -040037 map_sysmem(CFG_SYS_NAND_U_BOOT_DST,
38 CFG_SYS_NAND_U_BOOT_SIZE));
Simon Glass2a2ee2a2016-09-24 18:20:13 -060039 spl_set_header_raw_uboot(spl_image);
Heiko Schocher0c3117b2014-10-31 08:31:00 +010040 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020041
42 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010043}
44#else
Lokesh Vutla8bd88772016-05-24 10:34:42 +053045
Sean Anderson0ddfa862023-11-08 11:48:44 -050046__weak u32 nand_spl_adjust_offset(u32 sector, u32 offs)
47{
48 return offs;
49}
50
51static ulong spl_nand_read(struct spl_load_info *load, ulong offs, ulong size,
52 void *dst)
Lokesh Vutla8bd88772016-05-24 10:34:42 +053053{
Dario Binacchi84dd1902020-05-27 13:56:21 +020054 int err;
Tim Harvey39cb8502021-03-01 14:33:27 -080055 ulong sector;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053056
Weijie Gao4620e8a2022-05-20 11:24:04 +080057 debug("%s: offs %lx, size %lx, dst %p\n",
58 __func__, offs, size, dst);
59
Sean Anderson0ddfa862023-11-08 11:48:44 -050060 sector = *(int *)load->priv;
61 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
Weijie Gao4620e8a2022-05-20 11:24:04 +080062 err = nand_spl_load_image(offs, size, dst);
Sean Anderson11f83462023-11-08 11:48:51 -050063 spl_set_bl_len(load, nand_page_size());
Weijie Gao4620e8a2022-05-20 11:24:04 +080064 if (err)
65 return 0;
66
67 return size;
68}
69
Simon Glass2a2ee2a2016-09-24 18:20:13 -060070static int spl_nand_load_element(struct spl_image_info *spl_image,
Sean Anderson11f83462023-11-08 11:48:51 -050071 struct spl_boot_device *bootdev, int offset)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020072{
Sean Anderson11f83462023-11-08 11:48:51 -050073 struct spl_load_info load;
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020074
Sean Anderson11f83462023-11-08 11:48:51 -050075 load.priv = &offset;
76 spl_set_bl_len(&load, 1);
77 load.read = spl_nand_read;
78 return spl_load(spl_image, bootdev, &load, 0, offset);
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020079}
80
Simon Glass2a2ee2a2016-09-24 18:20:13 -060081static int spl_nand_load_image(struct spl_image_info *spl_image,
82 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040083{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020084 int err;
Simon Schwarzdf163a52012-03-15 04:01:36 +000085
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010086#ifdef CONFIG_SPL_NAND_SOFTECC
87 debug("spl: nand - using sw ecc\n");
88#else
Tom Rini8082fda2012-08-13 14:11:06 -070089 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010090#endif
Tom Rini8082fda2012-08-13 14:11:06 -070091 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040092
Tom Rini71150072021-10-30 23:03:48 -040093#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz379c19a2012-03-15 04:01:38 +000094 if (!spl_start_uboot()) {
Sean Anderson11f83462023-11-08 11:48:51 -050095 int *src, *dst;
96 struct legacy_img_hdr *header =
97 spl_get_load_buffer(0, sizeof(*header));
98
Simon Schwarzdf163a52012-03-15 04:01:36 +000099 /*
100 * load parameter image
101 * load to temp position since nand_spl_load_image reads
102 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +0000103 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +0000104 * following sections like BSS
105 */
106 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
107 CONFIG_CMD_SPL_WRITE_SIZE,
Simon Glass98463902022-10-20 18:22:39 -0600108 (void *)CONFIG_TEXT_BASE);
Simon Schwarzdf163a52012-03-15 04:01:36 +0000109 /* copy to destintion */
Simon Glass9cbdc3a2023-09-26 08:14:17 -0600110 for (dst = (int *)CONFIG_SPL_PAYLOAD_ARGS_ADDR,
111 src = (int *)CONFIG_TEXT_BASE;
112 src < (int *)(CONFIG_TEXT_BASE +
113 CONFIG_CMD_SPL_WRITE_SIZE);
114 src++, dst++) {
Simon Schwarzdf163a52012-03-15 04:01:36 +0000115 writel(readl(src), dst);
116 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400117
Simon Schwarzdf163a52012-03-15 04:01:36 +0000118 /* load linux */
119 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900120 sizeof(*header), (void *)header);
Pali Rohár2e0429b2022-01-14 14:31:38 +0100121 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200122 if (err)
123 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000124 if (header->ih_os == IH_OS_LINUX) {
125 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200126 err = nand_spl_load_image(
127 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600128 spl_image->size,
129 (void *)spl_image->load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000130 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200131 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000132 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700133 puts("The Expected Linux image was not "
134 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000135 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700136 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000137 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000138 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000139#endif
140#ifdef CONFIG_NAND_ENV_DST
Sean Anderson11f83462023-11-08 11:48:51 -0500141 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000142#ifdef CONFIG_ENV_OFFSET_REDUND
Sean Anderson11f83462023-11-08 11:48:51 -0500143 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000144#endif
145#endif
146 /* Load u-boot */
Sean Anderson11f83462023-11-08 11:48:51 -0500147 err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page());
Boris Brezillon80ef7002016-06-06 10:16:58 +0200148#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
149#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
150 if (err)
Pali Rohár2e0429b2022-01-14 14:31:38 +0100151 err = spl_nand_load_element(spl_image, bootdev,
Sean Anderson11f83462023-11-08 11:48:51 -0500152 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200153#endif
154#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400155 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200156 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400157}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100158#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600159/* Use priorty 1 so that Ubi can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700160SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);