1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2013 Freescale Semiconductor, Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <mmc.h>
9*4882a593Smuzhiyun #include <malloc.h>
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * The environment variables are written to just after the u-boot image
13*4882a593Smuzhiyun * on SDCard, so we must read the MBR to get the start address and code
14*4882a593Smuzhiyun * length of the u-boot image, then calculate the address of the env.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun #define ESDHC_BOOT_IMAGE_SIZE 0x48
17*4882a593Smuzhiyun #define ESDHC_BOOT_IMAGE_ADDR 0x50
18*4882a593Smuzhiyun #define MBRDBR_BOOT_SIG_55 0x1fe
19*4882a593Smuzhiyun #define MBRDBR_BOOT_SIG_AA 0x1ff
20*4882a593Smuzhiyun #define CONFIG_CFG_DATA_SECTOR 0
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun
mmc_spl_load_image(uint32_t offs,unsigned int size,void * vdst)23*4882a593Smuzhiyun void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun uint blk_start, blk_cnt, err;
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun struct mmc *mmc = find_mmc_device(0);
28*4882a593Smuzhiyun if (!mmc) {
29*4882a593Smuzhiyun puts("spl: mmc device not found!!\n");
30*4882a593Smuzhiyun hang();
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun if (mmc_init(mmc)) {
34*4882a593Smuzhiyun puts("MMC init failed\n");
35*4882a593Smuzhiyun return;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun blk_start = ALIGN(offs, mmc->read_bl_len) / mmc->read_bl_len;
39*4882a593Smuzhiyun blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt,
42*4882a593Smuzhiyun vdst);
43*4882a593Smuzhiyun if (err != blk_cnt) {
44*4882a593Smuzhiyun puts("spl: mmc read failed!!\n");
45*4882a593Smuzhiyun hang();
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun * The main entry for mmc booting. It's necessary that SDRAM is already
51*4882a593Smuzhiyun * configured and available since this code loads the main U-Boot image
52*4882a593Smuzhiyun * from mmc into SDRAM and starts it from there.
53*4882a593Smuzhiyun */
54*4882a593Smuzhiyun
mmc_boot(void)55*4882a593Smuzhiyun void __noreturn mmc_boot(void)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun __attribute__((noreturn)) void (*uboot)(void);
58*4882a593Smuzhiyun uint blk_start, blk_cnt, err;
59*4882a593Smuzhiyun #ifndef CONFIG_FSL_CORENET
60*4882a593Smuzhiyun uchar *tmp_buf;
61*4882a593Smuzhiyun u32 blklen;
62*4882a593Smuzhiyun uchar val;
63*4882a593Smuzhiyun uint i, byte_num;
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun u32 offset, code_len;
66*4882a593Smuzhiyun struct mmc *mmc;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun mmc = find_mmc_device(0);
69*4882a593Smuzhiyun if (!mmc) {
70*4882a593Smuzhiyun puts("spl: mmc device not found!!\n");
71*4882a593Smuzhiyun hang();
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun #ifdef CONFIG_FSL_CORENET
75*4882a593Smuzhiyun offset = CONFIG_SYS_MMC_U_BOOT_OFFS;
76*4882a593Smuzhiyun code_len = CONFIG_SYS_MMC_U_BOOT_SIZE;
77*4882a593Smuzhiyun #else
78*4882a593Smuzhiyun blklen = mmc->read_bl_len;
79*4882a593Smuzhiyun tmp_buf = malloc(blklen);
80*4882a593Smuzhiyun if (!tmp_buf) {
81*4882a593Smuzhiyun puts("spl: malloc memory failed!!\n");
82*4882a593Smuzhiyun hang();
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun memset(tmp_buf, 0, blklen);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * Read source addr from sd card
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun err = mmc->block_dev.block_read(&mmc->block_dev,
90*4882a593Smuzhiyun CONFIG_CFG_DATA_SECTOR, 1, tmp_buf);
91*4882a593Smuzhiyun if (err != 1) {
92*4882a593Smuzhiyun puts("spl: mmc read failed!!\n");
93*4882a593Smuzhiyun free(tmp_buf);
94*4882a593Smuzhiyun hang();
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun val = *(tmp_buf + MBRDBR_BOOT_SIG_55);
98*4882a593Smuzhiyun if (0x55 != val) {
99*4882a593Smuzhiyun puts("spl: mmc signature is not valid!!\n");
100*4882a593Smuzhiyun free(tmp_buf);
101*4882a593Smuzhiyun hang();
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun val = *(tmp_buf + MBRDBR_BOOT_SIG_AA);
104*4882a593Smuzhiyun if (0xAA != val) {
105*4882a593Smuzhiyun puts("spl: mmc signature is not valid!!\n");
106*4882a593Smuzhiyun free(tmp_buf);
107*4882a593Smuzhiyun hang();
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun byte_num = 4;
111*4882a593Smuzhiyun offset = 0;
112*4882a593Smuzhiyun for (i = 0; i < byte_num; i++) {
113*4882a593Smuzhiyun val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i);
114*4882a593Smuzhiyun offset = (offset << 8) + val;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun offset += CONFIG_SYS_MMC_U_BOOT_OFFS;
117*4882a593Smuzhiyun /* Get the code size from offset 0x48 */
118*4882a593Smuzhiyun byte_num = 4;
119*4882a593Smuzhiyun code_len = 0;
120*4882a593Smuzhiyun for (i = 0; i < byte_num; i++) {
121*4882a593Smuzhiyun val = *(tmp_buf + ESDHC_BOOT_IMAGE_SIZE + i);
122*4882a593Smuzhiyun code_len = (code_len << 8) + val;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun code_len -= CONFIG_SYS_MMC_U_BOOT_OFFS;
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun * Load U-Boot image from mmc into RAM
127*4882a593Smuzhiyun */
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
130*4882a593Smuzhiyun blk_cnt = ALIGN(code_len, mmc->read_bl_len) / mmc->read_bl_len;
131*4882a593Smuzhiyun err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt,
132*4882a593Smuzhiyun (uchar *)CONFIG_SYS_MMC_U_BOOT_DST);
133*4882a593Smuzhiyun if (err != blk_cnt) {
134*4882a593Smuzhiyun puts("spl: mmc read failed!!\n");
135*4882a593Smuzhiyun #ifndef CONFIG_FSL_CORENET
136*4882a593Smuzhiyun free(tmp_buf);
137*4882a593Smuzhiyun #endif
138*4882a593Smuzhiyun hang();
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * Clean d-cache and invalidate i-cache, to
143*4882a593Smuzhiyun * make sure that no stale data is executed.
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun flush_cache(CONFIG_SYS_MMC_U_BOOT_DST, CONFIG_SYS_MMC_U_BOOT_SIZE);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun * Jump to U-Boot image
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun uboot = (void *)CONFIG_SYS_MMC_U_BOOT_START;
151*4882a593Smuzhiyun (*uboot)();
152*4882a593Smuzhiyun }
153