Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177639675
D61610.1794920161.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
D61610.1794920161.diff
View Options
diff --git a/sys/dev/uart/uart_dev_ns8250.h b/sys/dev/uart/uart_dev_ns8250.h
--- 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.
*/
diff --git a/sys/dev/uart/uart_dev_ns8250.c b/sys/dev/uart/uart_dev_ns8250.c
--- a/sys/dev/uart/uart_dev_ns8250.c
+++ b/sys/dev/uart/uart_dev_ns8250.c
@@ -167,6 +167,10 @@
}
}
+/*
+ * 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 @@
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 @@
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 @@
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 @@
/* 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 @@
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 @@
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 @@
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
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 17, 12:56 PM (23 h, 34 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41799643
Default Alt Text
D61610.1794920161.diff (3 KB)
Attached To
Mode
D61610: uart: Factor out the ns8250 baud rate generator code
Attached
Detach File
Event Timeline