Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2013 Freescale Semiconductor, Inc. |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 8 | #include <hang.h> |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 9 | #include <mmc.h> |
| 10 | #include <malloc.h> |
| 11 | |
| 12 | /* |
| 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. |
| 16 | */ |
| 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 |
| 22 | |
Prabhakar Kushwaha | 9ea255a | 2014-04-08 19:13:22 +0530 | [diff] [blame] | 23 | |
| 24 | void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst) |
| 25 | { |
| 26 | uint blk_start, blk_cnt, err; |
| 27 | |
| 28 | struct mmc *mmc = find_mmc_device(0); |
| 29 | if (!mmc) { |
| 30 | puts("spl: mmc device not found!!\n"); |
| 31 | hang(); |
| 32 | } |
| 33 | |
| 34 | if (mmc_init(mmc)) { |
| 35 | puts("MMC init failed\n"); |
| 36 | return; |
| 37 | } |
| 38 | |
| 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; |
| 41 | |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 42 | err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt, |
| 43 | vdst); |
Prabhakar Kushwaha | 9ea255a | 2014-04-08 19:13:22 +0530 | [diff] [blame] | 44 | if (err != blk_cnt) { |
| 45 | puts("spl: mmc read failed!!\n"); |
| 46 | hang(); |
| 47 | } |
| 48 | } |
| 49 | |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 50 | /* |
| 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. |
| 54 | */ |
| 55 | |
| 56 | void __noreturn mmc_boot(void) |
| 57 | { |
| 58 | __attribute__((noreturn)) void (*uboot)(void); |
| 59 | uint blk_start, blk_cnt, err; |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 60 | #ifndef CONFIG_FSL_CORENET |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 61 | uchar *tmp_buf; |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 62 | u32 blklen; |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 63 | uchar val; |
| 64 | uint i, byte_num; |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 65 | #endif |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 66 | u32 offset, code_len; |
| 67 | struct mmc *mmc; |
| 68 | |
| 69 | mmc = find_mmc_device(0); |
| 70 | if (!mmc) { |
| 71 | puts("spl: mmc device not found!!\n"); |
| 72 | hang(); |
| 73 | } |
| 74 | |
Priyanka Jain | 538f5f7 | 2013-11-28 10:12:16 +0530 | [diff] [blame] | 75 | #ifdef CONFIG_FSL_CORENET |
| 76 | offset = CONFIG_SYS_MMC_U_BOOT_OFFS; |
| 77 | code_len = CONFIG_SYS_MMC_U_BOOT_SIZE; |
| 78 | #else |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 79 | blklen = mmc->read_bl_len; |
| 80 | tmp_buf = malloc(blklen); |
| 81 | if (!tmp_buf) { |
| 82 | puts("spl: malloc memory failed!!\n"); |
| 83 | hang(); |
| 84 | } |
| 85 | memset(tmp_buf, 0, blklen); |
| 86 | |
| 87 | /* |
| 88 | * Read source addr from sd card |
| 89 | */ |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 90 | err = mmc->block_dev.block_read(&mmc->block_dev, |
| 91 | CONFIG_CFG_DATA_SECTOR, 1, tmp_buf); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 92 | if (err != 1) { |
| 93 | puts("spl: mmc read failed!!\n"); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 94 | hang(); |
| 95 | } |
| 96 | |
| 97 | val = *(tmp_buf + MBRDBR_BOOT_SIG_55); |
| 98 | if (0x55 != val) { |
| 99 | puts("spl: mmc signature is not valid!!\n"); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 100 | hang(); |
| 101 | } |
| 102 | val = *(tmp_buf + MBRDBR_BOOT_SIG_AA); |
| 103 | if (0xAA != val) { |
| 104 | puts("spl: mmc signature is not valid!!\n"); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 105 | hang(); |
| 106 | } |
| 107 | |
| 108 | byte_num = 4; |
| 109 | offset = 0; |
| 110 | for (i = 0; i < byte_num; i++) { |
| 111 | val = *(tmp_buf + ESDHC_BOOT_IMAGE_ADDR + i); |
| 112 | offset = (offset << 8) + val; |
| 113 | } |
| 114 | offset += CONFIG_SYS_MMC_U_BOOT_OFFS; |
| 115 | /* Get the code size from offset 0x48 */ |
| 116 | byte_num = 4; |
| 117 | code_len = 0; |
| 118 | for (i = 0; i < byte_num; i++) { |
| 119 | val = *(tmp_buf + ESDHC_BOOT_IMAGE_SIZE + i); |
| 120 | code_len = (code_len << 8) + val; |
| 121 | } |
| 122 | code_len -= CONFIG_SYS_MMC_U_BOOT_OFFS; |
| 123 | /* |
| 124 | * Load U-Boot image from mmc into RAM |
| 125 | */ |
Priyanka Jain | 538f5f7 | 2013-11-28 10:12:16 +0530 | [diff] [blame] | 126 | #endif |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 127 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 128 | blk_cnt = ALIGN(code_len, mmc->read_bl_len) / mmc->read_bl_len; |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 129 | err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt, |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 130 | (uchar *)CONFIG_SYS_MMC_U_BOOT_DST); |
| 131 | if (err != blk_cnt) { |
| 132 | puts("spl: mmc read failed!!\n"); |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 133 | #ifndef CONFIG_FSL_CORENET |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 134 | free(tmp_buf); |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 135 | #endif |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 136 | hang(); |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Clean d-cache and invalidate i-cache, to |
| 141 | * make sure that no stale data is executed. |
| 142 | */ |
| 143 | flush_cache(CONFIG_SYS_MMC_U_BOOT_DST, CONFIG_SYS_MMC_U_BOOT_SIZE); |
| 144 | |
| 145 | /* |
| 146 | * Jump to U-Boot image |
| 147 | */ |
| 148 | uboot = (void *)CONFIG_SYS_MMC_U_BOOT_START; |
| 149 | (*uboot)(); |
| 150 | } |