phy-mxs-usb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * Copyright 2012-2014 Freescale Semiconductor, Inc.
  3. * Copyright (C) 2012 Marek Vasut <marex@denx.de>
  4. * on behalf of DENX Software Engineering GmbH
  5. *
  6. * The code contained herein is licensed under the GNU General Public
  7. * License. You may obtain a copy of the GNU General Public License
  8. * Version 2 or later at the following locations:
  9. *
  10. * http://www.opensource.org/licenses/gpl-license.html
  11. * http://www.gnu.org/copyleft/gpl.html
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/clk.h>
  17. #include <linux/usb/otg.h>
  18. #include <linux/stmp_device.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/of_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/mfd/syscon.h>
  25. #define DRIVER_NAME "mxs_phy"
  26. #define HW_USBPHY_PWD 0x00
  27. #define HW_USBPHY_CTRL 0x30
  28. #define HW_USBPHY_CTRL_SET 0x34
  29. #define HW_USBPHY_CTRL_CLR 0x38
  30. #define HW_USBPHY_DEBUG_SET 0x54
  31. #define HW_USBPHY_DEBUG_CLR 0x58
  32. #define HW_USBPHY_IP 0x90
  33. #define HW_USBPHY_IP_SET 0x94
  34. #define HW_USBPHY_IP_CLR 0x98
  35. #define BM_USBPHY_CTRL_SFTRST BIT(31)
  36. #define BM_USBPHY_CTRL_CLKGATE BIT(30)
  37. #define BM_USBPHY_CTRL_ENAUTOSET_USBCLKS BIT(26)
  38. #define BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE BIT(25)
  39. #define BM_USBPHY_CTRL_ENVBUSCHG_WKUP BIT(23)
  40. #define BM_USBPHY_CTRL_ENIDCHG_WKUP BIT(22)
  41. #define BM_USBPHY_CTRL_ENDPDMCHG_WKUP BIT(21)
  42. #define BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD BIT(20)
  43. #define BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE BIT(19)
  44. #define BM_USBPHY_CTRL_ENAUTO_PWRON_PLL BIT(18)
  45. #define BM_USBPHY_CTRL_ENUTMILEVEL3 BIT(15)
  46. #define BM_USBPHY_CTRL_ENUTMILEVEL2 BIT(14)
  47. #define BM_USBPHY_CTRL_ENHOSTDISCONDETECT BIT(1)
  48. #define BM_USBPHY_IP_FIX (BIT(17) | BIT(18))
  49. #define BM_USBPHY_DEBUG_CLKGATE BIT(30)
  50. /* Anatop Registers */
  51. #define ANADIG_ANA_MISC0 0x150
  52. #define ANADIG_ANA_MISC0_SET 0x154
  53. #define ANADIG_ANA_MISC0_CLR 0x158
  54. #define ANADIG_USB1_VBUS_DET_STAT 0x1c0
  55. #define ANADIG_USB2_VBUS_DET_STAT 0x220
  56. #define ANADIG_USB1_LOOPBACK_SET 0x1e4
  57. #define ANADIG_USB1_LOOPBACK_CLR 0x1e8
  58. #define ANADIG_USB2_LOOPBACK_SET 0x244
  59. #define ANADIG_USB2_LOOPBACK_CLR 0x248
  60. #define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG BIT(12)
  61. #define BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL BIT(11)
  62. #define BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID BIT(3)
  63. #define BM_ANADIG_USB2_VBUS_DET_STAT_VBUS_VALID BIT(3)
  64. #define BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 BIT(2)
  65. #define BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN BIT(5)
  66. #define BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 BIT(2)
  67. #define BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN BIT(5)
  68. #define to_mxs_phy(p) container_of((p), struct mxs_phy, phy)
  69. /* Do disconnection between PHY and controller without vbus */
  70. #define MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS BIT(0)
  71. /*
  72. * The PHY will be in messy if there is a wakeup after putting
  73. * bus to suspend (set portsc.suspendM) but before setting PHY to low
  74. * power mode (set portsc.phcd).
  75. */
  76. #define MXS_PHY_ABNORMAL_IN_SUSPEND BIT(1)
  77. /*
  78. * The SOF sends too fast after resuming, it will cause disconnection
  79. * between host and high speed device.
  80. */
  81. #define MXS_PHY_SENDING_SOF_TOO_FAST BIT(2)
  82. /*
  83. * IC has bug fixes logic, they include
  84. * MXS_PHY_ABNORMAL_IN_SUSPEND and MXS_PHY_SENDING_SOF_TOO_FAST
  85. * which are described at above flags, the RTL will handle it
  86. * according to different versions.
  87. */
  88. #define MXS_PHY_NEED_IP_FIX BIT(3)
  89. struct mxs_phy_data {
  90. unsigned int flags;
  91. };
  92. static const struct mxs_phy_data imx23_phy_data = {
  93. .flags = MXS_PHY_ABNORMAL_IN_SUSPEND | MXS_PHY_SENDING_SOF_TOO_FAST,
  94. };
  95. static const struct mxs_phy_data imx6q_phy_data = {
  96. .flags = MXS_PHY_SENDING_SOF_TOO_FAST |
  97. MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  98. MXS_PHY_NEED_IP_FIX,
  99. };
  100. static const struct mxs_phy_data imx6sl_phy_data = {
  101. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  102. MXS_PHY_NEED_IP_FIX,
  103. };
  104. static const struct mxs_phy_data vf610_phy_data = {
  105. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  106. MXS_PHY_NEED_IP_FIX,
  107. };
  108. static const struct mxs_phy_data imx6sx_phy_data = {
  109. .flags = MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS |
  110. MXS_PHY_NEED_IP_FIX,
  111. };
  112. static const struct of_device_id mxs_phy_dt_ids[] = {
  113. { .compatible = "fsl,imx6sx-usbphy", .data = &imx6sx_phy_data, },
  114. { .compatible = "fsl,imx6sl-usbphy", .data = &imx6sl_phy_data, },
  115. { .compatible = "fsl,imx6q-usbphy", .data = &imx6q_phy_data, },
  116. { .compatible = "fsl,imx23-usbphy", .data = &imx23_phy_data, },
  117. { .compatible = "fsl,vf610-usbphy", .data = &vf610_phy_data, },
  118. { /* sentinel */ }
  119. };
  120. MODULE_DEVICE_TABLE(of, mxs_phy_dt_ids);
  121. struct mxs_phy {
  122. struct usb_phy phy;
  123. struct clk *clk;
  124. const struct mxs_phy_data *data;
  125. struct regmap *regmap_anatop;
  126. int port_id;
  127. };
  128. static inline bool is_imx6q_phy(struct mxs_phy *mxs_phy)
  129. {
  130. return mxs_phy->data == &imx6q_phy_data;
  131. }
  132. static inline bool is_imx6sl_phy(struct mxs_phy *mxs_phy)
  133. {
  134. return mxs_phy->data == &imx6sl_phy_data;
  135. }
  136. /*
  137. * PHY needs some 32K cycles to switch from 32K clock to
  138. * bus (such as AHB/AXI, etc) clock.
  139. */
  140. static void mxs_phy_clock_switch_delay(void)
  141. {
  142. usleep_range(300, 400);
  143. }
  144. static int mxs_phy_hw_init(struct mxs_phy *mxs_phy)
  145. {
  146. int ret;
  147. void __iomem *base = mxs_phy->phy.io_priv;
  148. ret = stmp_reset_block(base + HW_USBPHY_CTRL);
  149. if (ret)
  150. return ret;
  151. /* Power up the PHY */
  152. writel(0, base + HW_USBPHY_PWD);
  153. /*
  154. * USB PHY Ctrl Setting
  155. * - Auto clock/power on
  156. * - Enable full/low speed support
  157. */
  158. writel(BM_USBPHY_CTRL_ENAUTOSET_USBCLKS |
  159. BM_USBPHY_CTRL_ENAUTOCLR_USBCLKGATE |
  160. BM_USBPHY_CTRL_ENAUTOCLR_PHY_PWD |
  161. BM_USBPHY_CTRL_ENAUTOCLR_CLKGATE |
  162. BM_USBPHY_CTRL_ENAUTO_PWRON_PLL |
  163. BM_USBPHY_CTRL_ENUTMILEVEL2 |
  164. BM_USBPHY_CTRL_ENUTMILEVEL3,
  165. base + HW_USBPHY_CTRL_SET);
  166. if (mxs_phy->data->flags & MXS_PHY_NEED_IP_FIX)
  167. writel(BM_USBPHY_IP_FIX, base + HW_USBPHY_IP_SET);
  168. return 0;
  169. }
  170. /* Return true if the vbus is there */
  171. static bool mxs_phy_get_vbus_status(struct mxs_phy *mxs_phy)
  172. {
  173. unsigned int vbus_value;
  174. if (mxs_phy->port_id == 0)
  175. regmap_read(mxs_phy->regmap_anatop,
  176. ANADIG_USB1_VBUS_DET_STAT,
  177. &vbus_value);
  178. else if (mxs_phy->port_id == 1)
  179. regmap_read(mxs_phy->regmap_anatop,
  180. ANADIG_USB2_VBUS_DET_STAT,
  181. &vbus_value);
  182. if (vbus_value & BM_ANADIG_USB1_VBUS_DET_STAT_VBUS_VALID)
  183. return true;
  184. else
  185. return false;
  186. }
  187. static void __mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool disconnect)
  188. {
  189. void __iomem *base = mxs_phy->phy.io_priv;
  190. u32 reg;
  191. if (disconnect)
  192. writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
  193. base + HW_USBPHY_DEBUG_CLR);
  194. if (mxs_phy->port_id == 0) {
  195. reg = disconnect ? ANADIG_USB1_LOOPBACK_SET
  196. : ANADIG_USB1_LOOPBACK_CLR;
  197. regmap_write(mxs_phy->regmap_anatop, reg,
  198. BM_ANADIG_USB1_LOOPBACK_UTMI_DIG_TST1 |
  199. BM_ANADIG_USB1_LOOPBACK_TSTI_TX_EN);
  200. } else if (mxs_phy->port_id == 1) {
  201. reg = disconnect ? ANADIG_USB2_LOOPBACK_SET
  202. : ANADIG_USB2_LOOPBACK_CLR;
  203. regmap_write(mxs_phy->regmap_anatop, reg,
  204. BM_ANADIG_USB2_LOOPBACK_UTMI_DIG_TST1 |
  205. BM_ANADIG_USB2_LOOPBACK_TSTI_TX_EN);
  206. }
  207. if (!disconnect)
  208. writel_relaxed(BM_USBPHY_DEBUG_CLKGATE,
  209. base + HW_USBPHY_DEBUG_SET);
  210. /* Delay some time, and let Linestate be SE0 for controller */
  211. if (disconnect)
  212. usleep_range(500, 1000);
  213. }
  214. static void mxs_phy_disconnect_line(struct mxs_phy *mxs_phy, bool on)
  215. {
  216. bool vbus_is_on = false;
  217. /* If the SoCs don't need to disconnect line without vbus, quit */
  218. if (!(mxs_phy->data->flags & MXS_PHY_DISCONNECT_LINE_WITHOUT_VBUS))
  219. return;
  220. /* If the SoCs don't have anatop, quit */
  221. if (!mxs_phy->regmap_anatop)
  222. return;
  223. vbus_is_on = mxs_phy_get_vbus_status(mxs_phy);
  224. if (on && !vbus_is_on)
  225. __mxs_phy_disconnect_line(mxs_phy, true);
  226. else
  227. __mxs_phy_disconnect_line(mxs_phy, false);
  228. }
  229. static int mxs_phy_init(struct usb_phy *phy)
  230. {
  231. int ret;
  232. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  233. mxs_phy_clock_switch_delay();
  234. ret = clk_prepare_enable(mxs_phy->clk);
  235. if (ret)
  236. return ret;
  237. return mxs_phy_hw_init(mxs_phy);
  238. }
  239. static void mxs_phy_shutdown(struct usb_phy *phy)
  240. {
  241. struct mxs_phy *mxs_phy = to_mxs_phy(phy);
  242. writel(BM_USBPHY_CTRL_CLKGATE,
  243. phy->io_priv + HW_USBPHY_CTRL_SET);
  244. clk_disable_unprepare(mxs_phy->clk);
  245. }
  246. static int mxs_phy_suspend(struct usb_phy *x, int suspend)
  247. {
  248. int ret;
  249. struct mxs_phy *mxs_phy = to_mxs_phy(x);
  250. if (suspend) {
  251. writel(0xffffffff, x->io_priv + HW_USBPHY_PWD);
  252. writel(BM_USBPHY_CTRL_CLKGATE,
  253. x->io_priv + HW_USBPHY_CTRL_SET);
  254. clk_disable_unprepare(mxs_phy->clk);
  255. } else {
  256. mxs_phy_clock_switch_delay();
  257. ret = clk_prepare_enable(mxs_phy->clk);
  258. if (ret)
  259. return ret;
  260. writel(BM_USBPHY_CTRL_CLKGATE,
  261. x->io_priv + HW_USBPHY_CTRL_CLR);
  262. writel(0, x->io_priv + HW_USBPHY_PWD);
  263. }
  264. return 0;
  265. }
  266. static int mxs_phy_set_wakeup(struct usb_phy *x, bool enabled)
  267. {
  268. struct mxs_phy *mxs_phy = to_mxs_phy(x);
  269. u32 value = BM_USBPHY_CTRL_ENVBUSCHG_WKUP |
  270. BM_USBPHY_CTRL_ENDPDMCHG_WKUP |
  271. BM_USBPHY_CTRL_ENIDCHG_WKUP;
  272. if (enabled) {
  273. mxs_phy_disconnect_line(mxs_phy, true);
  274. writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_SET);
  275. } else {
  276. writel_relaxed(value, x->io_priv + HW_USBPHY_CTRL_CLR);
  277. mxs_phy_disconnect_line(mxs_phy, false);
  278. }
  279. return 0;
  280. }
  281. static int mxs_phy_on_connect(struct usb_phy *phy,
  282. enum usb_device_speed speed)
  283. {
  284. dev_dbg(phy->dev, "%s device has connected\n",
  285. (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
  286. if (speed == USB_SPEED_HIGH)
  287. writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  288. phy->io_priv + HW_USBPHY_CTRL_SET);
  289. return 0;
  290. }
  291. static int mxs_phy_on_disconnect(struct usb_phy *phy,
  292. enum usb_device_speed speed)
  293. {
  294. dev_dbg(phy->dev, "%s device has disconnected\n",
  295. (speed == USB_SPEED_HIGH) ? "HS" : "FS/LS");
  296. if (speed == USB_SPEED_HIGH)
  297. writel(BM_USBPHY_CTRL_ENHOSTDISCONDETECT,
  298. phy->io_priv + HW_USBPHY_CTRL_CLR);
  299. return 0;
  300. }
  301. static int mxs_phy_probe(struct platform_device *pdev)
  302. {
  303. struct resource *res;
  304. void __iomem *base;
  305. struct clk *clk;
  306. struct mxs_phy *mxs_phy;
  307. int ret;
  308. const struct of_device_id *of_id =
  309. of_match_device(mxs_phy_dt_ids, &pdev->dev);
  310. struct device_node *np = pdev->dev.of_node;
  311. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  312. base = devm_ioremap_resource(&pdev->dev, res);
  313. if (IS_ERR(base))
  314. return PTR_ERR(base);
  315. clk = devm_clk_get(&pdev->dev, NULL);
  316. if (IS_ERR(clk)) {
  317. dev_err(&pdev->dev,
  318. "can't get the clock, err=%ld", PTR_ERR(clk));
  319. return PTR_ERR(clk);
  320. }
  321. mxs_phy = devm_kzalloc(&pdev->dev, sizeof(*mxs_phy), GFP_KERNEL);
  322. if (!mxs_phy) {
  323. dev_err(&pdev->dev, "Failed to allocate USB PHY structure!\n");
  324. return -ENOMEM;
  325. }
  326. /* Some SoCs don't have anatop registers */
  327. if (of_get_property(np, "fsl,anatop", NULL)) {
  328. mxs_phy->regmap_anatop = syscon_regmap_lookup_by_phandle
  329. (np, "fsl,anatop");
  330. if (IS_ERR(mxs_phy->regmap_anatop)) {
  331. dev_dbg(&pdev->dev,
  332. "failed to find regmap for anatop\n");
  333. return PTR_ERR(mxs_phy->regmap_anatop);
  334. }
  335. }
  336. ret = of_alias_get_id(np, "usbphy");
  337. if (ret < 0)
  338. dev_dbg(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  339. mxs_phy->port_id = ret;
  340. mxs_phy->phy.io_priv = base;
  341. mxs_phy->phy.dev = &pdev->dev;
  342. mxs_phy->phy.label = DRIVER_NAME;
  343. mxs_phy->phy.init = mxs_phy_init;
  344. mxs_phy->phy.shutdown = mxs_phy_shutdown;
  345. mxs_phy->phy.set_suspend = mxs_phy_suspend;
  346. mxs_phy->phy.notify_connect = mxs_phy_on_connect;
  347. mxs_phy->phy.notify_disconnect = mxs_phy_on_disconnect;
  348. mxs_phy->phy.type = USB_PHY_TYPE_USB2;
  349. mxs_phy->phy.set_wakeup = mxs_phy_set_wakeup;
  350. mxs_phy->clk = clk;
  351. mxs_phy->data = of_id->data;
  352. platform_set_drvdata(pdev, mxs_phy);
  353. device_set_wakeup_capable(&pdev->dev, true);
  354. ret = usb_add_phy_dev(&mxs_phy->phy);
  355. if (ret)
  356. return ret;
  357. return 0;
  358. }
  359. static int mxs_phy_remove(struct platform_device *pdev)
  360. {
  361. struct mxs_phy *mxs_phy = platform_get_drvdata(pdev);
  362. usb_remove_phy(&mxs_phy->phy);
  363. return 0;
  364. }
  365. #ifdef CONFIG_PM_SLEEP
  366. static void mxs_phy_enable_ldo_in_suspend(struct mxs_phy *mxs_phy, bool on)
  367. {
  368. unsigned int reg = on ? ANADIG_ANA_MISC0_SET : ANADIG_ANA_MISC0_CLR;
  369. /* If the SoCs don't have anatop, quit */
  370. if (!mxs_phy->regmap_anatop)
  371. return;
  372. if (is_imx6q_phy(mxs_phy))
  373. regmap_write(mxs_phy->regmap_anatop, reg,
  374. BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG);
  375. else if (is_imx6sl_phy(mxs_phy))
  376. regmap_write(mxs_phy->regmap_anatop,
  377. reg, BM_ANADIG_ANA_MISC0_STOP_MODE_CONFIG_SL);
  378. }
  379. static int mxs_phy_system_suspend(struct device *dev)
  380. {
  381. struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
  382. if (device_may_wakeup(dev))
  383. mxs_phy_enable_ldo_in_suspend(mxs_phy, true);
  384. return 0;
  385. }
  386. static int mxs_phy_system_resume(struct device *dev)
  387. {
  388. struct mxs_phy *mxs_phy = dev_get_drvdata(dev);
  389. if (device_may_wakeup(dev))
  390. mxs_phy_enable_ldo_in_suspend(mxs_phy, false);
  391. return 0;
  392. }
  393. #endif /* CONFIG_PM_SLEEP */
  394. static SIMPLE_DEV_PM_OPS(mxs_phy_pm, mxs_phy_system_suspend,
  395. mxs_phy_system_resume);
  396. static struct platform_driver mxs_phy_driver = {
  397. .probe = mxs_phy_probe,
  398. .remove = mxs_phy_remove,
  399. .driver = {
  400. .name = DRIVER_NAME,
  401. .owner = THIS_MODULE,
  402. .of_match_table = mxs_phy_dt_ids,
  403. .pm = &mxs_phy_pm,
  404. },
  405. };
  406. static int __init mxs_phy_module_init(void)
  407. {
  408. return platform_driver_register(&mxs_phy_driver);
  409. }
  410. postcore_initcall(mxs_phy_module_init);
  411. static void __exit mxs_phy_module_exit(void)
  412. {
  413. platform_driver_unregister(&mxs_phy_driver);
  414. }
  415. module_exit(mxs_phy_module_exit);
  416. MODULE_ALIAS("platform:mxs-usb-phy");
  417. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  418. MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
  419. MODULE_DESCRIPTION("Freescale MXS USB PHY driver");
  420. MODULE_LICENSE("GPL");