From nobody Fri Nov 20 01:13:39 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jVy5c0JgWz71pWg for ; Fri, 20 Nov 2026 01:13:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jVy5b6MChz4YBk for ; Fri, 20 Nov 2026 01:13:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795137219; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PByX3VDUm9q+48ZsI2JW2oZsagG+psfhAafyaWRZXnU=; b=yUA7hXMTQ0M3mimyBzOim+ya30E4Nwji4fPweSH0UUSywxpy94Uu0+DJg8gPRDtXImKrk5 5d0hruRRBxOhg8SUIUOd4G7gP2UJcWe2KQ3L8y+92jyy6YwiCFcQI7+IboJ3D4wcaz4WR2 2p3KXNoCEwIxaxdbkoSKLwtHV8+HoW1oRkSYJk7EziyqcyV/xnborQU0I4tEgRu2uG9KGY n3n0yoWbmIQr2rMPkqR0eGmrReFohOKR2NSLaNuA+U9lKgITx0JfYiQ7ZQzn06df9XVUnC dWiV6BO60hNsITNLUQ6YEM2miqEi1wth03TPoUpuGUZ/YCTeIEUs0J55Ze8aCw== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795137219; b=nww+XkWWzLCnP+FtFZZzKPVZwrowt1+yRSDO1Wo57W3yIOyVkffGJUZotTHvoem0AzHD9X O8yK3cm/FFqn4DRAcFlT1UqhY8LU4//UsqIeuGXdyLgYBOjuhVfZoCFU6F3H50+gomogOp /N920WINTndTEpIj9I6XDv9g1869G9OjbZnMVX0ryx7A5bqPsB+YaDkm4Eeoj+plNyf5Le jsp1coGOq/mSHw6Pp1NPFcL5OZs/BfnJYP/m8LOvZN1LiAfTDCZLU0wAaKzDLtIaq7nJWu 1q2apK5/aAn4Sbg8nCLJK/l/EzqKIMupAc/H2sAR35OKapFrcM03r/+jR6QlTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795137219; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PByX3VDUm9q+48ZsI2JW2oZsagG+psfhAafyaWRZXnU=; b=xz5HHxYXlM6efyANDXNr8HdQJSlZkUSYMKwiuAnXF0qot8W7j9DZBeyDz3mARzbyDWof5X wflCYiLpJ1V0nbUZnQ+1n8vdWHZD8ElXmK0x+JR8qVHhC6wr3rlNf+14NC4BQOPbz9mMqe KffCwixD+eUgxze5nPsmxtKVOa2wv6Xbm7A3WStJrUm3nSjYFdN0JTNGBB3ki0I+d7WxMP RFOq2CJkoFV10seQmPWI7XRzBI8cKH4CXAakA/UaGt0JMveCvHwHnNeSMJ8vFEd06eyJj0 QmB+7CAcFIgu5A+NJRNCPDMrPuxH/rUftN7uGfim68/ZJAa9j2yt6eCgqAR8lw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jVy5b61NSzQPw for ; Fri, 20 Nov 2026 01:13:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1775 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Fri, 20 Nov 2026 01:13:39 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Hayzam Sherif Subject: git: e7723c78614a - main - uart: Factor out the ns8250 baud rate generator code List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: hayzam X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: e7723c78614aa6fb4f51eb60982c33550d81c61f Auto-Submitted: auto-generated Date: Fri, 20 Nov 2026 01:13:39 +0000 Message-Id: <6aff9ec3.1775.79373942@gitrepo.freebsd.org> The branch main has been updated by hayzam: URL: https://cgit.FreeBSD.org/src/commit/?id=e7723c78614aa6fb4f51eb60982c33550d81c61f commit e7723c78614aa6fb4f51eb60982c33550d81c61f Author: Hayzam Sherif AuthorDate: 2026-11-16 06:48:51 +0000 Commit: Hayzam Sherif 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. */