pinctrl-tegra-xusb.c 24 KB

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