spl_mmc.c 4.8 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>
11
#include <linux/compiler.h>
S
Simon Schwarz 已提交
12 13
#include <asm/u-boot.h>
#include <mmc.h>
14
#include <image.h>
S
Simon Schwarz 已提交
15 16 17

DECLARE_GLOBAL_DATA_PTR;

18
static int mmc_load_image_raw_sector(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

	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
25
					 sizeof(struct image_header));
S
Simon Schwarz 已提交
26 27

	/* 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
	image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
39
			     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 block read error\n");
49
#endif
50 51

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

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#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

70 71 72
#ifdef CONFIG_SPL_OS_BOOT
static int mmc_load_image_raw_os(struct mmc *mmc)
{
73 74 75 76 77 78 79
	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) {
80
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
81
		printf("spl: mmc block read error\n");
82
#endif
83 84 85
		return -1;
	}

86
	return mmc_load_image_raw_sector(mmc,
87
		CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
88 89 90
}
#endif

S
Simon Schwarz 已提交
91 92 93 94
void spl_mmc_load_image(void)
{
	struct mmc *mmc;
	u32 boot_mode;
95 96
	int err;
	__maybe_unused int part;
S
Simon Schwarz 已提交
97 98

	mmc_initialize(gd->bd);
99

S
Simon Schwarz 已提交
100 101 102
	/* We register only one device. So, the dev id is always 0 */
	mmc = find_mmc_device(0);
	if (!mmc) {
103
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
104
		puts("spl: mmc device not found\n");
105
#endif
S
Simon Schwarz 已提交
106 107 108 109 110
		hang();
	}

	err = mmc_init(mmc);
	if (err) {
111
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
112
		printf("spl: mmc init failed with error: %d\n", err);
113
#endif
S
Simon Schwarz 已提交
114 115
		hang();
	}
116

117
	boot_mode = spl_boot_mode();
118 119 120 121
	switch (boot_mode) {
	case MMCSD_MODE_RAW:
		debug("spl: mmc boot mode: raw\n");

122
#ifdef CONFIG_SPL_OS_BOOT
123 124 125 126 127
		if (!spl_start_uboot()) {
			err = mmc_load_image_raw_os(mmc);
			if (!err)
				return;
		}
128
#endif
129 130 131 132 133
#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,
134
			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
135
#endif
136 137
		if (!err)
			return;
G
Guillaume GARDET 已提交
138
#if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
139 140 141
	case MMCSD_MODE_FS:
		debug("spl: mmc boot mode: fs\n");

G
Guillaume GARDET 已提交
142
#ifdef CONFIG_SPL_FAT_SUPPORT
143
#ifdef CONFIG_SPL_OS_BOOT
144 145 146 147 148 149
		if (!spl_start_uboot()) {
			err = spl_load_image_fat_os(&mmc->block_dev,
				CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
			if (!err)
				return;
		}
150
#endif
151
		err = spl_load_image_fat(&mmc->block_dev,
152 153 154 155 156
					 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
					 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
		if (!err)
			return;
#endif
G
Guillaume GARDET 已提交
157 158
#ifdef CONFIG_SPL_EXT_SUPPORT
#ifdef CONFIG_SPL_OS_BOOT
159 160 161 162 163 164
		if (!spl_start_uboot()) {
			err = spl_load_image_ext_os(&mmc->block_dev,
				CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
			if (!err)
				return;
		}
165
#endif
G
Guillaume GARDET 已提交
166
		err = spl_load_image_ext(&mmc->block_dev,
167 168 169 170 171 172
					 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
					 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
		if (!err)
			return;
#endif
#endif
173
#ifdef CONFIG_SUPPORT_EMMC_BOOT
174
	case MMCSD_MODE_EMMCBOOT:
175 176 177 178 179
		/*
		 * 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).
		 */
180
		part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
181 182 183 184 185 186

		if (part == 7)
			part = 0;

		if (mmc_switch_part(0, part)) {
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
187
			puts("spl: mmc partition switch failed\n");
188 189 190
#endif
			hang();
		}
191

192
#ifdef CONFIG_SPL_OS_BOOT
193 194 195 196 197
		if (!spl_start_uboot()) {
			err = mmc_load_image_raw_os(mmc);
			if (!err)
				return;
		}
198
#endif
199 200 201 202
#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
203
		err = mmc_load_image_raw_sector(mmc,
204
			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
205
#endif
206 207
		if (!err)
			return;
208
#endif
209 210
	case MMCSD_MODE_UNDEFINED:
	default:
211
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
212 213 214 215
		if (err)
			puts("spl: mmc: no boot mode left to try\n");
		else
			puts("spl: mmc: wrong boot mode\n");
216
#endif
217
		hang();
218
	}
S
Simon Schwarz 已提交
219
}