Skip to contentSkip to conversation

tty/serial: atmel: RS485 & ISO7816: wait for TXRDY before sending data

PATCH In review [email protected] 3 participants 5 messages Jan 7, 2020 – Jan 9, 2020

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

PATCH 1/1 tty/serial: atmel: RS485 & ISO7816: wait for TXRDY before sending data

At this moment, TXEMPTY is checked before sending data on RS485 and ISO7816 modes. However, TXEMPTY is risen when FIFO (if used) or the Transmit Shift Register are empty, even though TXRDY might be up and controller is able to receive data. Since the controller sends data only when TXEMPTY is ready, on RS485, when DMA is not used, the RTS pin is driven low after each byte. With this patch, the characters will be transmitted when TXRDY is up and so, RTS pin will remain high between bytes.
The performance improvement on RS485 is about 8% with a baudrate of 300.

---
 drivers/tty/serial/atmel_serial.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/tty/serial/atmel_serial.c +19 -7
index a8dc8af83f39..19c8fb9faa36 100644
@@ -313,7 +313,11 @@ static int atmel_config_rs485(struct uart_port *port,
 
 	if (rs485conf->flags & SER_RS485_ENABLED) {
 		dev_dbg(port->dev, "Setting UART to RS485\n");
-		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+		if (port->rs485.flags & SER_RS485_RX_DURING_TX)
+			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+		else
+			atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+
 		atmel_uart_writel(port, ATMEL_US_TTGR,
 				  rs485conf->delay_rts_after_send);
 		mode |= ATMEL_US_USMODE_RS485;
@@ -831,7 +835,7 @@ static void atmel_tx_chars(struct uart_port *port)
 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
 
 	if (port->x_char &&
-	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
+	    (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) {
 		atmel_uart_write_char(port, port->x_char);
 		port->icount.tx++;
 		port->x_char = 0;
@@ -839,8 +843,7 @@ static void atmel_tx_chars(struct uart_port *port)
 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
 		return;
 
-	while (atmel_uart_readl(port, ATMEL_US_CSR) &
-	       atmel_port->tx_done_mask) {
+	while (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY) {
 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
 		port->icount.tx++;
@@ -851,10 +854,20 @@ static void atmel_tx_chars(struct uart_port *port)
 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
 		uart_write_wakeup(port);
 
-	if (!uart_circ_empty(xmit))
+	if (!uart_circ_empty(xmit)) {
+		/* we still have characters to transmit, so we should continue
+		 * transmitting them when TX is ready, regardless of
+		 * mode or duplexity
+		 */
+		atmel_port->tx_done_mask |= ATMEL_US_TXRDY;
+
 		/* Enable interrupts */
 		atmel_uart_writel(port, ATMEL_US_IER,
 				  atmel_port->tx_done_mask);
+	} else {
+		if (atmel_uart_is_half_duplex(port))
+			atmel_port->tx_done_mask &= ~ATMEL_US_TXRDY;
+	}
 }
 
 static void atmel_complete_tx_dma(void *arg)
@@ -2525,8 +2538,7 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port,
 	 * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or
 	 * ENDTX|TXBUFE
 	 */
-	if (port->rs485.flags & SER_RS485_ENABLED ||
-	    port->iso7816.flags & SER_ISO7816_ENABLED)
+	if (atmel_uart_is_half_duplex(port))
 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
 	else if (atmel_use_pdc_tx(port)) {
 		port->fifosize = PDC_BUFFER_SIZE;
Signature · 1 line
2.20.1
Acked-by on v1 1/1
#1 lore raw

Le 07/01/2020 à 12:17, [email protected] a écrit :

Quoted 10 lines

At this moment, TXEMPTY is checked before sending data on RS485 and ISO7816 modes. However, TXEMPTY is risen when FIFO (if used) or the Transmit Shift Register are empty, even though TXRDY might be up and controller is able to receive data. Since the controller sends data only when TXEMPTY is ready, on RS485, when DMA is not used, the RTS pin is driven low after each byte. With this patch, the characters will be transmitted when TXRDY is up and so, RTS pin will remain high between bytes.
The performance improvement on RS485 is about 8% with a baudrate of 300.

seems ok to me

NB: MS exchange has added some =3D and =20 here and there, but git am doesn't seems to be bothered by them.

Quoted 73 lines
---
 drivers/tty/serial/atmel_serial.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/tty/serial/atmel_serial.c +19 -7
index a8dc8af83f39..19c8fb9faa36 100644
@@ -313,7 +313,11 @@ static int atmel_config_rs485(struct uart_port *port,
 
 	if (rs485conf->flags & SER_RS485_ENABLED) {
 		dev_dbg(port->dev, "Setting UART to RS485\n");
-		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+		if (port->rs485.flags & SER_RS485_RX_DURING_TX)
+			atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+		else
+			atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+
 		atmel_uart_writel(port, ATMEL_US_TTGR,
 				  rs485conf->delay_rts_after_send);
 		mode |= ATMEL_US_USMODE_RS485;
@@ -831,7 +835,7 @@ static void atmel_tx_chars(struct uart_port *port)
 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
 
 	if (port->x_char &&
-	    (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
+	    (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) {
 		atmel_uart_write_char(port, port->x_char);
 		port->icount.tx++;
 		port->x_char = 0;
@@ -839,8 +843,7 @@ static void atmel_tx_chars(struct uart_port *port)
 	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
 		return;
 
-	while (atmel_uart_readl(port, ATMEL_US_CSR) &
-	       atmel_port->tx_done_mask) {
+	while (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY) {
 		atmel_uart_write_char(port, xmit->buf[xmit->tail]);
 		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
 		port->icount.tx++;
@@ -851,10 +854,20 @@ static void atmel_tx_chars(struct uart_port *port)
 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
 		uart_write_wakeup(port);
 
-	if (!uart_circ_empty(xmit))
+	if (!uart_circ_empty(xmit)) {
+		/* we still have characters to transmit, so we should continue
+		 * transmitting them when TX is ready, regardless of
+		 * mode or duplexity
+		 */
+		atmel_port->tx_done_mask |= ATMEL_US_TXRDY;
+
 		/* Enable interrupts */
 		atmel_uart_writel(port, ATMEL_US_IER,
 				  atmel_port->tx_done_mask);
+	} else {
+		if (atmel_uart_is_half_duplex(port))
+			atmel_port->tx_done_mask &= ~ATMEL_US_TXRDY;
+	}
 }
 
 static void atmel_complete_tx_dma(void *arg)
@@ -2525,8 +2538,7 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port,
 	 * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or
 	 * ENDTX|TXBUFE
 	 */
-	if (port->rs485.flags & SER_RS485_ENABLED ||
-	    port->iso7816.flags & SER_ISO7816_ENABLED)
+	if (atmel_uart_is_half_duplex(port))
 		atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
 	else if (atmel_use_pdc_tx(port)) {
 		port->fifosize = PDC_BUFFER_SIZE;
#2 lore raw
Quoted 12 lines · Richard Genoud

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 9 lines · Le 07/01/2020 à 12:17, [email protected] a écrit :

At this moment, TXEMPTY is checked before sending data on RS485 and ISO7816 modes. However, TXEMPTY is risen when FIFO (if used) or the Transmit Shift Register are empty, even though TXRDY might be up and controller is able to receive data. Since the controller sends data only when TXEMPTY is ready, on RS485, when DMA is not used, the RTS pin is driven low after each byte. With this patch, the characters will be transmitted when TXRDY is up and so, RTS pin will remain high between bytes.
The performance improvement on RS485 is about 8% with a baudrate of 300.

Quoted 6 lines
Quoted 1 line

seems ok to me

NB: MS exchange has added some =3D and =20 here and there, but git am doesn't seems to be bothered by them.

We are struggling with some e-mail changes, sorry for that. Thanks for letting me know about this.

Quoted 74 lines
Quoted 73 lines
---
  drivers/tty/serial/atmel_serial.c | 26 +++++++++++++++++++-------
  1 file changed, 19 insertions(+), 7 deletions(-)
drivers/tty/serial/atmel_serial.c +19 -7
index a8dc8af83f39..19c8fb9faa36 100644
@@ -313,7 +313,11 @@ static int atmel_config_rs485(struct uart_port *port,
 
       if (rs485conf->flags & SER_RS485_ENABLED) {
               dev_dbg(port->dev, "Setting UART to RS485\n");
-             atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+             if (port->rs485.flags & SER_RS485_RX_DURING_TX)
+                     atmel_port->tx_done_mask = ATMEL_US_TXRDY;
+             else
+                     atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
+
               atmel_uart_writel(port, ATMEL_US_TTGR,
                                 rs485conf->delay_rts_after_send);
               mode |= ATMEL_US_USMODE_RS485;
@@ -831,7 +835,7 @@ static void atmel_tx_chars(struct uart_port *port)
       struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
 
       if (port->x_char &&
-         (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
+         (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) {
               atmel_uart_write_char(port, port->x_char);
               port->icount.tx++;
               port->x_char = 0;
@@ -839,8 +843,7 @@ static void atmel_tx_chars(struct uart_port *port)
       if (uart_circ_empty(xmit) || uart_tx_stopped(port))
               return;
 
-     while (atmel_uart_readl(port, ATMEL_US_CSR) &
-            atmel_port->tx_done_mask) {
+     while (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY) {
               atmel_uart_write_char(port, xmit->buf[xmit->tail]);
               xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
               port->icount.tx++;
@@ -851,10 +854,20 @@ static void atmel_tx_chars(struct uart_port *port)
       if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
               uart_write_wakeup(port);
 
-     if (!uart_circ_empty(xmit))
+     if (!uart_circ_empty(xmit)) {
+             /* we still have characters to transmit, so we should continue
+              * transmitting them when TX is ready, regardless of
+              * mode or duplexity
+              */
+             atmel_port->tx_done_mask |= ATMEL_US_TXRDY;
+
               /* Enable interrupts */
               atmel_uart_writel(port, ATMEL_US_IER,
                                 atmel_port->tx_done_mask);
+     } else {
+             if (atmel_uart_is_half_duplex(port))
+                     atmel_port->tx_done_mask &= ~ATMEL_US_TXRDY;
+     }
  }
 
  static void atmel_complete_tx_dma(void *arg)
@@ -2525,8 +2538,7 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port,
        * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or
        * ENDTX|TXBUFE
        */
-     if (port->rs485.flags & SER_RS485_ENABLED ||
-         port->iso7816.flags & SER_ISO7816_ENABLED)
+     if (atmel_uart_is_half_duplex(port))
               atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
       else if (atmel_use_pdc_tx(port)) {
               port->fifosize = PDC_BUFFER_SIZE;
#3 lore raw
In reply to Richard Genoud

Hello,

Quoted 2 lines · Richard Genoud

NB: MS exchange has added some =3D and =20 here and there, but git am doesn't seems to be bothered by them.

Unless I missed something I cannot confirm. In mutt I don't see any =3D or =20.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#4 lore raw

Le 09/01/2020 à 08:33, Uwe Kleine-König a écrit :

Quoted 8 lines

Hello,

Quoted 2 lines · Richard Genoud

NB: MS exchange has added some =3D and =20 here and there, but git am doesn't seems to be bothered by them.

Unless I missed something I cannot confirm. In mutt I don't see any =3D or =20.

It's strange indeed. I double check under thunderbird and directly on my webmail (posteo.de), there're =3D lying around in the message source. But not on the other patches from linux-serial.
weird...

Quoted 4 lines

Best regards
Uwe