spl_mmc: mmc_load_image_raw(): Add sector argument
[platform/kernel/u-boot.git] / common / spl / spl_mmc.c
1 /*
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>
26 #include <spl.h>
27 #include <asm/u-boot.h>
28 #include <asm/utils.h>
29 #include <mmc.h>
30 #include <fat.h>
31 #include <version.h>
32
33 DECLARE_GLOBAL_DATA_PTR;
34
35 static int mmc_load_image_raw(struct mmc *mmc, unsigned long sector)
36 {
37         unsigned long err;
38         u32 image_size_sectors;
39         struct image_header *header;
40
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, sector, 1, header);
46         if (err == 0)
47                 goto end;
48
49         spl_parse_image_header(header);
50
51         /* convert size to sectors - round up */
52         image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
53                                 mmc->read_bl_len;
54
55         /* Read the header too to avoid extra memcpy */
56         err = mmc->block_dev.block_read(0, sector, image_size_sectors,
57                                         (void *)spl_image.load_addr);
58
59 end:
60         if (err == 0)
61                 printf("spl: mmc blk read err - %lu\n", err);
62
63         return (err == 0);
64 }
65
66 #ifdef CONFIG_SPL_FAT_SUPPORT
67 static int mmc_load_image_fat(struct mmc *mmc, const char *filename)
68 {
69         int err;
70         struct image_header *header;
71
72         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
73                                                 sizeof(struct image_header));
74
75         err = file_fat_read(filename, header, sizeof(struct image_header));
76         if (err <= 0)
77                 goto end;
78
79         spl_parse_image_header(header);
80
81         err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
82
83 end:
84         if (err <= 0)
85                 printf("spl: error reading image %s, err - %d\n",
86                        filename, err);
87
88         return (err <= 0);
89 }
90
91 #ifdef CONFIG_SPL_OS_BOOT
92 static int mmc_load_image_fat_os(struct mmc *mmc)
93 {
94         int err;
95
96         err = file_fat_read(CONFIG_SPL_FAT_LOAD_ARGS_NAME,
97                             (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
98         if (err <= 0) {
99                 printf("spl: error reading image %s, err - %d\n",
100                        CONFIG_SPL_FAT_LOAD_ARGS_NAME, err);
101                 return -1;
102         }
103
104         return mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_KERNEL_NAME);
105 }
106 #endif
107
108 #endif
109
110 void spl_mmc_load_image(void)
111 {
112         struct mmc *mmc;
113         int err;
114         u32 boot_mode;
115
116         mmc_initialize(gd->bd);
117         /* We register only one device. So, the dev id is always 0 */
118         mmc = find_mmc_device(0);
119         if (!mmc) {
120                 puts("spl: mmc device not found!!\n");
121                 hang();
122         }
123
124         err = mmc_init(mmc);
125         if (err) {
126                 printf("spl: mmc init failed: err - %d\n", err);
127                 hang();
128         }
129
130         boot_mode = spl_boot_mode();
131         if (boot_mode == MMCSD_MODE_RAW) {
132                 debug("boot mode - RAW\n");
133                 err = mmc_load_image_raw(mmc,
134                                          CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
135 #ifdef CONFIG_SPL_FAT_SUPPORT
136         } else if (boot_mode == MMCSD_MODE_FAT) {
137                 debug("boot mode - FAT\n");
138
139                 err = fat_register_device(&mmc->block_dev,
140                                           CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
141                 if (err) {
142                         printf("spl: fat register err - %d\n", err);
143                         hang();
144                 }
145
146 #ifdef CONFIG_SPL_OS_BOOT
147                 if (spl_start_uboot() || mmc_load_image_fat_os(mmc))
148 #endif
149                 err = mmc_load_image_fat(mmc, CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME);
150 #endif
151         } else {
152                 puts("spl: wrong MMC boot mode\n");
153                 hang();
154         }
155
156         if (err)
157                 hang();
158 }