Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1578694pxb; Mon, 22 Feb 2021 05:52:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUh6wdCg9RU72ElhI3Sim5M43/NNYyuIZoOT6Ly7jV8zWzNyNVoEuPJgsWyncEFz/d9fjL X-Received: by 2002:a05:6402:424a:: with SMTP id g10mr22477255edb.236.1614001964781; Mon, 22 Feb 2021 05:52:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614001964; cv=none; d=google.com; s=arc-20160816; b=RblAY05tN9ar03DyrFYa4XZulhhGC0IIwEFBMyU3VAKqGXnGIdqdAQ9QwBBVCv9RZv UP/ILm1nU5QPvZXhSAKlVVsYvpJK52Q6WZePM71WB29bjH4p2c823b9JqwxWUgpQ/6Lo GXf45/9zn3Z1LeSMeYwGjXxBekjT7B1TcyRZW+LuUi/UBynWljy74i4CFe7mzKMYtjZs LyT18vfearfAY5g4CqaVZWP7Kv4r/t5U03mfOovpKMgy4oYnYLhRjuuLBJAyuGdEx60E vEr7Wd72LxL+hn1KSl/adoN3uUbqOI2MPZIiPp0z6LKtyCjiLPEOIE/iyQgWT6TAObiB EIfA== 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=VvrQY7M/uEPPes+gX80JCoP7xddfD/VhQTF+MgxCevc=; b=LNW352rl26L0lsAavWfdU4FJcWL+3yNaD/hqGm99TI2XfyJjAGOSJat2Lj1RaJog0o ajB4DTVbewB+fXjvOQLheMXFbeMQ2sog4Y/Fhkswvrg8/LL/QltbtgD78k7k71IQB2Ys SXvX/sJrUisuXl98EeKnxPJW9fFWZroSwQ2VurG/k/PGOnZxRybYsuiLrViPJSrNdiYM mkiT6+m/pgqpCluMKrMu0iiQlAbFc9YTZZTtYbzM+4RhgTCx3Ti8Bo1sVKk4jT7N0oA0 EW1kl201GPiAosjEvu9Yh51sKV0aLCjbxYQ1ZDlXe3vevhBRY/OdqU+OqPJYXmcRnJLE NYhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=J9WekR8t; 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 s5si4545633edw.463.2021.02.22.05.52.22; Mon, 22 Feb 2021 05:52:44 -0800 (PST) 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=J9WekR8t; 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 S231341AbhBVNtN (ORCPT + 99 others); Mon, 22 Feb 2021 08:49:13 -0500 Received: from mail.kernel.org ([198.145.29.99]:57120 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231292AbhBVMpK (ORCPT ); Mon, 22 Feb 2021 07:45:10 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9967464F0B; Mon, 22 Feb 2021 12:41:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1613997697; bh=JfVYjEi0zIlpP6xg5PEVzrHOmDViZ2UvhwrbqBwjkHM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J9WekR8tcQcmzhI2JUrgZ/Uzq77WQORV28fzMklLwNAzD32V3fT7PMzmRfqt2NzUA A78qVGSGoe0l6Yy/wAJZI3wnbGP9etKAJ2lyX5LSZbo/M+iuZA0DULY5NC2YjwAUUo 4ufJWr0MnOify1gg0DLgtj1kuKUvdir8TAdV+KJg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Heikki Krogerus , Serge Semin , Sudip Mukherjee Subject: [PATCH 4.9 30/49] usb: dwc3: ulpi: Replace CPU-based busyloop with Protocol-based one Date: Mon, 22 Feb 2021 13:36:28 +0100 Message-Id: <20210222121027.167570999@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210222121022.546148341@linuxfoundation.org> References: <20210222121022.546148341@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: Serge Semin commit fca3f138105727c3a22edda32d02f91ce1bf11c9 upstream Originally the procedure of the ULPI transaction finish detection has been developed as a simple busy-loop with just decrementing counter and no delays. It's wrong since on different systems the loop will take a different time to complete. So if the system bus and CPU are fast enough to overtake the ULPI bus and the companion PHY reaction, then we'll get to take a false timeout error. Fix this by converting the busy-loop procedure to take the standard bus speed, address value and the registers access mode into account for the busy-loop delay calculation. Here is the way the fix works. It's known that the ULPI bus is clocked with 60MHz signal. In accordance with [1] the ULPI bus protocol is created so to spend 5 and 6 clock periods for immediate register write and read operations respectively, and 6 and 7 clock periods - for the extended register writes and reads. Based on that we can easily pre-calculate the time which will be needed for the controller to perform a requested IO operation. Note we'll still preserve the attempts counter in case if the DWC USB3 controller has got some internals delays. [1] UTMI+ Low Pin Interface (ULPI) Specification, Revision 1.1, October 20, 2004, pp. 30 - 36. Fixes: 88bc9d194ff6 ("usb: dwc3: add ULPI interface support") Acked-by: Heikki Krogerus Signed-off-by: Serge Semin Link: https://lore.kernel.org/r/20201210085008.13264-3-Sergey.Semin@baikalelectronics.ru Cc: stable Signed-off-by: Greg Kroah-Hartman [sudip: adjust context] Signed-off-by: Sudip Mukherjee Signed-off-by: Greg Kroah-Hartman --- drivers/usb/dwc3/ulpi.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) --- a/drivers/usb/dwc3/ulpi.c +++ b/drivers/usb/dwc3/ulpi.c @@ -10,6 +10,8 @@ * published by the Free Software Foundation. */ +#include +#include #include #include "core.h" @@ -20,12 +22,22 @@ DWC3_GUSB2PHYACC_ADDR(ULPI_ACCESS_EXTENDED) | \ DWC3_GUSB2PHYACC_EXTEND_ADDR(a) : DWC3_GUSB2PHYACC_ADDR(a)) -static int dwc3_ulpi_busyloop(struct dwc3 *dwc) +#define DWC3_ULPI_BASE_DELAY DIV_ROUND_UP(NSEC_PER_SEC, 60000000L) + +static int dwc3_ulpi_busyloop(struct dwc3 *dwc, u8 addr, bool read) { + unsigned long ns = 5L * DWC3_ULPI_BASE_DELAY; unsigned int count = 1000; u32 reg; + if (addr >= ULPI_EXT_VENDOR_SPECIFIC) + ns += DWC3_ULPI_BASE_DELAY; + + if (read) + ns += DWC3_ULPI_BASE_DELAY; + while (count--) { + ndelay(ns); reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYACC(0)); if (!(reg & DWC3_GUSB2PHYACC_BUSY)) return 0; @@ -44,7 +56,7 @@ static int dwc3_ulpi_read(struct device reg = DWC3_GUSB2PHYACC_NEWREGREQ | DWC3_ULPI_ADDR(addr); dwc3_writel(dwc->regs, DWC3_GUSB2PHYACC(0), reg); - ret = dwc3_ulpi_busyloop(dwc); + ret = dwc3_ulpi_busyloop(dwc, addr, true); if (ret) return ret; @@ -62,7 +74,7 @@ static int dwc3_ulpi_write(struct device reg |= DWC3_GUSB2PHYACC_WRITE | val; dwc3_writel(dwc->regs, DWC3_GUSB2PHYACC(0), reg); - return dwc3_ulpi_busyloop(dwc); + return dwc3_ulpi_busyloop(dwc, addr, false); } static const struct ulpi_ops dwc3_ulpi_ops = {