usb_phy.c 21 KB

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