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 13
#include <asm/u-boot.h>
#include <mmc.h>
#include <fat.h>
14
#include <version.h>
15
#include <image.h>
S
Simon Schwarz 已提交
16 17 18

DECLARE_GLOBAL_DATA_PTR;

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

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

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

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

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

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

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

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

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

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

	return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
}
#endif

72
#ifdef CONFIG_SPL_FAT_SUPPORT
73
static int mmc_load_image_fat(struct mmc *mmc, const char *filename)
S
Simon Schwarz 已提交
74
{
75
	int err;
S
Simon Schwarz 已提交
76 77 78 79 80
	struct image_header *header;

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

81
	err = file_fat_read(filename, header, sizeof(struct image_header));
S
Simon Schwarz 已提交
82 83 84 85 86
	if (err <= 0)
		goto end;

	spl_parse_image_header(header);

87
	err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
S
Simon Schwarz 已提交
88 89

end:
90
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
91
	if (err <= 0)
S
Simon Schwarz 已提交
92
		printf("spl: error reading image %s, err - %d\n",
93
		       filename, err);
94
#endif
95 96

	return (err <= 0);
S
Simon Schwarz 已提交
97
}
98 99 100 101 102 103 104 105 106

#ifdef CONFIG_SPL_OS_BOOT
static int mmc_load_image_fat_os(struct mmc *mmc)
{
	int err;

	err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
			    (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
	if (err <= 0) {
107
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
108 109
		printf("spl: error reading image %s, err - %d\n",
		       CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
110
#endif
111 112 113 114 115 116 117
		return -1;
	}

	return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
}
#endif

118
#endif
S
Simon Schwarz 已提交
119 120 121 122 123 124 125 126 127 128 129

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) {
130
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
S
Simon Schwarz 已提交
131
		puts("spl: mmc device not found!!\n");
132
#endif
S
Simon Schwarz 已提交
133 134 135 136 137
		hang();
	}

	err = mmc_init(mmc);
	if (err) {
138
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
S
Simon Schwarz 已提交
139
		printf("spl: mmc init failed: err - %d\n", err);
140
#endif
S
Simon Schwarz 已提交
141 142
		hang();
	}
143

144
	boot_mode = spl_boot_mode();
S
Simon Schwarz 已提交
145 146
	if (boot_mode == MMCSD_MODE_RAW) {
		debug("boot mode - RAW\n");
147 148 149
#ifdef CONFIG_SPL_OS_BOOT
		if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
#endif
150 151
		err = mmc_load_image_raw(mmc,
					 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
152
#ifdef CONFIG_SPL_FAT_SUPPORT
S
Simon Schwarz 已提交
153 154
	} else if (boot_mode == MMCSD_MODE_FAT) {
		debug("boot mode - FAT\n");
155 156 157 158

		err = fat_register_device(&mmc->block_dev,
					  CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
		if (err) {
159
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
160
			printf("spl: fat register err - %d\n", err);
161
#endif
162 163 164
			hang();
		}

165 166 167
#ifdef CONFIG_SPL_OS_BOOT
		if (spl_start_uboot() || mmc_load_image_fat_os(mmc))
#endif
168
		err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
169
#endif
S
Simon Schwarz 已提交
170
	} else {
171
#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
S
Simon Schwarz 已提交
172
		puts("spl: wrong MMC boot mode\n");
173
#endif
S
Simon Schwarz 已提交
174 175
		hang();
	}
176 177 178

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