fsl-dt-fixup.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2009, 2011 Freescale Semiconductor, Inc.
  4. *
  5. * (C) Copyright 2008, Excito Elektronik i Sk=E5ne AB
  6. *
  7. * Author: Tor Krill tor@excito.com
  8. */
  9. #include <common.h>
  10. #include <usb.h>
  11. #include <asm/io.h>
  12. #include <hwconfig.h>
  13. #include <fsl_errata.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. /* USB Controllers */
  20. #define FSL_USB2_MPH "fsl-usb2-mph"
  21. #define FSL_USB2_DR "fsl-usb2-dr"
  22. #define CHIPIDEA_USB2 "chipidea,usb2"
  23. #define SNPS_DWC3 "snps,dwc3"
  24. static const char * const compat_usb_fsl[] = {
  25. FSL_USB2_MPH,
  26. FSL_USB2_DR,
  27. SNPS_DWC3,
  28. NULL
  29. };
  30. static int fdt_usb_get_node_type(void *blob, int start_offset,
  31. int *node_offset, const char **node_type)
  32. {
  33. int i;
  34. int ret = -ENOENT;
  35. for (i = 0; compat_usb_fsl[i]; i++) {
  36. *node_offset = fdt_node_offset_by_compatible
  37. (blob, start_offset,
  38. compat_usb_fsl[i]);
  39. if (*node_offset >= 0) {
  40. *node_type = compat_usb_fsl[i];
  41. ret = 0;
  42. break;
  43. }
  44. }
  45. return ret;
  46. }
  47. static int fdt_fixup_usb_mode_phy_type(void *blob, const char *mode,
  48. const char *phy_type, int start_offset)
  49. {
  50. const char *prop_mode = "dr_mode";
  51. const char *prop_type = "phy_type";
  52. const char *node_type = NULL;
  53. int node_offset;
  54. int err;
  55. err = fdt_usb_get_node_type(blob, start_offset,
  56. &node_offset, &node_type);
  57. if (err < 0)
  58. return err;
  59. if (mode) {
  60. err = fdt_setprop(blob, node_offset, prop_mode, mode,
  61. strlen(mode) + 1);
  62. if (err < 0)
  63. printf("WARNING: could not set %s for %s: %s.\n",
  64. prop_mode, node_type, fdt_strerror(err));
  65. }
  66. if (phy_type) {
  67. err = fdt_setprop(blob, node_offset, prop_type, phy_type,
  68. strlen(phy_type) + 1);
  69. if (err < 0)
  70. printf("WARNING: could not set %s for %s: %s.\n",
  71. prop_type, node_type, fdt_strerror(err));
  72. }
  73. return node_offset;
  74. }
  75. static int fsl_fdt_fixup_usb_erratum(void *blob, const char *prop_erratum,
  76. const char *controller_type,
  77. 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 fsl_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 = fsl_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 fsl_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_erratum_a008751_off = -1;
  123. int usb_mode_off = -1;
  124. int usb_phy_off = -1;
  125. char str[5];
  126. int i, j;
  127. int ret;
  128. for (i = 1; i <= CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  129. const char *dr_mode_type = NULL;
  130. const char *dr_phy_type = NULL;
  131. int mode_idx = -1, phy_idx = -1;
  132. snprintf(str, 5, "%s%d", "usb", i);
  133. if (hwconfig(str)) {
  134. for (j = 0; j < ARRAY_SIZE(modes); j++) {
  135. if (hwconfig_subarg_cmp(str, "dr_mode",
  136. modes[j])) {
  137. mode_idx = j;
  138. break;
  139. }
  140. }
  141. for (j = 0; j < ARRAY_SIZE(phys); j++) {
  142. if (hwconfig_subarg_cmp(str, "phy_type",
  143. phys[j])) {
  144. phy_idx = j;
  145. break;
  146. }
  147. }
  148. if (mode_idx < 0 && phy_idx < 0) {
  149. printf("WARNING: invalid phy or mode\n");
  150. return;
  151. }
  152. if (mode_idx > -1)
  153. dr_mode_type = modes[mode_idx];
  154. if (phy_idx > -1)
  155. dr_phy_type = phys[phy_idx];
  156. }
  157. if (has_dual_phy())
  158. dr_phy_type = phys[2];
  159. usb_mode_off = fdt_fixup_usb_mode_phy_type(blob,
  160. dr_mode_type, NULL,
  161. usb_mode_off);
  162. if (usb_mode_off < 0)
  163. return;
  164. usb_phy_off = fdt_fixup_usb_mode_phy_type(blob,
  165. NULL, dr_phy_type,
  166. usb_phy_off);
  167. if (usb_phy_off < 0)
  168. return;
  169. ret = fsl_fdt_fixup_erratum(&usb_erratum_a006261_off, blob,
  170. CHIPIDEA_USB2, "a006261",
  171. has_erratum_a006261);
  172. if (ret == -ENOSPC)
  173. return;
  174. ret = fsl_fdt_fixup_erratum(&usb_erratum_a007075_off, blob,
  175. CHIPIDEA_USB2, "a007075",
  176. has_erratum_a007075);
  177. if (ret == -ENOSPC)
  178. return;
  179. ret = fsl_fdt_fixup_erratum(&usb_erratum_a007792_off, blob,
  180. CHIPIDEA_USB2, "a007792",
  181. has_erratum_a007792);
  182. if (ret == -ENOSPC)
  183. return;
  184. ret = fsl_fdt_fixup_erratum(&usb_erratum_a005697_off, blob,
  185. CHIPIDEA_USB2, "a005697",
  186. has_erratum_a005697);
  187. if (ret == -ENOSPC)
  188. return;
  189. ret = fsl_fdt_fixup_erratum(&usb_erratum_a008751_off, blob,
  190. SNPS_DWC3, "a008751",
  191. has_erratum_a008751);
  192. if (ret == -ENOSPC)
  193. return;
  194. }
  195. }