phy-tegra-usb.c 27 KB

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