1 /* 2 * Copyright (C) 2011 3 * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 #include <common.h> 8 #include <config.h> 9 #include <spl.h> 10 #include <spl_rkfw.h> 11 #include <asm/io.h> 12 #include <nand.h> 13 #include <linux/libfdt_env.h> 14 #include <fdt.h> 15 16 #if defined(CONFIG_SPL_NAND_RAW_ONLY) 17 int spl_nand_load_image(struct spl_image_info *spl_image, 18 struct spl_boot_device *bootdev) 19 { 20 nand_init(); 21 22 nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS, 23 CONFIG_SYS_NAND_U_BOOT_SIZE, 24 (void *)CONFIG_SYS_NAND_U_BOOT_DST); 25 spl_set_header_raw_uboot(spl_image); 26 nand_deselect(); 27 28 return 0; 29 } 30 #else 31 32 static ulong spl_nand_fit_read(struct spl_load_info *load, ulong offs, 33 ulong size, void *dst) 34 { 35 int ret; 36 37 ret = nand_spl_load_image(offs, size, dst); 38 if (!ret) 39 return size; 40 else 41 return 0; 42 } 43 44 #ifdef CONFIG_SPL_LOAD_RKFW 45 static ulong spl_nand_rkfw_read(struct spl_load_info *load, ulong offs, 46 ulong size, void *dst) 47 { 48 int ret; 49 50 ret = nand_spl_load_image(offs * 512, size * 512, dst); 51 if (!ret) 52 return size; 53 else 54 return 0; 55 } 56 #endif 57 58 static int spl_nand_load_element(struct spl_image_info *spl_image, 59 int offset, struct image_header *header) 60 { 61 int err; 62 63 #ifdef CONFIG_SPL_LOAD_RKFW 64 u32 trust_sectors = CONFIG_RKFW_TRUST_SECTOR; 65 u32 uboot_sectors = CONFIG_RKFW_U_BOOT_SECTOR; 66 u32 boot_sectors = CONFIG_RKFW_BOOT_SECTOR; 67 struct spl_load_info load; 68 int ret; 69 70 load.dev = NULL; 71 load.priv = NULL; 72 load.filename = NULL; 73 load.bl_len = 1; 74 load.read = spl_nand_rkfw_read; 75 76 ret = spl_load_rkfw_image(spl_image, &load, 77 trust_sectors, 78 uboot_sectors, 79 boot_sectors); 80 if (!ret || ret != -EAGAIN) 81 return ret; 82 #endif 83 84 err = nand_spl_load_image(offset, sizeof(*header), (void *)header); 85 if (err) 86 return err; 87 88 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && 89 image_get_magic(header) == FDT_MAGIC) { 90 struct spl_load_info load; 91 92 debug("Found FIT\n"); 93 load.dev = NULL; 94 load.priv = NULL; 95 load.filename = NULL; 96 load.bl_len = 1; 97 load.read = spl_nand_fit_read; 98 return spl_load_simple_fit(spl_image, &load, offset, header); 99 } else { 100 err = spl_parse_image_header(spl_image, header); 101 if (err) 102 return err; 103 return nand_spl_load_image(offset, spl_image->size, 104 (void *)(ulong)spl_image->load_addr); 105 } 106 } 107 108 static int spl_nand_load_image(struct spl_image_info *spl_image, 109 struct spl_boot_device *bootdev) 110 { 111 int err; 112 struct image_header *header; 113 int *src __attribute__((unused)); 114 int *dst __attribute__((unused)); 115 116 #ifdef CONFIG_SPL_NAND_SOFTECC 117 debug("spl: nand - using sw ecc\n"); 118 #else 119 debug("spl: nand - using hw ecc\n"); 120 #endif 121 nand_init(); 122 123 /*use CONFIG_SYS_TEXT_BASE as temporary storage area */ 124 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE); 125 #ifdef CONFIG_SPL_OS_BOOT 126 if (!spl_start_uboot()) { 127 /* 128 * load parameter image 129 * load to temp position since nand_spl_load_image reads 130 * a whole block which is typically larger than 131 * CONFIG_CMD_SPL_WRITE_SIZE therefore may overwrite 132 * following sections like BSS 133 */ 134 nand_spl_load_image(CONFIG_CMD_SPL_NAND_OFS, 135 CONFIG_CMD_SPL_WRITE_SIZE, 136 (void *)CONFIG_SYS_TEXT_BASE); 137 /* copy to destintion */ 138 for (dst = (int *)CONFIG_SYS_SPL_ARGS_ADDR, 139 src = (int *)CONFIG_SYS_TEXT_BASE; 140 src < (int *)(CONFIG_SYS_TEXT_BASE + 141 CONFIG_CMD_SPL_WRITE_SIZE); 142 src++, dst++) { 143 writel(readl(src), dst); 144 } 145 146 /* load linux */ 147 nand_spl_load_image(CONFIG_SYS_NAND_SPL_KERNEL_OFFS, 148 sizeof(*header), (void *)header); 149 err = spl_parse_image_header(spl_image, header); 150 if (err) 151 return err; 152 if (header->ih_os == IH_OS_LINUX) { 153 /* happy - was a linux */ 154 err = nand_spl_load_image( 155 CONFIG_SYS_NAND_SPL_KERNEL_OFFS, 156 spl_image->size, 157 (void *)spl_image->load_addr); 158 nand_deselect(); 159 return err; 160 } else { 161 puts("The Expected Linux image was not " 162 "found. Please check your NAND " 163 "configuration.\n"); 164 puts("Trying to start u-boot now...\n"); 165 } 166 } 167 #endif 168 #ifdef CONFIG_NAND_ENV_DST 169 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET, header); 170 #ifdef CONFIG_ENV_OFFSET_REDUND 171 spl_nand_load_element(spl_image, CONFIG_ENV_OFFSET_REDUND, header); 172 #endif 173 #endif 174 /* Load u-boot */ 175 err = spl_nand_load_element(spl_image, CONFIG_SYS_NAND_U_BOOT_OFFS, 176 header); 177 #ifdef CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND 178 #if CONFIG_SYS_NAND_U_BOOT_OFFS != CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND 179 if (err) 180 err = spl_nand_load_element(spl_image, 181 CONFIG_SYS_NAND_U_BOOT_OFFS_REDUND, 182 header); 183 #endif 184 #endif 185 nand_deselect(); 186 return err; 187 } 188 #endif 189 /* Use priorty 1 so that Ubi can override this */ 190 SPL_LOAD_IMAGE_METHOD("NAND", 1, BOOT_DEVICE_NAND, spl_nand_load_image); 191