Skip to contentSkip to conversation

serial: 8250_dw: retain programmed timing for BUSY waits

PATCH In review khaliforce 4 participants 5 messages Oct 12

Subsystems (3)
lore
Share & tools
lore

khaliforce

author
#0 lore raw

PATCH 1/1 serial: 8250_dw: retain programmed timing for BUSY waits

Baud changes update frame_time before the programmed divisor changes. A switch to a faster baud rate therefore shortens the DesignWare BUSY wait.
The wait can expire while a frame at the old baud rate remains active.

Use the programmed frame duration for FIFO and BUSY waits. Account for UART clock changes.

Fixes a7b9ce39fbe4 ("serial: 8250_dw: Ensure BUSY is deasserted")

---
Compared the complete patch off/on on MangoPi MQ Pro UART0. Kept other port patches, kernel options and DTB identical. Tested BUSY changes from 9600, 1200 and 300 to 115200. All 11 paired changes fail without the patch and pass with it. Each trial returns 64 matching loopback bytes.

 drivers/tty/serial/8250/8250_dw.c    | 20 ++++++++++++++++++--
 drivers/tty/serial/8250/8250_dwlib.c |  4 ++++
 drivers/tty/serial/8250/8250_dwlib.h |  4 ++++
 3 files changed, 26 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_dw.c +18 -2
index 51d026f20825..03be503bcef0 100644
@@ -17,6 +17,8 @@
 #include <linux/device.h>
 #include <linux/io.h>
 #include <linux/lockdep.h>
+#include <linux/math64.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
@@ -131,6 +133,9 @@ static int dw8250_idle_enter(struct uart_port *p)
 	struct dw8250_data *d = to_dw8250_data(p->private_data);
 	unsigned int usr_reg = d->pdata ? d->pdata->usr_reg : DW_UART_USR;
 	struct uart_8250_port *up = up_to_u8250p(p);
+	unsigned int frame_time = d->data.frame_time ?: p->frame_time;
+	unsigned int saved_frame_time = p->frame_time;
+	u64 scaled_frame_time;
 	int retries;
 	u32 lsr;
 
@@ -139,6 +144,12 @@ static int dw8250_idle_enter(struct uart_port *p)
 	if (d->uart_16550_compatible)
 		return 0;
 
+	if (d->data.frame_time && d->data.frame_time_uartclk && p->uartclk) {
+		scaled_frame_time = (u64)frame_time * d->data.frame_time_uartclk;
+		scaled_frame_time = DIV_ROUND_UP_ULL(scaled_frame_time, p->uartclk);
+		frame_time = min_t(u64, scaled_frame_time, U32_MAX);
+	}
+
 	d->in_idle = 1;
 
 	/* Prevent triggering interrupt from RBR filling */
@@ -156,8 +167,10 @@ static int dw8250_idle_enter(struct uart_port *p)
 	 *
 	 * FIXME: frame_time delay is too long with very low baudrates.
 	 */
+	p->frame_time = frame_time;
 	serial8250_fifo_wait_for_lsr_thre(up, NULL, p->fifosize);
-	ndelay(p->frame_time);
+	p->frame_time = saved_frame_time;
+	ndelay(frame_time);
 
 	serial_port_out(p, UART_MCR, up->mcr | UART_MCR_LOOP);
 
@@ -167,7 +180,7 @@ static int dw8250_idle_enter(struct uart_port *p)
 		if (!(serial_port_in(p, usr_reg) & DW_UART_USR_BUSY))
 			break;
 		/* FIXME: frame_time delay is too long with very low baudrates. */
-		ndelay(p->frame_time);
+		ndelay(frame_time);
 	} while (--retries);
 
 	lsr = serial_lsr_in(up);
@@ -188,6 +201,7 @@ static int dw8250_idle_enter(struct uart_port *p)
 static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
 			       unsigned int quot, unsigned int quot_frac)
 {
+	struct dw8250_port_data *d = p->private_data;
 	struct uart_8250_port *up = up_to_u8250p(p);
 	int ret;
 
@@ -200,6 +214,8 @@ static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
 		goto idle_failed;
 
 	serial_dl_write(up, quot);
+	d->frame_time = p->frame_time;
+	d->frame_time_uartclk = p->uartclk;
 	serial_port_out(p, UART_LCR, up->lcr);
 
 idle_failed:
drivers/tty/serial/8250/8250_dwlib.c +4 -0
index 8859e66d2d71..d74ff203f904 100644
@@ -39,8 +39,12 @@ static unsigned int dw8250_get_divisor(struct uart_port *p, unsigned int baud,
 static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
 			       unsigned int quot, unsigned int quot_frac)
 {
+	struct dw8250_port_data *d = p->private_data;
+
 	dw8250_writel_ext(p, DW_UART_DLF, quot_frac);
 	serial8250_do_set_divisor(p, baud, quot);
+	d->frame_time = p->frame_time;
+	d->frame_time_uartclk = p->uartclk;
 }
 
 void dw8250_do_set_termios(struct uart_port *p, struct ktermios *termios,
drivers/tty/serial/8250/8250_dwlib.h +4 -0
index 1fe52332e774..5472c13ca92e 100644
@@ -88,6 +88,10 @@ struct dw8250_port_data {
 	u32			cpr_value;
 	u8			dlf_size;
 
+	/* Timing for the programmed divisor. */
+	u32			frame_time;
+	u32			frame_time_uartclk;
+
 	/* RS485 variables */
 	bool			hw_rs485_support;
 };
Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

Baud changes update frame_time before the programmed divisor changes. A switch to a faster baud rate therefore shortens the DesignWare BUSY wait.
The wait can expire while a frame at the old baud rate remains active.

Use the programmed frame duration for FIFO and BUSY waits. Account for UART clock changes.

Fixes a7b9ce39fbe4 ("serial: 8250_dw: Ensure BUSY is deasserted")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/1791785420.8010663.945898876521967.uart-busy-frame-time@gmail.com?part=1
#2 lore raw
In reply to khaliforce
Quoted 8 lines · khaliforce

Baud changes update frame_time before the programmed divisor changes. A switch to a faster baud rate therefore shortens the DesignWare BUSY wait.
The wait can expire while a frame at the old baud rate remains active.

Use the programmed frame duration for FIFO and BUSY waits. Account for UART clock changes.

Quoted 8 lines
Fixes a7b9ce39fbe4 ("serial: 8250_dw: Ensure BUSY is deasserted")

---
Compared the complete patch off/on on MangoPi MQ Pro UART0. Kept other port patches, kernel options and DTB identical. Tested BUSY changes from 9600, 1200 and 300 to 115200. All 11 paired changes fail without the patch and pass with it. Each trial returns 64 matching loopback bytes.

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- It looks like you did not use your "real" name for the patch on either the Signed-off-by: line, or the From: line (both of which have to match). Please read the kernel file, Documentation/process/submitting-patches.rst for how to do this correctly.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot

#3 lore raw
In reply to khaliforce

drivers/tty/serial/8250/8250_dw.c static int dw8250_idle_enter(struct uart_port *p) · line 150

Quoted 4 lines · khaliforce
drivers/tty/serial/8250/8250_dw.c +4 -0
+	if (d->data.frame_time && d->data.frame_time_uartclk && p->uartclk) {
+		scaled_frame_time = (u64)frame_time * d->data.frame_time_uartclk;
+		scaled_frame_time = DIV_ROUND_UP_ULL(scaled_frame_time, p->uartclk);
+		frame_time = min_t(u64, scaled_frame_time, U32_MAX);

This looks like a LLM wrote this, as min_t() is what they love to use. Are you sure this is correct?

thanks,

greg k-h

maintainer
#4 lore raw
In reply to khaliforce
Quoted 1 line · khaliforce

Baud changes update frame_time before the programmed divisor changes.

Why not fix this problem instead? It would seem wrong to have wrong value in ->frame_time.

--
i.

Quoted 128 lines

A switch to a faster baud rate therefore shortens the DesignWare BUSY wait.
The wait can expire while a frame at the old baud rate remains active.

Use the programmed frame duration for FIFO and BUSY waits. Account for UART clock changes.

Fixes a7b9ce39fbe4 ("serial: 8250_dw: Ensure BUSY is deasserted")

---
Compared the complete patch off/on on MangoPi MQ Pro UART0. Kept other port patches, kernel options and DTB identical. Tested BUSY changes from 9600, 1200 and 300 to 115200. All 11 paired changes fail without the patch and pass with it. Each trial returns 64 matching loopback bytes.

 drivers/tty/serial/8250/8250_dw.c    | 20 ++++++++++++++++++--
 drivers/tty/serial/8250/8250_dwlib.c |  4 ++++
 drivers/tty/serial/8250/8250_dwlib.h |  4 ++++
 3 files changed, 26 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_dw.c +18 -2
index 51d026f20825..03be503bcef0 100644
@@ -17,6 +17,8 @@
 #include <linux/device.h>
 #include <linux/io.h>
 #include <linux/lockdep.h>
+#include <linux/math64.h>
+#include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
@@ -131,6 +133,9 @@ static int dw8250_idle_enter(struct uart_port *p)
 	struct dw8250_data *d = to_dw8250_data(p->private_data);
 	unsigned int usr_reg = d->pdata ? d->pdata->usr_reg : DW_UART_USR;
 	struct uart_8250_port *up = up_to_u8250p(p);
+	unsigned int frame_time = d->data.frame_time ?: p->frame_time;
+	unsigned int saved_frame_time = p->frame_time;
+	u64 scaled_frame_time;
 	int retries;
 	u32 lsr;
 
@@ -139,6 +144,12 @@ static int dw8250_idle_enter(struct uart_port *p)
 	if (d->uart_16550_compatible)
 		return 0;
 
+	if (d->data.frame_time && d->data.frame_time_uartclk && p->uartclk) {
+		scaled_frame_time = (u64)frame_time * d->data.frame_time_uartclk;
+		scaled_frame_time = DIV_ROUND_UP_ULL(scaled_frame_time, p->uartclk);
+		frame_time = min_t(u64, scaled_frame_time, U32_MAX);
+	}
+
 	d->in_idle = 1;
 
 	/* Prevent triggering interrupt from RBR filling */
@@ -156,8 +167,10 @@ static int dw8250_idle_enter(struct uart_port *p)
 	 *
 	 * FIXME: frame_time delay is too long with very low baudrates.
 	 */
+	p->frame_time = frame_time;
 	serial8250_fifo_wait_for_lsr_thre(up, NULL, p->fifosize);
-	ndelay(p->frame_time);
+	p->frame_time = saved_frame_time;
+	ndelay(frame_time);
 
 	serial_port_out(p, UART_MCR, up->mcr | UART_MCR_LOOP);
 
@@ -167,7 +180,7 @@ static int dw8250_idle_enter(struct uart_port *p)
 		if (!(serial_port_in(p, usr_reg) & DW_UART_USR_BUSY))
 			break;
 		/* FIXME: frame_time delay is too long with very low baudrates. */
-		ndelay(p->frame_time);
+		ndelay(frame_time);
 	} while (--retries);
 
 	lsr = serial_lsr_in(up);
@@ -188,6 +201,7 @@ static int dw8250_idle_enter(struct uart_port *p)
 static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
 			       unsigned int quot, unsigned int quot_frac)
 {
+	struct dw8250_port_data *d = p->private_data;
 	struct uart_8250_port *up = up_to_u8250p(p);
 	int ret;
 
@@ -200,6 +214,8 @@ static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
 		goto idle_failed;
 
 	serial_dl_write(up, quot);
+	d->frame_time = p->frame_time;
+	d->frame_time_uartclk = p->uartclk;
 	serial_port_out(p, UART_LCR, up->lcr);
 
 idle_failed:
drivers/tty/serial/8250/8250_dwlib.c +4 -0
index 8859e66d2d71..d74ff203f904 100644
@@ -39,8 +39,12 @@ static unsigned int dw8250_get_divisor(struct uart_port *p, unsigned int baud,
 static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
 			       unsigned int quot, unsigned int quot_frac)
 {
+	struct dw8250_port_data *d = p->private_data;
+
 	dw8250_writel_ext(p, DW_UART_DLF, quot_frac);
 	serial8250_do_set_divisor(p, baud, quot);
+	d->frame_time = p->frame_time;
+	d->frame_time_uartclk = p->uartclk;
 }
 
 void dw8250_do_set_termios(struct uart_port *p, struct ktermios *termios,
drivers/tty/serial/8250/8250_dwlib.h +4 -0
index 1fe52332e774..5472c13ca92e 100644
@@ -88,6 +88,10 @@ struct dw8250_port_data {
 	u32			cpr_value;
 	u8			dlf_size;
 
+	/* Timing for the programmed divisor. */
+	u32			frame_time;
+	u32			frame_time_uartclk;
+
 	/* RS485 variables */
 	bool			hw_rs485_support;
 };