Skip to content

Commit ed672ac

Browse files
committed
Merge branch 'bits/040-dwc3' into asahi-wip
2 parents 262c83c + 49b1418 commit ed672ac

6 files changed

Lines changed: 188 additions & 74 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 <[email protected]>
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 & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -105,27 +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-
112-
reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
113-
if (enable && !dwc->dis_u3_susphy_quirk)
114-
reg |= DWC3_GUSB3PIPECTL_SUSPHY;
115-
else
116-
reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
117-
118-
dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
119-
120-
reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
121-
if (enable && !dwc->dis_u2_susphy_quirk)
122-
reg |= DWC3_GUSB2PHYCFG_SUSPHY;
123-
else
124-
reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
125-
126-
dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
127-
}
128-
129108
void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
130109
{
131110
u32 reg;
@@ -138,6 +117,9 @@ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode)
138117
dwc->current_dr_role = mode;
139118
}
140119

120+
static void dwc3_core_exit(struct dwc3 *dwc);
121+
static int dwc3_core_init_for_resume(struct dwc3 *dwc);
122+
141123
static void __dwc3_set_mode(struct work_struct *work)
142124
{
143125
struct dwc3 *dwc = work_to_dwc(work);
@@ -157,7 +139,7 @@ static void __dwc3_set_mode(struct work_struct *work)
157139
if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
158140
dwc3_otg_update(dwc, 0);
159141

160-
if (!desired_dr_role)
142+
if (!desired_dr_role && !dwc->role_switch_reset_quirk)
161143
goto out;
162144

163145
if (desired_dr_role == dwc->current_dr_role)
@@ -185,13 +167,32 @@ static void __dwc3_set_mode(struct work_struct *work)
185167
break;
186168
}
187169

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+
188188
/*
189189
* When current_dr_role is not set, there's no role switching.
190190
* Only perform GCTL.CoreSoftReset when there's DRD role switching.
191191
*/
192-
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) ||
193194
DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
194-
desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
195+
desired_dr_role != DWC3_GCTL_PRTCAP_OTG))) {
195196
reg = dwc3_readl(dwc->regs, DWC3_GCTL);
196197
reg |= DWC3_GCTL_CORESOFTRESET;
197198
dwc3_writel(dwc->regs, DWC3_GCTL, reg);
@@ -617,8 +618,11 @@ static int dwc3_core_ulpi_init(struct dwc3 *dwc)
617618

618619
static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
619620
{
621+
unsigned int hw_mode;
620622
u32 reg;
621623

624+
hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
625+
622626
reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(index));
623627

624628
/*
@@ -628,16 +632,21 @@ static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
628632
reg &= ~DWC3_GUSB3PIPECTL_UX_EXIT_PX;
629633

630634
/*
631-
* Above DWC_usb3.0 1.94a, it is recommended to set
632-
* DWC3_GUSB3PIPECTL_SUSPHY to '0' during coreConsultant configuration.
633-
* So default value will be '0' when the core is reset. Application
634-
* needs to set it to '1' after the core initialization is completed.
635-
*
636-
* Similarly for DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be
637-
* cleared after power-on reset, and it can be set after core
638-
* initialization.
635+
* Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
636+
* to '0' during coreConsultant configuration. So default value
637+
* will be '0' when the core is reset. Application needs to set it
638+
* to '1' after the core initialization is completed.
639+
*/
640+
if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
641+
reg |= DWC3_GUSB3PIPECTL_SUSPHY;
642+
643+
/*
644+
* For DRD controllers, GUSB3PIPECTL.SUSPENDENABLE must be cleared after
645+
* power-on reset, and it can be set after core initialization, which is
646+
* after device soft-reset during initialization.
639647
*/
640-
reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
648+
if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
649+
reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
641650

642651
if (dwc->u2ss_inp3_quirk)
643652
reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
@@ -663,6 +672,9 @@ static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
663672
if (dwc->tx_de_emphasis_quirk)
664673
reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
665674

675+
if (dwc->dis_u3_susphy_quirk)
676+
reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
677+
666678
if (dwc->dis_del_phy_power_chg_quirk)
667679
reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
668680

@@ -673,8 +685,11 @@ static int dwc3_ss_phy_setup(struct dwc3 *dwc, int index)
673685

674686
static int dwc3_hs_phy_setup(struct dwc3 *dwc, int index)
675687
{
688+
unsigned int hw_mode;
676689
u32 reg;
677690

691+
hw_mode = DWC3_GHWPARAMS0_MODE(dwc->hwparams.hwparams0);
692+
678693
reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(index));
679694

680695
/* Select the HS PHY interface */
@@ -717,15 +732,24 @@ static int dwc3_hs_phy_setup(struct dwc3 *dwc, int index)
717732
}
718733

719734
/*
720-
* Above DWC_usb3.0 1.94a, it is recommended to set
721-
* DWC3_GUSB2PHYCFG_SUSPHY to '0' during coreConsultant configuration.
722-
* So default value will be '0' when the core is reset. Application
723-
* needs to set it to '1' after the core initialization is completed.
724-
*
725-
* Similarly for DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared
726-
* after power-on reset, and it can be set after core initialization.
735+
* Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
736+
* '0' during coreConsultant configuration. So default value will
737+
* be '0' when the core is reset. Application needs to set it to
738+
* '1' after the core initialization is completed.
739+
*/
740+
if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A))
741+
reg |= DWC3_GUSB2PHYCFG_SUSPHY;
742+
743+
/*
744+
* For DRD controllers, GUSB2PHYCFG.SUSPHY must be cleared after
745+
* power-on reset, and it can be set after core initialization, which is
746+
* after device soft-reset during initialization.
727747
*/
728-
reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
748+
if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD)
749+
reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
750+
751+
if (dwc->dis_u2_susphy_quirk)
752+
reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
729753

730754
if (dwc->dis_enblslpm_quirk)
731755
reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
@@ -1324,6 +1348,21 @@ static int dwc3_core_init(struct dwc3 *dwc)
13241348
if (ret)
13251349
goto err_exit_phy;
13261350

1351+
if (hw_mode == DWC3_GHWPARAMS0_MODE_DRD &&
1352+
!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A)) {
1353+
if (!dwc->dis_u3_susphy_quirk) {
1354+
reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1355+
reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1356+
dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1357+
}
1358+
1359+
if (!dwc->dis_u2_susphy_quirk) {
1360+
reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1361+
reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1362+
dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1363+
}
1364+
}
1365+
13271366
dwc3_core_setup_global_control(dwc);
13281367
dwc3_core_num_eps(dwc);
13291368

@@ -1546,6 +1585,18 @@ static int dwc3_core_init_mode(struct dwc3 *dwc)
15461585
ret = dwc3_drd_init(dwc);
15471586
if (ret)
15481587
return dev_err_probe(dev, ret, "failed to initialize dual-role\n");
1588+
1589+
/*
1590+
* If the role switch reset quirk is required the first role
1591+
* switch notification will initialize the core such that we
1592+
* have to shut it down here. Make sure that the __dwc3_set_mode
1593+
* queued by dwc3_drd_init has completed before since it
1594+
* may still try to access MMIO.
1595+
*/
1596+
if (dwc->role_switch_reset_quirk) {
1597+
flush_work(&dwc->drd_work);
1598+
dwc3_core_exit(dwc);
1599+
}
15491600
break;
15501601
default:
15511602
dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
@@ -2100,6 +2151,22 @@ static int dwc3_probe(struct platform_device *pdev)
21002151
if (ret)
21012152
goto err_put_psy;
21022153

2154+
if (dev->of_node) {
2155+
if (of_device_is_compatible(dev->of_node, "apple,dwc3")) {
2156+
if (!IS_ENABLED(CONFIG_USB_ROLE_SWITCH) ||
2157+
!IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)) {
2158+
dev_err(dev,
2159+
"Apple DWC3 requires role switch support.\n"
2160+
);
2161+
ret = -EINVAL;
2162+
goto err_put_psy;
2163+
}
2164+
2165+
dwc->dr_mode = USB_DR_MODE_OTG;
2166+
dwc->role_switch_reset_quirk = true;
2167+
}
2168+
}
2169+
21032170
ret = reset_control_deassert(dwc->reset);
21042171
if (ret)
21052172
goto err_put_psy;
@@ -2239,7 +2306,6 @@ static void dwc3_remove(struct platform_device *pdev)
22392306
power_supply_put(dwc->usb_psy);
22402307
}
22412308

2242-
#ifdef CONFIG_PM
22432309
static int dwc3_core_init_for_resume(struct dwc3 *dwc)
22442310
{
22452311
int ret;
@@ -2266,6 +2332,7 @@ static int dwc3_core_init_for_resume(struct dwc3 *dwc)
22662332
return ret;
22672333
}
22682334

2335+
#ifdef CONFIG_PM
22692336
static int dwc3_suspend_common(struct dwc3 *dwc, pm_message_t msg)
22702337
{
22712338
u32 reg;

drivers/usb/dwc3/core.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ struct dwc3_scratchpad_array {
11451145
* @sys_wakeup: set if the device may do system wakeup.
11461146
* @wakeup_configured: set if the device is configured for remote wakeup.
11471147
* @suspended: set to track suspend event due to U3/L2.
1148+
* @role_switch_reset_quirk: set to force reinitialization after any role switch
11481149
* @imod_interval: set the interrupt moderation interval in 250ns
11491150
* increments or 0 to disable.
11501151
* @max_cfg_eps: current max number of IN eps used across all USB configs.
@@ -1374,6 +1375,8 @@ struct dwc3 {
13741375
unsigned wakeup_configured:1;
13751376
unsigned suspended:1;
13761377

1378+
unsigned role_switch_reset_quirk:1;
1379+
13771380
u16 imod_interval;
13781381

13791382
int max_cfg_eps;
@@ -1592,7 +1595,6 @@ int dwc3_event_buffers_setup(struct dwc3 *dwc);
15921595
void dwc3_event_buffers_cleanup(struct dwc3 *dwc);
15931596

15941597
int dwc3_core_soft_reset(struct dwc3 *dwc);
1595-
void dwc3_enable_susphy(struct dwc3 *dwc, bool enable);
15961598

15971599
#if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
15981600
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;

drivers/usb/dwc3/gadget.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,7 +2922,6 @@ static int __dwc3_gadget_start(struct dwc3 *dwc)
29222922
dwc3_ep0_out_start(dwc);
29232923

29242924
dwc3_gadget_enable_irq(dwc);
2925-
dwc3_enable_susphy(dwc, true);
29262925

29272926
return 0;
29282927

@@ -4689,7 +4688,6 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
46894688
if (!dwc->gadget)
46904689
return;
46914690

4692-
dwc3_enable_susphy(dwc, false);
46934691
usb_del_gadget(dwc->gadget);
46944692
dwc3_gadget_free_endpoints(dwc);
46954693
usb_put_gadget(dwc->gadget);

0 commit comments

Comments
 (0)