Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * dwc3-imx.c - NXP i.MX Soc USB3 Specific Glue layer
4 *
5 * Copyright 2026 NXP
6 */
7
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_platform.h>
14#include <linux/platform_device.h>
15#include <linux/pm_runtime.h>
16
17#include "core.h"
18#include "glue.h"
19
20/* USB wakeup registers */
21#define USB_WAKEUP_CTRL 0x00
22
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)
37
38#define USB_WAKEUP_EN_MASK GENMASK(5, 0)
39
40/* USB glue registers */
41#define USB_CTRL0 0x00
42#define USB_CTRL1 0x04
43
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 */
47
48#define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
49#define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
50
51struct dwc3_imx {
52 struct dwc3 dwc;
53 struct device *dev;
54 void __iomem *blkctl_base;
55 void __iomem *glue_base;
56 struct clk *hsio_clk;
57 struct clk *suspend_clk;
58 int irq;
59 bool pm_suspended;
60 bool wakeup_pending;
61 unsigned permanent_attached:1;
62 unsigned disable_pwr_ctrl:1;
63 unsigned overcur_active_low:1;
64 unsigned power_active_low:1;
65};
66
67#define to_dwc3_imx(d) container_of((d), struct dwc3_imx, dwc)
68
69static void dwc3_imx_get_property(struct dwc3_imx *dwc_imx)
70{
71 struct device *dev = dwc_imx->dev;
72
73 dwc_imx->permanent_attached =
74 device_property_read_bool(dev, "fsl,permanently-attached");
75 dwc_imx->disable_pwr_ctrl =
76 device_property_read_bool(dev, "fsl,disable-port-power-control");
77 dwc_imx->overcur_active_low =
78 device_property_read_bool(dev, "fsl,over-current-active-low");
79 dwc_imx->power_active_low =
80 device_property_read_bool(dev, "fsl,power-active-low");
81}
82
83static void dwc3_imx_configure_glue(struct dwc3_imx *dwc_imx)
84{
85 u32 value;
86
87 if (!dwc_imx->glue_base)
88 return;
89
90 value = readl(dwc_imx->glue_base + USB_CTRL0);
91
92 if (dwc_imx->permanent_attached)
93 value |= USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED;
94 else
95 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
96
97 if (dwc_imx->disable_pwr_ctrl)
98 value &= ~USB_CTRL0_PORTPWR_EN;
99 else
100 value |= USB_CTRL0_PORTPWR_EN;
101
102 writel(value, dwc_imx->glue_base + USB_CTRL0);
103
104 value = readl(dwc_imx->glue_base + USB_CTRL1);
105 if (dwc_imx->overcur_active_low)
106 value |= USB_CTRL1_OC_POLARITY;
107 else
108 value &= ~USB_CTRL1_OC_POLARITY;
109
110 if (dwc_imx->power_active_low)
111 value |= USB_CTRL1_PWR_POLARITY;
112 else
113 value &= ~USB_CTRL1_PWR_POLARITY;
114
115 writel(value, dwc_imx->glue_base + USB_CTRL1);
116}
117
118static void dwc3_imx_wakeup_enable(struct dwc3_imx *dwc_imx, pm_message_t msg)
119{
120 struct dwc3 *dwc = &dwc_imx->dwc;
121 u32 val;
122
123 val = readl(dwc_imx->blkctl_base + USB_WAKEUP_CTRL);
124
125 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST && dwc->xhci) {
126 val |= USB_WAKEUP_EN | USB_WAKEUP_DPDM_EN;
127 if (PMSG_IS_AUTO(msg))
128 val |= USB_WAKEUP_SS_CONN | USB_WAKEUP_U3_EN;
129 } else {
130 val |= USB_WAKEUP_EN | USB_WAKEUP_VBUS_EN |
131 USB_WAKEUP_VBUS_SRC_SESS_VAL;
132 }
133
134 writel(val, dwc_imx->blkctl_base + USB_WAKEUP_CTRL);
135}
136
137static void dwc3_imx_wakeup_disable(struct dwc3_imx *dwc_imx)
138{
139 u32 val;
140
141 val = readl(dwc_imx->blkctl_base + USB_WAKEUP_CTRL);
142 val &= ~(USB_WAKEUP_EN | USB_WAKEUP_EN_MASK);
143 writel(val, dwc_imx->blkctl_base + USB_WAKEUP_CTRL);
144}
145
146static irqreturn_t dwc3_imx_interrupt(int irq, void *data)
147{
148 struct dwc3_imx *dwc_imx = data;
149 struct dwc3 *dwc = &dwc_imx->dwc;
150
151 if (!dwc_imx->pm_suspended)
152 return IRQ_HANDLED;
153
154 disable_irq_nosync(dwc_imx->irq);
155 dwc_imx->wakeup_pending = true;
156
157 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST && dwc->xhci)
158 pm_runtime_resume(&dwc->xhci->dev);
159 else if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE)
160 pm_runtime_get(dwc->dev);
161
162 return IRQ_HANDLED;
163}
164
165static void dwc3_imx_pre_set_role(struct dwc3 *dwc, enum usb_role role)
166{
167 if (role == USB_ROLE_HOST)
168 /*
169 * For xhci host, we need disable dwc core auto
170 * suspend, because during this auto suspend delay(5s),
171 * xhci host RUN_STOP is cleared and wakeup is not
172 * enabled, if device is inserted, xhci host can't
173 * response the connection.
174 */
175 pm_runtime_dont_use_autosuspend(dwc->dev);
176 else
177 pm_runtime_use_autosuspend(dwc->dev);
178}
179
180static struct dwc3_glue_ops dwc3_imx_glue_ops = {
181 .pre_set_role = dwc3_imx_pre_set_role,
182};
183
184static const struct property_entry dwc3_imx_properties[] = {
185 PROPERTY_ENTRY_BOOL("xhci-missing-cas-quirk"),
186 PROPERTY_ENTRY_BOOL("xhci-skip-phy-init-quirk"),
187 {},
188};
189
190static const struct software_node dwc3_imx_swnode = {
191 .properties = dwc3_imx_properties,
192};
193
194static int dwc3_imx_probe(struct platform_device *pdev)
195{
196 struct device *dev = &pdev->dev;
197 struct dwc3_imx *dwc_imx;
198 struct dwc3 *dwc;
199 struct resource *res;
200 const char *irq_name;
201 struct dwc3_probe_data probe_data = {};
202 int ret, irq;
203
204 dwc_imx = devm_kzalloc(dev, sizeof(*dwc_imx), GFP_KERNEL);
205 if (!dwc_imx)
206 return -ENOMEM;
207
208 platform_set_drvdata(pdev, dwc_imx);
209 dwc_imx->dev = dev;
210
211 dwc3_imx_get_property(dwc_imx);
212
213 dwc_imx->blkctl_base = devm_platform_ioremap_resource_byname(pdev, "blkctl");
214 if (IS_ERR(dwc_imx->blkctl_base))
215 return PTR_ERR(dwc_imx->blkctl_base);
216
217 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "glue");
218 if (!res) {
219 dev_warn(dev, "Base address for glue layer missing\n");
220 } else {
221 dwc_imx->glue_base = devm_ioremap_resource(dev, res);
222 if (IS_ERR(dwc_imx->glue_base))
223 return PTR_ERR(dwc_imx->glue_base);
224 }
225
226 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "core");
227 if (!res)
228 return dev_err_probe(dev, -ENODEV, "missing core memory resource\n");
229
230 dwc_imx->hsio_clk = devm_clk_get_enabled(dev, "hsio");
231 if (IS_ERR(dwc_imx->hsio_clk))
232 return dev_err_probe(dev, PTR_ERR(dwc_imx->hsio_clk),
233 "Failed to get hsio clk\n");
234
235 dwc_imx->suspend_clk = devm_clk_get_enabled(dev, "suspend");
236 if (IS_ERR(dwc_imx->suspend_clk))
237 return dev_err_probe(dev, PTR_ERR(dwc_imx->suspend_clk),
238 "Failed to get suspend clk\n");
239
240 irq = platform_get_irq_byname(pdev, "wakeup");
241 if (irq < 0)
242 return irq;
243 dwc_imx->irq = irq;
244
245 irq_name = devm_kasprintf(dev, GFP_KERNEL, "%s:wakeup", dev_name(dev));
246 if (!irq_name)
247 return dev_err_probe(dev, -ENOMEM, "failed to create irq_name\n");
248
249 ret = devm_request_threaded_irq(dev, irq, NULL, dwc3_imx_interrupt,
250 IRQF_ONESHOT | IRQF_NO_AUTOEN,
251 irq_name, dwc_imx);
252 if (ret)
253 return dev_err_probe(dev, ret, "failed to request IRQ #%d\n", irq);
254
255 ret = device_add_software_node(dev, &dwc3_imx_swnode);
256 if (ret)
257 return dev_err_probe(dev, ret, "failed to add software node\n");
258
259 dwc3_imx_configure_glue(dwc_imx);
260
261 dwc = &dwc_imx->dwc;
262 dwc->dev = dev;
263 dwc->glue_ops = &dwc3_imx_glue_ops;
264
265 probe_data.res = res;
266 probe_data.dwc = dwc;
267 probe_data.properties = DWC3_DEFAULT_PROPERTIES;
268 probe_data.properties.needs_full_reinit = true;
269
270 ret = dwc3_core_probe(&probe_data);
271 if (ret) {
272 device_remove_software_node(dev);
273 return ret;
274 }
275
276 device_set_wakeup_capable(dev, true);
277 return 0;
278}
279
280static void dwc3_imx_remove(struct platform_device *pdev)
281{
282 struct device *dev = &pdev->dev;
283 struct dwc3 *dwc = dev_get_drvdata(dev);
284
285 dwc3_core_remove(dwc);
286 device_remove_software_node(dev);
287}
288
289static void dwc3_imx_suspend(struct dwc3_imx *dwc_imx, pm_message_t msg)
290{
291 if (dwc_imx->pm_suspended)
292 return;
293
294 if (PMSG_IS_AUTO(msg) || device_may_wakeup(dwc_imx->dev))
295 dwc3_imx_wakeup_enable(dwc_imx, msg);
296
297 enable_irq(dwc_imx->irq);
298 dwc_imx->pm_suspended = true;
299}
300
301static void dwc3_imx_resume(struct dwc3_imx *dwc_imx, pm_message_t msg)
302{
303 struct dwc3 *dwc = &dwc_imx->dwc;
304
305 if (!dwc_imx->pm_suspended)
306 return;
307
308 dwc_imx->pm_suspended = false;
309 if (!dwc_imx->wakeup_pending)
310 disable_irq_nosync(dwc_imx->irq);
311
312 dwc3_imx_wakeup_disable(dwc_imx);
313
314 /* Upon power loss any previous configuration is lost, restore it */
315 dwc3_imx_configure_glue(dwc_imx);
316
317 if (dwc_imx->wakeup_pending) {
318 dwc_imx->wakeup_pending = false;
319 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE)
320 pm_runtime_put_autosuspend(dwc->dev);
321 else
322 /*
323 * Add wait for xhci switch from suspend
324 * clock to normal clock to detect connection.
325 */
326 usleep_range(9000, 10000);
327 }
328}
329
330static int dwc3_imx_runtime_suspend(struct device *dev)
331{
332 struct dwc3 *dwc = dev_get_drvdata(dev);
333 struct dwc3_imx *dwc_imx = to_dwc3_imx(dwc);
334 int ret;
335
336 ret = dwc3_runtime_suspend(dwc);
337 if (ret)
338 return ret;
339
340 dwc3_imx_suspend(dwc_imx, PMSG_AUTO_SUSPEND);
341 return 0;
342}
343
344static int dwc3_imx_runtime_resume(struct device *dev)
345{
346 struct dwc3 *dwc = dev_get_drvdata(dev);
347 struct dwc3_imx *dwc_imx = to_dwc3_imx(dwc);
348
349 dwc3_imx_resume(dwc_imx, PMSG_AUTO_RESUME);
350 return dwc3_runtime_resume(dwc);
351}
352
353static int dwc3_imx_runtime_idle(struct device *dev)
354{
355 return dwc3_runtime_idle(dev_get_drvdata(dev));
356}
357
358static int dwc3_imx_pm_suspend(struct device *dev)
359{
360 struct dwc3 *dwc = dev_get_drvdata(dev);
361 struct dwc3_imx *dwc_imx = to_dwc3_imx(dwc);
362 int ret;
363
364 ret = dwc3_pm_suspend(dwc);
365 if (ret)
366 return ret;
367
368 dwc3_imx_suspend(dwc_imx, PMSG_SUSPEND);
369
370 if (device_may_wakeup(dev)) {
371 enable_irq_wake(dwc_imx->irq);
372 device_set_out_band_wakeup(dev);
373 } else {
374 clk_disable_unprepare(dwc_imx->suspend_clk);
375 }
376
377 clk_disable_unprepare(dwc_imx->hsio_clk);
378
379 return 0;
380}
381
382static int dwc3_imx_pm_resume(struct device *dev)
383{
384 struct dwc3 *dwc = dev_get_drvdata(dev);
385 struct dwc3_imx *dwc_imx = to_dwc3_imx(dwc);
386 int ret;
387
388 if (device_may_wakeup(dwc_imx->dev)) {
389 disable_irq_wake(dwc_imx->irq);
390 } else {
391 ret = clk_prepare_enable(dwc_imx->suspend_clk);
392 if (ret)
393 return ret;
394 }
395
396 ret = clk_prepare_enable(dwc_imx->hsio_clk);
397 if (ret) {
398 clk_disable_unprepare(dwc_imx->suspend_clk);
399 return ret;
400 }
401
402 dwc3_imx_resume(dwc_imx, PMSG_RESUME);
403
404 ret = dwc3_pm_resume(dwc);
405 if (ret)
406 return ret;
407
408 return 0;
409}
410
411static void dwc3_imx_complete(struct device *dev)
412{
413 dwc3_pm_complete(dev_get_drvdata(dev));
414}
415
416static int dwc3_imx_prepare(struct device *dev)
417{
418 return dwc3_pm_prepare(dev_get_drvdata(dev));
419}
420
421static const struct dev_pm_ops dwc3_imx_dev_pm_ops = {
422 SYSTEM_SLEEP_PM_OPS(dwc3_imx_pm_suspend, dwc3_imx_pm_resume)
423 RUNTIME_PM_OPS(dwc3_imx_runtime_suspend, dwc3_imx_runtime_resume,
424 dwc3_imx_runtime_idle)
425 .complete = pm_sleep_ptr(dwc3_imx_complete),
426 .prepare = pm_sleep_ptr(dwc3_imx_prepare),
427};
428
429static const struct of_device_id dwc3_imx_of_match[] = {
430 { .compatible = "nxp,imx8mp-dwc3", },
431 {},
432};
433MODULE_DEVICE_TABLE(of, dwc3_imx_of_match);
434
435static struct platform_driver dwc3_imx_driver = {
436 .probe = dwc3_imx_probe,
437 .remove = dwc3_imx_remove,
438 .driver = {
439 .name = "imx-dwc3",
440 .pm = pm_ptr(&dwc3_imx_dev_pm_ops),
441 .of_match_table = dwc3_imx_of_match,
442 },
443};
444
445module_platform_driver(dwc3_imx_driver);
446
447MODULE_LICENSE("GPL");
448MODULE_DESCRIPTION("DesignWare USB3 i.MX Glue Layer");