public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot-Users] [PATCH] Fix integer overflow warning in calc_divisor()
@ 2008-07-11 14:24 Hugo Villeneuve
  2008-07-13 13:14 ` Wolfgang Denk
  0 siblings, 1 reply; 10+ messages in thread
From: Hugo Villeneuve @ 2008-07-11 14:24 UTC (permalink / raw)
  To: u-boot

Fix the integer overflow warning when rounding the serial
port clock divisor value in calc_divisor().

Signed-off-by: Hugo Villeneuve <hugo.villeneuve@lyrtech.com>

---

 drivers/serial/serial.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c
index 182ca2d..4ccaee2 100644
--- a/drivers/serial/serial.c
+++ b/drivers/serial/serial.c
@@ -124,6 +124,8 @@ static NS16550_t serial_ports[4] = {
 
 static int calc_divisor (NS16550_t port)
 {
+	uint32_t clk_divisor;
+
 #ifdef CONFIG_OMAP1510
 	/* If can't cleanly clock 115200 set div to 1 */
 	if ((CFG_NS16550_CLK == 12000000) && (gd->baudrate == 115200)) {
@@ -147,10 +149,15 @@ static int calc_divisor (NS16550_t port)
 
 	/* Compute divisor value. Normally, we should simply return:
 	 *   CFG_NS16550_CLK) / MODE_X_DIV / gd->baudrate
-	 * but we need to round that value by adding 0.5 or 8/16.
+	 * but we need to round that value by adding 0.5 (2/4).
 	 * Rounding is especially important at high baud rates.
 	 */
-	return (((16 * CFG_NS16550_CLK) / MODE_X_DIV / gd->baudrate) + 8) / 16;
+	clk_divisor = (((4 * CFG_NS16550_CLK) /
+			(MODE_X_DIV * gd->baudrate)) + 2) / 4;
+
+	debug("NS16550 clock divisor = %d\n", clk_divisor);
+
+	return clk_divisor;
 }
 
 #if !defined(CONFIG_SERIAL_MULTI)

^ permalink raw reply related	[flat|nested] 10+ messages in thread
* [U-Boot-Users] [PATCH V2] Round the serial port clock divisor value returned by calc_divisor()
@ 2008-07-08 18:54 Hugo Villeneuve
  2008-07-09 22:49 ` Wolfgang Denk
  2008-07-10 22:39 ` Wolfgang Denk
  0 siblings, 2 replies; 10+ messages in thread
From: Hugo Villeneuve @ 2008-07-08 18:54 UTC (permalink / raw)
  To: u-boot

Round the serial port clock divisor value returned by
calc_divisor().

Signed-off-by: Hugo Villeneuve <hugo.villeneuve@lyrtech.com>
Signed-off-by: John Roberts <john.roberts@pwav.com>

---

Rounding is important, especially when using high baud rates
values like 115200bps. When using the non-rounded value, some
boards will work and some won't.

 drivers/serial/serial.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/drivers/serial/serial.c b/drivers/serial/serial.c
index 76425d8..1192f07 100644
--- a/drivers/serial/serial.c
+++ b/drivers/serial/serial.c
@@ -144,8 +144,12 @@ static int calc_divisor (NS16550_t port)
 #else
 #define MODE_X_DIV 16
 #endif
-	return (CFG_NS16550_CLK / MODE_X_DIV / gd->baudrate);
 
+	/* Compute divisor value. Normally, we should simply return:
+	 *   CFG_NS16550_CLK) / MODE_X_DIV / gd->baudrate
+	 * but we need to round that value by adding 0.5 or 8/16.
+	 * Rounding is especially important at high baud rates. */
+	return (((16 * CFG_NS16550_CLK) / MODE_X_DIV / gd->baudrate) + 8) / 16;
 }
 
 #if !defined(CONFIG_SERIAL_MULTI)

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2008-07-15 15:19 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-07-11 14:24 [U-Boot-Users] [PATCH] Fix integer overflow warning in calc_divisor() Hugo Villeneuve
2008-07-13 13:14 ` Wolfgang Denk
2008-07-14 14:58   ` Hugo Villeneuve
2008-07-14 21:04     ` Wolfgang Denk
2008-07-15  2:44       ` [U-Boot-Users] [PATCH v2] Round the serial port clock divisor value returned by calc_divisor() Jerry Van Baren
2008-07-15 15:19         ` [U-Boot-Users] [PATCH v2] Round the serial port clock divisor valuereturned " Hugo Villeneuve
  -- strict thread matches above, loose matches on Subject: below --
2008-07-08 18:54 [U-Boot-Users] [PATCH V2] Round the serial port clock divisor value returned " Hugo Villeneuve
2008-07-09 22:49 ` Wolfgang Denk
2008-07-10 22:39 ` Wolfgang Denk
2008-07-10 23:54   ` Jerry Van Baren

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox