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 | */ |
Pali Rohár | 8cff7ac | 2022-04-03 00:16:59 +0200 | [diff] [blame] | 17 | #define ESDHC_BOOT_SIGNATURE_OFF 0x40 |
| 18 | #define ESDHC_BOOT_SIGNATURE 0x424f4f54 |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 19 | #define ESDHC_BOOT_IMAGE_SIZE 0x48 |
| 20 | #define ESDHC_BOOT_IMAGE_ADDR 0x50 |
| 21 | #define MBRDBR_BOOT_SIG_55 0x1fe |
| 22 | #define MBRDBR_BOOT_SIG_AA 0x1ff |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 23 | |
Prabhakar Kushwaha | 9ea255a | 2014-04-08 19:13:22 +0530 | [diff] [blame] | 24 | |
| 25 | void mmc_spl_load_image(uint32_t offs, unsigned int size, void *vdst) |
| 26 | { |
| 27 | uint blk_start, blk_cnt, err; |
| 28 | |
| 29 | struct mmc *mmc = find_mmc_device(0); |
| 30 | if (!mmc) { |
| 31 | puts("spl: mmc device not found!!\n"); |
| 32 | hang(); |
| 33 | } |
| 34 | |
| 35 | if (mmc_init(mmc)) { |
| 36 | puts("MMC init failed\n"); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | blk_start = ALIGN(offs, mmc->read_bl_len) / mmc->read_bl_len; |
| 41 | blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len; |
| 42 | |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 43 | err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt, |
| 44 | vdst); |
Prabhakar Kushwaha | 9ea255a | 2014-04-08 19:13:22 +0530 | [diff] [blame] | 45 | if (err != blk_cnt) { |
| 46 | puts("spl: mmc read failed!!\n"); |
| 47 | hang(); |
| 48 | } |
| 49 | } |
| 50 | |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 51 | /* |
| 52 | * The main entry for mmc booting. It's necessary that SDRAM is already |
| 53 | * configured and available since this code loads the main U-Boot image |
| 54 | * from mmc into SDRAM and starts it from there. |
| 55 | */ |
| 56 | |
| 57 | void __noreturn mmc_boot(void) |
| 58 | { |
| 59 | __attribute__((noreturn)) void (*uboot)(void); |
| 60 | uint blk_start, blk_cnt, err; |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 61 | #ifndef CONFIG_FSL_CORENET |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 62 | uchar *tmp_buf; |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 63 | u32 blklen; |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 64 | u32 blk_off; |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 65 | uchar val; |
Pali Rohár | 8cff7ac | 2022-04-03 00:16:59 +0200 | [diff] [blame] | 66 | #ifndef CONFIG_SPL_FSL_PBL |
| 67 | u32 val32; |
| 68 | #endif |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 69 | uint i, byte_num; |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 70 | u32 sector; |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 71 | #endif |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 72 | u32 offset, code_len; |
| 73 | struct mmc *mmc; |
| 74 | |
| 75 | mmc = find_mmc_device(0); |
| 76 | if (!mmc) { |
| 77 | puts("spl: mmc device not found!!\n"); |
| 78 | hang(); |
| 79 | } |
| 80 | |
Pali Rohár | a897c34 | 2022-04-03 00:17:01 +0200 | [diff] [blame] | 81 | if (mmc_init(mmc)) { |
| 82 | puts("spl: mmc device init failed!\n"); |
| 83 | hang(); |
| 84 | } |
| 85 | |
Priyanka Jain | 538f5f7 | 2013-11-28 10:12:16 +0530 | [diff] [blame] | 86 | #ifdef CONFIG_FSL_CORENET |
| 87 | offset = CONFIG_SYS_MMC_U_BOOT_OFFS; |
Priyanka Jain | 538f5f7 | 2013-11-28 10:12:16 +0530 | [diff] [blame] | 88 | #else |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 89 | blklen = mmc->read_bl_len; |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 90 | if (blklen < 512) |
| 91 | blklen = 512; |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 92 | tmp_buf = malloc(blklen); |
| 93 | if (!tmp_buf) { |
| 94 | puts("spl: malloc memory failed!!\n"); |
| 95 | hang(); |
| 96 | } |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 97 | |
| 98 | sector = 0; |
| 99 | again: |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 100 | memset(tmp_buf, 0, blklen); |
| 101 | |
| 102 | /* |
| 103 | * Read source addr from sd card |
| 104 | */ |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 105 | blk_start = (sector * 512) / mmc->read_bl_len; |
| 106 | blk_off = (sector * 512) % mmc->read_bl_len; |
| 107 | blk_cnt = DIV_ROUND_UP(512, mmc->read_bl_len); |
| 108 | err = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt, tmp_buf); |
Pali Rohár | 1ee59ef | 2022-05-11 20:28:28 +0200 | [diff] [blame] | 109 | if (err != blk_cnt) { |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 110 | puts("spl: mmc read failed!!\n"); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 111 | hang(); |
| 112 | } |
| 113 | |
Pali Rohár | 8cff7ac | 2022-04-03 00:16:59 +0200 | [diff] [blame] | 114 | #ifdef CONFIG_SPL_FSL_PBL |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 115 | val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_55); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 116 | if (0x55 != val) { |
Pali Rohár | 8cff7ac | 2022-04-03 00:16:59 +0200 | [diff] [blame] | 117 | puts("spl: mmc MBR/DBR signature is not valid!!\n"); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 118 | hang(); |
| 119 | } |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 120 | val = *(tmp_buf + blk_off + MBRDBR_BOOT_SIG_AA); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 121 | if (0xAA != val) { |
Pali Rohár | 8cff7ac | 2022-04-03 00:16:59 +0200 | [diff] [blame] | 122 | puts("spl: mmc MBR/DBR signature is not valid!!\n"); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 123 | hang(); |
| 124 | } |
Pali Rohár | 8cff7ac | 2022-04-03 00:16:59 +0200 | [diff] [blame] | 125 | #else |
| 126 | /* |
| 127 | * Booting from On-Chip ROM (eSDHC or eSPI), Document Number: AN3659, Rev. 2, 06/2012. |
| 128 | * Pre-PBL BootROMs (MPC8536E, MPC8569E, P2020, P1011, P1012, P1013, P1020, P1021, P1022) |
| 129 | * require custom BOOT signature on sector 0 and MBR/DBR signature is not required at all. |
| 130 | */ |
| 131 | byte_num = 4; |
| 132 | val32 = 0; |
| 133 | for (i = 0; i < byte_num; i++) { |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 134 | val = *(tmp_buf + blk_off + ESDHC_BOOT_SIGNATURE_OFF + i); |
Pali Rohár | 8cff7ac | 2022-04-03 00:16:59 +0200 | [diff] [blame] | 135 | val32 = (val32 << 8) + val; |
| 136 | } |
| 137 | if (val32 != ESDHC_BOOT_SIGNATURE) { |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 138 | /* BOOT signature may be on the first 24 sectors (each being 512 bytes) */ |
| 139 | if (++sector < 24) |
| 140 | goto again; |
Pali Rohár | 8cff7ac | 2022-04-03 00:16:59 +0200 | [diff] [blame] | 141 | puts("spl: mmc BOOT signature is not valid!!\n"); |
| 142 | hang(); |
| 143 | } |
| 144 | #endif |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 145 | |
| 146 | byte_num = 4; |
| 147 | offset = 0; |
| 148 | for (i = 0; i < byte_num; i++) { |
Pali Rohár | eca1811 | 2022-04-04 18:33:11 +0200 | [diff] [blame] | 149 | val = *(tmp_buf + blk_off + ESDHC_BOOT_IMAGE_ADDR + i); |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 150 | offset = (offset << 8) + val; |
| 151 | } |
| 152 | offset += CONFIG_SYS_MMC_U_BOOT_OFFS; |
Pali Rohár | 2d83207 | 2022-04-03 00:17:00 +0200 | [diff] [blame] | 153 | #endif |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 154 | /* |
| 155 | * Load U-Boot image from mmc into RAM |
| 156 | */ |
Pali Rohár | 2d83207 | 2022-04-03 00:17:00 +0200 | [diff] [blame] | 157 | code_len = CONFIG_SYS_MMC_U_BOOT_SIZE; |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 158 | blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len; |
| 159 | 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] | 160 | 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] | 161 | (uchar *)CONFIG_SYS_MMC_U_BOOT_DST); |
| 162 | if (err != blk_cnt) { |
| 163 | puts("spl: mmc read failed!!\n"); |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 164 | #ifndef CONFIG_FSL_CORENET |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 165 | free(tmp_buf); |
Prabhakar Kushwaha | 568bb76 | 2014-03-31 15:32:03 +0530 | [diff] [blame] | 166 | #endif |
Ying Zhang | 9ff7026 | 2013-08-16 15:16:11 +0800 | [diff] [blame] | 167 | hang(); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Clean d-cache and invalidate i-cache, to |
| 172 | * make sure that no stale data is executed. |
| 173 | */ |
| 174 | flush_cache(CONFIG_SYS_MMC_U_BOOT_DST, CONFIG_SYS_MMC_U_BOOT_SIZE); |
| 175 | |
| 176 | /* |
| 177 | * Jump to U-Boot image |
| 178 | */ |
| 179 | uboot = (void *)CONFIG_SYS_MMC_U_BOOT_START; |
| 180 | (*uboot)(); |
| 181 | } |