common.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <asm/global_data.h>
  11. #include <linux/usb/otg.h>
  12. #include <linux/usb/ch9.h>
  13. #include <linux/usb/phy.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. static const char *const usb_dr_modes[] = {
  16. [USB_DR_MODE_UNKNOWN] = "",
  17. [USB_DR_MODE_HOST] = "host",
  18. [USB_DR_MODE_PERIPHERAL] = "peripheral",
  19. [USB_DR_MODE_OTG] = "otg",
  20. };
  21. enum usb_dr_mode usb_get_dr_mode(ofnode node)
  22. {
  23. const char *dr_mode;
  24. int i;
  25. dr_mode = ofnode_read_string(node, "dr_mode");
  26. if (!dr_mode) {
  27. pr_err("usb dr_mode not found\n");
  28. return USB_DR_MODE_UNKNOWN;
  29. }
  30. for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
  31. if (!strcmp(dr_mode, usb_dr_modes[i]))
  32. return i;
  33. return USB_DR_MODE_UNKNOWN;
  34. }
  35. static const char *const speed_names[] = {
  36. [USB_SPEED_UNKNOWN] = "UNKNOWN",
  37. [USB_SPEED_LOW] = "low-speed",
  38. [USB_SPEED_FULL] = "full-speed",
  39. [USB_SPEED_HIGH] = "high-speed",
  40. [USB_SPEED_WIRELESS] = "wireless",
  41. [USB_SPEED_SUPER] = "super-speed",
  42. [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
  43. };
  44. const char *usb_speed_string(enum usb_device_speed speed)
  45. {
  46. if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
  47. speed = USB_SPEED_UNKNOWN;
  48. return speed_names[speed];
  49. }
  50. enum usb_device_speed usb_get_maximum_speed(ofnode node)
  51. {
  52. const char *max_speed;
  53. int i;
  54. max_speed = ofnode_read_string(node, "maximum-speed");
  55. if (!max_speed) {
  56. pr_err("usb maximum-speed not found\n");
  57. return USB_SPEED_UNKNOWN;
  58. }
  59. for (i = 0; i < ARRAY_SIZE(speed_names); i++)
  60. if (!strcmp(max_speed, speed_names[i]))
  61. return i;
  62. return USB_SPEED_UNKNOWN;
  63. }
  64. #if CONFIG_IS_ENABLED(DM_USB)
  65. static const char *const usbphy_modes[] = {
  66. [USBPHY_INTERFACE_MODE_UNKNOWN] = "",
  67. [USBPHY_INTERFACE_MODE_UTMI] = "utmi",
  68. [USBPHY_INTERFACE_MODE_UTMIW] = "utmi_wide",
  69. [USBPHY_INTERFACE_MODE_ULPI] = "ulpi",
  70. [USBPHY_INTERFACE_MODE_SERIAL] = "serial",
  71. [USBPHY_INTERFACE_MODE_HSIC] = "hsic",
  72. };
  73. enum usb_phy_interface usb_get_phy_mode(ofnode node)
  74. {
  75. const char *phy_type;
  76. int i;
  77. phy_type = ofnode_get_property(node, "phy_type", NULL);
  78. if (!phy_type)
  79. return USBPHY_INTERFACE_MODE_UNKNOWN;
  80. for (i = 0; i < ARRAY_SIZE(usbphy_modes); i++)
  81. if (!strcmp(phy_type, usbphy_modes[i]))
  82. return i;
  83. return USBPHY_INTERFACE_MODE_UNKNOWN;
  84. }
  85. #endif