1 // SPDX-License-Identifier: GPL-2.0
3 * dwc3-imx8mp.c - NXP imx8mp Specific Glue layer
5 * Copyright (c) 2020 NXP.
9 #include <linux/interrupt.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
14 #include <linux/of_platform.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
20 /* USB wakeup registers */
21 #define USB_WAKEUP_CTRL 0x00
23 /* Global wakeup interrupt enable, also used to clear interrupt */
24 #define USB_WAKEUP_EN BIT(31)
25 /* Wakeup from connect or disconnect, only for superspeed */
26 #define USB_WAKEUP_SS_CONN BIT(5)
27 /* 0 select vbus_valid, 1 select sessvld */
28 #define USB_WAKEUP_VBUS_SRC_SESS_VAL BIT(4)
29 /* Enable signal for wake up from u3 state */
30 #define USB_WAKEUP_U3_EN BIT(3)
31 /* Enable signal for wake up from id change */
32 #define USB_WAKEUP_ID_EN BIT(2)
33 /* Enable signal for wake up from vbus change */
34 #define USB_WAKEUP_VBUS_EN BIT(1)
35 /* Enable signal for wake up from dp/dm change */
36 #define USB_WAKEUP_DPDM_EN BIT(0)
38 #define USB_WAKEUP_EN_MASK GENMASK(5, 0)
40 /* USB glue registers */
41 #define USB_CTRL0 0x00
42 #define USB_CTRL1 0x04
44 #define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
45 #define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
46 #define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
48 #define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
49 #define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
53 struct platform_device *dwc3;
54 void __iomem *hsio_blk_base;
55 void __iomem *glue_base;
57 struct clk *suspend_clk;
63 static void imx8mp_configure_glue(struct dwc3_imx8mp *dwc3_imx)
65 struct device *dev = dwc3_imx->dev;
68 if (!dwc3_imx->glue_base)
71 value = readl(dwc3_imx->glue_base + USB_CTRL0);
73 if (device_property_read_bool(dev, "fsl,permanently-attached"))
74 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
76 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
78 if (device_property_read_bool(dev, "fsl,disable-port-power-control"))
79 value &= ~(USB_CTRL0_PORTPWR_EN);
81 value |= USB_CTRL0_PORTPWR_EN;
83 writel(value, dwc3_imx->glue_base + USB_CTRL0);
85 value = readl(dwc3_imx->glue_base + USB_CTRL1);
86 if (device_property_read_bool(dev, "fsl,over-current-active-low"))
87 value |= USB_CTRL1_OC_POLARITY;
89 value &= ~USB_CTRL1_OC_POLARITY;
91 if (device_property_read_bool(dev, "fsl,power-active-low"))
92 value |= USB_CTRL1_PWR_POLARITY;
94 value &= ~USB_CTRL1_PWR_POLARITY;
96 writel(value, dwc3_imx->glue_base + USB_CTRL1);
99 static void dwc3_imx8mp_wakeup_enable(struct dwc3_imx8mp *dwc3_imx)
101 struct dwc3 *dwc3 = platform_get_drvdata(dwc3_imx->dwc3);
107 val = readl(dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
109 if ((dwc3->current_dr_role == DWC3_GCTL_PRTCAP_HOST) && dwc3->xhci)
110 val |= USB_WAKEUP_EN | USB_WAKEUP_SS_CONN |
111 USB_WAKEUP_U3_EN | USB_WAKEUP_DPDM_EN;
112 else if (dwc3->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE)
113 val |= USB_WAKEUP_EN | USB_WAKEUP_VBUS_EN |
114 USB_WAKEUP_VBUS_SRC_SESS_VAL;
116 writel(val, dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
119 static void dwc3_imx8mp_wakeup_disable(struct dwc3_imx8mp *dwc3_imx)
123 val = readl(dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
124 val &= ~(USB_WAKEUP_EN | USB_WAKEUP_EN_MASK);
125 writel(val, dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
128 static irqreturn_t dwc3_imx8mp_interrupt(int irq, void *_dwc3_imx)
130 struct dwc3_imx8mp *dwc3_imx = _dwc3_imx;
131 struct dwc3 *dwc = platform_get_drvdata(dwc3_imx->dwc3);
133 if (!dwc3_imx->pm_suspended)
136 disable_irq_nosync(dwc3_imx->irq);
137 dwc3_imx->wakeup_pending = true;
139 if ((dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST) && dwc->xhci)
140 pm_runtime_resume(&dwc->xhci->dev);
141 else if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE)
142 pm_runtime_get(dwc->dev);
147 static int dwc3_imx8mp_probe(struct platform_device *pdev)
149 struct device *dev = &pdev->dev;
150 struct device_node *dwc3_np, *node = dev->of_node;
151 struct dwc3_imx8mp *dwc3_imx;
152 struct resource *res;
156 dev_err(dev, "device node not found\n");
160 dwc3_imx = devm_kzalloc(dev, sizeof(*dwc3_imx), GFP_KERNEL);
164 platform_set_drvdata(pdev, dwc3_imx);
168 dwc3_imx->hsio_blk_base = devm_platform_ioremap_resource(pdev, 0);
169 if (IS_ERR(dwc3_imx->hsio_blk_base))
170 return PTR_ERR(dwc3_imx->hsio_blk_base);
172 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
174 dev_warn(dev, "Base address for glue layer missing. Continuing without, some features are missing though.");
176 dwc3_imx->glue_base = devm_ioremap_resource(dev, res);
177 if (IS_ERR(dwc3_imx->glue_base))
178 return PTR_ERR(dwc3_imx->glue_base);
181 dwc3_imx->hsio_clk = devm_clk_get(dev, "hsio");
182 if (IS_ERR(dwc3_imx->hsio_clk)) {
183 err = PTR_ERR(dwc3_imx->hsio_clk);
184 dev_err(dev, "Failed to get hsio clk, err=%d\n", err);
188 err = clk_prepare_enable(dwc3_imx->hsio_clk);
190 dev_err(dev, "Failed to enable hsio clk, err=%d\n", err);
194 dwc3_imx->suspend_clk = devm_clk_get(dev, "suspend");
195 if (IS_ERR(dwc3_imx->suspend_clk)) {
196 err = PTR_ERR(dwc3_imx->suspend_clk);
197 dev_err(dev, "Failed to get suspend clk, err=%d\n", err);
198 goto disable_hsio_clk;
201 err = clk_prepare_enable(dwc3_imx->suspend_clk);
203 dev_err(dev, "Failed to enable suspend clk, err=%d\n", err);
204 goto disable_hsio_clk;
207 irq = platform_get_irq(pdev, 0);
214 imx8mp_configure_glue(dwc3_imx);
216 pm_runtime_set_active(dev);
217 pm_runtime_enable(dev);
218 err = pm_runtime_get_sync(dev);
222 dwc3_np = of_get_compatible_child(node, "snps,dwc3");
225 dev_err(dev, "failed to find dwc3 core child\n");
229 err = of_platform_populate(node, NULL, NULL, dev);
231 dev_err(&pdev->dev, "failed to create dwc3 core\n");
235 dwc3_imx->dwc3 = of_find_device_by_node(dwc3_np);
236 if (!dwc3_imx->dwc3) {
237 dev_err(dev, "failed to get dwc3 platform device\n");
241 of_node_put(dwc3_np);
243 err = devm_request_threaded_irq(dev, irq, NULL, dwc3_imx8mp_interrupt,
244 IRQF_ONESHOT, dev_name(dev), dwc3_imx);
246 dev_err(dev, "failed to request IRQ #%d --> %d\n", irq, err);
250 device_set_wakeup_capable(dev, true);
256 of_platform_depopulate(dev);
258 of_node_put(dwc3_np);
260 pm_runtime_disable(dev);
261 pm_runtime_put_noidle(dev);
263 clk_disable_unprepare(dwc3_imx->suspend_clk);
265 clk_disable_unprepare(dwc3_imx->hsio_clk);
270 static void dwc3_imx8mp_remove(struct platform_device *pdev)
272 struct dwc3_imx8mp *dwc3_imx = platform_get_drvdata(pdev);
273 struct device *dev = &pdev->dev;
275 pm_runtime_get_sync(dev);
276 of_platform_depopulate(dev);
278 clk_disable_unprepare(dwc3_imx->suspend_clk);
279 clk_disable_unprepare(dwc3_imx->hsio_clk);
281 pm_runtime_disable(dev);
282 pm_runtime_put_noidle(dev);
285 static int __maybe_unused dwc3_imx8mp_suspend(struct dwc3_imx8mp *dwc3_imx,
288 if (dwc3_imx->pm_suspended)
292 if (PMSG_IS_AUTO(msg) || device_may_wakeup(dwc3_imx->dev))
293 dwc3_imx8mp_wakeup_enable(dwc3_imx);
295 dwc3_imx->pm_suspended = true;
300 static int __maybe_unused dwc3_imx8mp_resume(struct dwc3_imx8mp *dwc3_imx,
303 struct dwc3 *dwc = platform_get_drvdata(dwc3_imx->dwc3);
306 if (!dwc3_imx->pm_suspended)
310 dwc3_imx8mp_wakeup_disable(dwc3_imx);
311 dwc3_imx->pm_suspended = false;
313 /* Upon power loss any previous configuration is lost, restore it */
314 imx8mp_configure_glue(dwc3_imx);
316 if (dwc3_imx->wakeup_pending) {
317 dwc3_imx->wakeup_pending = false;
318 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE) {
319 pm_runtime_mark_last_busy(dwc->dev);
320 pm_runtime_put_autosuspend(dwc->dev);
323 * Add wait for xhci switch from suspend
324 * clock to normal clock to detect connection.
326 usleep_range(9000, 10000);
328 enable_irq(dwc3_imx->irq);
334 static int __maybe_unused dwc3_imx8mp_pm_suspend(struct device *dev)
336 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
339 ret = dwc3_imx8mp_suspend(dwc3_imx, PMSG_SUSPEND);
341 if (device_may_wakeup(dwc3_imx->dev))
342 enable_irq_wake(dwc3_imx->irq);
344 clk_disable_unprepare(dwc3_imx->suspend_clk);
346 clk_disable_unprepare(dwc3_imx->hsio_clk);
347 dev_dbg(dev, "dwc3 imx8mp pm suspend.\n");
352 static int __maybe_unused dwc3_imx8mp_pm_resume(struct device *dev)
354 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
357 if (device_may_wakeup(dwc3_imx->dev)) {
358 disable_irq_wake(dwc3_imx->irq);
360 ret = clk_prepare_enable(dwc3_imx->suspend_clk);
365 ret = clk_prepare_enable(dwc3_imx->hsio_clk);
369 ret = dwc3_imx8mp_resume(dwc3_imx, PMSG_RESUME);
371 pm_runtime_disable(dev);
372 pm_runtime_set_active(dev);
373 pm_runtime_enable(dev);
375 dev_dbg(dev, "dwc3 imx8mp pm resume.\n");
380 static int __maybe_unused dwc3_imx8mp_runtime_suspend(struct device *dev)
382 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
384 dev_dbg(dev, "dwc3 imx8mp runtime suspend.\n");
386 return dwc3_imx8mp_suspend(dwc3_imx, PMSG_AUTO_SUSPEND);
389 static int __maybe_unused dwc3_imx8mp_runtime_resume(struct device *dev)
391 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
393 dev_dbg(dev, "dwc3 imx8mp runtime resume.\n");
395 return dwc3_imx8mp_resume(dwc3_imx, PMSG_AUTO_RESUME);
398 static const struct dev_pm_ops dwc3_imx8mp_dev_pm_ops = {
399 SET_SYSTEM_SLEEP_PM_OPS(dwc3_imx8mp_pm_suspend, dwc3_imx8mp_pm_resume)
400 SET_RUNTIME_PM_OPS(dwc3_imx8mp_runtime_suspend,
401 dwc3_imx8mp_runtime_resume, NULL)
404 static const struct of_device_id dwc3_imx8mp_of_match[] = {
405 { .compatible = "fsl,imx8mp-dwc3", },
408 MODULE_DEVICE_TABLE(of, dwc3_imx8mp_of_match);
410 static struct platform_driver dwc3_imx8mp_driver = {
411 .probe = dwc3_imx8mp_probe,
412 .remove_new = dwc3_imx8mp_remove,
414 .name = "imx8mp-dwc3",
415 .pm = &dwc3_imx8mp_dev_pm_ops,
416 .of_match_table = dwc3_imx8mp_of_match,
420 module_platform_driver(dwc3_imx8mp_driver);
422 MODULE_ALIAS("platform:imx8mp-dwc3");
424 MODULE_LICENSE("GPL v2");
425 MODULE_DESCRIPTION("DesignWare USB3 imx8mp Glue Layer");