pinctrl-tegra-xusb.c 24 KB

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