serial: stm32: fix a recursive locking in stm32_config_rs485
[platform/kernel/linux-rpi.git] / drivers / tty / serial / stm32-usart.c
index 0a7953e..d096e55 100644 (file)
@@ -105,9 +105,7 @@ static int stm32_config_rs485(struct uart_port *port,
        struct stm32_usart_config *cfg = &stm32_port->info->cfg;
        u32 usartdiv, baud, cr1, cr3;
        bool over8;
-       unsigned long flags;
 
-       spin_lock_irqsave(&port->lock, flags);
        stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
 
        port->rs485 = *rs485conf;
@@ -147,7 +145,6 @@ static int stm32_config_rs485(struct uart_port *port,
        }
 
        stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
-       spin_unlock_irqrestore(&port->lock, flags);
 
        return 0;
 }
@@ -290,21 +287,6 @@ static void stm32_tx_dma_complete(void *arg)
        struct uart_port *port = arg;
        struct stm32_port *stm32port = to_stm32_port(port);
        struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
-       unsigned int isr;
-       int ret;
-
-       ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
-                                               isr,
-                                               (isr & USART_SR_TC),
-                                               10, 100000);
-
-       if (ret)
-               dev_err(port->dev, "terminal count not set\n");
-
-       if (ofs->icr == UNDEF_REG)
-               stm32_clr_bits(port, ofs->isr, USART_SR_TC);
-       else
-               stm32_set_bits(port, ofs->icr, USART_CR_TC);
 
        stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
        stm32port->tx_dma_busy = false;
@@ -396,7 +378,6 @@ static void stm32_transmit_chars_dma(struct uart_port *port)
        /* Issue pending DMA TX requests */
        dma_async_issue_pending(stm32port->tx_ch);
 
-       stm32_clr_bits(port, ofs->isr, USART_SR_TC);
        stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
 
        xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
@@ -420,15 +401,15 @@ static void stm32_transmit_chars(struct uart_port *port)
                return;
        }
 
-       if (uart_tx_stopped(port)) {
-               stm32_stop_tx(port);
+       if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
+               stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
                return;
        }
 
-       if (uart_circ_empty(xmit)) {
-               stm32_stop_tx(port);
-               return;
-       }
+       if (ofs->icr == UNDEF_REG)
+               stm32_clr_bits(port, ofs->isr, USART_SR_TC);
+       else
+               stm32_set_bits(port, ofs->icr, USART_ICR_TCCF);
 
        if (stm32_port->tx_ch)
                stm32_transmit_chars_dma(port);
@@ -439,7 +420,7 @@ static void stm32_transmit_chars(struct uart_port *port)
                uart_write_wakeup(port);
 
        if (uart_circ_empty(xmit))
-               stm32_stop_tx(port);
+               stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
 }
 
 static irqreturn_t stm32_interrupt(int irq, void *ptr)
@@ -573,7 +554,6 @@ static int stm32_startup(struct uart_port *port)
 {
        struct stm32_port *stm32_port = to_stm32_port(port);
        struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
-       struct stm32_usart_config *cfg = &stm32_port->info->cfg;
        const char *name = to_platform_device(port->dev)->name;
        u32 val;
        int ret;
@@ -584,15 +564,6 @@ static int stm32_startup(struct uart_port *port)
        if (ret)
                return ret;
 
-       if (cfg->has_wakeup && stm32_port->wakeirq >= 0) {
-               ret = dev_pm_set_dedicated_wake_irq(port->dev,
-                                                   stm32_port->wakeirq);
-               if (ret) {
-                       free_irq(port->irq, port);
-                       return ret;
-               }
-       }
-
        val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
        if (stm32_port->fifoen)
                val |= USART_CR1_FIFOEN;
@@ -606,15 +577,23 @@ static void stm32_shutdown(struct uart_port *port)
        struct stm32_port *stm32_port = to_stm32_port(port);
        struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
        struct stm32_usart_config *cfg = &stm32_port->info->cfg;
-       u32 val;
+       u32 val, isr;
+       int ret;
 
        val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
        val |= BIT(cfg->uart_enable_bit);
        if (stm32_port->fifoen)
                val |= USART_CR1_FIFOEN;
+
+       ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
+                                        isr, (isr & USART_SR_TC),
+                                        10, 100000);
+
+       if (ret)
+               dev_err(port->dev, "transmission complete not set\n");
+
        stm32_clr_bits(port, ofs->cr1, val);
 
-       dev_pm_clear_wake_irq(port->dev);
        free_irq(port->irq, port);
 }
 
@@ -1086,11 +1065,18 @@ static int stm32_serial_probe(struct platform_device *pdev)
                ret = device_init_wakeup(&pdev->dev, true);
                if (ret)
                        goto err_uninit;
+
+               ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
+                                                   stm32port->wakeirq);
+               if (ret)
+                       goto err_nowup;
+
+               device_set_wakeup_enable(&pdev->dev, false);
        }
 
        ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
        if (ret)
-               goto err_nowup;
+               goto err_wirq;
 
        ret = stm32_of_dma_rx_probe(stm32port, pdev);
        if (ret)
@@ -1104,6 +1090,10 @@ static int stm32_serial_probe(struct platform_device *pdev)
 
        return 0;
 
+err_wirq:
+       if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
+               dev_pm_clear_wake_irq(&pdev->dev);
+
 err_nowup:
        if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0)
                device_init_wakeup(&pdev->dev, false);
@@ -1141,8 +1131,10 @@ static int stm32_serial_remove(struct platform_device *pdev)
                                  TX_BUF_L, stm32_port->tx_buf,
                                  stm32_port->tx_dma_buf);
 
-       if (cfg->has_wakeup && stm32_port->wakeirq >= 0)
+       if (cfg->has_wakeup && stm32_port->wakeirq >= 0) {
+               dev_pm_clear_wake_irq(&pdev->dev);
                device_init_wakeup(&pdev->dev, false);
+       }
 
        clk_disable_unprepare(stm32_port->clk);