blob: 31fc4b57d486bbc63e0b6c175d0c473ad03cdfdf [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>
13#include <asm/u-boot.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020014#include <errno.h>
Dan Murphycb367e12014-01-16 11:23:30 -060015#include <usb.h>
16#include <fat.h>
17
Dan Murphycb367e12014-01-16 11:23:30 -060018static int usb_stor_curr_dev = -1; /* current device */
Dan Murphycb367e12014-01-16 11:23:30 -060019
Faiz Abbas05e3a152020-08-03 11:35:04 +053020int spl_usb_load(struct spl_image_info *spl_image,
21 struct spl_boot_device *bootdev, int partition,
22 const char *filename)
Dan Murphycb367e12014-01-16 11:23:30 -060023{
Faiz Abbas5dd935a2020-08-03 11:35:05 +053024 int err = 0;
Simon Glasse3394752016-02-29 15:25:34 -070025 struct blk_desc *stor_dev;
Faiz Abbas5dd935a2020-08-03 11:35:05 +053026 static bool usb_init_pending = true;
27
28 if (usb_init_pending) {
29 usb_stop();
30 err = usb_init();
31 usb_init_pending = false;
32 }
Dan Murphycb367e12014-01-16 11:23:30 -060033
Dan Murphycb367e12014-01-16 11:23:30 -060034 if (err) {
35#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
36 printf("%s: usb init failed: err - %d\n", __func__, err);
37#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020038 return err;
Dan Murphycb367e12014-01-16 11:23:30 -060039 }
40
Dan Murphycb367e12014-01-16 11:23:30 -060041 /* try to recognize storage devices immediately */
42 usb_stor_curr_dev = usb_stor_scan(1);
Simon Glassfada3f92022-09-17 09:00:09 -060043 stor_dev = blk_get_devnum_by_uclass_id(UCLASS_USB, usb_stor_curr_dev);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020044 if (!stor_dev)
45 return -ENODEV;
Dan Murphycb367e12014-01-16 11:23:30 -060046
47 debug("boot mode - FAT\n");
48
Tom Rinic2e78882021-10-30 23:03:48 -040049#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass3eb382a2016-09-24 18:20:15 -060050 if (spl_start_uboot() ||
Pali Rohárdda8f882022-01-14 14:31:38 +010051 spl_load_image_fat_os(spl_image, bootdev, stor_dev, partition))
Dan Murphycb367e12014-01-16 11:23:30 -060052#endif
Simon Glass3eb382a2016-09-24 18:20:15 -060053 {
Pali Rohárdda8f882022-01-14 14:31:38 +010054 err = spl_load_image_fat(spl_image, bootdev, stor_dev, partition, filename);
Simon Glass3eb382a2016-09-24 18:20:15 -060055 }
Dan Murphycb367e12014-01-16 11:23:30 -060056
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020057 if (err) {
58 puts("Error loading from USB device\n");
59 return err;
60 }
61
62 return 0;
Dan Murphycb367e12014-01-16 11:23:30 -060063}
Faiz Abbas05e3a152020-08-03 11:35:04 +053064
65static int spl_usb_load_image(struct spl_image_info *spl_image,
66 struct spl_boot_device *bootdev)
67{
68 return spl_usb_load(spl_image, bootdev,
69 CONFIG_SYS_USB_FAT_BOOT_PARTITION,
70 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
71}
Simon Glass4fc1f252016-11-30 15:30:50 -070072SPL_LOAD_IMAGE_METHOD("USB", 0, BOOT_DEVICE_USB, spl_usb_load_image);