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