Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010 |
| 3 | * Texas Instruments, <www.ti.com> |
| 4 | * |
| 5 | * Aneesh V <aneesh@ti.com> |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 8 | */ |
| 9 | #include <common.h> |
Simon Glass | f22851a | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 10 | #include <dm.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> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 13 | #include <asm/u-boot.h> |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 15 | #include <mmc.h> |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 16 | #include <image.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 20 | static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 21 | { |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 22 | unsigned long count; |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 23 | u32 image_size_sectors; |
| 24 | struct image_header *header; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 25 | |
| 26 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 27 | sizeof(struct image_header)); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 28 | |
| 29 | /* read image header to find the image size & load address */ |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 30 | count = mmc->block_dev.block_read(0, sector, 1, header); |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame] | 31 | debug("read sector %lx, count=%lu\n", sector, count); |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 32 | if (count == 0) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 33 | goto end; |
| 34 | |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame] | 35 | if (image_get_magic(header) != IH_MAGIC) { |
| 36 | puts("bad magic\n"); |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 37 | return -1; |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame] | 38 | } |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 39 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 40 | spl_parse_image_header(header); |
| 41 | |
| 42 | /* convert size to sectors - round up */ |
Tom Rini | a0b9fa5 | 2012-08-14 10:25:15 -0700 | [diff] [blame] | 43 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 44 | mmc->read_bl_len; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 45 | |
| 46 | /* Read the header too to avoid extra memcpy */ |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 47 | count = mmc->block_dev.block_read(0, sector, image_size_sectors, |
Gong Qianyu | 2ae4589 | 2015-07-30 14:07:12 +0800 | [diff] [blame] | 48 | (void *)(ulong)spl_image.load_addr); |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame] | 49 | debug("read %x sectors to %x\n", image_size_sectors, |
| 50 | spl_image.load_addr); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 51 | |
| 52 | end: |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 53 | if (count == 0) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 54 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 55 | puts("spl: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 56 | #endif |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 57 | return -1; |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 58 | } |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 59 | |
| 60 | return 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 63 | #ifdef CONFIG_DM_MMC |
| 64 | static int spl_mmc_find_device(struct mmc **mmc) |
| 65 | { |
| 66 | struct udevice *dev; |
| 67 | int err; |
| 68 | |
| 69 | err = mmc_initialize(NULL); |
| 70 | if (err) { |
| 71 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 72 | printf("spl: could not initialize mmc. error: %d\n", err); |
| 73 | #endif |
| 74 | return err; |
| 75 | } |
| 76 | |
| 77 | err = uclass_get_device(UCLASS_MMC, 0, &dev); |
| 78 | if (err) { |
| 79 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 80 | printf("spl: could not find mmc device. error: %d\n", err); |
| 81 | #endif |
| 82 | return err; |
| 83 | } |
| 84 | |
| 85 | *mmc = NULL; |
| 86 | *mmc = mmc_get_mmc_dev(dev); |
| 87 | return *mmc != NULL ? 0 : -ENODEV; |
| 88 | } |
| 89 | #else |
| 90 | static int spl_mmc_find_device(struct mmc **mmc) |
| 91 | { |
| 92 | int err; |
| 93 | |
| 94 | err = mmc_initialize(gd->bd); |
| 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 | } |
| 101 | |
| 102 | /* We register only one device. So, the dev id is always 0 */ |
| 103 | *mmc = find_mmc_device(0); |
| 104 | if (!*mmc) { |
| 105 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 106 | puts("spl: mmc device not found\n"); |
| 107 | #endif |
| 108 | return -ENODEV; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | #endif |
| 114 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 115 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
| 116 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) |
| 117 | { |
| 118 | disk_partition_t info; |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 119 | int err; |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 120 | |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 121 | err = get_partition_info(&mmc->block_dev, partition, &info); |
| 122 | if (err) { |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 123 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 124 | puts("spl: partition error\n"); |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 125 | #endif |
| 126 | return -1; |
| 127 | } |
| 128 | |
Stefan Roese | d0503cd | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 129 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR |
| 130 | return mmc_load_image_raw_sector(mmc, info.start + |
| 131 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
| 132 | #else |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 133 | return mmc_load_image_raw_sector(mmc, info.start); |
Stefan Roese | d0503cd | 2015-07-20 11:20:39 +0200 | [diff] [blame] | 134 | #endif |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 135 | } |
Nikita Kiryanov | 011cd8f | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 136 | #else |
| 137 | #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1 |
| 138 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) |
| 139 | { |
| 140 | return -ENOSYS; |
| 141 | } |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 142 | #endif |
| 143 | |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 144 | #ifdef CONFIG_SPL_OS_BOOT |
| 145 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 146 | { |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 147 | unsigned long count; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 148 | |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 149 | count = mmc->block_dev.block_read(0, |
| 150 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 151 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 152 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); |
| 153 | if (count == 0) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 154 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 155 | puts("spl: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 156 | #endif |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 157 | return -1; |
| 158 | } |
| 159 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 160 | return mmc_load_image_raw_sector(mmc, |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 161 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 162 | } |
Nikita Kiryanov | cc49f55 | 2015-11-08 17:11:45 +0200 | [diff] [blame] | 163 | #else |
| 164 | int spl_start_uboot(void) |
| 165 | { |
| 166 | return 1; |
| 167 | } |
| 168 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 169 | { |
| 170 | return -ENOSYS; |
| 171 | } |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 172 | #endif |
| 173 | |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 174 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
| 175 | int spl_mmc_do_fs_boot(struct mmc *mmc) |
| 176 | { |
| 177 | int err = -ENOSYS; |
| 178 | |
| 179 | #ifdef CONFIG_SPL_FAT_SUPPORT |
| 180 | if (!spl_start_uboot()) { |
| 181 | err = spl_load_image_fat_os(&mmc->block_dev, |
| 182 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 183 | if (!err) |
| 184 | return err; |
| 185 | } |
| 186 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
| 187 | err = spl_load_image_fat(&mmc->block_dev, |
| 188 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 189 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 190 | if (!err) |
| 191 | return err; |
| 192 | #endif |
| 193 | #endif |
| 194 | #ifdef CONFIG_SPL_EXT_SUPPORT |
| 195 | if (!spl_start_uboot()) { |
| 196 | err = spl_load_image_ext_os(&mmc->block_dev, |
| 197 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 198 | if (!err) |
| 199 | return err; |
| 200 | } |
| 201 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
| 202 | err = spl_load_image_ext(&mmc->block_dev, |
| 203 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 204 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 205 | if (!err) |
| 206 | return err; |
| 207 | #endif |
| 208 | #endif |
| 209 | |
| 210 | #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT) |
| 211 | err = -ENOENT; |
| 212 | #endif |
| 213 | |
| 214 | return err; |
| 215 | } |
| 216 | #else |
| 217 | int spl_mmc_do_fs_boot(struct mmc *mmc) |
| 218 | { |
| 219 | return -ENOSYS; |
| 220 | } |
| 221 | #endif |
| 222 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 223 | void spl_mmc_load_image(void) |
| 224 | { |
| 225 | struct mmc *mmc; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 226 | u32 boot_mode; |
Simon Glass | f22851a | 2015-06-23 15:38:51 -0600 | [diff] [blame] | 227 | int err = 0; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 228 | __maybe_unused int part; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 229 | |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 230 | if (spl_mmc_find_device(&mmc)) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 231 | hang(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 232 | |
Nikita Kiryanov | 022ddb8 | 2015-11-08 17:11:44 +0200 | [diff] [blame] | 233 | err = mmc_init(mmc); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 234 | if (err) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 235 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 236 | printf("spl: mmc init failed with error: %d\n", err); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 237 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 238 | hang(); |
| 239 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 240 | |
Tom Rini | a76ff95 | 2012-08-14 09:19:44 -0700 | [diff] [blame] | 241 | boot_mode = spl_boot_mode(); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 242 | switch (boot_mode) { |
Nikita Kiryanov | 2b3ae5f | 2015-11-08 17:11:48 +0200 | [diff] [blame^] | 243 | case MMCSD_MODE_EMMCBOOT: |
| 244 | /* |
| 245 | * We need to check what the partition is configured to. |
| 246 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 247 | * which is the first physical partition (0). |
| 248 | */ |
| 249 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
| 250 | |
| 251 | if (part == 7) |
| 252 | part = 0; |
| 253 | |
| 254 | if (mmc_switch_part(0, part)) { |
| 255 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 256 | puts("spl: mmc partition switch failed\n"); |
| 257 | #endif |
| 258 | hang(); |
| 259 | } |
| 260 | /* Fall through */ |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 261 | case MMCSD_MODE_RAW: |
| 262 | debug("spl: mmc boot mode: raw\n"); |
| 263 | |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 264 | if (!spl_start_uboot()) { |
| 265 | err = mmc_load_image_raw_os(mmc); |
| 266 | if (!err) |
| 267 | return; |
| 268 | } |
Nikita Kiryanov | 011cd8f | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 269 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 270 | err = mmc_load_image_raw_partition(mmc, |
| 271 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 272 | if (!err) |
| 273 | return; |
Nikita Kiryanov | 011cd8f | 2015-11-08 17:11:46 +0200 | [diff] [blame] | 274 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 275 | err = mmc_load_image_raw_sector(mmc, |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 276 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 277 | if (!err) |
| 278 | return; |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 279 | #endif |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 280 | break; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 281 | case MMCSD_MODE_FS: |
| 282 | debug("spl: mmc boot mode: fs\n"); |
| 283 | |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 284 | err = spl_mmc_do_fs_boot(mmc); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 285 | if (!err) |
| 286 | return; |
Nikita Kiryanov | 642648c | 2015-11-08 17:11:47 +0200 | [diff] [blame] | 287 | |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 288 | break; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 289 | case MMCSD_MODE_UNDEFINED: |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 290 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 291 | default: |
| 292 | puts("spl: mmc: wrong boot mode\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 293 | #endif |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 294 | } |
Nikita Kiryanov | ed52f4d | 2015-11-08 17:11:43 +0200 | [diff] [blame] | 295 | |
| 296 | hang(); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 297 | } |