blob: 3b0a15242389e43d303d1338f9491e4c30dac3a4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Schwarze21d2d82011-09-14 15:33:34 -04002/*
3 * Copyright (C) 2011
4 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
Simon Schwarze21d2d82011-09-14 15:33:34 -04005 */
6#include <common.h>
Tom Rini9ef51bd2012-08-14 14:33:02 -07007#include <config.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06008#include <fdt_support.h>
9#include <image.h>
Sean Anderson952ed672023-10-14 16:47:44 -040010#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Tom Rini28591df2012-08-13 12:03:19 -070012#include <spl.h>
Sean Anderson1c398062023-11-08 11:48:51 -050013#include <spl_load.h>
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000014#include <asm/io.h>
Sean Anderson6f5be662023-11-04 16:37:46 -040015#include <mapmem.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040016#include <nand.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt_env.h>
Lokesh Vutla12c49222016-05-24 10:34:42 +053018#include <fdt.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040019
Peng Fan59a62732019-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 Schochercf000272014-10-31 08:31:00 +010025#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi93e88022018-07-04 15:53:37 +020026static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glassee306792016-09-24 18:20:13 -060027 struct spl_boot_device *bootdev)
Simon Schwarze21d2d82011-09-14 15:33:34 -040028{
Heiko Schochercf000272014-10-31 08:31:00 +010029 nand_init();
30
Stefan Roese438b6cd2019-03-26 13:04:00 +010031 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
Tom Rinib4213492022-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 Roese438b6cd2019-03-26 13:04:00 +010034
Peng Fan59a62732019-09-23 10:18:43 +080035 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Tom Rinib4213492022-11-12 17:36:51 -050036 CFG_SYS_NAND_U_BOOT_SIZE,
Sean Anderson6f5be662023-11-04 16:37:46 -040037 map_sysmem(CFG_SYS_NAND_U_BOOT_DST,
38 CFG_SYS_NAND_U_BOOT_SIZE));
Simon Glassee306792016-09-24 18:20:13 -060039 spl_set_header_raw_uboot(spl_image);
Heiko Schochercf000272014-10-31 08:31:00 +010040 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020041
42 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010043}
44#else
Lokesh Vutla12c49222016-05-24 10:34:42 +053045
Sean Anderson06dce4c2023-11-08 11:48:44 -050046__weak u32 nand_spl_adjust_offset(u32 sector, u32 offs)
Lokesh Vutla12c49222016-05-24 10:34:42 +053047{
Sean Anderson06dce4c2023-11-08 11:48:44 -050048 return offs;
Lokesh Vutla12c49222016-05-24 10:34:42 +053049}
50
Sean Anderson06dce4c2023-11-08 11:48:44 -050051static ulong spl_nand_read(struct spl_load_info *load, ulong offs, ulong size,
52 void *dst)
developer1e885e02022-05-20 11:24:04 +080053{
54 int err;
Sean Anderson06dce4c2023-11-08 11:48:44 -050055 ulong sector;
developer1e885e02022-05-20 11:24:04 +080056
57 debug("%s: offs %lx, size %lx, dst %p\n",
58 __func__, offs, size, dst);
59
Sean Anderson06dce4c2023-11-08 11:48:44 -050060 sector = *(int *)load->priv;
61 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
developer1e885e02022-05-20 11:24:04 +080062 err = nand_spl_load_image(offs, size, dst);
Sean Anderson1c398062023-11-08 11:48:51 -050063 spl_set_bl_len(load, nand_page_size());
developer1e885e02022-05-20 11:24:04 +080064 if (err)
65 return 0;
66
67 return size;
68}
69
Simon Glassee306792016-09-24 18:20:13 -060070static int spl_nand_load_element(struct spl_image_info *spl_image,
Sean Anderson1c398062023-11-08 11:48:51 -050071 struct spl_boot_device *bootdev, int offset)
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020072{
Sean Anderson1c398062023-11-08 11:48:51 -050073 struct spl_load_info load;
developer1e885e02022-05-20 11:24:04 +080074
Sean Anderson1c398062023-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 Kiryanov08fb32e62015-11-08 17:11:42 +020079}
80
Simon Glassee306792016-09-24 18:20:13 -060081static int spl_nand_load_image(struct spl_image_info *spl_image,
82 struct spl_boot_device *bootdev)
Heiko Schochercf000272014-10-31 08:31:00 +010083{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020084 int err;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000085
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010086#ifdef CONFIG_SPL_NAND_SOFTECC
87 debug("spl: nand - using sw ecc\n");
88#else
Tom Rini09ebf1a2012-08-13 14:11:06 -070089 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010090#endif
Tom Rini09ebf1a2012-08-13 14:11:06 -070091 nand_init();
Simon Schwarze21d2d82011-09-14 15:33:34 -040092
Tom Rinic2e78882021-10-30 23:03:48 -040093#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz305bc4c2012-03-15 04:01:38 +000094 if (!spl_start_uboot()) {
Sean Anderson1c398062023-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 Schwarz6c3ddde2012-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 Vasut6d016762012-03-31 07:47:17 +0000103 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-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 Glass72cc5382022-10-20 18:22:39 -0600108 (void *)CONFIG_TEXT_BASE);
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000109 /* copy to destintion */
Simon Glass259cdb42023-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 Schwarz6c3ddde2012-03-15 04:01:36 +0000115 writel(readl(src), dst);
116 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400117
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000118 /* load linux */
119 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +0900120 sizeof(*header), (void *)header);
Pali Rohárdda8f882022-01-14 14:31:38 +0100121 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +0200122 if (err)
123 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000124 if (header->ih_os == IH_OS_LINUX) {
125 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200126 err = nand_spl_load_image(
127 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glassee306792016-09-24 18:20:13 -0600128 spl_image->size,
129 (void *)spl_image->load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000130 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200131 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000132 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -0700133 puts("The Expected Linux image was not "
134 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000135 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -0700136 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000137 }
138 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000139#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400140#ifdef CONFIG_NAND_ENV_DST
Sean Anderson1c398062023-11-08 11:48:51 -0500141 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400142#ifdef CONFIG_ENV_OFFSET_REDUND
Sean Anderson1c398062023-11-08 11:48:51 -0500143 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400144#endif
145#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000146 /* Load u-boot */
Sean Anderson1c398062023-11-08 11:48:51 -0500147 err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page());
Boris Brezillon4f238e72016-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árdda8f882022-01-14 14:31:38 +0100151 err = spl_nand_load_element(spl_image, bootdev,
Sean Anderson1c398062023-11-08 11:48:51 -0500152 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND);
Boris Brezillon4f238e72016-06-06 10:16:58 +0200153#endif
154#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400155 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200156 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400157}
Heiko Schochercf000272014-10-31 08:31:00 +0100158#endif
Simon Glass6cf82c92016-09-24 18:20:02 -0600159/* Use priorty 1 so that Ubi can override this */
Simon Glass4fc1f252016-11-30 15:30:50 -0700160SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);