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> |
Simon Glass | dc3dedf | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 11 | #include <spl.h> |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 13 | #include <errno.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 14 | #include <asm/u-boot.h> |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 16 | #include <mmc.h> |
Tom Rini | e4c444b | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 17 | #include <image.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 21 | static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc, |
| 22 | ulong sector, struct image_header *header) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 23 | { |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 24 | u32 image_size_sectors; |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 25 | unsigned long count; |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 26 | int ret; |
Tom Rini | e4c444b | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 27 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 28 | ret = spl_parse_image_header(spl_image, header); |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 29 | if (ret) |
| 30 | return ret; |
| 31 | |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 32 | /* convert size to sectors - round up */ |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 33 | image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) / |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 34 | mmc->read_bl_len; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 35 | |
| 36 | /* Read the header too to avoid extra memcpy */ |
Simon Glass | ef5609c | 2016-05-14 14:03:06 -0600 | [diff] [blame] | 37 | count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors, |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 38 | (void *)(ulong)spl_image->load_addr); |
Simon Glass | fdfa39d | 2015-06-23 15:38:47 -0600 | [diff] [blame] | 39 | debug("read %x sectors to %x\n", image_size_sectors, |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 40 | spl_image->load_addr); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 41 | if (count != image_size_sectors) |
| 42 | return -EIO; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 47 | static ulong h_spl_load_read(struct spl_load_info *load, ulong sector, |
| 48 | ulong count, void *buf) |
| 49 | { |
| 50 | struct mmc *mmc = load->dev; |
| 51 | |
Simon Glass | ef5609c | 2016-05-14 14:03:06 -0600 | [diff] [blame] | 52 | return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 53 | } |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 54 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 55 | static int mmc_load_image_raw_sector(struct spl_image_info *spl_image, |
| 56 | struct mmc *mmc, unsigned long sector) |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 57 | { |
| 58 | unsigned long count; |
| 59 | struct image_header *header; |
| 60 | int ret = 0; |
| 61 | |
| 62 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 63 | sizeof(struct image_header)); |
| 64 | |
| 65 | /* read image header to find the image size & load address */ |
Simon Glass | ef5609c | 2016-05-14 14:03:06 -0600 | [diff] [blame] | 66 | count = blk_dread(mmc_get_blk_desc(mmc), sector, 1, header); |
Simon Glass | 96debd1 | 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; |
| 70 | goto end; |
| 71 | } |
| 72 | |
Masahiro Yamada | 4976f48 | 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 | 96debd1 | 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 | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 80 | load.filename = NULL; |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 81 | load.bl_len = mmc->read_bl_len; |
| 82 | load.read = h_spl_load_read; |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 83 | ret = spl_load_simple_fit(spl_image, &load, sector, header); |
Masahiro Yamada | 4976f48 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 84 | } else { |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 85 | ret = mmc_load_legacy(spl_image, mmc, sector, header); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 86 | } |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 87 | |
| 88 | end: |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 89 | if (ret) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 90 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 5bbf409 | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 91 | puts("mmc_load_image_raw_sector: mmc block read error\n"); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 92 | #endif |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 93 | return -1; |
Paul Kocialkowski | 1ec2646 | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 94 | } |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 95 | |
| 96 | return 0; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Nikita Kiryanov | a1e56cf | 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 | 99c7a51 | 2015-12-02 10:59:12 -0700 | [diff] [blame] | 116 | static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device) |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 117 | { |
Simon Glass | b4857aa | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 118 | #ifdef CONFIG_DM_MMC |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 119 | struct udevice *dev; |
Simon Glass | b4857aa | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 120 | #endif |
Nikita Kiryanov | a1e56cf | 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 | 4188ba3 | 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 | b4857aa | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 135 | #ifdef CONFIG_DM_MMC |
Nikita Kiryanov | a1e56cf | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 136 | err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev); |
Simon Glass | b4857aa | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 137 | if (!err) |
| 138 | *mmcp = mmc_get_mmc_dev(dev); |
| 139 | #else |
| 140 | *mmcp = find_mmc_device(mmc_dev); |
| 141 | err = *mmcp ? 0 : -ENODEV; |
| 142 | #endif |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 143 | if (err) { |
| 144 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 145 | printf("spl: could not find mmc device. error: %d\n", err); |
| 146 | #endif |
| 147 | return err; |
| 148 | } |
| 149 | |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 150 | return 0; |
| 151 | } |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 152 | |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 153 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 154 | static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, |
| 155 | struct mmc *mmc, int partition) |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 156 | { |
| 157 | disk_partition_t info; |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 158 | int err; |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 159 | |
Simon Glass | 1e2b3ef | 2016-07-05 17:10:18 -0600 | [diff] [blame] | 160 | err = part_get_info(mmc_get_blk_desc(mmc), partition, &info); |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 161 | if (err) { |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 162 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 1ec2646 | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 163 | puts("spl: partition error\n"); |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 164 | #endif |
| 165 | return -1; |
| 166 | } |
| 167 | |
Stefan Roese | 4bfcc54 | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 168 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 169 | return mmc_load_image_raw_sector(spl_image, mmc, |
| 170 | info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Stefan Roese | 4bfcc54 | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 171 | #else |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 172 | return mmc_load_image_raw_sector(spl_image, mmc, info.start); |
Stefan Roese | 4bfcc54 | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 173 | #endif |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 174 | } |
Nikita Kiryanov | d074ebb | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 175 | #else |
| 176 | #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1 |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 177 | static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, |
| 178 | struct mmc *mmc, int partition) |
Nikita Kiryanov | d074ebb | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 179 | { |
| 180 | return -ENOSYS; |
| 181 | } |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 182 | #endif |
| 183 | |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 184 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 185 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
| 186 | struct mmc *mmc) |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 187 | { |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 188 | unsigned long count; |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 189 | int ret; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 190 | |
Simon Glass | 87bce4e | 2016-06-12 23:30:32 -0600 | [diff] [blame] | 191 | count = blk_dread(mmc_get_blk_desc(mmc), |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 192 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 193 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 194 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); |
| 195 | if (count == 0) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 196 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 5bbf409 | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 197 | puts("mmc_load_image_raw_os: mmc block read error\n"); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 198 | #endif |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 199 | return -1; |
| 200 | } |
| 201 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 202 | ret = mmc_load_image_raw_sector(spl_image, mmc, |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 203 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 204 | if (ret) |
| 205 | return ret; |
| 206 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 207 | if (spl_image->os != IH_OS_LINUX) { |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 208 | puts("Expected Linux image is not found. Trying to start U-boot\n"); |
| 209 | return -ENOENT; |
| 210 | } |
| 211 | |
| 212 | return 0; |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 213 | } |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 214 | #else |
| 215 | int spl_start_uboot(void) |
| 216 | { |
| 217 | return 1; |
| 218 | } |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 219 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
| 220 | struct mmc *mmc) |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 221 | { |
| 222 | return -ENOSYS; |
| 223 | } |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 224 | #endif |
| 225 | |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 226 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 227 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc) |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 228 | { |
| 229 | int err = -ENOSYS; |
| 230 | |
| 231 | #ifdef CONFIG_SPL_FAT_SUPPORT |
| 232 | if (!spl_start_uboot()) { |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 233 | err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 234 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 235 | if (!err) |
| 236 | return err; |
| 237 | } |
| 238 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 239 | err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 240 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 241 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 242 | if (!err) |
| 243 | return err; |
| 244 | #endif |
| 245 | #endif |
| 246 | #ifdef CONFIG_SPL_EXT_SUPPORT |
| 247 | if (!spl_start_uboot()) { |
Simon Glass | b4a6c2a | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 248 | err = spl_load_image_ext_os(spl_image, &mmc->block_dev, |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 249 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 250 | if (!err) |
| 251 | return err; |
| 252 | } |
| 253 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Simon Glass | b4a6c2a | 2016-09-24 18:20:14 -0600 | [diff] [blame] | 254 | err = spl_load_image_ext(spl_image, &mmc->block_dev, |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 255 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 256 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 257 | if (!err) |
| 258 | return err; |
| 259 | #endif |
| 260 | #endif |
| 261 | |
| 262 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) |
| 263 | err = -ENOENT; |
| 264 | #endif |
| 265 | |
| 266 | return err; |
| 267 | } |
| 268 | #else |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 269 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc) |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 270 | { |
| 271 | return -ENOSYS; |
| 272 | } |
| 273 | #endif |
| 274 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 275 | static int spl_mmc_load_image(struct spl_image_info *spl_image, |
| 276 | struct spl_boot_device *bootdev) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 277 | { |
Simon Glass | d773a00 | 2015-12-02 10:59:11 -0700 | [diff] [blame] | 278 | struct mmc *mmc = NULL; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 279 | u32 boot_mode; |
Simon Glass | dc3dedf | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 280 | int err = 0; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 281 | __maybe_unused int part; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 282 | |
Simon Glass | ecdfd69 | 2016-09-24 18:19:57 -0600 | [diff] [blame] | 283 | err = spl_mmc_find_device(&mmc, bootdev->boot_device); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 284 | if (err) |
| 285 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 286 | |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 287 | err = mmc_init(mmc); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 288 | if (err) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 289 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 290 | printf("spl: mmc init failed with error: %d\n", err); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 291 | #endif |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 292 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 293 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 294 | |
Simon Glass | ecdfd69 | 2016-09-24 18:19:57 -0600 | [diff] [blame] | 295 | boot_mode = spl_boot_mode(bootdev->boot_device); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 296 | err = -EINVAL; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 297 | switch (boot_mode) { |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 298 | case MMCSD_MODE_EMMCBOOT: |
| 299 | /* |
| 300 | * We need to check what the partition is configured to. |
| 301 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 302 | * which is the first physical partition (0). |
| 303 | */ |
| 304 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
| 305 | |
| 306 | if (part == 7) |
| 307 | part = 0; |
| 308 | |
Simon Glass | 69f45cd | 2016-05-01 13:52:29 -0600 | [diff] [blame] | 309 | err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 310 | if (err) { |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 311 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 312 | puts("spl: mmc partition switch failed\n"); |
| 313 | #endif |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 314 | return err; |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 315 | } |
| 316 | /* Fall through */ |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 317 | case MMCSD_MODE_RAW: |
| 318 | debug("spl: mmc boot mode: raw\n"); |
| 319 | |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 320 | if (!spl_start_uboot()) { |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 321 | err = mmc_load_image_raw_os(spl_image, mmc); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 322 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 323 | return err; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 324 | } |
Nikita Kiryanov | d074ebb | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 325 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 326 | err = mmc_load_image_raw_partition(spl_image, mmc, |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 327 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 328 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 329 | return err; |
Nikita Kiryanov | d074ebb | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 330 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 331 | err = mmc_load_image_raw_sector(spl_image, mmc, |
Paul Kocialkowski | 3ae8f4c | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 332 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 333 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 334 | return err; |
Paul Kocialkowski | 3ae8f4c | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 335 | #endif |
Guillaume GARDET | 86a0df7 | 2016-02-18 18:17:36 +0100 | [diff] [blame] | 336 | /* If RAW mode fails, try FS mode. */ |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 337 | case MMCSD_MODE_FS: |
| 338 | debug("spl: mmc boot mode: fs\n"); |
| 339 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 340 | err = spl_mmc_do_fs_boot(spl_image, mmc); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 341 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 342 | return err; |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 343 | |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 344 | break; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 345 | case MMCSD_MODE_UNDEFINED: |
Guillaume GARDET | 2c84c9a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 346 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 347 | default: |
| 348 | puts("spl: mmc: wrong boot mode\n"); |
Guillaume GARDET | 2c84c9a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 349 | #endif |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 350 | } |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 351 | |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 352 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 353 | } |
Simon Glass | 0fed9c7 | 2016-09-24 18:20:00 -0600 | [diff] [blame] | 354 | |
| 355 | SPL_LOAD_IMAGE_METHOD(0, BOOT_DEVICE_MMC1, spl_mmc_load_image); |
| 356 | SPL_LOAD_IMAGE_METHOD(0, BOOT_DEVICE_MMC2, spl_mmc_load_image); |
| 357 | SPL_LOAD_IMAGE_METHOD(0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image); |