phy-tegra-usb.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010 Google, Inc.
  4. * Copyright (C) 2013 NVIDIA Corporation
  5. *
  6. * Author:
  7. * Erik Gilling <[email protected]>
  8. * Benoit Goby <[email protected]>
  9. * Venu Byravarasu <[email protected]>
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/export.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/iopoll.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/resource.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/usb/ehci_def.h>
  25. #include <linux/usb/of.h>
  26. #include <linux/usb/tegra_usb_phy.h>
  27. #include <linux/usb/ulpi.h>
  28. #define ULPI_VIEWPORT 0x170
  29. /* PORTSC PTS/PHCD bits, Tegra20 only */
  30. #define TEGRA_USB_PORTSC1 0x184
  31. #define TEGRA_USB_PORTSC1_PTS(x) (((x) & 0x3) << 30)
  32. #define TEGRA_USB_PORTSC1_PHCD BIT(23)
  33. /* HOSTPC1 PTS/PHCD bits, Tegra30 and above */
  34. #define TEGRA_USB_HOSTPC1_DEVLC 0x1b4
  35. #define TEGRA_USB_HOSTPC1_DEVLC_PTS(x) (((x) & 0x7) << 29)
  36. #define TEGRA_USB_HOSTPC1_DEVLC_PHCD BIT(22)
  37. /* Bits of PORTSC1, which will get cleared by writing 1 into them */
  38. #define TEGRA_PORTSC1_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC)
  39. #define USB_SUSP_CTRL 0x400
  40. #define USB_WAKE_ON_RESUME_EN BIT(2)
  41. #define USB_WAKE_ON_CNNT_EN_DEV BIT(3)
  42. #define USB_WAKE_ON_DISCON_EN_DEV BIT(4)
  43. #define USB_SUSP_CLR BIT(5)
  44. #define USB_PHY_CLK_VALID BIT(7)
  45. #define UTMIP_RESET BIT(11)
  46. #define UHSIC_RESET BIT(11)
  47. #define UTMIP_PHY_ENABLE BIT(12)
  48. #define ULPI_PHY_ENABLE BIT(13)
  49. #define USB_SUSP_SET BIT(14)
  50. #define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
  51. #define USB_PHY_VBUS_SENSORS 0x404
  52. #define B_SESS_VLD_WAKEUP_EN BIT(14)
  53. #define A_SESS_VLD_WAKEUP_EN BIT(22)
  54. #define A_VBUS_VLD_WAKEUP_EN BIT(30)
  55. #define USB_PHY_VBUS_WAKEUP_ID 0x408
  56. #define ID_INT_EN BIT(0)
  57. #define ID_CHG_DET BIT(1)
  58. #define VBUS_WAKEUP_INT_EN BIT(8)
  59. #define VBUS_WAKEUP_CHG_DET BIT(9)
  60. #define VBUS_WAKEUP_STS BIT(10)
  61. #define VBUS_WAKEUP_WAKEUP_EN BIT(30)
  62. #define USB1_LEGACY_CTRL 0x410
  63. #define USB1_NO_LEGACY_MODE BIT(0)
  64. #define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
  65. #define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
  66. #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
  67. (1 << 1)
  68. #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
  69. #define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
  70. #define ULPI_TIMING_CTRL_0 0x424
  71. #define ULPI_OUTPUT_PINMUX_BYP BIT(10)
  72. #define ULPI_CLKOUT_PINMUX_BYP BIT(11)
  73. #define ULPI_TIMING_CTRL_1 0x428
  74. #define ULPI_DATA_TRIMMER_LOAD BIT(0)
  75. #define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
  76. #define ULPI_STPDIRNXT_TRIMMER_LOAD BIT(16)
  77. #define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
  78. #define ULPI_DIR_TRIMMER_LOAD BIT(24)
  79. #define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
  80. #define UTMIP_PLL_CFG1 0x804
  81. #define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
  82. #define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
  83. #define UTMIP_XCVR_CFG0 0x808
  84. #define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
  85. #define UTMIP_XCVR_SETUP_MSB(x) ((((x) & 0x70) >> 4) << 22)
  86. #define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
  87. #define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
  88. #define UTMIP_FORCE_PD_POWERDOWN BIT(14)
  89. #define UTMIP_FORCE_PD2_POWERDOWN BIT(16)
  90. #define UTMIP_FORCE_PDZI_POWERDOWN BIT(18)
  91. #define UTMIP_XCVR_LSBIAS_SEL BIT(21)
  92. #define UTMIP_XCVR_HSSLEW(x) (((x) & 0x3) << 4)
  93. #define UTMIP_XCVR_HSSLEW_MSB(x) ((((x) & 0x1fc) >> 2) << 25)
  94. #define UTMIP_BIAS_CFG0 0x80c
  95. #define UTMIP_OTGPD BIT(11)
  96. #define UTMIP_BIASPD BIT(10)
  97. #define UTMIP_HSSQUELCH_LEVEL(x) (((x) & 0x3) << 0)
  98. #define UTMIP_HSDISCON_LEVEL(x) (((x) & 0x3) << 2)
  99. #define UTMIP_HSDISCON_LEVEL_MSB(x) ((((x) & 0x4) >> 2) << 24)
  100. #define UTMIP_HSRX_CFG0 0x810
  101. #define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
  102. #define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
  103. #define UTMIP_HSRX_CFG1 0x814
  104. #define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
  105. #define UTMIP_TX_CFG0 0x820
  106. #define UTMIP_FS_PREABMLE_J BIT(19)
  107. #define UTMIP_HS_DISCON_DISABLE BIT(8)
  108. #define UTMIP_MISC_CFG0 0x824
  109. #define UTMIP_DPDM_OBSERVE BIT(26)
  110. #define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
  111. #define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
  112. #define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
  113. #define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
  114. #define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
  115. #define UTMIP_SUSPEND_EXIT_ON_EDGE BIT(22)
  116. #define UTMIP_MISC_CFG1 0x828
  117. #define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
  118. #define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
  119. #define UTMIP_DEBOUNCE_CFG0 0x82c
  120. #define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
  121. #define UTMIP_BAT_CHRG_CFG0 0x830
  122. #define UTMIP_PD_CHRG BIT(0)
  123. #define UTMIP_SPARE_CFG0 0x834
  124. #define FUSE_SETUP_SEL BIT(3)
  125. #define UTMIP_XCVR_CFG1 0x838
  126. #define UTMIP_FORCE_PDDISC_POWERDOWN BIT(0)
  127. #define UTMIP_FORCE_PDCHRP_POWERDOWN BIT(2)
  128. #define UTMIP_FORCE_PDDR_POWERDOWN BIT(4)
  129. #define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
  130. #define UTMIP_BIAS_CFG1 0x83c
  131. #define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
  132. /* For Tegra30 and above only, the address is different in Tegra20 */
  133. #define USB_USBMODE 0x1f8
  134. #define USB_USBMODE_MASK (3 << 0)
  135. #define USB_USBMODE_HOST (3 << 0)
  136. #define USB_USBMODE_DEVICE (2 << 0)
  137. #define PMC_USB_AO 0xf0
  138. #define VBUS_WAKEUP_PD_P0 BIT(2)
  139. #define ID_PD_P0 BIT(3)
  140. static DEFINE_SPINLOCK(utmip_pad_lock);
  141. static unsigned int utmip_pad_count;
  142. struct tegra_xtal_freq {
  143. unsigned int freq;
  144. u8 enable_delay;
  145. u8 stable_count;
  146. u8 active_delay;
  147. u8 xtal_freq_count;
  148. u16 debounce;
  149. };
  150. static const struct tegra_xtal_freq tegra_freq_table[] = {
  151. {
  152. .freq = 12000000,
  153. .enable_delay = 0x02,
  154. .stable_count = 0x2F,
  155. .active_delay = 0x04,
  156. .xtal_freq_count = 0x76,
  157. .debounce = 0x7530,
  158. },
  159. {
  160. .freq = 13000000,
  161. .enable_delay = 0x02,
  162. .stable_count = 0x33,
  163. .active_delay = 0x05,
  164. .xtal_freq_count = 0x7F,
  165. .debounce = 0x7EF4,
  166. },
  167. {
  168. .freq = 19200000,
  169. .enable_delay = 0x03,
  170. .stable_count = 0x4B,
  171. .active_delay = 0x06,
  172. .xtal_freq_count = 0xBB,
  173. .debounce = 0xBB80,
  174. },
  175. {
  176. .freq = 26000000,
  177. .enable_delay = 0x04,
  178. .stable_count = 0x66,
  179. .active_delay = 0x09,
  180. .xtal_freq_count = 0xFE,
  181. .debounce = 0xFDE8,
  182. },
  183. };
  184. static inline struct tegra_usb_phy *to_tegra_usb_phy(struct usb_phy *u_phy)
  185. {
  186. return container_of(u_phy, struct tegra_usb_phy, u_phy);
  187. }
  188. static void set_pts(struct tegra_usb_phy *phy, u8 pts_val)
  189. {
  190. void __iomem *base = phy->regs;
  191. u32 val;
  192. if (phy->soc_config->has_hostpc) {
  193. val = readl_relaxed(base + TEGRA_USB_HOSTPC1_DEVLC);
  194. val &= ~TEGRA_USB_HOSTPC1_DEVLC_PTS(~0);
  195. val |= TEGRA_USB_HOSTPC1_DEVLC_PTS(pts_val);
  196. writel_relaxed(val, base + TEGRA_USB_HOSTPC1_DEVLC);
  197. } else {
  198. val = readl_relaxed(base + TEGRA_USB_PORTSC1);
  199. val &= ~TEGRA_PORTSC1_RWC_BITS;
  200. val &= ~TEGRA_USB_PORTSC1_PTS(~0);
  201. val |= TEGRA_USB_PORTSC1_PTS(pts_val);
  202. writel_relaxed(val, base + TEGRA_USB_PORTSC1);
  203. }
  204. }
  205. static void set_phcd(struct tegra_usb_phy *phy, bool enable)
  206. {
  207. void __iomem *base = phy->regs;
  208. u32 val;
  209. if (phy->soc_config->has_hostpc) {
  210. val = readl_relaxed(base + TEGRA_USB_HOSTPC1_DEVLC);
  211. if (enable)
  212. val |= TEGRA_USB_HOSTPC1_DEVLC_PHCD;
  213. else
  214. val &= ~TEGRA_USB_HOSTPC1_DEVLC_PHCD;
  215. writel_relaxed(val, base + TEGRA_USB_HOSTPC1_DEVLC);
  216. } else {
  217. val = readl_relaxed(base + TEGRA_USB_PORTSC1) & ~PORT_RWC_BITS;
  218. if (enable)
  219. val |= TEGRA_USB_PORTSC1_PHCD;
  220. else
  221. val &= ~TEGRA_USB_PORTSC1_PHCD;
  222. writel_relaxed(val, base + TEGRA_USB_PORTSC1);
  223. }
  224. }
  225. static int utmip_pad_open(struct tegra_usb_phy *phy)
  226. {
  227. int ret;
  228. ret = clk_prepare_enable(phy->pad_clk);
  229. if (ret) {
  230. dev_err(phy->u_phy.dev,
  231. "Failed to enable UTMI-pads clock: %d\n", ret);
  232. return ret;
  233. }
  234. spin_lock(&utmip_pad_lock);
  235. ret = reset_control_deassert(phy->pad_rst);
  236. if (ret) {
  237. dev_err(phy->u_phy.dev,
  238. "Failed to initialize UTMI-pads reset: %d\n", ret);
  239. goto unlock;
  240. }
  241. ret = reset_control_assert(phy->pad_rst);
  242. if (ret) {
  243. dev_err(phy->u_phy.dev,
  244. "Failed to assert UTMI-pads reset: %d\n", ret);
  245. goto unlock;
  246. }
  247. udelay(1);
  248. ret = reset_control_deassert(phy->pad_rst);
  249. if (ret)
  250. dev_err(phy->u_phy.dev,
  251. "Failed to deassert UTMI-pads reset: %d\n", ret);
  252. unlock:
  253. spin_unlock(&utmip_pad_lock);
  254. clk_disable_unprepare(phy->pad_clk);
  255. return ret;
  256. }
  257. static int utmip_pad_close(struct tegra_usb_phy *phy)
  258. {
  259. int ret;
  260. ret = clk_prepare_enable(phy->pad_clk);
  261. if (ret) {
  262. dev_err(phy->u_phy.dev,
  263. "Failed to enable UTMI-pads clock: %d\n", ret);
  264. return ret;
  265. }
  266. ret = reset_control_assert(phy->pad_rst);
  267. if (ret)
  268. dev_err(phy->u_phy.dev,
  269. "Failed to assert UTMI-pads reset: %d\n", ret);
  270. udelay(1);
  271. clk_disable_unprepare(phy->pad_clk);
  272. return ret;
  273. }
  274. static int utmip_pad_power_on(struct tegra_usb_phy *phy)
  275. {
  276. struct tegra_utmip_config *config = phy->config;
  277. void __iomem *base = phy->pad_regs;
  278. u32 val;
  279. int err;
  280. err = clk_prepare_enable(phy->pad_clk);
  281. if (err)
  282. return err;
  283. spin_lock(&utmip_pad_lock);
  284. if (utmip_pad_count++ == 0) {
  285. val = readl_relaxed(base + UTMIP_BIAS_CFG0);
  286. val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
  287. if (phy->soc_config->requires_extra_tuning_parameters) {
  288. val &= ~(UTMIP_HSSQUELCH_LEVEL(~0) |
  289. UTMIP_HSDISCON_LEVEL(~0) |
  290. UTMIP_HSDISCON_LEVEL_MSB(~0));
  291. val |= UTMIP_HSSQUELCH_LEVEL(config->hssquelch_level);
  292. val |= UTMIP_HSDISCON_LEVEL(config->hsdiscon_level);
  293. val |= UTMIP_HSDISCON_LEVEL_MSB(config->hsdiscon_level);
  294. }
  295. writel_relaxed(val, base + UTMIP_BIAS_CFG0);
  296. }
  297. if (phy->pad_wakeup) {
  298. phy->pad_wakeup = false;
  299. utmip_pad_count--;
  300. }
  301. spin_unlock(&utmip_pad_lock);
  302. clk_disable_unprepare(phy->pad_clk);
  303. return 0;
  304. }
  305. static int utmip_pad_power_off(struct tegra_usb_phy *phy)
  306. {
  307. void __iomem *base = phy->pad_regs;
  308. u32 val;
  309. int ret;
  310. ret = clk_prepare_enable(phy->pad_clk);
  311. if (ret)
  312. return ret;
  313. spin_lock(&utmip_pad_lock);
  314. if (!utmip_pad_count) {
  315. dev_err(phy->u_phy.dev, "UTMIP pad already powered off\n");
  316. ret = -EINVAL;
  317. goto ulock;
  318. }
  319. /*
  320. * In accordance to TRM, OTG and Bias pad circuits could be turned off
  321. * to save power if wake is enabled, but the VBUS-change detection
  322. * method is board-specific and these circuits may need to be enabled
  323. * to generate wakeup event, hence we will just keep them both enabled.
  324. */
  325. if (phy->wakeup_enabled) {
  326. phy->pad_wakeup = true;
  327. utmip_pad_count++;
  328. }
  329. if (--utmip_pad_count == 0) {
  330. val = readl_relaxed(base + UTMIP_BIAS_CFG0);
  331. val |= UTMIP_OTGPD | UTMIP_BIASPD;
  332. writel_relaxed(val, base + UTMIP_BIAS_CFG0);
  333. }
  334. ulock:
  335. spin_unlock(&utmip_pad_lock);
  336. clk_disable_unprepare(phy->pad_clk);
  337. return ret;
  338. }
  339. static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
  340. {
  341. u32 tmp;
  342. return readl_relaxed_poll_timeout(reg, tmp, (tmp & mask) == result,
  343. 2000, 6000);
  344. }
  345. static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
  346. {
  347. void __iomem *base = phy->regs;
  348. u32 val;
  349. /*
  350. * The USB driver may have already initiated the phy clock
  351. * disable so wait to see if the clock turns off and if not
  352. * then proceed with gating the clock.
  353. */
  354. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) == 0)
  355. return;
  356. if (phy->is_legacy_phy) {
  357. val = readl_relaxed(base + USB_SUSP_CTRL);
  358. val |= USB_SUSP_SET;
  359. writel_relaxed(val, base + USB_SUSP_CTRL);
  360. usleep_range(10, 100);
  361. val = readl_relaxed(base + USB_SUSP_CTRL);
  362. val &= ~USB_SUSP_SET;
  363. writel_relaxed(val, base + USB_SUSP_CTRL);
  364. } else {
  365. set_phcd(phy, true);
  366. }
  367. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0))
  368. dev_err(phy->u_phy.dev,
  369. "Timeout waiting for PHY to stabilize on disable\n");
  370. }
  371. static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
  372. {
  373. void __iomem *base = phy->regs;
  374. u32 val;
  375. /*
  376. * The USB driver may have already initiated the phy clock
  377. * enable so wait to see if the clock turns on and if not
  378. * then proceed with ungating the clock.
  379. */
  380. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
  381. USB_PHY_CLK_VALID) == 0)
  382. return;
  383. if (phy->is_legacy_phy) {
  384. val = readl_relaxed(base + USB_SUSP_CTRL);
  385. val |= USB_SUSP_CLR;
  386. writel_relaxed(val, base + USB_SUSP_CTRL);
  387. usleep_range(10, 100);
  388. val = readl_relaxed(base + USB_SUSP_CTRL);
  389. val &= ~USB_SUSP_CLR;
  390. writel_relaxed(val, base + USB_SUSP_CTRL);
  391. } else {
  392. set_phcd(phy, false);
  393. }
  394. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
  395. USB_PHY_CLK_VALID))
  396. dev_err(phy->u_phy.dev,
  397. "Timeout waiting for PHY to stabilize on enable\n");
  398. }
  399. static int utmi_phy_power_on(struct tegra_usb_phy *phy)
  400. {
  401. struct tegra_utmip_config *config = phy->config;
  402. void __iomem *base = phy->regs;
  403. u32 val;
  404. int err;
  405. val = readl_relaxed(base + USB_SUSP_CTRL);
  406. val |= UTMIP_RESET;
  407. writel_relaxed(val, base + USB_SUSP_CTRL);
  408. if (phy->is_legacy_phy) {
  409. val = readl_relaxed(base + USB1_LEGACY_CTRL);
  410. val |= USB1_NO_LEGACY_MODE;
  411. writel_relaxed(val, base + USB1_LEGACY_CTRL);
  412. }
  413. val = readl_relaxed(base + UTMIP_TX_CFG0);
  414. val |= UTMIP_FS_PREABMLE_J;
  415. writel_relaxed(val, base + UTMIP_TX_CFG0);
  416. val = readl_relaxed(base + UTMIP_HSRX_CFG0);
  417. val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
  418. val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
  419. val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
  420. writel_relaxed(val, base + UTMIP_HSRX_CFG0);
  421. val = readl_relaxed(base + UTMIP_HSRX_CFG1);
  422. val &= ~UTMIP_HS_SYNC_START_DLY(~0);
  423. val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
  424. writel_relaxed(val, base + UTMIP_HSRX_CFG1);
  425. val = readl_relaxed(base + UTMIP_DEBOUNCE_CFG0);
  426. val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
  427. val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
  428. writel_relaxed(val, base + UTMIP_DEBOUNCE_CFG0);
  429. val = readl_relaxed(base + UTMIP_MISC_CFG0);
  430. val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
  431. writel_relaxed(val, base + UTMIP_MISC_CFG0);
  432. if (!phy->soc_config->utmi_pll_config_in_car_module) {
  433. val = readl_relaxed(base + UTMIP_MISC_CFG1);
  434. val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) |
  435. UTMIP_PLLU_STABLE_COUNT(~0));
  436. val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
  437. UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
  438. writel_relaxed(val, base + UTMIP_MISC_CFG1);
  439. val = readl_relaxed(base + UTMIP_PLL_CFG1);
  440. val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) |
  441. UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
  442. val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
  443. UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
  444. writel_relaxed(val, base + UTMIP_PLL_CFG1);
  445. }
  446. val = readl_relaxed(base + USB_SUSP_CTRL);
  447. val &= ~USB_WAKE_ON_RESUME_EN;
  448. writel_relaxed(val, base + USB_SUSP_CTRL);
  449. if (phy->mode != USB_DR_MODE_HOST) {
  450. val = readl_relaxed(base + USB_SUSP_CTRL);
  451. val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
  452. writel_relaxed(val, base + USB_SUSP_CTRL);
  453. val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
  454. val &= ~VBUS_WAKEUP_WAKEUP_EN;
  455. val &= ~(ID_CHG_DET | VBUS_WAKEUP_CHG_DET);
  456. writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
  457. val = readl_relaxed(base + USB_PHY_VBUS_SENSORS);
  458. val &= ~(A_VBUS_VLD_WAKEUP_EN | A_SESS_VLD_WAKEUP_EN);
  459. val &= ~(B_SESS_VLD_WAKEUP_EN);
  460. writel_relaxed(val, base + USB_PHY_VBUS_SENSORS);
  461. val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
  462. val &= ~UTMIP_PD_CHRG;
  463. writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
  464. } else {
  465. val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
  466. val |= UTMIP_PD_CHRG;
  467. writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
  468. }
  469. err = utmip_pad_power_on(phy);
  470. if (err)
  471. return err;
  472. val = readl_relaxed(base + UTMIP_XCVR_CFG0);
  473. val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
  474. UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_LSBIAS_SEL |
  475. UTMIP_XCVR_SETUP(~0) | UTMIP_XCVR_SETUP_MSB(~0) |
  476. UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0));
  477. if (!config->xcvr_setup_use_fuses) {
  478. val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
  479. val |= UTMIP_XCVR_SETUP_MSB(config->xcvr_setup);
  480. }
  481. val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
  482. val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
  483. if (phy->soc_config->requires_extra_tuning_parameters) {
  484. val &= ~(UTMIP_XCVR_HSSLEW(~0) | UTMIP_XCVR_HSSLEW_MSB(~0));
  485. val |= UTMIP_XCVR_HSSLEW(config->xcvr_hsslew);
  486. val |= UTMIP_XCVR_HSSLEW_MSB(config->xcvr_hsslew);
  487. }
  488. writel_relaxed(val, base + UTMIP_XCVR_CFG0);
  489. val = readl_relaxed(base + UTMIP_XCVR_CFG1);
  490. val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
  491. UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
  492. val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
  493. writel_relaxed(val, base + UTMIP_XCVR_CFG1);
  494. val = readl_relaxed(base + UTMIP_BIAS_CFG1);
  495. val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
  496. val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
  497. writel_relaxed(val, base + UTMIP_BIAS_CFG1);
  498. val = readl_relaxed(base + UTMIP_SPARE_CFG0);
  499. if (config->xcvr_setup_use_fuses)
  500. val |= FUSE_SETUP_SEL;
  501. else
  502. val &= ~FUSE_SETUP_SEL;
  503. writel_relaxed(val, base + UTMIP_SPARE_CFG0);
  504. if (!phy->is_legacy_phy) {
  505. val = readl_relaxed(base + USB_SUSP_CTRL);
  506. val |= UTMIP_PHY_ENABLE;
  507. writel_relaxed(val, base + USB_SUSP_CTRL);
  508. }
  509. val = readl_relaxed(base + USB_SUSP_CTRL);
  510. val &= ~UTMIP_RESET;
  511. writel_relaxed(val, base + USB_SUSP_CTRL);
  512. if (phy->is_legacy_phy) {
  513. val = readl_relaxed(base + USB1_LEGACY_CTRL);
  514. val &= ~USB1_VBUS_SENSE_CTL_MASK;
  515. val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
  516. writel_relaxed(val, base + USB1_LEGACY_CTRL);
  517. val = readl_relaxed(base + USB_SUSP_CTRL);
  518. val &= ~USB_SUSP_SET;
  519. writel_relaxed(val, base + USB_SUSP_CTRL);
  520. }
  521. utmi_phy_clk_enable(phy);
  522. if (phy->soc_config->requires_usbmode_setup) {
  523. val = readl_relaxed(base + USB_USBMODE);
  524. val &= ~USB_USBMODE_MASK;
  525. if (phy->mode == USB_DR_MODE_HOST)
  526. val |= USB_USBMODE_HOST;
  527. else
  528. val |= USB_USBMODE_DEVICE;
  529. writel_relaxed(val, base + USB_USBMODE);
  530. }
  531. if (!phy->is_legacy_phy)
  532. set_pts(phy, 0);
  533. return 0;
  534. }
  535. static int utmi_phy_power_off(struct tegra_usb_phy *phy)
  536. {
  537. void __iomem *base = phy->regs;
  538. u32 val;
  539. /*
  540. * Give hardware time to settle down after VBUS disconnection,
  541. * otherwise PHY will immediately wake up from suspend.
  542. */
  543. if (phy->wakeup_enabled && phy->mode != USB_DR_MODE_HOST)
  544. readl_relaxed_poll_timeout(base + USB_PHY_VBUS_WAKEUP_ID,
  545. val, !(val & VBUS_WAKEUP_STS),
  546. 5000, 100000);
  547. utmi_phy_clk_disable(phy);
  548. /* PHY won't resume if reset is asserted */
  549. if (!phy->wakeup_enabled) {
  550. val = readl_relaxed(base + USB_SUSP_CTRL);
  551. val |= UTMIP_RESET;
  552. writel_relaxed(val, base + USB_SUSP_CTRL);
  553. }
  554. val = readl_relaxed(base + UTMIP_BAT_CHRG_CFG0);
  555. val |= UTMIP_PD_CHRG;
  556. writel_relaxed(val, base + UTMIP_BAT_CHRG_CFG0);
  557. if (!phy->wakeup_enabled) {
  558. val = readl_relaxed(base + UTMIP_XCVR_CFG0);
  559. val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
  560. UTMIP_FORCE_PDZI_POWERDOWN;
  561. writel_relaxed(val, base + UTMIP_XCVR_CFG0);
  562. }
  563. val = readl_relaxed(base + UTMIP_XCVR_CFG1);
  564. val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
  565. UTMIP_FORCE_PDDR_POWERDOWN;
  566. writel_relaxed(val, base + UTMIP_XCVR_CFG1);
  567. if (phy->wakeup_enabled) {
  568. val = readl_relaxed(base + USB_SUSP_CTRL);
  569. val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
  570. val |= USB_WAKEUP_DEBOUNCE_COUNT(5);
  571. val |= USB_WAKE_ON_RESUME_EN;
  572. writel_relaxed(val, base + USB_SUSP_CTRL);
  573. /*
  574. * Ask VBUS sensor to generate wake event once cable is
  575. * connected.
  576. */
  577. if (phy->mode != USB_DR_MODE_HOST) {
  578. val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
  579. val |= VBUS_WAKEUP_WAKEUP_EN;
  580. val &= ~(ID_CHG_DET | VBUS_WAKEUP_CHG_DET);
  581. writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
  582. val = readl_relaxed(base + USB_PHY_VBUS_SENSORS);
  583. val |= A_VBUS_VLD_WAKEUP_EN;
  584. writel_relaxed(val, base + USB_PHY_VBUS_SENSORS);
  585. }
  586. }
  587. return utmip_pad_power_off(phy);
  588. }
  589. static void utmi_phy_preresume(struct tegra_usb_phy *phy)
  590. {
  591. void __iomem *base = phy->regs;
  592. u32 val;
  593. val = readl_relaxed(base + UTMIP_TX_CFG0);
  594. val |= UTMIP_HS_DISCON_DISABLE;
  595. writel_relaxed(val, base + UTMIP_TX_CFG0);
  596. }
  597. static void utmi_phy_postresume(struct tegra_usb_phy *phy)
  598. {
  599. void __iomem *base = phy->regs;
  600. u32 val;
  601. val = readl_relaxed(base + UTMIP_TX_CFG0);
  602. val &= ~UTMIP_HS_DISCON_DISABLE;
  603. writel_relaxed(val, base + UTMIP_TX_CFG0);
  604. }
  605. static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
  606. enum tegra_usb_phy_port_speed port_speed)
  607. {
  608. void __iomem *base = phy->regs;
  609. u32 val;
  610. val = readl_relaxed(base + UTMIP_MISC_CFG0);
  611. val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
  612. if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
  613. val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
  614. else
  615. val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
  616. writel_relaxed(val, base + UTMIP_MISC_CFG0);
  617. usleep_range(1, 10);
  618. val = readl_relaxed(base + UTMIP_MISC_CFG0);
  619. val |= UTMIP_DPDM_OBSERVE;
  620. writel_relaxed(val, base + UTMIP_MISC_CFG0);
  621. usleep_range(10, 100);
  622. }
  623. static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
  624. {
  625. void __iomem *base = phy->regs;
  626. u32 val;
  627. val = readl_relaxed(base + UTMIP_MISC_CFG0);
  628. val &= ~UTMIP_DPDM_OBSERVE;
  629. writel_relaxed(val, base + UTMIP_MISC_CFG0);
  630. usleep_range(10, 100);
  631. }
  632. static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
  633. {
  634. void __iomem *base = phy->regs;
  635. u32 val;
  636. int err;
  637. gpiod_set_value_cansleep(phy->reset_gpio, 1);
  638. err = clk_prepare_enable(phy->clk);
  639. if (err)
  640. return err;
  641. usleep_range(5000, 6000);
  642. gpiod_set_value_cansleep(phy->reset_gpio, 0);
  643. usleep_range(1000, 2000);
  644. val = readl_relaxed(base + USB_SUSP_CTRL);
  645. val |= UHSIC_RESET;
  646. writel_relaxed(val, base + USB_SUSP_CTRL);
  647. val = readl_relaxed(base + ULPI_TIMING_CTRL_0);
  648. val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
  649. writel_relaxed(val, base + ULPI_TIMING_CTRL_0);
  650. val = readl_relaxed(base + USB_SUSP_CTRL);
  651. val |= ULPI_PHY_ENABLE;
  652. writel_relaxed(val, base + USB_SUSP_CTRL);
  653. val = 0;
  654. writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
  655. val |= ULPI_DATA_TRIMMER_SEL(4);
  656. val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
  657. val |= ULPI_DIR_TRIMMER_SEL(4);
  658. writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
  659. usleep_range(10, 100);
  660. val |= ULPI_DATA_TRIMMER_LOAD;
  661. val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
  662. val |= ULPI_DIR_TRIMMER_LOAD;
  663. writel_relaxed(val, base + ULPI_TIMING_CTRL_1);
  664. /* Fix VbusInvalid due to floating VBUS */
  665. err = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
  666. if (err) {
  667. dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", err);
  668. goto disable_clk;
  669. }
  670. err = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
  671. if (err) {
  672. dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", err);
  673. goto disable_clk;
  674. }
  675. val = readl_relaxed(base + USB_SUSP_CTRL);
  676. val |= USB_SUSP_CLR;
  677. writel_relaxed(val, base + USB_SUSP_CTRL);
  678. usleep_range(100, 1000);
  679. val = readl_relaxed(base + USB_SUSP_CTRL);
  680. val &= ~USB_SUSP_CLR;
  681. writel_relaxed(val, base + USB_SUSP_CTRL);
  682. return 0;
  683. disable_clk:
  684. clk_disable_unprepare(phy->clk);
  685. return err;
  686. }
  687. static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
  688. {
  689. gpiod_set_value_cansleep(phy->reset_gpio, 1);
  690. usleep_range(5000, 6000);
  691. clk_disable_unprepare(phy->clk);
  692. /*
  693. * Wakeup currently unimplemented for ULPI, thus PHY needs to be
  694. * force-resumed.
  695. */
  696. if (WARN_ON_ONCE(phy->wakeup_enabled)) {
  697. ulpi_phy_power_on(phy);
  698. return -EOPNOTSUPP;
  699. }
  700. return 0;
  701. }
  702. static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
  703. {
  704. int err;
  705. if (phy->powered_on)
  706. return 0;
  707. if (phy->is_ulpi_phy)
  708. err = ulpi_phy_power_on(phy);
  709. else
  710. err = utmi_phy_power_on(phy);
  711. if (err)
  712. return err;
  713. phy->powered_on = true;
  714. /* Let PHY settle down */
  715. usleep_range(2000, 2500);
  716. return 0;
  717. }
  718. static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
  719. {
  720. int err;
  721. if (!phy->powered_on)
  722. return 0;
  723. if (phy->is_ulpi_phy)
  724. err = ulpi_phy_power_off(phy);
  725. else
  726. err = utmi_phy_power_off(phy);
  727. if (err)
  728. return err;
  729. phy->powered_on = false;
  730. return 0;
  731. }
  732. static void tegra_usb_phy_shutdown(struct usb_phy *u_phy)
  733. {
  734. struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
  735. if (WARN_ON(!phy->freq))
  736. return;
  737. usb_phy_set_wakeup(u_phy, false);
  738. tegra_usb_phy_power_off(phy);
  739. if (!phy->is_ulpi_phy)
  740. utmip_pad_close(phy);
  741. regulator_disable(phy->vbus);
  742. clk_disable_unprepare(phy->pll_u);
  743. phy->freq = NULL;
  744. }
  745. static irqreturn_t tegra_usb_phy_isr(int irq, void *data)
  746. {
  747. u32 val, int_mask = ID_CHG_DET | VBUS_WAKEUP_CHG_DET;
  748. struct tegra_usb_phy *phy = data;
  749. void __iomem *base = phy->regs;
  750. /*
  751. * The PHY interrupt also wakes the USB controller driver since
  752. * interrupt is shared. We don't do anything in the PHY driver,
  753. * so just clear the interrupt.
  754. */
  755. val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
  756. writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
  757. return val & int_mask ? IRQ_HANDLED : IRQ_NONE;
  758. }
  759. static int tegra_usb_phy_set_wakeup(struct usb_phy *u_phy, bool enable)
  760. {
  761. struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
  762. void __iomem *base = phy->regs;
  763. int ret = 0;
  764. u32 val;
  765. if (phy->wakeup_enabled && phy->mode != USB_DR_MODE_HOST &&
  766. phy->irq > 0) {
  767. disable_irq(phy->irq);
  768. val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
  769. val &= ~(ID_INT_EN | VBUS_WAKEUP_INT_EN);
  770. writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
  771. enable_irq(phy->irq);
  772. free_irq(phy->irq, phy);
  773. phy->wakeup_enabled = false;
  774. }
  775. if (enable && phy->mode != USB_DR_MODE_HOST && phy->irq > 0) {
  776. ret = request_irq(phy->irq, tegra_usb_phy_isr, IRQF_SHARED,
  777. dev_name(phy->u_phy.dev), phy);
  778. if (!ret) {
  779. disable_irq(phy->irq);
  780. /*
  781. * USB clock will be resumed once wake event will be
  782. * generated. The ID-change event requires to have
  783. * interrupts enabled, otherwise it won't be generated.
  784. */
  785. val = readl_relaxed(base + USB_PHY_VBUS_WAKEUP_ID);
  786. val |= ID_INT_EN | VBUS_WAKEUP_INT_EN;
  787. writel_relaxed(val, base + USB_PHY_VBUS_WAKEUP_ID);
  788. enable_irq(phy->irq);
  789. } else {
  790. dev_err(phy->u_phy.dev,
  791. "Failed to request interrupt: %d", ret);
  792. enable = false;
  793. }
  794. }
  795. phy->wakeup_enabled = enable;
  796. return ret;
  797. }
  798. static int tegra_usb_phy_set_suspend(struct usb_phy *u_phy, int suspend)
  799. {
  800. struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
  801. int ret;
  802. if (WARN_ON(!phy->freq))
  803. return -EINVAL;
  804. /*
  805. * PHY is sharing IRQ with the CI driver, hence here we either
  806. * disable interrupt for both PHY and CI or for CI only. The
  807. * interrupt needs to be disabled while hardware is reprogrammed
  808. * because interrupt touches the programmed registers, and thus,
  809. * there could be a race condition.
  810. */
  811. if (phy->irq > 0)
  812. disable_irq(phy->irq);
  813. if (suspend)
  814. ret = tegra_usb_phy_power_off(phy);
  815. else
  816. ret = tegra_usb_phy_power_on(phy);
  817. if (phy->irq > 0)
  818. enable_irq(phy->irq);
  819. return ret;
  820. }
  821. static int tegra_usb_phy_configure_pmc(struct tegra_usb_phy *phy)
  822. {
  823. int err, val = 0;
  824. /* older device-trees don't have PMC regmap */
  825. if (!phy->pmc_regmap)
  826. return 0;
  827. /*
  828. * Tegra20 has a different layout of PMC USB register bits and AO is
  829. * enabled by default after system reset on Tegra20, so assume nothing
  830. * to do on Tegra20.
  831. */
  832. if (!phy->soc_config->requires_pmc_ao_power_up)
  833. return 0;
  834. /* enable VBUS wake-up detector */
  835. if (phy->mode != USB_DR_MODE_HOST)
  836. val |= VBUS_WAKEUP_PD_P0 << phy->instance * 4;
  837. /* enable ID-pin ACC detector for OTG mode switching */
  838. if (phy->mode == USB_DR_MODE_OTG)
  839. val |= ID_PD_P0 << phy->instance * 4;
  840. /* disable detectors to reset them */
  841. err = regmap_set_bits(phy->pmc_regmap, PMC_USB_AO, val);
  842. if (err) {
  843. dev_err(phy->u_phy.dev, "Failed to disable PMC AO: %d\n", err);
  844. return err;
  845. }
  846. usleep_range(10, 100);
  847. /* enable detectors */
  848. err = regmap_clear_bits(phy->pmc_regmap, PMC_USB_AO, val);
  849. if (err) {
  850. dev_err(phy->u_phy.dev, "Failed to enable PMC AO: %d\n", err);
  851. return err;
  852. }
  853. /* detectors starts to work after 10ms */
  854. usleep_range(10000, 15000);
  855. return 0;
  856. }
  857. static int tegra_usb_phy_init(struct usb_phy *u_phy)
  858. {
  859. struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
  860. unsigned long parent_rate;
  861. unsigned int i;
  862. int err;
  863. if (WARN_ON(phy->freq))
  864. return 0;
  865. err = clk_prepare_enable(phy->pll_u);
  866. if (err)
  867. return err;
  868. parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
  869. for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
  870. if (tegra_freq_table[i].freq == parent_rate) {
  871. phy->freq = &tegra_freq_table[i];
  872. break;
  873. }
  874. }
  875. if (!phy->freq) {
  876. dev_err(phy->u_phy.dev, "Invalid pll_u parent rate %ld\n",
  877. parent_rate);
  878. err = -EINVAL;
  879. goto disable_clk;
  880. }
  881. err = regulator_enable(phy->vbus);
  882. if (err) {
  883. dev_err(phy->u_phy.dev,
  884. "Failed to enable USB VBUS regulator: %d\n", err);
  885. goto disable_clk;
  886. }
  887. if (!phy->is_ulpi_phy) {
  888. err = utmip_pad_open(phy);
  889. if (err)
  890. goto disable_vbus;
  891. }
  892. err = tegra_usb_phy_configure_pmc(phy);
  893. if (err)
  894. goto close_phy;
  895. err = tegra_usb_phy_power_on(phy);
  896. if (err)
  897. goto close_phy;
  898. return 0;
  899. close_phy:
  900. if (!phy->is_ulpi_phy)
  901. utmip_pad_close(phy);
  902. disable_vbus:
  903. regulator_disable(phy->vbus);
  904. disable_clk:
  905. clk_disable_unprepare(phy->pll_u);
  906. phy->freq = NULL;
  907. return err;
  908. }
  909. void tegra_usb_phy_preresume(struct usb_phy *u_phy)
  910. {
  911. struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
  912. if (!phy->is_ulpi_phy)
  913. utmi_phy_preresume(phy);
  914. }
  915. EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
  916. void tegra_usb_phy_postresume(struct usb_phy *u_phy)
  917. {
  918. struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
  919. if (!phy->is_ulpi_phy)
  920. utmi_phy_postresume(phy);
  921. }
  922. EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
  923. void tegra_ehci_phy_restore_start(struct usb_phy *u_phy,
  924. enum tegra_usb_phy_port_speed port_speed)
  925. {
  926. struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
  927. if (!phy->is_ulpi_phy)
  928. utmi_phy_restore_start(phy, port_speed);
  929. }
  930. EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
  931. void tegra_ehci_phy_restore_end(struct usb_phy *u_phy)
  932. {
  933. struct tegra_usb_phy *phy = to_tegra_usb_phy(u_phy);
  934. if (!phy->is_ulpi_phy)
  935. utmi_phy_restore_end(phy);
  936. }
  937. EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
  938. static int read_utmi_param(struct platform_device *pdev, const char *param,
  939. u8 *dest)
  940. {
  941. u32 value;
  942. int err;
  943. err = of_property_read_u32(pdev->dev.of_node, param, &value);
  944. if (err)
  945. dev_err(&pdev->dev,
  946. "Failed to read USB UTMI parameter %s: %d\n",
  947. param, err);
  948. else
  949. *dest = value;
  950. return err;
  951. }
  952. static int utmi_phy_probe(struct tegra_usb_phy *tegra_phy,
  953. struct platform_device *pdev)
  954. {
  955. struct tegra_utmip_config *config;
  956. struct resource *res;
  957. int err;
  958. tegra_phy->is_ulpi_phy = false;
  959. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  960. if (!res) {
  961. dev_err(&pdev->dev, "Failed to get UTMI pad regs\n");
  962. return -ENXIO;
  963. }
  964. /*
  965. * Note that UTMI pad registers are shared by all PHYs, therefore
  966. * devm_platform_ioremap_resource() can't be used here.
  967. */
  968. tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
  969. resource_size(res));
  970. if (!tegra_phy->pad_regs) {
  971. dev_err(&pdev->dev, "Failed to remap UTMI pad regs\n");
  972. return -ENOMEM;
  973. }
  974. tegra_phy->config = devm_kzalloc(&pdev->dev, sizeof(*config),
  975. GFP_KERNEL);
  976. if (!tegra_phy->config)
  977. return -ENOMEM;
  978. config = tegra_phy->config;
  979. err = read_utmi_param(pdev, "nvidia,hssync-start-delay",
  980. &config->hssync_start_delay);
  981. if (err)
  982. return err;
  983. err = read_utmi_param(pdev, "nvidia,elastic-limit",
  984. &config->elastic_limit);
  985. if (err)
  986. return err;
  987. err = read_utmi_param(pdev, "nvidia,idle-wait-delay",
  988. &config->idle_wait_delay);
  989. if (err)
  990. return err;
  991. err = read_utmi_param(pdev, "nvidia,term-range-adj",
  992. &config->term_range_adj);
  993. if (err)
  994. return err;
  995. err = read_utmi_param(pdev, "nvidia,xcvr-lsfslew",
  996. &config->xcvr_lsfslew);
  997. if (err)
  998. return err;
  999. err = read_utmi_param(pdev, "nvidia,xcvr-lsrslew",
  1000. &config->xcvr_lsrslew);
  1001. if (err)
  1002. return err;
  1003. if (tegra_phy->soc_config->requires_extra_tuning_parameters) {
  1004. err = read_utmi_param(pdev, "nvidia,xcvr-hsslew",
  1005. &config->xcvr_hsslew);
  1006. if (err)
  1007. return err;
  1008. err = read_utmi_param(pdev, "nvidia,hssquelch-level",
  1009. &config->hssquelch_level);
  1010. if (err)
  1011. return err;
  1012. err = read_utmi_param(pdev, "nvidia,hsdiscon-level",
  1013. &config->hsdiscon_level);
  1014. if (err)
  1015. return err;
  1016. }
  1017. config->xcvr_setup_use_fuses = of_property_read_bool(
  1018. pdev->dev.of_node, "nvidia,xcvr-setup-use-fuses");
  1019. if (!config->xcvr_setup_use_fuses) {
  1020. err = read_utmi_param(pdev, "nvidia,xcvr-setup",
  1021. &config->xcvr_setup);
  1022. if (err)
  1023. return err;
  1024. }
  1025. return 0;
  1026. }
  1027. static void tegra_usb_phy_put_pmc_device(void *dev)
  1028. {
  1029. put_device(dev);
  1030. }
  1031. static int tegra_usb_phy_parse_pmc(struct device *dev,
  1032. struct tegra_usb_phy *phy)
  1033. {
  1034. struct platform_device *pmc_pdev;
  1035. struct of_phandle_args args;
  1036. int err;
  1037. err = of_parse_phandle_with_fixed_args(dev->of_node, "nvidia,pmc",
  1038. 1, 0, &args);
  1039. if (err) {
  1040. if (err != -ENOENT)
  1041. return err;
  1042. dev_warn_once(dev, "nvidia,pmc is missing, please update your device-tree\n");
  1043. return 0;
  1044. }
  1045. pmc_pdev = of_find_device_by_node(args.np);
  1046. of_node_put(args.np);
  1047. if (!pmc_pdev)
  1048. return -ENODEV;
  1049. err = devm_add_action_or_reset(dev, tegra_usb_phy_put_pmc_device,
  1050. &pmc_pdev->dev);
  1051. if (err)
  1052. return err;
  1053. if (!platform_get_drvdata(pmc_pdev))
  1054. return -EPROBE_DEFER;
  1055. phy->pmc_regmap = dev_get_regmap(&pmc_pdev->dev, "usb_sleepwalk");
  1056. if (!phy->pmc_regmap)
  1057. return -EINVAL;
  1058. phy->instance = args.args[0];
  1059. return 0;
  1060. }
  1061. static const struct tegra_phy_soc_config tegra20_soc_config = {
  1062. .utmi_pll_config_in_car_module = false,
  1063. .has_hostpc = false,
  1064. .requires_usbmode_setup = false,
  1065. .requires_extra_tuning_parameters = false,
  1066. .requires_pmc_ao_power_up = false,
  1067. };
  1068. static const struct tegra_phy_soc_config tegra30_soc_config = {
  1069. .utmi_pll_config_in_car_module = true,
  1070. .has_hostpc = true,
  1071. .requires_usbmode_setup = true,
  1072. .requires_extra_tuning_parameters = true,
  1073. .requires_pmc_ao_power_up = true,
  1074. };
  1075. static const struct of_device_id tegra_usb_phy_id_table[] = {
  1076. { .compatible = "nvidia,tegra30-usb-phy", .data = &tegra30_soc_config },
  1077. { .compatible = "nvidia,tegra20-usb-phy", .data = &tegra20_soc_config },
  1078. { },
  1079. };
  1080. MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
  1081. static int tegra_usb_phy_probe(struct platform_device *pdev)
  1082. {
  1083. struct device_node *np = pdev->dev.of_node;
  1084. struct tegra_usb_phy *tegra_phy;
  1085. enum usb_phy_interface phy_type;
  1086. struct reset_control *reset;
  1087. struct gpio_desc *gpiod;
  1088. struct resource *res;
  1089. struct usb_phy *phy;
  1090. int err;
  1091. tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
  1092. if (!tegra_phy)
  1093. return -ENOMEM;
  1094. tegra_phy->soc_config = of_device_get_match_data(&pdev->dev);
  1095. tegra_phy->irq = platform_get_irq_optional(pdev, 0);
  1096. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1097. if (!res) {
  1098. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  1099. return -ENXIO;
  1100. }
  1101. /*
  1102. * Note that PHY and USB controller are using shared registers,
  1103. * therefore devm_platform_ioremap_resource() can't be used here.
  1104. */
  1105. tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
  1106. resource_size(res));
  1107. if (!tegra_phy->regs) {
  1108. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  1109. return -ENOMEM;
  1110. }
  1111. tegra_phy->is_legacy_phy =
  1112. of_property_read_bool(np, "nvidia,has-legacy-mode");
  1113. if (of_find_property(np, "dr_mode", NULL))
  1114. tegra_phy->mode = usb_get_dr_mode(&pdev->dev);
  1115. else
  1116. tegra_phy->mode = USB_DR_MODE_HOST;
  1117. if (tegra_phy->mode == USB_DR_MODE_UNKNOWN) {
  1118. dev_err(&pdev->dev, "dr_mode is invalid\n");
  1119. return -EINVAL;
  1120. }
  1121. /* On some boards, the VBUS regulator doesn't need to be controlled */
  1122. tegra_phy->vbus = devm_regulator_get(&pdev->dev, "vbus");
  1123. if (IS_ERR(tegra_phy->vbus))
  1124. return PTR_ERR(tegra_phy->vbus);
  1125. tegra_phy->pll_u = devm_clk_get(&pdev->dev, "pll_u");
  1126. err = PTR_ERR_OR_ZERO(tegra_phy->pll_u);
  1127. if (err) {
  1128. dev_err(&pdev->dev, "Failed to get pll_u clock: %d\n", err);
  1129. return err;
  1130. }
  1131. err = tegra_usb_phy_parse_pmc(&pdev->dev, tegra_phy);
  1132. if (err) {
  1133. dev_err_probe(&pdev->dev, err, "Failed to get PMC regmap\n");
  1134. return err;
  1135. }
  1136. phy_type = of_usb_get_phy_mode(np);
  1137. switch (phy_type) {
  1138. case USBPHY_INTERFACE_MODE_UTMI:
  1139. err = utmi_phy_probe(tegra_phy, pdev);
  1140. if (err)
  1141. return err;
  1142. tegra_phy->pad_clk = devm_clk_get(&pdev->dev, "utmi-pads");
  1143. err = PTR_ERR_OR_ZERO(tegra_phy->pad_clk);
  1144. if (err) {
  1145. dev_err(&pdev->dev,
  1146. "Failed to get UTMIP pad clock: %d\n", err);
  1147. return err;
  1148. }
  1149. reset = devm_reset_control_get_optional_shared(&pdev->dev,
  1150. "utmi-pads");
  1151. err = PTR_ERR_OR_ZERO(reset);
  1152. if (err) {
  1153. dev_err(&pdev->dev,
  1154. "Failed to get UTMI-pads reset: %d\n", err);
  1155. return err;
  1156. }
  1157. tegra_phy->pad_rst = reset;
  1158. break;
  1159. case USBPHY_INTERFACE_MODE_ULPI:
  1160. tegra_phy->is_ulpi_phy = true;
  1161. tegra_phy->clk = devm_clk_get(&pdev->dev, "ulpi-link");
  1162. err = PTR_ERR_OR_ZERO(tegra_phy->clk);
  1163. if (err) {
  1164. dev_err(&pdev->dev,
  1165. "Failed to get ULPI clock: %d\n", err);
  1166. return err;
  1167. }
  1168. gpiod = devm_gpiod_get(&pdev->dev, "nvidia,phy-reset",
  1169. GPIOD_OUT_HIGH);
  1170. err = PTR_ERR_OR_ZERO(gpiod);
  1171. if (err) {
  1172. dev_err(&pdev->dev,
  1173. "Request failed for reset GPIO: %d\n", err);
  1174. return err;
  1175. }
  1176. err = gpiod_set_consumer_name(gpiod, "ulpi_phy_reset_b");
  1177. if (err) {
  1178. dev_err(&pdev->dev,
  1179. "Failed to set up reset GPIO name: %d\n", err);
  1180. return err;
  1181. }
  1182. tegra_phy->reset_gpio = gpiod;
  1183. phy = devm_otg_ulpi_create(&pdev->dev,
  1184. &ulpi_viewport_access_ops, 0);
  1185. if (!phy) {
  1186. dev_err(&pdev->dev, "Failed to create ULPI OTG\n");
  1187. return -ENOMEM;
  1188. }
  1189. tegra_phy->ulpi = phy;
  1190. tegra_phy->ulpi->io_priv = tegra_phy->regs + ULPI_VIEWPORT;
  1191. break;
  1192. default:
  1193. dev_err(&pdev->dev, "phy_type %u is invalid or unsupported\n",
  1194. phy_type);
  1195. return -EINVAL;
  1196. }
  1197. tegra_phy->u_phy.dev = &pdev->dev;
  1198. tegra_phy->u_phy.init = tegra_usb_phy_init;
  1199. tegra_phy->u_phy.shutdown = tegra_usb_phy_shutdown;
  1200. tegra_phy->u_phy.set_wakeup = tegra_usb_phy_set_wakeup;
  1201. tegra_phy->u_phy.set_suspend = tegra_usb_phy_set_suspend;
  1202. platform_set_drvdata(pdev, tegra_phy);
  1203. return usb_add_phy_dev(&tegra_phy->u_phy);
  1204. }
  1205. static int tegra_usb_phy_remove(struct platform_device *pdev)
  1206. {
  1207. struct tegra_usb_phy *tegra_phy = platform_get_drvdata(pdev);
  1208. usb_remove_phy(&tegra_phy->u_phy);
  1209. return 0;
  1210. }
  1211. static struct platform_driver tegra_usb_phy_driver = {
  1212. .probe = tegra_usb_phy_probe,
  1213. .remove = tegra_usb_phy_remove,
  1214. .driver = {
  1215. .name = "tegra-phy",
  1216. .of_match_table = tegra_usb_phy_id_table,
  1217. },
  1218. };
  1219. module_platform_driver(tegra_usb_phy_driver);
  1220. MODULE_DESCRIPTION("Tegra USB PHY driver");
  1221. MODULE_LICENSE("GPL v2");