git: e7723c78614a - main - uart: Factor out the ns8250 baud rate generator code

From: Hayzam Sherif <hayzam_at_FreeBSD.org>
Date: Fri, 20 Nov 2026 01:13:39 UTC
The branch main has been updated by hayzam:

URL: https://cgit.FreeBSD.org/src/commit/?id=e7723c78614aa6fb4f51eb60982c33550d81c61f

commit e7723c78614aa6fb4f51eb60982c33550d81c61f
Author:     Hayzam Sherif <hayzam@FreeBSD.org>
AuthorDate: 2026-11-16 06:48:51 +0000
Commit:     Hayzam Sherif <hayzam@FreeBSD.org>
CommitDate: 2026-11-20 01:10:53 +0000

    uart: Factor out the ns8250 baud rate generator code
    
    Move the code that programs the divisor latch out of ns8250_param()
    into ns8250_set_baudrate(), and the code that computes the rate from
    it out of the UART_IOCTL_BAUD handler into ns8250_get_baudrate().
    
    ns8250_get_divisor() now returns the input clock cycles per bit,
    prescaler included, in units of 1 / NS8250_DIVISOR_SCALE, and the
    rate, the delays and the rclk guess are derived from it.  This leaves
    room for other prescalers and for fractional dividers.
    
    ns8250_delay() is now computed in 64 bits with one formula.  The old
    code split at a divisor of 134 to keep 16000000 * divisor within an int
    (ebecffe93038), and above 134 divided by rclk / 1000 rounded down.  With
    a clock that is not a multiple of 1 kHz this made the delay longer by
    (rclk % 1000) / (rclk - rclk % 1000): 0.011% with a 1.8432 MHz clock,
    e.g. 20002 instead of 20000 us at 50 baud.  Up to a divisor of 134 the
    result is the same.  The delay is only a polling interval for
    ns8250_drain() and the FIFO size probe.
    
    No other functional change intended.
    
    Reviewed by:    mmel
    Approved by:    markj (mentor)
    Sponsored by:   Alchemilla Ventures Private Limited
    Differential Revision:  https://reviews.freebsd.org/D61610
---
 sys/dev/uart/uart_dev_ns8250.c | 68 ++++++++++++++++++++++++++++++------------
 sys/dev/uart/uart_dev_ns8250.h |  6 ++++
 2 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/sys/dev/uart/uart_dev_ns8250.c b/sys/dev/uart/uart_dev_ns8250.c
index 60377c0a1e96..e00be7a187c6 100644
--- a/sys/dev/uart/uart_dev_ns8250.c
+++ b/sys/dev/uart/uart_dev_ns8250.c
@@ -167,6 +167,10 @@ ns8250_clrint(struct uart_bas *bas)
 	}
 }
 
+/*
+ * The divisor in use: input clock cycles per bit, prescaler included, in
+ * units of 1 / NS8250_DIVISOR_SCALE.  0 if the divisor latch is 0.
+ */
 static uint32_t
 ns8250_get_divisor(struct uart_bas *bas)
 {
@@ -181,20 +185,20 @@ ns8250_get_divisor(struct uart_bas *bas)
 	uart_setreg(bas, REG_LCR, lcr);
 	uart_barrier(bas);
 
-	return (divisor);
+	/* The 16550 prescaler divides by 16. */
+	return (divisor * 16 * NS8250_DIVISOR_SCALE);
 }
 
 static int
 ns8250_delay(struct uart_bas *bas)
 {
-	int divisor;
+	uint32_t divisor;
 
 	divisor = ns8250_get_divisor(bas);
 
-	/* 1/10th the time to transmit 1 character (estimate). */
-	if (divisor <= 134)
-		return (16000000 * divisor / bas->rclk);
-	return (16000 * divisor / (bas->rclk / 1000));
+	/* 1/10th the time to transmit 1 character (estimate): one bit. */
+	return ((uint64_t)divisor * (1000000 / NS8250_DIVISOR_SCALE) /
+	    bas->rclk);
 }
 
 static int
@@ -221,6 +225,38 @@ ns8250_divisor(int rclk, int baudrate)
 	return (divisor);
 }
 
+static int
+ns8250_get_baudrate(struct uart_bas *bas)
+{
+	uint32_t divisor;
+
+	divisor = ns8250_get_divisor(bas);
+	if (divisor == 0)
+		return (0);
+	return ((uint64_t)bas->rclk * NS8250_DIVISOR_SCALE / divisor);
+}
+
+/* Set the rate.  Nothing is changed if the rate cannot be set. */
+static int
+ns8250_set_baudrate(struct uart_bas *bas, int baudrate)
+{
+	int divisor;
+	uint8_t lcr;
+
+	divisor = ns8250_divisor(bas->rclk, baudrate);
+	if (divisor == 0)
+		return (EINVAL);
+	lcr = uart_getreg(bas, REG_LCR);
+	uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
+	uart_barrier(bas);
+	uart_setreg(bas, REG_DLL, divisor & 0xff);
+	uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
+	uart_barrier(bas);
+	uart_setreg(bas, REG_LCR, lcr);
+	uart_barrier(bas);
+	return (0);
+}
+
 static int
 ns8250_drain(struct uart_bas *bas, int what)
 {
@@ -315,7 +351,7 @@ static int
 ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
     int parity)
 {
-	int divisor;
+	int error;
 	uint8_t lcr;
 
 	/* Don't change settings when running on Hyper-V */
@@ -337,14 +373,9 @@ ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
 
 	/* Set baudrate if we know a rclk and both are not 0. */
 	if (baudrate > 0 && bas->rclk > 0) {
-		divisor = ns8250_divisor(bas->rclk, baudrate);
-		if (divisor == 0)
-			return (EINVAL);
-		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
-		uart_barrier(bas);
-		uart_setreg(bas, REG_DLL, divisor & 0xff);
-		uart_setreg(bas, REG_DLH, (divisor >> 8) & 0xff);
-		uart_barrier(bas);
+		error = ns8250_set_baudrate(bas, baudrate);
+		if (error != 0)
+			return (error);
 	}
 
 	/* Set LCR and clear DLAB. */
@@ -421,7 +452,7 @@ ns8250_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
 		uint32_t div;
 
 		div = ns8250_get_divisor(bas);
-		bas->rclk = baudrate * div * 16;
+		bas->rclk = (uint64_t)baudrate * div / NS8250_DIVISOR_SCALE;
 	}
 
 	/*
@@ -766,7 +797,7 @@ int
 ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
 {
 	struct uart_bas *bas;
-	int baudrate, divisor, error;
+	int baudrate, error;
 	uint8_t efr, lcr;
 
 	bas = &sc->sc_bas;
@@ -813,8 +844,7 @@ ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
 		uart_barrier(bas);
 		break;
 	case UART_IOCTL_BAUD:
-		divisor = ns8250_get_divisor(bas);
-		baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
+		baudrate = ns8250_get_baudrate(bas);
 		if (baudrate > 0)
 			*(int*)data = baudrate;
 		else
diff --git a/sys/dev/uart/uart_dev_ns8250.h b/sys/dev/uart/uart_dev_ns8250.h
index e8a17e96c268..5f4399282930 100644
--- a/sys/dev/uart/uart_dev_ns8250.h
+++ b/sys/dev/uart/uart_dev_ns8250.h
@@ -29,6 +29,12 @@
 #ifndef _DEV_UART_DEV_NS8250_H_
 #define _DEV_UART_DEV_NS8250_H_
 
+/*
+ * Divisors are given in units of 1 / NS8250_DIVISOR_SCALE input clock
+ * cycles per bit, so that prescalers and fractional dividers fit.
+ */
+#define	NS8250_DIVISOR_SCALE	100
+
 /*
  * High-level UART interface.
  */