Merge tag 'xilinx-for-v2020.10' of https://gitlab.denx.de/u-boot/custodians/u-boot...
[platform/kernel/u-boot.git] / common / spl / spl_usb.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014
4  * Texas Instruments, <www.ti.com>
5  *
6  * Dan Murphy <dmurphy@ti.com>
7  *
8  * Derived work from spl_mmc.c
9  */
10
11 #include <common.h>
12 #include <log.h>
13 #include <spl.h>
14 #include <asm/u-boot.h>
15 #include <errno.h>
16 #include <usb.h>
17 #include <fat.h>
18
19 static int usb_stor_curr_dev = -1; /* current device */
20
21 static int spl_usb_load_image(struct spl_image_info *spl_image,
22                               struct spl_boot_device *bootdev)
23 {
24         int err;
25         struct blk_desc *stor_dev;
26
27         usb_stop();
28         err = usb_init();
29         if (err) {
30 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
31                 printf("%s: usb init failed: err - %d\n", __func__, err);
32 #endif
33                 return err;
34         }
35
36         /* try to recognize storage devices immediately */
37         usb_stor_curr_dev = usb_stor_scan(1);
38         stor_dev = blk_get_devnum_by_type(IF_TYPE_USB, usb_stor_curr_dev);
39         if (!stor_dev)
40                 return -ENODEV;
41
42         debug("boot mode - FAT\n");
43
44 #ifdef CONFIG_SPL_OS_BOOT
45         if (spl_start_uboot() ||
46             spl_load_image_fat_os(spl_image, stor_dev,
47                                   CONFIG_SYS_USB_FAT_BOOT_PARTITION))
48 #endif
49         {
50                 err = spl_load_image_fat(spl_image, stor_dev,
51                                         CONFIG_SYS_USB_FAT_BOOT_PARTITION,
52                                         CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
53         }
54
55         if (err) {
56                 puts("Error loading from USB device\n");
57                 return err;
58         }
59
60         return 0;
61 }
62 SPL_LOAD_IMAGE_METHOD("USB", 0, BOOT_DEVICE_USB, spl_usb_load_image);