Skip to content

Commit a520c6d

Browse files
svenpeter42jannau
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 <sven@svenpeter.dev>
1 parent 1564007 commit a520c6d

3 files changed

Lines changed: 67 additions & 5 deletions

File tree

drivers/usb/dwc3/core.c

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
137137
dwc->current_dr_role = mode;
138138
}
139139

140+
static void dwc3_core_exit(struct dwc3 *dwc);
141+
static int dwc3_core_init_for_resume(struct dwc3 *dwc);
142+
140143
static void __dwc3_set_mode(struct work_struct *work)
141144
{
142145
struct dwc3 *dwc = work_to_dwc(work);
@@ -155,7 +158,7 @@ static void __dwc3_set_mode(struct work_struct *work)
155158
if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
156159
dwc3_otg_update(dwc, 0);
157160

158-
if (!desired_dr_role)
161+
if (!desired_dr_role && !dwc->role_switch_reset_quirk)
159162
goto out;
160163

161164
if (desired_dr_role == dwc->current_dr_role)
@@ -183,13 +186,32 @@ static void __dwc3_set_mode(struct work_struct *work)
183186
break;
184187
}
185188

189+
if (dwc->role_switch_reset_quirk) {
190+
if (dwc->current_dr_role) {
191+
dwc->current_dr_role = 0;
192+
dwc3_core_exit(dwc);
193+
}
194+
195+
if (desired_dr_role) {
196+
ret = dwc3_core_init_for_resume(dwc);
197+
if (ret) {
198+
dev_err(dwc->dev,
199+
"failed to reinitialize core\n");
200+
goto out;
201+
}
202+
} else {
203+
goto out;
204+
}
205+
}
206+
186207
/*
187208
* When current_dr_role is not set, there's no role switching.
188209
* Only perform GCTL.CoreSoftReset when there's DRD role switching.
189210
*/
190-
if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
211+
if (dwc->role_switch_reset_quirk ||
212+
(dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
191213
DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
192-
desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
214+
desired_dr_role != DWC3_GCTL_PRTCAP_OTG))) {
193215
reg = dwc3_readl(dwc->regs, DWC3_GCTL);
194216
reg |= DWC3_GCTL_CORESOFTRESET;
195217
dwc3_writel(dwc->regs, DWC3_GCTL, reg);
@@ -1426,6 +1448,18 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
14261448
ret = dwc3_drd_init(dwc);
14271449
if (ret)
14281450
return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1451+
1452+
/*
1453+
* If the role switch reset quirk is required the first role
1454+
* switch notification will initialize the core such that we
1455+
* have to shut it down here. Make sure that the __dwc3_set_mode
1456+
* queued by dwc3_drd_init has completed before since it
1457+
* may still try to access MMIO.
1458+
*/
1459+
if (dwc->role_switch_reset_quirk) {
1460+
flush_work(&dwc->drd_work);
1461+
dwc3_core_exit(dwc);
1462+
}
14291463
break;
14301464
default:
14311465
dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
@@ -1930,6 +1964,22 @@ static int dwc3_probe(struct platform_device *pdev)
19301964
if (ret)
19311965
goto err_put_psy;
19321966

1967+
if (dev->of_node) {
1968+
if (of_device_is_compatible(dev->of_node, "apple,dwc3")) {
1969+
if (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
1970+
!IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)) {
1971+
dev_err(dev,
1972+
"Apple DWC3 requires role switch support.\n"
1973+
);
1974+
ret = -EINVAL;
1975+
goto err_put_psy;
1976+
}
1977+
1978+
dwc->dr_mode = USB_DR_MODE_OTG;
1979+
dwc->role_switch_reset_quirk = true;
1980+
}
1981+
}
1982+
19331983
ret = reset_control_deassert(dwc->reset);
19341984
if (ret)
19351985
goto err_put_psy;
@@ -2055,7 +2105,6 @@ static void dwc3_remove(struct platform_device *pdev)
20552105
power_supply_put(dwc->usb_psy);
20562106
}
20572107

2058-
#ifdef CONFIG_PM
20592108
static int dwc3_core_init_for_resume(struct dwc3 *dwc)
20602109
{
20612110
int ret;
@@ -2082,6 +2131,7 @@ static int dwc3_core_init_for_resume(struct dwc3 *dwc)
20822131
return ret;
20832132
}
20842133

2134+
#ifdef CONFIG_PM
20852135
static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
20862136
{
20872137
unsigned long flags;

drivers/usb/dwc3/core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,7 @@ struct dwc3_scratchpad_array {
11361136
* @sys_wakeup: set if the device may do system wakeup.
11371137
* @wakeup_configured: set if the device is configured for remote wakeup.
11381138
* @suspended: set to track suspend event due to U3/L2.
1139+
* @role_switch_reset_quirk: set to force reinitialization after any role switch
11391140
* @imod_interval: set the interrupt moderation interval in 250ns
11401141
* increments or 0 to disable.
11411142
* @max_cfg_eps: current max number of IN eps used across all USB configs.
@@ -1362,6 +1363,8 @@ struct dwc3 {
13621363
unsigned wakeup_configured:1;
13631364
unsigned suspended:1;
13641365

1366+
unsigned role_switch_reset_quirk:1;
1367+
13651368
u16 imod_interval;
13661369

13671370
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)