Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Texas Instruments, <www.ti.com> |
| 5 | * |
| 6 | * Aneesh V <aneesh@ti.com> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 7 | */ |
| 8 | #include <common.h> |
Simon Glass | dc3dedf | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 10 | #include <spl.h> |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 11 | #include <linux/compiler.h> |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 12 | #include <errno.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 13 | #include <asm/u-boot.h> |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 15 | #include <mmc.h> |
Tom Rini | e4c444b | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 16 | #include <image.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 17 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 18 | static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc, |
| 19 | ulong sector, struct image_header *header) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 20 | { |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 21 | u32 image_size_sectors; |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 22 | unsigned long count; |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 23 | int ret; |
Tom Rini | e4c444b | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 24 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 25 | ret = spl_parse_image_header(spl_image, header); |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 26 | if (ret) |
| 27 | return ret; |
| 28 | |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 29 | /* convert size to sectors - round up */ |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 30 | image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) / |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 31 | mmc->read_bl_len; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 32 | |
| 33 | /* Read the header too to avoid extra memcpy */ |
Simon Glass | ef5609c | 2016-05-14 14:03:06 -0600 | [diff] [blame] | 34 | count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors, |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 35 | (void *)(ulong)spl_image->load_addr); |
Andre Przywara | 11e1479 | 2017-01-02 11:48:31 +0000 | [diff] [blame] | 36 | debug("read %x sectors to %lx\n", image_size_sectors, |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 37 | spl_image->load_addr); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 38 | if (count != image_size_sectors) |
| 39 | return -EIO; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 44 | static ulong h_spl_load_read(struct spl_load_info *load, ulong sector, |
| 45 | ulong count, void *buf) |
| 46 | { |
| 47 | struct mmc *mmc = load->dev; |
| 48 | |
Simon Glass | ef5609c | 2016-05-14 14:03:06 -0600 | [diff] [blame] | 49 | return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 50 | } |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 51 | |
Seung-Woo Kim | b016b58 | 2017-09-19 13:29:45 +0900 | [diff] [blame] | 52 | static __maybe_unused |
| 53 | int mmc_load_image_raw_sector(struct spl_image_info *spl_image, |
| 54 | struct mmc *mmc, unsigned long sector) |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 55 | { |
| 56 | unsigned long count; |
| 57 | struct image_header *header; |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 58 | struct blk_desc *bd = mmc_get_blk_desc(mmc); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 59 | int ret = 0; |
| 60 | |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 61 | header = spl_get_load_buffer(-sizeof(*header), bd->blksz); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 62 | |
| 63 | /* read image header to find the image size & load address */ |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 64 | count = blk_dread(bd, sector, 1, header); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 65 | debug("hdr read sector %lx, count=%lu\n", sector, count); |
| 66 | if (count == 0) { |
| 67 | ret = -EIO; |
| 68 | goto end; |
| 69 | } |
| 70 | |
Masahiro Yamada | 4976f48 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 71 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 72 | image_get_magic(header) == FDT_MAGIC) { |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 73 | struct spl_load_info load; |
| 74 | |
| 75 | debug("Found FIT\n"); |
| 76 | load.dev = mmc; |
| 77 | load.priv = NULL; |
Lokesh Vutla | eafd541 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 78 | load.filename = NULL; |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 79 | load.bl_len = mmc->read_bl_len; |
| 80 | load.read = h_spl_load_read; |
Simon Glass | f4d7d85 | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 81 | ret = spl_load_simple_fit(spl_image, &load, sector, header); |
Masahiro Yamada | 4976f48 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 82 | } else { |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 83 | ret = mmc_load_legacy(spl_image, mmc, sector, header); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 84 | } |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 85 | |
| 86 | end: |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 87 | if (ret) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 88 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 5bbf409 | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 89 | puts("mmc_load_image_raw_sector: mmc block read error\n"); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 90 | #endif |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 91 | return -1; |
Paul Kocialkowski | 1ec2646 | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 92 | } |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 93 | |
| 94 | return 0; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Masahiro Yamada | 207d8b3 | 2017-06-22 16:52:36 +0900 | [diff] [blame] | 97 | static int spl_mmc_get_device_index(u32 boot_device) |
Nikita Kiryanov | a1e56cf | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 98 | { |
| 99 | switch (boot_device) { |
| 100 | case BOOT_DEVICE_MMC1: |
| 101 | return 0; |
| 102 | case BOOT_DEVICE_MMC2: |
| 103 | case BOOT_DEVICE_MMC2_2: |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 108 | printf("spl: unsupported mmc boot device.\n"); |
| 109 | #endif |
| 110 | |
| 111 | return -ENODEV; |
| 112 | } |
| 113 | |
Simon Glass | 99c7a51 | 2015-12-02 10:59:12 -0700 | [diff] [blame] | 114 | static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device) |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 115 | { |
Simon Glass | c4d660d | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 116 | #if CONFIG_IS_ENABLED(DM_MMC) |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 117 | struct udevice *dev; |
Simon Glass | b4857aa | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 118 | #endif |
Nikita Kiryanov | a1e56cf | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 119 | int err, mmc_dev; |
| 120 | |
| 121 | mmc_dev = spl_mmc_get_device_index(boot_device); |
| 122 | if (mmc_dev < 0) |
| 123 | return mmc_dev; |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 124 | |
| 125 | err = mmc_initialize(NULL); |
| 126 | if (err) { |
| 127 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 128 | printf("spl: could not initialize mmc. error: %d\n", err); |
| 129 | #endif |
| 130 | return err; |
| 131 | } |
| 132 | |
Simon Glass | c4d660d | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 133 | #if CONFIG_IS_ENABLED(DM_MMC) |
Nikita Kiryanov | a1e56cf | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 134 | err = uclass_get_device(UCLASS_MMC, mmc_dev, &dev); |
Simon Glass | b4857aa | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 135 | if (!err) |
| 136 | *mmcp = mmc_get_mmc_dev(dev); |
| 137 | #else |
| 138 | *mmcp = find_mmc_device(mmc_dev); |
| 139 | err = *mmcp ? 0 : -ENODEV; |
| 140 | #endif |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 141 | if (err) { |
| 142 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Alex Kiernan | aa608b6 | 2018-09-18 08:11:55 +0000 | [diff] [blame^] | 143 | printf("spl: could not find mmc device %d. error: %d\n", |
| 144 | mmc_dev, err); |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 145 | #endif |
| 146 | return err; |
| 147 | } |
| 148 | |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 149 | return 0; |
| 150 | } |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 151 | |
Dalon Westergreen | 949123e | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 152 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 153 | static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, |
| 154 | struct mmc *mmc, int partition) |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 155 | { |
| 156 | disk_partition_t info; |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 157 | int err; |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 158 | |
Dalon Westergreen | f0fb4fa | 2017-02-10 17:15:34 -0800 | [diff] [blame] | 159 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE |
| 160 | int type_part; |
| 161 | /* Only support MBR so DOS_ENTRY_NUMBERS */ |
| 162 | for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) { |
| 163 | err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info); |
| 164 | if (err) |
| 165 | continue; |
| 166 | if (info.sys_ind == |
| 167 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) { |
| 168 | partition = type_part; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | #endif |
| 173 | |
Simon Glass | 1e2b3ef | 2016-07-05 17:10:18 -0600 | [diff] [blame] | 174 | err = part_get_info(mmc_get_blk_desc(mmc), partition, &info); |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 175 | if (err) { |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 176 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 1ec2646 | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 177 | puts("spl: partition error\n"); |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 178 | #endif |
| 179 | return -1; |
| 180 | } |
| 181 | |
Semen Protsenko | 38fed8a | 2016-11-16 19:19:05 +0200 | [diff] [blame] | 182 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 183 | return mmc_load_image_raw_sector(spl_image, mmc, |
| 184 | info.start + CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Stefan Roese | 4bfcc54 | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 185 | #else |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 186 | return mmc_load_image_raw_sector(spl_image, mmc, info.start); |
Stefan Roese | 4bfcc54 | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 187 | #endif |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 188 | } |
| 189 | #endif |
| 190 | |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 191 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 192 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
| 193 | struct mmc *mmc) |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 194 | { |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 195 | int ret; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 196 | |
York Sun | 7267fbf | 2018-06-26 09:44:39 -0700 | [diff] [blame] | 197 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR) |
| 198 | unsigned long count; |
| 199 | |
Simon Glass | 87bce4e | 2016-06-12 23:30:32 -0600 | [diff] [blame] | 200 | count = blk_dread(mmc_get_blk_desc(mmc), |
Paul Kocialkowski | 3bc37b6 | 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 | 9585dd3 | 2017-08-14 21:01:30 -0400 | [diff] [blame] | 204 | if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 205 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 5bbf409 | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 206 | puts("mmc_load_image_raw_os: mmc block read error\n"); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 207 | #endif |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 208 | return -1; |
| 209 | } |
York Sun | 7267fbf | 2018-06-26 09:44:39 -0700 | [diff] [blame] | 210 | #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */ |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 211 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 212 | ret = mmc_load_image_raw_sector(spl_image, mmc, |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 213 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 214 | if (ret) |
| 215 | return ret; |
| 216 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 217 | if (spl_image->os != IH_OS_LINUX) { |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 218 | puts("Expected Linux image is not found. Trying to start U-boot\n"); |
| 219 | return -ENOENT; |
| 220 | } |
| 221 | |
| 222 | return 0; |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 223 | } |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 224 | #else |
| 225 | int spl_start_uboot(void) |
| 226 | { |
| 227 | return 1; |
| 228 | } |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 229 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
| 230 | struct mmc *mmc) |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 231 | { |
| 232 | return -ENOSYS; |
| 233 | } |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 234 | #endif |
| 235 | |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 236 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 237 | 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] | 238 | { |
| 239 | int err = -ENOSYS; |
| 240 | |
| 241 | #ifdef CONFIG_SPL_FAT_SUPPORT |
| 242 | if (!spl_start_uboot()) { |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 243 | 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] | 244 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 245 | if (!err) |
| 246 | return err; |
| 247 | } |
| 248 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 249 | err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 250 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 251 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 252 | if (!err) |
| 253 | return err; |
| 254 | #endif |
| 255 | #endif |
| 256 | #ifdef CONFIG_SPL_EXT_SUPPORT |
| 257 | if (!spl_start_uboot()) { |
Michal Simek | 1a92541 | 2016-12-01 11:09:01 +0100 | [diff] [blame] | 258 | err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 259 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 260 | if (!err) |
| 261 | return err; |
| 262 | } |
| 263 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Michal Simek | 1a92541 | 2016-12-01 11:09:01 +0100 | [diff] [blame] | 264 | err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 265 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 266 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 267 | if (!err) |
| 268 | return err; |
| 269 | #endif |
| 270 | #endif |
| 271 | |
| 272 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) |
| 273 | err = -ENOENT; |
| 274 | #endif |
| 275 | |
| 276 | return err; |
| 277 | } |
| 278 | #else |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 279 | 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] | 280 | { |
| 281 | return -ENOSYS; |
| 282 | } |
| 283 | #endif |
| 284 | |
Lukasz Majewski | d695d66 | 2018-02-03 08:29:52 +0100 | [diff] [blame] | 285 | u32 __weak spl_boot_mode(const u32 boot_device) |
| 286 | { |
| 287 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) |
| 288 | return MMCSD_MODE_FS; |
| 289 | #elif defined(CONFIG_SUPPORT_EMMC_BOOT) |
| 290 | return MMCSD_MODE_EMMCBOOT; |
| 291 | #else |
| 292 | return MMCSD_MODE_RAW; |
| 293 | #endif |
| 294 | } |
| 295 | |
Patrick Delaunay | 35a6696 | 2018-03-20 10:54:51 +0100 | [diff] [blame] | 296 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
| 297 | __weak |
| 298 | int spl_boot_partition(const u32 boot_device) |
| 299 | { |
| 300 | return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION; |
| 301 | } |
| 302 | #endif |
| 303 | |
Marek Vasut | 09410c6 | 2016-12-01 02:06:35 +0100 | [diff] [blame] | 304 | int spl_mmc_load_image(struct spl_image_info *spl_image, |
| 305 | struct spl_boot_device *bootdev) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 306 | { |
Simon Glass | d773a00 | 2015-12-02 10:59:11 -0700 | [diff] [blame] | 307 | struct mmc *mmc = NULL; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 308 | u32 boot_mode; |
Simon Glass | dc3dedf | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 309 | int err = 0; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 310 | __maybe_unused int part; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 311 | |
Simon Glass | ecdfd69 | 2016-09-24 18:19:57 -0600 | [diff] [blame] | 312 | err = spl_mmc_find_device(&mmc, bootdev->boot_device); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 313 | if (err) |
| 314 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 315 | |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 316 | err = mmc_init(mmc); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 317 | if (err) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 318 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 319 | printf("spl: mmc init failed with error: %d\n", err); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 320 | #endif |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 321 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 322 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 323 | |
Simon Glass | ecdfd69 | 2016-09-24 18:19:57 -0600 | [diff] [blame] | 324 | boot_mode = spl_boot_mode(bootdev->boot_device); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 325 | err = -EINVAL; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 326 | switch (boot_mode) { |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 327 | case MMCSD_MODE_EMMCBOOT: |
| 328 | /* |
| 329 | * We need to check what the partition is configured to. |
| 330 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 331 | * which is the first physical partition (0). |
| 332 | */ |
| 333 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
| 334 | |
| 335 | if (part == 7) |
| 336 | part = 0; |
| 337 | |
Marek Vasut | b5b838f | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 338 | if (CONFIG_IS_ENABLED(MMC_TINY)) |
| 339 | err = mmc_switch_part(mmc, part); |
| 340 | else |
| 341 | err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); |
| 342 | |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 343 | if (err) { |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 344 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 345 | puts("spl: mmc partition switch failed\n"); |
| 346 | #endif |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 347 | return err; |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 348 | } |
| 349 | /* Fall through */ |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 350 | case MMCSD_MODE_RAW: |
| 351 | debug("spl: mmc boot mode: raw\n"); |
| 352 | |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 353 | if (!spl_start_uboot()) { |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 354 | err = mmc_load_image_raw_os(spl_image, mmc); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 355 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 356 | return err; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 357 | } |
Dalon Westergreen | 949123e | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 358 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Patrick Delaunay | 35a6696 | 2018-03-20 10:54:51 +0100 | [diff] [blame] | 359 | err = spl_boot_partition(bootdev->boot_device); |
| 360 | if (!err) |
| 361 | return err; |
| 362 | |
| 363 | err = mmc_load_image_raw_partition(spl_image, mmc, err); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 364 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 365 | return err; |
Dalon Westergreen | 949123e | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 366 | #endif |
Semen Protsenko | 38fed8a | 2016-11-16 19:19:05 +0200 | [diff] [blame] | 367 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 368 | err = mmc_load_image_raw_sector(spl_image, mmc, |
Paul Kocialkowski | 3ae8f4c | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 369 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 370 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 371 | return err; |
Paul Kocialkowski | 3ae8f4c | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 372 | #endif |
Guillaume GARDET | 86a0df7 | 2016-02-18 18:17:36 +0100 | [diff] [blame] | 373 | /* If RAW mode fails, try FS mode. */ |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 374 | case MMCSD_MODE_FS: |
| 375 | debug("spl: mmc boot mode: fs\n"); |
| 376 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 377 | err = spl_mmc_do_fs_boot(spl_image, mmc); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 378 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 379 | return err; |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 380 | |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 381 | break; |
Guillaume GARDET | 2c84c9a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 382 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 383 | default: |
| 384 | puts("spl: mmc: wrong boot mode\n"); |
Guillaume GARDET | 2c84c9a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 385 | #endif |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 386 | } |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 387 | |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 388 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 389 | } |
Simon Glass | 0fed9c7 | 2016-09-24 18:20:00 -0600 | [diff] [blame] | 390 | |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 391 | SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image); |
| 392 | SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image); |
| 393 | SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image); |