blob: 932da56ab6db7bd9daa4617041001377c583a44a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dan Murphycb367e12014-01-16 11:23:30 -06002/*
3 * (C) Copyright 2014
4 * Texas Instruments, <www.ti.com>
5 *
6 * Dan Murphy <dmurphy@ti.com>
7 *
Dan Murphycb367e12014-01-16 11:23:30 -06008 * Derived work from spl_mmc.c
9 */
10
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Dan Murphycb367e12014-01-16 11:23:30 -060012#include <spl.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020013#include <errno.h>
Dan Murphycb367e12014-01-16 11:23:30 -060014#include <usb.h>
15#include <fat.h>
16
Dan Murphycb367e12014-01-16 11:23:30 -060017static int usb_stor_curr_dev = -1; /* current device */
Dan Murphycb367e12014-01-16 11:23:30 -060018
Faiz Abbas05e3a152020-08-03 11:35:04 +053019int spl_usb_load(struct spl_image_info *spl_image,
20 struct spl_boot_device *bootdev, int partition,
21 const char *filename)
Dan Murphycb367e12014-01-16 11:23:30 -060022{
Faiz Abbas5dd935a2020-08-03 11:35:05 +053023 int err = 0;
Simon Glasse3394752016-02-29 15:25:34 -070024 struct blk_desc *stor_dev;
Faiz Abbas5dd935a2020-08-03 11:35:05 +053025 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 }
Dan Murphycb367e12014-01-16 11:23:30 -060032
Dan Murphycb367e12014-01-16 11:23:30 -060033 if (err) {
34#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
35 printf("%s: usb init failed: err - %d\n", __func__, err);
36#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020037 return err;
Dan Murphycb367e12014-01-16 11:23:30 -060038 }
39
Dan Murphycb367e12014-01-16 11:23:30 -060040 /* try to recognize storage devices immediately */
41 usb_stor_curr_dev = usb_stor_scan(1);
Simon Glassfada3f92022-09-17 09:00:09 -060042 stor_dev = blk_get_devnum_by_uclass_id(UCLASS_USB, usb_stor_curr_dev);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020043 if (!stor_dev)
44 return -ENODEV;
Dan Murphycb367e12014-01-16 11:23:30 -060045
46 debug("boot mode - FAT\n");
47
Tom Rinic2e78882021-10-30 23:03:48 -040048#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass3eb382a2016-09-24 18:20:15 -060049 if (spl_start_uboot() ||
Pali Rohárdda8f882022-01-14 14:31:38 +010050 spl_load_image_fat_os(spl_image, bootdev, stor_dev, partition))
Dan Murphycb367e12014-01-16 11:23:30 -060051#endif
Simon Glass3eb382a2016-09-24 18:20:15 -060052 {
Pali Rohárdda8f882022-01-14 14:31:38 +010053 err = spl_load_image_fat(spl_image, bootdev, stor_dev, partition, filename);
Simon Glass3eb382a2016-09-24 18:20:15 -060054 }
Dan Murphycb367e12014-01-16 11:23:30 -060055
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020056 if (err) {
57 puts("Error loading from USB device\n");
58 return err;
59 }
60
61 return 0;
Dan Murphycb367e12014-01-16 11:23:30 -060062}
Faiz Abbas05e3a152020-08-03 11:35:04 +053063
64static 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}
Simon Glass4fc1f252016-11-30 15:30:50 -070071SPL_LOAD_IMAGE_METHOD("USB", 0, BOOT_DEVICE_USB, spl_usb_load_image);