public inbox for linux-mmc@vger.kernel.org
 help / color / mirror / Atom feed
From: Yong Mao <yong.mao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
To: Daniel Kurtz <djkurtz-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
Cc: Ulf Hansson <ulf.hansson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>,
	srv_heupstream
	<srv_heupstream-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>,
	Linus Walleij
	<linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>,
	linux-mmc-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	"linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	"moderated list:ARM/Mediatek SoC support"
	<linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org>,
	Chaotian Jing
	<chaotian.jing-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>,
	Chunfeng Yun
	<chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>,
	Eddie Huang <eddie.huang-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>,
	"linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org"
	<linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org>
Subject: Re: [PATCH v1] mmc: mediatek: Fixed bug where clock frequency could be set wrong
Date: Wed, 1 Mar 2017 17:56:00 +0800	[thread overview]
Message-ID: <1488362160.27826.27.camel@mhfsdcap03> (raw)
In-Reply-To: <CAGS+omCaP99xvpu-A-NVGwdCSKETgtDBxxcYxjTRkFCyzBNL-Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>

On Tue, 2017-02-28 at 14:56 +0800, Daniel Kurtz wrote:
> On Fri, Feb 24, 2017 at 5:38 PM, Yong Mao <yong.mao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org> wrote:
> > From:   Yong Mao <yong.mao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> > To:     Daniel Kurtz <djkurtz-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org>
> > Subject:        Re: [PATCH v1] mmc: mediatek: Fixed bug where clock frequency
> > could be set wrong
> > Date:   Fri, 24 Feb 2017 17:33:37 +0800
> >
> >
> > On Fri, 2017-02-24 at 17:52 +0900, Daniel Kurtz wrote:
> >> On Fri, Feb 24, 2017 at 5:22 PM, Yong Mao <yong.mao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> > wrote:
> >> >
> >> > From: yong mao <yong.mao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> >> >
> >> > This patch can fix two issues:
> >> >
> >> > Issue 1:
> >> > The maximum value of clock divider is 0xff.
> >> > Because the type of div is u32, div may be larger than max_div.
> >> > In this case, we should use max_div to set the clock frequency.
> >> >
> >> > Issue 2:
> >> > In previous code, we can not set the correct clock frequency when
> >> > div equals 0xff.
> >> >
> >> > Signed-off-by: Yong Mao <yong.mao-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> >> > Signed-off-by: Chaotian Jing <chaotian.jing-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org>
> >> > ---
> >> >  drivers/mmc/host/mtk-sd.c |   13 ++++++++++++-
> >> >  1 file changed, 12 insertions(+), 1 deletion(-)
> >> >
> >> > diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
> >> > index 07f3236..3174445 100644
> >> > --- a/drivers/mmc/host/mtk-sd.c
> >> > +++ b/drivers/mmc/host/mtk-sd.c
> >> > @@ -540,6 +540,7 @@ static void msdc_set_mclk(struct msdc_host
> > *host, unsigned char timing, u32 hz)
> >> >         u32 mode;
> >> >         u32 flags;
> >> >         u32 div;
> >> > +       u32 max_div;
> >>
> >> There's really no need for this variable.  Just use 0xff below.
> > For all of our IC, max_div is not a constant.
> > We will upstream another patch which max_div will get the different
> > value depending on the IC.
> > Therefore, we keep the max_div as a variable here.
> 
> Please add the variable in the patch that uses it as a variable.
> 
> >
> >>
> >> >         u32 sclk;
> >> >
> >> >         if (!hz) {
> >> > @@ -590,8 +591,18 @@ static void msdc_set_mclk(struct msdc_host
> > *host, unsigned char timing, u32 hz)
> >> >                         sclk = (host->src_clk_freq >> 2) / div;
> >> >                 }
> >> >         }
> >> > +
> >> > +       /**
> >> > +        * The maximum value of div is 0xff.
> >> > +        * Check if the div is larger than max_div.
> >> > +        */
> >> > +       max_div = 0xff;
> >> > +       if (div > max_div) {
> >> > +               div = max_div;
> >> > +               sclk = (host->src_clk_freq >> 2) / div;
> >> > +       }
> >> >         sdr_set_field(host->base + MSDC_CFG, MSDC_CFG_CKMOD |
> > MSDC_CFG_CKDIV,
> >> > -                       (mode << 8) | (div % 0xff));
> >> > +                     (mode << 8) | div);
> >>
> >> Hmm, I don't know much about this sub-system, but should we even be
> >> allowing requests to set a frequency that we can't actually achieve
> >> with the divider?
> >>
> >
> > No. We can not get a frequency that we can't actually achieve with the
> > divider. This patch is to solve this kind of issue.
> 
> Sorry, I am trying to understand why we need this patch.
> 
> AFAICT, it looks like sometimes msdc_set_mclk() is being called with
> hz that cannot be generated by your hardware.  In particular,
> sometimes the original code computes "div > 255".
> To work around this problem, this patch just caps the divider value to
> 255, which is the maximum divider provided by the hardware.  However,
> presumably this means that in this case we won't actually be
> generating the requested hz value.
> 
> So, can you please explain in what exact scenario this patch is
> required, and justify why it is ok to generate a clock other than the
> requested in this case?
> 
> -Dan
> 

This issue is hidden deeply. It is a boundary related issue.
Let me take the real value to explain how this issue occurs.

1. mmc->f_min = host->src_clk_freq / (4 * 255);
   mmc->f_min = 400000000 / (4 * 255) = 392156;
2. mmc core tries to initialize emmc by using 400000hz.
   If the first try failed, it will retry by using max(300000hz,
mmc->f_min)= 392156hz
3. msdc_set_mclk will be invoked by mmc core to set the clock.
   and then following code will be executed.   
div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2);
(div = (400000000 + (392156 << 2) - 1)) / (392156 << 2) = 256)

Why do we use (host->src_clk_freq + ((hz << 2) - 1))?
==> Because if we can not get a proper clock frequency, we should set a
clock frequency which is less than proper clock frequency.

4. In this IC, it only use 8 bits to indicate the value of clock
divider. Therefore, 256 is overflow, it is equal 0 here.
And then we will get a wrong 100000000Hz clock frequency.

Can you understand how this issue occurs now?

> >
> >
> >> >         sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
> >> >         while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB))
> >> >                 cpu_relax();
> >> > --
> >> > 1.7.9.5
> >> >
> >> >
> >> > _______________________________________________
> >> > Linux-mediatek mailing list
> >> > Linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
> >> > http://lists.infradead.org/mailman/listinfo/linux-mediatek
> >
> >
> >

  parent reply	other threads:[~2017-03-01  9:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-24  8:22 [PATCH v1] Fixed bug where clock frequency could be set wrong Yong Mao
2017-02-24  8:22 ` [PATCH v1] mmc: mediatek: " Yong Mao
2017-02-24  8:52   ` Daniel Kurtz
     [not found]     ` <CAGS+omDuPwXqTJ2CrLbLvSHdp3CL895qop1x6dLT1u7bG7XnVw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-02-24  9:38       ` Yong Mao
2017-02-28  6:56         ` Daniel Kurtz
     [not found]           ` <CAGS+omCaP99xvpu-A-NVGwdCSKETgtDBxxcYxjTRkFCyzBNL-Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-03-01  9:56             ` Yong Mao [this message]
2017-03-02 12:20               ` Daniel Kurtz
     [not found]                 ` <CAGS+omBw_vXMmjtjyE+9CbDBP06u_ngnEreEf9jfQvNw9dCktg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-03-04  6:44                   ` Yong Mao

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=1488362160.27826.27.camel@mhfsdcap03 \
    --to=yong.mao-nus5lvnupcjwk0htik3j/w@public.gmane.org \
    --cc=chaotian.jing-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
    --cc=chunfeng.yun-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
    --cc=djkurtz-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org \
    --cc=eddie.huang-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
    --cc=linus.walleij-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
    --cc=linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-mediatek-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
    --cc=linux-mmc-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=srv_heupstream-NuS5LvNUpcJWk0Htik3J/w@public.gmane.org \
    --cc=ulf.hansson-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org \
    /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