blob: 7efdcb88b73412b7602c72af2e265f068031bb06 [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{
Peter Korsgaardf2471292013-03-21 04:55:17 +000037 unsigned long err;
38 u32 image_size_sectors;
39 struct image_header *header;
Simon Schwarze21d2d82011-09-14 15:33:34 -040040
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 */
45 err = mmc->block_dev.block_read(0,
46 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1,
Peter Korsgaardf2471292013-03-21 04:55:17 +000047 header);
Simon Schwarze21d2d82011-09-14 15:33:34 -040048
Peter Korsgaardf2471292013-03-21 04:55:17 +000049 if (err == 0)
Simon Schwarze21d2d82011-09-14 15:33:34 -040050 goto end;
51
52 spl_parse_image_header(header);
53
54 /* convert size to sectors - round up */
Tom Rinia0b9fa52012-08-14 10:25:15 -070055 image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
56 mmc->read_bl_len;
Simon Schwarze21d2d82011-09-14 15:33:34 -040057
58 /* Read the header too to avoid extra memcpy */
59 err = mmc->block_dev.block_read(0,
60 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
61 image_size_sectors, (void *)spl_image.load_addr);
62
63end:
Peter Korsgaardf2471292013-03-21 04:55:17 +000064 if (err == 0) {
65 printf("spl: mmc blk read err - %lu\n", err);
Simon Schwarze21d2d82011-09-14 15:33:34 -040066 hang();
67 }
68}
69
Tom Rinia0d8cca2012-08-10 09:27:14 -070070#ifdef CONFIG_SPL_FAT_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -040071static void mmc_load_image_fat(struct mmc *mmc)
72{
Peter Korsgaardf2471292013-03-21 04:55:17 +000073 int err;
Simon Schwarze21d2d82011-09-14 15:33:34 -040074 struct image_header *header;
75
76 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
77 sizeof(struct image_header));
78
79 err = fat_register_device(&mmc->block_dev,
80 CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
81 if (err) {
82 printf("spl: fat register err - %d\n", err);
83 hang();
84 }
85
86 err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
Peter Korsgaardf2471292013-03-21 04:55:17 +000087 header, sizeof(struct image_header));
Simon Schwarze21d2d82011-09-14 15:33:34 -040088 if (err <= 0)
89 goto end;
90
91 spl_parse_image_header(header);
92
93 err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
94 (u8 *)spl_image.load_addr, 0);
95
96end:
97 if (err <= 0) {
98 printf("spl: error reading image %s, err - %d\n",
99 CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME, err);
100 hang();
101 }
102}
Tom Rinia0d8cca2012-08-10 09:27:14 -0700103#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400104
105void spl_mmc_load_image(void)
106{
107 struct mmc *mmc;
108 int err;
109 u32 boot_mode;
110
111 mmc_initialize(gd->bd);
112 /* We register only one device. So, the dev id is always 0 */
113 mmc = find_mmc_device(0);
114 if (!mmc) {
115 puts("spl: mmc device not found!!\n");
116 hang();
117 }
118
119 err = mmc_init(mmc);
120 if (err) {
121 printf("spl: mmc init failed: err - %d\n", err);
122 hang();
123 }
Tom Rinia76ff952012-08-14 09:19:44 -0700124 boot_mode = spl_boot_mode();
Simon Schwarze21d2d82011-09-14 15:33:34 -0400125 if (boot_mode == MMCSD_MODE_RAW) {
126 debug("boot mode - RAW\n");
127 mmc_load_image_raw(mmc);
Tom Rinia0d8cca2012-08-10 09:27:14 -0700128#ifdef CONFIG_SPL_FAT_SUPPORT
Simon Schwarze21d2d82011-09-14 15:33:34 -0400129 } else if (boot_mode == MMCSD_MODE_FAT) {
130 debug("boot mode - FAT\n");
131 mmc_load_image_fat(mmc);
Tom Rinia0d8cca2012-08-10 09:27:14 -0700132#endif
Simon Schwarze21d2d82011-09-14 15:33:34 -0400133 } else {
134 puts("spl: wrong MMC boot mode\n");
135 hang();
136 }
137}