1 // SPDX-License-Identifier: GPL-2.0
3 * dwc3-of-simple.c - OF glue layer for simple integrations
5 * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
7 * Author: Felipe Balbi <balbi@ti.com>
9 * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov
10 * <iivanov@mm-sol.com> and the original patch adding support for Xilinx' SoC
11 * by Subbaraya Sundeep Bhatta <subbaraya.sundeep.bhatta@xilinx.com>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/clk.h>
21 #include <linux/of_platform.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/reset.h>
24 #include <linux/soc/xilinx/zynqmp/fw.h>
25 #include <linux/slab.h>
27 #include <linux/phy/phy-zynqmp.h>
28 #include <linux/of_address.h>
32 /* Xilinx USB 3.0 IP Register */
33 #define XLNX_USB_COHERENCY 0x005C
34 #define XLNX_USB_COHERENCY_ENABLE 0x1
36 /* ULPI control registers */
37 #define ULPI_OTG_CTRL_SET 0xB
38 #define ULPI_OTG_CTRL_CLEAR 0XC
39 #define OTG_CTRL_DRVVBUS_OFFSET 5
41 #define XLNX_USB_CUR_PWR_STATE 0x0000
42 #define XLNX_CUR_PWR_STATE_D0 0x00
43 #define XLNX_CUR_PWR_STATE_D3 0x0F
44 #define XLNX_CUR_PWR_STATE_BITMASK 0x0F
46 #define XLNX_USB_PME_ENABLE 0x0034
47 #define XLNX_PME_ENABLE_SIG_GEN 0x01
49 #define XLNX_USB_REQ_PWR_STATE 0x003c
50 #define XLNX_REQ_PWR_STATE_D0 0x00
51 #define XLNX_REQ_PWR_STATE_D3 0x03
53 /* Number of retries for USB operations */
54 #define DWC3_PWR_STATE_RETRIES 1000
55 #define DWC3_PWR_TIMEOUT 100
57 #define DWC3_OF_ADDRESS(ADDR) ((ADDR) - DWC3_GLOBALS_REGS_START)
59 struct dwc3_of_simple {
67 bool dis_u3_susphy_quirk;
68 bool enable_d3_suspend;
70 struct reset_control *resets;
75 int dwc3_enable_hw_coherency(struct device *dev)
77 struct device_node *node = of_get_parent(dev->of_node);
79 if (of_device_is_compatible(node, "xlnx,zynqmp-dwc3")) {
80 struct platform_device *pdev_parent;
81 struct dwc3_of_simple *simple;
85 pdev_parent = of_find_device_by_node(node);
86 simple = platform_get_drvdata(pdev_parent);
89 reg = readl(regs + XLNX_USB_COHERENCY);
90 reg |= XLNX_USB_COHERENCY_ENABLE;
91 writel(reg, regs + XLNX_USB_COHERENCY);
96 EXPORT_SYMBOL(dwc3_enable_hw_coherency);
98 void dwc3_set_simple_data(struct dwc3 *dwc)
100 struct device_node *node = of_get_parent(dwc->dev->of_node);
102 if (node && of_device_is_compatible(node, "xlnx,zynqmp-dwc3")) {
103 struct platform_device *pdev_parent;
104 struct dwc3_of_simple *simple;
106 pdev_parent = of_find_device_by_node(node);
107 simple = platform_get_drvdata(pdev_parent);
109 /* Set (struct dwc3 *) to simple->dwc for future use */
113 EXPORT_SYMBOL(dwc3_set_simple_data);
115 void dwc3_simple_check_quirks(struct dwc3 *dwc)
117 struct device_node *node = of_get_parent(dwc->dev->of_node);
119 if (node && of_device_is_compatible(node, "xlnx,zynqmp-dwc3")) {
120 struct platform_device *pdev_parent;
121 struct dwc3_of_simple *simple;
123 pdev_parent = of_find_device_by_node(node);
124 simple = platform_get_drvdata(pdev_parent);
126 /* Add snps,dis_u3_susphy_quirk */
127 dwc->dis_u3_susphy_quirk = simple->dis_u3_susphy_quirk;
130 EXPORT_SYMBOL(dwc3_simple_check_quirks);
132 void dwc3_simple_wakeup_capable(struct device *dev, bool wakeup)
134 struct device_node *node = of_node_get(dev->parent->of_node);
136 /* check for valid parent node */
138 if (!of_device_is_compatible(node, "xlnx,zynqmp-dwc3"))
139 node = of_get_next_parent(node);
145 struct platform_device *pdev_parent;
146 struct dwc3_of_simple *simple;
148 pdev_parent = of_find_device_by_node(node);
149 simple = platform_get_drvdata(pdev_parent);
151 /* Set wakeup capable as true or false */
152 simple->wakeup_capable = wakeup;
154 /* Allow D3 state if wakeup capable only */
155 simple->enable_d3_suspend = wakeup;
158 EXPORT_SYMBOL(dwc3_simple_wakeup_capable);
160 static int dwc3_simple_set_phydata(struct dwc3_of_simple *simple)
162 struct device *dev = simple->dev;
163 struct device_node *np = dev->of_node;
166 np = of_get_next_child(np, NULL);
169 phy = of_phy_get(np, "usb3-phy");
171 dev_err(dev, "%s: Can't find usb3-phy\n", __func__);
175 /* Store phy for future usage */
178 /* assign USB vendor regs addr to phy platform_data */
179 phy->dev.platform_data = simple->regs;
183 dev_err(dev, "%s: Can't find child node\n", __func__);
190 static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
192 struct device *dev = simple->dev;
193 struct device_node *np = dev->of_node;
196 simple->num_clocks = count;
201 simple->clks = devm_kcalloc(dev, simple->num_clocks,
202 sizeof(struct clk *), GFP_KERNEL);
206 for (i = 0; i < simple->num_clocks; i++) {
210 clk = of_clk_get(np, i);
213 clk_disable_unprepare(simple->clks[i]);
214 clk_put(simple->clks[i]);
219 ret = clk_prepare_enable(clk);
222 clk_disable_unprepare(simple->clks[i]);
223 clk_put(simple->clks[i]);
230 simple->clks[i] = clk;
236 static int dwc3_of_simple_probe(struct platform_device *pdev)
238 struct dwc3_of_simple *simple;
239 struct device *dev = &pdev->dev;
240 struct device_node *np = dev->of_node;
244 bool shared_resets = false;
246 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
250 platform_set_drvdata(pdev, simple);
253 if (of_device_is_compatible(pdev->dev.of_node,
254 "xlnx,zynqmp-dwc3")) {
257 struct resource *res;
260 res = platform_get_resource(pdev,
263 regs = devm_ioremap_resource(&pdev->dev, res);
265 return PTR_ERR(regs);
267 /* Store the usb control regs into simple for further usage */
270 /* read Silicon version using nvmem driver */
271 soc_rev = zynqmp_nvmem_get_silicon_version(&pdev->dev,
274 if (PTR_ERR(soc_rev) == -EPROBE_DEFER) {
275 /* Do a deferred probe */
276 return -EPROBE_DEFER;
278 } else if (!IS_ERR(soc_rev) &&
279 (*soc_rev < ZYNQMP_SILICON_V4)) {
280 /* Add snps,dis_u3_susphy_quirk
281 * for SOC revison less than v4
283 simple->dis_u3_susphy_quirk = true;
286 /* Update soc_rev to simple for future use */
287 simple->soc_rev = *soc_rev;
289 /* Clean soc_rev if got a valid pointer from nvmem driver
290 * else we may end up in kernel panic
292 if (!IS_ERR(soc_rev))
296 /* Set phy data for future use */
297 dwc3_simple_set_phydata(simple);
300 * Some controllers need to toggle the usb3-otg reset before trying to
301 * initialize the PHY, otherwise the PHY times out.
303 if (of_device_is_compatible(np, "rockchip,rk3399-dwc3"))
304 simple->need_reset = true;
306 if (of_device_is_compatible(np, "amlogic,meson-axg-dwc3") ||
307 of_device_is_compatible(np, "amlogic,meson-gxl-dwc3")) {
308 shared_resets = true;
309 simple->pulse_resets = true;
312 simple->resets = of_reset_control_array_get(np, shared_resets, true);
313 if (IS_ERR(simple->resets)) {
314 ret = PTR_ERR(simple->resets);
315 dev_err(dev, "failed to get device resets, err=%d\n", ret);
319 if (simple->pulse_resets) {
320 ret = reset_control_reset(simple->resets);
324 ret = reset_control_deassert(simple->resets);
329 ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
330 "clocks", "#clock-cells"));
332 goto err_resetc_assert;
334 ret = of_platform_populate(np, NULL, NULL, dev);
336 for (i = 0; i < simple->num_clocks; i++) {
337 clk_disable_unprepare(simple->clks[i]);
338 clk_put(simple->clks[i]);
341 goto err_resetc_assert;
344 platform_set_drvdata(pdev, simple);
346 pm_runtime_set_active(dev);
347 pm_runtime_enable(dev);
348 pm_runtime_get_sync(dev);
353 if (!simple->pulse_resets)
354 reset_control_assert(simple->resets);
357 reset_control_put(simple->resets);
361 static int dwc3_of_simple_remove(struct platform_device *pdev)
363 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
364 struct device *dev = &pdev->dev;
367 of_platform_depopulate(dev);
369 for (i = 0; i < simple->num_clocks; i++) {
370 clk_disable_unprepare(simple->clks[i]);
371 clk_put(simple->clks[i]);
373 simple->num_clocks = 0;
375 if (!simple->pulse_resets)
376 reset_control_assert(simple->resets);
378 reset_control_put(simple->resets);
380 pm_runtime_disable(dev);
381 pm_runtime_put_noidle(dev);
382 pm_runtime_set_suspended(dev);
387 static void dwc3_simple_vbus(struct dwc3 *dwc, bool vbus_off)
393 addr = ULPI_OTG_CTRL_CLEAR;
395 addr = ULPI_OTG_CTRL_SET;
397 val = (1 << OTG_CTRL_DRVVBUS_OFFSET);
399 reg = DWC3_GUSB2PHYACC_NEWREGREQ | DWC3_GUSB2PHYACC_ADDR(addr);
400 reg |= DWC3_GUSB2PHYACC_WRITE | val;
402 addr = DWC3_OF_ADDRESS(DWC3_GUSB2PHYACC(0));
403 writel(reg, dwc->regs + addr);
406 void dwc3_usb2phycfg(struct dwc3 *dwc, bool suspend)
410 addr = DWC3_OF_ADDRESS(DWC3_GUSB2PHYCFG(0));
413 reg = readl(dwc->regs + addr);
414 if (!(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
415 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
416 writel(reg, (dwc->regs + addr));
419 reg = readl(dwc->regs + addr);
420 if ((reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
421 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
422 writel(reg, (dwc->regs + addr));
427 int dwc3_set_usb_core_power(struct dwc3 *dwc, bool on)
430 void __iomem *reg_base;
431 struct platform_device *pdev_parent;
432 struct dwc3_of_simple *simple;
433 struct device_node *node = of_get_parent(dwc->dev->of_node);
435 /* this is for Xilinx devices only */
436 if (!of_device_is_compatible(node, "xlnx,zynqmp-dwc3"))
439 pdev_parent = of_find_device_by_node(node);
440 simple = platform_get_drvdata(pdev_parent);
441 reg_base = simple->regs;
443 /* Check if entering into D3 state is allowed during suspend */
444 if ((simple->soc_rev < ZYNQMP_SILICON_V4) || !simple->enable_d3_suspend)
451 dev_dbg(dwc->dev, "trying to set power state to D0....\n");
453 /* Release USB core reset , which was assert during D3 entry */
454 xpsgtr_usb_crst_release(simple->phy);
456 /* change power state to D0 */
457 writel(XLNX_REQ_PWR_STATE_D0,
458 reg_base + XLNX_USB_REQ_PWR_STATE);
460 /* wait till current state is changed to D0 */
461 retries = DWC3_PWR_STATE_RETRIES;
463 reg = readl(reg_base + XLNX_USB_CUR_PWR_STATE);
464 if ((reg & XLNX_CUR_PWR_STATE_BITMASK) ==
465 XLNX_CUR_PWR_STATE_D0)
468 udelay(DWC3_PWR_TIMEOUT);
472 dev_err(dwc->dev, "Failed to set power state to D0\n");
478 /* Clear Suspend PHY bit if dis_u2_susphy_quirk is set */
479 if (dwc->dis_u2_susphy_quirk)
480 dwc3_usb2phycfg(dwc, false);
482 dev_dbg(dwc->dev, "Trying to set power state to D3...\n");
485 * Set Suspend PHY bit before entering D3 if
486 * dis_u2_susphy_quirk is set
488 if (dwc->dis_u2_susphy_quirk)
489 dwc3_usb2phycfg(dwc, true);
491 /* enable PME to wakeup from hibernation */
492 writel(XLNX_PME_ENABLE_SIG_GEN, reg_base + XLNX_USB_PME_ENABLE);
494 /* change power state to D3 */
495 writel(XLNX_REQ_PWR_STATE_D3,
496 reg_base + XLNX_USB_REQ_PWR_STATE);
498 /* wait till current state is changed to D3 */
499 retries = DWC3_PWR_STATE_RETRIES;
501 reg = readl(reg_base + XLNX_USB_CUR_PWR_STATE);
502 if ((reg & XLNX_CUR_PWR_STATE_BITMASK) ==
503 XLNX_CUR_PWR_STATE_D3)
506 udelay(DWC3_PWR_TIMEOUT);
510 dev_err(dwc->dev, "Failed to set power state to D3\n");
514 /* Assert USB core reset after entering D3 state */
515 xpsgtr_usb_crst_assert(simple->phy);
522 EXPORT_SYMBOL(dwc3_set_usb_core_power);
524 static int __maybe_unused dwc3_of_simple_runtime_suspend(struct device *dev)
526 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
529 for (i = 0; i < simple->num_clocks; i++)
530 clk_disable(simple->clks[i]);
535 static int __maybe_unused dwc3_of_simple_runtime_resume(struct device *dev)
537 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
541 for (i = 0; i < simple->num_clocks; i++) {
542 ret = clk_enable(simple->clks[i]);
545 clk_disable(simple->clks[i]);
549 /* Ask ULPI to turn ON Vbus */
550 dwc3_simple_vbus(simple->dwc, false);
556 static int __maybe_unused dwc3_of_simple_suspend(struct device *dev)
558 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
561 if (!simple->wakeup_capable && !simple->dwc->is_d3) {
562 /* Ask ULPI to turn OFF Vbus */
563 dwc3_simple_vbus(simple->dwc, true);
565 /* Disable the clocks */
566 for (i = 0; i < simple->num_clocks; i++)
567 clk_disable(simple->clks[i]);
570 if (simple->need_reset)
571 reset_control_assert(simple->resets);
576 static int __maybe_unused dwc3_of_simple_resume(struct device *dev)
578 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
582 if (simple->wakeup_capable || simple->dwc->is_d3)
585 for (i = 0; i < simple->num_clocks; i++) {
586 ret = clk_enable(simple->clks[i]);
589 clk_disable(simple->clks[i]);
594 if (simple->need_reset)
595 reset_control_deassert(simple->resets);
600 static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
601 SET_SYSTEM_SLEEP_PM_OPS(dwc3_of_simple_suspend, dwc3_of_simple_resume)
602 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
603 dwc3_of_simple_runtime_resume, NULL)
606 static const struct of_device_id of_dwc3_simple_match[] = {
607 { .compatible = "rockchip,rk3399-dwc3" },
608 { .compatible = "xlnx,zynqmp-dwc3" },
609 { .compatible = "xlnx,versal-dwc3" },
610 { .compatible = "cavium,octeon-7130-usb-uctl" },
611 { .compatible = "sprd,sc9860-dwc3" },
612 { .compatible = "amlogic,meson-axg-dwc3" },
613 { .compatible = "amlogic,meson-gxl-dwc3" },
614 { .compatible = "allwinner,sun50i-h6-dwc3" },
617 MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
619 static struct platform_driver dwc3_of_simple_driver = {
620 .probe = dwc3_of_simple_probe,
621 .remove = dwc3_of_simple_remove,
623 .name = "dwc3-of-simple",
624 .of_match_table = of_dwc3_simple_match,
625 .pm = &dwc3_of_simple_dev_pm_ops,
629 module_platform_driver(dwc3_of_simple_driver);
630 MODULE_LICENSE("GPL v2");
631 MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
632 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");