xusb-padctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Copyright (c) 2014, 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 <fdtdec.h>
  10. #include <malloc.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch-tegra/xusb-padctl.h>
  14. #include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
  15. #define XUSB_PADCTL_ELPG_PROGRAM 0x01c
  16. #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN (1 << 26)
  17. #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY (1 << 25)
  18. #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN (1 << 24)
  19. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1 0x040
  20. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET (1 << 19)
  21. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK (0xf << 12)
  22. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST (1 << 1)
  23. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2 0x044
  24. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN (1 << 6)
  25. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN (1 << 5)
  26. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL (1 << 4)
  27. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1 0x138
  28. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET (1 << 27)
  29. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE (1 << 24)
  30. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD (1 << 3)
  31. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST (1 << 1)
  32. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ (1 << 0)
  33. #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1 0x148
  34. #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD (1 << 1)
  35. #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ (1 << 0)
  36. enum tegra124_function {
  37. TEGRA124_FUNC_SNPS,
  38. TEGRA124_FUNC_XUSB,
  39. TEGRA124_FUNC_UART,
  40. TEGRA124_FUNC_PCIE,
  41. TEGRA124_FUNC_USB3,
  42. TEGRA124_FUNC_SATA,
  43. TEGRA124_FUNC_RSVD,
  44. };
  45. static const char *const tegra124_functions[] = {
  46. "snps",
  47. "xusb",
  48. "uart",
  49. "pcie",
  50. "usb3",
  51. "sata",
  52. "rsvd",
  53. };
  54. static const unsigned int tegra124_otg_functions[] = {
  55. TEGRA124_FUNC_SNPS,
  56. TEGRA124_FUNC_XUSB,
  57. TEGRA124_FUNC_UART,
  58. TEGRA124_FUNC_RSVD,
  59. };
  60. static const unsigned int tegra124_usb_functions[] = {
  61. TEGRA124_FUNC_SNPS,
  62. TEGRA124_FUNC_XUSB,
  63. };
  64. static const unsigned int tegra124_pci_functions[] = {
  65. TEGRA124_FUNC_PCIE,
  66. TEGRA124_FUNC_USB3,
  67. TEGRA124_FUNC_SATA,
  68. TEGRA124_FUNC_RSVD,
  69. };
  70. struct tegra_xusb_padctl_lane {
  71. const char *name;
  72. unsigned int offset;
  73. unsigned int shift;
  74. unsigned int mask;
  75. unsigned int iddq;
  76. const unsigned int *funcs;
  77. unsigned int num_funcs;
  78. };
  79. #define TEGRA124_LANE(_name, _offset, _shift, _mask, _iddq, _funcs) \
  80. { \
  81. .name = _name, \
  82. .offset = _offset, \
  83. .shift = _shift, \
  84. .mask = _mask, \
  85. .iddq = _iddq, \
  86. .num_funcs = ARRAY_SIZE(tegra124_##_funcs##_functions), \
  87. .funcs = tegra124_##_funcs##_functions, \
  88. }
  89. static const struct tegra_xusb_padctl_lane tegra124_lanes[] = {
  90. TEGRA124_LANE("otg-0", 0x004, 0, 0x3, 0, otg),
  91. TEGRA124_LANE("otg-1", 0x004, 2, 0x3, 0, otg),
  92. TEGRA124_LANE("otg-2", 0x004, 4, 0x3, 0, otg),
  93. TEGRA124_LANE("ulpi-0", 0x004, 12, 0x1, 0, usb),
  94. TEGRA124_LANE("hsic-0", 0x004, 14, 0x1, 0, usb),
  95. TEGRA124_LANE("hsic-1", 0x004, 15, 0x1, 0, usb),
  96. TEGRA124_LANE("pcie-0", 0x134, 16, 0x3, 1, pci),
  97. TEGRA124_LANE("pcie-1", 0x134, 18, 0x3, 2, pci),
  98. TEGRA124_LANE("pcie-2", 0x134, 20, 0x3, 3, pci),
  99. TEGRA124_LANE("pcie-3", 0x134, 22, 0x3, 4, pci),
  100. TEGRA124_LANE("pcie-4", 0x134, 24, 0x3, 5, pci),
  101. TEGRA124_LANE("sata-0", 0x134, 26, 0x3, 6, pci),
  102. };
  103. struct tegra_xusb_phy_ops {
  104. int (*prepare)(struct tegra_xusb_phy *phy);
  105. int (*enable)(struct tegra_xusb_phy *phy);
  106. int (*disable)(struct tegra_xusb_phy *phy);
  107. int (*unprepare)(struct tegra_xusb_phy *phy);
  108. };
  109. struct tegra_xusb_phy {
  110. const struct tegra_xusb_phy_ops *ops;
  111. struct tegra_xusb_padctl *padctl;
  112. };
  113. struct tegra_xusb_padctl_pin {
  114. const struct tegra_xusb_padctl_lane *lane;
  115. unsigned int func;
  116. int iddq;
  117. };
  118. #define MAX_GROUPS 3
  119. #define MAX_PINS 6
  120. struct tegra_xusb_padctl_group {
  121. const char *name;
  122. const char *pins[MAX_PINS];
  123. unsigned int num_pins;
  124. const char *func;
  125. int iddq;
  126. };
  127. struct tegra_xusb_padctl_config {
  128. const char *name;
  129. struct tegra_xusb_padctl_group groups[MAX_GROUPS];
  130. unsigned int num_groups;
  131. };
  132. struct tegra_xusb_padctl {
  133. struct fdt_resource regs;
  134. unsigned int enable;
  135. struct tegra_xusb_phy phys[2];
  136. const struct tegra_xusb_padctl_lane *lanes;
  137. unsigned int num_lanes;
  138. const char *const *functions;
  139. unsigned int num_functions;
  140. struct tegra_xusb_padctl_config config;
  141. };
  142. static inline u32 padctl_readl(struct tegra_xusb_padctl *padctl,
  143. unsigned long offset)
  144. {
  145. return readl(padctl->regs.start + offset);
  146. }
  147. static inline void padctl_writel(struct tegra_xusb_padctl *padctl,
  148. u32 value, unsigned long offset)
  149. {
  150. writel(value, padctl->regs.start + offset);
  151. }
  152. static int tegra_xusb_padctl_enable(struct tegra_xusb_padctl *padctl)
  153. {
  154. u32 value;
  155. if (padctl->enable++ > 0)
  156. return 0;
  157. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  158. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
  159. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  160. udelay(100);
  161. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  162. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
  163. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  164. udelay(100);
  165. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  166. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
  167. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  168. return 0;
  169. }
  170. static int tegra_xusb_padctl_disable(struct tegra_xusb_padctl *padctl)
  171. {
  172. u32 value;
  173. if (padctl->enable == 0) {
  174. error("tegra-xusb-padctl: unbalanced enable/disable");
  175. return 0;
  176. }
  177. if (--padctl->enable > 0)
  178. return 0;
  179. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  180. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
  181. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  182. udelay(100);
  183. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  184. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
  185. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  186. udelay(100);
  187. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  188. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
  189. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  190. return 0;
  191. }
  192. static int phy_prepare(struct tegra_xusb_phy *phy)
  193. {
  194. return tegra_xusb_padctl_enable(phy->padctl);
  195. }
  196. static int phy_unprepare(struct tegra_xusb_phy *phy)
  197. {
  198. return tegra_xusb_padctl_disable(phy->padctl);
  199. }
  200. static int pcie_phy_enable(struct tegra_xusb_phy *phy)
  201. {
  202. struct tegra_xusb_padctl *padctl = phy->padctl;
  203. int err = -ETIMEDOUT;
  204. unsigned long start;
  205. u32 value;
  206. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  207. value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK;
  208. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  209. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
  210. value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN |
  211. XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN |
  212. XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL;
  213. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
  214. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  215. value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
  216. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  217. start = get_timer(0);
  218. while (get_timer(start) < 50) {
  219. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  220. if (value & XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET) {
  221. err = 0;
  222. break;
  223. }
  224. }
  225. return err;
  226. }
  227. static int pcie_phy_disable(struct tegra_xusb_phy *phy)
  228. {
  229. struct tegra_xusb_padctl *padctl = phy->padctl;
  230. u32 value;
  231. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  232. value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
  233. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  234. return 0;
  235. }
  236. static int sata_phy_enable(struct tegra_xusb_phy *phy)
  237. {
  238. struct tegra_xusb_padctl *padctl = phy->padctl;
  239. int err = -ETIMEDOUT;
  240. unsigned long start;
  241. u32 value;
  242. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  243. value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
  244. value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
  245. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  246. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  247. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
  248. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
  249. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  250. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  251. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
  252. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  253. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  254. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
  255. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  256. start = get_timer(0);
  257. while (get_timer(start) < 50) {
  258. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  259. if (value & XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET) {
  260. err = 0;
  261. break;
  262. }
  263. }
  264. return err;
  265. }
  266. static int sata_phy_disable(struct tegra_xusb_phy *phy)
  267. {
  268. struct tegra_xusb_padctl *padctl = phy->padctl;
  269. u32 value;
  270. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  271. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
  272. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  273. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  274. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
  275. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  276. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  277. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
  278. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
  279. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  280. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  281. value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
  282. value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
  283. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  284. return 0;
  285. }
  286. static const struct tegra_xusb_phy_ops pcie_phy_ops = {
  287. .prepare = phy_prepare,
  288. .enable = pcie_phy_enable,
  289. .disable = pcie_phy_disable,
  290. .unprepare = phy_unprepare,
  291. };
  292. static const struct tegra_xusb_phy_ops sata_phy_ops = {
  293. .prepare = phy_prepare,
  294. .enable = sata_phy_enable,
  295. .disable = sata_phy_disable,
  296. .unprepare = phy_unprepare,
  297. };
  298. static struct tegra_xusb_padctl *padctl = &(struct tegra_xusb_padctl) {
  299. .phys = {
  300. [0] = {
  301. .ops = &pcie_phy_ops,
  302. },
  303. [1] = {
  304. .ops = &sata_phy_ops,
  305. },
  306. },
  307. };
  308. static const struct tegra_xusb_padctl_lane *
  309. tegra_xusb_padctl_find_lane(struct tegra_xusb_padctl *padctl, const char *name)
  310. {
  311. unsigned int i;
  312. for (i = 0; i < padctl->num_lanes; i++)
  313. if (strcmp(name, padctl->lanes[i].name) == 0)
  314. return &padctl->lanes[i];
  315. return NULL;
  316. }
  317. static int
  318. tegra_xusb_padctl_group_parse_dt(struct tegra_xusb_padctl *padctl,
  319. struct tegra_xusb_padctl_group *group,
  320. const void *fdt, int node)
  321. {
  322. unsigned int i;
  323. int len, err;
  324. group->name = fdt_get_name(fdt, node, &len);
  325. len = fdt_count_strings(fdt, node, "nvidia,lanes");
  326. if (len < 0) {
  327. error("tegra-xusb-padctl: failed to parse \"nvidia,lanes\" property");
  328. return -EINVAL;
  329. }
  330. group->num_pins = len;
  331. for (i = 0; i < group->num_pins; i++) {
  332. err = fdt_get_string_index(fdt, node, "nvidia,lanes", i,
  333. &group->pins[i]);
  334. if (err < 0) {
  335. error("tegra-xusb-padctl: failed to read string from \"nvidia,lanes\" property");
  336. return -EINVAL;
  337. }
  338. }
  339. group->num_pins = len;
  340. err = fdt_get_string(fdt, node, "nvidia,function", &group->func);
  341. if (err < 0) {
  342. error("tegra-xusb-padctl: failed to parse \"nvidia,func\" property");
  343. return -EINVAL;
  344. }
  345. group->iddq = fdtdec_get_int(fdt, node, "nvidia,iddq", -1);
  346. return 0;
  347. }
  348. static int tegra_xusb_padctl_find_function(struct tegra_xusb_padctl *padctl,
  349. const char *name)
  350. {
  351. unsigned int i;
  352. for (i = 0; i < padctl->num_functions; i++)
  353. if (strcmp(name, padctl->functions[i]) == 0)
  354. return i;
  355. return -ENOENT;
  356. }
  357. static int
  358. tegra_xusb_padctl_lane_find_function(struct tegra_xusb_padctl *padctl,
  359. const struct tegra_xusb_padctl_lane *lane,
  360. const char *name)
  361. {
  362. unsigned int i;
  363. int func;
  364. func = tegra_xusb_padctl_find_function(padctl, name);
  365. if (func < 0)
  366. return func;
  367. for (i = 0; i < lane->num_funcs; i++)
  368. if (lane->funcs[i] == func)
  369. return i;
  370. return -ENOENT;
  371. }
  372. static int
  373. tegra_xusb_padctl_group_apply(struct tegra_xusb_padctl *padctl,
  374. const struct tegra_xusb_padctl_group *group)
  375. {
  376. unsigned int i;
  377. for (i = 0; i < group->num_pins; i++) {
  378. const struct tegra_xusb_padctl_lane *lane;
  379. unsigned int func;
  380. u32 value;
  381. lane = tegra_xusb_padctl_find_lane(padctl, group->pins[i]);
  382. if (!lane) {
  383. error("tegra-xusb-padctl: no lane for pin %s",
  384. group->pins[i]);
  385. continue;
  386. }
  387. func = tegra_xusb_padctl_lane_find_function(padctl, lane,
  388. group->func);
  389. if (func < 0) {
  390. error("tegra-xusb-padctl: function %s invalid for lane %s: %d",
  391. group->func, lane->name, func);
  392. continue;
  393. }
  394. value = padctl_readl(padctl, lane->offset);
  395. /* set pin function */
  396. value &= ~(lane->mask << lane->shift);
  397. value |= func << lane->shift;
  398. /*
  399. * Set IDDQ if supported on the lane and specified in the
  400. * configuration.
  401. */
  402. if (lane->iddq > 0 && group->iddq >= 0) {
  403. if (group->iddq != 0)
  404. value &= ~(1 << lane->iddq);
  405. else
  406. value |= 1 << lane->iddq;
  407. }
  408. padctl_writel(padctl, value, lane->offset);
  409. }
  410. return 0;
  411. }
  412. static int
  413. tegra_xusb_padctl_config_apply(struct tegra_xusb_padctl *padctl,
  414. struct tegra_xusb_padctl_config *config)
  415. {
  416. unsigned int i;
  417. for (i = 0; i < config->num_groups; i++) {
  418. const struct tegra_xusb_padctl_group *group;
  419. int err;
  420. group = &config->groups[i];
  421. err = tegra_xusb_padctl_group_apply(padctl, group);
  422. if (err < 0) {
  423. error("tegra-xusb-padctl: failed to apply group %s: %d",
  424. group->name, err);
  425. continue;
  426. }
  427. }
  428. return 0;
  429. }
  430. static int
  431. tegra_xusb_padctl_config_parse_dt(struct tegra_xusb_padctl *padctl,
  432. struct tegra_xusb_padctl_config *config,
  433. const void *fdt, int node)
  434. {
  435. int subnode;
  436. config->name = fdt_get_name(fdt, node, NULL);
  437. fdt_for_each_subnode(fdt, subnode, node) {
  438. struct tegra_xusb_padctl_group *group;
  439. int err;
  440. group = &config->groups[config->num_groups];
  441. err = tegra_xusb_padctl_group_parse_dt(padctl, group, fdt,
  442. subnode);
  443. if (err < 0) {
  444. error("tegra-xusb-padctl: failed to parse group %s",
  445. group->name);
  446. return err;
  447. }
  448. config->num_groups++;
  449. }
  450. return 0;
  451. }
  452. static int tegra_xusb_padctl_parse_dt(struct tegra_xusb_padctl *padctl,
  453. const void *fdt, int node)
  454. {
  455. int subnode, err;
  456. err = fdt_get_resource(fdt, node, "reg", 0, &padctl->regs);
  457. if (err < 0) {
  458. error("tegra-xusb-padctl: registers not found");
  459. return err;
  460. }
  461. fdt_for_each_subnode(fdt, subnode, node) {
  462. struct tegra_xusb_padctl_config *config = &padctl->config;
  463. err = tegra_xusb_padctl_config_parse_dt(padctl, config, fdt,
  464. subnode);
  465. if (err < 0) {
  466. error("tegra-xusb-padctl: failed to parse entry %s: %d",
  467. config->name, err);
  468. continue;
  469. }
  470. }
  471. return 0;
  472. }
  473. static int process_nodes(const void *fdt, int nodes[], unsigned int count)
  474. {
  475. unsigned int i;
  476. for (i = 0; i < count; i++) {
  477. enum fdt_compat_id id;
  478. int err;
  479. if (!fdtdec_get_is_enabled(fdt, nodes[i]))
  480. continue;
  481. id = fdtdec_lookup(fdt, nodes[i]);
  482. switch (id) {
  483. case COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL:
  484. break;
  485. default:
  486. error("tegra-xusb-padctl: unsupported compatible: %s",
  487. fdtdec_get_compatible(id));
  488. continue;
  489. }
  490. padctl->num_lanes = ARRAY_SIZE(tegra124_lanes);
  491. padctl->lanes = tegra124_lanes;
  492. padctl->num_functions = ARRAY_SIZE(tegra124_functions);
  493. padctl->functions = tegra124_functions;
  494. err = tegra_xusb_padctl_parse_dt(padctl, fdt, nodes[i]);
  495. if (err < 0) {
  496. error("tegra-xusb-padctl: failed to parse DT: %d",
  497. err);
  498. continue;
  499. }
  500. /* deassert XUSB padctl reset */
  501. reset_set_enable(PERIPH_ID_XUSB_PADCTL, 0);
  502. err = tegra_xusb_padctl_config_apply(padctl, &padctl->config);
  503. if (err < 0) {
  504. error("tegra-xusb-padctl: failed to apply pinmux: %d",
  505. err);
  506. continue;
  507. }
  508. /* only a single instance is supported */
  509. break;
  510. }
  511. return 0;
  512. }
  513. struct tegra_xusb_phy *tegra_xusb_phy_get(unsigned int type)
  514. {
  515. struct tegra_xusb_phy *phy = NULL;
  516. switch (type) {
  517. case TEGRA_XUSB_PADCTL_PCIE:
  518. phy = &padctl->phys[0];
  519. phy->padctl = padctl;
  520. break;
  521. case TEGRA_XUSB_PADCTL_SATA:
  522. phy = &padctl->phys[1];
  523. phy->padctl = padctl;
  524. break;
  525. }
  526. return phy;
  527. }
  528. int tegra_xusb_phy_prepare(struct tegra_xusb_phy *phy)
  529. {
  530. if (phy && phy->ops && phy->ops->prepare)
  531. return phy->ops->prepare(phy);
  532. return phy ? -ENOSYS : -EINVAL;
  533. }
  534. int tegra_xusb_phy_enable(struct tegra_xusb_phy *phy)
  535. {
  536. if (phy && phy->ops && phy->ops->enable)
  537. return phy->ops->enable(phy);
  538. return phy ? -ENOSYS : -EINVAL;
  539. }
  540. int tegra_xusb_phy_disable(struct tegra_xusb_phy *phy)
  541. {
  542. if (phy && phy->ops && phy->ops->disable)
  543. return phy->ops->disable(phy);
  544. return phy ? -ENOSYS : -EINVAL;
  545. }
  546. int tegra_xusb_phy_unprepare(struct tegra_xusb_phy *phy)
  547. {
  548. if (phy && phy->ops && phy->ops->unprepare)
  549. return phy->ops->unprepare(phy);
  550. return phy ? -ENOSYS : -EINVAL;
  551. }
  552. void tegra_xusb_padctl_init(const void *fdt)
  553. {
  554. int count, nodes[1];
  555. count = fdtdec_find_aliases_for_id(fdt, "padctl",
  556. COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
  557. nodes, ARRAY_SIZE(nodes));
  558. if (process_nodes(fdt, nodes, count))
  559. return;
  560. }