blob: 6f5b43e559f3b80b44b67f90f55844b1d3c12553 [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>
26#include <asm/u-boot.h>
27#include <asm/utils.h>
28#include <asm/arch/sys_proto.h>
29#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#include <asm/omap_common.h>
33#include <asm/arch/mmc_host_def.h>
34
35DECLARE_GLOBAL_DATA_PTR;
36
37#ifdef CONFIG_GENERIC_MMC
38int board_mmc_init(bd_t *bis)
39{
40 switch (omap_boot_device()) {
41 case BOOT_DEVICE_MMC1:
42 omap_mmc_init(0);
43 break;
44 case BOOT_DEVICE_MMC2:
45 omap_mmc_init(1);
46 break;
47 }
48 return 0;
49}
50#endif
51
52static void mmc_load_image_raw(struct mmc *mmc)
53{
54 u32 image_size_sectors, err;
55 const struct image_header *header;
56
57 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
58 sizeof(struct image_header));
59
60 /* read image header to find the image size & load address */
61 err = mmc->block_dev.block_read(0,
62 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR, 1,
63 (void *)header);
64
65 if (err <= 0)
66 goto end;
67
68 spl_parse_image_header(header);
69
70 /* convert size to sectors - round up */
71 image_size_sectors = (spl_image.size + MMCSD_SECTOR_SIZE - 1) /
72 MMCSD_SECTOR_SIZE;
73
74 /* Read the header too to avoid extra memcpy */
75 err = mmc->block_dev.block_read(0,
76 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR,
77 image_size_sectors, (void *)spl_image.load_addr);
78
79end:
80 if (err <= 0) {
81 printf("spl: mmc blk read err - %d\n", err);
82 hang();
83 }
84}
85
86static void mmc_load_image_fat(struct mmc *mmc)
87{
88 s32 err;
89 struct image_header *header;
90
91 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
92 sizeof(struct image_header));
93
94 err = fat_register_device(&mmc->block_dev,
95 CONFIG_SYS_MMC_SD_FAT_BOOT_PARTITION);
96 if (err) {
97 printf("spl: fat register err - %d\n", err);
98 hang();
99 }
100
101 err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
102 (u8 *)header, sizeof(struct image_header));
103 if (err <= 0)
104 goto end;
105
106 spl_parse_image_header(header);
107
108 err = file_fat_read(CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME,
109 (u8 *)spl_image.load_addr, 0);
110
111end:
112 if (err <= 0) {
113 printf("spl: error reading image %s, err - %d\n",
114 CONFIG_SPL_FAT_LOAD_PAYLOAD_NAME, err);
115 hang();
116 }
117}
118
119void spl_mmc_load_image(void)
120{
121 struct mmc *mmc;
122 int err;
123 u32 boot_mode;
124
125 mmc_initialize(gd->bd);
126 /* We register only one device. So, the dev id is always 0 */
127 mmc = find_mmc_device(0);
128 if (!mmc) {
129 puts("spl: mmc device not found!!\n");
130 hang();
131 }
132
133 err = mmc_init(mmc);
134 if (err) {
135 printf("spl: mmc init failed: err - %d\n", err);
136 hang();
137 }
138 boot_mode = omap_boot_mode();
139 if (boot_mode == MMCSD_MODE_RAW) {
140 debug("boot mode - RAW\n");
141 mmc_load_image_raw(mmc);
142 } else if (boot_mode == MMCSD_MODE_FAT) {
143 debug("boot mode - FAT\n");
144 mmc_load_image_fat(mmc);
145 } else {
146 puts("spl: wrong MMC boot mode\n");
147 hang();
148 }
149}