Simon Schwarz | 9ea5c6e | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 10 | #include <spl.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 11 | #include <asm/u-boot.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 12 | #include <mmc.h> |
Simon Glass | efb2172 | 2011-10-10 08:55:19 +0000 | [diff] [blame] | 13 | #include <version.h> |
Tom Rini | e4c444b | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 14 | #include <image.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 18 | static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 19 | { |
Peter Korsgaard | 2e22210 | 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 | 9ea5c6e | 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 | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 28 | err = mmc->block_dev.block_read(0, sector, 1, header); |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 29 | if (err == 0) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 30 | goto end; |
| 31 | |
Tom Rini | e4c444b | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 32 | if (image_get_magic(header) != IH_MAGIC) |
| 33 | return -1; |
| 34 | |
Simon Schwarz | 9ea5c6e | 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 | f088125 | 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 | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 40 | |
| 41 | /* Read the header too to avoid extra memcpy */ |
Peter Korsgaard | 721931f | 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 | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 44 | |
| 45 | end: |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 46 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 47 | if (err == 0) |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 48 | printf("spl: mmc blk read err - %lu\n", err); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 49 | #endif |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 50 | |
| 51 | return (err == 0); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 54 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
| 55 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) |
| 56 | { |
| 57 | disk_partition_t info; |
| 58 | |
| 59 | if (get_partition_info(&mmc->block_dev, partition, &info)) { |
| 60 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 61 | printf("spl: partition error\n"); |
| 62 | #endif |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | return mmc_load_image_raw_sector(mmc, info.start); |
| 67 | } |
| 68 | #endif |
| 69 | |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 70 | #ifdef CONFIG_SPL_OS_BOOT |
| 71 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 72 | { |
| 73 | if (!mmc->block_dev.block_read(0, |
| 74 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 75 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 76 | (void *)CONFIG_SYS_SPL_ARGS_ADDR)) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 77 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 78 | printf("mmc args blk read error\n"); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 79 | #endif |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 80 | return -1; |
| 81 | } |
| 82 | |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 83 | return mmc_load_image_raw_sector(mmc, |
| 84 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 85 | } |
| 86 | #endif |
| 87 | |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 88 | void spl_mmc_load_image(void) |
| 89 | { |
| 90 | struct mmc *mmc; |
| 91 | int err; |
| 92 | u32 boot_mode; |
| 93 | |
| 94 | mmc_initialize(gd->bd); |
| 95 | /* We register only one device. So, the dev id is always 0 */ |
| 96 | mmc = find_mmc_device(0); |
| 97 | if (!mmc) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 98 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 99 | puts("spl: mmc device not found!!\n"); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 100 | #endif |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 101 | hang(); |
| 102 | } |
| 103 | |
| 104 | err = mmc_init(mmc); |
| 105 | if (err) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 106 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 107 | printf("spl: mmc init failed: err - %d\n", err); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 108 | #endif |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 109 | hang(); |
| 110 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 111 | |
Tom Rini | 37189a1 | 2012-08-14 09:19:44 -0700 | [diff] [blame] | 112 | boot_mode = spl_boot_mode(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 113 | if (boot_mode == MMCSD_MODE_RAW) { |
| 114 | debug("boot mode - RAW\n"); |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 115 | #ifdef CONFIG_SPL_OS_BOOT |
| 116 | if (spl_start_uboot() || mmc_load_image_raw_os(mmc)) |
| 117 | #endif |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 118 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
| 119 | err = mmc_load_image_raw_partition(mmc, |
| 120 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); |
| 121 | #else |
| 122 | err = mmc_load_image_raw_sector(mmc, |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 123 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 124 | #endif |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 125 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) |
Guillaume GARDET | 4c5bbc2 | 2014-11-18 10:44:46 +0100 | [diff] [blame] | 126 | } |
| 127 | if (err || boot_mode == MMCSD_MODE_FS) { |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 128 | debug("boot mode - FS\n"); |
| 129 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Peter Korsgaard | 7ad2cc7 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 130 | #ifdef CONFIG_SPL_OS_BOOT |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 131 | if (spl_start_uboot() || spl_load_image_fat_os(&mmc->block_dev, |
Paul Kocialkowski | e2ccdf8 | 2014-11-08 23:14:55 +0100 | [diff] [blame] | 132 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION)) |
Peter Korsgaard | 7ad2cc7 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 133 | #endif |
Dan Murphy | 773b594 | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 134 | err = spl_load_image_fat(&mmc->block_dev, |
Paul Kocialkowski | e2ccdf8 | 2014-11-08 23:14:55 +0100 | [diff] [blame] | 135 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
Guillaume GARDET | 205b4f3 | 2014-10-15 17:53:11 +0200 | [diff] [blame] | 136 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 137 | if(err) |
| 138 | #endif /* CONFIG_SPL_FAT_SUPPORT */ |
| 139 | { |
| 140 | #ifdef CONFIG_SPL_EXT_SUPPORT |
| 141 | #ifdef CONFIG_SPL_OS_BOOT |
| 142 | if (spl_start_uboot() || spl_load_image_ext_os(&mmc->block_dev, |
Paul Kocialkowski | e2ccdf8 | 2014-11-08 23:14:55 +0100 | [diff] [blame] | 143 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION)) |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 144 | #endif |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 145 | err = spl_load_image_ext(&mmc->block_dev, |
Paul Kocialkowski | e2ccdf8 | 2014-11-08 23:14:55 +0100 | [diff] [blame] | 146 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
Guillaume GARDET | 592f922 | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 147 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 148 | #endif /* CONFIG_SPL_EXT_SUPPORT */ |
| 149 | } |
| 150 | #endif /* defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) */ |
Tom Rini | 7dbe63b | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 151 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 152 | } else if (boot_mode == MMCSD_MODE_EMMCBOOT) { |
| 153 | /* |
| 154 | * We need to check what the partition is configured to. |
| 155 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 156 | * which is the first physical partition (0). |
| 157 | */ |
| 158 | int part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
| 159 | |
| 160 | if (part == 7) |
| 161 | part = 0; |
| 162 | |
| 163 | if (mmc_switch_part(0, part)) { |
| 164 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 165 | puts("MMC partition switch failed\n"); |
| 166 | #endif |
| 167 | hang(); |
| 168 | } |
| 169 | #ifdef CONFIG_SPL_OS_BOOT |
| 170 | if (spl_start_uboot() || mmc_load_image_raw_os(mmc)) |
| 171 | #endif |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 172 | err = mmc_load_image_raw_sector(mmc, |
Tom Rini | 7dbe63b | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 173 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 174 | #endif |
Guillaume GARDET | 2c84c9a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | switch(boot_mode){ |
| 178 | case MMCSD_MODE_RAW: |
| 179 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) |
| 180 | case MMCSD_MODE_FS: |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 181 | #endif |
Guillaume GARDET | 2c84c9a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 182 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
| 183 | case MMCSD_MODE_EMMCBOOT: |
| 184 | #endif |
| 185 | /* Boot mode is ok. Nothing to do. */ |
| 186 | break; |
| 187 | case MMCSD_MODE_UNDEFINED: |
| 188 | default: |
| 189 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 190 | puts("spl: wrong MMC boot mode\n"); |
| 191 | #endif |
| 192 | hang(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 193 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 194 | |
| 195 | if (err) |
| 196 | hang(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 197 | } |