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 <spl.h> 11 #include <linux/compiler.h> 12 #include <asm/u-boot.h> 13 #include <mmc.h> 14 #include <image.h> 15 16 DECLARE_GLOBAL_DATA_PTR; 17 18 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector) 19 { 20 unsigned long count; 21 u32 image_size_sectors; 22 struct image_header *header; 23 24 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - 25 sizeof(struct image_header)); 26 27 /* read image header to find the image size & load address */ 28 count = mmc->block_dev.block_read(0, sector, 1, header); 29 debug("read sector %lx, count=%lu\n", sector, count); 30 if (count == 0) 31 goto end; 32 33 if (image_get_magic(header) != IH_MAGIC) { 34 puts("bad magic\n"); 35 return -1; 36 } 37 38 spl_parse_image_header(header); 39 40 /* convert size to sectors - round up */ 41 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / 42 mmc->read_bl_len; 43 44 /* Read the header too to avoid extra memcpy */ 45 count = mmc->block_dev.block_read(0, sector, image_size_sectors, 46 (void *)spl_image.load_addr); 47 debug("read %x sectors to %x\n", image_size_sectors, 48 spl_image.load_addr); 49 50 end: 51 if (count == 0) { 52 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 53 puts("spl: mmc block read error\n"); 54 #endif 55 return -1; 56 } 57 58 return 0; 59 } 60 61 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION 62 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) 63 { 64 disk_partition_t info; 65 int err; 66 67 err = get_partition_info(&mmc->block_dev, partition, &info); 68 if (err) { 69 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 70 puts("spl: partition error\n"); 71 #endif 72 return -1; 73 } 74 75 return mmc_load_image_raw_sector(mmc, info.start); 76 } 77 #endif 78 79 #ifdef CONFIG_SPL_OS_BOOT 80 static int mmc_load_image_raw_os(struct mmc *mmc) 81 { 82 unsigned long count; 83 84 count = mmc->block_dev.block_read(0, 85 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, 86 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, 87 (void *) CONFIG_SYS_SPL_ARGS_ADDR); 88 if (count == 0) { 89 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 90 puts("spl: mmc block read error\n"); 91 #endif 92 return -1; 93 } 94 95 return mmc_load_image_raw_sector(mmc, 96 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); 97 } 98 #endif 99 100 void spl_mmc_load_image(void) 101 { 102 struct mmc *mmc; 103 u32 boot_mode; 104 int err; 105 __maybe_unused int part; 106 107 mmc_initialize(gd->bd); 108 109 /* We register only one device. So, the dev id is always 0 */ 110 mmc = find_mmc_device(0); 111 if (!mmc) { 112 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 113 puts("spl: mmc device not found\n"); 114 #endif 115 hang(); 116 } 117 118 err = mmc_init(mmc); 119 if (err) { 120 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 121 printf("spl: mmc init failed with error: %d\n", err); 122 #endif 123 hang(); 124 } 125 126 boot_mode = spl_boot_mode(); 127 switch (boot_mode) { 128 case MMCSD_MODE_RAW: 129 debug("spl: mmc boot mode: raw\n"); 130 131 #ifdef CONFIG_SPL_OS_BOOT 132 if (!spl_start_uboot()) { 133 err = mmc_load_image_raw_os(mmc); 134 if (!err) 135 return; 136 } 137 #endif 138 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION) 139 err = mmc_load_image_raw_partition(mmc, 140 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); 141 if (!err) 142 return; 143 #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) 144 err = mmc_load_image_raw_sector(mmc, 145 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); 146 if (!err) 147 return; 148 #endif 149 case MMCSD_MODE_FS: 150 debug("spl: mmc boot mode: fs\n"); 151 152 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION 153 #ifdef CONFIG_SPL_FAT_SUPPORT 154 #ifdef CONFIG_SPL_OS_BOOT 155 if (!spl_start_uboot()) { 156 err = spl_load_image_fat_os(&mmc->block_dev, 157 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); 158 if (!err) 159 return; 160 } 161 #endif 162 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME 163 err = spl_load_image_fat(&mmc->block_dev, 164 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, 165 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); 166 if (!err) 167 return; 168 #endif 169 #endif 170 #ifdef CONFIG_SPL_EXT_SUPPORT 171 #ifdef CONFIG_SPL_OS_BOOT 172 if (!spl_start_uboot()) { 173 err = spl_load_image_ext_os(&mmc->block_dev, 174 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); 175 if (!err) 176 return; 177 } 178 #endif 179 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME 180 err = spl_load_image_ext(&mmc->block_dev, 181 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, 182 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); 183 if (!err) 184 return; 185 #endif 186 #endif 187 #endif 188 #ifdef CONFIG_SUPPORT_EMMC_BOOT 189 case MMCSD_MODE_EMMCBOOT: 190 /* 191 * We need to check what the partition is configured to. 192 * 1 and 2 match up to boot0 / boot1 and 7 is user data 193 * which is the first physical partition (0). 194 */ 195 part = (mmc->part_config >> 3) & PART_ACCESS_MASK; 196 197 if (part == 7) 198 part = 0; 199 200 if (mmc_switch_part(0, part)) { 201 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 202 puts("spl: mmc partition switch failed\n"); 203 #endif 204 hang(); 205 } 206 207 #ifdef CONFIG_SPL_OS_BOOT 208 if (!spl_start_uboot()) { 209 err = mmc_load_image_raw_os(mmc); 210 if (!err) 211 return; 212 } 213 #endif 214 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION) 215 err = mmc_load_image_raw_partition(mmc, 216 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); 217 if (!err) 218 return; 219 #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) 220 err = mmc_load_image_raw_sector(mmc, 221 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); 222 if (!err) 223 return; 224 #endif 225 #endif 226 case MMCSD_MODE_UNDEFINED: 227 default: 228 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT 229 if (err) 230 puts("spl: mmc: no boot mode left to try\n"); 231 else 232 puts("spl: mmc: wrong boot mode\n"); 233 #endif 234 hang(); 235 } 236 } 237