blob: e2bcefb111e1f94bfc78b54a86cd09c19ff546ff [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 */
6#include <common.h>
Tom Rini9ef51bd2012-08-14 14:33:02 -07007#include <config.h>
Tom Rini28591df2012-08-13 12:03:19 -07008#include <spl.h>
Simon Schwarz6c3ddde2012-03-15 04:01:36 +00009#include <asm/io.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040010#include <nand.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090011#include <linux/libfdt_env.h>
Lokesh Vutla12c49222016-05-24 10:34:42 +053012#include <fdt.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040013
Heiko Schochercf000272014-10-31 08:31:00 +010014#if defined(CONFIG_SPL_NAND_RAW_ONLY)
Michael Trimarchi93e88022018-07-04 15:53:37 +020015static int spl_nand_load_image(struct spl_image_info *spl_image,
Simon Glassee306792016-09-24 18:20:13 -060016 struct spl_boot_device *bootdev)
Simon Schwarze21d2d82011-09-14 15:33:34 -040017{
Heiko Schochercf000272014-10-31 08:31:00 +010018 nand_init();
19
Stefan Roese438b6cd2019-03-26 13:04:00 +010020 printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n",
21 CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
22 CONFIG_SYS_NAND_U_BOOT_DST);
23
Heiko Schochercf000272014-10-31 08:31:00 +010024 nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
25 CONFIG_SYS_NAND_U_BOOT_SIZE,
26 (void *)CONFIG_SYS_NAND_U_BOOT_DST);
Simon Glassee306792016-09-24 18:20:13 -060027 spl_set_header_raw_uboot(spl_image);
Heiko Schochercf000272014-10-31 08:31:00 +010028 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020029
30 return 0;
Heiko Schochercf000272014-10-31 08:31:00 +010031}
32#else
Lokesh Vutla12c49222016-05-24 10:34:42 +053033
34static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs,
35 ulong size, void *dst)
36{
37 int ret;
38
39 ret = nand_spl_load_image(offs, size, dst);
40 if (!ret)
41 return size;
42 else
43 return 0;
44}
45
Simon Glassee306792016-09-24 18:20:13 -060046static int spl_nand_load_element(struct spl_image_info *spl_image,
47 int offset, struct image_header *header)
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020048{
49 int err;
50
51 err = nand_spl_load_image(offset, sizeof(*header), (void *)header);
52 if (err)
53 return err;
54
Lokesh Vutla12c49222016-05-24 10:34:42 +053055 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
56 image_get_magic(header) == FDT_MAGIC) {
57 struct spl_load_info load;
Marek Vasut02266e22016-04-29 00:44:54 +020058
Lokesh Vutla12c49222016-05-24 10:34:42 +053059 debug("Found FIT\n");
60 load.dev = NULL;
61 load.priv = NULL;
62 load.filename = NULL;
63 load.bl_len = 1;
64 load.read = spl_nand_fit_read;
Simon Glass43a734f2016-09-24 18:20:16 -060065 return spl_load_simple_fit(spl_image, &load, offset, header);
Lokesh Vutla12c49222016-05-24 10:34:42 +053066 } else {
Simon Glassee306792016-09-24 18:20:13 -060067 err = spl_parse_image_header(spl_image, header);
Lokesh Vutla12c49222016-05-24 10:34:42 +053068 if (err)
69 return err;
Simon Glassee306792016-09-24 18:20:13 -060070 return nand_spl_load_image(offset, spl_image->size,
71 (void *)(ulong)spl_image->load_addr);
Lokesh Vutla12c49222016-05-24 10:34:42 +053072 }
Nikita Kiryanov08fb32e62015-11-08 17:11:42 +020073}
74
Simon Glassee306792016-09-24 18:20:13 -060075static int spl_nand_load_image(struct spl_image_info *spl_image,
76 struct spl_boot_device *bootdev)
Heiko Schochercf000272014-10-31 08:31:00 +010077{
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020078 int err;
Simon Schwarze21d2d82011-09-14 15:33:34 -040079 struct image_header *header;
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000080 int *src __attribute__((unused));
81 int *dst __attribute__((unused));
82
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
Marek Vasut1dda46f2018-08-14 11:27:02 +020090 header = spl_get_load_buffer(0, sizeof(*header));
91
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000092#ifdef CONFIG_SPL_OS_BOOT
Simon Schwarz305bc4c2012-03-15 04:01:38 +000093 if (!spl_start_uboot()) {
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000094 /*
95 * load parameter image
96 * load to temp position since nand_spl_load_image reads
97 * a whole block which is typically larger than
Marek Vasut6d016762012-03-31 07:47:17 +000098 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite
Simon Schwarz6c3ddde2012-03-15 04:01:36 +000099 * following sections like BSS
100 */
101 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS,
102 CONFIG_CMD_SPL_WRITE_SIZE,
103 (void *)CONFIG_SYS_TEXT_BASE);
104 /* copy to destintion */
105 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR,
106 src = (int *)CONFIG_SYS_TEXT_BASE;
107 src < (int *)(CONFIG_SYS_TEXT_BASE +
108 CONFIG_CMD_SPL_WRITE_SIZE);
109 src++, dst++) {
110 writel(readl(src), dst);
111 }
Simon Schwarze21d2d82011-09-14 15:33:34 -0400112
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000113 /* load linux */
114 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Masahiro Yamadaeac6cd62014-07-10 20:43:16 +0900115 sizeof(*header), (void *)header);
Simon Glassee306792016-09-24 18:20:13 -0600116 err = spl_parse_image_header(spl_image, header);
Marek Vasut02266e22016-04-29 00:44:54 +0200117 if (err)
118 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000119 if (header->ih_os == IH_OS_LINUX) {
120 /* happy - was a linux */
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200121 err = nand_spl_load_image(
122 CONFIG_SYS_NAND_SPL_KERNEL_OFFS,
Simon Glassee306792016-09-24 18:20:13 -0600123 spl_image->size,
124 (void *)spl_image->load_addr);
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000125 nand_deselect();
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200126 return err;
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000127 } else {
Tom Rini9ef51bd2012-08-14 14:33:02 -0700128 puts("The Expected Linux image was not "
129 "found. Please check your NAND "
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000130 "configuration.\n");
Tom Rini9ef51bd2012-08-14 14:33:02 -0700131 puts("Trying to start u-boot now...\n");
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000132 }
133 }
Simon Schwarz6c3ddde2012-03-15 04:01:36 +0000134#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400135#ifdef CONFIG_NAND_ENV_DST
Simon Glassee306792016-09-24 18:20:13 -0600136 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400137#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassee306792016-09-24 18:20:13 -0600138 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header);
Simon Schwarze21d2d82011-09-14 15:33:34 -0400139#endif
140#endif
Simon Schwarz305bc4c2012-03-15 04:01:38 +0000141 /* Load u-boot */
Simon Glassee306792016-09-24 18:20:13 -0600142 err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS,
143 header);
Boris Brezillon4f238e72016-06-06 10:16:58 +0200144#ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
145#if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND
146 if (err)
Simon Glassee306792016-09-24 18:20:13 -0600147 err = spl_nand_load_element(spl_image,
148 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND,
Boris Brezillon4f238e72016-06-06 10:16:58 +0200149 header);
150#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);