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