phy-mxs-usb.c 27 KB

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