blob: 753c6a014a65d0306e99e2dcfe8fff4414f422a3 [file] [log] [blame]
Simon Schwarze21d2d82011-09-14 15:33:34 -04001/*
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 Rini28591df2012-08-13 12:03:19 -070026#include <spl.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040027#include <asm/u-boot.h>
28#include <asm/utils.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040029#include <mmc.h>
30#include <fat.h>
Simon Glass50402412011-10-10 08:55:19 +000031#include <version.h>
Simon Schwarze21d2d82011-09-14 15:33:34 -040032
33DECLARE_GLOBAL_DATA_PTR;
34
Simon Schwarze21d2d82011-09-14 15:33:34 -040035static void mmc_load_image_raw(struct mmc *mmc)
36{
37 u32 image_size_sectors, err;
38 const struct image_header *header;
39
40 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
41 sizeof(struct image_header));
42
43 /* read image header to find the image size & load address */
44 err = mmc->block_dev.block_read(0,
45 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1,
46 (void *)header);
47
48 if (err <= 0)
49 goto end;
50
51 spl_parse_image_header(header);
52
53 /* convert size to sectors - round up */
Tom Rinia0b9fa52012-08-14 10:25:15 -070054 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
55 mmc->read_bl_len;
Simon Schwarze21d2d82011-09-14 15:33:34 -040056
57 /* Read the header too to avoid extra memcpy */
58 err = mmc->block_dev.block_read(0,
59 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
60 image_size_sectors, (void *)spl_image.load_addr);
61
62end:
63 if (err <= 0) {
64 printf("spl: mmc blk read err - %d\n", err);
65 hang();
66 }
67}
68
Tom Rinia0d8cca2012-08-10 09:27:14 -070069#ifdef CONFIG_SPL_FAT_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -040070static void mmc_load_image_fat(struct mmc *mmc)
71{
72 s32 err;
73 struct image_header *header;
74
75 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
76 sizeof(struct image_header));
77
78 err = fat_register_device(&mmc->block_dev,
79 CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
80 if (err) {
81 printf("spl: fat register err - %d\n", err);
82 hang();
83 }
84
85 err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
86 (u8 *)header, sizeof(struct image_header));
87 if (err <= 0)
88 goto end;
89
90 spl_parse_image_header(header);
91
92 err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
93 (u8 *)spl_image.load_addr, 0);
94
95end:
96 if (err <= 0) {
97 printf("spl: error reading image %s, err - %d\n",
98 CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME, err);
99 hang();
100 }
101}
Tom Rinia0d8cca2012-08-10 09:27:14 -0700102#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400103
104void spl_mmc_load_image(void)
105{
106 struct mmc *mmc;
107 int err;
108 u32 boot_mode;
109
110 mmc_initialize(gd->bd);
111 /* We register only one device. So, the dev id is always 0 */
112 mmc = find_mmc_device(0);
113 if (!mmc) {
114 puts("spl: mmc device not found!!\n");
115 hang();
116 }
117
118 err = mmc_init(mmc);
119 if (err) {
120 printf("spl: mmc init failed: err - %d\n", err);
121 hang();
122 }
Tom Rinia76ff952012-08-14 09:19:44 -0700123 boot_mode = spl_boot_mode();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400124 if (boot_mode == MMCSD_MODE_RAW) {
125 debug("boot mode - RAW\n");
126 mmc_load_image_raw(mmc);
Tom Rinia0d8cca2012-08-10 09:27:14 -0700127#ifdef CONFIG_SPL_FAT_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -0400128 } else if (boot_mode == MMCSD_MODE_FAT) {
129 debug("boot mode - FAT\n");
130 mmc_load_image_fat(mmc);
Tom Rinia0d8cca2012-08-10 09:27:14 -0700131#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400132 } else {
133 puts("spl: wrong MMC boot mode\n");
134 hang();
135 }
136}