pinctrl-tegra-xusb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/phy/phy.h>
  18. #include <linux/pinctrl/pinctrl.h>
  19. #include <linux/pinctrl/pinmux.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/reset.h>
  22. #include <linux/slab.h>
  23. #include <dt-bindings/pinctrl/pinctrl-tegra-xusb.h>
  24. #include "../core.h"
  25. #include "../pinctrl-utils.h"
  26. #define XUSB_PADCTL_ELPG_PROGRAM 0x01c
  27. #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN (1 << 26)
  28. #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY (1 << 25)
  29. #define XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN (1 << 24)
  30. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1 0x040
  31. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET (1 << 19)
  32. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK (0xf << 12)
  33. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST (1 << 1)
  34. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2 0x044
  35. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN (1 << 6)
  36. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN (1 << 5)
  37. #define XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL (1 << 4)
  38. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1 0x138
  39. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET (1 << 27)
  40. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE (1 << 24)
  41. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD (1 << 3)
  42. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST (1 << 1)
  43. #define XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ (1 << 0)
  44. #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1 0x148
  45. #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD (1 << 1)
  46. #define XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ (1 << 0)
  47. struct tegra_xusb_padctl_function {
  48. const char *name;
  49. const char * const *groups;
  50. unsigned int num_groups;
  51. };
  52. struct tegra_xusb_padctl_soc {
  53. const struct pinctrl_pin_desc *pins;
  54. unsigned int num_pins;
  55. const struct tegra_xusb_padctl_function *functions;
  56. unsigned int num_functions;
  57. const struct tegra_xusb_padctl_lane *lanes;
  58. unsigned int num_lanes;
  59. };
  60. struct tegra_xusb_padctl_lane {
  61. const char *name;
  62. unsigned int offset;
  63. unsigned int shift;
  64. unsigned int mask;
  65. unsigned int iddq;
  66. const unsigned int *funcs;
  67. unsigned int num_funcs;
  68. };
  69. struct tegra_xusb_padctl {
  70. struct device *dev;
  71. void __iomem *regs;
  72. struct mutex lock;
  73. struct reset_control *rst;
  74. const struct tegra_xusb_padctl_soc *soc;
  75. struct pinctrl_dev *pinctrl;
  76. struct pinctrl_desc desc;
  77. struct phy_provider *provider;
  78. struct phy *phys[2];
  79. unsigned int enable;
  80. };
  81. static inline void padctl_writel(struct tegra_xusb_padctl *padctl, u32 value,
  82. unsigned long offset)
  83. {
  84. writel(value, padctl->regs + offset);
  85. }
  86. static inline u32 padctl_readl(struct tegra_xusb_padctl *padctl,
  87. unsigned long offset)
  88. {
  89. return readl(padctl->regs + offset);
  90. }
  91. static int tegra_xusb_padctl_get_groups_count(struct pinctrl_dev *pinctrl)
  92. {
  93. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  94. return padctl->soc->num_pins;
  95. }
  96. static const char *tegra_xusb_padctl_get_group_name(struct pinctrl_dev *pinctrl,
  97. unsigned int group)
  98. {
  99. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  100. return padctl->soc->pins[group].name;
  101. }
  102. static int tegra_xusb_padctl_get_group_pins(struct pinctrl_dev *pinctrl,
  103. unsigned group,
  104. const unsigned **pins,
  105. unsigned *num_pins)
  106. {
  107. /*
  108. * For the tegra-xusb pad controller groups are synonomous
  109. * with lanes/pins and there is always one lane/pin per group.
  110. */
  111. *pins = &pinctrl->desc->pins[group].number;
  112. *num_pins = 1;
  113. return 0;
  114. }
  115. enum tegra_xusb_padctl_param {
  116. TEGRA_XUSB_PADCTL_IDDQ,
  117. };
  118. static const struct tegra_xusb_padctl_property {
  119. const char *name;
  120. enum tegra_xusb_padctl_param param;
  121. } properties[] = {
  122. { "nvidia,iddq", TEGRA_XUSB_PADCTL_IDDQ },
  123. };
  124. #define TEGRA_XUSB_PADCTL_PACK(param, value) ((param) << 16 | (value))
  125. #define TEGRA_XUSB_PADCTL_UNPACK_PARAM(config) ((config) >> 16)
  126. #define TEGRA_XUSB_PADCTL_UNPACK_VALUE(config) ((config) & 0xffff)
  127. static int tegra_xusb_padctl_parse_subnode(struct tegra_xusb_padctl *padctl,
  128. struct device_node *np,
  129. struct pinctrl_map **maps,
  130. unsigned int *reserved_maps,
  131. unsigned int *num_maps)
  132. {
  133. unsigned int i, reserve = 0, num_configs = 0;
  134. unsigned long config, *configs = NULL;
  135. const char *function, *group;
  136. struct property *prop;
  137. int err = 0;
  138. u32 value;
  139. err = of_property_read_string(np, "nvidia,function", &function);
  140. if (err < 0) {
  141. if (err != -EINVAL)
  142. return err;
  143. function = NULL;
  144. }
  145. for (i = 0; i < ARRAY_SIZE(properties); i++) {
  146. err = of_property_read_u32(np, properties[i].name, &value);
  147. if (err < 0) {
  148. if (err == -EINVAL)
  149. continue;
  150. goto out;
  151. }
  152. config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, value);
  153. err = pinctrl_utils_add_config(padctl->pinctrl, &configs,
  154. &num_configs, config);
  155. if (err < 0)
  156. goto out;
  157. }
  158. if (function)
  159. reserve++;
  160. if (num_configs)
  161. reserve++;
  162. err = of_property_count_strings(np, "nvidia,lanes");
  163. if (err < 0)
  164. goto out;
  165. reserve *= err;
  166. err = pinctrl_utils_reserve_map(padctl->pinctrl, maps, reserved_maps,
  167. num_maps, reserve);
  168. if (err < 0)
  169. goto out;
  170. of_property_for_each_string(np, "nvidia,lanes", prop, group) {
  171. if (function) {
  172. err = pinctrl_utils_add_map_mux(padctl->pinctrl, maps,
  173. reserved_maps, num_maps, group,
  174. function);
  175. if (err < 0)
  176. goto out;
  177. }
  178. if (num_configs) {
  179. err = pinctrl_utils_add_map_configs(padctl->pinctrl,
  180. maps, reserved_maps, num_maps, group,
  181. configs, num_configs,
  182. PIN_MAP_TYPE_CONFIGS_GROUP);
  183. if (err < 0)
  184. goto out;
  185. }
  186. }
  187. err = 0;
  188. out:
  189. kfree(configs);
  190. return err;
  191. }
  192. static int tegra_xusb_padctl_dt_node_to_map(struct pinctrl_dev *pinctrl,
  193. struct device_node *parent,
  194. struct pinctrl_map **maps,
  195. unsigned int *num_maps)
  196. {
  197. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  198. unsigned int reserved_maps = 0;
  199. struct device_node *np;
  200. int err;
  201. *num_maps = 0;
  202. *maps = NULL;
  203. for_each_child_of_node(parent, np) {
  204. err = tegra_xusb_padctl_parse_subnode(padctl, np, maps,
  205. &reserved_maps,
  206. num_maps);
  207. if (err < 0) {
  208. of_node_put(np);
  209. return err;
  210. }
  211. }
  212. return 0;
  213. }
  214. static const struct pinctrl_ops tegra_xusb_padctl_pinctrl_ops = {
  215. .get_groups_count = tegra_xusb_padctl_get_groups_count,
  216. .get_group_name = tegra_xusb_padctl_get_group_name,
  217. .get_group_pins = tegra_xusb_padctl_get_group_pins,
  218. .dt_node_to_map = tegra_xusb_padctl_dt_node_to_map,
  219. .dt_free_map = pinctrl_utils_free_map,
  220. };
  221. static int tegra_xusb_padctl_get_functions_count(struct pinctrl_dev *pinctrl)
  222. {
  223. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  224. return padctl->soc->num_functions;
  225. }
  226. static const char *
  227. tegra_xusb_padctl_get_function_name(struct pinctrl_dev *pinctrl,
  228. unsigned int function)
  229. {
  230. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  231. return padctl->soc->functions[function].name;
  232. }
  233. static int tegra_xusb_padctl_get_function_groups(struct pinctrl_dev *pinctrl,
  234. unsigned int function,
  235. const char * const **groups,
  236. unsigned * const num_groups)
  237. {
  238. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  239. *num_groups = padctl->soc->functions[function].num_groups;
  240. *groups = padctl->soc->functions[function].groups;
  241. return 0;
  242. }
  243. static int tegra_xusb_padctl_pinmux_set(struct pinctrl_dev *pinctrl,
  244. unsigned int function,
  245. unsigned int group)
  246. {
  247. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  248. const struct tegra_xusb_padctl_lane *lane;
  249. unsigned int i;
  250. u32 value;
  251. lane = &padctl->soc->lanes[group];
  252. for (i = 0; i < lane->num_funcs; i++)
  253. if (lane->funcs[i] == function)
  254. break;
  255. if (i >= lane->num_funcs)
  256. return -EINVAL;
  257. value = padctl_readl(padctl, lane->offset);
  258. value &= ~(lane->mask << lane->shift);
  259. value |= i << lane->shift;
  260. padctl_writel(padctl, value, lane->offset);
  261. return 0;
  262. }
  263. static const struct pinmux_ops tegra_xusb_padctl_pinmux_ops = {
  264. .get_functions_count = tegra_xusb_padctl_get_functions_count,
  265. .get_function_name = tegra_xusb_padctl_get_function_name,
  266. .get_function_groups = tegra_xusb_padctl_get_function_groups,
  267. .set_mux = tegra_xusb_padctl_pinmux_set,
  268. };
  269. static int tegra_xusb_padctl_pinconf_group_get(struct pinctrl_dev *pinctrl,
  270. unsigned int group,
  271. unsigned long *config)
  272. {
  273. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  274. const struct tegra_xusb_padctl_lane *lane;
  275. enum tegra_xusb_padctl_param param;
  276. u32 value;
  277. param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(*config);
  278. lane = &padctl->soc->lanes[group];
  279. switch (param) {
  280. case TEGRA_XUSB_PADCTL_IDDQ:
  281. /* lanes with iddq == 0 don't support this parameter */
  282. if (lane->iddq == 0)
  283. return -EINVAL;
  284. value = padctl_readl(padctl, lane->offset);
  285. if (value & BIT(lane->iddq))
  286. value = 0;
  287. else
  288. value = 1;
  289. *config = TEGRA_XUSB_PADCTL_PACK(param, value);
  290. break;
  291. default:
  292. dev_err(padctl->dev, "invalid configuration parameter: %04x\n",
  293. param);
  294. return -ENOTSUPP;
  295. }
  296. return 0;
  297. }
  298. static int tegra_xusb_padctl_pinconf_group_set(struct pinctrl_dev *pinctrl,
  299. unsigned int group,
  300. unsigned long *configs,
  301. unsigned int num_configs)
  302. {
  303. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  304. const struct tegra_xusb_padctl_lane *lane;
  305. enum tegra_xusb_padctl_param param;
  306. unsigned long value;
  307. unsigned int i;
  308. u32 regval;
  309. lane = &padctl->soc->lanes[group];
  310. for (i = 0; i < num_configs; i++) {
  311. param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(configs[i]);
  312. value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(configs[i]);
  313. switch (param) {
  314. case TEGRA_XUSB_PADCTL_IDDQ:
  315. /* lanes with iddq == 0 don't support this parameter */
  316. if (lane->iddq == 0)
  317. return -EINVAL;
  318. regval = padctl_readl(padctl, lane->offset);
  319. if (value)
  320. regval &= ~BIT(lane->iddq);
  321. else
  322. regval |= BIT(lane->iddq);
  323. padctl_writel(padctl, regval, lane->offset);
  324. break;
  325. default:
  326. dev_err(padctl->dev,
  327. "invalid configuration parameter: %04x\n",
  328. param);
  329. return -ENOTSUPP;
  330. }
  331. }
  332. return 0;
  333. }
  334. #ifdef CONFIG_DEBUG_FS
  335. static const char *strip_prefix(const char *s)
  336. {
  337. const char *comma = strchr(s, ',');
  338. if (!comma)
  339. return s;
  340. return comma + 1;
  341. }
  342. static void
  343. tegra_xusb_padctl_pinconf_group_dbg_show(struct pinctrl_dev *pinctrl,
  344. struct seq_file *s,
  345. unsigned int group)
  346. {
  347. unsigned int i;
  348. for (i = 0; i < ARRAY_SIZE(properties); i++) {
  349. unsigned long config, value;
  350. int err;
  351. config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, 0);
  352. err = tegra_xusb_padctl_pinconf_group_get(pinctrl, group,
  353. &config);
  354. if (err < 0)
  355. continue;
  356. value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
  357. seq_printf(s, "\n\t%s=%lu\n", strip_prefix(properties[i].name),
  358. value);
  359. }
  360. }
  361. static void
  362. tegra_xusb_padctl_pinconf_config_dbg_show(struct pinctrl_dev *pinctrl,
  363. struct seq_file *s,
  364. unsigned long config)
  365. {
  366. enum tegra_xusb_padctl_param param;
  367. const char *name = "unknown";
  368. unsigned long value;
  369. unsigned int i;
  370. param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(config);
  371. value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
  372. for (i = 0; i < ARRAY_SIZE(properties); i++) {
  373. if (properties[i].param == param) {
  374. name = properties[i].name;
  375. break;
  376. }
  377. }
  378. seq_printf(s, "%s=%lu", strip_prefix(name), value);
  379. }
  380. #endif
  381. static const struct pinconf_ops tegra_xusb_padctl_pinconf_ops = {
  382. .pin_config_group_get = tegra_xusb_padctl_pinconf_group_get,
  383. .pin_config_group_set = tegra_xusb_padctl_pinconf_group_set,
  384. #ifdef CONFIG_DEBUG_FS
  385. .pin_config_group_dbg_show = tegra_xusb_padctl_pinconf_group_dbg_show,
  386. .pin_config_config_dbg_show = tegra_xusb_padctl_pinconf_config_dbg_show,
  387. #endif
  388. };
  389. static int tegra_xusb_padctl_enable(struct tegra_xusb_padctl *padctl)
  390. {
  391. u32 value;
  392. mutex_lock(&padctl->lock);
  393. if (padctl->enable++ > 0)
  394. goto out;
  395. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  396. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
  397. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  398. usleep_range(100, 200);
  399. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  400. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
  401. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  402. usleep_range(100, 200);
  403. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  404. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
  405. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  406. out:
  407. mutex_unlock(&padctl->lock);
  408. return 0;
  409. }
  410. static int tegra_xusb_padctl_disable(struct tegra_xusb_padctl *padctl)
  411. {
  412. u32 value;
  413. mutex_lock(&padctl->lock);
  414. if (WARN_ON(padctl->enable == 0))
  415. goto out;
  416. if (--padctl->enable > 0)
  417. goto out;
  418. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  419. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
  420. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  421. usleep_range(100, 200);
  422. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  423. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
  424. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  425. usleep_range(100, 200);
  426. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  427. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
  428. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  429. out:
  430. mutex_unlock(&padctl->lock);
  431. return 0;
  432. }
  433. static int tegra_xusb_phy_init(struct phy *phy)
  434. {
  435. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  436. return tegra_xusb_padctl_enable(padctl);
  437. }
  438. static int tegra_xusb_phy_exit(struct phy *phy)
  439. {
  440. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  441. return tegra_xusb_padctl_disable(padctl);
  442. }
  443. static int pcie_phy_power_on(struct phy *phy)
  444. {
  445. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  446. unsigned long timeout;
  447. int err = -ETIMEDOUT;
  448. u32 value;
  449. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  450. value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK;
  451. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  452. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
  453. value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN |
  454. XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN |
  455. XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL;
  456. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
  457. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  458. value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
  459. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  460. timeout = jiffies + msecs_to_jiffies(50);
  461. while (time_before(jiffies, timeout)) {
  462. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  463. if (value & XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET) {
  464. err = 0;
  465. break;
  466. }
  467. usleep_range(100, 200);
  468. }
  469. return err;
  470. }
  471. static int pcie_phy_power_off(struct phy *phy)
  472. {
  473. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  474. u32 value;
  475. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  476. value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
  477. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  478. return 0;
  479. }
  480. static const struct phy_ops pcie_phy_ops = {
  481. .init = tegra_xusb_phy_init,
  482. .exit = tegra_xusb_phy_exit,
  483. .power_on = pcie_phy_power_on,
  484. .power_off = pcie_phy_power_off,
  485. .owner = THIS_MODULE,
  486. };
  487. static int sata_phy_power_on(struct phy *phy)
  488. {
  489. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  490. unsigned long timeout;
  491. int err = -ETIMEDOUT;
  492. u32 value;
  493. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  494. value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
  495. value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
  496. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  497. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  498. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
  499. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
  500. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  501. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  502. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
  503. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  504. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  505. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
  506. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  507. timeout = jiffies + msecs_to_jiffies(50);
  508. while (time_before(jiffies, timeout)) {
  509. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  510. if (value & XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET) {
  511. err = 0;
  512. break;
  513. }
  514. usleep_range(100, 200);
  515. }
  516. return err;
  517. }
  518. static int sata_phy_power_off(struct phy *phy)
  519. {
  520. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  521. u32 value;
  522. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  523. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
  524. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  525. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  526. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
  527. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  528. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  529. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
  530. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
  531. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  532. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  533. value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
  534. value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
  535. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  536. return 0;
  537. }
  538. static const struct phy_ops sata_phy_ops = {
  539. .init = tegra_xusb_phy_init,
  540. .exit = tegra_xusb_phy_exit,
  541. .power_on = sata_phy_power_on,
  542. .power_off = sata_phy_power_off,
  543. .owner = THIS_MODULE,
  544. };
  545. static struct phy *tegra_xusb_padctl_xlate(struct device *dev,
  546. struct of_phandle_args *args)
  547. {
  548. struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
  549. unsigned int index = args->args[0];
  550. if (args->args_count <= 0)
  551. return ERR_PTR(-EINVAL);
  552. if (index >= ARRAY_SIZE(padctl->phys))
  553. return ERR_PTR(-EINVAL);
  554. return padctl->phys[index];
  555. }
  556. #define PIN_OTG_0 0
  557. #define PIN_OTG_1 1
  558. #define PIN_OTG_2 2
  559. #define PIN_ULPI_0 3
  560. #define PIN_HSIC_0 4
  561. #define PIN_HSIC_1 5
  562. #define PIN_PCIE_0 6
  563. #define PIN_PCIE_1 7
  564. #define PIN_PCIE_2 8
  565. #define PIN_PCIE_3 9
  566. #define PIN_PCIE_4 10
  567. #define PIN_SATA_0 11
  568. static const struct pinctrl_pin_desc tegra124_pins[] = {
  569. PINCTRL_PIN(PIN_OTG_0, "otg-0"),
  570. PINCTRL_PIN(PIN_OTG_1, "otg-1"),
  571. PINCTRL_PIN(PIN_OTG_2, "otg-2"),
  572. PINCTRL_PIN(PIN_ULPI_0, "ulpi-0"),
  573. PINCTRL_PIN(PIN_HSIC_0, "hsic-0"),
  574. PINCTRL_PIN(PIN_HSIC_1, "hsic-1"),
  575. PINCTRL_PIN(PIN_PCIE_0, "pcie-0"),
  576. PINCTRL_PIN(PIN_PCIE_1, "pcie-1"),
  577. PINCTRL_PIN(PIN_PCIE_2, "pcie-2"),
  578. PINCTRL_PIN(PIN_PCIE_3, "pcie-3"),
  579. PINCTRL_PIN(PIN_PCIE_4, "pcie-4"),
  580. PINCTRL_PIN(PIN_SATA_0, "sata-0"),
  581. };
  582. static const char * const tegra124_snps_groups[] = {
  583. "otg-0",
  584. "otg-1",
  585. "otg-2",
  586. "ulpi-0",
  587. "hsic-0",
  588. "hsic-1",
  589. };
  590. static const char * const tegra124_xusb_groups[] = {
  591. "otg-0",
  592. "otg-1",
  593. "otg-2",
  594. "ulpi-0",
  595. "hsic-0",
  596. "hsic-1",
  597. };
  598. static const char * const tegra124_uart_groups[] = {
  599. "otg-0",
  600. "otg-1",
  601. "otg-2",
  602. };
  603. static const char * const tegra124_pcie_groups[] = {
  604. "pcie-0",
  605. "pcie-1",
  606. "pcie-2",
  607. "pcie-3",
  608. "pcie-4",
  609. };
  610. static const char * const tegra124_usb3_groups[] = {
  611. "pcie-0",
  612. "pcie-1",
  613. "sata-0",
  614. };
  615. static const char * const tegra124_sata_groups[] = {
  616. "sata-0",
  617. };
  618. static const char * const tegra124_rsvd_groups[] = {
  619. "otg-0",
  620. "otg-1",
  621. "otg-2",
  622. "pcie-0",
  623. "pcie-1",
  624. "pcie-2",
  625. "pcie-3",
  626. "pcie-4",
  627. "sata-0",
  628. };
  629. #define TEGRA124_FUNCTION(_name) \
  630. { \
  631. .name = #_name, \
  632. .num_groups = ARRAY_SIZE(tegra124_##_name##_groups), \
  633. .groups = tegra124_##_name##_groups, \
  634. }
  635. static struct tegra_xusb_padctl_function tegra124_functions[] = {
  636. TEGRA124_FUNCTION(snps),
  637. TEGRA124_FUNCTION(xusb),
  638. TEGRA124_FUNCTION(uart),
  639. TEGRA124_FUNCTION(pcie),
  640. TEGRA124_FUNCTION(usb3),
  641. TEGRA124_FUNCTION(sata),
  642. TEGRA124_FUNCTION(rsvd),
  643. };
  644. enum tegra124_function {
  645. TEGRA124_FUNC_SNPS,
  646. TEGRA124_FUNC_XUSB,
  647. TEGRA124_FUNC_UART,
  648. TEGRA124_FUNC_PCIE,
  649. TEGRA124_FUNC_USB3,
  650. TEGRA124_FUNC_SATA,
  651. TEGRA124_FUNC_RSVD,
  652. };
  653. static const unsigned int tegra124_otg_functions[] = {
  654. TEGRA124_FUNC_SNPS,
  655. TEGRA124_FUNC_XUSB,
  656. TEGRA124_FUNC_UART,
  657. TEGRA124_FUNC_RSVD,
  658. };
  659. static const unsigned int tegra124_usb_functions[] = {
  660. TEGRA124_FUNC_SNPS,
  661. TEGRA124_FUNC_XUSB,
  662. };
  663. static const unsigned int tegra124_pci_functions[] = {
  664. TEGRA124_FUNC_PCIE,
  665. TEGRA124_FUNC_USB3,
  666. TEGRA124_FUNC_SATA,
  667. TEGRA124_FUNC_RSVD,
  668. };
  669. #define TEGRA124_LANE(_name, _offset, _shift, _mask, _iddq, _funcs) \
  670. { \
  671. .name = _name, \
  672. .offset = _offset, \
  673. .shift = _shift, \
  674. .mask = _mask, \
  675. .iddq = _iddq, \
  676. .num_funcs = ARRAY_SIZE(tegra124_##_funcs##_functions), \
  677. .funcs = tegra124_##_funcs##_functions, \
  678. }
  679. static const struct tegra_xusb_padctl_lane tegra124_lanes[] = {
  680. TEGRA124_LANE("otg-0", 0x004, 0, 0x3, 0, otg),
  681. TEGRA124_LANE("otg-1", 0x004, 2, 0x3, 0, otg),
  682. TEGRA124_LANE("otg-2", 0x004, 4, 0x3, 0, otg),
  683. TEGRA124_LANE("ulpi-0", 0x004, 12, 0x1, 0, usb),
  684. TEGRA124_LANE("hsic-0", 0x004, 14, 0x1, 0, usb),
  685. TEGRA124_LANE("hsic-1", 0x004, 15, 0x1, 0, usb),
  686. TEGRA124_LANE("pcie-0", 0x134, 16, 0x3, 1, pci),
  687. TEGRA124_LANE("pcie-1", 0x134, 18, 0x3, 2, pci),
  688. TEGRA124_LANE("pcie-2", 0x134, 20, 0x3, 3, pci),
  689. TEGRA124_LANE("pcie-3", 0x134, 22, 0x3, 4, pci),
  690. TEGRA124_LANE("pcie-4", 0x134, 24, 0x3, 5, pci),
  691. TEGRA124_LANE("sata-0", 0x134, 26, 0x3, 6, pci),
  692. };
  693. static const struct tegra_xusb_padctl_soc tegra124_soc = {
  694. .num_pins = ARRAY_SIZE(tegra124_pins),
  695. .pins = tegra124_pins,
  696. .num_functions = ARRAY_SIZE(tegra124_functions),
  697. .functions = tegra124_functions,
  698. .num_lanes = ARRAY_SIZE(tegra124_lanes),
  699. .lanes = tegra124_lanes,
  700. };
  701. static const struct of_device_id tegra_xusb_padctl_of_match[] = {
  702. { .compatible = "nvidia,tegra124-xusb-padctl", .data = &tegra124_soc },
  703. { }
  704. };
  705. MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
  706. int tegra_xusb_padctl_legacy_probe(struct platform_device *pdev)
  707. {
  708. struct tegra_xusb_padctl *padctl;
  709. const struct of_device_id *match;
  710. struct resource *res;
  711. struct phy *phy;
  712. int err;
  713. padctl = devm_kzalloc(&pdev->dev, sizeof(*padctl), GFP_KERNEL);
  714. if (!padctl)
  715. return -ENOMEM;
  716. platform_set_drvdata(pdev, padctl);
  717. mutex_init(&padctl->lock);
  718. padctl->dev = &pdev->dev;
  719. match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
  720. padctl->soc = match->data;
  721. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  722. padctl->regs = devm_ioremap_resource(&pdev->dev, res);
  723. if (IS_ERR(padctl->regs))
  724. return PTR_ERR(padctl->regs);
  725. padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
  726. if (IS_ERR(padctl->rst))
  727. return PTR_ERR(padctl->rst);
  728. err = reset_control_deassert(padctl->rst);
  729. if (err < 0)
  730. return err;
  731. memset(&padctl->desc, 0, sizeof(padctl->desc));
  732. padctl->desc.name = dev_name(padctl->dev);
  733. padctl->desc.pins = tegra124_pins;
  734. padctl->desc.npins = ARRAY_SIZE(tegra124_pins);
  735. padctl->desc.pctlops = &tegra_xusb_padctl_pinctrl_ops;
  736. padctl->desc.pmxops = &tegra_xusb_padctl_pinmux_ops;
  737. padctl->desc.confops = &tegra_xusb_padctl_pinconf_ops;
  738. padctl->desc.owner = THIS_MODULE;
  739. padctl->pinctrl = devm_pinctrl_register(&pdev->dev, &padctl->desc,
  740. padctl);
  741. if (IS_ERR(padctl->pinctrl)) {
  742. dev_err(&pdev->dev, "failed to register pincontrol\n");
  743. err = PTR_ERR(padctl->pinctrl);
  744. goto reset;
  745. }
  746. phy = devm_phy_create(&pdev->dev, NULL, &pcie_phy_ops);
  747. if (IS_ERR(phy)) {
  748. err = PTR_ERR(phy);
  749. goto reset;
  750. }
  751. padctl->phys[TEGRA_XUSB_PADCTL_PCIE] = phy;
  752. phy_set_drvdata(phy, padctl);
  753. phy = devm_phy_create(&pdev->dev, NULL, &sata_phy_ops);
  754. if (IS_ERR(phy)) {
  755. err = PTR_ERR(phy);
  756. goto reset;
  757. }
  758. padctl->phys[TEGRA_XUSB_PADCTL_SATA] = phy;
  759. phy_set_drvdata(phy, padctl);
  760. padctl->provider = devm_of_phy_provider_register(&pdev->dev,
  761. tegra_xusb_padctl_xlate);
  762. if (IS_ERR(padctl->provider)) {
  763. err = PTR_ERR(padctl->provider);
  764. dev_err(&pdev->dev, "failed to register PHYs: %d\n", err);
  765. goto reset;
  766. }
  767. return 0;
  768. reset:
  769. reset_control_assert(padctl->rst);
  770. return err;
  771. }
  772. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_legacy_probe);
  773. int tegra_xusb_padctl_legacy_remove(struct platform_device *pdev)
  774. {
  775. struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
  776. int err;
  777. err = reset_control_assert(padctl->rst);
  778. if (err < 0)
  779. dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
  780. return err;
  781. }
  782. EXPORT_SYMBOL_GPL(tegra_xusb_padctl_legacy_remove);