Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 |
| 3 | * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
Tom Rini | 9ef51bd | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 8 | #include <config.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 9 | #include <spl.h> |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 10 | #include <asm/io.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 11 | #include <nand.h> |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 12 | #include <libfdt_env.h> |
| 13 | #include <fdt.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 14 | |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 15 | #if defined(CONFIG_SPL_NAND_RAW_ONLY) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 16 | int spl_nand_load_image(void) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 17 | { |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 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); |
| 23 | spl_set_header_raw_uboot(); |
| 24 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 25 | |
| 26 | return 0; |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 27 | } |
| 28 | #else |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 29 | |
| 30 | static 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 | |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 42 | static int spl_nand_load_element(int offset, struct image_header *header) |
| 43 | { |
| 44 | int err; |
| 45 | |
| 46 | err = nand_spl_load_image(offset, sizeof(*header), (void *)header); |
| 47 | if (err) |
| 48 | return err; |
| 49 | |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 50 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 51 | image_get_magic(header) == FDT_MAGIC) { |
| 52 | struct spl_load_info load; |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 53 | |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 54 | debug("Found FIT\n"); |
| 55 | load.dev = NULL; |
| 56 | load.priv = NULL; |
| 57 | load.filename = NULL; |
| 58 | load.bl_len = 1; |
| 59 | load.read = spl_nand_fit_read; |
| 60 | return spl_load_simple_fit(&load, offset, header); |
| 61 | } else { |
| 62 | err = spl_parse_image_header(header); |
| 63 | if (err) |
| 64 | return err; |
| 65 | return nand_spl_load_image(offset, spl_image.size, |
| 66 | (void *)(ulong)spl_image.load_addr); |
| 67 | } |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 70 | int spl_nand_load_image(void) |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 71 | { |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 72 | int err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 73 | struct image_header *header; |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 74 | int *src __attribute__((unused)); |
| 75 | int *dst __attribute__((unused)); |
| 76 | |
Ahmed Samir Khalil | b976eb2 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 77 | #ifdef CONFIG_SPL_NAND_SOFTECC |
| 78 | debug("spl: nand - using sw ecc\n"); |
| 79 | #else |
Tom Rini | 09ebf1a | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 80 | debug("spl: nand - using hw ecc\n"); |
Ahmed Samir Khalil | b976eb2 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 81 | #endif |
Tom Rini | 09ebf1a | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 82 | nand_init(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 83 | |
| 84 | /*use CONFIG_SYS_TEXT_BASE as temporary storage area */ |
| 85 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE); |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 86 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 87 | if (!spl_start_uboot()) { |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 88 | /* |
| 89 | * load parameter image |
| 90 | * load to temp position since nand_spl_load_image reads |
| 91 | * a whole block which is typically larger than |
Marek Vasut | 6d01676 | 2012-03-31 07:47:17 +0000 | [diff] [blame] | 92 | * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 93 | * following sections like BSS |
| 94 | */ |
| 95 | nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS, |
| 96 | CONFIG_CMD_SPL_WRITE_SIZE, |
| 97 | (void *)CONFIG_SYS_TEXT_BASE); |
| 98 | /* copy to destintion */ |
| 99 | for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR, |
| 100 | src = (int *)CONFIG_SYS_TEXT_BASE; |
| 101 | src < (int *)(CONFIG_SYS_TEXT_BASE + |
| 102 | CONFIG_CMD_SPL_WRITE_SIZE); |
| 103 | src++, dst++) { |
| 104 | writel(readl(src), dst); |
| 105 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 106 | |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 107 | /* load linux */ |
| 108 | nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Masahiro Yamada | eac6cd6 | 2014-07-10 20:43:16 +0900 | [diff] [blame] | 109 | sizeof(*header), (void *)header); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 110 | err = spl_parse_image_header(header); |
| 111 | if (err) |
| 112 | return err; |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 113 | if (header->ih_os == IH_OS_LINUX) { |
| 114 | /* happy - was a linux */ |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 115 | err = nand_spl_load_image( |
| 116 | CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
| 117 | spl_image.size, |
| 118 | (void *)spl_image.load_addr); |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 119 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 120 | return err; |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 121 | } else { |
Tom Rini | 9ef51bd | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 122 | puts("The Expected Linux image was not " |
| 123 | "found. Please check your NAND " |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 124 | "configuration.\n"); |
Tom Rini | 9ef51bd | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 125 | puts("Trying to start u-boot now...\n"); |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 128 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 129 | #ifdef CONFIG_NAND_ENV_DST |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 130 | spl_nand_load_element(CONFIG_ENV_OFFSET, header); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 131 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 132 | spl_nand_load_element(CONFIG_ENV_OFFSET_REDUND, header); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 133 | #endif |
| 134 | #endif |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 135 | /* Load u-boot */ |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 136 | err = spl_nand_load_element(CONFIG_SYS_NAND_U_BOOT_OFFS, header); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 137 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 138 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 139 | } |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 140 | #endif |