phy-tegra-usb.c 23 KB

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