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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E0869C38A02 for ; Sun, 30 Oct 2022 21:27:12 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4A668853D2; Sun, 30 Oct 2022 22:27:11 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="WiHO0BNq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8AE0D853CC; Sun, 30 Oct 2022 22:27:09 +0100 (CET) Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9E599853C9 for ; Sun, 30 Oct 2022 22:27:06 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qt1-x82e.google.com with SMTP id e15so2584583qts.1 for ; Sun, 30 Oct 2022 14:27:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=nc8eYwrd0R/OsAbd/Mh0CQnoy0hsKeYjmu5XFdgeBxU=; b=WiHO0BNql81xVxGKq8jULlJxjrrEo1v6vLGdSYIz6fK9nmcXT9pKNxmPXE2Ai9Q6KY FdWTeE0M0H61cp7R9t2kLrNkdbufbY0e7c8maB0cFNgw9k1dbMYrhKnrjIKACLkKXUde bAXKKGoexIwMx9lDJqfcK8b7rtPiRsFn128LRLjfbMqQjbCXYwyL02LuOwhNkWGmZTvp J7QhbUPSxXg/PHFR6EMOvA0frBmPD49fARguDrvNkuasHWfKJWoa7bxOOdDNyXHkdx/W giTA3PWCN3gP0yBEZlojkcYWLPZx/V6NjtfMPdMs0CtVpi4MCahW3UdyOYUkYAdt82OS gz/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nc8eYwrd0R/OsAbd/Mh0CQnoy0hsKeYjmu5XFdgeBxU=; b=PVnQd33WMK+z8VUt4LD/bDZwcKPD67lcGSdGWh10s349c4Z78yLqI8PkOi54/3dinV zWjiqPFTIHvCFRogwlK4jLFPxhO0dgz4bil30gSiPa3y/0QDPoTPf+Jue/w6LXDdhRkT 1wTsBAMxINzjuvrugmNWKopYPwjFxmKmGXPcPIOfkd31r1Sytjrv5OqJaFD9He6QhiD3 6W2RWp6by2hWlGFFBtQZNKstaPood+igP+n0eWuEDho/G4/bXB0ob4/HZ9defz1CMCjT O73WIaZaZdwZfoRopIigkY7NStWKfQPPYGnLXTlxt/xSKR9URo70gyKA5u/yypjGIaLF kEAg== X-Gm-Message-State: ACrzQf3C5MHHW8UnXmA45qAnao/2ASOKUeOUwce1lkqVutdhQ2/b44kS wYbvZIehAFzBzwZan3J9RUI= X-Google-Smtp-Source: AMsMyM7GbPXB4NCLiNyAqlyV5dpe54vbtkVf9rXAn6d+t7LdwsGL+IXbfqocdabpl6UN0fhghArWwQ== X-Received: by 2002:ac8:5356:0:b0:3a5:2751:ced6 with SMTP id d22-20020ac85356000000b003a52751ced6mr696812qto.473.1667165225434; Sun, 30 Oct 2022 14:27:05 -0700 (PDT) Received: from [192.168.1.201] (pool-173-73-95-180.washdc.fios.verizon.net. [173.73.95.180]) by smtp.gmail.com with ESMTPSA id d17-20020a05620a241100b006ce7d9dea7asm2828056qkn.13.2022.10.30.14.27.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 30 Oct 2022 14:27:05 -0700 (PDT) Message-ID: <36e8f068-df5a-3385-ba89-b83afb40dba3@gmail.com> Date: Sun, 30 Oct 2022 17:27:04 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: [PATCH 4/4] usb: dwc3: Program GFLADJ Content-Language: en-US To: Marek Vasut , u-boot@lists.denx.de Cc: Sean Anderson , Angus Ainslie , Bin Meng , Fabio Estevam , Kunihiko Hayashi , Michal Simek , Peng Fan , Stefano Babic References: <20221030210948.344661-1-marex@denx.de> <20221030210948.344661-4-marex@denx.de> From: Sean Anderson In-Reply-To: <20221030210948.344661-4-marex@denx.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On 10/30/22 17:09, Marek Vasut wrote: > From: Sean Anderson > > GUCTL.REFCLKPER can only account for clock frequencies with integer > periods. To address this, program REFCLK_FLADJ with the relative error > caused by period truncation. The formula given in the register reference > has been rearranged to allow calculation based on rate (instead of > period), and to allow for fixed-point arithmetic. > > Additionally, calculate a value for 240MHZDECR. This configures a > simulated 240Mhz clock using a counter with one fractional bit (PLS1). > > This register is programmed only for versions >= 2.50a, since this is > the check also used by commit db2be4e9e30c ("usb: dwc3: Add frame length > adjustment quirk"). > > [ marek: Ported from Linux kernel commit > 596c87856e08d ("usb: dwc3: Program GFLADJ") ] > > Signed-off-by: Sean Anderson > Signed-off-by: Marek Vasut # Port from Linux > --- > Cc: Angus Ainslie > Cc: Bin Meng > Cc: Fabio Estevam > Cc: Kunihiko Hayashi > Cc: Michal Simek > Cc: Peng Fan > Cc: Sean Anderson > Cc: Stefano Babic > --- > drivers/usb/dwc3/core.c | 41 +++++++++++++++++++++++++++++++++++++++++ > drivers/usb/dwc3/core.h | 3 +++ > 2 files changed, 44 insertions(+) > > diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c > index cf66976a088..fcd062dc898 100644 > --- a/drivers/usb/dwc3/core.c > +++ b/drivers/usb/dwc3/core.c > @@ -29,6 +29,7 @@ > #include > #include > #include > +#include > > #include "core.h" > #include "gadget.h" > @@ -36,6 +37,8 @@ > > #include "linux-compat.h" > > +#define NSEC_PER_SEC 1000000000L > + > static LIST_HEAD(dwc3_list); > /* -------------------------------------------------------------------------- */ > > @@ -127,6 +130,8 @@ static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj) > static void dwc3_ref_clk_period(struct dwc3 *dwc) > { > unsigned long period; > + unsigned long fladj; > + unsigned long decr; > unsigned long rate; > u32 reg; > > @@ -137,6 +142,7 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc) > period = NSEC_PER_SEC / rate; > } else if (dwc->ref_clk_per) { > period = dwc->ref_clk_per; > + rate = NSEC_PER_SEC / period; > } else { > return; > } > @@ -145,6 +151,41 @@ static void dwc3_ref_clk_period(struct dwc3 *dwc) > reg &= ~DWC3_GUCTL_REFCLKPER_MASK; > reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period); > dwc3_writel(dwc->regs, DWC3_GUCTL, reg); > + > + if (dwc->revision <= DWC3_REVISION_250A) > + return; > + > + /* > + * The calculation below is > + * > + * 125000 * (NSEC_PER_SEC / (rate * period) - 1) > + * > + * but rearranged for fixed-point arithmetic. The division must be > + * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and > + * neither does rate * period). > + * > + * Note that rate * period ~= NSEC_PER_SECOND, minus the number of > + * nanoseconds of error caused by the truncation which happened during > + * the division when calculating rate or period (whichever one was > + * derived from the other). We first calculate the relative error, then > + * scale it to units of 8 ppm. > + */ > + fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period); > + fladj -= 125000; > + > + /* > + * The documented 240MHz constant is scaled by 2 to get PLS1 as well. > + */ > + decr = 480000000 / rate; > + > + reg = dwc3_readl(dwc->regs, DWC3_GFLADJ); > + reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK > + & ~DWC3_GFLADJ_240MHZDECR > + & ~DWC3_GFLADJ_240MHZDECR_PLS1; > + reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj) > + | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1) > + | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1); > + dwc3_writel(dwc->regs, DWC3_GFLADJ, reg); > } > > /** > diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h > index b4a7d9e52bc..532746dd88d 100644 > --- a/drivers/usb/dwc3/core.h > +++ b/drivers/usb/dwc3/core.h > @@ -248,6 +248,9 @@ > /* Global Frame Length Adjustment Register */ > #define DWC3_GFLADJ_30MHZ_SDBND_SEL (1 << 7) > #define DWC3_GFLADJ_30MHZ_MASK 0x3f > +#define DWC3_GFLADJ_REFCLK_FLADJ_MASK GENMASK(21, 8) > +#define DWC3_GFLADJ_240MHZDECR GENMASK(30, 24) > +#define DWC3_GFLADJ_240MHZDECR_PLS1 BIT(31) > > /* Global User Control Register*/ > #define DWC3_GUCTL_REFCLKPER_MASK 0xffc00000 Reviewed-by: Sean Anderson