Skip to contentSkip to conversation

tty/serial: atmel: fix out of range clock divider handling

PATCH Superseded David Engraf 4 participants 9 messages Dec 11, 2019 – Dec 16, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tty/serial: atmel: fix out of range clock divider handling

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Fix by writing the mode register after calculating the baudrate.

---
 drivers/tty/serial/atmel_serial.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/tty/serial/atmel_serial.c +3 -3
index a8dc8af83f39..9983e2fabbac 100644
@@ -2270,9 +2270,6 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 		mode |= ATMEL_US_USMODE_NORMAL;
 	}
 
-	/* set the mode, clock divisor, parity, stop bits and data size */
-	atmel_uart_writel(port, ATMEL_US_MR, mode);
-
 	/*
 	 * when switching the mode, set the RTS line state according to the
 	 * new mode, otherwise keep the former state
@@ -2315,6 +2312,9 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 	}
 	quot = cd | fp << ATMEL_US_FP_OFFSET;
 
+	/* set the mode, clock divisor, parity, stop bits and data size */
+	atmel_uart_writel(port, ATMEL_US_MR, mode);
+
 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Signature · 1 line
2.17.1
#1 lore raw

Hi,

Le 11/12/2019 à 17:29, David Engraf a écrit :

Quoted 6 lines

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Fix by writing the mode register after calculating the baudrate.

It seems that this bug was introduced by:
commit 5bf5635ac170 ("tty/serial: atmel: add fractional baud rate support")

Could you add the "Fixes:" header ?

Ludovic, could you check if this was your intent at the time ?

drivers/tty/serial/atmel_serial.c static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, · line 2272

Quoted 12 lines
---
 drivers/tty/serial/atmel_serial.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/tty/serial/atmel_serial.c +0 -0
index a8dc8af83f39..9983e2fabbac 100644
@@ -2270,9 +2270,6 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 		mode |= ATMEL_US_USMODE_NORMAL;
 	}
 

I think it's better to mo move the "Set baud rate" block here (cf bellow)

drivers/tty/serial/atmel_serial.c static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, · line 2317

Quoted 13 lines
drivers/tty/serial/atmel_serial.c +3 -3
-	/* set the mode, clock divisor, parity, stop bits and data size */
-	atmel_uart_writel(port, ATMEL_US_MR, mode);
-
 	/*
 	 * when switching the mode, set the RTS line state according to the
 	 * new mode, otherwise keep the former state
@@ -2315,6 +2312,9 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 	}
 	quot = cd | fp << ATMEL_US_FP_OFFSET;
 
+	/* set the mode, clock divisor, parity, stop bits and data size */
+	atmel_uart_writel(port, ATMEL_US_MR, mode);
+

I think your patch is good, but I'll be happier if instead of moving those 2 lines here, the whole "Set the baud rate" block was moved before "atmel_uart_writel(port, ATMEL_US_MR, mode);"

That's because at line 2291 the ATMEL_US_CR register is set with ATMEL_US_RTSDIS or ATMEL_US_RTSEN.
And those 2 values have a different effect depending on US_MR.USART_MODE

Quoting from the relase manual:
RTSEN:
1: Drives RTS pin to 1 if US_MR.USART_MODE field = 2, else drives RTS pin to 0 if US_MR.USART_MODE field = 0.

RTSDIS:
1: Drives RTS pin to 0 if US_MR.USART_MODE field = 2, else drives RTS pin to 1 if US_MR.USART_MODE field = 0.

So, I think it's better to set the mode register before setting the control register.

drivers/tty/serial/atmel_serial.c static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, · line 2320

Quoted 4 lines
drivers/tty/serial/atmel_serial.c +0 -0
 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);

Thanks !

#2 lore raw

Hi,

Quoted 6 lines · Richard Genoud

Hi,

Le 11/12/2019 à 17:29, David Engraf a écrit :

Quoted 3 lines

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Quoted 8 lines
Quoted 3 lines

Fix by writing the mode register after calculating the baudrate.

It seems that this bug was introduced by:
commit 5bf5635ac170 ("tty/serial: atmel: add fractional baud rate support")

Could you add the "Fixes:" header ?

Sure.

Quoted 42 lines

Ludovic, could you check if this was your intent at the time ?

drivers/tty/serial/atmel_serial.c static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, · line 2272

Quoted 12 lines
---
  drivers/tty/serial/atmel_serial.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/tty/serial/atmel_serial.c +0 -0
index a8dc8af83f39..9983e2fabbac 100644
@@ -2270,9 +2270,6 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
  		mode |= ATMEL_US_USMODE_NORMAL;
  	}
 

I think it's better to mo move the "Set baud rate" block here (cf bellow)

drivers/tty/serial/atmel_serial.c static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, · line 2317

Quoted 13 lines
drivers/tty/serial/atmel_serial.c +3 -3
-	/* set the mode, clock divisor, parity, stop bits and data size */
-	atmel_uart_writel(port, ATMEL_US_MR, mode);
-
  	/*
  	 * when switching the mode, set the RTS line state according to the
  	 * new mode, otherwise keep the former state
@@ -2315,6 +2312,9 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
  	}
  	quot = cd | fp << ATMEL_US_FP_OFFSET;
 
+	/* set the mode, clock divisor, parity, stop bits and data size */
+	atmel_uart_writel(port, ATMEL_US_MR, mode);
+

I think your patch is good, but I'll be happier if instead of moving those 2 lines here, the whole "Set the baud rate" block was moved before "atmel_uart_writel(port, ATMEL_US_MR, mode);"

That's because at line 2291 the ATMEL_US_CR register is set with ATMEL_US_RTSDIS or ATMEL_US_RTSEN.
And those 2 values have a different effect depending on US_MR.USART_MODE

Quoting from the relase manual:
RTSEN:
1: Drives RTS pin to 1 if US_MR.USART_MODE field = 2, else drives RTS pin to 0 if US_MR.USART_MODE field = 0.

Quoted 6 lines

RTSDIS:
1: Drives RTS pin to 0 if US_MR.USART_MODE field = 2, else drives RTS pin to 1 if US_MR.USART_MODE field = 0.

So, I think it's better to set the mode register before setting the control register.

I fully agree, the RTS pin configuration depends on USART_MODE. I will make a new version of the patch.

Thanks
- David

Quoted 8 lines

drivers/tty/serial/atmel_serial.c static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, · line 2320

Quoted 5 lines
drivers/tty/serial/atmel_serial.c +0 -0
  	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
  		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
  	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);

Thanks !

#3 lore raw

PATCH v2 1/1 tty/serial: atmel: fix out of range clock divider handling

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Fix by doing the baud rate calulation before setting the mode.

Fixes 5bf5635ac170 ("tty/serial: atmel: add fractional baud rate support")
---
 drivers/tty/serial/atmel_serial.c | 43 ++++++++++++++++---------------
 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/tty/serial/atmel_serial.c +22 -21
index a8dc8af83f39..1ba9bc667e13 100644
@@ -2270,27 +2270,6 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 		mode |= ATMEL_US_USMODE_NORMAL;
 	}
 
-	/* set the mode, clock divisor, parity, stop bits and data size */
-	atmel_uart_writel(port, ATMEL_US_MR, mode);
-
-	/*
-	 * when switching the mode, set the RTS line state according to the
-	 * new mode, otherwise keep the former state
-	 */
-	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
-		unsigned int rts_state;
-
-		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
-			/* let the hardware control the RTS line */
-			rts_state = ATMEL_US_RTSDIS;
-		} else {
-			/* force RTS line to low level */
-			rts_state = ATMEL_US_RTSEN;
-		}
-
-		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
-	}
-
 	/*
 	 * Set the baud rate:
 	 * Fractional baudrate allows to setup output frequency more
@@ -2317,6 +2296,28 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 
 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
+
+	/* set the mode, clock divisor, parity, stop bits and data size */
+	atmel_uart_writel(port, ATMEL_US_MR, mode);
+
+	/*
+	 * when switching the mode, set the RTS line state according to the
+	 * new mode, otherwise keep the former state
+	 */
+	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
+		unsigned int rts_state;
+
+		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
+			/* let the hardware control the RTS line */
+			rts_state = ATMEL_US_RTSDIS;
+		} else {
+			/* force RTS line to low level */
+			rts_state = ATMEL_US_RTSEN;
+		}
+
+		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
+	}
+
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
 	atmel_port->tx_stopped = false;
Signature · 1 line
2.17.1
maintainer
#4 lore raw
Quoted 10 lines · David Engraf

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Fix by doing the baud rate calulation before setting the mode.

Fixes 5bf5635ac170 ("tty/serial: atmel: add fractional baud rate support")
---
 drivers/tty/serial/atmel_serial.c | 43 ++++++++++++++++---------------
 1 file changed, 22 insertions(+), 21 deletions(-)

What changed from v1?

Always put that below the --- line.

v3 please?

thanks,

greg k-h

#5 lore raw
Quoted 12 lines · Greg KH
Quoted 10 lines · David Engraf

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Fix by doing the baud rate calulation before setting the mode.

Fixes 5bf5635ac170 ("tty/serial: atmel: add fractional baud rate support")
---
  drivers/tty/serial/atmel_serial.c | 43 ++++++++++++++++---------------
  1 file changed, 22 insertions(+), 21 deletions(-)
Quoted 3 lines

What changed from v1?

Always put that below the --- line.

Oh sorry.

Quoted 1 line

v3 please?

Sure.

Best regards
- David

Quoted 4 lines

thanks,

greg k-h

#6 lore raw

PATCH v3 1/1 tty/serial: atmel: fix out of range clock divider handling

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Fix by doing the baud rate calulation before setting the mode.

Fixes 5bf5635ac170 ("tty/serial: atmel: add fractional baud rate support")

---
Changes since v1:
- moves set baud rate block before setting the mode register because ATMEL_US_RTSDIS and ATMEL_US_RTSEN depend on ATMEL_US_MR.mode

---
 drivers/tty/serial/atmel_serial.c | 43 ++++++++++++++++---------------
 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/tty/serial/atmel_serial.c +22 -21
index a8dc8af83f39..1ba9bc667e13 100644
@@ -2270,27 +2270,6 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 		mode |= ATMEL_US_USMODE_NORMAL;
 	}
 
-	/* set the mode, clock divisor, parity, stop bits and data size */
-	atmel_uart_writel(port, ATMEL_US_MR, mode);
-
-	/*
-	 * when switching the mode, set the RTS line state according to the
-	 * new mode, otherwise keep the former state
-	 */
-	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
-		unsigned int rts_state;
-
-		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
-			/* let the hardware control the RTS line */
-			rts_state = ATMEL_US_RTSDIS;
-		} else {
-			/* force RTS line to low level */
-			rts_state = ATMEL_US_RTSEN;
-		}
-
-		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
-	}
-
 	/*
 	 * Set the baud rate:
 	 * Fractional baudrate allows to setup output frequency more
@@ -2317,6 +2296,28 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 
 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
+
+	/* set the mode, clock divisor, parity, stop bits and data size */
+	atmel_uart_writel(port, ATMEL_US_MR, mode);
+
+	/*
+	 * when switching the mode, set the RTS line state according to the
+	 * new mode, otherwise keep the former state
+	 */
+	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
+		unsigned int rts_state;
+
+		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
+			/* let the hardware control the RTS line */
+			rts_state = ATMEL_US_RTSDIS;
+		} else {
+			/* force RTS line to low level */
+			rts_state = ATMEL_US_RTSEN;
+		}
+
+		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
+	}
+
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
 	atmel_port->tx_stopped = false;
Signature · 1 line
2.17.1
Acked-by on v3 1/1
#7 lore raw

Le 16/12/2019 à 09:54, David Engraf a écrit :

Quoted 7 lines

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Fix by doing the baud rate calulation before setting the mode.

Fixes 5bf5635ac170 ("tty/serial: atmel: add fractional baud rate support")
Quoted 61 lines

---
Changes since v1:
- moves set baud rate block before setting the mode register because ATMEL_US_RTSDIS and ATMEL_US_RTSEN depend on ATMEL_US_MR.mode

---
 drivers/tty/serial/atmel_serial.c | 43 ++++++++++++++++---------------
 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/tty/serial/atmel_serial.c +15 -21
index a8dc8af83f39..1ba9bc667e13 100644
@@ -2270,27 +2270,6 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 		mode |= ATMEL_US_USMODE_NORMAL;
 	}
 
-	/* set the mode, clock divisor, parity, stop bits and data size */
-	atmel_uart_writel(port, ATMEL_US_MR, mode);
-
-	/*
-	 * when switching the mode, set the RTS line state according to the
-	 * new mode, otherwise keep the former state
-	 */
-	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
-		unsigned int rts_state;
-
-		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
-			/* let the hardware control the RTS line */
-			rts_state = ATMEL_US_RTSDIS;
-		} else {
-			/* force RTS line to low level */
-			rts_state = ATMEL_US_RTSEN;
-		}
-
-		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
-	}
-
 	/*
 	 * Set the baud rate:
 	 * Fractional baudrate allows to setup output frequency more
@@ -2317,6 +2296,28 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 
 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
+
+	/* set the mode, clock divisor, parity, stop bits and data size */
+	atmel_uart_writel(port, ATMEL_US_MR, mode);
+
+	/*
+	 * when switching the mode, set the RTS line state according to the
+	 * new mode, otherwise keep the former state
+	 */
+	if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
+		unsigned int rts_state;
+
+		if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
+			/* let the hardware control the RTS line */
+			rts_state = ATMEL_US_RTSDIS;
+		} else {

drivers/tty/serial/atmel_serial.c static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, · line 2323

Quoted 10 lines
drivers/tty/serial/atmel_serial.c +7 -0
+			/* force RTS line to low level */
+			rts_state = ATMEL_US_RTSEN;
+		}
+
+		atmel_uart_writel(port, ATMEL_US_CR, rts_state);
+	}
+
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
 	atmel_port->tx_stopped = false;

Thanks !

Acked-by on v3 1/1
#8 lore raw
In reply to David Engraf
Quoted 7 lines · David Engraf

Use MCK_DIV8 when the clock divider is > 65535. Unfortunately the mode register was already written thus the clock selection is ignored.

Fix by doing the baud rate calulation before setting the mode.

Fixes 5bf5635ac170 ("tty/serial: atmel: add fractional baud rate support")

Thanks for the fix

Regards

Quoted 73 lines

---
Changes since v1:
- moves set baud rate block before setting the mode register because ATMEL_US_RTSDIS and ATMEL_US_RTSEN depend on ATMEL_US_MR.mode

---
 drivers/tty/serial/atmel_serial.c | 43 ++++++++++++++++---------------
 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/tty/serial/atmel_serial.c +22 -22
index a8dc8af83f39..1ba9bc667e13 100644
@@ -2270,27 +2270,6 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
                mode |= ATMEL_US_USMODE_NORMAL;
        }
 
-       /* set the mode, clock divisor, parity, stop bits and data size */
-       atmel_uart_writel(port, ATMEL_US_MR, mode);
-
-       /*
-        * when switching the mode, set the RTS line state according to the
-        * new mode, otherwise keep the former state
-        */
-       if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
-               unsigned int rts_state;
-
-               if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
-                       /* let the hardware control the RTS line */
-                       rts_state = ATMEL_US_RTSDIS;
-               } else {
-                       /* force RTS line to low level */
-                       rts_state = ATMEL_US_RTSEN;
-               }
-
-               atmel_uart_writel(port, ATMEL_US_CR, rts_state);
-       }
-
        /*
         * Set the baud rate:
         * Fractional baudrate allows to setup output frequency more
@@ -2317,6 +2296,28 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 
        if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
                atmel_uart_writel(port, ATMEL_US_BRGR, quot);
+
+       /* set the mode, clock divisor, parity, stop bits and data size */
+       atmel_uart_writel(port, ATMEL_US_MR, mode);
+
+       /*
+        * when switching the mode, set the RTS line state according to the
+        * new mode, otherwise keep the former state
+        */
+       if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
+               unsigned int rts_state;
+
+               if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
+                       /* let the hardware control the RTS line */
+                       rts_state = ATMEL_US_RTSDIS;
+               } else {
+                       /* force RTS line to low level */
+                       rts_state = ATMEL_US_RTSEN;
+               }
+
+               atmel_uart_writel(port, ATMEL_US_CR, rts_state);
+       }
+
        atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
        atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
        atmel_port->tx_stopped = false;
--

2.17.1