tegra_usb_phy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * Copyright (C) 2010 Google, Inc.
  3. *
  4. * Author:
  5. * Erik Gilling <konkers@google.com>
  6. * Benoit Goby <benoit@android.com>
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/resource.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/export.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/gpio.h>
  26. #include <linux/of.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/usb/otg.h>
  29. #include <linux/usb/ulpi.h>
  30. #include <asm/mach-types.h>
  31. #include <linux/usb/tegra_usb_phy.h>
  32. #define TEGRA_USB_BASE 0xC5000000
  33. #define TEGRA_USB_SIZE SZ_16K
  34. #define ULPI_VIEWPORT 0x170
  35. #define USB_SUSP_CTRL 0x400
  36. #define USB_WAKE_ON_CNNT_EN_DEV (1 << 3)
  37. #define USB_WAKE_ON_DISCON_EN_DEV (1 << 4)
  38. #define USB_SUSP_CLR (1 << 5)
  39. #define USB_PHY_CLK_VALID (1 << 7)
  40. #define UTMIP_RESET (1 << 11)
  41. #define UHSIC_RESET (1 << 11)
  42. #define UTMIP_PHY_ENABLE (1 << 12)
  43. #define ULPI_PHY_ENABLE (1 << 13)
  44. #define USB_SUSP_SET (1 << 14)
  45. #define USB_WAKEUP_DEBOUNCE_COUNT(x) (((x) & 0x7) << 16)
  46. #define USB1_LEGACY_CTRL 0x410
  47. #define USB1_NO_LEGACY_MODE (1 << 0)
  48. #define USB1_VBUS_SENSE_CTL_MASK (3 << 1)
  49. #define USB1_VBUS_SENSE_CTL_VBUS_WAKEUP (0 << 1)
  50. #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD_OR_VBUS_WAKEUP \
  51. (1 << 1)
  52. #define USB1_VBUS_SENSE_CTL_AB_SESS_VLD (2 << 1)
  53. #define USB1_VBUS_SENSE_CTL_A_SESS_VLD (3 << 1)
  54. #define ULPI_TIMING_CTRL_0 0x424
  55. #define ULPI_OUTPUT_PINMUX_BYP (1 << 10)
  56. #define ULPI_CLKOUT_PINMUX_BYP (1 << 11)
  57. #define ULPI_TIMING_CTRL_1 0x428
  58. #define ULPI_DATA_TRIMMER_LOAD (1 << 0)
  59. #define ULPI_DATA_TRIMMER_SEL(x) (((x) & 0x7) << 1)
  60. #define ULPI_STPDIRNXT_TRIMMER_LOAD (1 << 16)
  61. #define ULPI_STPDIRNXT_TRIMMER_SEL(x) (((x) & 0x7) << 17)
  62. #define ULPI_DIR_TRIMMER_LOAD (1 << 24)
  63. #define ULPI_DIR_TRIMMER_SEL(x) (((x) & 0x7) << 25)
  64. #define UTMIP_PLL_CFG1 0x804
  65. #define UTMIP_XTAL_FREQ_COUNT(x) (((x) & 0xfff) << 0)
  66. #define UTMIP_PLLU_ENABLE_DLY_COUNT(x) (((x) & 0x1f) << 27)
  67. #define UTMIP_XCVR_CFG0 0x808
  68. #define UTMIP_XCVR_SETUP(x) (((x) & 0xf) << 0)
  69. #define UTMIP_XCVR_LSRSLEW(x) (((x) & 0x3) << 8)
  70. #define UTMIP_XCVR_LSFSLEW(x) (((x) & 0x3) << 10)
  71. #define UTMIP_FORCE_PD_POWERDOWN (1 << 14)
  72. #define UTMIP_FORCE_PD2_POWERDOWN (1 << 16)
  73. #define UTMIP_FORCE_PDZI_POWERDOWN (1 << 18)
  74. #define UTMIP_XCVR_HSSLEW_MSB(x) (((x) & 0x7f) << 25)
  75. #define UTMIP_BIAS_CFG0 0x80c
  76. #define UTMIP_OTGPD (1 << 11)
  77. #define UTMIP_BIASPD (1 << 10)
  78. #define UTMIP_HSRX_CFG0 0x810
  79. #define UTMIP_ELASTIC_LIMIT(x) (((x) & 0x1f) << 10)
  80. #define UTMIP_IDLE_WAIT(x) (((x) & 0x1f) << 15)
  81. #define UTMIP_HSRX_CFG1 0x814
  82. #define UTMIP_HS_SYNC_START_DLY(x) (((x) & 0x1f) << 1)
  83. #define UTMIP_TX_CFG0 0x820
  84. #define UTMIP_FS_PREABMLE_J (1 << 19)
  85. #define UTMIP_HS_DISCON_DISABLE (1 << 8)
  86. #define UTMIP_MISC_CFG0 0x824
  87. #define UTMIP_DPDM_OBSERVE (1 << 26)
  88. #define UTMIP_DPDM_OBSERVE_SEL(x) (((x) & 0xf) << 27)
  89. #define UTMIP_DPDM_OBSERVE_SEL_FS_J UTMIP_DPDM_OBSERVE_SEL(0xf)
  90. #define UTMIP_DPDM_OBSERVE_SEL_FS_K UTMIP_DPDM_OBSERVE_SEL(0xe)
  91. #define UTMIP_DPDM_OBSERVE_SEL_FS_SE1 UTMIP_DPDM_OBSERVE_SEL(0xd)
  92. #define UTMIP_DPDM_OBSERVE_SEL_FS_SE0 UTMIP_DPDM_OBSERVE_SEL(0xc)
  93. #define UTMIP_SUSPEND_EXIT_ON_EDGE (1 << 22)
  94. #define UTMIP_MISC_CFG1 0x828
  95. #define UTMIP_PLL_ACTIVE_DLY_COUNT(x) (((x) & 0x1f) << 18)
  96. #define UTMIP_PLLU_STABLE_COUNT(x) (((x) & 0xfff) << 6)
  97. #define UTMIP_DEBOUNCE_CFG0 0x82c
  98. #define UTMIP_BIAS_DEBOUNCE_A(x) (((x) & 0xffff) << 0)
  99. #define UTMIP_BAT_CHRG_CFG0 0x830
  100. #define UTMIP_PD_CHRG (1 << 0)
  101. #define UTMIP_SPARE_CFG0 0x834
  102. #define FUSE_SETUP_SEL (1 << 3)
  103. #define UTMIP_XCVR_CFG1 0x838
  104. #define UTMIP_FORCE_PDDISC_POWERDOWN (1 << 0)
  105. #define UTMIP_FORCE_PDCHRP_POWERDOWN (1 << 2)
  106. #define UTMIP_FORCE_PDDR_POWERDOWN (1 << 4)
  107. #define UTMIP_XCVR_TERM_RANGE_ADJ(x) (((x) & 0xf) << 18)
  108. #define UTMIP_BIAS_CFG1 0x83c
  109. #define UTMIP_BIAS_PDTRK_COUNT(x) (((x) & 0x1f) << 3)
  110. static DEFINE_SPINLOCK(utmip_pad_lock);
  111. static int utmip_pad_count;
  112. struct tegra_xtal_freq {
  113. int freq;
  114. u8 enable_delay;
  115. u8 stable_count;
  116. u8 active_delay;
  117. u8 xtal_freq_count;
  118. u16 debounce;
  119. };
  120. static const struct tegra_xtal_freq tegra_freq_table[] = {
  121. {
  122. .freq = 12000000,
  123. .enable_delay = 0x02,
  124. .stable_count = 0x2F,
  125. .active_delay = 0x04,
  126. .xtal_freq_count = 0x76,
  127. .debounce = 0x7530,
  128. },
  129. {
  130. .freq = 13000000,
  131. .enable_delay = 0x02,
  132. .stable_count = 0x33,
  133. .active_delay = 0x05,
  134. .xtal_freq_count = 0x7F,
  135. .debounce = 0x7EF4,
  136. },
  137. {
  138. .freq = 19200000,
  139. .enable_delay = 0x03,
  140. .stable_count = 0x4B,
  141. .active_delay = 0x06,
  142. .xtal_freq_count = 0xBB,
  143. .debounce = 0xBB80,
  144. },
  145. {
  146. .freq = 26000000,
  147. .enable_delay = 0x04,
  148. .stable_count = 0x66,
  149. .active_delay = 0x09,
  150. .xtal_freq_count = 0xFE,
  151. .debounce = 0xFDE8,
  152. },
  153. };
  154. static struct tegra_utmip_config utmip_default[] = {
  155. [0] = {
  156. .hssync_start_delay = 9,
  157. .idle_wait_delay = 17,
  158. .elastic_limit = 16,
  159. .term_range_adj = 6,
  160. .xcvr_setup = 9,
  161. .xcvr_lsfslew = 1,
  162. .xcvr_lsrslew = 1,
  163. },
  164. [2] = {
  165. .hssync_start_delay = 9,
  166. .idle_wait_delay = 17,
  167. .elastic_limit = 16,
  168. .term_range_adj = 6,
  169. .xcvr_setup = 9,
  170. .xcvr_lsfslew = 2,
  171. .xcvr_lsrslew = 2,
  172. },
  173. };
  174. static int utmip_pad_open(struct tegra_usb_phy *phy)
  175. {
  176. phy->pad_clk = clk_get_sys("utmip-pad", NULL);
  177. if (IS_ERR(phy->pad_clk)) {
  178. pr_err("%s: can't get utmip pad clock\n", __func__);
  179. return PTR_ERR(phy->pad_clk);
  180. }
  181. if (phy->is_legacy_phy) {
  182. phy->pad_regs = phy->regs;
  183. } else {
  184. phy->pad_regs = ioremap(TEGRA_USB_BASE, TEGRA_USB_SIZE);
  185. if (!phy->pad_regs) {
  186. pr_err("%s: can't remap usb registers\n", __func__);
  187. clk_put(phy->pad_clk);
  188. return -ENOMEM;
  189. }
  190. }
  191. return 0;
  192. }
  193. static void utmip_pad_close(struct tegra_usb_phy *phy)
  194. {
  195. if (!phy->is_legacy_phy)
  196. iounmap(phy->pad_regs);
  197. clk_put(phy->pad_clk);
  198. }
  199. static void utmip_pad_power_on(struct tegra_usb_phy *phy)
  200. {
  201. unsigned long val, flags;
  202. void __iomem *base = phy->pad_regs;
  203. clk_prepare_enable(phy->pad_clk);
  204. spin_lock_irqsave(&utmip_pad_lock, flags);
  205. if (utmip_pad_count++ == 0) {
  206. val = readl(base + UTMIP_BIAS_CFG0);
  207. val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
  208. writel(val, base + UTMIP_BIAS_CFG0);
  209. }
  210. spin_unlock_irqrestore(&utmip_pad_lock, flags);
  211. clk_disable_unprepare(phy->pad_clk);
  212. }
  213. static int utmip_pad_power_off(struct tegra_usb_phy *phy)
  214. {
  215. unsigned long val, flags;
  216. void __iomem *base = phy->pad_regs;
  217. if (!utmip_pad_count) {
  218. pr_err("%s: utmip pad already powered off\n", __func__);
  219. return -EINVAL;
  220. }
  221. clk_prepare_enable(phy->pad_clk);
  222. spin_lock_irqsave(&utmip_pad_lock, flags);
  223. if (--utmip_pad_count == 0) {
  224. val = readl(base + UTMIP_BIAS_CFG0);
  225. val |= UTMIP_OTGPD | UTMIP_BIASPD;
  226. writel(val, base + UTMIP_BIAS_CFG0);
  227. }
  228. spin_unlock_irqrestore(&utmip_pad_lock, flags);
  229. clk_disable_unprepare(phy->pad_clk);
  230. return 0;
  231. }
  232. static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
  233. {
  234. unsigned long timeout = 2000;
  235. do {
  236. if ((readl(reg) & mask) == result)
  237. return 0;
  238. udelay(1);
  239. timeout--;
  240. } while (timeout);
  241. return -1;
  242. }
  243. static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
  244. {
  245. unsigned long val;
  246. void __iomem *base = phy->regs;
  247. if (phy->is_legacy_phy) {
  248. val = readl(base + USB_SUSP_CTRL);
  249. val |= USB_SUSP_SET;
  250. writel(val, base + USB_SUSP_CTRL);
  251. udelay(10);
  252. val = readl(base + USB_SUSP_CTRL);
  253. val &= ~USB_SUSP_SET;
  254. writel(val, base + USB_SUSP_CTRL);
  255. } else
  256. tegra_ehci_set_phcd(&phy->u_phy, true);
  257. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) < 0)
  258. pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
  259. }
  260. static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
  261. {
  262. unsigned long val;
  263. void __iomem *base = phy->regs;
  264. if (phy->is_legacy_phy) {
  265. val = readl(base + USB_SUSP_CTRL);
  266. val |= USB_SUSP_CLR;
  267. writel(val, base + USB_SUSP_CTRL);
  268. udelay(10);
  269. val = readl(base + USB_SUSP_CTRL);
  270. val &= ~USB_SUSP_CLR;
  271. writel(val, base + USB_SUSP_CTRL);
  272. } else
  273. tegra_ehci_set_phcd(&phy->u_phy, false);
  274. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
  275. USB_PHY_CLK_VALID))
  276. pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
  277. }
  278. static int utmi_phy_power_on(struct tegra_usb_phy *phy)
  279. {
  280. unsigned long val;
  281. void __iomem *base = phy->regs;
  282. struct tegra_utmip_config *config = phy->config;
  283. val = readl(base + USB_SUSP_CTRL);
  284. val |= UTMIP_RESET;
  285. writel(val, base + USB_SUSP_CTRL);
  286. if (phy->is_legacy_phy) {
  287. val = readl(base + USB1_LEGACY_CTRL);
  288. val |= USB1_NO_LEGACY_MODE;
  289. writel(val, base + USB1_LEGACY_CTRL);
  290. }
  291. val = readl(base + UTMIP_TX_CFG0);
  292. val &= ~UTMIP_FS_PREABMLE_J;
  293. writel(val, base + UTMIP_TX_CFG0);
  294. val = readl(base + UTMIP_HSRX_CFG0);
  295. val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
  296. val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
  297. val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
  298. writel(val, base + UTMIP_HSRX_CFG0);
  299. val = readl(base + UTMIP_HSRX_CFG1);
  300. val &= ~UTMIP_HS_SYNC_START_DLY(~0);
  301. val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
  302. writel(val, base + UTMIP_HSRX_CFG1);
  303. val = readl(base + UTMIP_DEBOUNCE_CFG0);
  304. val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
  305. val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
  306. writel(val, base + UTMIP_DEBOUNCE_CFG0);
  307. val = readl(base + UTMIP_MISC_CFG0);
  308. val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
  309. writel(val, base + UTMIP_MISC_CFG0);
  310. val = readl(base + UTMIP_MISC_CFG1);
  311. val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) | UTMIP_PLLU_STABLE_COUNT(~0));
  312. val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
  313. UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
  314. writel(val, base + UTMIP_MISC_CFG1);
  315. val = readl(base + UTMIP_PLL_CFG1);
  316. val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) | UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
  317. val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
  318. UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
  319. writel(val, base + UTMIP_PLL_CFG1);
  320. if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE) {
  321. val = readl(base + USB_SUSP_CTRL);
  322. val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
  323. writel(val, base + USB_SUSP_CTRL);
  324. }
  325. utmip_pad_power_on(phy);
  326. val = readl(base + UTMIP_XCVR_CFG0);
  327. val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
  328. UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_SETUP(~0) |
  329. UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0) |
  330. UTMIP_XCVR_HSSLEW_MSB(~0));
  331. val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
  332. val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
  333. val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
  334. writel(val, base + UTMIP_XCVR_CFG0);
  335. val = readl(base + UTMIP_XCVR_CFG1);
  336. val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
  337. UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
  338. val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
  339. writel(val, base + UTMIP_XCVR_CFG1);
  340. val = readl(base + UTMIP_BAT_CHRG_CFG0);
  341. val &= ~UTMIP_PD_CHRG;
  342. writel(val, base + UTMIP_BAT_CHRG_CFG0);
  343. val = readl(base + UTMIP_BIAS_CFG1);
  344. val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
  345. val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
  346. writel(val, base + UTMIP_BIAS_CFG1);
  347. if (phy->is_legacy_phy) {
  348. val = readl(base + UTMIP_SPARE_CFG0);
  349. if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE)
  350. val &= ~FUSE_SETUP_SEL;
  351. else
  352. val |= FUSE_SETUP_SEL;
  353. writel(val, base + UTMIP_SPARE_CFG0);
  354. } else {
  355. val = readl(base + USB_SUSP_CTRL);
  356. val |= UTMIP_PHY_ENABLE;
  357. writel(val, base + USB_SUSP_CTRL);
  358. }
  359. val = readl(base + USB_SUSP_CTRL);
  360. val &= ~UTMIP_RESET;
  361. writel(val, base + USB_SUSP_CTRL);
  362. if (phy->is_legacy_phy) {
  363. val = readl(base + USB1_LEGACY_CTRL);
  364. val &= ~USB1_VBUS_SENSE_CTL_MASK;
  365. val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
  366. writel(val, base + USB1_LEGACY_CTRL);
  367. val = readl(base + USB_SUSP_CTRL);
  368. val &= ~USB_SUSP_SET;
  369. writel(val, base + USB_SUSP_CTRL);
  370. }
  371. utmi_phy_clk_enable(phy);
  372. if (!phy->is_legacy_phy)
  373. tegra_ehci_set_pts(&phy->u_phy, 0);
  374. return 0;
  375. }
  376. static int utmi_phy_power_off(struct tegra_usb_phy *phy)
  377. {
  378. unsigned long val;
  379. void __iomem *base = phy->regs;
  380. utmi_phy_clk_disable(phy);
  381. if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE) {
  382. val = readl(base + USB_SUSP_CTRL);
  383. val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
  384. val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
  385. writel(val, base + USB_SUSP_CTRL);
  386. }
  387. val = readl(base + USB_SUSP_CTRL);
  388. val |= UTMIP_RESET;
  389. writel(val, base + USB_SUSP_CTRL);
  390. val = readl(base + UTMIP_BAT_CHRG_CFG0);
  391. val |= UTMIP_PD_CHRG;
  392. writel(val, base + UTMIP_BAT_CHRG_CFG0);
  393. val = readl(base + UTMIP_XCVR_CFG0);
  394. val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
  395. UTMIP_FORCE_PDZI_POWERDOWN;
  396. writel(val, base + UTMIP_XCVR_CFG0);
  397. val = readl(base + UTMIP_XCVR_CFG1);
  398. val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
  399. UTMIP_FORCE_PDDR_POWERDOWN;
  400. writel(val, base + UTMIP_XCVR_CFG1);
  401. return utmip_pad_power_off(phy);
  402. }
  403. static void utmi_phy_preresume(struct tegra_usb_phy *phy)
  404. {
  405. unsigned long val;
  406. void __iomem *base = phy->regs;
  407. val = readl(base + UTMIP_TX_CFG0);
  408. val |= UTMIP_HS_DISCON_DISABLE;
  409. writel(val, base + UTMIP_TX_CFG0);
  410. }
  411. static void utmi_phy_postresume(struct tegra_usb_phy *phy)
  412. {
  413. unsigned long val;
  414. void __iomem *base = phy->regs;
  415. val = readl(base + UTMIP_TX_CFG0);
  416. val &= ~UTMIP_HS_DISCON_DISABLE;
  417. writel(val, base + UTMIP_TX_CFG0);
  418. }
  419. static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
  420. enum tegra_usb_phy_port_speed port_speed)
  421. {
  422. unsigned long val;
  423. void __iomem *base = phy->regs;
  424. val = readl(base + UTMIP_MISC_CFG0);
  425. val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
  426. if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
  427. val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
  428. else
  429. val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
  430. writel(val, base + UTMIP_MISC_CFG0);
  431. udelay(1);
  432. val = readl(base + UTMIP_MISC_CFG0);
  433. val |= UTMIP_DPDM_OBSERVE;
  434. writel(val, base + UTMIP_MISC_CFG0);
  435. udelay(10);
  436. }
  437. static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
  438. {
  439. unsigned long val;
  440. void __iomem *base = phy->regs;
  441. val = readl(base + UTMIP_MISC_CFG0);
  442. val &= ~UTMIP_DPDM_OBSERVE;
  443. writel(val, base + UTMIP_MISC_CFG0);
  444. udelay(10);
  445. }
  446. static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
  447. {
  448. int ret;
  449. unsigned long val;
  450. void __iomem *base = phy->regs;
  451. struct tegra_ulpi_config *config = phy->config;
  452. gpio_direction_output(config->reset_gpio, 0);
  453. msleep(5);
  454. gpio_direction_output(config->reset_gpio, 1);
  455. clk_prepare_enable(phy->clk);
  456. msleep(1);
  457. val = readl(base + USB_SUSP_CTRL);
  458. val |= UHSIC_RESET;
  459. writel(val, base + USB_SUSP_CTRL);
  460. val = readl(base + ULPI_TIMING_CTRL_0);
  461. val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
  462. writel(val, base + ULPI_TIMING_CTRL_0);
  463. val = readl(base + USB_SUSP_CTRL);
  464. val |= ULPI_PHY_ENABLE;
  465. writel(val, base + USB_SUSP_CTRL);
  466. val = 0;
  467. writel(val, base + ULPI_TIMING_CTRL_1);
  468. val |= ULPI_DATA_TRIMMER_SEL(4);
  469. val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
  470. val |= ULPI_DIR_TRIMMER_SEL(4);
  471. writel(val, base + ULPI_TIMING_CTRL_1);
  472. udelay(10);
  473. val |= ULPI_DATA_TRIMMER_LOAD;
  474. val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
  475. val |= ULPI_DIR_TRIMMER_LOAD;
  476. writel(val, base + ULPI_TIMING_CTRL_1);
  477. /* Fix VbusInvalid due to floating VBUS */
  478. ret = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
  479. if (ret) {
  480. pr_err("%s: ulpi write failed\n", __func__);
  481. return ret;
  482. }
  483. ret = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
  484. if (ret) {
  485. pr_err("%s: ulpi write failed\n", __func__);
  486. return ret;
  487. }
  488. val = readl(base + USB_SUSP_CTRL);
  489. val |= USB_SUSP_CLR;
  490. writel(val, base + USB_SUSP_CTRL);
  491. udelay(100);
  492. val = readl(base + USB_SUSP_CTRL);
  493. val &= ~USB_SUSP_CLR;
  494. writel(val, base + USB_SUSP_CTRL);
  495. return 0;
  496. }
  497. static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
  498. {
  499. struct tegra_ulpi_config *config = phy->config;
  500. clk_disable(phy->clk);
  501. return gpio_direction_output(config->reset_gpio, 0);
  502. }
  503. static int tegra_phy_init(struct usb_phy *x)
  504. {
  505. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  506. struct tegra_ulpi_config *ulpi_config;
  507. int err;
  508. if (phy->is_ulpi_phy) {
  509. ulpi_config = phy->config;
  510. phy->clk = clk_get_sys(NULL, ulpi_config->clk);
  511. if (IS_ERR(phy->clk)) {
  512. pr_err("%s: can't get ulpi clock\n", __func__);
  513. err = -ENXIO;
  514. goto err1;
  515. }
  516. if (!gpio_is_valid(ulpi_config->reset_gpio))
  517. ulpi_config->reset_gpio =
  518. of_get_named_gpio(phy->dev->of_node,
  519. "nvidia,phy-reset-gpio", 0);
  520. if (!gpio_is_valid(ulpi_config->reset_gpio)) {
  521. pr_err("%s: invalid reset gpio: %d\n", __func__,
  522. ulpi_config->reset_gpio);
  523. err = -EINVAL;
  524. goto err1;
  525. }
  526. gpio_request(ulpi_config->reset_gpio, "ulpi_phy_reset_b");
  527. gpio_direction_output(ulpi_config->reset_gpio, 0);
  528. phy->ulpi = otg_ulpi_create(&ulpi_viewport_access_ops, 0);
  529. phy->ulpi->io_priv = phy->regs + ULPI_VIEWPORT;
  530. } else {
  531. err = utmip_pad_open(phy);
  532. if (err < 0)
  533. goto err1;
  534. }
  535. return 0;
  536. err1:
  537. clk_disable_unprepare(phy->pll_u);
  538. clk_put(phy->pll_u);
  539. return err;
  540. }
  541. static void tegra_usb_phy_close(struct usb_phy *x)
  542. {
  543. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  544. if (phy->is_ulpi_phy)
  545. clk_put(phy->clk);
  546. else
  547. utmip_pad_close(phy);
  548. clk_disable_unprepare(phy->pll_u);
  549. clk_put(phy->pll_u);
  550. kfree(phy);
  551. }
  552. static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
  553. {
  554. if (phy->is_ulpi_phy)
  555. return ulpi_phy_power_on(phy);
  556. else
  557. return utmi_phy_power_on(phy);
  558. }
  559. static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
  560. {
  561. if (phy->is_ulpi_phy)
  562. return ulpi_phy_power_off(phy);
  563. else
  564. return utmi_phy_power_off(phy);
  565. }
  566. static int tegra_usb_phy_suspend(struct usb_phy *x, int suspend)
  567. {
  568. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  569. if (suspend)
  570. return tegra_usb_phy_power_off(phy);
  571. else
  572. return tegra_usb_phy_power_on(phy);
  573. }
  574. struct tegra_usb_phy *tegra_usb_phy_open(struct device *dev, int instance,
  575. void __iomem *regs, void *config, enum tegra_usb_phy_mode phy_mode)
  576. {
  577. struct tegra_usb_phy *phy;
  578. unsigned long parent_rate;
  579. int i;
  580. int err;
  581. struct device_node *np = dev->of_node;
  582. phy = kzalloc(sizeof(struct tegra_usb_phy), GFP_KERNEL);
  583. if (!phy)
  584. return ERR_PTR(-ENOMEM);
  585. phy->instance = instance;
  586. phy->regs = regs;
  587. phy->config = config;
  588. phy->mode = phy_mode;
  589. phy->dev = dev;
  590. phy->is_legacy_phy =
  591. of_property_read_bool(np, "nvidia,has-legacy-mode");
  592. err = of_property_match_string(np, "phy_type", "ulpi");
  593. if (err < 0)
  594. phy->is_ulpi_phy = false;
  595. else
  596. phy->is_ulpi_phy = true;
  597. if (!phy->config) {
  598. if (phy->is_ulpi_phy) {
  599. pr_err("%s: ulpi phy configuration missing", __func__);
  600. err = -EINVAL;
  601. goto err0;
  602. } else {
  603. phy->config = &utmip_default[instance];
  604. }
  605. }
  606. phy->pll_u = clk_get_sys(NULL, "pll_u");
  607. if (IS_ERR(phy->pll_u)) {
  608. pr_err("Can't get pll_u clock\n");
  609. err = PTR_ERR(phy->pll_u);
  610. goto err0;
  611. }
  612. clk_prepare_enable(phy->pll_u);
  613. parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
  614. for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
  615. if (tegra_freq_table[i].freq == parent_rate) {
  616. phy->freq = &tegra_freq_table[i];
  617. break;
  618. }
  619. }
  620. if (!phy->freq) {
  621. pr_err("invalid pll_u parent rate %ld\n", parent_rate);
  622. err = -EINVAL;
  623. goto err1;
  624. }
  625. phy->u_phy.init = tegra_phy_init;
  626. phy->u_phy.shutdown = tegra_usb_phy_close;
  627. phy->u_phy.set_suspend = tegra_usb_phy_suspend;
  628. return phy;
  629. err1:
  630. clk_disable_unprepare(phy->pll_u);
  631. clk_put(phy->pll_u);
  632. err0:
  633. kfree(phy);
  634. return ERR_PTR(err);
  635. }
  636. EXPORT_SYMBOL_GPL(tegra_usb_phy_open);
  637. void tegra_usb_phy_preresume(struct usb_phy *x)
  638. {
  639. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  640. if (!phy->is_ulpi_phy)
  641. utmi_phy_preresume(phy);
  642. }
  643. EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
  644. void tegra_usb_phy_postresume(struct usb_phy *x)
  645. {
  646. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  647. if (!phy->is_ulpi_phy)
  648. utmi_phy_postresume(phy);
  649. }
  650. EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
  651. void tegra_ehci_phy_restore_start(struct usb_phy *x,
  652. enum tegra_usb_phy_port_speed port_speed)
  653. {
  654. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  655. if (!phy->is_ulpi_phy)
  656. utmi_phy_restore_start(phy, port_speed);
  657. }
  658. EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
  659. void tegra_ehci_phy_restore_end(struct usb_phy *x)
  660. {
  661. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  662. if (!phy->is_ulpi_phy)
  663. utmi_phy_restore_end(phy);
  664. }
  665. EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);