Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 |
| 4 | * Texas Instruments, <www.ti.com> |
| 5 | * |
| 6 | * Dan Murphy <dmurphy@ti.com> |
| 7 | * |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 8 | * Derived work from spl_mmc.c |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 13 | #include <spl.h> |
| 14 | #include <asm/u-boot.h> |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 15 | #include <errno.h> |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 16 | #include <usb.h> |
| 17 | #include <fat.h> |
| 18 | |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 19 | static int usb_stor_curr_dev = -1; /* current device */ |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 20 | |
Faiz Abbas | c3ab97c | 2020-08-03 11:35:04 +0530 | [diff] [blame] | 21 | int spl_usb_load(struct spl_image_info *spl_image, |
| 22 | struct spl_boot_device *bootdev, int partition, |
| 23 | const char *filename) |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 24 | { |
Faiz Abbas | 39388ae | 2020-08-03 11:35:05 +0530 | [diff] [blame] | 25 | int err = 0; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 26 | struct blk_desc *stor_dev; |
Faiz Abbas | 39388ae | 2020-08-03 11:35:05 +0530 | [diff] [blame] | 27 | static bool usb_init_pending = true; |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 28 | |
Faiz Abbas | 39388ae | 2020-08-03 11:35:05 +0530 | [diff] [blame] | 29 | if (usb_init_pending) { |
| 30 | usb_stop(); |
| 31 | err = usb_init(); |
| 32 | usb_init_pending = false; |
| 33 | } |
| 34 | |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 35 | if (err) { |
| 36 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 37 | printf("%s: usb init failed: err - %d\n", __func__, err); |
| 38 | #endif |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 39 | return err; |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 40 | } |
| 41 | |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 42 | /* try to recognize storage devices immediately */ |
| 43 | usb_stor_curr_dev = usb_stor_scan(1); |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 44 | stor_dev = blk_get_devnum_by_uclass_id(UCLASS_USB, usb_stor_curr_dev); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 45 | if (!stor_dev) |
| 46 | return -ENODEV; |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 47 | |
| 48 | debug("boot mode - FAT\n"); |
| 49 | |
Tom Rini | 7115007 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 50 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 51 | if (spl_start_uboot() || |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 52 | spl_load_image_fat_os(spl_image, bootdev, stor_dev, partition)) |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 53 | #endif |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 54 | { |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 55 | err = spl_load_image_fat(spl_image, bootdev, stor_dev, partition, filename); |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 56 | } |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 57 | |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 58 | if (err) { |
| 59 | puts("Error loading from USB device\n"); |
| 60 | return err; |
| 61 | } |
| 62 | |
| 63 | return 0; |
Dan Murphy | 8cffe5b | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 64 | } |
Faiz Abbas | c3ab97c | 2020-08-03 11:35:04 +0530 | [diff] [blame] | 65 | |
| 66 | static int spl_usb_load_image(struct spl_image_info *spl_image, |
| 67 | struct spl_boot_device *bootdev) |
| 68 | { |
| 69 | return spl_usb_load(spl_image, bootdev, |
| 70 | CONFIG_SYS_USB_FAT_BOOT_PARTITION, |
| 71 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 72 | } |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 73 | SPL_LOAD_IMAGE_METHOD("USB", 0, BOOT_DEVICE_USB, spl_usb_load_image); |