fsl-dt-fixup.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 fsl_fdt_fixup_usb_erratum(void *blob, const char *prop_erratum,
  77. const char *controller_type,
  78. int start_offset)
  79. {
  80. int node_offset, err;
  81. const char *node_type = NULL;
  82. const char *node_name = NULL;
  83. err = fdt_usb_get_node_type(blob, start_offset,
  84. &node_offset, &node_type);
  85. if (err < 0)
  86. return err;
  87. if (!strcmp(node_type, FSL_USB2_MPH) || !strcmp(node_type, FSL_USB2_DR))
  88. node_name = CHIPIDEA_USB2;
  89. else
  90. node_name = node_type;
  91. if (strcmp(node_name, controller_type))
  92. return err;
  93. err = fdt_setprop(blob, node_offset, prop_erratum, NULL, 0);
  94. if (err < 0) {
  95. printf("ERROR: could not set %s for %s: %s.\n",
  96. prop_erratum, node_type, fdt_strerror(err));
  97. }
  98. return node_offset;
  99. }
  100. static int fsl_fdt_fixup_erratum(int *usb_erratum_off, void *blob,
  101. const char *controller_type, char *str,
  102. bool (*has_erratum)(void))
  103. {
  104. char buf[32] = {0};
  105. snprintf(buf, sizeof(buf), "fsl,usb-erratum-%s", str);
  106. if (!has_erratum())
  107. return -EINVAL;
  108. *usb_erratum_off = fsl_fdt_fixup_usb_erratum(blob, buf, controller_type,
  109. *usb_erratum_off);
  110. if (*usb_erratum_off < 0)
  111. return -ENOSPC;
  112. debug("Adding USB erratum %s\n", str);
  113. return 0;
  114. }
  115. void fsl_fdt_fixup_dr_usb(void *blob, bd_t *bd)
  116. {
  117. static const char * const modes[] = { "host", "peripheral", "otg" };
  118. static const char * const phys[] = { "ulpi", "utmi", "utmi_dual" };
  119. int usb_erratum_a006261_off = -1;
  120. int usb_erratum_a007075_off = -1;
  121. int usb_erratum_a007792_off = -1;
  122. int usb_erratum_a005697_off = -1;
  123. int usb_erratum_a008751_off = -1;
  124. int usb_mode_off = -1;
  125. int usb_phy_off = -1;
  126. char str[5];
  127. int i, j;
  128. int ret;
  129. for (i = 1; i <= CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
  130. const char *dr_mode_type = NULL;
  131. const char *dr_phy_type = NULL;
  132. int mode_idx = -1, phy_idx = -1;
  133. snprintf(str, 5, "%s%d", "usb", i);
  134. if (hwconfig(str)) {
  135. for (j = 0; j < ARRAY_SIZE(modes); j++) {
  136. if (hwconfig_subarg_cmp(str, "dr_mode",
  137. modes[j])) {
  138. mode_idx = j;
  139. break;
  140. }
  141. }
  142. for (j = 0; j < ARRAY_SIZE(phys); j++) {
  143. if (hwconfig_subarg_cmp(str, "phy_type",
  144. phys[j])) {
  145. phy_idx = j;
  146. break;
  147. }
  148. }
  149. if (mode_idx < 0 && phy_idx < 0) {
  150. printf("WARNING: invalid phy or mode\n");
  151. return;
  152. }
  153. if (mode_idx > -1)
  154. dr_mode_type = modes[mode_idx];
  155. if (phy_idx > -1)
  156. dr_phy_type = phys[phy_idx];
  157. }
  158. if (has_dual_phy())
  159. dr_phy_type = phys[2];
  160. usb_mode_off = fdt_fixup_usb_mode_phy_type(blob,
  161. dr_mode_type, NULL,
  162. usb_mode_off);
  163. if (usb_mode_off < 0)
  164. return;
  165. usb_phy_off = fdt_fixup_usb_mode_phy_type(blob,
  166. NULL, dr_phy_type,
  167. usb_phy_off);
  168. if (usb_phy_off < 0)
  169. return;
  170. ret = fsl_fdt_fixup_erratum(&usb_erratum_a006261_off, blob,
  171. CHIPIDEA_USB2, "a006261",
  172. has_erratum_a006261);
  173. if (ret == -ENOSPC)
  174. return;
  175. ret = fsl_fdt_fixup_erratum(&usb_erratum_a007075_off, blob,
  176. CHIPIDEA_USB2, "a007075",
  177. has_erratum_a007075);
  178. if (ret == -ENOSPC)
  179. return;
  180. ret = fsl_fdt_fixup_erratum(&usb_erratum_a007792_off, blob,
  181. CHIPIDEA_USB2, "a007792",
  182. has_erratum_a007792);
  183. if (ret == -ENOSPC)
  184. return;
  185. ret = fsl_fdt_fixup_erratum(&usb_erratum_a005697_off, blob,
  186. CHIPIDEA_USB2, "a005697",
  187. has_erratum_a005697);
  188. if (ret == -ENOSPC)
  189. return;
  190. ret = fsl_fdt_fixup_erratum(&usb_erratum_a008751_off, blob,
  191. SNPS_DWC3, "a008751",
  192. has_erratum_a008751);
  193. if (ret == -ENOSPC)
  194. return;
  195. }
  196. }