Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | e21d2d8 | 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 | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 7 | */ |
| 8 | #include <common.h> |
Simon Glass | f22851a | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame^] | 10 | #include <part.h> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 11 | #include <spl.h> |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 13 | #include <errno.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 14 | #include <asm/u-boot.h> |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 16 | #include <mmc.h> |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 17 | #include <image.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 18 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 19 | static int mmc_load_legacy(struct spl_image_info *spl_image, struct mmc *mmc, |
| 20 | ulong sector, struct image_header *header) |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 21 | { |
| 22 | u32 image_size_sectors; |
| 23 | unsigned long count; |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 24 | int ret; |
| 25 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 26 | ret = spl_parse_image_header(spl_image, header); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 27 | if (ret) |
| 28 | return ret; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 29 | |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 30 | /* convert size to sectors - round up */ |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 31 | image_size_sectors = (spl_image->size + mmc->read_bl_len - 1) / |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 32 | mmc->read_bl_len; |
| 33 | |
| 34 | /* Read the header too to avoid extra memcpy */ |
Simon Glass | 670d304 | 2016-05-14 14:03:06 -0600 | [diff] [blame] | 35 | count = blk_dread(mmc_get_blk_desc(mmc), sector, image_size_sectors, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 36 | (void *)(ulong)spl_image->load_addr); |
Andre Przywara | 6c52607 | 2017-01-02 11:48:31 +0000 | [diff] [blame] | 37 | debug("read %x sectors to %lx\n", image_size_sectors, |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 38 | spl_image->load_addr); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 39 | if (count != image_size_sectors) |
| 40 | return -EIO; |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 45 | static ulong h_spl_load_read(struct spl_load_info *load, ulong sector, |
| 46 | ulong count, void *buf) |
| 47 | { |
| 48 | struct mmc *mmc = load->dev; |
| 49 | |
Simon Glass | 670d304 | 2016-05-14 14:03:06 -0600 | [diff] [blame] | 50 | return blk_dread(mmc_get_blk_desc(mmc), sector, count, buf); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 51 | } |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 52 | |
Baruch Siach | fef81be | 2020-01-15 09:08:08 +0200 | [diff] [blame] | 53 | static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part) |
| 54 | { |
| 55 | #if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR) |
| 56 | if (part == 0) |
| 57 | return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET; |
| 58 | #endif |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Seung-Woo Kim | c0a4e2d | 2017-09-19 13:29:45 +0900 | [diff] [blame] | 63 | static __maybe_unused |
| 64 | int mmc_load_image_raw_sector(struct spl_image_info *spl_image, |
| 65 | struct mmc *mmc, unsigned long sector) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 66 | { |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 67 | unsigned long count; |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 68 | struct image_header *header; |
Marek Vasut | 1dda46f | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 69 | struct blk_desc *bd = mmc_get_blk_desc(mmc); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 70 | int ret = 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 71 | |
Marek Vasut | 1dda46f | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 72 | header = spl_get_load_buffer(-sizeof(*header), bd->blksz); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 73 | |
| 74 | /* read image header to find the image size & load address */ |
Marek Vasut | 1dda46f | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 75 | count = blk_dread(bd, sector, 1, header); |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 76 | debug("hdr read sector %lx, count=%lu\n", sector, count); |
| 77 | if (count == 0) { |
| 78 | ret = -EIO; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 79 | goto end; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 80 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 81 | |
Masahiro Yamada | 802f1c3 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 82 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 83 | image_get_magic(header) == FDT_MAGIC) { |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 84 | struct spl_load_info load; |
| 85 | |
| 86 | debug("Found FIT\n"); |
| 87 | load.dev = mmc; |
| 88 | load.priv = NULL; |
Lokesh Vutla | f62cef1 | 2016-05-24 10:34:38 +0530 | [diff] [blame] | 89 | load.filename = NULL; |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 90 | load.bl_len = mmc->read_bl_len; |
| 91 | load.read = h_spl_load_read; |
Simon Glass | 43a734f | 2016-09-24 18:20:16 -0600 | [diff] [blame] | 92 | ret = spl_load_simple_fit(spl_image, &load, sector, header); |
Peng Fan | d867076 | 2019-08-22 07:42:38 +0000 | [diff] [blame] | 93 | } else if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) { |
| 94 | struct spl_load_info load; |
| 95 | |
| 96 | load.dev = mmc; |
| 97 | load.priv = NULL; |
| 98 | load.filename = NULL; |
| 99 | load.bl_len = mmc->read_bl_len; |
| 100 | load.read = h_spl_load_read; |
| 101 | |
| 102 | ret = spl_load_imx_container(spl_image, &load, sector); |
Masahiro Yamada | 802f1c3 | 2016-03-16 12:10:00 +0900 | [diff] [blame] | 103 | } else { |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 104 | ret = mmc_load_legacy(spl_image, mmc, sector, header); |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame] | 105 | } |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 106 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 107 | end: |
Simon Glass | b52c7fc | 2016-02-22 22:55:58 -0700 | [diff] [blame] | 108 | if (ret) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 109 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 37342ab | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 110 | puts("mmc_load_image_raw_sector: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 111 | #endif |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 112 | return -1; |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 113 | } |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 114 | |
| 115 | return 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Masahiro Yamada | 2a4a20c | 2017-06-22 16:52:36 +0900 | [diff] [blame] | 118 | static int spl_mmc_get_device_index(u32 boot_device) |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 119 | { |
| 120 | switch (boot_device) { |
| 121 | case BOOT_DEVICE_MMC1: |
| 122 | return 0; |
| 123 | case BOOT_DEVICE_MMC2: |
| 124 | case BOOT_DEVICE_MMC2_2: |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 129 | printf("spl: unsupported mmc boot device.\n"); |
| 130 | #endif |
| 131 | |
| 132 | return -ENODEV; |
| 133 | } |
| 134 | |
Simon Glass | 4faf63b | 2015-12-02 10:59:12 -0700 | [diff] [blame] | 135 | static int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device) |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 136 | { |
Nikita Kiryanov | e63b2dd | 2015-11-08 17:11:54 +0200 | [diff] [blame] | 137 | int err, mmc_dev; |
| 138 | |
| 139 | mmc_dev = spl_mmc_get_device_index(boot_device); |
| 140 | if (mmc_dev < 0) |
| 141 | return mmc_dev; |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 142 | |
Lokesh Vutla | c59b41c | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 143 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 144 | err = mmc_init_device(mmc_dev); |
| 145 | #else |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 146 | err = mmc_initialize(NULL); |
Lokesh Vutla | c59b41c | 2019-09-09 14:40:36 +0530 | [diff] [blame] | 147 | #endif /* DM_MMC */ |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 148 | if (err) { |
| 149 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 150 | printf("spl: could not initialize mmc. error: %d\n", err); |
| 151 | #endif |
| 152 | return err; |
| 153 | } |
Simon Glass | 51ace32 | 2015-12-02 10:59:13 -0700 | [diff] [blame] | 154 | *mmcp = find_mmc_device(mmc_dev); |
| 155 | err = *mmcp ? 0 : -ENODEV; |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 156 | if (err) { |
| 157 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Alex Kiernan | f5920d0 | 2018-09-18 08:11:55 +0000 | [diff] [blame] | 158 | printf("spl: could not find mmc device %d. error: %d\n", |
| 159 | mmc_dev, err); |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 160 | #endif |
| 161 | return err; |
| 162 | } |
| 163 | |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 164 | return 0; |
| 165 | } |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 166 | |
Dalon Westergreen | f5ba985 | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 167 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 168 | static int mmc_load_image_raw_partition(struct spl_image_info *spl_image, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 169 | struct mmc *mmc, int partition, |
| 170 | unsigned long sector) |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 171 | { |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 172 | struct disk_partition info; |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 173 | int err; |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 174 | |
Dalon Westergreen | 8d770f4 | 2017-02-10 17:15:34 -0800 | [diff] [blame] | 175 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE |
| 176 | int type_part; |
| 177 | /* Only support MBR so DOS_ENTRY_NUMBERS */ |
| 178 | for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) { |
| 179 | err = part_get_info(mmc_get_blk_desc(mmc), type_part, &info); |
| 180 | if (err) |
| 181 | continue; |
| 182 | if (info.sys_ind == |
| 183 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) { |
| 184 | partition = type_part; |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | #endif |
| 189 | |
Simon Glass | 55877f9 | 2016-07-05 17:10:18 -0600 | [diff] [blame] | 190 | err = part_get_info(mmc_get_blk_desc(mmc), partition, &info); |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 191 | if (err) { |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 192 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 193 | puts("spl: partition error\n"); |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 194 | #endif |
| 195 | return -1; |
| 196 | } |
| 197 | |
Semen Protsenko | 7e3ba48 | 2016-11-16 19:19:05 +0200 | [diff] [blame] | 198 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 199 | return mmc_load_image_raw_sector(spl_image, mmc, info.start + sector); |
Stefan Roese | d0503cd | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 200 | #else |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 201 | return mmc_load_image_raw_sector(spl_image, mmc, info.start); |
Stefan Roese | d0503cd | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 202 | #endif |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 203 | } |
| 204 | #endif |
| 205 | |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 206 | #ifdef CONFIG_SPL_OS_BOOT |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 207 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
| 208 | struct mmc *mmc) |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 209 | { |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 210 | int ret; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 211 | |
York Sun | cea8821 | 2018-06-26 09:44:39 -0700 | [diff] [blame] | 212 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR) |
| 213 | unsigned long count; |
| 214 | |
Simon Glass | 70061ba | 2016-06-12 23:30:32 -0600 | [diff] [blame] | 215 | count = blk_dread(mmc_get_blk_desc(mmc), |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 216 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 217 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 218 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); |
Tom Rini | 195bc4b | 2017-08-14 21:01:30 -0400 | [diff] [blame] | 219 | if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 220 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Fabio Estevam | 37342ab | 2016-04-20 23:12:04 -0300 | [diff] [blame] | 221 | puts("mmc_load_image_raw_os: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 222 | #endif |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 223 | return -1; |
| 224 | } |
York Sun | cea8821 | 2018-06-26 09:44:39 -0700 | [diff] [blame] | 225 | #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */ |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 226 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 227 | ret = mmc_load_image_raw_sector(spl_image, mmc, |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 228 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 229 | if (ret) |
| 230 | return ret; |
| 231 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 232 | if (spl_image->os != IH_OS_LINUX) { |
Lokesh Vutla | 9dee77f | 2016-04-11 15:24:50 +0530 | [diff] [blame] | 233 | puts("Expected Linux image is not found. Trying to start U-boot\n"); |
| 234 | return -ENOENT; |
| 235 | } |
| 236 | |
| 237 | return 0; |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 238 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 239 | #else |
| 240 | int spl_start_uboot(void) |
| 241 | { |
| 242 | return 1; |
| 243 | } |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 244 | static int mmc_load_image_raw_os(struct spl_image_info *spl_image, |
| 245 | struct mmc *mmc) |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 246 | { |
| 247 | return -ENOSYS; |
| 248 | } |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 249 | #endif |
| 250 | |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 251 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 252 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc, |
| 253 | const char *filename) |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 254 | { |
| 255 | int err = -ENOSYS; |
| 256 | |
Tien Fong Chee | 6fd0a71 | 2019-01-23 14:20:03 +0800 | [diff] [blame] | 257 | #ifdef CONFIG_SPL_FS_FAT |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 258 | if (!spl_start_uboot()) { |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 259 | err = spl_load_image_fat_os(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 260 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 261 | if (!err) |
| 262 | return err; |
| 263 | } |
| 264 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 265 | err = spl_load_image_fat(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 266 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 267 | filename); |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 268 | if (!err) |
| 269 | return err; |
| 270 | #endif |
| 271 | #endif |
Tien Fong Chee | 6091dd1 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 272 | #ifdef CONFIG_SPL_FS_EXT4 |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 273 | if (!spl_start_uboot()) { |
Michal Simek | 0ef6ec8 | 2016-12-01 11:09:01 +0100 | [diff] [blame] | 274 | err = spl_load_image_ext_os(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 275 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 276 | if (!err) |
| 277 | return err; |
| 278 | } |
| 279 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Michal Simek | 0ef6ec8 | 2016-12-01 11:09:01 +0100 | [diff] [blame] | 280 | err = spl_load_image_ext(spl_image, mmc_get_blk_desc(mmc), |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 281 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 282 | filename); |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 283 | if (!err) |
| 284 | return err; |
| 285 | #endif |
| 286 | #endif |
| 287 | |
Tien Fong Chee | 6091dd1 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 288 | #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4) |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 289 | err = -ENOENT; |
| 290 | #endif |
| 291 | |
| 292 | return err; |
| 293 | } |
| 294 | #else |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 295 | static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image, struct mmc *mmc, |
| 296 | const char *filename) |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 297 | { |
| 298 | return -ENOSYS; |
| 299 | } |
| 300 | #endif |
| 301 | |
Harald Seiler | 0bf7ab1 | 2020-04-15 11:33:30 +0200 | [diff] [blame] | 302 | u32 __weak spl_mmc_boot_mode(const u32 boot_device) |
Lukasz Majewski | 7c88d10 | 2018-02-03 08:29:52 +0100 | [diff] [blame] | 303 | { |
Tien Fong Chee | 6091dd1 | 2019-01-23 14:20:05 +0800 | [diff] [blame] | 304 | #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4) |
Lukasz Majewski | 7c88d10 | 2018-02-03 08:29:52 +0100 | [diff] [blame] | 305 | return MMCSD_MODE_FS; |
| 306 | #elif defined(CONFIG_SUPPORT_EMMC_BOOT) |
| 307 | return MMCSD_MODE_EMMCBOOT; |
| 308 | #else |
| 309 | return MMCSD_MODE_RAW; |
| 310 | #endif |
| 311 | } |
| 312 | |
Patrick Delaunay | 113aa50 | 2018-03-20 10:54:51 +0100 | [diff] [blame] | 313 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Harald Seiler | bf16c30 | 2020-04-15 11:33:31 +0200 | [diff] [blame] | 314 | int __weak spl_mmc_boot_partition(const u32 boot_device) |
Patrick Delaunay | 113aa50 | 2018-03-20 10:54:51 +0100 | [diff] [blame] | 315 | { |
| 316 | return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION; |
| 317 | } |
| 318 | #endif |
| 319 | |
Faiz Abbas | 0bdf563 | 2020-02-26 13:44:35 +0530 | [diff] [blame] | 320 | unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc, |
| 321 | unsigned long raw_sect) |
Peng Fan | 315cf59 | 2019-09-23 10:18:40 +0800 | [diff] [blame] | 322 | { |
Faiz Abbas | 0bdf563 | 2020-02-26 13:44:35 +0530 | [diff] [blame] | 323 | return raw_sect; |
Peng Fan | 315cf59 | 2019-09-23 10:18:40 +0800 | [diff] [blame] | 324 | } |
| 325 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 326 | int spl_mmc_load(struct spl_image_info *spl_image, |
| 327 | struct spl_boot_device *bootdev, |
| 328 | const char *filename, |
| 329 | int raw_part, |
| 330 | unsigned long raw_sect) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 331 | { |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 332 | static struct mmc *mmc; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 333 | u32 boot_mode; |
Simon Glass | f22851a | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 334 | int err = 0; |
Baruch Siach | fef81be | 2020-01-15 09:08:08 +0200 | [diff] [blame] | 335 | __maybe_unused int part = 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 336 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 337 | /* Perform peripheral init only once */ |
| 338 | if (!mmc) { |
| 339 | err = spl_mmc_find_device(&mmc, bootdev->boot_device); |
| 340 | if (err) |
| 341 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 342 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 343 | err = mmc_init(mmc); |
| 344 | if (err) { |
| 345 | mmc = NULL; |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 346 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 347 | printf("spl: mmc init failed with error: %d\n", err); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 348 | #endif |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 349 | return err; |
| 350 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 351 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 352 | |
Harald Seiler | 0bf7ab1 | 2020-04-15 11:33:30 +0200 | [diff] [blame] | 353 | boot_mode = spl_mmc_boot_mode(bootdev->boot_device); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 354 | err = -EINVAL; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 355 | switch (boot_mode) { |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 356 | case MMCSD_MODE_EMMCBOOT: |
Mans Rullgard | e3b39c8 | 2019-09-03 16:16:50 +0200 | [diff] [blame] | 357 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION |
| 358 | part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION; |
| 359 | #else |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 360 | /* |
| 361 | * We need to check what the partition is configured to. |
| 362 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 363 | * which is the first physical partition (0). |
| 364 | */ |
| 365 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 366 | |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 367 | if (part == 7) |
| 368 | part = 0; |
Mans Rullgard | e3b39c8 | 2019-09-03 16:16:50 +0200 | [diff] [blame] | 369 | #endif |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 370 | |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 371 | if (CONFIG_IS_ENABLED(MMC_TINY)) |
| 372 | err = mmc_switch_part(mmc, part); |
| 373 | else |
| 374 | err = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part); |
Marek Vasut | f537e39 | 2016-12-01 02:06:33 +0100 | [diff] [blame] | 375 | |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 376 | if (err) { |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 377 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 378 | puts("spl: mmc partition switch failed\n"); |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame] | 379 | #endif |
Mans Rullgard | ec046a5 | 2019-09-03 16:17:17 +0200 | [diff] [blame] | 380 | return err; |
| 381 | } |
| 382 | /* Fall through */ |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 383 | case MMCSD_MODE_RAW: |
| 384 | debug("spl: mmc boot mode: raw\n"); |
| 385 | |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 386 | if (!spl_start_uboot()) { |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 387 | err = mmc_load_image_raw_os(spl_image, mmc); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 388 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 389 | return err; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 390 | } |
Peng Fan | dc7d322 | 2019-10-23 01:43:30 +0000 | [diff] [blame] | 391 | |
Faiz Abbas | 0bdf563 | 2020-02-26 13:44:35 +0530 | [diff] [blame] | 392 | raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect); |
Peng Fan | dc7d322 | 2019-10-23 01:43:30 +0000 | [diff] [blame] | 393 | |
Dalon Westergreen | f5ba985 | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 394 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 395 | err = mmc_load_image_raw_partition(spl_image, mmc, raw_part, |
| 396 | raw_sect); |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 397 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 398 | return err; |
Dalon Westergreen | f5ba985 | 2017-02-10 17:15:35 -0800 | [diff] [blame] | 399 | #endif |
Semen Protsenko | 7e3ba48 | 2016-11-16 19:19:05 +0200 | [diff] [blame] | 400 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR |
Baruch Siach | fef81be | 2020-01-15 09:08:08 +0200 | [diff] [blame] | 401 | err = mmc_load_image_raw_sector(spl_image, mmc, |
| 402 | raw_sect + spl_mmc_raw_uboot_offset(part)); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 403 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 404 | return err; |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 405 | #endif |
Guillaume GARDET | 834dd88 | 2016-02-18 18:17:36 +0100 | [diff] [blame] | 406 | /* If RAW mode fails, try FS mode. */ |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 407 | case MMCSD_MODE_FS: |
| 408 | debug("spl: mmc boot mode: fs\n"); |
| 409 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 410 | err = spl_mmc_do_fs_boot(spl_image, mmc, filename); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 411 | if (!err) |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 412 | return err; |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 413 | |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 414 | break; |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 415 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 416 | default: |
| 417 | puts("spl: mmc: wrong boot mode\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 418 | #endif |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 419 | } |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 420 | |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 421 | return err; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 422 | } |
Simon Glass | d449cb8 | 2016-09-24 18:20:00 -0600 | [diff] [blame] | 423 | |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 424 | int spl_mmc_load_image(struct spl_image_info *spl_image, |
| 425 | struct spl_boot_device *bootdev) |
| 426 | { |
| 427 | return spl_mmc_load(spl_image, bootdev, |
| 428 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
| 429 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME, |
| 430 | #else |
| 431 | NULL, |
| 432 | #endif |
| 433 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
Harald Seiler | bf16c30 | 2020-04-15 11:33:31 +0200 | [diff] [blame] | 434 | spl_mmc_boot_partition(bootdev->boot_device), |
Andreas Dannenberg | 8d9f7f1 | 2019-06-04 17:55:46 -0500 | [diff] [blame] | 435 | #else |
| 436 | 0, |
| 437 | #endif |
| 438 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR |
| 439 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 440 | #else |
| 441 | 0); |
| 442 | #endif |
| 443 | } |
| 444 | |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 445 | SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image); |
| 446 | SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image); |
| 447 | SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image); |