blob: fc2f2260f888a308d26d7a63fb5db6e5b95b78df [file] [log] [blame]
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 *
5 * Aneesh V <aneesh@ti.com>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -04008 */
9#include <common.h>
Tom Rini47f7bca2012-08-13 12:03:19 -070010#include <spl.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040011#include <asm/u-boot.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040012#include <mmc.h>
13#include <fat.h>
Simon Glassefb21722011-10-10 08:55:19 +000014#include <version.h>
Tom Rinie4c444b2013-06-28 14:43:01 -040015#include <image.h>
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040016
17DECLARE_GLOBAL_DATA_PTR;
18
Peter Korsgaard721931f2013-05-13 08:36:28 +000019static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040020{
Peter Korsgaard2e222102013-03-21 04:55:17 +000021 unsigned long err;
22 u32 image_size_sectors;
23 struct image_header *header;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040024
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 Korsgaard721931f2013-05-13 08:36:28 +000029 err = mmc->block_dev.block_read(0, sector, 1, header);
Peter Korsgaard2e222102013-03-21 04:55:17 +000030 if (err == 0)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040031 goto end;
32
Tom Rinie4c444b2013-06-28 14:43:01 -040033 if (image_get_magic(header) != IH_MAGIC)
34 return -1;
35
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040036 spl_parse_image_header(header);
37
38 /* convert size to sectors - round up */
Tom Rinif0881252012-08-14 10:25:15 -070039 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
40 mmc->read_bl_len;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040041
42 /* Read the header too to avoid extra memcpy */
Peter Korsgaard721931f2013-05-13 08:36:28 +000043 err = mmc->block_dev.block_read(0, sector, image_size_sectors,
44 (void *)spl_image.load_addr);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040045
46end:
Paul Burton8112f5f2013-09-04 16:12:24 +010047#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000048 if (err == 0)
Peter Korsgaard2e222102013-03-21 04:55:17 +000049 printf("spl: mmc blk read err - %lu\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +010050#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000051
52 return (err == 0);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040053}
54
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000055#ifdef CONFIG_SPL_OS_BOOT
56static 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 Burton8112f5f2013-09-04 16:12:24 +010062#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000063 printf("mmc args blk read error\n");
Paul Burton8112f5f2013-09-04 16:12:24 +010064#endif
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +000065 return -1;
66 }
67
68 return mmc_load_image_raw(mmc, CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
69}
70#endif
71
Tom Rini0da113e2012-08-10 09:27:14 -070072#ifdef CONFIG_SPL_FAT_SUPPORT
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +000073static int mmc_load_image_fat(struct mmc *mmc, const char *filename)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040074{
Peter Korsgaard2e222102013-03-21 04:55:17 +000075 int err;
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040076 struct image_header *header;
77
78 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
79 sizeof(struct image_header));
80
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +000081 err = file_fat_read(filename, header, sizeof(struct image_header));
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040082 if (err <= 0)
83 goto end;
84
85 spl_parse_image_header(header);
86
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +000087 err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040088
89end:
Paul Burton8112f5f2013-09-04 16:12:24 +010090#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000091 if (err <= 0)
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040092 printf("spl: error reading image %s, err - %d\n",
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +000093 filename, err);
Paul Burton8112f5f2013-09-04 16:12:24 +010094#endif
Peter Korsgaard79adb7a2013-05-13 08:36:25 +000095
96 return (err <= 0);
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -040097}
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +000098
99#ifdef CONFIG_SPL_OS_BOOT
100static 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 Burton8112f5f2013-09-04 16:12:24 +0100107#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000108 printf("spl: error reading image %s, err - %d\n",
109 CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100110#endif
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000111 return -1;
112 }
113
114 return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
115}
116#endif
117
Tom Rini0da113e2012-08-10 09:27:14 -0700118#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400119
120void 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 Burton8112f5f2013-09-04 16:12:24 +0100130#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400131 puts("spl: mmc device not found!!\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100132#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400133 hang();
134 }
135
136 err = mmc_init(mmc);
137 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100138#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400139 printf("spl: mmc init failed: err - %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100140#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400141 hang();
142 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000143
Tom Rini37189a12012-08-14 09:19:44 -0700144 boot_mode = spl_boot_mode();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400145 if (boot_mode == MMCSD_MODE_RAW) {
146 debug("boot mode - RAW\n");
Peter Korsgaard2b75b0a2013-05-13 08:36:29 +0000147#ifdef CONFIG_SPL_OS_BOOT
148 if (spl_start_uboot() || mmc_load_image_raw_os(mmc))
149#endif
Peter Korsgaard721931f2013-05-13 08:36:28 +0000150 err = mmc_load_image_raw(mmc,
151 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
Tom Rini0da113e2012-08-10 09:27:14 -0700152#ifdef CONFIG_SPL_FAT_SUPPORT
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400153 } else if (boot_mode == MMCSD_MODE_FAT) {
154 debug("boot mode - FAT\n");
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +0000155
156 err = fat_register_device(&mmc->block_dev,
157 CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
158 if (err) {
Paul Burton8112f5f2013-09-04 16:12:24 +0100159#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +0000160 printf("spl: fat register err - %d\n", err);
Paul Burton8112f5f2013-09-04 16:12:24 +0100161#endif
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +0000162 hang();
163 }
164
Peter Korsgaard7ad2cc72013-05-13 08:36:27 +0000165#ifdef CONFIG_SPL_OS_BOOT
166 if (spl_start_uboot() || mmc_load_image_fat_os(mmc))
167#endif
Peter Korsgaard2fabd0b2013-05-13 08:36:26 +0000168 err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
Tom Rini0da113e2012-08-10 09:27:14 -0700169#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400170 } else {
Paul Burton8112f5f2013-09-04 16:12:24 +0100171#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400172 puts("spl: wrong MMC boot mode\n");
Paul Burton8112f5f2013-09-04 16:12:24 +0100173#endif
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400174 hang();
175 }
Peter Korsgaard79adb7a2013-05-13 08:36:25 +0000176
177 if (err)
178 hang();
Simon Schwarz9ea5c6e2011-09-14 15:33:34 -0400179}