fsl-dt-fixup.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_errata.h>
  15. #include <fsl_usb.h>
  16. #include <fdt_support.h>
  17. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  18. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  19. #endif
  20. /* USB Controllers */
  21. #define FSL_USB2_MPH "fsl-usb2-mph"
  22. #define FSL_USB2_DR "fsl-usb2-dr"
  23. #define CHIPIDEA_USB2 "chipidea,usb2"
  24. #define SNPS_DWC3 "snps,dwc3"
  25. static const char * const compat_usb_fsl[] = {
  26. FSL_USB2_MPH,
  27. FSL_USB2_DR,
  28. SNPS_DWC3,
  29. NULL
  30. };
  31. static int fdt_usb_get_node_type(void *blob, int start_offset,
  32. int *node_offset, const char **node_type)
  33. {
  34. int i;
  35. int ret = -ENOENT;
  36. for (i = 0; compat_usb_fsl[i]; i++) {
  37. *node_offset = fdt_node_offset_by_compatible
  38. (blob, start_offset,
  39. compat_usb_fsl[i]);
  40. if (*node_offset >= 0) {
  41. *node_type = compat_usb_fsl[i];
  42. ret = 0;
  43. break;
  44. }
  45. }
  46. return ret;
  47. }
  48. static int fdt_fixup_usb_mode_phy_type(void *blob, const char *mode,
  49. const char *phy_type, int start_offset)
  50. {
  51. const char *prop_mode = "dr_mode";
  52. const char *prop_type = "phy_type";
  53. const char *node_type = NULL;
  54. int node_offset;
  55. int err;
  56. err = fdt_usb_get_node_type(blob, start_offset,
  57. &node_offset, &node_type);
  58. if (err < 0)
  59. return err;
  60. if (mode) {
  61. err = fdt_setprop(blob, node_offset, prop_mode, mode,
  62. strlen(mode) + 1);
  63. if (err < 0)
  64. printf("WARNING: could not set %s for %s: %s.\n",
  65. prop_mode, node_type, fdt_strerror(err));
  66. }
  67. if (phy_type) {
  68. err = fdt_setprop(blob, node_offset, prop_type, phy_type,
  69. strlen(phy_type) + 1);
  70. if (err < 0)
  71. printf("WARNING: could not set %s for %s: %s.\n",
  72. prop_type, node_type, fdt_strerror(err));
  73. }
  74. return node_offset;
  75. }
  76. static int fdt_fixup_usb_erratum(void *blob, const char *prop_erratum,
  77. const char *controller_type, int start_offset)
  78. {
  79. int node_offset, err;
  80. const char *node_type = NULL;
  81. const char *node_name = NULL;
  82. err = fdt_usb_get_node_type(blob, start_offset,
  83. &node_offset, &node_type);
  84. if (err < 0)
  85. return err;
  86. if (!strcmp(node_type, FSL_USB2_MPH) || !strcmp(node_type, FSL_USB2_DR))
  87. node_name = CHIPIDEA_USB2;
  88. else
  89. node_name = node_type;
  90. if (strcmp(node_name, controller_type))
  91. return err;
  92. err = fdt_setprop(blob, node_offset, prop_erratum, NULL, 0);
  93. if (err < 0) {
  94. printf("ERROR: could not set %s for %s: %s.\n",
  95. prop_erratum, node_type, fdt_strerror(err));
  96. }
  97. return node_offset;
  98. }
  99. static int fdt_fixup_erratum(int *usb_erratum_off, void *blob,
  100. const char *controller_type, char *str,
  101. bool (*has_erratum)(void))
  102. {
  103. char buf[32] = {0};
  104. snprintf(buf, sizeof(buf), "fsl,usb-erratum-%s", str);
  105. if (!has_erratum())
  106. return -EINVAL;
  107. *usb_erratum_off = fdt_fixup_usb_erratum(blob, buf, controller_type,
  108. *usb_erratum_off);
  109. if (*usb_erratum_off < 0)
  110. return -ENOSPC;
  111. debug("Adding USB erratum %s\n", str);
  112. return 0;
  113. }
  114. void fdt_fixup_dr_usb(void *blob, bd_t *bd)
  115. {
  116. static const char * const modes[] = { "host", "peripheral", "otg" };
  117. static const char * const phys[] = { "ulpi", "utmi", "utmi_dual" };
  118. int usb_erratum_a006261_off = -1;
  119. int usb_erratum_a007075_off = -1;
  120. int usb_erratum_a007792_off = -1;
  121. int usb_erratum_a005697_off = -1;
  122. int usb_mode_off = -1;
  123. int usb_phy_off = -1;
  124. char str[5];
  125. int i, j;
  126. int ret;
  127. for (i = 1; i <= CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  128. const char *dr_mode_type = NULL;
  129. const char *dr_phy_type = NULL;
  130. int mode_idx = -1, phy_idx = -1;
  131. snprintf(str, 5, "%s%d", "usb", i);
  132. if (hwconfig(str)) {
  133. for (j = 0; j < ARRAY_SIZE(modes); j++) {
  134. if (hwconfig_subarg_cmp(str, "dr_mode",
  135. modes[j])) {
  136. mode_idx = j;
  137. break;
  138. }
  139. }
  140. for (j = 0; j < ARRAY_SIZE(phys); j++) {
  141. if (hwconfig_subarg_cmp(str, "phy_type",
  142. phys[j])) {
  143. phy_idx = j;
  144. break;
  145. }
  146. }
  147. if (mode_idx < 0 && phy_idx < 0) {
  148. printf("WARNING: invalid phy or mode\n");
  149. return;
  150. }
  151. if (mode_idx > -1)
  152. dr_mode_type = modes[mode_idx];
  153. if (phy_idx > -1)
  154. dr_phy_type = phys[phy_idx];
  155. }
  156. if (has_dual_phy())
  157. dr_phy_type = phys[2];
  158. usb_mode_off = fdt_fixup_usb_mode_phy_type(blob,
  159. dr_mode_type, NULL,
  160. usb_mode_off);
  161. if (usb_mode_off < 0)
  162. return;
  163. usb_phy_off = fdt_fixup_usb_mode_phy_type(blob,
  164. NULL, dr_phy_type,
  165. usb_phy_off);
  166. if (usb_phy_off < 0)
  167. return;
  168. ret = fdt_fixup_erratum(&usb_erratum_a006261_off, blob,
  169. CHIPIDEA_USB2, "a006261",
  170. has_erratum_a006261);
  171. if (ret == -ENOSPC)
  172. return;
  173. ret = fdt_fixup_erratum(&usb_erratum_a007075_off, blob,
  174. CHIPIDEA_USB2, "a007075",
  175. has_erratum_a007075);
  176. if (ret == -ENOSPC)
  177. return;
  178. ret = fdt_fixup_erratum(&usb_erratum_a007792_off, blob,
  179. CHIPIDEA_USB2, "a007792",
  180. has_erratum_a007792);
  181. if (ret == -ENOSPC)
  182. return;
  183. ret = fdt_fixup_erratum(&usb_erratum_a005697_off, blob,
  184. CHIPIDEA_USB2, "a005697",
  185. has_erratum_a005697);
  186. if (ret == -ENOSPC)
  187. return;
  188. }
  189. }