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 <log.h>
12 #include <spl.h>
13 #include <errno.h>
14 #include <usb.h>
15 #include <fat.h>
16
17 static int usb_stor_curr_dev = -1; /* current device */
18
spl_usb_load(struct spl_image_info * spl_image,struct spl_boot_device * bootdev,int partition,const char * filename)19 int spl_usb_load(struct spl_image_info *spl_image,
20 struct spl_boot_device *bootdev, int partition,
21 const char *filename)
22 {
23 int err = 0;
24 struct blk_desc *stor_dev;
25 static bool usb_init_pending = true;
26
27 if (usb_init_pending) {
28 usb_stop();
29 err = usb_init();
30 usb_init_pending = false;
31 }
32
33 if (err) {
34 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
35 printf("%s: usb init failed: err - %d\n", __func__, err);
36 #endif
37 return err;
38 }
39
40 /* try to recognize storage devices immediately */
41 usb_stor_curr_dev = usb_stor_scan(1);
42 stor_dev = blk_get_devnum_by_uclass_id(UCLASS_USB, usb_stor_curr_dev);
43 if (!stor_dev)
44 return -ENODEV;
45
46 debug("boot mode - FAT\n");
47
48 #if CONFIG_IS_ENABLED(OS_BOOT)
49 if (spl_start_uboot() ||
50 spl_load_image_fat_os(spl_image, bootdev, stor_dev, partition))
51 #endif
52 {
53 err = spl_load_image_fat(spl_image, bootdev, stor_dev, partition, filename);
54 }
55
56 if (err) {
57 puts("Error loading from USB device\n");
58 return err;
59 }
60
61 return 0;
62 }
63
spl_usb_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)64 static int spl_usb_load_image(struct spl_image_info *spl_image,
65 struct spl_boot_device *bootdev)
66 {
67 return spl_usb_load(spl_image, bootdev,
68 CONFIG_SYS_USB_FAT_BOOT_PARTITION,
69 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
70 }
71 SPL_LOAD_IMAGE_METHOD("USB", 0, BOOT_DEVICE_USB, spl_usb_load_image);
72