xusb-padctl-common.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. */
  6. #define pr_fmt(fmt) "tegra-xusb-padctl: " fmt
  7. #include <common.h>
  8. #include <errno.h>
  9. #include "xusb-padctl-common.h"
  10. #include <asm/arch/clock.h>
  11. int tegra_xusb_phy_prepare(struct tegra_xusb_phy *phy)
  12. {
  13. if (phy && phy->ops && phy->ops->prepare)
  14. return phy->ops->prepare(phy);
  15. return phy ? -ENOSYS : -EINVAL;
  16. }
  17. int tegra_xusb_phy_enable(struct tegra_xusb_phy *phy)
  18. {
  19. if (phy && phy->ops && phy->ops->enable)
  20. return phy->ops->enable(phy);
  21. return phy ? -ENOSYS : -EINVAL;
  22. }
  23. int tegra_xusb_phy_disable(struct tegra_xusb_phy *phy)
  24. {
  25. if (phy && phy->ops && phy->ops->disable)
  26. return phy->ops->disable(phy);
  27. return phy ? -ENOSYS : -EINVAL;
  28. }
  29. int tegra_xusb_phy_unprepare(struct tegra_xusb_phy *phy)
  30. {
  31. if (phy && phy->ops && phy->ops->unprepare)
  32. return phy->ops->unprepare(phy);
  33. return phy ? -ENOSYS : -EINVAL;
  34. }
  35. struct tegra_xusb_phy *tegra_xusb_phy_get(unsigned int type)
  36. {
  37. struct tegra_xusb_phy *phy;
  38. int i;
  39. for (i = 0; i < padctl.socdata->num_phys; i++) {
  40. phy = &padctl.socdata->phys[i];
  41. if (phy->type != type)
  42. continue;
  43. return phy;
  44. }
  45. return NULL;
  46. }
  47. static const struct tegra_xusb_padctl_lane *
  48. tegra_xusb_padctl_find_lane(struct tegra_xusb_padctl *padctl, const char *name)
  49. {
  50. unsigned int i;
  51. for (i = 0; i < padctl->socdata->num_lanes; i++)
  52. if (strcmp(name, padctl->socdata->lanes[i].name) == 0)
  53. return &padctl->socdata->lanes[i];
  54. return NULL;
  55. }
  56. static int
  57. tegra_xusb_padctl_group_parse_dt(struct tegra_xusb_padctl *padctl,
  58. struct tegra_xusb_padctl_group *group,
  59. const void *fdt, int node)
  60. {
  61. unsigned int i;
  62. int len;
  63. group->name = fdt_get_name(fdt, node, &len);
  64. len = fdt_stringlist_count(fdt, node, "nvidia,lanes");
  65. if (len < 0) {
  66. error("failed to parse \"nvidia,lanes\" property");
  67. return -EINVAL;
  68. }
  69. group->num_pins = len;
  70. for (i = 0; i < group->num_pins; i++) {
  71. group->pins[i] = fdt_stringlist_get(fdt, node, "nvidia,lanes",
  72. i, NULL);
  73. if (!group->pins[i]) {
  74. error("failed to read string from \"nvidia,lanes\" property");
  75. return -EINVAL;
  76. }
  77. }
  78. group->num_pins = len;
  79. group->func = fdt_stringlist_get(fdt, node, "nvidia,function", 0, NULL);
  80. if (!group->func) {
  81. error("failed to parse \"nvidia,func\" property");
  82. return -EINVAL;
  83. }
  84. group->iddq = fdtdec_get_int(fdt, node, "nvidia,iddq", -1);
  85. return 0;
  86. }
  87. static int tegra_xusb_padctl_find_function(struct tegra_xusb_padctl *padctl,
  88. const char *name)
  89. {
  90. unsigned int i;
  91. for (i = 0; i < padctl->socdata->num_functions; i++)
  92. if (strcmp(name, padctl->socdata->functions[i]) == 0)
  93. return i;
  94. return -ENOENT;
  95. }
  96. static int
  97. tegra_xusb_padctl_lane_find_function(struct tegra_xusb_padctl *padctl,
  98. const struct tegra_xusb_padctl_lane *lane,
  99. const char *name)
  100. {
  101. unsigned int i;
  102. int func;
  103. func = tegra_xusb_padctl_find_function(padctl, name);
  104. if (func < 0)
  105. return func;
  106. for (i = 0; i < lane->num_funcs; i++)
  107. if (lane->funcs[i] == func)
  108. return i;
  109. return -ENOENT;
  110. }
  111. static int
  112. tegra_xusb_padctl_group_apply(struct tegra_xusb_padctl *padctl,
  113. const struct tegra_xusb_padctl_group *group)
  114. {
  115. unsigned int i;
  116. for (i = 0; i < group->num_pins; i++) {
  117. const struct tegra_xusb_padctl_lane *lane;
  118. unsigned int func;
  119. u32 value;
  120. lane = tegra_xusb_padctl_find_lane(padctl, group->pins[i]);
  121. if (!lane) {
  122. error("no lane for pin %s", group->pins[i]);
  123. continue;
  124. }
  125. func = tegra_xusb_padctl_lane_find_function(padctl, lane,
  126. group->func);
  127. if (func < 0) {
  128. error("function %s invalid for lane %s: %d",
  129. group->func, lane->name, func);
  130. continue;
  131. }
  132. value = padctl_readl(padctl, lane->offset);
  133. /* set pin function */
  134. value &= ~(lane->mask << lane->shift);
  135. value |= func << lane->shift;
  136. /*
  137. * Set IDDQ if supported on the lane and specified in the
  138. * configuration.
  139. */
  140. if (lane->iddq > 0 && group->iddq >= 0) {
  141. if (group->iddq != 0)
  142. value &= ~(1 << lane->iddq);
  143. else
  144. value |= 1 << lane->iddq;
  145. }
  146. padctl_writel(padctl, value, lane->offset);
  147. }
  148. return 0;
  149. }
  150. static int
  151. tegra_xusb_padctl_config_apply(struct tegra_xusb_padctl *padctl,
  152. struct tegra_xusb_padctl_config *config)
  153. {
  154. unsigned int i;
  155. for (i = 0; i < config->num_groups; i++) {
  156. const struct tegra_xusb_padctl_group *group;
  157. int err;
  158. group = &config->groups[i];
  159. err = tegra_xusb_padctl_group_apply(padctl, group);
  160. if (err < 0) {
  161. error("failed to apply group %s: %d",
  162. group->name, err);
  163. continue;
  164. }
  165. }
  166. return 0;
  167. }
  168. static int
  169. tegra_xusb_padctl_config_parse_dt(struct tegra_xusb_padctl *padctl,
  170. struct tegra_xusb_padctl_config *config,
  171. const void *fdt, int node)
  172. {
  173. int subnode;
  174. config->name = fdt_get_name(fdt, node, NULL);
  175. fdt_for_each_subnode(subnode, fdt, node) {
  176. struct tegra_xusb_padctl_group *group;
  177. int err;
  178. group = &config->groups[config->num_groups];
  179. err = tegra_xusb_padctl_group_parse_dt(padctl, group, fdt,
  180. subnode);
  181. if (err < 0) {
  182. error("failed to parse group %s", group->name);
  183. return err;
  184. }
  185. config->num_groups++;
  186. }
  187. return 0;
  188. }
  189. static int tegra_xusb_padctl_parse_dt(struct tegra_xusb_padctl *padctl,
  190. const void *fdt, int node)
  191. {
  192. int subnode, err;
  193. err = fdt_get_resource(fdt, node, "reg", 0, &padctl->regs);
  194. if (err < 0) {
  195. error("registers not found");
  196. return err;
  197. }
  198. fdt_for_each_subnode(subnode, fdt, node) {
  199. struct tegra_xusb_padctl_config *config = &padctl->config;
  200. err = tegra_xusb_padctl_config_parse_dt(padctl, config, fdt,
  201. subnode);
  202. if (err < 0) {
  203. error("failed to parse entry %s: %d",
  204. config->name, err);
  205. continue;
  206. }
  207. }
  208. return 0;
  209. }
  210. struct tegra_xusb_padctl padctl;
  211. int tegra_xusb_process_nodes(const void *fdt, int nodes[], unsigned int count,
  212. const struct tegra_xusb_padctl_soc *socdata)
  213. {
  214. unsigned int i;
  215. int err;
  216. for (i = 0; i < count; i++) {
  217. if (!fdtdec_get_is_enabled(fdt, nodes[i]))
  218. continue;
  219. padctl.socdata = socdata;
  220. err = tegra_xusb_padctl_parse_dt(&padctl, fdt, nodes[i]);
  221. if (err < 0) {
  222. error("failed to parse DT: %d", err);
  223. continue;
  224. }
  225. /* deassert XUSB padctl reset */
  226. reset_set_enable(PERIPH_ID_XUSB_PADCTL, 0);
  227. err = tegra_xusb_padctl_config_apply(&padctl, &padctl.config);
  228. if (err < 0) {
  229. error("failed to apply pinmux: %d", err);
  230. continue;
  231. }
  232. /* only a single instance is supported */
  233. break;
  234. }
  235. return 0;
  236. }