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
8 #include <linux/cleanup.h>
9 #include <linux/clk.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18
19 #include "core.h"
20
21 /* USB wakeup registers */
22 #define USB_WAKEUP_CTRL 0x00
23
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)
38
39 #define USB_WAKEUP_EN_MASK GENMASK(5, 0)
40
41 /* USB glue registers */
42 #define USB_CTRL0 0x00
43 #define USB_CTRL1 0x04
44
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 */
48
49 #define USB_CTRL1_OC_POLARITY BIT(16) /* 0 - HIGH / 1 - LOW */
50 #define USB_CTRL1_PWR_POLARITY BIT(17) /* 0 - HIGH / 1 - LOW */
51
52 struct dwc3_imx8mp {
53 struct device *dev;
54 struct platform_device *dwc3_pdev;
55 void __iomem *hsio_blk_base;
56 void __iomem *glue_base;
57 struct clk *hsio_clk;
58 struct clk *suspend_clk;
59 int irq;
60 bool pm_suspended;
61 bool wakeup_pending;
62 };
63
imx8mp_configure_glue(struct dwc3_imx8mp * dwc3_imx)64 static void imx8mp_configure_glue(struct dwc3_imx8mp *dwc3_imx)
65 {
66 struct device *dev = dwc3_imx->dev;
67 u32 value;
68
69 if (!dwc3_imx->glue_base)
70 return;
71
72 value = readl(dwc3_imx->glue_base + USB_CTRL0);
73
74 if (device_property_read_bool(dev, "fsl,permanently-attached"))
75 value |= (USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
76 else
77 value &= ~(USB_CTRL0_USB2_FIXED | USB_CTRL0_USB3_FIXED);
78
79 if (device_property_read_bool(dev, "fsl,disable-port-power-control"))
80 value &= ~(USB_CTRL0_PORTPWR_EN);
81 else
82 value |= USB_CTRL0_PORTPWR_EN;
83
84 writel(value, dwc3_imx->glue_base + USB_CTRL0);
85
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;
89 else
90 value &= ~USB_CTRL1_OC_POLARITY;
91
92 if (device_property_read_bool(dev, "fsl,power-active-low"))
93 value |= USB_CTRL1_PWR_POLARITY;
94 else
95 value &= ~USB_CTRL1_PWR_POLARITY;
96
97 writel(value, dwc3_imx->glue_base + USB_CTRL1);
98 }
99
dwc3_imx8mp_wakeup_enable(struct dwc3_imx8mp * dwc3_imx,pm_message_t msg)100 static void dwc3_imx8mp_wakeup_enable(struct dwc3_imx8mp *dwc3_imx,
101 pm_message_t msg)
102 {
103 struct dwc3 *dwc3 = platform_get_drvdata(dwc3_imx->dwc3_pdev);
104 u32 val;
105
106 if (!dwc3)
107 return;
108
109 val = readl(dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
110
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;
115 } else {
116 val |= USB_WAKEUP_EN | USB_WAKEUP_VBUS_EN |
117 USB_WAKEUP_VBUS_SRC_SESS_VAL;
118 }
119
120 writel(val, dwc3_imx->hsio_blk_base + USB_WAKEUP_CTRL);
121 }
122
dwc3_imx8mp_wakeup_disable(struct dwc3_imx8mp * dwc3_imx)123 static void dwc3_imx8mp_wakeup_disable(struct dwc3_imx8mp *dwc3_imx)
124 {
125 u32 val;
126
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);
130 }
131
132 static const struct property_entry dwc3_imx8mp_properties[] = {
133 PROPERTY_ENTRY_BOOL("xhci-missing-cas-quirk"),
134 PROPERTY_ENTRY_BOOL("xhci-skip-phy-init-quirk"),
135 {},
136 };
137
138 static const struct software_node dwc3_imx8mp_swnode = {
139 .properties = dwc3_imx8mp_properties,
140 };
141
dwc3_imx8mp_interrupt(int irq,void * _dwc3_imx)142 static irqreturn_t dwc3_imx8mp_interrupt(int irq, void *_dwc3_imx)
143 {
144 struct dwc3_imx8mp *dwc3_imx = _dwc3_imx;
145 struct dwc3 *dwc = platform_get_drvdata(dwc3_imx->dwc3_pdev);
146
147 if (!dwc3_imx->pm_suspended)
148 return IRQ_HANDLED;
149
150 disable_irq_nosync(dwc3_imx->irq);
151 dwc3_imx->wakeup_pending = true;
152
153 if ((dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST) && dwc->xhci)
154 pm_runtime_resume(&dwc->xhci->dev);
155 else if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE)
156 pm_runtime_get(dwc->dev);
157
158 return IRQ_HANDLED;
159 }
160
dwc3_imx_pre_set_role(struct dwc3 * dwc,enum usb_role role)161 static void dwc3_imx_pre_set_role(struct dwc3 *dwc, enum usb_role role)
162 {
163 if (role == USB_ROLE_HOST)
164 /*
165 * For xhci host, we need disable dwc core auto
166 * suspend, because during this auto suspend delay(5s),
167 * xhci host RUN_STOP is cleared and wakeup is not
168 * enabled, if device is inserted, xhci host can't
169 * response the connection.
170 */
171 pm_runtime_dont_use_autosuspend(dwc->dev);
172 else
173 pm_runtime_use_autosuspend(dwc->dev);
174 }
175
176 struct dwc3_glue_ops dwc3_imx_glue_ops = {
177 .pre_set_role = dwc3_imx_pre_set_role,
178 };
179
dwc3_imx8mp_probe(struct platform_device * pdev)180 static int dwc3_imx8mp_probe(struct platform_device *pdev)
181 {
182 struct device *dev = &pdev->dev;
183 struct device_node *node = dev->of_node;
184 struct dwc3_imx8mp *dwc3_imx;
185 struct dwc3 *dwc3;
186 struct resource *res;
187 int err, irq;
188
189 if (!node) {
190 dev_err(dev, "device node not found\n");
191 return -EINVAL;
192 }
193
194 dwc3_imx = devm_kzalloc(dev, sizeof(*dwc3_imx), GFP_KERNEL);
195 if (!dwc3_imx)
196 return -ENOMEM;
197
198 platform_set_drvdata(pdev, dwc3_imx);
199
200 dwc3_imx->dev = dev;
201
202 dwc3_imx->hsio_blk_base = devm_platform_ioremap_resource(pdev, 0);
203 if (IS_ERR(dwc3_imx->hsio_blk_base))
204 return PTR_ERR(dwc3_imx->hsio_blk_base);
205
206 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
207 if (!res) {
208 dev_warn(dev, "Base address for glue layer missing. Continuing without, some features are missing though.");
209 } else {
210 dwc3_imx->glue_base = devm_ioremap_resource(dev, res);
211 if (IS_ERR(dwc3_imx->glue_base))
212 return PTR_ERR(dwc3_imx->glue_base);
213 }
214
215 dwc3_imx->hsio_clk = devm_clk_get_enabled(dev, "hsio");
216 if (IS_ERR(dwc3_imx->hsio_clk))
217 return dev_err_probe(dev, PTR_ERR(dwc3_imx->hsio_clk),
218 "Failed to get hsio clk\n");
219
220 dwc3_imx->suspend_clk = devm_clk_get_enabled(dev, "suspend");
221 if (IS_ERR(dwc3_imx->suspend_clk))
222 return dev_err_probe(dev, PTR_ERR(dwc3_imx->suspend_clk),
223 "Failed to get suspend clk\n");
224
225 irq = platform_get_irq(pdev, 0);
226 if (irq < 0)
227 return irq;
228 dwc3_imx->irq = irq;
229
230 struct device_node *dwc3_np __free(device_node) = of_get_compatible_child(node,
231 "snps,dwc3");
232 if (!dwc3_np)
233 return dev_err_probe(dev, -ENODEV, "failed to find dwc3 core child\n");
234
235 imx8mp_configure_glue(dwc3_imx);
236
237 pm_runtime_set_active(dev);
238 pm_runtime_enable(dev);
239 err = pm_runtime_get_sync(dev);
240 if (err < 0)
241 goto disable_rpm;
242
243 err = device_add_software_node(dev, &dwc3_imx8mp_swnode);
244 if (err) {
245 err = -ENODEV;
246 dev_err(dev, "failed to add software node\n");
247 goto disable_rpm;
248 }
249
250 err = of_platform_populate(node, NULL, NULL, dev);
251 if (err) {
252 dev_err(&pdev->dev, "failed to create dwc3 core\n");
253 goto remove_swnode;
254 }
255
256 dwc3_imx->dwc3_pdev = of_find_device_by_node(dwc3_np);
257 if (!dwc3_imx->dwc3_pdev) {
258 dev_err(dev, "failed to get dwc3 platform device\n");
259 err = -ENODEV;
260 goto depopulate;
261 }
262
263 dwc3 = platform_get_drvdata(dwc3_imx->dwc3_pdev);
264 if (!dwc3) {
265 err = dev_err_probe(dev, -EPROBE_DEFER, "failed to get dwc3 platform data\n");
266 goto put_dwc3;
267 }
268
269 dwc3->glue_ops = &dwc3_imx_glue_ops;
270
271 if (dwc3->dr_mode == USB_DR_MODE_HOST)
272 pm_runtime_dont_use_autosuspend(dwc3->dev);
273
274 err = devm_request_threaded_irq(dev, irq, NULL, dwc3_imx8mp_interrupt,
275 IRQF_ONESHOT, dev_name(dev), dwc3_imx);
276 if (err) {
277 dev_err(dev, "failed to request IRQ #%d --> %d\n", irq, err);
278 goto put_dwc3;
279 }
280
281 device_set_wakeup_capable(dev, true);
282 pm_runtime_put(dev);
283
284 return 0;
285
286 put_dwc3:
287 put_device(&dwc3_imx->dwc3_pdev->dev);
288 depopulate:
289 of_platform_depopulate(dev);
290 remove_swnode:
291 device_remove_software_node(dev);
292 disable_rpm:
293 pm_runtime_disable(dev);
294 pm_runtime_put_noidle(dev);
295
296 return err;
297 }
298
dwc3_imx8mp_remove(struct platform_device * pdev)299 static void dwc3_imx8mp_remove(struct platform_device *pdev)
300 {
301 struct dwc3_imx8mp *dwc3_imx = platform_get_drvdata(pdev);
302 struct device *dev = &pdev->dev;
303
304 put_device(&dwc3_imx->dwc3_pdev->dev);
305
306 pm_runtime_get_sync(dev);
307 of_platform_depopulate(dev);
308 device_remove_software_node(dev);
309
310 pm_runtime_disable(dev);
311 pm_runtime_put_noidle(dev);
312 }
313
dwc3_imx8mp_suspend(struct dwc3_imx8mp * dwc3_imx,pm_message_t msg)314 static int dwc3_imx8mp_suspend(struct dwc3_imx8mp *dwc3_imx, pm_message_t msg)
315 {
316 if (dwc3_imx->pm_suspended)
317 return 0;
318
319 /* Wakeup enable */
320 if (PMSG_IS_AUTO(msg) || device_may_wakeup(dwc3_imx->dev))
321 dwc3_imx8mp_wakeup_enable(dwc3_imx, msg);
322
323 dwc3_imx->pm_suspended = true;
324
325 return 0;
326 }
327
dwc3_imx8mp_resume(struct dwc3_imx8mp * dwc3_imx,pm_message_t msg)328 static int dwc3_imx8mp_resume(struct dwc3_imx8mp *dwc3_imx, pm_message_t msg)
329 {
330 struct dwc3 *dwc = platform_get_drvdata(dwc3_imx->dwc3_pdev);
331 int ret = 0;
332
333 if (!dwc3_imx->pm_suspended)
334 return 0;
335
336 /* Wakeup disable */
337 dwc3_imx8mp_wakeup_disable(dwc3_imx);
338 dwc3_imx->pm_suspended = false;
339
340 /* Upon power loss any previous configuration is lost, restore it */
341 imx8mp_configure_glue(dwc3_imx);
342
343 if (dwc3_imx->wakeup_pending) {
344 dwc3_imx->wakeup_pending = false;
345 if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE) {
346 pm_runtime_put_autosuspend(dwc->dev);
347 } else {
348 /*
349 * Add wait for xhci switch from suspend
350 * clock to normal clock to detect connection.
351 */
352 usleep_range(9000, 10000);
353 }
354 enable_irq(dwc3_imx->irq);
355 }
356
357 return ret;
358 }
359
dwc3_imx8mp_pm_suspend(struct device * dev)360 static int dwc3_imx8mp_pm_suspend(struct device *dev)
361 {
362 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
363 int ret;
364
365 ret = dwc3_imx8mp_suspend(dwc3_imx, PMSG_SUSPEND);
366
367 if (device_may_wakeup(dwc3_imx->dev)) {
368 enable_irq_wake(dwc3_imx->irq);
369
370 if (device_is_compatible(dev, "fsl,imx95-dwc3"))
371 device_set_out_band_wakeup(dev);
372
373 } else {
374 clk_disable_unprepare(dwc3_imx->suspend_clk);
375 }
376
377 clk_disable_unprepare(dwc3_imx->hsio_clk);
378 dev_dbg(dev, "dwc3 imx8mp pm suspend.\n");
379
380 return ret;
381 }
382
dwc3_imx8mp_pm_resume(struct device * dev)383 static int dwc3_imx8mp_pm_resume(struct device *dev)
384 {
385 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
386 int ret;
387
388 if (device_may_wakeup(dwc3_imx->dev)) {
389 disable_irq_wake(dwc3_imx->irq);
390 } else {
391 ret = clk_prepare_enable(dwc3_imx->suspend_clk);
392 if (ret)
393 return ret;
394 }
395
396 ret = clk_prepare_enable(dwc3_imx->hsio_clk);
397 if (ret) {
398 clk_disable_unprepare(dwc3_imx->suspend_clk);
399 return ret;
400 }
401
402 ret = dwc3_imx8mp_resume(dwc3_imx, PMSG_RESUME);
403
404 pm_runtime_disable(dev);
405 pm_runtime_set_active(dev);
406 pm_runtime_enable(dev);
407
408 dev_dbg(dev, "dwc3 imx8mp pm resume.\n");
409
410 return ret;
411 }
412
dwc3_imx8mp_runtime_suspend(struct device * dev)413 static int dwc3_imx8mp_runtime_suspend(struct device *dev)
414 {
415 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
416
417 dev_dbg(dev, "dwc3 imx8mp runtime suspend.\n");
418
419 return dwc3_imx8mp_suspend(dwc3_imx, PMSG_AUTO_SUSPEND);
420 }
421
dwc3_imx8mp_runtime_resume(struct device * dev)422 static int dwc3_imx8mp_runtime_resume(struct device *dev)
423 {
424 struct dwc3_imx8mp *dwc3_imx = dev_get_drvdata(dev);
425
426 dev_dbg(dev, "dwc3 imx8mp runtime resume.\n");
427
428 return dwc3_imx8mp_resume(dwc3_imx, PMSG_AUTO_RESUME);
429 }
430
431 static const struct dev_pm_ops dwc3_imx8mp_dev_pm_ops = {
432 SYSTEM_SLEEP_PM_OPS(dwc3_imx8mp_pm_suspend, dwc3_imx8mp_pm_resume)
433 RUNTIME_PM_OPS(dwc3_imx8mp_runtime_suspend, dwc3_imx8mp_runtime_resume,
434 NULL)
435 };
436
437 static const struct of_device_id dwc3_imx8mp_of_match[] = {
438 { .compatible = "fsl,imx8mp-dwc3", },
439 {},
440 };
441 MODULE_DEVICE_TABLE(of, dwc3_imx8mp_of_match);
442
443 static struct platform_driver dwc3_imx8mp_driver = {
444 .probe = dwc3_imx8mp_probe,
445 .remove = dwc3_imx8mp_remove,
446 .driver = {
447 .name = "imx8mp-dwc3",
448 .pm = pm_ptr(&dwc3_imx8mp_dev_pm_ops),
449 .of_match_table = dwc3_imx8mp_of_match,
450 },
451 };
452
453 module_platform_driver(dwc3_imx8mp_driver);
454
455 MODULE_ALIAS("platform:imx8mp-dwc3");
456 MODULE_AUTHOR("jun.li@nxp.com");
457 MODULE_LICENSE("GPL v2");
458 MODULE_DESCRIPTION("DesignWare USB3 imx8mp Glue Layer");
459