mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH] serial: lpuart: Avoid division by zero when requested baudrate is
Date: Tue,  8 May 2018 14:15:02 -0700	[thread overview]
Message-ID: <20180508211502.5476-1-andrew.smirnov@gmail.com> (raw)

With serdev device support added there's now a corner case where:

 1. There is a DT node for a serdev device on one of the UARTs
 2. There is no driver that binds against serdev device's compatibility
    string

with 1 and 2 being true it is possible to end up in a situation where
a particualr UART has not been initalized to any baudrate when
clock_notifier_call_chain() gets called. This effectively translates
to

	set_baudrate(uart, 0);

which for LPUART driver result in a division by zero.

To avoid this problem, convert lpuart_serial_setbaudrate() to treat
zero baudrate as a request to disable the UART. While we are at it add
a BUG_ON() to lpuart_setbrg() to simplify finding any future bugs.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 drivers/serial/serial_lpuart.c | 13 ++++++++-----
 include/serial/lpuart.h        |  2 ++
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/serial/serial_lpuart.c b/drivers/serial/serial_lpuart.c
index f28035a32..246fc3d3a 100644
--- a/drivers/serial/serial_lpuart.c
+++ b/drivers/serial/serial_lpuart.c
@@ -68,11 +68,14 @@ static int lpuart_serial_setbaudrate(struct console_device *cdev,
 
 	lpuart_enable(lpuart, false);
 
-	lpuart_setbrg(lpuart->base,
-		      clk_get_rate(lpuart->clk),
-		      baudrate);
-
-	lpuart_enable(lpuart, true);
+	/*
+	 * We treat baudrate of 0 as a request to disable UART
+	 */
+	if (baudrate) {
+		lpuart_setbrg(lpuart->base, clk_get_rate(lpuart->clk),
+			      baudrate);
+		lpuart_enable(lpuart, true);
+	}
 
 	lpuart->baudrate = baudrate;
 
diff --git a/include/serial/lpuart.h b/include/serial/lpuart.h
index a920291de..9c6e271eb 100644
--- a/include/serial/lpuart.h
+++ b/include/serial/lpuart.h
@@ -228,6 +228,8 @@ static inline void lpuart_setbrg(void __iomem *base,
 	unsigned int bfra;
 	u16 sbr;
 
+	BUG_ON(!baudrate);
+
 	sbr = (u16) (refclock / (16 * baudrate));
 
 	writeb(sbr >> 8,   base + UARTBDH);
-- 
2.17.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

             reply	other threads:[~2018-05-08 21:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-08 21:15 Andrey Smirnov [this message]
2018-05-14  7:14 ` Sascha Hauer
2018-05-15  0:20   ` Andrey Smirnov
2018-05-15  6:07     ` Sascha Hauer
2018-08-11 19:35       ` Andrey Smirnov
2018-08-13  7:14         ` Sascha Hauer

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=20180508211502.5476-1-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@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