blob: 92ae96f66ef3fd77d41cb118b5df9c136d1248a3 [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
11#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Dan Murphycb367e12014-01-16 11:23:30 -060013#include <spl.h>
14#include <asm/u-boot.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020015#include <errno.h>
Dan Murphycb367e12014-01-16 11:23:30 -060016#include <usb.h>
17#include <fat.h>
18
Dan Murphycb367e12014-01-16 11:23:30 -060019static int usb_stor_curr_dev = -1; /* current device */
Dan Murphycb367e12014-01-16 11:23:30 -060020
Faiz Abbas05e3a152020-08-03 11:35:04 +053021int spl_usb_load(struct spl_image_info *spl_image,
22 struct spl_boot_device *bootdev, int partition,
23 const char *filename)
Dan Murphycb367e12014-01-16 11:23:30 -060024{
25 int err;
Simon Glasse3394752016-02-29 15:25:34 -070026 struct blk_desc *stor_dev;
Dan Murphycb367e12014-01-16 11:23:30 -060027
28 usb_stop();
29 err = usb_init();
30 if (err) {
31#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
32 printf("%s: usb init failed: err - %d\n", __func__, err);
33#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020034 return err;
Dan Murphycb367e12014-01-16 11:23:30 -060035 }
36
Dan Murphycb367e12014-01-16 11:23:30 -060037 /* try to recognize storage devices immediately */
38 usb_stor_curr_dev = usb_stor_scan(1);
Simon Glasscf437782016-05-01 11:36:13 -060039 stor_dev = blk_get_devnum_by_type(IF_TYPE_USB, usb_stor_curr_dev);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020040 if (!stor_dev)
41 return -ENODEV;
Dan Murphycb367e12014-01-16 11:23:30 -060042
43 debug("boot mode - FAT\n");
44
45#ifdef CONFIG_SPL_OS_BOOT
Simon Glass3eb382a2016-09-24 18:20:15 -060046 if (spl_start_uboot() ||
Faiz Abbas05e3a152020-08-03 11:35:04 +053047 spl_load_image_fat_os(spl_image, stor_dev, partition))
Dan Murphycb367e12014-01-16 11:23:30 -060048#endif
Simon Glass3eb382a2016-09-24 18:20:15 -060049 {
Faiz Abbas05e3a152020-08-03 11:35:04 +053050 err = spl_load_image_fat(spl_image, stor_dev, partition, filename);
Simon Glass3eb382a2016-09-24 18:20:15 -060051 }
Dan Murphycb367e12014-01-16 11:23:30 -060052
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020053 if (err) {
54 puts("Error loading from USB device\n");
55 return err;
56 }
57
58 return 0;
Dan Murphycb367e12014-01-16 11:23:30 -060059}
Faiz Abbas05e3a152020-08-03 11:35:04 +053060
61static int spl_usb_load_image(struct spl_image_info *spl_image,
62 struct spl_boot_device *bootdev)
63{
64 return spl_usb_load(spl_image, bootdev,
65 CONFIG_SYS_USB_FAT_BOOT_PARTITION,
66 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
67}
Simon Glass4fc1f252016-11-30 15:30:50 -070068SPL_LOAD_IMAGE_METHOD("USB", 0, BOOT_DEVICE_USB, spl_usb_load_image);