blob: d3185dc0170c4b2bf6807b0c94a7583388f71700 [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>
Tom Rini47f7bca2012-08-13 12:03:19 -07008#include <spl.h>
Simon Schwarzdf163a52012-03-15 04:01:36 +00009#include <asm/io.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040010#include <nand.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090011#include <linux/libfdt_env.h>
Lokesh Vutla8bd88772016-05-24 10:34:42 +053012#include <fdt.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040013
Peng Fan2c792292019-09-23 10:18:43 +080014uint32_t __weak spl_nand_get_uboot_raw_page(void)
15{
16 return CONFIG_SYS_NAND_U_BOOT_OFFS;
17}
18
Heiko Schocher0c3117b2014-10-31 08:31:00 +010019#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi25dabd72018-07-04 15:53:37 +020020static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060021 struct spl_boot_device *bootdev)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010022{
23 nand_init();
24
Stefan Roese443b3ce2019-03-26 13:04:00 +010025 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
26 CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
27 CONFIG_SYS_NAND_U_BOOT_DST);
28
Peng Fan2c792292019-09-23 10:18:43 +080029 nand_spl_load_image(spl_nand_get_uboot_raw_page(),
Heiko Schocher0c3117b2014-10-31 08:31:00 +010030 CONFIG_SYS_NAND_U_BOOT_SIZE,
31 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
Simon Glass2a2ee2a2016-09-24 18:20:13 -060032 spl_set_header_raw_uboot(spl_image);
Heiko Schocher0c3117b2014-10-31 08:31:00 +010033 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020034
35 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010036}
37#else
Lokesh Vutla8bd88772016-05-24 10:34:42 +053038
39static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
40 ulong size, void *dst)
41{
42 int ret;
43
44 ret = nand_spl_load_image(offs, size, dst);
45 if (!ret)
46 return size;
47 else
48 return 0;
49}
50
Simon Glass2a2ee2a2016-09-24 18:20:13 -060051static int spl_nand_load_element(struct spl_image_info *spl_image,
52 int offset, struct image_header *header)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020053{
54 int err;
55
56 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
57 if (err)
58 return err;
59
Lokesh Vutla8bd88772016-05-24 10:34:42 +053060 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
61 image_get_magic(header) == FDT_MAGIC) {
62 struct spl_load_info load;
Marek Vasut7e0f2262016-04-29 00:44:54 +020063
Lokesh Vutla8bd88772016-05-24 10:34:42 +053064 debug("Found FIT\n");
65 load.dev = NULL;
66 load.priv = NULL;
67 load.filename = NULL;
68 load.bl_len = 1;
69 load.read = spl_nand_fit_read;
Simon Glassf4d7d852016-09-24 18:20:16 -060070 return spl_load_simple_fit(spl_image, &load, offset, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053071 } else {
Simon Glass2a2ee2a2016-09-24 18:20:13 -060072 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053073 if (err)
74 return err;
Simon Glass2a2ee2a2016-09-24 18:20:13 -060075 return nand_spl_load_image(offset, spl_image->size,
76 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053077 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020078}
79
Simon Glass2a2ee2a2016-09-24 18:20:13 -060080static int spl_nand_load_image(struct spl_image_info *spl_image,
81 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040082{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020083 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040084 struct image_header *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +000085 int *src __attribute__((unused));
86 int *dst __attribute__((unused));
87
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010088#ifdef CONFIG_SPL_NAND_SOFTECC
89 debug("spl: nand - using sw ecc\n");
90#else
Tom Rini8082fda2012-08-13 14:11:06 -070091 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010092#endif
Tom Rini8082fda2012-08-13 14:11:06 -070093 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040094
Marek Vasut04ce5422018-08-14 11:27:02 +020095 header = spl_get_load_buffer(0, sizeof(*header));
96
Simon Schwarzdf163a52012-03-15 04:01:36 +000097#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz379c19a2012-03-15 04:01:38 +000098 if (!spl_start_uboot()) {
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,
108 (void *)CONFIG_SYS_TEXT_BASE);
109 /* copy to destintion */
110 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
111 src = (int *)CONFIG_SYS_TEXT_BASE;
112 src < (int *)(CONFIG_SYS_TEXT_BASE +
113 CONFIG_CMD_SPL_WRITE_SIZE);
114 src++, dst++) {
115 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);
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600121 err = spl_parse_image_header(spl_image, 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
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600141 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000142#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600143 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000144#endif
145#endif
146 /* Load u-boot */
Peng Fan2c792292019-09-23 10:18:43 +0800147 err = spl_nand_load_element(spl_image, spl_nand_get_uboot_raw_page(),
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600148 header);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200149#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
150#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
151 if (err)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600152 err = spl_nand_load_element(spl_image,
153 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon80ef7002016-06-06 10:16:58 +0200154 header);
155#endif
156#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400157 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200158 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400159}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100160#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600161/* Use priorty 1 so that Ubi can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700162SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);