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