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