spl_mmc.c 3.9 KB
Newer Older
S
Simon Schwarz 已提交
1 2 3 4 5 6
/*
 * (C) Copyright 2010
 * Texas Instruments, <www.ti.com>
 *
 * Aneesh V <aneesh@ti.com>
 *
7
 * SPDX-License-Identifier:	GPL-2.0+
S
Simon Schwarz 已提交
8 9
 */
#include <common.h>
10
#include <spl.h>
S
Simon Schwarz 已提交
11 12
#include <asm/u-boot.h>
#include <mmc.h>
13
#include <version.h>
14
#include <image.h>
S
Simon Schwarz 已提交
15 16 17

DECLARE_GLOBAL_DATA_PTR;

18
static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector)
S
Simon Schwarz 已提交
19
{
20 21 22
	unsigned long err;
	u32 image_size_sectors;
	struct image_header *header;
S
Simon Schwarz 已提交
23 24 25 26 27

	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
						sizeof(struct image_header));

	/* read image header to find the image size & load address */
28
	err = mmc->block_dev.block_read(0, sector, 1, header);
29
	if (err == 0)
S
Simon Schwarz 已提交
30 31
		goto end;

32 33 34
	if (image_get_magic(header) != IH_MAGIC)
		return -1;

S
Simon Schwarz 已提交
35 36 37
	spl_parse_image_header(header);

	/* convert size to sectors - round up */
T
Tom Rini 已提交
38 39
	image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
				mmc->read_bl_len;
S
Simon Schwarz 已提交
40 41

	/* Read the header too to avoid extra memcpy */
42 43
	err = mmc->block_dev.block_read(0, sector, image_size_sectors,
					(void *)spl_image.load_addr);
S
Simon Schwarz 已提交
44 45

end:
46
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
47
	if (err == 0)
48
		printf("spl: mmc blk read err - %lu\n", err);
49
#endif
50 51

	return (err == 0);
S
Simon Schwarz 已提交
52 53
}

54 55 56 57 58 59 60
#ifdef CONFIG_SPL_OS_BOOT
static int mmc_load_image_raw_os(struct mmc *mmc)
{
	if (!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)) {
61
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
62
		printf("mmc args blk read error\n");
63
#endif
64 65 66 67 68 69 70
		return -1;
	}

	return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
}
#endif

S
Simon Schwarz 已提交
71 72 73 74 75 76 77 78 79 80
void spl_mmc_load_image(void)
{
	struct mmc *mmc;
	int err;
	u32 boot_mode;

	mmc_initialize(gd->bd);
	/* We register only one device. So, the dev id is always 0 */
	mmc = find_mmc_device(0);
	if (!mmc) {
81
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
S
Simon Schwarz 已提交
82
		puts("spl: mmc device not found!!\n");
83
#endif
S
Simon Schwarz 已提交
84 85 86 87 88
		hang();
	}

	err = mmc_init(mmc);
	if (err) {
89
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
S
Simon Schwarz 已提交
90
		printf("spl: mmc init failed: err - %d\n", err);
91
#endif
S
Simon Schwarz 已提交
92 93
		hang();
	}
94

95
	boot_mode = spl_boot_mode();
S
Simon Schwarz 已提交
96 97
	if (boot_mode == MMCSD_MODE_RAW) {
		debug("boot mode - RAW\n");
98 99 100
#ifdef CONFIG_SPL_OS_BOOT
		if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
#endif
101
		err = mmc_load_image_raw(mmc,
102
			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
G
Guillaume GARDET 已提交
103
#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
104
	} else if (boot_mode == MMCSD_MODE_FS) {
G
Guillaume GARDET 已提交
105 106
		debug("boot mode - FS\n");
#ifdef CONFIG_SPL_FAT_SUPPORT
107
#ifdef CONFIG_SPL_OS_BOOT
108
		if (spl_start_uboot() || spl_load_image_fat_os(&mmc->block_dev,
109
								CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
110
#endif
111
		err = spl_load_image_fat(&mmc->block_dev,
112
					CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
113
					CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
G
Guillaume GARDET 已提交
114 115 116 117 118 119
		if(err)
#endif /* CONFIG_SPL_FAT_SUPPORT */
		{
#ifdef CONFIG_SPL_EXT_SUPPORT
#ifdef CONFIG_SPL_OS_BOOT
		if (spl_start_uboot() || spl_load_image_ext_os(&mmc->block_dev,
120
								CONFIG_SYS_MMCSD_FS_BOOT_PARTITION))
121
#endif
G
Guillaume GARDET 已提交
122
		err = spl_load_image_ext(&mmc->block_dev,
123
					CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
G
Guillaume GARDET 已提交
124 125 126 127
					CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
#endif /* CONFIG_SPL_EXT_SUPPORT */
		}
#endif /* defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) */
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#ifdef CONFIG_SUPPORT_EMMC_BOOT
	} else if (boot_mode == 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).
		 */
		int part = (mmc->part_config >> 3) & PART_ACCESS_MASK;

		if (part == 7)
			part = 0;

		if (mmc_switch_part(0, part)) {
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
			puts("MMC partition switch failed\n");
#endif
			hang();
		}
#ifdef CONFIG_SPL_OS_BOOT
		if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
#endif
		err = mmc_load_image_raw(mmc,
			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
151
#endif
S
Simon Schwarz 已提交
152
	} else {
153
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
S
Simon Schwarz 已提交
154
		puts("spl: wrong MMC boot mode\n");
155
#endif
S
Simon Schwarz 已提交
156 157
		hang();
	}
158 159 160

	if (err)
		hang();
S
Simon Schwarz 已提交
161
}