1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2010
4 * Texas Instruments, <www.ti.com>
5 *
6 * Aneesh V <aneesh@ti.com>
7 */
8 #include <dm.h>
9 #include <log.h>
10 #include <part.h>
11 #include <spl.h>
12 #include <spl_load.h>
13 #include <linux/compiler.h>
14 #include <errno.h>
15 #include <errno.h>
16 #include <mmc.h>
17 #include <image.h>
18 #include <imx_container.h>
19
h_spl_load_read(struct spl_load_info * load,ulong off,ulong size,void * buf)20 static ulong h_spl_load_read(struct spl_load_info *load, ulong off,
21 ulong size, void *buf)
22 {
23 struct blk_desc *bd = load->priv;
24 lbaint_t sector = off >> bd->log2blksz;
25 lbaint_t count = size >> bd->log2blksz;
26
27 return blk_dread(bd, sector, count, buf) << bd->log2blksz;
28 }
29
spl_mmc_raw_uboot_offset(int part)30 static __maybe_unused unsigned long spl_mmc_raw_uboot_offset(int part)
31 {
32 #if IS_ENABLED(CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR)
33 if (part == 0)
34 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_DATA_PART_OFFSET;
35 #endif
36
37 return 0;
38 }
39
40 static __maybe_unused
mmc_load_image_raw_sector(struct spl_image_info * spl_image,struct spl_boot_device * bootdev,struct mmc * mmc,unsigned long sector)41 int mmc_load_image_raw_sector(struct spl_image_info *spl_image,
42 struct spl_boot_device *bootdev,
43 struct mmc *mmc, unsigned long sector)
44 {
45 int ret;
46 struct blk_desc *bd = mmc_get_blk_desc(mmc);
47 struct spl_load_info load;
48
49 spl_load_init(&load, h_spl_load_read, bd, bd->blksz);
50 ret = spl_load(spl_image, bootdev, &load, 0, sector << bd->log2blksz);
51 if (ret) {
52 puts("mmc_load_image_raw_sector: mmc block read error\n");
53 log_debug("(error=%d)\n", ret);
54 return ret;
55 }
56
57 return 0;
58 }
59
spl_mmc_get_device_index(uint boot_device)60 static int spl_mmc_get_device_index(uint boot_device)
61 {
62 switch (boot_device) {
63 case BOOT_DEVICE_MMC1:
64 return 0;
65 case BOOT_DEVICE_MMC2:
66 case BOOT_DEVICE_MMC2_2:
67 return 1;
68 }
69
70 printf("spl: unsupported mmc boot device.\n");
71
72 return -ENODEV;
73 }
74
spl_mmc_find_device(struct mmc ** mmcp,int mmc_dev)75 static int spl_mmc_find_device(struct mmc **mmcp, int mmc_dev)
76 {
77 int ret;
78
79 #if CONFIG_IS_ENABLED(DM_MMC)
80 struct udevice *dev;
81 struct uclass *uc;
82
83 log_debug("Selecting MMC dev %d; seqs:\n", mmc_dev);
84 if (_LOG_DEBUG) {
85 uclass_id_foreach_dev(UCLASS_MMC, dev, uc)
86 log_debug("%d: %s\n", dev_seq(dev), dev->name);
87 }
88 ret = mmc_init_device(mmc_dev);
89 #else
90 ret = mmc_initialize(NULL);
91 #endif /* DM_MMC */
92 if (ret) {
93 printf("spl: could not initialize mmc. error: %d\n", ret);
94 return ret;
95 }
96 *mmcp = find_mmc_device(mmc_dev);
97 ret = *mmcp ? 0 : -ENODEV;
98 if (ret) {
99 printf("spl: could not find mmc device %d. error: %d\n",
100 mmc_dev, ret);
101 return ret;
102 }
103 #if CONFIG_IS_ENABLED(DM_MMC)
104 log_debug("mmc %d: %s\n", mmc_dev, (*mmcp)->dev->name);
105 #endif
106
107 return 0;
108 }
109
110 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
mmc_load_image_raw_partition(struct spl_image_info * spl_image,struct spl_boot_device * bootdev,struct mmc * mmc,int partition,unsigned long sector)111 static int mmc_load_image_raw_partition(struct spl_image_info *spl_image,
112 struct spl_boot_device *bootdev,
113 struct mmc *mmc, int partition,
114 unsigned long sector)
115 {
116 struct disk_partition info;
117 int ret;
118
119 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION_TYPE
120 int type_part;
121 /* Only support MBR so DOS_ENTRY_NUMBERS */
122 for (type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
123 ret = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
124 if (ret)
125 continue;
126 if (info.sys_ind ==
127 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_TYPE) {
128 partition = type_part;
129 break;
130 }
131 }
132 #endif
133
134 ret = part_get_info(mmc_get_blk_desc(mmc), partition, &info);
135 if (ret) {
136 puts("spl: partition error\n");
137 return ret;
138 }
139
140 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
141 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start + sector);
142 #else
143 return mmc_load_image_raw_sector(spl_image, bootdev, mmc, info.start);
144 #endif
145 }
146 #endif
147
148 #if CONFIG_IS_ENABLED(FALCON_BOOT_MMCSD)
mmc_load_image_raw_os(struct spl_image_info * spl_image,struct spl_boot_device * bootdev,struct mmc * mmc)149 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
150 struct spl_boot_device *bootdev,
151 struct mmc *mmc)
152 {
153 int ret;
154
155 #if defined(CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR)
156 unsigned long count;
157
158 count = blk_dread(mmc_get_blk_desc(mmc),
159 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR,
160 CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS,
161 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR);
162 if (count != CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTORS) {
163 puts("mmc_load_image_raw_os: mmc block read error\n");
164 return -EIO;
165 }
166 #endif /* CONFIG_SYS_MMCSD_RAW_MODE_ARGS_SECTOR */
167
168 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
169 CONFIG_SYS_MMCSD_RAW_MODE_KERNEL_SECTOR);
170 if (ret)
171 return ret;
172
173 if (spl_image->os != IH_OS_LINUX && spl_image->os != IH_OS_TEE) {
174 puts("Expected image is not found. Trying to start U-Boot\n");
175 return -ENOENT;
176 }
177
178 return 0;
179 }
180 #else
mmc_load_image_raw_os(struct spl_image_info * spl_image,struct spl_boot_device * bootdev,struct mmc * mmc)181 static int mmc_load_image_raw_os(struct spl_image_info *spl_image,
182 struct spl_boot_device *bootdev,
183 struct mmc *mmc)
184 {
185 return -ENOSYS;
186 }
187 #endif
188
189 #ifndef CONFIG_SPL_OS_BOOT
spl_start_uboot(void)190 int spl_start_uboot(void)
191 {
192 return 1;
193 }
194 #endif
195
196 #ifdef CONFIG_SYS_MMCSD_FS_BOOT
spl_mmc_do_fs_boot(struct spl_image_info * spl_image,struct spl_boot_device * bootdev,struct mmc * mmc,const char * filename)197 static int spl_mmc_do_fs_boot(struct spl_image_info *spl_image,
198 struct spl_boot_device *bootdev,
199 struct mmc *mmc,
200 const char *filename)
201 {
202 int ret = -ENOSYS;
203
204 __maybe_unused int partition = CONFIG_SYS_MMCSD_FS_BOOT_PARTITION;
205
206 #if CONFIG_SYS_MMCSD_FS_BOOT_PARTITION == -1
207 {
208 struct disk_partition info;
209 debug("Checking for the first MBR bootable partition\n");
210 for (int type_part = 1; type_part <= DOS_ENTRY_NUMBERS; type_part++) {
211 ret = part_get_info(mmc_get_blk_desc(mmc), type_part, &info);
212 if (ret)
213 continue;
214 debug("Partition %d is of type %d and bootable=%d\n", type_part, info.sys_ind, info.bootable);
215 if (info.bootable != 0) {
216 debug("Partition %d is bootable, using it\n", type_part);
217 partition = type_part;
218 break;
219 }
220 }
221 printf("Using first bootable partition: %d\n", partition);
222 if (partition == CONFIG_SYS_MMCSD_FS_BOOT_PARTITION) {
223 return -ENOSYS;
224 }
225 }
226 #endif
227
228 #ifdef CONFIG_SPL_FS_FAT
229 if (!spl_start_uboot()) {
230 ret = spl_load_image_fat_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
231 partition);
232 if (!ret)
233 return 0;
234 }
235 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
236 ret = spl_load_image_fat(spl_image, bootdev, mmc_get_blk_desc(mmc),
237 partition,
238 filename);
239 if (!ret)
240 return ret;
241 #endif
242 #endif
243 #ifdef CONFIG_SPL_FS_EXT4
244 if (!spl_start_uboot()) {
245 ret = spl_load_image_ext_os(spl_image, bootdev, mmc_get_blk_desc(mmc),
246 partition);
247 if (!ret)
248 return 0;
249 }
250 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
251 ret = spl_load_image_ext(spl_image, bootdev, mmc_get_blk_desc(mmc),
252 partition,
253 filename);
254 if (!ret)
255 return 0;
256 #endif
257 #endif
258
259 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
260 ret = -ENOENT;
261 #endif
262
263 return ret;
264 }
265 #endif
266
spl_mmc_boot_mode(struct mmc * mmc,const u32 boot_device)267 u32 __weak spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
268 {
269 #if defined(CONFIG_SPL_FS_FAT) || defined(CONFIG_SPL_FS_EXT4)
270 return MMCSD_MODE_FS;
271 #elif defined(CONFIG_SUPPORT_EMMC_BOOT)
272 return MMCSD_MODE_EMMCBOOT;
273 #else
274 return MMCSD_MODE_RAW;
275 #endif
276 }
277
278 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
spl_mmc_boot_partition(const u32 boot_device)279 int __weak spl_mmc_boot_partition(const u32 boot_device)
280 {
281 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
282 }
283 #endif
284
arch_spl_mmc_get_uboot_raw_sector(struct mmc * mmc,unsigned long raw_sect)285 unsigned long __weak arch_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
286 unsigned long raw_sect)
287 {
288 return raw_sect;
289 }
290
board_spl_mmc_get_uboot_raw_sector(struct mmc * mmc,unsigned long raw_sect)291 unsigned long __weak board_spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
292 unsigned long raw_sect)
293 {
294 return arch_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
295 }
296
spl_mmc_get_uboot_raw_sector(struct mmc * mmc,unsigned long raw_sect)297 unsigned long __weak spl_mmc_get_uboot_raw_sector(struct mmc *mmc,
298 unsigned long raw_sect)
299 {
300 return board_spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
301 }
302
default_spl_mmc_emmc_boot_partition(struct mmc * mmc)303 int default_spl_mmc_emmc_boot_partition(struct mmc *mmc)
304 {
305 int part;
306 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION
307 part = CONFIG_SYS_MMCSD_RAW_MODE_EMMC_BOOT_PARTITION;
308 #else
309 /*
310 * We need to check what the partition is configured to.
311 * 1 and 2 match up to boot0 / boot1 and 7 is user data
312 * which is the first physical partition (0).
313 */
314 part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
315 if (part == EMMC_BOOT_PART_USER)
316 part = EMMC_HWPART_DEFAULT;
317 #endif
318 return part;
319 }
320
spl_mmc_emmc_boot_partition(struct mmc * mmc)321 int __weak spl_mmc_emmc_boot_partition(struct mmc *mmc)
322 {
323 return default_spl_mmc_emmc_boot_partition(mmc);
324 }
325
spl_mmc_get_mmc_devnum(struct mmc * mmc)326 static int spl_mmc_get_mmc_devnum(struct mmc *mmc)
327 {
328 struct blk_desc *block_dev;
329 #if !CONFIG_IS_ENABLED(BLK)
330 block_dev = &mmc->block_dev;
331 #else
332 block_dev = mmc_get_blk_desc(mmc);
333 #endif
334 return block_dev->devnum;
335 }
336
337 static struct mmc *mmc;
338
spl_mmc_clear_cache(void)339 void spl_mmc_clear_cache(void)
340 {
341 mmc = NULL;
342 }
343
spl_mmc_load(struct spl_image_info * spl_image,struct spl_boot_device * bootdev,const char * filename,int raw_part,unsigned long raw_sect)344 int spl_mmc_load(struct spl_image_info *spl_image,
345 struct spl_boot_device *bootdev,
346 const char *filename,
347 int raw_part,
348 unsigned long raw_sect)
349 {
350 u32 boot_mode;
351 int ret = 0;
352 __maybe_unused int part = 0;
353 int mmc_dev;
354
355 /* Perform peripheral init only once for an mmc device */
356 mmc_dev = spl_mmc_get_device_index(bootdev->boot_device);
357 log_debug("boot_device=%d, mmc_dev=%d\n", bootdev->boot_device,
358 mmc_dev);
359 if (!mmc || spl_mmc_get_mmc_devnum(mmc) != mmc_dev) {
360 ret = spl_mmc_find_device(&mmc, mmc_dev);
361 if (ret)
362 return ret;
363
364 ret = mmc_init(mmc);
365 if (ret) {
366 mmc = NULL;
367 printf("spl: mmc init failed with error: %d\n", ret);
368 return ret;
369 }
370 }
371
372 boot_mode = spl_mmc_boot_mode(mmc, bootdev->boot_device);
373 ret = -EINVAL;
374 switch (boot_mode) {
375 case MMCSD_MODE_EMMCBOOT:
376 part = spl_mmc_emmc_boot_partition(mmc);
377
378 if (CONFIG_IS_ENABLED(MMC_TINY))
379 ret = mmc_switch_part(mmc, part);
380 else
381 ret = blk_dselect_hwpart(mmc_get_blk_desc(mmc), part);
382
383 if (ret) {
384 puts("spl: mmc partition switch failed\n");
385 return ret;
386 }
387 /* Fall through */
388 case MMCSD_MODE_RAW:
389 debug("spl: mmc boot mode: raw\n");
390
391 if (!spl_start_uboot()) {
392 ret = mmc_load_image_raw_os(spl_image, bootdev, mmc);
393 if (!ret)
394 return 0;
395 }
396
397 raw_sect = spl_mmc_get_uboot_raw_sector(mmc, raw_sect);
398
399 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
400 ret = mmc_load_image_raw_partition(spl_image, bootdev,
401 mmc, raw_part,
402 raw_sect);
403 if (!ret)
404 return 0;
405 #endif
406 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_SECTOR
407 ret = mmc_load_image_raw_sector(spl_image, bootdev, mmc,
408 raw_sect +
409 spl_mmc_raw_uboot_offset(part));
410 if (!ret)
411 return 0;
412 #endif
413 /* If RAW mode fails, try FS mode. */
414 fallthrough;
415 #ifdef CONFIG_SYS_MMCSD_FS_BOOT
416 case MMCSD_MODE_FS:
417 debug("spl: mmc boot mode: fs\n");
418
419 ret = spl_mmc_do_fs_boot(spl_image, bootdev, mmc, filename);
420 if (!ret)
421 return 0;
422
423 break;
424 #endif
425 default:
426 puts("spl: mmc: wrong boot mode\n");
427 }
428
429 return ret;
430 }
431
spl_mmc_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)432 int spl_mmc_load_image(struct spl_image_info *spl_image,
433 struct spl_boot_device *bootdev)
434 {
435 return spl_mmc_load(spl_image, bootdev,
436 #ifdef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
437 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
438 #else
439 NULL,
440 #endif
441 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION
442 spl_mmc_boot_partition(bootdev->boot_device),
443 #else
444 0,
445 #endif
446 #ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR
447 CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR);
448 #else
449 0);
450 #endif
451 }
452
453 SPL_LOAD_IMAGE_METHOD("MMC1", 0, BOOT_DEVICE_MMC1, spl_mmc_load_image);
454 SPL_LOAD_IMAGE_METHOD("MMC2", 0, BOOT_DEVICE_MMC2, spl_mmc_load_image);
455 SPL_LOAD_IMAGE_METHOD("MMC2_2", 0, BOOT_DEVICE_MMC2_2, spl_mmc_load_image);
456