2 * Copyright 2013 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
12 * The environment variables are written to just after the u-boot image
13 * on SDCard, so we must read the MBR to get the start address and code
14 * length of the u-boot image, then calculate the address of the env.
16 #define ESDHC_BOOT_IMAGE_SIZE 0x48
17 #define ESDHC_BOOT_IMAGE_ADDR 0x50
18 #define MBRDBR_BOOT_SIG_55 0x1fe
19 #define MBRDBR_BOOT_SIG_AA 0x1ff
20 #define CONFIG_CFG_DATA_SECTOR 0
23 * The main entry for mmc booting. It's necessary that SDRAM is already
24 * configured and available since this code loads the main U-Boot image
25 * from mmc into SDRAM and starts it from there.
28 void __noreturn mmc_boot(void)
30 __attribute__((noreturn)) void (*uboot)(void);
31 uint blk_start, blk_cnt, err;
39 mmc = find_mmc_device(0);
41 puts("spl: mmc device not found!!\n");
45 blklen = mmc->read_bl_len;
46 tmp_buf = malloc(blklen);
48 puts("spl: malloc memory failed!!\n");
51 memset(tmp_buf, 0, blklen);
54 * Read source addr from sd card
56 err = mmc->block_dev.block_read(0, CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
58 puts("spl: mmc read failed!!\n");
63 val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
65 puts("spl: mmc signature is not valid!!\n");
69 val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
71 puts("spl: mmc signature is not valid!!\n");
78 for (i = 0; i < byte_num; i++) {
79 val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
80 offset = (offset << 8) + val;
82 offset += CONFIG_SYS_MMC_U_BOOT_OFFS;
83 /* Get the code size from offset 0x48 */
86 for (i = 0; i < byte_num; i++) {
87 val = *(tmp_buf + ESDHC_BOOT_IMAGE_SIZE + i);
88 code_len = (code_len << 8) + val;
90 code_len -= CONFIG_SYS_MMC_U_BOOT_OFFS;
92 * Load U-Boot image from mmc into RAM
94 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
95 blk_cnt = ALIGN(code_len, mmc->read_bl_len) / mmc->read_bl_len;
96 err = mmc->block_dev.block_read(0, blk_start, blk_cnt,
97 (uchar *)CONFIG_SYS_MMC_U_BOOT_DST);
99 puts("spl: mmc read failed!!\n");
105 * Clean d-cache and invalidate i-cache, to
106 * make sure that no stale data is executed.
108 flush_cache(CONFIG_SYS_MMC_U_BOOT_DST, CONFIG_SYS_MMC_U_BOOT_SIZE);
111 * Jump to U-Boot image
113 uboot = (void *)CONFIG_SYS_MMC_U_BOOT_START;