usb_phy.c 20 KB

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