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); |
Peng Fan | dd7d091 | 2019-08-22 07:42:38 +0000 | [diff] [blame] | 82 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) { |
| 83 | struct spl_load_info load; |
| 84 | |
| 85 | load.dev = mmc; |
| 86 | load.priv = NULL; |
| 87 | load.filename = NULL; |
| 88 | load.bl_len = mmc->read_bl_len; |
| 89 | load.read = h_spl_load_read; |
| 90 | |
| 91 | ret = spl_load_imx_container(spl_image, &load, sector); |
Masahiro Yamada | 4976f48 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 92 | } else { |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 93 | ret = mmc_load_legacy(spl_image, mmc, sector, header); |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 94 | } |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 95 | |
| 96 | end: |
Simon Glass | 96debd1 | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 97 | if (ret) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 98 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 5bbf409 | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 99 | puts("mmc_load_image_raw_sector: mmc block read error\n"); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 100 | #endif |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 101 | return -1; |
Paul Kocialkowski | 1ec2646 | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 102 | } |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 103 | |
| 104 | return 0; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Masahiro Yamada | 207d8b3 | 2017-06-22 16:52:36 +0900 | [diff] [blame] | 107 | static int spl_mmc_get_device_index(u32 boot_device) |
Nikita Kiryanov | a1e56cf | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 108 | { |
| 109 | switch (boot_device) { |
| 110 | case BOOT_DEVICE_MMC1: |
| 111 | return 0; |
| 112 | case BOOT_DEVICE_MMC2: |
| 113 | case BOOT_DEVICE_MMC2_2: |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 118 | printf("spl: unsupported mmc boot device.\n"); |
| 119 | #endif |
| 120 | |
| 121 | return -ENODEV; |
| 122 | } |
| 123 | |
Simon Glass | 99c7a51 | 2015-12-02 10:59:12 -0700 | [diff] [blame] | 124 | static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device) |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 125 | { |
Nikita Kiryanov | a1e56cf | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 126 | int err, mmc_dev; |
| 127 | |
| 128 | mmc_dev = spl_mmc_get_device_index(boot_device); |
| 129 | if (mmc_dev < 0) |
| 130 | return mmc_dev; |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 131 | |
Lokesh Vutla | 80f0201 | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 132 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 133 | err = mmc_init_device(mmc_dev); |
| 134 | #else |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 135 | err = mmc_initialize(NULL); |
Lokesh Vutla | 80f0201 | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 136 | #endif /* DM_MMC */ |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 137 | if (err) { |
| 138 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 139 | printf("spl: could not initialize mmc. error: %d\n", err); |
| 140 | #endif |
| 141 | return err; |
| 142 | } |
Simon Glass | b4857aa | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 143 | *mmcp = find_mmc_device(mmc_dev); |
| 144 | err = *mmcp ? 0 : -ENODEV; |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 145 | if (err) { |
| 146 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Alex Kiernan | aa608b6 | 2018-09-18 08:11:55 +0000 | [diff] [blame] | 147 | printf("spl: could not find mmc device %d. error: %d\n", |
| 148 | mmc_dev, err); |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 149 | #endif |
| 150 | return err; |
| 151 | } |
| 152 | |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 153 | return 0; |
| 154 | } |
Nikita Kiryanov | 4188ba3 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 155 | |
Dalon Westergreen | 949123e | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 156 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 157 | static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 158 | struct mmc *mmc, int partition, |
| 159 | unsigned long sector) |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 160 | { |
| 161 | disk_partition_t info; |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 162 | int err; |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 163 | |
Dalon Westergreen | f0fb4fa | 2017-02-10 17:15:34 -0800 | [diff] [blame] | 164 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE |
| 165 | int type_part; |
| 166 | /* Only support MBR so DOS_ENTRY_NUMBERS */ |
| 167 | for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) { |
| 168 | err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info); |
| 169 | if (err) |
| 170 | continue; |
| 171 | if (info.sys_ind == |
| 172 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) { |
| 173 | partition = type_part; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | #endif |
| 178 | |
Simon Glass | 1e2b3ef | 2016-07-05 17:10:18 -0600 | [diff] [blame] | 179 | err = part_get_info(mmc_get_blk_desc(mmc), partition, &info); |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 180 | if (err) { |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 181 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 1ec2646 | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 182 | puts("spl: partition error\n"); |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 183 | #endif |
| 184 | return -1; |
| 185 | } |
| 186 | |
Semen Protsenko | 38fed8a | 2016-11-16 19:19:05 +0200 | [diff] [blame] | 187 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 188 | return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector); |
Stefan Roese | 4bfcc54 | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 189 | #else |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 190 | return mmc_load_image_raw_sector(spl_image, mmc, info.start); |
Stefan Roese | 4bfcc54 | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 191 | #endif |
Paul Kocialkowski | b97300b | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 192 | } |
| 193 | #endif |
| 194 | |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 195 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 196 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
| 197 | struct mmc *mmc) |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 198 | { |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 199 | int ret; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 200 | |
York Sun | 7267fbf | 2018-06-26 09:44:39 -0700 | [diff] [blame] | 201 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR) |
| 202 | unsigned long count; |
| 203 | |
Simon Glass | 87bce4e | 2016-06-12 23:30:32 -0600 | [diff] [blame] | 204 | count = blk_dread(mmc_get_blk_desc(mmc), |
Paul Kocialkowski | 3bc37b6 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 205 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 206 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 207 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); |
Tom Rini | 9585dd3 | 2017-08-14 21:01:30 -0400 | [diff] [blame] | 208 | if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) { |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 209 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 5bbf409 | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 210 | puts("mmc_load_image_raw_os: mmc block read error\n"); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 211 | #endif |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 212 | return -1; |
| 213 | } |
York Sun | 7267fbf | 2018-06-26 09:44:39 -0700 | [diff] [blame] | 214 | #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */ |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 215 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 216 | ret = mmc_load_image_raw_sector(spl_image, mmc, |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 217 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 218 | if (ret) |
| 219 | return ret; |
| 220 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 221 | if (spl_image->os != IH_OS_LINUX) { |
Lokesh Vutla | 811906a | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 222 | puts("Expected Linux image is not found. Trying to start U-boot\n"); |
| 223 | return -ENOENT; |
| 224 | } |
| 225 | |
| 226 | return 0; |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 227 | } |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 228 | #else |
| 229 | int spl_start_uboot(void) |
| 230 | { |
| 231 | return 1; |
| 232 | } |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 233 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
| 234 | struct mmc *mmc) |
Nikita Kiryanov | 339245b | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 235 | { |
| 236 | return -ENOSYS; |
| 237 | } |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 238 | #endif |
| 239 | |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 240 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 241 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc, |
| 242 | const char *filename) |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 243 | { |
| 244 | int err = -ENOSYS; |
| 245 | |
Tien Fong Chee | 0c3a9ed | 2019-01-23 14:20:03 +0800 | [diff] [blame] | 246 | #ifdef CONFIG_SPL_FS_FAT |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 247 | if (!spl_start_uboot()) { |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 248 | 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] | 249 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 250 | if (!err) |
| 251 | return err; |
| 252 | } |
| 253 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 254 | err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 255 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 256 | filename); |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 257 | if (!err) |
| 258 | return err; |
| 259 | #endif |
| 260 | #endif |
Tien Fong Chee | f4b4092 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 261 | #ifdef CONFIG_SPL_FS_EXT4 |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 262 | if (!spl_start_uboot()) { |
Michal Simek | 1a92541 | 2016-12-01 11:09:01 +0100 | [diff] [blame] | 263 | 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] | 264 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 265 | if (!err) |
| 266 | return err; |
| 267 | } |
| 268 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Michal Simek | 1a92541 | 2016-12-01 11:09:01 +0100 | [diff] [blame] | 269 | err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 270 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 271 | filename); |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 272 | if (!err) |
| 273 | return err; |
| 274 | #endif |
| 275 | #endif |
| 276 | |
Tien Fong Chee | f4b4092 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 277 | #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4) |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 278 | err = -ENOENT; |
| 279 | #endif |
| 280 | |
| 281 | return err; |
| 282 | } |
| 283 | #else |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 284 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc, |
| 285 | const char *filename) |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 286 | { |
| 287 | return -ENOSYS; |
| 288 | } |
| 289 | #endif |
| 290 | |
Lukasz Majewski | d695d66 | 2018-02-03 08:29:52 +0100 | [diff] [blame] | 291 | u32 __weak spl_boot_mode(const u32 boot_device) |
| 292 | { |
Tien Fong Chee | f4b4092 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 293 | #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4) |
Lukasz Majewski | d695d66 | 2018-02-03 08:29:52 +0100 | [diff] [blame] | 294 | return MMCSD_MODE_FS; |
| 295 | #elif defined(CONFIG_SUPPORT_EMMC_BOOT) |
| 296 | return MMCSD_MODE_EMMCBOOT; |
| 297 | #else |
| 298 | return MMCSD_MODE_RAW; |
| 299 | #endif |
| 300 | } |
| 301 | |
Patrick Delaunay | 35a6696 | 2018-03-20 10:54:51 +0100 | [diff] [blame] | 302 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
| 303 | __weak |
| 304 | int spl_boot_partition(const u32 boot_device) |
| 305 | { |
| 306 | return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION; |
| 307 | } |
| 308 | #endif |
| 309 | |
Peng Fan | 30ecc9c | 2019-09-23 10:18:40 +0800 | [diff] [blame] | 310 | unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc) |
| 311 | { |
| 312 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR |
| 313 | return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR; |
| 314 | #else |
| 315 | return 0; |
| 316 | #endif |
| 317 | } |
| 318 | |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 319 | int spl_mmc_load(struct spl_image_info *spl_image, |
| 320 | struct spl_boot_device *bootdev, |
| 321 | const char *filename, |
| 322 | int raw_part, |
| 323 | unsigned long raw_sect) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 324 | { |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 325 | static struct mmc *mmc; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 326 | u32 boot_mode; |
Simon Glass | dc3dedf | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 327 | int err = 0; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 328 | __maybe_unused int part; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 329 | |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 330 | /* Perform peripheral init only once */ |
| 331 | if (!mmc) { |
| 332 | err = spl_mmc_find_device(&mmc, bootdev->boot_device); |
| 333 | if (err) |
| 334 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 335 | |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 336 | err = mmc_init(mmc); |
| 337 | if (err) { |
| 338 | mmc = NULL; |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 339 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 340 | printf("spl: mmc init failed with error: %d\n", err); |
Paul Burton | 8112f5f | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 341 | #endif |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 342 | return err; |
| 343 | } |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 344 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 345 | |
Peng Fan | 30ecc9c | 2019-09-23 10:18:40 +0800 | [diff] [blame] | 346 | raw_sect = spl_mmc_get_uboot_raw_sector(mmc); |
| 347 | |
Simon Glass | ecdfd69 | 2016-09-24 18:19:57 -0600 | [diff] [blame] | 348 | boot_mode = spl_boot_mode(bootdev->boot_device); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 349 | err = -EINVAL; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 350 | switch (boot_mode) { |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 351 | case MMCSD_MODE_EMMCBOOT: |
Mans Rullgard | 17241ea | 2019-09-03 16:16:50 +0200 | [diff] [blame] | 352 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION |
| 353 | part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION; |
| 354 | #else |
Mans Rullgard | 9243990 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 355 | /* |
| 356 | * We need to check what the partition is configured to. |
| 357 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 358 | * which is the first physical partition (0). |
| 359 | */ |
| 360 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 361 | |
Mans Rullgard | 9243990 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 362 | if (part == 7) |
| 363 | part = 0; |
Mans Rullgard | 17241ea | 2019-09-03 16:16:50 +0200 | [diff] [blame] | 364 | #endif |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 365 | |
Mans Rullgard | 9243990 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 366 | if (CONFIG_IS_ENABLED(MMC_TINY)) |
| 367 | err = mmc_switch_part(mmc, part); |
| 368 | else |
| 369 | err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); |
Marek Vasut | b5b838f | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 370 | |
Mans Rullgard | 9243990 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 371 | if (err) { |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 372 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Mans Rullgard | 9243990 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 373 | puts("spl: mmc partition switch failed\n"); |
Nikita Kiryanov | 83cdf6f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 374 | #endif |
Mans Rullgard | 9243990 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 375 | return err; |
| 376 | } |
| 377 | /* Fall through */ |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 378 | case MMCSD_MODE_RAW: |
| 379 | debug("spl: mmc boot mode: raw\n"); |
| 380 | |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 381 | if (!spl_start_uboot()) { |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 382 | err = mmc_load_image_raw_os(spl_image, mmc); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 383 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 384 | return err; |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 385 | } |
Dalon Westergreen | 949123e | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 386 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 387 | err = mmc_load_image_raw_partition(spl_image, mmc, raw_part, |
| 388 | raw_sect); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 389 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 390 | return err; |
Dalon Westergreen | 949123e | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 391 | #endif |
Semen Protsenko | 38fed8a | 2016-11-16 19:19:05 +0200 | [diff] [blame] | 392 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 393 | err = mmc_load_image_raw_sector(spl_image, mmc, raw_sect); |
Paul Kocialkowski | 3ae8f4c | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 394 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 395 | return err; |
Paul Kocialkowski | 3ae8f4c | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 396 | #endif |
Guillaume GARDET | 86a0df7 | 2016-02-18 18:17:36 +0100 | [diff] [blame] | 397 | /* If RAW mode fails, try FS mode. */ |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 398 | case MMCSD_MODE_FS: |
| 399 | debug("spl: mmc boot mode: fs\n"); |
| 400 | |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 401 | err = spl_mmc_do_fs_boot(spl_image, mmc, filename); |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 402 | if (!err) |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 403 | return err; |
Nikita Kiryanov | f52b729 | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 404 | |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 405 | break; |
Guillaume GARDET | 2c84c9a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 406 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 407 | default: |
| 408 | puts("spl: mmc: wrong boot mode\n"); |
Guillaume GARDET | 2c84c9a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 409 | #endif |
Paul Kocialkowski | 91199f4 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 410 | } |
Nikita Kiryanov | fd61d39 | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 411 | |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 412 | return err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 413 | } |
Simon Glass | 0fed9c7 | 2016-09-24 18:20:00 -0600 | [diff] [blame] | 414 | |
Andreas Dannenberg | e1eb6ad | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 415 | int spl_mmc_load_image(struct spl_image_info *spl_image, |
| 416 | struct spl_boot_device *bootdev) |
| 417 | { |
| 418 | return spl_mmc_load(spl_image, bootdev, |
| 419 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
| 420 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME, |
| 421 | #else |
| 422 | NULL, |
| 423 | #endif |
| 424 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
| 425 | spl_boot_partition(bootdev->boot_device), |
| 426 | #else |
| 427 | 0, |
| 428 | #endif |
| 429 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR |
| 430 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 431 | #else |
| 432 | 0); |
| 433 | #endif |
| 434 | } |
| 435 | |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 436 | SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image); |
| 437 | SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image); |
| 438 | SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image); |