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