spl_usb.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2014
  4. * Texas Instruments, <www.ti.com>
  5. *
  6. * Dan Murphy <dmurphy@ti.com>
  7. *
  8. * Derived work from spl_mmc.c
  9. */
  10. #include <common.h>
  11. #include <log.h>
  12. #include <spl.h>
  13. #include <asm/u-boot.h>
  14. #include <errno.h>
  15. #include <usb.h>
  16. #include <fat.h>
  17. static int usb_stor_curr_dev = -1; /* current device */
  18. int spl_usb_load(struct spl_image_info *spl_image,
  19. struct spl_boot_device *bootdev, int partition,
  20. const char *filename)
  21. {
  22. int err = 0;
  23. struct blk_desc *stor_dev;
  24. static bool usb_init_pending = true;
  25. if (usb_init_pending) {
  26. usb_stop();
  27. err = usb_init();
  28. usb_init_pending = false;
  29. }
  30. if (err) {
  31. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  32. printf("%s: usb init failed: err - %d\n", __func__, err);
  33. #endif
  34. return err;
  35. }
  36. /* try to recognize storage devices immediately */
  37. usb_stor_curr_dev = usb_stor_scan(1);
  38. stor_dev = blk_get_devnum_by_type(IF_TYPE_USB, usb_stor_curr_dev);
  39. if (!stor_dev)
  40. return -ENODEV;
  41. debug("boot mode - FAT\n");
  42. #ifdef CONFIG_SPL_OS_BOOT
  43. if (spl_start_uboot() ||
  44. spl_load_image_fat_os(spl_image, stor_dev, partition))
  45. #endif
  46. {
  47. err = spl_load_image_fat(spl_image, stor_dev, partition, filename);
  48. }
  49. if (err) {
  50. puts("Error loading from USB device\n");
  51. return err;
  52. }
  53. return 0;
  54. }
  55. static int spl_usb_load_image(struct spl_image_info *spl_image,
  56. struct spl_boot_device *bootdev)
  57. {
  58. return spl_usb_load(spl_image, bootdev,
  59. CONFIG_SYS_USB_FAT_BOOT_PARTITION,
  60. CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
  61. }
  62. SPL_LOAD_IMAGE_METHOD("USB", 0, BOOT_DEVICE_USB, spl_usb_load_image);