All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: "A.S. Dong" <aisheng.dong@nxp.com>
Cc: "linux-serial@vger.kernel.org" <linux-serial@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	linux-arm Mailing List <linux-arm-kernel@lists.infradead.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jslaby@suse.com>, Andy Duan <fugang.duan@nxp.com>,
	Stefan Agner <stefan@agner.ch>, Mingkai Hu <mingkai.hu@nxp.com>,
	"Y.B. Lu" <yangbo.lu@nxp.com>, Dong Aisheng <dongas86@gmail.com>
Subject: Re: [PATCH 6/6] tty: serial: lpuart: add a more accurate baud rate calculation method
Date: Fri, 9 Jun 2017 12:26:20 +0300	[thread overview]
Message-ID: <CAHp75VcSToSfbUZ2TP0OsLRjV4TNpq7D_=4tnB_+rDvS2jp22Q@mail.gmail.com> (raw)
In-Reply-To: <AM3PR04MB306214514B820D66535E28B80CE0@AM3PR04MB306.eurprd04.prod.outlook.com>

On Fri, Jun 9, 2017 at 11:01 AM, A.S. Dong <aisheng.dong@nxp.com> wrote:

>> >> > +       u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
>> >> > +       u32 clk = sport->port.uartclk;
>> >> > +
>> >> > +       /*
>> >> > +        * The idea is to use the best OSR (over-sampling rate)
>> possible.
>> >> > +        * Note, OSR is typically hard-set to 16 in other LPUART
>> >> instantiations.
>> >> > +        * Loop to find the best OSR value possible, one that
>> >> > + generates
>> >> minimum
>> >> > +        * baud_diff iterate through the rest of the supported
>> >> > + values of
>> >> OSR.
>> >> > +        *
>> >> > +        * Calculation Formula:
>> >> > +        *  Baud Rate = baud clock / ((OSR+1) × SBR)
>> >> > +        */
>> >> > +       baud_diff = baudrate;
>> >> > +       osr = 0;
>> >> > +       sbr = 0;
>> >> > +
>> >>
>> >> > +       for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
>>
>> I missed one thing, what happened by default to OSR? What is the value in
>> use?
>>
>
> No valid default value. (osc/sbr are 0 by default)
> If no proper osc and sbr calculated, a WARNING will show.

Okay, so, it means the maximum supported speed is UART clock / 4. Correct?

>> So, the algo is the following:
>>
>> Assume the ranges like this:
>> OSR = [4 ... 32]
>> SBR = [2 ... 8192]
>>
>
> Baud Rate = baud clock / ((OSR+1) × SBR)
>
> In HW:
> OSR range : 3 – 31
> SBR range: 1 – 8191

I've read that, but think outside the box.

>> Then:
>>
>> 1. Get ratio factor as
>>       ratio = CLK / desired baud rate
>> 2. If ratio < 8192 * 9 / 2, just use (ratio / 4, 4) as (OSR, SBR) setting.
>> (Needs clarification on OSR < 4)
>
> Sorry that I'm a bit mess here.
> What is 8192 * 9 /2 meaning?

I forgot the details...

> And for (ratio / 4, 4) as (OSR,SBR), take 115200 as an example:
> Assuming baud clock 24Mhz.
>
> Ratio = 24000000 / 115200 = 208
> OSR = Ratio / 4 = 52
> Then OSR is out of range which seems wrong.

...yes...

>> 3. if ratio >= 8192 * 31, just use those
>> two numbers (8192, 31). You can't do anything better there.
>
> This actually may not happen.
> Even take a 9600 as example, the clk becomes:
> 8191 * 31 * 9600 = 2.4GHz
> Which is theoretically not exist.
>
>> 4. Otherwise, get a minimum required factor of OSR
>>       osr_min = ratio / 8192
>> 5. Start your loop from osr_min + 1 to 31.
>>
>> 6 (optional). Of course you may not consider baud_diff > osr_min, it's I
>> suppose obvious
>>
>> P.S. Note, all divisions by 2^n are just simple right shifts. Diffs are
>> calculated as multiplication of OSR and SBR in comparison to ratio. One
>> division so far.

> I'm not quite understand the approach.

...lemme prepare a python script demonstrating it.

> How about you send a separate baud algorithm improvement patch later?

Why not to do it right a way?

Just describe it in a comment if you afraid of reader can't understand
from the code.

-- 
With Best Regards,
Andy Shevchenko

WARNING: multiple messages have this Message-ID (diff)
From: andy.shevchenko@gmail.com (Andy Shevchenko)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 6/6] tty: serial: lpuart: add a more accurate baud rate calculation method
Date: Fri, 9 Jun 2017 12:26:20 +0300	[thread overview]
Message-ID: <CAHp75VcSToSfbUZ2TP0OsLRjV4TNpq7D_=4tnB_+rDvS2jp22Q@mail.gmail.com> (raw)
In-Reply-To: <AM3PR04MB306214514B820D66535E28B80CE0@AM3PR04MB306.eurprd04.prod.outlook.com>

On Fri, Jun 9, 2017 at 11:01 AM, A.S. Dong <aisheng.dong@nxp.com> wrote:

>> >> > +       u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
>> >> > +       u32 clk = sport->port.uartclk;
>> >> > +
>> >> > +       /*
>> >> > +        * The idea is to use the best OSR (over-sampling rate)
>> possible.
>> >> > +        * Note, OSR is typically hard-set to 16 in other LPUART
>> >> instantiations.
>> >> > +        * Loop to find the best OSR value possible, one that
>> >> > + generates
>> >> minimum
>> >> > +        * baud_diff iterate through the rest of the supported
>> >> > + values of
>> >> OSR.
>> >> > +        *
>> >> > +        * Calculation Formula:
>> >> > +        *  Baud Rate = baud clock / ((OSR+1) ? SBR)
>> >> > +        */
>> >> > +       baud_diff = baudrate;
>> >> > +       osr = 0;
>> >> > +       sbr = 0;
>> >> > +
>> >>
>> >> > +       for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
>>
>> I missed one thing, what happened by default to OSR? What is the value in
>> use?
>>
>
> No valid default value. (osc/sbr are 0 by default)
> If no proper osc and sbr calculated, a WARNING will show.

Okay, so, it means the maximum supported speed is UART clock / 4. Correct?

>> So, the algo is the following:
>>
>> Assume the ranges like this:
>> OSR = [4 ... 32]
>> SBR = [2 ... 8192]
>>
>
> Baud Rate = baud clock / ((OSR+1) ? SBR)
>
> In HW:
> OSR range : 3 ? 31
> SBR range: 1 ? 8191

I've read that, but think outside the box.

>> Then:
>>
>> 1. Get ratio factor as
>>       ratio = CLK / desired baud rate
>> 2. If ratio < 8192 * 9 / 2, just use (ratio / 4, 4) as (OSR, SBR) setting.
>> (Needs clarification on OSR < 4)
>
> Sorry that I'm a bit mess here.
> What is 8192 * 9 /2 meaning?

I forgot the details...

> And for (ratio / 4, 4) as (OSR,SBR), take 115200 as an example:
> Assuming baud clock 24Mhz.
>
> Ratio = 24000000 / 115200 = 208
> OSR = Ratio / 4 = 52
> Then OSR is out of range which seems wrong.

...yes...

>> 3. if ratio >= 8192 * 31, just use those
>> two numbers (8192, 31). You can't do anything better there.
>
> This actually may not happen.
> Even take a 9600 as example, the clk becomes:
> 8191 * 31 * 9600 = 2.4GHz
> Which is theoretically not exist.
>
>> 4. Otherwise, get a minimum required factor of OSR
>>       osr_min = ratio / 8192
>> 5. Start your loop from osr_min + 1 to 31.
>>
>> 6 (optional). Of course you may not consider baud_diff > osr_min, it's I
>> suppose obvious
>>
>> P.S. Note, all divisions by 2^n are just simple right shifts. Diffs are
>> calculated as multiplication of OSR and SBR in comparison to ratio. One
>> division so far.

> I'm not quite understand the approach.

...lemme prepare a python script demonstrating it.

> How about you send a separate baud algorithm improvement patch later?

Why not to do it right a way?

Just describe it in a comment if you afraid of reader can't understand
from the code.

-- 
With Best Regards,
Andy Shevchenko

  reply	other threads:[~2017-06-09  9:26 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-09  7:50 [PATCH 0/6] tty: serial: lpuart: add imx7ulp support Dong Aisheng
2017-05-09  7:50 ` Dong Aisheng
2017-05-09  7:50 ` Dong Aisheng
2017-05-09  7:50 ` [PATCH 1/6] tty: serial: lpuart: introduce lpuart_soc_data to represent SoC property Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-10  3:50   ` Stefan Agner
2017-05-10  3:50     ` Stefan Agner
2017-05-10  6:06     ` Dong Aisheng
2017-05-10  6:06       ` Dong Aisheng
2017-05-10  6:06       ` Dong Aisheng
2017-05-09  7:50 ` [PATCH 2/6] tty: serial: lpuart: add little endian 32 bit register support Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-10  3:58   ` Stefan Agner
2017-05-10  3:58     ` Stefan Agner
2017-05-10  6:19     ` Dong Aisheng
2017-05-10  6:19       ` Dong Aisheng
2017-05-10  6:19       ` Dong Aisheng
2017-05-09  7:50 ` [PATCH 3/6] dt-bindings: serial: fsl-lpuart: add i.MX7ULP support Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-12 20:12   ` Rob Herring
2017-05-12 20:12     ` Rob Herring
2017-05-12 20:12     ` Rob Herring
2017-05-09  7:50 ` [PATCH 4/6] tty: serial: lpuart: add imx7ulp support Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-10  4:10   ` Stefan Agner
2017-05-10  4:10     ` Stefan Agner
2017-05-10  6:14     ` Dong Aisheng
2017-05-10  6:14       ` Dong Aisheng
2017-05-10  6:14       ` Dong Aisheng
2017-05-10 20:37       ` Stefan Agner
2017-05-10 20:37         ` Stefan Agner
2017-05-12 13:28         ` Dong Aisheng
2017-05-12 13:28           ` Dong Aisheng
2017-05-09  7:50 ` [PATCH 5/6] tty: serial: lpuart: add earlycon support for imx7ulp Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-09  7:50 ` [PATCH 6/6] tty: serial: lpuart: add a more accurate baud rate calculation method Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-09  7:50   ` Dong Aisheng
2017-05-28  0:04   ` Andy Shevchenko
2017-05-28  0:04     ` Andy Shevchenko
2017-05-31 14:18     ` A.S. Dong
2017-05-31 14:18       ` A.S. Dong
2017-06-02 17:11       ` Andy Shevchenko
2017-06-02 17:11         ` Andy Shevchenko
2017-06-09  8:01         ` A.S. Dong
2017-06-09  8:01           ` A.S. Dong
2017-06-09  9:26           ` Andy Shevchenko [this message]
2017-06-09  9:26             ` Andy Shevchenko
2017-06-09 14:20             ` A.S. Dong
2017-06-09 14:20               ` A.S. Dong
2017-06-09 15:48               ` Andy Shevchenko
2017-06-09 15:48                 ` Andy Shevchenko
2017-06-12 14:23                 ` A.S. Dong
2017-06-12 14:23                   ` A.S. Dong
2017-05-09 11:13 ` [PATCH 0/6] tty: serial: lpuart: add imx7ulp support Andy Duan
2017-05-09 11:13   ` Andy Duan
2017-05-09 11:13   ` Andy Duan

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='CAHp75VcSToSfbUZ2TP0OsLRjV4TNpq7D_=4tnB_+rDvS2jp22Q@mail.gmail.com' \
    --to=andy.shevchenko@gmail.com \
    --cc=aisheng.dong@nxp.com \
    --cc=dongas86@gmail.com \
    --cc=fugang.duan@nxp.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=mingkai.hu@nxp.com \
    --cc=stefan@agner.ch \
    --cc=yangbo.lu@nxp.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.