[PATCH AUTOSEL 4.4 06/28] USB: serial: ftdi_sio: use rounding when calculating baud rate divisors

From: Sasha Levin
Date: Tue Jan 08 2019 - 14:39:47 EST


From: Nikolaj Fogh <nikolajfogh@xxxxxxxxx>

[ Upstream commit 6abd837104a3a8e1cda64fc4d7675f6c3ece9d8b ]

Improve baud-rate generation by using rounding-to-closest instead of
truncation in divisor calculation.

Results have been verified by logic analyzer on an FT232RT (232BM) chip.
The following table shows the wanted baud rate, the baud rate obtained
with the old method (truncation), with the new method (rounding) and the
baud rate generated by the windows 10 driver. The numbers in parentheses
is the error.

+- Wanted --+------ Old -------+------ New -------+------ Win -------+
| Â 9600Â | Â 9600 (0.00%)Â |ÂÂ 9604 (0.05%)Â |ÂÂ 9605 (0.05%)Â |
| Â 19200 Â | Â 19200 (0.00%)Â |ÂÂ 19199 (0.01%)Â |ÂÂ 19198 (0.01%)Â |
| Â 38400 Â | Â 38395 (0.01%)Â |ÂÂ 38431 (0.08%)Â |ÂÂ 38394 (0.02%)Â |
| Â 57600 Â | Â 57725 (0.22%)Â |ÂÂ 57540 (0.10%)Â |ÂÂ 57673 (0.13%)Â |
|Â 115200 Â |Â 115307 (0.09%)Â |Â 115330 (0.11%)Â |Â 115320 (0.10%)Â |
|Â 921600 Â |Â 919963 (0.18%)Â |Â 920386 (0.13%)Â |Â 920810 (0.09%)Â |
|Â 961200 Â |Â 996512 (3.67%)Â |Â 956480 (0.49%)Â |Â 956937 (0.44%)Â |
+-----------+------------------+------------------+------------------+

The error due to noise in the measurements is in the order of a few
tenths of a %. As can be seen, the baud rate is significantly improved
for some rates (e.g. 961200), and corresponds to the output given by the
windows driver.

The theoretical baud rate has been calculated for all baud rates from 1
to 3M, and as expected, the error is centered around 0, with a triangle
shape instead of a sawtooth, so the maximum error is decreased to half.

Signed-off-by: Nikolaj Fogh <nikolajfogh@xxxxxxxxx>
[ johan: edit commit message slightly ]
Signed-off-by: Johan Hovold <johan@xxxxxxxxxx>
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
---
drivers/usb/serial/ftdi_sio.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/serial/ftdi_sio.c b/drivers/usb/serial/ftdi_sio.c
index 3e5b189a79b4..49d4ca167bf3 100644
--- a/drivers/usb/serial/ftdi_sio.c
+++ b/drivers/usb/serial/ftdi_sio.c
@@ -1119,7 +1119,7 @@ static unsigned short int ftdi_232am_baud_base_to_divisor(int baud, int base)
{
unsigned short int divisor;
/* divisor shifted 3 bits to the left */
- int divisor3 = base / 2 / baud;
+ int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud);
if ((divisor3 & 0x7) == 7)
divisor3++; /* round x.7/8 up to x+1 */
divisor = divisor3 >> 3;
@@ -1145,7 +1145,7 @@ static __u32 ftdi_232bm_baud_base_to_divisor(int baud, int base)
static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 };
__u32 divisor;
/* divisor shifted 3 bits to the left */
- int divisor3 = base / 2 / baud;
+ int divisor3 = DIV_ROUND_CLOSEST(base, 2 * baud);
divisor = divisor3 >> 3;
divisor |= (__u32)divfrac[divisor3 & 0x7] << 14;
/* Deal with special cases for highest baud rates. */
@@ -1168,7 +1168,7 @@ static __u32 ftdi_2232h_baud_base_to_divisor(int baud, int base)
int divisor3;

/* hi-speed baud rate is 10-bit sampling instead of 16-bit */
- divisor3 = base * 8 / (baud * 10);
+ divisor3 = DIV_ROUND_CLOSEST(8 * base, 10 * baud);

divisor = divisor3 >> 3;
divisor |= (__u32)divfrac[divisor3 & 0x7] << 14;
--
2.19.1