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