blob: d1abda6e4ce1f555defbf3463bcda9b767b13d00 [file] [log] [blame]
Simon Schwarze21d2d82011-09-14 15:33:34 -04001/*
2 * Copyright (C) 2011
3 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarze21d2d82011-09-14 15:33:34 -04006 */
7#include <common.h>
Tom Rini9ef51bd2012-08-14 14:33:02 -07008#include <config.h>
Tom Rini28591df2012-08-13 12:03:19 -07009#include <spl.h>
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000010#include <asm/io.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040011#include <nand.h>
Lokesh Vutla12c49222016-05-24 10:34:42 +053012#include <libfdt_env.h>
13#include <fdt.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040014
Heiko Schochercf000272014-10-31 08:31:00 +010015#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Simon Glassee306792016-09-24 18:20:13 -060016int spl_nand_load_image(struct spl_image_info *spl_image,
17 struct spl_boot_device *bootdev)
Simon Schwarze21d2d82011-09-14 15:33:34 -040018{
Heiko Schochercf000272014-10-31 08:31:00 +010019 nand_init();
20
21 nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
22 CONFIG_SYS_NAND_U_BOOT_SIZE,
23 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
Simon Glassee306792016-09-24 18:20:13 -060024 spl_set_header_raw_uboot(spl_image);
Heiko Schochercf000272014-10-31 08:31:00 +010025 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020026
27 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010028}
29#else
Lokesh Vutla12c49222016-05-24 10:34:42 +053030
31static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
32 ulong size, void *dst)
33{
34 int ret;
35
36 ret = nand_spl_load_image(offs, size, dst);
37 if (!ret)
38 return size;
39 else
40 return 0;
41}
42
Simon Glassee306792016-09-24 18:20:13 -060043static int spl_nand_load_element(struct spl_image_info *spl_image,
44 int offset, struct image_header *header)
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020045{
46 int err;
47
48 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
49 if (err)
50 return err;
51
Lokesh Vutla12c49222016-05-24 10:34:42 +053052 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
53 image_get_magic(header) == FDT_MAGIC) {
54 struct spl_load_info load;
Marek Vasut02266e22016-04-29 00:44:54 +020055
Lokesh Vutla12c49222016-05-24 10:34:42 +053056 debug("Found FIT\n");
57 load.dev = NULL;
58 load.priv = NULL;
59 load.filename = NULL;
60 load.bl_len = 1;
61 load.read = spl_nand_fit_read;
Simon Glass43a734f2016-09-24 18:20:16 -060062 return spl_load_simple_fit(spl_image, &load, offset, header);
Lokesh Vutla12c49222016-05-24 10:34:42 +053063 } else {
Simon Glassee306792016-09-24 18:20:13 -060064 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla12c49222016-05-24 10:34:42 +053065 if (err)
66 return err;
Simon Glassee306792016-09-24 18:20:13 -060067 return nand_spl_load_image(offset, spl_image->size,
68 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla12c49222016-05-24 10:34:42 +053069 }
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020070}
71
Simon Glassee306792016-09-24 18:20:13 -060072static int spl_nand_load_image(struct spl_image_info *spl_image,
73 struct spl_boot_device *bootdev)
Heiko Schochercf000272014-10-31 08:31:00 +010074{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020075 int err;
Simon Schwarze21d2d82011-09-14 15:33:34 -040076 struct image_header *header;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000077 int *src __attribute__((unused));
78 int *dst __attribute__((unused));
79
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010080#ifdef CONFIG_SPL_NAND_SOFTECC
81 debug("spl: nand - using sw ecc\n");
82#else
Tom Rini09ebf1a2012-08-13 14:11:06 -070083 debug("spl: nand - using hw ecc\n");
Ahmed Samir Khalilb976eb22016-03-25 13:13:17 +010084#endif
Tom Rini09ebf1a2012-08-13 14:11:06 -070085 nand_init();
Simon Schwarze21d2d82011-09-14 15:33:34 -040086
87 /*use CONFIG_SYS_TEXT_BASE as temporary storage area */
88 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE);
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000089#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz305bc4c2012-03-15 04:01:38 +000090 if (!spl_start_uboot()) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000091 /*
92 * load parameter image
93 * load to temp position since nand_spl_load_image reads
94 * a whole block which is typically larger than
Marek Vasut6d016762012-03-31 07:47:17 +000095 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000096 * following sections like BSS
97 */
98 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
99 CONFIG_CMD_SPL_WRITE_SIZE,
100 (void *)CONFIG_SYS_TEXT_BASE);
101 /* copy to destintion */
102 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
103 src = (int *)CONFIG_SYS_TEXT_BASE;
104 src < (int *)(CONFIG_SYS_TEXT_BASE +
105 CONFIG_CMD_SPL_WRITE_SIZE);
106 src++, dst++) {
107 writel(readl(src), dst);
108 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400109
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000110 /* load linux */
111 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +0900112 sizeof(*header), (void *)header);
Simon Glassee306792016-09-24 18:20:13 -0600113 err = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +0200114 if (err)
115 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000116 if (header->ih_os == IH_OS_LINUX) {
117 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200118 err = nand_spl_load_image(
119 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glassee306792016-09-24 18:20:13 -0600120 spl_image->size,
121 (void *)spl_image->load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000122 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200123 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000124 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -0700125 puts("The Expected Linux image was not "
126 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000127 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -0700128 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000129 }
130 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000131#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400132#ifdef CONFIG_NAND_ENV_DST
Simon Glassee306792016-09-24 18:20:13 -0600133 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400134#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassee306792016-09-24 18:20:13 -0600135 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400136#endif
137#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000138 /* Load u-boot */
Simon Glassee306792016-09-24 18:20:13 -0600139 err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS,
140 header);
Boris Brezillon4f238e72016-06-06 10:16:58 +0200141#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
142#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
143 if (err)
Simon Glassee306792016-09-24 18:20:13 -0600144 err = spl_nand_load_element(spl_image,
145 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon4f238e72016-06-06 10:16:58 +0200146 header);
147#endif
148#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400149 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200150 return err;
Simon Schwarze21d2d82011-09-14 15:33:34 -0400151}
Heiko Schochercf000272014-10-31 08:31:00 +0100152#endif
Simon Glass6cf82c92016-09-24 18:20:02 -0600153/* Use priorty 1 so that Ubi can override this */
154SPL_LOAD_IMAGE_METHOD(1, BOOT_DEVICE_NAND, spl_nand_load_image);