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 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | #include <common.h> |
Tom Rini | 47f7bca | 2012-08-13 12:03:19 -0700 | [diff] [blame] | 26 | #include <spl.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 27 | #include <asm/u-boot.h> |
| 28 | #include <asm/utils.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 29 | #include <mmc.h> |
| 30 | #include <fat.h> |
Simon Glass | efb2172 | 2011-10-10 08:55:19 +0000 | [diff] [blame] | 31 | #include <version.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 32 | |
| 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
Peter Korsgaard | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 35 | static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 36 | { |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 37 | unsigned long err; |
| 38 | u32 image_size_sectors; |
| 39 | struct image_header *header; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 40 | |
| 41 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 42 | sizeof(struct image_header)); |
| 43 | |
| 44 | /* read image header to find the image size & load address */ |
Peter Korsgaard | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 45 | err = mmc->block_dev.block_read(0, sector, 1, header); |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 46 | if (err == 0) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 47 | goto end; |
| 48 | |
| 49 | spl_parse_image_header(header); |
| 50 | |
| 51 | /* convert size to sectors - round up */ |
Tom Rini | f088125 | 2012-08-14 10:25:15 -0700 | [diff] [blame] | 52 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
| 53 | mmc->read_bl_len; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 54 | |
| 55 | /* Read the header too to avoid extra memcpy */ |
Peter Korsgaard | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 56 | err = mmc->block_dev.block_read(0, sector, image_size_sectors, |
| 57 | (void *)spl_image.load_addr); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 58 | |
| 59 | end: |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 60 | if (err == 0) |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 61 | printf("spl: mmc blk read err - %lu\n", err); |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 62 | |
| 63 | return (err == 0); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 66 | #ifdef CONFIG_SPL_OS_BOOT |
| 67 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 68 | { |
| 69 | if (!mmc->block_dev.block_read(0, |
| 70 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 71 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 72 | (void *)CONFIG_SYS_SPL_ARGS_ADDR)) { |
| 73 | printf("mmc args blk read error\n"); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
| 78 | } |
| 79 | #endif |
| 80 | |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 81 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 82 | static int mmc_load_image_fat(struct mmc *mmc, const char *filename) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 83 | { |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 84 | int err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 85 | struct image_header *header; |
| 86 | |
| 87 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 88 | sizeof(struct image_header)); |
| 89 | |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 90 | err = file_fat_read(filename, header, sizeof(struct image_header)); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 91 | if (err <= 0) |
| 92 | goto end; |
| 93 | |
| 94 | spl_parse_image_header(header); |
| 95 | |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 96 | err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 97 | |
| 98 | end: |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 99 | if (err <= 0) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 100 | printf("spl: error reading image %s, err - %d\n", |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 101 | filename, err); |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 102 | |
| 103 | return (err <= 0); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 104 | } |
Peter Korsgaard | 7ad2cc7 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 105 | |
| 106 | #ifdef CONFIG_SPL_OS_BOOT |
| 107 | static int mmc_load_image_fat_os(struct mmc *mmc) |
| 108 | { |
| 109 | int err; |
| 110 | |
| 111 | err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME, |
| 112 | (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); |
| 113 | if (err <= 0) { |
| 114 | printf("spl: error reading image %s, err - %d\n", |
| 115 | CONFIG_SPL_FAT_LOAD_ARGS_NAME, err); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME); |
| 120 | } |
| 121 | #endif |
| 122 | |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 123 | #endif |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 124 | |
| 125 | void spl_mmc_load_image(void) |
| 126 | { |
| 127 | struct mmc *mmc; |
| 128 | int err; |
| 129 | u32 boot_mode; |
| 130 | |
| 131 | mmc_initialize(gd->bd); |
| 132 | /* We register only one device. So, the dev id is always 0 */ |
| 133 | mmc = find_mmc_device(0); |
| 134 | if (!mmc) { |
| 135 | puts("spl: mmc device not found!!\n"); |
| 136 | hang(); |
| 137 | } |
| 138 | |
| 139 | err = mmc_init(mmc); |
| 140 | if (err) { |
| 141 | printf("spl: mmc init failed: err - %d\n", err); |
| 142 | hang(); |
| 143 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 144 | |
Tom Rini | 37189a1 | 2012-08-14 09:19:44 -0700 | [diff] [blame] | 145 | boot_mode = spl_boot_mode(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 146 | if (boot_mode == MMCSD_MODE_RAW) { |
| 147 | debug("boot mode - RAW\n"); |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 148 | #ifdef CONFIG_SPL_OS_BOOT |
| 149 | if (spl_start_uboot() || mmc_load_image_raw_os(mmc)) |
| 150 | #endif |
Peter Korsgaard | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 151 | err = mmc_load_image_raw(mmc, |
| 152 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 153 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 154 | } else if (boot_mode == MMCSD_MODE_FAT) { |
| 155 | debug("boot mode - FAT\n"); |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 156 | |
| 157 | err = fat_register_device(&mmc->block_dev, |
| 158 | CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION); |
| 159 | if (err) { |
| 160 | printf("spl: fat register err - %d\n", err); |
| 161 | hang(); |
| 162 | } |
| 163 | |
Peter Korsgaard | 7ad2cc7 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 164 | #ifdef CONFIG_SPL_OS_BOOT |
| 165 | if (spl_start_uboot() || mmc_load_image_fat_os(mmc)) |
| 166 | #endif |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 167 | err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME); |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 168 | #endif |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 169 | } else { |
| 170 | puts("spl: wrong MMC boot mode\n"); |
| 171 | hang(); |
| 172 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 173 | |
| 174 | if (err) |
| 175 | hang(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 176 | } |