Simon Schwarz | 9ea5c6e | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
Tom Rini | d97b4ce | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 8 | #include <config.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 9 | #include <spl.h> |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 10 | #include <asm/io.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 11 | #include <nand.h> |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 12 | #include <libfdt_env.h> |
| 13 | #include <fdt.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 14 | |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 15 | #if defined(CONFIG_SPL_NAND_RAW_ONLY) |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 16 | int spl_nand_load_image(struct spl_image_info *spl_image, |
| 17 | struct spl_boot_device *bootdev) |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 18 | { |
| 19 | 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 Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 24 | spl_set_header_raw_uboot(spl_image); |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 25 | nand_deselect(); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 26 | |
| 27 | return 0; |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 28 | } |
| 29 | #else |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 30 | |
| 31 | static 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 Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 43 | static int spl_nand_load_element(struct spl_image_info *spl_image, |
| 44 | int offset, struct image_header *header) |
Nikita Kiryanov | 483ab3d | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 45 | { |
| 46 | int err; |
| 47 | |
| 48 | err = nand_spl_load_image(offset, sizeof(*header), (void *)header); |
| 49 | if (err) |
| 50 | return err; |
| 51 | |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 52 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 53 | image_get_magic(header) == FDT_MAGIC) { |
| 54 | struct spl_load_info load; |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 55 | |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 56 | 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 Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame^] | 62 | return spl_load_simple_fit(spl_image, &load, offset, header); |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 63 | } else { |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 64 | err = spl_parse_image_header(spl_image, header); |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 65 | if (err) |
| 66 | return err; |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 67 | return nand_spl_load_image(offset, spl_image->size, |
| 68 | (void *)(ulong)spl_image->load_addr); |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 69 | } |
Nikita Kiryanov | 483ab3d | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 72 | static int spl_nand_load_image(struct spl_image_info *spl_image, |
| 73 | struct spl_boot_device *bootdev) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 74 | { |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 75 | int err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 76 | struct image_header *header; |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 77 | int *src __attribute__((unused)); |
| 78 | int *dst __attribute__((unused)); |
| 79 | |
Ahmed Samir Khalil | 8122d21 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 80 | #ifdef CONFIG_SPL_NAND_SOFTECC |
| 81 | debug("spl: nand - using sw ecc\n"); |
| 82 | #else |
Tom Rini | 8082fda | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 83 | debug("spl: nand - using hw ecc\n"); |
Ahmed Samir Khalil | 8122d21 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 84 | #endif |
Tom Rini | 8082fda | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 85 | nand_init(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 86 | |
| 87 | /*use CONFIG_SYS_TEXT_BASE as temporary storage area */ |
| 88 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE); |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 89 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 90 | if (!spl_start_uboot()) { |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 91 | /* |
| 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 Vasut | b6e95fd | 2012-03-31 07:47:17 +0000 | [diff] [blame] | 95 | * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 96 | * 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 Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 109 | |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 110 | /* load linux */ |
| 111 | nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Masahiro Yamada | c13bb16 | 2014-07-10 20:43:16 +0900 | [diff] [blame] | 112 | sizeof(*header), (void *)header); |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 113 | err = spl_parse_image_header(spl_image, header); |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 114 | if (err) |
| 115 | return err; |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 116 | if (header->ih_os == IH_OS_LINUX) { |
| 117 | /* happy - was a linux */ |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 118 | err = nand_spl_load_image( |
| 119 | CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 120 | spl_image->size, |
| 121 | (void *)spl_image->load_addr); |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 122 | nand_deselect(); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 123 | return err; |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 124 | } else { |
Tom Rini | d97b4ce | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 125 | puts("The Expected Linux image was not " |
| 126 | "found. Please check your NAND " |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 127 | "configuration.\n"); |
Tom Rini | d97b4ce | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 128 | puts("Trying to start u-boot now...\n"); |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 129 | } |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 130 | } |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 131 | #endif |
| 132 | #ifdef CONFIG_NAND_ENV_DST |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 133 | spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header); |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 134 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 135 | spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header); |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 136 | #endif |
| 137 | #endif |
| 138 | /* Load u-boot */ |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 139 | err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS, |
| 140 | header); |
Boris Brezillon | 80ef700 | 2016-06-06 10:16:58 +0200 | [diff] [blame] | 141 | #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 Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 144 | err = spl_nand_load_element(spl_image, |
| 145 | CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND, |
Boris Brezillon | 80ef700 | 2016-06-06 10:16:58 +0200 | [diff] [blame] | 146 | header); |
| 147 | #endif |
| 148 | #endif |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 149 | nand_deselect(); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 150 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 151 | } |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 152 | #endif |
Simon Glass | d5c2b11 | 2016-09-24 18:20:02 -0600 | [diff] [blame] | 153 | /* Use priorty 1 so that Ubi can override this */ |
| 154 | SPL_LOAD_IMAGE_METHOD(1, BOOT_DEVICE_NAND, spl_nand_load_image); |