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> |
Simon Glass | f22851a | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 11 | #include <spl.h> |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 13 | #include <errno.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 14 | #include <asm/u-boot.h> |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 16 | #include <mmc.h> |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 17 | #include <image.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 21 | static int mmc_load_legacy(struct mmc *mmc, ulong sector, |
| 22 | struct image_header *header) |
| 23 | { |
| 24 | u32 image_size_sectors; |
| 25 | unsigned long count; |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 26 | int ret; |
| 27 | |
| 28 | ret = spl_parse_image_header(header); |
| 29 | if (ret) |
| 30 | return ret; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 31 | |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 32 | /* convert size to sectors - round up */ |
| 33 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
| 34 | mmc->read_bl_len; |
| 35 | |
| 36 | /* Read the header too to avoid extra memcpy */ |
| 37 | count = mmc->block_dev.block_read(&mmc->block_dev, sector, |
| 38 | image_size_sectors, |
| 39 | (void *)(ulong)spl_image.load_addr); |
| 40 | debug("read %x sectors to %x\n", image_size_sectors, |
| 41 | spl_image.load_addr); |
| 42 | if (count != image_size_sectors) |
| 43 | return -EIO; |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 48 | static ulong h_spl_load_read(struct spl_load_info *load, ulong sector, |
| 49 | ulong count, void *buf) |
| 50 | { |
| 51 | struct mmc *mmc = load->dev; |
| 52 | |
| 53 | return mmc->block_dev.block_read(&mmc->block_dev, sector, count, buf); |
| 54 | } |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 55 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 56 | static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 57 | { |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 58 | unsigned long count; |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 59 | struct image_header *header; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 60 | int ret = 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 61 | |
| 62 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 63 | sizeof(struct image_header)); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 64 | |
| 65 | /* read image header to find the image size & load address */ |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 66 | count = mmc->block_dev.block_read(&mmc->block_dev, sector, 1, header); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 67 | debug("hdr read sector %lx, count=%lu\n", sector, count); |
| 68 | if (count == 0) { |
| 69 | ret = -EIO; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 70 | goto end; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 71 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 72 | |
Masahiro Yamada | 802f1c3 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 73 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 74 | image_get_magic(header) == FDT_MAGIC) { |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 75 | struct spl_load_info load; |
| 76 | |
| 77 | debug("Found FIT\n"); |
| 78 | load.dev = mmc; |
| 79 | load.priv = NULL; |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 80 | load.filename = NULL; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 81 | load.bl_len = mmc->read_bl_len; |
| 82 | load.read = h_spl_load_read; |
| 83 | ret = spl_load_simple_fit(&load, sector, header); |
Masahiro Yamada | 802f1c3 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 84 | } else { |
| 85 | ret = mmc_load_legacy(mmc, sector, header); |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame] | 86 | } |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 87 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 88 | end: |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 89 | if (ret) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 90 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 37342ab | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 91 | puts("mmc_load_image_raw_sector: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 92 | #endif |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 93 | return -1; |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 94 | } |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 95 | |
| 96 | return 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 99 | int spl_mmc_get_device_index(u32 boot_device) |
| 100 | { |
| 101 | switch (boot_device) { |
| 102 | case BOOT_DEVICE_MMC1: |
| 103 | return 0; |
| 104 | case BOOT_DEVICE_MMC2: |
| 105 | case BOOT_DEVICE_MMC2_2: |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 110 | printf("spl: unsupported mmc boot device.\n"); |
| 111 | #endif |
| 112 | |
| 113 | return -ENODEV; |
| 114 | } |
| 115 | |
Simon Glass | 4faf63b | 2015-12-02 10:59:12 -0700 | [diff] [blame] | 116 | static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device) |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 117 | { |
Simon Glass | 51ace32 | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 118 | #ifdef CONFIG_DM_MMC |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 119 | struct udevice *dev; |
Simon Glass | 51ace32 | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 120 | #endif |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 121 | int err, mmc_dev; |
| 122 | |
| 123 | mmc_dev = spl_mmc_get_device_index(boot_device); |
| 124 | if (mmc_dev < 0) |
| 125 | return mmc_dev; |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 126 | |
| 127 | err = mmc_initialize(NULL); |
| 128 | if (err) { |
| 129 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 130 | printf("spl: could not initialize mmc. error: %d\n", err); |
| 131 | #endif |
| 132 | return err; |
| 133 | } |
| 134 | |
Simon Glass | 51ace32 | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 135 | #ifdef CONFIG_DM_MMC |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 136 | err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev); |
Simon Glass | 51ace32 | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 137 | if (!err) |
| 138 | *mmcp = mmc_get_mmc_dev(dev); |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 139 | #else |
Simon Glass | 51ace32 | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 140 | *mmcp = find_mmc_device(mmc_dev); |
| 141 | err = *mmcp ? 0 : -ENODEV; |
| 142 | #endif |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 143 | if (err) { |
| 144 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Simon Glass | 51ace32 | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 145 | printf("spl: could not find mmc device. error: %d\n", err); |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 146 | #endif |
| 147 | return err; |
| 148 | } |
| 149 | |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 150 | return 0; |
| 151 | } |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 152 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 153 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
| 154 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) |
| 155 | { |
| 156 | disk_partition_t info; |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 157 | int err; |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 158 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 159 | err = part_get_info(&mmc->block_dev, partition, &info); |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 160 | if (err) { |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 161 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 162 | puts("spl: partition error\n"); |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 163 | #endif |
| 164 | return -1; |
| 165 | } |
| 166 | |
Stefan Roese | d0503cd | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 167 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR |
| 168 | return mmc_load_image_raw_sector(mmc, info.start + |
| 169 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 170 | #else |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 171 | return mmc_load_image_raw_sector(mmc, info.start); |
Stefan Roese | d0503cd | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 172 | #endif |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 173 | } |
Nikita Kiryanov | 011cd8f | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 174 | #else |
| 175 | #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1 |
| 176 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) |
| 177 | { |
| 178 | return -ENOSYS; |
| 179 | } |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 180 | #endif |
| 181 | |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 182 | #ifdef CONFIG_SPL_OS_BOOT |
| 183 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 184 | { |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 185 | unsigned long count; |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 186 | int ret; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 187 | |
Stephen Warren | e73f296 | 2015-12-07 11:38:48 -0700 | [diff] [blame] | 188 | count = mmc->block_dev.block_read(&mmc->block_dev, |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 189 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 190 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 191 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); |
| 192 | if (count == 0) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 193 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 37342ab | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 194 | puts("mmc_load_image_raw_os: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 195 | #endif |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 199 | ret = mmc_load_image_raw_sector(mmc, |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 200 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 201 | if (ret) |
| 202 | return ret; |
| 203 | |
| 204 | if (spl_image.os != IH_OS_LINUX) { |
| 205 | puts("Expected Linux image is not found. Trying to start U-boot\n"); |
| 206 | return -ENOENT; |
| 207 | } |
| 208 | |
| 209 | return 0; |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 210 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 211 | #else |
| 212 | int spl_start_uboot(void) |
| 213 | { |
| 214 | return 1; |
| 215 | } |
| 216 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 217 | { |
| 218 | return -ENOSYS; |
| 219 | } |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 220 | #endif |
| 221 | |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 222 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
| 223 | int spl_mmc_do_fs_boot(struct mmc *mmc) |
| 224 | { |
| 225 | int err = -ENOSYS; |
| 226 | |
| 227 | #ifdef CONFIG_SPL_FAT_SUPPORT |
| 228 | if (!spl_start_uboot()) { |
| 229 | err = spl_load_image_fat_os(&mmc->block_dev, |
| 230 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 231 | if (!err) |
| 232 | return err; |
| 233 | } |
| 234 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
| 235 | err = spl_load_image_fat(&mmc->block_dev, |
| 236 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 237 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 238 | if (!err) |
| 239 | return err; |
| 240 | #endif |
| 241 | #endif |
| 242 | #ifdef CONFIG_SPL_EXT_SUPPORT |
| 243 | if (!spl_start_uboot()) { |
| 244 | err = spl_load_image_ext_os(&mmc->block_dev, |
| 245 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 246 | if (!err) |
| 247 | return err; |
| 248 | } |
| 249 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
| 250 | err = spl_load_image_ext(&mmc->block_dev, |
| 251 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 252 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 253 | if (!err) |
| 254 | return err; |
| 255 | #endif |
| 256 | #endif |
| 257 | |
| 258 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) |
| 259 | err = -ENOENT; |
| 260 | #endif |
| 261 | |
| 262 | return err; |
| 263 | } |
| 264 | #else |
| 265 | int spl_mmc_do_fs_boot(struct mmc *mmc) |
| 266 | { |
| 267 | return -ENOSYS; |
| 268 | } |
| 269 | #endif |
| 270 | |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 271 | int spl_mmc_load_image(u32 boot_device) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 272 | { |
Simon Glass | a2b771c | 2015-12-02 10:59:11 -0700 | [diff] [blame] | 273 | struct mmc *mmc = NULL; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 274 | u32 boot_mode; |
Simon Glass | f22851a | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 275 | int err = 0; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 276 | __maybe_unused int part; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 277 | |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 278 | err = spl_mmc_find_device(&mmc, boot_device); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 279 | if (err) |
| 280 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 281 | |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 282 | err = mmc_init(mmc); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 283 | if (err) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 284 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 285 | printf("spl: mmc init failed with error: %d\n", err); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 286 | #endif |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 287 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 288 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 289 | |
Tom Rini | a76ff95 | 2012-08-14 09:19:44 -0700 | [diff] [blame] | 290 | boot_mode = spl_boot_mode(); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 291 | err = -EINVAL; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 292 | switch (boot_mode) { |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 293 | case MMCSD_MODE_EMMCBOOT: |
| 294 | /* |
| 295 | * We need to check what the partition is configured to. |
| 296 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 297 | * which is the first physical partition (0). |
| 298 | */ |
| 299 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
| 300 | |
| 301 | if (part == 7) |
| 302 | part = 0; |
| 303 | |
Simon Glass | 11f2bb6 | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 304 | err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 305 | if (err) { |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 306 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 307 | puts("spl: mmc partition switch failed\n"); |
| 308 | #endif |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 309 | return err; |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 310 | } |
| 311 | /* Fall through */ |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 312 | case MMCSD_MODE_RAW: |
| 313 | debug("spl: mmc boot mode: raw\n"); |
| 314 | |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 315 | if (!spl_start_uboot()) { |
| 316 | err = mmc_load_image_raw_os(mmc); |
| 317 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 318 | return err; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 319 | } |
Nikita Kiryanov | 011cd8f | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 320 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 321 | err = mmc_load_image_raw_partition(mmc, |
| 322 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 323 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 324 | return err; |
Nikita Kiryanov | 011cd8f | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 325 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 326 | err = mmc_load_image_raw_sector(mmc, |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 327 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 328 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 329 | return err; |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 330 | #endif |
Guillaume GARDET | 834dd88 | 2016-02-18 18:17:36 +0100 | [diff] [blame] | 331 | /* If RAW mode fails, try FS mode. */ |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 332 | case MMCSD_MODE_FS: |
| 333 | debug("spl: mmc boot mode: fs\n"); |
| 334 | |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 335 | err = spl_mmc_do_fs_boot(mmc); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 336 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 337 | return err; |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 338 | |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 339 | break; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 340 | case MMCSD_MODE_UNDEFINED: |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 341 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 342 | default: |
| 343 | puts("spl: mmc: wrong boot mode\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 344 | #endif |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 345 | } |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 346 | |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 347 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 348 | } |