Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010 |
| 3 | * Texas Instruments, <www.ti.com> |
| 4 | * |
| 5 | * Aneesh V <aneesh@ti.com> |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 10 | #include <spl.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 11 | #include <asm/u-boot.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 12 | #include <mmc.h> |
Simon Glass | 5040241 | 2011-10-10 08:55:19 +0000 | [diff] [blame] | 13 | #include <version.h> |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 14 | #include <image.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Peter Korsgaard | 15af8f5 | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 18 | static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 19 | { |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 20 | unsigned long err; |
| 21 | u32 image_size_sectors; |
| 22 | struct image_header *header; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 23 | |
| 24 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 25 | sizeof(struct image_header)); |
| 26 | |
| 27 | /* read image header to find the image size & load address */ |
Peter Korsgaard | 15af8f5 | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 28 | err = mmc->block_dev.block_read(0, sector, 1, header); |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 29 | if (err == 0) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 30 | goto end; |
| 31 | |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 32 | if (image_get_magic(header) != IH_MAGIC) |
| 33 | return -1; |
| 34 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 35 | spl_parse_image_header(header); |
| 36 | |
| 37 | /* convert size to sectors - round up */ |
Tom Rini | a0b9fa5 | 2012-08-14 10:25:15 -0700 | [diff] [blame] | 38 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
| 39 | mmc->read_bl_len; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 40 | |
| 41 | /* Read the header too to avoid extra memcpy */ |
Peter Korsgaard | 15af8f5 | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 42 | err = mmc->block_dev.block_read(0, sector, image_size_sectors, |
| 43 | (void *)spl_image.load_addr); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 44 | |
| 45 | end: |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 46 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 47 | if (err == 0) |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 48 | printf("spl: mmc blk read err - %lu\n", err); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 49 | #endif |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 50 | |
| 51 | return (err == 0); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 54 | #ifdef CONFIG_SPL_OS_BOOT |
| 55 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 56 | { |
| 57 | if (!mmc->block_dev.block_read(0, |
| 58 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 59 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 60 | (void *)CONFIG_SYS_SPL_ARGS_ADDR)) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 61 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 62 | printf("mmc args blk read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 63 | #endif |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
| 68 | } |
| 69 | #endif |
| 70 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 71 | void spl_mmc_load_image(void) |
| 72 | { |
| 73 | struct mmc *mmc; |
| 74 | int err; |
| 75 | u32 boot_mode; |
| 76 | |
| 77 | mmc_initialize(gd->bd); |
| 78 | /* We register only one device. So, the dev id is always 0 */ |
| 79 | mmc = find_mmc_device(0); |
| 80 | if (!mmc) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 81 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 82 | puts("spl: mmc device not found!!\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 83 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 84 | hang(); |
| 85 | } |
| 86 | |
| 87 | err = mmc_init(mmc); |
| 88 | if (err) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 89 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 90 | printf("spl: mmc init failed: err - %d\n", err); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 91 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 92 | hang(); |
| 93 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 94 | |
Tom Rini | a76ff95 | 2012-08-14 09:19:44 -0700 | [diff] [blame] | 95 | boot_mode = spl_boot_mode(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 96 | if (boot_mode == MMCSD_MODE_RAW) { |
| 97 | debug("boot mode - RAW\n"); |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 98 | #ifdef CONFIG_SPL_OS_BOOT |
| 99 | if (spl_start_uboot() || mmc_load_image_raw_os(mmc)) |
| 100 | #endif |
Peter Korsgaard | 15af8f5 | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 101 | err = mmc_load_image_raw(mmc, |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 102 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 103 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 104 | } else if (boot_mode == MMCSD_MODE_FS) { |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 105 | debug("boot mode - FS\n"); |
| 106 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Peter Korsgaard | 465f1f8 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 107 | #ifdef CONFIG_SPL_OS_BOOT |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 108 | if (spl_start_uboot() || spl_load_image_fat_os(&mmc->block_dev, |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 109 | CONFIG_SYS_MMC_SD_FS_BOOT_PARTITION)) |
Peter Korsgaard | 465f1f8 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 110 | #endif |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 111 | err = spl_load_image_fat(&mmc->block_dev, |
Guillaume GARDET | 602a16c | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 112 | CONFIG_SYS_MMC_SD_FS_BOOT_PARTITION, |
| 113 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 114 | if(err) |
| 115 | #endif /* CONFIG_SPL_FAT_SUPPORT */ |
| 116 | { |
| 117 | #ifdef CONFIG_SPL_EXT_SUPPORT |
| 118 | #ifdef CONFIG_SPL_OS_BOOT |
| 119 | if (spl_start_uboot() || spl_load_image_ext_os(&mmc->block_dev, |
| 120 | CONFIG_SYS_MMC_SD_FS_BOOT_PARTITION)) |
Tom Rini | a0d8cca | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 121 | #endif |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 122 | err = spl_load_image_ext(&mmc->block_dev, |
| 123 | CONFIG_SYS_MMC_SD_FS_BOOT_PARTITION, |
| 124 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 125 | #endif /* CONFIG_SPL_EXT_SUPPORT */ |
| 126 | } |
| 127 | #endif /* defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) */ |
Tom Rini | cc61269 | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 128 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 129 | } else if (boot_mode == MMCSD_MODE_EMMCBOOT) { |
| 130 | /* |
| 131 | * We need to check what the partition is configured to. |
| 132 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 133 | * which is the first physical partition (0). |
| 134 | */ |
| 135 | int part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
| 136 | |
| 137 | if (part == 7) |
| 138 | part = 0; |
| 139 | |
| 140 | if (mmc_switch_part(0, part)) { |
| 141 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 142 | puts("MMC partition switch failed\n"); |
| 143 | #endif |
| 144 | hang(); |
| 145 | } |
| 146 | #ifdef CONFIG_SPL_OS_BOOT |
| 147 | if (spl_start_uboot() || mmc_load_image_raw_os(mmc)) |
| 148 | #endif |
| 149 | err = mmc_load_image_raw(mmc, |
| 150 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 151 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 152 | } else { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 153 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 154 | puts("spl: wrong MMC boot mode\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 155 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 156 | hang(); |
| 157 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 158 | |
| 159 | if (err) |
| 160 | hang(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 161 | } |