Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | e21d2d8 | 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 | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
Tom Rini | 9ef51bd | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 7 | #include <config.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 8 | #include <fdt_support.h> |
| 9 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 11 | #include <spl.h> |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 12 | #include <asm/io.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 13 | #include <nand.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 14 | #include <linux/libfdt_env.h> |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 15 | #include <fdt.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 16 | |
Peng Fan | 59a6273 | 2019-09-23 10:18:43 +0800 | [diff] [blame] | 17 | uint32_t __weak spl_nand_get_uboot_raw_page(void) |
| 18 | { |
| 19 | return CONFIG_SYS_NAND_U_BOOT_OFFS; |
| 20 | } |
| 21 | |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 22 | #if defined(CONFIG_SPL_NAND_RAW_ONLY) |
Michael Trimarchi | 93e8802 | 2018-07-04 15:53:37 +0200 | [diff] [blame] | 23 | static int spl_nand_load_image(struct spl_image_info *spl_image, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 24 | struct spl_boot_device *bootdev) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 25 | { |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 26 | nand_init(); |
| 27 | |
Stefan Roese | 438b6cd | 2019-03-26 13:04:00 +0100 | [diff] [blame] | 28 | printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n", |
| 29 | CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE, |
| 30 | CONFIG_SYS_NAND_U_BOOT_DST); |
| 31 | |
Peng Fan | 59a6273 | 2019-09-23 10:18:43 +0800 | [diff] [blame] | 32 | nand_spl_load_image(spl_nand_get_uboot_raw_page(), |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 33 | CONFIG_SYS_NAND_U_BOOT_SIZE, |
| 34 | (void *)CONFIG_SYS_NAND_U_BOOT_DST); |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 35 | spl_set_header_raw_uboot(spl_image); |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 36 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 37 | |
| 38 | return 0; |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 39 | } |
| 40 | #else |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 41 | |
| 42 | static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs, |
| 43 | ulong size, void *dst) |
| 44 | { |
Dario Binacchi | fdcb979 | 2020-05-27 13:56:21 +0200 | [diff] [blame] | 45 | int err; |
Tim Harvey | 26205a3 | 2021-03-01 14:33:27 -0800 | [diff] [blame] | 46 | ulong sector; |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 47 | |
Dario Binacchi | a6d91ca | 2020-05-27 13:56:20 +0200 | [diff] [blame] | 48 | sector = *(int *)load->priv; |
Tim Harvey | a2c63f0 | 2021-03-01 14:33:28 -0800 | [diff] [blame] | 49 | offs *= load->bl_len; |
| 50 | size *= load->bl_len; |
Michael Trimarchi | 7396b7d | 2022-05-15 11:35:33 +0200 | [diff] [blame] | 51 | offs = sector + nand_spl_adjust_offset(sector, offs - sector); |
Dario Binacchi | fdcb979 | 2020-05-27 13:56:21 +0200 | [diff] [blame] | 52 | err = nand_spl_load_image(offs, size, dst); |
| 53 | if (err) |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 54 | return 0; |
Dario Binacchi | fdcb979 | 2020-05-27 13:56:21 +0200 | [diff] [blame] | 55 | |
Tim Harvey | a2c63f0 | 2021-03-01 14:33:28 -0800 | [diff] [blame] | 56 | return size / load->bl_len; |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 57 | } |
| 58 | |
developer | 1e885e0 | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 59 | static ulong spl_nand_legacy_read(struct spl_load_info *load, ulong offs, |
| 60 | ulong size, void *dst) |
| 61 | { |
| 62 | int err; |
| 63 | |
| 64 | debug("%s: offs %lx, size %lx, dst %p\n", |
| 65 | __func__, offs, size, dst); |
| 66 | |
| 67 | err = nand_spl_load_image(offs, size, dst); |
| 68 | if (err) |
| 69 | return 0; |
| 70 | |
| 71 | return size; |
| 72 | } |
| 73 | |
Tim Harvey | a2c63f0 | 2021-03-01 14:33:28 -0800 | [diff] [blame] | 74 | struct mtd_info * __weak nand_get_mtd(void) |
| 75 | { |
| 76 | return NULL; |
| 77 | } |
| 78 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 79 | static int spl_nand_load_element(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 80 | struct spl_boot_device *bootdev, |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 81 | int offset, struct legacy_img_hdr *header) |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 82 | { |
Tim Harvey | a2c63f0 | 2021-03-01 14:33:28 -0800 | [diff] [blame] | 83 | struct mtd_info *mtd = nand_get_mtd(); |
| 84 | int bl_len = mtd ? mtd->writesize : 1; |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 85 | int err; |
| 86 | |
| 87 | err = nand_spl_load_image(offset, sizeof(*header), (void *)header); |
| 88 | if (err) |
| 89 | return err; |
| 90 | |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 91 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 92 | image_get_magic(header) == FDT_MAGIC) { |
| 93 | struct spl_load_info load; |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 94 | |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 95 | debug("Found FIT\n"); |
| 96 | load.dev = NULL; |
Dario Binacchi | a6d91ca | 2020-05-27 13:56:20 +0200 | [diff] [blame] | 97 | load.priv = &offset; |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 98 | load.filename = NULL; |
Tim Harvey | a2c63f0 | 2021-03-01 14:33:28 -0800 | [diff] [blame] | 99 | load.bl_len = bl_len; |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 100 | load.read = spl_nand_fit_read; |
Tim Harvey | a2c63f0 | 2021-03-01 14:33:28 -0800 | [diff] [blame] | 101 | return spl_load_simple_fit(spl_image, &load, offset / bl_len, header); |
Peng Fan | 519ce8d | 2019-09-23 10:18:46 +0800 | [diff] [blame] | 102 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) { |
| 103 | struct spl_load_info load; |
| 104 | |
| 105 | load.dev = NULL; |
| 106 | load.priv = NULL; |
| 107 | load.filename = NULL; |
Tim Harvey | a2c63f0 | 2021-03-01 14:33:28 -0800 | [diff] [blame] | 108 | load.bl_len = bl_len; |
Peng Fan | 519ce8d | 2019-09-23 10:18:46 +0800 | [diff] [blame] | 109 | load.read = spl_nand_fit_read; |
Tim Harvey | a2c63f0 | 2021-03-01 14:33:28 -0800 | [diff] [blame] | 110 | return spl_load_imx_container(spl_image, &load, offset / bl_len); |
developer | 1e885e0 | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 111 | } else if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT) && |
| 112 | image_get_magic(header) == IH_MAGIC) { |
| 113 | struct spl_load_info load; |
| 114 | |
| 115 | debug("Found legacy image\n"); |
| 116 | load.dev = NULL; |
| 117 | load.priv = NULL; |
| 118 | load.filename = NULL; |
| 119 | load.bl_len = 1; |
| 120 | load.read = spl_nand_legacy_read; |
| 121 | |
| 122 | return spl_load_legacy_img(spl_image, bootdev, &load, offset); |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 123 | } else { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 124 | err = spl_parse_image_header(spl_image, bootdev, header); |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 125 | if (err) |
| 126 | return err; |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 127 | return nand_spl_load_image(offset, spl_image->size, |
| 128 | (void *)(ulong)spl_image->load_addr); |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 129 | } |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 132 | static int spl_nand_load_image(struct spl_image_info *spl_image, |
| 133 | struct spl_boot_device *bootdev) |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 134 | { |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 135 | int err; |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 136 | struct legacy_img_hdr *header; |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 137 | int *src __attribute__((unused)); |
| 138 | int *dst __attribute__((unused)); |
| 139 | |
Ahmed Samir Khalil | b976eb2 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 140 | #ifdef CONFIG_SPL_NAND_SOFTECC |
| 141 | debug("spl: nand - using sw ecc\n"); |
| 142 | #else |
Tom Rini | 09ebf1a | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 143 | debug("spl: nand - using hw ecc\n"); |
Ahmed Samir Khalil | b976eb2 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 144 | #endif |
Tom Rini | 09ebf1a | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 145 | nand_init(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 146 | |
Marek Vasut | 1dda46f | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 147 | header = spl_get_load_buffer(0, sizeof(*header)); |
| 148 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 149 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 150 | if (!spl_start_uboot()) { |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 151 | /* |
| 152 | * load parameter image |
| 153 | * load to temp position since nand_spl_load_image reads |
| 154 | * a whole block which is typically larger than |
Marek Vasut | 6d01676 | 2012-03-31 07:47:17 +0000 | [diff] [blame] | 155 | * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 156 | * following sections like BSS |
| 157 | */ |
| 158 | nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS, |
| 159 | CONFIG_CMD_SPL_WRITE_SIZE, |
| 160 | (void *)CONFIG_SYS_TEXT_BASE); |
| 161 | /* copy to destintion */ |
| 162 | for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR, |
| 163 | src = (int *)CONFIG_SYS_TEXT_BASE; |
| 164 | src < (int *)(CONFIG_SYS_TEXT_BASE + |
| 165 | CONFIG_CMD_SPL_WRITE_SIZE); |
| 166 | src++, dst++) { |
| 167 | writel(readl(src), dst); |
| 168 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 169 | |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 170 | /* load linux */ |
| 171 | nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Masahiro Yamada | eac6cd6 | 2014-07-10 20:43:16 +0900 | [diff] [blame] | 172 | sizeof(*header), (void *)header); |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 173 | err = spl_parse_image_header(spl_image, bootdev, header); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 174 | if (err) |
| 175 | return err; |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 176 | if (header->ih_os == IH_OS_LINUX) { |
| 177 | /* happy - was a linux */ |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 178 | err = nand_spl_load_image( |
| 179 | CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 180 | spl_image->size, |
| 181 | (void *)spl_image->load_addr); |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 182 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 183 | return err; |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 184 | } else { |
Tom Rini | 9ef51bd | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 185 | puts("The Expected Linux image was not " |
| 186 | "found. Please check your NAND " |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 187 | "configuration.\n"); |
Tom Rini | 9ef51bd | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 188 | puts("Trying to start u-boot now...\n"); |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 191 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 192 | #ifdef CONFIG_NAND_ENV_DST |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 193 | spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET, header); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 194 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 195 | spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND, header); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 196 | #endif |
| 197 | #endif |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 198 | /* Load u-boot */ |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 199 | err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page(), |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 200 | header); |
Boris Brezillon | 4f238e7 | 2016-06-06 10:16:58 +0200 | [diff] [blame] | 201 | #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND |
| 202 | #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND |
| 203 | if (err) |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 204 | err = spl_nand_load_element(spl_image, bootdev, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 205 | CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND, |
Boris Brezillon | 4f238e7 | 2016-06-06 10:16:58 +0200 | [diff] [blame] | 206 | header); |
| 207 | #endif |
| 208 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 209 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 210 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 211 | } |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 212 | #endif |
Simon Glass | 6cf82c9 | 2016-09-24 18:20:02 -0600 | [diff] [blame] | 213 | /* Use priorty 1 so that Ubi can override this */ |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 214 | SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image); |