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