Skip to contentSkip to conversation

serial/8250/8250_early: Prevent rounding error in uartclk

PATCH In review Alexey Brodkin 3 participants 4 messages Oct 3, 2012 – Oct 5, 2012 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 serial/8250/8250_early: Prevent rounding error in uartclk

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).

This 5% error in baud rate leads to garbage on receiving end, while 0.5% doesn't.

The issue showed up when using the stock 8250 driver for Synopsys DW UART. This was on a FPGA with ~12MHz UART clock. When we enabled early serial, we saw garbage which was narrowed down to the rounding error.

So the bug had been latent and it only showed up with such low cock rates.

---
 drivers/tty/serial/8250/8250_early.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/8250/8250_early.c +1 -1
index eaafb98..843a150 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 = DIV_ROUND_CLOSEST(port->uartclk, 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
#1 lore raw
Quoted 2 lines

So the bug had been latent and it only showed up with such low cock rates.

clock ?

Perhaps Greg can tweak the description as it goes in - otherwise all fine with me

#2 lore raw
Quoted 8 lines · Alan Cox
Quoted 2 lines

So the bug had been latent and it only showed up with such low cock rates.

clock ?

Perhaps Greg can tweak the description as it goes in - otherwise all fine with me

Sorry for this.
Essentially I meant "clock".
May do another respin if needed.

maintainer
#3 lore raw
Quoted 4 lines · Alexey Brodkin
Quoted 3 lines · Alan Cox
Quoted 2 lines

So the bug had been latent and it only showed up with such low cock rates.

Quoted 8 lines
Quoted 5 lines

clock ?

Perhaps Greg can tweak the description as it goes in - otherwise all fine with me

Sorry for this.
Essentially I meant "clock".
May do another respin if needed.

Don't worry, I can edit it :)

greg k-h