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