Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dan Murphy | cb367e1 | 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 | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 8 | * Derived work from spl_mmc.c |
| 9 | */ |
| 10 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 12 | #include <spl.h> |
| 13 | #include <asm/u-boot.h> |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 15 | #include <usb.h> |
| 16 | #include <fat.h> |
| 17 | |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 18 | static int usb_stor_curr_dev = -1; /* current device */ |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 19 | |
Faiz Abbas | 05e3a15 | 2020-08-03 11:35:04 +0530 | [diff] [blame] | 20 | int spl_usb_load(struct spl_image_info *spl_image, |
| 21 | struct spl_boot_device *bootdev, int partition, |
| 22 | const char *filename) |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 23 | { |
Faiz Abbas | 5dd935a | 2020-08-03 11:35:05 +0530 | [diff] [blame] | 24 | int err = 0; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 25 | struct blk_desc *stor_dev; |
Faiz Abbas | 5dd935a | 2020-08-03 11:35:05 +0530 | [diff] [blame] | 26 | 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 Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 33 | |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 34 | if (err) { |
| 35 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 36 | printf("%s: usb init failed: err - %d\n", __func__, err); |
| 37 | #endif |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 38 | return err; |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 39 | } |
| 40 | |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 41 | /* try to recognize storage devices immediately */ |
| 42 | usb_stor_curr_dev = usb_stor_scan(1); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 43 | stor_dev = blk_get_devnum_by_uclass_id(UCLASS_USB, usb_stor_curr_dev); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 44 | if (!stor_dev) |
| 45 | return -ENODEV; |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 46 | |
| 47 | debug("boot mode - FAT\n"); |
| 48 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 49 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 50 | if (spl_start_uboot() || |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 51 | spl_load_image_fat_os(spl_image, bootdev, stor_dev, partition)) |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 52 | #endif |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 53 | { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 54 | err = spl_load_image_fat(spl_image, bootdev, stor_dev, partition, filename); |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 55 | } |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 56 | |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 57 | if (err) { |
| 58 | puts("Error loading from USB device\n"); |
| 59 | return err; |
| 60 | } |
| 61 | |
| 62 | return 0; |
Dan Murphy | cb367e1 | 2014-01-16 11:23:30 -0600 | [diff] [blame] | 63 | } |
Faiz Abbas | 05e3a15 | 2020-08-03 11:35:04 +0530 | [diff] [blame] | 64 | |
| 65 | static 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 Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 72 | SPL_LOAD_IMAGE_METHOD("USB", 0, BOOT_DEVICE_USB, spl_usb_load_image); |