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