Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 |
| 4 | * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
Tom Rini | d97b4ce | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 7 | #include <config.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 8 | #include <fdt_support.h> |
| 9 | #include <image.h> |
Sean Anderson | ab12179 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 10 | #include <imx_container.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 12 | #include <spl.h> |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 13 | #include <asm/io.h> |
Sean Anderson | bd9573c | 2023-11-04 16:37:46 -0400 | [diff] [blame] | 14 | #include <mapmem.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 15 | #include <nand.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 16 | #include <linux/libfdt_env.h> |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 17 | #include <fdt.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 18 | |
Peng Fan | 2c79229 | 2019-09-23 10:18:43 +0800 | [diff] [blame] | 19 | uint32_t __weak spl_nand_get_uboot_raw_page(void) |
| 20 | { |
| 21 | return CONFIG_SYS_NAND_U_BOOT_OFFS; |
| 22 | } |
| 23 | |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 24 | #if defined(CONFIG_SPL_NAND_RAW_ONLY) |
Michael Trimarchi | 25dabd7 | 2018-07-04 15:53:37 +0200 | [diff] [blame] | 25 | static int spl_nand_load_image(struct spl_image_info *spl_image, |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 26 | struct spl_boot_device *bootdev) |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 27 | { |
| 28 | nand_init(); |
| 29 | |
Stefan Roese | 443b3ce | 2019-03-26 13:04:00 +0100 | [diff] [blame] | 30 | printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n", |
Tom Rini | 4e59094 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 31 | CONFIG_SYS_NAND_U_BOOT_OFFS, CFG_SYS_NAND_U_BOOT_SIZE, |
| 32 | CFG_SYS_NAND_U_BOOT_DST); |
Stefan Roese | 443b3ce | 2019-03-26 13:04:00 +0100 | [diff] [blame] | 33 | |
Peng Fan | 2c79229 | 2019-09-23 10:18:43 +0800 | [diff] [blame] | 34 | nand_spl_load_image(spl_nand_get_uboot_raw_page(), |
Tom Rini | 4e59094 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 35 | CFG_SYS_NAND_U_BOOT_SIZE, |
Sean Anderson | bd9573c | 2023-11-04 16:37:46 -0400 | [diff] [blame] | 36 | map_sysmem(CFG_SYS_NAND_U_BOOT_DST, |
| 37 | CFG_SYS_NAND_U_BOOT_SIZE)); |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 38 | spl_set_header_raw_uboot(spl_image); |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 39 | nand_deselect(); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 40 | |
| 41 | return 0; |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 42 | } |
| 43 | #else |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 44 | |
Sean Anderson | 0ddfa86 | 2023-11-08 11:48:44 -0500 | [diff] [blame^] | 45 | __weak u32 nand_spl_adjust_offset(u32 sector, u32 offs) |
| 46 | { |
| 47 | return offs; |
| 48 | } |
| 49 | |
| 50 | static ulong spl_nand_read(struct spl_load_info *load, ulong offs, ulong size, |
| 51 | void *dst) |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 52 | { |
Dario Binacchi | 84dd190 | 2020-05-27 13:56:21 +0200 | [diff] [blame] | 53 | int err; |
Tim Harvey | 39cb850 | 2021-03-01 14:33:27 -0800 | [diff] [blame] | 54 | ulong sector; |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 55 | |
Weijie Gao | 4620e8a | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 56 | debug("%s: offs %lx, size %lx, dst %p\n", |
| 57 | __func__, offs, size, dst); |
| 58 | |
Sean Anderson | 0ddfa86 | 2023-11-08 11:48:44 -0500 | [diff] [blame^] | 59 | sector = *(int *)load->priv; |
| 60 | offs = sector + nand_spl_adjust_offset(sector, offs - sector); |
Weijie Gao | 4620e8a | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 61 | err = nand_spl_load_image(offs, size, dst); |
| 62 | if (err) |
| 63 | return 0; |
| 64 | |
| 65 | return size; |
| 66 | } |
| 67 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 68 | static int spl_nand_load_element(struct spl_image_info *spl_image, |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 69 | struct spl_boot_device *bootdev, |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 70 | int offset, struct legacy_img_hdr *header) |
Nikita Kiryanov | 483ab3d | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 71 | { |
Sean Anderson | 38ef64e | 2023-11-04 16:37:44 -0400 | [diff] [blame] | 72 | int bl_len; |
Nikita Kiryanov | 483ab3d | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 73 | int err; |
| 74 | |
| 75 | err = nand_spl_load_image(offset, sizeof(*header), (void *)header); |
| 76 | if (err) |
| 77 | return err; |
| 78 | |
Sean Anderson | 38ef64e | 2023-11-04 16:37:44 -0400 | [diff] [blame] | 79 | bl_len = nand_page_size(); |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 80 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 81 | image_get_magic(header) == FDT_MAGIC) { |
| 82 | struct spl_load_info load; |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 83 | |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 84 | debug("Found FIT\n"); |
Dario Binacchi | 9f6a14c | 2020-05-27 13:56:20 +0200 | [diff] [blame] | 85 | load.priv = &offset; |
Sean Anderson | 5271e35 | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 86 | spl_set_bl_len(&load, bl_len); |
Sean Anderson | 0ddfa86 | 2023-11-08 11:48:44 -0500 | [diff] [blame^] | 87 | load.read = spl_nand_read; |
Sean Anderson | 73c40fc | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 88 | return spl_load_simple_fit(spl_image, &load, offset, header); |
Sean Anderson | ab12179 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 89 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) && |
| 90 | valid_container_hdr((void *)header)) { |
Peng Fan | 00e180c | 2019-09-23 10:18:46 +0800 | [diff] [blame] | 91 | struct spl_load_info load; |
| 92 | |
Sean Anderson | 23fe0c0 | 2023-11-04 16:37:39 -0400 | [diff] [blame] | 93 | load.priv = &offset; |
Sean Anderson | 5271e35 | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 94 | spl_set_bl_len(&load, bl_len); |
Sean Anderson | 0ddfa86 | 2023-11-08 11:48:44 -0500 | [diff] [blame^] | 95 | load.read = spl_nand_read; |
Sean Anderson | 73c40fc | 2023-11-08 11:48:40 -0500 | [diff] [blame] | 96 | return spl_load_imx_container(spl_image, &load, offset); |
Weijie Gao | 4620e8a | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 97 | } else if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT) && |
| 98 | image_get_magic(header) == IH_MAGIC) { |
| 99 | struct spl_load_info load; |
| 100 | |
| 101 | debug("Found legacy image\n"); |
Sean Anderson | 0ddfa86 | 2023-11-08 11:48:44 -0500 | [diff] [blame^] | 102 | load.priv = &offset; |
Sean Anderson | 5271e35 | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 103 | spl_set_bl_len(&load, IS_ENABLED(CONFIG_SPL_LZMA) ? bl_len : 1); |
Sean Anderson | 0ddfa86 | 2023-11-08 11:48:44 -0500 | [diff] [blame^] | 104 | load.read = spl_nand_read; |
Roger Quadros | 06377c5 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 105 | return spl_load_legacy_img(spl_image, bootdev, &load, offset, header); |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 106 | } else { |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 107 | err = spl_parse_image_header(spl_image, bootdev, header); |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 108 | if (err) |
| 109 | return err; |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 110 | return nand_spl_load_image(offset, spl_image->size, |
Sean Anderson | bd9573c | 2023-11-04 16:37:46 -0400 | [diff] [blame] | 111 | map_sysmem(spl_image->load_addr, |
| 112 | spl_image->size)); |
Lokesh Vutla | 8bd8877 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 113 | } |
Nikita Kiryanov | 483ab3d | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 116 | static int spl_nand_load_image(struct spl_image_info *spl_image, |
| 117 | struct spl_boot_device *bootdev) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 118 | { |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 119 | int err; |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 120 | struct legacy_img_hdr *header; |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 121 | int *src __attribute__((unused)); |
| 122 | int *dst __attribute__((unused)); |
| 123 | |
Ahmed Samir Khalil | 8122d21 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 124 | #ifdef CONFIG_SPL_NAND_SOFTECC |
| 125 | debug("spl: nand - using sw ecc\n"); |
| 126 | #else |
Tom Rini | 8082fda | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 127 | debug("spl: nand - using hw ecc\n"); |
Ahmed Samir Khalil | 8122d21 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 128 | #endif |
Tom Rini | 8082fda | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 129 | nand_init(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 130 | |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 131 | header = spl_get_load_buffer(0, sizeof(*header)); |
| 132 | |
Tom Rini | 7115007 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 133 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 134 | if (!spl_start_uboot()) { |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 135 | /* |
| 136 | * load parameter image |
| 137 | * load to temp position since nand_spl_load_image reads |
| 138 | * a whole block which is typically larger than |
Marek Vasut | b6e95fd | 2012-03-31 07:47:17 +0000 | [diff] [blame] | 139 | * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 140 | * following sections like BSS |
| 141 | */ |
| 142 | nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS, |
| 143 | CONFIG_CMD_SPL_WRITE_SIZE, |
Simon Glass | 9846390 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 144 | (void *)CONFIG_TEXT_BASE); |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 145 | /* copy to destintion */ |
Simon Glass | 9cbdc3a | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 146 | for (dst = (int *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, |
| 147 | src = (int *)CONFIG_TEXT_BASE; |
| 148 | src < (int *)(CONFIG_TEXT_BASE + |
| 149 | CONFIG_CMD_SPL_WRITE_SIZE); |
| 150 | src++, dst++) { |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 151 | writel(readl(src), dst); |
| 152 | } |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 153 | |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 154 | /* load linux */ |
| 155 | nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Masahiro Yamada | c13bb16 | 2014-07-10 20:43:16 +0900 | [diff] [blame] | 156 | sizeof(*header), (void *)header); |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 157 | err = spl_parse_image_header(spl_image, bootdev, header); |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 158 | if (err) |
| 159 | return err; |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 160 | if (header->ih_os == IH_OS_LINUX) { |
| 161 | /* happy - was a linux */ |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 162 | err = nand_spl_load_image( |
| 163 | CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 164 | spl_image->size, |
| 165 | (void *)spl_image->load_addr); |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 166 | nand_deselect(); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 167 | return err; |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 168 | } else { |
Tom Rini | d97b4ce | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 169 | puts("The Expected Linux image was not " |
| 170 | "found. Please check your NAND " |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 171 | "configuration.\n"); |
Tom Rini | d97b4ce | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 172 | puts("Trying to start u-boot now...\n"); |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 173 | } |
Simon Schwarz | df163a5 | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 174 | } |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 175 | #endif |
| 176 | #ifdef CONFIG_NAND_ENV_DST |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 177 | spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET, header); |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 178 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 179 | spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND, header); |
Simon Schwarz | 379c19a | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 180 | #endif |
| 181 | #endif |
| 182 | /* Load u-boot */ |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 183 | err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page(), |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 184 | header); |
Boris Brezillon | 80ef700 | 2016-06-06 10:16:58 +0200 | [diff] [blame] | 185 | #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND |
| 186 | #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND |
| 187 | if (err) |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 188 | err = spl_nand_load_element(spl_image, bootdev, |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 189 | CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND, |
Boris Brezillon | 80ef700 | 2016-06-06 10:16:58 +0200 | [diff] [blame] | 190 | header); |
| 191 | #endif |
| 192 | #endif |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 193 | nand_deselect(); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 194 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 195 | } |
Heiko Schocher | 0c3117b | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 196 | #endif |
Simon Glass | d5c2b11 | 2016-09-24 18:20:02 -0600 | [diff] [blame] | 197 | /* Use priorty 1 so that Ubi can override this */ |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 198 | SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image); |