1 // SPDX-License-Identifier: GPL-2.0
3 * dwc3-keystone.c - Keystone Specific Glue layer
5 * Copyright (C) 2010-2013 Texas Instruments Incorporated - https://www.ti.com
7 * Author: WingMan Kwok <w-kwok2@ti.com>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/dma-mapping.h>
16 #include <linux/of_platform.h>
17 #include <linux/phy/phy.h>
18 #include <linux/pm_runtime.h>
20 /* USBSS register offsets */
21 #define USBSS_REVISION 0x0000
22 #define USBSS_SYSCONFIG 0x0010
23 #define USBSS_IRQ_EOI 0x0018
24 #define USBSS_IRQSTATUS_RAW_0 0x0020
25 #define USBSS_IRQSTATUS_0 0x0024
26 #define USBSS_IRQENABLE_SET_0 0x0028
27 #define USBSS_IRQENABLE_CLR_0 0x002c
29 /* IRQ register bits */
30 #define USBSS_IRQ_EOI_LINE(n) BIT(n)
31 #define USBSS_IRQ_EVENT_ST BIT(0)
32 #define USBSS_IRQ_COREIRQ_EN BIT(0)
33 #define USBSS_IRQ_COREIRQ_CLR BIT(0)
35 struct dwc3_keystone
{
41 static inline u32
kdwc3_readl(void __iomem
*base
, u32 offset
)
43 return readl(base
+ offset
);
46 static inline void kdwc3_writel(void __iomem
*base
, u32 offset
, u32 value
)
48 writel(value
, base
+ offset
);
51 static void kdwc3_enable_irqs(struct dwc3_keystone
*kdwc
)
55 val
= kdwc3_readl(kdwc
->usbss
, USBSS_IRQENABLE_SET_0
);
56 val
|= USBSS_IRQ_COREIRQ_EN
;
57 kdwc3_writel(kdwc
->usbss
, USBSS_IRQENABLE_SET_0
, val
);
60 static void kdwc3_disable_irqs(struct dwc3_keystone
*kdwc
)
64 val
= kdwc3_readl(kdwc
->usbss
, USBSS_IRQENABLE_SET_0
);
65 val
&= ~USBSS_IRQ_COREIRQ_EN
;
66 kdwc3_writel(kdwc
->usbss
, USBSS_IRQENABLE_SET_0
, val
);
69 static irqreturn_t
dwc3_keystone_interrupt(int irq
, void *_kdwc
)
71 struct dwc3_keystone
*kdwc
= _kdwc
;
73 kdwc3_writel(kdwc
->usbss
, USBSS_IRQENABLE_CLR_0
, USBSS_IRQ_COREIRQ_CLR
);
74 kdwc3_writel(kdwc
->usbss
, USBSS_IRQSTATUS_0
, USBSS_IRQ_EVENT_ST
);
75 kdwc3_writel(kdwc
->usbss
, USBSS_IRQENABLE_SET_0
, USBSS_IRQ_COREIRQ_EN
);
76 kdwc3_writel(kdwc
->usbss
, USBSS_IRQ_EOI
, USBSS_IRQ_EOI_LINE(0));
81 static int kdwc3_probe(struct platform_device
*pdev
)
83 struct device
*dev
= &pdev
->dev
;
84 struct device_node
*node
= pdev
->dev
.of_node
;
85 struct dwc3_keystone
*kdwc
;
88 kdwc
= devm_kzalloc(dev
, sizeof(*kdwc
), GFP_KERNEL
);
92 platform_set_drvdata(pdev
, kdwc
);
96 kdwc
->usbss
= devm_platform_ioremap_resource(pdev
, 0);
97 if (IS_ERR(kdwc
->usbss
))
98 return PTR_ERR(kdwc
->usbss
);
100 /* PSC dependency on AM65 needs SERDES0 to be powered before USB0 */
101 kdwc
->usb3_phy
= devm_phy_optional_get(dev
, "usb3-phy");
102 if (IS_ERR(kdwc
->usb3_phy
)) {
103 error
= PTR_ERR(kdwc
->usb3_phy
);
104 if (error
!= -EPROBE_DEFER
)
105 dev_err(dev
, "couldn't get usb3 phy: %d\n", error
);
110 phy_pm_runtime_get_sync(kdwc
->usb3_phy
);
112 error
= phy_reset(kdwc
->usb3_phy
);
114 dev_err(dev
, "usb3 phy reset failed: %d\n", error
);
118 error
= phy_init(kdwc
->usb3_phy
);
120 dev_err(dev
, "usb3 phy init failed: %d\n", error
);
124 error
= phy_power_on(kdwc
->usb3_phy
);
126 dev_err(dev
, "usb3 phy power on failed: %d\n", error
);
127 phy_exit(kdwc
->usb3_phy
);
131 pm_runtime_enable(kdwc
->dev
);
132 error
= pm_runtime_get_sync(kdwc
->dev
);
134 dev_err(kdwc
->dev
, "pm_runtime_get_sync failed, error %d\n",
139 /* IRQ processing not required currently for AM65 */
140 if (of_device_is_compatible(node
, "ti,am654-dwc3"))
143 irq
= platform_get_irq(pdev
, 0);
149 error
= devm_request_irq(dev
, irq
, dwc3_keystone_interrupt
, IRQF_SHARED
,
150 dev_name(dev
), kdwc
);
152 dev_err(dev
, "failed to request IRQ #%d --> %d\n",
157 kdwc3_enable_irqs(kdwc
);
160 error
= of_platform_populate(node
, NULL
, NULL
, dev
);
162 dev_err(&pdev
->dev
, "failed to create dwc3 core\n");
169 kdwc3_disable_irqs(kdwc
);
171 pm_runtime_put_sync(kdwc
->dev
);
172 pm_runtime_disable(kdwc
->dev
);
173 phy_power_off(kdwc
->usb3_phy
);
174 phy_exit(kdwc
->usb3_phy
);
175 phy_pm_runtime_put_sync(kdwc
->usb3_phy
);
180 static int kdwc3_remove_core(struct device
*dev
, void *c
)
182 struct platform_device
*pdev
= to_platform_device(dev
);
184 platform_device_unregister(pdev
);
189 static int kdwc3_remove(struct platform_device
*pdev
)
191 struct dwc3_keystone
*kdwc
= platform_get_drvdata(pdev
);
192 struct device_node
*node
= pdev
->dev
.of_node
;
194 if (!of_device_is_compatible(node
, "ti,am654-dwc3"))
195 kdwc3_disable_irqs(kdwc
);
197 device_for_each_child(&pdev
->dev
, NULL
, kdwc3_remove_core
);
198 pm_runtime_put_sync(kdwc
->dev
);
199 pm_runtime_disable(kdwc
->dev
);
201 phy_power_off(kdwc
->usb3_phy
);
202 phy_exit(kdwc
->usb3_phy
);
203 phy_pm_runtime_put_sync(kdwc
->usb3_phy
);
205 platform_set_drvdata(pdev
, NULL
);
210 static const struct of_device_id kdwc3_of_match
[] = {
211 { .compatible
= "ti,keystone-dwc3", },
212 { .compatible
= "ti,am654-dwc3" },
215 MODULE_DEVICE_TABLE(of
, kdwc3_of_match
);
217 static struct platform_driver kdwc3_driver
= {
218 .probe
= kdwc3_probe
,
219 .remove
= kdwc3_remove
,
221 .name
= "keystone-dwc3",
222 .of_match_table
= kdwc3_of_match
,
226 module_platform_driver(kdwc3_driver
);
228 MODULE_ALIAS("platform:keystone-dwc3");
229 MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
230 MODULE_LICENSE("GPL v2");
231 MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");