aboutsummaryrefslogtreecommitdiff
path: root/lib/utils/serial/uart8250.c
diff options
context:
space:
mode:
authorAndrew Jones <ajones@ventanamicro.com>2022-07-18 19:20:28 +0200
committerAnup Patel <anup@brainfault.org>2022-07-30 11:39:42 +0530
commitf27203525aa2fc20d0074feb892a6f8433f84e3a (patch)
treedb3f886de35c8bd270564e97278ae9ab08833d95 /lib/utils/serial/uart8250.c
parent7198e1d06f486173e6565ff6c718f30bf8c2ff30 (diff)
downloadopensbi-f27203525aa2fc20d0074feb892a6f8433f84e3a.zip
opensbi-f27203525aa2fc20d0074feb892a6f8433f84e3a.tar.gz
opensbi-f27203525aa2fc20d0074feb892a6f8433f84e3a.tar.bz2
lib: utils/serial: Ensure baudrate is non-zero before using
RISC-V doesn't generate exceptions on divide-by-zero, but the result, all bits set, is not likely what people expect either. In all cases where we divide by baudrate there's a chance it's zero (when the DT it came from is "bad"). To avoid difficult to debug situations, leave baudrate dependent registers alone when baudrate is zero, as, also in all cases, it appears we can skip initialization of those registers and still [hopefully] have a functioning UART. Signed-off-by: Andrew Jones <ajones@ventanamicro.com> Reviewed-by: Anup Patel <anup@brainfault.org>
Diffstat (limited to 'lib/utils/serial/uart8250.c')
-rw-r--r--lib/utils/serial/uart8250.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/lib/utils/serial/uart8250.c b/lib/utils/serial/uart8250.c
index 38ea11a..99bf1bf 100644
--- a/lib/utils/serial/uart8250.c
+++ b/lib/utils/serial/uart8250.c
@@ -93,7 +93,7 @@ static struct sbi_console_device uart8250_console = {
int uart8250_init(unsigned long base, u32 in_freq, u32 baudrate, u32 reg_shift,
u32 reg_width, u32 reg_offset)
{
- u16 bdiv;
+ u16 bdiv = 0;
uart8250_base = (volatile char *)base + reg_offset;
uart8250_reg_shift = reg_shift;
@@ -101,7 +101,10 @@ 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 + 8 * uart8250_baudrate) / (16 * uart8250_baudrate);
+ if (uart8250_baudrate) {
+ bdiv = (uart8250_in_freq + 8 * uart8250_baudrate) /
+ (16 * uart8250_baudrate);
+ }
/* Disable all interrupts */
set_reg(UART_IER_OFFSET, 0x00);