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