phy-mxs-usb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2012-2014 Freescale Semiconductor, Inc.
  4. * Copyright (C) 2012 Marek Vasut <[email protected]>
  5. * on behalf of DENX Software Engineering GmbH
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/clk.h>
  11. #include <linux/usb/otg.h>
  12. #include <linux/stmp_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/of_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/iopoll.h>
  20. #define DRIVER_NAME "mxs_phy"
  21. /* Register Macro */
  22. #define HW_USBPHY_PWD 0x00
  23. #define HW_USBPHY_TX 0x10
  24. #define HW_USBPHY_CTRL 0x30
  25. #define HW_USBPHY_CTRL_SET 0x34
  26. #define HW_USBPHY_CTRL_CLR 0x38
  27. #define HW_USBPHY_DEBUG_SET 0x54
  28. #define HW_USBPHY_DEBUG_CLR 0x58
  29. #define HW_USBPHY_IP 0x90
  30. #define HW_USBPHY_IP_SET 0x94
  31. #define HW_USBPHY_IP_CLR 0x98
  32. #define GM_USBPHY_TX_TXCAL45DP(x) (((x) & 0xf) << 16)
  33. #define GM_USBPHY_TX_TXCAL45DN(x) (((x) & 0xf) << 8)
  34. #define GM_USBPHY_TX_D_CAL(x) (((x) & 0xf) << 0)
  35. /* imx7ulp */
  36. #define HW_USBPHY_PLL_SIC 0xa0
  37. #define HW_USBPHY_PLL_SIC_SET 0xa4
  38. #define HW_USBPHY_PLL_SIC_CLR 0xa8
  39. #define BM_USBPHY_CTRL_SFTRST BIT(31)
  40. #define BM_USBPHY_CTRL_CLKGATE BIT(30)
  41. #define BM_USBPHY_CTRL_OTG_ID_VALUE BIT(27)
  42. #define BM_USBPHY_CTRL_ENAUTOSET_USBCLKS BIT(26)
  43. #define BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE BIT(25)
  44. #define BM_USBPHY_CTRL_ENVBUSCHG_WKUP BIT(23)
  45. #define BM_USBPHY_CTRL_ENIDCHG_WKUP BIT(22)
  46. #define BM_USBPHY_CTRL_ENDPDMCHG_WKUP BIT(21)
  47. #define BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD BIT(20)
  48. #define BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE BIT(19)
  49. #define BM_USBPHY_CTRL_ENAUTO_PWRON_PLL BIT(18)
  50. #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
  51. #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
  52. #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
  53. #define BM_USBPHY_IP_FIX (BIT(17) | BIT(18))
  54. #define BM_USBPHY_DEBUG_CLKGATE BIT(30)
  55. /* imx7ulp */
  56. #define BM_USBPHY_PLL_LOCK BIT(31)
  57. #define BM_USBPHY_PLL_REG_ENABLE BIT(21)
  58. #define BM_USBPHY_PLL_BYPASS BIT(16)
  59. #define BM_USBPHY_PLL_POWER BIT(12)
  60. #define BM_USBPHY_PLL_EN_USB_CLKS BIT(6)
  61. /* Anatop Registers */
  62. #define ANADIG_ANA_MISC0 0x150
  63. #define ANADIG_ANA_MISC0_SET 0x154
  64. #define ANADIG_ANA_MISC0_CLR 0x158
  65. #define ANADIG_USB1_CHRG_DETECT_SET 0x1b4
  66. #define ANADIG_USB1_CHRG_DETECT_CLR 0x1b8
  67. #define ANADIG_USB2_CHRG_DETECT_SET 0x214
  68. #define ANADIG_USB1_CHRG_DETECT_EN_B BIT(20)
  69. #define ANADIG_USB1_CHRG_DETECT_CHK_CHRG_B BIT(19)
  70. #define ANADIG_USB1_CHRG_DETECT_CHK_CONTACT BIT(18)
  71. #define ANADIG_USB1_VBUS_DET_STAT 0x1c0
  72. #define ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID BIT(3)
  73. #define ANADIG_USB1_CHRG_DET_STAT 0x1d0
  74. #define ANADIG_USB1_CHRG_DET_STAT_DM_STATE BIT(2)
  75. #define ANADIG_USB1_CHRG_DET_STAT_CHRG_DETECTED BIT(1)
  76. #define ANADIG_USB1_CHRG_DET_STAT_PLUG_CONTACT BIT(0)
  77. #define ANADIG_USB2_VBUS_DET_STAT 0x220
  78. #define ANADIG_USB1_LOOPBACK_SET 0x1e4
  79. #define ANADIG_USB1_LOOPBACK_CLR 0x1e8
  80. #define ANADIG_USB1_LOOPBACK_UTMI_TESTSTART BIT(0)
  81. #define ANADIG_USB2_LOOPBACK_SET 0x244
  82. #define ANADIG_USB2_LOOPBACK_CLR 0x248
  83. #define ANADIG_USB1_MISC 0x1f0
  84. #define ANADIG_USB2_MISC 0x250
  85. #define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG BIT(12)
  86. #define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL BIT(11)
  87. #define BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID BIT(3)
  88. #define BM_ANADIG_USB2_VBUS_DET_STAT_VBUS_VALID BIT(3)
  89. #define BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 BIT(2)
  90. #define BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN BIT(5)
  91. #define BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 BIT(2)
  92. #define BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN BIT(5)
  93. #define BM_ANADIG_USB1_MISC_RX_VPIN_FS BIT(29)
  94. #define BM_ANADIG_USB1_MISC_RX_VMIN_FS BIT(28)
  95. #define BM_ANADIG_USB2_MISC_RX_VPIN_FS BIT(29)
  96. #define BM_ANADIG_USB2_MISC_RX_VMIN_FS BIT(28)
  97. #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
  98. /* Do disconnection between PHY and controller without vbus */
  99. #define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
  100. /*
  101. * The PHY will be in messy if there is a wakeup after putting
  102. * bus to suspend (set portsc.suspendM) but before setting PHY to low
  103. * power mode (set portsc.phcd).
  104. */
  105. #define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
  106. /*
  107. * The SOF sends too fast after resuming, it will cause disconnection
  108. * between host and high speed device.
  109. */
  110. #define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
  111. /*
  112. * IC has bug fixes logic, they include
  113. * MXS_PHY_ABNORMAL_IN_SUSPEND and MXS_PHY_SENDING_SOF_TOO_FAST
  114. * which are described at above flags, the RTL will handle it
  115. * according to different versions.
  116. */
  117. #define MXS_PHY_NEED_IP_FIX BIT(3)
  118. /* Minimum and maximum values for device tree entries */
  119. #define MXS_PHY_TX_CAL45_MIN 35
  120. #define MXS_PHY_TX_CAL45_MAX 54
  121. #define MXS_PHY_TX_D_CAL_MIN 79
  122. #define MXS_PHY_TX_D_CAL_MAX 119
  123. struct mxs_phy_data {
  124. unsigned int flags;
  125. };
  126. static const struct mxs_phy_data imx23_phy_data = {
  127. .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
  128. };
  129. static const struct mxs_phy_data imx6q_phy_data = {
  130. .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
  131. MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  132. MXS_PHY_NEED_IP_FIX,
  133. };
  134. static const struct mxs_phy_data imx6sl_phy_data = {
  135. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  136. MXS_PHY_NEED_IP_FIX,
  137. };
  138. static const struct mxs_phy_data vf610_phy_data = {
  139. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  140. MXS_PHY_NEED_IP_FIX,
  141. };
  142. static const struct mxs_phy_data imx6sx_phy_data = {
  143. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
  144. };
  145. static const struct mxs_phy_data imx6ul_phy_data = {
  146. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS,
  147. };
  148. static const struct mxs_phy_data imx7ulp_phy_data = {
  149. };
  150. static const struct of_device_id mxs_phy_dt_ids[] = {
  151. { .compatible = "fsl,imx6sx-usbphy", .data = &imx6sx_phy_data, },
  152. { .compatible = "fsl,imx6sl-usbphy", .data = &imx6sl_phy_data, },
  153. { .compatible = "fsl,imx6q-usbphy", .data = &imx6q_phy_data, },
  154. { .compatible = "fsl,imx23-usbphy", .data = &imx23_phy_data, },
  155. { .compatible = "fsl,vf610-usbphy", .data = &vf610_phy_data, },
  156. { .compatible = "fsl,imx6ul-usbphy", .data = &imx6ul_phy_data, },
  157. { .compatible = "fsl,imx7ulp-usbphy", .data = &imx7ulp_phy_data, },
  158. { /* sentinel */ }
  159. };
  160. MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
  161. struct mxs_phy {
  162. struct usb_phy phy;
  163. struct clk *clk;
  164. const struct mxs_phy_data *data;
  165. struct regmap *regmap_anatop;
  166. int port_id;
  167. u32 tx_reg_set;
  168. u32 tx_reg_mask;
  169. };
  170. static inline bool is_imx6q_phy(struct mxs_phy *mxs_phy)
  171. {
  172. return mxs_phy->data == &imx6q_phy_data;
  173. }
  174. static inline bool is_imx6sl_phy(struct mxs_phy *mxs_phy)
  175. {
  176. return mxs_phy->data == &imx6sl_phy_data;
  177. }
  178. static inline bool is_imx7ulp_phy(struct mxs_phy *mxs_phy)
  179. {
  180. return mxs_phy->data == &imx7ulp_phy_data;
  181. }
  182. /*
  183. * PHY needs some 32K cycles to switch from 32K clock to
  184. * bus (such as AHB/AXI, etc) clock.
  185. */
  186. static void mxs_phy_clock_switch_delay(void)
  187. {
  188. usleep_range(300, 400);
  189. }
  190. static void mxs_phy_tx_init(struct mxs_phy *mxs_phy)
  191. {
  192. void __iomem *base = mxs_phy->phy.io_priv;
  193. u32 phytx;
  194. /* Update TX register if there is anything to write */
  195. if (mxs_phy->tx_reg_mask) {
  196. phytx = readl(base + HW_USBPHY_TX);
  197. phytx &= ~mxs_phy->tx_reg_mask;
  198. phytx |= mxs_phy->tx_reg_set;
  199. writel(phytx, base + HW_USBPHY_TX);
  200. }
  201. }
  202. static int mxs_phy_pll_enable(void __iomem *base, bool enable)
  203. {
  204. int ret = 0;
  205. if (enable) {
  206. u32 value;
  207. writel(BM_USBPHY_PLL_REG_ENABLE, base + HW_USBPHY_PLL_SIC_SET);
  208. writel(BM_USBPHY_PLL_BYPASS, base + HW_USBPHY_PLL_SIC_CLR);
  209. writel(BM_USBPHY_PLL_POWER, base + HW_USBPHY_PLL_SIC_SET);
  210. ret = readl_poll_timeout(base + HW_USBPHY_PLL_SIC,
  211. value, (value & BM_USBPHY_PLL_LOCK) != 0,
  212. 100, 10000);
  213. if (ret)
  214. return ret;
  215. writel(BM_USBPHY_PLL_EN_USB_CLKS, base +
  216. HW_USBPHY_PLL_SIC_SET);
  217. } else {
  218. writel(BM_USBPHY_PLL_EN_USB_CLKS, base +
  219. HW_USBPHY_PLL_SIC_CLR);
  220. writel(BM_USBPHY_PLL_POWER, base + HW_USBPHY_PLL_SIC_CLR);
  221. writel(BM_USBPHY_PLL_BYPASS, base + HW_USBPHY_PLL_SIC_SET);
  222. writel(BM_USBPHY_PLL_REG_ENABLE, base + HW_USBPHY_PLL_SIC_CLR);
  223. }
  224. return ret;
  225. }
  226. static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
  227. {
  228. int ret;
  229. void __iomem *base = mxs_phy->phy.io_priv;
  230. if (is_imx7ulp_phy(mxs_phy)) {
  231. ret = mxs_phy_pll_enable(base, true);
  232. if (ret)
  233. return ret;
  234. }
  235. ret = stmp_reset_block(base + HW_USBPHY_CTRL);
  236. if (ret)
  237. goto disable_pll;
  238. /* Power up the PHY */
  239. writel(0, base + HW_USBPHY_PWD);
  240. /*
  241. * USB PHY Ctrl Setting
  242. * - Auto clock/power on
  243. * - Enable full/low speed support
  244. */
  245. writel(BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
  246. BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
  247. BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
  248. BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
  249. BM_USBPHY_CTRL_ENAUTO_PWRON_PLL |
  250. BM_USBPHY_CTRL_ENUTMILEVEL2 |
  251. BM_USBPHY_CTRL_ENUTMILEVEL3,
  252. base + HW_USBPHY_CTRL_SET);
  253. if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX)
  254. writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET);
  255. if (mxs_phy->regmap_anatop) {
  256. unsigned int reg = mxs_phy->port_id ?
  257. ANADIG_USB1_CHRG_DETECT_SET :
  258. ANADIG_USB2_CHRG_DETECT_SET;
  259. /*
  260. * The external charger detector needs to be disabled,
  261. * or the signal at DP will be poor
  262. */
  263. regmap_write(mxs_phy->regmap_anatop, reg,
  264. ANADIG_USB1_CHRG_DETECT_EN_B |
  265. ANADIG_USB1_CHRG_DETECT_CHK_CHRG_B);
  266. }
  267. mxs_phy_tx_init(mxs_phy);
  268. return 0;
  269. disable_pll:
  270. if (is_imx7ulp_phy(mxs_phy))
  271. mxs_phy_pll_enable(base, false);
  272. return ret;
  273. }
  274. /* Return true if the vbus is there */
  275. static bool mxs_phy_get_vbus_status(struct mxs_phy *mxs_phy)
  276. {
  277. unsigned int vbus_value = 0;
  278. if (!mxs_phy->regmap_anatop)
  279. return false;
  280. if (mxs_phy->port_id == 0)
  281. regmap_read(mxs_phy->regmap_anatop,
  282. ANADIG_USB1_VBUS_DET_STAT,
  283. &vbus_value);
  284. else if (mxs_phy->port_id == 1)
  285. regmap_read(mxs_phy->regmap_anatop,
  286. ANADIG_USB2_VBUS_DET_STAT,
  287. &vbus_value);
  288. if (vbus_value & BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID)
  289. return true;
  290. else
  291. return false;
  292. }
  293. static void __mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool disconnect)
  294. {
  295. void __iomem *base = mxs_phy->phy.io_priv;
  296. u32 reg;
  297. if (disconnect)
  298. writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
  299. base + HW_USBPHY_DEBUG_CLR);
  300. if (mxs_phy->port_id == 0) {
  301. reg = disconnect ? ANADIG_USB1_LOOPBACK_SET
  302. : ANADIG_USB1_LOOPBACK_CLR;
  303. regmap_write(mxs_phy->regmap_anatop, reg,
  304. BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 |
  305. BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN);
  306. } else if (mxs_phy->port_id == 1) {
  307. reg = disconnect ? ANADIG_USB2_LOOPBACK_SET
  308. : ANADIG_USB2_LOOPBACK_CLR;
  309. regmap_write(mxs_phy->regmap_anatop, reg,
  310. BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 |
  311. BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN);
  312. }
  313. if (!disconnect)
  314. writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
  315. base + HW_USBPHY_DEBUG_SET);
  316. /* Delay some time, and let Linestate be SE0 for controller */
  317. if (disconnect)
  318. usleep_range(500, 1000);
  319. }
  320. static bool mxs_phy_is_otg_host(struct mxs_phy *mxs_phy)
  321. {
  322. return IS_ENABLED(CONFIG_USB_OTG) &&
  323. mxs_phy->phy.last_event == USB_EVENT_ID;
  324. }
  325. static void mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool on)
  326. {
  327. bool vbus_is_on = false;
  328. /* If the SoCs don't need to disconnect line without vbus, quit */
  329. if (!(mxs_phy->data->flags & MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS))
  330. return;
  331. /* If the SoCs don't have anatop, quit */
  332. if (!mxs_phy->regmap_anatop)
  333. return;
  334. vbus_is_on = mxs_phy_get_vbus_status(mxs_phy);
  335. if (on && !vbus_is_on && !mxs_phy_is_otg_host(mxs_phy))
  336. __mxs_phy_disconnect_line(mxs_phy, true);
  337. else
  338. __mxs_phy_disconnect_line(mxs_phy, false);
  339. }
  340. static int mxs_phy_init(struct usb_phy *phy)
  341. {
  342. int ret;
  343. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  344. mxs_phy_clock_switch_delay();
  345. ret = clk_prepare_enable(mxs_phy->clk);
  346. if (ret)
  347. return ret;
  348. return mxs_phy_hw_init(mxs_phy);
  349. }
  350. static void mxs_phy_shutdown(struct usb_phy *phy)
  351. {
  352. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  353. u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
  354. BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
  355. BM_USBPHY_CTRL_ENIDCHG_WKUP |
  356. BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
  357. BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
  358. BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
  359. BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
  360. BM_USBPHY_CTRL_ENAUTO_PWRON_PLL;
  361. writel(value, phy->io_priv + HW_USBPHY_CTRL_CLR);
  362. writel(0xffffffff, phy->io_priv + HW_USBPHY_PWD);
  363. writel(BM_USBPHY_CTRL_CLKGATE,
  364. phy->io_priv + HW_USBPHY_CTRL_SET);
  365. if (is_imx7ulp_phy(mxs_phy))
  366. mxs_phy_pll_enable(phy->io_priv, false);
  367. clk_disable_unprepare(mxs_phy->clk);
  368. }
  369. static bool mxs_phy_is_low_speed_connection(struct mxs_phy *mxs_phy)
  370. {
  371. unsigned int line_state;
  372. /* bit definition is the same for all controllers */
  373. unsigned int dp_bit = BM_ANADIG_USB1_MISC_RX_VPIN_FS,
  374. dm_bit = BM_ANADIG_USB1_MISC_RX_VMIN_FS;
  375. unsigned int reg = ANADIG_USB1_MISC;
  376. /* If the SoCs don't have anatop, quit */
  377. if (!mxs_phy->regmap_anatop)
  378. return false;
  379. if (mxs_phy->port_id == 0)
  380. reg = ANADIG_USB1_MISC;
  381. else if (mxs_phy->port_id == 1)
  382. reg = ANADIG_USB2_MISC;
  383. regmap_read(mxs_phy->regmap_anatop, reg, &line_state);
  384. if ((line_state & (dp_bit | dm_bit)) == dm_bit)
  385. return true;
  386. else
  387. return false;
  388. }
  389. static int mxs_phy_suspend(struct usb_phy *x, int suspend)
  390. {
  391. int ret;
  392. struct mxs_phy *mxs_phy = to_mxs_phy(x);
  393. bool low_speed_connection, vbus_is_on;
  394. low_speed_connection = mxs_phy_is_low_speed_connection(mxs_phy);
  395. vbus_is_on = mxs_phy_get_vbus_status(mxs_phy);
  396. if (suspend) {
  397. /*
  398. * FIXME: Do not power down RXPWD1PT1 bit for low speed
  399. * connect. The low speed connection will have problem at
  400. * very rare cases during usb suspend and resume process.
  401. */
  402. if (low_speed_connection & vbus_is_on) {
  403. /*
  404. * If value to be set as pwd value is not 0xffffffff,
  405. * several 32Khz cycles are needed.
  406. */
  407. mxs_phy_clock_switch_delay();
  408. writel(0xffbfffff, x->io_priv + HW_USBPHY_PWD);
  409. } else {
  410. writel(0xffffffff, x->io_priv + HW_USBPHY_PWD);
  411. }
  412. writel(BM_USBPHY_CTRL_CLKGATE,
  413. x->io_priv + HW_USBPHY_CTRL_SET);
  414. clk_disable_unprepare(mxs_phy->clk);
  415. } else {
  416. mxs_phy_clock_switch_delay();
  417. ret = clk_prepare_enable(mxs_phy->clk);
  418. if (ret)
  419. return ret;
  420. writel(BM_USBPHY_CTRL_CLKGATE,
  421. x->io_priv + HW_USBPHY_CTRL_CLR);
  422. writel(0, x->io_priv + HW_USBPHY_PWD);
  423. }
  424. return 0;
  425. }
  426. static int mxs_phy_set_wakeup(struct usb_phy *x, bool enabled)
  427. {
  428. struct mxs_phy *mxs_phy = to_mxs_phy(x);
  429. u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
  430. BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
  431. BM_USBPHY_CTRL_ENIDCHG_WKUP;
  432. if (enabled) {
  433. mxs_phy_disconnect_line(mxs_phy, true);
  434. writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_SET);
  435. } else {
  436. writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_CLR);
  437. mxs_phy_disconnect_line(mxs_phy, false);
  438. }
  439. return 0;
  440. }
  441. static int mxs_phy_on_connect(struct usb_phy *phy,
  442. enum usb_device_speed speed)
  443. {
  444. dev_dbg(phy->dev, "%s device has connected\n",
  445. (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
  446. if (speed == USB_SPEED_HIGH)
  447. writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  448. phy->io_priv + HW_USBPHY_CTRL_SET);
  449. return 0;
  450. }
  451. static int mxs_phy_on_disconnect(struct usb_phy *phy,
  452. enum usb_device_speed speed)
  453. {
  454. dev_dbg(phy->dev, "%s device has disconnected\n",
  455. (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
  456. /* Sometimes, the speed is not high speed when the error occurs */
  457. if (readl(phy->io_priv + HW_USBPHY_CTRL) &
  458. BM_USBPHY_CTRL_ENHOSTDISCONDETECT)
  459. writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  460. phy->io_priv + HW_USBPHY_CTRL_CLR);
  461. return 0;
  462. }
  463. #define MXS_USB_CHARGER_DATA_CONTACT_TIMEOUT 100
  464. static int mxs_charger_data_contact_detect(struct mxs_phy *x)
  465. {
  466. struct regmap *regmap = x->regmap_anatop;
  467. int i, stable_contact_count = 0;
  468. u32 val;
  469. /* Check if vbus is valid */
  470. regmap_read(regmap, ANADIG_USB1_VBUS_DET_STAT, &val);
  471. if (!(val & ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID)) {
  472. dev_err(x->phy.dev, "vbus is not valid\n");
  473. return -EINVAL;
  474. }
  475. /* Enable charger detector */
  476. regmap_write(regmap, ANADIG_USB1_CHRG_DETECT_CLR,
  477. ANADIG_USB1_CHRG_DETECT_EN_B);
  478. /*
  479. * - Do not check whether a charger is connected to the USB port
  480. * - Check whether the USB plug has been in contact with each other
  481. */
  482. regmap_write(regmap, ANADIG_USB1_CHRG_DETECT_SET,
  483. ANADIG_USB1_CHRG_DETECT_CHK_CONTACT |
  484. ANADIG_USB1_CHRG_DETECT_CHK_CHRG_B);
  485. /* Check if plug is connected */
  486. for (i = 0; i < MXS_USB_CHARGER_DATA_CONTACT_TIMEOUT; i++) {
  487. regmap_read(regmap, ANADIG_USB1_CHRG_DET_STAT, &val);
  488. if (val & ANADIG_USB1_CHRG_DET_STAT_PLUG_CONTACT) {
  489. stable_contact_count++;
  490. if (stable_contact_count > 5)
  491. /* Data pin makes contact */
  492. break;
  493. else
  494. usleep_range(5000, 10000);
  495. } else {
  496. stable_contact_count = 0;
  497. usleep_range(5000, 6000);
  498. }
  499. }
  500. if (i == MXS_USB_CHARGER_DATA_CONTACT_TIMEOUT) {
  501. dev_err(x->phy.dev,
  502. "Data pin can't make good contact.\n");
  503. /* Disable charger detector */
  504. regmap_write(regmap, ANADIG_USB1_CHRG_DETECT_SET,
  505. ANADIG_USB1_CHRG_DETECT_EN_B |
  506. ANADIG_USB1_CHRG_DETECT_CHK_CHRG_B);
  507. return -ENXIO;
  508. }
  509. return 0;
  510. }
  511. static enum usb_charger_type mxs_charger_primary_detection(struct mxs_phy *x)
  512. {
  513. struct regmap *regmap = x->regmap_anatop;
  514. enum usb_charger_type chgr_type = UNKNOWN_TYPE;
  515. u32 val;
  516. /*
  517. * - Do check whether a charger is connected to the USB port
  518. * - Do not Check whether the USB plug has been in contact with
  519. * each other
  520. */
  521. regmap_write(regmap, ANADIG_USB1_CHRG_DETECT_CLR,
  522. ANADIG_USB1_CHRG_DETECT_CHK_CONTACT |
  523. ANADIG_USB1_CHRG_DETECT_CHK_CHRG_B);
  524. msleep(100);
  525. /* Check if it is a charger */
  526. regmap_read(regmap, ANADIG_USB1_CHRG_DET_STAT, &val);
  527. if (!(val & ANADIG_USB1_CHRG_DET_STAT_CHRG_DETECTED)) {
  528. chgr_type = SDP_TYPE;
  529. dev_dbg(x->phy.dev, "It is a standard downstream port\n");
  530. }
  531. /* Disable charger detector */
  532. regmap_write(regmap, ANADIG_USB1_CHRG_DETECT_SET,
  533. ANADIG_USB1_CHRG_DETECT_EN_B |
  534. ANADIG_USB1_CHRG_DETECT_CHK_CHRG_B);
  535. return chgr_type;
  536. }
  537. /*
  538. * It must be called after DP is pulled up, which is used to
  539. * differentiate DCP and CDP.
  540. */
  541. static enum usb_charger_type mxs_charger_secondary_detection(struct mxs_phy *x)
  542. {
  543. struct regmap *regmap = x->regmap_anatop;
  544. int val;
  545. msleep(80);
  546. regmap_read(regmap, ANADIG_USB1_CHRG_DET_STAT, &val);
  547. if (val & ANADIG_USB1_CHRG_DET_STAT_DM_STATE) {
  548. dev_dbg(x->phy.dev, "It is a dedicate charging port\n");
  549. return DCP_TYPE;
  550. } else {
  551. dev_dbg(x->phy.dev, "It is a charging downstream port\n");
  552. return CDP_TYPE;
  553. }
  554. }
  555. static enum usb_charger_type mxs_phy_charger_detect(struct usb_phy *phy)
  556. {
  557. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  558. struct regmap *regmap = mxs_phy->regmap_anatop;
  559. void __iomem *base = phy->io_priv;
  560. enum usb_charger_type chgr_type = UNKNOWN_TYPE;
  561. if (!regmap)
  562. return UNKNOWN_TYPE;
  563. if (mxs_charger_data_contact_detect(mxs_phy))
  564. return chgr_type;
  565. chgr_type = mxs_charger_primary_detection(mxs_phy);
  566. if (chgr_type != SDP_TYPE) {
  567. /* Pull up DP via test */
  568. writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
  569. base + HW_USBPHY_DEBUG_CLR);
  570. regmap_write(regmap, ANADIG_USB1_LOOPBACK_SET,
  571. ANADIG_USB1_LOOPBACK_UTMI_TESTSTART);
  572. chgr_type = mxs_charger_secondary_detection(mxs_phy);
  573. /* Stop the test */
  574. regmap_write(regmap, ANADIG_USB1_LOOPBACK_CLR,
  575. ANADIG_USB1_LOOPBACK_UTMI_TESTSTART);
  576. writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
  577. base + HW_USBPHY_DEBUG_SET);
  578. }
  579. return chgr_type;
  580. }
  581. static int mxs_phy_probe(struct platform_device *pdev)
  582. {
  583. void __iomem *base;
  584. struct clk *clk;
  585. struct mxs_phy *mxs_phy;
  586. int ret;
  587. struct device_node *np = pdev->dev.of_node;
  588. u32 val;
  589. base = devm_platform_ioremap_resource(pdev, 0);
  590. if (IS_ERR(base))
  591. return PTR_ERR(base);
  592. clk = devm_clk_get(&pdev->dev, NULL);
  593. if (IS_ERR(clk)) {
  594. dev_err(&pdev->dev,
  595. "can't get the clock, err=%ld", PTR_ERR(clk));
  596. return PTR_ERR(clk);
  597. }
  598. mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
  599. if (!mxs_phy)
  600. return -ENOMEM;
  601. /* Some SoCs don't have anatop registers */
  602. if (of_get_property(np, "fsl,anatop", NULL)) {
  603. mxs_phy->regmap_anatop = syscon_regmap_lookup_by_phandle
  604. (np, "fsl,anatop");
  605. if (IS_ERR(mxs_phy->regmap_anatop)) {
  606. dev_dbg(&pdev->dev,
  607. "failed to find regmap for anatop\n");
  608. return PTR_ERR(mxs_phy->regmap_anatop);
  609. }
  610. }
  611. /* Precompute which bits of the TX register are to be updated, if any */
  612. if (!of_property_read_u32(np, "fsl,tx-cal-45-dn-ohms", &val) &&
  613. val >= MXS_PHY_TX_CAL45_MIN && val <= MXS_PHY_TX_CAL45_MAX) {
  614. /* Scale to a 4-bit value */
  615. val = (MXS_PHY_TX_CAL45_MAX - val) * 0xF
  616. / (MXS_PHY_TX_CAL45_MAX - MXS_PHY_TX_CAL45_MIN);
  617. mxs_phy->tx_reg_mask |= GM_USBPHY_TX_TXCAL45DN(~0);
  618. mxs_phy->tx_reg_set |= GM_USBPHY_TX_TXCAL45DN(val);
  619. }
  620. if (!of_property_read_u32(np, "fsl,tx-cal-45-dp-ohms", &val) &&
  621. val >= MXS_PHY_TX_CAL45_MIN && val <= MXS_PHY_TX_CAL45_MAX) {
  622. /* Scale to a 4-bit value. */
  623. val = (MXS_PHY_TX_CAL45_MAX - val) * 0xF
  624. / (MXS_PHY_TX_CAL45_MAX - MXS_PHY_TX_CAL45_MIN);
  625. mxs_phy->tx_reg_mask |= GM_USBPHY_TX_TXCAL45DP(~0);
  626. mxs_phy->tx_reg_set |= GM_USBPHY_TX_TXCAL45DP(val);
  627. }
  628. if (!of_property_read_u32(np, "fsl,tx-d-cal", &val) &&
  629. val >= MXS_PHY_TX_D_CAL_MIN && val <= MXS_PHY_TX_D_CAL_MAX) {
  630. /* Scale to a 4-bit value. Round up the values and heavily
  631. * weight the rounding by adding 2/3 of the denominator.
  632. */
  633. val = ((MXS_PHY_TX_D_CAL_MAX - val) * 0xF
  634. + (MXS_PHY_TX_D_CAL_MAX - MXS_PHY_TX_D_CAL_MIN) * 2/3)
  635. / (MXS_PHY_TX_D_CAL_MAX - MXS_PHY_TX_D_CAL_MIN);
  636. mxs_phy->tx_reg_mask |= GM_USBPHY_TX_D_CAL(~0);
  637. mxs_phy->tx_reg_set |= GM_USBPHY_TX_D_CAL(val);
  638. }
  639. ret = of_alias_get_id(np, "usbphy");
  640. if (ret < 0)
  641. dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  642. mxs_phy->port_id = ret;
  643. mxs_phy->phy.io_priv = base;
  644. mxs_phy->phy.dev = &pdev->dev;
  645. mxs_phy->phy.label = DRIVER_NAME;
  646. mxs_phy->phy.init = mxs_phy_init;
  647. mxs_phy->phy.shutdown = mxs_phy_shutdown;
  648. mxs_phy->phy.set_suspend = mxs_phy_suspend;
  649. mxs_phy->phy.notify_connect = mxs_phy_on_connect;
  650. mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
  651. mxs_phy->phy.type = USB_PHY_TYPE_USB2;
  652. mxs_phy->phy.set_wakeup = mxs_phy_set_wakeup;
  653. mxs_phy->phy.charger_detect = mxs_phy_charger_detect;
  654. mxs_phy->clk = clk;
  655. mxs_phy->data = of_device_get_match_data(&pdev->dev);
  656. platform_set_drvdata(pdev, mxs_phy);
  657. device_set_wakeup_capable(&pdev->dev, true);
  658. return usb_add_phy_dev(&mxs_phy->phy);
  659. }
  660. static int mxs_phy_remove(struct platform_device *pdev)
  661. {
  662. struct mxs_phy *mxs_phy = platform_get_drvdata(pdev);
  663. usb_remove_phy(&mxs_phy->phy);
  664. return 0;
  665. }
  666. #ifdef CONFIG_PM_SLEEP
  667. static void mxs_phy_enable_ldo_in_suspend(struct mxs_phy *mxs_phy, bool on)
  668. {
  669. unsigned int reg = on ? ANADIG_ANA_MISC0_SET : ANADIG_ANA_MISC0_CLR;
  670. /* If the SoCs don't have anatop, quit */
  671. if (!mxs_phy->regmap_anatop)
  672. return;
  673. if (is_imx6q_phy(mxs_phy))
  674. regmap_write(mxs_phy->regmap_anatop, reg,
  675. BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG);
  676. else if (is_imx6sl_phy(mxs_phy))
  677. regmap_write(mxs_phy->regmap_anatop,
  678. reg, BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL);
  679. }
  680. static int mxs_phy_system_suspend(struct device *dev)
  681. {
  682. struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
  683. if (device_may_wakeup(dev))
  684. mxs_phy_enable_ldo_in_suspend(mxs_phy, true);
  685. return 0;
  686. }
  687. static int mxs_phy_system_resume(struct device *dev)
  688. {
  689. struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
  690. if (device_may_wakeup(dev))
  691. mxs_phy_enable_ldo_in_suspend(mxs_phy, false);
  692. return 0;
  693. }
  694. #endif /* CONFIG_PM_SLEEP */
  695. static SIMPLE_DEV_PM_OPS(mxs_phy_pm, mxs_phy_system_suspend,
  696. mxs_phy_system_resume);
  697. static struct platform_driver mxs_phy_driver = {
  698. .probe = mxs_phy_probe,
  699. .remove = mxs_phy_remove,
  700. .driver = {
  701. .name = DRIVER_NAME,
  702. .of_match_table = mxs_phy_dt_ids,
  703. .pm = &mxs_phy_pm,
  704. },
  705. };
  706. static int __init mxs_phy_module_init(void)
  707. {
  708. return platform_driver_register(&mxs_phy_driver);
  709. }
  710. postcore_initcall(mxs_phy_module_init);
  711. static void __exit mxs_phy_module_exit(void)
  712. {
  713. platform_driver_unregister(&mxs_phy_driver);
  714. }
  715. module_exit(mxs_phy_module_exit);
  716. MODULE_ALIAS("platform:mxs-usb-phy");
  717. MODULE_AUTHOR("Marek Vasut <[email protected]>");
  718. MODULE_AUTHOR("Richard Zhao <[email protected]>");
  719. MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
  720. MODULE_LICENSE("GPL");