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> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 11 | #include <asm/u-boot.h> |
| 12 | #include <asm/utils.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 13 | #include <mmc.h> |
| 14 | #include <fat.h> |
Simon Glass | efb2172 | 2011-10-10 08:55:19 +0000 | [diff] [blame] | 15 | #include <version.h> |
Tom Rini | e4c444b | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 16 | #include <image.h> |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Peter Korsgaard | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 20 | static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 21 | { |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 22 | unsigned long err; |
| 23 | u32 image_size_sectors; |
| 24 | struct image_header *header; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 25 | |
| 26 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 27 | sizeof(struct image_header)); |
| 28 | |
| 29 | /* read image header to find the image size & load address */ |
Peter Korsgaard | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 30 | err = mmc->block_dev.block_read(0, sector, 1, header); |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 31 | if (err == 0) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 32 | goto end; |
| 33 | |
Tom Rini | e4c444b | 2013-06-28 14:43:01 -0400 | [diff] [blame] | 34 | if (image_get_magic(header) != IH_MAGIC) |
| 35 | return -1; |
| 36 | |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 37 | spl_parse_image_header(header); |
| 38 | |
| 39 | /* convert size to sectors - round up */ |
Tom Rini | f088125 | 2012-08-14 10:25:15 -0700 | [diff] [blame] | 40 | image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) / |
| 41 | mmc->read_bl_len; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 42 | |
| 43 | /* Read the header too to avoid extra memcpy */ |
Peter Korsgaard | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 44 | err = mmc->block_dev.block_read(0, sector, image_size_sectors, |
| 45 | (void *)spl_image.load_addr); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 46 | |
| 47 | end: |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 48 | if (err == 0) |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 49 | printf("spl: mmc blk read err - %lu\n", err); |
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 | |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 54 | #ifdef CONFIG_SPL_OS_BOOT |
| 55 | static int mmc_load_image_raw_os(struct mmc *mmc) |
| 56 | { |
| 57 | if (!mmc->block_dev.block_read(0, |
| 58 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR, |
| 59 | CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS, |
| 60 | (void *)CONFIG_SYS_SPL_ARGS_ADDR)) { |
| 61 | printf("mmc args blk read error\n"); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR); |
| 66 | } |
| 67 | #endif |
| 68 | |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 69 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 70 | static int mmc_load_image_fat(struct mmc *mmc, const char *filename) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 71 | { |
Peter Korsgaard | 2e22210 | 2013-03-21 04:55:17 +0000 | [diff] [blame] | 72 | int err; |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 73 | struct image_header *header; |
| 74 | |
| 75 | header = (struct image_header *)(CONFIG_SYS_TEXT_BASE - |
| 76 | sizeof(struct image_header)); |
| 77 | |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 78 | err = file_fat_read(filename, header, sizeof(struct image_header)); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 79 | if (err <= 0) |
| 80 | goto end; |
| 81 | |
| 82 | spl_parse_image_header(header); |
| 83 | |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 84 | err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 85 | |
| 86 | end: |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 87 | if (err <= 0) |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 88 | printf("spl: error reading image %s, err - %d\n", |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 89 | filename, err); |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 90 | |
| 91 | return (err <= 0); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 92 | } |
Peter Korsgaard | 7ad2cc7 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 93 | |
| 94 | #ifdef CONFIG_SPL_OS_BOOT |
| 95 | static int mmc_load_image_fat_os(struct mmc *mmc) |
| 96 | { |
| 97 | int err; |
| 98 | |
| 99 | err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME, |
| 100 | (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0); |
| 101 | if (err <= 0) { |
| 102 | printf("spl: error reading image %s, err - %d\n", |
| 103 | CONFIG_SPL_FAT_LOAD_ARGS_NAME, err); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME); |
| 108 | } |
| 109 | #endif |
| 110 | |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 111 | #endif |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 112 | |
| 113 | void spl_mmc_load_image(void) |
| 114 | { |
| 115 | struct mmc *mmc; |
| 116 | int err; |
| 117 | u32 boot_mode; |
| 118 | |
| 119 | mmc_initialize(gd->bd); |
| 120 | /* We register only one device. So, the dev id is always 0 */ |
| 121 | mmc = find_mmc_device(0); |
| 122 | if (!mmc) { |
| 123 | puts("spl: mmc device not found!!\n"); |
| 124 | hang(); |
| 125 | } |
| 126 | |
| 127 | err = mmc_init(mmc); |
| 128 | if (err) { |
| 129 | printf("spl: mmc init failed: err - %d\n", err); |
| 130 | hang(); |
| 131 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 132 | |
Tom Rini | 37189a1 | 2012-08-14 09:19:44 -0700 | [diff] [blame] | 133 | boot_mode = spl_boot_mode(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 134 | if (boot_mode == MMCSD_MODE_RAW) { |
| 135 | debug("boot mode - RAW\n"); |
Peter Korsgaard | 2b75b0a | 2013-05-13 08:36:29 +0000 | [diff] [blame] | 136 | #ifdef CONFIG_SPL_OS_BOOT |
| 137 | if (spl_start_uboot() || mmc_load_image_raw_os(mmc)) |
| 138 | #endif |
Peter Korsgaard | 721931f | 2013-05-13 08:36:28 +0000 | [diff] [blame] | 139 | err = mmc_load_image_raw(mmc, |
| 140 | CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR); |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 141 | #ifdef CONFIG_SPL_FAT_SUPPORT |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 142 | } else if (boot_mode == MMCSD_MODE_FAT) { |
| 143 | debug("boot mode - FAT\n"); |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 144 | |
| 145 | err = fat_register_device(&mmc->block_dev, |
| 146 | CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION); |
| 147 | if (err) { |
| 148 | printf("spl: fat register err - %d\n", err); |
| 149 | hang(); |
| 150 | } |
| 151 | |
Peter Korsgaard | 7ad2cc7 | 2013-05-13 08:36:27 +0000 | [diff] [blame] | 152 | #ifdef CONFIG_SPL_OS_BOOT |
| 153 | if (spl_start_uboot() || mmc_load_image_fat_os(mmc)) |
| 154 | #endif |
Peter Korsgaard | 2fabd0b | 2013-05-13 08:36:26 +0000 | [diff] [blame] | 155 | err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME); |
Tom Rini | 0da113e | 2012-08-10 09:27:14 -0700 | [diff] [blame] | 156 | #endif |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 157 | } else { |
| 158 | puts("spl: wrong MMC boot mode\n"); |
| 159 | hang(); |
| 160 | } |
Peter Korsgaard | 79adb7a | 2013-05-13 08:36:25 +0000 | [diff] [blame] | 161 | |
| 162 | if (err) |
| 163 | hang(); |
Simon Schwarz | 9ea5c6e | 2011-09-14 15:33:34 -0400 | [diff] [blame] | 164 | } |