common.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Provides code common for host and device side USB.
  4. *
  5. * (C) Copyright 2016
  6. * Texas Instruments Incorporated, <www.ti.com>
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <linux/usb/otg.h>
  11. #include <linux/usb/ch9.h>
  12. #include <linux/usb/phy.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static const char *const usb_dr_modes[] = {
  15. [USB_DR_MODE_UNKNOWN] = "",
  16. [USB_DR_MODE_HOST] = "host",
  17. [USB_DR_MODE_PERIPHERAL] = "peripheral",
  18. [USB_DR_MODE_OTG] = "otg",
  19. };
  20. enum usb_dr_mode usb_get_dr_mode(ofnode node)
  21. {
  22. const char *dr_mode;
  23. int i;
  24. dr_mode = ofnode_read_string(node, "dr_mode");
  25. if (!dr_mode) {
  26. pr_err("usb dr_mode not found\n");
  27. return USB_DR_MODE_UNKNOWN;
  28. }
  29. for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
  30. if (!strcmp(dr_mode, usb_dr_modes[i]))
  31. return i;
  32. return USB_DR_MODE_UNKNOWN;
  33. }
  34. static const char *const speed_names[] = {
  35. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  36. [USB_SPEED_LOW] = "low-speed",
  37. [USB_SPEED_FULL] = "full-speed",
  38. [USB_SPEED_HIGH] = "high-speed",
  39. [USB_SPEED_WIRELESS] = "wireless",
  40. [USB_SPEED_SUPER] = "super-speed",
  41. [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
  42. };
  43. const char *usb_speed_string(enum usb_device_speed speed)
  44. {
  45. if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
  46. speed = USB_SPEED_UNKNOWN;
  47. return speed_names[speed];
  48. }
  49. enum usb_device_speed usb_get_maximum_speed(ofnode node)
  50. {
  51. const char *max_speed;
  52. int i;
  53. max_speed = ofnode_read_string(node, "maximum-speed");
  54. if (!max_speed) {
  55. pr_err("usb maximum-speed not found\n");
  56. return USB_SPEED_UNKNOWN;
  57. }
  58. for (i = 0; i < ARRAY_SIZE(speed_names); i++)
  59. if (!strcmp(max_speed, speed_names[i]))
  60. return i;
  61. return USB_SPEED_UNKNOWN;
  62. }
  63. #if CONFIG_IS_ENABLED(DM_USB)
  64. static const char *const usbphy_modes[] = {
  65. [USBPHY_INTERFACE_MODE_UNKNOWN] = "",
  66. [USBPHY_INTERFACE_MODE_UTMI] = "utmi",
  67. [USBPHY_INTERFACE_MODE_UTMIW] = "utmi_wide",
  68. };
  69. enum usb_phy_interface usb_get_phy_mode(ofnode node)
  70. {
  71. const char *phy_type;
  72. int i;
  73. phy_type = ofnode_get_property(node, "phy_type", NULL);
  74. if (!phy_type)
  75. return USBPHY_INTERFACE_MODE_UNKNOWN;
  76. for (i = 0; i < ARRAY_SIZE(usbphy_modes); i++)
  77. if (!strcmp(phy_type, usbphy_modes[i]))
  78. return i;
  79. return USBPHY_INTERFACE_MODE_UNKNOWN;
  80. }
  81. #endif