fsl-dt-fixup.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * (C) Copyright 2009, 2011 Freescale Semiconductor, Inc.
  3. *
  4. * (C) Copyright 2008, Excito Elektronik i Sk=E5ne AB
  5. *
  6. * Author: Tor Krill tor@excito.com
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <usb.h>
  12. #include <asm/io.h>
  13. #include <hwconfig.h>
  14. #include <fsl_usb.h>
  15. #include <fdt_support.h>
  16. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  17. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  18. #endif
  19. static const char * const compat_usb_fsl[] = {
  20. "fsl-usb2-mph",
  21. "fsl-usb2-dr",
  22. "snps,dwc3",
  23. NULL
  24. };
  25. static const char *fdt_usb_get_node_type(void *blob, int start_offset,
  26. int *node_offset)
  27. {
  28. const char *node_type = NULL;
  29. int i;
  30. for (i = 0; compat_usb_fsl[i]; i++) {
  31. *node_offset = fdt_node_offset_by_compatible
  32. (blob, start_offset,
  33. compat_usb_fsl[i]);
  34. if (*node_offset >= 0) {
  35. node_type = compat_usb_fsl[i];
  36. break;
  37. }
  38. }
  39. return node_type;
  40. }
  41. static int fdt_fixup_usb_mode_phy_type(void *blob, const char *mode,
  42. const char *phy_type, int start_offset)
  43. {
  44. const char *prop_mode = "dr_mode";
  45. const char *prop_type = "phy_type";
  46. const char *node_type = NULL;
  47. int node_offset;
  48. int err;
  49. node_type = fdt_usb_get_node_type(blob, start_offset, &node_offset);
  50. if (!node_type)
  51. return -1;
  52. if (mode) {
  53. err = fdt_setprop(blob, node_offset, prop_mode, mode,
  54. strlen(mode) + 1);
  55. if (err < 0)
  56. printf("WARNING: could not set %s for %s: %s.\n",
  57. prop_mode, node_type, fdt_strerror(err));
  58. }
  59. if (phy_type) {
  60. err = fdt_setprop(blob, node_offset, prop_type, phy_type,
  61. strlen(phy_type) + 1);
  62. if (err < 0)
  63. printf("WARNING: could not set %s for %s: %s.\n",
  64. prop_type, node_type, fdt_strerror(err));
  65. }
  66. return node_offset;
  67. }
  68. static int fdt_fixup_usb_erratum(void *blob, const char *prop_erratum,
  69. int start_offset)
  70. {
  71. int node_offset, err;
  72. const char *node_type = NULL;
  73. node_type = fdt_usb_get_node_type(blob, start_offset, &node_offset);
  74. if (!node_type)
  75. return -1;
  76. err = fdt_setprop(blob, node_offset, prop_erratum, NULL, 0);
  77. if (err < 0) {
  78. printf("ERROR: could not set %s for %s: %s.\n",
  79. prop_erratum, node_type, fdt_strerror(err));
  80. }
  81. return node_offset;
  82. }
  83. void fdt_fixup_dr_usb(void *blob, bd_t *bd)
  84. {
  85. static const char * const modes[] = { "host", "peripheral", "otg" };
  86. static const char * const phys[] = { "ulpi", "utmi", "utmi_dual" };
  87. int usb_erratum_a006261_off = -1;
  88. int usb_erratum_a007075_off = -1;
  89. int usb_erratum_a007792_off = -1;
  90. int usb_erratum_a005697_off = -1;
  91. int usb_mode_off = -1;
  92. int usb_phy_off = -1;
  93. char str[5];
  94. int i, j;
  95. for (i = 1; i <= CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  96. const char *dr_mode_type = NULL;
  97. const char *dr_phy_type = NULL;
  98. int mode_idx = -1, phy_idx = -1;
  99. snprintf(str, 5, "%s%d", "usb", i);
  100. if (hwconfig(str)) {
  101. for (j = 0; j < ARRAY_SIZE(modes); j++) {
  102. if (hwconfig_subarg_cmp(str, "dr_mode",
  103. modes[j])) {
  104. mode_idx = j;
  105. break;
  106. }
  107. }
  108. for (j = 0; j < ARRAY_SIZE(phys); j++) {
  109. if (hwconfig_subarg_cmp(str, "phy_type",
  110. phys[j])) {
  111. phy_idx = j;
  112. break;
  113. }
  114. }
  115. if (mode_idx < 0 && phy_idx < 0) {
  116. printf("WARNING: invalid phy or mode\n");
  117. return;
  118. }
  119. if (mode_idx > -1)
  120. dr_mode_type = modes[mode_idx];
  121. if (phy_idx > -1)
  122. dr_phy_type = phys[phy_idx];
  123. }
  124. if (has_dual_phy())
  125. dr_phy_type = phys[2];
  126. usb_mode_off = fdt_fixup_usb_mode_phy_type(blob,
  127. dr_mode_type, NULL,
  128. usb_mode_off);
  129. if (usb_mode_off < 0)
  130. return;
  131. usb_phy_off = fdt_fixup_usb_mode_phy_type(blob,
  132. NULL, dr_phy_type,
  133. usb_phy_off);
  134. if (usb_phy_off < 0)
  135. return;
  136. if (has_erratum_a006261()) {
  137. usb_erratum_a006261_off = fdt_fixup_usb_erratum
  138. (blob,
  139. "fsl,usb-erratum-a006261",
  140. usb_erratum_a006261_off);
  141. if (usb_erratum_a006261_off < 0)
  142. return;
  143. }
  144. if (has_erratum_a007075()) {
  145. usb_erratum_a007075_off = fdt_fixup_usb_erratum
  146. (blob,
  147. "fsl,usb-erratum-a007075",
  148. usb_erratum_a007075_off);
  149. if (usb_erratum_a007075_off < 0)
  150. return;
  151. }
  152. if (has_erratum_a007792()) {
  153. usb_erratum_a007792_off = fdt_fixup_usb_erratum
  154. (blob,
  155. "fsl,usb-erratum-a007792",
  156. usb_erratum_a007792_off);
  157. if (usb_erratum_a007792_off < 0)
  158. return;
  159. }
  160. if (has_erratum_a005697()) {
  161. usb_erratum_a005697_off = fdt_fixup_usb_erratum
  162. (blob,
  163. "fsl,usb-erratum-a005697",
  164. usb_erratum_a005697_off);
  165. if (usb_erratum_a005697_off < 0)
  166. return;
  167. }
  168. }
  169. }