1 // SPDX-License-Identifier: GPL-2.0
3 * drd.c - DesignWare USB3 DRD Controller Dual-role support
5 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Roger Quadros <rogerq@ti.com>
10 #include <linux/extcon.h>
11 #include <linux/platform_device.h>
17 static void dwc3_otg_disable_events(struct dwc3
*dwc
, u32 disable_mask
)
19 u32 reg
= dwc3_readl(dwc
->regs
, DWC3_OEVTEN
);
21 reg
&= ~(disable_mask
);
22 dwc3_writel(dwc
->regs
, DWC3_OEVTEN
, reg
);
25 static void dwc3_otg_enable_events(struct dwc3
*dwc
, u32 enable_mask
)
27 u32 reg
= dwc3_readl(dwc
->regs
, DWC3_OEVTEN
);
30 dwc3_writel(dwc
->regs
, DWC3_OEVTEN
, reg
);
33 static void dwc3_otg_clear_events(struct dwc3
*dwc
)
35 u32 reg
= dwc3_readl(dwc
->regs
, DWC3_OEVT
);
37 dwc3_writel(dwc
->regs
, DWC3_OEVTEN
, reg
);
40 #define DWC3_OTG_ALL_EVENTS (DWC3_OEVTEN_XHCIRUNSTPSETEN | \
41 DWC3_OEVTEN_DEVRUNSTPSETEN | DWC3_OEVTEN_HIBENTRYEN | \
42 DWC3_OEVTEN_CONIDSTSCHNGEN | DWC3_OEVTEN_HRRCONFNOTIFEN | \
43 DWC3_OEVTEN_HRRINITNOTIFEN | DWC3_OEVTEN_ADEVIDLEEN | \
44 DWC3_OEVTEN_ADEVBHOSTENDEN | DWC3_OEVTEN_ADEVHOSTEN | \
45 DWC3_OEVTEN_ADEVHNPCHNGEN | DWC3_OEVTEN_ADEVSRPDETEN | \
46 DWC3_OEVTEN_ADEVSESSENDDETEN | DWC3_OEVTEN_BDEVBHOSTENDEN | \
47 DWC3_OEVTEN_BDEVHNPCHNGEN | DWC3_OEVTEN_BDEVSESSVLDDETEN | \
48 DWC3_OEVTEN_BDEVVBUSCHNGEN)
50 static irqreturn_t
dwc3_otg_thread_irq(int irq
, void *_dwc
)
52 struct dwc3
*dwc
= _dwc
;
54 spin_lock(&dwc
->lock
);
55 if (dwc
->otg_restart_host
) {
56 dwc3_otg_host_init(dwc
);
57 dwc
->otg_restart_host
= 0;
60 spin_unlock(&dwc
->lock
);
62 dwc3_set_mode(dwc
, DWC3_GCTL_PRTCAP_OTG
);
67 static irqreturn_t
dwc3_otg_irq(int irq
, void *_dwc
)
70 struct dwc3
*dwc
= _dwc
;
71 irqreturn_t ret
= IRQ_NONE
;
73 reg
= dwc3_readl(dwc
->regs
, DWC3_OEVT
);
75 /* ignore non OTG events, we can't disable them in OEVTEN */
76 if (!(reg
& DWC3_OTG_ALL_EVENTS
)) {
77 dwc3_writel(dwc
->regs
, DWC3_OEVT
, reg
);
81 if (dwc
->current_otg_role
== DWC3_OTG_ROLE_HOST
&&
82 !(reg
& DWC3_OEVT_DEVICEMODE
))
83 dwc
->otg_restart_host
= 1;
84 dwc3_writel(dwc
->regs
, DWC3_OEVT
, reg
);
85 ret
= IRQ_WAKE_THREAD
;
91 static void dwc3_otgregs_init(struct dwc3
*dwc
)
96 * Prevent host/device reset from resetting OTG core.
97 * If we don't do this then xhci_reset (USBCMD.HCRST) will reset
98 * the signal outputs sent to the PHY, the OTG FSM logic of the
99 * core and also the resets to the VBUS filters inside the core.
101 reg
= dwc3_readl(dwc
->regs
, DWC3_OCFG
);
102 reg
|= DWC3_OCFG_SFTRSTMASK
;
103 dwc3_writel(dwc
->regs
, DWC3_OCFG
, reg
);
105 /* Disable hibernation for simplicity */
106 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
107 reg
&= ~DWC3_GCTL_GBLHIBERNATIONEN
;
108 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
111 * Initialize OTG registers as per
112 * Figure 11-4 OTG Driver Overall Programming Flow
114 /* OCFG.SRPCap = 0, OCFG.HNPCap = 0 */
115 reg
= dwc3_readl(dwc
->regs
, DWC3_OCFG
);
116 reg
&= ~(DWC3_OCFG_SRPCAP
| DWC3_OCFG_HNPCAP
);
117 dwc3_writel(dwc
->regs
, DWC3_OCFG
, reg
);
119 dwc3_otg_clear_events(dwc
);
121 dwc3_otg_disable_events(dwc
, DWC3_OTG_ALL_EVENTS
);
122 /* OEVTEN.ConIDStsChngEn = 1. Instead we enable all events */
123 dwc3_otg_enable_events(dwc
, DWC3_OTG_ALL_EVENTS
);
125 * OCTL.PeriMode = 1, OCTL.DevSetHNPEn = 0, OCTL.HstSetHNPEn = 0,
128 reg
= dwc3_readl(dwc
->regs
, DWC3_OCTL
);
129 reg
|= DWC3_OCTL_PERIMODE
;
130 reg
&= ~(DWC3_OCTL_DEVSETHNPEN
| DWC3_OCTL_HSTSETHNPEN
|
132 dwc3_writel(dwc
->regs
, DWC3_OCTL
, reg
);
135 static int dwc3_otg_get_irq(struct dwc3
*dwc
)
137 struct platform_device
*dwc3_pdev
= to_platform_device(dwc
->dev
);
140 irq
= platform_get_irq_byname(dwc3_pdev
, "otg");
144 if (irq
== -EPROBE_DEFER
)
147 irq
= platform_get_irq_byname(dwc3_pdev
, "dwc_usb3");
151 if (irq
== -EPROBE_DEFER
)
154 irq
= platform_get_irq(dwc3_pdev
, 0);
158 if (irq
!= -EPROBE_DEFER
)
159 dev_err(dwc
->dev
, "missing OTG IRQ\n");
168 void dwc3_otg_init(struct dwc3
*dwc
)
173 * As per Figure 11-4 OTG Driver Overall Programming Flow,
174 * block "Initialize GCTL for OTG operation".
176 /* GCTL.PrtCapDir=2'b11 */
177 dwc3_set_prtcap(dwc
, DWC3_GCTL_PRTCAP_OTG
);
178 /* GUSB2PHYCFG0.SusPHY=0 */
179 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
180 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
181 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
183 /* Initialize OTG registers */
184 dwc3_otgregs_init(dwc
);
187 void dwc3_otg_exit(struct dwc3
*dwc
)
189 /* disable all OTG IRQs */
190 dwc3_otg_disable_events(dwc
, DWC3_OTG_ALL_EVENTS
);
191 /* clear all events */
192 dwc3_otg_clear_events(dwc
);
195 /* should be called before Host controller driver is started */
196 void dwc3_otg_host_init(struct dwc3
*dwc
)
200 /* As per Figure 11-10 A-Device Flow Diagram */
201 /* OCFG.HNPCap = 0, OCFG.SRPCap = 0. Already 0 */
204 * OCTL.PeriMode=0, OCTL.TermSelDLPulse = 0,
205 * OCTL.DevSetHNPEn = 0, OCTL.HstSetHNPEn = 0
207 reg
= dwc3_readl(dwc
->regs
, DWC3_OCTL
);
208 reg
&= ~(DWC3_OCTL_PERIMODE
| DWC3_OCTL_TERMSELIDPULSE
|
209 DWC3_OCTL_DEVSETHNPEN
| DWC3_OCTL_HSTSETHNPEN
);
210 dwc3_writel(dwc
->regs
, DWC3_OCTL
, reg
);
213 * OCFG.DisPrtPwrCutoff = 0/1
215 reg
= dwc3_readl(dwc
->regs
, DWC3_OCFG
);
216 reg
&= ~DWC3_OCFG_DISPWRCUTTOFF
;
217 dwc3_writel(dwc
->regs
, DWC3_OCFG
, reg
);
220 * OCFG.SRPCap = 1, OCFG.HNPCap = GHWPARAMS6.HNP_CAP
221 * We don't want SRP/HNP for simple dual-role so leave
226 * OEVTEN.OTGADevHostEvntEn = 1
227 * OEVTEN.OTGADevSessEndDetEvntEn = 1
228 * We don't want HNP/role-swap so leave these disabled.
231 /* GUSB2PHYCFG.ULPIAutoRes = 1/0, GUSB2PHYCFG.SusPHY = 1 */
232 if (!dwc
->dis_u2_susphy_quirk
) {
233 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
234 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
235 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
238 /* Set Port Power to enable VBUS: OCTL.PrtPwrCtl = 1 */
239 reg
= dwc3_readl(dwc
->regs
, DWC3_OCTL
);
240 reg
|= DWC3_OCTL_PRTPWRCTL
;
241 dwc3_writel(dwc
->regs
, DWC3_OCTL
, reg
);
244 /* should be called after Host controller driver is stopped */
245 static void dwc3_otg_host_exit(struct dwc3
*dwc
)
250 * Exit from A-device flow as per
251 * Figure 11-4 OTG Driver Overall Programming Flow
255 * OEVTEN.OTGADevBHostEndEvntEn=0, OEVTEN.OTGADevHNPChngEvntEn=0
256 * OEVTEN.OTGADevSessEndDetEvntEn=0,
257 * OEVTEN.OTGADevHostEvntEn = 0
258 * But we don't disable any OTG events
261 /* OCTL.HstSetHNPEn = 0, OCTL.PrtPwrCtl=0 */
262 reg
= dwc3_readl(dwc
->regs
, DWC3_OCTL
);
263 reg
&= ~(DWC3_OCTL_HSTSETHNPEN
| DWC3_OCTL_PRTPWRCTL
);
264 dwc3_writel(dwc
->regs
, DWC3_OCTL
, reg
);
267 /* should be called before the gadget controller driver is started */
268 static void dwc3_otg_device_init(struct dwc3
*dwc
)
272 /* As per Figure 11-20 B-Device Flow Diagram */
275 * OCFG.HNPCap = GHWPARAMS6.HNP_CAP, OCFG.SRPCap = 1
276 * but we keep them 0 for simple dual-role operation.
278 reg
= dwc3_readl(dwc
->regs
, DWC3_OCFG
);
279 /* OCFG.OTGSftRstMsk = 0/1 */
280 reg
|= DWC3_OCFG_SFTRSTMASK
;
281 dwc3_writel(dwc
->regs
, DWC3_OCFG
, reg
);
284 * OCTL.TermSelDLPulse = 0/1, OCTL.HNPReq = 0
285 * OCTL.DevSetHNPEn = 0, OCTL.HstSetHNPEn = 0
287 reg
= dwc3_readl(dwc
->regs
, DWC3_OCTL
);
288 reg
|= DWC3_OCTL_PERIMODE
;
289 reg
&= ~(DWC3_OCTL_TERMSELIDPULSE
| DWC3_OCTL_HNPREQ
|
290 DWC3_OCTL_DEVSETHNPEN
| DWC3_OCTL_HSTSETHNPEN
);
291 dwc3_writel(dwc
->regs
, DWC3_OCTL
, reg
);
292 /* OEVTEN.OTGBDevSesVldDetEvntEn = 1 */
293 dwc3_otg_enable_events(dwc
, DWC3_OEVTEN_BDEVSESSVLDDETEN
);
294 /* GUSB2PHYCFG.ULPIAutoRes = 0, GUSB2PHYCFG0.SusPHY = 1 */
295 if (!dwc
->dis_u2_susphy_quirk
) {
296 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
297 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
298 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
300 /* GCTL.GblHibernationEn = 0. Already 0. */
303 /* should be called after the gadget controller driver is stopped */
304 static void dwc3_otg_device_exit(struct dwc3
*dwc
)
309 * Exit from B-device flow as per
310 * Figure 11-4 OTG Driver Overall Programming Flow
314 * OEVTEN.OTGBDevHNPChngEvntEn = 0
315 * OEVTEN.OTGBDevVBusChngEvntEn = 0
316 * OEVTEN.OTGBDevBHostEndEvntEn = 0
318 dwc3_otg_disable_events(dwc
, DWC3_OEVTEN_BDEVHNPCHNGEN
|
319 DWC3_OEVTEN_BDEVVBUSCHNGEN
|
320 DWC3_OEVTEN_BDEVBHOSTENDEN
);
322 /* OCTL.DevSetHNPEn = 0, OCTL.HNPReq = 0, OCTL.PeriMode=1 */
323 reg
= dwc3_readl(dwc
->regs
, DWC3_OCTL
);
324 reg
&= ~(DWC3_OCTL_DEVSETHNPEN
| DWC3_OCTL_HNPREQ
);
325 reg
|= DWC3_OCTL_PERIMODE
;
326 dwc3_writel(dwc
->regs
, DWC3_OCTL
, reg
);
329 void dwc3_otg_update(struct dwc3
*dwc
, bool ignore_idstatus
)
336 if (dwc
->dr_mode
!= USB_DR_MODE_OTG
)
339 /* don't do anything if debug user changed role to not OTG */
340 if (dwc
->current_dr_role
!= DWC3_GCTL_PRTCAP_OTG
)
343 if (!ignore_idstatus
) {
344 reg
= dwc3_readl(dwc
->regs
, DWC3_OSTS
);
345 id
= !!(reg
& DWC3_OSTS_CONIDSTS
);
347 dwc
->desired_otg_role
= id
? DWC3_OTG_ROLE_DEVICE
:
351 if (dwc
->desired_otg_role
== dwc
->current_otg_role
)
354 switch (dwc
->current_otg_role
) {
355 case DWC3_OTG_ROLE_HOST
:
357 spin_lock_irqsave(&dwc
->lock
, flags
);
358 dwc3_otg_host_exit(dwc
);
359 spin_unlock_irqrestore(&dwc
->lock
, flags
);
361 case DWC3_OTG_ROLE_DEVICE
:
362 dwc3_gadget_exit(dwc
);
363 spin_lock_irqsave(&dwc
->lock
, flags
);
364 dwc3_event_buffers_cleanup(dwc
);
365 dwc3_otg_device_exit(dwc
);
366 spin_unlock_irqrestore(&dwc
->lock
, flags
);
372 spin_lock_irqsave(&dwc
->lock
, flags
);
374 dwc
->current_otg_role
= dwc
->desired_otg_role
;
376 spin_unlock_irqrestore(&dwc
->lock
, flags
);
378 switch (dwc
->desired_otg_role
) {
379 case DWC3_OTG_ROLE_HOST
:
380 spin_lock_irqsave(&dwc
->lock
, flags
);
381 dwc3_otgregs_init(dwc
);
382 dwc3_otg_host_init(dwc
);
383 spin_unlock_irqrestore(&dwc
->lock
, flags
);
384 ret
= dwc3_host_init(dwc
);
386 dev_err(dwc
->dev
, "failed to initialize host\n");
389 otg_set_vbus(dwc
->usb2_phy
->otg
, true);
390 if (dwc
->usb2_generic_phy
)
391 phy_set_mode(dwc
->usb2_generic_phy
,
395 case DWC3_OTG_ROLE_DEVICE
:
396 spin_lock_irqsave(&dwc
->lock
, flags
);
397 dwc3_otgregs_init(dwc
);
398 dwc3_otg_device_init(dwc
);
399 dwc3_event_buffers_setup(dwc
);
400 spin_unlock_irqrestore(&dwc
->lock
, flags
);
403 otg_set_vbus(dwc
->usb2_phy
->otg
, false);
404 if (dwc
->usb2_generic_phy
)
405 phy_set_mode(dwc
->usb2_generic_phy
,
406 PHY_MODE_USB_DEVICE
);
407 ret
= dwc3_gadget_init(dwc
);
409 dev_err(dwc
->dev
, "failed to initialize peripheral\n");
416 static void dwc3_drd_update(struct dwc3
*dwc
)
421 id
= extcon_get_state(dwc
->edev
, EXTCON_USB_HOST
);
424 dwc3_set_mode(dwc
, id
?
425 DWC3_GCTL_PRTCAP_HOST
:
426 DWC3_GCTL_PRTCAP_DEVICE
);
430 static int dwc3_drd_notifier(struct notifier_block
*nb
,
431 unsigned long event
, void *ptr
)
433 struct dwc3
*dwc
= container_of(nb
, struct dwc3
, edev_nb
);
435 dwc3_set_mode(dwc
, event
?
436 DWC3_GCTL_PRTCAP_HOST
:
437 DWC3_GCTL_PRTCAP_DEVICE
);
442 int dwc3_drd_init(struct dwc3
*dwc
)
446 if (dwc
->dev
->of_node
&&
447 of_property_read_bool(dwc
->dev
->of_node
, "extcon")) {
448 dwc
->edev
= extcon_get_edev_by_phandle(dwc
->dev
, 0);
450 if (IS_ERR(dwc
->edev
))
451 return PTR_ERR(dwc
->edev
);
453 dwc
->edev_nb
.notifier_call
= dwc3_drd_notifier
;
454 ret
= extcon_register_notifier(dwc
->edev
, EXTCON_USB_HOST
,
457 dev_err(dwc
->dev
, "couldn't register cable notifier\n");
461 dwc3_drd_update(dwc
);
463 dwc3_set_prtcap(dwc
, DWC3_GCTL_PRTCAP_OTG
);
464 dwc
->current_dr_role
= DWC3_GCTL_PRTCAP_OTG
;
466 /* use OTG block to get ID event */
467 irq
= dwc3_otg_get_irq(dwc
);
473 /* disable all OTG IRQs */
474 dwc3_otg_disable_events(dwc
, DWC3_OTG_ALL_EVENTS
);
475 /* clear all events */
476 dwc3_otg_clear_events(dwc
);
478 ret
= request_threaded_irq(dwc
->otg_irq
, dwc3_otg_irq
,
480 IRQF_SHARED
, "dwc3-otg", dwc
);
482 dev_err(dwc
->dev
, "failed to request irq #%d --> %d\n",
489 dwc3_set_mode(dwc
, DWC3_GCTL_PRTCAP_OTG
);
495 void dwc3_drd_exit(struct dwc3
*dwc
)
500 extcon_unregister_notifier(dwc
->edev
, EXTCON_USB_HOST
,
503 cancel_work_sync(&dwc
->drd_work
);
505 /* debug user might have changed role, clean based on current role */
506 switch (dwc
->current_dr_role
) {
507 case DWC3_GCTL_PRTCAP_HOST
:
510 case DWC3_GCTL_PRTCAP_DEVICE
:
511 dwc3_gadget_exit(dwc
);
512 dwc3_event_buffers_cleanup(dwc
);
514 case DWC3_GCTL_PRTCAP_OTG
:
516 spin_lock_irqsave(&dwc
->lock
, flags
);
517 dwc
->desired_otg_role
= DWC3_OTG_ROLE_IDLE
;
518 spin_unlock_irqrestore(&dwc
->lock
, flags
);
519 dwc3_otg_update(dwc
, 1);
526 free_irq(dwc
->otg_irq
, dwc
);