usb_phy.c 20 KB

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