phy-tegra-usb.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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 <konkers@google.com>
  8. * Benoit Goby <benoit@android.com>
  9. * Venu Byravarasu <vbyravarasu@nvidia.com>
  10. */
  11. #include <linux/resource.h>
  12. #include <linux/delay.h>
  13. #include <linux/slab.h>
  14. #include <linux/err.h>
  15. #include <linux/export.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/gpio.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/usb/otg.h>
  24. #include <linux/usb/ulpi.h>
  25. #include <linux/usb/of.h>
  26. #include <linux/usb/ehci_def.h>
  27. #include <linux/usb/tegra_usb_phy.h>
  28. #include <linux/regulator/consumer.h>
  29. #define ULPI_VIEWPORT 0x170
  30. /* PORTSC PTS/PHCD bits, Tegra20 only */
  31. #define TEGRA_USB_PORTSC1 0x184
  32. #define TEGRA_USB_PORTSC1_PTS(x) (((x) & 0x3) << 30)
  33. #define TEGRA_USB_PORTSC1_PHCD (1 << 23)
  34. /* HOSTPC1 PTS/PHCD bits, Tegra30 and above */
  35. #define TEGRA_USB_HOSTPC1_DEVLC 0x1b4
  36. #define TEGRA_USB_HOSTPC1_DEVLC_PTS(x) (((x) & 0x7) << 29)
  37. #define TEGRA_USB_HOSTPC1_DEVLC_PHCD (1 << 22)
  38. /* Bits of PORTSC1, which will get cleared by writing 1 into them */
  39. #define TEGRA_PORTSC1_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC)
  40. #define USB_SUSP_CTRL 0x400
  41. #define USB_WAKE_ON_CNNT_EN_DEV (1 << 3)
  42. #define USB_WAKE_ON_DISCON_EN_DEV (1 << 4)
  43. #define USB_SUSP_CLR (1 << 5)
  44. #define USB_PHY_CLK_VALID (1 << 7)
  45. #define UTMIP_RESET (1 << 11)
  46. #define UHSIC_RESET (1 << 11)
  47. #define UTMIP_PHY_ENABLE (1 << 12)
  48. #define ULPI_PHY_ENABLE (1 << 13)
  49. #define USB_SUSP_SET (1 << 14)
  50. #define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
  51. #define USB1_LEGACY_CTRL 0x410
  52. #define USB1_NO_LEGACY_MODE (1 << 0)
  53. #define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
  54. #define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
  55. #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
  56. (1 << 1)
  57. #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
  58. #define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
  59. #define ULPI_TIMING_CTRL_0 0x424
  60. #define ULPI_OUTPUT_PINMUX_BYP (1 << 10)
  61. #define ULPI_CLKOUT_PINMUX_BYP (1 << 11)
  62. #define ULPI_TIMING_CTRL_1 0x428
  63. #define ULPI_DATA_TRIMMER_LOAD (1 << 0)
  64. #define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
  65. #define ULPI_STPDIRNXT_TRIMMER_LOAD (1 << 16)
  66. #define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
  67. #define ULPI_DIR_TRIMMER_LOAD (1 << 24)
  68. #define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
  69. #define UTMIP_PLL_CFG1 0x804
  70. #define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
  71. #define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
  72. #define UTMIP_XCVR_CFG0 0x808
  73. #define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
  74. #define UTMIP_XCVR_SETUP_MSB(x) ((((x) & 0x70) >> 4) << 22)
  75. #define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
  76. #define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
  77. #define UTMIP_FORCE_PD_POWERDOWN (1 << 14)
  78. #define UTMIP_FORCE_PD2_POWERDOWN (1 << 16)
  79. #define UTMIP_FORCE_PDZI_POWERDOWN (1 << 18)
  80. #define UTMIP_XCVR_LSBIAS_SEL (1 << 21)
  81. #define UTMIP_XCVR_HSSLEW(x) (((x) & 0x3) << 4)
  82. #define UTMIP_XCVR_HSSLEW_MSB(x) ((((x) & 0x1fc) >> 2) << 25)
  83. #define UTMIP_BIAS_CFG0 0x80c
  84. #define UTMIP_OTGPD (1 << 11)
  85. #define UTMIP_BIASPD (1 << 10)
  86. #define UTMIP_HSSQUELCH_LEVEL(x) (((x) & 0x3) << 0)
  87. #define UTMIP_HSDISCON_LEVEL(x) (((x) & 0x3) << 2)
  88. #define UTMIP_HSDISCON_LEVEL_MSB(x) ((((x) & 0x4) >> 2) << 24)
  89. #define UTMIP_HSRX_CFG0 0x810
  90. #define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
  91. #define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
  92. #define UTMIP_HSRX_CFG1 0x814
  93. #define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
  94. #define UTMIP_TX_CFG0 0x820
  95. #define UTMIP_FS_PREABMLE_J (1 << 19)
  96. #define UTMIP_HS_DISCON_DISABLE (1 << 8)
  97. #define UTMIP_MISC_CFG0 0x824
  98. #define UTMIP_DPDM_OBSERVE (1 << 26)
  99. #define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
  100. #define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
  101. #define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
  102. #define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
  103. #define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
  104. #define UTMIP_SUSPEND_EXIT_ON_EDGE (1 << 22)
  105. #define UTMIP_MISC_CFG1 0x828
  106. #define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
  107. #define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
  108. #define UTMIP_DEBOUNCE_CFG0 0x82c
  109. #define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
  110. #define UTMIP_BAT_CHRG_CFG0 0x830
  111. #define UTMIP_PD_CHRG (1 << 0)
  112. #define UTMIP_SPARE_CFG0 0x834
  113. #define FUSE_SETUP_SEL (1 << 3)
  114. #define UTMIP_XCVR_CFG1 0x838
  115. #define UTMIP_FORCE_PDDISC_POWERDOWN (1 << 0)
  116. #define UTMIP_FORCE_PDCHRP_POWERDOWN (1 << 2)
  117. #define UTMIP_FORCE_PDDR_POWERDOWN (1 << 4)
  118. #define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
  119. #define UTMIP_BIAS_CFG1 0x83c
  120. #define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
  121. /* For Tegra30 and above only, the address is different in Tegra20 */
  122. #define USB_USBMODE 0x1f8
  123. #define USB_USBMODE_MASK (3 << 0)
  124. #define USB_USBMODE_HOST (3 << 0)
  125. #define USB_USBMODE_DEVICE (2 << 0)
  126. static DEFINE_SPINLOCK(utmip_pad_lock);
  127. static int utmip_pad_count;
  128. struct tegra_xtal_freq {
  129. int freq;
  130. u8 enable_delay;
  131. u8 stable_count;
  132. u8 active_delay;
  133. u8 xtal_freq_count;
  134. u16 debounce;
  135. };
  136. static const struct tegra_xtal_freq tegra_freq_table[] = {
  137. {
  138. .freq = 12000000,
  139. .enable_delay = 0x02,
  140. .stable_count = 0x2F,
  141. .active_delay = 0x04,
  142. .xtal_freq_count = 0x76,
  143. .debounce = 0x7530,
  144. },
  145. {
  146. .freq = 13000000,
  147. .enable_delay = 0x02,
  148. .stable_count = 0x33,
  149. .active_delay = 0x05,
  150. .xtal_freq_count = 0x7F,
  151. .debounce = 0x7EF4,
  152. },
  153. {
  154. .freq = 19200000,
  155. .enable_delay = 0x03,
  156. .stable_count = 0x4B,
  157. .active_delay = 0x06,
  158. .xtal_freq_count = 0xBB,
  159. .debounce = 0xBB80,
  160. },
  161. {
  162. .freq = 26000000,
  163. .enable_delay = 0x04,
  164. .stable_count = 0x66,
  165. .active_delay = 0x09,
  166. .xtal_freq_count = 0xFE,
  167. .debounce = 0xFDE8,
  168. },
  169. };
  170. static void set_pts(struct tegra_usb_phy *phy, u8 pts_val)
  171. {
  172. void __iomem *base = phy->regs;
  173. unsigned long val;
  174. if (phy->soc_config->has_hostpc) {
  175. val = readl(base + TEGRA_USB_HOSTPC1_DEVLC);
  176. val &= ~TEGRA_USB_HOSTPC1_DEVLC_PTS(~0);
  177. val |= TEGRA_USB_HOSTPC1_DEVLC_PTS(pts_val);
  178. writel(val, base + TEGRA_USB_HOSTPC1_DEVLC);
  179. } else {
  180. val = readl(base + TEGRA_USB_PORTSC1) & ~TEGRA_PORTSC1_RWC_BITS;
  181. val &= ~TEGRA_USB_PORTSC1_PTS(~0);
  182. val |= TEGRA_USB_PORTSC1_PTS(pts_val);
  183. writel(val, base + TEGRA_USB_PORTSC1);
  184. }
  185. }
  186. static void set_phcd(struct tegra_usb_phy *phy, bool enable)
  187. {
  188. void __iomem *base = phy->regs;
  189. unsigned long val;
  190. if (phy->soc_config->has_hostpc) {
  191. val = readl(base + TEGRA_USB_HOSTPC1_DEVLC);
  192. if (enable)
  193. val |= TEGRA_USB_HOSTPC1_DEVLC_PHCD;
  194. else
  195. val &= ~TEGRA_USB_HOSTPC1_DEVLC_PHCD;
  196. writel(val, base + TEGRA_USB_HOSTPC1_DEVLC);
  197. } else {
  198. val = readl(base + TEGRA_USB_PORTSC1) & ~PORT_RWC_BITS;
  199. if (enable)
  200. val |= TEGRA_USB_PORTSC1_PHCD;
  201. else
  202. val &= ~TEGRA_USB_PORTSC1_PHCD;
  203. writel(val, base + TEGRA_USB_PORTSC1);
  204. }
  205. }
  206. static int utmip_pad_open(struct tegra_usb_phy *phy)
  207. {
  208. int ret;
  209. phy->pad_clk = devm_clk_get(phy->u_phy.dev, "utmi-pads");
  210. if (IS_ERR(phy->pad_clk)) {
  211. ret = PTR_ERR(phy->pad_clk);
  212. dev_err(phy->u_phy.dev,
  213. "Failed to get UTMIP pad clock: %d\n", ret);
  214. return ret;
  215. }
  216. phy->pad_rst = devm_reset_control_get_optional_shared(
  217. phy->u_phy.dev, "utmi-pads");
  218. if (IS_ERR(phy->pad_rst)) {
  219. ret = PTR_ERR(phy->pad_rst);
  220. dev_err(phy->u_phy.dev,
  221. "Failed to get UTMI-pads reset: %d\n", ret);
  222. return ret;
  223. }
  224. ret = clk_prepare_enable(phy->pad_clk);
  225. if (ret) {
  226. dev_err(phy->u_phy.dev,
  227. "Failed to enable UTMI-pads clock: %d\n", ret);
  228. return ret;
  229. }
  230. spin_lock(&utmip_pad_lock);
  231. ret = reset_control_deassert(phy->pad_rst);
  232. if (ret) {
  233. dev_err(phy->u_phy.dev,
  234. "Failed to initialize UTMI-pads reset: %d\n", ret);
  235. goto unlock;
  236. }
  237. ret = reset_control_assert(phy->pad_rst);
  238. if (ret) {
  239. dev_err(phy->u_phy.dev,
  240. "Failed to assert UTMI-pads reset: %d\n", ret);
  241. goto unlock;
  242. }
  243. udelay(1);
  244. ret = reset_control_deassert(phy->pad_rst);
  245. if (ret)
  246. dev_err(phy->u_phy.dev,
  247. "Failed to deassert UTMI-pads reset: %d\n", ret);
  248. unlock:
  249. spin_unlock(&utmip_pad_lock);
  250. clk_disable_unprepare(phy->pad_clk);
  251. return ret;
  252. }
  253. static int utmip_pad_close(struct tegra_usb_phy *phy)
  254. {
  255. int ret;
  256. ret = clk_prepare_enable(phy->pad_clk);
  257. if (ret) {
  258. dev_err(phy->u_phy.dev,
  259. "Failed to enable UTMI-pads clock: %d\n", ret);
  260. return ret;
  261. }
  262. ret = reset_control_assert(phy->pad_rst);
  263. if (ret)
  264. dev_err(phy->u_phy.dev,
  265. "Failed to assert UTMI-pads reset: %d\n", ret);
  266. udelay(1);
  267. clk_disable_unprepare(phy->pad_clk);
  268. return ret;
  269. }
  270. static void utmip_pad_power_on(struct tegra_usb_phy *phy)
  271. {
  272. unsigned long val, flags;
  273. void __iomem *base = phy->pad_regs;
  274. struct tegra_utmip_config *config = phy->config;
  275. clk_prepare_enable(phy->pad_clk);
  276. spin_lock_irqsave(&utmip_pad_lock, flags);
  277. if (utmip_pad_count++ == 0) {
  278. val = readl(base + UTMIP_BIAS_CFG0);
  279. val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
  280. if (phy->soc_config->requires_extra_tuning_parameters) {
  281. val &= ~(UTMIP_HSSQUELCH_LEVEL(~0) |
  282. UTMIP_HSDISCON_LEVEL(~0) |
  283. UTMIP_HSDISCON_LEVEL_MSB(~0));
  284. val |= UTMIP_HSSQUELCH_LEVEL(config->hssquelch_level);
  285. val |= UTMIP_HSDISCON_LEVEL(config->hsdiscon_level);
  286. val |= UTMIP_HSDISCON_LEVEL_MSB(config->hsdiscon_level);
  287. }
  288. writel(val, base + UTMIP_BIAS_CFG0);
  289. }
  290. spin_unlock_irqrestore(&utmip_pad_lock, flags);
  291. clk_disable_unprepare(phy->pad_clk);
  292. }
  293. static int utmip_pad_power_off(struct tegra_usb_phy *phy)
  294. {
  295. unsigned long val, flags;
  296. void __iomem *base = phy->pad_regs;
  297. if (!utmip_pad_count) {
  298. dev_err(phy->u_phy.dev, "UTMIP pad already powered off\n");
  299. return -EINVAL;
  300. }
  301. clk_prepare_enable(phy->pad_clk);
  302. spin_lock_irqsave(&utmip_pad_lock, flags);
  303. if (--utmip_pad_count == 0) {
  304. val = readl(base + UTMIP_BIAS_CFG0);
  305. val |= UTMIP_OTGPD | UTMIP_BIASPD;
  306. writel(val, base + UTMIP_BIAS_CFG0);
  307. }
  308. spin_unlock_irqrestore(&utmip_pad_lock, flags);
  309. clk_disable_unprepare(phy->pad_clk);
  310. return 0;
  311. }
  312. static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
  313. {
  314. u32 tmp;
  315. return readl_poll_timeout(reg, tmp, (tmp & mask) == result,
  316. 2000, 6000);
  317. }
  318. static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
  319. {
  320. unsigned long val;
  321. void __iomem *base = phy->regs;
  322. /*
  323. * The USB driver may have already initiated the phy clock
  324. * disable so wait to see if the clock turns off and if not
  325. * then proceed with gating the clock.
  326. */
  327. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) == 0)
  328. return;
  329. if (phy->is_legacy_phy) {
  330. val = readl(base + USB_SUSP_CTRL);
  331. val |= USB_SUSP_SET;
  332. writel(val, base + USB_SUSP_CTRL);
  333. udelay(10);
  334. val = readl(base + USB_SUSP_CTRL);
  335. val &= ~USB_SUSP_SET;
  336. writel(val, base + USB_SUSP_CTRL);
  337. } else
  338. set_phcd(phy, true);
  339. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) < 0)
  340. dev_err(phy->u_phy.dev,
  341. "Timeout waiting for PHY to stabilize on disable\n");
  342. }
  343. static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
  344. {
  345. unsigned long val;
  346. void __iomem *base = phy->regs;
  347. /*
  348. * The USB driver may have already initiated the phy clock
  349. * enable so wait to see if the clock turns on and if not
  350. * then proceed with ungating the clock.
  351. */
  352. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
  353. USB_PHY_CLK_VALID) == 0)
  354. return;
  355. if (phy->is_legacy_phy) {
  356. val = readl(base + USB_SUSP_CTRL);
  357. val |= USB_SUSP_CLR;
  358. writel(val, base + USB_SUSP_CTRL);
  359. udelay(10);
  360. val = readl(base + USB_SUSP_CTRL);
  361. val &= ~USB_SUSP_CLR;
  362. writel(val, base + USB_SUSP_CTRL);
  363. } else
  364. set_phcd(phy, false);
  365. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
  366. USB_PHY_CLK_VALID))
  367. dev_err(phy->u_phy.dev,
  368. "Timeout waiting for PHY to stabilize on enable\n");
  369. }
  370. static int utmi_phy_power_on(struct tegra_usb_phy *phy)
  371. {
  372. unsigned long val;
  373. void __iomem *base = phy->regs;
  374. struct tegra_utmip_config *config = phy->config;
  375. val = readl(base + USB_SUSP_CTRL);
  376. val |= UTMIP_RESET;
  377. writel(val, base + USB_SUSP_CTRL);
  378. if (phy->is_legacy_phy) {
  379. val = readl(base + USB1_LEGACY_CTRL);
  380. val |= USB1_NO_LEGACY_MODE;
  381. writel(val, base + USB1_LEGACY_CTRL);
  382. }
  383. val = readl(base + UTMIP_TX_CFG0);
  384. val |= UTMIP_FS_PREABMLE_J;
  385. writel(val, base + UTMIP_TX_CFG0);
  386. val = readl(base + UTMIP_HSRX_CFG0);
  387. val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
  388. val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
  389. val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
  390. writel(val, base + UTMIP_HSRX_CFG0);
  391. val = readl(base + UTMIP_HSRX_CFG1);
  392. val &= ~UTMIP_HS_SYNC_START_DLY(~0);
  393. val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
  394. writel(val, base + UTMIP_HSRX_CFG1);
  395. val = readl(base + UTMIP_DEBOUNCE_CFG0);
  396. val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
  397. val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
  398. writel(val, base + UTMIP_DEBOUNCE_CFG0);
  399. val = readl(base + UTMIP_MISC_CFG0);
  400. val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
  401. writel(val, base + UTMIP_MISC_CFG0);
  402. if (!phy->soc_config->utmi_pll_config_in_car_module) {
  403. val = readl(base + UTMIP_MISC_CFG1);
  404. val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) |
  405. UTMIP_PLLU_STABLE_COUNT(~0));
  406. val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
  407. UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
  408. writel(val, base + UTMIP_MISC_CFG1);
  409. val = readl(base + UTMIP_PLL_CFG1);
  410. val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) |
  411. UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
  412. val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
  413. UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
  414. writel(val, base + UTMIP_PLL_CFG1);
  415. }
  416. if (phy->mode == USB_DR_MODE_PERIPHERAL) {
  417. val = readl(base + USB_SUSP_CTRL);
  418. val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
  419. writel(val, base + USB_SUSP_CTRL);
  420. val = readl(base + UTMIP_BAT_CHRG_CFG0);
  421. val &= ~UTMIP_PD_CHRG;
  422. writel(val, base + UTMIP_BAT_CHRG_CFG0);
  423. } else {
  424. val = readl(base + UTMIP_BAT_CHRG_CFG0);
  425. val |= UTMIP_PD_CHRG;
  426. writel(val, base + UTMIP_BAT_CHRG_CFG0);
  427. }
  428. utmip_pad_power_on(phy);
  429. val = readl(base + UTMIP_XCVR_CFG0);
  430. val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
  431. UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_LSBIAS_SEL |
  432. UTMIP_XCVR_SETUP(~0) | UTMIP_XCVR_SETUP_MSB(~0) |
  433. UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0));
  434. if (!config->xcvr_setup_use_fuses) {
  435. val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
  436. val |= UTMIP_XCVR_SETUP_MSB(config->xcvr_setup);
  437. }
  438. val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
  439. val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
  440. if (phy->soc_config->requires_extra_tuning_parameters) {
  441. val &= ~(UTMIP_XCVR_HSSLEW(~0) | UTMIP_XCVR_HSSLEW_MSB(~0));
  442. val |= UTMIP_XCVR_HSSLEW(config->xcvr_hsslew);
  443. val |= UTMIP_XCVR_HSSLEW_MSB(config->xcvr_hsslew);
  444. }
  445. writel(val, base + UTMIP_XCVR_CFG0);
  446. val = readl(base + UTMIP_XCVR_CFG1);
  447. val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
  448. UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
  449. val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
  450. writel(val, base + UTMIP_XCVR_CFG1);
  451. val = readl(base + UTMIP_BIAS_CFG1);
  452. val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
  453. val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
  454. writel(val, base + UTMIP_BIAS_CFG1);
  455. val = readl(base + UTMIP_SPARE_CFG0);
  456. if (config->xcvr_setup_use_fuses)
  457. val |= FUSE_SETUP_SEL;
  458. else
  459. val &= ~FUSE_SETUP_SEL;
  460. writel(val, base + UTMIP_SPARE_CFG0);
  461. if (!phy->is_legacy_phy) {
  462. val = readl(base + USB_SUSP_CTRL);
  463. val |= UTMIP_PHY_ENABLE;
  464. writel(val, base + USB_SUSP_CTRL);
  465. }
  466. val = readl(base + USB_SUSP_CTRL);
  467. val &= ~UTMIP_RESET;
  468. writel(val, base + USB_SUSP_CTRL);
  469. if (phy->is_legacy_phy) {
  470. val = readl(base + USB1_LEGACY_CTRL);
  471. val &= ~USB1_VBUS_SENSE_CTL_MASK;
  472. val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
  473. writel(val, base + USB1_LEGACY_CTRL);
  474. val = readl(base + USB_SUSP_CTRL);
  475. val &= ~USB_SUSP_SET;
  476. writel(val, base + USB_SUSP_CTRL);
  477. }
  478. utmi_phy_clk_enable(phy);
  479. if (phy->soc_config->requires_usbmode_setup) {
  480. val = readl(base + USB_USBMODE);
  481. val &= ~USB_USBMODE_MASK;
  482. if (phy->mode == USB_DR_MODE_HOST)
  483. val |= USB_USBMODE_HOST;
  484. else
  485. val |= USB_USBMODE_DEVICE;
  486. writel(val, base + USB_USBMODE);
  487. }
  488. if (!phy->is_legacy_phy)
  489. set_pts(phy, 0);
  490. return 0;
  491. }
  492. static int utmi_phy_power_off(struct tegra_usb_phy *phy)
  493. {
  494. unsigned long val;
  495. void __iomem *base = phy->regs;
  496. utmi_phy_clk_disable(phy);
  497. if (phy->mode == USB_DR_MODE_PERIPHERAL) {
  498. val = readl(base + USB_SUSP_CTRL);
  499. val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
  500. val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
  501. writel(val, base + USB_SUSP_CTRL);
  502. }
  503. val = readl(base + USB_SUSP_CTRL);
  504. val |= UTMIP_RESET;
  505. writel(val, base + USB_SUSP_CTRL);
  506. val = readl(base + UTMIP_BAT_CHRG_CFG0);
  507. val |= UTMIP_PD_CHRG;
  508. writel(val, base + UTMIP_BAT_CHRG_CFG0);
  509. val = readl(base + UTMIP_XCVR_CFG0);
  510. val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
  511. UTMIP_FORCE_PDZI_POWERDOWN;
  512. writel(val, base + UTMIP_XCVR_CFG0);
  513. val = readl(base + UTMIP_XCVR_CFG1);
  514. val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
  515. UTMIP_FORCE_PDDR_POWERDOWN;
  516. writel(val, base + UTMIP_XCVR_CFG1);
  517. return utmip_pad_power_off(phy);
  518. }
  519. static void utmi_phy_preresume(struct tegra_usb_phy *phy)
  520. {
  521. unsigned long val;
  522. void __iomem *base = phy->regs;
  523. val = readl(base + UTMIP_TX_CFG0);
  524. val |= UTMIP_HS_DISCON_DISABLE;
  525. writel(val, base + UTMIP_TX_CFG0);
  526. }
  527. static void utmi_phy_postresume(struct tegra_usb_phy *phy)
  528. {
  529. unsigned long val;
  530. void __iomem *base = phy->regs;
  531. val = readl(base + UTMIP_TX_CFG0);
  532. val &= ~UTMIP_HS_DISCON_DISABLE;
  533. writel(val, base + UTMIP_TX_CFG0);
  534. }
  535. static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
  536. enum tegra_usb_phy_port_speed port_speed)
  537. {
  538. unsigned long val;
  539. void __iomem *base = phy->regs;
  540. val = readl(base + UTMIP_MISC_CFG0);
  541. val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
  542. if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
  543. val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
  544. else
  545. val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
  546. writel(val, base + UTMIP_MISC_CFG0);
  547. udelay(1);
  548. val = readl(base + UTMIP_MISC_CFG0);
  549. val |= UTMIP_DPDM_OBSERVE;
  550. writel(val, base + UTMIP_MISC_CFG0);
  551. udelay(10);
  552. }
  553. static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
  554. {
  555. unsigned long val;
  556. void __iomem *base = phy->regs;
  557. val = readl(base + UTMIP_MISC_CFG0);
  558. val &= ~UTMIP_DPDM_OBSERVE;
  559. writel(val, base + UTMIP_MISC_CFG0);
  560. udelay(10);
  561. }
  562. static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
  563. {
  564. int ret;
  565. unsigned long val;
  566. void __iomem *base = phy->regs;
  567. ret = gpio_direction_output(phy->reset_gpio, 0);
  568. if (ret < 0) {
  569. dev_err(phy->u_phy.dev, "GPIO %d not set to 0: %d\n",
  570. phy->reset_gpio, ret);
  571. return ret;
  572. }
  573. msleep(5);
  574. ret = gpio_direction_output(phy->reset_gpio, 1);
  575. if (ret < 0) {
  576. dev_err(phy->u_phy.dev, "GPIO %d not set to 1: %d\n",
  577. phy->reset_gpio, ret);
  578. return ret;
  579. }
  580. clk_prepare_enable(phy->clk);
  581. msleep(1);
  582. val = readl(base + USB_SUSP_CTRL);
  583. val |= UHSIC_RESET;
  584. writel(val, base + USB_SUSP_CTRL);
  585. val = readl(base + ULPI_TIMING_CTRL_0);
  586. val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
  587. writel(val, base + ULPI_TIMING_CTRL_0);
  588. val = readl(base + USB_SUSP_CTRL);
  589. val |= ULPI_PHY_ENABLE;
  590. writel(val, base + USB_SUSP_CTRL);
  591. val = 0;
  592. writel(val, base + ULPI_TIMING_CTRL_1);
  593. val |= ULPI_DATA_TRIMMER_SEL(4);
  594. val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
  595. val |= ULPI_DIR_TRIMMER_SEL(4);
  596. writel(val, base + ULPI_TIMING_CTRL_1);
  597. udelay(10);
  598. val |= ULPI_DATA_TRIMMER_LOAD;
  599. val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
  600. val |= ULPI_DIR_TRIMMER_LOAD;
  601. writel(val, base + ULPI_TIMING_CTRL_1);
  602. /* Fix VbusInvalid due to floating VBUS */
  603. ret = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
  604. if (ret) {
  605. dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", ret);
  606. return ret;
  607. }
  608. ret = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
  609. if (ret) {
  610. dev_err(phy->u_phy.dev, "ULPI write failed: %d\n", ret);
  611. return ret;
  612. }
  613. val = readl(base + USB_SUSP_CTRL);
  614. val |= USB_SUSP_CLR;
  615. writel(val, base + USB_SUSP_CTRL);
  616. udelay(100);
  617. val = readl(base + USB_SUSP_CTRL);
  618. val &= ~USB_SUSP_CLR;
  619. writel(val, base + USB_SUSP_CTRL);
  620. return 0;
  621. }
  622. static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
  623. {
  624. clk_disable(phy->clk);
  625. return gpio_direction_output(phy->reset_gpio, 0);
  626. }
  627. static void tegra_usb_phy_close(struct tegra_usb_phy *phy)
  628. {
  629. if (!IS_ERR(phy->vbus))
  630. regulator_disable(phy->vbus);
  631. if (!phy->is_ulpi_phy)
  632. utmip_pad_close(phy);
  633. clk_disable_unprepare(phy->pll_u);
  634. }
  635. static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
  636. {
  637. if (phy->is_ulpi_phy)
  638. return ulpi_phy_power_on(phy);
  639. else
  640. return utmi_phy_power_on(phy);
  641. }
  642. static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
  643. {
  644. if (phy->is_ulpi_phy)
  645. return ulpi_phy_power_off(phy);
  646. else
  647. return utmi_phy_power_off(phy);
  648. }
  649. static int tegra_usb_phy_suspend(struct usb_phy *x, int suspend)
  650. {
  651. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  652. if (suspend)
  653. return tegra_usb_phy_power_off(phy);
  654. else
  655. return tegra_usb_phy_power_on(phy);
  656. }
  657. static int ulpi_open(struct tegra_usb_phy *phy)
  658. {
  659. int err;
  660. phy->clk = devm_clk_get(phy->u_phy.dev, "ulpi-link");
  661. if (IS_ERR(phy->clk)) {
  662. err = PTR_ERR(phy->clk);
  663. dev_err(phy->u_phy.dev, "Failed to get ULPI clock: %d\n", err);
  664. return err;
  665. }
  666. err = devm_gpio_request(phy->u_phy.dev, phy->reset_gpio,
  667. "ulpi_phy_reset_b");
  668. if (err < 0) {
  669. dev_err(phy->u_phy.dev, "Request failed for GPIO %d: %d\n",
  670. phy->reset_gpio, err);
  671. return err;
  672. }
  673. err = gpio_direction_output(phy->reset_gpio, 0);
  674. if (err < 0) {
  675. dev_err(phy->u_phy.dev,
  676. "GPIO %d direction not set to output: %d\n",
  677. phy->reset_gpio, err);
  678. return err;
  679. }
  680. phy->ulpi = otg_ulpi_create(&ulpi_viewport_access_ops, 0);
  681. if (!phy->ulpi) {
  682. dev_err(phy->u_phy.dev, "Failed to create ULPI OTG\n");
  683. err = -ENOMEM;
  684. return err;
  685. }
  686. phy->ulpi->io_priv = phy->regs + ULPI_VIEWPORT;
  687. return 0;
  688. }
  689. static int tegra_usb_phy_init(struct tegra_usb_phy *phy)
  690. {
  691. unsigned long parent_rate;
  692. int i;
  693. int err;
  694. phy->pll_u = devm_clk_get(phy->u_phy.dev, "pll_u");
  695. if (IS_ERR(phy->pll_u)) {
  696. err = PTR_ERR(phy->pll_u);
  697. dev_err(phy->u_phy.dev,
  698. "Failed to get pll_u clock: %d\n", err);
  699. return err;
  700. }
  701. err = clk_prepare_enable(phy->pll_u);
  702. if (err)
  703. return err;
  704. parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
  705. for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
  706. if (tegra_freq_table[i].freq == parent_rate) {
  707. phy->freq = &tegra_freq_table[i];
  708. break;
  709. }
  710. }
  711. if (!phy->freq) {
  712. dev_err(phy->u_phy.dev, "Invalid pll_u parent rate %ld\n",
  713. parent_rate);
  714. err = -EINVAL;
  715. goto fail;
  716. }
  717. if (!IS_ERR(phy->vbus)) {
  718. err = regulator_enable(phy->vbus);
  719. if (err) {
  720. dev_err(phy->u_phy.dev,
  721. "Failed to enable USB VBUS regulator: %d\n",
  722. err);
  723. goto fail;
  724. }
  725. }
  726. if (phy->is_ulpi_phy)
  727. err = ulpi_open(phy);
  728. else
  729. err = utmip_pad_open(phy);
  730. if (err < 0)
  731. goto fail;
  732. return 0;
  733. fail:
  734. clk_disable_unprepare(phy->pll_u);
  735. return err;
  736. }
  737. void tegra_usb_phy_preresume(struct usb_phy *x)
  738. {
  739. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  740. if (!phy->is_ulpi_phy)
  741. utmi_phy_preresume(phy);
  742. }
  743. EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
  744. void tegra_usb_phy_postresume(struct usb_phy *x)
  745. {
  746. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  747. if (!phy->is_ulpi_phy)
  748. utmi_phy_postresume(phy);
  749. }
  750. EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
  751. void tegra_ehci_phy_restore_start(struct usb_phy *x,
  752. enum tegra_usb_phy_port_speed port_speed)
  753. {
  754. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  755. if (!phy->is_ulpi_phy)
  756. utmi_phy_restore_start(phy, port_speed);
  757. }
  758. EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
  759. void tegra_ehci_phy_restore_end(struct usb_phy *x)
  760. {
  761. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  762. if (!phy->is_ulpi_phy)
  763. utmi_phy_restore_end(phy);
  764. }
  765. EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
  766. static int read_utmi_param(struct platform_device *pdev, const char *param,
  767. u8 *dest)
  768. {
  769. u32 value;
  770. int err = of_property_read_u32(pdev->dev.of_node, param, &value);
  771. *dest = (u8)value;
  772. if (err < 0)
  773. dev_err(&pdev->dev,
  774. "Failed to read USB UTMI parameter %s: %d\n",
  775. param, err);
  776. return err;
  777. }
  778. static int utmi_phy_probe(struct tegra_usb_phy *tegra_phy,
  779. struct platform_device *pdev)
  780. {
  781. struct resource *res;
  782. int err;
  783. struct tegra_utmip_config *config;
  784. tegra_phy->is_ulpi_phy = false;
  785. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  786. if (!res) {
  787. dev_err(&pdev->dev, "Failed to get UTMI pad regs\n");
  788. return -ENXIO;
  789. }
  790. tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
  791. resource_size(res));
  792. if (!tegra_phy->pad_regs) {
  793. dev_err(&pdev->dev, "Failed to remap UTMI pad regs\n");
  794. return -ENOMEM;
  795. }
  796. tegra_phy->config = devm_kzalloc(&pdev->dev, sizeof(*config),
  797. GFP_KERNEL);
  798. if (!tegra_phy->config)
  799. return -ENOMEM;
  800. config = tegra_phy->config;
  801. err = read_utmi_param(pdev, "nvidia,hssync-start-delay",
  802. &config->hssync_start_delay);
  803. if (err < 0)
  804. return err;
  805. err = read_utmi_param(pdev, "nvidia,elastic-limit",
  806. &config->elastic_limit);
  807. if (err < 0)
  808. return err;
  809. err = read_utmi_param(pdev, "nvidia,idle-wait-delay",
  810. &config->idle_wait_delay);
  811. if (err < 0)
  812. return err;
  813. err = read_utmi_param(pdev, "nvidia,term-range-adj",
  814. &config->term_range_adj);
  815. if (err < 0)
  816. return err;
  817. err = read_utmi_param(pdev, "nvidia,xcvr-lsfslew",
  818. &config->xcvr_lsfslew);
  819. if (err < 0)
  820. return err;
  821. err = read_utmi_param(pdev, "nvidia,xcvr-lsrslew",
  822. &config->xcvr_lsrslew);
  823. if (err < 0)
  824. return err;
  825. if (tegra_phy->soc_config->requires_extra_tuning_parameters) {
  826. err = read_utmi_param(pdev, "nvidia,xcvr-hsslew",
  827. &config->xcvr_hsslew);
  828. if (err < 0)
  829. return err;
  830. err = read_utmi_param(pdev, "nvidia,hssquelch-level",
  831. &config->hssquelch_level);
  832. if (err < 0)
  833. return err;
  834. err = read_utmi_param(pdev, "nvidia,hsdiscon-level",
  835. &config->hsdiscon_level);
  836. if (err < 0)
  837. return err;
  838. }
  839. config->xcvr_setup_use_fuses = of_property_read_bool(
  840. pdev->dev.of_node, "nvidia,xcvr-setup-use-fuses");
  841. if (!config->xcvr_setup_use_fuses) {
  842. err = read_utmi_param(pdev, "nvidia,xcvr-setup",
  843. &config->xcvr_setup);
  844. if (err < 0)
  845. return err;
  846. }
  847. return 0;
  848. }
  849. static const struct tegra_phy_soc_config tegra20_soc_config = {
  850. .utmi_pll_config_in_car_module = false,
  851. .has_hostpc = false,
  852. .requires_usbmode_setup = false,
  853. .requires_extra_tuning_parameters = false,
  854. };
  855. static const struct tegra_phy_soc_config tegra30_soc_config = {
  856. .utmi_pll_config_in_car_module = true,
  857. .has_hostpc = true,
  858. .requires_usbmode_setup = true,
  859. .requires_extra_tuning_parameters = true,
  860. };
  861. static const struct of_device_id tegra_usb_phy_id_table[] = {
  862. { .compatible = "nvidia,tegra30-usb-phy", .data = &tegra30_soc_config },
  863. { .compatible = "nvidia,tegra20-usb-phy", .data = &tegra20_soc_config },
  864. { },
  865. };
  866. MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
  867. static int tegra_usb_phy_probe(struct platform_device *pdev)
  868. {
  869. const struct of_device_id *match;
  870. struct resource *res;
  871. struct tegra_usb_phy *tegra_phy = NULL;
  872. struct device_node *np = pdev->dev.of_node;
  873. enum usb_phy_interface phy_type;
  874. int err;
  875. tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
  876. if (!tegra_phy)
  877. return -ENOMEM;
  878. match = of_match_device(tegra_usb_phy_id_table, &pdev->dev);
  879. if (!match) {
  880. dev_err(&pdev->dev, "Error: No device match found\n");
  881. return -ENODEV;
  882. }
  883. tegra_phy->soc_config = match->data;
  884. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  885. if (!res) {
  886. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  887. return -ENXIO;
  888. }
  889. tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
  890. resource_size(res));
  891. if (!tegra_phy->regs) {
  892. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  893. return -ENOMEM;
  894. }
  895. tegra_phy->is_legacy_phy =
  896. of_property_read_bool(np, "nvidia,has-legacy-mode");
  897. phy_type = of_usb_get_phy_mode(np);
  898. switch (phy_type) {
  899. case USBPHY_INTERFACE_MODE_UTMI:
  900. err = utmi_phy_probe(tegra_phy, pdev);
  901. if (err < 0)
  902. return err;
  903. break;
  904. case USBPHY_INTERFACE_MODE_ULPI:
  905. tegra_phy->is_ulpi_phy = true;
  906. tegra_phy->reset_gpio =
  907. of_get_named_gpio(np, "nvidia,phy-reset-gpio", 0);
  908. if (!gpio_is_valid(tegra_phy->reset_gpio)) {
  909. dev_err(&pdev->dev,
  910. "Invalid GPIO: %d\n", tegra_phy->reset_gpio);
  911. return tegra_phy->reset_gpio;
  912. }
  913. tegra_phy->config = NULL;
  914. break;
  915. default:
  916. dev_err(&pdev->dev, "phy_type %u is invalid or unsupported\n",
  917. phy_type);
  918. return -EINVAL;
  919. }
  920. if (of_find_property(np, "dr_mode", NULL))
  921. tegra_phy->mode = usb_get_dr_mode(&pdev->dev);
  922. else
  923. tegra_phy->mode = USB_DR_MODE_HOST;
  924. if (tegra_phy->mode == USB_DR_MODE_UNKNOWN) {
  925. dev_err(&pdev->dev, "dr_mode is invalid\n");
  926. return -EINVAL;
  927. }
  928. /* On some boards, the VBUS regulator doesn't need to be controlled */
  929. if (of_find_property(np, "vbus-supply", NULL)) {
  930. tegra_phy->vbus = devm_regulator_get(&pdev->dev, "vbus");
  931. if (IS_ERR(tegra_phy->vbus))
  932. return PTR_ERR(tegra_phy->vbus);
  933. } else {
  934. dev_notice(&pdev->dev, "no vbus regulator");
  935. tegra_phy->vbus = ERR_PTR(-ENODEV);
  936. }
  937. tegra_phy->u_phy.dev = &pdev->dev;
  938. err = tegra_usb_phy_init(tegra_phy);
  939. if (err < 0)
  940. return err;
  941. tegra_phy->u_phy.set_suspend = tegra_usb_phy_suspend;
  942. platform_set_drvdata(pdev, tegra_phy);
  943. err = usb_add_phy_dev(&tegra_phy->u_phy);
  944. if (err < 0) {
  945. tegra_usb_phy_close(tegra_phy);
  946. return err;
  947. }
  948. return 0;
  949. }
  950. static int tegra_usb_phy_remove(struct platform_device *pdev)
  951. {
  952. struct tegra_usb_phy *tegra_phy = platform_get_drvdata(pdev);
  953. usb_remove_phy(&tegra_phy->u_phy);
  954. tegra_usb_phy_close(tegra_phy);
  955. return 0;
  956. }
  957. static struct platform_driver tegra_usb_phy_driver = {
  958. .probe = tegra_usb_phy_probe,
  959. .remove = tegra_usb_phy_remove,
  960. .driver = {
  961. .name = "tegra-phy",
  962. .of_match_table = tegra_usb_phy_id_table,
  963. },
  964. };
  965. module_platform_driver(tegra_usb_phy_driver);
  966. MODULE_DESCRIPTION("Tegra USB PHY driver");
  967. MODULE_LICENSE("GPL v2");