OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <ajones@ventanamicro.com>
To: opensbi@lists.infradead.org
Subject: [PATCH 4/4] lib: utils/serial: Ensure baudrate is non-zero before using
Date: Mon, 18 Jul 2022 19:20:28 +0200	[thread overview]
Message-ID: <20220718172028.2006166-5-ajones@ventanamicro.com> (raw)
In-Reply-To: <20220718172028.2006166-1-ajones@ventanamicro.com>

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>
---
 lib/utils/serial/gaisler-uart.c | 2 +-
 lib/utils/serial/shakti-uart.c  | 8 ++++++--
 lib/utils/serial/sifive-uart.c  | 2 +-
 lib/utils/serial/uart8250.c     | 7 +++++--
 4 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/lib/utils/serial/gaisler-uart.c b/lib/utils/serial/gaisler-uart.c
index 5f30ee43b719..eec75cca0c98 100644
--- a/lib/utils/serial/gaisler-uart.c
+++ b/lib/utils/serial/gaisler-uart.c
@@ -70,7 +70,7 @@ int gaisler_uart_init(unsigned long base, u32 in_freq, u32 baudrate)
 	uart_base = (volatile char *)base;
 
 	/* Configure baudrate */
-	if (in_freq)
+	if (in_freq && baudrate)
 		set_reg(UART_REG_SCALER, in_freq / (baudrate * 8 + 7));
 
 	ctrl = get_reg(UART_REG_CTRL);
diff --git a/lib/utils/serial/shakti-uart.c b/lib/utils/serial/shakti-uart.c
index 5f2fe75c9f33..2f09f141b9cf 100644
--- a/lib/utils/serial/shakti-uart.c
+++ b/lib/utils/serial/shakti-uart.c
@@ -47,8 +47,12 @@ static struct sbi_console_device shakti_console = {
 int shakti_uart_init(unsigned long base, u32 in_freq, u32 baudrate)
 {
 	uart_base = (volatile char *)base;
-	u16 baud = (u16)(in_freq/(16 * baudrate));
-	writew(baud, uart_base + REG_BAUD);
+	u16 baud;
+
+	if (baudrate) {
+		baud = (u16)(in_freq / (16 * baudrate));
+		writew(baud, uart_base + REG_BAUD);
+	}
 
 	sbi_console_set_device(&shakti_console);
 
diff --git a/lib/utils/serial/sifive-uart.c b/lib/utils/serial/sifive-uart.c
index 7078611a5274..3581d47a6d4b 100644
--- a/lib/utils/serial/sifive-uart.c
+++ b/lib/utils/serial/sifive-uart.c
@@ -97,7 +97,7 @@ int sifive_uart_init(unsigned long base, u32 in_freq, u32 baudrate)
 	uart_baudrate = baudrate;
 
 	/* Configure baudrate */
-	if (in_freq)
+	if (in_freq && baudrate)
 		set_reg(UART_REG_DIV, uart_min_clk_divisor(in_freq, baudrate));
 
 	/* Disable interrupts */
diff --git a/lib/utils/serial/uart8250.c b/lib/utils/serial/uart8250.c
index 38ea11af4d31..99bf1bf733f2 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);
-- 
2.36.1



  parent reply	other threads:[~2022-07-18 17:20 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-18 17:20 [PATCH 0/4] lib: utils/serial: Collection of UART code improvements Andrew Jones
2022-07-18 17:20 ` [PATCH 1/4] lib: utils/fdt: Factor out common uart node code Andrew Jones
2022-07-30  5:24   ` Anup Patel
2022-07-30 10:19     ` Anup Patel
2022-07-18 17:20 ` [PATCH 2/4] lib: utils/serial: Initialize platform_uart_data to zero Andrew Jones
2022-07-18 17:42   ` Jessica Clarke
2022-07-18 18:35     ` Andrew Jones
2022-07-18 18:39       ` Jessica Clarke
2022-07-19 10:30         ` Andrew Jones
2022-07-30  5:25   ` Anup Patel
2022-07-30 10:20     ` Anup Patel
2022-07-18 17:20 ` [PATCH 3/4] lib: serial: Clean up coding style in sifive-uart.c Andrew Jones
2022-07-20 15:13   ` Xiang W
2022-07-30  5:27   ` Anup Patel
2022-07-30 10:20     ` Anup Patel
2022-07-18 17:20 ` Andrew Jones [this message]
2022-07-20 15:12   ` [PATCH 4/4] lib: utils/serial: Ensure baudrate is non-zero before using Xiang W
2022-07-20 17:16     ` Andrew Jones
2022-07-20 17:25       ` Xiang W
2022-07-21  5:35         ` Andrew Jones
2022-07-30  5:31   ` Anup Patel
2022-07-30 10:21     ` Anup Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220718172028.2006166-5-ajones@ventanamicro.com \
    --to=ajones@ventanamicro.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox