Skip to content

Commit 9765fd1

Browse files
svenpeter42marcan
authored andcommitted
usb: dwc3: Add support for Apple DWC3
As mad as it sounds, the dwc3 controller present on the Apple M1 must be reset and reinitialized whenever a device is unplugged from the root port or when the PHY mode is changed. This is required for at least the following reasons: - The USB2 D+/D- lines are connected through a stateful eUSB2 repeater which in turn is controlled by a variant of the TI TPS6598x USB PD chip. When the USB PD controller detects a hotplug event it resets the eUSB2 repeater. Afterwards, no new device is recognized before the DWC3 core and PHY are reset as well because the eUSB2 repeater and the PHY/dwc3 block disagree about the current state. - It's possible to completely break the dwc3 controller by switching it to device mode and unplugging the cable at just the wrong time. If this happens dwc3 behaves as if no device is connected. CORESOFTRESET will also never clear after it has been set. The only workaround is to trigger a hard reset of the entire dwc3 core with its external reset line. - Whenever the PHY mode is changed (to e.g. transition to DisplayPort alternate mode or USB4) dwc3 has to be shutdown and reinitialized. Otherwise the Type-C port will not be useable until the entire SoC has been reset. All of this can be easily worked around by respecting transitions to USB_ROLE_NONE and making sure the external reset line is asserted when switching roles. Signed-off-by: Sven Peter <[email protected]>
1 parent c269595 commit 9765fd1

3 files changed

Lines changed: 65 additions & 5 deletions

File tree

drivers/usb/dwc3/core.c

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
116116
dwc->current_dr_role = mode;
117117
}
118118

119+
static void dwc3_core_exit(struct dwc3 *dwc);
120+
static int dwc3_core_init_for_resume(struct dwc3 *dwc);
121+
119122
static void __dwc3_set_mode(struct work_struct *work)
120123
{
121124
struct dwc3 *dwc = work_to_dwc(work);
@@ -134,7 +137,7 @@ static void __dwc3_set_mode(struct work_struct *work)
134137
if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
135138
dwc3_otg_update(dwc, 0);
136139

137-
if (!desired_dr_role)
140+
if (!desired_dr_role && !dwc->role_switch_reset_quirk)
138141
goto out;
139142

140143
if (desired_dr_role == dwc->current_dr_role)
@@ -162,13 +165,32 @@ static void __dwc3_set_mode(struct work_struct *work)
162165
break;
163166
}
164167

168+
if (dwc->role_switch_reset_quirk) {
169+
if (dwc->current_dr_role) {
170+
dwc->current_dr_role = 0;
171+
dwc3_core_exit(dwc);
172+
}
173+
174+
if (desired_dr_role) {
175+
ret = dwc3_core_init_for_resume(dwc);
176+
if (ret) {
177+
dev_err(dwc->dev,
178+
"failed to reinitialize core\n");
179+
goto out;
180+
}
181+
} else {
182+
goto out;
183+
}
184+
}
185+
165186
/*
166187
* When current_dr_role is not set, there's no role switching.
167188
* Only perform GCTL.CoreSoftReset when there's DRD role switching.
168189
*/
169-
if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
190+
if (dwc->role_switch_reset_quirk ||
191+
(dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
170192
DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
171-
desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
193+
desired_dr_role != DWC3_GCTL_PRTCAP_OTG))) {
172194
reg = dwc3_readl(dwc->regs, DWC3_GCTL);
173195
reg |= DWC3_GCTL_CORESOFTRESET;
174196
dwc3_writel(dwc->regs, DWC3_GCTL, reg);
@@ -1405,6 +1427,18 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
14051427
ret = dwc3_drd_init(dwc);
14061428
if (ret)
14071429
return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1430+
1431+
/*
1432+
* If the role switch reset quirk is required the first role
1433+
* switch notification will initialize the core such that we
1434+
* have to shut it down here. Make sure that the __dwc3_set_mode
1435+
* queued by dwc3_drd_init has completed before since it
1436+
* may still try to access MMIO.
1437+
*/
1438+
if (dwc->role_switch_reset_quirk) {
1439+
flush_work(&dwc->drd_work);
1440+
dwc3_core_exit(dwc);
1441+
}
14081442
break;
14091443
default:
14101444
dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
@@ -1854,6 +1888,20 @@ static int dwc3_probe(struct platform_device *pdev)
18541888
goto put_usb_psy;
18551889
}
18561890
}
1891+
1892+
if (of_device_is_compatible(dev->of_node, "apple,dwc3")) {
1893+
if (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
1894+
!IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)) {
1895+
dev_err(dev,
1896+
"Apple DWC3 requires role switch support.\n"
1897+
);
1898+
ret = -EINVAL;
1899+
goto put_usb_psy;
1900+
}
1901+
1902+
dwc->dr_mode = USB_DR_MODE_OTG;
1903+
dwc->role_switch_reset_quirk = true;
1904+
}
18571905
}
18581906

18591907
ret = reset_control_deassert(dwc->reset);
@@ -1996,7 +2044,6 @@ static int dwc3_remove(struct platform_device *pdev)
19962044
return 0;
19972045
}
19982046

1999-
#ifdef CONFIG_PM
20002047
static int dwc3_core_init_for_resume(struct dwc3 *dwc)
20012048
{
20022049
int ret;
@@ -2023,6 +2070,7 @@ static int dwc3_core_init_for_resume(struct dwc3 *dwc)
20232070
return ret;
20242071
}
20252072

2073+
#ifdef CONFIG_PM
20262074
static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
20272075
{
20282076
unsigned long flags;

drivers/usb/dwc3/core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ struct dwc3_scratchpad_array {
11101110
* 3 - Reserved
11111111
* @dis_metastability_quirk: set to disable metastability quirk.
11121112
* @dis_split_quirk: set to disable split boundary.
1113+
* @role_switch_reset_quirk: set to force reinitialization after any role switch
11131114
* @imod_interval: set the interrupt moderation interval in 250ns
11141115
* increments or 0 to disable.
11151116
* @max_cfg_eps: current max number of IN eps used across all USB configs.
@@ -1328,6 +1329,8 @@ struct dwc3 {
13281329
unsigned dis_split_quirk:1;
13291330
unsigned async_callbacks:1;
13301331

1332+
unsigned role_switch_reset_quirk:1;
1333+
13311334
u16 imod_interval;
13321335

13331336
int max_cfg_eps;

drivers/usb/dwc3/drd.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw,
461461
break;
462462
}
463463

464+
if (dwc->role_switch_reset_quirk && role == USB_ROLE_NONE)
465+
mode = 0;
466+
464467
dwc3_set_mode(dwc, mode);
465468
return 0;
466469
}
@@ -489,6 +492,10 @@ static enum usb_role dwc3_usb_role_switch_get(struct usb_role_switch *sw)
489492
role = USB_ROLE_DEVICE;
490493
break;
491494
}
495+
496+
if (dwc->role_switch_reset_quirk && !dwc->current_dr_role)
497+
role = USB_ROLE_NONE;
498+
492499
spin_unlock_irqrestore(&dwc->lock, flags);
493500
return role;
494501
}
@@ -499,7 +506,9 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc)
499506
u32 mode;
500507

501508
dwc->role_switch_default_mode = usb_get_role_switch_default_mode(dwc->dev);
502-
if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) {
509+
if (dwc->role_switch_reset_quirk) {
510+
mode = 0;
511+
} else if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) {
503512
mode = DWC3_GCTL_PRTCAP_HOST;
504513
} else {
505514
dwc->role_switch_default_mode = USB_DR_MODE_PERIPHERAL;

0 commit comments

Comments
 (0)