Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0 |
cbdc0f54 | 2 | /* |
72246da4 FB |
3 | * core.c - DesignWare USB3 DRD Controller Core file |
4 | * | |
10623b87 | 5 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com |
72246da4 FB |
6 | * |
7 | * Authors: Felipe Balbi <balbi@ti.com>, | |
8 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> | |
72246da4 FB |
9 | */ |
10 | ||
fe8abf33 | 11 | #include <linux/clk.h> |
fa0ea13e | 12 | #include <linux/version.h> |
a72e658b | 13 | #include <linux/module.h> |
72246da4 FB |
14 | #include <linux/kernel.h> |
15 | #include <linux/slab.h> | |
16 | #include <linux/spinlock.h> | |
17 | #include <linux/platform_device.h> | |
18 | #include <linux/pm_runtime.h> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/ioport.h> | |
21 | #include <linux/io.h> | |
22 | #include <linux/list.h> | |
23 | #include <linux/delay.h> | |
24 | #include <linux/dma-mapping.h> | |
457e84b6 | 25 | #include <linux/of.h> |
d182c2e1 | 26 | #include <linux/of_graph.h> |
404905a6 | 27 | #include <linux/acpi.h> |
6344475f | 28 | #include <linux/pinctrl/consumer.h> |
850e6340 | 29 | #include <linux/pinctrl/devinfo.h> |
fe8abf33 | 30 | #include <linux/reset.h> |
7bee3188 | 31 | #include <linux/bitfield.h> |
72246da4 FB |
32 | |
33 | #include <linux/usb/ch9.h> | |
34 | #include <linux/usb/gadget.h> | |
f7e846f0 | 35 | #include <linux/usb/of.h> |
a45c82b8 | 36 | #include <linux/usb/otg.h> |
72246da4 FB |
37 | |
38 | #include "core.h" | |
39 | #include "gadget.h" | |
613a2e65 | 40 | #include "glue.h" |
72246da4 FB |
41 | #include "io.h" |
42 | ||
43 | #include "debug.h" | |
921e109c | 44 | #include "../host/xhci-ext-caps.h" |
72246da4 | 45 | |
fc8bb91b | 46 | #define DWC3_DEFAULT_AUTOSUSPEND_DELAY 5000 /* ms */ |
8300dd23 | 47 | |
9d6173e1 TN |
48 | /** |
49 | * dwc3_get_dr_mode - Validates and sets dr_mode | |
50 | * @dwc: pointer to our context structure | |
51 | */ | |
52 | static int dwc3_get_dr_mode(struct dwc3 *dwc) | |
53 | { | |
54 | enum usb_dr_mode mode; | |
55 | struct device *dev = dwc->dev; | |
56 | unsigned int hw_mode; | |
57 | ||
58 | if (dwc->dr_mode == USB_DR_MODE_UNKNOWN) | |
59 | dwc->dr_mode = USB_DR_MODE_OTG; | |
60 | ||
61 | mode = dwc->dr_mode; | |
62 | hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0); | |
63 | ||
64 | switch (hw_mode) { | |
65 | case DWC3_GHWPARAMS0_MODE_GADGET: | |
66 | if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) { | |
67 | dev_err(dev, | |
68 | "Controller does not support host mode.\n"); | |
69 | return -EINVAL; | |
70 | } | |
71 | mode = USB_DR_MODE_PERIPHERAL; | |
72 | break; | |
73 | case DWC3_GHWPARAMS0_MODE_HOST: | |
74 | if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) { | |
75 | dev_err(dev, | |
76 | "Controller does not support device mode.\n"); | |
77 | return -EINVAL; | |
78 | } | |
79 | mode = USB_DR_MODE_HOST; | |
80 | break; | |
81 | default: | |
82 | if (IS_ENABLED(CONFIG_USB_DWC3_HOST)) | |
83 | mode = USB_DR_MODE_HOST; | |
84 | else if (IS_ENABLED(CONFIG_USB_DWC3_GADGET)) | |
85 | mode = USB_DR_MODE_PERIPHERAL; | |
a7700468 TN |
86 | |
87 | /* | |
89a9cc47 TN |
88 | * DWC_usb31 and DWC_usb3 v3.30a and higher do not support OTG |
89 | * mode. If the controller supports DRD but the dr_mode is not | |
90 | * specified or set to OTG, then set the mode to peripheral. | |
a7700468 | 91 | */ |
d182c2e1 | 92 | if (mode == USB_DR_MODE_OTG && !dwc->edev && |
8bb14308 TN |
93 | (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) || |
94 | !device_property_read_bool(dwc->dev, "usb-role-switch")) && | |
9af21dd6 | 95 | !DWC3_VER_IS_PRIOR(DWC3, 330A)) |
a7700468 | 96 | mode = USB_DR_MODE_PERIPHERAL; |
9d6173e1 TN |
97 | } |
98 | ||
99 | if (mode != dwc->dr_mode) { | |
100 | dev_warn(dev, | |
101 | "Configuration mismatch. dr_mode forced to %s\n", | |
102 | mode == USB_DR_MODE_HOST ? "host" : "gadget"); | |
103 | ||
104 | dwc->dr_mode = mode; | |
105 | } | |
106 | ||
107 | return 0; | |
108 | } | |
109 | ||
6d735722 TN |
110 | void dwc3_enable_susphy(struct dwc3 *dwc, bool enable) |
111 | { | |
112 | u32 reg; | |
17926415 | 113 | int i; |
6d735722 | 114 | |
17926415 TN |
115 | for (i = 0; i < dwc->num_usb3_ports; i++) { |
116 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(i)); | |
117 | if (enable && !dwc->dis_u3_susphy_quirk) | |
118 | reg |= DWC3_GUSB3PIPECTL_SUSPHY; | |
119 | else | |
120 | reg &= ~DWC3_GUSB3PIPECTL_SUSPHY; | |
6d735722 | 121 | |
17926415 TN |
122 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(i), reg); |
123 | } | |
6d735722 | 124 | |
17926415 TN |
125 | for (i = 0; i < dwc->num_usb2_ports; i++) { |
126 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i)); | |
127 | if (enable && !dwc->dis_u2_susphy_quirk) | |
128 | reg |= DWC3_GUSB2PHYCFG_SUSPHY; | |
129 | else | |
130 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; | |
6d735722 | 131 | |
17926415 TN |
132 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg); |
133 | } | |
6d735722 TN |
134 | } |
135 | ||
cc5bfc4e | 136 | void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode, bool ignore_susphy) |
3140e8cb | 137 | { |
cc5bfc4e | 138 | unsigned int hw_mode; |
3140e8cb SAS |
139 | u32 reg; |
140 | ||
141 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); | |
cc5bfc4e TN |
142 | |
143 | /* | |
144 | * For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE and | |
145 | * GUSB2PHYCFG.SUSPHY should be cleared during mode switching, | |
146 | * and they can be set after core initialization. | |
147 | */ | |
148 | hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0); | |
149 | if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD && !ignore_susphy) { | |
150 | if (DWC3_GCTL_PRTCAP(reg) != mode) | |
151 | dwc3_enable_susphy(dwc, false); | |
152 | } | |
153 | ||
3140e8cb SAS |
154 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); |
155 | reg |= DWC3_GCTL_PRTCAPDIR(mode); | |
156 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
c4a5153e MG |
157 | |
158 | dwc->current_dr_role = mode; | |
41ce1456 RQ |
159 | } |
160 | ||
161 | static void __dwc3_set_mode(struct work_struct *work) | |
162 | { | |
163 | struct dwc3 *dwc = work_to_dwc(work); | |
164 | unsigned long flags; | |
165 | int ret; | |
f580170f | 166 | u32 reg; |
62c73bfe | 167 | u32 desired_dr_role; |
30a46746 | 168 | int i; |
41ce1456 | 169 | |
f88359e1 | 170 | mutex_lock(&dwc->mutex); |
62c73bfe SP |
171 | spin_lock_irqsave(&dwc->lock, flags); |
172 | desired_dr_role = dwc->desired_dr_role; | |
173 | spin_unlock_irqrestore(&dwc->lock, flags); | |
f88359e1 | 174 | |
c2cd3452 MK |
175 | pm_runtime_get_sync(dwc->dev); |
176 | ||
f09cc79b RQ |
177 | if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG) |
178 | dwc3_otg_update(dwc, 0); | |
179 | ||
62c73bfe | 180 | if (!desired_dr_role) |
c2cd3452 | 181 | goto out; |
41ce1456 | 182 | |
62c73bfe | 183 | if (desired_dr_role == dwc->current_dr_role) |
c2cd3452 | 184 | goto out; |
41ce1456 | 185 | |
62c73bfe | 186 | if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev) |
c2cd3452 | 187 | goto out; |
41ce1456 RQ |
188 | |
189 | switch (dwc->current_dr_role) { | |
190 | case DWC3_GCTL_PRTCAP_HOST: | |
191 | dwc3_host_exit(dwc); | |
192 | break; | |
193 | case DWC3_GCTL_PRTCAP_DEVICE: | |
194 | dwc3_gadget_exit(dwc); | |
195 | dwc3_event_buffers_cleanup(dwc); | |
196 | break; | |
f09cc79b RQ |
197 | case DWC3_GCTL_PRTCAP_OTG: |
198 | dwc3_otg_exit(dwc); | |
199 | spin_lock_irqsave(&dwc->lock, flags); | |
200 | dwc->desired_otg_role = DWC3_OTG_ROLE_IDLE; | |
201 | spin_unlock_irqrestore(&dwc->lock, flags); | |
202 | dwc3_otg_update(dwc, 1); | |
203 | break; | |
41ce1456 RQ |
204 | default: |
205 | break; | |
206 | } | |
207 | ||
07903626 RK |
208 | /* |
209 | * When current_dr_role is not set, there's no role switching. | |
210 | * Only perform GCTL.CoreSoftReset when there's DRD role switching. | |
211 | */ | |
212 | if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) || | |
213 | DWC3_VER_IS_PRIOR(DWC31, 190A)) && | |
62c73bfe | 214 | desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) { |
f88359e1 YC |
215 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
216 | reg |= DWC3_GCTL_CORESOFTRESET; | |
217 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
218 | ||
219 | /* | |
220 | * Wait for internal clocks to synchronized. DWC_usb31 and | |
221 | * DWC_usb32 may need at least 50ms (less for DWC_usb3). To | |
222 | * keep it consistent across different IPs, let's wait up to | |
223 | * 100ms before clearing GCTL.CORESOFTRESET. | |
224 | */ | |
225 | msleep(100); | |
226 | ||
227 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); | |
228 | reg &= ~DWC3_GCTL_CORESOFTRESET; | |
229 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
230 | } | |
231 | ||
41ce1456 RQ |
232 | spin_lock_irqsave(&dwc->lock, flags); |
233 | ||
cc5bfc4e | 234 | dwc3_set_prtcap(dwc, desired_dr_role, false); |
6b3261a2 | 235 | |
41ce1456 RQ |
236 | spin_unlock_irqrestore(&dwc->lock, flags); |
237 | ||
62c73bfe | 238 | switch (desired_dr_role) { |
41ce1456 RQ |
239 | case DWC3_GCTL_PRTCAP_HOST: |
240 | ret = dwc3_host_init(dwc); | |
958d1a4c | 241 | if (ret) { |
41ce1456 | 242 | dev_err(dwc->dev, "failed to initialize host\n"); |
958d1a4c FB |
243 | } else { |
244 | if (dwc->usb2_phy) | |
245 | otg_set_vbus(dwc->usb2_phy->otg, true); | |
30a46746 KK |
246 | |
247 | for (i = 0; i < dwc->num_usb2_ports; i++) | |
248 | phy_set_mode(dwc->usb2_generic_phy[i], PHY_MODE_USB_HOST); | |
249 | for (i = 0; i < dwc->num_usb3_ports; i++) | |
250 | phy_set_mode(dwc->usb3_generic_phy[i], PHY_MODE_USB_HOST); | |
251 | ||
f580170f YC |
252 | if (dwc->dis_split_quirk) { |
253 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL3); | |
254 | reg |= DWC3_GUCTL3_SPLITDISABLE; | |
255 | dwc3_writel(dwc->regs, DWC3_GUCTL3, reg); | |
256 | } | |
958d1a4c | 257 | } |
41ce1456 RQ |
258 | break; |
259 | case DWC3_GCTL_PRTCAP_DEVICE: | |
f88359e1 YC |
260 | dwc3_core_soft_reset(dwc); |
261 | ||
41ce1456 | 262 | dwc3_event_buffers_setup(dwc); |
958d1a4c FB |
263 | |
264 | if (dwc->usb2_phy) | |
265 | otg_set_vbus(dwc->usb2_phy->otg, false); | |
30a46746 KK |
266 | phy_set_mode(dwc->usb2_generic_phy[0], PHY_MODE_USB_DEVICE); |
267 | phy_set_mode(dwc->usb3_generic_phy[0], PHY_MODE_USB_DEVICE); | |
958d1a4c | 268 | |
41ce1456 RQ |
269 | ret = dwc3_gadget_init(dwc); |
270 | if (ret) | |
271 | dev_err(dwc->dev, "failed to initialize peripheral\n"); | |
272 | break; | |
f09cc79b RQ |
273 | case DWC3_GCTL_PRTCAP_OTG: |
274 | dwc3_otg_init(dwc); | |
275 | dwc3_otg_update(dwc, 0); | |
276 | break; | |
41ce1456 RQ |
277 | default: |
278 | break; | |
279 | } | |
f09cc79b | 280 | |
c2cd3452 MK |
281 | out: |
282 | pm_runtime_mark_last_busy(dwc->dev); | |
283 | pm_runtime_put_autosuspend(dwc->dev); | |
f88359e1 | 284 | mutex_unlock(&dwc->mutex); |
41ce1456 RQ |
285 | } |
286 | ||
287 | void dwc3_set_mode(struct dwc3 *dwc, u32 mode) | |
288 | { | |
289 | unsigned long flags; | |
290 | ||
dc336b19 LJ |
291 | if (dwc->dr_mode != USB_DR_MODE_OTG) |
292 | return; | |
293 | ||
41ce1456 RQ |
294 | spin_lock_irqsave(&dwc->lock, flags); |
295 | dwc->desired_dr_role = mode; | |
296 | spin_unlock_irqrestore(&dwc->lock, flags); | |
297 | ||
084a804e | 298 | queue_work(system_freezable_wq, &dwc->drd_work); |
3140e8cb | 299 | } |
8300dd23 | 300 | |
cf6d867d FB |
301 | u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type) |
302 | { | |
303 | struct dwc3 *dwc = dep->dwc; | |
304 | u32 reg; | |
305 | ||
306 | dwc3_writel(dwc->regs, DWC3_GDBGFIFOSPACE, | |
307 | DWC3_GDBGFIFOSPACE_NUM(dep->number) | | |
308 | DWC3_GDBGFIFOSPACE_TYPE(type)); | |
309 | ||
310 | reg = dwc3_readl(dwc->regs, DWC3_GDBGFIFOSPACE); | |
311 | ||
312 | return DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(reg); | |
313 | } | |
314 | ||
72246da4 FB |
315 | /** |
316 | * dwc3_core_soft_reset - Issues core soft reset and PHY reset | |
317 | * @dwc: pointer to our context structure | |
318 | */ | |
0066472d | 319 | int dwc3_core_soft_reset(struct dwc3 *dwc) |
72246da4 FB |
320 | { |
321 | u32 reg; | |
f59dcab1 | 322 | int retries = 1000; |
72246da4 | 323 | |
f59dcab1 FB |
324 | /* |
325 | * We're resetting only the device side because, if we're in host mode, | |
326 | * XHCI driver will reset the host block. If dwc3 was configured for | |
afe28cd6 | 327 | * host-only mode, then we can return early. |
f59dcab1 | 328 | */ |
afe28cd6 | 329 | if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST) |
f59dcab1 | 330 | return 0; |
72246da4 | 331 | |
f59dcab1 FB |
332 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
333 | reg |= DWC3_DCTL_CSFTRST; | |
f4fd84ae TN |
334 | reg &= ~DWC3_DCTL_RUN_STOP; |
335 | dwc3_gadget_dctl_write_safe(dwc, reg); | |
72246da4 | 336 | |
4749e0e6 TN |
337 | /* |
338 | * For DWC_usb31 controller 1.90a and later, the DCTL.CSFRST bit | |
339 | * is cleared only after all the clocks are synchronized. This can | |
340 | * take a little more than 50ms. Set the polling rate at 20ms | |
341 | * for 10 times instead. | |
342 | */ | |
9af21dd6 | 343 | if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32)) |
4749e0e6 TN |
344 | retries = 10; |
345 | ||
f59dcab1 FB |
346 | do { |
347 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); | |
348 | if (!(reg & DWC3_DCTL_CSFTRST)) | |
fab38333 | 349 | goto done; |
45627ac6 | 350 | |
9af21dd6 | 351 | if (DWC3_VER_IS_WITHIN(DWC31, 190A, ANY) || DWC3_IP_IS(DWC32)) |
4749e0e6 TN |
352 | msleep(20); |
353 | else | |
354 | udelay(1); | |
f59dcab1 | 355 | } while (--retries); |
57303488 | 356 | |
859bdc35 | 357 | dev_warn(dwc->dev, "DWC3 controller soft reset failed.\n"); |
f59dcab1 | 358 | return -ETIMEDOUT; |
fab38333 TN |
359 | |
360 | done: | |
361 | /* | |
4749e0e6 TN |
362 | * For DWC_usb31 controller 1.80a and prior, once DCTL.CSFRST bit |
363 | * is cleared, we must wait at least 50ms before accessing the PHY | |
364 | * domain (synchronization delay). | |
fab38333 | 365 | */ |
9af21dd6 | 366 | if (DWC3_VER_IS_WITHIN(DWC31, ANY, 180A)) |
fab38333 TN |
367 | msleep(50); |
368 | ||
369 | return 0; | |
72246da4 FB |
370 | } |
371 | ||
db2be4e9 NB |
372 | /* |
373 | * dwc3_frame_length_adjustment - Adjusts frame length if required | |
374 | * @dwc3: Pointer to our controller context structure | |
db2be4e9 | 375 | */ |
bcdb3272 | 376 | static void dwc3_frame_length_adjustment(struct dwc3 *dwc) |
db2be4e9 NB |
377 | { |
378 | u32 reg; | |
379 | u32 dft; | |
380 | ||
9af21dd6 | 381 | if (DWC3_VER_IS_PRIOR(DWC3, 250A)) |
db2be4e9 NB |
382 | return; |
383 | ||
bcdb3272 | 384 | if (dwc->fladj == 0) |
db2be4e9 NB |
385 | return; |
386 | ||
387 | reg = dwc3_readl(dwc->regs, DWC3_GFLADJ); | |
388 | dft = reg & DWC3_GFLADJ_30MHZ_MASK; | |
a7d9874c | 389 | if (dft != dwc->fladj) { |
db2be4e9 | 390 | reg &= ~DWC3_GFLADJ_30MHZ_MASK; |
bcdb3272 | 391 | reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | dwc->fladj; |
db2be4e9 NB |
392 | dwc3_writel(dwc->regs, DWC3_GFLADJ, reg); |
393 | } | |
394 | } | |
395 | ||
7bee3188 BP |
396 | /** |
397 | * dwc3_ref_clk_period - Reference clock period configuration | |
398 | * Default reference clock period depends on hardware | |
399 | * configuration. For systems with reference clock that differs | |
400 | * from the default, this will set clock period in DWC3_GUCTL | |
401 | * register. | |
402 | * @dwc: Pointer to our controller context structure | |
7bee3188 BP |
403 | */ |
404 | static void dwc3_ref_clk_period(struct dwc3 *dwc) | |
405 | { | |
5114c3ee | 406 | unsigned long period; |
596c8785 SA |
407 | unsigned long fladj; |
408 | unsigned long decr; | |
5114c3ee | 409 | unsigned long rate; |
7bee3188 BP |
410 | u32 reg; |
411 | ||
5114c3ee SA |
412 | if (dwc->ref_clk) { |
413 | rate = clk_get_rate(dwc->ref_clk); | |
414 | if (!rate) | |
415 | return; | |
416 | period = NSEC_PER_SEC / rate; | |
417 | } else if (dwc->ref_clk_per) { | |
418 | period = dwc->ref_clk_per; | |
596c8785 | 419 | rate = NSEC_PER_SEC / period; |
5114c3ee | 420 | } else { |
7bee3188 | 421 | return; |
5114c3ee | 422 | } |
7bee3188 BP |
423 | |
424 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL); | |
425 | reg &= ~DWC3_GUCTL_REFCLKPER_MASK; | |
5114c3ee | 426 | reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period); |
7bee3188 | 427 | dwc3_writel(dwc->regs, DWC3_GUCTL, reg); |
7bee3188 | 428 | |
596c8785 SA |
429 | if (DWC3_VER_IS_PRIOR(DWC3, 250A)) |
430 | return; | |
431 | ||
432 | /* | |
433 | * The calculation below is | |
434 | * | |
435 | * 125000 * (NSEC_PER_SEC / (rate * period) - 1) | |
436 | * | |
437 | * but rearranged for fixed-point arithmetic. The division must be | |
438 | * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and | |
439 | * neither does rate * period). | |
440 | * | |
441 | * Note that rate * period ~= NSEC_PER_SECOND, minus the number of | |
442 | * nanoseconds of error caused by the truncation which happened during | |
443 | * the division when calculating rate or period (whichever one was | |
444 | * derived from the other). We first calculate the relative error, then | |
445 | * scale it to units of 8 ppm. | |
446 | */ | |
447 | fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period); | |
448 | fladj -= 125000; | |
449 | ||
450 | /* | |
451 | * The documented 240MHz constant is scaled by 2 to get PLS1 as well. | |
452 | */ | |
453 | decr = 480000000 / rate; | |
454 | ||
455 | reg = dwc3_readl(dwc->regs, DWC3_GFLADJ); | |
456 | reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK | |
457 | & ~DWC3_GFLADJ_240MHZDECR | |
458 | & ~DWC3_GFLADJ_240MHZDECR_PLS1; | |
459 | reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj) | |
460 | | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1) | |
461 | | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1); | |
a6fc2f1b AS |
462 | |
463 | if (dwc->gfladj_refclk_lpm_sel) | |
464 | reg |= DWC3_GFLADJ_REFCLK_LPM_SEL; | |
465 | ||
596c8785 SA |
466 | dwc3_writel(dwc->regs, DWC3_GFLADJ, reg); |
467 | } | |
7bee3188 | 468 | |
72246da4 FB |
469 | /** |
470 | * dwc3_free_one_event_buffer - Frees one event buffer | |
471 | * @dwc: Pointer to our controller context structure | |
472 | * @evt: Pointer to event buffer to be freed | |
473 | */ | |
474 | static void dwc3_free_one_event_buffer(struct dwc3 *dwc, | |
475 | struct dwc3_event_buffer *evt) | |
476 | { | |
d64ff406 | 477 | dma_free_coherent(dwc->sysdev, evt->length, evt->buf, evt->dma); |
72246da4 FB |
478 | } |
479 | ||
480 | /** | |
1d046793 | 481 | * dwc3_alloc_one_event_buffer - Allocates one event buffer structure |
72246da4 FB |
482 | * @dwc: Pointer to our controller context structure |
483 | * @length: size of the event buffer | |
484 | * | |
1d046793 | 485 | * Returns a pointer to the allocated event buffer structure on success |
72246da4 FB |
486 | * otherwise ERR_PTR(errno). |
487 | */ | |
67d0b500 | 488 | static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc, |
ca80ca61 | 489 | unsigned int length) |
72246da4 FB |
490 | { |
491 | struct dwc3_event_buffer *evt; | |
492 | ||
380f0d28 | 493 | evt = devm_kzalloc(dwc->dev, sizeof(*evt), GFP_KERNEL); |
72246da4 FB |
494 | if (!evt) |
495 | return ERR_PTR(-ENOMEM); | |
496 | ||
497 | evt->dwc = dwc; | |
498 | evt->length = length; | |
d9fa4c63 JY |
499 | evt->cache = devm_kzalloc(dwc->dev, length, GFP_KERNEL); |
500 | if (!evt->cache) | |
501 | return ERR_PTR(-ENOMEM); | |
502 | ||
d64ff406 | 503 | evt->buf = dma_alloc_coherent(dwc->sysdev, length, |
72246da4 | 504 | &evt->dma, GFP_KERNEL); |
e32672f0 | 505 | if (!evt->buf) |
72246da4 | 506 | return ERR_PTR(-ENOMEM); |
72246da4 FB |
507 | |
508 | return evt; | |
509 | } | |
510 | ||
511 | /** | |
512 | * dwc3_free_event_buffers - frees all allocated event buffers | |
513 | * @dwc: Pointer to our controller context structure | |
514 | */ | |
515 | static void dwc3_free_event_buffers(struct dwc3 *dwc) | |
516 | { | |
517 | struct dwc3_event_buffer *evt; | |
72246da4 | 518 | |
696c8b12 | 519 | evt = dwc->ev_buf; |
660e9bde FB |
520 | if (evt) |
521 | dwc3_free_one_event_buffer(dwc, evt); | |
72246da4 FB |
522 | } |
523 | ||
524 | /** | |
525 | * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length | |
1d046793 | 526 | * @dwc: pointer to our controller context structure |
72246da4 FB |
527 | * @length: size of event buffer |
528 | * | |
1d046793 | 529 | * Returns 0 on success otherwise negative errno. In the error case, dwc |
72246da4 FB |
530 | * may contain some buffers allocated but not all which were requested. |
531 | */ | |
ca80ca61 | 532 | static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned int length) |
72246da4 | 533 | { |
660e9bde | 534 | struct dwc3_event_buffer *evt; |
89d7f962 KK |
535 | unsigned int hw_mode; |
536 | ||
537 | hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0); | |
538 | if (hw_mode == DWC3_GHWPARAMS0_MODE_HOST) { | |
539 | dwc->ev_buf = NULL; | |
540 | return 0; | |
541 | } | |
72246da4 | 542 | |
660e9bde FB |
543 | evt = dwc3_alloc_one_event_buffer(dwc, length); |
544 | if (IS_ERR(evt)) { | |
545 | dev_err(dwc->dev, "can't allocate event buffer\n"); | |
546 | return PTR_ERR(evt); | |
72246da4 | 547 | } |
696c8b12 | 548 | dwc->ev_buf = evt; |
72246da4 FB |
549 | |
550 | return 0; | |
551 | } | |
552 | ||
553 | /** | |
554 | * dwc3_event_buffers_setup - setup our allocated event buffers | |
1d046793 | 555 | * @dwc: pointer to our controller context structure |
72246da4 FB |
556 | * |
557 | * Returns 0 on success otherwise negative errno. | |
558 | */ | |
f09cc79b | 559 | int dwc3_event_buffers_setup(struct dwc3 *dwc) |
72246da4 FB |
560 | { |
561 | struct dwc3_event_buffer *evt; | |
0d410e89 | 562 | u32 reg; |
72246da4 | 563 | |
89d7f962 KK |
564 | if (!dwc->ev_buf) |
565 | return 0; | |
566 | ||
696c8b12 | 567 | evt = dwc->ev_buf; |
660e9bde | 568 | evt->lpos = 0; |
660e9bde FB |
569 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), |
570 | lower_32_bits(evt->dma)); | |
571 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), | |
572 | upper_32_bits(evt->dma)); | |
573 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), | |
574 | DWC3_GEVNTSIZ_SIZE(evt->length)); | |
72246da4 | 575 | |
0d410e89 SG |
576 | /* Clear any stale event */ |
577 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); | |
578 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), reg); | |
72246da4 FB |
579 | return 0; |
580 | } | |
581 | ||
f09cc79b | 582 | void dwc3_event_buffers_cleanup(struct dwc3 *dwc) |
72246da4 FB |
583 | { |
584 | struct dwc3_event_buffer *evt; | |
14e49718 | 585 | u32 reg; |
72246da4 | 586 | |
89d7f962 KK |
587 | if (!dwc->ev_buf) |
588 | return; | |
14e49718 SG |
589 | /* |
590 | * Exynos platforms may not be able to access event buffer if the | |
591 | * controller failed to halt on dwc3_core_exit(). | |
592 | */ | |
593 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); | |
594 | if (!(reg & DWC3_DSTS_DEVCTRLHLT)) | |
595 | return; | |
89d7f962 | 596 | |
696c8b12 | 597 | evt = dwc->ev_buf; |
7acd85e0 | 598 | |
660e9bde | 599 | evt->lpos = 0; |
7acd85e0 | 600 | |
660e9bde FB |
601 | dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(0), 0); |
602 | dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(0), 0); | |
603 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), DWC3_GEVNTSIZ_INTMASK | |
604 | | DWC3_GEVNTSIZ_SIZE(0)); | |
0d410e89 SG |
605 | |
606 | /* Clear any stale event */ | |
607 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); | |
608 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), reg); | |
72246da4 FB |
609 | } |
610 | ||
789451f6 FB |
611 | static void dwc3_core_num_eps(struct dwc3 *dwc) |
612 | { | |
613 | struct dwc3_hwparams *parms = &dwc->hwparams; | |
614 | ||
47d3946e | 615 | dwc->num_eps = DWC3_NUM_EPS(parms); |
789451f6 FB |
616 | } |
617 | ||
41ac7b3a | 618 | static void dwc3_cache_hwparams(struct dwc3 *dwc) |
26ceca97 FB |
619 | { |
620 | struct dwc3_hwparams *parms = &dwc->hwparams; | |
621 | ||
622 | parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0); | |
623 | parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1); | |
624 | parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2); | |
625 | parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3); | |
626 | parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4); | |
627 | parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5); | |
628 | parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); | |
629 | parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7); | |
630 | parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8); | |
16710380 TN |
631 | |
632 | if (DWC3_IP_IS(DWC32)) | |
633 | parms->hwparams9 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS9); | |
26ceca97 FB |
634 | } |
635 | ||
d504bfa6 RSP |
636 | static void dwc3_config_soc_bus(struct dwc3 *dwc) |
637 | { | |
638 | if (dwc->gsbuscfg0_reqinfo != DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED) { | |
639 | u32 reg; | |
640 | ||
641 | reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0); | |
642 | reg &= ~DWC3_GSBUSCFG0_REQINFO(~0); | |
643 | reg |= DWC3_GSBUSCFG0_REQINFO(dwc->gsbuscfg0_reqinfo); | |
644 | dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, reg); | |
645 | } | |
646 | } | |
647 | ||
98112041 RQ |
648 | static int dwc3_core_ulpi_init(struct dwc3 *dwc) |
649 | { | |
650 | int intf; | |
651 | int ret = 0; | |
652 | ||
653 | intf = DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3); | |
654 | ||
655 | if (intf == DWC3_GHWPARAMS3_HSPHY_IFC_ULPI || | |
656 | (intf == DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI && | |
657 | dwc->hsphy_interface && | |
658 | !strncmp(dwc->hsphy_interface, "ulpi", 4))) | |
659 | ret = dwc3_ulpi_init(dwc); | |
660 | ||
661 | return ret; | |
662 | } | |
663 | ||
30a46746 | 664 | static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index) |
b5a65c40 HR |
665 | { |
666 | u32 reg; | |
667 | ||
30a46746 | 668 | reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(index)); |
b5a65c40 | 669 | |
1966b865 FB |
670 | /* |
671 | * Make sure UX_EXIT_PX is cleared as that causes issues with some | |
672 | * PHYs. Also, this bit is not supposed to be used in normal operation. | |
673 | */ | |
674 | reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX; | |
675 | ||
cc5bfc4e | 676 | /* Ensure the GUSB3PIPECTL.SUSPENDENABLE is cleared prior to phy init. */ |
6d735722 | 677 | reg &= ~DWC3_GUSB3PIPECTL_SUSPHY; |
9ba3aca8 | 678 | |
b5a65c40 HR |
679 | if (dwc->u2ss_inp3_quirk) |
680 | reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK; | |
681 | ||
e58dd357 RB |
682 | if (dwc->dis_rxdet_inp3_quirk) |
683 | reg |= DWC3_GUSB3PIPECTL_DISRXDETINP3; | |
684 | ||
df31f5b3 HR |
685 | if (dwc->req_p1p2p3_quirk) |
686 | reg |= DWC3_GUSB3PIPECTL_REQP1P2P3; | |
687 | ||
a2a1d0f5 HR |
688 | if (dwc->del_p1p2p3_quirk) |
689 | reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN; | |
690 | ||
41c06ffd HR |
691 | if (dwc->del_phy_power_chg_quirk) |
692 | reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE; | |
693 | ||
fb67afca HR |
694 | if (dwc->lfps_filter_quirk) |
695 | reg |= DWC3_GUSB3PIPECTL_LFPSFILT; | |
696 | ||
14f4ac53 HR |
697 | if (dwc->rx_detect_poll_quirk) |
698 | reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL; | |
699 | ||
6b6a0c9a HR |
700 | if (dwc->tx_de_emphasis_quirk) |
701 | reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis); | |
702 | ||
00fe081d WW |
703 | if (dwc->dis_del_phy_power_chg_quirk) |
704 | reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE; | |
705 | ||
30a46746 KK |
706 | dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(index), reg); |
707 | ||
708 | return 0; | |
709 | } | |
710 | ||
711 | static int dwc3_hs_phy_setup(struct dwc3 *dwc, int index) | |
712 | { | |
30a46746 | 713 | u32 reg; |
b5a65c40 | 714 | |
30a46746 | 715 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(index)); |
2164a476 | 716 | |
3e10a2ce HK |
717 | /* Select the HS PHY interface */ |
718 | switch (DWC3_GHWPARAMS3_HSPHY_IFC(dwc->hwparams.hwparams3)) { | |
719 | case DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI: | |
43cacb03 FB |
720 | if (dwc->hsphy_interface && |
721 | !strncmp(dwc->hsphy_interface, "utmi", 4)) { | |
3e10a2ce | 722 | reg &= ~DWC3_GUSB2PHYCFG_ULPI_UTMI; |
88bc9d19 | 723 | break; |
43cacb03 FB |
724 | } else if (dwc->hsphy_interface && |
725 | !strncmp(dwc->hsphy_interface, "ulpi", 4)) { | |
3e10a2ce | 726 | reg |= DWC3_GUSB2PHYCFG_ULPI_UTMI; |
30a46746 | 727 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(index), reg); |
3e10a2ce | 728 | } else { |
88bc9d19 HK |
729 | /* Relying on default value. */ |
730 | if (!(reg & DWC3_GUSB2PHYCFG_ULPI_UTMI)) | |
731 | break; | |
3e10a2ce | 732 | } |
df561f66 | 733 | fallthrough; |
88bc9d19 | 734 | case DWC3_GHWPARAMS3_HSPHY_IFC_ULPI: |
3e10a2ce HK |
735 | default: |
736 | break; | |
737 | } | |
738 | ||
32f2ed86 WW |
739 | switch (dwc->hsphy_mode) { |
740 | case USBPHY_INTERFACE_MODE_UTMI: | |
741 | reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK | | |
742 | DWC3_GUSB2PHYCFG_USBTRDTIM_MASK); | |
743 | reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) | | |
744 | DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT); | |
745 | break; | |
746 | case USBPHY_INTERFACE_MODE_UTMIW: | |
747 | reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK | | |
748 | DWC3_GUSB2PHYCFG_USBTRDTIM_MASK); | |
749 | reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) | | |
750 | DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT); | |
751 | break; | |
752 | default: | |
753 | break; | |
754 | } | |
755 | ||
cc5bfc4e | 756 | /* Ensure the GUSB2PHYCFG.SUSPHY is cleared prior to phy init. */ |
6d735722 | 757 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
0effe0a3 | 758 | |
ec791d14 JY |
759 | if (dwc->dis_enblslpm_quirk) |
760 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; | |
eafeacf1 TN |
761 | else |
762 | reg |= DWC3_GUSB2PHYCFG_ENBLSLPM; | |
ec791d14 | 763 | |
a6fc2f1b | 764 | if (dwc->dis_u2_freeclk_exists_quirk || dwc->gfladj_refclk_lpm_sel) |
16199f33 WW |
765 | reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS; |
766 | ||
b84ba26c PM |
767 | /* |
768 | * Some ULPI USB PHY does not support internal VBUS supply, to drive | |
769 | * the CPEN pin requires the configuration of the ULPI DRVVBUSEXTERNAL | |
770 | * bit of OTG_CTRL register. Controller configures the USB2 PHY | |
771 | * ULPIEXTVBUSDRV bit[17] of the GUSB2PHYCFG register to drive vBus | |
772 | * with an external supply. | |
773 | */ | |
774 | if (dwc->ulpi_ext_vbus_drv) | |
775 | reg |= DWC3_GUSB2PHYCFG_ULPIEXTVBUSDRV; | |
776 | ||
30a46746 KK |
777 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(index), reg); |
778 | ||
779 | return 0; | |
780 | } | |
781 | ||
782 | /** | |
783 | * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core | |
784 | * @dwc: Pointer to our controller context structure | |
785 | * | |
786 | * Returns 0 on success. The USB PHY interfaces are configured but not | |
787 | * initialized. The PHY interfaces and the PHYs get initialized together with | |
788 | * the core in dwc3_core_init. | |
789 | */ | |
790 | static int dwc3_phy_setup(struct dwc3 *dwc) | |
791 | { | |
792 | int i; | |
793 | int ret; | |
794 | ||
795 | for (i = 0; i < dwc->num_usb3_ports; i++) { | |
796 | ret = dwc3_ss_phy_setup(dwc, i); | |
797 | if (ret) | |
798 | return ret; | |
799 | } | |
800 | ||
801 | for (i = 0; i < dwc->num_usb2_ports; i++) { | |
802 | ret = dwc3_hs_phy_setup(dwc, i); | |
803 | if (ret) | |
804 | return ret; | |
805 | } | |
88bc9d19 HK |
806 | |
807 | return 0; | |
b5a65c40 HR |
808 | } |
809 | ||
1d72fab4 JH |
810 | static int dwc3_phy_init(struct dwc3 *dwc) |
811 | { | |
812 | int ret; | |
30a46746 KK |
813 | int i; |
814 | int j; | |
1d72fab4 JH |
815 | |
816 | usb_phy_init(dwc->usb2_phy); | |
817 | usb_phy_init(dwc->usb3_phy); | |
818 | ||
30a46746 KK |
819 | for (i = 0; i < dwc->num_usb2_ports; i++) { |
820 | ret = phy_init(dwc->usb2_generic_phy[i]); | |
821 | if (ret < 0) | |
822 | goto err_exit_usb2_phy; | |
823 | } | |
1d72fab4 | 824 | |
30a46746 KK |
825 | for (j = 0; j < dwc->num_usb3_ports; j++) { |
826 | ret = phy_init(dwc->usb3_generic_phy[j]); | |
827 | if (ret < 0) | |
828 | goto err_exit_usb3_phy; | |
829 | } | |
1d72fab4 | 830 | |
cc5bfc4e TN |
831 | /* |
832 | * Above DWC_usb3.0 1.94a, it is recommended to set | |
833 | * DWC3_GUSB3PIPECTL_SUSPHY and DWC3_GUSB2PHYCFG_SUSPHY to '0' during | |
834 | * coreConsultant configuration. So default value will be '0' when the | |
835 | * core is reset. Application needs to set it to '1' after the core | |
836 | * initialization is completed. | |
837 | * | |
838 | * Certain phy requires to be in P0 power state during initialization. | |
839 | * Make sure GUSB3PIPECTL.SUSPENDENABLE and GUSB2PHYCFG.SUSPHY are clear | |
840 | * prior to phy init to maintain in the P0 state. | |
841 | * | |
842 | * After phy initialization, some phy operations can only be executed | |
843 | * while in lower P states. Ensure GUSB3PIPECTL.SUSPENDENABLE and | |
844 | * GUSB2PHYCFG.SUSPHY are set soon after initialization to avoid | |
845 | * blocking phy ops. | |
846 | */ | |
847 | if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) | |
848 | dwc3_enable_susphy(dwc, true); | |
849 | ||
1d72fab4 JH |
850 | return 0; |
851 | ||
30a46746 KK |
852 | err_exit_usb3_phy: |
853 | while (--j >= 0) | |
854 | phy_exit(dwc->usb3_generic_phy[j]); | |
855 | ||
1d72fab4 | 856 | err_exit_usb2_phy: |
30a46746 KK |
857 | while (--i >= 0) |
858 | phy_exit(dwc->usb2_generic_phy[i]); | |
859 | ||
1d72fab4 JH |
860 | usb_phy_shutdown(dwc->usb3_phy); |
861 | usb_phy_shutdown(dwc->usb2_phy); | |
862 | ||
863 | return ret; | |
864 | } | |
865 | ||
866 | static void dwc3_phy_exit(struct dwc3 *dwc) | |
867 | { | |
30a46746 KK |
868 | int i; |
869 | ||
870 | for (i = 0; i < dwc->num_usb3_ports; i++) | |
871 | phy_exit(dwc->usb3_generic_phy[i]); | |
872 | ||
873 | for (i = 0; i < dwc->num_usb2_ports; i++) | |
874 | phy_exit(dwc->usb2_generic_phy[i]); | |
1d72fab4 JH |
875 | |
876 | usb_phy_shutdown(dwc->usb3_phy); | |
877 | usb_phy_shutdown(dwc->usb2_phy); | |
878 | } | |
879 | ||
880 | static int dwc3_phy_power_on(struct dwc3 *dwc) | |
881 | { | |
882 | int ret; | |
30a46746 KK |
883 | int i; |
884 | int j; | |
1d72fab4 JH |
885 | |
886 | usb_phy_set_suspend(dwc->usb2_phy, 0); | |
887 | usb_phy_set_suspend(dwc->usb3_phy, 0); | |
888 | ||
30a46746 KK |
889 | for (i = 0; i < dwc->num_usb2_ports; i++) { |
890 | ret = phy_power_on(dwc->usb2_generic_phy[i]); | |
891 | if (ret < 0) | |
892 | goto err_power_off_usb2_phy; | |
893 | } | |
1d72fab4 | 894 | |
30a46746 KK |
895 | for (j = 0; j < dwc->num_usb3_ports; j++) { |
896 | ret = phy_power_on(dwc->usb3_generic_phy[j]); | |
897 | if (ret < 0) | |
898 | goto err_power_off_usb3_phy; | |
899 | } | |
1d72fab4 JH |
900 | |
901 | return 0; | |
902 | ||
30a46746 KK |
903 | err_power_off_usb3_phy: |
904 | while (--j >= 0) | |
905 | phy_power_off(dwc->usb3_generic_phy[j]); | |
906 | ||
1d72fab4 | 907 | err_power_off_usb2_phy: |
30a46746 KK |
908 | while (--i >= 0) |
909 | phy_power_off(dwc->usb2_generic_phy[i]); | |
910 | ||
1d72fab4 JH |
911 | usb_phy_set_suspend(dwc->usb3_phy, 1); |
912 | usb_phy_set_suspend(dwc->usb2_phy, 1); | |
913 | ||
914 | return ret; | |
915 | } | |
916 | ||
917 | static void dwc3_phy_power_off(struct dwc3 *dwc) | |
918 | { | |
30a46746 KK |
919 | int i; |
920 | ||
921 | for (i = 0; i < dwc->num_usb3_ports; i++) | |
922 | phy_power_off(dwc->usb3_generic_phy[i]); | |
923 | ||
924 | for (i = 0; i < dwc->num_usb2_ports; i++) | |
925 | phy_power_off(dwc->usb2_generic_phy[i]); | |
1d72fab4 JH |
926 | |
927 | usb_phy_set_suspend(dwc->usb3_phy, 1); | |
928 | usb_phy_set_suspend(dwc->usb2_phy, 1); | |
929 | } | |
930 | ||
33fb697e SA |
931 | static int dwc3_clk_enable(struct dwc3 *dwc) |
932 | { | |
933 | int ret; | |
934 | ||
935 | ret = clk_prepare_enable(dwc->bus_clk); | |
936 | if (ret) | |
937 | return ret; | |
938 | ||
939 | ret = clk_prepare_enable(dwc->ref_clk); | |
940 | if (ret) | |
941 | goto disable_bus_clk; | |
942 | ||
943 | ret = clk_prepare_enable(dwc->susp_clk); | |
944 | if (ret) | |
945 | goto disable_ref_clk; | |
946 | ||
97789b93 SR |
947 | ret = clk_prepare_enable(dwc->utmi_clk); |
948 | if (ret) | |
949 | goto disable_susp_clk; | |
950 | ||
951 | ret = clk_prepare_enable(dwc->pipe_clk); | |
952 | if (ret) | |
953 | goto disable_utmi_clk; | |
954 | ||
33fb697e SA |
955 | return 0; |
956 | ||
97789b93 SR |
957 | disable_utmi_clk: |
958 | clk_disable_unprepare(dwc->utmi_clk); | |
959 | disable_susp_clk: | |
960 | clk_disable_unprepare(dwc->susp_clk); | |
33fb697e SA |
961 | disable_ref_clk: |
962 | clk_disable_unprepare(dwc->ref_clk); | |
963 | disable_bus_clk: | |
964 | clk_disable_unprepare(dwc->bus_clk); | |
965 | return ret; | |
966 | } | |
967 | ||
968 | static void dwc3_clk_disable(struct dwc3 *dwc) | |
969 | { | |
97789b93 SR |
970 | clk_disable_unprepare(dwc->pipe_clk); |
971 | clk_disable_unprepare(dwc->utmi_clk); | |
33fb697e SA |
972 | clk_disable_unprepare(dwc->susp_clk); |
973 | clk_disable_unprepare(dwc->ref_clk); | |
974 | clk_disable_unprepare(dwc->bus_clk); | |
975 | } | |
976 | ||
c499ff71 FB |
977 | static void dwc3_core_exit(struct dwc3 *dwc) |
978 | { | |
979 | dwc3_event_buffers_cleanup(dwc); | |
1d72fab4 JH |
980 | dwc3_phy_power_off(dwc); |
981 | dwc3_phy_exit(dwc); | |
33fb697e | 982 | dwc3_clk_disable(dwc); |
fe8abf33 | 983 | reset_control_assert(dwc->reset); |
c499ff71 FB |
984 | } |
985 | ||
0759956f | 986 | static bool dwc3_core_is_valid(struct dwc3 *dwc) |
72246da4 | 987 | { |
0759956f | 988 | u32 reg; |
72246da4 | 989 | |
7650bd74 | 990 | reg = dwc3_readl(dwc->regs, DWC3_GSNPSID); |
9af21dd6 | 991 | dwc->ip = DWC3_GSNPS_ID(reg); |
0759956f | 992 | |
7650bd74 | 993 | /* This should read as U3 followed by revision number */ |
9af21dd6 | 994 | if (DWC3_IP_IS(DWC3)) { |
690fb371 | 995 | dwc->revision = reg; |
9af21dd6 | 996 | } else if (DWC3_IP_IS(DWC31) || DWC3_IP_IS(DWC32)) { |
690fb371 | 997 | dwc->revision = dwc3_readl(dwc->regs, DWC3_VER_NUMBER); |
475d8e01 | 998 | dwc->version_type = dwc3_readl(dwc->regs, DWC3_VER_TYPE); |
690fb371 | 999 | } else { |
0759956f | 1000 | return false; |
7650bd74 | 1001 | } |
7650bd74 | 1002 | |
0759956f FB |
1003 | return true; |
1004 | } | |
58a0f23f | 1005 | |
941f918e | 1006 | static void dwc3_core_setup_global_control(struct dwc3 *dwc) |
0759956f | 1007 | { |
fc1d1a71 JW |
1008 | unsigned int power_opt; |
1009 | unsigned int hw_mode; | |
941f918e | 1010 | u32 reg; |
c499ff71 | 1011 | |
4878a028 | 1012 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
3e87c42a | 1013 | reg &= ~DWC3_GCTL_SCALEDOWN_MASK; |
fc1d1a71 JW |
1014 | hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0); |
1015 | power_opt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); | |
4878a028 | 1016 | |
fc1d1a71 | 1017 | switch (power_opt) { |
4878a028 | 1018 | case DWC3_GHWPARAMS1_EN_PWROPT_CLK: |
32a4a135 FB |
1019 | /** |
1020 | * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an | |
1021 | * issue which would cause xHCI compliance tests to fail. | |
1022 | * | |
1023 | * Because of that we cannot enable clock gating on such | |
1024 | * configurations. | |
1025 | * | |
1026 | * Refers to: | |
1027 | * | |
1028 | * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based | |
1029 | * SOF/ITP Mode Used | |
1030 | */ | |
1031 | if ((dwc->dr_mode == USB_DR_MODE_HOST || | |
1032 | dwc->dr_mode == USB_DR_MODE_OTG) && | |
9af21dd6 | 1033 | DWC3_VER_IS_WITHIN(DWC3, 210A, 250A)) |
32a4a135 FB |
1034 | reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC; |
1035 | else | |
1036 | reg &= ~DWC3_GCTL_DSBLCLKGTNG; | |
4878a028 | 1037 | break; |
0ffcaf37 | 1038 | case DWC3_GHWPARAMS1_EN_PWROPT_HIB: |
2eac3992 HR |
1039 | /* |
1040 | * REVISIT Enabling this bit so that host-mode hibernation | |
1041 | * will work. Device-mode hibernation is not yet implemented. | |
1042 | */ | |
1043 | reg |= DWC3_GCTL_GBLHIBERNATIONEN; | |
0ffcaf37 | 1044 | break; |
4878a028 | 1045 | default: |
5eb30ced FB |
1046 | /* nothing */ |
1047 | break; | |
4878a028 SAS |
1048 | } |
1049 | ||
fc1d1a71 JW |
1050 | /* |
1051 | * This is a workaround for STAR#4846132, which only affects | |
1052 | * DWC_usb31 version2.00a operating in host mode. | |
1053 | * | |
1054 | * There is a problem in DWC_usb31 version 2.00a operating | |
1055 | * in host mode that would cause a CSR read timeout When CSR | |
1056 | * read coincides with RAM Clock Gating Entry. By disable | |
1057 | * Clock Gating, sacrificing power consumption for normal | |
1058 | * operation. | |
1059 | */ | |
1060 | if (power_opt != DWC3_GHWPARAMS1_EN_PWROPT_NO && | |
1061 | hw_mode != DWC3_GHWPARAMS0_MODE_GADGET && DWC3_VER_IS(DWC31, 200A)) | |
1062 | reg |= DWC3_GCTL_DSBLCLKGTNG; | |
1063 | ||
946bd579 HR |
1064 | /* check if current dwc3 is on simulation board */ |
1065 | if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) { | |
6af19fd1 | 1066 | dev_info(dwc->dev, "Running with FPGA optimizations\n"); |
946bd579 HR |
1067 | dwc->is_fpga = true; |
1068 | } | |
1069 | ||
3b81221a HR |
1070 | WARN_ONCE(dwc->disable_scramble_quirk && !dwc->is_fpga, |
1071 | "disable_scramble cannot be used on non-FPGA builds\n"); | |
1072 | ||
1073 | if (dwc->disable_scramble_quirk && dwc->is_fpga) | |
1074 | reg |= DWC3_GCTL_DISSCRAMBLE; | |
1075 | else | |
1076 | reg &= ~DWC3_GCTL_DISSCRAMBLE; | |
1077 | ||
9a5b2f31 HR |
1078 | if (dwc->u2exit_lfps_quirk) |
1079 | reg |= DWC3_GCTL_U2EXIT_LFPS; | |
1080 | ||
4878a028 SAS |
1081 | /* |
1082 | * WORKAROUND: DWC3 revisions <1.90a have a bug | |
1d046793 | 1083 | * where the device can fail to connect at SuperSpeed |
4878a028 | 1084 | * and falls back to high-speed mode which causes |
1d046793 | 1085 | * the device to enter a Connect/Disconnect loop |
4878a028 | 1086 | */ |
9af21dd6 | 1087 | if (DWC3_VER_IS_PRIOR(DWC3, 190A)) |
4878a028 SAS |
1088 | reg |= DWC3_GCTL_U2RSTECN; |
1089 | ||
1090 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
941f918e FB |
1091 | } |
1092 | ||
f54edb53 | 1093 | static int dwc3_core_get_phy(struct dwc3 *dwc); |
98112041 | 1094 | static int dwc3_core_ulpi_init(struct dwc3 *dwc); |
f54edb53 | 1095 | |
d9612c2f PM |
1096 | /* set global incr burst type configuration registers */ |
1097 | static void dwc3_set_incr_burst_type(struct dwc3 *dwc) | |
1098 | { | |
1099 | struct device *dev = dwc->dev; | |
1100 | /* incrx_mode : for INCR burst type. */ | |
1101 | bool incrx_mode; | |
1102 | /* incrx_size : for size of INCRX burst. */ | |
1103 | u32 incrx_size; | |
1104 | u32 *vals; | |
1105 | u32 cfg; | |
1106 | int ntype; | |
1107 | int ret; | |
1108 | int i; | |
1109 | ||
1110 | cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0); | |
1111 | ||
1112 | /* | |
1113 | * Handle property "snps,incr-burst-type-adjustment". | |
1114 | * Get the number of value from this property: | |
1115 | * result <= 0, means this property is not supported. | |
1116 | * result = 1, means INCRx burst mode supported. | |
1117 | * result > 1, means undefined length burst mode supported. | |
1118 | */ | |
a6e5e679 | 1119 | ntype = device_property_count_u32(dev, "snps,incr-burst-type-adjustment"); |
d9612c2f PM |
1120 | if (ntype <= 0) |
1121 | return; | |
1122 | ||
1123 | vals = kcalloc(ntype, sizeof(u32), GFP_KERNEL); | |
4ea15088 | 1124 | if (!vals) |
d9612c2f | 1125 | return; |
d9612c2f PM |
1126 | |
1127 | /* Get INCR burst type, and parse it */ | |
1128 | ret = device_property_read_u32_array(dev, | |
1129 | "snps,incr-burst-type-adjustment", vals, ntype); | |
1130 | if (ret) { | |
75ecb9dd | 1131 | kfree(vals); |
d9612c2f PM |
1132 | dev_err(dev, "Error to get property\n"); |
1133 | return; | |
1134 | } | |
1135 | ||
1136 | incrx_size = *vals; | |
1137 | ||
1138 | if (ntype > 1) { | |
1139 | /* INCRX (undefined length) burst mode */ | |
1140 | incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE; | |
1141 | for (i = 1; i < ntype; i++) { | |
1142 | if (vals[i] > incrx_size) | |
1143 | incrx_size = vals[i]; | |
1144 | } | |
1145 | } else { | |
1146 | /* INCRX burst mode */ | |
1147 | incrx_mode = INCRX_BURST_MODE; | |
1148 | } | |
1149 | ||
75ecb9dd AS |
1150 | kfree(vals); |
1151 | ||
d9612c2f PM |
1152 | /* Enable Undefined Length INCR Burst and Enable INCRx Burst */ |
1153 | cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK; | |
1154 | if (incrx_mode) | |
1155 | cfg |= DWC3_GSBUSCFG0_INCRBRSTENA; | |
1156 | switch (incrx_size) { | |
1157 | case 256: | |
1158 | cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA; | |
1159 | break; | |
1160 | case 128: | |
1161 | cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA; | |
1162 | break; | |
1163 | case 64: | |
1164 | cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA; | |
1165 | break; | |
1166 | case 32: | |
1167 | cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA; | |
1168 | break; | |
1169 | case 16: | |
1170 | cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA; | |
1171 | break; | |
1172 | case 8: | |
1173 | cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA; | |
1174 | break; | |
1175 | case 4: | |
1176 | cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA; | |
1177 | break; | |
1178 | case 1: | |
1179 | break; | |
1180 | default: | |
1181 | dev_err(dev, "Invalid property\n"); | |
1182 | break; | |
1183 | } | |
1184 | ||
1185 | dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg); | |
1186 | } | |
1187 | ||
3497b9a5 LJ |
1188 | static void dwc3_set_power_down_clk_scale(struct dwc3 *dwc) |
1189 | { | |
1190 | u32 scale; | |
1191 | u32 reg; | |
1192 | ||
1193 | if (!dwc->susp_clk) | |
1194 | return; | |
1195 | ||
1196 | /* | |
1197 | * The power down scale field specifies how many suspend_clk | |
1198 | * periods fit into a 16KHz clock period. When performing | |
1199 | * the division, round up the remainder. | |
1200 | * | |
1201 | * The power down scale value is calculated using the fastest | |
1202 | * frequency of the suspend_clk. If it isn't fixed (but within | |
1203 | * the accuracy requirement), the driver may not know the max | |
1204 | * rate of the suspend_clk, so only update the power down scale | |
1205 | * if the default is less than the calculated value from | |
1206 | * clk_get_rate() or if the default is questionably high | |
1207 | * (3x or more) to be within the requirement. | |
1208 | */ | |
1209 | scale = DIV_ROUND_UP(clk_get_rate(dwc->susp_clk), 16000); | |
1210 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); | |
1211 | if ((reg & DWC3_GCTL_PWRDNSCALE_MASK) < DWC3_GCTL_PWRDNSCALE(scale) || | |
1212 | (reg & DWC3_GCTL_PWRDNSCALE_MASK) > DWC3_GCTL_PWRDNSCALE(scale*3)) { | |
1213 | reg &= ~(DWC3_GCTL_PWRDNSCALE_MASK); | |
1214 | reg |= DWC3_GCTL_PWRDNSCALE(scale); | |
1215 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); | |
1216 | } | |
1217 | } | |
1218 | ||
e72fc8d6 SC |
1219 | static void dwc3_config_threshold(struct dwc3 *dwc) |
1220 | { | |
1221 | u32 reg; | |
1222 | u8 rx_thr_num; | |
1223 | u8 rx_maxburst; | |
1224 | u8 tx_thr_num; | |
1225 | u8 tx_maxburst; | |
1226 | ||
1227 | /* | |
1228 | * Must config both number of packets and max burst settings to enable | |
1229 | * RX and/or TX threshold. | |
1230 | */ | |
1231 | if (!DWC3_IP_IS(DWC3) && dwc->dr_mode == USB_DR_MODE_HOST) { | |
1232 | rx_thr_num = dwc->rx_thr_num_pkt_prd; | |
1233 | rx_maxburst = dwc->rx_max_burst_prd; | |
1234 | tx_thr_num = dwc->tx_thr_num_pkt_prd; | |
1235 | tx_maxburst = dwc->tx_max_burst_prd; | |
1236 | ||
1237 | if (rx_thr_num && rx_maxburst) { | |
1238 | reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); | |
1239 | reg |= DWC31_RXTHRNUMPKTSEL_PRD; | |
1240 | ||
1241 | reg &= ~DWC31_RXTHRNUMPKT_PRD(~0); | |
1242 | reg |= DWC31_RXTHRNUMPKT_PRD(rx_thr_num); | |
1243 | ||
1244 | reg &= ~DWC31_MAXRXBURSTSIZE_PRD(~0); | |
1245 | reg |= DWC31_MAXRXBURSTSIZE_PRD(rx_maxburst); | |
1246 | ||
1247 | dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); | |
1248 | } | |
1249 | ||
1250 | if (tx_thr_num && tx_maxburst) { | |
1251 | reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG); | |
1252 | reg |= DWC31_TXTHRNUMPKTSEL_PRD; | |
1253 | ||
1254 | reg &= ~DWC31_TXTHRNUMPKT_PRD(~0); | |
1255 | reg |= DWC31_TXTHRNUMPKT_PRD(tx_thr_num); | |
1256 | ||
1257 | reg &= ~DWC31_MAXTXBURSTSIZE_PRD(~0); | |
1258 | reg |= DWC31_MAXTXBURSTSIZE_PRD(tx_maxburst); | |
1259 | ||
1260 | dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg); | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | rx_thr_num = dwc->rx_thr_num_pkt; | |
1265 | rx_maxburst = dwc->rx_max_burst; | |
1266 | tx_thr_num = dwc->tx_thr_num_pkt; | |
1267 | tx_maxburst = dwc->tx_max_burst; | |
1268 | ||
1269 | if (DWC3_IP_IS(DWC3)) { | |
1270 | if (rx_thr_num && rx_maxburst) { | |
1271 | reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); | |
1272 | reg |= DWC3_GRXTHRCFG_PKTCNTSEL; | |
1273 | ||
1274 | reg &= ~DWC3_GRXTHRCFG_RXPKTCNT(~0); | |
1275 | reg |= DWC3_GRXTHRCFG_RXPKTCNT(rx_thr_num); | |
1276 | ||
1277 | reg &= ~DWC3_GRXTHRCFG_MAXRXBURSTSIZE(~0); | |
1278 | reg |= DWC3_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst); | |
1279 | ||
1280 | dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); | |
1281 | } | |
1282 | ||
1283 | if (tx_thr_num && tx_maxburst) { | |
1284 | reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG); | |
1285 | reg |= DWC3_GTXTHRCFG_PKTCNTSEL; | |
1286 | ||
1287 | reg &= ~DWC3_GTXTHRCFG_TXPKTCNT(~0); | |
1288 | reg |= DWC3_GTXTHRCFG_TXPKTCNT(tx_thr_num); | |
1289 | ||
1290 | reg &= ~DWC3_GTXTHRCFG_MAXTXBURSTSIZE(~0); | |
1291 | reg |= DWC3_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst); | |
1292 | ||
1293 | dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg); | |
1294 | } | |
1295 | } else { | |
1296 | if (rx_thr_num && rx_maxburst) { | |
1297 | reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); | |
1298 | reg |= DWC31_GRXTHRCFG_PKTCNTSEL; | |
1299 | ||
1300 | reg &= ~DWC31_GRXTHRCFG_RXPKTCNT(~0); | |
1301 | reg |= DWC31_GRXTHRCFG_RXPKTCNT(rx_thr_num); | |
1302 | ||
1303 | reg &= ~DWC31_GRXTHRCFG_MAXRXBURSTSIZE(~0); | |
1304 | reg |= DWC31_GRXTHRCFG_MAXRXBURSTSIZE(rx_maxburst); | |
1305 | ||
1306 | dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); | |
1307 | } | |
1308 | ||
1309 | if (tx_thr_num && tx_maxburst) { | |
1310 | reg = dwc3_readl(dwc->regs, DWC3_GTXTHRCFG); | |
1311 | reg |= DWC31_GTXTHRCFG_PKTCNTSEL; | |
1312 | ||
1313 | reg &= ~DWC31_GTXTHRCFG_TXPKTCNT(~0); | |
1314 | reg |= DWC31_GTXTHRCFG_TXPKTCNT(tx_thr_num); | |
1315 | ||
1316 | reg &= ~DWC31_GTXTHRCFG_MAXTXBURSTSIZE(~0); | |
1317 | reg |= DWC31_GTXTHRCFG_MAXTXBURSTSIZE(tx_maxburst); | |
1318 | ||
1319 | dwc3_writel(dwc->regs, DWC3_GTXTHRCFG, reg); | |
1320 | } | |
1321 | } | |
1322 | } | |
1323 | ||
941f918e FB |
1324 | /** |
1325 | * dwc3_core_init - Low-level initialization of DWC3 Core | |
1326 | * @dwc: Pointer to our controller context structure | |
1327 | * | |
1328 | * Returns 0 on success otherwise negative errno. | |
1329 | */ | |
1330 | static int dwc3_core_init(struct dwc3 *dwc) | |
1331 | { | |
9ba3aca8 | 1332 | unsigned int hw_mode; |
941f918e FB |
1333 | u32 reg; |
1334 | int ret; | |
1335 | ||
9ba3aca8 TN |
1336 | hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0); |
1337 | ||
941f918e FB |
1338 | /* |
1339 | * Write Linux Version Code to our GUID register so it's easy to figure | |
1340 | * out which kernel version a bug was found. | |
1341 | */ | |
1342 | dwc3_writel(dwc->regs, DWC3_GUID, LINUX_VERSION_CODE); | |
1343 | ||
98112041 | 1344 | ret = dwc3_phy_setup(dwc); |
941f918e | 1345 | if (ret) |
d2f19782 | 1346 | return ret; |
4878a028 | 1347 | |
98112041 RQ |
1348 | if (!dwc->ulpi_ready) { |
1349 | ret = dwc3_core_ulpi_init(dwc); | |
63130462 FT |
1350 | if (ret) { |
1351 | if (ret == -ETIMEDOUT) { | |
1352 | dwc3_core_soft_reset(dwc); | |
1353 | ret = -EPROBE_DEFER; | |
1354 | } | |
d2f19782 | 1355 | return ret; |
63130462 | 1356 | } |
98112041 RQ |
1357 | dwc->ulpi_ready = true; |
1358 | } | |
4878a028 | 1359 | |
98112041 RQ |
1360 | if (!dwc->phys_ready) { |
1361 | ret = dwc3_core_get_phy(dwc); | |
1362 | if (ret) | |
d2f19782 | 1363 | goto err_exit_ulpi; |
98112041 RQ |
1364 | dwc->phys_ready = true; |
1365 | } | |
1366 | ||
1d72fab4 JH |
1367 | ret = dwc3_phy_init(dwc); |
1368 | if (ret) | |
1369 | goto err_exit_ulpi; | |
8cfac9a6 | 1370 | |
98112041 | 1371 | ret = dwc3_core_soft_reset(dwc); |
f54edb53 | 1372 | if (ret) |
1d72fab4 | 1373 | goto err_exit_phy; |
f54edb53 | 1374 | |
941f918e | 1375 | dwc3_core_setup_global_control(dwc); |
c499ff71 | 1376 | dwc3_core_num_eps(dwc); |
0ffcaf37 | 1377 | |
3497b9a5 LJ |
1378 | /* Set power down scale of suspend_clk */ |
1379 | dwc3_set_power_down_clk_scale(dwc); | |
1380 | ||
c499ff71 FB |
1381 | /* Adjust Frame Length */ |
1382 | dwc3_frame_length_adjustment(dwc); | |
1383 | ||
7bee3188 BP |
1384 | /* Adjust Reference Clock Period */ |
1385 | dwc3_ref_clk_period(dwc); | |
1386 | ||
d9612c2f PM |
1387 | dwc3_set_incr_burst_type(dwc); |
1388 | ||
d504bfa6 RSP |
1389 | dwc3_config_soc_bus(dwc); |
1390 | ||
8018018d | 1391 | ret = dwc3_phy_power_on(dwc); |
1d72fab4 JH |
1392 | if (ret) |
1393 | goto err_exit_phy; | |
c499ff71 FB |
1394 | |
1395 | ret = dwc3_event_buffers_setup(dwc); | |
1396 | if (ret) { | |
1397 | dev_err(dwc->dev, "failed to setup event buffers\n"); | |
1d72fab4 | 1398 | goto err_power_off_phy; |
c499ff71 FB |
1399 | } |
1400 | ||
06281d46 JY |
1401 | /* |
1402 | * ENDXFER polling is available on version 3.10a and later of | |
1403 | * the DWC_usb3 controller. It is NOT available in the | |
1404 | * DWC_usb31 controller. | |
1405 | */ | |
9af21dd6 | 1406 | if (DWC3_VER_IS_WITHIN(DWC3, 310A, ANY)) { |
06281d46 JY |
1407 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL2); |
1408 | reg |= DWC3_GUCTL2_RST_ACTBITLATER; | |
1409 | dwc3_writel(dwc->regs, DWC3_GUCTL2, reg); | |
1410 | } | |
1411 | ||
9149c9b0 FH |
1412 | /* |
1413 | * STAR 9001285599: This issue affects DWC_usb3 version 3.20a | |
1414 | * only. If the PM TIMER ECM is enabled through GUCTL2[19], the | |
1415 | * link compliance test (TD7.21) may fail. If the ECN is not | |
1416 | * enabled (GUCTL2[19] = 0), the controller will use the old timer | |
1417 | * value (5us), which is still acceptable for the link compliance | |
1418 | * test. Therefore, do not enable PM TIMER ECM in 3.20a by | |
1419 | * setting GUCTL2[19] by default; instead, use GUCTL2[19] = 0. | |
1420 | */ | |
1421 | if (DWC3_VER_IS(DWC3, 320A)) { | |
1422 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL2); | |
1423 | reg &= ~DWC3_GUCTL2_LC_TIMER; | |
1424 | dwc3_writel(dwc->regs, DWC3_GUCTL2, reg); | |
1425 | } | |
1426 | ||
63d7f981 PM |
1427 | /* |
1428 | * When configured in HOST mode, after issuing U3/L2 exit controller | |
57d7a6b9 | 1429 | * fails to send proper CRC checksum in CRC5 field. Because of this |
63d7f981 PM |
1430 | * behaviour Transaction Error is generated, resulting in reset and |
1431 | * re-enumeration of usb device attached. All the termsel, xcvrsel, | |
1432 | * opmode becomes 0 during end of resume. Enabling bit 10 of GUCTL1 | |
1433 | * will correct this problem. This option is to support certain | |
1434 | * legacy ULPI PHYs. | |
1435 | */ | |
1436 | if (dwc->resume_hs_terminations) { | |
1437 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL1); | |
1438 | reg |= DWC3_GUCTL1_RESUME_OPMODE_HS_HOST; | |
1439 | dwc3_writel(dwc->regs, DWC3_GUCTL1, reg); | |
1440 | } | |
1441 | ||
9af21dd6 | 1442 | if (!DWC3_VER_IS_PRIOR(DWC3, 250A)) { |
0bb39ca1 | 1443 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL1); |
65db7a0c WW |
1444 | |
1445 | /* | |
1446 | * Enable hardware control of sending remote wakeup | |
1447 | * in HS when the device is in the L1 state. | |
1448 | */ | |
9af21dd6 | 1449 | if (!DWC3_VER_IS_PRIOR(DWC3, 290A)) |
65db7a0c WW |
1450 | reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW; |
1451 | ||
843714bb JP |
1452 | /* |
1453 | * Decouple USB 2.0 L1 & L2 events which will allow for | |
1454 | * gadget driver to only receive U3/L2 suspend & wakeup | |
1455 | * events and prevent the more frequent L1 LPM transitions | |
1456 | * from interrupting the driver. | |
1457 | */ | |
1458 | if (!DWC3_VER_IS_PRIOR(DWC3, 300A)) | |
1459 | reg |= DWC3_GUCTL1_DEV_DECOUPLE_L1L2_EVT; | |
1460 | ||
65db7a0c WW |
1461 | if (dwc->dis_tx_ipgap_linecheck_quirk) |
1462 | reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS; | |
1463 | ||
7ba6b09f NA |
1464 | if (dwc->parkmode_disable_ss_quirk) |
1465 | reg |= DWC3_GUCTL1_PARKMODE_DISABLE_SS; | |
1466 | ||
d21a797a SC |
1467 | if (dwc->parkmode_disable_hs_quirk) |
1468 | reg |= DWC3_GUCTL1_PARKMODE_DISABLE_HS; | |
1469 | ||
606c096a TN |
1470 | if (DWC3_VER_IS_WITHIN(DWC3, 290A, ANY)) { |
1471 | if (dwc->maximum_speed == USB_SPEED_FULL || | |
1472 | dwc->maximum_speed == USB_SPEED_HIGH) | |
1473 | reg |= DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK; | |
1474 | else | |
1475 | reg &= ~DWC3_GUCTL1_DEV_FORCE_20_CLK_FOR_30_CLK; | |
1476 | } | |
62b20e6e | 1477 | |
0bb39ca1 JY |
1478 | dwc3_writel(dwc->regs, DWC3_GUCTL1, reg); |
1479 | } | |
1480 | ||
e72fc8d6 | 1481 | dwc3_config_threshold(dwc); |
938a5ad1 | 1482 | |
91736d06 KK |
1483 | /* |
1484 | * Modify this for all supported Super Speed ports when | |
1485 | * multiport support is added. | |
1486 | */ | |
1487 | if (hw_mode != DWC3_GHWPARAMS0_MODE_GADGET && | |
1488 | (DWC3_IP_IS(DWC31)) && | |
1489 | dwc->maximum_speed == USB_SPEED_SUPER) { | |
ce25e2a8 KK |
1490 | int i; |
1491 | ||
1492 | for (i = 0; i < dwc->num_usb3_ports; i++) { | |
1493 | reg = dwc3_readl(dwc->regs, DWC3_LLUCTL(i)); | |
1494 | reg |= DWC3_LLUCTL_FORCE_GEN1; | |
1495 | dwc3_writel(dwc->regs, DWC3_LLUCTL(i), reg); | |
1496 | } | |
91736d06 KK |
1497 | } |
1498 | ||
04d5b4c2 FH |
1499 | /* |
1500 | * STAR 9001346572: This issue affects DWC_usb31 versions 1.80a and | |
1501 | * prior. When an active endpoint not currently cached in the host | |
1502 | * controller is chosen to be cached to the same index as an endpoint | |
1503 | * receiving NAKs, the endpoint receiving NAKs enters continuous | |
1504 | * retry mode. This prevents it from being evicted from the host | |
1505 | * controller cache, blocking the new endpoint from being cached and | |
1506 | * serviced. | |
1507 | * | |
1508 | * To resolve this, for controller versions 1.70a and 1.80a, set the | |
1509 | * GUCTL3 bit[16] (USB2.0 Internal Retry Disable) to 1. This bit | |
1510 | * disables the USB2.0 internal retry feature. The GUCTL3[16] register | |
1511 | * function is available only from version 1.70a. | |
1512 | */ | |
1513 | if (DWC3_VER_IS_WITHIN(DWC31, 170A, 180A)) { | |
1514 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL3); | |
1515 | reg |= DWC3_GUCTL3_USB20_RETRY_DISABLE; | |
1516 | dwc3_writel(dwc->regs, DWC3_GUCTL3, reg); | |
1517 | } | |
1518 | ||
72246da4 FB |
1519 | return 0; |
1520 | ||
1d72fab4 JH |
1521 | err_power_off_phy: |
1522 | dwc3_phy_power_off(dwc); | |
1523 | err_exit_phy: | |
1524 | dwc3_phy_exit(dwc); | |
d2f19782 | 1525 | err_exit_ulpi: |
98112041 RQ |
1526 | dwc3_ulpi_exit(dwc); |
1527 | ||
72246da4 FB |
1528 | return ret; |
1529 | } | |
1530 | ||
3c9f94ac | 1531 | static int dwc3_core_get_phy(struct dwc3 *dwc) |
72246da4 | 1532 | { |
3c9f94ac | 1533 | struct device *dev = dwc->dev; |
941ea361 | 1534 | struct device_node *node = dev->of_node; |
30a46746 | 1535 | char phy_name[9]; |
3c9f94ac | 1536 | int ret; |
3f12222a | 1537 | u8 i; |
72246da4 | 1538 | |
5088b6f5 KVA |
1539 | if (node) { |
1540 | dwc->usb2_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0); | |
1541 | dwc->usb3_phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 1); | |
bb674907 FB |
1542 | } else { |
1543 | dwc->usb2_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2); | |
1544 | dwc->usb3_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB3); | |
5088b6f5 KVA |
1545 | } |
1546 | ||
d105e7f8 FB |
1547 | if (IS_ERR(dwc->usb2_phy)) { |
1548 | ret = PTR_ERR(dwc->usb2_phy); | |
d090c7a2 | 1549 | if (ret == -ENXIO || ret == -ENODEV) |
122f06e6 | 1550 | dwc->usb2_phy = NULL; |
d090c7a2 | 1551 | else |
0c0a20f6 | 1552 | return dev_err_probe(dev, ret, "no usb2 phy configured\n"); |
51e1e7bc FB |
1553 | } |
1554 | ||
d105e7f8 | 1555 | if (IS_ERR(dwc->usb3_phy)) { |
315955d7 | 1556 | ret = PTR_ERR(dwc->usb3_phy); |
d090c7a2 | 1557 | if (ret == -ENXIO || ret == -ENODEV) |
122f06e6 | 1558 | dwc->usb3_phy = NULL; |
d090c7a2 | 1559 | else |
0c0a20f6 | 1560 | return dev_err_probe(dev, ret, "no usb3 phy configured\n"); |
51e1e7bc FB |
1561 | } |
1562 | ||
30a46746 KK |
1563 | for (i = 0; i < dwc->num_usb2_ports; i++) { |
1564 | if (dwc->num_usb2_ports == 1) | |
1565 | snprintf(phy_name, sizeof(phy_name), "usb2-phy"); | |
d090c7a2 | 1566 | else |
3f12222a | 1567 | snprintf(phy_name, sizeof(phy_name), "usb2-%u", i); |
30a46746 KK |
1568 | |
1569 | dwc->usb2_generic_phy[i] = devm_phy_get(dev, phy_name); | |
1570 | if (IS_ERR(dwc->usb2_generic_phy[i])) { | |
1571 | ret = PTR_ERR(dwc->usb2_generic_phy[i]); | |
1572 | if (ret == -ENOSYS || ret == -ENODEV) | |
1573 | dwc->usb2_generic_phy[i] = NULL; | |
1574 | else | |
1575 | return dev_err_probe(dev, ret, "failed to lookup phy %s\n", | |
1576 | phy_name); | |
1577 | } | |
57303488 KVA |
1578 | } |
1579 | ||
30a46746 KK |
1580 | for (i = 0; i < dwc->num_usb3_ports; i++) { |
1581 | if (dwc->num_usb3_ports == 1) | |
1582 | snprintf(phy_name, sizeof(phy_name), "usb3-phy"); | |
d090c7a2 | 1583 | else |
3f12222a | 1584 | snprintf(phy_name, sizeof(phy_name), "usb3-%u", i); |
30a46746 KK |
1585 | |
1586 | dwc->usb3_generic_phy[i] = devm_phy_get(dev, phy_name); | |
1587 | if (IS_ERR(dwc->usb3_generic_phy[i])) { | |
1588 | ret = PTR_ERR(dwc->usb3_generic_phy[i]); | |
1589 | if (ret == -ENOSYS || ret == -ENODEV) | |
1590 | dwc->usb3_generic_phy[i] = NULL; | |
1591 | else | |
1592 | return dev_err_probe(dev, ret, "failed to lookup phy %s\n", | |
1593 | phy_name); | |
1594 | } | |
57303488 KVA |
1595 | } |
1596 | ||
3c9f94ac FB |
1597 | return 0; |
1598 | } | |
1599 | ||
5f94adfe FB |
1600 | static int dwc3_core_init_mode(struct dwc3 *dwc) |
1601 | { | |
1602 | struct device *dev = dwc->dev; | |
1603 | int ret; | |
30a46746 | 1604 | int i; |
5f94adfe FB |
1605 | |
1606 | switch (dwc->dr_mode) { | |
1607 | case USB_DR_MODE_PERIPHERAL: | |
cc5bfc4e | 1608 | dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE, false); |
958d1a4c FB |
1609 | |
1610 | if (dwc->usb2_phy) | |
1611 | otg_set_vbus(dwc->usb2_phy->otg, false); | |
30a46746 KK |
1612 | phy_set_mode(dwc->usb2_generic_phy[0], PHY_MODE_USB_DEVICE); |
1613 | phy_set_mode(dwc->usb3_generic_phy[0], PHY_MODE_USB_DEVICE); | |
958d1a4c | 1614 | |
5f94adfe | 1615 | ret = dwc3_gadget_init(dwc); |
0c0a20f6 AS |
1616 | if (ret) |
1617 | return dev_err_probe(dev, ret, "failed to initialize gadget\n"); | |
5f94adfe FB |
1618 | break; |
1619 | case USB_DR_MODE_HOST: | |
cc5bfc4e | 1620 | dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST, false); |
958d1a4c FB |
1621 | |
1622 | if (dwc->usb2_phy) | |
1623 | otg_set_vbus(dwc->usb2_phy->otg, true); | |
30a46746 KK |
1624 | for (i = 0; i < dwc->num_usb2_ports; i++) |
1625 | phy_set_mode(dwc->usb2_generic_phy[i], PHY_MODE_USB_HOST); | |
1626 | for (i = 0; i < dwc->num_usb3_ports; i++) | |
1627 | phy_set_mode(dwc->usb3_generic_phy[i], PHY_MODE_USB_HOST); | |
958d1a4c | 1628 | |
5f94adfe | 1629 | ret = dwc3_host_init(dwc); |
0c0a20f6 AS |
1630 | if (ret) |
1631 | return dev_err_probe(dev, ret, "failed to initialize host\n"); | |
5f94adfe FB |
1632 | break; |
1633 | case USB_DR_MODE_OTG: | |
41ce1456 | 1634 | INIT_WORK(&dwc->drd_work, __dwc3_set_mode); |
9840354f | 1635 | ret = dwc3_drd_init(dwc); |
0c0a20f6 AS |
1636 | if (ret) |
1637 | return dev_err_probe(dev, ret, "failed to initialize dual-role\n"); | |
5f94adfe FB |
1638 | break; |
1639 | default: | |
1640 | dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode); | |
1641 | return -EINVAL; | |
1642 | } | |
1643 | ||
1644 | return 0; | |
1645 | } | |
1646 | ||
1647 | static void dwc3_core_exit_mode(struct dwc3 *dwc) | |
1648 | { | |
1649 | switch (dwc->dr_mode) { | |
1650 | case USB_DR_MODE_PERIPHERAL: | |
1651 | dwc3_gadget_exit(dwc); | |
1652 | break; | |
1653 | case USB_DR_MODE_HOST: | |
1654 | dwc3_host_exit(dwc); | |
1655 | break; | |
1656 | case USB_DR_MODE_OTG: | |
9840354f | 1657 | dwc3_drd_exit(dwc); |
5f94adfe FB |
1658 | break; |
1659 | default: | |
1660 | /* do nothing */ | |
1661 | break; | |
1662 | } | |
09ed259f BL |
1663 | |
1664 | /* de-assert DRVVBUS for HOST and OTG mode */ | |
cc5bfc4e | 1665 | dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE, true); |
5f94adfe FB |
1666 | } |
1667 | ||
d504bfa6 RSP |
1668 | static void dwc3_get_software_properties(struct dwc3 *dwc) |
1669 | { | |
1670 | struct device *tmpdev; | |
1671 | u16 gsbuscfg0_reqinfo; | |
1672 | int ret; | |
1673 | ||
1674 | dwc->gsbuscfg0_reqinfo = DWC3_GSBUSCFG0_REQINFO_UNSPECIFIED; | |
1675 | ||
1676 | /* | |
1677 | * Iterate over all parent nodes for finding swnode properties | |
1678 | * and non-DT (non-ABI) properties. | |
1679 | */ | |
1680 | for (tmpdev = dwc->dev; tmpdev; tmpdev = tmpdev->parent) { | |
1681 | ret = device_property_read_u16(tmpdev, | |
1682 | "snps,gsbuscfg0-reqinfo", | |
1683 | &gsbuscfg0_reqinfo); | |
1684 | if (!ret) | |
1685 | dwc->gsbuscfg0_reqinfo = gsbuscfg0_reqinfo; | |
1686 | } | |
1687 | } | |
1688 | ||
c5ac6116 | 1689 | static void dwc3_get_properties(struct dwc3 *dwc) |
3c9f94ac | 1690 | { |
c5ac6116 | 1691 | struct device *dev = dwc->dev; |
80caf7d2 | 1692 | u8 lpm_nyet_threshold; |
6b6a0c9a | 1693 | u8 tx_de_emphasis; |
460d098c | 1694 | u8 hird_threshold; |
e72fc8d6 SC |
1695 | u8 rx_thr_num_pkt = 0; |
1696 | u8 rx_max_burst = 0; | |
1697 | u8 tx_thr_num_pkt = 0; | |
1698 | u8 tx_max_burst = 0; | |
f28ad906 TN |
1699 | u8 rx_thr_num_pkt_prd = 0; |
1700 | u8 rx_max_burst_prd = 0; | |
1701 | u8 tx_thr_num_pkt_prd = 0; | |
1702 | u8 tx_max_burst_prd = 0; | |
9f607a30 | 1703 | u8 tx_fifo_resize_max_num; |
8da76444 | 1704 | u16 num_hc_interrupters; |
3c9f94ac | 1705 | |
80caf7d2 | 1706 | /* default to highest possible threshold */ |
8d791929 | 1707 | lpm_nyet_threshold = 0xf; |
80caf7d2 | 1708 | |
6b6a0c9a HR |
1709 | /* default to -3.5dB de-emphasis */ |
1710 | tx_de_emphasis = 1; | |
1711 | ||
460d098c HR |
1712 | /* |
1713 | * default to assert utmi_sleep_n and use maximum allowed HIRD | |
1714 | * threshold value of 0b1100 | |
1715 | */ | |
1716 | hird_threshold = 12; | |
1717 | ||
9f607a30 WC |
1718 | /* |
1719 | * default to a TXFIFO size large enough to fit 6 max packets. This | |
1720 | * allows for systems with larger bus latencies to have some headroom | |
1721 | * for endpoints that have a large bMaxBurst value. | |
1722 | */ | |
1723 | tx_fifo_resize_max_num = 6; | |
1724 | ||
8da76444 WC |
1725 | /* default to a single XHCI interrupter */ |
1726 | num_hc_interrupters = 1; | |
1727 | ||
63863b98 | 1728 | dwc->maximum_speed = usb_get_maximum_speed(dev); |
67848146 | 1729 | dwc->max_ssp_rate = usb_get_maximum_ssp_rate(dev); |
06e7114f | 1730 | dwc->dr_mode = usb_get_dr_mode(dev); |
32f2ed86 | 1731 | dwc->hsphy_mode = of_usb_get_phy_mode(dev->of_node); |
63863b98 | 1732 | |
d64ff406 AB |
1733 | dwc->sysdev_is_parent = device_property_read_bool(dev, |
1734 | "linux,sysdev_is_parent"); | |
1735 | if (dwc->sysdev_is_parent) | |
1736 | dwc->sysdev = dwc->dev->parent; | |
1737 | else | |
1738 | dwc->sysdev = dwc->dev; | |
1739 | ||
f9aa4113 TN |
1740 | dwc->sys_wakeup = device_may_wakeup(dwc->sysdev); |
1741 | ||
3d128919 | 1742 | dwc->has_lpm_erratum = device_property_read_bool(dev, |
80caf7d2 | 1743 | "snps,has-lpm-erratum"); |
3d128919 | 1744 | device_property_read_u8(dev, "snps,lpm-nyet-threshold", |
80caf7d2 | 1745 | &lpm_nyet_threshold); |
3d128919 | 1746 | dwc->is_utmi_l1_suspend = device_property_read_bool(dev, |
460d098c | 1747 | "snps,is-utmi-l1-suspend"); |
3d128919 | 1748 | device_property_read_u8(dev, "snps,hird-threshold", |
460d098c | 1749 | &hird_threshold); |
d92021f6 TN |
1750 | dwc->dis_start_transfer_quirk = device_property_read_bool(dev, |
1751 | "snps,dis-start-transfer-quirk"); | |
3d128919 | 1752 | dwc->usb3_lpm_capable = device_property_read_bool(dev, |
eac68e8f | 1753 | "snps,usb3_lpm_capable"); |
022a0208 TN |
1754 | dwc->usb2_lpm_disable = device_property_read_bool(dev, |
1755 | "snps,usb2-lpm-disable"); | |
475e8be5 TN |
1756 | dwc->usb2_gadget_lpm_disable = device_property_read_bool(dev, |
1757 | "snps,usb2-gadget-lpm-disable"); | |
e72fc8d6 SC |
1758 | device_property_read_u8(dev, "snps,rx-thr-num-pkt", |
1759 | &rx_thr_num_pkt); | |
1760 | device_property_read_u8(dev, "snps,rx-max-burst", | |
1761 | &rx_max_burst); | |
1762 | device_property_read_u8(dev, "snps,tx-thr-num-pkt", | |
1763 | &tx_thr_num_pkt); | |
1764 | device_property_read_u8(dev, "snps,tx-max-burst", | |
1765 | &tx_max_burst); | |
938a5ad1 TN |
1766 | device_property_read_u8(dev, "snps,rx-thr-num-pkt-prd", |
1767 | &rx_thr_num_pkt_prd); | |
1768 | device_property_read_u8(dev, "snps,rx-max-burst-prd", | |
1769 | &rx_max_burst_prd); | |
1770 | device_property_read_u8(dev, "snps,tx-thr-num-pkt-prd", | |
1771 | &tx_thr_num_pkt_prd); | |
1772 | device_property_read_u8(dev, "snps,tx-max-burst-prd", | |
1773 | &tx_max_burst_prd); | |
8da76444 WC |
1774 | device_property_read_u16(dev, "num-hc-interrupters", |
1775 | &num_hc_interrupters); | |
1776 | /* DWC3 core allowed to have a max of 8 interrupters */ | |
1777 | if (num_hc_interrupters > 8) | |
1778 | num_hc_interrupters = 8; | |
1779 | ||
9f607a30 WC |
1780 | dwc->do_fifo_resize = device_property_read_bool(dev, |
1781 | "tx-fifo-resize"); | |
1782 | if (dwc->do_fifo_resize) | |
1783 | device_property_read_u8(dev, "tx-fifo-max-num", | |
1784 | &tx_fifo_resize_max_num); | |
3c9f94ac | 1785 | |
3d128919 | 1786 | dwc->disable_scramble_quirk = device_property_read_bool(dev, |
3b81221a | 1787 | "snps,disable_scramble_quirk"); |
3d128919 | 1788 | dwc->u2exit_lfps_quirk = device_property_read_bool(dev, |
9a5b2f31 | 1789 | "snps,u2exit_lfps_quirk"); |
3d128919 | 1790 | dwc->u2ss_inp3_quirk = device_property_read_bool(dev, |
b5a65c40 | 1791 | "snps,u2ss_inp3_quirk"); |
3d128919 | 1792 | dwc->req_p1p2p3_quirk = device_property_read_bool(dev, |
df31f5b3 | 1793 | "snps,req_p1p2p3_quirk"); |
3d128919 | 1794 | dwc->del_p1p2p3_quirk = device_property_read_bool(dev, |
a2a1d0f5 | 1795 | "snps,del_p1p2p3_quirk"); |
3d128919 | 1796 | dwc->del_phy_power_chg_quirk = device_property_read_bool(dev, |
41c06ffd | 1797 | "snps,del_phy_power_chg_quirk"); |
3d128919 | 1798 | dwc->lfps_filter_quirk = device_property_read_bool(dev, |
fb67afca | 1799 | "snps,lfps_filter_quirk"); |
3d128919 | 1800 | dwc->rx_detect_poll_quirk = device_property_read_bool(dev, |
14f4ac53 | 1801 | "snps,rx_detect_poll_quirk"); |
3d128919 | 1802 | dwc->dis_u3_susphy_quirk = device_property_read_bool(dev, |
59acfa20 | 1803 | "snps,dis_u3_susphy_quirk"); |
3d128919 | 1804 | dwc->dis_u2_susphy_quirk = device_property_read_bool(dev, |
0effe0a3 | 1805 | "snps,dis_u2_susphy_quirk"); |
ec791d14 JY |
1806 | dwc->dis_enblslpm_quirk = device_property_read_bool(dev, |
1807 | "snps,dis_enblslpm_quirk"); | |
729dcffd AKV |
1808 | dwc->dis_u1_entry_quirk = device_property_read_bool(dev, |
1809 | "snps,dis-u1-entry-quirk"); | |
1810 | dwc->dis_u2_entry_quirk = device_property_read_bool(dev, | |
1811 | "snps,dis-u2-entry-quirk"); | |
e58dd357 RB |
1812 | dwc->dis_rxdet_inp3_quirk = device_property_read_bool(dev, |
1813 | "snps,dis_rxdet_inp3_quirk"); | |
16199f33 WW |
1814 | dwc->dis_u2_freeclk_exists_quirk = device_property_read_bool(dev, |
1815 | "snps,dis-u2-freeclk-exists-quirk"); | |
00fe081d WW |
1816 | dwc->dis_del_phy_power_chg_quirk = device_property_read_bool(dev, |
1817 | "snps,dis-del-phy-power-chg-quirk"); | |
65db7a0c WW |
1818 | dwc->dis_tx_ipgap_linecheck_quirk = device_property_read_bool(dev, |
1819 | "snps,dis-tx-ipgap-linecheck-quirk"); | |
63d7f981 PM |
1820 | dwc->resume_hs_terminations = device_property_read_bool(dev, |
1821 | "snps,resume-hs-terminations"); | |
b84ba26c PM |
1822 | dwc->ulpi_ext_vbus_drv = device_property_read_bool(dev, |
1823 | "snps,ulpi-ext-vbus-drv"); | |
7ba6b09f NA |
1824 | dwc->parkmode_disable_ss_quirk = device_property_read_bool(dev, |
1825 | "snps,parkmode-disable-ss-quirk"); | |
d21a797a SC |
1826 | dwc->parkmode_disable_hs_quirk = device_property_read_bool(dev, |
1827 | "snps,parkmode-disable-hs-quirk"); | |
a6fc2f1b AS |
1828 | dwc->gfladj_refclk_lpm_sel = device_property_read_bool(dev, |
1829 | "snps,gfladj-refclk-lpm-sel-quirk"); | |
6b6a0c9a | 1830 | |
3d128919 | 1831 | dwc->tx_de_emphasis_quirk = device_property_read_bool(dev, |
6b6a0c9a | 1832 | "snps,tx_de_emphasis_quirk"); |
3d128919 | 1833 | device_property_read_u8(dev, "snps,tx_de_emphasis", |
6b6a0c9a | 1834 | &tx_de_emphasis); |
3d128919 HK |
1835 | device_property_read_string(dev, "snps,hsphy_interface", |
1836 | &dwc->hsphy_interface); | |
1837 | device_property_read_u32(dev, "snps,quirk-frame-length-adjustment", | |
bcdb3272 | 1838 | &dwc->fladj); |
7bee3188 BP |
1839 | device_property_read_u32(dev, "snps,ref-clock-period-ns", |
1840 | &dwc->ref_clk_per); | |
3d128919 | 1841 | |
42bf02ec RQ |
1842 | dwc->dis_metastability_quirk = device_property_read_bool(dev, |
1843 | "snps,dis_metastability_quirk"); | |
1844 | ||
f580170f YC |
1845 | dwc->dis_split_quirk = device_property_read_bool(dev, |
1846 | "snps,dis-split-quirk"); | |
1847 | ||
80caf7d2 | 1848 | dwc->lpm_nyet_threshold = lpm_nyet_threshold; |
6b6a0c9a | 1849 | dwc->tx_de_emphasis = tx_de_emphasis; |
80caf7d2 | 1850 | |
16fe4f30 | 1851 | dwc->hird_threshold = hird_threshold; |
460d098c | 1852 | |
e72fc8d6 SC |
1853 | dwc->rx_thr_num_pkt = rx_thr_num_pkt; |
1854 | dwc->rx_max_burst = rx_max_burst; | |
1855 | ||
1856 | dwc->tx_thr_num_pkt = tx_thr_num_pkt; | |
1857 | dwc->tx_max_burst = tx_max_burst; | |
1858 | ||
938a5ad1 TN |
1859 | dwc->rx_thr_num_pkt_prd = rx_thr_num_pkt_prd; |
1860 | dwc->rx_max_burst_prd = rx_max_burst_prd; | |
1861 | ||
1862 | dwc->tx_thr_num_pkt_prd = tx_thr_num_pkt_prd; | |
1863 | dwc->tx_max_burst_prd = tx_max_burst_prd; | |
1864 | ||
9f607a30 | 1865 | dwc->tx_fifo_resize_max_num = tx_fifo_resize_max_num; |
8da76444 WC |
1866 | |
1867 | dwc->num_hc_interrupters = num_hc_interrupters; | |
cf40b86b JY |
1868 | } |
1869 | ||
1870 | /* check whether the core supports IMOD */ | |
1871 | bool dwc3_has_imod(struct dwc3 *dwc) | |
1872 | { | |
9af21dd6 TN |
1873 | return DWC3_VER_IS_WITHIN(DWC3, 300A, ANY) || |
1874 | DWC3_VER_IS_WITHIN(DWC31, 120A, ANY) || | |
1875 | DWC3_IP_IS(DWC32); | |
c5ac6116 FB |
1876 | } |
1877 | ||
7ac51a12 JY |
1878 | static void dwc3_check_params(struct dwc3 *dwc) |
1879 | { | |
1880 | struct device *dev = dwc->dev; | |
b574ce3e TN |
1881 | unsigned int hwparam_gen = |
1882 | DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3); | |
7ac51a12 | 1883 | |
28632b44 | 1884 | /* |
69c58dee BJS |
1885 | * Enable IMOD for all supporting controllers. |
1886 | * | |
1887 | * Particularly, DWC_usb3 v3.00a must enable this feature for | |
1888 | * the following reason: | |
1889 | * | |
28632b44 JY |
1890 | * Workaround for STAR 9000961433 which affects only version |
1891 | * 3.00a of the DWC_usb3 core. This prevents the controller | |
1892 | * interrupt from being masked while handling events. IMOD | |
1893 | * allows us to work around this issue. Enable it for the | |
1894 | * affected version. | |
1895 | */ | |
69c58dee | 1896 | if (dwc3_has_imod((dwc))) |
28632b44 JY |
1897 | dwc->imod_interval = 1; |
1898 | ||
7ac51a12 JY |
1899 | /* Check the maximum_speed parameter */ |
1900 | switch (dwc->maximum_speed) { | |
7ac51a12 JY |
1901 | case USB_SPEED_FULL: |
1902 | case USB_SPEED_HIGH: | |
e518bdd9 | 1903 | break; |
7ac51a12 | 1904 | case USB_SPEED_SUPER: |
e518bdd9 TN |
1905 | if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) |
1906 | dev_warn(dev, "UDC doesn't support Gen 1\n"); | |
1907 | break; | |
7ac51a12 | 1908 | case USB_SPEED_SUPER_PLUS: |
e518bdd9 TN |
1909 | if ((DWC3_IP_IS(DWC32) && |
1910 | hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_DIS) || | |
1911 | (!DWC3_IP_IS(DWC32) && | |
1912 | hwparam_gen != DWC3_GHWPARAMS3_SSPHY_IFC_GEN2)) | |
1913 | dev_warn(dev, "UDC doesn't support SSP\n"); | |
7ac51a12 JY |
1914 | break; |
1915 | default: | |
1916 | dev_err(dev, "invalid maximum_speed parameter %d\n", | |
1917 | dwc->maximum_speed); | |
df561f66 | 1918 | fallthrough; |
7ac51a12 | 1919 | case USB_SPEED_UNKNOWN: |
b574ce3e TN |
1920 | switch (hwparam_gen) { |
1921 | case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2: | |
7ac51a12 | 1922 | dwc->maximum_speed = USB_SPEED_SUPER_PLUS; |
b574ce3e TN |
1923 | break; |
1924 | case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1: | |
1925 | if (DWC3_IP_IS(DWC32)) | |
1926 | dwc->maximum_speed = USB_SPEED_SUPER_PLUS; | |
1927 | else | |
1928 | dwc->maximum_speed = USB_SPEED_SUPER; | |
1929 | break; | |
1930 | case DWC3_GHWPARAMS3_SSPHY_IFC_DIS: | |
1931 | dwc->maximum_speed = USB_SPEED_HIGH; | |
1932 | break; | |
1933 | default: | |
1934 | dwc->maximum_speed = USB_SPEED_SUPER; | |
1935 | break; | |
1936 | } | |
7ac51a12 JY |
1937 | break; |
1938 | } | |
67848146 TN |
1939 | |
1940 | /* | |
1941 | * Currently the controller does not have visibility into the HW | |
1942 | * parameter to determine the maximum number of lanes the HW supports. | |
1943 | * If the number of lanes is not specified in the device property, then | |
1944 | * set the default to support dual-lane for DWC_usb32 and single-lane | |
1945 | * for DWC_usb31 for super-speed-plus. | |
1946 | */ | |
1947 | if (dwc->maximum_speed == USB_SPEED_SUPER_PLUS) { | |
1948 | switch (dwc->max_ssp_rate) { | |
1949 | case USB_SSP_GEN_2x1: | |
1950 | if (hwparam_gen == DWC3_GHWPARAMS3_SSPHY_IFC_GEN1) | |
1951 | dev_warn(dev, "UDC only supports Gen 1\n"); | |
1952 | break; | |
1953 | case USB_SSP_GEN_1x2: | |
1954 | case USB_SSP_GEN_2x2: | |
1955 | if (DWC3_IP_IS(DWC31)) | |
1956 | dev_warn(dev, "UDC only supports single lane\n"); | |
1957 | break; | |
1958 | case USB_SSP_GEN_UNKNOWN: | |
1959 | default: | |
1960 | switch (hwparam_gen) { | |
1961 | case DWC3_GHWPARAMS3_SSPHY_IFC_GEN2: | |
1962 | if (DWC3_IP_IS(DWC32)) | |
1963 | dwc->max_ssp_rate = USB_SSP_GEN_2x2; | |
1964 | else | |
1965 | dwc->max_ssp_rate = USB_SSP_GEN_2x1; | |
1966 | break; | |
1967 | case DWC3_GHWPARAMS3_SSPHY_IFC_GEN1: | |
1968 | if (DWC3_IP_IS(DWC32)) | |
1969 | dwc->max_ssp_rate = USB_SSP_GEN_1x2; | |
1970 | break; | |
1971 | } | |
1972 | break; | |
1973 | } | |
1974 | } | |
7ac51a12 JY |
1975 | } |
1976 | ||
d182c2e1 AS |
1977 | static struct extcon_dev *dwc3_get_extcon(struct dwc3 *dwc) |
1978 | { | |
1979 | struct device *dev = dwc->dev; | |
1980 | struct device_node *np_phy; | |
1981 | struct extcon_dev *edev = NULL; | |
1982 | const char *name; | |
1983 | ||
6ff78df5 | 1984 | if (device_property_present(dev, "extcon")) |
d182c2e1 AS |
1985 | return extcon_get_edev_by_phandle(dev, 0); |
1986 | ||
1987 | /* | |
1988 | * Device tree platforms should get extcon via phandle. | |
1989 | * On ACPI platforms, we get the name from a device property. | |
1990 | * This device property is for kernel internal use only and | |
1991 | * is expected to be set by the glue code. | |
1992 | */ | |
1993 | if (device_property_read_string(dev, "linux,extcon-name", &name) == 0) | |
1994 | return extcon_get_extcon_dev(name); | |
1995 | ||
d68cc25b JG |
1996 | /* |
1997 | * Check explicitly if "usb-role-switch" is used since | |
1998 | * extcon_find_edev_by_node() can not be used to check the absence of | |
1999 | * an extcon device. In the absence of an device it will always return | |
2000 | * EPROBE_DEFER. | |
2001 | */ | |
2002 | if (IS_ENABLED(CONFIG_USB_ROLE_SWITCH) && | |
2003 | device_property_read_bool(dev, "usb-role-switch")) | |
2004 | return NULL; | |
2005 | ||
d182c2e1 AS |
2006 | /* |
2007 | * Try to get an extcon device from the USB PHY controller's "port" | |
2008 | * node. Check if it has the "port" node first, to avoid printing the | |
2009 | * error message from underlying code, as it's a valid case: extcon | |
2010 | * device (and "port" node) may be missing in case of "usb-role-switch" | |
2011 | * or OTG mode. | |
2012 | */ | |
2013 | np_phy = of_parse_phandle(dev->of_node, "phys", 0); | |
2014 | if (of_graph_is_present(np_phy)) { | |
2015 | struct device_node *np_conn; | |
2016 | ||
2017 | np_conn = of_graph_get_remote_node(np_phy, -1, -1); | |
2018 | if (np_conn) | |
2019 | edev = extcon_find_edev_by_node(np_conn); | |
2020 | of_node_put(np_conn); | |
2021 | } | |
2022 | of_node_put(np_phy); | |
2023 | ||
2024 | return edev; | |
2025 | } | |
2026 | ||
bd828574 JH |
2027 | static int dwc3_get_clocks(struct dwc3 *dwc) |
2028 | { | |
2029 | struct device *dev = dwc->dev; | |
2030 | ||
2031 | if (!dev->of_node) | |
2032 | return 0; | |
2033 | ||
2034 | /* | |
2035 | * Clocks are optional, but new DT platforms should support all clocks | |
2036 | * as required by the DT-binding. | |
2037 | * Some devices have different clock names in legacy device trees, | |
2038 | * check for them to retain backwards compatibility. | |
2039 | */ | |
2040 | dwc->bus_clk = devm_clk_get_optional(dev, "bus_early"); | |
2041 | if (IS_ERR(dwc->bus_clk)) { | |
2042 | return dev_err_probe(dev, PTR_ERR(dwc->bus_clk), | |
2043 | "could not get bus clock\n"); | |
2044 | } | |
2045 | ||
2046 | if (dwc->bus_clk == NULL) { | |
2047 | dwc->bus_clk = devm_clk_get_optional(dev, "bus_clk"); | |
2048 | if (IS_ERR(dwc->bus_clk)) { | |
2049 | return dev_err_probe(dev, PTR_ERR(dwc->bus_clk), | |
2050 | "could not get bus clock\n"); | |
2051 | } | |
2052 | } | |
2053 | ||
2054 | dwc->ref_clk = devm_clk_get_optional(dev, "ref"); | |
2055 | if (IS_ERR(dwc->ref_clk)) { | |
2056 | return dev_err_probe(dev, PTR_ERR(dwc->ref_clk), | |
2057 | "could not get ref clock\n"); | |
2058 | } | |
2059 | ||
2060 | if (dwc->ref_clk == NULL) { | |
2061 | dwc->ref_clk = devm_clk_get_optional(dev, "ref_clk"); | |
2062 | if (IS_ERR(dwc->ref_clk)) { | |
2063 | return dev_err_probe(dev, PTR_ERR(dwc->ref_clk), | |
2064 | "could not get ref clock\n"); | |
2065 | } | |
2066 | } | |
2067 | ||
2068 | dwc->susp_clk = devm_clk_get_optional(dev, "suspend"); | |
2069 | if (IS_ERR(dwc->susp_clk)) { | |
2070 | return dev_err_probe(dev, PTR_ERR(dwc->susp_clk), | |
2071 | "could not get suspend clock\n"); | |
2072 | } | |
2073 | ||
2074 | if (dwc->susp_clk == NULL) { | |
2075 | dwc->susp_clk = devm_clk_get_optional(dev, "suspend_clk"); | |
2076 | if (IS_ERR(dwc->susp_clk)) { | |
2077 | return dev_err_probe(dev, PTR_ERR(dwc->susp_clk), | |
2078 | "could not get suspend clock\n"); | |
2079 | } | |
2080 | } | |
2081 | ||
97789b93 SR |
2082 | /* specific to Rockchip RK3588 */ |
2083 | dwc->utmi_clk = devm_clk_get_optional(dev, "utmi"); | |
2084 | if (IS_ERR(dwc->utmi_clk)) { | |
2085 | return dev_err_probe(dev, PTR_ERR(dwc->utmi_clk), | |
2086 | "could not get utmi clock\n"); | |
2087 | } | |
2088 | ||
2089 | /* specific to Rockchip RK3588 */ | |
2090 | dwc->pipe_clk = devm_clk_get_optional(dev, "pipe"); | |
2091 | if (IS_ERR(dwc->pipe_clk)) { | |
2092 | return dev_err_probe(dev, PTR_ERR(dwc->pipe_clk), | |
2093 | "could not get pipe clock\n"); | |
2094 | } | |
2095 | ||
bd828574 JH |
2096 | return 0; |
2097 | } | |
2098 | ||
921e109c KK |
2099 | static int dwc3_get_num_ports(struct dwc3 *dwc) |
2100 | { | |
2101 | void __iomem *base; | |
2102 | u8 major_revision; | |
2103 | u32 offset; | |
2104 | u32 val; | |
2105 | ||
2106 | /* | |
2107 | * Remap xHCI address space to access XHCI ext cap regs since it is | |
2108 | * needed to get information on number of ports present. | |
2109 | */ | |
2110 | base = ioremap(dwc->xhci_resources[0].start, | |
2111 | resource_size(&dwc->xhci_resources[0])); | |
2112 | if (!base) | |
2113 | return -ENOMEM; | |
2114 | ||
2115 | offset = 0; | |
2116 | do { | |
2117 | offset = xhci_find_next_ext_cap(base, offset, | |
2118 | XHCI_EXT_CAPS_PROTOCOL); | |
2119 | if (!offset) | |
2120 | break; | |
2121 | ||
2122 | val = readl(base + offset); | |
2123 | major_revision = XHCI_EXT_PORT_MAJOR(val); | |
2124 | ||
2125 | val = readl(base + offset + 0x08); | |
2126 | if (major_revision == 0x03) { | |
2127 | dwc->num_usb3_ports += XHCI_EXT_PORT_COUNT(val); | |
2128 | } else if (major_revision <= 0x02) { | |
2129 | dwc->num_usb2_ports += XHCI_EXT_PORT_COUNT(val); | |
2130 | } else { | |
2131 | dev_warn(dwc->dev, "unrecognized port major revision %d\n", | |
2132 | major_revision); | |
2133 | } | |
2134 | } while (1); | |
2135 | ||
2136 | dev_dbg(dwc->dev, "hs-ports: %u ss-ports: %u\n", | |
2137 | dwc->num_usb2_ports, dwc->num_usb3_ports); | |
2138 | ||
2139 | iounmap(base); | |
2140 | ||
30a46746 KK |
2141 | if (dwc->num_usb2_ports > DWC3_USB2_MAX_PORTS || |
2142 | dwc->num_usb3_ports > DWC3_USB3_MAX_PORTS) | |
2143 | return -EINVAL; | |
2144 | ||
921e109c KK |
2145 | return 0; |
2146 | } | |
2147 | ||
66e0ea34 KT |
2148 | static struct power_supply *dwc3_get_usb_power_supply(struct dwc3 *dwc) |
2149 | { | |
2150 | struct power_supply *usb_psy; | |
2151 | const char *usb_psy_name; | |
2152 | int ret; | |
2153 | ||
2154 | ret = device_property_read_string(dwc->dev, "usb-psy-name", &usb_psy_name); | |
2155 | if (ret < 0) | |
2156 | return NULL; | |
2157 | ||
2158 | usb_psy = power_supply_get_by_name(usb_psy_name); | |
2159 | if (!usb_psy) | |
2160 | return ERR_PTR(-EPROBE_DEFER); | |
2161 | ||
2162 | return usb_psy; | |
2163 | } | |
2164 | ||
613a2e65 | 2165 | int dwc3_core_probe(const struct dwc3_probe_data *data) |
c5ac6116 | 2166 | { |
613a2e65 BA |
2167 | struct dwc3 *dwc = data->dwc; |
2168 | struct device *dev = dwc->dev; | |
2169 | struct resource dwc_res; | |
921e109c | 2170 | unsigned int hw_mode; |
9a4d7dd1 | 2171 | void __iomem *regs; |
613a2e65 | 2172 | struct resource *res = data->res; |
c5ac6116 FB |
2173 | int ret; |
2174 | ||
c5ac6116 FB |
2175 | dwc->xhci_resources[0].start = res->start; |
2176 | dwc->xhci_resources[0].end = dwc->xhci_resources[0].start + | |
2177 | DWC3_XHCI_REGS_END; | |
2178 | dwc->xhci_resources[0].flags = res->flags; | |
2179 | dwc->xhci_resources[0].name = res->name; | |
2180 | ||
c5ac6116 FB |
2181 | /* |
2182 | * Request memory region but exclude xHCI regs, | |
2183 | * since it will be requested by the xhci-plat driver. | |
2184 | */ | |
44feb8e6 MY |
2185 | dwc_res = *res; |
2186 | dwc_res.start += DWC3_GLOBALS_REGS_START; | |
2187 | ||
ec5eb438 SC |
2188 | if (dev->of_node) { |
2189 | struct device_node *parent = of_get_parent(dev->of_node); | |
2190 | ||
2191 | if (of_device_is_compatible(parent, "realtek,rtd-dwc3")) { | |
2192 | dwc_res.start -= DWC3_GLOBALS_REGS_START; | |
2193 | dwc_res.start += DWC3_RTK_RTD_GLOBALS_REGS_START; | |
2194 | } | |
2195 | ||
2196 | of_node_put(parent); | |
2197 | } | |
2198 | ||
44feb8e6 MY |
2199 | regs = devm_ioremap_resource(dev, &dwc_res); |
2200 | if (IS_ERR(regs)) | |
2201 | return PTR_ERR(regs); | |
c5ac6116 FB |
2202 | |
2203 | dwc->regs = regs; | |
44feb8e6 | 2204 | dwc->regs_size = resource_size(&dwc_res); |
c5ac6116 FB |
2205 | |
2206 | dwc3_get_properties(dwc); | |
2207 | ||
d504bfa6 RSP |
2208 | dwc3_get_software_properties(dwc); |
2209 | ||
66e0ea34 KT |
2210 | dwc->usb_psy = dwc3_get_usb_power_supply(dwc); |
2211 | if (IS_ERR(dwc->usb_psy)) | |
2212 | return dev_err_probe(dev, PTR_ERR(dwc->usb_psy), "couldn't get usb power supply\n"); | |
2213 | ||
170940f7 BA |
2214 | if (!data->ignore_clocks_and_resets) { |
2215 | dwc->reset = devm_reset_control_array_get_optional_shared(dev); | |
2216 | if (IS_ERR(dwc->reset)) { | |
2217 | ret = PTR_ERR(dwc->reset); | |
2218 | goto err_put_psy; | |
2219 | } | |
fe8abf33 | 2220 | |
170940f7 BA |
2221 | ret = dwc3_get_clocks(dwc); |
2222 | if (ret) | |
2223 | goto err_put_psy; | |
2224 | } | |
fe8abf33 MY |
2225 | |
2226 | ret = reset_control_deassert(dwc->reset); | |
2227 | if (ret) | |
fe296046 | 2228 | goto err_put_psy; |
fe8abf33 | 2229 | |
33fb697e | 2230 | ret = dwc3_clk_enable(dwc); |
fe8abf33 | 2231 | if (ret) |
fe296046 | 2232 | goto err_assert_reset; |
fe8abf33 | 2233 | |
dc1b5d9a EBS |
2234 | if (!dwc3_core_is_valid(dwc)) { |
2235 | dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n"); | |
2236 | ret = -ENODEV; | |
fe296046 | 2237 | goto err_disable_clks; |
dc1b5d9a EBS |
2238 | } |
2239 | ||
613a2e65 | 2240 | dev_set_drvdata(dev, dwc); |
2917e718 | 2241 | dwc3_cache_hwparams(dwc); |
6c89cce0 | 2242 | |
91062e66 WW |
2243 | if (!dwc->sysdev_is_parent && |
2244 | DWC3_GHWPARAMS0_AWIDTH(dwc->hwparams.hwparams0) == 64) { | |
2245 | ret = dma_set_mask_and_coherent(dwc->sysdev, DMA_BIT_MASK(64)); | |
2246 | if (ret) | |
fe296046 | 2247 | goto err_disable_clks; |
91062e66 WW |
2248 | } |
2249 | ||
921e109c KK |
2250 | /* |
2251 | * Currently only DWC3 controllers that are host-only capable | |
2252 | * can have more than one port. | |
2253 | */ | |
2254 | hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0); | |
2255 | if (hw_mode == DWC3_GHWPARAMS0_MODE_HOST) { | |
2256 | ret = dwc3_get_num_ports(dwc); | |
2257 | if (ret) | |
2258 | goto err_disable_clks; | |
2259 | } else { | |
2260 | dwc->num_usb2_ports = 1; | |
2261 | dwc->num_usb3_ports = 1; | |
2262 | } | |
2263 | ||
72246da4 | 2264 | spin_lock_init(&dwc->lock); |
f88359e1 | 2265 | mutex_init(&dwc->mutex); |
72246da4 | 2266 | |
9a8ad10c | 2267 | pm_runtime_get_noresume(dev); |
fc8bb91b FB |
2268 | pm_runtime_set_active(dev); |
2269 | pm_runtime_use_autosuspend(dev); | |
2270 | pm_runtime_set_autosuspend_delay(dev, DWC3_DEFAULT_AUTOSUSPEND_DELAY); | |
802ca850 | 2271 | pm_runtime_enable(dev); |
32808237 | 2272 | |
802ca850 | 2273 | pm_runtime_forbid(dev); |
72246da4 | 2274 | |
3921426b FB |
2275 | ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE); |
2276 | if (ret) { | |
2277 | dev_err(dwc->dev, "failed to allocate event buffers\n"); | |
2278 | ret = -ENOMEM; | |
fe296046 | 2279 | goto err_allow_rpm; |
3921426b FB |
2280 | } |
2281 | ||
d182c2e1 AS |
2282 | dwc->edev = dwc3_get_extcon(dwc); |
2283 | if (IS_ERR(dwc->edev)) { | |
2284 | ret = dev_err_probe(dwc->dev, PTR_ERR(dwc->edev), "failed to get extcon\n"); | |
fe296046 | 2285 | goto err_free_event_buffers; |
d182c2e1 AS |
2286 | } |
2287 | ||
9d6173e1 TN |
2288 | ret = dwc3_get_dr_mode(dwc); |
2289 | if (ret) | |
fe296046 | 2290 | goto err_free_event_buffers; |
32a4a135 | 2291 | |
72246da4 FB |
2292 | ret = dwc3_core_init(dwc); |
2293 | if (ret) { | |
0c0a20f6 | 2294 | dev_err_probe(dev, ret, "failed to initialize core\n"); |
fe296046 | 2295 | goto err_free_event_buffers; |
72246da4 FB |
2296 | } |
2297 | ||
7ac51a12 | 2298 | dwc3_check_params(dwc); |
84524d12 | 2299 | dwc3_debugfs_init(dwc); |
2c7f1bd9 | 2300 | |
5f94adfe FB |
2301 | ret = dwc3_core_init_mode(dwc); |
2302 | if (ret) | |
fe296046 | 2303 | goto err_exit_debugfs; |
72246da4 | 2304 | |
fc8bb91b | 2305 | pm_runtime_put(dev); |
72246da4 | 2306 | |
8bbae288 RR |
2307 | dma_set_max_seg_size(dev, UINT_MAX); |
2308 | ||
72246da4 FB |
2309 | return 0; |
2310 | ||
fe296046 | 2311 | err_exit_debugfs: |
84524d12 | 2312 | dwc3_debugfs_exit(dwc); |
c499ff71 | 2313 | dwc3_event_buffers_cleanup(dwc); |
1d72fab4 JH |
2314 | dwc3_phy_power_off(dwc); |
2315 | dwc3_phy_exit(dwc); | |
08fd9a82 | 2316 | dwc3_ulpi_exit(dwc); |
fe296046 | 2317 | err_free_event_buffers: |
3921426b | 2318 | dwc3_free_event_buffers(dwc); |
fe296046 | 2319 | err_allow_rpm: |
9a8ad10c JH |
2320 | pm_runtime_allow(dev); |
2321 | pm_runtime_disable(dev); | |
6b3b2402 | 2322 | pm_runtime_dont_use_autosuspend(dev); |
9a8ad10c JH |
2323 | pm_runtime_set_suspended(dev); |
2324 | pm_runtime_put_noidle(dev); | |
fe296046 | 2325 | err_disable_clks: |
33fb697e | 2326 | dwc3_clk_disable(dwc); |
fe296046 | 2327 | err_assert_reset: |
fe8abf33 | 2328 | reset_control_assert(dwc->reset); |
fe296046 | 2329 | err_put_psy: |
b0bf77cd | 2330 | if (dwc->usb_psy) |
6f0764b5 RC |
2331 | power_supply_put(dwc->usb_psy); |
2332 | ||
72246da4 FB |
2333 | return ret; |
2334 | } | |
613a2e65 | 2335 | EXPORT_SYMBOL_GPL(dwc3_core_probe); |
72246da4 | 2336 | |
613a2e65 | 2337 | static int dwc3_probe(struct platform_device *pdev) |
72246da4 | 2338 | { |
170940f7 | 2339 | struct dwc3_probe_data probe_data = {}; |
613a2e65 BA |
2340 | struct resource *res; |
2341 | struct dwc3 *dwc; | |
3da1f6ee | 2342 | |
613a2e65 BA |
2343 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
2344 | if (!res) { | |
2345 | dev_err(&pdev->dev, "missing memory resource\n"); | |
2346 | return -ENODEV; | |
2347 | } | |
2348 | ||
2349 | dwc = devm_kzalloc(&pdev->dev, sizeof(*dwc), GFP_KERNEL); | |
2350 | if (!dwc) | |
2351 | return -ENOMEM; | |
2352 | ||
2353 | dwc->dev = &pdev->dev; | |
2354 | ||
2355 | probe_data.dwc = dwc; | |
2356 | probe_data.res = res; | |
2357 | ||
2358 | return dwc3_core_probe(&probe_data); | |
2359 | } | |
2360 | ||
2361 | void dwc3_core_remove(struct dwc3 *dwc) | |
2362 | { | |
2363 | pm_runtime_get_sync(dwc->dev); | |
72246da4 | 2364 | |
dc99f16f | 2365 | dwc3_core_exit_mode(dwc); |
2a042767 | 2366 | dwc3_debugfs_exit(dwc); |
8ba007a9 | 2367 | |
72246da4 | 2368 | dwc3_core_exit(dwc); |
88bc9d19 | 2369 | dwc3_ulpi_exit(dwc); |
72246da4 | 2370 | |
613a2e65 BA |
2371 | pm_runtime_allow(dwc->dev); |
2372 | pm_runtime_disable(dwc->dev); | |
2373 | pm_runtime_dont_use_autosuspend(dwc->dev); | |
2374 | pm_runtime_put_noidle(dwc->dev); | |
e3dbb657 JH |
2375 | /* |
2376 | * HACK: Clear the driver data, which is currently accessed by parent | |
2377 | * glue drivers, before allowing the parent to suspend. | |
2378 | */ | |
613a2e65 BA |
2379 | dev_set_drvdata(dwc->dev, NULL); |
2380 | pm_runtime_set_suspended(dwc->dev); | |
72246da4 | 2381 | |
fc8bb91b | 2382 | dwc3_free_event_buffers(dwc); |
fc8bb91b | 2383 | |
b0bf77cd | 2384 | if (dwc->usb_psy) |
6f0764b5 | 2385 | power_supply_put(dwc->usb_psy); |
72246da4 | 2386 | } |
613a2e65 BA |
2387 | EXPORT_SYMBOL_GPL(dwc3_core_remove); |
2388 | ||
2389 | static void dwc3_remove(struct platform_device *pdev) | |
2390 | { | |
2391 | dwc3_core_remove(platform_get_drvdata(pdev)); | |
2392 | } | |
72246da4 | 2393 | |
fc8bb91b | 2394 | #ifdef CONFIG_PM |
fe8abf33 MY |
2395 | static int dwc3_core_init_for_resume(struct dwc3 *dwc) |
2396 | { | |
2397 | int ret; | |
2398 | ||
2399 | ret = reset_control_deassert(dwc->reset); | |
2400 | if (ret) | |
2401 | return ret; | |
2402 | ||
33fb697e | 2403 | ret = dwc3_clk_enable(dwc); |
fe8abf33 MY |
2404 | if (ret) |
2405 | goto assert_reset; | |
2406 | ||
fe8abf33 MY |
2407 | ret = dwc3_core_init(dwc); |
2408 | if (ret) | |
2409 | goto disable_clks; | |
2410 | ||
2411 | return 0; | |
2412 | ||
2413 | disable_clks: | |
33fb697e | 2414 | dwc3_clk_disable(dwc); |
fe8abf33 MY |
2415 | assert_reset: |
2416 | reset_control_assert(dwc->reset); | |
2417 | ||
2418 | return ret; | |
2419 | } | |
2420 | ||
c4a5153e | 2421 | static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg) |
7415f17c | 2422 | { |
bcb12877 | 2423 | u32 reg; |
30a46746 | 2424 | int i; |
7415f17c | 2425 | |
9cfb31e4 RQ |
2426 | if (!pm_runtime_suspended(dwc->dev) && !PMSG_IS_AUTO(msg)) { |
2427 | dwc->susphy_state = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) & | |
2428 | DWC3_GUSB2PHYCFG_SUSPHY) || | |
2429 | (dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)) & | |
2430 | DWC3_GUSB3PIPECTL_SUSPHY); | |
2431 | /* | |
2432 | * TI AM62 platform requires SUSPHY to be | |
2433 | * enabled for system suspend to work. | |
2434 | */ | |
2435 | if (!dwc->susphy_state) | |
2436 | dwc3_enable_susphy(dwc, true); | |
2437 | } | |
705e3ce3 | 2438 | |
689bf72c MG |
2439 | switch (dwc->current_dr_role) { |
2440 | case DWC3_GCTL_PRTCAP_DEVICE: | |
0227cc84 LJ |
2441 | if (pm_runtime_suspended(dwc->dev)) |
2442 | break; | |
7415f17c | 2443 | dwc3_gadget_suspend(dwc); |
41a91c60 | 2444 | synchronize_irq(dwc->irq_gadget); |
689bf72c | 2445 | dwc3_core_exit(dwc); |
51f5d49a | 2446 | break; |
689bf72c | 2447 | case DWC3_GCTL_PRTCAP_HOST: |
e3fafbd8 | 2448 | if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) { |
c4a5153e | 2449 | dwc3_core_exit(dwc); |
bcb12877 MG |
2450 | break; |
2451 | } | |
2452 | ||
2453 | /* Let controller to suspend HSPHY before PHY driver suspends */ | |
2454 | if (dwc->dis_u2_susphy_quirk || | |
2455 | dwc->dis_enblslpm_quirk) { | |
30a46746 KK |
2456 | for (i = 0; i < dwc->num_usb2_ports; i++) { |
2457 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i)); | |
2458 | reg |= DWC3_GUSB2PHYCFG_ENBLSLPM | | |
2459 | DWC3_GUSB2PHYCFG_SUSPHY; | |
2460 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg); | |
2461 | } | |
bcb12877 MG |
2462 | |
2463 | /* Give some time for USB2 PHY to suspend */ | |
2464 | usleep_range(5000, 6000); | |
2465 | } | |
2466 | ||
30a46746 KK |
2467 | for (i = 0; i < dwc->num_usb2_ports; i++) |
2468 | phy_pm_runtime_put_sync(dwc->usb2_generic_phy[i]); | |
2469 | for (i = 0; i < dwc->num_usb3_ports; i++) | |
2470 | phy_pm_runtime_put_sync(dwc->usb3_generic_phy[i]); | |
c4a5153e | 2471 | break; |
f09cc79b RQ |
2472 | case DWC3_GCTL_PRTCAP_OTG: |
2473 | /* do nothing during runtime_suspend */ | |
2474 | if (PMSG_IS_AUTO(msg)) | |
2475 | break; | |
2476 | ||
2477 | if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) { | |
f09cc79b | 2478 | dwc3_gadget_suspend(dwc); |
41a91c60 | 2479 | synchronize_irq(dwc->irq_gadget); |
f09cc79b RQ |
2480 | } |
2481 | ||
2482 | dwc3_otg_exit(dwc); | |
2483 | dwc3_core_exit(dwc); | |
2484 | break; | |
7415f17c | 2485 | default: |
51f5d49a | 2486 | /* do nothing */ |
7415f17c FB |
2487 | break; |
2488 | } | |
2489 | ||
7415f17c FB |
2490 | return 0; |
2491 | } | |
2492 | ||
c4a5153e | 2493 | static int dwc3_resume_common(struct dwc3 *dwc, pm_message_t msg) |
7415f17c | 2494 | { |
57303488 | 2495 | int ret; |
bcb12877 | 2496 | u32 reg; |
30a46746 | 2497 | int i; |
7415f17c | 2498 | |
689bf72c MG |
2499 | switch (dwc->current_dr_role) { |
2500 | case DWC3_GCTL_PRTCAP_DEVICE: | |
fe8abf33 | 2501 | ret = dwc3_core_init_for_resume(dwc); |
689bf72c MG |
2502 | if (ret) |
2503 | return ret; | |
5c4ad318 | 2504 | |
cc5bfc4e | 2505 | dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE, true); |
7415f17c | 2506 | dwc3_gadget_resume(dwc); |
689bf72c MG |
2507 | break; |
2508 | case DWC3_GCTL_PRTCAP_HOST: | |
e3fafbd8 | 2509 | if (!PMSG_IS_AUTO(msg) && !device_may_wakeup(dwc->dev)) { |
fe8abf33 | 2510 | ret = dwc3_core_init_for_resume(dwc); |
c4a5153e MG |
2511 | if (ret) |
2512 | return ret; | |
cc5bfc4e | 2513 | dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST, true); |
bcb12877 | 2514 | break; |
c4a5153e | 2515 | } |
bcb12877 | 2516 | /* Restore GUSB2PHYCFG bits that were modified in suspend */ |
30a46746 KK |
2517 | for (i = 0; i < dwc->num_usb2_ports; i++) { |
2518 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i)); | |
2519 | if (dwc->dis_u2_susphy_quirk) | |
2520 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; | |
bcb12877 | 2521 | |
30a46746 KK |
2522 | if (dwc->dis_enblslpm_quirk) |
2523 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; | |
bcb12877 | 2524 | |
30a46746 KK |
2525 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg); |
2526 | } | |
bcb12877 | 2527 | |
30a46746 KK |
2528 | for (i = 0; i < dwc->num_usb2_ports; i++) |
2529 | phy_pm_runtime_get_sync(dwc->usb2_generic_phy[i]); | |
2530 | for (i = 0; i < dwc->num_usb3_ports; i++) | |
2531 | phy_pm_runtime_get_sync(dwc->usb3_generic_phy[i]); | |
f09cc79b RQ |
2532 | break; |
2533 | case DWC3_GCTL_PRTCAP_OTG: | |
2534 | /* nothing to do on runtime_resume */ | |
2535 | if (PMSG_IS_AUTO(msg)) | |
2536 | break; | |
2537 | ||
0e5a3c82 | 2538 | ret = dwc3_core_init_for_resume(dwc); |
f09cc79b RQ |
2539 | if (ret) |
2540 | return ret; | |
2541 | ||
cc5bfc4e | 2542 | dwc3_set_prtcap(dwc, dwc->current_dr_role, true); |
f09cc79b RQ |
2543 | |
2544 | dwc3_otg_init(dwc); | |
2545 | if (dwc->current_otg_role == DWC3_OTG_ROLE_HOST) { | |
2546 | dwc3_otg_host_init(dwc); | |
2547 | } else if (dwc->current_otg_role == DWC3_OTG_ROLE_DEVICE) { | |
f09cc79b | 2548 | dwc3_gadget_resume(dwc); |
c4a5153e | 2549 | } |
f09cc79b | 2550 | |
c4a5153e | 2551 | break; |
7415f17c FB |
2552 | default: |
2553 | /* do nothing */ | |
2554 | break; | |
2555 | } | |
2556 | ||
705e3ce3 RQ |
2557 | if (!PMSG_IS_AUTO(msg)) { |
2558 | /* restore SUSPHY state to that before system suspend. */ | |
2559 | dwc3_enable_susphy(dwc, dwc->susphy_state); | |
2560 | } | |
2561 | ||
fc8bb91b FB |
2562 | return 0; |
2563 | } | |
2564 | ||
2565 | static int dwc3_runtime_checks(struct dwc3 *dwc) | |
2566 | { | |
689bf72c | 2567 | switch (dwc->current_dr_role) { |
c4a5153e | 2568 | case DWC3_GCTL_PRTCAP_DEVICE: |
fc8bb91b FB |
2569 | if (dwc->connected) |
2570 | return -EBUSY; | |
2571 | break; | |
c4a5153e | 2572 | case DWC3_GCTL_PRTCAP_HOST: |
fc8bb91b FB |
2573 | default: |
2574 | /* do nothing */ | |
2575 | break; | |
2576 | } | |
2577 | ||
2578 | return 0; | |
2579 | } | |
2580 | ||
613a2e65 | 2581 | int dwc3_runtime_suspend(struct dwc3 *dwc) |
fc8bb91b | 2582 | { |
fc8bb91b FB |
2583 | int ret; |
2584 | ||
2585 | if (dwc3_runtime_checks(dwc)) | |
2586 | return -EBUSY; | |
2587 | ||
c4a5153e | 2588 | ret = dwc3_suspend_common(dwc, PMSG_AUTO_SUSPEND); |
fc8bb91b FB |
2589 | if (ret) |
2590 | return ret; | |
2591 | ||
fc8bb91b FB |
2592 | return 0; |
2593 | } | |
613a2e65 | 2594 | EXPORT_SYMBOL_GPL(dwc3_runtime_suspend); |
fc8bb91b | 2595 | |
613a2e65 | 2596 | int dwc3_runtime_resume(struct dwc3 *dwc) |
fc8bb91b | 2597 | { |
613a2e65 | 2598 | struct device *dev = dwc->dev; |
fc8bb91b FB |
2599 | int ret; |
2600 | ||
c4a5153e | 2601 | ret = dwc3_resume_common(dwc, PMSG_AUTO_RESUME); |
fc8bb91b FB |
2602 | if (ret) |
2603 | return ret; | |
2604 | ||
689bf72c MG |
2605 | switch (dwc->current_dr_role) { |
2606 | case DWC3_GCTL_PRTCAP_DEVICE: | |
0d410e89 | 2607 | if (dwc->pending_events) { |
613a2e65 | 2608 | pm_runtime_put(dev); |
0d410e89 SG |
2609 | dwc->pending_events = false; |
2610 | enable_irq(dwc->irq_gadget); | |
2611 | } | |
fc8bb91b | 2612 | break; |
689bf72c | 2613 | case DWC3_GCTL_PRTCAP_HOST: |
fc8bb91b FB |
2614 | default: |
2615 | /* do nothing */ | |
2616 | break; | |
2617 | } | |
2618 | ||
2619 | pm_runtime_mark_last_busy(dev); | |
2620 | ||
2621 | return 0; | |
2622 | } | |
613a2e65 | 2623 | EXPORT_SYMBOL_GPL(dwc3_runtime_resume); |
fc8bb91b | 2624 | |
613a2e65 | 2625 | int dwc3_runtime_idle(struct dwc3 *dwc) |
fc8bb91b | 2626 | { |
613a2e65 | 2627 | struct device *dev = dwc->dev; |
fc8bb91b | 2628 | |
689bf72c MG |
2629 | switch (dwc->current_dr_role) { |
2630 | case DWC3_GCTL_PRTCAP_DEVICE: | |
fc8bb91b FB |
2631 | if (dwc3_runtime_checks(dwc)) |
2632 | return -EBUSY; | |
2633 | break; | |
689bf72c | 2634 | case DWC3_GCTL_PRTCAP_HOST: |
fc8bb91b FB |
2635 | default: |
2636 | /* do nothing */ | |
2637 | break; | |
2638 | } | |
2639 | ||
2640 | pm_runtime_mark_last_busy(dev); | |
2641 | pm_runtime_autosuspend(dev); | |
2642 | ||
2643 | return 0; | |
2644 | } | |
613a2e65 BA |
2645 | EXPORT_SYMBOL_GPL(dwc3_runtime_idle); |
2646 | ||
2647 | static int dwc3_plat_runtime_suspend(struct device *dev) | |
2648 | { | |
2649 | return dwc3_runtime_suspend(dev_get_drvdata(dev)); | |
2650 | } | |
2651 | ||
2652 | static int dwc3_plat_runtime_resume(struct device *dev) | |
2653 | { | |
2654 | return dwc3_runtime_resume(dev_get_drvdata(dev)); | |
2655 | } | |
2656 | ||
2657 | static int dwc3_plat_runtime_idle(struct device *dev) | |
2658 | { | |
2659 | return dwc3_runtime_idle(dev_get_drvdata(dev)); | |
2660 | } | |
fc8bb91b FB |
2661 | #endif /* CONFIG_PM */ |
2662 | ||
2663 | #ifdef CONFIG_PM_SLEEP | |
613a2e65 | 2664 | int dwc3_pm_suspend(struct dwc3 *dwc) |
fc8bb91b | 2665 | { |
613a2e65 | 2666 | struct device *dev = dwc->dev; |
fc8bb91b FB |
2667 | int ret; |
2668 | ||
c4a5153e | 2669 | ret = dwc3_suspend_common(dwc, PMSG_SUSPEND); |
fc8bb91b FB |
2670 | if (ret) |
2671 | return ret; | |
2672 | ||
2673 | pinctrl_pm_select_sleep_state(dev); | |
2674 | ||
2675 | return 0; | |
2676 | } | |
613a2e65 | 2677 | EXPORT_SYMBOL_GPL(dwc3_pm_suspend); |
fc8bb91b | 2678 | |
613a2e65 | 2679 | int dwc3_pm_resume(struct dwc3 *dwc) |
fc8bb91b | 2680 | { |
613a2e65 | 2681 | struct device *dev = dwc->dev; |
897e13a8 | 2682 | int ret = 0; |
fc8bb91b FB |
2683 | |
2684 | pinctrl_pm_select_default_state(dev); | |
2685 | ||
68c26fe5 | 2686 | pm_runtime_disable(dev); |
e3a9bd24 RC |
2687 | ret = pm_runtime_set_active(dev); |
2688 | if (ret) | |
2689 | goto out; | |
68c26fe5 | 2690 | |
c4a5153e | 2691 | ret = dwc3_resume_common(dwc, PMSG_RESUME); |
897e13a8 | 2692 | if (ret) |
68c26fe5 | 2693 | pm_runtime_set_suspended(dev); |
fc8bb91b | 2694 | |
e3a9bd24 | 2695 | out: |
7415f17c FB |
2696 | pm_runtime_enable(dev); |
2697 | ||
897e13a8 | 2698 | return ret; |
7415f17c | 2699 | } |
613a2e65 | 2700 | EXPORT_SYMBOL_GPL(dwc3_pm_resume); |
f580170f | 2701 | |
613a2e65 | 2702 | void dwc3_pm_complete(struct dwc3 *dwc) |
f580170f | 2703 | { |
f580170f YC |
2704 | u32 reg; |
2705 | ||
2706 | if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_HOST && | |
2707 | dwc->dis_split_quirk) { | |
2708 | reg = dwc3_readl(dwc->regs, DWC3_GUCTL3); | |
2709 | reg |= DWC3_GUCTL3_SPLITDISABLE; | |
2710 | dwc3_writel(dwc->regs, DWC3_GUCTL3, reg); | |
2711 | } | |
2712 | } | |
613a2e65 | 2713 | EXPORT_SYMBOL_GPL(dwc3_pm_complete); |
850e6340 | 2714 | |
613a2e65 | 2715 | int dwc3_pm_prepare(struct dwc3 *dwc) |
850e6340 | 2716 | { |
613a2e65 | 2717 | struct device *dev = dwc->dev; |
850e6340 RL |
2718 | |
2719 | /* | |
2720 | * Indicate to the PM core that it may safely leave the device in | |
2721 | * runtime suspend if runtime-suspended already in device mode. | |
2722 | */ | |
2723 | if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_DEVICE && | |
2724 | pm_runtime_suspended(dev) && | |
2725 | !dev_pinctrl(dev)) | |
2726 | return 1; | |
2727 | ||
2728 | return 0; | |
2729 | } | |
613a2e65 BA |
2730 | EXPORT_SYMBOL_GPL(dwc3_pm_prepare); |
2731 | ||
2732 | static int dwc3_plat_suspend(struct device *dev) | |
2733 | { | |
2734 | return dwc3_pm_suspend(dev_get_drvdata(dev)); | |
2735 | } | |
2736 | ||
2737 | static int dwc3_plat_resume(struct device *dev) | |
2738 | { | |
2739 | return dwc3_pm_resume(dev_get_drvdata(dev)); | |
2740 | } | |
2741 | ||
2742 | static void dwc3_plat_complete(struct device *dev) | |
2743 | { | |
2744 | dwc3_pm_complete(dev_get_drvdata(dev)); | |
2745 | } | |
2746 | ||
2747 | static int dwc3_plat_prepare(struct device *dev) | |
2748 | { | |
2749 | return dwc3_pm_prepare(dev_get_drvdata(dev)); | |
2750 | } | |
f580170f | 2751 | #else |
613a2e65 BA |
2752 | #define dwc3_plat_complete NULL |
2753 | #define dwc3_plat_prepare NULL | |
7f370ed0 | 2754 | #endif /* CONFIG_PM_SLEEP */ |
7415f17c FB |
2755 | |
2756 | static const struct dev_pm_ops dwc3_dev_pm_ops = { | |
613a2e65 BA |
2757 | SET_SYSTEM_SLEEP_PM_OPS(dwc3_plat_suspend, dwc3_plat_resume) |
2758 | .complete = dwc3_plat_complete, | |
2759 | .prepare = dwc3_plat_prepare, | |
0d410e89 SG |
2760 | /* |
2761 | * Runtime suspend halts the controller on disconnection. It relies on | |
2762 | * platforms with custom connection notification to start the controller | |
2763 | * again. | |
2764 | */ | |
613a2e65 BA |
2765 | SET_RUNTIME_PM_OPS(dwc3_plat_runtime_suspend, dwc3_plat_runtime_resume, |
2766 | dwc3_plat_runtime_idle) | |
7415f17c FB |
2767 | }; |
2768 | ||
5088b6f5 KVA |
2769 | #ifdef CONFIG_OF |
2770 | static const struct of_device_id of_dwc3_match[] = { | |
22a5aa17 FB |
2771 | { |
2772 | .compatible = "snps,dwc3" | |
2773 | }, | |
5088b6f5 KVA |
2774 | { |
2775 | .compatible = "synopsys,dwc3" | |
2776 | }, | |
2777 | { }, | |
2778 | }; | |
2779 | MODULE_DEVICE_TABLE(of, of_dwc3_match); | |
2780 | #endif | |
2781 | ||
404905a6 HK |
2782 | #ifdef CONFIG_ACPI |
2783 | ||
2784 | #define ACPI_ID_INTEL_BSW "808622B7" | |
2785 | ||
2786 | static const struct acpi_device_id dwc3_acpi_match[] = { | |
2787 | { ACPI_ID_INTEL_BSW, 0 }, | |
2788 | { }, | |
2789 | }; | |
2790 | MODULE_DEVICE_TABLE(acpi, dwc3_acpi_match); | |
2791 | #endif | |
2792 | ||
72246da4 FB |
2793 | static struct platform_driver dwc3_driver = { |
2794 | .probe = dwc3_probe, | |
9a0749d6 | 2795 | .remove = dwc3_remove, |
72246da4 FB |
2796 | .driver = { |
2797 | .name = "dwc3", | |
5088b6f5 | 2798 | .of_match_table = of_match_ptr(of_dwc3_match), |
404905a6 | 2799 | .acpi_match_table = ACPI_PTR(dwc3_acpi_match), |
7f370ed0 | 2800 | .pm = &dwc3_dev_pm_ops, |
72246da4 | 2801 | }, |
72246da4 FB |
2802 | }; |
2803 | ||
b1116dcc TK |
2804 | module_platform_driver(dwc3_driver); |
2805 | ||
7ae4fc4d | 2806 | MODULE_ALIAS("platform:dwc3"); |
72246da4 | 2807 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); |
5945f789 | 2808 | MODULE_LICENSE("GPL v2"); |
72246da4 | 2809 | MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver"); |