Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2490277pxj; Mon, 10 May 2021 04:21:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyCYPUvJ9Xd+bW6ZYtDf5PkrMwRlcvQncsgaYkwp3SoNmUNx6orEZb8yK1r9gC9TnxTSCg4 X-Received: by 2002:a17:906:4a55:: with SMTP id a21mr24355414ejv.215.1620645707006; Mon, 10 May 2021 04:21:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620645706; cv=none; d=google.com; s=arc-20160816; b=TDp1JBbWeug5c6cM7f0a9UxbEvEOwxKtFpdOmmfkte3VqDNhQqMv3Xt6C9MC341jr4 qoPwYJW+FwgNA1SB0OQ5fHZq/l3bi4J5SYBnISAAn5zJy0ROaOe/dV8nuDR598eE+cgO LWlY+So6Cw5KdPg7TTCOQz6MgVeI3Y6Oh/ZyqMDUHl+i8Y0gT4cOc70yQfhl3BwJ7rym aLlaWtzfyHcJckvxdZ07RXzDm1rq1/LKRZ7tZb+WI7Tpe06YSUmStMt5uoOgpv7MnXdY w4EaI3N0Q6mFyTOUDsdcOaolxE8C3w9/nSQKtOgwwmsnGqo/OZf45KUoJsOCaOgyq8qr 0Chg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=FWVdifMnMw/57anaKaXG6pV34j0bSskA44Qe6/qTDJY=; b=bGXPCG7Qltq87YCgRBCiYtOtpUfLiu1FVw44Vh9+j90Thn2MjSDDXFaT0UNcLIymTh NcCCBJ5QTp8HiMQaYCvObd/PiSYeHwDAeaXsAoFb73dB8RR3LUkXGyxViJoaqlPQ17Pw KNXmysjg6yGi33z0thn0sWzH5QFt1aZvzW+43XW9qej5gJewTYBaF8gDcHlzKmIVYDI6 A9nmZ5rA0Eb/ZW/3KZv7Hxf39KxbQjM7Cvjn9QvFK39RvBxWyMltH/5GdcZbLlky65hN y/6HcLuwtowxJSQryzvGw9FPvzFMQQsRYDAVdlrHdUI/4FNvjRYc6z4CoDXMfifI7PI9 JirQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oPsLTiGO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g8si15009161eds.14.2021.05.10.04.21.23; Mon, 10 May 2021 04:21:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=oPsLTiGO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238720AbhEJLSl (ORCPT + 99 others); Mon, 10 May 2021 07:18:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:52744 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233852AbhEJKzb (ORCPT ); Mon, 10 May 2021 06:55:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 534F06197B; Mon, 10 May 2021 10:42:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620643364; bh=VeoYrpX0w42wUk3oct7e9z8D++Z1g/izGdPTLk9JEf8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oPsLTiGO1it+RMYuFH8MQIjXAmlRHrX0aQUzxPgEIEeFJ8zxNz+dOGzcZqZlfUc8w 7vQ5ViolmNDAa1f2I+RwYMztYhR//G7jKlkAEc7J3CYqUnlwBwhoYDMxMwStZ5vj6L rHcfvv0Pky8JF2tEPjVzitokm+TrR9VtRyR1Yt18= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andy Shevchenko , Ferry Toth , Wesley Cheng , John Stultz , Yu Chen , Thinh Nguyen Subject: [PATCH 5.10 286/299] usb: dwc3: core: Do core softreset when switch mode Date: Mon, 10 May 2021 12:21:23 +0200 Message-Id: <20210510102014.367763782@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102004.821838356@linuxfoundation.org> References: <20210510102004.821838356@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yu Chen commit f88359e1588b85cf0e8209ab7d6620085f3441d9 upstream. 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/ Fixes: 41ce1456e1db ("usb: dwc3: core: make dwc3_set_mode() work properly") Cc: Andy Shevchenko Cc: Ferry Toth Cc: Wesley Cheng Cc: Tested-by: John Stultz Tested-by: Wesley Cheng Signed-off-by: Yu Chen Signed-off-by: John Stultz Signed-off-by: Thinh Nguyen Link: https://lore.kernel.org/r/374440f8dcd4f06c02c2caf4b1efde86774e02d9.1618521663.git.Thinh.Nguyen@synopsys.com Signed-off-by: Greg Kroah-Hartman --- drivers/usb/dwc3/core.c | 27 +++++++++++++++++++++++++++ drivers/usb/dwc3/core.h | 5 +++++ 2 files changed, 32 insertions(+) --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -114,6 +114,8 @@ void dwc3_set_prtcap(struct dwc3 *dwc, u 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_ 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_ 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_ } 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_ 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) @@ -1529,6 +1555,7 @@ static int dwc3_probe(struct platform_de dwc3_cache_hwparams(dwc); spin_lock_init(&dwc->lock); + mutex_init(&dwc->mutex); pm_runtime_set_active(dev); pm_runtime_use_autosuspend(dev); --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -942,6 +943,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 @@ -1078,6 +1080,9 @@ struct dwc3 { /* device lock */ spinlock_t lock; + /* mode switching lock */ + struct mutex mutex; + struct device *dev; struct device *sysdev;