dwc3-imx8mp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dwc3-imx8mp.c - NXP imx8mp Specific Glue layer
  4. *
  5. * Copyright (c) 2020 NXP.
  6. */
  7. #include <linux/cleanup.h>
  8. #include <linux/clk.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include "core.h"
  18. /* USB wakeup registers */
  19. #define USB_WAKEUP_CTRL 0x00
  20. /* Global wakeup interrupt enable, also used to clear interrupt */
  21. #define USB_WAKEUP_EN BIT(31)
  22. /* Wakeup from connect or disconnect, only for superspeed */
  23. #define USB_WAKEUP_SS_CONN BIT(5)
  24. /* 0 select vbus_valid, 1 select sessvld */
  25. #define USB_WAKEUP_VBUS_SRC_SESS_VAL BIT(4)
  26. /* Enable signal for wake up from u3 state */
  27. #define USB_WAKEUP_U3_EN BIT(3)
  28. /* Enable signal for wake up from id change */
  29. #define USB_WAKEUP_ID_EN BIT(2)
  30. /* Enable signal for wake up from vbus change */
  31. #define USB_WAKEUP_VBUS_EN BIT(1)
  32. /* Enable signal for wake up from dp/dm change */
  33. #define USB_WAKEUP_DPDM_EN BIT(0)
  34. #define USB_WAKEUP_EN_MASK GENMASK(5, 0)
  35. /* USB glue registers */
  36. #define USB_CTRL0 0x00
  37. #define USB_CTRL1 0x04
  38. #define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
  39. #define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
  40. #define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
  41. #define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
  42. #define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
  43. struct dwc3_imx8mp {
  44. struct device *dev;
  45. struct platform_device *dwc3_pdev;
  46. void __iomem *hsio_blk_base;
  47. void __iomem *glue_base;
  48. struct clk *hsio_clk;
  49. struct clk *suspend_clk;
  50. int irq;
  51. bool pm_suspended;
  52. bool wakeup_pending;
  53. };
  54. static void imx8mp_configure_glue(struct dwc3_imx8mp *dwc3_imx)
  55. {
  56. struct device *dev = dwc3_imx->dev;
  57. u32 value;
  58. if (!dwc3_imx->glue_base)
  59. return;
  60. value = readl(dwc3_imx->glue_base + USB_CTRL0);
  61. if (device_property_read_bool(dev, "fsl,permanently-attached"))
  62. value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
  63. else
  64. value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
  65. if (device_property_read_bool(dev, "fsl,disable-port-power-control"))
  66. value &= ~(USB_CTRL0_PORTPWR_EN);
  67. else
  68. value |= USB_CTRL0_PORTPWR_EN;
  69. writel(value, dwc3_imx->glue_base + USB_CTRL0);
  70. value = readl(dwc3_imx->glue_base + USB_CTRL1);
  71. if (device_property_read_bool(dev, "fsl,over-current-active-low"))
  72. value |= USB_CTRL1_OC_POLARITY;
  73. else
  74. value &= ~USB_CTRL1_OC_POLARITY;
  75. if (device_property_read_bool(dev, "fsl,power-active-low"))
  76. value |= USB_CTRL1_PWR_POLARITY;
  77. else
  78. value &= ~USB_CTRL1_PWR_POLARITY;
  79. writel(value, dwc3_imx->glue_base + USB_CTRL1);
  80. }
  81. static void dwc3_imx8mp_wakeup_enable(struct dwc3_imx8mp *dwc3_imx,
  82. pm_message_t msg)
  83. {
  84. struct dwc3 *dwc3 = platform_get_drvdata(dwc3_imx->dwc3_pdev);
  85. u32 val;
  86. if (!dwc3)
  87. return;
  88. val = readl(dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
  89. if ((dwc3->current_dr_role == DWC3_GCTL_PRTCAP_HOST) && dwc3->xhci) {
  90. val |= USB_WAKEUP_EN | USB_WAKEUP_DPDM_EN;
  91. if (PMSG_IS_AUTO(msg))
  92. val |= USB_WAKEUP_SS_CONN | USB_WAKEUP_U3_EN;
  93. } else {
  94. val |= USB_WAKEUP_EN | USB_WAKEUP_VBUS_EN |
  95. USB_WAKEUP_VBUS_SRC_SESS_VAL;
  96. }
  97. writel(val, dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
  98. }
  99. static void dwc3_imx8mp_wakeup_disable(struct dwc3_imx8mp *dwc3_imx)
  100. {
  101. u32 val;
  102. val = readl(dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
  103. val &= ~(USB_WAKEUP_EN | USB_WAKEUP_EN_MASK);
  104. writel(val, dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
  105. }
  106. static const struct property_entry dwc3_imx8mp_properties[] = {
  107. PROPERTY_ENTRY_BOOL("xhci-missing-cas-quirk"),
  108. PROPERTY_ENTRY_BOOL("xhci-skip-phy-init-quirk"),
  109. {},
  110. };
  111. static const struct software_node dwc3_imx8mp_swnode = {
  112. .properties = dwc3_imx8mp_properties,
  113. };
  114. static irqreturn_t dwc3_imx8mp_interrupt(int irq, void *_dwc3_imx)
  115. {
  116. struct dwc3_imx8mp *dwc3_imx = _dwc3_imx;
  117. struct dwc3 *dwc = platform_get_drvdata(dwc3_imx->dwc3_pdev);
  118. if (!dwc3_imx->pm_suspended)
  119. return IRQ_HANDLED;
  120. disable_irq_nosync(dwc3_imx->irq);
  121. dwc3_imx->wakeup_pending = true;
  122. if ((dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST) && dwc->xhci)
  123. pm_runtime_resume(&dwc->xhci->dev);
  124. else if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE)
  125. pm_runtime_get(dwc->dev);
  126. return IRQ_HANDLED;
  127. }
  128. static void dwc3_imx_pre_set_role(struct dwc3 *dwc, enum usb_role role)
  129. {
  130. if (role == USB_ROLE_HOST)
  131. /*
  132. * For xhci host, we need disable dwc core auto
  133. * suspend, because during this auto suspend delay(5s),
  134. * xhci host RUN_STOP is cleared and wakeup is not
  135. * enabled, if device is inserted, xhci host can't
  136. * response the connection.
  137. */
  138. pm_runtime_dont_use_autosuspend(dwc->dev);
  139. else
  140. pm_runtime_use_autosuspend(dwc->dev);
  141. }
  142. struct dwc3_glue_ops dwc3_imx_glue_ops = {
  143. .pre_set_role = dwc3_imx_pre_set_role,
  144. };
  145. static int dwc3_imx8mp_probe(struct platform_device *pdev)
  146. {
  147. struct device *dev = &pdev->dev;
  148. struct device_node *node = dev->of_node;
  149. struct dwc3_imx8mp *dwc3_imx;
  150. struct dwc3 *dwc3;
  151. struct resource *res;
  152. int err, irq;
  153. if (!node) {
  154. dev_err(dev, "device node not found\n");
  155. return -EINVAL;
  156. }
  157. dwc3_imx = devm_kzalloc(dev, sizeof(*dwc3_imx), GFP_KERNEL);
  158. if (!dwc3_imx)
  159. return -ENOMEM;
  160. platform_set_drvdata(pdev, dwc3_imx);
  161. dwc3_imx->dev = dev;
  162. dwc3_imx->hsio_blk_base = devm_platform_ioremap_resource(pdev, 0);
  163. if (IS_ERR(dwc3_imx->hsio_blk_base))
  164. return PTR_ERR(dwc3_imx->hsio_blk_base);
  165. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  166. if (!res) {
  167. dev_warn(dev, "Base address for glue layer missing. Continuing without, some features are missing though.");
  168. } else {
  169. dwc3_imx->glue_base = devm_ioremap_resource(dev, res);
  170. if (IS_ERR(dwc3_imx->glue_base))
  171. return PTR_ERR(dwc3_imx->glue_base);
  172. }
  173. dwc3_imx->hsio_clk = devm_clk_get_enabled(dev, "hsio");
  174. if (IS_ERR(dwc3_imx->hsio_clk))
  175. return dev_err_probe(dev, PTR_ERR(dwc3_imx->hsio_clk),
  176. "Failed to get hsio clk\n");
  177. dwc3_imx->suspend_clk = devm_clk_get_enabled(dev, "suspend");
  178. if (IS_ERR(dwc3_imx->suspend_clk))
  179. return dev_err_probe(dev, PTR_ERR(dwc3_imx->suspend_clk),
  180. "Failed to get suspend clk\n");
  181. irq = platform_get_irq(pdev, 0);
  182. if (irq < 0)
  183. return irq;
  184. dwc3_imx->irq = irq;
  185. struct device_node *dwc3_np __free(device_node) = of_get_compatible_child(node,
  186. "snps,dwc3");
  187. if (!dwc3_np)
  188. return dev_err_probe(dev, -ENODEV, "failed to find dwc3 core child\n");
  189. imx8mp_configure_glue(dwc3_imx);
  190. pm_runtime_set_active(dev);
  191. pm_runtime_enable(dev);
  192. err = pm_runtime_get_sync(dev);
  193. if (err < 0)
  194. goto disable_rpm;
  195. err = device_add_software_node(dev, &dwc3_imx8mp_swnode);
  196. if (err) {
  197. err = -ENODEV;
  198. dev_err(dev, "failed to add software node\n");
  199. goto disable_rpm;
  200. }
  201. err = of_platform_populate(node, NULL, NULL, dev);
  202. if (err) {
  203. dev_err(&pdev->dev, "failed to create dwc3 core\n");
  204. goto remove_swnode;
  205. }
  206. dwc3_imx->dwc3_pdev = of_find_device_by_node(dwc3_np);
  207. if (!dwc3_imx->dwc3_pdev) {
  208. dev_err(dev, "failed to get dwc3 platform device\n");
  209. err = -ENODEV;
  210. goto depopulate;
  211. }
  212. dwc3 = platform_get_drvdata(dwc3_imx->dwc3_pdev);
  213. if (!dwc3) {
  214. err = dev_err_probe(dev, -EPROBE_DEFER, "failed to get dwc3 platform data\n");
  215. goto put_dwc3;
  216. }
  217. dwc3->glue_ops = &dwc3_imx_glue_ops;
  218. if (dwc3->dr_mode == USB_DR_MODE_HOST)
  219. pm_runtime_dont_use_autosuspend(dwc3->dev);
  220. err = devm_request_threaded_irq(dev, irq, NULL, dwc3_imx8mp_interrupt,
  221. IRQF_ONESHOT, dev_name(dev), dwc3_imx);
  222. if (err) {
  223. dev_err(dev, "failed to request IRQ #%d --> %d\n", irq, err);
  224. goto put_dwc3;
  225. }
  226. device_set_wakeup_capable(dev, true);
  227. pm_runtime_put(dev);
  228. return 0;
  229. put_dwc3:
  230. put_device(&dwc3_imx->dwc3_pdev->dev);
  231. depopulate:
  232. of_platform_depopulate(dev);
  233. remove_swnode:
  234. device_remove_software_node(dev);
  235. disable_rpm:
  236. pm_runtime_disable(dev);
  237. pm_runtime_put_noidle(dev);
  238. return err;
  239. }
  240. static void dwc3_imx8mp_remove(struct platform_device *pdev)
  241. {
  242. struct dwc3_imx8mp *dwc3_imx = platform_get_drvdata(pdev);
  243. struct device *dev = &pdev->dev;
  244. put_device(&dwc3_imx->dwc3_pdev->dev);
  245. pm_runtime_get_sync(dev);
  246. of_platform_depopulate(dev);
  247. device_remove_software_node(dev);
  248. pm_runtime_disable(dev);
  249. pm_runtime_put_noidle(dev);
  250. }
  251. static int dwc3_imx8mp_suspend(struct dwc3_imx8mp *dwc3_imx, pm_message_t msg)
  252. {
  253. if (dwc3_imx->pm_suspended)
  254. return 0;
  255. /* Wakeup enable */
  256. if (PMSG_IS_AUTO(msg) || device_may_wakeup(dwc3_imx->dev))
  257. dwc3_imx8mp_wakeup_enable(dwc3_imx, msg);
  258. dwc3_imx->pm_suspended = true;
  259. return 0;
  260. }
  261. static int dwc3_imx8mp_resume(struct dwc3_imx8mp *dwc3_imx, pm_message_t msg)
  262. {
  263. struct dwc3 *dwc = platform_get_drvdata(dwc3_imx->dwc3_pdev);
  264. int ret = 0;
  265. if (!dwc3_imx->pm_suspended)
  266. return 0;
  267. /* Wakeup disable */
  268. dwc3_imx8mp_wakeup_disable(dwc3_imx);
  269. dwc3_imx->pm_suspended = false;
  270. /* Upon power loss any previous configuration is lost, restore it */
  271. imx8mp_configure_glue(dwc3_imx);
  272. if (dwc3_imx->wakeup_pending) {
  273. dwc3_imx->wakeup_pending = false;
  274. if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE) {
  275. pm_runtime_put_autosuspend(dwc->dev);
  276. } else {
  277. /*
  278. * Add wait for xhci switch from suspend
  279. * clock to normal clock to detect connection.
  280. */
  281. usleep_range(9000, 10000);
  282. }
  283. enable_irq(dwc3_imx->irq);
  284. }
  285. return ret;
  286. }
  287. static int dwc3_imx8mp_pm_suspend(struct device *dev)
  288. {
  289. struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
  290. int ret;
  291. ret = dwc3_imx8mp_suspend(dwc3_imx, PMSG_SUSPEND);
  292. if (device_may_wakeup(dwc3_imx->dev)) {
  293. enable_irq_wake(dwc3_imx->irq);
  294. if (device_is_compatible(dev, "fsl,imx95-dwc3"))
  295. device_set_out_band_wakeup(dev);
  296. } else {
  297. clk_disable_unprepare(dwc3_imx->suspend_clk);
  298. }
  299. clk_disable_unprepare(dwc3_imx->hsio_clk);
  300. dev_dbg(dev, "dwc3 imx8mp pm suspend.\n");
  301. return ret;
  302. }
  303. static int dwc3_imx8mp_pm_resume(struct device *dev)
  304. {
  305. struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
  306. int ret;
  307. if (device_may_wakeup(dwc3_imx->dev)) {
  308. disable_irq_wake(dwc3_imx->irq);
  309. } else {
  310. ret = clk_prepare_enable(dwc3_imx->suspend_clk);
  311. if (ret)
  312. return ret;
  313. }
  314. ret = clk_prepare_enable(dwc3_imx->hsio_clk);
  315. if (ret) {
  316. clk_disable_unprepare(dwc3_imx->suspend_clk);
  317. return ret;
  318. }
  319. ret = dwc3_imx8mp_resume(dwc3_imx, PMSG_RESUME);
  320. pm_runtime_disable(dev);
  321. pm_runtime_set_active(dev);
  322. pm_runtime_enable(dev);
  323. dev_dbg(dev, "dwc3 imx8mp pm resume.\n");
  324. return ret;
  325. }
  326. static int dwc3_imx8mp_runtime_suspend(struct device *dev)
  327. {
  328. struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
  329. dev_dbg(dev, "dwc3 imx8mp runtime suspend.\n");
  330. return dwc3_imx8mp_suspend(dwc3_imx, PMSG_AUTO_SUSPEND);
  331. }
  332. static int dwc3_imx8mp_runtime_resume(struct device *dev)
  333. {
  334. struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
  335. dev_dbg(dev, "dwc3 imx8mp runtime resume.\n");
  336. return dwc3_imx8mp_resume(dwc3_imx, PMSG_AUTO_RESUME);
  337. }
  338. static const struct dev_pm_ops dwc3_imx8mp_dev_pm_ops = {
  339. SYSTEM_SLEEP_PM_OPS(dwc3_imx8mp_pm_suspend, dwc3_imx8mp_pm_resume)
  340. RUNTIME_PM_OPS(dwc3_imx8mp_runtime_suspend, dwc3_imx8mp_runtime_resume,
  341. NULL)
  342. };
  343. static const struct of_device_id dwc3_imx8mp_of_match[] = {
  344. { .compatible = "fsl,imx8mp-dwc3", },
  345. {},
  346. };
  347. MODULE_DEVICE_TABLE(of, dwc3_imx8mp_of_match);
  348. static struct platform_driver dwc3_imx8mp_driver = {
  349. .probe = dwc3_imx8mp_probe,
  350. .remove = dwc3_imx8mp_remove,
  351. .driver = {
  352. .name = "imx8mp-dwc3",
  353. .pm = pm_ptr(&dwc3_imx8mp_dev_pm_ops),
  354. .of_match_table = dwc3_imx8mp_of_match,
  355. },
  356. };
  357. module_platform_driver(dwc3_imx8mp_driver);
  358. MODULE_ALIAS("platform:imx8mp-dwc3");
  359. MODULE_AUTHOR("jun.li@nxp.com");
  360. MODULE_LICENSE("GPL v2");
  361. MODULE_DESCRIPTION("DesignWare USB3 imx8mp Glue Layer");