Page MenuHomeFreeBSD

D61610.1794918153.diff
No OneTemporary

Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None

D61610.1794918153.diff

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,39 @@
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 for the frame format in lcr, and leave LCR set to lcr.
+ * Nothing is changed if the rate cannot be set.
+ */
+static int
+ns8250_set_baudrate(struct uart_bas *bas, int baudrate, uint8_t lcr)
+{
+ int divisor;
+
+ 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);
+ uart_setreg(bas, REG_LCR, lcr);
+ uart_barrier(bas);
+ return (0);
+}
+
static int
ns8250_drain(struct uart_bas *bas, int what)
{
@@ -315,7 +352,6 @@
ns8250_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
int parity)
{
- int divisor;
uint8_t lcr;
/* Don't change settings when running on Hyper-V */
@@ -336,16 +372,8 @@
lcr |= parity << 3;
/* 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);
- }
+ if (baudrate > 0 && bas->rclk > 0)
+ return (ns8250_set_baudrate(bas, baudrate, lcr));
/* Set LCR and clear DLAB. */
uart_setreg(bas, REG_LCR, lcr);
@@ -421,7 +449,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 +794,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 +841,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

Mime Type
text/plain
Expires
Tue, Nov 17, 12:22 PM (23 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41797263
Default Alt Text
D61610.1794918153.diff (3 KB)

Event Timeline