From patchwork Mon Dec 6 15:56:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabrice Gasnier X-Patchwork-Id: 521254 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A398C433EF for ; Mon, 6 Dec 2021 16:02:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381939AbhLFQFA (ORCPT ); Mon, 6 Dec 2021 11:05:00 -0500 Received: from mx08-00178001.pphosted.com ([91.207.212.93]:48620 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1444094AbhLFQCF (ORCPT ); Mon, 6 Dec 2021 11:02:05 -0500 Received: from pps.filterd (m0046661.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 1B6AXUYw010394; Mon, 6 Dec 2021 16:58:23 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=selector1; bh=pDwLNxoPVdngPGAwJNxCRNmFgUdFa3lK4eYudZI1Jbk=; b=2P6Tv4nX5h07mFWourGbGL/m9oyNX13k+v78t3Lft/4/yNH8t2DOgoVKyybBdTdDcOX3 d+nDYRSM5xC3AkpB+TTj3fb3Zmlk+AjlFK+T62XjGEPhmn4hVcJkRQLSBQBHLij5pQG+ XIa/OHuw+If66RPQPf/w/NfYxdVq4T8W5qJe/BczNTUGhC35+i3DaBp8SaZSMFJVpTek KlKQAesEyFr2uYer36jf0Mtnt63agEiW0LgYVjOGXCpTap7ggZIgamnuyo4Upu8o3LKl LSTjEjp8gw9lQk6TdQYrWm9sYlFCgJvhkuhZ1J9N3Cynr21jppUGss0WL1L7kQpIrdT1 Ow== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3csb4j3dx2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Dec 2021 16:58:23 +0100 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 76D02100034; Mon, 6 Dec 2021 16:58:22 +0100 (CET) Received: from Webmail-eu.st.com (sfhdag2node2.st.com [10.75.127.5]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 6EEC320930D; Mon, 6 Dec 2021 16:58:22 +0100 (CET) Received: from localhost (10.75.127.51) by SFHDAG2NODE2.st.com (10.75.127.5) with Microsoft SMTP Server (TLS) id 15.0.1497.26; Mon, 6 Dec 2021 16:58:21 +0100 From: Fabrice Gasnier To: , , CC: , , , , , , Subject: [PATCH 1/3] dt-bindings: usb: document role-switch-default-mode property in dwc2 Date: Mon, 6 Dec 2021 16:56:41 +0100 Message-ID: <1638806203-6624-2-git-send-email-fabrice.gasnier@foss.st.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1638806203-6624-1-git-send-email-fabrice.gasnier@foss.st.com> References: <1638806203-6624-1-git-send-email-fabrice.gasnier@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.75.127.51] X-ClientProxiedBy: SFHDAG2NODE3.st.com (10.75.127.6) To SFHDAG2NODE2.st.com (10.75.127.5) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2021-12-06_05,2021-12-06_02,2021-12-02_01 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org role-switch-default-mode property may be used with usb-role-switch property to define the default operation mode (by forcing the a-valid or b-valid) at init. Signed-off-by: Amelie Delaunay Signed-off-by: Fabrice Gasnier Reviewed-by: Rob Herring --- Documentation/devicetree/bindings/usb/dwc2.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Documentation/devicetree/bindings/usb/dwc2.yaml b/Documentation/devicetree/bindings/usb/dwc2.yaml index 94e7276..f00867e 100644 --- a/Documentation/devicetree/bindings/usb/dwc2.yaml +++ b/Documentation/devicetree/bindings/usb/dwc2.yaml @@ -114,6 +114,8 @@ properties: usb-role-switch: true + role-switch-default-mode: true + g-rx-fifo-size: $ref: /schemas/types.yaml#/definitions/uint32 description: size of rx fifo size in gadget mode. @@ -145,6 +147,7 @@ properties: dependencies: port: [ usb-role-switch ] + role-switch-default-mode: [ usb-role-switch ] required: - compatible From patchwork Mon Dec 6 15:56:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabrice Gasnier X-Patchwork-Id: 522481 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F080AC433FE for ; Mon, 6 Dec 2021 16:02:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1444155AbhLFQFF (ORCPT ); Mon, 6 Dec 2021 11:05:05 -0500 Received: from mx07-00178001.pphosted.com ([185.132.182.106]:37810 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1444112AbhLFQCH (ORCPT ); Mon, 6 Dec 2021 11:02:07 -0500 Received: from pps.filterd (m0288072.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 1B6F9whL003232; Mon, 6 Dec 2021 16:58:24 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=selector1; bh=LuQGWl9MC186XjkSHLspk8zPVPBxHOXk77t/6daUfYY=; b=8RvrBHWzawcNLW0ZOXDI8+HJIJVGqOf6agyZBkzuJHdBU1QMM2bqO/WUZOwU6l0mlsg8 3bGZdM/x4fwNAkdlxBFSc9fYdmzOltI/4Asb0HevL3q6DbJsXI7oKrkCxiJ5rzWg7/CC F6wzxIFIsnVj62QpQY46/SCUOdfQfwu/WH9Ih/p5t0MABd5aCC4OvenMHtzv72CvRdhO s/OZ23rrlrC8KyRBzttxBPgNPJW1Md6EGgzMgpo/TOR68sphmnJFRDO4zBnCwEdlmqi2 ZnjLDbC6X/9Ni0eRmANAoV/VnzEOFeOObmOFVmhpP15p/CCGrR5wTShblYLLbL3awEhh vg== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3csmx0g8c2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Dec 2021 16:58:24 +0100 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id DC29E10002A; Mon, 6 Dec 2021 16:58:23 +0100 (CET) Received: from Webmail-eu.st.com (sfhdag2node2.st.com [10.75.127.5]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id D448620930D; Mon, 6 Dec 2021 16:58:23 +0100 (CET) Received: from localhost (10.75.127.51) by SFHDAG2NODE2.st.com (10.75.127.5) with Microsoft SMTP Server (TLS) id 15.0.1497.26; Mon, 6 Dec 2021 16:58:23 +0100 From: Fabrice Gasnier To: , , CC: , , , , , , Subject: [PATCH 2/3] usb: dwc2: drd: add role-switch-default-node support Date: Mon, 6 Dec 2021 16:56:42 +0100 Message-ID: <1638806203-6624-3-git-send-email-fabrice.gasnier@foss.st.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1638806203-6624-1-git-send-email-fabrice.gasnier@foss.st.com> References: <1638806203-6624-1-git-send-email-fabrice.gasnier@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.75.127.51] X-ClientProxiedBy: SFHDAG2NODE1.st.com (10.75.127.4) To SFHDAG2NODE2.st.com (10.75.127.5) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2021-12-06_05,2021-12-06_02,2021-12-02_01 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org When using usb-role-switch, while the usb role is not yet define (USB_ROLE_NONE), the user may want to configure the default mode to host or device. Use role-switch-default-mode for that purpose. Signed-off-by: Amelie Delaunay Signed-off-by: Fabrice Gasnier --- drivers/usb/dwc2/core.h | 3 +++ drivers/usb/dwc2/drd.c | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/drivers/usb/dwc2/core.h b/drivers/usb/dwc2/core.h index 37185eb..e2fe642 100644 --- a/drivers/usb/dwc2/core.h +++ b/drivers/usb/dwc2/core.h @@ -869,6 +869,8 @@ struct dwc2_hregs_backup { * - USB_DR_MODE_HOST * - USB_DR_MODE_OTG * @role_sw: usb_role_switch handle + * @role_sw_default_mode: default operation mode of controller while usb role + * is USB_ROLE_NONE * @hcd_enabled: Host mode sub-driver initialization indicator. * @gadget_enabled: Peripheral mode sub-driver initialization indicator. * @ll_hw_enabled: Status of low-level hardware resources. @@ -1065,6 +1067,7 @@ struct dwc2_hsotg { enum usb_otg_state op_state; enum usb_dr_mode dr_mode; struct usb_role_switch *role_sw; + enum usb_dr_mode role_sw_default_mode; unsigned int hcd_enabled:1; unsigned int gadget_enabled:1; unsigned int ll_hw_enabled:1; diff --git a/drivers/usb/dwc2/drd.c b/drivers/usb/dwc2/drd.c index aa6eb76..4f453ec 100644 --- a/drivers/usb/dwc2/drd.c +++ b/drivers/usb/dwc2/drd.c @@ -24,6 +24,10 @@ static void dwc2_ovr_init(struct dwc2_hsotg *hsotg) gotgctl |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN; gotgctl |= GOTGCTL_DBNCE_FLTR_BYPASS; gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL); + if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST) + gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL; + else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL) + gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL; dwc2_writel(hsotg, gotgctl, GOTGCTL); spin_unlock_irqrestore(&hsotg->lock, flags); @@ -105,6 +109,14 @@ static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role) spin_lock_irqsave(&hsotg->lock, flags); + if (role == USB_ROLE_NONE) { + /* default operation mode when usb role is USB_ROLE_NONE */ + if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST) + role = USB_ROLE_HOST; + else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL) + role = USB_ROLE_DEVICE; + } + if (role == USB_ROLE_HOST) { already = dwc2_ovr_avalid(hsotg, true); } else if (role == USB_ROLE_DEVICE) { @@ -146,6 +158,7 @@ int dwc2_drd_init(struct dwc2_hsotg *hsotg) if (!device_property_read_bool(hsotg->dev, "usb-role-switch")) return 0; + hsotg->role_sw_default_mode = usb_get_role_switch_default_mode(hsotg->dev); role_sw_desc.driver_data = hsotg; role_sw_desc.fwnode = dev_fwnode(hsotg->dev); role_sw_desc.set = dwc2_drd_role_sw_set; From patchwork Mon Dec 6 15:56:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabrice Gasnier X-Patchwork-Id: 522482 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 530F6C433F5 for ; Mon, 6 Dec 2021 16:01:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1389306AbhLFQE6 (ORCPT ); Mon, 6 Dec 2021 11:04:58 -0500 Received: from mx08-00178001.pphosted.com ([91.207.212.93]:46800 "EHLO mx07-00178001.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1444096AbhLFQCF (ORCPT ); Mon, 6 Dec 2021 11:02:05 -0500 Received: from pps.filterd (m0046660.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 1B6DBspb016754; Mon, 6 Dec 2021 16:58:26 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=selector1; bh=tvsWt/WajNW/cDfhB+hHbcbDBrmdVc3RwtncRhs+lpY=; b=oqhoHEiaddEry9YI/kn67OgIkaHZngu2HGdOYe1wHwbjeEfluXP+8Nua0ysnyGkoiENl yoaUceUhp6ZXRtgV6IM420nSyVpGFHdYkUm6Oj13mrR+i5B1gMigF15PvUc9ayBylkEV FTyjrY7cBynTcAb2Pqx0SYVgWU9qa+PmlPQNHrtPUpztQC/gB2KKirl//2uVspYW95/T No7EzPmBI7A1pkSHeNvB7QpQ1A/HDF1ck7QN0O93iXEDkhlM35mr2zlpZi1nXXcBea0G a+VqmEfXxURhS6YesEAOZlKziTV5Hm368m2VjX6DnXEx86/kdqbKik0el15FzRmgoziY mA== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3cseqrjfah-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Dec 2021 16:58:26 +0100 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 6FDD110002A; Mon, 6 Dec 2021 16:58:25 +0100 (CET) Received: from Webmail-eu.st.com (sfhdag2node2.st.com [10.75.127.5]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 67C3120930D; Mon, 6 Dec 2021 16:58:25 +0100 (CET) Received: from localhost (10.75.127.51) by SFHDAG2NODE2.st.com (10.75.127.5) with Microsoft SMTP Server (TLS) id 15.0.1497.26; Mon, 6 Dec 2021 16:58:24 +0100 From: Fabrice Gasnier To: , , CC: , , , , , , Subject: [PATCH 3/3] usb: dwc2: drd: restore role and overrides upon resume Date: Mon, 6 Dec 2021 16:56:43 +0100 Message-ID: <1638806203-6624-4-git-send-email-fabrice.gasnier@foss.st.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1638806203-6624-1-git-send-email-fabrice.gasnier@foss.st.com> References: <1638806203-6624-1-git-send-email-fabrice.gasnier@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.75.127.51] X-ClientProxiedBy: SFHDAG1NODE2.st.com (10.75.127.2) To SFHDAG2NODE2.st.com (10.75.127.5) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2021-12-06_05,2021-12-06_02,2021-12-02_01 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org Override enable bits may not be restored when going to low power mode (e.g. when in DWC2_POWER_DOWN_PARAM_NONE). These bits are set when probing/initializing drd (role switch). Restore them upon resume from low power mode (in case these have been lost). To achieve this, the last known role is restored upon resume. And the override enable bits are always set when configuring aval, bval and vbval. When resuming, forcing the role should be done only once, or this can cause port changes in HOST mode for instance. So, only restore FORCEDEVMODE/FORCEHOSTMODE when role_sw is unused Signed-off-by: Fabrice Gasnier Acked-by: Minas Harutyunyan --- drivers/usb/dwc2/drd.c | 38 ++++++++++++++++++++++++++++++++++++-- drivers/usb/dwc2/platform.c | 10 ++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/drivers/usb/dwc2/drd.c b/drivers/usb/dwc2/drd.c index 4f453ec..1b39c47 100644 --- a/drivers/usb/dwc2/drd.c +++ b/drivers/usb/dwc2/drd.c @@ -13,6 +13,10 @@ #include #include "core.h" +#define dwc2_ovr_gotgctl(gotgctl) \ + ((gotgctl) |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN | \ + GOTGCTL_DBNCE_FLTR_BYPASS) + static void dwc2_ovr_init(struct dwc2_hsotg *hsotg) { unsigned long flags; @@ -21,8 +25,7 @@ static void dwc2_ovr_init(struct dwc2_hsotg *hsotg) spin_lock_irqsave(&hsotg->lock, flags); gotgctl = dwc2_readl(hsotg, GOTGCTL); - gotgctl |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN; - gotgctl |= GOTGCTL_DBNCE_FLTR_BYPASS; + dwc2_ovr_gotgctl(gotgctl); gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL); if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST) gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL; @@ -44,6 +47,9 @@ static int dwc2_ovr_avalid(struct dwc2_hsotg *hsotg, bool valid) (!valid && !(gotgctl & GOTGCTL_ASESVLD))) return -EALREADY; + /* Always enable overrides to handle the resume case */ + dwc2_ovr_gotgctl(gotgctl); + gotgctl &= ~GOTGCTL_BVALOVAL; if (valid) gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL; @@ -63,6 +69,9 @@ static int dwc2_ovr_bvalid(struct dwc2_hsotg *hsotg, bool valid) (!valid && !(gotgctl & GOTGCTL_BSESVLD))) return -EALREADY; + /* Always enable overrides to handle the resume case */ + dwc2_ovr_gotgctl(gotgctl); + gotgctl &= ~GOTGCTL_AVALOVAL; if (valid) gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL; @@ -196,6 +205,31 @@ void dwc2_drd_suspend(struct dwc2_hsotg *hsotg) void dwc2_drd_resume(struct dwc2_hsotg *hsotg) { u32 gintsts, gintmsk; + enum usb_role role; + + if (hsotg->role_sw) { + /* get last known role (as the get ops isn't implemented by this driver) */ + role = usb_role_switch_get_role(hsotg->role_sw); + + if (role == USB_ROLE_NONE) { + if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST) + role = USB_ROLE_HOST; + else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL) + role = USB_ROLE_DEVICE; + } + + /* restore last role that may have been lost */ + if (role == USB_ROLE_HOST) + dwc2_ovr_avalid(hsotg, true); + else if (role == USB_ROLE_DEVICE) + dwc2_ovr_bvalid(hsotg, true); + + dwc2_force_mode(hsotg, role == USB_ROLE_HOST); + + dev_dbg(hsotg->dev, "resuming %s-session valid\n", + role == USB_ROLE_NONE ? "No" : + role == USB_ROLE_HOST ? "A" : "B"); + } if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) { gintsts = dwc2_readl(hsotg, GINTSTS); diff --git a/drivers/usb/dwc2/platform.c b/drivers/usb/dwc2/platform.c index c8f18f3..e6a7fc0 100644 --- a/drivers/usb/dwc2/platform.c +++ b/drivers/usb/dwc2/platform.c @@ -748,10 +748,12 @@ static int __maybe_unused dwc2_resume(struct device *dev) spin_unlock_irqrestore(&dwc2->lock, flags); } - /* Need to restore FORCEDEVMODE/FORCEHOSTMODE */ - dwc2_force_dr_mode(dwc2); - - dwc2_drd_resume(dwc2); + if (!dwc2->role_sw) { + /* Need to restore FORCEDEVMODE/FORCEHOSTMODE */ + dwc2_force_dr_mode(dwc2); + } else { + dwc2_drd_resume(dwc2); + } if (dwc2_is_device_mode(dwc2)) ret = dwc2_hsotg_resume(dwc2);