pinctrl-tegra-xusb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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. return err;
  209. }
  210. return 0;
  211. }
  212. static const struct pinctrl_ops tegra_xusb_padctl_pinctrl_ops = {
  213. .get_groups_count = tegra_xusb_padctl_get_groups_count,
  214. .get_group_name = tegra_xusb_padctl_get_group_name,
  215. .get_group_pins = tegra_xusb_padctl_get_group_pins,
  216. .dt_node_to_map = tegra_xusb_padctl_dt_node_to_map,
  217. .dt_free_map = pinctrl_utils_dt_free_map,
  218. };
  219. static int tegra_xusb_padctl_get_functions_count(struct pinctrl_dev *pinctrl)
  220. {
  221. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  222. return padctl->soc->num_functions;
  223. }
  224. static const char *
  225. tegra_xusb_padctl_get_function_name(struct pinctrl_dev *pinctrl,
  226. unsigned int function)
  227. {
  228. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  229. return padctl->soc->functions[function].name;
  230. }
  231. static int tegra_xusb_padctl_get_function_groups(struct pinctrl_dev *pinctrl,
  232. unsigned int function,
  233. const char * const **groups,
  234. unsigned * const num_groups)
  235. {
  236. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  237. *num_groups = padctl->soc->functions[function].num_groups;
  238. *groups = padctl->soc->functions[function].groups;
  239. return 0;
  240. }
  241. static int tegra_xusb_padctl_pinmux_set(struct pinctrl_dev *pinctrl,
  242. unsigned int function,
  243. unsigned int group)
  244. {
  245. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  246. const struct tegra_xusb_padctl_lane *lane;
  247. unsigned int i;
  248. u32 value;
  249. lane = &padctl->soc->lanes[group];
  250. for (i = 0; i < lane->num_funcs; i++)
  251. if (lane->funcs[i] == function)
  252. break;
  253. if (i >= lane->num_funcs)
  254. return -EINVAL;
  255. value = padctl_readl(padctl, lane->offset);
  256. value &= ~(lane->mask << lane->shift);
  257. value |= i << lane->shift;
  258. padctl_writel(padctl, value, lane->offset);
  259. return 0;
  260. }
  261. static const struct pinmux_ops tegra_xusb_padctl_pinmux_ops = {
  262. .get_functions_count = tegra_xusb_padctl_get_functions_count,
  263. .get_function_name = tegra_xusb_padctl_get_function_name,
  264. .get_function_groups = tegra_xusb_padctl_get_function_groups,
  265. .set_mux = tegra_xusb_padctl_pinmux_set,
  266. };
  267. static int tegra_xusb_padctl_pinconf_group_get(struct pinctrl_dev *pinctrl,
  268. unsigned int group,
  269. unsigned long *config)
  270. {
  271. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  272. const struct tegra_xusb_padctl_lane *lane;
  273. enum tegra_xusb_padctl_param param;
  274. u32 value;
  275. param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(*config);
  276. lane = &padctl->soc->lanes[group];
  277. switch (param) {
  278. case TEGRA_XUSB_PADCTL_IDDQ:
  279. /* lanes with iddq == 0 don't support this parameter */
  280. if (lane->iddq == 0)
  281. return -EINVAL;
  282. value = padctl_readl(padctl, lane->offset);
  283. if (value & BIT(lane->iddq))
  284. value = 0;
  285. else
  286. value = 1;
  287. *config = TEGRA_XUSB_PADCTL_PACK(param, value);
  288. break;
  289. default:
  290. dev_err(padctl->dev, "invalid configuration parameter: %04x\n",
  291. param);
  292. return -ENOTSUPP;
  293. }
  294. return 0;
  295. }
  296. static int tegra_xusb_padctl_pinconf_group_set(struct pinctrl_dev *pinctrl,
  297. unsigned int group,
  298. unsigned long *configs,
  299. unsigned int num_configs)
  300. {
  301. struct tegra_xusb_padctl *padctl = pinctrl_dev_get_drvdata(pinctrl);
  302. const struct tegra_xusb_padctl_lane *lane;
  303. enum tegra_xusb_padctl_param param;
  304. unsigned long value;
  305. unsigned int i;
  306. u32 regval;
  307. lane = &padctl->soc->lanes[group];
  308. for (i = 0; i < num_configs; i++) {
  309. param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(configs[i]);
  310. value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(configs[i]);
  311. switch (param) {
  312. case TEGRA_XUSB_PADCTL_IDDQ:
  313. /* lanes with iddq == 0 don't support this parameter */
  314. if (lane->iddq == 0)
  315. return -EINVAL;
  316. regval = padctl_readl(padctl, lane->offset);
  317. if (value)
  318. regval &= ~BIT(lane->iddq);
  319. else
  320. regval |= BIT(lane->iddq);
  321. padctl_writel(padctl, regval, lane->offset);
  322. break;
  323. default:
  324. dev_err(padctl->dev,
  325. "invalid configuration parameter: %04x\n",
  326. param);
  327. return -ENOTSUPP;
  328. }
  329. }
  330. return 0;
  331. }
  332. #ifdef CONFIG_DEBUG_FS
  333. static const char *strip_prefix(const char *s)
  334. {
  335. const char *comma = strchr(s, ',');
  336. if (!comma)
  337. return s;
  338. return comma + 1;
  339. }
  340. static void
  341. tegra_xusb_padctl_pinconf_group_dbg_show(struct pinctrl_dev *pinctrl,
  342. struct seq_file *s,
  343. unsigned int group)
  344. {
  345. unsigned int i;
  346. for (i = 0; i < ARRAY_SIZE(properties); i++) {
  347. unsigned long config, value;
  348. int err;
  349. config = TEGRA_XUSB_PADCTL_PACK(properties[i].param, 0);
  350. err = tegra_xusb_padctl_pinconf_group_get(pinctrl, group,
  351. &config);
  352. if (err < 0)
  353. continue;
  354. value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
  355. seq_printf(s, "\n\t%s=%lu\n", strip_prefix(properties[i].name),
  356. value);
  357. }
  358. }
  359. static void
  360. tegra_xusb_padctl_pinconf_config_dbg_show(struct pinctrl_dev *pinctrl,
  361. struct seq_file *s,
  362. unsigned long config)
  363. {
  364. enum tegra_xusb_padctl_param param;
  365. const char *name = "unknown";
  366. unsigned long value;
  367. unsigned int i;
  368. param = TEGRA_XUSB_PADCTL_UNPACK_PARAM(config);
  369. value = TEGRA_XUSB_PADCTL_UNPACK_VALUE(config);
  370. for (i = 0; i < ARRAY_SIZE(properties); i++) {
  371. if (properties[i].param == param) {
  372. name = properties[i].name;
  373. break;
  374. }
  375. }
  376. seq_printf(s, "%s=%lu", strip_prefix(name), value);
  377. }
  378. #endif
  379. static const struct pinconf_ops tegra_xusb_padctl_pinconf_ops = {
  380. .pin_config_group_get = tegra_xusb_padctl_pinconf_group_get,
  381. .pin_config_group_set = tegra_xusb_padctl_pinconf_group_set,
  382. #ifdef CONFIG_DEBUG_FS
  383. .pin_config_group_dbg_show = tegra_xusb_padctl_pinconf_group_dbg_show,
  384. .pin_config_config_dbg_show = tegra_xusb_padctl_pinconf_config_dbg_show,
  385. #endif
  386. };
  387. static int tegra_xusb_padctl_enable(struct tegra_xusb_padctl *padctl)
  388. {
  389. u32 value;
  390. mutex_lock(&padctl->lock);
  391. if (padctl->enable++ > 0)
  392. goto out;
  393. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  394. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
  395. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  396. usleep_range(100, 200);
  397. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  398. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
  399. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  400. usleep_range(100, 200);
  401. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  402. value &= ~XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
  403. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  404. out:
  405. mutex_unlock(&padctl->lock);
  406. return 0;
  407. }
  408. static int tegra_xusb_padctl_disable(struct tegra_xusb_padctl *padctl)
  409. {
  410. u32 value;
  411. mutex_lock(&padctl->lock);
  412. if (WARN_ON(padctl->enable == 0))
  413. goto out;
  414. if (--padctl->enable > 0)
  415. goto out;
  416. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  417. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_VCORE_DOWN;
  418. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  419. usleep_range(100, 200);
  420. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  421. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN_EARLY;
  422. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  423. usleep_range(100, 200);
  424. value = padctl_readl(padctl, XUSB_PADCTL_ELPG_PROGRAM);
  425. value |= XUSB_PADCTL_ELPG_PROGRAM_AUX_MUX_LP0_CLAMP_EN;
  426. padctl_writel(padctl, value, XUSB_PADCTL_ELPG_PROGRAM);
  427. out:
  428. mutex_unlock(&padctl->lock);
  429. return 0;
  430. }
  431. static int tegra_xusb_phy_init(struct phy *phy)
  432. {
  433. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  434. return tegra_xusb_padctl_enable(padctl);
  435. }
  436. static int tegra_xusb_phy_exit(struct phy *phy)
  437. {
  438. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  439. return tegra_xusb_padctl_disable(padctl);
  440. }
  441. static int pcie_phy_power_on(struct phy *phy)
  442. {
  443. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  444. unsigned long timeout;
  445. int err = -ETIMEDOUT;
  446. u32 value;
  447. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  448. value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_REFCLK_SEL_MASK;
  449. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  450. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
  451. value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL2_REFCLKBUF_EN |
  452. XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_EN |
  453. XUSB_PADCTL_IOPHY_PLL_P0_CTL2_TXCLKREF_SEL;
  454. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL2);
  455. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  456. value |= XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
  457. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  458. timeout = jiffies + msecs_to_jiffies(50);
  459. while (time_before(jiffies, timeout)) {
  460. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  461. if (value & XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL0_LOCKDET) {
  462. err = 0;
  463. break;
  464. }
  465. usleep_range(100, 200);
  466. }
  467. return err;
  468. }
  469. static int pcie_phy_power_off(struct phy *phy)
  470. {
  471. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  472. u32 value;
  473. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  474. value &= ~XUSB_PADCTL_IOPHY_PLL_P0_CTL1_PLL_RST;
  475. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_P0_CTL1);
  476. return 0;
  477. }
  478. static const struct phy_ops pcie_phy_ops = {
  479. .init = tegra_xusb_phy_init,
  480. .exit = tegra_xusb_phy_exit,
  481. .power_on = pcie_phy_power_on,
  482. .power_off = pcie_phy_power_off,
  483. .owner = THIS_MODULE,
  484. };
  485. static int sata_phy_power_on(struct phy *phy)
  486. {
  487. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  488. unsigned long timeout;
  489. int err = -ETIMEDOUT;
  490. u32 value;
  491. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  492. value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
  493. value &= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
  494. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  495. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  496. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
  497. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
  498. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  499. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  500. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
  501. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  502. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  503. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
  504. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  505. timeout = jiffies + msecs_to_jiffies(50);
  506. while (time_before(jiffies, timeout)) {
  507. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  508. if (value & XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_LOCKDET) {
  509. err = 0;
  510. break;
  511. }
  512. usleep_range(100, 200);
  513. }
  514. return err;
  515. }
  516. static int sata_phy_power_off(struct phy *phy)
  517. {
  518. struct tegra_xusb_padctl *padctl = phy_get_drvdata(phy);
  519. u32 value;
  520. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  521. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_RST;
  522. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  523. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  524. value &= ~XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL1_MODE;
  525. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  526. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  527. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_PWR_OVRD;
  528. value |= XUSB_PADCTL_IOPHY_PLL_S0_CTL1_PLL_IDDQ;
  529. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_PLL_S0_CTL1);
  530. value = padctl_readl(padctl, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  531. value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ_OVRD;
  532. value |= ~XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1_IDDQ;
  533. padctl_writel(padctl, value, XUSB_PADCTL_IOPHY_MISC_PAD_S0_CTL1);
  534. return 0;
  535. }
  536. static const struct phy_ops sata_phy_ops = {
  537. .init = tegra_xusb_phy_init,
  538. .exit = tegra_xusb_phy_exit,
  539. .power_on = sata_phy_power_on,
  540. .power_off = sata_phy_power_off,
  541. .owner = THIS_MODULE,
  542. };
  543. static struct phy *tegra_xusb_padctl_xlate(struct device *dev,
  544. struct of_phandle_args *args)
  545. {
  546. struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
  547. unsigned int index = args->args[0];
  548. if (args->args_count <= 0)
  549. return ERR_PTR(-EINVAL);
  550. if (index >= ARRAY_SIZE(padctl->phys))
  551. return ERR_PTR(-EINVAL);
  552. return padctl->phys[index];
  553. }
  554. #define PIN_OTG_0 0
  555. #define PIN_OTG_1 1
  556. #define PIN_OTG_2 2
  557. #define PIN_ULPI_0 3
  558. #define PIN_HSIC_0 4
  559. #define PIN_HSIC_1 5
  560. #define PIN_PCIE_0 6
  561. #define PIN_PCIE_1 7
  562. #define PIN_PCIE_2 8
  563. #define PIN_PCIE_3 9
  564. #define PIN_PCIE_4 10
  565. #define PIN_SATA_0 11
  566. static const struct pinctrl_pin_desc tegra124_pins[] = {
  567. PINCTRL_PIN(PIN_OTG_0, "otg-0"),
  568. PINCTRL_PIN(PIN_OTG_1, "otg-1"),
  569. PINCTRL_PIN(PIN_OTG_2, "otg-2"),
  570. PINCTRL_PIN(PIN_ULPI_0, "ulpi-0"),
  571. PINCTRL_PIN(PIN_HSIC_0, "hsic-0"),
  572. PINCTRL_PIN(PIN_HSIC_1, "hsic-1"),
  573. PINCTRL_PIN(PIN_PCIE_0, "pcie-0"),
  574. PINCTRL_PIN(PIN_PCIE_1, "pcie-1"),
  575. PINCTRL_PIN(PIN_PCIE_2, "pcie-2"),
  576. PINCTRL_PIN(PIN_PCIE_3, "pcie-3"),
  577. PINCTRL_PIN(PIN_PCIE_4, "pcie-4"),
  578. PINCTRL_PIN(PIN_SATA_0, "sata-0"),
  579. };
  580. static const char * const tegra124_snps_groups[] = {
  581. "otg-0",
  582. "otg-1",
  583. "otg-2",
  584. "ulpi-0",
  585. "hsic-0",
  586. "hsic-1",
  587. };
  588. static const char * const tegra124_xusb_groups[] = {
  589. "otg-0",
  590. "otg-1",
  591. "otg-2",
  592. "ulpi-0",
  593. "hsic-0",
  594. "hsic-1",
  595. };
  596. static const char * const tegra124_uart_groups[] = {
  597. "otg-0",
  598. "otg-1",
  599. "otg-2",
  600. };
  601. static const char * const tegra124_pcie_groups[] = {
  602. "pcie-0",
  603. "pcie-1",
  604. "pcie-2",
  605. "pcie-3",
  606. "pcie-4",
  607. "sata-0",
  608. };
  609. static const char * const tegra124_usb3_groups[] = {
  610. "pcie-0",
  611. "pcie-1",
  612. "pcie-2",
  613. "pcie-3",
  614. "pcie-4",
  615. "sata-0",
  616. };
  617. static const char * const tegra124_sata_groups[] = {
  618. "pcie-0",
  619. "pcie-1",
  620. "pcie-2",
  621. "pcie-3",
  622. "pcie-4",
  623. "sata-0",
  624. };
  625. static const char * const tegra124_rsvd_groups[] = {
  626. "otg-0",
  627. "otg-1",
  628. "otg-2",
  629. "pcie-0",
  630. "pcie-1",
  631. "pcie-2",
  632. "pcie-3",
  633. "pcie-4",
  634. "sata-0",
  635. };
  636. #define TEGRA124_FUNCTION(_name) \
  637. { \
  638. .name = #_name, \
  639. .num_groups = ARRAY_SIZE(tegra124_##_name##_groups), \
  640. .groups = tegra124_##_name##_groups, \
  641. }
  642. static struct tegra_xusb_padctl_function tegra124_functions[] = {
  643. TEGRA124_FUNCTION(snps),
  644. TEGRA124_FUNCTION(xusb),
  645. TEGRA124_FUNCTION(uart),
  646. TEGRA124_FUNCTION(pcie),
  647. TEGRA124_FUNCTION(usb3),
  648. TEGRA124_FUNCTION(sata),
  649. TEGRA124_FUNCTION(rsvd),
  650. };
  651. enum tegra124_function {
  652. TEGRA124_FUNC_SNPS,
  653. TEGRA124_FUNC_XUSB,
  654. TEGRA124_FUNC_UART,
  655. TEGRA124_FUNC_PCIE,
  656. TEGRA124_FUNC_USB3,
  657. TEGRA124_FUNC_SATA,
  658. TEGRA124_FUNC_RSVD,
  659. };
  660. static const unsigned int tegra124_otg_functions[] = {
  661. TEGRA124_FUNC_SNPS,
  662. TEGRA124_FUNC_XUSB,
  663. TEGRA124_FUNC_UART,
  664. TEGRA124_FUNC_RSVD,
  665. };
  666. static const unsigned int tegra124_usb_functions[] = {
  667. TEGRA124_FUNC_SNPS,
  668. TEGRA124_FUNC_XUSB,
  669. };
  670. static const unsigned int tegra124_pci_functions[] = {
  671. TEGRA124_FUNC_PCIE,
  672. TEGRA124_FUNC_USB3,
  673. TEGRA124_FUNC_SATA,
  674. TEGRA124_FUNC_RSVD,
  675. };
  676. #define TEGRA124_LANE(_name, _offset, _shift, _mask, _iddq, _funcs) \
  677. { \
  678. .name = _name, \
  679. .offset = _offset, \
  680. .shift = _shift, \
  681. .mask = _mask, \
  682. .iddq = _iddq, \
  683. .num_funcs = ARRAY_SIZE(tegra124_##_funcs##_functions), \
  684. .funcs = tegra124_##_funcs##_functions, \
  685. }
  686. static const struct tegra_xusb_padctl_lane tegra124_lanes[] = {
  687. TEGRA124_LANE("otg-0", 0x004, 0, 0x3, 0, otg),
  688. TEGRA124_LANE("otg-1", 0x004, 2, 0x3, 0, otg),
  689. TEGRA124_LANE("otg-2", 0x004, 4, 0x3, 0, otg),
  690. TEGRA124_LANE("ulpi-0", 0x004, 12, 0x1, 0, usb),
  691. TEGRA124_LANE("hsic-0", 0x004, 14, 0x1, 0, usb),
  692. TEGRA124_LANE("hsic-1", 0x004, 15, 0x1, 0, usb),
  693. TEGRA124_LANE("pcie-0", 0x134, 16, 0x3, 1, pci),
  694. TEGRA124_LANE("pcie-1", 0x134, 18, 0x3, 2, pci),
  695. TEGRA124_LANE("pcie-2", 0x134, 20, 0x3, 3, pci),
  696. TEGRA124_LANE("pcie-3", 0x134, 22, 0x3, 4, pci),
  697. TEGRA124_LANE("pcie-4", 0x134, 24, 0x3, 5, pci),
  698. TEGRA124_LANE("sata-0", 0x134, 26, 0x3, 6, pci),
  699. };
  700. static const struct tegra_xusb_padctl_soc tegra124_soc = {
  701. .num_pins = ARRAY_SIZE(tegra124_pins),
  702. .pins = tegra124_pins,
  703. .num_functions = ARRAY_SIZE(tegra124_functions),
  704. .functions = tegra124_functions,
  705. .num_lanes = ARRAY_SIZE(tegra124_lanes),
  706. .lanes = tegra124_lanes,
  707. };
  708. static const struct of_device_id tegra_xusb_padctl_of_match[] = {
  709. { .compatible = "nvidia,tegra124-xusb-padctl", .data = &tegra124_soc },
  710. { }
  711. };
  712. MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
  713. static int tegra_xusb_padctl_probe(struct platform_device *pdev)
  714. {
  715. struct tegra_xusb_padctl *padctl;
  716. const struct of_device_id *match;
  717. struct resource *res;
  718. struct phy *phy;
  719. int err;
  720. padctl = devm_kzalloc(&pdev->dev, sizeof(*padctl), GFP_KERNEL);
  721. if (!padctl)
  722. return -ENOMEM;
  723. platform_set_drvdata(pdev, padctl);
  724. mutex_init(&padctl->lock);
  725. padctl->dev = &pdev->dev;
  726. match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
  727. padctl->soc = match->data;
  728. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  729. padctl->regs = devm_ioremap_resource(&pdev->dev, res);
  730. if (IS_ERR(padctl->regs))
  731. return PTR_ERR(padctl->regs);
  732. padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
  733. if (IS_ERR(padctl->rst))
  734. return PTR_ERR(padctl->rst);
  735. err = reset_control_deassert(padctl->rst);
  736. if (err < 0)
  737. return err;
  738. memset(&padctl->desc, 0, sizeof(padctl->desc));
  739. padctl->desc.name = dev_name(padctl->dev);
  740. padctl->desc.pins = tegra124_pins;
  741. padctl->desc.npins = ARRAY_SIZE(tegra124_pins);
  742. padctl->desc.pctlops = &tegra_xusb_padctl_pinctrl_ops;
  743. padctl->desc.pmxops = &tegra_xusb_padctl_pinmux_ops;
  744. padctl->desc.confops = &tegra_xusb_padctl_pinconf_ops;
  745. padctl->desc.owner = THIS_MODULE;
  746. padctl->pinctrl = pinctrl_register(&padctl->desc, &pdev->dev, padctl);
  747. if (IS_ERR(padctl->pinctrl)) {
  748. dev_err(&pdev->dev, "failed to register pincontrol\n");
  749. err = PTR_ERR(padctl->pinctrl);
  750. goto reset;
  751. }
  752. phy = devm_phy_create(&pdev->dev, NULL, &pcie_phy_ops);
  753. if (IS_ERR(phy)) {
  754. err = PTR_ERR(phy);
  755. goto unregister;
  756. }
  757. padctl->phys[TEGRA_XUSB_PADCTL_PCIE] = phy;
  758. phy_set_drvdata(phy, padctl);
  759. phy = devm_phy_create(&pdev->dev, NULL, &sata_phy_ops);
  760. if (IS_ERR(phy)) {
  761. err = PTR_ERR(phy);
  762. goto unregister;
  763. }
  764. padctl->phys[TEGRA_XUSB_PADCTL_SATA] = phy;
  765. phy_set_drvdata(phy, padctl);
  766. padctl->provider = devm_of_phy_provider_register(&pdev->dev,
  767. tegra_xusb_padctl_xlate);
  768. if (IS_ERR(padctl->provider)) {
  769. err = PTR_ERR(padctl->provider);
  770. dev_err(&pdev->dev, "failed to register PHYs: %d\n", err);
  771. goto unregister;
  772. }
  773. return 0;
  774. unregister:
  775. pinctrl_unregister(padctl->pinctrl);
  776. reset:
  777. reset_control_assert(padctl->rst);
  778. return err;
  779. }
  780. static int tegra_xusb_padctl_remove(struct platform_device *pdev)
  781. {
  782. struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
  783. int err;
  784. pinctrl_unregister(padctl->pinctrl);
  785. err = reset_control_assert(padctl->rst);
  786. if (err < 0)
  787. dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
  788. return err;
  789. }
  790. static struct platform_driver tegra_xusb_padctl_driver = {
  791. .driver = {
  792. .name = "tegra-xusb-padctl",
  793. .of_match_table = tegra_xusb_padctl_of_match,
  794. },
  795. .probe = tegra_xusb_padctl_probe,
  796. .remove = tegra_xusb_padctl_remove,
  797. };
  798. module_platform_driver(tegra_xusb_padctl_driver);
  799. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  800. MODULE_DESCRIPTION("Tegra 124 XUSB Pad Control driver");
  801. MODULE_LICENSE("GPL v2");