1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 248a6092fSMaxime Coquelin /* 348a6092fSMaxime Coquelin * Copyright (C) Maxime Coquelin 2015 43e5fcbacSBich HEMON * Copyright (C) STMicroelectronics SA 2017 5ada8618fSAlexandre TORGUE * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com> 68ebd9665SErwan Le Ray * Gerald Baeza <gerald.baeza@foss.st.com> 78ebd9665SErwan Le Ray * Erwan Le Ray <erwan.leray@foss.st.com> 848a6092fSMaxime Coquelin * 948a6092fSMaxime Coquelin * Inspired by st-asc.c from STMicroelectronics (c) 1048a6092fSMaxime Coquelin */ 1148a6092fSMaxime Coquelin 1234891872SAlexandre TORGUE #include <linux/clk.h> 1348a6092fSMaxime Coquelin #include <linux/console.h> 1448a6092fSMaxime Coquelin #include <linux/delay.h> 1534891872SAlexandre TORGUE #include <linux/dma-direction.h> 1634891872SAlexandre TORGUE #include <linux/dmaengine.h> 1734891872SAlexandre TORGUE #include <linux/dma-mapping.h> 1834891872SAlexandre TORGUE #include <linux/io.h> 1934891872SAlexandre TORGUE #include <linux/iopoll.h> 2034891872SAlexandre TORGUE #include <linux/irq.h> 2134891872SAlexandre TORGUE #include <linux/module.h> 2248a6092fSMaxime Coquelin #include <linux/of.h> 2348a6092fSMaxime Coquelin #include <linux/of_platform.h> 2494616d9aSErwan Le Ray #include <linux/pinctrl/consumer.h> 2534891872SAlexandre TORGUE #include <linux/platform_device.h> 2634891872SAlexandre TORGUE #include <linux/pm_runtime.h> 27270e5a74SFabrice Gasnier #include <linux/pm_wakeirq.h> 2848a6092fSMaxime Coquelin #include <linux/serial_core.h> 2934891872SAlexandre TORGUE #include <linux/serial.h> 3034891872SAlexandre TORGUE #include <linux/spinlock.h> 3134891872SAlexandre TORGUE #include <linux/sysrq.h> 3234891872SAlexandre TORGUE #include <linux/tty_flip.h> 3334891872SAlexandre TORGUE #include <linux/tty.h> 3448a6092fSMaxime Coquelin 356cf61b9bSManivannan Sadhasivam #include "serial_mctrl_gpio.h" 36bc5a0b55SAlexandre TORGUE #include "stm32-usart.h" 3748a6092fSMaxime Coquelin 3856f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port); 3956f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port); 4048a6092fSMaxime Coquelin 4148a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 4248a6092fSMaxime Coquelin { 4348a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 4448a6092fSMaxime Coquelin } 4548a6092fSMaxime Coquelin 4656f9a76cSErwan Le Ray static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits) 4748a6092fSMaxime Coquelin { 4848a6092fSMaxime Coquelin u32 val; 4948a6092fSMaxime Coquelin 5048a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5148a6092fSMaxime Coquelin val |= bits; 5248a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 5348a6092fSMaxime Coquelin } 5448a6092fSMaxime Coquelin 5556f9a76cSErwan Le Ray static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits) 5648a6092fSMaxime Coquelin { 5748a6092fSMaxime Coquelin u32 val; 5848a6092fSMaxime Coquelin 5948a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 6048a6092fSMaxime Coquelin val &= ~bits; 6148a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 6248a6092fSMaxime Coquelin } 6348a6092fSMaxime Coquelin 6456f9a76cSErwan Le Ray static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, 651bcda09dSBich HEMON u32 delay_DDE, u32 baud) 661bcda09dSBich HEMON { 671bcda09dSBich HEMON u32 rs485_deat_dedt; 681bcda09dSBich HEMON u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); 691bcda09dSBich HEMON bool over8; 701bcda09dSBich HEMON 711bcda09dSBich HEMON *cr3 |= USART_CR3_DEM; 721bcda09dSBich HEMON over8 = *cr1 & USART_CR1_OVER8; 731bcda09dSBich HEMON 741bcda09dSBich HEMON if (over8) 751bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 8; 761bcda09dSBich HEMON else 771bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 16; 781bcda09dSBich HEMON 791bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 801bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 811bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 821bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & 831bcda09dSBich HEMON USART_CR1_DEAT_MASK; 841bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 851bcda09dSBich HEMON 861bcda09dSBich HEMON if (over8) 871bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 8; 881bcda09dSBich HEMON else 891bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 16; 901bcda09dSBich HEMON 911bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 921bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 931bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 941bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & 951bcda09dSBich HEMON USART_CR1_DEDT_MASK; 961bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 971bcda09dSBich HEMON } 981bcda09dSBich HEMON 9956f9a76cSErwan Le Ray static int stm32_usart_config_rs485(struct uart_port *port, 1001bcda09dSBich HEMON struct serial_rs485 *rs485conf) 1011bcda09dSBich HEMON { 1021bcda09dSBich HEMON struct stm32_port *stm32_port = to_stm32_port(port); 103d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 104d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1051bcda09dSBich HEMON u32 usartdiv, baud, cr1, cr3; 1061bcda09dSBich HEMON bool over8; 1071bcda09dSBich HEMON 10856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1091bcda09dSBich HEMON 1101bcda09dSBich HEMON port->rs485 = *rs485conf; 1111bcda09dSBich HEMON 1121bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RX_DURING_TX; 1131bcda09dSBich HEMON 1141bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 1151bcda09dSBich HEMON cr1 = readl_relaxed(port->membase + ofs->cr1); 1161bcda09dSBich HEMON cr3 = readl_relaxed(port->membase + ofs->cr3); 1171bcda09dSBich HEMON usartdiv = readl_relaxed(port->membase + ofs->brr); 1181bcda09dSBich HEMON usartdiv = usartdiv & GENMASK(15, 0); 1191bcda09dSBich HEMON over8 = cr1 & USART_CR1_OVER8; 1201bcda09dSBich HEMON 1211bcda09dSBich HEMON if (over8) 1221bcda09dSBich HEMON usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) 1231bcda09dSBich HEMON << USART_BRR_04_R_SHIFT; 1241bcda09dSBich HEMON 1251bcda09dSBich HEMON baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); 12656f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 1271bcda09dSBich HEMON rs485conf->delay_rts_before_send, 12856f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 12956f9a76cSErwan Le Ray baud); 1301bcda09dSBich HEMON 1311bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 1321bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 1331bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 1341bcda09dSBich HEMON } else { 1351bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 1361bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 1371bcda09dSBich HEMON } 1381bcda09dSBich HEMON 1391bcda09dSBich HEMON writel_relaxed(cr3, port->membase + ofs->cr3); 1401bcda09dSBich HEMON writel_relaxed(cr1, port->membase + ofs->cr1); 1411bcda09dSBich HEMON } else { 14256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, 14356f9a76cSErwan Le Ray USART_CR3_DEM | USART_CR3_DEP); 14456f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, 1451bcda09dSBich HEMON USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 1461bcda09dSBich HEMON } 1471bcda09dSBich HEMON 14856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1491bcda09dSBich HEMON 1501bcda09dSBich HEMON return 0; 1511bcda09dSBich HEMON } 1521bcda09dSBich HEMON 15356f9a76cSErwan Le Ray static int stm32_usart_init_rs485(struct uart_port *port, 1541bcda09dSBich HEMON struct platform_device *pdev) 1551bcda09dSBich HEMON { 1561bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 1571bcda09dSBich HEMON 1581bcda09dSBich HEMON rs485conf->flags = 0; 1591bcda09dSBich HEMON rs485conf->delay_rts_before_send = 0; 1601bcda09dSBich HEMON rs485conf->delay_rts_after_send = 0; 1611bcda09dSBich HEMON 1621bcda09dSBich HEMON if (!pdev->dev.of_node) 1631bcda09dSBich HEMON return -ENODEV; 1641bcda09dSBich HEMON 165c150c0f3SLukas Wunner return uart_get_rs485_mode(port); 1661bcda09dSBich HEMON } 1671bcda09dSBich HEMON 16856f9a76cSErwan Le Ray static int stm32_usart_pending_rx(struct uart_port *port, u32 *sr, 16956f9a76cSErwan Le Ray int *last_res, bool threaded) 17034891872SAlexandre TORGUE { 17134891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 172d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 17334891872SAlexandre TORGUE enum dma_status status; 17434891872SAlexandre TORGUE struct dma_tx_state state; 17534891872SAlexandre TORGUE 17634891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 17734891872SAlexandre TORGUE 17834891872SAlexandre TORGUE if (threaded && stm32_port->rx_ch) { 17934891872SAlexandre TORGUE status = dmaengine_tx_status(stm32_port->rx_ch, 18034891872SAlexandre TORGUE stm32_port->rx_ch->cookie, 18134891872SAlexandre TORGUE &state); 18292fc0023SErwan Le Ray if (status == DMA_IN_PROGRESS && (*last_res != state.residue)) 18334891872SAlexandre TORGUE return 1; 18434891872SAlexandre TORGUE else 18534891872SAlexandre TORGUE return 0; 18634891872SAlexandre TORGUE } else if (*sr & USART_SR_RXNE) { 18734891872SAlexandre TORGUE return 1; 18834891872SAlexandre TORGUE } 18934891872SAlexandre TORGUE return 0; 19034891872SAlexandre TORGUE } 19134891872SAlexandre TORGUE 19256f9a76cSErwan Le Ray static unsigned long stm32_usart_get_char(struct uart_port *port, u32 *sr, 1936c5962f3SErwan Le Ray int *last_res) 19434891872SAlexandre TORGUE { 19534891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 196d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 19734891872SAlexandre TORGUE unsigned long c; 19834891872SAlexandre TORGUE 19934891872SAlexandre TORGUE if (stm32_port->rx_ch) { 20034891872SAlexandre TORGUE c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; 20134891872SAlexandre TORGUE if ((*last_res) == 0) 20234891872SAlexandre TORGUE *last_res = RX_BUF_L; 20334891872SAlexandre TORGUE } else { 2046c5962f3SErwan Le Ray c = readl_relaxed(port->membase + ofs->rdr); 2056c5962f3SErwan Le Ray /* apply RDR data mask */ 2066c5962f3SErwan Le Ray c &= stm32_port->rdr_mask; 20734891872SAlexandre TORGUE } 2086c5962f3SErwan Le Ray 2096c5962f3SErwan Le Ray return c; 21034891872SAlexandre TORGUE } 21134891872SAlexandre TORGUE 21256f9a76cSErwan Le Ray static void stm32_usart_receive_chars(struct uart_port *port, bool threaded) 21348a6092fSMaxime Coquelin { 21448a6092fSMaxime Coquelin struct tty_port *tport = &port->state->port; 215ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 216d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 217ad767681SErwan Le Ray unsigned long c, flags; 21848a6092fSMaxime Coquelin u32 sr; 21948a6092fSMaxime Coquelin char flag; 22048a6092fSMaxime Coquelin 221ad767681SErwan Le Ray if (threaded) 222ad767681SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 223ad767681SErwan Le Ray else 224ad767681SErwan Le Ray spin_lock(&port->lock); 225ad767681SErwan Le Ray 22656f9a76cSErwan Le Ray while (stm32_usart_pending_rx(port, &sr, &stm32_port->last_res, 22756f9a76cSErwan Le Ray threaded)) { 22848a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22948a6092fSMaxime Coquelin flag = TTY_NORMAL; 23048a6092fSMaxime Coquelin 2314f01d833SErwan Le Ray /* 2324f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2334f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2344f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2354f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2364f01d833SErwan Le Ray * and clear status bits of the next rx data. 2374f01d833SErwan Le Ray * 2384f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2394f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 2404f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 2414f01d833SErwan Le Ray */ 2424f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 2431250ed71SFabrice Gasnier writel_relaxed(sr & USART_SR_ERR_MASK, 2441250ed71SFabrice Gasnier port->membase + ofs->icr); 2454f01d833SErwan Le Ray 24656f9a76cSErwan Le Ray c = stm32_usart_get_char(port, &sr, &stm32_port->last_res); 2474f01d833SErwan Le Ray port->icount.rx++; 24848a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 2494f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 25048a6092fSMaxime Coquelin port->icount.overrun++; 25148a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 25248a6092fSMaxime Coquelin port->icount.parity++; 25348a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 2544f01d833SErwan Le Ray /* Break detection if character is null */ 2554f01d833SErwan Le Ray if (!c) { 2564f01d833SErwan Le Ray port->icount.brk++; 2574f01d833SErwan Le Ray if (uart_handle_break(port)) 2584f01d833SErwan Le Ray continue; 2594f01d833SErwan Le Ray } else { 26048a6092fSMaxime Coquelin port->icount.frame++; 26148a6092fSMaxime Coquelin } 2624f01d833SErwan Le Ray } 26348a6092fSMaxime Coquelin 26448a6092fSMaxime Coquelin sr &= port->read_status_mask; 26548a6092fSMaxime Coquelin 2664f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26748a6092fSMaxime Coquelin flag = TTY_PARITY; 2684f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 2694f01d833SErwan Le Ray if (!c) 2704f01d833SErwan Le Ray flag = TTY_BREAK; 2714f01d833SErwan Le Ray else 27248a6092fSMaxime Coquelin flag = TTY_FRAME; 27348a6092fSMaxime Coquelin } 2744f01d833SErwan Le Ray } 27548a6092fSMaxime Coquelin 27648a6092fSMaxime Coquelin if (uart_handle_sysrq_char(port, c)) 27748a6092fSMaxime Coquelin continue; 27848a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27948a6092fSMaxime Coquelin } 28048a6092fSMaxime Coquelin 281ad767681SErwan Le Ray if (threaded) 282ad767681SErwan Le Ray spin_unlock_irqrestore(&port->lock, flags); 283ad767681SErwan Le Ray else 28448a6092fSMaxime Coquelin spin_unlock(&port->lock); 285ad767681SErwan Le Ray 28648a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 28748a6092fSMaxime Coquelin } 28848a6092fSMaxime Coquelin 28956f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg) 29034891872SAlexandre TORGUE { 29134891872SAlexandre TORGUE struct uart_port *port = arg; 29234891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 293d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 294f16b90c2SErwan Le Ray unsigned long flags; 29534891872SAlexandre TORGUE 296fb4f2e04SErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 29756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 29834891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 29934891872SAlexandre TORGUE 30034891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 301f16b90c2SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 30256f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 303f16b90c2SErwan Le Ray spin_unlock_irqrestore(&port->lock, flags); 30434891872SAlexandre TORGUE } 30534891872SAlexandre TORGUE 30656f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port) 307d075719eSErwan Le Ray { 308d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 309d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 310d075719eSErwan Le Ray 311d075719eSErwan Le Ray /* 312d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 313d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 314d075719eSErwan Le Ray */ 315d075719eSErwan Le Ray if (stm32_port->fifoen) 31656f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 317d075719eSErwan Le Ray else 31856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 319d075719eSErwan Le Ray } 320d075719eSErwan Le Ray 32156f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port) 322d075719eSErwan Le Ray { 323d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 324d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 325d075719eSErwan Le Ray 326d075719eSErwan Le Ray if (stm32_port->fifoen) 32756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 328d075719eSErwan Le Ray else 32956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 330d075719eSErwan Le Ray } 331d075719eSErwan Le Ray 33256f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port) 33334891872SAlexandre TORGUE { 33434891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 335d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 33634891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 33734891872SAlexandre TORGUE 33834891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 33956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 34034891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 34134891872SAlexandre TORGUE } 34234891872SAlexandre TORGUE 3435d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 3445d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 3455d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 3465d9176edSErwan Le Ray break; 34734891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 34834891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 34934891872SAlexandre TORGUE port->icount.tx++; 35034891872SAlexandre TORGUE } 35134891872SAlexandre TORGUE 3525d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 3535d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 35456f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 3555d9176edSErwan Le Ray else 35656f9a76cSErwan Le Ray stm32_usart_tx_interrupt_enable(port); 3575d9176edSErwan Le Ray } 3585d9176edSErwan Le Ray 35956f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port) 36034891872SAlexandre TORGUE { 36134891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 362d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 36334891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 36434891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 36534891872SAlexandre TORGUE unsigned int count, i; 36634891872SAlexandre TORGUE 36734891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 36834891872SAlexandre TORGUE return; 36934891872SAlexandre TORGUE 37034891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 37134891872SAlexandre TORGUE 37234891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 37334891872SAlexandre TORGUE 37434891872SAlexandre TORGUE if (count > TX_BUF_L) 37534891872SAlexandre TORGUE count = TX_BUF_L; 37634891872SAlexandre TORGUE 37734891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 37834891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 37934891872SAlexandre TORGUE } else { 38034891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 38134891872SAlexandre TORGUE size_t two; 38234891872SAlexandre TORGUE 38334891872SAlexandre TORGUE if (one > count) 38434891872SAlexandre TORGUE one = count; 38534891872SAlexandre TORGUE two = count - one; 38634891872SAlexandre TORGUE 38734891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 38834891872SAlexandre TORGUE if (two) 38934891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 39034891872SAlexandre TORGUE } 39134891872SAlexandre TORGUE 39234891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 39334891872SAlexandre TORGUE stm32port->tx_dma_buf, 39434891872SAlexandre TORGUE count, 39534891872SAlexandre TORGUE DMA_MEM_TO_DEV, 39634891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 39734891872SAlexandre TORGUE 398e7997f7fSErwan Le Ray if (!desc) 399e7997f7fSErwan Le Ray goto fallback_err; 40034891872SAlexandre TORGUE 40156f9a76cSErwan Le Ray desc->callback = stm32_usart_tx_dma_complete; 40234891872SAlexandre TORGUE desc->callback_param = port; 40334891872SAlexandre TORGUE 40434891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 405e7997f7fSErwan Le Ray if (dma_submit_error(dmaengine_submit(desc))) { 406e7997f7fSErwan Le Ray /* dma no yet started, safe to free resources */ 407e7997f7fSErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 408e7997f7fSErwan Le Ray goto fallback_err; 409e7997f7fSErwan Le Ray } 41034891872SAlexandre TORGUE 41134891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 41234891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 41334891872SAlexandre TORGUE 41456f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 41534891872SAlexandre TORGUE 41634891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 41734891872SAlexandre TORGUE port->icount.tx += count; 418e7997f7fSErwan Le Ray return; 419e7997f7fSErwan Le Ray 420e7997f7fSErwan Le Ray fallback_err: 421e7997f7fSErwan Le Ray for (i = count; i > 0; i--) 42256f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 42334891872SAlexandre TORGUE } 42434891872SAlexandre TORGUE 42556f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port) 42648a6092fSMaxime Coquelin { 427ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 428d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 42948a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 43048a6092fSMaxime Coquelin 43148a6092fSMaxime Coquelin if (port->x_char) { 43234891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 43356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 434ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 43548a6092fSMaxime Coquelin port->x_char = 0; 43648a6092fSMaxime Coquelin port->icount.tx++; 43734891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 43856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 43948a6092fSMaxime Coquelin return; 44048a6092fSMaxime Coquelin } 44148a6092fSMaxime Coquelin 442b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 44356f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 44448a6092fSMaxime Coquelin return; 44548a6092fSMaxime Coquelin } 44648a6092fSMaxime Coquelin 44764c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 44856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC); 44964c32eabSErwan Le Ray else 4501250ed71SFabrice Gasnier writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr); 45164c32eabSErwan Le Ray 45234891872SAlexandre TORGUE if (stm32_port->tx_ch) 45356f9a76cSErwan Le Ray stm32_usart_transmit_chars_dma(port); 45434891872SAlexandre TORGUE else 45556f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 45648a6092fSMaxime Coquelin 45748a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 45848a6092fSMaxime Coquelin uart_write_wakeup(port); 45948a6092fSMaxime Coquelin 46048a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 46156f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 46248a6092fSMaxime Coquelin } 46348a6092fSMaxime Coquelin 46456f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) 46548a6092fSMaxime Coquelin { 46648a6092fSMaxime Coquelin struct uart_port *port = ptr; 46712761869SErwan Le Ray struct tty_port *tport = &port->state->port; 468ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 469d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 47048a6092fSMaxime Coquelin u32 sr; 47148a6092fSMaxime Coquelin 472ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 47348a6092fSMaxime Coquelin 4744cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 4754cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 4764cc0ed62SErwan Le Ray port->membase + ofs->icr); 4774cc0ed62SErwan Le Ray 47812761869SErwan Le Ray if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) { 47912761869SErwan Le Ray /* Clear wake up flag and disable wake up interrupt */ 480270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 481270e5a74SFabrice Gasnier port->membase + ofs->icr); 48212761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 48312761869SErwan Le Ray if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 48412761869SErwan Le Ray pm_wakeup_event(tport->tty->dev, 0); 48512761869SErwan Le Ray } 486270e5a74SFabrice Gasnier 48734891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 48856f9a76cSErwan Le Ray stm32_usart_receive_chars(port, false); 48948a6092fSMaxime Coquelin 490ad767681SErwan Le Ray if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) { 491ad767681SErwan Le Ray spin_lock(&port->lock); 49256f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 49301d32d71SAlexandre TORGUE spin_unlock(&port->lock); 494ad767681SErwan Le Ray } 49501d32d71SAlexandre TORGUE 49634891872SAlexandre TORGUE if (stm32_port->rx_ch) 49734891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 49834891872SAlexandre TORGUE else 49934891872SAlexandre TORGUE return IRQ_HANDLED; 50034891872SAlexandre TORGUE } 50134891872SAlexandre TORGUE 50256f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr) 50334891872SAlexandre TORGUE { 50434891872SAlexandre TORGUE struct uart_port *port = ptr; 50534891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 50634891872SAlexandre TORGUE 50734891872SAlexandre TORGUE if (stm32_port->rx_ch) 50856f9a76cSErwan Le Ray stm32_usart_receive_chars(port, true); 50934891872SAlexandre TORGUE 51048a6092fSMaxime Coquelin return IRQ_HANDLED; 51148a6092fSMaxime Coquelin } 51248a6092fSMaxime Coquelin 51356f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port) 51448a6092fSMaxime Coquelin { 515ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 516d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 517ada8618fSAlexandre TORGUE 5183db1d524SErwan Le Ray if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC) 5193db1d524SErwan Le Ray return TIOCSER_TEMT; 5203db1d524SErwan Le Ray 5213db1d524SErwan Le Ray return 0; 52248a6092fSMaxime Coquelin } 52348a6092fSMaxime Coquelin 52456f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl) 52548a6092fSMaxime Coquelin { 526ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 527d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 528ada8618fSAlexandre TORGUE 52948a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 53056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE); 53148a6092fSMaxime Coquelin else 53256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 5336cf61b9bSManivannan Sadhasivam 5346cf61b9bSManivannan Sadhasivam mctrl_gpio_set(stm32_port->gpios, mctrl); 53548a6092fSMaxime Coquelin } 53648a6092fSMaxime Coquelin 53756f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port) 53848a6092fSMaxime Coquelin { 5396cf61b9bSManivannan Sadhasivam struct stm32_port *stm32_port = to_stm32_port(port); 5406cf61b9bSManivannan Sadhasivam unsigned int ret; 5416cf61b9bSManivannan Sadhasivam 54248a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 5436cf61b9bSManivannan Sadhasivam ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 5446cf61b9bSManivannan Sadhasivam 5456cf61b9bSManivannan Sadhasivam return mctrl_gpio_get(stm32_port->gpios, &ret); 5466cf61b9bSManivannan Sadhasivam } 5476cf61b9bSManivannan Sadhasivam 54856f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port) 5496cf61b9bSManivannan Sadhasivam { 5506cf61b9bSManivannan Sadhasivam mctrl_gpio_enable_ms(to_stm32_port(port)->gpios); 5516cf61b9bSManivannan Sadhasivam } 5526cf61b9bSManivannan Sadhasivam 55356f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port) 5546cf61b9bSManivannan Sadhasivam { 5556cf61b9bSManivannan Sadhasivam mctrl_gpio_disable_ms(to_stm32_port(port)->gpios); 55648a6092fSMaxime Coquelin } 55748a6092fSMaxime Coquelin 55848a6092fSMaxime Coquelin /* Transmit stop */ 55956f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port) 56048a6092fSMaxime Coquelin { 561ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 562ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 563ad0c2748SMarek Vasut 56456f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 565ad0c2748SMarek Vasut 566ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 567ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 568ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 569ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 570ad0c2748SMarek Vasut } else { 571ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 572ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 573ad0c2748SMarek Vasut } 574ad0c2748SMarek Vasut } 57548a6092fSMaxime Coquelin } 57648a6092fSMaxime Coquelin 57748a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 57856f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port) 57948a6092fSMaxime Coquelin { 580ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 581ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 58248a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 58348a6092fSMaxime Coquelin 58448a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 58548a6092fSMaxime Coquelin return; 58648a6092fSMaxime Coquelin 587ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 588ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 589ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 590ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 591ad0c2748SMarek Vasut } else { 592ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 593ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 594ad0c2748SMarek Vasut } 595ad0c2748SMarek Vasut } 596ad0c2748SMarek Vasut 59756f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 59848a6092fSMaxime Coquelin } 59948a6092fSMaxime Coquelin 6003d82be8bSErwan Le Ray /* Flush the transmit buffer. */ 6013d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port) 6023d82be8bSErwan Le Ray { 6033d82be8bSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 6043d82be8bSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 6053d82be8bSErwan Le Ray 6063d82be8bSErwan Le Ray if (stm32_port->tx_ch) { 6073d82be8bSErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 6083d82be8bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 6093d82be8bSErwan Le Ray stm32_port->tx_dma_busy = false; 6103d82be8bSErwan Le Ray } 6113d82be8bSErwan Le Ray } 6123d82be8bSErwan Le Ray 61348a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 61456f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port) 61548a6092fSMaxime Coquelin { 616ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 617d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 61848a6092fSMaxime Coquelin unsigned long flags; 61948a6092fSMaxime Coquelin 62048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 62156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 622d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 62356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 624d0a6a7bcSErwan Le Ray 62548a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 62648a6092fSMaxime Coquelin } 62748a6092fSMaxime Coquelin 62848a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 62956f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port) 63048a6092fSMaxime Coquelin { 631ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 632d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 63348a6092fSMaxime Coquelin unsigned long flags; 63448a6092fSMaxime Coquelin 63548a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 63656f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 637d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 63856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq); 639d0a6a7bcSErwan Le Ray 64048a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 64148a6092fSMaxime Coquelin } 64248a6092fSMaxime Coquelin 64348a6092fSMaxime Coquelin /* Receive stop */ 64456f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port) 64548a6092fSMaxime Coquelin { 646ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 647d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 648ada8618fSAlexandre TORGUE 64956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 650d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 65156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 65248a6092fSMaxime Coquelin } 65348a6092fSMaxime Coquelin 65448a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 65556f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state) 65648a6092fSMaxime Coquelin { 65748a6092fSMaxime Coquelin } 65848a6092fSMaxime Coquelin 65956f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port) 66048a6092fSMaxime Coquelin { 661ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 662d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 663f4518a8aSErwan Le Ray const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 66448a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 66548a6092fSMaxime Coquelin u32 val; 66648a6092fSMaxime Coquelin int ret; 66748a6092fSMaxime Coquelin 66856f9a76cSErwan Le Ray ret = request_threaded_irq(port->irq, stm32_usart_interrupt, 66956f9a76cSErwan Le Ray stm32_usart_threaded_interrupt, 67034891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 67148a6092fSMaxime Coquelin if (ret) 67248a6092fSMaxime Coquelin return ret; 67348a6092fSMaxime Coquelin 67484872dc4SErwan Le Ray /* RX FIFO Flush */ 67584872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 676315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr); 67748a6092fSMaxime Coquelin 67825a8e761SErwan Le Ray /* RX enabling */ 679f4518a8aSErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit); 68056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, val); 68184872dc4SErwan Le Ray 68248a6092fSMaxime Coquelin return 0; 68348a6092fSMaxime Coquelin } 68448a6092fSMaxime Coquelin 68556f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port) 68648a6092fSMaxime Coquelin { 687ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 688d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 689d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 69064c32eabSErwan Le Ray u32 val, isr; 69164c32eabSErwan Le Ray int ret; 69248a6092fSMaxime Coquelin 6936cf61b9bSManivannan Sadhasivam /* Disable modem control interrupts */ 69456f9a76cSErwan Le Ray stm32_usart_disable_ms(port); 6956cf61b9bSManivannan Sadhasivam 6964cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 6974cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 69887f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 699351a762aSGerald Baeza if (stm32_port->fifoen) 700351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 70164c32eabSErwan Le Ray 70264c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 70364c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 70464c32eabSErwan Le Ray 10, 100000); 70564c32eabSErwan Le Ray 706c31c3ea0SErwan Le Ray /* Send the TC error message only when ISR_TC is not set */ 70764c32eabSErwan Le Ray if (ret) 708c31c3ea0SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 70964c32eabSErwan Le Ray 7109f77d192SErwan Le Ray /* flush RX & TX FIFO */ 7119f77d192SErwan Le Ray if (ofs->rqr != UNDEF_REG) 7129f77d192SErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 7139f77d192SErwan Le Ray port->membase + ofs->rqr); 7149f77d192SErwan Le Ray 71556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, val); 71648a6092fSMaxime Coquelin 71748a6092fSMaxime Coquelin free_irq(port->irq, port); 71848a6092fSMaxime Coquelin } 71948a6092fSMaxime Coquelin 72056f9a76cSErwan Le Ray static unsigned int stm32_usart_get_databits(struct ktermios *termios) 721c8a9d043SErwan Le Ray { 722c8a9d043SErwan Le Ray unsigned int bits; 723c8a9d043SErwan Le Ray 724c8a9d043SErwan Le Ray tcflag_t cflag = termios->c_cflag; 725c8a9d043SErwan Le Ray 726c8a9d043SErwan Le Ray switch (cflag & CSIZE) { 727c8a9d043SErwan Le Ray /* 728c8a9d043SErwan Le Ray * CSIZE settings are not necessarily supported in hardware. 729c8a9d043SErwan Le Ray * CSIZE unsupported configurations are handled here to set word length 730c8a9d043SErwan Le Ray * to 8 bits word as default configuration and to print debug message. 731c8a9d043SErwan Le Ray */ 732c8a9d043SErwan Le Ray case CS5: 733c8a9d043SErwan Le Ray bits = 5; 734c8a9d043SErwan Le Ray break; 735c8a9d043SErwan Le Ray case CS6: 736c8a9d043SErwan Le Ray bits = 6; 737c8a9d043SErwan Le Ray break; 738c8a9d043SErwan Le Ray case CS7: 739c8a9d043SErwan Le Ray bits = 7; 740c8a9d043SErwan Le Ray break; 741c8a9d043SErwan Le Ray /* default including CS8 */ 742c8a9d043SErwan Le Ray default: 743c8a9d043SErwan Le Ray bits = 8; 744c8a9d043SErwan Le Ray break; 745c8a9d043SErwan Le Ray } 746c8a9d043SErwan Le Ray 747c8a9d043SErwan Le Ray return bits; 748c8a9d043SErwan Le Ray } 749c8a9d043SErwan Le Ray 75056f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port, 75156f9a76cSErwan Le Ray struct ktermios *termios, 75248a6092fSMaxime Coquelin struct ktermios *old) 75348a6092fSMaxime Coquelin { 75448a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 755d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 756d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 7571bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 758c8a9d043SErwan Le Ray unsigned int baud, bits; 75948a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 76048a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 761f264c6f6SErwan Le Ray u32 cr1, cr2, cr3, isr; 76248a6092fSMaxime Coquelin unsigned long flags; 763f264c6f6SErwan Le Ray int ret; 76448a6092fSMaxime Coquelin 76548a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 76648a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 76748a6092fSMaxime Coquelin 76848a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 76948a6092fSMaxime Coquelin 77048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 77148a6092fSMaxime Coquelin 772f264c6f6SErwan Le Ray ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 773f264c6f6SErwan Le Ray isr, 774f264c6f6SErwan Le Ray (isr & USART_SR_TC), 775f264c6f6SErwan Le Ray 10, 100000); 776f264c6f6SErwan Le Ray 777f264c6f6SErwan Le Ray /* Send the TC error message only when ISR_TC is not set. */ 778f264c6f6SErwan Le Ray if (ret) 779f264c6f6SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 780f264c6f6SErwan Le Ray 78148a6092fSMaxime Coquelin /* Stop serial port and reset value */ 782ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 78348a6092fSMaxime Coquelin 78484872dc4SErwan Le Ray /* flush RX & TX FIFO */ 78584872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 786315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 787315e2d8aSErwan Le Ray port->membase + ofs->rqr); 7881bcda09dSBich HEMON 78984872dc4SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 790351a762aSGerald Baeza if (stm32_port->fifoen) 791351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 79248a6092fSMaxime Coquelin cr2 = 0; 79325a8e761SErwan Le Ray 79425a8e761SErwan Le Ray /* Tx and RX FIFO configuration */ 795d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 79625a8e761SErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE; 79725a8e761SErwan Le Ray if (stm32_port->fifoen) { 79825a8e761SErwan Le Ray cr3 &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK); 79925a8e761SErwan Le Ray cr3 |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT; 80025a8e761SErwan Le Ray cr3 |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT; 80125a8e761SErwan Le Ray } 80248a6092fSMaxime Coquelin 80348a6092fSMaxime Coquelin if (cflag & CSTOPB) 80448a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 80548a6092fSMaxime Coquelin 80656f9a76cSErwan Le Ray bits = stm32_usart_get_databits(termios); 8076c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 808c8a9d043SErwan Le Ray 80948a6092fSMaxime Coquelin if (cflag & PARENB) { 810c8a9d043SErwan Le Ray bits++; 81148a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 812c8a9d043SErwan Le Ray } 813c8a9d043SErwan Le Ray 814c8a9d043SErwan Le Ray /* 815c8a9d043SErwan Le Ray * Word length configuration: 816c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 817c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 818c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 819c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 820c8a9d043SErwan Le Ray */ 821c8a9d043SErwan Le Ray if (bits == 9) 822ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 823c8a9d043SErwan Le Ray else if ((bits == 7) && cfg->has_7bits_data) 824c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 825c8a9d043SErwan Le Ray else if (bits != 8) 826c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 827c8a9d043SErwan Le Ray , bits); 82848a6092fSMaxime Coquelin 8294cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 8304cc0ed62SErwan Le Ray stm32_port->fifoen)) { 8314cc0ed62SErwan Le Ray if (cflag & CSTOPB) 8324cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 8334cc0ed62SErwan Le Ray else 8344cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 8354cc0ed62SErwan Le Ray 8364cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 8374cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 8384cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 8394cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 840d0a6a7bcSErwan Le Ray /* Not using dma, enable fifo threshold irq */ 841d0a6a7bcSErwan Le Ray if (!stm32_port->rx_ch) 842d0a6a7bcSErwan Le Ray stm32_port->cr3_irq = USART_CR3_RXFTIE; 8434cc0ed62SErwan Le Ray } 8444cc0ed62SErwan Le Ray 845d0a6a7bcSErwan Le Ray cr1 |= stm32_port->cr1_irq; 846d0a6a7bcSErwan Le Ray cr3 |= stm32_port->cr3_irq; 847d0a6a7bcSErwan Le Ray 84848a6092fSMaxime Coquelin if (cflag & PARODD) 84948a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 85048a6092fSMaxime Coquelin 85148a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 85248a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 85348a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 85435abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 85548a6092fSMaxime Coquelin } 85648a6092fSMaxime Coquelin 85748a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 85848a6092fSMaxime Coquelin 85948a6092fSMaxime Coquelin /* 86048a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 86148a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 86248a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 86348a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 86448a6092fSMaxime Coquelin */ 86548a6092fSMaxime Coquelin if (usartdiv < 16) { 86648a6092fSMaxime Coquelin oversampling = 8; 8671bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 86856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8); 86948a6092fSMaxime Coquelin } else { 87048a6092fSMaxime Coquelin oversampling = 16; 8711bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 87256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 87348a6092fSMaxime Coquelin } 87448a6092fSMaxime Coquelin 87548a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 87648a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 877ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 87848a6092fSMaxime Coquelin 87948a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 88048a6092fSMaxime Coquelin 88148a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 88248a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 88348a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 88448a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 8854f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 88648a6092fSMaxime Coquelin 88748a6092fSMaxime Coquelin /* Characters to ignore */ 88848a6092fSMaxime Coquelin port->ignore_status_mask = 0; 88948a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 89048a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 89148a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 8924f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 89348a6092fSMaxime Coquelin /* 89448a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 89548a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 89648a6092fSMaxime Coquelin */ 89748a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 89848a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 89948a6092fSMaxime Coquelin } 90048a6092fSMaxime Coquelin 90148a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 90248a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 90348a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 90448a6092fSMaxime Coquelin 90534891872SAlexandre TORGUE if (stm32_port->rx_ch) 90634891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 90734891872SAlexandre TORGUE 9081bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 90956f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 9101bcda09dSBich HEMON rs485conf->delay_rts_before_send, 91156f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 91256f9a76cSErwan Le Ray baud); 9131bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 9141bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 9151bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 9161bcda09dSBich HEMON } else { 9171bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 9181bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 9191bcda09dSBich HEMON } 9201bcda09dSBich HEMON 9211bcda09dSBich HEMON } else { 9221bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 9231bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 9241bcda09dSBich HEMON } 9251bcda09dSBich HEMON 92612761869SErwan Le Ray /* Configure wake up from low power on start bit detection */ 927*3d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 92812761869SErwan Le Ray cr3 &= ~USART_CR3_WUS_MASK; 92912761869SErwan Le Ray cr3 |= USART_CR3_WUS_START_BIT; 93012761869SErwan Le Ray } 93112761869SErwan Le Ray 932ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 933ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 934ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 93548a6092fSMaxime Coquelin 93656f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 93748a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 938436c9793SErwan Le Ray 939436c9793SErwan Le Ray /* Handle modem control interrupts */ 940436c9793SErwan Le Ray if (UART_ENABLE_MS(port, termios->c_cflag)) 941436c9793SErwan Le Ray stm32_usart_enable_ms(port); 942436c9793SErwan Le Ray else 943436c9793SErwan Le Ray stm32_usart_disable_ms(port); 94448a6092fSMaxime Coquelin } 94548a6092fSMaxime Coquelin 94656f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port) 94748a6092fSMaxime Coquelin { 94848a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 94948a6092fSMaxime Coquelin } 95048a6092fSMaxime Coquelin 95156f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port) 95248a6092fSMaxime Coquelin { 95348a6092fSMaxime Coquelin } 95448a6092fSMaxime Coquelin 95556f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port) 95648a6092fSMaxime Coquelin { 95748a6092fSMaxime Coquelin return 0; 95848a6092fSMaxime Coquelin } 95948a6092fSMaxime Coquelin 96056f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags) 96148a6092fSMaxime Coquelin { 96248a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 96348a6092fSMaxime Coquelin port->type = PORT_STM32; 96448a6092fSMaxime Coquelin } 96548a6092fSMaxime Coquelin 96648a6092fSMaxime Coquelin static int 96756f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser) 96848a6092fSMaxime Coquelin { 96948a6092fSMaxime Coquelin /* No user changeable parameters */ 97048a6092fSMaxime Coquelin return -EINVAL; 97148a6092fSMaxime Coquelin } 97248a6092fSMaxime Coquelin 97356f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state, 97448a6092fSMaxime Coquelin unsigned int oldstate) 97548a6092fSMaxime Coquelin { 97648a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 97748a6092fSMaxime Coquelin struct stm32_port, port); 978d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 979d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32port->info->cfg; 98048a6092fSMaxime Coquelin unsigned long flags = 0; 98148a6092fSMaxime Coquelin 98248a6092fSMaxime Coquelin switch (state) { 98348a6092fSMaxime Coquelin case UART_PM_STATE_ON: 984fb6dcef6SErwan Le Ray pm_runtime_get_sync(port->dev); 98548a6092fSMaxime Coquelin break; 98648a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 98748a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 98856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 98948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 990fb6dcef6SErwan Le Ray pm_runtime_put_sync(port->dev); 99148a6092fSMaxime Coquelin break; 99248a6092fSMaxime Coquelin } 99348a6092fSMaxime Coquelin } 99448a6092fSMaxime Coquelin 99548a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 99656f9a76cSErwan Le Ray .tx_empty = stm32_usart_tx_empty, 99756f9a76cSErwan Le Ray .set_mctrl = stm32_usart_set_mctrl, 99856f9a76cSErwan Le Ray .get_mctrl = stm32_usart_get_mctrl, 99956f9a76cSErwan Le Ray .stop_tx = stm32_usart_stop_tx, 100056f9a76cSErwan Le Ray .start_tx = stm32_usart_start_tx, 100156f9a76cSErwan Le Ray .throttle = stm32_usart_throttle, 100256f9a76cSErwan Le Ray .unthrottle = stm32_usart_unthrottle, 100356f9a76cSErwan Le Ray .stop_rx = stm32_usart_stop_rx, 100456f9a76cSErwan Le Ray .enable_ms = stm32_usart_enable_ms, 100556f9a76cSErwan Le Ray .break_ctl = stm32_usart_break_ctl, 100656f9a76cSErwan Le Ray .startup = stm32_usart_startup, 100756f9a76cSErwan Le Ray .shutdown = stm32_usart_shutdown, 10083d82be8bSErwan Le Ray .flush_buffer = stm32_usart_flush_buffer, 100956f9a76cSErwan Le Ray .set_termios = stm32_usart_set_termios, 101056f9a76cSErwan Le Ray .pm = stm32_usart_pm, 101156f9a76cSErwan Le Ray .type = stm32_usart_type, 101256f9a76cSErwan Le Ray .release_port = stm32_usart_release_port, 101356f9a76cSErwan Le Ray .request_port = stm32_usart_request_port, 101456f9a76cSErwan Le Ray .config_port = stm32_usart_config_port, 101556f9a76cSErwan Le Ray .verify_port = stm32_usart_verify_port, 101648a6092fSMaxime Coquelin }; 101748a6092fSMaxime Coquelin 101897f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port) 101997f3a085SErwan Le Ray { 102097f3a085SErwan Le Ray clk_disable_unprepare(stm32port->clk); 102197f3a085SErwan Le Ray } 102297f3a085SErwan Le Ray 102356f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port, 102448a6092fSMaxime Coquelin struct platform_device *pdev) 102548a6092fSMaxime Coquelin { 102648a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 102748a6092fSMaxime Coquelin struct resource *res; 1028e0f2a902SErwan Le Ray int ret, irq; 102948a6092fSMaxime Coquelin 1030e0f2a902SErwan Le Ray irq = platform_get_irq(pdev, 0); 1031e0f2a902SErwan Le Ray if (irq <= 0) 1032e0f2a902SErwan Le Ray return irq ? : -ENODEV; 103392fc0023SErwan Le Ray 103448a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 103548a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 103648a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 103748a6092fSMaxime Coquelin port->dev = &pdev->dev; 1038d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 10399feedaa7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE); 1040e0f2a902SErwan Le Ray port->irq = irq; 104156f9a76cSErwan Le Ray port->rs485_config = stm32_usart_config_rs485; 10427d8f6861SBich HEMON 104356f9a76cSErwan Le Ray ret = stm32_usart_init_rs485(port, pdev); 1044c150c0f3SLukas Wunner if (ret) 1045c150c0f3SLukas Wunner return ret; 10467d8f6861SBich HEMON 1047*3d530017SAlexandre Torgue stm32port->wakeup_src = stm32port->info->cfg.has_wakeup && 1048*3d530017SAlexandre Torgue of_property_read_bool(pdev->dev.of_node, "wakeup-source"); 10492c58e560SErwan Le Ray 1050351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 105148a6092fSMaxime Coquelin 105248a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 105348a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 105448a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 105548a6092fSMaxime Coquelin return PTR_ERR(port->membase); 105648a6092fSMaxime Coquelin port->mapbase = res->start; 105748a6092fSMaxime Coquelin 105848a6092fSMaxime Coquelin spin_lock_init(&port->lock); 105948a6092fSMaxime Coquelin 106048a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 106148a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 106248a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 106348a6092fSMaxime Coquelin 106448a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 106548a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 106648a6092fSMaxime Coquelin if (ret) 106748a6092fSMaxime Coquelin return ret; 106848a6092fSMaxime Coquelin 106948a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 1070ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 107148a6092fSMaxime Coquelin ret = -EINVAL; 10726cf61b9bSManivannan Sadhasivam goto err_clk; 1073ada80043SFabrice Gasnier } 107448a6092fSMaxime Coquelin 10756cf61b9bSManivannan Sadhasivam stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0); 10766cf61b9bSManivannan Sadhasivam if (IS_ERR(stm32port->gpios)) { 10776cf61b9bSManivannan Sadhasivam ret = PTR_ERR(stm32port->gpios); 10786cf61b9bSManivannan Sadhasivam goto err_clk; 10796cf61b9bSManivannan Sadhasivam } 10806cf61b9bSManivannan Sadhasivam 10819359369aSErwan Le Ray /* 10829359369aSErwan Le Ray * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts" 10839359369aSErwan Le Ray * properties should not be specified. 10849359369aSErwan Le Ray */ 10856cf61b9bSManivannan Sadhasivam if (stm32port->hw_flow_control) { 10866cf61b9bSManivannan Sadhasivam if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) || 10876cf61b9bSManivannan Sadhasivam mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) { 10886cf61b9bSManivannan Sadhasivam dev_err(&pdev->dev, "Conflicting RTS/CTS config\n"); 10896cf61b9bSManivannan Sadhasivam ret = -EINVAL; 10906cf61b9bSManivannan Sadhasivam goto err_clk; 10916cf61b9bSManivannan Sadhasivam } 10926cf61b9bSManivannan Sadhasivam } 10936cf61b9bSManivannan Sadhasivam 10946cf61b9bSManivannan Sadhasivam return ret; 10956cf61b9bSManivannan Sadhasivam 10966cf61b9bSManivannan Sadhasivam err_clk: 10976cf61b9bSManivannan Sadhasivam clk_disable_unprepare(stm32port->clk); 10986cf61b9bSManivannan Sadhasivam 109948a6092fSMaxime Coquelin return ret; 110048a6092fSMaxime Coquelin } 110148a6092fSMaxime Coquelin 110256f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev) 110348a6092fSMaxime Coquelin { 110448a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 110548a6092fSMaxime Coquelin int id; 110648a6092fSMaxime Coquelin 110748a6092fSMaxime Coquelin if (!np) 110848a6092fSMaxime Coquelin return NULL; 110948a6092fSMaxime Coquelin 111048a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 1111e5707915SGerald Baeza if (id < 0) { 1112e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 1113e5707915SGerald Baeza return NULL; 1114e5707915SGerald Baeza } 111548a6092fSMaxime Coquelin 111648a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 111748a6092fSMaxime Coquelin return NULL; 111848a6092fSMaxime Coquelin 11196fd9fffbSErwan Le Ray stm32_ports[id].hw_flow_control = 11206fd9fffbSErwan Le Ray of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ || 11216fd9fffbSErwan Le Ray of_property_read_bool (np, "uart-has-rtscts"); 112248a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 11234cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 1124d0a6a7bcSErwan Le Ray stm32_ports[id].cr3_irq = 0; 1125e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 112648a6092fSMaxime Coquelin return &stm32_ports[id]; 112748a6092fSMaxime Coquelin } 112848a6092fSMaxime Coquelin 112948a6092fSMaxime Coquelin #ifdef CONFIG_OF 113048a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 1131ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 1132ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 1133270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 113448a6092fSMaxime Coquelin {}, 113548a6092fSMaxime Coquelin }; 113648a6092fSMaxime Coquelin 113748a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 113848a6092fSMaxime Coquelin #endif 113948a6092fSMaxime Coquelin 114056f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port, 114134891872SAlexandre TORGUE struct platform_device *pdev) 114234891872SAlexandre TORGUE { 1143d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 114434891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 114534891872SAlexandre TORGUE struct device *dev = &pdev->dev; 114634891872SAlexandre TORGUE struct dma_slave_config config; 114734891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 114834891872SAlexandre TORGUE int ret; 114934891872SAlexandre TORGUE 115034891872SAlexandre TORGUE /* Request DMA RX channel */ 115134891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 115234891872SAlexandre TORGUE if (!stm32port->rx_ch) { 115334891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 115434891872SAlexandre TORGUE return -ENODEV; 115534891872SAlexandre TORGUE } 115634891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 115734891872SAlexandre TORGUE &stm32port->rx_dma_buf, 115834891872SAlexandre TORGUE GFP_KERNEL); 115934891872SAlexandre TORGUE if (!stm32port->rx_buf) { 116034891872SAlexandre TORGUE ret = -ENOMEM; 116134891872SAlexandre TORGUE goto alloc_err; 116234891872SAlexandre TORGUE } 116334891872SAlexandre TORGUE 116434891872SAlexandre TORGUE /* Configure DMA channel */ 116534891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 11668e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 116734891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 116834891872SAlexandre TORGUE 116934891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 117034891872SAlexandre TORGUE if (ret < 0) { 117134891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 117234891872SAlexandre TORGUE ret = -ENODEV; 117334891872SAlexandre TORGUE goto config_err; 117434891872SAlexandre TORGUE } 117534891872SAlexandre TORGUE 117634891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 117734891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 117834891872SAlexandre TORGUE stm32port->rx_dma_buf, 117934891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 118034891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 118134891872SAlexandre TORGUE if (!desc) { 118234891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 118334891872SAlexandre TORGUE ret = -ENODEV; 118434891872SAlexandre TORGUE goto config_err; 118534891872SAlexandre TORGUE } 118634891872SAlexandre TORGUE 118734891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 118834891872SAlexandre TORGUE desc->callback = NULL; 118934891872SAlexandre TORGUE desc->callback_param = NULL; 119034891872SAlexandre TORGUE 119134891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 1192e7997f7fSErwan Le Ray ret = dma_submit_error(dmaengine_submit(desc)); 1193e7997f7fSErwan Le Ray if (ret) { 1194e7997f7fSErwan Le Ray dmaengine_terminate_sync(stm32port->rx_ch); 1195e7997f7fSErwan Le Ray goto config_err; 1196e7997f7fSErwan Le Ray } 119734891872SAlexandre TORGUE 119834891872SAlexandre TORGUE /* Issue pending DMA requests */ 119934891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 120034891872SAlexandre TORGUE 120134891872SAlexandre TORGUE return 0; 120234891872SAlexandre TORGUE 120334891872SAlexandre TORGUE config_err: 120434891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 120534891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 120634891872SAlexandre TORGUE stm32port->rx_dma_buf); 120734891872SAlexandre TORGUE 120834891872SAlexandre TORGUE alloc_err: 120934891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 121034891872SAlexandre TORGUE stm32port->rx_ch = NULL; 121134891872SAlexandre TORGUE 121234891872SAlexandre TORGUE return ret; 121334891872SAlexandre TORGUE } 121434891872SAlexandre TORGUE 121556f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port, 121634891872SAlexandre TORGUE struct platform_device *pdev) 121734891872SAlexandre TORGUE { 1218d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 121934891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 122034891872SAlexandre TORGUE struct device *dev = &pdev->dev; 122134891872SAlexandre TORGUE struct dma_slave_config config; 122234891872SAlexandre TORGUE int ret; 122334891872SAlexandre TORGUE 122434891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 122534891872SAlexandre TORGUE 122634891872SAlexandre TORGUE /* Request DMA TX channel */ 122734891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 122834891872SAlexandre TORGUE if (!stm32port->tx_ch) { 122934891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 123034891872SAlexandre TORGUE return -ENODEV; 123134891872SAlexandre TORGUE } 123234891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 123334891872SAlexandre TORGUE &stm32port->tx_dma_buf, 123434891872SAlexandre TORGUE GFP_KERNEL); 123534891872SAlexandre TORGUE if (!stm32port->tx_buf) { 123634891872SAlexandre TORGUE ret = -ENOMEM; 123734891872SAlexandre TORGUE goto alloc_err; 123834891872SAlexandre TORGUE } 123934891872SAlexandre TORGUE 124034891872SAlexandre TORGUE /* Configure DMA channel */ 124134891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 12428e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 124334891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 124434891872SAlexandre TORGUE 124534891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 124634891872SAlexandre TORGUE if (ret < 0) { 124734891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 124834891872SAlexandre TORGUE ret = -ENODEV; 124934891872SAlexandre TORGUE goto config_err; 125034891872SAlexandre TORGUE } 125134891872SAlexandre TORGUE 125234891872SAlexandre TORGUE return 0; 125334891872SAlexandre TORGUE 125434891872SAlexandre TORGUE config_err: 125534891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 125634891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 125734891872SAlexandre TORGUE stm32port->tx_dma_buf); 125834891872SAlexandre TORGUE 125934891872SAlexandre TORGUE alloc_err: 126034891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 126134891872SAlexandre TORGUE stm32port->tx_ch = NULL; 126234891872SAlexandre TORGUE 126334891872SAlexandre TORGUE return ret; 126434891872SAlexandre TORGUE } 126534891872SAlexandre TORGUE 126656f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev) 126748a6092fSMaxime Coquelin { 126848a6092fSMaxime Coquelin struct stm32_port *stm32port; 1269ada8618fSAlexandre TORGUE int ret; 127048a6092fSMaxime Coquelin 127156f9a76cSErwan Le Ray stm32port = stm32_usart_of_get_port(pdev); 127248a6092fSMaxime Coquelin if (!stm32port) 127348a6092fSMaxime Coquelin return -ENODEV; 127448a6092fSMaxime Coquelin 1275d825f0beSStephen Boyd stm32port->info = of_device_get_match_data(&pdev->dev); 1276d825f0beSStephen Boyd if (!stm32port->info) 1277ada8618fSAlexandre TORGUE return -EINVAL; 1278ada8618fSAlexandre TORGUE 127956f9a76cSErwan Le Ray ret = stm32_usart_init_port(stm32port, pdev); 128048a6092fSMaxime Coquelin if (ret) 128148a6092fSMaxime Coquelin return ret; 128248a6092fSMaxime Coquelin 1283*3d530017SAlexandre Torgue if (stm32port->wakeup_src) { 1284*3d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, true); 1285*3d530017SAlexandre Torgue ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq); 12865297f274SErwan Le Ray if (ret) 12875297f274SErwan Le Ray goto err_nowup; 1288270e5a74SFabrice Gasnier } 1289270e5a74SFabrice Gasnier 129056f9a76cSErwan Le Ray ret = stm32_usart_of_dma_rx_probe(stm32port, pdev); 129134891872SAlexandre TORGUE if (ret) 129234891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 129334891872SAlexandre TORGUE 129456f9a76cSErwan Le Ray ret = stm32_usart_of_dma_tx_probe(stm32port, pdev); 129534891872SAlexandre TORGUE if (ret) 129634891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 129734891872SAlexandre TORGUE 129848a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 129948a6092fSMaxime Coquelin 1300fb6dcef6SErwan Le Ray pm_runtime_get_noresume(&pdev->dev); 1301fb6dcef6SErwan Le Ray pm_runtime_set_active(&pdev->dev); 1302fb6dcef6SErwan Le Ray pm_runtime_enable(&pdev->dev); 130387fd0741SErwan Le Ray 130487fd0741SErwan Le Ray ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 130587fd0741SErwan Le Ray if (ret) 130687fd0741SErwan Le Ray goto err_port; 130787fd0741SErwan Le Ray 1308fb6dcef6SErwan Le Ray pm_runtime_put_sync(&pdev->dev); 1309fb6dcef6SErwan Le Ray 131048a6092fSMaxime Coquelin return 0; 1311ada80043SFabrice Gasnier 131287fd0741SErwan Le Ray err_port: 131387fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 131487fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 131587fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 131687fd0741SErwan Le Ray 131787fd0741SErwan Le Ray if (stm32port->rx_ch) { 131887fd0741SErwan Le Ray dmaengine_terminate_async(stm32port->rx_ch); 131987fd0741SErwan Le Ray dma_release_channel(stm32port->rx_ch); 132087fd0741SErwan Le Ray } 132187fd0741SErwan Le Ray 132287fd0741SErwan Le Ray if (stm32port->rx_dma_buf) 132387fd0741SErwan Le Ray dma_free_coherent(&pdev->dev, 132487fd0741SErwan Le Ray RX_BUF_L, stm32port->rx_buf, 132587fd0741SErwan Le Ray stm32port->rx_dma_buf); 132687fd0741SErwan Le Ray 132787fd0741SErwan Le Ray if (stm32port->tx_ch) { 132887fd0741SErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 132987fd0741SErwan Le Ray dma_release_channel(stm32port->tx_ch); 133087fd0741SErwan Le Ray } 133187fd0741SErwan Le Ray 133287fd0741SErwan Le Ray if (stm32port->tx_dma_buf) 133387fd0741SErwan Le Ray dma_free_coherent(&pdev->dev, 133487fd0741SErwan Le Ray TX_BUF_L, stm32port->tx_buf, 133587fd0741SErwan Le Ray stm32port->tx_dma_buf); 133687fd0741SErwan Le Ray 1337*3d530017SAlexandre Torgue if (stm32port->wakeup_src) 13385297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 13395297f274SErwan Le Ray 1340270e5a74SFabrice Gasnier err_nowup: 1341*3d530017SAlexandre Torgue if (stm32port->wakeup_src) 1342*3d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, false); 1343270e5a74SFabrice Gasnier 134497f3a085SErwan Le Ray stm32_usart_deinit_port(stm32port); 1345ada80043SFabrice Gasnier 1346ada80043SFabrice Gasnier return ret; 134748a6092fSMaxime Coquelin } 134848a6092fSMaxime Coquelin 134956f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev) 135048a6092fSMaxime Coquelin { 135148a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1352511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1353d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1354fb6dcef6SErwan Le Ray int err; 1355fb6dcef6SErwan Le Ray 1356fb6dcef6SErwan Le Ray pm_runtime_get_sync(&pdev->dev); 135787fd0741SErwan Le Ray err = uart_remove_one_port(&stm32_usart_driver, port); 135887fd0741SErwan Le Ray if (err) 135987fd0741SErwan Le Ray return(err); 136087fd0741SErwan Le Ray 136187fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 136287fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 136387fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 136434891872SAlexandre TORGUE 136556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 136634891872SAlexandre TORGUE 136787fd0741SErwan Le Ray if (stm32_port->rx_ch) { 136887fd0741SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 136934891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 137087fd0741SErwan Le Ray } 137134891872SAlexandre TORGUE 137234891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 137334891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 137434891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 137534891872SAlexandre TORGUE stm32_port->rx_dma_buf); 137634891872SAlexandre TORGUE 137756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 137834891872SAlexandre TORGUE 137987fd0741SErwan Le Ray if (stm32_port->tx_ch) { 138087fd0741SErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 138134891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 138287fd0741SErwan Le Ray } 138334891872SAlexandre TORGUE 138434891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 138534891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 138634891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 138734891872SAlexandre TORGUE stm32_port->tx_dma_buf); 1388511c7b1bSAlexandre TORGUE 1389*3d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 13905297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1391270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 13925297f274SErwan Le Ray } 1393270e5a74SFabrice Gasnier 139497f3a085SErwan Le Ray stm32_usart_deinit_port(stm32_port); 139548a6092fSMaxime Coquelin 139687fd0741SErwan Le Ray return 0; 139748a6092fSMaxime Coquelin } 139848a6092fSMaxime Coquelin 139948a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 140056f9a76cSErwan Le Ray static void stm32_usart_console_putchar(struct uart_port *port, int ch) 140148a6092fSMaxime Coquelin { 1402ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1403d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1404ada8618fSAlexandre TORGUE 1405ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 140648a6092fSMaxime Coquelin cpu_relax(); 140748a6092fSMaxime Coquelin 1408ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 140948a6092fSMaxime Coquelin } 141048a6092fSMaxime Coquelin 141156f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s, 141292fc0023SErwan Le Ray unsigned int cnt) 141348a6092fSMaxime Coquelin { 141448a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1415ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1416d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1417d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 141848a6092fSMaxime Coquelin unsigned long flags; 141948a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 142048a6092fSMaxime Coquelin int locked = 1; 142148a6092fSMaxime Coquelin 142248a6092fSMaxime Coquelin local_irq_save(flags); 142348a6092fSMaxime Coquelin if (port->sysrq) 142448a6092fSMaxime Coquelin locked = 0; 142548a6092fSMaxime Coquelin else if (oops_in_progress) 142648a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 142748a6092fSMaxime Coquelin else 142848a6092fSMaxime Coquelin spin_lock(&port->lock); 142948a6092fSMaxime Coquelin 143087f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1431ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 143248a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 143387f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1434ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 143548a6092fSMaxime Coquelin 143656f9a76cSErwan Le Ray uart_console_write(port, s, cnt, stm32_usart_console_putchar); 143748a6092fSMaxime Coquelin 143848a6092fSMaxime Coquelin /* Restore interrupt state */ 1439ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 144048a6092fSMaxime Coquelin 144148a6092fSMaxime Coquelin if (locked) 144248a6092fSMaxime Coquelin spin_unlock(&port->lock); 144348a6092fSMaxime Coquelin local_irq_restore(flags); 144448a6092fSMaxime Coquelin } 144548a6092fSMaxime Coquelin 144656f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options) 144748a6092fSMaxime Coquelin { 144848a6092fSMaxime Coquelin struct stm32_port *stm32port; 144948a6092fSMaxime Coquelin int baud = 9600; 145048a6092fSMaxime Coquelin int bits = 8; 145148a6092fSMaxime Coquelin int parity = 'n'; 145248a6092fSMaxime Coquelin int flow = 'n'; 145348a6092fSMaxime Coquelin 145448a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 145548a6092fSMaxime Coquelin return -ENODEV; 145648a6092fSMaxime Coquelin 145748a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 145848a6092fSMaxime Coquelin 145948a6092fSMaxime Coquelin /* 146048a6092fSMaxime Coquelin * This driver does not support early console initialization 146148a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 146248a6092fSMaxime Coquelin * this to be called during the uart port registration when the 146348a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 146448a6092fSMaxime Coquelin */ 146592fc0023SErwan Le Ray if (stm32port->port.mapbase == 0 || !stm32port->port.membase) 146648a6092fSMaxime Coquelin return -ENXIO; 146748a6092fSMaxime Coquelin 146848a6092fSMaxime Coquelin if (options) 146948a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 147048a6092fSMaxime Coquelin 147148a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 147248a6092fSMaxime Coquelin } 147348a6092fSMaxime Coquelin 147448a6092fSMaxime Coquelin static struct console stm32_console = { 147548a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 147648a6092fSMaxime Coquelin .device = uart_console_device, 147756f9a76cSErwan Le Ray .write = stm32_usart_console_write, 147856f9a76cSErwan Le Ray .setup = stm32_usart_console_setup, 147948a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 148048a6092fSMaxime Coquelin .index = -1, 148148a6092fSMaxime Coquelin .data = &stm32_usart_driver, 148248a6092fSMaxime Coquelin }; 148348a6092fSMaxime Coquelin 148448a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 148548a6092fSMaxime Coquelin 148648a6092fSMaxime Coquelin #else 148748a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 148848a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 148948a6092fSMaxime Coquelin 149048a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 149148a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 149248a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 149348a6092fSMaxime Coquelin .major = 0, 149448a6092fSMaxime Coquelin .minor = 0, 149548a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 149648a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 149748a6092fSMaxime Coquelin }; 149848a6092fSMaxime Coquelin 149956f9a76cSErwan Le Ray static void __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port, 1500fe94347dSErwan Le Ray bool enable) 1501270e5a74SFabrice Gasnier { 1502270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1503d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1504270e5a74SFabrice Gasnier 1505*3d530017SAlexandre Torgue if (!stm32_port->wakeup_src) 1506270e5a74SFabrice Gasnier return; 1507270e5a74SFabrice Gasnier 150812761869SErwan Le Ray /* 150912761869SErwan Le Ray * Enable low-power wake-up and wake-up irq if argument is set to 151012761869SErwan Le Ray * "enable", disable low-power wake-up and wake-up irq otherwise 151112761869SErwan Le Ray */ 1512270e5a74SFabrice Gasnier if (enable) { 151356f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM); 151412761869SErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE); 1515270e5a74SFabrice Gasnier } else { 151656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM); 151712761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 1518270e5a74SFabrice Gasnier } 1519270e5a74SFabrice Gasnier } 1520270e5a74SFabrice Gasnier 152156f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev) 1522270e5a74SFabrice Gasnier { 1523270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1524270e5a74SFabrice Gasnier 1525270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1526270e5a74SFabrice Gasnier 15271631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 152856f9a76cSErwan Le Ray stm32_usart_serial_en_wakeup(port, true); 1529270e5a74SFabrice Gasnier 153055484fccSErwan Le Ray /* 153155484fccSErwan Le Ray * When "no_console_suspend" is enabled, keep the pinctrl default state 153255484fccSErwan Le Ray * and rely on bootloader stage to restore this state upon resume. 153355484fccSErwan Le Ray * Otherwise, apply the idle or sleep states depending on wakeup 153455484fccSErwan Le Ray * capabilities. 153555484fccSErwan Le Ray */ 153655484fccSErwan Le Ray if (console_suspend_enabled || !uart_console(port)) { 15371631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 153855484fccSErwan Le Ray pinctrl_pm_select_idle_state(dev); 153955484fccSErwan Le Ray else 154094616d9aSErwan Le Ray pinctrl_pm_select_sleep_state(dev); 154155484fccSErwan Le Ray } 154294616d9aSErwan Le Ray 1543270e5a74SFabrice Gasnier return 0; 1544270e5a74SFabrice Gasnier } 1545270e5a74SFabrice Gasnier 154656f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev) 1547270e5a74SFabrice Gasnier { 1548270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1549270e5a74SFabrice Gasnier 155094616d9aSErwan Le Ray pinctrl_pm_select_default_state(dev); 155194616d9aSErwan Le Ray 15521631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 155356f9a76cSErwan Le Ray stm32_usart_serial_en_wakeup(port, false); 1554270e5a74SFabrice Gasnier 1555270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1556270e5a74SFabrice Gasnier } 1557270e5a74SFabrice Gasnier 155856f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev) 1559fb6dcef6SErwan Le Ray { 1560fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1561fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1562fb6dcef6SErwan Le Ray struct stm32_port, port); 1563fb6dcef6SErwan Le Ray 1564fb6dcef6SErwan Le Ray clk_disable_unprepare(stm32port->clk); 1565fb6dcef6SErwan Le Ray 1566fb6dcef6SErwan Le Ray return 0; 1567fb6dcef6SErwan Le Ray } 1568fb6dcef6SErwan Le Ray 156956f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev) 1570fb6dcef6SErwan Le Ray { 1571fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1572fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1573fb6dcef6SErwan Le Ray struct stm32_port, port); 1574fb6dcef6SErwan Le Ray 1575fb6dcef6SErwan Le Ray return clk_prepare_enable(stm32port->clk); 1576fb6dcef6SErwan Le Ray } 1577fb6dcef6SErwan Le Ray 1578270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 157956f9a76cSErwan Le Ray SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend, 158056f9a76cSErwan Le Ray stm32_usart_runtime_resume, NULL) 158156f9a76cSErwan Le Ray SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend, 158256f9a76cSErwan Le Ray stm32_usart_serial_resume) 1583270e5a74SFabrice Gasnier }; 1584270e5a74SFabrice Gasnier 158548a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 158656f9a76cSErwan Le Ray .probe = stm32_usart_serial_probe, 158756f9a76cSErwan Le Ray .remove = stm32_usart_serial_remove, 158848a6092fSMaxime Coquelin .driver = { 158948a6092fSMaxime Coquelin .name = DRIVER_NAME, 1590270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 159148a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 159248a6092fSMaxime Coquelin }, 159348a6092fSMaxime Coquelin }; 159448a6092fSMaxime Coquelin 159556f9a76cSErwan Le Ray static int __init stm32_usart_init(void) 159648a6092fSMaxime Coquelin { 159748a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 159848a6092fSMaxime Coquelin int ret; 159948a6092fSMaxime Coquelin 160048a6092fSMaxime Coquelin pr_info("%s\n", banner); 160148a6092fSMaxime Coquelin 160248a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 160348a6092fSMaxime Coquelin if (ret) 160448a6092fSMaxime Coquelin return ret; 160548a6092fSMaxime Coquelin 160648a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 160748a6092fSMaxime Coquelin if (ret) 160848a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 160948a6092fSMaxime Coquelin 161048a6092fSMaxime Coquelin return ret; 161148a6092fSMaxime Coquelin } 161248a6092fSMaxime Coquelin 161356f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void) 161448a6092fSMaxime Coquelin { 161548a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 161648a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 161748a6092fSMaxime Coquelin } 161848a6092fSMaxime Coquelin 161956f9a76cSErwan Le Ray module_init(stm32_usart_init); 162056f9a76cSErwan Le Ray module_exit(stm32_usart_exit); 162148a6092fSMaxime Coquelin 162248a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 162348a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 162448a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1625