blob: 59f4a84a36ac9a39be89b9c412bcec12a56c50fe [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",
29 CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
30 CONFIG_SYS_NAND_U_BOOT_DST);
31
Peng Fan2c792292019-09-23 10:18:43 +080032 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Heiko Schocher0c3117b2014-10-31 08:31:00 +010033 CONFIG_SYS_NAND_U_BOOT_SIZE,
34 (void *)CONFIG_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#ifdef CONFIG_SYS_NAND_BLOCK_SIZE
47 ulong sector;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053048
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020049 sector = *(int *)load->priv;
50 offs = sector + nand_spl_adjust_offset(sector, offs - sector);
Tim Harveyaa0032f2021-03-01 14:33:28 -080051#else
52 offs *= load->bl_len;
53 size *= load->bl_len;
Tim Harvey39cb8502021-03-01 14:33:27 -080054#endif
Dario Binacchi84dd1902020-05-27 13:56:21 +020055 err = nand_spl_load_image(offs, size, dst);
56 if (err)
Lokesh Vutla8bd88772016-05-24 10:34:42 +053057 return 0;
Dario Binacchi84dd1902020-05-27 13:56:21 +020058
Tim Harveyaa0032f2021-03-01 14:33:28 -080059 return size / load->bl_len;
60}
61
62struct mtd_info * __weak nand_get_mtd(void)
63{
64 return NULL;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053065}
66
Simon Glass2a2ee2a2016-09-24 18:20:13 -060067static int spl_nand_load_element(struct spl_image_info *spl_image,
68 int offset, struct image_header *header)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020069{
Tim Harveyaa0032f2021-03-01 14:33:28 -080070 struct mtd_info *mtd = nand_get_mtd();
71 int bl_len = mtd ? mtd->writesize : 1;
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020072 int err;
73
74 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
75 if (err)
76 return err;
77
Lokesh Vutla8bd88772016-05-24 10:34:42 +053078 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
79 image_get_magic(header) == FDT_MAGIC) {
80 struct spl_load_info load;
Marek Vasut7e0f2262016-04-29 00:44:54 +020081
Lokesh Vutla8bd88772016-05-24 10:34:42 +053082 debug("Found FIT\n");
83 load.dev = NULL;
Dario Binacchi9f6a14c2020-05-27 13:56:20 +020084 load.priv = &offset;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053085 load.filename = NULL;
Tim Harveyaa0032f2021-03-01 14:33:28 -080086 load.bl_len = bl_len;
Lokesh Vutla8bd88772016-05-24 10:34:42 +053087 load.read = spl_nand_fit_read;
Tim Harveyaa0032f2021-03-01 14:33:28 -080088 return spl_load_simple_fit(spl_image, &load, offset / bl_len, header);
Peng Fan00e180c2019-09-23 10:18:46 +080089 } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) {
90 struct spl_load_info load;
91
92 load.dev = NULL;
93 load.priv = NULL;
94 load.filename = NULL;
Tim Harveyaa0032f2021-03-01 14:33:28 -080095 load.bl_len = bl_len;
Peng Fan00e180c2019-09-23 10:18:46 +080096 load.read = spl_nand_fit_read;
Tim Harveyaa0032f2021-03-01 14:33:28 -080097 return spl_load_imx_container(spl_image, &load, offset / bl_len);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053098 } else {
Simon Glass2a2ee2a2016-09-24 18:20:13 -060099 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530100 if (err)
101 return err;
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600102 return nand_spl_load_image(offset, spl_image->size,
103 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla8bd88772016-05-24 10:34:42 +0530104 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +0200105}
106
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600107static int spl_nand_load_image(struct spl_image_info *spl_image,
108 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400109{
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200110 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400111 struct image_header *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +0000112 int *src __attribute__((unused));
113 int *dst __attribute__((unused));
114
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100115#ifdef CONFIG_SPL_NAND_SOFTECC
116 debug("spl: nand - using sw ecc\n");
117#else
Tom Rini8082fda2012-08-13 14:11:06 -0700118 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +0100119#endif
Tom Rini8082fda2012-08-13 14:11:06 -0700120 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400121
Marek Vasut04ce5422018-08-14 11:27:02 +0200122 header = spl_get_load_buffer(0, sizeof(*header));
123
Simon Schwarzdf163a52012-03-15 04:01:36 +0000124#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz379c19a2012-03-15 04:01:38 +0000125 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-03-15 04:01:36 +0000126 /*
127 * load parameter image
128 * load to temp position since nand_spl_load_image reads
129 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +0000130 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +0000131 * following sections like BSS
132 */
133 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
134 CONFIG_CMD_SPL_WRITE_SIZE,
135 (void *)CONFIG_SYS_TEXT_BASE);
136 /* copy to destintion */
137 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
138 src = (int *)CONFIG_SYS_TEXT_BASE;
139 src < (int *)(CONFIG_SYS_TEXT_BASE +
140 CONFIG_CMD_SPL_WRITE_SIZE);
141 src++, dst++) {
142 writel(readl(src), dst);
143 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400144
Simon Schwarzdf163a52012-03-15 04:01:36 +0000145 /* load linux */
146 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900147 sizeof(*header), (void *)header);
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600148 err = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200149 if (err)
150 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000151 if (header->ih_os == IH_OS_LINUX) {
152 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200153 err = nand_spl_load_image(
154 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600155 spl_image->size,
156 (void *)spl_image->load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000157 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200158 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000159 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700160 puts("The Expected Linux image was not "
161 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000162 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700163 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000164 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000165 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000166#endif
167#ifdef CONFIG_NAND_ENV_DST
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600168 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000169#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600170 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000171#endif
172#endif
173 /* Load u-boot */
Peng Fan2c792292019-09-23 10:18:43 +0800174 err = spl_nand_load_element(spl_image, spl_nand_get_uboot_raw_page(),
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600175 header);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200176#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
177#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
178 if (err)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600179 err = spl_nand_load_element(spl_image,
180 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon80ef7002016-06-06 10:16:58 +0200181 header);
182#endif
183#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400184 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200185 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400186}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100187#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600188/* Use priorty 1 so that Ubi can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700189SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);