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 void mmc_spl_load_image(uint32_t offs
, unsigned int size
, void *vdst
)
25 uint blk_start
, blk_cnt
, err
;
27 struct mmc
*mmc
= find_mmc_device(0);
29 puts("spl: mmc device not found!!\n");
34 puts("MMC init failed\n");
38 blk_start
= ALIGN(offs
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
39 blk_cnt
= ALIGN(size
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
41 err
= mmc
->block_dev
.block_read(&mmc
->block_dev
, blk_start
, blk_cnt
,
44 puts("spl: mmc read failed!!\n");
50 * The main entry for mmc booting. It's necessary that SDRAM is already
51 * configured and available since this code loads the main U-Boot image
52 * from mmc into SDRAM and starts it from there.
55 void __noreturn
mmc_boot(void)
57 __attribute__((noreturn
)) void (*uboot
)(void);
58 uint blk_start
, blk_cnt
, err
;
59 #ifndef CONFIG_FSL_CORENET
68 mmc
= find_mmc_device(0);
70 puts("spl: mmc device not found!!\n");
74 #ifdef CONFIG_FSL_CORENET
75 offset
= CONFIG_SYS_MMC_U_BOOT_OFFS
;
76 code_len
= CONFIG_SYS_MMC_U_BOOT_SIZE
;
78 blklen
= mmc
->read_bl_len
;
79 tmp_buf
= malloc(blklen
);
81 puts("spl: malloc memory failed!!\n");
84 memset(tmp_buf
, 0, blklen
);
87 * Read source addr from sd card
89 err
= mmc
->block_dev
.block_read(&mmc
->block_dev
,
90 CONFIG_CFG_DATA_SECTOR
, 1, tmp_buf
);
92 puts("spl: mmc read failed!!\n");
97 val
= *(tmp_buf
+ MBRDBR_BOOT_SIG_55
);
99 puts("spl: mmc signature is not valid!!\n");
103 val
= *(tmp_buf
+ MBRDBR_BOOT_SIG_AA
);
105 puts("spl: mmc signature is not valid!!\n");
112 for (i
= 0; i
< byte_num
; i
++) {
113 val
= *(tmp_buf
+ ESDHC_BOOT_IMAGE_ADDR
+ i
);
114 offset
= (offset
<< 8) + val
;
116 offset
+= CONFIG_SYS_MMC_U_BOOT_OFFS
;
117 /* Get the code size from offset 0x48 */
120 for (i
= 0; i
< byte_num
; i
++) {
121 val
= *(tmp_buf
+ ESDHC_BOOT_IMAGE_SIZE
+ i
);
122 code_len
= (code_len
<< 8) + val
;
124 code_len
-= CONFIG_SYS_MMC_U_BOOT_OFFS
;
126 * Load U-Boot image from mmc into RAM
129 blk_start
= ALIGN(offset
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
130 blk_cnt
= ALIGN(code_len
, mmc
->read_bl_len
) / mmc
->read_bl_len
;
131 err
= mmc
->block_dev
.block_read(&mmc
->block_dev
, blk_start
, blk_cnt
,
132 (uchar
*)CONFIG_SYS_MMC_U_BOOT_DST
);
133 if (err
!= blk_cnt
) {
134 puts("spl: mmc read failed!!\n");
135 #ifndef CONFIG_FSL_CORENET
142 * Clean d-cache and invalidate i-cache, to
143 * make sure that no stale data is executed.
145 flush_cache(CONFIG_SYS_MMC_U_BOOT_DST
, CONFIG_SYS_MMC_U_BOOT_SIZE
);
148 * Jump to U-Boot image
150 uboot
= (void *)CONFIG_SYS_MMC_U_BOOT_START
;