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> |
Tom Rini | 28591df | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 10 | #include <spl.h> |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 11 | #include <linux/compiler.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 12 | #include <asm/u-boot.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 13 | #include <mmc.h> |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 14 | #include <image.h> |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 18 | 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] | 19 | { |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 20 | unsigned long count; |
Peter Korsgaard | f247129 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 21 | u32 image_size_sectors; |
| 22 | struct image_header *header; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 23 | |
| 24 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 25 | sizeof(struct image_header)); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 26 | |
| 27 | /* read image header to find the image size & load address */ |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 28 | count = mmc->block_dev.block_read(0, sector, 1, header); |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame^] | 29 | debug("read sector %lx, count=%lu\n", sector, count); |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 30 | if (count == 0) |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 31 | goto end; |
| 32 | |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame^] | 33 | if (image_get_magic(header) != IH_MAGIC) { |
| 34 | puts("bad magic\n"); |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 35 | return -1; |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame^] | 36 | } |
Tom Rini | 1530e8a | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 37 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 38 | spl_parse_image_header(header); |
| 39 | |
| 40 | /* convert size to sectors - round up */ |
Tom Rini | a0b9fa5 | 2012-08-14 10:25:15 -0700 | [diff] [blame] | 41 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 42 | mmc->read_bl_len; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 43 | |
| 44 | /* Read the header too to avoid extra memcpy */ |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 45 | count = mmc->block_dev.block_read(0, sector, image_size_sectors, |
Simon Glass | 5614db8 | 2015-06-23 15:38:47 -0600 | [diff] [blame^] | 46 | (void *)spl_image.load_addr); |
| 47 | debug("read %x sectors to %x\n", image_size_sectors, |
| 48 | spl_image.load_addr); |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 49 | |
| 50 | end: |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 51 | if (count == 0) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 52 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 53 | puts("spl: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 54 | #endif |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 55 | return -1; |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 56 | } |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 57 | |
| 58 | return 0; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 61 | #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION |
| 62 | static int mmc_load_image_raw_partition(struct mmc *mmc, int partition) |
| 63 | { |
| 64 | disk_partition_t info; |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 65 | int err; |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 66 | |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 67 | err = get_partition_info(&mmc->block_dev, partition, &info); |
| 68 | if (err) { |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 69 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 70 | puts("spl: partition error\n"); |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 71 | #endif |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | return mmc_load_image_raw_sector(mmc, info.start); |
| 76 | } |
| 77 | #endif |
| 78 | |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 79 | #ifdef CONFIG_SPL_OS_BOOT |
| 80 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 81 | { |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 82 | unsigned long count; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 83 | |
Paul Kocialkowski | 2d69f25 | 2015-05-22 12:45:35 +0200 | [diff] [blame] | 84 | count = mmc->block_dev.block_read(0, |
| 85 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 86 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 87 | (void *) CONFIG_SYS_SPL_ARGS_ADDR); |
| 88 | if (count == 0) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 89 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 01c723f | 2015-06-03 18:48:51 +0200 | [diff] [blame] | 90 | puts("spl: mmc block read error\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 91 | #endif |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 92 | return -1; |
| 93 | } |
| 94 | |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 95 | return mmc_load_image_raw_sector(mmc, |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 96 | CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 97 | } |
| 98 | #endif |
| 99 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 100 | void spl_mmc_load_image(void) |
| 101 | { |
| 102 | struct mmc *mmc; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 103 | u32 boot_mode; |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 104 | int err; |
| 105 | __maybe_unused int part; |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 106 | |
| 107 | mmc_initialize(gd->bd); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 108 | |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 109 | /* We register only one device. So, the dev id is always 0 */ |
| 110 | mmc = find_mmc_device(0); |
| 111 | if (!mmc) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 112 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 113 | puts("spl: mmc device not found\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 114 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 115 | hang(); |
| 116 | } |
| 117 | |
| 118 | err = mmc_init(mmc); |
| 119 | if (err) { |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 120 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 121 | printf("spl: mmc init failed with error: %d\n", err); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 122 | #endif |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 123 | hang(); |
| 124 | } |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 125 | |
Tom Rini | a76ff95 | 2012-08-14 09:19:44 -0700 | [diff] [blame] | 126 | boot_mode = spl_boot_mode(); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 127 | switch (boot_mode) { |
| 128 | case MMCSD_MODE_RAW: |
| 129 | debug("spl: mmc boot mode: raw\n"); |
| 130 | |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 131 | #ifdef CONFIG_SPL_OS_BOOT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 132 | if (!spl_start_uboot()) { |
| 133 | err = mmc_load_image_raw_os(mmc); |
| 134 | if (!err) |
| 135 | return; |
| 136 | } |
Peter Korsgaard | 01b542f | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 137 | #endif |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 138 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION) |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 139 | err = mmc_load_image_raw_partition(mmc, |
| 140 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 141 | if (!err) |
| 142 | return; |
| 143 | #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 144 | err = mmc_load_image_raw_sector(mmc, |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 145 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 146 | if (!err) |
| 147 | return; |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 148 | #endif |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 149 | case MMCSD_MODE_FS: |
| 150 | debug("spl: mmc boot mode: fs\n"); |
| 151 | |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 152 | #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 153 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Peter Korsgaard | 465f1f8 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 154 | #ifdef CONFIG_SPL_OS_BOOT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 155 | if (!spl_start_uboot()) { |
| 156 | err = spl_load_image_fat_os(&mmc->block_dev, |
| 157 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 158 | if (!err) |
| 159 | return; |
| 160 | } |
Peter Korsgaard | 465f1f8 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 161 | #endif |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 162 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Dan Murphy | b7b5b0c | 2014-01-16 11:23:29 -0600 | [diff] [blame] | 163 | err = spl_load_image_fat(&mmc->block_dev, |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 164 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 165 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 166 | if (!err) |
| 167 | return; |
| 168 | #endif |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 169 | #endif |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 170 | #ifdef CONFIG_SPL_EXT_SUPPORT |
| 171 | #ifdef CONFIG_SPL_OS_BOOT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 172 | if (!spl_start_uboot()) { |
| 173 | err = spl_load_image_ext_os(&mmc->block_dev, |
| 174 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION); |
| 175 | if (!err) |
| 176 | return; |
| 177 | } |
Tom Rini | a0d8cca | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 178 | #endif |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 179 | #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
Guillaume GARDET | 1eb410c | 2014-10-15 17:53:12 +0200 | [diff] [blame] | 180 | err = spl_load_image_ext(&mmc->block_dev, |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 181 | CONFIG_SYS_MMCSD_FS_BOOT_PARTITION, |
| 182 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 183 | if (!err) |
| 184 | return; |
| 185 | #endif |
| 186 | #endif |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 187 | #endif |
Tom Rini | cc61269 | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 188 | #ifdef CONFIG_SUPPORT_EMMC_BOOT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 189 | case MMCSD_MODE_EMMCBOOT: |
Tom Rini | cc61269 | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 190 | /* |
| 191 | * We need to check what the partition is configured to. |
| 192 | * 1 and 2 match up to boot0 / boot1 and 7 is user data |
| 193 | * which is the first physical partition (0). |
| 194 | */ |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 195 | part = (mmc->part_config >> 3) & PART_ACCESS_MASK; |
Tom Rini | cc61269 | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 196 | |
| 197 | if (part == 7) |
| 198 | part = 0; |
| 199 | |
| 200 | if (mmc_switch_part(0, part)) { |
| 201 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 202 | puts("spl: mmc partition switch failed\n"); |
Tom Rini | cc61269 | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 203 | #endif |
| 204 | hang(); |
| 205 | } |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 206 | |
Tom Rini | cc61269 | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 207 | #ifdef CONFIG_SPL_OS_BOOT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 208 | if (!spl_start_uboot()) { |
| 209 | err = mmc_load_image_raw_os(mmc); |
| 210 | if (!err) |
| 211 | return; |
| 212 | } |
Tom Rini | cc61269 | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 213 | #endif |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 214 | #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION) |
Paul Kocialkowski | 409d4f7 | 2015-04-27 10:20:23 +0200 | [diff] [blame] | 215 | err = mmc_load_image_raw_partition(mmc, |
| 216 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION); |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 217 | if (!err) |
| 218 | return; |
| 219 | #elif defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR) |
Paul Kocialkowski | 17675c8 | 2014-11-08 23:14:56 +0100 | [diff] [blame] | 220 | err = mmc_load_image_raw_sector(mmc, |
Tom Rini | cc61269 | 2014-02-05 10:24:18 -0500 | [diff] [blame] | 221 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 222 | if (!err) |
| 223 | return; |
Guillaume GARDET | 1143b0a | 2014-12-16 12:00:44 +0100 | [diff] [blame] | 224 | #endif |
Paul Kocialkowski | 4bd66e7 | 2015-06-08 23:05:09 +0200 | [diff] [blame] | 225 | #endif |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 226 | case MMCSD_MODE_UNDEFINED: |
| 227 | default: |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 228 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 229 | if (err) |
| 230 | puts("spl: mmc: no boot mode left to try\n"); |
| 231 | else |
| 232 | puts("spl: mmc: wrong boot mode\n"); |
Paul Burton | a8015fd | 2013-09-04 16:12:24 +0100 | [diff] [blame] | 233 | #endif |
Peter Korsgaard | 5e5483d | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 234 | hang(); |
Paul Kocialkowski | 0d13972 | 2015-04-27 10:20:22 +0200 | [diff] [blame] | 235 | } |
Simon Schwarz | e21d2d8 | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 236 | } |