The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] serial: tegra: do the baud tolerance math in 64 bits
@ 2026-11-22  5:11 EMCP333 via B4 Relay
  2026-11-22  9:30 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: EMCP333 via B4 Relay @ 2026-11-22  5:11 UTC (permalink / raw)
  To: Laxman Dewangan, Greg Kroah-Hartman, Jiri Slaby
  Cc: Thierry Reding, Jonathan Hunter, Krishna Yarlagadda, linux-serial,
	linux-tegra, linux-kernel, EMCP333

From: EMCP333 <emcp@enser.io>

On a 32-bit kernel both halves of the baud tolerance logic overflow
`long`, so a Tegra20/30/114/124 board whose DT has an
nvidia,adjust-baud-rates entry runs its fast UART rates at the wrong
speed, and the range check then reports the wrong speed as fine.

tegra_get_tolerance_rate() returns rate + rate * tolerance / 10000, with
rate = 16 x baud. For any baud above ~986 kbaud, rate * tolerance
exceeds 2^31 and wraps, so the adjustment comes out negative and the
clock divider rounds the request down a further half step. On a Tegra
Note 7 (pll_p 408 MHz, nvidia,adjust-baud-rates = <1000000 4000000 136>),
read back from clk_summary:

    baud      wanted uartc   got uartc     actual baud   error
    3000000   48652800       45333334      2833333       -5.56 %
    1500000   24326400       23314286      1457143       -2.86 %

Both "got" values follow exactly from the wrapped arithmetic. With 64-bit
math the clock comes out at 48000000 and 24000000, i.e. 3000000 and
1500000 baud exactly.

tegra_check_rate_in_range() is meant to catch this, and cannot: it
computes (long)(configured - required) * 10000, which wraps for any
delta above ~215 kHz. The -5.56 % above reads as -0.18 % and passes the
window, so "configured baud rate is out of range" is never printed.

Found through Bluetooth: hci_ll moves a WL18xx on uartc to 3 Mbaud after
the firmware download, the host lands 5.56 % low, and the controller
never answers the next command (HCI_Reset times out). With this change
the controller comes up at 3 Mbaud and scans. Tested on that board with
an otherwise unmodified driver on a next-20260422-based kernel; 64-bit
kernels are unaffected, as long is 64 bits there.

Use div_s64() for both expressions so the result does not depend on the
width of long.

The code and this changelog were prepared with the help of an AI coding
assistant. The submitter has reviewed both and takes responsibility for
them.

Fixes: f04a3cc8d455 ("serial: tegra: add support to adjust baud rate")
Fixes: d781ec21bae6 ("serial: tegra: report clk rate errors")
Signed-off-by: EMCP333 <emcp@enser.io>
---
 drivers/tty/serial/serial-tegra.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/tty/serial/serial-tegra.c b/drivers/tty/serial/serial-tegra.c
index e93873a1d1..b5973c0a95 100644
--- a/drivers/tty/serial/serial-tegra.c
+++ b/drivers/tty/serial/serial-tegra.c
@@ -18,6 +18,7 @@
 #include <linux/err.h>
 #include <linux/io.h>
 #include <linux/irq.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/pagemap.h>
@@ -365,11 +366,13 @@ static long tegra_get_tolerance_rate(struct tegra_uart_port *tup,
 {
 	int i;
 
+	/* 64-bit: rate * tolerance overflows a 32-bit long */
 	for (i = 0; i < tup->n_adjustable_baud_rates; ++i) {
 		if (baud >= tup->baud_tolerance[i].lower_range_baud &&
 		    baud <= tup->baud_tolerance[i].upper_range_baud)
-			return (rate + (rate *
-				tup->baud_tolerance[i].tolerance) / 10000);
+			return rate + div_s64((s64)rate *
+					      tup->baud_tolerance[i].tolerance,
+					      10000);
 	}
 
 	return rate;
@@ -379,8 +382,9 @@ static int tegra_check_rate_in_range(struct tegra_uart_port *tup)
 {
 	long diff;
 
-	diff = ((long)(tup->configured_rate - tup->required_rate) * 10000)
-		/ tup->required_rate;
+	/* 64-bit: delta * 10000 overflows a 32-bit long above ~215 kHz */
+	diff = div_s64((s64)(tup->configured_rate - tup->required_rate) * 10000,
+		       tup->required_rate);
 	if (diff < (tup->cdata->error_tolerance_low_range * 100) ||
 	    diff > (tup->cdata->error_tolerance_high_range * 100)) {
 		dev_err(tup->uport.dev,

---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261122-b4-serial-tegra-baud-tolerance-fc6308ac8c64

Best regards,
--  
EMCP333 <emcp@enser.io>



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

* Re: [PATCH] serial: tegra: do the baud tolerance math in 64 bits
  2026-11-22  5:11 [PATCH] serial: tegra: do the baud tolerance math in 64 bits EMCP333 via B4 Relay
@ 2026-11-22  9:30 ` Greg Kroah-Hartman
       [not found]   ` <CAOY-3nCEb=fMMwHyecpvs5jVTod90tG7L8HvbDWZ+OgBfSJY0A@mail.gmail.com>
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-22  9:30 UTC (permalink / raw)
  To: emcp
  Cc: Laxman Dewangan, Jiri Slaby, Thierry Reding, Jonathan Hunter,
	Krishna Yarlagadda, linux-serial, linux-tegra, linux-kernel

On Sun, Nov 22, 2026 at 05:11:23AM +0000, EMCP333 via B4 Relay wrote:
> From: EMCP333 <emcp@enser.io>

Real name please.

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

* Re: [PATCH] serial: tegra: do the baud tolerance math in 64 bits
       [not found]   ` <CAOY-3nCEb=fMMwHyecpvs5jVTod90tG7L8HvbDWZ+OgBfSJY0A@mail.gmail.com>
@ 2026-11-22 15:34     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-22 15:34 UTC (permalink / raw)
  To: EMCP333
  Cc: Laxman Dewangan, Jiri Slaby, Thierry Reding, Jonathan Hunter,
	Krishna Yarlagadda, linux-serial, linux-tegra, linux-kernel


A: http://en.wikipedia.org/wiki/Top_post
Q: Were do I find info about this thing called top-posting?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?

A: No.
Q: Should I include quotations after my reply?

http://daringfireball.net/2007/07/on_top

On Sun, Nov 22, 2026 at 07:07:00AM -0800, EMCP333 wrote:
> Hi Greg
> 
> It's Erik Peterson
> 
> Been my first attempt to work on the kernel with NURA (formerly
> postmarketOS, which doesn't take AI submissions directly) and I think it
> clearly stated when to indicate AI use which I believe I did.

The issue is you need to sign off and put From: using your real name,
there was not any issue with using a LLM here at all.

Please fix up and resend.

thanks,

greg k-h

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

end of thread, other threads:[~2026-11-22 15:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22  5:11 [PATCH] serial: tegra: do the baud tolerance math in 64 bits EMCP333 via B4 Relay
2026-11-22  9:30 ` Greg Kroah-Hartman
     [not found]   ` <CAOY-3nCEb=fMMwHyecpvs5jVTod90tG7L8HvbDWZ+OgBfSJY0A@mail.gmail.com>
2026-11-22 15:34     ` Greg Kroah-Hartman

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