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