LPUART fixes and improvements
v3 cover LPUART fixes and improvements
Everyone:
This series contains fixes/improvements to LPUART dirver I came up with recently as well as fixes picked up from Toradex and NXP Vybrid repos.
Feedback is welcome!
Changes since [v2]:
- Series rebased on Greg's tty-testing branch
Changes since [v1]:
- Dropped "tty: serial: fsl_lpuart: Drop unnecessary sg_set_buf() call" due to being a duplicate of "tty: serial: fsl_lpuart: remove sg_set_buf() for sport->rx_sgl"
- Fixed build break in "tty: serial: fsl_lpuart: Introduce lpuart_tx_dma_startup()"
Thanks,
Andrey Smirnov
[v2] lore.kernel.org/r/[email protected] [v1] lore.kernel.org/r/[email protected]
Andrey Smirnov (6):
tty: serial: fsl_lpuart: Introduce lpuart_tx_dma_startup()
tty: serial: fsl_lpuart: Introduce lpuart_rx_dma_startup()
tty: serial: fsl_lpuart: Introduce lpuart32_configure()
tty: serial: fsl_lpuart: Introduce lpuart*_setup_watermark_enable()
tty: serial: fsl_lpuart: Don't enable TIE in .startup() or .resume()
tty: serial: fsl_lpuart: Ignore TX/RX interrupts if DMA is enabled
drivers/tty/serial/fsl_lpuart.c | 195 +++++++++++++++----------------- 1 file changed, 91 insertions(+), 104 deletions(-)
Signature · 1 line
2.21.0
PATCH v3 1/6 tty: serial: fsl_lpuart: Introduce lpuart_tx_dma_startup()
Code configure DMA TX path in lpuart_startup(), lpuart32_startup() and lpuart_resume() is doing exactly the same thing, so move it into a standalone subroutine.
--- drivers/tty/serial/fsl_lpuart.c | 53 ++++++++++++++------------------- 1 file changed, 23 insertions(+), 30 deletions(-)
drivers/tty/serial/fsl_lpuart.c +23 -30
@@ -1435,6 +1435,26 @@ static void rx_dma_timer_init(struct lpuart_port *sport)add_timer (&sport->lpuart_timer); } +static void lpuart_tx_dma_startup (struct lpuart_port *sport) +{ +u32 uartbaud; + +if (sport->dma_tx_chan && !lpuart_dma_tx_request (&sport->port)) { +init_waitqueue_head (&sport->dma_wait); + sport->lpuart_dma_tx_use = true; +if (lpuart_is_32 (sport)) { + uartbaud =lpuart32_read (&sport->port,UARTBAUD ); +lpuart32_write (&sport->port, + uartbaud |UARTBAUD_TDMAE ,UARTBAUD ); + }else { +writeb (readb (sport->port.membase +UARTCR5 ) | +UARTCR5_TDMAS , sport->port.membase +UARTCR5 ); + } + }else { + sport->lpuart_dma_tx_use = false; + } +} +static int lpuart_startup (struct uart_port *port) { struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
@@ -1471,14 +1491,7 @@ static int lpuart_startup(struct uart_port *port) sport->lpuart_dma_rx_use = false; } -if (sport->dma_tx_chan && !lpuart_dma_tx_request (port)) { -init_waitqueue_head (&sport->dma_wait); - sport->lpuart_dma_tx_use = true; - temp =readb (port->membase +UARTCR5 ); -writeb (temp |UARTCR5_TDMAS , port->membase +UARTCR5 ); - }else { - sport->lpuart_dma_tx_use = false; - } +lpuart_tx_dma_startup (sport);spin_unlock_irqrestore (&sport->port.lock, flags);
@@ -1521,14 +1534,7 @@ static int lpuart32_startup(struct uart_port *port) sport->lpuart_dma_rx_use = false; } -if (sport->dma_tx_chan && !lpuart_dma_tx_request (port)) { -init_waitqueue_head (&sport->dma_wait); - sport->lpuart_dma_tx_use = true; - temp =lpuart32_read (&sport->port,UARTBAUD ); -lpuart32_write (&sport->port, temp |UARTBAUD_TDMAE ,UARTBAUD ); - }else { - sport->lpuart_dma_tx_use = false; - } +lpuart_tx_dma_startup (sport);if (sport->lpuart_dma_rx_use) {/* RXWATER must be 0 */
@@ -2577,20 +2583,7 @@ static int lpuart_resume(struct device *dev) } } -if (sport->dma_tx_chan && !lpuart_dma_tx_request (&sport->port)) { -init_waitqueue_head (&sport->dma_wait); - sport->lpuart_dma_tx_use = true; -if (lpuart_is_32 (sport)) { - temp =lpuart32_read (&sport->port,UARTBAUD ); -lpuart32_write (&sport->port, - temp |UARTBAUD_TDMAE ,UARTBAUD ); - }else { -writeb (readb (sport->port.membase +UARTCR5 ) | -UARTCR5_TDMAS , sport->port.membase +UARTCR5 ); - } - }else { - sport->lpuart_dma_tx_use = false; - } +lpuart_tx_dma_startup (sport);if (lpuart_is_32 (sport)) {if (sport->lpuart_dma_rx_use) {
Signature · 1 line
2.21.0
PATCH v3 2/6 tty: serial: fsl_lpuart: Introduce lpuart_rx_dma_startup()
In reply to Andrey SmirnovCode doing initial DMA RX configuration in lpuart_startup() and lpuart32_startup() is exactly the same, so move it into a standalone subroutine.
--- drivers/tty/serial/fsl_lpuart.c | 40 ++++++++++++++------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
drivers/tty/serial/fsl_lpuart.c +17 -23
@@ -1455,6 +1455,21 @@ static void lpuart_tx_dma_startup(struct lpuart_port *sport) } } +static void lpuart_rx_dma_startup (struct lpuart_port *sport) +{ +if (sport->dma_rx_chan && !lpuart_start_rx_dma (sport)) { +/* set Rx DMA timeout */ + sport->dma_rx_timeout =msecs_to_jiffies (DMA_RX_TIMEOUT ); +if (!sport->dma_rx_timeout) + sport->dma_rx_timeout =1 ; + + sport->lpuart_dma_rx_use = true; +rx_dma_timer_init (sport); + }else { + sport->lpuart_dma_rx_use = false; + } +} +static int lpuart_startup (struct uart_port *port) { struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
@@ -1479,18 +1494,7 @@ static int lpuart_startup(struct uart_port *port) temp |=UARTCR2_RIE |UARTCR2_TIE |UARTCR2_RE |UARTCR2_TE ;writeb (temp, sport->port.membase +UARTCR2 ); -if (sport->dma_rx_chan && !lpuart_start_rx_dma (sport)) { -/* set Rx DMA timeout */ - sport->dma_rx_timeout =msecs_to_jiffies (DMA_RX_TIMEOUT ); -if (!sport->dma_rx_timeout) - sport->dma_rx_timeout =1 ; - - sport->lpuart_dma_rx_use = true; -rx_dma_timer_init (sport); - }else { - sport->lpuart_dma_rx_use = false; - } - +lpuart_rx_dma_startup (sport);lpuart_tx_dma_startup (sport);spin_unlock_irqrestore (&sport->port.lock, flags);
@@ -1522,18 +1526,8 @@ static int lpuart32_startup(struct uart_port *port) temp |=UARTCTRL_RE |UARTCTRL_TE |UARTCTRL_ILIE ;lpuart32_write (&sport->port, temp,UARTCTRL ); -if (sport->dma_rx_chan && !lpuart_start_rx_dma (sport)) { -/* set Rx DMA timeout */ - sport->dma_rx_timeout =msecs_to_jiffies (DMA_RX_TIMEOUT ); -if (!sport->dma_rx_timeout) - sport->dma_rx_timeout =1 ; - - sport->lpuart_dma_rx_use = true; -rx_dma_timer_init (sport); - }else { - sport->lpuart_dma_rx_use = false; - } +lpuart_rx_dma_startup (sport);lpuart_tx_dma_startup (sport);if (sport->lpuart_dma_rx_use) {
Signature · 1 line
2.21.0
PATCH v3 3/6 tty: serial: fsl_lpuart: Introduce lpuart32_configure()
In reply to Andrey SmirnovCode doing final steps of TX/RX configuration in lpuart32_startup() and lpuart_resume() is identical, so move it into a standalone subroutine.
--- drivers/tty/serial/fsl_lpuart.c | 48 +++++++++++++++------------------ 1 file changed, 21 insertions(+), 27 deletions(-)
drivers/tty/serial/fsl_lpuart.c +21 -27
@@ -1502,6 +1502,24 @@ static int lpuart_startup(struct uart_port *port)return 0 ; } +static void lpuart32_configure (struct lpuart_port *sport) +{ +unsigned long temp; + +if (sport->lpuart_dma_rx_use) { +/* RXWATER must be 0 */ + temp =lpuart32_read (&sport->port,UARTWATER ); + temp &= ~(UARTWATER_WATER_MASK <<UARTWATER_RXWATER_OFF ); +lpuart32_write (&sport->port, temp,UARTWATER ); + } + temp =lpuart32_read (&sport->port,UARTCTRL ); +if (!sport->lpuart_dma_rx_use) + temp |=UARTCTRL_RIE ; +if (!sport->lpuart_dma_tx_use) + temp |=UARTCTRL_TIE ; +lpuart32_write (&sport->port, temp,UARTCTRL ); +} +static int lpuart32_startup (struct uart_port *port) { struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
@@ -1530,18 +1548,7 @@ static int lpuart32_startup(struct uart_port *port)lpuart_rx_dma_startup (sport);lpuart_tx_dma_startup (sport); -if (sport->lpuart_dma_rx_use) { -/* RXWATER must be 0 */ - temp =lpuart32_read (&sport->port,UARTWATER ); - temp &= ~(UARTWATER_WATER_MASK <<UARTWATER_RXWATER_OFF ); -lpuart32_write (&sport->port, temp,UARTWATER ); - } - temp =lpuart32_read (&sport->port,UARTCTRL ); -if (!sport->lpuart_dma_rx_use) - temp |=UARTCTRL_RIE ; -if (!sport->lpuart_dma_tx_use) - temp |=UARTCTRL_TIE ; -lpuart32_write (&sport->port, temp,UARTCTRL ); +lpuart32_configure (sport);spin_unlock_irqrestore (&sport->port.lock, flags);return 0 ;
@@ -2579,21 +2586,8 @@ static int lpuart_resume(struct device *dev)lpuart_tx_dma_startup (sport); -if (lpuart_is_32 (sport)) { -if (sport->lpuart_dma_rx_use) { -/* RXWATER must be 0 */ - temp =lpuart32_read (&sport->port,UARTWATER ); - temp &= ~(UARTWATER_WATER_MASK << -UARTWATER_RXWATER_OFF ); -lpuart32_write (&sport->port, temp,UARTWATER ); - } - temp =lpuart32_read (&sport->port,UARTCTRL ); -if (!sport->lpuart_dma_rx_use) - temp |=UARTCTRL_RIE ; -if (!sport->lpuart_dma_tx_use) - temp |=UARTCTRL_TIE ; -lpuart32_write (&sport->port, temp,UARTCTRL ); - } +if (lpuart_is_32 (sport)) +lpuart32_configure (sport);uart_resume_port (&lpuart_reg, &sport->port);
Signature · 1 line
2.21.0
PATCH v3 4/6 tty: serial: fsl_lpuart: Introduce lpuart*_setup_watermark_enable()
In reply to Andrey SmirnovMost users of lpuart*_setup_watermark() enable identical set of flags right after the call, so combine those two action into a subroutine and make use of it.
--- drivers/tty/serial/fsl_lpuart.c | 50 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-)
drivers/tty/serial/fsl_lpuart.c +28 -22
@@ -1403,6 +1403,17 @@ static void lpuart_setup_watermark(struct lpuart_port *sport)writeb (cr2_saved, sport->port.membase +UARTCR2 ); } +static void lpuart_setup_watermark_enable (struct lpuart_port *sport) +{ +unsigned char cr2; + +lpuart_setup_watermark (sport); + + cr2 =readb (sport->port.membase +UARTCR2 ); + cr2 |=UARTCR2_RIE |UARTCR2_TIE |UARTCR2_RE |UARTCR2_TE ; +writeb (cr2, sport->port.membase +UARTCR2 ); +} +static void lpuart32_setup_watermark (struct lpuart_port *sport) {unsigned long val, ctrl;
@@ -1428,6 +1439,17 @@ static void lpuart32_setup_watermark(struct lpuart_port *sport)lpuart32_write (&sport->port, ctrl_saved,UARTCTRL ); } +static void lpuart32_setup_watermark_enable (struct lpuart_port *sport) +{ +u32 temp; + +lpuart32_setup_watermark (sport); + + temp =lpuart32_read (&sport->port,UARTCTRL ); + temp |=UARTCTRL_RE |UARTCTRL_TE |UARTCTRL_ILIE ; +lpuart32_write (&sport->port, temp,UARTCTRL ); +} +static void rx_dma_timer_init (struct lpuart_port *sport) {timer_setup (&sport->lpuart_timer, lpuart_timer_func,0 );
@@ -1488,11 +1510,7 @@ static int lpuart_startup(struct uart_port *port)spin_lock_irqsave (&sport->port.lock, flags); -lpuart_setup_watermark (sport); - - temp =readb (sport->port.membase +UARTCR2 ); - temp |=UARTCR2_RIE |UARTCR2_TIE |UARTCR2_RE |UARTCR2_TE ; -writeb (temp, sport->port.membase +UARTCR2 ); +lpuart_setup_watermark_enable (sport);lpuart_rx_dma_startup (sport);lpuart_tx_dma_startup (sport);
@@ -1538,11 +1556,7 @@ static int lpuart32_startup(struct uart_port *port)spin_lock_irqsave (&sport->port.lock, flags); -lpuart32_setup_watermark (sport); - - temp =lpuart32_read (&sport->port,UARTCTRL ); - temp |=UARTCTRL_RE |UARTCTRL_TE |UARTCTRL_ILIE ; -lpuart32_write (&sport->port, temp,UARTCTRL ); +lpuart32_setup_watermark_enable (sport);lpuart_rx_dma_startup (sport);
@@ -2558,22 +2572,14 @@ static int lpuart_resume(struct device *dev) {struct lpuart_port *sport =dev_get_drvdata (dev);bool irq_wake =irqd_is_wakeup_set (irq_get_irq_data (sport->port.irq)); -unsigned long temp;if (sport->port.suspended && !irq_wake)lpuart_enable_clks (sport); -if (lpuart_is_32 (sport)) { -lpuart32_setup_watermark (sport); - temp =lpuart32_read (&sport->port,UARTCTRL ); - temp |=UARTCTRL_RE |UARTCTRL_TE |UARTCTRL_ILIE ; -lpuart32_write (&sport->port, temp,UARTCTRL ); - }else { -lpuart_setup_watermark (sport); - temp =readb (sport->port.membase +UARTCR2 ); - temp |=UARTCR2_RIE |UARTCR2_TIE |UARTCR2_RE |UARTCR2_TE ; -writeb (temp, sport->port.membase +UARTCR2 ); - } +if (lpuart_is_32 (sport)) +lpuart32_setup_watermark_enable (sport); +else +lpuart_setup_watermark_enable (sport);if (sport->lpuart_dma_rx_use) {if (irq_wake) {
Signature · 1 line
2.21.0
PATCH v3 5/6 tty: serial: fsl_lpuart: Don't enable TIE in .startup() or .resume()
In reply to Andrey SmirnovEnabling TIE in .startup() callback causes the driver to start (or at least try) to transmit data before .start_tx() is called. Which, while harmless (since TIE handler will immediately disable it), is a no-op and shouldn't really happen. Drop UARTCR2_TIE from list of bits set in lpuart_startup().
This change will also not enable TIE in .resume(), but it seems that, similart to .startup(), transmit interrupt shouldn't be enabled there either.
--- drivers/tty/serial/fsl_lpuart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/fsl_lpuart.c +1 -1
@@ -1410,7 +1410,7 @@ static void lpuart_setup_watermark_enable(struct lpuart_port *sport)lpuart_setup_watermark (sport); cr2 =readb (sport->port.membase +UARTCR2 ); - cr2 |=UARTCR2_RIE |UARTCR2_TIE |UARTCR2_RE |UARTCR2_TE ; + cr2 |=UARTCR2_RIE |UARTCR2_RE |UARTCR2_TE ;writeb (cr2, sport->port.membase +UARTCR2 ); }
Signature · 1 line
2.21.0
PATCH v3 6/6 tty: serial: fsl_lpuart: Ignore TX/RX interrupts if DMA is enabled
In reply to Andrey SmirnovIn a mixed DMA/IRQ use-case (e.g.: DMA for TX, IRQ for RX), interrupt handler might try to handle Rx/Tx condition it shouldn't. Change the code to only handle TX/RX event if corresponding path isn't being handled by DMA.
--- drivers/tty/serial/fsl_lpuart.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/serial/fsl_lpuart.c +2 -2
@@ -967,10 +967,10 @@ static irqreturn_t lpuart_int(int irq, void *dev_id) sts =readb (sport->port.membase +UARTSR1 ); -if (sts &UARTSR1_RDRF ) +if (sts &UARTSR1_RDRF && !sport->lpuart_dma_rx_use)lpuart_rxint (sport); -if (sts &UARTSR1_TDRE ) +if (sts &UARTSR1_TDRE && !sport->lpuart_dma_tx_use)lpuart_txint (sport);return IRQ_HANDLED ;
Signature · 1 line
2.21.0