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