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