Skip to content

Commit 0aff27d

Browse files
committed
Merge branch 'bits/040-dwc3' into asahi-wip
2 parents 81eb391 + fcef351 commit 0aff27d

6 files changed

Lines changed: 188 additions & 79 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: 109 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -105,32 +105,6 @@ static int dwc3_get_dr_mode(struct dwc3 *dwc)
105105
return 0;
106106
}
107107

108-
void dwc3_enable_susphy(struct dwc3 *dwc, bool enable)
109-
{
110-
u32 reg;
111-
int i;
112-
113-
for (i = 0; i < dwc->num_usb3_ports; i++) {
114-
reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(i));
115-
if (enable && !dwc->dis_u3_susphy_quirk)
116-
reg |= DWC3_GUSB3PIPECTL_SUSPHY;
117-
else
118-
reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
119-
120-
dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(i), reg);
121-
}
122-
123-
for (i = 0; i < dwc->num_usb2_ports; i++) {
124-
reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(i));
125-
if (enable && !dwc->dis_u2_susphy_quirk)
126-
reg |= DWC3_GUSB2PHYCFG_SUSPHY;
127-
else
128-
reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
129-
130-
dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(i), reg);
131-
}
132-
}
133-
134108
void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
135109
{
136110
u32 reg;
@@ -143,6 +117,9 @@ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
143117
dwc->current_dr_role = mode;
144118
}
145119

120+
static void dwc3_core_exit(struct dwc3 *dwc);
121+
static int dwc3_core_init_for_resume(struct dwc3 *dwc);
122+
146123
static void __dwc3_set_mode(struct work_struct *work)
147124
{
148125
struct dwc3 *dwc = work_to_dwc(work);
@@ -162,7 +139,7 @@ static void __dwc3_set_mode(struct work_struct *work)
162139
if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
163140
dwc3_otg_update(dwc, 0);
164141

165-
if (!desired_dr_role)
142+
if (!desired_dr_role && !dwc->role_switch_reset_quirk)
166143
goto out;
167144

168145
if (desired_dr_role == dwc->current_dr_role)
@@ -190,13 +167,32 @@ static void __dwc3_set_mode(struct work_struct *work)
190167
break;
191168
}
192169

170+
if (dwc->role_switch_reset_quirk) {
171+
if (dwc->current_dr_role) {
172+
dwc->current_dr_role = 0;
173+
dwc3_core_exit(dwc);
174+
}
175+
176+
if (desired_dr_role) {
177+
ret = dwc3_core_init_for_resume(dwc);
178+
if (ret) {
179+
dev_err(dwc->dev,
180+
"failed to reinitialize core\n");
181+
goto out;
182+
}
183+
} else {
184+
goto out;
185+
}
186+
}
187+
193188
/*
194189
* When current_dr_role is not set, there's no role switching.
195190
* Only perform GCTL.CoreSoftReset when there's DRD role switching.
196191
*/
197-
if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
192+
if (dwc->role_switch_reset_quirk ||
193+
(dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
198194
DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
199-
desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
195+
desired_dr_role != DWC3_GCTL_PRTCAP_OTG))) {
200196
reg = dwc3_readl(dwc->regs, DWC3_GCTL);
201197
reg |= DWC3_GCTL_CORESOFTRESET;
202198
dwc3_writel(dwc->regs, DWC3_GCTL, reg);
@@ -642,8 +638,11 @@ static int dwc3_core_ulpi_init(struct dwc3 *dwc)
642638

643639
static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
644640
{
641+
unsigned int hw_mode;
645642
u32 reg;
646643

644+
hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
645+
647646
reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(index));
648647

649648
/*
@@ -653,16 +652,21 @@ static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
653652
reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
654653

655654
/*
656-
* Above DWC_usb3.0 1.94a, it is recommended to set
657-
* DWC3_GUSB3PIPECTL_SUSPHY to '0' during coreConsultant configuration.
658-
* So default value will be '0' when the core is reset. Application
659-
* needs to set it to '1' after the core initialization is completed.
660-
*
661-
* Similarly for DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be
662-
* cleared after power-on reset, and it can be set after core
663-
* initialization.
655+
* Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
656+
* to '0' during coreConsultant configuration. So default value
657+
* will be '0' when the core is reset. Application needs to set it
658+
* to '1' after the core initialization is completed.
664659
*/
665-
reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
660+
if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
661+
reg |= DWC3_GUSB3PIPECTL_SUSPHY;
662+
663+
/*
664+
* For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
665+
* power-on reset, and it can be set after core initialization, which is
666+
* after device soft-reset during initialization.
667+
*/
668+
if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
669+
reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
666670

667671
if (dwc->u2ss_inp3_quirk)
668672
reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
@@ -688,6 +692,9 @@ static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
688692
if (dwc->tx_de_emphasis_quirk)
689693
reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
690694

695+
if (dwc->dis_u3_susphy_quirk)
696+
reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
697+
691698
if (dwc->dis_del_phy_power_chg_quirk)
692699
reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
693700

@@ -698,8 +705,11 @@ static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
698705

699706
static int dwc3_hs_phy_setup(struct dwc3 *dwc, int index)
700707
{
708+
unsigned int hw_mode;
701709
u32 reg;
702710

711+
hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
712+
703713
reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(index));
704714

705715
/* Select the HS PHY interface */
@@ -742,15 +752,24 @@ static int dwc3_hs_phy_setup(struct dwc3 *dwc, int index)
742752
}
743753

744754
/*
745-
* Above DWC_usb3.0 1.94a, it is recommended to set
746-
* DWC3_GUSB2PHYCFG_SUSPHY to '0' during coreConsultant configuration.
747-
* So default value will be '0' when the core is reset. Application
748-
* needs to set it to '1' after the core initialization is completed.
749-
*
750-
* Similarly for DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared
751-
* after power-on reset, and it can be set after core initialization.
755+
* Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
756+
* '0' during coreConsultant configuration. So default value will
757+
* be '0' when the core is reset. Application needs to set it to
758+
* '1' after the core initialization is completed.
759+
*/
760+
if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
761+
reg |= DWC3_GUSB2PHYCFG_SUSPHY;
762+
763+
/*
764+
* For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
765+
* power-on reset, and it can be set after core initialization, which is
766+
* after device soft-reset during initialization.
752767
*/
753-
reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
768+
if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
769+
reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
770+
771+
if (dwc->dis_u2_susphy_quirk)
772+
reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
754773

755774
if (dwc->dis_enblslpm_quirk)
756775
reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
@@ -1349,6 +1368,21 @@ static int dwc3_core_init(struct dwc3 *dwc)
13491368
if (ret)
13501369
goto err_exit_phy;
13511370

1371+
if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
1372+
!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
1373+
if (!dwc->dis_u3_susphy_quirk) {
1374+
reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1375+
reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1376+
dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1377+
}
1378+
1379+
if (!dwc->dis_u2_susphy_quirk) {
1380+
reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1381+
reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1382+
dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1383+
}
1384+
}
1385+
13521386
dwc3_core_setup_global_control(dwc);
13531387
dwc3_core_num_eps(dwc);
13541388

@@ -1588,6 +1622,18 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
15881622
ret = dwc3_drd_init(dwc);
15891623
if (ret)
15901624
return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1625+
1626+
/*
1627+
* If the role switch reset quirk is required the first role
1628+
* switch notification will initialize the core such that we
1629+
* have to shut it down here. Make sure that the __dwc3_set_mode
1630+
* queued by dwc3_drd_init has completed before since it
1631+
* may still try to access MMIO.
1632+
*/
1633+
if (dwc->role_switch_reset_quirk) {
1634+
flush_work(&dwc->drd_work);
1635+
dwc3_core_exit(dwc);
1636+
}
15911637
break;
15921638
default:
15931639
dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
@@ -2165,6 +2211,22 @@ static int dwc3_probe(struct platform_device *pdev)
21652211
if (ret)
21662212
goto err_put_psy;
21672213

2214+
if (dev->of_node) {
2215+
if (of_device_is_compatible(dev->of_node, "apple,dwc3")) {
2216+
if (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
2217+
!IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)) {
2218+
dev_err(dev,
2219+
"Apple DWC3 requires role switch support.\n"
2220+
);
2221+
ret = -EINVAL;
2222+
goto err_put_psy;
2223+
}
2224+
2225+
dwc->dr_mode = USB_DR_MODE_OTG;
2226+
dwc->role_switch_reset_quirk = true;
2227+
}
2228+
}
2229+
21682230
ret = reset_control_deassert(dwc->reset);
21692231
if (ret)
21702232
goto err_put_psy;
@@ -2304,7 +2366,6 @@ static void dwc3_remove(struct platform_device *pdev)
23042366
power_supply_put(dwc->usb_psy);
23052367
}
23062368

2307-
#ifdef CONFIG_PM
23082369
static int dwc3_core_init_for_resume(struct dwc3 *dwc)
23092370
{
23102371
int ret;
@@ -2331,6 +2392,7 @@ static int dwc3_core_init_for_resume(struct dwc3 *dwc)
23312392
return ret;
23322393
}
23332394

2395+
#ifdef CONFIG_PM
23342396
static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
23352397
{
23362398
u32 reg;

drivers/usb/dwc3/core.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@ struct dwc3_scratchpad_array {
11501150
* @sys_wakeup: set if the device may do system wakeup.
11511151
* @wakeup_configured: set if the device is configured for remote wakeup.
11521152
* @suspended: set to track suspend event due to U3/L2.
1153+
* @role_switch_reset_quirk: set to force reinitialization after any role switch
11531154
* @imod_interval: set the interrupt moderation interval in 250ns
11541155
* increments or 0 to disable.
11551156
* @max_cfg_eps: current max number of IN eps used across all USB configs.
@@ -1383,6 +1384,8 @@ struct dwc3 {
13831384
unsigned wakeup_configured:1;
13841385
unsigned suspended:1;
13851386

1387+
unsigned role_switch_reset_quirk:1;
1388+
13861389
u16 imod_interval;
13871390

13881391
int max_cfg_eps;
@@ -1602,7 +1605,6 @@ int dwc3_event_buffers_setup(struct dwc3 *dwc);
16021605
void dwc3_event_buffers_cleanup(struct dwc3 *dwc);
16031606

16041607
int dwc3_core_soft_reset(struct dwc3 *dwc);
1605-
void dwc3_enable_susphy(struct dwc3 *dwc, bool enable);
16061608

16071609
#if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
16081610
int dwc3_host_init(struct dwc3 *dwc);

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;

0 commit comments

Comments
 (0)