pinctrl-tegra-xusb.c 24 KB

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