phy-tegra-usb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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/regulator/consumer.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 void set_pts(struct tegra_usb_phy *phy, u8 pts_val)
  164. {
  165. void __iomem *base = phy->regs;
  166. unsigned long val;
  167. val = readl(base + TEGRA_USB_PORTSC1) & ~TEGRA_PORTSC1_RWC_BITS;
  168. val &= ~TEGRA_USB_PORTSC1_PTS(3);
  169. val |= TEGRA_USB_PORTSC1_PTS(pts_val & 3);
  170. writel(val, base + TEGRA_USB_PORTSC1);
  171. }
  172. static void set_phcd(struct tegra_usb_phy *phy, bool enable)
  173. {
  174. void __iomem *base = phy->regs;
  175. unsigned long val;
  176. val = readl(base + TEGRA_USB_PORTSC1) & ~TEGRA_PORTSC1_RWC_BITS;
  177. if (enable)
  178. val |= TEGRA_USB_PORTSC1_PHCD;
  179. else
  180. val &= ~TEGRA_USB_PORTSC1_PHCD;
  181. writel(val, base + TEGRA_USB_PORTSC1);
  182. }
  183. static int utmip_pad_open(struct tegra_usb_phy *phy)
  184. {
  185. phy->pad_clk = devm_clk_get(phy->u_phy.dev, "utmi-pads");
  186. if (IS_ERR(phy->pad_clk)) {
  187. pr_err("%s: can't get utmip pad clock\n", __func__);
  188. return PTR_ERR(phy->pad_clk);
  189. }
  190. return 0;
  191. }
  192. static void utmip_pad_power_on(struct tegra_usb_phy *phy)
  193. {
  194. unsigned long val, flags;
  195. void __iomem *base = phy->pad_regs;
  196. clk_prepare_enable(phy->pad_clk);
  197. spin_lock_irqsave(&utmip_pad_lock, flags);
  198. if (utmip_pad_count++ == 0) {
  199. val = readl(base + UTMIP_BIAS_CFG0);
  200. val &= ~(UTMIP_OTGPD | UTMIP_BIASPD);
  201. writel(val, base + UTMIP_BIAS_CFG0);
  202. }
  203. spin_unlock_irqrestore(&utmip_pad_lock, flags);
  204. clk_disable_unprepare(phy->pad_clk);
  205. }
  206. static int utmip_pad_power_off(struct tegra_usb_phy *phy)
  207. {
  208. unsigned long val, flags;
  209. void __iomem *base = phy->pad_regs;
  210. if (!utmip_pad_count) {
  211. pr_err("%s: utmip pad already powered off\n", __func__);
  212. return -EINVAL;
  213. }
  214. clk_prepare_enable(phy->pad_clk);
  215. spin_lock_irqsave(&utmip_pad_lock, flags);
  216. if (--utmip_pad_count == 0) {
  217. val = readl(base + UTMIP_BIAS_CFG0);
  218. val |= UTMIP_OTGPD | UTMIP_BIASPD;
  219. writel(val, base + UTMIP_BIAS_CFG0);
  220. }
  221. spin_unlock_irqrestore(&utmip_pad_lock, flags);
  222. clk_disable_unprepare(phy->pad_clk);
  223. return 0;
  224. }
  225. static int utmi_wait_register(void __iomem *reg, u32 mask, u32 result)
  226. {
  227. unsigned long timeout = 2000;
  228. do {
  229. if ((readl(reg) & mask) == result)
  230. return 0;
  231. udelay(1);
  232. timeout--;
  233. } while (timeout);
  234. return -1;
  235. }
  236. static void utmi_phy_clk_disable(struct tegra_usb_phy *phy)
  237. {
  238. unsigned long val;
  239. void __iomem *base = phy->regs;
  240. if (phy->is_legacy_phy) {
  241. val = readl(base + USB_SUSP_CTRL);
  242. val |= USB_SUSP_SET;
  243. writel(val, base + USB_SUSP_CTRL);
  244. udelay(10);
  245. val = readl(base + USB_SUSP_CTRL);
  246. val &= ~USB_SUSP_SET;
  247. writel(val, base + USB_SUSP_CTRL);
  248. } else
  249. set_phcd(phy, true);
  250. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID, 0) < 0)
  251. pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
  252. }
  253. static void utmi_phy_clk_enable(struct tegra_usb_phy *phy)
  254. {
  255. unsigned long val;
  256. void __iomem *base = phy->regs;
  257. if (phy->is_legacy_phy) {
  258. val = readl(base + USB_SUSP_CTRL);
  259. val |= USB_SUSP_CLR;
  260. writel(val, base + USB_SUSP_CTRL);
  261. udelay(10);
  262. val = readl(base + USB_SUSP_CTRL);
  263. val &= ~USB_SUSP_CLR;
  264. writel(val, base + USB_SUSP_CTRL);
  265. } else
  266. set_phcd(phy, false);
  267. if (utmi_wait_register(base + USB_SUSP_CTRL, USB_PHY_CLK_VALID,
  268. USB_PHY_CLK_VALID))
  269. pr_err("%s: timeout waiting for phy to stabilize\n", __func__);
  270. }
  271. static int utmi_phy_power_on(struct tegra_usb_phy *phy)
  272. {
  273. unsigned long val;
  274. void __iomem *base = phy->regs;
  275. struct tegra_utmip_config *config = phy->config;
  276. val = readl(base + USB_SUSP_CTRL);
  277. val |= UTMIP_RESET;
  278. writel(val, base + USB_SUSP_CTRL);
  279. if (phy->is_legacy_phy) {
  280. val = readl(base + USB1_LEGACY_CTRL);
  281. val |= USB1_NO_LEGACY_MODE;
  282. writel(val, base + USB1_LEGACY_CTRL);
  283. }
  284. val = readl(base + UTMIP_TX_CFG0);
  285. val &= ~UTMIP_FS_PREABMLE_J;
  286. writel(val, base + UTMIP_TX_CFG0);
  287. val = readl(base + UTMIP_HSRX_CFG0);
  288. val &= ~(UTMIP_IDLE_WAIT(~0) | UTMIP_ELASTIC_LIMIT(~0));
  289. val |= UTMIP_IDLE_WAIT(config->idle_wait_delay);
  290. val |= UTMIP_ELASTIC_LIMIT(config->elastic_limit);
  291. writel(val, base + UTMIP_HSRX_CFG0);
  292. val = readl(base + UTMIP_HSRX_CFG1);
  293. val &= ~UTMIP_HS_SYNC_START_DLY(~0);
  294. val |= UTMIP_HS_SYNC_START_DLY(config->hssync_start_delay);
  295. writel(val, base + UTMIP_HSRX_CFG1);
  296. val = readl(base + UTMIP_DEBOUNCE_CFG0);
  297. val &= ~UTMIP_BIAS_DEBOUNCE_A(~0);
  298. val |= UTMIP_BIAS_DEBOUNCE_A(phy->freq->debounce);
  299. writel(val, base + UTMIP_DEBOUNCE_CFG0);
  300. val = readl(base + UTMIP_MISC_CFG0);
  301. val &= ~UTMIP_SUSPEND_EXIT_ON_EDGE;
  302. writel(val, base + UTMIP_MISC_CFG0);
  303. val = readl(base + UTMIP_MISC_CFG1);
  304. val &= ~(UTMIP_PLL_ACTIVE_DLY_COUNT(~0) | UTMIP_PLLU_STABLE_COUNT(~0));
  305. val |= UTMIP_PLL_ACTIVE_DLY_COUNT(phy->freq->active_delay) |
  306. UTMIP_PLLU_STABLE_COUNT(phy->freq->stable_count);
  307. writel(val, base + UTMIP_MISC_CFG1);
  308. val = readl(base + UTMIP_PLL_CFG1);
  309. val &= ~(UTMIP_XTAL_FREQ_COUNT(~0) | UTMIP_PLLU_ENABLE_DLY_COUNT(~0));
  310. val |= UTMIP_XTAL_FREQ_COUNT(phy->freq->xtal_freq_count) |
  311. UTMIP_PLLU_ENABLE_DLY_COUNT(phy->freq->enable_delay);
  312. writel(val, base + UTMIP_PLL_CFG1);
  313. if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE) {
  314. val = readl(base + USB_SUSP_CTRL);
  315. val &= ~(USB_WAKE_ON_CNNT_EN_DEV | USB_WAKE_ON_DISCON_EN_DEV);
  316. writel(val, base + USB_SUSP_CTRL);
  317. }
  318. utmip_pad_power_on(phy);
  319. val = readl(base + UTMIP_XCVR_CFG0);
  320. val &= ~(UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
  321. UTMIP_FORCE_PDZI_POWERDOWN | UTMIP_XCVR_SETUP(~0) |
  322. UTMIP_XCVR_LSFSLEW(~0) | UTMIP_XCVR_LSRSLEW(~0) |
  323. UTMIP_XCVR_HSSLEW_MSB(~0));
  324. val |= UTMIP_XCVR_SETUP(config->xcvr_setup);
  325. val |= UTMIP_XCVR_LSFSLEW(config->xcvr_lsfslew);
  326. val |= UTMIP_XCVR_LSRSLEW(config->xcvr_lsrslew);
  327. writel(val, base + UTMIP_XCVR_CFG0);
  328. val = readl(base + UTMIP_XCVR_CFG1);
  329. val &= ~(UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
  330. UTMIP_FORCE_PDDR_POWERDOWN | UTMIP_XCVR_TERM_RANGE_ADJ(~0));
  331. val |= UTMIP_XCVR_TERM_RANGE_ADJ(config->term_range_adj);
  332. writel(val, base + UTMIP_XCVR_CFG1);
  333. val = readl(base + UTMIP_BAT_CHRG_CFG0);
  334. val &= ~UTMIP_PD_CHRG;
  335. writel(val, base + UTMIP_BAT_CHRG_CFG0);
  336. val = readl(base + UTMIP_BIAS_CFG1);
  337. val &= ~UTMIP_BIAS_PDTRK_COUNT(~0);
  338. val |= UTMIP_BIAS_PDTRK_COUNT(0x5);
  339. writel(val, base + UTMIP_BIAS_CFG1);
  340. if (phy->is_legacy_phy) {
  341. val = readl(base + UTMIP_SPARE_CFG0);
  342. if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE)
  343. val &= ~FUSE_SETUP_SEL;
  344. else
  345. val |= FUSE_SETUP_SEL;
  346. writel(val, base + UTMIP_SPARE_CFG0);
  347. } else {
  348. val = readl(base + USB_SUSP_CTRL);
  349. val |= UTMIP_PHY_ENABLE;
  350. writel(val, base + USB_SUSP_CTRL);
  351. }
  352. val = readl(base + USB_SUSP_CTRL);
  353. val &= ~UTMIP_RESET;
  354. writel(val, base + USB_SUSP_CTRL);
  355. if (phy->is_legacy_phy) {
  356. val = readl(base + USB1_LEGACY_CTRL);
  357. val &= ~USB1_VBUS_SENSE_CTL_MASK;
  358. val |= USB1_VBUS_SENSE_CTL_A_SESS_VLD;
  359. writel(val, base + USB1_LEGACY_CTRL);
  360. val = readl(base + USB_SUSP_CTRL);
  361. val &= ~USB_SUSP_SET;
  362. writel(val, base + USB_SUSP_CTRL);
  363. }
  364. utmi_phy_clk_enable(phy);
  365. if (!phy->is_legacy_phy)
  366. set_pts(phy, 0);
  367. return 0;
  368. }
  369. static int utmi_phy_power_off(struct tegra_usb_phy *phy)
  370. {
  371. unsigned long val;
  372. void __iomem *base = phy->regs;
  373. utmi_phy_clk_disable(phy);
  374. if (phy->mode == TEGRA_USB_PHY_MODE_DEVICE) {
  375. val = readl(base + USB_SUSP_CTRL);
  376. val &= ~USB_WAKEUP_DEBOUNCE_COUNT(~0);
  377. val |= USB_WAKE_ON_CNNT_EN_DEV | USB_WAKEUP_DEBOUNCE_COUNT(5);
  378. writel(val, base + USB_SUSP_CTRL);
  379. }
  380. val = readl(base + USB_SUSP_CTRL);
  381. val |= UTMIP_RESET;
  382. writel(val, base + USB_SUSP_CTRL);
  383. val = readl(base + UTMIP_BAT_CHRG_CFG0);
  384. val |= UTMIP_PD_CHRG;
  385. writel(val, base + UTMIP_BAT_CHRG_CFG0);
  386. val = readl(base + UTMIP_XCVR_CFG0);
  387. val |= UTMIP_FORCE_PD_POWERDOWN | UTMIP_FORCE_PD2_POWERDOWN |
  388. UTMIP_FORCE_PDZI_POWERDOWN;
  389. writel(val, base + UTMIP_XCVR_CFG0);
  390. val = readl(base + UTMIP_XCVR_CFG1);
  391. val |= UTMIP_FORCE_PDDISC_POWERDOWN | UTMIP_FORCE_PDCHRP_POWERDOWN |
  392. UTMIP_FORCE_PDDR_POWERDOWN;
  393. writel(val, base + UTMIP_XCVR_CFG1);
  394. return utmip_pad_power_off(phy);
  395. }
  396. static void utmi_phy_preresume(struct tegra_usb_phy *phy)
  397. {
  398. unsigned long val;
  399. void __iomem *base = phy->regs;
  400. val = readl(base + UTMIP_TX_CFG0);
  401. val |= UTMIP_HS_DISCON_DISABLE;
  402. writel(val, base + UTMIP_TX_CFG0);
  403. }
  404. static void utmi_phy_postresume(struct tegra_usb_phy *phy)
  405. {
  406. unsigned long val;
  407. void __iomem *base = phy->regs;
  408. val = readl(base + UTMIP_TX_CFG0);
  409. val &= ~UTMIP_HS_DISCON_DISABLE;
  410. writel(val, base + UTMIP_TX_CFG0);
  411. }
  412. static void utmi_phy_restore_start(struct tegra_usb_phy *phy,
  413. enum tegra_usb_phy_port_speed port_speed)
  414. {
  415. unsigned long val;
  416. void __iomem *base = phy->regs;
  417. val = readl(base + UTMIP_MISC_CFG0);
  418. val &= ~UTMIP_DPDM_OBSERVE_SEL(~0);
  419. if (port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
  420. val |= UTMIP_DPDM_OBSERVE_SEL_FS_K;
  421. else
  422. val |= UTMIP_DPDM_OBSERVE_SEL_FS_J;
  423. writel(val, base + UTMIP_MISC_CFG0);
  424. udelay(1);
  425. val = readl(base + UTMIP_MISC_CFG0);
  426. val |= UTMIP_DPDM_OBSERVE;
  427. writel(val, base + UTMIP_MISC_CFG0);
  428. udelay(10);
  429. }
  430. static void utmi_phy_restore_end(struct tegra_usb_phy *phy)
  431. {
  432. unsigned long val;
  433. void __iomem *base = phy->regs;
  434. val = readl(base + UTMIP_MISC_CFG0);
  435. val &= ~UTMIP_DPDM_OBSERVE;
  436. writel(val, base + UTMIP_MISC_CFG0);
  437. udelay(10);
  438. }
  439. static int ulpi_phy_power_on(struct tegra_usb_phy *phy)
  440. {
  441. int ret;
  442. unsigned long val;
  443. void __iomem *base = phy->regs;
  444. ret = gpio_direction_output(phy->reset_gpio, 0);
  445. if (ret < 0) {
  446. dev_err(phy->u_phy.dev, "gpio %d not set to 0\n",
  447. phy->reset_gpio);
  448. return ret;
  449. }
  450. msleep(5);
  451. ret = gpio_direction_output(phy->reset_gpio, 1);
  452. if (ret < 0) {
  453. dev_err(phy->u_phy.dev, "gpio %d not set to 1\n",
  454. phy->reset_gpio);
  455. return ret;
  456. }
  457. clk_prepare_enable(phy->clk);
  458. msleep(1);
  459. val = readl(base + USB_SUSP_CTRL);
  460. val |= UHSIC_RESET;
  461. writel(val, base + USB_SUSP_CTRL);
  462. val = readl(base + ULPI_TIMING_CTRL_0);
  463. val |= ULPI_OUTPUT_PINMUX_BYP | ULPI_CLKOUT_PINMUX_BYP;
  464. writel(val, base + ULPI_TIMING_CTRL_0);
  465. val = readl(base + USB_SUSP_CTRL);
  466. val |= ULPI_PHY_ENABLE;
  467. writel(val, base + USB_SUSP_CTRL);
  468. val = 0;
  469. writel(val, base + ULPI_TIMING_CTRL_1);
  470. val |= ULPI_DATA_TRIMMER_SEL(4);
  471. val |= ULPI_STPDIRNXT_TRIMMER_SEL(4);
  472. val |= ULPI_DIR_TRIMMER_SEL(4);
  473. writel(val, base + ULPI_TIMING_CTRL_1);
  474. udelay(10);
  475. val |= ULPI_DATA_TRIMMER_LOAD;
  476. val |= ULPI_STPDIRNXT_TRIMMER_LOAD;
  477. val |= ULPI_DIR_TRIMMER_LOAD;
  478. writel(val, base + ULPI_TIMING_CTRL_1);
  479. /* Fix VbusInvalid due to floating VBUS */
  480. ret = usb_phy_io_write(phy->ulpi, 0x40, 0x08);
  481. if (ret) {
  482. pr_err("%s: ulpi write failed\n", __func__);
  483. return ret;
  484. }
  485. ret = usb_phy_io_write(phy->ulpi, 0x80, 0x0B);
  486. if (ret) {
  487. pr_err("%s: ulpi write failed\n", __func__);
  488. return ret;
  489. }
  490. val = readl(base + USB_SUSP_CTRL);
  491. val |= USB_SUSP_CLR;
  492. writel(val, base + USB_SUSP_CTRL);
  493. udelay(100);
  494. val = readl(base + USB_SUSP_CTRL);
  495. val &= ~USB_SUSP_CLR;
  496. writel(val, base + USB_SUSP_CTRL);
  497. return 0;
  498. }
  499. static int ulpi_phy_power_off(struct tegra_usb_phy *phy)
  500. {
  501. clk_disable(phy->clk);
  502. return gpio_direction_output(phy->reset_gpio, 0);
  503. }
  504. static void tegra_usb_phy_close(struct usb_phy *x)
  505. {
  506. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  507. if (!IS_ERR(phy->vbus))
  508. regulator_disable(phy->vbus);
  509. clk_disable_unprepare(phy->pll_u);
  510. }
  511. static int tegra_usb_phy_power_on(struct tegra_usb_phy *phy)
  512. {
  513. if (phy->is_ulpi_phy)
  514. return ulpi_phy_power_on(phy);
  515. else
  516. return utmi_phy_power_on(phy);
  517. }
  518. static int tegra_usb_phy_power_off(struct tegra_usb_phy *phy)
  519. {
  520. if (phy->is_ulpi_phy)
  521. return ulpi_phy_power_off(phy);
  522. else
  523. return utmi_phy_power_off(phy);
  524. }
  525. static int tegra_usb_phy_suspend(struct usb_phy *x, int suspend)
  526. {
  527. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  528. if (suspend)
  529. return tegra_usb_phy_power_off(phy);
  530. else
  531. return tegra_usb_phy_power_on(phy);
  532. }
  533. static int ulpi_open(struct tegra_usb_phy *phy)
  534. {
  535. int err;
  536. phy->clk = devm_clk_get(phy->u_phy.dev, "ulpi-link");
  537. if (IS_ERR(phy->clk)) {
  538. pr_err("%s: can't get ulpi clock\n", __func__);
  539. return PTR_ERR(phy->clk);
  540. }
  541. err = devm_gpio_request(phy->u_phy.dev, phy->reset_gpio,
  542. "ulpi_phy_reset_b");
  543. if (err < 0) {
  544. dev_err(phy->u_phy.dev, "request failed for gpio: %d\n",
  545. phy->reset_gpio);
  546. return err;
  547. }
  548. err = gpio_direction_output(phy->reset_gpio, 0);
  549. if (err < 0) {
  550. dev_err(phy->u_phy.dev, "gpio %d direction not set to output\n",
  551. phy->reset_gpio);
  552. return err;
  553. }
  554. phy->ulpi = otg_ulpi_create(&ulpi_viewport_access_ops, 0);
  555. if (!phy->ulpi) {
  556. dev_err(phy->u_phy.dev, "otg_ulpi_create returned NULL\n");
  557. err = -ENOMEM;
  558. return err;
  559. }
  560. phy->ulpi->io_priv = phy->regs + ULPI_VIEWPORT;
  561. return 0;
  562. }
  563. static int tegra_usb_phy_init(struct tegra_usb_phy *phy)
  564. {
  565. unsigned long parent_rate;
  566. int i;
  567. int err;
  568. phy->pll_u = devm_clk_get(phy->u_phy.dev, "pll_u");
  569. if (IS_ERR(phy->pll_u)) {
  570. pr_err("Can't get pll_u clock\n");
  571. return PTR_ERR(phy->pll_u);
  572. }
  573. err = clk_prepare_enable(phy->pll_u);
  574. if (err)
  575. return err;
  576. parent_rate = clk_get_rate(clk_get_parent(phy->pll_u));
  577. for (i = 0; i < ARRAY_SIZE(tegra_freq_table); i++) {
  578. if (tegra_freq_table[i].freq == parent_rate) {
  579. phy->freq = &tegra_freq_table[i];
  580. break;
  581. }
  582. }
  583. if (!phy->freq) {
  584. pr_err("invalid pll_u parent rate %ld\n", parent_rate);
  585. err = -EINVAL;
  586. goto fail;
  587. }
  588. if (!IS_ERR(phy->vbus)) {
  589. err = regulator_enable(phy->vbus);
  590. if (err) {
  591. dev_err(phy->u_phy.dev,
  592. "failed to enable usb vbus regulator: %d\n",
  593. err);
  594. goto fail;
  595. }
  596. }
  597. if (phy->is_ulpi_phy)
  598. err = ulpi_open(phy);
  599. else
  600. err = utmip_pad_open(phy);
  601. if (err < 0)
  602. goto fail;
  603. return 0;
  604. fail:
  605. clk_disable_unprepare(phy->pll_u);
  606. return err;
  607. }
  608. void tegra_usb_phy_preresume(struct usb_phy *x)
  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_preresume(phy);
  613. }
  614. EXPORT_SYMBOL_GPL(tegra_usb_phy_preresume);
  615. void tegra_usb_phy_postresume(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_postresume(phy);
  620. }
  621. EXPORT_SYMBOL_GPL(tegra_usb_phy_postresume);
  622. void tegra_ehci_phy_restore_start(struct usb_phy *x,
  623. enum tegra_usb_phy_port_speed port_speed)
  624. {
  625. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  626. if (!phy->is_ulpi_phy)
  627. utmi_phy_restore_start(phy, port_speed);
  628. }
  629. EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_start);
  630. void tegra_ehci_phy_restore_end(struct usb_phy *x)
  631. {
  632. struct tegra_usb_phy *phy = container_of(x, struct tegra_usb_phy, u_phy);
  633. if (!phy->is_ulpi_phy)
  634. utmi_phy_restore_end(phy);
  635. }
  636. EXPORT_SYMBOL_GPL(tegra_ehci_phy_restore_end);
  637. static int read_utmi_param(struct platform_device *pdev, const char *param,
  638. u8 *dest)
  639. {
  640. u32 value;
  641. int err = of_property_read_u32(pdev->dev.of_node, param, &value);
  642. *dest = (u8)value;
  643. if (err < 0)
  644. dev_err(&pdev->dev, "Failed to read USB UTMI parameter %s: %d\n",
  645. param, err);
  646. return err;
  647. }
  648. static int utmi_phy_probe(struct tegra_usb_phy *tegra_phy,
  649. struct platform_device *pdev)
  650. {
  651. struct resource *res;
  652. int err;
  653. struct tegra_utmip_config *config;
  654. tegra_phy->is_ulpi_phy = false;
  655. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  656. if (!res) {
  657. dev_err(&pdev->dev, "Failed to get UTMI Pad regs\n");
  658. return -ENXIO;
  659. }
  660. tegra_phy->pad_regs = devm_ioremap(&pdev->dev, res->start,
  661. resource_size(res));
  662. if (!tegra_phy->regs) {
  663. dev_err(&pdev->dev, "Failed to remap UTMI Pad regs\n");
  664. return -ENOMEM;
  665. }
  666. tegra_phy->config = devm_kzalloc(&pdev->dev,
  667. sizeof(*tegra_phy->config), GFP_KERNEL);
  668. if (!tegra_phy->config) {
  669. dev_err(&pdev->dev,
  670. "unable to allocate memory for USB UTMIP config\n");
  671. return -ENOMEM;
  672. }
  673. config = tegra_phy->config;
  674. err = read_utmi_param(pdev, "nvidia,hssync-start-delay",
  675. &config->hssync_start_delay);
  676. if (err < 0)
  677. return err;
  678. err = read_utmi_param(pdev, "nvidia,elastic-limit",
  679. &config->elastic_limit);
  680. if (err < 0)
  681. return err;
  682. err = read_utmi_param(pdev, "nvidia,idle-wait-delay",
  683. &config->idle_wait_delay);
  684. if (err < 0)
  685. return err;
  686. err = read_utmi_param(pdev, "nvidia,term-range-adj",
  687. &config->term_range_adj);
  688. if (err < 0)
  689. return err;
  690. err = read_utmi_param(pdev, "nvidia,xcvr-setup",
  691. &config->xcvr_setup);
  692. if (err < 0)
  693. return err;
  694. err = read_utmi_param(pdev, "nvidia,xcvr-lsfslew",
  695. &config->xcvr_lsfslew);
  696. if (err < 0)
  697. return err;
  698. err = read_utmi_param(pdev, "nvidia,xcvr-lsrslew",
  699. &config->xcvr_lsrslew);
  700. if (err < 0)
  701. return err;
  702. return 0;
  703. }
  704. static int tegra_usb_phy_probe(struct platform_device *pdev)
  705. {
  706. struct resource *res;
  707. struct tegra_usb_phy *tegra_phy = NULL;
  708. struct device_node *np = pdev->dev.of_node;
  709. int err;
  710. tegra_phy = devm_kzalloc(&pdev->dev, sizeof(*tegra_phy), GFP_KERNEL);
  711. if (!tegra_phy) {
  712. dev_err(&pdev->dev, "unable to allocate memory for USB2 PHY\n");
  713. return -ENOMEM;
  714. }
  715. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  716. if (!res) {
  717. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  718. return -ENXIO;
  719. }
  720. tegra_phy->regs = devm_ioremap(&pdev->dev, res->start,
  721. resource_size(res));
  722. if (!tegra_phy->regs) {
  723. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  724. return -ENOMEM;
  725. }
  726. tegra_phy->is_legacy_phy =
  727. of_property_read_bool(np, "nvidia,has-legacy-mode");
  728. err = of_property_match_string(np, "phy_type", "ulpi");
  729. if (err < 0) {
  730. err = utmi_phy_probe(tegra_phy, pdev);
  731. if (err < 0)
  732. return err;
  733. } else {
  734. tegra_phy->is_ulpi_phy = true;
  735. tegra_phy->reset_gpio =
  736. of_get_named_gpio(np, "nvidia,phy-reset-gpio", 0);
  737. if (!gpio_is_valid(tegra_phy->reset_gpio)) {
  738. dev_err(&pdev->dev, "invalid gpio: %d\n",
  739. tegra_phy->reset_gpio);
  740. return tegra_phy->reset_gpio;
  741. }
  742. tegra_phy->config = NULL;
  743. }
  744. err = of_property_match_string(np, "dr_mode", "otg");
  745. if (err < 0) {
  746. err = of_property_match_string(np, "dr_mode", "peripheral");
  747. if (err < 0)
  748. tegra_phy->mode = TEGRA_USB_PHY_MODE_HOST;
  749. else
  750. tegra_phy->mode = TEGRA_USB_PHY_MODE_DEVICE;
  751. } else
  752. tegra_phy->mode = TEGRA_USB_PHY_MODE_OTG;
  753. /* On some boards, the VBUS regulator doesn't need to be controlled */
  754. if (of_find_property(np, "vbus-supply", NULL)) {
  755. tegra_phy->vbus = devm_regulator_get(&pdev->dev, "vbus");
  756. if (IS_ERR(tegra_phy->vbus))
  757. return PTR_ERR(tegra_phy->vbus);
  758. } else {
  759. dev_notice(&pdev->dev, "no vbus regulator");
  760. tegra_phy->vbus = ERR_PTR(-ENODEV);
  761. }
  762. tegra_phy->u_phy.dev = &pdev->dev;
  763. err = tegra_usb_phy_init(tegra_phy);
  764. if (err < 0)
  765. return err;
  766. tegra_phy->u_phy.shutdown = tegra_usb_phy_close;
  767. tegra_phy->u_phy.set_suspend = tegra_usb_phy_suspend;
  768. dev_set_drvdata(&pdev->dev, tegra_phy);
  769. err = usb_add_phy_dev(&tegra_phy->u_phy);
  770. if (err < 0) {
  771. tegra_usb_phy_close(&tegra_phy->u_phy);
  772. return err;
  773. }
  774. return 0;
  775. }
  776. static int tegra_usb_phy_remove(struct platform_device *pdev)
  777. {
  778. struct tegra_usb_phy *tegra_phy = platform_get_drvdata(pdev);
  779. usb_remove_phy(&tegra_phy->u_phy);
  780. return 0;
  781. }
  782. static struct of_device_id tegra_usb_phy_id_table[] = {
  783. { .compatible = "nvidia,tegra20-usb-phy", },
  784. { },
  785. };
  786. MODULE_DEVICE_TABLE(of, tegra_usb_phy_id_table);
  787. static struct platform_driver tegra_usb_phy_driver = {
  788. .probe = tegra_usb_phy_probe,
  789. .remove = tegra_usb_phy_remove,
  790. .driver = {
  791. .name = "tegra-phy",
  792. .owner = THIS_MODULE,
  793. .of_match_table = of_match_ptr(tegra_usb_phy_id_table),
  794. },
  795. };
  796. module_platform_driver(tegra_usb_phy_driver);
  797. MODULE_DESCRIPTION("Tegra USB PHY driver");
  798. MODULE_LICENSE("GPL v2");