1 // SPDX-License-Identifier: GPL-2.0
3 * dwc3-imx8mp.c - NXP imx8mp Specific Glue layer
5 * Copyright (c) 2020 NXP.
8 #include <linux/cleanup.h>
10 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
21 /* USB wakeup registers */
22 #define USB_WAKEUP_CTRL 0x00
24 /* Global wakeup interrupt enable, also used to clear interrupt */
25 #define USB_WAKEUP_EN BIT(31)
26 /* Wakeup from connect or disconnect, only for superspeed */
27 #define USB_WAKEUP_SS_CONN BIT(5)
28 /* 0 select vbus_valid, 1 select sessvld */
29 #define USB_WAKEUP_VBUS_SRC_SESS_VAL BIT(4)
30 /* Enable signal for wake up from u3 state */
31 #define USB_WAKEUP_U3_EN BIT(3)
32 /* Enable signal for wake up from id change */
33 #define USB_WAKEUP_ID_EN BIT(2)
34 /* Enable signal for wake up from vbus change */
35 #define USB_WAKEUP_VBUS_EN BIT(1)
36 /* Enable signal for wake up from dp/dm change */
37 #define USB_WAKEUP_DPDM_EN BIT(0)
39 #define USB_WAKEUP_EN_MASK GENMASK(5, 0)
41 /* USB glue registers */
42 #define USB_CTRL0 0x00
43 #define USB_CTRL1 0x04
45 #define USB_CTRL0_PORTPWR_EN BIT(12) /* 1 - PPC enabled (default) */
46 #define USB_CTRL0_USB3_FIXED BIT(22) /* 1 - USB3 permanent attached */
47 #define USB_CTRL0_USB2_FIXED BIT(23) /* 1 - USB2 permanent attached */
49 #define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
50 #define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
54 struct platform_device *dwc3;
55 void __iomem *hsio_blk_base;
56 void __iomem *glue_base;
58 struct clk *suspend_clk;
64 static void imx8mp_configure_glue(struct dwc3_imx8mp *dwc3_imx)
66 struct device *dev = dwc3_imx->dev;
69 if (!dwc3_imx->glue_base)
72 value = readl(dwc3_imx->glue_base + USB_CTRL0);
74 if (device_property_read_bool(dev, "fsl,permanently-attached"))
75 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
77 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
79 if (device_property_read_bool(dev, "fsl,disable-port-power-control"))
80 value &= ~(USB_CTRL0_PORTPWR_EN);
82 value |= USB_CTRL0_PORTPWR_EN;
84 writel(value, dwc3_imx->glue_base + USB_CTRL0);
86 value = readl(dwc3_imx->glue_base + USB_CTRL1);
87 if (device_property_read_bool(dev, "fsl,over-current-active-low"))
88 value |= USB_CTRL1_OC_POLARITY;
90 value &= ~USB_CTRL1_OC_POLARITY;
92 if (device_property_read_bool(dev, "fsl,power-active-low"))
93 value |= USB_CTRL1_PWR_POLARITY;
95 value &= ~USB_CTRL1_PWR_POLARITY;
97 writel(value, dwc3_imx->glue_base + USB_CTRL1);
100 static void dwc3_imx8mp_wakeup_enable(struct dwc3_imx8mp *dwc3_imx,
103 struct dwc3 *dwc3 = platform_get_drvdata(dwc3_imx->dwc3);
109 val = readl(dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
111 if ((dwc3->current_dr_role == DWC3_GCTL_PRTCAP_HOST) && dwc3->xhci) {
112 val |= USB_WAKEUP_EN | USB_WAKEUP_DPDM_EN;
113 if (PMSG_IS_AUTO(msg))
114 val |= USB_WAKEUP_SS_CONN | USB_WAKEUP_U3_EN;
116 val |= USB_WAKEUP_EN | USB_WAKEUP_VBUS_EN |
117 USB_WAKEUP_VBUS_SRC_SESS_VAL;
120 writel(val, dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
123 static void dwc3_imx8mp_wakeup_disable(struct dwc3_imx8mp *dwc3_imx)
127 val = readl(dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
128 val &= ~(USB_WAKEUP_EN | USB_WAKEUP_EN_MASK);
129 writel(val, dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
132 static irqreturn_t dwc3_imx8mp_interrupt(int irq, void *_dwc3_imx)
134 struct dwc3_imx8mp *dwc3_imx = _dwc3_imx;
135 struct dwc3 *dwc = platform_get_drvdata(dwc3_imx->dwc3);
137 if (!dwc3_imx->pm_suspended)
140 disable_irq_nosync(dwc3_imx->irq);
141 dwc3_imx->wakeup_pending = true;
143 if ((dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST) && dwc->xhci)
144 pm_runtime_resume(&dwc->xhci->dev);
145 else if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE)
146 pm_runtime_get(dwc->dev);
151 static int dwc3_imx8mp_set_software_node(struct device *dev)
153 struct property_entry props[3] = { 0 };
156 props[prop_idx++] = PROPERTY_ENTRY_BOOL("xhci-missing-cas-quirk");
157 props[prop_idx++] = PROPERTY_ENTRY_BOOL("xhci-skip-phy-init-quirk");
159 return device_create_managed_software_node(dev, props, NULL);
162 static int dwc3_imx8mp_probe(struct platform_device *pdev)
164 struct device *dev = &pdev->dev;
165 struct device_node *node = dev->of_node;
166 struct dwc3_imx8mp *dwc3_imx;
167 struct resource *res;
171 dev_err(dev, "device node not found\n");
175 dwc3_imx = devm_kzalloc(dev, sizeof(*dwc3_imx), GFP_KERNEL);
179 platform_set_drvdata(pdev, dwc3_imx);
183 dwc3_imx->hsio_blk_base = devm_platform_ioremap_resource(pdev, 0);
184 if (IS_ERR(dwc3_imx->hsio_blk_base))
185 return PTR_ERR(dwc3_imx->hsio_blk_base);
187 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
189 dev_warn(dev, "Base address for glue layer missing. Continuing without, some features are missing though.");
191 dwc3_imx->glue_base = devm_ioremap_resource(dev, res);
192 if (IS_ERR(dwc3_imx->glue_base))
193 return PTR_ERR(dwc3_imx->glue_base);
196 dwc3_imx->hsio_clk = devm_clk_get_enabled(dev, "hsio");
197 if (IS_ERR(dwc3_imx->hsio_clk))
198 return dev_err_probe(dev, PTR_ERR(dwc3_imx->hsio_clk),
199 "Failed to get hsio clk\n");
201 dwc3_imx->suspend_clk = devm_clk_get_enabled(dev, "suspend");
202 if (IS_ERR(dwc3_imx->suspend_clk))
203 return dev_err_probe(dev, PTR_ERR(dwc3_imx->suspend_clk),
204 "Failed to get suspend clk\n");
206 irq = platform_get_irq(pdev, 0);
211 struct device_node *dwc3_np __free(device_node) = of_get_compatible_child(node,
214 return dev_err_probe(dev, -ENODEV, "failed to find dwc3 core child\n");
216 imx8mp_configure_glue(dwc3_imx);
218 pm_runtime_set_active(dev);
219 pm_runtime_enable(dev);
220 err = pm_runtime_get_sync(dev);
224 err = dwc3_imx8mp_set_software_node(dev);
227 dev_err(dev, "failed to create software node\n");
231 err = of_platform_populate(node, NULL, NULL, dev);
233 dev_err(&pdev->dev, "failed to create dwc3 core\n");
237 dwc3_imx->dwc3 = of_find_device_by_node(dwc3_np);
238 if (!dwc3_imx->dwc3) {
239 dev_err(dev, "failed to get dwc3 platform device\n");
244 err = devm_request_threaded_irq(dev, irq, NULL, dwc3_imx8mp_interrupt,
245 IRQF_ONESHOT, dev_name(dev), dwc3_imx);
247 dev_err(dev, "failed to request IRQ #%d --> %d\n", irq, err);
251 device_set_wakeup_capable(dev, true);
257 of_platform_depopulate(dev);
259 pm_runtime_disable(dev);
260 pm_runtime_put_noidle(dev);
265 static void dwc3_imx8mp_remove(struct platform_device *pdev)
267 struct device *dev = &pdev->dev;
269 pm_runtime_get_sync(dev);
270 of_platform_depopulate(dev);
272 pm_runtime_disable(dev);
273 pm_runtime_put_noidle(dev);
276 static int dwc3_imx8mp_suspend(struct dwc3_imx8mp *dwc3_imx, pm_message_t msg)
278 if (dwc3_imx->pm_suspended)
282 if (PMSG_IS_AUTO(msg) || device_may_wakeup(dwc3_imx->dev))
283 dwc3_imx8mp_wakeup_enable(dwc3_imx, msg);
285 dwc3_imx->pm_suspended = true;
290 static int dwc3_imx8mp_resume(struct dwc3_imx8mp *dwc3_imx, pm_message_t msg)
292 struct dwc3 *dwc = platform_get_drvdata(dwc3_imx->dwc3);
295 if (!dwc3_imx->pm_suspended)
299 dwc3_imx8mp_wakeup_disable(dwc3_imx);
300 dwc3_imx->pm_suspended = false;
302 /* Upon power loss any previous configuration is lost, restore it */
303 imx8mp_configure_glue(dwc3_imx);
305 if (dwc3_imx->wakeup_pending) {
306 dwc3_imx->wakeup_pending = false;
307 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE) {
308 pm_runtime_mark_last_busy(dwc->dev);
309 pm_runtime_put_autosuspend(dwc->dev);
312 * Add wait for xhci switch from suspend
313 * clock to normal clock to detect connection.
315 usleep_range(9000, 10000);
317 enable_irq(dwc3_imx->irq);
323 static int dwc3_imx8mp_pm_suspend(struct device *dev)
325 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
328 ret = dwc3_imx8mp_suspend(dwc3_imx, PMSG_SUSPEND);
330 if (device_may_wakeup(dwc3_imx->dev))
331 enable_irq_wake(dwc3_imx->irq);
333 clk_disable_unprepare(dwc3_imx->suspend_clk);
335 clk_disable_unprepare(dwc3_imx->hsio_clk);
336 dev_dbg(dev, "dwc3 imx8mp pm suspend.\n");
341 static int dwc3_imx8mp_pm_resume(struct device *dev)
343 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
346 if (device_may_wakeup(dwc3_imx->dev)) {
347 disable_irq_wake(dwc3_imx->irq);
349 ret = clk_prepare_enable(dwc3_imx->suspend_clk);
354 ret = clk_prepare_enable(dwc3_imx->hsio_clk);
356 clk_disable_unprepare(dwc3_imx->suspend_clk);
360 ret = dwc3_imx8mp_resume(dwc3_imx, PMSG_RESUME);
362 pm_runtime_disable(dev);
363 pm_runtime_set_active(dev);
364 pm_runtime_enable(dev);
366 dev_dbg(dev, "dwc3 imx8mp pm resume.\n");
371 static int dwc3_imx8mp_runtime_suspend(struct device *dev)
373 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
375 dev_dbg(dev, "dwc3 imx8mp runtime suspend.\n");
377 return dwc3_imx8mp_suspend(dwc3_imx, PMSG_AUTO_SUSPEND);
380 static int dwc3_imx8mp_runtime_resume(struct device *dev)
382 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
384 dev_dbg(dev, "dwc3 imx8mp runtime resume.\n");
386 return dwc3_imx8mp_resume(dwc3_imx, PMSG_AUTO_RESUME);
389 static const struct dev_pm_ops dwc3_imx8mp_dev_pm_ops = {
390 SYSTEM_SLEEP_PM_OPS(dwc3_imx8mp_pm_suspend, dwc3_imx8mp_pm_resume)
391 RUNTIME_PM_OPS(dwc3_imx8mp_runtime_suspend, dwc3_imx8mp_runtime_resume,
395 static const struct of_device_id dwc3_imx8mp_of_match[] = {
396 { .compatible = "fsl,imx8mp-dwc3", },
399 MODULE_DEVICE_TABLE(of, dwc3_imx8mp_of_match);
401 static struct platform_driver dwc3_imx8mp_driver = {
402 .probe = dwc3_imx8mp_probe,
403 .remove_new = dwc3_imx8mp_remove,
405 .name = "imx8mp-dwc3",
406 .pm = pm_ptr(&dwc3_imx8mp_dev_pm_ops),
407 .of_match_table = dwc3_imx8mp_of_match,
411 module_platform_driver(dwc3_imx8mp_driver);
413 MODULE_ALIAS("platform:imx8mp-dwc3");
414 MODULE_AUTHOR("jun.li@nxp.com");
415 MODULE_LICENSE("GPL v2");
416 MODULE_DESCRIPTION("DesignWare USB3 imx8mp Glue Layer");