phy-tegra-usb.c 22 KB

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