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