blob: 6eb190f1ea540da99fc2a6db9c3bedf92aadf4c8 [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
Heiko Schocher0c3117b2014-10-31 08:31:00 +010014#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi25dabd72018-07-04 15:53:37 +020015static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glass2a2ee2a2016-09-24 18:20:13 -060016 struct spl_boot_device *bootdev)
Heiko Schocher0c3117b2014-10-31 08:31:00 +010017{
18 nand_init();
19
20 nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
21 CONFIG_SYS_NAND_U_BOOT_SIZE,
22 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
Simon Glass2a2ee2a2016-09-24 18:20:13 -060023 spl_set_header_raw_uboot(spl_image);
Heiko Schocher0c3117b2014-10-31 08:31:00 +010024 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +020025
26 return 0;
Heiko Schocher0c3117b2014-10-31 08:31:00 +010027}
28#else
Lokesh Vutla8bd88772016-05-24 10:34:42 +053029
30static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
31 ulong size, void *dst)
32{
33 int ret;
34
35 ret = nand_spl_load_image(offs, size, dst);
36 if (!ret)
37 return size;
38 else
39 return 0;
40}
41
Simon Glass2a2ee2a2016-09-24 18:20:13 -060042static int spl_nand_load_element(struct spl_image_info *spl_image,
43 int offset, struct image_header *header)
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020044{
45 int err;
46
47 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
48 if (err)
49 return err;
50
Lokesh Vutla8bd88772016-05-24 10:34:42 +053051 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
52 image_get_magic(header) == FDT_MAGIC) {
53 struct spl_load_info load;
Marek Vasut7e0f2262016-04-29 00:44:54 +020054
Lokesh Vutla8bd88772016-05-24 10:34:42 +053055 debug("Found FIT\n");
56 load.dev = NULL;
57 load.priv = NULL;
58 load.filename = NULL;
59 load.bl_len = 1;
60 load.read = spl_nand_fit_read;
Simon Glassf4d7d852016-09-24 18:20:16 -060061 return spl_load_simple_fit(spl_image, &load, offset, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053062 } else {
Simon Glass2a2ee2a2016-09-24 18:20:13 -060063 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053064 if (err)
65 return err;
Simon Glass2a2ee2a2016-09-24 18:20:13 -060066 return nand_spl_load_image(offset, spl_image->size,
67 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla8bd88772016-05-24 10:34:42 +053068 }
Nikita Kiryanov483ab3d2015-11-08 17:11:42 +020069}
70
Simon Glass2a2ee2a2016-09-24 18:20:13 -060071static int spl_nand_load_image(struct spl_image_info *spl_image,
72 struct spl_boot_device *bootdev)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040073{
Nikita Kiryanov36afd452015-11-08 17:11:49 +020074 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040075 struct image_header *header;
Simon Schwarzdf163a52012-03-15 04:01:36 +000076 int *src __attribute__((unused));
77 int *dst __attribute__((unused));
78
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010079#ifdef CONFIG_SPL_NAND_SOFTECC
80 debug("spl: nand - using sw ecc\n");
81#else
Tom Rini8082fda2012-08-13 14:11:06 -070082 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalil8122d212016-03-25 13:13:17 +010083#endif
Tom Rini8082fda2012-08-13 14:11:06 -070084 nand_init();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040085
Marek Vasut04ce5422018-08-14 11:27:02 +020086 header = spl_get_load_buffer(0, sizeof(*header));
87
Simon Schwarzdf163a52012-03-15 04:01:36 +000088#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz379c19a2012-03-15 04:01:38 +000089 if (!spl_start_uboot()) {
Simon Schwarzdf163a52012-03-15 04:01:36 +000090 /*
91 * load parameter image
92 * load to temp position since nand_spl_load_image reads
93 * a whole block which is typically larger than
Marek Vasutb6e95fd2012-03-31 07:47:17 +000094 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarzdf163a52012-03-15 04:01:36 +000095 * following sections like BSS
96 */
97 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
98 CONFIG_CMD_SPL_WRITE_SIZE,
99 (void *)CONFIG_SYS_TEXT_BASE);
100 /* copy to destintion */
101 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
102 src = (int *)CONFIG_SYS_TEXT_BASE;
103 src < (int *)(CONFIG_SYS_TEXT_BASE +
104 CONFIG_CMD_SPL_WRITE_SIZE);
105 src++, dst++) {
106 writel(readl(src), dst);
107 }
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400108
Simon Schwarzdf163a52012-03-15 04:01:36 +0000109 /* load linux */
110 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadac13bb162014-07-10 20:43:16 +0900111 sizeof(*header), (void *)header);
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600112 err = spl_parse_image_header(spl_image, header);
Marek Vasut7e0f2262016-04-29 00:44:54 +0200113 if (err)
114 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000115 if (header->ih_os == IH_OS_LINUX) {
116 /* happy - was a linux */
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200117 err = nand_spl_load_image(
118 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600119 spl_image->size,
120 (void *)spl_image->load_addr);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000121 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200122 return err;
Simon Schwarz379c19a2012-03-15 04:01:38 +0000123 } else {
Tom Rinid97b4ce2012-08-14 14:33:02 -0700124 puts("The Expected Linux image was not "
125 "found. Please check your NAND "
Simon Schwarz379c19a2012-03-15 04:01:38 +0000126 "configuration.\n");
Tom Rinid97b4ce2012-08-14 14:33:02 -0700127 puts("Trying to start u-boot now...\n");
Simon Schwarz379c19a2012-03-15 04:01:38 +0000128 }
Simon Schwarzdf163a52012-03-15 04:01:36 +0000129 }
Simon Schwarz379c19a2012-03-15 04:01:38 +0000130#endif
131#ifdef CONFIG_NAND_ENV_DST
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600132 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000133#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600134 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarz379c19a2012-03-15 04:01:38 +0000135#endif
136#endif
137 /* Load u-boot */
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600138 err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS,
139 header);
Boris Brezillon80ef7002016-06-06 10:16:58 +0200140#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
141#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
142 if (err)
Simon Glass2a2ee2a2016-09-24 18:20:13 -0600143 err = spl_nand_load_element(spl_image,
144 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon80ef7002016-06-06 10:16:58 +0200145 header);
146#endif
147#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400148 nand_deselect();
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200149 return err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400150}
Heiko Schocher0c3117b2014-10-31 08:31:00 +0100151#endif
Simon Glassd5c2b112016-09-24 18:20:02 -0600152/* Use priorty 1 so that Ubi can override this */
Simon Glassebc4ef62016-11-30 15:30:50 -0700153SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image);