Skip to content

Commit 8ac0d10

Browse files
committed
Merge branch 'bits/040-dwc3' into asahi-wip
2 parents 1b9fd6f + f59caa2 commit 8ac0d10

5 files changed

Lines changed: 138 additions & 7 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
%YAML 1.2
3+
---
4+
$id: http://devicetree.org/schemas/usb/apple,dwc3.yaml#
5+
$schema: http://devicetree.org/meta-schemas/core.yaml#
6+
7+
title: Apple Silicon DWC3 USB controller
8+
9+
maintainers:
10+
- Sven Peter <sven@svenpeter.dev>
11+
12+
description:
13+
On Apple Silicon SoCs such as the M1 each Type-C port has a corresponding
14+
USB controller based on the Synopsys DesignWare USB3 controller.
15+
16+
The common content of this binding is defined in snps,dwc3.yaml.
17+
18+
allOf:
19+
- $ref: snps,dwc3.yaml#
20+
21+
select:
22+
properties:
23+
compatible:
24+
contains:
25+
const: apple,dwc3
26+
required:
27+
- compatible
28+
29+
properties:
30+
compatible:
31+
items:
32+
- enum:
33+
- apple,t8103-dwc3
34+
- apple,t6000-dwc3
35+
- const: apple,dwc3
36+
- const: snps,dwc3
37+
38+
reg:
39+
maxItems: 1
40+
41+
interrupts:
42+
maxItems: 1
43+
44+
unevaluatedProperties: false
45+
46+
required:
47+
- compatible
48+
- reg
49+
- interrupts
50+
51+
examples:
52+
- |
53+
#include <dt-bindings/interrupt-controller/apple-aic.h>
54+
#include <dt-bindings/interrupt-controller/irq.h>
55+
56+
usb@82280000 {
57+
compatible = "apple,t8103-dwc3", "apple,dwc3", "snps,dwc3";
58+
reg = <0x82280000 0x10000>;
59+
interrupts = <AIC_IRQ 777 IRQ_TYPE_LEVEL_HIGH>;
60+
61+
dr_mode = "otg";
62+
usb-role-switch;
63+
role-switch-default-mode = "host";
64+
};

drivers/usb/dwc3/core.c

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
143143
dwc->current_dr_role = mode;
144144
}
145145

146+
static void dwc3_core_exit(struct dwc3 *dwc);
147+
static int dwc3_core_init_for_resume(struct dwc3 *dwc);
148+
146149
static void __dwc3_set_mode(struct work_struct *work)
147150
{
148151
struct dwc3 *dwc = work_to_dwc(work);
@@ -162,7 +165,7 @@ static void __dwc3_set_mode(struct work_struct *work)
162165
if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
163166
dwc3_otg_update(dwc, 0);
164167

165-
if (!desired_dr_role)
168+
if (!desired_dr_role && !dwc->role_switch_reset_quirk)
166169
goto out;
167170

168171
if (desired_dr_role == dwc->current_dr_role)
@@ -190,13 +193,32 @@ static void __dwc3_set_mode(struct work_struct *work)
190193
break;
191194
}
192195

196+
if (dwc->role_switch_reset_quirk) {
197+
if (dwc->current_dr_role) {
198+
dwc->current_dr_role = 0;
199+
dwc3_core_exit(dwc);
200+
}
201+
202+
if (desired_dr_role) {
203+
ret = dwc3_core_init_for_resume(dwc);
204+
if (ret) {
205+
dev_err(dwc->dev,
206+
"failed to reinitialize core\n");
207+
goto out;
208+
}
209+
} else {
210+
goto out;
211+
}
212+
}
213+
193214
/*
194215
* When current_dr_role is not set, there's no role switching.
195216
* Only perform GCTL.CoreSoftReset when there's DRD role switching.
196217
*/
197-
if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
218+
if (dwc->role_switch_reset_quirk ||
219+
(dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
198220
DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
199-
desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
221+
desired_dr_role != DWC3_GCTL_PRTCAP_OTG))) {
200222
reg = dwc3_readl(dwc->regs, DWC3_GCTL);
201223
reg |= DWC3_GCTL_CORESOFTRESET;
202224
dwc3_writel(dwc->regs, DWC3_GCTL, reg);
@@ -1355,6 +1377,9 @@ static int dwc3_core_init(struct dwc3 *dwc)
13551377
if (ret)
13561378
goto err_exit_phy;
13571379

1380+
if (dwc->role_switch_reset_quirk)
1381+
dwc3_enable_susphy(dwc, true);
1382+
13581383
dwc3_core_setup_global_control(dwc);
13591384
dwc3_core_num_eps(dwc);
13601385

@@ -1594,6 +1619,18 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
15941619
ret = dwc3_drd_init(dwc);
15951620
if (ret)
15961621
return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1622+
1623+
/*
1624+
* If the role switch reset quirk is required the first role
1625+
* switch notification will initialize the core such that we
1626+
* have to shut it down here. Make sure that the __dwc3_set_mode
1627+
* queued by dwc3_drd_init has completed before since it
1628+
* may still try to access MMIO.
1629+
*/
1630+
if (dwc->role_switch_reset_quirk) {
1631+
flush_work(&dwc->drd_work);
1632+
dwc3_core_exit(dwc);
1633+
}
15971634
break;
15981635
default:
15991636
dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
@@ -2171,6 +2208,22 @@ static int dwc3_probe(struct platform_device *pdev)
21712208
if (ret)
21722209
goto err_put_psy;
21732210

2211+
if (dev->of_node) {
2212+
if (of_device_is_compatible(dev->of_node, "apple,dwc3")) {
2213+
if (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
2214+
!IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)) {
2215+
dev_err(dev,
2216+
"Apple DWC3 requires role switch support.\n"
2217+
);
2218+
ret = -EINVAL;
2219+
goto err_put_psy;
2220+
}
2221+
2222+
dwc->dr_mode = USB_DR_MODE_OTG;
2223+
dwc->role_switch_reset_quirk = true;
2224+
}
2225+
}
2226+
21742227
ret = reset_control_deassert(dwc->reset);
21752228
if (ret)
21762229
goto err_put_psy;
@@ -2310,7 +2363,6 @@ static void dwc3_remove(struct platform_device *pdev)
23102363
power_supply_put(dwc->usb_psy);
23112364
}
23122365

2313-
#ifdef CONFIG_PM
23142366
static int dwc3_core_init_for_resume(struct dwc3 *dwc)
23152367
{
23162368
int ret;
@@ -2337,6 +2389,7 @@ static int dwc3_core_init_for_resume(struct dwc3 *dwc)
23372389
return ret;
23382390
}
23392391

2392+
#ifdef CONFIG_PM
23402393
static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
23412394
{
23422395
u32 reg;

drivers/usb/dwc3/core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ struct dwc3_scratchpad_array {
11521152
* @suspended: set to track suspend event due to U3/L2.
11531153
* @susphy_state: state of DWC3_GUSB2PHYCFG_SUSPHY + DWC3_GUSB3PIPECTL_SUSPHY
11541154
* before PM suspend.
1155+
* @role_switch_reset_quirk: set to force reinitialization after any role switch
11551156
* @imod_interval: set the interrupt moderation interval in 250ns
11561157
* increments or 0 to disable.
11571158
* @max_cfg_eps: current max number of IN eps used across all USB configs.
@@ -1386,6 +1387,8 @@ struct dwc3 {
13861387
unsigned suspended:1;
13871388
unsigned susphy_state:1;
13881389

1390+
unsigned role_switch_reset_quirk:1;
1391+
13891392
u16 imod_interval;
13901393

13911394
int max_cfg_eps;

drivers/usb/dwc3/drd.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw,
464464
break;
465465
}
466466

467+
if (dwc->role_switch_reset_quirk && role == USB_ROLE_NONE)
468+
mode = 0;
469+
467470
dwc3_set_mode(dwc, mode);
468471
return 0;
469472
}
@@ -492,6 +495,10 @@ static enum usb_role dwc3_usb_role_switch_get(struct usb_role_switch *sw)
492495
role = USB_ROLE_DEVICE;
493496
break;
494497
}
498+
499+
if (dwc->role_switch_reset_quirk && !dwc->current_dr_role)
500+
role = USB_ROLE_NONE;
501+
495502
spin_unlock_irqrestore(&dwc->lock, flags);
496503
return role;
497504
}
@@ -502,7 +509,9 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc)
502509
u32 mode;
503510

504511
dwc->role_switch_default_mode = usb_get_role_switch_default_mode(dwc->dev);
505-
if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) {
512+
if (dwc->role_switch_reset_quirk) {
513+
mode = 0;
514+
} else if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) {
506515
mode = DWC3_GCTL_PRTCAP_HOST;
507516
} else {
508517
dwc->role_switch_default_mode = USB_DR_MODE_PERIPHERAL;

drivers/usb/dwc3/host.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ int dwc3_host_init(struct dwc3 *dwc)
135135
* Some platforms need to power off all Root hub ports immediately after DWC3 set to host
136136
* mode to avoid VBUS glitch happen when xhci get reset later.
137137
*/
138-
dwc3_power_off_all_roothub_ports(dwc);
138+
if (!dwc->role_switch_reset_quirk)
139+
dwc3_power_off_all_roothub_ports(dwc);
139140

140141
irq = dwc3_host_get_irq(dwc);
141142
if (irq < 0)
@@ -220,7 +221,8 @@ void dwc3_host_exit(struct dwc3 *dwc)
220221
if (dwc->sys_wakeup)
221222
device_init_wakeup(&dwc->xhci->dev, false);
222223

223-
dwc3_enable_susphy(dwc, false);
224+
if (!dwc->role_switch_reset_quirk)
225+
dwc3_enable_susphy(dwc, false);
224226
platform_device_unregister(dwc->xhci);
225227
dwc->xhci = NULL;
226228
}

0 commit comments

Comments
 (0)