serial: sh-sci: Fix HSCIF RX sampling point calculation
authorGeert Uytterhoeven <geert+renesas@glider.be>
Mon, 1 Apr 2019 11:25:10 +0000 (13:25 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 27 Apr 2019 07:36:36 +0000 (09:36 +0200)
commit38b7f09a9e83da3d8d786429520aace041e1d29e
treef3fa65596fb1bde51673e53eb9a139946dd0cca3
parentde6d6b8902fb2d26a46d266841364c3bfa70dc41
serial: sh-sci: Fix HSCIF RX sampling point calculation

commit ace965696da2611af759f0284e26342b7b6cec89 upstream.

There are several issues with the formula used for calculating the
deviation from the intended rate:
  1. While min_err and last_stop are signed, srr and baud are unsigned.
     Hence the signed values are promoted to unsigned, which will lead
     to a bogus value of deviation if min_err is negative,
  2. Srr is the register field value, which is one less than the actual
     sampling rate factor,
  3. The divisions do not use rounding.

Fix this by casting unsigned variables to int, adding one to srr, and
using a single DIV_ROUND_CLOSEST().

Fixes: 63ba1e00f178a448 ("serial: sh-sci: Support for HSCIF RX sampling point adjustment")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Reviewed-by: Mukesh Ojha <mojha@codeaurora.org>
Cc: stable <stable@vger.kernel.org>
Reviewed-by: Ulrich Hecht <uli+renesas@fpond.eu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/serial/sh-sci.c