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