Skip to contentSkip to conversation

tty/8250_early: Prevent rounding error in uartclk to baud ratio

PATCH Superseded Alexey Brodkin 4 participants 6 messages Sep 28, 2012 – Sep 29, 2012

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty/8250_early: Prevent rounding error in uartclk to baud ratio

Modify divisor to select the nearest baud rate divider rather than the lowest. It minimizes baud rate errors especially on low UART clock frequencies.

For example, if uartclk is 33000000 and baud is 115200 the ratio is about 17.9 The current code selects 17 (5% error) but should select 18 (0.5% error).

On the same lines as following:
http://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.9-rc3/2.6.9-rc3-mm2/broken-out/serial-pick-nearest-baud-rate-divider.patch

---
 drivers/tty/serial/8250_early.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/8250_early.c +1 -1
index eaafb98..cfc46b7 100644
@@ -140,7 +140,7 @@ static void __init init_port(struct early_serial8250_device *device)
 	serial_out(port, UART_FCR, 0);		/* no fifo */
 	serial_out(port, UART_MCR, 0x3);	/* DTR + RTS */
 
-	divisor = port->uartclk / (16 * device->baud);
+	divisor = (port->uartclk + (8 * device->baud)) / (16 *	device->baud);
 	c = serial_in(port, UART_LCR);
 	serial_out(port, UART_LCR, c | UART_LCR_DLAB);
 	serial_out(port, UART_DLL, divisor & 0xff);
Signature · 1 line
1.7.9.5
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · On Fri, 28 Sep 2012 16:19:08 +0400 Alexey Brodkin…

Modify divisor to select the nearest baud rate divider rather than the lowest. It minimizes baud rate errors especially on low UART clock frequencies.

Quoted 8 lines

For example, if uartclk is 33000000 and baud is 115200 the ratio is about 17.9 The current code selects 17 (5% error) but should select 18 (0.5% error).

On the same lines as following:
http://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.9-rc3/2.6.9-rc3-mm2/broken-out/serial-pick-nearest-baud-rate-divider.patch

Seems sensible to me.

#2 lore raw
In reply to Alexey Brodkin
Quoted 21 lines · Alexey Brodkin

Modify divisor to select the nearest baud rate divider rather than the lowest. It minimizes baud rate errors especially on low UART clock frequencies.

For example, if uartclk is 33000000 and baud is 115200 the ratio is about 17.9 The current code selects 17 (5% error) but should select 18 (0.5% error).

On the same lines as following:
http://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.9-rc3/2.6.9-rc3-mm2/broken-out/serial-pick-nearest-baud-rate-divider.patch

---
 drivers/tty/serial/8250_early.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/8250_early.c +0 -0
index eaafb98..cfc46b7 100644

drivers/tty/serial/8250_early.c static void __init init_port(struct early_serial8250_device *device) · line 143

Quoted 5 lines
drivers/tty/serial/8250_early.c +1 -1
@@ -140,7 +140,7 @@ static void __init init_port(struct early_serial8250_device *device)
 	serial_out(port, UART_FCR, 0);		/* no fifo */
 	serial_out(port, UART_MCR, 0x3);	/* DTR + RTS */
 
-	divisor = port->uartclk / (16 * device->baud);
+	divisor = (port->uartclk + (8 * device->baud)) / (16 *	device->baud);

So this should be in fact DIV_ROUND_CLOSEST(), right?

But anyway I'm missing any explanation of *why* this is needed? This should be part of the commit log. Is there any bug you are fixing here?

thanks,

Signature · 2 lines
js
suse labs
#3 lore raw
Quoted 25 lines · Jiri Slaby
Quoted 24 lines · Alexey Brodkin

Modify divisor to select the nearest baud rate divider rather than the lowest. It minimizes baud rate errors especially on low UART clock frequencies.

For example, if uartclk is 33000000 and baud is 115200 the ratio is about 17.9 The current code selects 17 (5% error) but should select 18 (0.5% error).

On the same lines as following:
http://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.9-rc3/2.6.9-rc3-mm2/broken-out/serial-pick-nearest-baud-rate-divider.patch

---
 drivers/tty/serial/8250_early.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/8250_early.c +0 -0
index eaafb98..cfc46b7 100644
@@ -140,7 +140,7 @@ static void __init init_port(struct early_serial8250_device *device)
 	serial_out(port, UART_FCR, 0);		/* no fifo */
 	serial_out(port, UART_MCR, 0x3);	/* DTR + RTS */
 
Quoted 6 lines

drivers/tty/serial/8250_early.c static void __init init_port(struct early_serial8250_device *device) · line 143

Quoted 2 lines
drivers/tty/serial/8250_early.c +1 -1
-	divisor = port->uartclk / (16 * device->baud);
+	divisor = (port->uartclk + (8 * device->baud)) / (16 *	device->baud);

So this should be in fact DIV_ROUND_CLOSEST(), right?

But anyway I'm missing any explanation of *why* this is needed? This should be part of the commit log. Is there any bug you are fixing here?

Actually the issue showed up when using the stock 8250 driver for Synopsys DW UART. This was on a FPGA with ~50MHz clk. When we enabled early serial, we saw garbage which Alexey narrowed down to the rounding error. So the bug had been latent and it only showed up with such low clk rates.

Thx,
Vineet

#4 lore raw
Quoted 30 lines · Vineet Gupta
Quoted 29 lines · Jiri Slaby
Quoted 21 lines · Alexey Brodkin

Modify divisor to select the nearest baud rate divider rather than the lowest. It minimizes baud rate errors especially on low UART clock frequencies.

For example, if uartclk is 33000000 and baud is 115200 the ratio is about 17.9 The current code selects 17 (5% error) but should select 18 (0.5% error).

On the same lines as following:
http://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.9-rc3/2.6.9-rc3-mm2/broken-out/serial-pick-nearest-baud-rate-divider.patch

---
 drivers/tty/serial/8250_early.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/8250_early.c +0 -0
index eaafb98..cfc46b7 100644

drivers/tty/serial/8250_early.c static void __init init_port(struct early_serial8250_device *device) · line 143

Quoted 5 lines
drivers/tty/serial/8250_early.c +1 -1
@@ -140,7 +140,7 @@ static void __init init_port(struct early_serial8250_device *device)
 	serial_out(port, UART_FCR, 0);		/* no fifo */
 	serial_out(port, UART_MCR, 0x3);	/* DTR + RTS */
 
-	divisor = port->uartclk / (16 * device->baud);
+	divisor = (port->uartclk + (8 * device->baud)) / (16 *	device->baud);

So this should be in fact DIV_ROUND_CLOSEST(), right?

Quoted 8 lines
Quoted 2 lines

But anyway I'm missing any explanation of *why* this is needed? This should be part of the commit log. Is there any bug you are fixing here?

Actually the issue showed up when using the stock 8250 driver for Synopsys DW UART. This was on a FPGA with ~50MHz clk. When we enabled early serial, we saw garbage which Alexey narrowed down to the rounding error. So the bug had been latent and it only showed up with such low clk rates.

Yes, this is a perfect argument I was asking for and should have been a part of the commit...

thanks,

Signature · 2 lines
js
suse labs
#5 lore raw
Quoted 33 lines · Jiri Slaby
Quoted 32 lines · Vineet Gupta
Quoted 31 lines · Jiri Slaby
Quoted 21 lines · Alexey Brodkin

Modify divisor to select the nearest baud rate divider rather than the lowest. It minimizes baud rate errors especially on low UART clock frequencies.

For example, if uartclk is 33000000 and baud is 115200 the ratio is about 17.9 The current code selects 17 (5% error) but should select 18 (0.5% error).

On the same lines as following:
http://www.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.9-rc3/2.6.9-rc3-mm2/broken-out/serial-pick-nearest-baud-rate-divider.patch

---
 drivers/tty/serial/8250_early.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/8250_early.c +0 -0
index eaafb98..cfc46b7 100644

drivers/tty/serial/8250_early.c static void __init init_port(struct early_serial8250_device *device) · line 143

Quoted 5 lines
drivers/tty/serial/8250_early.c +1 -1
@@ -140,7 +140,7 @@ static void __init init_port(struct early_serial8250_device *device)
 	serial_out(port, UART_FCR, 0);		/* no fifo */
 	serial_out(port, UART_MCR, 0x3);	/* DTR + RTS */
 
-	divisor = port->uartclk / (16 * device->baud);
+	divisor = (port->uartclk + (8 * device->baud)) / (16 *	device->baud);

So this should be in fact DIV_ROUND_CLOSEST(), right?

But anyway I'm missing any explanation of *why* this is needed? This should be part of the commit log. Is there any bug you are fixing here?

Quoted 7 lines
Quoted 5 lines

Actually the issue showed up when using the stock 8250 driver for Synopsys DW UART. This was on a FPGA with ~50MHz clk. When we enabled early serial, we saw garbage which Alexey narrowed down to the rounding error. So the bug had been latent and it only showed up with such low clk rates.

Yes, this is a perfect argument I was asking for and should have been a part of the commit...

First timer's anxiety I suppose ! Per your DIV_ROUND_CLOSEST() suggestion, Alexey has already sent out a v2 patch. I presume he needs to respin a v3 to beef up the commitlog along the lines of above !

Thx,
-Vineet