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> |
Sean Anderson | 952ed67 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 10 | #include <imx_container.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 12 | #include <spl.h> |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 13 | #include <spl_load.h> |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 14 | #include <asm/io.h> |
Sean Anderson | 6f5be66 | 2023-11-04 16:37:46 -0400 | [diff] [blame] | 15 | #include <mapmem.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 16 | #include <nand.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 17 | #include <linux/libfdt_env.h> |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 18 | #include <fdt.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 19 | |
Peng Fan | 59a6273 | 2019-09-23 10:18:43 +0800 | [diff] [blame] | 20 | uint32_t __weak spl_nand_get_uboot_raw_page(void) |
| 21 | { |
| 22 | return CONFIG_SYS_NAND_U_BOOT_OFFS; |
| 23 | } |
| 24 | |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 25 | #if defined(CONFIG_SPL_NAND_RAW_ONLY) |
Michael Trimarchi | 93e8802 | 2018-07-04 15:53:37 +0200 | [diff] [blame] | 26 | static int spl_nand_load_image(struct spl_image_info *spl_image, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 27 | struct spl_boot_device *bootdev) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 28 | { |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 29 | nand_init(); |
| 30 | |
Stefan Roese | 438b6cd | 2019-03-26 13:04:00 +0100 | [diff] [blame] | 31 | printf("Loading U-Boot from 0x%08x (size 0x%08x) to 0x%08x\n", |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 32 | CONFIG_SYS_NAND_U_BOOT_OFFS, CFG_SYS_NAND_U_BOOT_SIZE, |
| 33 | CFG_SYS_NAND_U_BOOT_DST); |
Stefan Roese | 438b6cd | 2019-03-26 13:04:00 +0100 | [diff] [blame] | 34 | |
Peng Fan | 59a6273 | 2019-09-23 10:18:43 +0800 | [diff] [blame] | 35 | nand_spl_load_image(spl_nand_get_uboot_raw_page(), |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 36 | CFG_SYS_NAND_U_BOOT_SIZE, |
Sean Anderson | 6f5be66 | 2023-11-04 16:37:46 -0400 | [diff] [blame] | 37 | map_sysmem(CFG_SYS_NAND_U_BOOT_DST, |
| 38 | CFG_SYS_NAND_U_BOOT_SIZE)); |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 39 | spl_set_header_raw_uboot(spl_image); |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 40 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 41 | |
| 42 | return 0; |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 43 | } |
| 44 | #else |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 45 | |
Sean Anderson | 06dce4c | 2023-11-08 11:48:44 -0500 | [diff] [blame] | 46 | __weak u32 nand_spl_adjust_offset(u32 sector, u32 offs) |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 47 | { |
Sean Anderson | 06dce4c | 2023-11-08 11:48:44 -0500 | [diff] [blame] | 48 | return offs; |
Lokesh Vutla | 12c4922 | 2016-05-24 10:34:42 +0530 | [diff] [blame] | 49 | } |
| 50 | |
Sean Anderson | 06dce4c | 2023-11-08 11:48:44 -0500 | [diff] [blame] | 51 | static ulong spl_nand_read(struct spl_load_info *load, ulong offs, ulong size, |
| 52 | void *dst) |
developer | 1e885e0 | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 53 | { |
| 54 | int err; |
Sean Anderson | 06dce4c | 2023-11-08 11:48:44 -0500 | [diff] [blame] | 55 | ulong sector; |
developer | 1e885e0 | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 56 | |
| 57 | debug("%s: offs %lx, size %lx, dst %p\n", |
| 58 | __func__, offs, size, dst); |
| 59 | |
Sean Anderson | 06dce4c | 2023-11-08 11:48:44 -0500 | [diff] [blame] | 60 | sector = *(int *)load->priv; |
| 61 | offs = sector + nand_spl_adjust_offset(sector, offs - sector); |
developer | 1e885e0 | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 62 | err = nand_spl_load_image(offs, size, dst); |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 63 | spl_set_bl_len(load, nand_page_size()); |
developer | 1e885e0 | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 64 | if (err) |
| 65 | return 0; |
| 66 | |
| 67 | return size; |
| 68 | } |
| 69 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 70 | static int spl_nand_load_element(struct spl_image_info *spl_image, |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 71 | struct spl_boot_device *bootdev, int offset) |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 72 | { |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 73 | struct spl_load_info load; |
developer | 1e885e0 | 2022-05-20 11:24:04 +0800 | [diff] [blame] | 74 | |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 75 | load.priv = &offset; |
| 76 | spl_set_bl_len(&load, 1); |
| 77 | load.read = spl_nand_read; |
| 78 | return spl_load(spl_image, bootdev, &load, 0, offset); |
Nikita Kiryanov | 08fb32e6 | 2015-11-08 17:11:42 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 81 | static int spl_nand_load_image(struct spl_image_info *spl_image, |
| 82 | struct spl_boot_device *bootdev) |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 83 | { |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 84 | int err; |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 85 | |
Ahmed Samir Khalil | b976eb2 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 86 | #ifdef CONFIG_SPL_NAND_SOFTECC |
| 87 | debug("spl: nand - using sw ecc\n"); |
| 88 | #else |
Tom Rini | 09ebf1a | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 89 | debug("spl: nand - using hw ecc\n"); |
Ahmed Samir Khalil | b976eb2 | 2016-03-25 13:13:17 +0100 | [diff] [blame] | 90 | #endif |
Tom Rini | 09ebf1a | 2012-08-13 14:11:06 -0700 | [diff] [blame] | 91 | nand_init(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 92 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 93 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 94 | if (!spl_start_uboot()) { |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 95 | int *src, *dst; |
| 96 | struct legacy_img_hdr *header = |
| 97 | spl_get_load_buffer(0, sizeof(*header)); |
| 98 | |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 99 | /* |
| 100 | * load parameter image |
| 101 | * load to temp position since nand_spl_load_image reads |
| 102 | * a whole block which is typically larger than |
Marek Vasut | 6d01676 | 2012-03-31 07:47:17 +0000 | [diff] [blame] | 103 | * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 104 | * following sections like BSS |
| 105 | */ |
| 106 | nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS, |
| 107 | CONFIG_CMD_SPL_WRITE_SIZE, |
Simon Glass | 72cc538 | 2022-10-20 18:22:39 -0600 | [diff] [blame] | 108 | (void *)CONFIG_TEXT_BASE); |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 109 | /* copy to destintion */ |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 110 | for (dst = (int *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, |
| 111 | src = (int *)CONFIG_TEXT_BASE; |
| 112 | src < (int *)(CONFIG_TEXT_BASE + |
| 113 | CONFIG_CMD_SPL_WRITE_SIZE); |
| 114 | src++, dst++) { |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 115 | writel(readl(src), dst); |
| 116 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 117 | |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 118 | /* load linux */ |
| 119 | nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Masahiro Yamada | eac6cd6 | 2014-07-10 20:43:16 +0900 | [diff] [blame] | 120 | sizeof(*header), (void *)header); |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 121 | err = spl_parse_image_header(spl_image, bootdev, header); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 122 | if (err) |
| 123 | return err; |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 124 | if (header->ih_os == IH_OS_LINUX) { |
| 125 | /* happy - was a linux */ |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 126 | err = nand_spl_load_image( |
| 127 | CONFIG_SYS_NAND_SPL_KERNEL_OFFS, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 128 | spl_image->size, |
| 129 | (void *)spl_image->load_addr); |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 130 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 131 | return err; |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 132 | } else { |
Tom Rini | 9ef51bd | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 133 | puts("The Expected Linux image was not " |
| 134 | "found. Please check your NAND " |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 135 | "configuration.\n"); |
Tom Rini | 9ef51bd | 2012-08-14 14:33:02 -0700 | [diff] [blame] | 136 | puts("Trying to start u-boot now...\n"); |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
Simon Schwarz | 6c3ddde | 2012-03-15 04:01:36 +0000 | [diff] [blame] | 139 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 140 | #ifdef CONFIG_NAND_ENV_DST |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 141 | spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 142 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 143 | spl_nand_load_element(spl_image, bootdev, CONFIG_ENV_OFFSET_REDUND); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 144 | #endif |
| 145 | #endif |
Simon Schwarz | 305bc4c | 2012-03-15 04:01:38 +0000 | [diff] [blame] | 146 | /* Load u-boot */ |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 147 | err = spl_nand_load_element(spl_image, bootdev, spl_nand_get_uboot_raw_page()); |
Boris Brezillon | 4f238e7 | 2016-06-06 10:16:58 +0200 | [diff] [blame] | 148 | #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND |
| 149 | #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND |
| 150 | if (err) |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 151 | err = spl_nand_load_element(spl_image, bootdev, |
Sean Anderson | 1c39806 | 2023-11-08 11:48:51 -0500 | [diff] [blame] | 152 | CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND); |
Boris Brezillon | 4f238e7 | 2016-06-06 10:16:58 +0200 | [diff] [blame] | 153 | #endif |
| 154 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 155 | nand_deselect(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 156 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 157 | } |
Heiko Schocher | cf00027 | 2014-10-31 08:31:00 +0100 | [diff] [blame] | 158 | #endif |
Simon Glass | 6cf82c9 | 2016-09-24 18:20:02 -0600 | [diff] [blame] | 159 | /* Use priorty 1 so that Ubi can override this */ |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 160 | SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image); |