xref: /openbmc/linux/drivers/usb/dwc3/dwc3-keystone.c (revision fe4549b1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dwc3-keystone.c - Keystone Specific Glue layer
4  *
5  * Copyright (C) 2010-2013 Texas Instruments Incorporated - https://www.ti.com
6  *
7  * Author: WingMan Kwok <w-kwok2@ti.com>
8  */
9 
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>
15 #include <linux/io.h>
16 #include <linux/of.h>
17 #include <linux/of_platform.h>
18 #include <linux/phy/phy.h>
19 #include <linux/pm_runtime.h>
20 
21 /* USBSS register offsets */
22 #define USBSS_REVISION		0x0000
23 #define USBSS_SYSCONFIG		0x0010
24 #define USBSS_IRQ_EOI		0x0018
25 #define USBSS_IRQSTATUS_RAW_0	0x0020
26 #define USBSS_IRQSTATUS_0	0x0024
27 #define USBSS_IRQENABLE_SET_0	0x0028
28 #define USBSS_IRQENABLE_CLR_0	0x002c
29 
30 /* IRQ register bits */
31 #define USBSS_IRQ_EOI_LINE(n)	BIT(n)
32 #define USBSS_IRQ_EVENT_ST	BIT(0)
33 #define USBSS_IRQ_COREIRQ_EN	BIT(0)
34 #define USBSS_IRQ_COREIRQ_CLR	BIT(0)
35 
36 struct dwc3_keystone {
37 	struct device			*dev;
38 	void __iomem			*usbss;
39 	struct phy			*usb3_phy;
40 };
41 
42 static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
43 {
44 	return readl(base + offset);
45 }
46 
47 static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
48 {
49 	writel(value, base + offset);
50 }
51 
52 static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc)
53 {
54 	u32 val;
55 
56 	val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
57 	val |= USBSS_IRQ_COREIRQ_EN;
58 	kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
59 }
60 
61 static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
62 {
63 	u32 val;
64 
65 	val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
66 	val &= ~USBSS_IRQ_COREIRQ_EN;
67 	kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
68 }
69 
70 static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
71 {
72 	struct dwc3_keystone	*kdwc = _kdwc;
73 
74 	kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_CLR_0, USBSS_IRQ_COREIRQ_CLR);
75 	kdwc3_writel(kdwc->usbss, USBSS_IRQSTATUS_0, USBSS_IRQ_EVENT_ST);
76 	kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, USBSS_IRQ_COREIRQ_EN);
77 	kdwc3_writel(kdwc->usbss, USBSS_IRQ_EOI, USBSS_IRQ_EOI_LINE(0));
78 
79 	return IRQ_HANDLED;
80 }
81 
82 static int kdwc3_probe(struct platform_device *pdev)
83 {
84 	struct device		*dev = &pdev->dev;
85 	struct device_node	*node = pdev->dev.of_node;
86 	struct dwc3_keystone	*kdwc;
87 	int			error, irq;
88 
89 	kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
90 	if (!kdwc)
91 		return -ENOMEM;
92 
93 	platform_set_drvdata(pdev, kdwc);
94 
95 	kdwc->dev = dev;
96 
97 	kdwc->usbss = devm_platform_ioremap_resource(pdev, 0);
98 	if (IS_ERR(kdwc->usbss))
99 		return PTR_ERR(kdwc->usbss);
100 
101 	/* PSC dependency on AM65 needs SERDES0 to be powered before USB0 */
102 	kdwc->usb3_phy = devm_phy_optional_get(dev, "usb3-phy");
103 	if (IS_ERR(kdwc->usb3_phy))
104 		return dev_err_probe(dev, PTR_ERR(kdwc->usb3_phy), "couldn't get usb3 phy\n");
105 
106 	phy_pm_runtime_get_sync(kdwc->usb3_phy);
107 
108 	error = phy_reset(kdwc->usb3_phy);
109 	if (error < 0) {
110 		dev_err(dev, "usb3 phy reset failed: %d\n", error);
111 		return error;
112 	}
113 
114 	error = phy_init(kdwc->usb3_phy);
115 	if (error < 0) {
116 		dev_err(dev, "usb3 phy init failed: %d\n", error);
117 		return error;
118 	}
119 
120 	error = phy_power_on(kdwc->usb3_phy);
121 	if (error < 0) {
122 		dev_err(dev, "usb3 phy power on failed: %d\n", error);
123 		phy_exit(kdwc->usb3_phy);
124 		return error;
125 	}
126 
127 	pm_runtime_enable(kdwc->dev);
128 	error = pm_runtime_get_sync(kdwc->dev);
129 	if (error < 0) {
130 		dev_err(kdwc->dev, "pm_runtime_get_sync failed, error %d\n",
131 			error);
132 		goto err_irq;
133 	}
134 
135 	/* IRQ processing not required currently for AM65 */
136 	if (of_device_is_compatible(node, "ti,am654-dwc3"))
137 		goto skip_irq;
138 
139 	irq = platform_get_irq(pdev, 0);
140 	if (irq < 0) {
141 		error = irq;
142 		goto err_irq;
143 	}
144 
145 	error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
146 			dev_name(dev), kdwc);
147 	if (error) {
148 		dev_err(dev, "failed to request IRQ #%d --> %d\n",
149 				irq, error);
150 		goto err_irq;
151 	}
152 
153 	kdwc3_enable_irqs(kdwc);
154 
155 skip_irq:
156 	error = of_platform_populate(node, NULL, NULL, dev);
157 	if (error) {
158 		dev_err(&pdev->dev, "failed to create dwc3 core\n");
159 		goto err_core;
160 	}
161 
162 	return 0;
163 
164 err_core:
165 	kdwc3_disable_irqs(kdwc);
166 err_irq:
167 	pm_runtime_put_sync(kdwc->dev);
168 	pm_runtime_disable(kdwc->dev);
169 	phy_power_off(kdwc->usb3_phy);
170 	phy_exit(kdwc->usb3_phy);
171 	phy_pm_runtime_put_sync(kdwc->usb3_phy);
172 
173 	return error;
174 }
175 
176 static int kdwc3_remove_core(struct device *dev, void *c)
177 {
178 	struct platform_device *pdev = to_platform_device(dev);
179 
180 	platform_device_unregister(pdev);
181 
182 	return 0;
183 }
184 
185 static void kdwc3_remove(struct platform_device *pdev)
186 {
187 	struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
188 	struct device_node *node = pdev->dev.of_node;
189 
190 	if (!of_device_is_compatible(node, "ti,am654-dwc3"))
191 		kdwc3_disable_irqs(kdwc);
192 
193 	device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
194 	pm_runtime_put_sync(kdwc->dev);
195 	pm_runtime_disable(kdwc->dev);
196 
197 	phy_power_off(kdwc->usb3_phy);
198 	phy_exit(kdwc->usb3_phy);
199 	phy_pm_runtime_put_sync(kdwc->usb3_phy);
200 }
201 
202 static const struct of_device_id kdwc3_of_match[] = {
203 	{ .compatible = "ti,keystone-dwc3", },
204 	{ .compatible = "ti,am654-dwc3" },
205 	{},
206 };
207 MODULE_DEVICE_TABLE(of, kdwc3_of_match);
208 
209 static struct platform_driver kdwc3_driver = {
210 	.probe		= kdwc3_probe,
211 	.remove_new	= kdwc3_remove,
212 	.driver		= {
213 		.name	= "keystone-dwc3",
214 		.of_match_table	= kdwc3_of_match,
215 	},
216 };
217 
218 module_platform_driver(kdwc3_driver);
219 
220 MODULE_ALIAS("platform:keystone-dwc3");
221 MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
222 MODULE_LICENSE("GPL v2");
223 MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");
224