phy-mxs-usb.c 22 KB

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