xref: /rk3399_rockchip-uboot/common/spl/spl_mmc.c (revision 7b9e980e870294ab36f5b0f36abcb034a4955d29)
1 /*
2  * (C) Copyright 2010
3  * Texas Instruments, <www.ti.com>
4  *
5  * Aneesh V <aneesh@ti.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 #include <common.h>
10 #include <dm.h>
11 #include <spl.h>
12 #include <linux/compiler.h>
13 #include <errno.h>
14 #include <asm/u-boot.h>
15 #include <errno.h>
16 #include <mmc.h>
17 #include <image.h>
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 static int mmc_load_image_raw_sector(struct mmc *mmc, unsigned long sector)
22 {
23 	unsigned long count;
24 	u32 image_size_sectors;
25 	struct image_header *header;
26 
27 	header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
28 					 sizeof(struct image_header));
29 
30 	/* read image header to find the image size & load address */
31 	count = mmc->block_dev.block_read(0, sector, 1, header);
32 	debug("read sector %lx, count=%lu\n", sector, count);
33 	if (count == 0)
34 		goto end;
35 
36 	if (image_get_magic(header) != IH_MAGIC) {
37 		puts("bad magic\n");
38 		return -1;
39 	}
40 
41 	spl_parse_image_header(header);
42 
43 	/* convert size to sectors - round up */
44 	image_size_sectors = (spl_image.size + mmc->read_bl_len - 1) /
45 			     mmc->read_bl_len;
46 
47 	/* Read the header too to avoid extra memcpy */
48 	count = mmc->block_dev.block_read(0, sector, image_size_sectors,
49 					  (void *)(ulong)spl_image.load_addr);
50 	debug("read %x sectors to %x\n", image_size_sectors,
51 	      spl_image.load_addr);
52 
53 end:
54 	if (count == 0) {
55 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
56 		puts("spl: mmc block read error\n");
57 #endif
58 		return -1;
59 	}
60 
61 	return 0;
62 }
63 
64 #ifdef CONFIG_DM_MMC
65 static int spl_mmc_find_device(struct mmc **mmc)
66 {
67 	struct udevice *dev;
68 	int err;
69 
70 	err = mmc_initialize(NULL);
71 	if (err) {
72 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
73 		printf("spl: could not initialize mmc. error: %d\n", err);
74 #endif
75 		return err;
76 	}
77 
78 	err = uclass_get_device(UCLASS_MMC, 0, &dev);
79 	if (err) {
80 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
81 		printf("spl: could not find mmc device. error: %d\n", err);
82 #endif
83 		return err;
84 	}
85 
86 	*mmc = NULL;
87 	*mmc = mmc_get_mmc_dev(dev);
88 	return *mmc != NULL ? 0 : -ENODEV;
89 }
90 #else
91 static int spl_mmc_find_device(struct mmc **mmc)
92 {
93 	int err;
94 
95 	err = mmc_initialize(gd->bd);
96 	if (err) {
97 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
98 		printf("spl: could not initialize mmc. error: %d\n", err);
99 #endif
100 		return err;
101 	}
102 
103 	/* We register only one device. So, the dev id is always 0 */
104 	*mmc = find_mmc_device(0);
105 	if (!*mmc) {
106 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
107 		puts("spl: mmc device not found\n");
108 #endif
109 		return -ENODEV;
110 	}
111 
112 	return 0;
113 }
114 #endif
115 
116 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
117 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
118 {
119 	disk_partition_t info;
120 	int err;
121 
122 	err = get_partition_info(&mmc->block_dev, partition, &info);
123 	if (err) {
124 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
125 		puts("spl: partition error\n");
126 #endif
127 		return -1;
128 	}
129 
130 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
131 	return mmc_load_image_raw_sector(mmc, info.start +
132 					 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
133 #else
134 	return mmc_load_image_raw_sector(mmc, info.start);
135 #endif
136 }
137 #else
138 #define CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION -1
139 static int mmc_load_image_raw_partition(struct mmc *mmc, int partition)
140 {
141 	return -ENOSYS;
142 }
143 #endif
144 
145 #ifdef CONFIG_SPL_OS_BOOT
146 static int mmc_load_image_raw_os(struct mmc *mmc)
147 {
148 	unsigned long count;
149 
150 	count = mmc->block_dev.block_read(0,
151 		CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
152 		CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
153 		(void *) CONFIG_SYS_SPL_ARGS_ADDR);
154 	if (count == 0) {
155 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
156 		puts("spl: mmc block read error\n");
157 #endif
158 		return -1;
159 	}
160 
161 	return mmc_load_image_raw_sector(mmc,
162 		CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
163 }
164 #else
165 int spl_start_uboot(void)
166 {
167 	return 1;
168 }
169 static int mmc_load_image_raw_os(struct mmc *mmc)
170 {
171 	return -ENOSYS;
172 }
173 #endif
174 
175 #ifdef CONFIG_SYS_MMCSD_FS_BOOT_PARTITION
176 int spl_mmc_do_fs_boot(struct mmc *mmc)
177 {
178 	int err = -ENOSYS;
179 
180 #ifdef CONFIG_SPL_FAT_SUPPORT
181 	if (!spl_start_uboot()) {
182 		err = spl_load_image_fat_os(&mmc->block_dev,
183 			CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
184 		if (!err)
185 			return err;
186 	}
187 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
188 	err = spl_load_image_fat(&mmc->block_dev,
189 				 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
190 				 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
191 	if (!err)
192 		return err;
193 #endif
194 #endif
195 #ifdef CONFIG_SPL_EXT_SUPPORT
196 	if (!spl_start_uboot()) {
197 		err = spl_load_image_ext_os(&mmc->block_dev,
198 			CONFIG_SYS_MMCSD_FS_BOOT_PARTITION);
199 		if (!err)
200 			return err;
201 	}
202 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
203 	err = spl_load_image_ext(&mmc->block_dev,
204 				 CONFIG_SYS_MMCSD_FS_BOOT_PARTITION,
205 				 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
206 	if (!err)
207 		return err;
208 #endif
209 #endif
210 
211 #if defined(CONFIG_SPL_FAT_SUPPORT) || defined(CONFIG_SPL_EXT_SUPPORT)
212 	err = -ENOENT;
213 #endif
214 
215 	return err;
216 }
217 #else
218 int spl_mmc_do_fs_boot(struct mmc *mmc)
219 {
220 	return -ENOSYS;
221 }
222 #endif
223 
224 int spl_mmc_load_image(void)
225 {
226 	struct mmc *mmc;
227 	u32 boot_mode;
228 	int err = 0;
229 	__maybe_unused int part;
230 
231 	err = spl_mmc_find_device(&mmc);
232 	if (err)
233 		return err;
234 
235 	err = mmc_init(mmc);
236 	if (err) {
237 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
238 		printf("spl: mmc init failed with error: %d\n", err);
239 #endif
240 		return err;
241 	}
242 
243 	boot_mode = spl_boot_mode();
244 	err = -EINVAL;
245 	switch (boot_mode) {
246 	case MMCSD_MODE_EMMCBOOT:
247 			/*
248 			 * We need to check what the partition is configured to.
249 			 * 1 and 2 match up to boot0 / boot1 and 7 is user data
250 			 * which is the first physical partition (0).
251 			 */
252 			part = (mmc->part_config >> 3) & PART_ACCESS_MASK;
253 
254 			if (part == 7)
255 				part = 0;
256 
257 			err = mmc_switch_part(0, part);
258 			if (err) {
259 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
260 				puts("spl: mmc partition switch failed\n");
261 #endif
262 				return err;
263 			}
264 			/* Fall through */
265 	case MMCSD_MODE_RAW:
266 		debug("spl: mmc boot mode: raw\n");
267 
268 		if (!spl_start_uboot()) {
269 			err = mmc_load_image_raw_os(mmc);
270 			if (!err)
271 				return err;
272 		}
273 
274 		err = mmc_load_image_raw_partition(mmc,
275 			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION);
276 		if (!err)
277 			return err;
278 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR)
279 		err = mmc_load_image_raw_sector(mmc,
280 			CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
281 		if (!err)
282 			return err;
283 #endif
284 		break;
285 	case MMCSD_MODE_FS:
286 		debug("spl: mmc boot mode: fs\n");
287 
288 		err = spl_mmc_do_fs_boot(mmc);
289 		if (!err)
290 			return err;
291 
292 		break;
293 	case MMCSD_MODE_UNDEFINED:
294 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
295 	default:
296 		puts("spl: mmc: wrong boot mode\n");
297 #endif
298 	}
299 
300 	return err;
301 }
302