Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jerry Huang | 1ac63e4 | 2011-01-24 17:09:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2011 Freescale Semiconductor, Inc. |
Jerry Huang | 1ac63e4 | 2011-01-24 17:09:54 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <mmc.h> |
| 8 | #include <malloc.h> |
| 9 | |
| 10 | /* |
| 11 | * The environment variables are written to just after the u-boot image |
| 12 | * on SDCard, so we must read the MBR to get the start address and code |
| 13 | * length of the u-boot image, then calculate the address of the env. |
| 14 | */ |
| 15 | #define ESDHC_BOOT_IMAGE_SIZE 0x48 |
| 16 | #define ESDHC_BOOT_IMAGE_ADDR 0x50 |
| 17 | |
Haijun.Zhang | f28bea0 | 2014-01-10 13:52:19 +0800 | [diff] [blame] | 18 | #define ESDHC_DEFAULT_ENVADDR 0x400 |
| 19 | |
Michael Heimpold | d196bd8 | 2013-04-10 10:36:19 +0000 | [diff] [blame] | 20 | int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr) |
Jerry Huang | 1ac63e4 | 2011-01-24 17:09:54 +0000 | [diff] [blame] | 21 | { |
| 22 | u8 *tmp_buf; |
| 23 | u32 blklen, code_offset, code_len, n; |
| 24 | |
| 25 | blklen = mmc->read_bl_len; |
| 26 | tmp_buf = malloc(blklen); |
| 27 | if (!tmp_buf) |
| 28 | return 1; |
| 29 | |
| 30 | /* read out the first block, get the config data information */ |
Stephen Warren | 7c4213f | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 31 | n = mmc->block_dev.block_read(&mmc->block_dev, 0, 1, tmp_buf); |
Jerry Huang | 1ac63e4 | 2011-01-24 17:09:54 +0000 | [diff] [blame] | 32 | if (!n) { |
| 33 | free(tmp_buf); |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | /* Get the Source Address, from offset 0x50 */ |
| 38 | code_offset = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_ADDR); |
| 39 | |
| 40 | /* Get the code size from offset 0x48 */ |
| 41 | code_len = *(u32 *)(tmp_buf + ESDHC_BOOT_IMAGE_SIZE); |
| 42 | |
Haijun.Zhang | f28bea0 | 2014-01-10 13:52:19 +0800 | [diff] [blame] | 43 | #ifdef CONFIG_ESDHC_HC_BLK_ADDR |
| 44 | /* |
| 45 | * On soc BSC9131, BSC9132: |
| 46 | * In High Capacity SD Cards (> 2 GBytes), the 32-bit source address and |
| 47 | * code length of these soc specify the memory address in block address |
| 48 | * format. Block length is fixed to 512 bytes as per the SD High |
| 49 | * Capacity specification. |
| 50 | */ |
| 51 | u64 tmp; |
| 52 | |
| 53 | if (mmc->high_capacity) { |
| 54 | tmp = (u64)code_offset * blklen; |
| 55 | tmp += code_len * blklen; |
| 56 | } else |
| 57 | tmp = code_offset + code_len; |
| 58 | |
| 59 | if ((tmp + CONFIG_ENV_SIZE > mmc->capacity) || |
| 60 | (tmp > 0xFFFFFFFFU)) |
| 61 | *env_addr = ESDHC_DEFAULT_ENVADDR; |
| 62 | else |
| 63 | *env_addr = tmp; |
| 64 | |
| 65 | free(tmp_buf); |
| 66 | |
| 67 | return 0; |
| 68 | #endif |
| 69 | |
Jerry Huang | 1ac63e4 | 2011-01-24 17:09:54 +0000 | [diff] [blame] | 70 | *env_addr = code_offset + code_len; |
| 71 | |
| 72 | free(tmp_buf); |
| 73 | |
| 74 | return 0; |
| 75 | } |