From patchwork Thu Apr 15 22:20:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thinh Nguyen X-Patchwork-Id: 421800 Delivered-To: patch@linaro.org Received: by 2002:a02:6a6f:0:0:0:0:0 with SMTP id m47csp864545jaf; Thu, 15 Apr 2021 15:20:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKVWcltDDsrFMXobbd68oakwBln3s8bVv45nsudC8rtoWVgYfnMD5HlMercmi9ldVgWYNe X-Received: by 2002:a17:902:b105:b029:eb:6c72:f95c with SMTP id q5-20020a170902b105b02900eb6c72f95cmr6090281plr.58.1618525242808; Thu, 15 Apr 2021 15:20:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618525242; cv=none; d=google.com; s=arc-20160816; b=ZSECSNJKRqUZzZ+K0kcBG9KgrxlPx+EImKfowFiK9MrZ+KGMIhkWXJXC0AZ5tg9i6a Xg65uyzVfryzBpSq5nXiWDs2ApuMk4P/NyA0bBoKSxnO9OM3I/1CCMQiwBaahPKhXZva fTrbbMOvXxdWBhT6pta/AW82eOeig7QWd4TC0UX8fYfNU/BxQPcwZlk8Zc+OIZbAa6Cr u1ciL74H1+7hBzqfO8UYfQMhyMUIPwFMBAWb6Ri3t23N4G18s7QNfiXPaOgbhsM7rnt7 IsRKzPA3bUA+2TCJ9/2BAns8dE02MJIzn31yJxrmA4tCqVbT4jg5Rt3iAuW91/kpQ3jm DLdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:from:references:in-reply-to :message-id:date:dkim-signature; bh=5wyeTxL93gVO0d7WJ/DmC1kxk47koCjIwzybBAM5KZc=; b=z7zEFUrGXDNoE1g/FMoZRjRJL+f+r9Wq00bO1mz4e7/G5kMz/R5V9by59tZDB7cEye TDGt69uHHSLhtEpaSc1BaggnS8thDzuIWltsGbHzUBFU/5ijp37D0/pQq0plwhCxyuRn bkR5uo+P3wyNsupA+1dDvRZJlYFdMBaAw0czjZD4LDmEwnfEB0lEKFo6fJztYaAhmz5f zYG3M3xbMSY+vp5gF8S9piS29Sx3IdwqLM/QfjAwZmsokwtV65B5e3cfYLQL5s2h5Hp1 QWUYfmhvMy8yDAXiddmHYozh89qqiZ7SSTOuM5zWUvv5w1vLZZCQo8H7bZtD+A5g+RVO Eh1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@synopsys.com header.s=mail header.b=KwA8p+4s; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=synopsys.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l125si4361319pfd.126.2021.04.15.15.20.42; Thu, 15 Apr 2021 15:20:42 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@synopsys.com header.s=mail header.b=KwA8p+4s; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=synopsys.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233361AbhDOWVC (ORCPT + 12 others); Thu, 15 Apr 2021 18:21:02 -0400 Received: from smtprelay-out1.synopsys.com ([149.117.87.133]:39984 "EHLO smtprelay-out1.synopsys.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235639AbhDOWVA (ORCPT ); Thu, 15 Apr 2021 18:21:00 -0400 Received: from mailhost.synopsys.com (sv2-mailhost1.synopsys.com [10.205.2.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by smtprelay-out1.synopsys.com (Postfix) with ESMTPS id 25A4AC014A; Thu, 15 Apr 2021 22:20:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=synopsys.com; s=mail; t=1618525232; bh=5HQDNpeNZRUoMYiIW1mV/yiCDuYVHIZnXagUKcQyFm0=; h=Date:In-Reply-To:References:From:Subject:To:Cc:From; b=KwA8p+4sqjU5kZwz7bIVb/kddvEtBNVC91u+uub3CDd4kg94NEfHRQXaIZ/QPue/5 UYv/QduBVGQN2Skgi8DoSsAmxTPK+eP56ivT7BlY20fID8hTbNg89aYO8AO4BCL91g xwnQjCS4gotd9tFQ+Wc74SK5WCzZEDg/jtYkcfQcbkX8W3ytI+C9iSHml4w793tZGB 09JQJa0GwCsqKc9avblLXnK6D6jO3HkoderHvDAErutiuFSrsdWeY2eqLExRL5/WlQ swYi//Aq/XMJ2mKZqxXm/5sJ1RMLIzX7Xf4/6zyXRIAEvEDnvoQbo0DrZECkUtOoBR xtt4uZszjP5rA== Received: from lab-vbox (unknown [10.205.138.63]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mailhost.synopsys.com (Postfix) with ESMTPSA id 50D78A0096; Thu, 15 Apr 2021 22:20:30 +0000 (UTC) Received: by lab-vbox (sSMTP sendmail emulation); Thu, 15 Apr 2021 15:20:30 -0700 Date: Thu, 15 Apr 2021 15:20:30 -0700 Message-Id: <374440f8dcd4f06c02c2caf4b1efde86774e02d9.1618521663.git.Thinh.Nguyen@synopsys.com> In-Reply-To: <2cb4e704b059a8cc91f37081c8ceb95c6492e416.1618503587.git.Thinh.Nguyen@synopsys.com> References: <2cb4e704b059a8cc91f37081c8ceb95c6492e416.1618503587.git.Thinh.Nguyen@synopsys.com> X-SNPS-Relay: synopsys.com From: Thinh Nguyen Subject: [PATCH v3] usb: dwc3: core: Do core softreset when switch mode To: Felipe Balbi , Greg Kroah-Hartman , linux-usb@vger.kernel.org Cc: John Youn , , Andy Shevchenko , John Stultz , Wesley Cheng , Ferry Toth , Yu Chen Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Yu Chen From: John Stultz According to the programming guide, to switch mode for DRD controller, the driver needs to do the following. To switch from device to host: 1. Reset controller with GCTL.CoreSoftReset 2. Set GCTL.PrtCapDir(host mode) 3. Reset the host with USBCMD.HCRESET 4. Then follow up with the initializing host registers sequence To switch from host to device: 1. Reset controller with GCTL.CoreSoftReset 2. Set GCTL.PrtCapDir(device mode) 3. Reset the device with DCTL.CSftRst 4. Then follow up with the initializing registers sequence Currently we're missing step 1) to do GCTL.CoreSoftReset and step 3) of switching from host to device. John Stult reported a lockup issue seen with HiKey960 platform without these steps[1]. Similar issue is observed with Ferry's testing platform[2]. So, apply the required steps along with some fixes to Yu Chen's and John Stultz's version. The main fixes to their versions are the missing wait for clocks synchronization before clearing GCTL.CoreSoftReset and only apply DCTL.CSftRst when switching from host to device. [1] https://lore.kernel.org/linux-usb/20210108015115.27920-1-john.stultz@linaro.org/ [2] https://lore.kernel.org/linux-usb/0ba7a6ba-e6a7-9cd4-0695-64fc927e01f1@gmail.com/ Cc: Andy Shevchenko Cc: Ferry Toth Cc: Wesley Cheng Cc: Fixes: 41ce1456e1db ("usb: dwc3: core: make dwc3_set_mode() work properly") Signed-off-by: Yu Chen Signed-off-by: John Stultz Signed-off-by: Thinh Nguyen --- Changes in v3: - Check if the desired mode is OTG, then keep the old flow - Remove condition for OTG support only since the device can still be configured DRD host/device mode only - Remove redundant hw_mode check since __dwc3_set_mode() only applies when hw_mode is DRD Changes in v2: - Initialize mutex per device and not as global mutex. - Add additional checks for DRD only mode drivers/usb/dwc3/core.c | 27 +++++++++++++++++++++++++++ drivers/usb/dwc3/core.h | 5 +++++ 2 files changed, 32 insertions(+) base-commit: 4b853c236c7b5161a2e444bd8b3c76fe5aa5ddcb -- 2.28.0 Tested-by: John Stultz Tested-by: Wesley Cheng diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index 5c25e6a72dbd..2f118ad43571 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -114,6 +114,8 @@ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode) dwc->current_dr_role = mode; } +static int dwc3_core_soft_reset(struct dwc3 *dwc); + static void __dwc3_set_mode(struct work_struct *work) { struct dwc3 *dwc = work_to_dwc(work); @@ -121,6 +123,8 @@ static void __dwc3_set_mode(struct work_struct *work) int ret; u32 reg; + mutex_lock(&dwc->mutex); + pm_runtime_get_sync(dwc->dev); if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG) @@ -154,6 +158,25 @@ static void __dwc3_set_mode(struct work_struct *work) break; } + /* For DRD host or device mode only */ + if (dwc->desired_dr_role != DWC3_GCTL_PRTCAP_OTG) { + reg = dwc3_readl(dwc->regs, DWC3_GCTL); + reg |= DWC3_GCTL_CORESOFTRESET; + dwc3_writel(dwc->regs, DWC3_GCTL, reg); + + /* + * Wait for internal clocks to synchronized. DWC_usb31 and + * DWC_usb32 may need at least 50ms (less for DWC_usb3). To + * keep it consistent across different IPs, let's wait up to + * 100ms before clearing GCTL.CORESOFTRESET. + */ + msleep(100); + + reg = dwc3_readl(dwc->regs, DWC3_GCTL); + reg &= ~DWC3_GCTL_CORESOFTRESET; + dwc3_writel(dwc->regs, DWC3_GCTL, reg); + } + spin_lock_irqsave(&dwc->lock, flags); dwc3_set_prtcap(dwc, dwc->desired_dr_role); @@ -178,6 +201,8 @@ static void __dwc3_set_mode(struct work_struct *work) } break; case DWC3_GCTL_PRTCAP_DEVICE: + dwc3_core_soft_reset(dwc); + dwc3_event_buffers_setup(dwc); if (dwc->usb2_phy) @@ -200,6 +225,7 @@ static void __dwc3_set_mode(struct work_struct *work) out: pm_runtime_mark_last_busy(dwc->dev); pm_runtime_put_autosuspend(dwc->dev); + mutex_unlock(&dwc->mutex); } void dwc3_set_mode(struct dwc3 *dwc, u32 mode) @@ -1553,6 +1579,7 @@ static int dwc3_probe(struct platform_device *pdev) dwc3_cache_hwparams(dwc); spin_lock_init(&dwc->lock); + mutex_init(&dwc->mutex); pm_runtime_set_active(dev); pm_runtime_use_autosuspend(dev); diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index 695ff2d791e4..7e3afa5378e8 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -947,6 +948,7 @@ struct dwc3_scratchpad_array { * @scratch_addr: dma address of scratchbuf * @ep0_in_setup: one control transfer is completed and enter setup phase * @lock: for synchronizing + * @mutex: for mode switching * @dev: pointer to our struct device * @sysdev: pointer to the DMA-capable device * @xhci: pointer to our xHCI child @@ -1088,6 +1090,9 @@ struct dwc3 { /* device lock */ spinlock_t lock; + /* mode switching lock */ + struct mutex mutex; + struct device *dev; struct device *sysdev;