lib: utils/serial: Round UART8250 baud rate divisor to nearest integer
PATCH 1/1 lib: utils/serial: Round UART8250 baud rate divisor to nearest integer
Previously, it was rounded down and that gives suboptimal results when non-standard clock sources or baud rates are used.
--- lib/utils/serial/uart8250.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/utils/serial/uart8250.c +1 -1
@@ -101,7 +101,7 @@ int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, uart8250_in_freq = in_freq; uart8250_baudrate = baudrate; - bdiv = uart8250_in_freq / (16 * uart8250_baudrate); + bdiv = (uart8250_in_freq +8 * uart8250_baudrate) / (16 * uart8250_baudrate);/* Disable all interrupts */ set_reg (UART_IER_OFFSET ,0x00 );
Signature · 1 line
2.20.1
PATCH 1/1 lib: utils/serial: Round UART8250 baud rate divisor to nearest integer
Quoted 14 lines · Jakub Luzny
Previously, it was rounded down and that gives suboptimal results when non-standard clock sources or baud rates are used.
Signed-off-by Jakub Luzny <[email protected]>---
lib/utils/serial/uart8250.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/utils/serial/uart8250.c +0
-0
lib/utils/serial/uart8250.c int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, · line 104
Quoted 5 lines
lib/utils/serial/uart8250.c +1
-1@@ -101,7 +101,7 @@ int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, uart8250_in_freq = in_freq; uart8250_baudrate = baudrate; - bdiv = uart8250_in_freq / (16 * uart8250_baudrate); + bdiv = (uart8250_in_freq +8 * uart8250_baudrate) / (16 * uart8250_baudrate);
This could do weird things if the numbers are sufficiently large to overflow (unlikely but maybe possible). You can instead use the simpler:
(uart8250_in_freq / (8 * uart8250_baudrate) + 1) / 2
which is approximately what FreeBSD does (it shifts rather than multiplies and divides).
Jess
PATCH 1/1 lib: utils/serial: Round UART8250 baud rate divisor to nearest integer
Yes, this could happen if the UART peripheral would run at clocks around 4GHz, which is really unlikely. My solution is basically what the Linux kernel does (through the DIV_ROUND_CLOSEST macro). I consider it more readable than the FreeBSD-like solution you propose. But if you disagree, I can send a patch following your proposal.
Jakub
Quoted 32 lines · Jessica Clarke
Quoted 14 lines · Jakub Luzny
Previously, it was rounded down and that gives suboptimal results when non-standard clock sources or baud rates are used.
Signed-off-by Jakub Luzny <[email protected]>---
lib/utils/serial/uart8250.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)lib/utils/serial/uart8250.c +0
-0lib/utils/serial/uart8250.c int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, · line 104
Quoted 5 lines
lib/utils/serial/uart8250.c +1
-1@@ -101,7 +101,7 @@ int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, uart8250_in_freq = in_freq; uart8250_baudrate = baudrate; - bdiv = uart8250_in_freq / (16 * uart8250_baudrate); + bdiv = (uart8250_in_freq +8 * uart8250_baudrate) / (16 * uart8250_baudrate);This could do weird things if the numbers are sufficiently large to overflow (unlikely but maybe possible). You can instead use the simpler:
(
uart8250_in_freq/ (8 *uart8250_baudrate) + 1) / 2which is approximately what FreeBSD does (it shifts rather than multiplies and divides).
Jess
Anup Patel
AppliedPATCH 1/1 lib: utils/serial: Round UART8250 baud rate divisor to nearest integer
In reply to Jakub LuznyQuoted 5 lines · Jakub Luzny
Previously, it was rounded down and that gives suboptimal results when non-standard clock sources or baud rates are used.
Signed-off-by Jakub Luzny <[email protected]>
Looks good to me.
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 25 lines
--- lib/utils/serial/uart8250.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/utils/serial/uart8250.c +1
-2@@ -101,7 +101,7 @@ int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift, uart8250_in_freq = in_freq; uart8250_baudrate = baudrate; - bdiv = uart8250_in_freq / (16 * uart8250_baudrate); + bdiv = (uart8250_in_freq +8 * uart8250_baudrate) / (16 * uart8250_baudrate);/* Disable all interrupts */ set_reg (UART_IER_OFFSET ,0x00 ); --2.20.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi