blob: 22883f4e8b9a7638512560e1835a9ee325b493dc [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 */
Tom Rini9ef51bd2012-08-14 14:33:02 -07006#include <config.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06007#include <fdt_support.h>
8#include <image.h>
Sean Anderson952ed672023-10-14 16:47:44 -04009#include <imx_container.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Tom Rini28591df2012-08-13 12:03:19 -070011#include <spl.h>
Sean Anderson1c398062023-11-08 11:48:51 -050012#include <spl_load.h>
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000013#include <asm/io.h>
Sean Anderson6f5be662023-11-04 16:37:46 -040014#include <mapmem.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040015#include <nand.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt_env.h>
Lokesh Vutla12c49222016-05-24 10:34:42 +053017#include <fdt.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040018
Peng Fan59a62732019-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 Schochercf000272014-10-31 08:31:00 +010024#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi93e88022018-07-04 15:53:37 +020025static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glassee306792016-09-24 18:20:13 -060026 struct spl_boot_device *bootdev)
Simon Schwarze21d2d82011-09-14 15:33:34 -040027{
Heiko Schochercf000272014-10-31 08:31:00 +010028 nand_init();
29
Stefan Roese438b6cd2019-03-26 13:04:00 +010030 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
Tom Rinib4213492022-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 Roese438b6cd2019-03-26 13:04:00 +010033
Peng Fan59a62732019-09-23 10:18:43 +080034 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Tom Rinib4213492022-11-12 17:36:51 -050035 CFG_SYS_NAND_U_BOOT_SIZE,
Sean Anderson6f5be662023-11-04 16:37:46 -040036 map_sysmem(CFG_SYS_NAND_U_BOOT_DST,
37 CFG_SYS_NAND_U_BOOT_SIZE));
Simon Glassee306792016-09-24 18:20:13 -060038 spl_set_header_raw_uboot(spl_image);
Heiko Schochercf000272014-10-31 08:31:00 +010039 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020040
41 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010042}
43#else
Lokesh Vutla12c49222016-05-24 10:34:42 +053044
Sean Anderson06dce4c2023-11-08 11:48:44 -050045__weak u32 nand_spl_adjust_offset(u32 sector, u32 offs)
Lokesh Vutla12c49222016-05-24 10:34:42 +053046{
Sean Anderson06dce4c2023-11-08 11:48:44 -050047 return offs;
Lokesh Vutla12c49222016-05-24 10:34:42 +053048}
49
Sean Anderson06dce4c2023-11-08 11:48:44 -050050static ulong spl_nand_read(struct spl_load_info *load, ulong offs, ulong size,
51 void *dst)
developer1e885e02022-05-20 11:24:04 +080052{
53 int err;
Sean Anderson06dce4c2023-11-08 11:48:44 -050054 ulong sector;
developer1e885e02022-05-20 11:24:04 +080055
56 debug("%s: offs %lx, size %lx, dst %p\n",
57 __func__, offs, size, dst);
58
Sean Anderson06dce4c2023-11-08 11:48:44 -050059 sector = *(int *)load->priv;
60 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
developer1e885e02022-05-20 11:24:04 +080061 err = nand_spl_load_image(offs, size, dst);
Sean Anderson1c398062023-11-08 11:48:51 -050062 spl_set_bl_len(load, nand_page_size());
developer1e885e02022-05-20 11:24:04 +080063 if (err)
64 return 0;
65
66 return size;
67}
68
Simon Glassee306792016-09-24 18:20:13 -060069static int spl_nand_load_element(struct spl_image_info *spl_image,
Sean Anderson1c398062023-11-08 11:48:51 -050070 struct spl_boot_device *bootdev, int offset)
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020071{
Sean Anderson1c398062023-11-08 11:48:51 -050072 struct spl_load_info load;
developer1e885e02022-05-20 11:24:04 +080073
Simon Glasse8066862024-08-22 07:55:02 -060074 spl_load_init(&load, spl_nand_read, &offset, 1);
Sean Anderson1c398062023-11-08 11:48:51 -050075 return spl_load(spl_image, bootdev, &load, 0, offset);
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020076}
77
Simon Glassee306792016-09-24 18:20:13 -060078static int spl_nand_load_image(struct spl_image_info *spl_image,
79 struct spl_boot_device *bootdev)
Heiko Schochercf000272014-10-31 08:31:00 +010080{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020081 int err;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000082
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010083#ifdef CONFIG_SPL_NAND_SOFTECC
84 debug("spl: nand - using sw ecc\n");
85#else
Tom Rini09ebf1a2012-08-13 14:11:06 -070086 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010087#endif
Tom Rini09ebf1a2012-08-13 14:11:06 -070088 nand_init();
Simon Schwarze21d2d82011-09-14 15:33:34 -040089
Tom Rinic2e78882021-10-30 23:03:48 -040090#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Schwarz305bc4c2012-03-15 04:01:38 +000091 if (!spl_start_uboot()) {
Sean Anderson1c398062023-11-08 11:48:51 -050092 int *src, *dst;
93 struct legacy_img_hdr *header =
94 spl_get_load_buffer(0, sizeof(*header));
95
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000096 /*
97 * load parameter image
98 * load to temp position since nand_spl_load_image reads
99 * a whole block which is typically larger than
Marek Vasut6d016762012-03-31 07:47:17 +0000100 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000101 * following sections like BSS
102 */
103 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
104 CONFIG_CMD_SPL_WRITE_SIZE,
Simon Glass72cc5382022-10-20 18:22:39 -0600105 (void *)CONFIG_TEXT_BASE);
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000106 /* copy to destintion */
Simon Glass259cdb42023-09-26 08:14:17 -0600107 for (dst = (int *)CONFIG_SPL_PAYLOAD_ARGS_ADDR,
108 src = (int *)CONFIG_TEXT_BASE;
109 src < (int *)(CONFIG_TEXT_BASE +
110 CONFIG_CMD_SPL_WRITE_SIZE);
111 src++, dst++) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000112 writel(readl(src), dst);
113 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400114
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000115 /* load linux */
116 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +0900117 sizeof(*header), (void *)header);
Pali Rohárdda8f882022-01-14 14:31:38 +0100118 err = spl_parse_image_header(spl_image, bootdev, header);
Marek Vasut02266e22016-04-29 00:44:54 +0200119 if (err)
120 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000121 if (header->ih_os == IH_OS_LINUX) {
122 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200123 err = nand_spl_load_image(
124 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glassee306792016-09-24 18:20:13 -0600125 spl_image->size,
126 (void *)spl_image->load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000127 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200128 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000129 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -0700130 puts("The Expected Linux image was not "
131 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000132 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -0700133 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000134 }
135 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000136#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400137#ifdef CONFIG_NAND_ENV_DST
Sean Anderson1c398062023-11-08 11:48:51 -0500138 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400139#ifdef CONFIG_ENV_OFFSET_REDUND
Sean Anderson1c398062023-11-08 11:48:51 -0500140 spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400141#endif
142#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000143 /* Load u-boot */
Sean Anderson1c398062023-11-08 11:48:51 -0500144 err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page());
Boris Brezillon4f238e72016-06-06 10:16:58 +0200145#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
146#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
147 if (err)
Pali Rohárdda8f882022-01-14 14:31:38 +0100148 err = spl_nand_load_element(spl_image, bootdev,
Sean Anderson1c398062023-11-08 11:48:51 -0500149 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND);
Boris Brezillon4f238e72016-06-06 10:16:58 +0200150#endif
151#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400152 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200153 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400154}
Heiko Schochercf000272014-10-31 08:31:00 +0100155#endif
Simon Glass6cf82c92016-09-24 18:20:02 -0600156/* Use priorty 1 so that Ubi can override this */
Simon Glass4fc1f252016-11-30 15:30:50 -0700157SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);