tegra_usb_phy.c 21 KB

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