1 /* 2 * (C) Copyright 2010 3 * Texas Instruments, <www.ti.com> 4 * 5 * Aneesh V <aneesh@ti.com> 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 #include <common.h> 10 #include <dm.h> 11 #include <spl.h> 12 #include <linux/compiler.h> 13 #include <errno.h> 14 #include <asm/u-boot.h> 15 #include <errno.h> 16 #include <mmc.h> 17 #include <image.h> 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc, 22 ulong sector, struct image_header *header) 23 { 24 u32 image_size_sectors; 25 unsigned long count; 26 int ret; 27 28 ret = spl_parse_image_header(spl_image, header); 29 if (ret) 30 return ret; 31 32 /* convert size to sectors - round up */ 33 image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) / 34 mmc->read_bl_len; 35 36 /* Read the header too to avoid extra memcpy */ 37 count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors, 38 (void *)(ulong)spl_image->load_addr); 39 debug("read %x sectors to %lx\n", image_size_sectors, 40 spl_image->load_addr); 41 if (count != image_size_sectors) 42 return -EIO; 43 44 return 0; 45 } 46 47 static ulong h_spl_load_read(struct spl_load_info *load, ulong sector, 48 ulong count, void *buf) 49 { 50 struct mmc *mmc = load->dev; 51 52 return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf); 53 } 54 55 static int mmc_load_image_raw_sector(struct spl_image_info *spl_image, 56 struct mmc *mmc, unsigned long sector) 57 { 58 unsigned long count; 59 struct image_header *header; 60 int ret = 0; 61 62 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - 63 sizeof(struct image_header)); 64 65 /* read image header to find the image size & load address */ 66 count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header); 67 debug("hdr read sector %lx, count=%lu\n", sector, count); 68 if (count == 0) { 69 ret = -EIO; 70 goto end; 71 } 72 73 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && 74 image_get_magic(header) == FDT_MAGIC) { 75 struct spl_load_info load; 76 77 debug("Found FIT\n"); 78 load.dev = mmc; 79 load.priv = NULL; 80 load.filename = NULL; 81 load.bl_len = mmc->read_bl_len; 82 load.read = h_spl_load_read; 83 ret = spl_load_simple_fit(spl_image, &load, sector, header); 84 } else { 85 ret = mmc_load_legacy(spl_image, mmc, sector, header); 86 } 87 88 end: 89 if (ret) { 90 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 91 puts("mmc_load_image_raw_sector: mmc block read error\n"); 92 #endif 93 return -1; 94 } 95 96 return 0; 97 } 98 99 int spl_mmc_get_device_index(u32 boot_device) 100 { 101 switch (boot_device) { 102 case BOOT_DEVICE_MMC1: 103 return 0; 104 case BOOT_DEVICE_MMC2: 105 case BOOT_DEVICE_MMC2_2: 106 return 1; 107 } 108 109 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 110 printf("spl: unsupported mmc boot device.\n"); 111 #endif 112 113 return -ENODEV; 114 } 115 116 static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device) 117 { 118 #ifdef CONFIG_DM_MMC 119 struct udevice *dev; 120 #endif 121 int err, mmc_dev; 122 123 mmc_dev = spl_mmc_get_device_index(boot_device); 124 if (mmc_dev < 0) 125 return mmc_dev; 126 127 err = mmc_initialize(NULL); 128 if (err) { 129 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 130 printf("spl: could not initialize mmc. error: %d\n", err); 131 #endif 132 return err; 133 } 134 135 #ifdef CONFIG_DM_MMC 136 err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev); 137 if (!err) 138 *mmcp = mmc_get_mmc_dev(dev); 139 #else 140 *mmcp = find_mmc_device(mmc_dev); 141 err = *mmcp ? 0 : -ENODEV; 142 #endif 143 if (err) { 144 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 145 printf("spl: could not find mmc device. error: %d\n", err); 146 #endif 147 return err; 148 } 149 150 return 0; 151 } 152 153 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION 154 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, 155 struct mmc *mmc, int partition) 156 { 157 disk_partition_t info; 158 int err; 159 160 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE 161 int type_part; 162 /* Only support MBR so DOS_ENTRY_NUMBERS */ 163 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) { 164 err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info); 165 if (err) 166 continue; 167 if (info.sys_ind == 168 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) { 169 partition = type_part; 170 break; 171 } 172 } 173 #endif 174 175 err = part_get_info(mmc_get_blk_desc(mmc), partition, &info); 176 if (err) { 177 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 178 puts("spl: partition error\n"); 179 #endif 180 return -1; 181 } 182 183 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR 184 return mmc_load_image_raw_sector(spl_image, mmc, 185 info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); 186 #else 187 return mmc_load_image_raw_sector(spl_image, mmc, info.start); 188 #endif 189 } 190 #else 191 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1 192 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, 193 struct mmc *mmc, int partition) 194 { 195 return -ENOSYS; 196 } 197 #endif 198 199 #ifdef CONFIG_SPL_OS_BOOT 200 static int mmc_load_image_raw_os(struct spl_image_info *spl_image, 201 struct mmc *mmc) 202 { 203 unsigned long count; 204 int ret; 205 206 count = blk_dread(mmc_get_blk_desc(mmc), 207 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, 208 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, 209 (void *) CONFIG_SYS_SPL_ARGS_ADDR); 210 if (count == 0) { 211 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 212 puts("mmc_load_image_raw_os: mmc block read error\n"); 213 #endif 214 return -1; 215 } 216 217 ret = mmc_load_image_raw_sector(spl_image, mmc, 218 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); 219 if (ret) 220 return ret; 221 222 if (spl_image->os != IH_OS_LINUX) { 223 puts("Expected Linux image is not found. Trying to start U-boot\n"); 224 return -ENOENT; 225 } 226 227 return 0; 228 } 229 #else 230 int spl_start_uboot(void) 231 { 232 return 1; 233 } 234 static int mmc_load_image_raw_os(struct spl_image_info *spl_image, 235 struct mmc *mmc) 236 { 237 return -ENOSYS; 238 } 239 #endif 240 241 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION 242 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc) 243 { 244 int err = -ENOSYS; 245 246 #ifdef CONFIG_SPL_FAT_SUPPORT 247 if (!spl_start_uboot()) { 248 err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc), 249 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); 250 if (!err) 251 return err; 252 } 253 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME 254 err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc), 255 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, 256 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); 257 if (!err) 258 return err; 259 #endif 260 #endif 261 #ifdef CONFIG_SPL_EXT_SUPPORT 262 if (!spl_start_uboot()) { 263 err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc), 264 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); 265 if (!err) 266 return err; 267 } 268 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME 269 err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc), 270 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, 271 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); 272 if (!err) 273 return err; 274 #endif 275 #endif 276 277 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) 278 err = -ENOENT; 279 #endif 280 281 return err; 282 } 283 #else 284 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc) 285 { 286 return -ENOSYS; 287 } 288 #endif 289 290 int spl_mmc_load_image(struct spl_image_info *spl_image, 291 struct spl_boot_device *bootdev) 292 { 293 struct mmc *mmc = NULL; 294 u32 boot_mode; 295 int err = 0; 296 __maybe_unused int part; 297 298 err = spl_mmc_find_device(&mmc, bootdev->boot_device); 299 if (err) 300 return err; 301 302 err = mmc_init(mmc); 303 if (err) { 304 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 305 printf("spl: mmc init failed with error: %d\n", err); 306 #endif 307 return err; 308 } 309 310 boot_mode = spl_boot_mode(bootdev->boot_device); 311 err = -EINVAL; 312 switch (boot_mode) { 313 case MMCSD_MODE_EMMCBOOT: 314 /* 315 * We need to check what the partition is configured to. 316 * 1 and 2 match up to boot0 / boot1 and 7 is user data 317 * which is the first physical partition (0). 318 */ 319 part = (mmc->part_config >> 3) & PART_ACCESS_MASK; 320 321 if (part == 7) 322 part = 0; 323 324 if (CONFIG_IS_ENABLED(MMC_TINY)) 325 err = mmc_switch_part(mmc, part); 326 else 327 err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); 328 329 if (err) { 330 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 331 puts("spl: mmc partition switch failed\n"); 332 #endif 333 return err; 334 } 335 /* Fall through */ 336 case MMCSD_MODE_RAW: 337 debug("spl: mmc boot mode: raw\n"); 338 339 if (!spl_start_uboot()) { 340 err = mmc_load_image_raw_os(spl_image, mmc); 341 if (!err) 342 return err; 343 } 344 345 err = mmc_load_image_raw_partition(spl_image, mmc, 346 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); 347 if (!err) 348 return err; 349 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR 350 err = mmc_load_image_raw_sector(spl_image, mmc, 351 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); 352 if (!err) 353 return err; 354 #endif 355 /* If RAW mode fails, try FS mode. */ 356 case MMCSD_MODE_FS: 357 debug("spl: mmc boot mode: fs\n"); 358 359 err = spl_mmc_do_fs_boot(spl_image, mmc); 360 if (!err) 361 return err; 362 363 break; 364 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 365 default: 366 puts("spl: mmc: wrong boot mode\n"); 367 #endif 368 } 369 370 return err; 371 } 372 373 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image); 374 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image); 375 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image); 376