mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexandre Courbot <acourbot@nvidia.com>
To: Tomeu Vizoso <tomeu.vizoso@collabora.com>, <linux-tegra@vger.kernel.org>
Cc: Javier Martinez Canillas <javier.martinez@collabora.co.uk>,
	"Mikko Perttunen" <mperttunen@nvidia.com>,
	Peter De Schrijver <pdeschrijver@nvidia.com>,
	Prashant Gaikwad <pgaikwad@nvidia.com>,
	"Mike Turquette" <mturquette@linaro.org>,
	Stephen Warren <swarren@wwwdotorg.org>,
	Thierry Reding <thierry.reding@gmail.com>,
	Alexandre Courbot <gnurou@gmail.com>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v3 11/13] clk: tegra: Add EMC clock driver
Date: Thu, 6 Nov 2014 17:04:25 +0900	[thread overview]
Message-ID: <545B2B89.8010801@nvidia.com> (raw)
In-Reply-To: <1414599796-30597-12-git-send-email-tomeu.vizoso@collabora.com>

On 10/30/2014 01:22 AM, Tomeu Vizoso wrote:
> From: Mikko Perttunen <mperttunen@nvidia.com>
>
> The driver is currently only tested on Tegra124 Jetson TK1, but should
> work with other Tegra124 boards, provided that correct EMC tables are
> provided through the device tree. Older chip models have differing
> timing change sequences, so they are not currently supported.
>
> Signed-off-by: Mikko Perttunen <mperttunen@nvidia.com>
> Signed-off-by: Tomeu Vizoso <tomeu.vizoso@collabora.com>
>
> ---
>
> v3:	* Add some locking to protect the registers that are shared with the MC
> 	  clock
>
> v2:	* Make sure that the clock is properly registered
> 	* Bail out early from attempts to set the same rate
> ---
>   drivers/clk/tegra/Makefile       |   2 +-
>   drivers/clk/tegra/clk-emc.c      | 470 +++++++++++++++++++++++++++++++++++++++
>   drivers/clk/tegra/clk-tegra124.c |   4 +-
>   drivers/clk/tegra/clk.h          |   2 +
>   4 files changed, 476 insertions(+), 2 deletions(-)
>   create mode 100644 drivers/clk/tegra/clk-emc.c
>
> diff --git a/drivers/clk/tegra/Makefile b/drivers/clk/tegra/Makefile
> index f7dfb72..240e5b4 100644
> --- a/drivers/clk/tegra/Makefile
> +++ b/drivers/clk/tegra/Makefile
> @@ -14,4 +14,4 @@ obj-y					+= clk-tegra-super-gen4.o
>   obj-$(CONFIG_ARCH_TEGRA_2x_SOC)         += clk-tegra20.o
>   obj-$(CONFIG_ARCH_TEGRA_3x_SOC)         += clk-tegra30.o
>   obj-$(CONFIG_ARCH_TEGRA_114_SOC)	+= clk-tegra114.o
> -obj-$(CONFIG_ARCH_TEGRA_124_SOC)	+= clk-tegra124.o
> +obj-$(CONFIG_ARCH_TEGRA_124_SOC)	+= clk-tegra124.o clk-emc.o
> diff --git a/drivers/clk/tegra/clk-emc.c b/drivers/clk/tegra/clk-emc.c
> new file mode 100644
> index 0000000..182a059
> --- /dev/null
> +++ b/drivers/clk/tegra/clk-emc.c
> @@ -0,0 +1,470 @@
> +/*
> + * drivers/clk/tegra/clk-emc.c
> + *
> + * Copyright (c) 2014, NVIDIA CORPORATION.  All rights reserved.
> + *
> + * Author:
> + *	Mikko Perttunen <mperttunen@nvidia.com>
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + */
> +
> +#include <linux/clk-provider.h>
> +#include <linux/clk.h>
> +#include <linux/clkdev.h>
> +#include <linux/delay.h>
> +#include <linux/module.h>
> +#include <linux/of_address.h>
> +#include <linux/of_platform.h>
> +#include <linux/platform_device.h>
> +#include <linux/sort.h>
> +#include <linux/string.h>
> +
> +#include <soc/tegra/fuse.h>
> +#include <soc/tegra/memory.h>
> +
> +#define CLK_SOURCE_EMC 0x19c
> +
> +#define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_SHIFT 0
> +#define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK 0xff
> +#define CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(x) (((x) & CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK) << \
> +					      CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_SHIFT)
> +
> +#define CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT 29
> +#define CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK 0x7
> +#define CLK_SOURCE_EMC_EMC_2X_CLK_SRC(x) (((x) & CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK) << \
> +					  CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT)
> +
> +const char *emc_parent_clk_names[] = {
> +	"pll_m", "pll_c", "pll_p", "clk_m", "pll_m_ud",
> +	"pll_c2", "pll_c3", "pll_c_ud"
> +};
> +
> +/* List of clock sources for various parents the EMC clock can have.
> + * When we change the timing to a timing with a parent that has the same
> + * clock source as the current parent, we must first change to a backup
> + * timing that has a different clock source.
> + */

Nit: comment style throughout this file.

> +
> +#define EMC_SRC_PLL_M 0
> +#define EMC_SRC_PLL_C 1
> +#define EMC_SRC_PLL_P 2
> +#define EMC_SRC_CLK_M 3
> +#define EMC_SRC_PLL_C2 4
> +#define EMC_SRC_PLL_C3 5
> +const char emc_parent_clk_sources[] = {
> +	EMC_SRC_PLL_M, EMC_SRC_PLL_C, EMC_SRC_PLL_P, EMC_SRC_CLK_M,
> +	EMC_SRC_PLL_M, EMC_SRC_PLL_C2, EMC_SRC_PLL_C3, EMC_SRC_PLL_C
> +};
> +
> +struct emc_timing {
> +	unsigned long rate, parent_rate;
> +	u8 parent_index;
> +	struct clk *parent;
> +	u32 ram_code;
> +};
> +
> +struct tegra_emc {
> +	struct clk_hw hw;
> +	void __iomem *clk_regs;
> +	struct clk *prev_parent;
> +	bool changing_timing;
> +
> +	int num_timings;
> +	struct emc_timing *timings;
> +	spinlock_t *lock;
> +};
> +
> +/* * * * * * * * * * * * * * * * * * * * * * * * * *
> + * Common clock framework callback implementations *
> + * * * * * * * * * * * * * * * * * * * * * * * * * */
> +
> +unsigned long emc_recalc_rate(struct clk_hw *hw, unsigned long parent_rate)

Shouldn't this function be static? Also applies to other functions in 
this file.

> +{
> +	struct tegra_emc *tegra = container_of(hw, struct tegra_emc, hw);
> +	u32 val, div;
> +
> +	/* CCF wrongly assumes that the parent won't change during set_rate,
> +	 * so get the parent rate explicitly.
> +	 */
> +	parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
> +
> +	val = readl(tegra->clk_regs + CLK_SOURCE_EMC);
> +	div = val & CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR_MASK;
> +
> +	return parent_rate / (div + 2) * 2;
> +}
> +
> +/* Rounds up unless no higher rate exists, in which case down. This way is
> + * safer since things have EMC rate floors. Also don't touch parent_rate
> + * since we don't want the CCF to play with our parent clocks.
> + */
> +long emc_round_rate(struct clk_hw *hw, unsigned long rate,
> +		    unsigned long *parent_rate)
> +{
> +	struct tegra_emc *tegra = container_of(hw, struct tegra_emc, hw);
> +	u8 ram_code = tegra_read_ram_code();
> +	struct emc_timing *timing;
> +	int i;
> +
> +	/* Returning the original rate will lead to a more sensible error
> +	 * message when emc_set_rate fails.
> +	 */
> +	if (tegra->num_timings == 0)
> +		return rate;
> +
> +	for (i = 0; i < tegra->num_timings; ++i) {
> +		timing = tegra->timings + i;
> +		if (timing->ram_code != ram_code)
> +			continue;
> +
> +		if (timing->rate >= rate)
> +			return timing->rate;
> +	}
> +
> +	return tegra->timings[tegra->num_timings - 1].rate;
> +}
> +
> +u8 emc_get_parent(struct clk_hw *hw)
> +{
> +	struct tegra_emc *tegra = container_of(hw, struct tegra_emc, hw);
> +	u32 val;
> +
> +	val = readl(tegra->clk_regs + CLK_SOURCE_EMC);
> +
> +	return (val >> CLK_SOURCE_EMC_EMC_2X_CLK_SRC_SHIFT)
> +		& CLK_SOURCE_EMC_EMC_2X_CLK_SRC_MASK;
> +}
> +
> +static int emc_set_timing(struct tegra_emc *tegra, struct emc_timing *timing)
> +{
> +	int err;
> +	u8 div;
> +	u32 car_value;
> +	unsigned long flags = 0;
> +
> +	pr_debug("going to rate %ld prate %ld p %s\n",
> +		timing->rate, timing->parent_rate,
> +		__clk_get_name(timing->parent));
> +
> +	if (emc_get_parent(&tegra->hw) == timing->parent_index &&
> +	    clk_get_rate(timing->parent) != timing->parent_rate) {
> +		BUG();
> +		return -EINVAL;
> +	}
> +
> +	tegra->changing_timing = true;
> +
> +	err = clk_set_rate(timing->parent, timing->parent_rate);
> +	if (err) {
> +		pr_err("cannot change parent %s rate to %ld: %d\n",
> +		       __clk_get_name(timing->parent),
> +		       timing->parent_rate, err);
> +
> +		return err;
> +	}
> +
> +	err = clk_prepare_enable(timing->parent);
> +	if (err) {
> +		pr_err("cannot enable parent clock: %d\n", err);
> +		return err;
> +	}
> +
> +	div = timing->parent_rate / (timing->rate / 2) - 2;
> +
> +	tegra_emc_prepare_timing_change(timing->rate);

Since there is no locking whatsoever in the EMC driver, I wonder if this 
function should be called after the spinlock is acquired to ensure it 
cannot be called twice?

> +
> +	spin_lock_irqsave(tegra->lock, flags);
> +
> +	car_value = readl(tegra->clk_regs + CLK_SOURCE_EMC);
> +
> +	car_value &= ~CLK_SOURCE_EMC_EMC_2X_CLK_SRC(~0);
> +	car_value |= CLK_SOURCE_EMC_EMC_2X_CLK_SRC(timing->parent_index);
> +
> +	car_value &= ~CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(~0);
> +	car_value |= CLK_SOURCE_EMC_EMC_2X_CLK_DIVISOR(div);
> +
> +	writel(car_value, tegra->clk_regs + CLK_SOURCE_EMC);
> +
> +	spin_unlock_irqrestore(tegra->lock, flags);
> +
> +	tegra_emc_complete_timing_change(timing->rate);

Same for this one.


  reply	other threads:[~2014-11-06  8:08 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-29 16:22 [PATCH v3 00/13] Tegra124 EMC (external memory controller) support Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 01/13] clk: tegra124: Remove old emc clock Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 02/13] of: Document long-ram-code property in nvidia,tegra20-apbmisc Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 03/13] soc/tegra: Add ram code reader helper Tomeu Vizoso
2014-11-06  5:25   ` Alexandre Courbot
2014-11-06  9:07     ` Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 04/13] of: document new emc-timings subnode in nvidia,tegra124-car Tomeu Vizoso
2014-11-06  6:37   ` Alexandre Courbot
2014-11-06  9:11     ` Tomeu Vizoso
2014-11-06  9:15       ` Alexandre Courbot
2014-11-06 15:12     ` Rob Herring
2014-11-07  5:31       ` Alexandre Courbot
2014-11-07 11:35         ` Mikko Perttunen
2014-11-07 13:03         ` Tomeu Vizoso
2014-11-07 13:10       ` Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 05/13] of: Document timings subnode of nvidia,tegra-mc Tomeu Vizoso
2014-11-06  8:12   ` Alexandre Courbot
2014-11-06 10:32     ` Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 06/13] of: Add Tegra124 EMC bindings Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 07/13] ARM: tegra: Add EMC to Tegra124 device tree Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 08/13] ARM: tegra: Add EMC timings to Jetson TK1 " Tomeu Vizoso
2014-11-06 12:21   ` Nikolaus Schulz
2014-11-07 16:12     ` Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 09/13] memory: tegra: Add API needed by the EMC driver Tomeu Vizoso
2014-11-06  7:26   ` Alexandre Courbot
2014-10-29 16:22 ` [PATCH v3 10/13] memory: tegra: Add EMC (external memory controller) driver Tomeu Vizoso
2014-11-06  7:56   ` Alexandre Courbot
2014-11-06 11:35     ` Mikko Perttunen
2014-11-07 16:16       ` Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 11/13] clk: tegra: Add EMC clock driver Tomeu Vizoso
2014-11-06  8:04   ` Alexandre Courbot [this message]
2014-11-06 11:41     ` Mikko Perttunen
2014-11-07  5:34       ` Alexandre Courbot
2014-10-29 16:22 ` [PATCH v3 12/13] memory: tegra: Add debugfs entry for getting and setting the EMC rate Tomeu Vizoso
2014-10-29 16:22 ` [PATCH v3 13/13] clk: tegra: Set the EMC clock as the parent of the MC clock Tomeu Vizoso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=545B2B89.8010801@nvidia.com \
    --to=acourbot@nvidia.com \
    --cc=gnurou@gmail.com \
    --cc=javier.martinez@collabora.co.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=mperttunen@nvidia.com \
    --cc=mturquette@linaro.org \
    --cc=pdeschrijver@nvidia.com \
    --cc=pgaikwad@nvidia.com \
    --cc=swarren@wwwdotorg.org \
    --cc=thierry.reding@gmail.com \
    --cc=tomeu.vizoso@collabora.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome