All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Dong Aisheng <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>,
	fugang.duan@nxp.com, Stefan Agner <stefan@agner.ch>,
	Mingkai.Hu@nxp.com, yangbo.lu@nxp.com
Subject: Re: [PATCH 6/6] tty: serial: lpuart: add a more accurate baud rate calculation method
Date: Sun, 28 May 2017 03:04:15 +0300	[thread overview]
Message-ID: <CAHp75Vemc5qoD7gbc_sJBSasYTFYSYOuWZv4N-4X60arWX8aJg@mail.gmail.com> (raw)
In-Reply-To: <1494316248-24052-7-git-send-email-aisheng.dong@nxp.com>

On Tue, May 9, 2017 at 10:50 AM, Dong Aisheng <aisheng.dong@nxp.com> wrote:
> On new LPUART versions, the oversampling ratio for the receiver can be
> changed from 4x (00011) to 32x (11111) which could help us get a more
> accurate baud rate divider.
>
> 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.

> +lpuart32_serial_setbrg(struct lpuart_port *sport, unsigned int baudrate)
> +{
> +       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 _think_ you may simplify this and avoid for-loop if you reconsider approach.

> +               /* calculate the temporary sbr value  */
> +               tmp_sbr = (clk / (baudrate * tmp_osr));
> +               if (tmp_sbr == 0)
> +                       tmp_sbr = 1;
> +
> +               /*
> +                * calculate the baud rate difference based on the temporary
> +                * osr and sbr values
> +                */

> +               tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;

(32 - 4 + 1) times division is called...

> +
> +               /* select best values between sbr and sbr+1 */
> +               tmp = clk / (tmp_osr * (tmp_sbr + 1));
> +               if (tmp_diff > (baudrate - tmp)) {
> +                       tmp_diff = baudrate - tmp;
> +                       tmp_sbr++;
> +               }
> +
> +               if (tmp_diff <= baud_diff) {
> +                       baud_diff = tmp_diff;
> +                       osr = tmp_osr;
> +                       sbr = tmp_sbr;
> +               }
> +       }

> +       /* handle buadrate outside acceptable rate */
> +       if (baud_diff > ((baudrate / 100) * 3))
> +               dev_warn(sport->port.dev,
> +                        "unacceptable baud rate difference of more than 3%%\n");

Shouldn't you fall back to previous setting?

> +
> +       tmp = lpuart32_read(sport->port.membase + UARTBAUD);
> +

> +       if ((osr > 3) && (osr < 8))

Isn't it

if (osr ^ BIT(2) < BIT(2))

?

> +               tmp |= UARTBAUD_BOTHEDGE;

> +}

> +       if (of_device_is_compatible(port->dev->of_node, "fsl,imx7ulp-lpuart")) {
> +               lpuart32_serial_setbrg(sport, baud);

> +       } else {
> +               sbr = sport->port.uartclk / (16 * baud);
> +               bd &= ~UARTBAUD_SBR_MASK;
> +               bd |= sbr & UARTBAUD_SBR_MASK;
> +               bd |= UARTBAUD_BOTHEDGE;
> +               bd &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE);
> +               lpuart32_write(bd, sport->port.membase + UARTBAUD);
> +       }

Perhaps it makes sense to split this to a helper function as well (in
a separate patch).

-- 
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: Sun, 28 May 2017 03:04:15 +0300	[thread overview]
Message-ID: <CAHp75Vemc5qoD7gbc_sJBSasYTFYSYOuWZv4N-4X60arWX8aJg@mail.gmail.com> (raw)
In-Reply-To: <1494316248-24052-7-git-send-email-aisheng.dong@nxp.com>

On Tue, May 9, 2017 at 10:50 AM, Dong Aisheng <aisheng.dong@nxp.com> wrote:
> On new LPUART versions, the oversampling ratio for the receiver can be
> changed from 4x (00011) to 32x (11111) which could help us get a more
> accurate baud rate divider.
>
> 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.

> +lpuart32_serial_setbrg(struct lpuart_port *sport, unsigned int baudrate)
> +{
> +       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 _think_ you may simplify this and avoid for-loop if you reconsider approach.

> +               /* calculate the temporary sbr value  */
> +               tmp_sbr = (clk / (baudrate * tmp_osr));
> +               if (tmp_sbr == 0)
> +                       tmp_sbr = 1;
> +
> +               /*
> +                * calculate the baud rate difference based on the temporary
> +                * osr and sbr values
> +                */

> +               tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;

(32 - 4 + 1) times division is called...

> +
> +               /* select best values between sbr and sbr+1 */
> +               tmp = clk / (tmp_osr * (tmp_sbr + 1));
> +               if (tmp_diff > (baudrate - tmp)) {
> +                       tmp_diff = baudrate - tmp;
> +                       tmp_sbr++;
> +               }
> +
> +               if (tmp_diff <= baud_diff) {
> +                       baud_diff = tmp_diff;
> +                       osr = tmp_osr;
> +                       sbr = tmp_sbr;
> +               }
> +       }

> +       /* handle buadrate outside acceptable rate */
> +       if (baud_diff > ((baudrate / 100) * 3))
> +               dev_warn(sport->port.dev,
> +                        "unacceptable baud rate difference of more than 3%%\n");

Shouldn't you fall back to previous setting?

> +
> +       tmp = lpuart32_read(sport->port.membase + UARTBAUD);
> +

> +       if ((osr > 3) && (osr < 8))

Isn't it

if (osr ^ BIT(2) < BIT(2))

?

> +               tmp |= UARTBAUD_BOTHEDGE;

> +}

> +       if (of_device_is_compatible(port->dev->of_node, "fsl,imx7ulp-lpuart")) {
> +               lpuart32_serial_setbrg(sport, baud);

> +       } else {
> +               sbr = sport->port.uartclk / (16 * baud);
> +               bd &= ~UARTBAUD_SBR_MASK;
> +               bd |= sbr & UARTBAUD_SBR_MASK;
> +               bd |= UARTBAUD_BOTHEDGE;
> +               bd &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE);
> +               lpuart32_write(bd, sport->port.membase + UARTBAUD);
> +       }

Perhaps it makes sense to split this to a helper function as well (in
a separate patch).

-- 
With Best Regards,
Andy Shevchenko

  reply	other threads:[~2017-05-28  0:04 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 [this message]
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
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=CAHp75Vemc5qoD7gbc_sJBSasYTFYSYOuWZv4N-4X60arWX8aJg@mail.gmail.com \
    --to=andy.shevchenko@gmail.com \
    --cc=Mingkai.Hu@nxp.com \
    --cc=aisheng.dong@nxp.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=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.