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>
|
2015-06-23 21:38:51 +00:00
|
|
|
#include <dm.h>
|
2012-08-13 19:03:19 +00:00
|
|
|
#include <spl.h>
|
2015-04-27 08:20:22 +00:00
|
|
|
#include <linux/compiler.h>
|
2015-11-08 15:11:49 +00:00
|
|
|
#include <errno.h>
|
2011-09-14 19:33:34 +00:00
|
|
|
#include <asm/u-boot.h>
|
2015-11-08 15:11:44 +00:00
|
|
|
#include <errno.h>
|
2011-09-14 19:33:34 +00:00
|
|
|
#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;
|
|
|
|
|
2016-02-23 05:55:58 +00:00
|
|
|
static int mmc_load_legacy(struct mmc *mmc, ulong sector,
|
|
|
|
struct image_header *header)
|
|
|
|
{
|
|
|
|
u32 image_size_sectors;
|
|
|
|
unsigned long count;
|
2016-04-28 22:44:54 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = spl_parse_image_header(header);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-02-23 05:55:58 +00:00
|
|
|
|
|
|
|
/* convert size to sectors - round up */
|
|
|
|
image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
|
|
|
|
mmc->read_bl_len;
|
|
|
|
|
|
|
|
/* Read the header too to avoid extra memcpy */
|
|
|
|
count = mmc->block_dev.block_read(&mmc->block_dev, sector,
|
|
|
|
image_size_sectors,
|
|
|
|
(void *)(ulong)spl_image.load_addr);
|
|
|
|
debug("read %x sectors to %x\n", image_size_sectors,
|
|
|
|
spl_image.load_addr);
|
|
|
|
if (count != image_size_sectors)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ulong h_spl_load_read(struct spl_load_info *load, ulong sector,
|
|
|
|
ulong count, void *buf)
|
|
|
|
{
|
|
|
|
struct mmc *mmc = load->dev;
|
|
|
|
|
|
|
|
return mmc->block_dev.block_read(&mmc->block_dev, sector, count, buf);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-05-22 10:45:35 +00:00
|
|
|
unsigned long count;
|
2013-03-21 04:55:17 +00:00
|
|
|
struct image_header *header;
|
2016-02-23 05:55:58 +00:00
|
|
|
int ret = 0;
|
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 */
|
2015-12-07 18:38:48 +00:00
|
|
|
count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header);
|
2016-02-23 05:55:58 +00:00
|
|
|
debug("hdr read sector %lx, count=%lu\n", sector, count);
|
|
|
|
if (count == 0) {
|
|
|
|
ret = -EIO;
|
2011-09-14 19:33:34 +00:00
|
|
|
goto end;
|
2016-02-23 05:55:58 +00:00
|
|
|
}
|
2011-09-14 19:33:34 +00:00
|
|
|
|
2016-03-16 03:10:00 +00:00
|
|
|
if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
|
|
|
|
image_get_magic(header) == FDT_MAGIC) {
|
2016-02-23 05:55:58 +00:00
|
|
|
struct spl_load_info load;
|
|
|
|
|
|
|
|
debug("Found FIT\n");
|
|
|
|
load.dev = mmc;
|
|
|
|
load.priv = NULL;
|
2016-05-24 05:04:38 +00:00
|
|
|
load.filename = NULL;
|
2016-02-23 05:55:58 +00:00
|
|
|
load.bl_len = mmc->read_bl_len;
|
|
|
|
load.read = h_spl_load_read;
|
|
|
|
ret = spl_load_simple_fit(&load, sector, header);
|
2016-03-16 03:10:00 +00:00
|
|
|
} else {
|
|
|
|
ret = mmc_load_legacy(mmc, sector, header);
|
2015-06-23 21:38:47 +00:00
|
|
|
}
|
2013-06-28 18:43:01 +00:00
|
|
|
|
2011-09-14 19:33:34 +00:00
|
|
|
end:
|
2016-02-23 05:55:58 +00:00
|
|
|
if (ret) {
|
2013-09-04 15:12:24 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2016-04-21 02:12:04 +00:00
|
|
|
puts("mmc_load_image_raw_sector: mmc block read error\n");
|
2013-09-04 15:12:24 +00:00
|
|
|
#endif
|
2015-05-22 10:45:35 +00:00
|
|
|
return -1;
|
2015-06-03 16:48:51 +00:00
|
|
|
}
|
2015-05-22 10:45:35 +00:00
|
|
|
|
|
|
|
return 0;
|
2011-09-14 19:33:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 15:11:54 +00:00
|
|
|
int spl_mmc_get_device_index(u32 boot_device)
|
|
|
|
{
|
|
|
|
switch (boot_device) {
|
|
|
|
case BOOT_DEVICE_MMC1:
|
|
|
|
return 0;
|
|
|
|
case BOOT_DEVICE_MMC2:
|
|
|
|
case BOOT_DEVICE_MMC2_2:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
|
|
|
printf("spl: unsupported mmc boot device.\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:59:12 +00:00
|
|
|
static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device)
|
2015-11-08 15:11:44 +00:00
|
|
|
{
|
2015-12-02 17:59:13 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2015-11-08 15:11:44 +00:00
|
|
|
struct udevice *dev;
|
2015-12-02 17:59:13 +00:00
|
|
|
#endif
|
2015-11-08 15:11:54 +00:00
|
|
|
int err, mmc_dev;
|
|
|
|
|
|
|
|
mmc_dev = spl_mmc_get_device_index(boot_device);
|
|
|
|
if (mmc_dev < 0)
|
|
|
|
return mmc_dev;
|
2015-11-08 15:11:44 +00:00
|
|
|
|
|
|
|
err = mmc_initialize(NULL);
|
|
|
|
if (err) {
|
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
|
|
|
printf("spl: could not initialize mmc. error: %d\n", err);
|
|
|
|
#endif
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:59:13 +00:00
|
|
|
#ifdef CONFIG_DM_MMC
|
2015-11-08 15:11:54 +00:00
|
|
|
err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev);
|
2015-12-02 17:59:13 +00:00
|
|
|
if (!err)
|
|
|
|
*mmcp = mmc_get_mmc_dev(dev);
|
2015-11-08 15:11:44 +00:00
|
|
|
#else
|
2015-12-02 17:59:13 +00:00
|
|
|
*mmcp = find_mmc_device(mmc_dev);
|
|
|
|
err = *mmcp ? 0 : -ENODEV;
|
|
|
|
#endif
|
2015-11-08 15:11:44 +00:00
|
|
|
if (err) {
|
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2015-12-02 17:59:13 +00:00
|
|
|
printf("spl: could not find mmc device. error: %d\n", err);
|
2015-11-08 15:11:44 +00:00
|
|
|
#endif
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2015-05-22 10:45:35 +00:00
|
|
|
int err;
|
2014-11-08 22:14:56 +00:00
|
|
|
|
2016-02-29 22:25:48 +00:00
|
|
|
err = part_get_info(&mmc->block_dev, partition, &info);
|
2015-05-22 10:45:35 +00:00
|
|
|
if (err) {
|
2014-11-08 22:14:56 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2015-06-03 16:48:51 +00:00
|
|
|
puts("spl: partition error\n");
|
2014-11-08 22:14:56 +00:00
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-20 09:20:39 +00:00
|
|
|
#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
|
|
|
|
return mmc_load_image_raw_sector(mmc, info.start +
|
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
|
|
|
|
#else
|
2014-11-08 22:14:56 +00:00
|
|
|
return mmc_load_image_raw_sector(mmc, info.start);
|
2015-07-20 09:20:39 +00:00
|
|
|
#endif
|
2014-11-08 22:14:56 +00:00
|
|
|
}
|
2015-11-08 15:11:46 +00:00
|
|
|
#else
|
|
|
|
#define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
|
|
|
|
static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2014-11-08 22:14:56 +00:00
|
|
|
#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-05-22 10:45:35 +00:00
|
|
|
unsigned long count;
|
2016-04-11 09:54:50 +00:00
|
|
|
int ret;
|
2015-04-27 08:20:22 +00:00
|
|
|
|
2015-12-07 18:38:48 +00:00
|
|
|
count = mmc->block_dev.block_read(&mmc->block_dev,
|
2015-05-22 10:45:35 +00:00
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
|
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
|
|
|
|
(void *) CONFIG_SYS_SPL_ARGS_ADDR);
|
|
|
|
if (count == 0) {
|
2013-09-04 15:12:24 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2016-04-21 02:12:04 +00:00
|
|
|
puts("mmc_load_image_raw_os: mmc block read error\n");
|
2013-09-04 15:12:24 +00:00
|
|
|
#endif
|
2013-05-13 08:36:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-04-11 09:54:50 +00:00
|
|
|
ret = mmc_load_image_raw_sector(mmc,
|
2015-04-27 08:20:22 +00:00
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
|
2016-04-11 09:54:50 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (spl_image.os != IH_OS_LINUX) {
|
|
|
|
puts("Expected Linux image is not found. Trying to start U-boot\n");
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-05-13 08:36:29 +00:00
|
|
|
}
|
2015-11-08 15:11:45 +00:00
|
|
|
#else
|
|
|
|
int spl_start_uboot(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static int mmc_load_image_raw_os(struct mmc *mmc)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2013-05-13 08:36:29 +00:00
|
|
|
#endif
|
|
|
|
|
2015-11-08 15:11:47 +00:00
|
|
|
#ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
|
|
|
|
int spl_mmc_do_fs_boot(struct mmc *mmc)
|
|
|
|
{
|
|
|
|
int err = -ENOSYS;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SPL_FAT_SUPPORT
|
|
|
|
if (!spl_start_uboot()) {
|
|
|
|
err = spl_load_image_fat_os(&mmc->block_dev,
|
|
|
|
CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
|
|
|
|
if (!err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
|
|
|
|
err = spl_load_image_fat(&mmc->block_dev,
|
|
|
|
CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
|
|
|
|
CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
|
|
|
|
if (!err)
|
|
|
|
return err;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SPL_EXT_SUPPORT
|
|
|
|
if (!spl_start_uboot()) {
|
|
|
|
err = spl_load_image_ext_os(&mmc->block_dev,
|
|
|
|
CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
|
|
|
|
if (!err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
|
|
|
|
err = spl_load_image_ext(&mmc->block_dev,
|
|
|
|
CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
|
|
|
|
CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
|
|
|
|
if (!err)
|
|
|
|
return err;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
|
|
|
|
err = -ENOENT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int spl_mmc_do_fs_boot(struct mmc *mmc)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-08 15:11:54 +00:00
|
|
|
int spl_mmc_load_image(u32 boot_device)
|
2011-09-14 19:33:34 +00:00
|
|
|
{
|
2015-12-02 17:59:11 +00:00
|
|
|
struct mmc *mmc = NULL;
|
2011-09-14 19:33:34 +00:00
|
|
|
u32 boot_mode;
|
2015-06-23 21:38:51 +00:00
|
|
|
int err = 0;
|
2015-04-27 08:20:22 +00:00
|
|
|
__maybe_unused int part;
|
2011-09-14 19:33:34 +00:00
|
|
|
|
2015-11-08 15:11:54 +00:00
|
|
|
err = spl_mmc_find_device(&mmc, boot_device);
|
2015-11-08 15:11:49 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2011-09-14 19:33:34 +00:00
|
|
|
|
2015-11-08 15:11:44 +00:00
|
|
|
err = mmc_init(mmc);
|
2011-09-14 19:33:34 +00:00
|
|
|
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
|
2015-11-08 15:11:49 +00:00
|
|
|
return err;
|
2011-09-14 19:33:34 +00:00
|
|
|
}
|
2013-05-13 08:36:25 +00:00
|
|
|
|
2012-08-14 16:19:44 +00:00
|
|
|
boot_mode = spl_boot_mode();
|
2015-11-08 15:11:49 +00:00
|
|
|
err = -EINVAL;
|
2015-04-27 08:20:22 +00:00
|
|
|
switch (boot_mode) {
|
2015-11-08 15:11:48 +00:00
|
|
|
case MMCSD_MODE_EMMCBOOT:
|
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
|
|
|
|
|
|
|
|
if (part == 7)
|
|
|
|
part = 0;
|
|
|
|
|
2016-05-01 19:52:29 +00:00
|
|
|
err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
|
2015-11-08 15:11:49 +00:00
|
|
|
if (err) {
|
2015-11-08 15:11:48 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
|
|
|
puts("spl: mmc partition switch failed\n");
|
|
|
|
#endif
|
2015-11-08 15:11:49 +00:00
|
|
|
return err;
|
2015-11-08 15:11:48 +00:00
|
|
|
}
|
|
|
|
/* Fall through */
|
2015-04-27 08:20:22 +00:00
|
|
|
case MMCSD_MODE_RAW:
|
|
|
|
debug("spl: mmc boot mode: raw\n");
|
|
|
|
|
|
|
|
if (!spl_start_uboot()) {
|
|
|
|
err = mmc_load_image_raw_os(mmc);
|
|
|
|
if (!err)
|
2015-11-08 15:11:49 +00:00
|
|
|
return err;
|
2015-04-27 08:20:22 +00:00
|
|
|
}
|
2015-11-08 15:11:46 +00:00
|
|
|
|
2014-11-08 22:14:56 +00:00
|
|
|
err = mmc_load_image_raw_partition(mmc,
|
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
|
2015-06-08 21:05:09 +00:00
|
|
|
if (!err)
|
2015-11-08 15:11:49 +00:00
|
|
|
return err;
|
2015-11-08 15:11:46 +00:00
|
|
|
#if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
|
2014-11-08 22:14:56 +00:00
|
|
|
err = mmc_load_image_raw_sector(mmc,
|
2014-01-16 17:23:29 +00:00
|
|
|
CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
|
2015-04-27 08:20:22 +00:00
|
|
|
if (!err)
|
2015-11-08 15:11:49 +00:00
|
|
|
return err;
|
2015-06-08 21:05:09 +00:00
|
|
|
#endif
|
2016-02-18 17:17:36 +00:00
|
|
|
/* If RAW mode fails, try FS mode. */
|
2015-04-27 08:20:22 +00:00
|
|
|
case MMCSD_MODE_FS:
|
|
|
|
debug("spl: mmc boot mode: fs\n");
|
|
|
|
|
2015-11-08 15:11:47 +00:00
|
|
|
err = spl_mmc_do_fs_boot(mmc);
|
2015-04-27 08:20:22 +00:00
|
|
|
if (!err)
|
2015-11-08 15:11:49 +00:00
|
|
|
return err;
|
2015-11-08 15:11:47 +00:00
|
|
|
|
2015-11-08 15:11:43 +00:00
|
|
|
break;
|
2015-04-27 08:20:22 +00:00
|
|
|
case MMCSD_MODE_UNDEFINED:
|
2013-09-04 15:12:24 +00:00
|
|
|
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
|
2015-11-08 15:11:43 +00:00
|
|
|
default:
|
|
|
|
puts("spl: mmc: wrong boot mode\n");
|
2013-09-04 15:12:24 +00:00
|
|
|
#endif
|
2015-04-27 08:20:22 +00:00
|
|
|
}
|
2015-11-08 15:11:43 +00:00
|
|
|
|
2015-11-08 15:11:49 +00:00
|
|
|
return err;
|
2011-09-14 19:33:34 +00:00
|
|
|
}
|