1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2013 Freescale Semiconductor, Inc.
13 * The environment variables are written to just after the u-boot image
14 * on SDCard, so we must read the MBR to get the start address and code
15 * length of the u-boot image, then calculate the address of the env.
17 #define ESDHC_BOOT_IMAGE_SIZE 0x48
18 #define ESDHC_BOOT_IMAGE_ADDR 0x50
19 #define MBRDBR_BOOT_SIG_55 0x1fe
20 #define MBRDBR_BOOT_SIG_AA 0x1ff
21 #define CONFIG_CFG_DATA_SECTOR 0
24 void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
26 uint blk_start, blk_cnt, err;
28 struct mmc *mmc = find_mmc_device(0);
30 puts("spl: mmc device not found!!\n");
35 puts("MMC init failed\n");
39 blk_start = ALIGN(offs, mmc->read_bl_len) / mmc->read_bl_len;
40 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
42 err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt,
45 puts("spl: mmc read failed!!\n");
51 * The main entry for mmc booting. It's necessary that SDRAM is already
52 * configured and available since this code loads the main U-Boot image
53 * from mmc into SDRAM and starts it from there.
56 void __noreturn mmc_boot(void)
58 __attribute__((noreturn)) void (*uboot)(void);
59 uint blk_start, blk_cnt, err;
60 #ifndef CONFIG_FSL_CORENET
69 mmc = find_mmc_device(0);
71 puts("spl: mmc device not found!!\n");
75 #ifdef CONFIG_FSL_CORENET
76 offset = CONFIG_SYS_MMC_U_BOOT_OFFS;
77 code_len = CONFIG_SYS_MMC_U_BOOT_SIZE;
79 blklen = mmc->read_bl_len;
80 tmp_buf = malloc(blklen);
82 puts("spl: malloc memory failed!!\n");
85 memset(tmp_buf, 0, blklen);
88 * Read source addr from sd card
90 err = mmc->block_dev.block_read(&mmc->block_dev,
91 CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
93 puts("spl: mmc read failed!!\n");
98 val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
100 puts("spl: mmc signature is not valid!!\n");
104 val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
106 puts("spl: mmc signature is not valid!!\n");
113 for (i = 0; i < byte_num; i++) {
114 val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
115 offset = (offset << 8) + val;
117 offset += CONFIG_SYS_MMC_U_BOOT_OFFS;
118 /* Get the code size from offset 0x48 */
121 for (i = 0; i < byte_num; i++) {
122 val = *(tmp_buf + ESDHC_BOOT_IMAGE_SIZE + i);
123 code_len = (code_len << 8) + val;
125 code_len -= CONFIG_SYS_MMC_U_BOOT_OFFS;
127 * Load U-Boot image from mmc into RAM
130 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
131 blk_cnt = ALIGN(code_len, mmc->read_bl_len) / mmc->read_bl_len;
132 err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt,
133 (uchar *)CONFIG_SYS_MMC_U_BOOT_DST);
134 if (err != blk_cnt) {
135 puts("spl: mmc read failed!!\n");
136 #ifndef CONFIG_FSL_CORENET
143 * Clean d-cache and invalidate i-cache, to
144 * make sure that no stale data is executed.
146 flush_cache(CONFIG_SYS_MMC_U_BOOT_DST, CONFIG_SYS_MMC_U_BOOT_SIZE);
149 * Jump to U-Boot image
151 uboot = (void *)CONFIG_SYS_MMC_U_BOOT_START;