phy-tegra-usb.c 25 KB

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