git: 4097cd06ca49 - main - uart/ns8250: Factor out reading the divisor

From: Warner Losh <imp_at_FreeBSD.org>
Date: Mon, 14 Oct 2024 22:17:36 UTC
The branch main has been updated by imp:

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

commit 4097cd06ca49a4a212c25177007509838c7d04a9
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-10-14 21:57:44 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-10-14 22:03:58 +0000

    uart/ns8250: Factor out reading the divisor
    
    We have two copies (soon to be three) of reading the divisor. Since it's
    a complicated tricky process, abstract it to its own routine.
    
    Sponsored by:           Netflix
    Reviewed by:            andrew, markj
    Differential Revision:  https://reviews.freebsd.org/D47074
---
 sys/dev/uart/uart_dev_ns8250.c | 27 +++++++++++++++------------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/sys/dev/uart/uart_dev_ns8250.c b/sys/dev/uart/uart_dev_ns8250.c
index 9f88a43f462a..d43a48f319e4 100644
--- a/sys/dev/uart/uart_dev_ns8250.c
+++ b/sys/dev/uart/uart_dev_ns8250.c
@@ -126,11 +126,11 @@ ns8250_clrint(struct uart_bas *bas)
 	}
 }
 
-static int
-ns8250_delay(struct uart_bas *bas)
+static uint32_t
+ns8250_get_divisor(struct uart_bas *bas)
 {
-	int divisor;
-	u_char lcr;
+	uint32_t divisor;
+	uint8_t lcr;
 
 	lcr = uart_getreg(bas, REG_LCR);
 	uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
@@ -140,6 +140,16 @@ ns8250_delay(struct uart_bas *bas)
 	uart_setreg(bas, REG_LCR, lcr);
 	uart_barrier(bas);
 
+	return (divisor);
+}
+
+static int
+ns8250_delay(struct uart_bas *bas)
+{
+	int divisor;
+
+	divisor = ns8250_get_divisor(bas);
+
 	/* 1/10th the time to transmit 1 character (estimate). */
 	if (divisor <= 134)
 		return (16000000 * divisor / bas->rclk);
@@ -727,14 +737,7 @@ ns8250_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
 		uart_barrier(bas);
 		break;
 	case UART_IOCTL_BAUD:
-		lcr = uart_getreg(bas, REG_LCR);
-		uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
-		uart_barrier(bas);
-		divisor = uart_getreg(bas, REG_DLL) |
-		    (uart_getreg(bas, REG_DLH) << 8);
-		uart_barrier(bas);
-		uart_setreg(bas, REG_LCR, lcr);
-		uart_barrier(bas);
+		divisor = ns8250_get_divisor(bas);
 		baudrate = (divisor > 0) ? bas->rclk / divisor / 16 : 0;
 		if (baudrate > 0)
 			*(int*)data = baudrate;