phy-tegra-usb.c 23 KB

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