From patchwork Wed Dec 20 14:27:58 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dong Aisheng X-Patchwork-Id: 10125791 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5312960245 for ; Wed, 20 Dec 2017 14:28:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F10B291CA for ; Wed, 20 Dec 2017 14:28:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 324D829785; Wed, 20 Dec 2017 14:28:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 44546291CA for ; Wed, 20 Dec 2017 14:28:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=VwyGNDvsNjFLg91G/rXFrHHa5xGOs+GYtdb2USK/Z5Q=; b=Dy49nhl7mqQdIM t4knmiFOfdhbyxpJLdi90oygCDgBlxlXFPZgtQjn/eb8hhudOZxQT34SKRee26QLLmvWAU6F/ZOrO 2c6EENFHPsphdFoOxhg614nWRHFMiBuVtxC9FasGsFymlh76LeQ329wyHB0/V9gbKaXkvWouHI5xf 2iZ6O7gQvJaJaHJ2W50a/VSy8Yq8OqGilKkeWHkZZs3ih2JjNNQXrXdKzwS08tN7wL/a6CrFqFTpt E7gZIizhOYVh0lwBPVGOQisnkCn5+AoWcVQnsmXGkHorXPYtC69c2rUqbKdc7i4IP6o3KCT4kBEk4 +HXLeSZTX4M/D5I/LJpA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1eRfM9-0000ai-BB; Wed, 20 Dec 2017 14:28:25 +0000 Received: from mail-pl0-x243.google.com ([2607:f8b0:400e:c01::243]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1eRfM4-0000YC-3t for linux-arm-kernel@lists.infradead.org; Wed, 20 Dec 2017 14:28:23 +0000 Received: by mail-pl0-x243.google.com with SMTP id i6so9091026plt.13 for ; Wed, 20 Dec 2017 06:28:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=5pdyaQXcYlZ4+3Wd+S7yIPbEdwJKoKWP25WIwiuWCYM=; b=Ra+ubuEFvqlHcmTmIwL8QOJqXQUg9fpQyWAsKtteRGnIZRhME8MIfqoixc1eQhC6AL 2saB2Jy/BzCob4vuLb9qAB1e7uKecZsg9X/ZuWm+3+a9zSId4dlFjNF5PYF/Qg/WF0cU LItCIequ3wmygvtuCr1VjWWCSTE2LgLTRxsOKSVCxIr1VQzu7s2jXPzfNJ26KWUp9UJJ Eg6M5UHwXirJBXKnkws1VUwtUK+fFWtu2IBtF/s1Hw0pkRFBclAiU40vCtVjnPvF0KHH ixTuzilca0hOm5tNSnoN7e9vu1jaLD3o+PxOio2kOOZoIXBT8CixevsQ1h73dEIDRRNp r71Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=5pdyaQXcYlZ4+3Wd+S7yIPbEdwJKoKWP25WIwiuWCYM=; b=gVJhhBNQ8WGaR4SX/TYSRrmpBRQekHjAzHqMIv8uK/DhxZ8AdZWYLDV/0X0Ku14Oxb tyigIp9k9k1R3W7nOdy5ziEdH9EvTF3xr3gUInjditzUiWMQ5cgJ1Zo6IjZwUU4+ZvOR HzWX2KCVOxD2q+O2uuiMJS3cyypLO5/h3ZJAQun749NnUhirX741IClzGvTKaUC0cuZd hMItDjKoUY0kJuH93h1lzMGDB7oK6Eo8UcUTuu6zUnes4wW5SA8T5rgswYk7XXs1Srdw 3jsOJMO0IdbZEGRnVAB4vMBC2OxdD248jSjwWv/mOFZroNrhMeMdahjk86T8SEiK/PRy i1WA== X-Gm-Message-State: AKGB3mIf3vL7fInvL6x151CmwNglMAdBR0DfcXvvFLa3gTUk1jN9yzvJ XbKtHUaBziwOypeIBre4Udg= X-Google-Smtp-Source: ACJfBouXzB5EHg+OyOgIP+0dajzNtOJoTqwT2eLjXydmFOfADI1jm6Fy36DdF+HrjMzkYoENnGG7Zg== X-Received: by 10.84.248.131 with SMTP id q3mr7311738pll.428.1513780088951; Wed, 20 Dec 2017 06:28:08 -0800 (PST) Received: from b29396-OptiPlex-7040 (gate-zmy3.freescale.com. [192.88.167.1]) by smtp.gmail.com with ESMTPSA id x22sm37526060pfa.169.2017.12.20.06.28.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Dec 2017 06:28:06 -0800 (PST) Date: Wed, 20 Dec 2017 22:27:58 +0800 From: Dong Aisheng To: Stephen Boyd Subject: Re: [PATCH V2 01/10] clk: clk-divider: add CLK_DIVIDER_ZERO_GATE clk support Message-ID: <20171220142757.GB30461@b29396-OptiPlex-7040> References: <1499946435-7177-1-git-send-email-aisheng.dong@nxp.com> <1499946435-7177-2-git-send-email-aisheng.dong@nxp.com> <20171102075039.GU30645@codeaurora.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20171102075039.GU30645@codeaurora.org> User-Agent: Mutt/1.5.24 (2015-08-30) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171220_062820_259576_E676AF23 X-CRM114-Status: GOOD ( 31.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Dong Aisheng , ping.bai@nxp.com, Anson.Huang@nxp.com, mturquette@baylibre.com, linux-kernel@vger.kernel.org, shawnguo@kernel.org, linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP On Thu, Nov 02, 2017 at 12:50:39AM -0700, Stephen Boyd wrote: > On 07/13, Dong Aisheng wrote: > > diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c > > index 9bb472c..55f8c41 100644 > > --- a/drivers/clk/clk-divider.c > > +++ b/drivers/clk/clk-divider.c > > @@ -123,6 +123,9 @@ unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, > > struct clk_divider *divider = to_clk_divider(hw); > > unsigned int div; > > > > + if (flags & CLK_DIVIDER_ZERO_GATE && !val) > > + return 0; > > + > > div = _get_div(table, val, flags, divider->width); > > if (!div) { > > WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), > > @@ -141,8 +144,13 @@ static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, > > struct clk_divider *divider = to_clk_divider(hw); > > unsigned int val; > > > > - val = clk_readl(divider->reg) >> divider->shift; > > - val &= div_mask(divider->width); > > + if ((divider->flags & CLK_DIVIDER_ZERO_GATE) && > > + !clk_hw_is_enabled(hw)) { > > This seems racy. Are we holding the register lock here? > Would you please clarify what type of racy you mean? Currently it only protects register write between set_rate and enable/disable, and other register read are not protected. e.g. in recalc_rate and is_enabled. And i did see similar users, e.g. drivers/clk/sunxi-ng/ccu_mult.c Should we still need protect them here? > > + val = divider->cached_val; > > + } else { > > + val = clk_readl(divider->reg) >> divider->shift; > > + val &= div_mask(divider->width); > > + } > > > > return divider_recalc_rate(hw, parent_rate, val, divider->table, > > divider->flags); > > @@ -392,6 +400,12 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, > > value = divider_get_val(rate, parent_rate, divider->table, > > divider->width, divider->flags); > > > > + if ((divider->flags & CLK_DIVIDER_ZERO_GATE) && > > + !clk_hw_is_enabled(hw)) { > > Same racy comment here. > > > + divider->cached_val = value; > > + return 0; > > + } > > + > > if (divider->lock) > > spin_lock_irqsave(divider->lock, flags); > > else > > @@ -414,10 +428,85 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, > > return 0; > > } > > > > +static int clk_divider_enable(struct clk_hw *hw) > > +{ > > + struct clk_divider *divider = to_clk_divider(hw); > > + unsigned long flags = 0; > > + u32 val; > > + > > + if (!(divider->flags & CLK_DIVIDER_ZERO_GATE)) > > + return 0; > > This is not good. We will always jump to these functions on > enable/disable for a divider although 99.9% of all dividers that > exist won't need to run this code at all. > I absolutely understand this concern. > Can you please move this logic into your own divider > implementation? The flag can be added to the generic layer if > necessary but I'd prefer to see this logic kept in the driver > that uses it. If we get more than one driver doing the cached > divider thing then we can think about moving it to the more > generic place like here, but for now we should be able to keep > this contained away from the basic types and handled by the > quirky driver that needs it. > If only for above issue, how about invent a clk_divider_gate_ops to separate the users of normal divider and zero gate divider: Anyway, if you still think it's not proper, i can put it in platform driver as you wish, just in the cost of a few duplicated codes. > > + > > + if (!divider->cached_val) { > > + pr_err("%s: no valid preset rate\n", clk_hw_get_name(hw)); > > + return -EINVAL; > > + } > > + > > + if (divider->lock) > > + spin_lock_irqsave(divider->lock, flags); > > + else > > + __acquire(divider->lock); > > + > > + /* restore div val */ > > + val = clk_readl(divider->reg); > > + val |= divider->cached_val << divider->shift; > > + clk_writel(val, divider->reg); > > + > > + if (divider->lock) > > + spin_unlock_irqrestore(divider->lock, flags); > > + else > > + __release(divider->lock); > > + > > + return 0; > > +} > > + > > +static void clk_divider_disable(struct clk_hw *hw) > > +{ > > + struct clk_divider *divider = to_clk_divider(hw); > > + unsigned long flags = 0; > > + u32 val; > > + > > + if (!(divider->flags & CLK_DIVIDER_ZERO_GATE)) > > + return; > > + > > + if (divider->lock) > > + spin_lock_irqsave(divider->lock, flags); > > + else > > + __acquire(divider->lock); > > + > > + /* store the current div val */ > > + val = clk_readl(divider->reg) >> divider->shift; > > + val &= div_mask(divider->width); > > + divider->cached_val = val; > > + clk_writel(0, divider->reg); > > + > > + if (divider->lock) > > + spin_unlock_irqrestore(divider->lock, flags); > > + else > > + __release(divider->lock); > > +} > > + > > +static int clk_divider_is_enabled(struct clk_hw *hw) > > +{ > > + struct clk_divider *divider = to_clk_divider(hw); > > + u32 val; > > + > > + if (!(divider->flags & CLK_DIVIDER_ZERO_GATE)) > > + return __clk_get_enable_count(hw->clk); > > The plan was to delete this API once OMAP stopped using it. > clk_hw_is_enabled() doesn't work? No, it did not work before because clk_hw_is_enabled will result in the dead loop by calling .is_enabled() callback again. That's why __clk_get_enable_count is used instead. However, with above new patch method, this issue was gone. > > > + > > + val = clk_readl(divider->reg) >> divider->shift; > > + val &= div_mask(divider->width); > > + > > + return val ? 1 : 0; > > +} > > + > > const struct clk_ops clk_divider_ops = { > > .recalc_rate = clk_divider_recalc_rate, > > .round_rate = clk_divider_round_rate, > > .set_rate = clk_divider_set_rate, > > + .enable = clk_divider_enable, > > + .disable = clk_divider_disable, > > + .is_enabled = clk_divider_is_enabled, > > }; > > EXPORT_SYMBOL_GPL(clk_divider_ops); > > > > @@ -436,6 +525,7 @@ static struct clk_hw *_register_divider(struct device *dev, const char *name, > > struct clk_divider *div; > > struct clk_hw *hw; > > struct clk_init_data init; > > + u32 val; > > int ret; > > > > if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { > > @@ -468,6 +558,12 @@ static struct clk_hw *_register_divider(struct device *dev, const char *name, > > div->hw.init = &init; > > div->table = table; > > > > + if (div->flags & CLK_DIVIDER_ZERO_GATE) { > > + val = clk_readl(reg) >> shift; > > + val &= div_mask(width); > > + div->cached_val = val; > > + } > > What if it isn't on? Setting cached_val to 0 is ok? > If it isn't on, then the cache_val should be 0. And recalc_rate will catch this case and return 0 as there's no proper pre-set rate. Regards Dong Aisheng > > + > > /* register the clock */ > > hw = &div->hw; > > ret = clk_hw_register(dev, hw); > > -- > Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, > a Linux Foundation Collaborative Project diff --git a/drivers/clk/clk-divider.c b/drivers/clk/clk-divider.c index 4ed516c..b51f3f9 100644 --- a/drivers/clk/clk-divider.c +++ b/drivers/clk/clk-divider.c @@ -125,6 +125,9 @@ unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate, div = _get_div(table, val, flags, divider->width); if (!div) { + if (flags & CLK_DIVIDER_ZERO_GATE) + return 0; + WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO), "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n", clk_hw_get_name(hw)); @@ -148,6 +151,23 @@ static unsigned long clk_divider_recalc_rate(struct clk_hw *hw, divider->flags); } +static unsigned long clk_divider_gate_recalc_rate(struct clk_hw *hw, + unsigned long parent_rate) +{ + struct clk_divider *divider = to_clk_divider(hw); + unsigned int val; + + if (!clk_hw_is_enabled(hw)) { + val = divider->cached_val; + } else { + val = clk_readl(divider->reg) >> divider->shift; + val &= div_mask(divider->width); + } + + return divider_recalc_rate(hw, parent_rate, val, divider->table, + divider->flags); +} + static bool _is_valid_table_div(const struct clk_div_table *table, unsigned int div) { @@ -416,6 +436,89 @@ static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate, return 0; } +static int clk_divider_gate_set_rate(struct clk_hw *hw, unsigned long rate, + unsigned long parent_rate) +{ + struct clk_divider *divider = to_clk_divider(hw); + int value; + + if (!clk_hw_is_enabled(hw)) { + value = divider_get_val(rate, parent_rate, divider->table, + divider->width, divider->flags); + if (value < 0) + return value; + + divider->cached_val = value; + + return 0; + } + + return clk_divider_set_rate(hw, rate, parent_rate); +} + +static int clk_divider_enable(struct clk_hw *hw) +{ + struct clk_divider *divider = to_clk_divider(hw); + unsigned long uninitialized_var(flags); + u32 val; + + if (!divider->cached_val) { + pr_err("%s: no valid preset rate\n", clk_hw_get_name(hw)); + return -EINVAL; + } + + if (divider->lock) + spin_lock_irqsave(divider->lock, flags); + else + __acquire(divider->lock); + + /* restore div val */ + val = clk_readl(divider->reg); + val |= divider->cached_val << divider->shift; + clk_writel(val, divider->reg); + + if (divider->lock) + spin_unlock_irqrestore(divider->lock, flags); + else + __release(divider->lock); + + return 0; +} + +static void clk_divider_disable(struct clk_hw *hw) +{ + struct clk_divider *divider = to_clk_divider(hw); + unsigned long uninitialized_var(flags); + u32 val; + + if (divider->lock) + spin_lock_irqsave(divider->lock, flags); + else + __acquire(divider->lock); + + /* store the current div val */ + val = clk_readl(divider->reg) >> divider->shift; + val &= div_mask(divider->width); + divider->cached_val = val; + clk_writel(0, divider->reg); + + if (divider->lock) + spin_unlock_irqrestore(divider->lock, flags); + else + __release(divider->lock); +} + +static int clk_divider_is_enabled(struct clk_hw *hw) +{ + struct clk_divider *divider = to_clk_divider(hw); + u32 val; + + val = clk_readl(divider->reg) >> divider->shift; + val &= div_mask(divider->width); + + return val ? 1 : 0; +} + const struct clk_ops clk_divider_ops = { .recalc_rate = clk_divider_recalc_rate, .round_rate = clk_divider_round_rate, @@ -423,6 +526,16 @@ const struct clk_ops clk_divider_ops = { }; EXPORT_SYMBOL_GPL(clk_divider_ops); +const struct clk_ops clk_divider_gate_ops = { + .recalc_rate = clk_divider_gate_recalc_rate, + .round_rate = clk_divider_round_rate, + .set_rate = clk_divider_gate_set_rate, + .enable = clk_divider_enable, + .disable = clk_divider_disable, + .is_enabled = clk_divider_is_enabled, +}; +EXPORT_SYMBOL_GPL(clk_divider_gate_ops); + const struct clk_ops clk_divider_ro_ops = { .recalc_rate = clk_divider_recalc_rate, .round_rate = clk_divider_round_rate, @@ -438,6 +551,7 @@ static struct clk_hw *_register_divider(struct device *dev, const char *name, struct clk_divider *div; struct clk_hw *hw; struct clk_init_data init; + u32 val; int ret; if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) { @@ -455,6 +569,8 @@ static struct clk_hw *_register_divider(struct device *dev, const char *name, init.name = name; if (clk_divider_flags & CLK_DIVIDER_READ_ONLY) init.ops = &clk_divider_ro_ops; + else if (clk_divider_flags & CLK_DIVIDER_ZERO_GATE) + init.ops = &clk_divider_gate_ops; else init.ops = &clk_divider_ops; init.flags = flags | CLK_IS_BASIC; @@ -470,6 +586,12 @@ static struct clk_hw *_register_divider(struct device *dev, const char *name, div->hw.init = &init; div->table = table; + if (div->flags & CLK_DIVIDER_ZERO_GATE) { + val = clk_readl(reg) >> shift; + val &= div_mask(width); + div->cached_val = val; + } + /* register the clock */ hw = &div->hw; ret = clk_hw_register(dev, hw); diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h index 7c925e6..5f33b73 100644 --- a/include/linux/clk-provider.h +++ b/include/linux/clk-provider.h @@ -358,6 +358,7 @@ struct clk_div_table { * @shift: shift to the divider bit field * @width: width of the divider bit field * @table: array of value/divider pairs, last entry should have div = 0 + * @cached_val: cached div hw value used for CLK_DIVIDER_ZERO_GATE * @lock: register lock * * Clock with an adjustable divider affecting its output frequency. Implements @@ -386,6 +387,12 @@ struct clk_div_table { * CLK_DIVIDER_MAX_AT_ZERO - For dividers which are like CLK_DIVIDER_ONE_BASED * except when the value read from the register is zero, the divisor is * 2^width of the field. + * CLK_DIVIDER_ZERO_GATE - For dividers which are like CLK_DIVIDER_ONE_BASED + * when the value read from the register is zero, it means the divisor + * is gated. For this case, the cached_val will be used to store the + * intermediate div for the normal rate operation, like set_rate/get_rate/ + * recalc_rate. When the divider is ungated, the driver will actually + * program the hardware to have the requested divider value. */ struct clk_divider { struct clk_hw hw; @@ -394,6 +401,7 @@ struct clk_divider { u8 width; u8 flags; const struct clk_div_table *table; + u32 cached_val; spinlock_t *lock; }; @@ -406,6 +414,7 @@ struct clk_divider { #define CLK_DIVIDER_ROUND_CLOSEST BIT(4) #define CLK_DIVIDER_READ_ONLY BIT(5) #define CLK_DIVIDER_MAX_AT_ZERO BIT(6) +#define CLK_DIVIDER_ZERO_GATE BIT(7) extern const struct clk_ops clk_divider_ops; extern const struct clk_ops clk_divider_ro_ops;