2011-09-14 19:33:34 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2010
|
|
|
|
* Texas Instruments, <www.ti.com>
|
|
|
|
*
|
|
|
|
* Aneesh V <aneesh@ti.com>
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2011-09-14 19:33:34 +00:00
|
|
|
*/
|
|
|
|
#include <common.h>
|
2012-08-13 19:03:19 +00:00
|
|
|
#include <spl.h>
|
2015-04-27 08:20:22 +00:00
|
|
|
#include <linux/compiler.h>
|
2011-09-14 19:33:34 +00:00
|
|
|
#include <asm/u-boot.h>
|
|
|
|
#include <mmc.h>
|
2013-06-28 18:43:01 +00:00
|
|
|
#include <image.h>
|
2011-09-14 19:33:34 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2014-11-08 22:14:56 +00:00
|
|
|
static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
|
2011-09-14 19:33:34 +00:00
|
|
|
{
|
2013-03-21 04:55:17 +00:00
|
|
|
unsigned long err;
|
|
|
|
u32 image_size_sectors;
|
|
|
|
struct image_header *header;
|
2011-09-14 19:33:34 +00:00
|
|
|
|
|
|
|
header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
|
2015-04-27 08:20:22 +00:00
|
|
|
sizeof(struct image_header));
|
2011-09-14 19:33:34 +00:00
|
|
|
|
|
|
|
/* read image header to find the image size & load address */
|
2013-05-13 08:36:28 +00:00
|
|
|
err = mmc->block_dev.block_read(0, sector, 1, header);
|
2013-03-21 04:55:17 +00:00
|
|
|
if (err == 0)
|
2011-09-14 19:33:34 +00:00
|
|
|
goto end;
|
|
|
|
|
2013-06-28 18:43:01 +00:00
|
|
|
if (image_get_magic(header) != IH_MAGIC)
|
|
|
|
return -1;
|
|
|
|
|
2011-09-14 19:33:34 +00:00
|
|
|
spl_parse_image_header(header);
|
|
|
|
|
|
|
|
/* convert size to sectors - round up */
|
2012-08-14 17:25:15 +00:00
|
|
|
image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
|
2015-04-27 08:20:22 +00:00
|
|
|
mmc->read_bl_len;
|
2011-09-14 19:33:34 +00:00
|
|
|
|
|
|
|
/* Read the header too to avoid extra memcpy */
|
2013-05-13 08:36:28 +00:00
|
|
|
err = mmc->block_dev.block_read(0, sector, image_size_sectors,
|
|
|
|
(void *)spl_image.load_addr);
|
2011-09-14 19:33:34 +00:00
|
|
|
|
|
|
|
end:
|
2013-09-04 15:12:24 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2013-05-13 08:36:25 +00:00
|
|
|
if (err == 0)
|
2015-04-27 08:20:22 +00:00
|
|
|
printf("spl: mmc block read error\n");
|
2013-09-04 15:12:24 +00:00
|
|
|
#endif
|
2013-05-13 08:36:25 +00:00
|
|
|
|
|
|
|
return (err == 0);
|
2011-09-14 19:33:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 22:14:56 +00:00
|
|
|
#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
|
|
|
|
static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
|
|
|
|
{
|
|
|
|
disk_partition_t info;
|
|
|
|
|
|
|
|
if (get_partition_info(&mmc->block_dev, partition, &info)) {
|
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
|
|
|
printf("spl: partition error\n");
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mmc_load_image_raw_sector(mmc, info.start);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-13 08:36:29 +00:00
|
|
|
#ifdef CONFIG_SPL_OS_BOOT
|
|
|
|
static int mmc_load_image_raw_os(struct mmc *mmc)
|
|
|
|
{
|
2015-04-27 08:20:22 +00:00
|
|
|
unsigned long err;
|
|
|
|
|
|
|
|
err = mmc->block_dev.block_read(0,
|
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
|
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
|
|
|
|
(void *)CONFIG_SYS_SPL_ARGS_ADDR);
|
|
|
|
if (err) {
|
2013-09-04 15:12:24 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2015-04-27 08:20:22 +00:00
|
|
|
printf("spl: mmc block read error\n");
|
2013-09-04 15:12:24 +00:00
|
|
|
#endif
|
2013-05-13 08:36:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-08 22:14:56 +00:00
|
|
|
return mmc_load_image_raw_sector(mmc,
|
2015-04-27 08:20:22 +00:00
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
|
2013-05-13 08:36:29 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-14 19:33:34 +00:00
|
|
|
void spl_mmc_load_image(void)
|
|
|
|
{
|
|
|
|
struct mmc *mmc;
|
|
|
|
u32 boot_mode;
|
2015-04-27 08:20:22 +00:00
|
|
|
int err;
|
|
|
|
__maybe_unused int part;
|
2011-09-14 19:33:34 +00:00
|
|
|
|
|
|
|
mmc_initialize(gd->bd);
|
2015-04-27 08:20:22 +00:00
|
|
|
|
2011-09-14 19:33:34 +00:00
|
|
|
/* We register only one device. So, the dev id is always 0 */
|
|
|
|
mmc = find_mmc_device(0);
|
|
|
|
if (!mmc) {
|
2013-09-04 15:12:24 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2015-04-27 08:20:22 +00:00
|
|
|
puts("spl: mmc device not found\n");
|
2013-09-04 15:12:24 +00:00
|
|
|
#endif
|
2011-09-14 19:33:34 +00:00
|
|
|
hang();
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mmc_init(mmc);
|
|
|
|
if (err) {
|
2013-09-04 15:12:24 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2015-04-27 08:20:22 +00:00
|
|
|
printf("spl: mmc init failed with error: %d\n", err);
|
2013-09-04 15:12:24 +00:00
|
|
|
#endif
|
2011-09-14 19:33:34 +00:00
|
|
|
hang();
|
|
|
|
}
|
2013-05-13 08:36:25 +00:00
|
|
|
|
2012-08-14 16:19:44 +00:00
|
|
|
boot_mode = spl_boot_mode();
|
2015-04-27 08:20:22 +00:00
|
|
|
switch (boot_mode) {
|
|
|
|
case MMCSD_MODE_RAW:
|
|
|
|
debug("spl: mmc boot mode: raw\n");
|
|
|
|
|
2013-05-13 08:36:29 +00:00
|
|
|
#ifdef CONFIG_SPL_OS_BOOT
|
2015-04-27 08:20:22 +00:00
|
|
|
if (!spl_start_uboot()) {
|
|
|
|
err = mmc_load_image_raw_os(mmc);
|
|
|
|
if (!err)
|
|
|
|
return;
|
|
|
|
}
|
2013-05-13 08:36:29 +00:00
|
|
|
#endif
|
2014-11-08 22:14:56 +00:00
|
|
|
#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
|
|
|
|
err = mmc_load_image_raw_partition(mmc,
|
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
|
|
|
|
#else
|
|
|
|
err = mmc_load_image_raw_sector(mmc,
|
2014-01-16 17:23:29 +00:00
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
|
2014-11-08 22:14:56 +00:00
|
|
|
#endif
|
2015-04-27 08:20:22 +00:00
|
|
|
if (!err)
|
|
|
|
return;
|
2014-10-15 15:53:12 +00:00
|
|
|
#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
|
2015-04-27 08:20:22 +00:00
|
|
|
case MMCSD_MODE_FS:
|
|
|
|
debug("spl: mmc boot mode: fs\n");
|
|
|
|
|
2014-10-15 15:53:12 +00:00
|
|
|
#ifdef CONFIG_SPL_FAT_SUPPORT
|
2013-05-13 08:36:27 +00:00
|
|
|
#ifdef CONFIG_SPL_OS_BOOT
|
2015-04-27 08:20:22 +00:00
|
|
|
if (!spl_start_uboot()) {
|
|
|
|
err = spl_load_image_fat_os(&mmc->block_dev,
|
|
|
|
CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
|
|
|
|
if (!err)
|
|
|
|
return;
|
|
|
|
}
|
2013-05-13 08:36:27 +00:00
|
|
|
#endif
|
2014-01-16 17:23:29 +00:00
|
|
|
err = spl_load_image_fat(&mmc->block_dev,
|
2015-04-27 08:20:22 +00:00
|
|
|
CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
|
|
|
|
CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
|
|
|
|
if (!err)
|
|
|
|
return;
|
|
|
|
#endif
|
2014-10-15 15:53:12 +00:00
|
|
|
#ifdef CONFIG_SPL_EXT_SUPPORT
|
|
|
|
#ifdef CONFIG_SPL_OS_BOOT
|
2015-04-27 08:20:22 +00:00
|
|
|
if (!spl_start_uboot()) {
|
|
|
|
err = spl_load_image_ext_os(&mmc->block_dev,
|
|
|
|
CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
|
|
|
|
if (!err)
|
|
|
|
return;
|
|
|
|
}
|
2014-02-05 15:24:18 +00:00
|
|
|
#endif
|
2014-10-15 15:53:12 +00:00
|
|
|
err = spl_load_image_ext(&mmc->block_dev,
|
2015-04-27 08:20:22 +00:00
|
|
|
CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
|
|
|
|
CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
|
|
|
|
if (!err)
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-02-05 15:24:18 +00:00
|
|
|
#ifdef CONFIG_SUPPORT_EMMC_BOOT
|
2015-04-27 08:20:22 +00:00
|
|
|
case MMCSD_MODE_EMMCBOOT:
|
2014-02-05 15:24:18 +00:00
|
|
|
/*
|
|
|
|
* We need to check what the partition is configured to.
|
|
|
|
* 1 and 2 match up to boot0 / boot1 and 7 is user data
|
|
|
|
* which is the first physical partition (0).
|
|
|
|
*/
|
2015-04-27 08:20:22 +00:00
|
|
|
part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
|
2014-02-05 15:24:18 +00:00
|
|
|
|
|
|
|
if (part == 7)
|
|
|
|
part = 0;
|
|
|
|
|
|
|
|
if (mmc_switch_part(0, part)) {
|
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2015-04-27 08:20:22 +00:00
|
|
|
puts("spl: mmc partition switch failed\n");
|
2014-02-05 15:24:18 +00:00
|
|
|
#endif
|
|
|
|
hang();
|
|
|
|
}
|
2015-04-27 08:20:22 +00:00
|
|
|
|
2014-02-05 15:24:18 +00:00
|
|
|
#ifdef CONFIG_SPL_OS_BOOT
|
2015-04-27 08:20:22 +00:00
|
|
|
if (!spl_start_uboot()) {
|
|
|
|
err = mmc_load_image_raw_os(mmc);
|
|
|
|
if (!err)
|
|
|
|
return;
|
|
|
|
}
|
2014-02-05 15:24:18 +00:00
|
|
|
#endif
|
2015-04-27 08:20:23 +00:00
|
|
|
#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
|
|
|
|
err = mmc_load_image_raw_partition(mmc,
|
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
|
|
|
|
#else
|
2014-11-08 22:14:56 +00:00
|
|
|
err = mmc_load_image_raw_sector(mmc,
|
2014-02-05 15:24:18 +00:00
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
|
2015-04-27 08:20:23 +00:00
|
|
|
#endif
|
2015-04-27 08:20:22 +00:00
|
|
|
if (!err)
|
|
|
|
return;
|
2012-08-10 16:27:14 +00:00
|
|
|
#endif
|
2015-04-27 08:20:22 +00:00
|
|
|
case MMCSD_MODE_UNDEFINED:
|
|
|
|
default:
|
2013-09-04 15:12:24 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2015-04-27 08:20:22 +00:00
|
|
|
if (err)
|
|
|
|
puts("spl: mmc: no boot mode left to try\n");
|
|
|
|
else
|
|
|
|
puts("spl: mmc: wrong boot mode\n");
|
2013-09-04 15:12:24 +00:00
|
|
|
#endif
|
2013-05-13 08:36:25 +00:00
|
|
|
hang();
|
2015-04-27 08:20:22 +00:00
|
|
|
}
|
2011-09-14 19:33:34 +00:00
|
|
|
}
|