xref: /openbmc/u-boot/drivers/mmc/fsl_esdhc_spl.c (revision 8aa42441)
1 /*
2  * Copyright 2013 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <mmc.h>
9 #include <malloc.h>
10 
11 /*
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.
15  */
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
21 
22 /*
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.
26  */
27 
28 void __noreturn mmc_boot(void)
29 {
30 	__attribute__((noreturn)) void (*uboot)(void);
31 	uint blk_start, blk_cnt, err;
32 	u32 blklen;
33 	uchar *tmp_buf;
34 	uchar val;
35 	uint i, byte_num;
36 	u32 offset, code_len;
37 	struct mmc *mmc;
38 
39 	mmc = find_mmc_device(0);
40 	if (!mmc) {
41 		puts("spl: mmc device not found!!\n");
42 		hang();
43 	}
44 
45 #ifdef CONFIG_FSL_CORENET
46 	offset = CONFIG_SYS_MMC_U_BOOT_OFFS;
47 	code_len = CONFIG_SYS_MMC_U_BOOT_SIZE;
48 #else
49 	blklen = mmc->read_bl_len;
50 	tmp_buf = malloc(blklen);
51 	if (!tmp_buf) {
52 		puts("spl: malloc memory failed!!\n");
53 		hang();
54 	}
55 	memset(tmp_buf, 0, blklen);
56 
57 	/*
58 	* Read source addr from sd card
59 	*/
60 	err = mmc->block_dev.block_read(0, CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
61 	if (err != 1) {
62 		puts("spl: mmc read failed!!\n");
63 		free(tmp_buf);
64 		hang();
65 	}
66 
67 	val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
68 	if (0x55 != val) {
69 		puts("spl: mmc signature is not valid!!\n");
70 		free(tmp_buf);
71 		hang();
72 	}
73 	val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
74 	if (0xAA != val) {
75 		puts("spl: mmc signature is not valid!!\n");
76 		free(tmp_buf);
77 		hang();
78 	}
79 
80 	byte_num = 4;
81 	offset = 0;
82 	for (i = 0; i < byte_num; i++) {
83 		val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
84 		offset = (offset << 8) + val;
85 	}
86 	offset += CONFIG_SYS_MMC_U_BOOT_OFFS;
87 	/* Get the code size from offset 0x48 */
88 	byte_num = 4;
89 	code_len = 0;
90 	for (i = 0; i < byte_num; i++) {
91 		val = *(tmp_buf + ESDHC_BOOT_IMAGE_SIZE + i);
92 		code_len = (code_len << 8) + val;
93 	}
94 	code_len -= CONFIG_SYS_MMC_U_BOOT_OFFS;
95 	/*
96 	* Load U-Boot image from mmc into RAM
97 	*/
98 #endif
99 	blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
100 	blk_cnt = ALIGN(code_len, mmc->read_bl_len) / mmc->read_bl_len;
101 	err = mmc->block_dev.block_read(0, blk_start, blk_cnt,
102 					(uchar *)CONFIG_SYS_MMC_U_BOOT_DST);
103 	if (err != blk_cnt) {
104 		puts("spl: mmc read failed!!\n");
105 		free(tmp_buf);
106 		hang();
107 	}
108 
109 	/*
110 	* Clean d-cache and invalidate i-cache, to
111 	* make sure that no stale data is executed.
112 	*/
113 	flush_cache(CONFIG_SYS_MMC_U_BOOT_DST, CONFIG_SYS_MMC_U_BOOT_SIZE);
114 
115 	/*
116 	* Jump to U-Boot image
117 	*/
118 	uboot = (void *)CONFIG_SYS_MMC_U_BOOT_START;
119 	(*uboot)();
120 }
121