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 16833bb2f6aSErwan Le Ray static bool stm32_usart_rx_dma_enabled(struct uart_port *port) 16934891872SAlexandre TORGUE { 17034891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 171d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 17233bb2f6aSErwan Le Ray 17333bb2f6aSErwan Le Ray if (!stm32_port->rx_ch) 17433bb2f6aSErwan Le Ray return false; 17533bb2f6aSErwan Le Ray 17633bb2f6aSErwan Le Ray return !!(readl_relaxed(port->membase + ofs->cr3) & USART_CR3_DMAR); 17733bb2f6aSErwan Le Ray } 17833bb2f6aSErwan Le Ray 17933bb2f6aSErwan Le Ray /* Return true when data is pending (in pio mode), and false when no data is pending. */ 18033bb2f6aSErwan Le Ray static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr) 18133bb2f6aSErwan Le Ray { 18233bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 18333bb2f6aSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 18434891872SAlexandre TORGUE 18534891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 18633bb2f6aSErwan Le Ray /* Get pending characters in RDR or FIFO */ 18733bb2f6aSErwan Le Ray if (*sr & USART_SR_RXNE) { 18833bb2f6aSErwan Le Ray /* Get all pending characters from the RDR or the FIFO when using interrupts */ 18933bb2f6aSErwan Le Ray if (!stm32_usart_rx_dma_enabled(port)) 19033bb2f6aSErwan Le Ray return true; 19134891872SAlexandre TORGUE 19233bb2f6aSErwan Le Ray /* Handle only RX data errors when using DMA */ 19333bb2f6aSErwan Le Ray if (*sr & USART_SR_ERR_MASK) 19433bb2f6aSErwan Le Ray return true; 19534891872SAlexandre TORGUE } 19634891872SAlexandre TORGUE 19733bb2f6aSErwan Le Ray return false; 19833bb2f6aSErwan Le Ray } 19933bb2f6aSErwan Le Ray 20033bb2f6aSErwan Le Ray static unsigned long stm32_usart_get_char_pio(struct uart_port *port) 20134891872SAlexandre TORGUE { 20234891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 203d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 20434891872SAlexandre TORGUE unsigned long c; 20534891872SAlexandre TORGUE 2066c5962f3SErwan Le Ray c = readl_relaxed(port->membase + ofs->rdr); 20733bb2f6aSErwan Le Ray /* Apply RDR data mask */ 2086c5962f3SErwan Le Ray c &= stm32_port->rdr_mask; 2096c5962f3SErwan Le Ray 2106c5962f3SErwan Le Ray return c; 21134891872SAlexandre TORGUE } 21234891872SAlexandre TORGUE 21333bb2f6aSErwan Le Ray static void stm32_usart_receive_chars_pio(struct uart_port *port) 21448a6092fSMaxime Coquelin { 215ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 216d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 21733bb2f6aSErwan Le Ray unsigned long c; 21848a6092fSMaxime Coquelin u32 sr; 21948a6092fSMaxime Coquelin char flag; 22048a6092fSMaxime Coquelin 22133bb2f6aSErwan Le Ray while (stm32_usart_pending_rx_pio(port, &sr)) { 22248a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22348a6092fSMaxime Coquelin flag = TTY_NORMAL; 22448a6092fSMaxime Coquelin 2254f01d833SErwan Le Ray /* 2264f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2274f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2284f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2294f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2304f01d833SErwan Le Ray * and clear status bits of the next rx data. 2314f01d833SErwan Le Ray * 2324f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2334f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 2344f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 2354f01d833SErwan Le Ray */ 2364f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 2371250ed71SFabrice Gasnier writel_relaxed(sr & USART_SR_ERR_MASK, 2381250ed71SFabrice Gasnier port->membase + ofs->icr); 2394f01d833SErwan Le Ray 24033bb2f6aSErwan Le Ray c = stm32_usart_get_char_pio(port); 2414f01d833SErwan Le Ray port->icount.rx++; 24248a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 2434f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 24448a6092fSMaxime Coquelin port->icount.overrun++; 24548a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 24648a6092fSMaxime Coquelin port->icount.parity++; 24748a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 2484f01d833SErwan Le Ray /* Break detection if character is null */ 2494f01d833SErwan Le Ray if (!c) { 2504f01d833SErwan Le Ray port->icount.brk++; 2514f01d833SErwan Le Ray if (uart_handle_break(port)) 2524f01d833SErwan Le Ray continue; 2534f01d833SErwan Le Ray } else { 25448a6092fSMaxime Coquelin port->icount.frame++; 25548a6092fSMaxime Coquelin } 2564f01d833SErwan Le Ray } 25748a6092fSMaxime Coquelin 25848a6092fSMaxime Coquelin sr &= port->read_status_mask; 25948a6092fSMaxime Coquelin 2604f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26148a6092fSMaxime Coquelin flag = TTY_PARITY; 2624f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 2634f01d833SErwan Le Ray if (!c) 2644f01d833SErwan Le Ray flag = TTY_BREAK; 2654f01d833SErwan Le Ray else 26648a6092fSMaxime Coquelin flag = TTY_FRAME; 26748a6092fSMaxime Coquelin } 2684f01d833SErwan Le Ray } 26948a6092fSMaxime Coquelin 270cea37afdSJohan Hovold if (uart_prepare_sysrq_char(port, c)) 27148a6092fSMaxime Coquelin continue; 27248a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27348a6092fSMaxime Coquelin } 27433bb2f6aSErwan Le Ray } 27533bb2f6aSErwan Le Ray 27633bb2f6aSErwan Le Ray static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size) 27733bb2f6aSErwan Le Ray { 27833bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 27933bb2f6aSErwan Le Ray struct tty_port *ttyport = &stm32_port->port.state->port; 28033bb2f6aSErwan Le Ray unsigned char *dma_start; 28133bb2f6aSErwan Le Ray int dma_count, i; 28233bb2f6aSErwan Le Ray 28333bb2f6aSErwan Le Ray dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res); 28433bb2f6aSErwan Le Ray 28533bb2f6aSErwan Le Ray /* 28633bb2f6aSErwan Le Ray * Apply rdr_mask on buffer in order to mask parity bit. 28733bb2f6aSErwan Le Ray * This loop is useless in cs8 mode because DMA copies only 28833bb2f6aSErwan Le Ray * 8 bits and already ignores parity bit. 28933bb2f6aSErwan Le Ray */ 29033bb2f6aSErwan Le Ray if (!(stm32_port->rdr_mask == (BIT(8) - 1))) 29133bb2f6aSErwan Le Ray for (i = 0; i < dma_size; i++) 29233bb2f6aSErwan Le Ray *(dma_start + i) &= stm32_port->rdr_mask; 29333bb2f6aSErwan Le Ray 29433bb2f6aSErwan Le Ray dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size); 29533bb2f6aSErwan Le Ray port->icount.rx += dma_count; 29633bb2f6aSErwan Le Ray if (dma_count != dma_size) 29733bb2f6aSErwan Le Ray port->icount.buf_overrun++; 29833bb2f6aSErwan Le Ray stm32_port->last_res -= dma_count; 29933bb2f6aSErwan Le Ray if (stm32_port->last_res == 0) 30033bb2f6aSErwan Le Ray stm32_port->last_res = RX_BUF_L; 30133bb2f6aSErwan Le Ray } 30233bb2f6aSErwan Le Ray 30333bb2f6aSErwan Le Ray static void stm32_usart_receive_chars_dma(struct uart_port *port) 30433bb2f6aSErwan Le Ray { 30533bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 30633bb2f6aSErwan Le Ray unsigned int dma_size; 30733bb2f6aSErwan Le Ray 30833bb2f6aSErwan Le Ray /* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */ 30933bb2f6aSErwan Le Ray if (stm32_port->rx_dma_state.residue > stm32_port->last_res) { 31033bb2f6aSErwan Le Ray /* Conditional first part: from last_res to end of DMA buffer */ 31133bb2f6aSErwan Le Ray dma_size = stm32_port->last_res; 31233bb2f6aSErwan Le Ray stm32_usart_push_buffer_dma(port, dma_size); 31333bb2f6aSErwan Le Ray } 31433bb2f6aSErwan Le Ray 31533bb2f6aSErwan Le Ray dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue; 31633bb2f6aSErwan Le Ray stm32_usart_push_buffer_dma(port, dma_size); 31733bb2f6aSErwan Le Ray } 31833bb2f6aSErwan Le Ray 31933bb2f6aSErwan Le Ray static void stm32_usart_receive_chars(struct uart_port *port, bool irqflag) 32033bb2f6aSErwan Le Ray { 32133bb2f6aSErwan Le Ray struct tty_port *tport = &port->state->port; 32233bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 32333bb2f6aSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 32433bb2f6aSErwan Le Ray enum dma_status rx_dma_status; 32533bb2f6aSErwan Le Ray unsigned long flags; 32633bb2f6aSErwan Le Ray u32 sr; 32733bb2f6aSErwan Le Ray 32833bb2f6aSErwan Le Ray if (irqflag) 32933bb2f6aSErwan Le Ray spin_lock_irqsave(&port->lock, flags); 33033bb2f6aSErwan Le Ray else 33133bb2f6aSErwan Le Ray spin_lock(&port->lock); 33233bb2f6aSErwan Le Ray 33333bb2f6aSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) { 33433bb2f6aSErwan Le Ray rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch, 33533bb2f6aSErwan Le Ray stm32_port->rx_ch->cookie, 33633bb2f6aSErwan Le Ray &stm32_port->rx_dma_state); 33733bb2f6aSErwan Le Ray if (rx_dma_status == DMA_IN_PROGRESS) { 33833bb2f6aSErwan Le Ray /* Empty DMA buffer */ 33933bb2f6aSErwan Le Ray stm32_usart_receive_chars_dma(port); 34033bb2f6aSErwan Le Ray sr = readl_relaxed(port->membase + ofs->isr); 34133bb2f6aSErwan Le Ray if (sr & USART_SR_ERR_MASK) { 34233bb2f6aSErwan Le Ray /* Disable DMA request line */ 34333bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 34433bb2f6aSErwan Le Ray 34533bb2f6aSErwan Le Ray /* Switch to PIO mode to handle the errors */ 34633bb2f6aSErwan Le Ray stm32_usart_receive_chars_pio(port); 34733bb2f6aSErwan Le Ray 34833bb2f6aSErwan Le Ray /* Switch back to DMA mode */ 34933bb2f6aSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 35033bb2f6aSErwan Le Ray } 35133bb2f6aSErwan Le Ray } else { 35233bb2f6aSErwan Le Ray /* Disable RX DMA */ 35333bb2f6aSErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 35433bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 35533bb2f6aSErwan Le Ray /* Fall back to interrupt mode */ 35633bb2f6aSErwan Le Ray dev_dbg(port->dev, "DMA error, fallback to irq mode\n"); 35733bb2f6aSErwan Le Ray stm32_usart_receive_chars_pio(port); 35833bb2f6aSErwan Le Ray } 35933bb2f6aSErwan Le Ray } else { 36033bb2f6aSErwan Le Ray stm32_usart_receive_chars_pio(port); 36133bb2f6aSErwan Le Ray } 36248a6092fSMaxime Coquelin 363cc58d0a3SErwan Le Ray if (irqflag) 364cc58d0a3SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, irqflag); 365cc58d0a3SErwan Le Ray else 366cea37afdSJohan Hovold uart_unlock_and_check_sysrq(port); 367ad767681SErwan Le Ray 36848a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 36948a6092fSMaxime Coquelin } 37048a6092fSMaxime Coquelin 37156f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg) 37234891872SAlexandre TORGUE { 37334891872SAlexandre TORGUE struct uart_port *port = arg; 37434891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 375d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 376f16b90c2SErwan Le Ray unsigned long flags; 37734891872SAlexandre TORGUE 378fb4f2e04SErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 37956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 38034891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 38134891872SAlexandre TORGUE 38234891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 383f16b90c2SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 38456f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 385f16b90c2SErwan Le Ray spin_unlock_irqrestore(&port->lock, flags); 38634891872SAlexandre TORGUE } 38734891872SAlexandre TORGUE 38856f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port) 389d075719eSErwan Le Ray { 390d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 391d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 392d075719eSErwan Le Ray 393d075719eSErwan Le Ray /* 394d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 395d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 396d075719eSErwan Le Ray */ 3972aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 39856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 399d075719eSErwan Le Ray else 40056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 401d075719eSErwan Le Ray } 402d075719eSErwan Le Ray 40333bb2f6aSErwan Le Ray static void stm32_usart_rx_dma_complete(void *arg) 40433bb2f6aSErwan Le Ray { 40533bb2f6aSErwan Le Ray struct uart_port *port = arg; 40633bb2f6aSErwan Le Ray 40733bb2f6aSErwan Le Ray stm32_usart_receive_chars(port, true); 40833bb2f6aSErwan Le Ray } 40933bb2f6aSErwan Le Ray 41056f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port) 411d075719eSErwan Le Ray { 412d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 413d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 414d075719eSErwan Le Ray 4152aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 41656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 417d075719eSErwan Le Ray else 41856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 419d075719eSErwan Le Ray } 420d075719eSErwan Le Ray 42156f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port) 42234891872SAlexandre TORGUE { 42334891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 424d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 42534891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 42634891872SAlexandre TORGUE 42734891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 42856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 42934891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 43034891872SAlexandre TORGUE } 43134891872SAlexandre TORGUE 4325d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 4335d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 4345d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 4355d9176edSErwan Le Ray break; 43634891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 43734891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 43834891872SAlexandre TORGUE port->icount.tx++; 43934891872SAlexandre TORGUE } 44034891872SAlexandre TORGUE 4415d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 4425d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 44356f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 4445d9176edSErwan Le Ray else 44556f9a76cSErwan Le Ray stm32_usart_tx_interrupt_enable(port); 4465d9176edSErwan Le Ray } 4475d9176edSErwan Le Ray 44856f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port) 44934891872SAlexandre TORGUE { 45034891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 451d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 45234891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 45334891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 45434891872SAlexandre TORGUE unsigned int count, i; 45534891872SAlexandre TORGUE 45634891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 45734891872SAlexandre TORGUE return; 45834891872SAlexandre TORGUE 45934891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 46034891872SAlexandre TORGUE 46134891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 46234891872SAlexandre TORGUE 46334891872SAlexandre TORGUE if (count > TX_BUF_L) 46434891872SAlexandre TORGUE count = TX_BUF_L; 46534891872SAlexandre TORGUE 46634891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 46734891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 46834891872SAlexandre TORGUE } else { 46934891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 47034891872SAlexandre TORGUE size_t two; 47134891872SAlexandre TORGUE 47234891872SAlexandre TORGUE if (one > count) 47334891872SAlexandre TORGUE one = count; 47434891872SAlexandre TORGUE two = count - one; 47534891872SAlexandre TORGUE 47634891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 47734891872SAlexandre TORGUE if (two) 47834891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 47934891872SAlexandre TORGUE } 48034891872SAlexandre TORGUE 48134891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 48234891872SAlexandre TORGUE stm32port->tx_dma_buf, 48334891872SAlexandre TORGUE count, 48434891872SAlexandre TORGUE DMA_MEM_TO_DEV, 48534891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 48634891872SAlexandre TORGUE 487e7997f7fSErwan Le Ray if (!desc) 488e7997f7fSErwan Le Ray goto fallback_err; 48934891872SAlexandre TORGUE 49056f9a76cSErwan Le Ray desc->callback = stm32_usart_tx_dma_complete; 49134891872SAlexandre TORGUE desc->callback_param = port; 49234891872SAlexandre TORGUE 49334891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 494e7997f7fSErwan Le Ray if (dma_submit_error(dmaengine_submit(desc))) { 495e7997f7fSErwan Le Ray /* dma no yet started, safe to free resources */ 496e7997f7fSErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 497e7997f7fSErwan Le Ray goto fallback_err; 498e7997f7fSErwan Le Ray } 49934891872SAlexandre TORGUE 50034891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 50134891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 50234891872SAlexandre TORGUE 50356f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 50434891872SAlexandre TORGUE 50534891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 50634891872SAlexandre TORGUE port->icount.tx += count; 507e7997f7fSErwan Le Ray return; 508e7997f7fSErwan Le Ray 509e7997f7fSErwan Le Ray fallback_err: 510e7997f7fSErwan Le Ray for (i = count; i > 0; i--) 51156f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 51234891872SAlexandre TORGUE } 51334891872SAlexandre TORGUE 51456f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port) 51548a6092fSMaxime Coquelin { 516ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 517d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 51848a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 51948a6092fSMaxime Coquelin 52048a6092fSMaxime Coquelin if (port->x_char) { 52134891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 52256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 523ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 52448a6092fSMaxime Coquelin port->x_char = 0; 52548a6092fSMaxime Coquelin port->icount.tx++; 52634891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 52756f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 52848a6092fSMaxime Coquelin return; 52948a6092fSMaxime Coquelin } 53048a6092fSMaxime Coquelin 531b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 53256f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 53348a6092fSMaxime Coquelin return; 53448a6092fSMaxime Coquelin } 53548a6092fSMaxime Coquelin 53664c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 53756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC); 53864c32eabSErwan Le Ray else 5391250ed71SFabrice Gasnier writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr); 54064c32eabSErwan Le Ray 54134891872SAlexandre TORGUE if (stm32_port->tx_ch) 54256f9a76cSErwan Le Ray stm32_usart_transmit_chars_dma(port); 54334891872SAlexandre TORGUE else 54456f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 54548a6092fSMaxime Coquelin 54648a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 54748a6092fSMaxime Coquelin uart_write_wakeup(port); 54848a6092fSMaxime Coquelin 54948a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 55056f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 55148a6092fSMaxime Coquelin } 55248a6092fSMaxime Coquelin 55356f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) 55448a6092fSMaxime Coquelin { 55548a6092fSMaxime Coquelin struct uart_port *port = ptr; 55612761869SErwan Le Ray struct tty_port *tport = &port->state->port; 557ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 558d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 55948a6092fSMaxime Coquelin u32 sr; 56048a6092fSMaxime Coquelin 561ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 56248a6092fSMaxime Coquelin 5634cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 5644cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 5654cc0ed62SErwan Le Ray port->membase + ofs->icr); 5664cc0ed62SErwan Le Ray 56712761869SErwan Le Ray if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) { 56812761869SErwan Le Ray /* Clear wake up flag and disable wake up interrupt */ 569270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 570270e5a74SFabrice Gasnier port->membase + ofs->icr); 57112761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 57212761869SErwan Le Ray if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 57312761869SErwan Le Ray pm_wakeup_event(tport->tty->dev, 0); 57412761869SErwan Le Ray } 575270e5a74SFabrice Gasnier 57633bb2f6aSErwan Le Ray /* 57733bb2f6aSErwan Le Ray * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request 57833bb2f6aSErwan Le Ray * line has been masked by HW and rx data are stacking in FIFO. 57933bb2f6aSErwan Le Ray */ 580d1ec8a2eSErwan Le Ray if (!stm32_port->throttled) { 58133bb2f6aSErwan Le Ray if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_enabled(port)) || 582d1ec8a2eSErwan Le Ray ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_enabled(port))) { 58356f9a76cSErwan Le Ray stm32_usart_receive_chars(port, false); 584d1ec8a2eSErwan Le Ray } 585d1ec8a2eSErwan Le Ray } 58648a6092fSMaxime Coquelin 587ad767681SErwan Le Ray if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) { 588ad767681SErwan Le Ray spin_lock(&port->lock); 58956f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 59001d32d71SAlexandre TORGUE spin_unlock(&port->lock); 591ad767681SErwan Le Ray } 59201d32d71SAlexandre TORGUE 59333bb2f6aSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) 59434891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 59534891872SAlexandre TORGUE else 59634891872SAlexandre TORGUE return IRQ_HANDLED; 59734891872SAlexandre TORGUE } 59834891872SAlexandre TORGUE 59956f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr) 60034891872SAlexandre TORGUE { 60134891872SAlexandre TORGUE struct uart_port *port = ptr; 602d1ec8a2eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 60334891872SAlexandre TORGUE 604cc58d0a3SErwan Le Ray /* Receiver timeout irq for DMA RX */ 605d1ec8a2eSErwan Le Ray if (!stm32_port->throttled) 606cc58d0a3SErwan Le Ray stm32_usart_receive_chars(port, false); 60734891872SAlexandre TORGUE 60848a6092fSMaxime Coquelin return IRQ_HANDLED; 60948a6092fSMaxime Coquelin } 61048a6092fSMaxime Coquelin 61156f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port) 61248a6092fSMaxime Coquelin { 613ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 614d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 615ada8618fSAlexandre TORGUE 6163db1d524SErwan Le Ray if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC) 6173db1d524SErwan Le Ray return TIOCSER_TEMT; 6183db1d524SErwan Le Ray 6193db1d524SErwan Le Ray return 0; 62048a6092fSMaxime Coquelin } 62148a6092fSMaxime Coquelin 62256f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl) 62348a6092fSMaxime Coquelin { 624ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 625d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 626ada8618fSAlexandre TORGUE 62748a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 62856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE); 62948a6092fSMaxime Coquelin else 63056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 6316cf61b9bSManivannan Sadhasivam 6326cf61b9bSManivannan Sadhasivam mctrl_gpio_set(stm32_port->gpios, mctrl); 63348a6092fSMaxime Coquelin } 63448a6092fSMaxime Coquelin 63556f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port) 63648a6092fSMaxime Coquelin { 6376cf61b9bSManivannan Sadhasivam struct stm32_port *stm32_port = to_stm32_port(port); 6386cf61b9bSManivannan Sadhasivam unsigned int ret; 6396cf61b9bSManivannan Sadhasivam 64048a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 6416cf61b9bSManivannan Sadhasivam ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 6426cf61b9bSManivannan Sadhasivam 6436cf61b9bSManivannan Sadhasivam return mctrl_gpio_get(stm32_port->gpios, &ret); 6446cf61b9bSManivannan Sadhasivam } 6456cf61b9bSManivannan Sadhasivam 64656f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port) 6476cf61b9bSManivannan Sadhasivam { 6486cf61b9bSManivannan Sadhasivam mctrl_gpio_enable_ms(to_stm32_port(port)->gpios); 6496cf61b9bSManivannan Sadhasivam } 6506cf61b9bSManivannan Sadhasivam 65156f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port) 6526cf61b9bSManivannan Sadhasivam { 6536cf61b9bSManivannan Sadhasivam mctrl_gpio_disable_ms(to_stm32_port(port)->gpios); 65448a6092fSMaxime Coquelin } 65548a6092fSMaxime Coquelin 65648a6092fSMaxime Coquelin /* Transmit stop */ 65756f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port) 65848a6092fSMaxime Coquelin { 659ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 660ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 661ad0c2748SMarek Vasut 66256f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 663ad0c2748SMarek Vasut 664ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 665ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 666ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 667ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 668ad0c2748SMarek Vasut } else { 669ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 670ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 671ad0c2748SMarek Vasut } 672ad0c2748SMarek Vasut } 67348a6092fSMaxime Coquelin } 67448a6092fSMaxime Coquelin 67548a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 67656f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port) 67748a6092fSMaxime Coquelin { 678ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 679ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 68048a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 68148a6092fSMaxime Coquelin 68248a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 68348a6092fSMaxime Coquelin return; 68448a6092fSMaxime Coquelin 685ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 686ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 687ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 688ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 689ad0c2748SMarek Vasut } else { 690ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 691ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 692ad0c2748SMarek Vasut } 693ad0c2748SMarek Vasut } 694ad0c2748SMarek Vasut 69556f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 69648a6092fSMaxime Coquelin } 69748a6092fSMaxime Coquelin 6983d82be8bSErwan Le Ray /* Flush the transmit buffer. */ 6993d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port) 7003d82be8bSErwan Le Ray { 7013d82be8bSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 7023d82be8bSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 7033d82be8bSErwan Le Ray 7043d82be8bSErwan Le Ray if (stm32_port->tx_ch) { 7053d82be8bSErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 7063d82be8bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 7073d82be8bSErwan Le Ray stm32_port->tx_dma_busy = false; 7083d82be8bSErwan Le Ray } 7093d82be8bSErwan Le Ray } 7103d82be8bSErwan Le Ray 71148a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 71256f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port) 71348a6092fSMaxime Coquelin { 714ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 715d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 71648a6092fSMaxime Coquelin unsigned long flags; 71748a6092fSMaxime Coquelin 71848a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 719d1ec8a2eSErwan Le Ray 720d1ec8a2eSErwan Le Ray /* 721d1ec8a2eSErwan Le Ray * Disable DMA request line if enabled, so the RX data gets queued into the FIFO. 722d1ec8a2eSErwan Le Ray * Hardware flow control is triggered when RX FIFO is full. 723d1ec8a2eSErwan Le Ray */ 724d1ec8a2eSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) 725d1ec8a2eSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 726d1ec8a2eSErwan Le Ray 72756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 728d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 72956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 730d0a6a7bcSErwan Le Ray 731d1ec8a2eSErwan Le Ray stm32_port->throttled = true; 73248a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 73348a6092fSMaxime Coquelin } 73448a6092fSMaxime Coquelin 73548a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 73656f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port) 73748a6092fSMaxime Coquelin { 738ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 739d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 74048a6092fSMaxime Coquelin unsigned long flags; 74148a6092fSMaxime Coquelin 74248a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 74356f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 744d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 74556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq); 746d0a6a7bcSErwan Le Ray 747d1ec8a2eSErwan Le Ray /* 748d1ec8a2eSErwan Le Ray * Switch back to DMA mode (re-enable DMA request line). 749d1ec8a2eSErwan Le Ray * Hardware flow control is stopped when FIFO is not full any more. 750d1ec8a2eSErwan Le Ray */ 751d1ec8a2eSErwan Le Ray if (stm32_port->rx_ch) 752d1ec8a2eSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 753d1ec8a2eSErwan Le Ray 754d1ec8a2eSErwan Le Ray stm32_port->throttled = false; 75548a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 75648a6092fSMaxime Coquelin } 75748a6092fSMaxime Coquelin 75848a6092fSMaxime Coquelin /* Receive stop */ 75956f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port) 76048a6092fSMaxime Coquelin { 761ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 762d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 763ada8618fSAlexandre TORGUE 764e0abc903SErwan Le Ray /* Disable DMA request line. */ 765e0abc903SErwan Le Ray if (stm32_port->rx_ch) 766e0abc903SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 767e0abc903SErwan Le Ray 76856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 769d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 77056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 77148a6092fSMaxime Coquelin } 77248a6092fSMaxime Coquelin 77348a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 77456f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state) 77548a6092fSMaxime Coquelin { 77648a6092fSMaxime Coquelin } 77748a6092fSMaxime Coquelin 778*6eeb348cSErwan Le Ray static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port) 779*6eeb348cSErwan Le Ray { 780*6eeb348cSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 781*6eeb348cSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 782*6eeb348cSErwan Le Ray struct dma_async_tx_descriptor *desc; 783*6eeb348cSErwan Le Ray int ret; 784*6eeb348cSErwan Le Ray 785*6eeb348cSErwan Le Ray stm32_port->last_res = RX_BUF_L; 786*6eeb348cSErwan Le Ray /* Prepare a DMA cyclic transaction */ 787*6eeb348cSErwan Le Ray desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch, 788*6eeb348cSErwan Le Ray stm32_port->rx_dma_buf, 789*6eeb348cSErwan Le Ray RX_BUF_L, RX_BUF_P, 790*6eeb348cSErwan Le Ray DMA_DEV_TO_MEM, 791*6eeb348cSErwan Le Ray DMA_PREP_INTERRUPT); 792*6eeb348cSErwan Le Ray if (!desc) { 793*6eeb348cSErwan Le Ray dev_err(port->dev, "rx dma prep cyclic failed\n"); 794*6eeb348cSErwan Le Ray return -ENODEV; 795*6eeb348cSErwan Le Ray } 796*6eeb348cSErwan Le Ray 797*6eeb348cSErwan Le Ray desc->callback = stm32_usart_rx_dma_complete; 798*6eeb348cSErwan Le Ray desc->callback_param = port; 799*6eeb348cSErwan Le Ray 800*6eeb348cSErwan Le Ray /* Push current DMA transaction in the pending queue */ 801*6eeb348cSErwan Le Ray ret = dma_submit_error(dmaengine_submit(desc)); 802*6eeb348cSErwan Le Ray if (ret) { 803*6eeb348cSErwan Le Ray dmaengine_terminate_sync(stm32_port->rx_ch); 804*6eeb348cSErwan Le Ray return ret; 805*6eeb348cSErwan Le Ray } 806*6eeb348cSErwan Le Ray 807*6eeb348cSErwan Le Ray /* Issue pending DMA requests */ 808*6eeb348cSErwan Le Ray dma_async_issue_pending(stm32_port->rx_ch); 809*6eeb348cSErwan Le Ray 810*6eeb348cSErwan Le Ray /* 811*6eeb348cSErwan Le Ray * DMA request line not re-enabled at resume when port is throttled. 812*6eeb348cSErwan Le Ray * It will be re-enabled by unthrottle ops. 813*6eeb348cSErwan Le Ray */ 814*6eeb348cSErwan Le Ray if (!stm32_port->throttled) 815*6eeb348cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 816*6eeb348cSErwan Le Ray 817*6eeb348cSErwan Le Ray return 0; 818*6eeb348cSErwan Le Ray } 819*6eeb348cSErwan Le Ray 82056f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port) 82148a6092fSMaxime Coquelin { 822ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 823d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 824f4518a8aSErwan Le Ray const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 82548a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 82648a6092fSMaxime Coquelin u32 val; 82748a6092fSMaxime Coquelin int ret; 82848a6092fSMaxime Coquelin 82956f9a76cSErwan Le Ray ret = request_threaded_irq(port->irq, stm32_usart_interrupt, 83056f9a76cSErwan Le Ray stm32_usart_threaded_interrupt, 831e359b441SJohan Hovold IRQF_ONESHOT | IRQF_NO_SUSPEND, 832e359b441SJohan Hovold name, port); 83348a6092fSMaxime Coquelin if (ret) 83448a6092fSMaxime Coquelin return ret; 83548a6092fSMaxime Coquelin 8363cd66593SMartin Devera if (stm32_port->swap) { 8373cd66593SMartin Devera val = readl_relaxed(port->membase + ofs->cr2); 8383cd66593SMartin Devera val |= USART_CR2_SWAP; 8393cd66593SMartin Devera writel_relaxed(val, port->membase + ofs->cr2); 8403cd66593SMartin Devera } 8413cd66593SMartin Devera 84284872dc4SErwan Le Ray /* RX FIFO Flush */ 84384872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 844315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr); 84548a6092fSMaxime Coquelin 846e0abc903SErwan Le Ray if (stm32_port->rx_ch) { 847*6eeb348cSErwan Le Ray ret = stm32_usart_start_rx_dma_cyclic(port); 848e0abc903SErwan Le Ray if (ret) { 849*6eeb348cSErwan Le Ray free_irq(port->irq, port); 850*6eeb348cSErwan Le Ray return ret; 851e0abc903SErwan Le Ray } 852e0abc903SErwan Le Ray } 853d1ec8a2eSErwan Le Ray 85425a8e761SErwan Le Ray /* RX enabling */ 855f4518a8aSErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit); 85656f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, val); 85784872dc4SErwan Le Ray 85848a6092fSMaxime Coquelin return 0; 85948a6092fSMaxime Coquelin } 86048a6092fSMaxime Coquelin 86156f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port) 86248a6092fSMaxime Coquelin { 863ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 864d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 865d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 86664c32eabSErwan Le Ray u32 val, isr; 86764c32eabSErwan Le Ray int ret; 86848a6092fSMaxime Coquelin 8696cf61b9bSManivannan Sadhasivam /* Disable modem control interrupts */ 87056f9a76cSErwan Le Ray stm32_usart_disable_ms(port); 8716cf61b9bSManivannan Sadhasivam 8724cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 8734cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 87487f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 875351a762aSGerald Baeza if (stm32_port->fifoen) 876351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 87764c32eabSErwan Le Ray 87864c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 87964c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 88064c32eabSErwan Le Ray 10, 100000); 88164c32eabSErwan Le Ray 882c31c3ea0SErwan Le Ray /* Send the TC error message only when ISR_TC is not set */ 88364c32eabSErwan Le Ray if (ret) 884c31c3ea0SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 88564c32eabSErwan Le Ray 886e0abc903SErwan Le Ray /* Disable RX DMA. */ 887e0abc903SErwan Le Ray if (stm32_port->rx_ch) 888e0abc903SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 889e0abc903SErwan Le Ray 8909f77d192SErwan Le Ray /* flush RX & TX FIFO */ 8919f77d192SErwan Le Ray if (ofs->rqr != UNDEF_REG) 8929f77d192SErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 8939f77d192SErwan Le Ray port->membase + ofs->rqr); 8949f77d192SErwan Le Ray 89556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, val); 89648a6092fSMaxime Coquelin 89748a6092fSMaxime Coquelin free_irq(port->irq, port); 89848a6092fSMaxime Coquelin } 89948a6092fSMaxime Coquelin 90056f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port, 90156f9a76cSErwan Le Ray struct ktermios *termios, 90248a6092fSMaxime Coquelin struct ktermios *old) 90348a6092fSMaxime Coquelin { 90448a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 905d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 906d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 9071bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 908c8a9d043SErwan Le Ray unsigned int baud, bits; 90948a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 91048a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 911f264c6f6SErwan Le Ray u32 cr1, cr2, cr3, isr; 91248a6092fSMaxime Coquelin unsigned long flags; 913f264c6f6SErwan Le Ray int ret; 91448a6092fSMaxime Coquelin 91548a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 91648a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 91748a6092fSMaxime Coquelin 91848a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 91948a6092fSMaxime Coquelin 92048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 92148a6092fSMaxime Coquelin 922f264c6f6SErwan Le Ray ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 923f264c6f6SErwan Le Ray isr, 924f264c6f6SErwan Le Ray (isr & USART_SR_TC), 925f264c6f6SErwan Le Ray 10, 100000); 926f264c6f6SErwan Le Ray 927f264c6f6SErwan Le Ray /* Send the TC error message only when ISR_TC is not set. */ 928f264c6f6SErwan Le Ray if (ret) 929f264c6f6SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 930f264c6f6SErwan Le Ray 93148a6092fSMaxime Coquelin /* Stop serial port and reset value */ 932ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 93348a6092fSMaxime Coquelin 93484872dc4SErwan Le Ray /* flush RX & TX FIFO */ 93584872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 936315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 937315e2d8aSErwan Le Ray port->membase + ofs->rqr); 9381bcda09dSBich HEMON 93984872dc4SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 940351a762aSGerald Baeza if (stm32_port->fifoen) 941351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 9423cd66593SMartin Devera cr2 = stm32_port->swap ? USART_CR2_SWAP : 0; 94325a8e761SErwan Le Ray 94425a8e761SErwan Le Ray /* Tx and RX FIFO configuration */ 945d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 94625a8e761SErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE; 94725a8e761SErwan Le Ray if (stm32_port->fifoen) { 9482aa1bbb2SFabrice Gasnier if (stm32_port->txftcfg >= 0) 9492aa1bbb2SFabrice Gasnier cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT; 9502aa1bbb2SFabrice Gasnier if (stm32_port->rxftcfg >= 0) 9512aa1bbb2SFabrice Gasnier cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT; 95225a8e761SErwan Le Ray } 95348a6092fSMaxime Coquelin 95448a6092fSMaxime Coquelin if (cflag & CSTOPB) 95548a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 95648a6092fSMaxime Coquelin 9573ec2ff37SJiri Slaby bits = tty_get_char_size(cflag); 9586c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 959c8a9d043SErwan Le Ray 96048a6092fSMaxime Coquelin if (cflag & PARENB) { 961c8a9d043SErwan Le Ray bits++; 96248a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 963c8a9d043SErwan Le Ray } 964c8a9d043SErwan Le Ray 965c8a9d043SErwan Le Ray /* 966c8a9d043SErwan Le Ray * Word length configuration: 967c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 968c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 969c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 970c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 971c8a9d043SErwan Le Ray */ 972c8a9d043SErwan Le Ray if (bits == 9) 973ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 974c8a9d043SErwan Le Ray else if ((bits == 7) && cfg->has_7bits_data) 975c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 976c8a9d043SErwan Le Ray else if (bits != 8) 977c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 978c8a9d043SErwan Le Ray , bits); 97948a6092fSMaxime Coquelin 9804cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 9812aa1bbb2SFabrice Gasnier (stm32_port->fifoen && 9822aa1bbb2SFabrice Gasnier stm32_port->rxftcfg >= 0))) { 9834cc0ed62SErwan Le Ray if (cflag & CSTOPB) 9844cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 9854cc0ed62SErwan Le Ray else 9864cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 9874cc0ed62SErwan Le Ray 9884cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 9894cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 9904cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 9914cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 99233bb2f6aSErwan Le Ray /* 99333bb2f6aSErwan Le Ray * Enable fifo threshold irq in two cases, either when there is no DMA, or when 99433bb2f6aSErwan Le Ray * wake up over usart, from low power until the DMA gets re-enabled by resume. 99533bb2f6aSErwan Le Ray */ 996d0a6a7bcSErwan Le Ray stm32_port->cr3_irq = USART_CR3_RXFTIE; 9974cc0ed62SErwan Le Ray } 9984cc0ed62SErwan Le Ray 999d0a6a7bcSErwan Le Ray cr1 |= stm32_port->cr1_irq; 1000d0a6a7bcSErwan Le Ray cr3 |= stm32_port->cr3_irq; 1001d0a6a7bcSErwan Le Ray 100248a6092fSMaxime Coquelin if (cflag & PARODD) 100348a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 100448a6092fSMaxime Coquelin 100548a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 100648a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 100748a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 100835abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 100948a6092fSMaxime Coquelin } 101048a6092fSMaxime Coquelin 101148a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 101248a6092fSMaxime Coquelin 101348a6092fSMaxime Coquelin /* 101448a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 101548a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 101648a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 101748a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 101848a6092fSMaxime Coquelin */ 101948a6092fSMaxime Coquelin if (usartdiv < 16) { 102048a6092fSMaxime Coquelin oversampling = 8; 10211bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 102256f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8); 102348a6092fSMaxime Coquelin } else { 102448a6092fSMaxime Coquelin oversampling = 16; 10251bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 102656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 102748a6092fSMaxime Coquelin } 102848a6092fSMaxime Coquelin 102948a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 103048a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 1031ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 103248a6092fSMaxime Coquelin 103348a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 103448a6092fSMaxime Coquelin 103548a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 103648a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 103748a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 103848a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 10394f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 104048a6092fSMaxime Coquelin 104148a6092fSMaxime Coquelin /* Characters to ignore */ 104248a6092fSMaxime Coquelin port->ignore_status_mask = 0; 104348a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 104448a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 104548a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 10464f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 104748a6092fSMaxime Coquelin /* 104848a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 104948a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 105048a6092fSMaxime Coquelin */ 105148a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 105248a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 105348a6092fSMaxime Coquelin } 105448a6092fSMaxime Coquelin 105548a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 105648a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 105748a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 105848a6092fSMaxime Coquelin 105933bb2f6aSErwan Le Ray if (stm32_port->rx_ch) { 106033bb2f6aSErwan Le Ray /* 106133bb2f6aSErwan Le Ray * Setup DMA to collect only valid data and enable error irqs. 106233bb2f6aSErwan Le Ray * This also enables break reception when using DMA. 106333bb2f6aSErwan Le Ray */ 106433bb2f6aSErwan Le Ray cr1 |= USART_CR1_PEIE; 106533bb2f6aSErwan Le Ray cr3 |= USART_CR3_EIE; 106634891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 106733bb2f6aSErwan Le Ray cr3 |= USART_CR3_DDRE; 106833bb2f6aSErwan Le Ray } 106934891872SAlexandre TORGUE 10701bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 107156f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 10721bcda09dSBich HEMON rs485conf->delay_rts_before_send, 107356f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 107456f9a76cSErwan Le Ray baud); 10751bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 10761bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 10771bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 10781bcda09dSBich HEMON } else { 10791bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 10801bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 10811bcda09dSBich HEMON } 10821bcda09dSBich HEMON 10831bcda09dSBich HEMON } else { 10841bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 10851bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 10861bcda09dSBich HEMON } 10871bcda09dSBich HEMON 108812761869SErwan Le Ray /* Configure wake up from low power on start bit detection */ 10893d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 109012761869SErwan Le Ray cr3 &= ~USART_CR3_WUS_MASK; 109112761869SErwan Le Ray cr3 |= USART_CR3_WUS_START_BIT; 109212761869SErwan Le Ray } 109312761869SErwan Le Ray 1094ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 1095ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 1096ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 109748a6092fSMaxime Coquelin 109856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 109948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 1100436c9793SErwan Le Ray 1101436c9793SErwan Le Ray /* Handle modem control interrupts */ 1102436c9793SErwan Le Ray if (UART_ENABLE_MS(port, termios->c_cflag)) 1103436c9793SErwan Le Ray stm32_usart_enable_ms(port); 1104436c9793SErwan Le Ray else 1105436c9793SErwan Le Ray stm32_usart_disable_ms(port); 110648a6092fSMaxime Coquelin } 110748a6092fSMaxime Coquelin 110856f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port) 110948a6092fSMaxime Coquelin { 111048a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 111148a6092fSMaxime Coquelin } 111248a6092fSMaxime Coquelin 111356f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port) 111448a6092fSMaxime Coquelin { 111548a6092fSMaxime Coquelin } 111648a6092fSMaxime Coquelin 111756f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port) 111848a6092fSMaxime Coquelin { 111948a6092fSMaxime Coquelin return 0; 112048a6092fSMaxime Coquelin } 112148a6092fSMaxime Coquelin 112256f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags) 112348a6092fSMaxime Coquelin { 112448a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 112548a6092fSMaxime Coquelin port->type = PORT_STM32; 112648a6092fSMaxime Coquelin } 112748a6092fSMaxime Coquelin 112848a6092fSMaxime Coquelin static int 112956f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser) 113048a6092fSMaxime Coquelin { 113148a6092fSMaxime Coquelin /* No user changeable parameters */ 113248a6092fSMaxime Coquelin return -EINVAL; 113348a6092fSMaxime Coquelin } 113448a6092fSMaxime Coquelin 113556f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state, 113648a6092fSMaxime Coquelin unsigned int oldstate) 113748a6092fSMaxime Coquelin { 113848a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 113948a6092fSMaxime Coquelin struct stm32_port, port); 1140d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 1141d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32port->info->cfg; 114218ee37e1SJohan Hovold unsigned long flags; 114348a6092fSMaxime Coquelin 114448a6092fSMaxime Coquelin switch (state) { 114548a6092fSMaxime Coquelin case UART_PM_STATE_ON: 1146fb6dcef6SErwan Le Ray pm_runtime_get_sync(port->dev); 114748a6092fSMaxime Coquelin break; 114848a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 114948a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 115056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 115148a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 1152fb6dcef6SErwan Le Ray pm_runtime_put_sync(port->dev); 115348a6092fSMaxime Coquelin break; 115448a6092fSMaxime Coquelin } 115548a6092fSMaxime Coquelin } 115648a6092fSMaxime Coquelin 115748a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 115856f9a76cSErwan Le Ray .tx_empty = stm32_usart_tx_empty, 115956f9a76cSErwan Le Ray .set_mctrl = stm32_usart_set_mctrl, 116056f9a76cSErwan Le Ray .get_mctrl = stm32_usart_get_mctrl, 116156f9a76cSErwan Le Ray .stop_tx = stm32_usart_stop_tx, 116256f9a76cSErwan Le Ray .start_tx = stm32_usart_start_tx, 116356f9a76cSErwan Le Ray .throttle = stm32_usart_throttle, 116456f9a76cSErwan Le Ray .unthrottle = stm32_usart_unthrottle, 116556f9a76cSErwan Le Ray .stop_rx = stm32_usart_stop_rx, 116656f9a76cSErwan Le Ray .enable_ms = stm32_usart_enable_ms, 116756f9a76cSErwan Le Ray .break_ctl = stm32_usart_break_ctl, 116856f9a76cSErwan Le Ray .startup = stm32_usart_startup, 116956f9a76cSErwan Le Ray .shutdown = stm32_usart_shutdown, 11703d82be8bSErwan Le Ray .flush_buffer = stm32_usart_flush_buffer, 117156f9a76cSErwan Le Ray .set_termios = stm32_usart_set_termios, 117256f9a76cSErwan Le Ray .pm = stm32_usart_pm, 117356f9a76cSErwan Le Ray .type = stm32_usart_type, 117456f9a76cSErwan Le Ray .release_port = stm32_usart_release_port, 117556f9a76cSErwan Le Ray .request_port = stm32_usart_request_port, 117656f9a76cSErwan Le Ray .config_port = stm32_usart_config_port, 117756f9a76cSErwan Le Ray .verify_port = stm32_usart_verify_port, 117848a6092fSMaxime Coquelin }; 117948a6092fSMaxime Coquelin 11802aa1bbb2SFabrice Gasnier /* 11812aa1bbb2SFabrice Gasnier * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG) 11822aa1bbb2SFabrice Gasnier * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case, 11832aa1bbb2SFabrice Gasnier * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE. 11842aa1bbb2SFabrice Gasnier * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1. 11852aa1bbb2SFabrice Gasnier */ 11862aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 }; 11872aa1bbb2SFabrice Gasnier 11882aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p, 11892aa1bbb2SFabrice Gasnier int *ftcfg) 11902aa1bbb2SFabrice Gasnier { 11912aa1bbb2SFabrice Gasnier u32 bytes, i; 11922aa1bbb2SFabrice Gasnier 11932aa1bbb2SFabrice Gasnier /* DT option to get RX & TX FIFO threshold (default to 8 bytes) */ 11942aa1bbb2SFabrice Gasnier if (of_property_read_u32(pdev->dev.of_node, p, &bytes)) 11952aa1bbb2SFabrice Gasnier bytes = 8; 11962aa1bbb2SFabrice Gasnier 11972aa1bbb2SFabrice Gasnier for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++) 11982aa1bbb2SFabrice Gasnier if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes) 11992aa1bbb2SFabrice Gasnier break; 12002aa1bbb2SFabrice Gasnier if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg)) 12012aa1bbb2SFabrice Gasnier i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1; 12022aa1bbb2SFabrice Gasnier 12032aa1bbb2SFabrice Gasnier dev_dbg(&pdev->dev, "%s set to %d bytes\n", p, 12042aa1bbb2SFabrice Gasnier stm32h7_usart_fifo_thresh_cfg[i]); 12052aa1bbb2SFabrice Gasnier 12062aa1bbb2SFabrice Gasnier /* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */ 12072aa1bbb2SFabrice Gasnier if (i) 12082aa1bbb2SFabrice Gasnier *ftcfg = i - 1; 12092aa1bbb2SFabrice Gasnier else 12102aa1bbb2SFabrice Gasnier *ftcfg = -EINVAL; 12112aa1bbb2SFabrice Gasnier } 12122aa1bbb2SFabrice Gasnier 121397f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port) 121497f3a085SErwan Le Ray { 121597f3a085SErwan Le Ray clk_disable_unprepare(stm32port->clk); 121697f3a085SErwan Le Ray } 121797f3a085SErwan Le Ray 121856f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port, 121948a6092fSMaxime Coquelin struct platform_device *pdev) 122048a6092fSMaxime Coquelin { 122148a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 122248a6092fSMaxime Coquelin struct resource *res; 1223e0f2a902SErwan Le Ray int ret, irq; 122448a6092fSMaxime Coquelin 1225e0f2a902SErwan Le Ray irq = platform_get_irq(pdev, 0); 1226217b04c6STang Bin if (irq < 0) 1227217b04c6STang Bin return irq; 122892fc0023SErwan Le Ray 122948a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 123048a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 123148a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 123248a6092fSMaxime Coquelin port->dev = &pdev->dev; 1233d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 12349feedaa7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE); 1235e0f2a902SErwan Le Ray port->irq = irq; 123656f9a76cSErwan Le Ray port->rs485_config = stm32_usart_config_rs485; 12377d8f6861SBich HEMON 123856f9a76cSErwan Le Ray ret = stm32_usart_init_rs485(port, pdev); 1239c150c0f3SLukas Wunner if (ret) 1240c150c0f3SLukas Wunner return ret; 12417d8f6861SBich HEMON 12423d530017SAlexandre Torgue stm32port->wakeup_src = stm32port->info->cfg.has_wakeup && 12433d530017SAlexandre Torgue of_property_read_bool(pdev->dev.of_node, "wakeup-source"); 12442c58e560SErwan Le Ray 12453cd66593SMartin Devera stm32port->swap = stm32port->info->cfg.has_swap && 12463cd66593SMartin Devera of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"); 12473cd66593SMartin Devera 1248351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 12492aa1bbb2SFabrice Gasnier if (stm32port->fifoen) { 12502aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "rx-threshold", 12512aa1bbb2SFabrice Gasnier &stm32port->rxftcfg); 12522aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "tx-threshold", 12532aa1bbb2SFabrice Gasnier &stm32port->txftcfg); 12542aa1bbb2SFabrice Gasnier } 125548a6092fSMaxime Coquelin 12563d881e32STang Bin port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 125748a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 125848a6092fSMaxime Coquelin return PTR_ERR(port->membase); 125948a6092fSMaxime Coquelin port->mapbase = res->start; 126048a6092fSMaxime Coquelin 126148a6092fSMaxime Coquelin spin_lock_init(&port->lock); 126248a6092fSMaxime Coquelin 126348a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 126448a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 126548a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 126648a6092fSMaxime Coquelin 126748a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 126848a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 126948a6092fSMaxime Coquelin if (ret) 127048a6092fSMaxime Coquelin return ret; 127148a6092fSMaxime Coquelin 127248a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 1273ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 127448a6092fSMaxime Coquelin ret = -EINVAL; 12756cf61b9bSManivannan Sadhasivam goto err_clk; 1276ada80043SFabrice Gasnier } 127748a6092fSMaxime Coquelin 12786cf61b9bSManivannan Sadhasivam stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0); 12796cf61b9bSManivannan Sadhasivam if (IS_ERR(stm32port->gpios)) { 12806cf61b9bSManivannan Sadhasivam ret = PTR_ERR(stm32port->gpios); 12816cf61b9bSManivannan Sadhasivam goto err_clk; 12826cf61b9bSManivannan Sadhasivam } 12836cf61b9bSManivannan Sadhasivam 12849359369aSErwan Le Ray /* 12859359369aSErwan Le Ray * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts" 12869359369aSErwan Le Ray * properties should not be specified. 12879359369aSErwan Le Ray */ 12886cf61b9bSManivannan Sadhasivam if (stm32port->hw_flow_control) { 12896cf61b9bSManivannan Sadhasivam if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) || 12906cf61b9bSManivannan Sadhasivam mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) { 12916cf61b9bSManivannan Sadhasivam dev_err(&pdev->dev, "Conflicting RTS/CTS config\n"); 12926cf61b9bSManivannan Sadhasivam ret = -EINVAL; 12936cf61b9bSManivannan Sadhasivam goto err_clk; 12946cf61b9bSManivannan Sadhasivam } 12956cf61b9bSManivannan Sadhasivam } 12966cf61b9bSManivannan Sadhasivam 12976cf61b9bSManivannan Sadhasivam return ret; 12986cf61b9bSManivannan Sadhasivam 12996cf61b9bSManivannan Sadhasivam err_clk: 13006cf61b9bSManivannan Sadhasivam clk_disable_unprepare(stm32port->clk); 13016cf61b9bSManivannan Sadhasivam 130248a6092fSMaxime Coquelin return ret; 130348a6092fSMaxime Coquelin } 130448a6092fSMaxime Coquelin 130556f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev) 130648a6092fSMaxime Coquelin { 130748a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 130848a6092fSMaxime Coquelin int id; 130948a6092fSMaxime Coquelin 131048a6092fSMaxime Coquelin if (!np) 131148a6092fSMaxime Coquelin return NULL; 131248a6092fSMaxime Coquelin 131348a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 1314e5707915SGerald Baeza if (id < 0) { 1315e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 1316e5707915SGerald Baeza return NULL; 1317e5707915SGerald Baeza } 131848a6092fSMaxime Coquelin 131948a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 132048a6092fSMaxime Coquelin return NULL; 132148a6092fSMaxime Coquelin 13226fd9fffbSErwan Le Ray stm32_ports[id].hw_flow_control = 13236fd9fffbSErwan Le Ray of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ || 13246fd9fffbSErwan Le Ray of_property_read_bool (np, "uart-has-rtscts"); 132548a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 13264cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 1327d0a6a7bcSErwan Le Ray stm32_ports[id].cr3_irq = 0; 1328e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 132948a6092fSMaxime Coquelin return &stm32_ports[id]; 133048a6092fSMaxime Coquelin } 133148a6092fSMaxime Coquelin 133248a6092fSMaxime Coquelin #ifdef CONFIG_OF 133348a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 1334ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 1335ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 1336270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 133748a6092fSMaxime Coquelin {}, 133848a6092fSMaxime Coquelin }; 133948a6092fSMaxime Coquelin 134048a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 134148a6092fSMaxime Coquelin #endif 134248a6092fSMaxime Coquelin 1343a7770a4bSErwan Le Ray static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port, 1344a7770a4bSErwan Le Ray struct platform_device *pdev) 1345a7770a4bSErwan Le Ray { 1346a7770a4bSErwan Le Ray if (stm32port->rx_buf) 1347a7770a4bSErwan Le Ray dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf, 1348a7770a4bSErwan Le Ray stm32port->rx_dma_buf); 1349a7770a4bSErwan Le Ray } 1350a7770a4bSErwan Le Ray 135156f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port, 135234891872SAlexandre TORGUE struct platform_device *pdev) 135334891872SAlexandre TORGUE { 1354d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 135534891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 135634891872SAlexandre TORGUE struct device *dev = &pdev->dev; 135734891872SAlexandre TORGUE struct dma_slave_config config; 135834891872SAlexandre TORGUE int ret; 135934891872SAlexandre TORGUE 1360e359b441SJohan Hovold /* 1361e359b441SJohan Hovold * Using DMA and threaded handler for the console could lead to 1362e359b441SJohan Hovold * deadlocks. 1363e359b441SJohan Hovold */ 1364e359b441SJohan Hovold if (uart_console(port)) 1365e359b441SJohan Hovold return -ENODEV; 1366e359b441SJohan Hovold 136759bd4eedSTang Bin stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L, 136834891872SAlexandre TORGUE &stm32port->rx_dma_buf, 136934891872SAlexandre TORGUE GFP_KERNEL); 1370a7770a4bSErwan Le Ray if (!stm32port->rx_buf) 1371a7770a4bSErwan Le Ray return -ENOMEM; 137234891872SAlexandre TORGUE 137334891872SAlexandre TORGUE /* Configure DMA channel */ 137434891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 13758e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 137634891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 137734891872SAlexandre TORGUE 137834891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 137934891872SAlexandre TORGUE if (ret < 0) { 138034891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 1381a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32port, pdev); 1382a7770a4bSErwan Le Ray return ret; 138334891872SAlexandre TORGUE } 138434891872SAlexandre TORGUE 138534891872SAlexandre TORGUE return 0; 1386a7770a4bSErwan Le Ray } 138734891872SAlexandre TORGUE 1388a7770a4bSErwan Le Ray static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port, 1389a7770a4bSErwan Le Ray struct platform_device *pdev) 1390a7770a4bSErwan Le Ray { 1391a7770a4bSErwan Le Ray if (stm32port->tx_buf) 1392a7770a4bSErwan Le Ray dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf, 1393a7770a4bSErwan Le Ray stm32port->tx_dma_buf); 139434891872SAlexandre TORGUE } 139534891872SAlexandre TORGUE 139656f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port, 139734891872SAlexandre TORGUE struct platform_device *pdev) 139834891872SAlexandre TORGUE { 1399d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 140034891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 140134891872SAlexandre TORGUE struct device *dev = &pdev->dev; 140234891872SAlexandre TORGUE struct dma_slave_config config; 140334891872SAlexandre TORGUE int ret; 140434891872SAlexandre TORGUE 140534891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 140634891872SAlexandre TORGUE 140759bd4eedSTang Bin stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L, 140834891872SAlexandre TORGUE &stm32port->tx_dma_buf, 140934891872SAlexandre TORGUE GFP_KERNEL); 1410a7770a4bSErwan Le Ray if (!stm32port->tx_buf) 1411a7770a4bSErwan Le Ray return -ENOMEM; 141234891872SAlexandre TORGUE 141334891872SAlexandre TORGUE /* Configure DMA channel */ 141434891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 14158e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 141634891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 141734891872SAlexandre TORGUE 141834891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 141934891872SAlexandre TORGUE if (ret < 0) { 142034891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 1421a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32port, pdev); 1422a7770a4bSErwan Le Ray return ret; 142334891872SAlexandre TORGUE } 142434891872SAlexandre TORGUE 142534891872SAlexandre TORGUE return 0; 142634891872SAlexandre TORGUE } 142734891872SAlexandre TORGUE 142856f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev) 142948a6092fSMaxime Coquelin { 143048a6092fSMaxime Coquelin struct stm32_port *stm32port; 1431ada8618fSAlexandre TORGUE int ret; 143248a6092fSMaxime Coquelin 143356f9a76cSErwan Le Ray stm32port = stm32_usart_of_get_port(pdev); 143448a6092fSMaxime Coquelin if (!stm32port) 143548a6092fSMaxime Coquelin return -ENODEV; 143648a6092fSMaxime Coquelin 1437d825f0beSStephen Boyd stm32port->info = of_device_get_match_data(&pdev->dev); 1438d825f0beSStephen Boyd if (!stm32port->info) 1439ada8618fSAlexandre TORGUE return -EINVAL; 1440ada8618fSAlexandre TORGUE 144156f9a76cSErwan Le Ray ret = stm32_usart_init_port(stm32port, pdev); 144248a6092fSMaxime Coquelin if (ret) 144348a6092fSMaxime Coquelin return ret; 144448a6092fSMaxime Coquelin 14453d530017SAlexandre Torgue if (stm32port->wakeup_src) { 14463d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, true); 14473d530017SAlexandre Torgue ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq); 14485297f274SErwan Le Ray if (ret) 1449a7770a4bSErwan Le Ray goto err_deinit_port; 1450270e5a74SFabrice Gasnier } 1451270e5a74SFabrice Gasnier 1452a7770a4bSErwan Le Ray stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx"); 1453a7770a4bSErwan Le Ray if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER) { 1454a7770a4bSErwan Le Ray ret = -EPROBE_DEFER; 1455a7770a4bSErwan Le Ray goto err_wakeirq; 1456a7770a4bSErwan Le Ray } 1457a7770a4bSErwan Le Ray /* Fall back in interrupt mode for any non-deferral error */ 1458a7770a4bSErwan Le Ray if (IS_ERR(stm32port->rx_ch)) 1459a7770a4bSErwan Le Ray stm32port->rx_ch = NULL; 146034891872SAlexandre TORGUE 1461a7770a4bSErwan Le Ray stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx"); 1462a7770a4bSErwan Le Ray if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) { 1463a7770a4bSErwan Le Ray ret = -EPROBE_DEFER; 1464a7770a4bSErwan Le Ray goto err_dma_rx; 1465a7770a4bSErwan Le Ray } 1466a7770a4bSErwan Le Ray /* Fall back in interrupt mode for any non-deferral error */ 1467a7770a4bSErwan Le Ray if (IS_ERR(stm32port->tx_ch)) 1468a7770a4bSErwan Le Ray stm32port->tx_ch = NULL; 1469a7770a4bSErwan Le Ray 1470a7770a4bSErwan Le Ray if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) { 1471a7770a4bSErwan Le Ray /* Fall back in interrupt mode */ 1472a7770a4bSErwan Le Ray dma_release_channel(stm32port->rx_ch); 1473a7770a4bSErwan Le Ray stm32port->rx_ch = NULL; 1474a7770a4bSErwan Le Ray } 1475a7770a4bSErwan Le Ray 1476a7770a4bSErwan Le Ray if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) { 1477a7770a4bSErwan Le Ray /* Fall back in interrupt mode */ 1478a7770a4bSErwan Le Ray dma_release_channel(stm32port->tx_ch); 1479a7770a4bSErwan Le Ray stm32port->tx_ch = NULL; 1480a7770a4bSErwan Le Ray } 1481a7770a4bSErwan Le Ray 1482a7770a4bSErwan Le Ray if (!stm32port->rx_ch) 1483a7770a4bSErwan Le Ray dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n"); 1484a7770a4bSErwan Le Ray if (!stm32port->tx_ch) 1485a7770a4bSErwan Le Ray dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n"); 148634891872SAlexandre TORGUE 148748a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 148848a6092fSMaxime Coquelin 1489fb6dcef6SErwan Le Ray pm_runtime_get_noresume(&pdev->dev); 1490fb6dcef6SErwan Le Ray pm_runtime_set_active(&pdev->dev); 1491fb6dcef6SErwan Le Ray pm_runtime_enable(&pdev->dev); 149287fd0741SErwan Le Ray 149387fd0741SErwan Le Ray ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 149487fd0741SErwan Le Ray if (ret) 149587fd0741SErwan Le Ray goto err_port; 149687fd0741SErwan Le Ray 1497fb6dcef6SErwan Le Ray pm_runtime_put_sync(&pdev->dev); 1498fb6dcef6SErwan Le Ray 149948a6092fSMaxime Coquelin return 0; 1500ada80043SFabrice Gasnier 150187fd0741SErwan Le Ray err_port: 150287fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 150387fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 150487fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 150587fd0741SErwan Le Ray 150687fd0741SErwan Le Ray if (stm32port->tx_ch) { 1507a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32port, pdev); 150887fd0741SErwan Le Ray dma_release_channel(stm32port->tx_ch); 150987fd0741SErwan Le Ray } 151087fd0741SErwan Le Ray 1511a7770a4bSErwan Le Ray if (stm32port->rx_ch) 1512a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32port, pdev); 151387fd0741SErwan Le Ray 1514a7770a4bSErwan Le Ray err_dma_rx: 1515a7770a4bSErwan Le Ray if (stm32port->rx_ch) 1516a7770a4bSErwan Le Ray dma_release_channel(stm32port->rx_ch); 1517a7770a4bSErwan Le Ray 1518a7770a4bSErwan Le Ray err_wakeirq: 15193d530017SAlexandre Torgue if (stm32port->wakeup_src) 15205297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 15215297f274SErwan Le Ray 1522a7770a4bSErwan Le Ray err_deinit_port: 15233d530017SAlexandre Torgue if (stm32port->wakeup_src) 15243d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, false); 1525270e5a74SFabrice Gasnier 152697f3a085SErwan Le Ray stm32_usart_deinit_port(stm32port); 1527ada80043SFabrice Gasnier 1528ada80043SFabrice Gasnier return ret; 152948a6092fSMaxime Coquelin } 153048a6092fSMaxime Coquelin 153156f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev) 153248a6092fSMaxime Coquelin { 153348a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1534511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1535d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1536fb6dcef6SErwan Le Ray int err; 153733bb2f6aSErwan Le Ray u32 cr3; 1538fb6dcef6SErwan Le Ray 1539fb6dcef6SErwan Le Ray pm_runtime_get_sync(&pdev->dev); 154087fd0741SErwan Le Ray err = uart_remove_one_port(&stm32_usart_driver, port); 154187fd0741SErwan Le Ray if (err) 154287fd0741SErwan Le Ray return(err); 154387fd0741SErwan Le Ray 154487fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 154587fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 154687fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 154734891872SAlexandre TORGUE 154833bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE); 154933bb2f6aSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 155033bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_EIE; 155133bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_DMAR; 155233bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_DDRE; 155333bb2f6aSErwan Le Ray writel_relaxed(cr3, port->membase + ofs->cr3); 155434891872SAlexandre TORGUE 155587fd0741SErwan Le Ray if (stm32_port->tx_ch) { 155687fd0741SErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 1557a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32_port, pdev); 155834891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 155987fd0741SErwan Le Ray } 156034891872SAlexandre TORGUE 1561a7770a4bSErwan Le Ray if (stm32_port->rx_ch) { 1562a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32_port, pdev); 1563a7770a4bSErwan Le Ray dma_release_channel(stm32_port->rx_ch); 1564a7770a4bSErwan Le Ray } 1565a7770a4bSErwan Le Ray 1566a7770a4bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 1567511c7b1bSAlexandre TORGUE 15683d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 15695297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1570270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 15715297f274SErwan Le Ray } 1572270e5a74SFabrice Gasnier 157397f3a085SErwan Le Ray stm32_usart_deinit_port(stm32_port); 157448a6092fSMaxime Coquelin 157587fd0741SErwan Le Ray return 0; 157648a6092fSMaxime Coquelin } 157748a6092fSMaxime Coquelin 157848a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 157956f9a76cSErwan Le Ray static void stm32_usart_console_putchar(struct uart_port *port, int ch) 158048a6092fSMaxime Coquelin { 1581ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1582d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1583ada8618fSAlexandre TORGUE 1584ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 158548a6092fSMaxime Coquelin cpu_relax(); 158648a6092fSMaxime Coquelin 1587ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 158848a6092fSMaxime Coquelin } 158948a6092fSMaxime Coquelin 159056f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s, 159192fc0023SErwan Le Ray unsigned int cnt) 159248a6092fSMaxime Coquelin { 159348a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1594ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1595d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1596d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 159748a6092fSMaxime Coquelin unsigned long flags; 159848a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 159948a6092fSMaxime Coquelin int locked = 1; 160048a6092fSMaxime Coquelin 1601cea37afdSJohan Hovold if (oops_in_progress) 1602cea37afdSJohan Hovold locked = spin_trylock_irqsave(&port->lock, flags); 160348a6092fSMaxime Coquelin else 1604cea37afdSJohan Hovold spin_lock_irqsave(&port->lock, flags); 160548a6092fSMaxime Coquelin 160687f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1607ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 160848a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 160987f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1610ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 161148a6092fSMaxime Coquelin 161256f9a76cSErwan Le Ray uart_console_write(port, s, cnt, stm32_usart_console_putchar); 161348a6092fSMaxime Coquelin 161448a6092fSMaxime Coquelin /* Restore interrupt state */ 1615ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 161648a6092fSMaxime Coquelin 161748a6092fSMaxime Coquelin if (locked) 1618cea37afdSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 161948a6092fSMaxime Coquelin } 162048a6092fSMaxime Coquelin 162156f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options) 162248a6092fSMaxime Coquelin { 162348a6092fSMaxime Coquelin struct stm32_port *stm32port; 162448a6092fSMaxime Coquelin int baud = 9600; 162548a6092fSMaxime Coquelin int bits = 8; 162648a6092fSMaxime Coquelin int parity = 'n'; 162748a6092fSMaxime Coquelin int flow = 'n'; 162848a6092fSMaxime Coquelin 162948a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 163048a6092fSMaxime Coquelin return -ENODEV; 163148a6092fSMaxime Coquelin 163248a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 163348a6092fSMaxime Coquelin 163448a6092fSMaxime Coquelin /* 163548a6092fSMaxime Coquelin * This driver does not support early console initialization 163648a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 163748a6092fSMaxime Coquelin * this to be called during the uart port registration when the 163848a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 163948a6092fSMaxime Coquelin */ 164092fc0023SErwan Le Ray if (stm32port->port.mapbase == 0 || !stm32port->port.membase) 164148a6092fSMaxime Coquelin return -ENXIO; 164248a6092fSMaxime Coquelin 164348a6092fSMaxime Coquelin if (options) 164448a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 164548a6092fSMaxime Coquelin 164648a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 164748a6092fSMaxime Coquelin } 164848a6092fSMaxime Coquelin 164948a6092fSMaxime Coquelin static struct console stm32_console = { 165048a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 165148a6092fSMaxime Coquelin .device = uart_console_device, 165256f9a76cSErwan Le Ray .write = stm32_usart_console_write, 165356f9a76cSErwan Le Ray .setup = stm32_usart_console_setup, 165448a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 165548a6092fSMaxime Coquelin .index = -1, 165648a6092fSMaxime Coquelin .data = &stm32_usart_driver, 165748a6092fSMaxime Coquelin }; 165848a6092fSMaxime Coquelin 165948a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 166048a6092fSMaxime Coquelin 166148a6092fSMaxime Coquelin #else 166248a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 166348a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 166448a6092fSMaxime Coquelin 166548a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 166648a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 166748a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 166848a6092fSMaxime Coquelin .major = 0, 166948a6092fSMaxime Coquelin .minor = 0, 167048a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 167148a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 167248a6092fSMaxime Coquelin }; 167348a6092fSMaxime Coquelin 1674*6eeb348cSErwan Le Ray static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port, 1675fe94347dSErwan Le Ray bool enable) 1676270e5a74SFabrice Gasnier { 1677270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1678d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1679*6eeb348cSErwan Le Ray struct tty_port *tport = &port->state->port; 1680*6eeb348cSErwan Le Ray int ret; 1681270e5a74SFabrice Gasnier 1682*6eeb348cSErwan Le Ray if (!stm32_port->wakeup_src || !tty_port_initialized(tport)) 1683*6eeb348cSErwan Le Ray return 0; 1684270e5a74SFabrice Gasnier 168512761869SErwan Le Ray /* 168612761869SErwan Le Ray * Enable low-power wake-up and wake-up irq if argument is set to 168712761869SErwan Le Ray * "enable", disable low-power wake-up and wake-up irq otherwise 168812761869SErwan Le Ray */ 1689270e5a74SFabrice Gasnier if (enable) { 169056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM); 169112761869SErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE); 1692*6eeb348cSErwan Le Ray 1693*6eeb348cSErwan Le Ray /* 1694*6eeb348cSErwan Le Ray * When DMA is used for reception, it must be disabled before 1695*6eeb348cSErwan Le Ray * entering low-power mode and re-enabled when exiting from 1696*6eeb348cSErwan Le Ray * low-power mode. 1697*6eeb348cSErwan Le Ray */ 1698*6eeb348cSErwan Le Ray if (stm32_port->rx_ch) { 1699*6eeb348cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 1700*6eeb348cSErwan Le Ray dmaengine_terminate_sync(stm32_port->rx_ch); 1701*6eeb348cSErwan Le Ray } 1702*6eeb348cSErwan Le Ray 1703*6eeb348cSErwan Le Ray /* Poll data from RX FIFO if any */ 1704*6eeb348cSErwan Le Ray stm32_usart_receive_chars(port, false); 1705270e5a74SFabrice Gasnier } else { 1706*6eeb348cSErwan Le Ray if (stm32_port->rx_ch) { 1707*6eeb348cSErwan Le Ray ret = stm32_usart_start_rx_dma_cyclic(port); 1708*6eeb348cSErwan Le Ray if (ret) 1709*6eeb348cSErwan Le Ray return ret; 1710*6eeb348cSErwan Le Ray } 1711*6eeb348cSErwan Le Ray 171256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM); 171312761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 1714270e5a74SFabrice Gasnier } 1715*6eeb348cSErwan Le Ray 1716*6eeb348cSErwan Le Ray return 0; 1717270e5a74SFabrice Gasnier } 1718270e5a74SFabrice Gasnier 171956f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev) 1720270e5a74SFabrice Gasnier { 1721270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1722*6eeb348cSErwan Le Ray int ret; 1723270e5a74SFabrice Gasnier 1724270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1725270e5a74SFabrice Gasnier 1726*6eeb348cSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) { 1727*6eeb348cSErwan Le Ray ret = stm32_usart_serial_en_wakeup(port, true); 1728*6eeb348cSErwan Le Ray if (ret) 1729*6eeb348cSErwan Le Ray return ret; 1730*6eeb348cSErwan Le Ray } 1731270e5a74SFabrice Gasnier 173255484fccSErwan Le Ray /* 173355484fccSErwan Le Ray * When "no_console_suspend" is enabled, keep the pinctrl default state 173455484fccSErwan Le Ray * and rely on bootloader stage to restore this state upon resume. 173555484fccSErwan Le Ray * Otherwise, apply the idle or sleep states depending on wakeup 173655484fccSErwan Le Ray * capabilities. 173755484fccSErwan Le Ray */ 173855484fccSErwan Le Ray if (console_suspend_enabled || !uart_console(port)) { 17391631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 174055484fccSErwan Le Ray pinctrl_pm_select_idle_state(dev); 174155484fccSErwan Le Ray else 174294616d9aSErwan Le Ray pinctrl_pm_select_sleep_state(dev); 174355484fccSErwan Le Ray } 174494616d9aSErwan Le Ray 1745270e5a74SFabrice Gasnier return 0; 1746270e5a74SFabrice Gasnier } 1747270e5a74SFabrice Gasnier 174856f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev) 1749270e5a74SFabrice Gasnier { 1750270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1751*6eeb348cSErwan Le Ray int ret; 1752270e5a74SFabrice Gasnier 175394616d9aSErwan Le Ray pinctrl_pm_select_default_state(dev); 175494616d9aSErwan Le Ray 1755*6eeb348cSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) { 1756*6eeb348cSErwan Le Ray ret = stm32_usart_serial_en_wakeup(port, false); 1757*6eeb348cSErwan Le Ray if (ret) 1758*6eeb348cSErwan Le Ray return ret; 1759*6eeb348cSErwan Le Ray } 1760270e5a74SFabrice Gasnier 1761270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1762270e5a74SFabrice Gasnier } 1763270e5a74SFabrice Gasnier 176456f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev) 1765fb6dcef6SErwan Le Ray { 1766fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1767fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1768fb6dcef6SErwan Le Ray struct stm32_port, port); 1769fb6dcef6SErwan Le Ray 1770fb6dcef6SErwan Le Ray clk_disable_unprepare(stm32port->clk); 1771fb6dcef6SErwan Le Ray 1772fb6dcef6SErwan Le Ray return 0; 1773fb6dcef6SErwan Le Ray } 1774fb6dcef6SErwan Le Ray 177556f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev) 1776fb6dcef6SErwan Le Ray { 1777fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1778fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1779fb6dcef6SErwan Le Ray struct stm32_port, port); 1780fb6dcef6SErwan Le Ray 1781fb6dcef6SErwan Le Ray return clk_prepare_enable(stm32port->clk); 1782fb6dcef6SErwan Le Ray } 1783fb6dcef6SErwan Le Ray 1784270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 178556f9a76cSErwan Le Ray SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend, 178656f9a76cSErwan Le Ray stm32_usart_runtime_resume, NULL) 178756f9a76cSErwan Le Ray SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend, 178856f9a76cSErwan Le Ray stm32_usart_serial_resume) 1789270e5a74SFabrice Gasnier }; 1790270e5a74SFabrice Gasnier 179148a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 179256f9a76cSErwan Le Ray .probe = stm32_usart_serial_probe, 179356f9a76cSErwan Le Ray .remove = stm32_usart_serial_remove, 179448a6092fSMaxime Coquelin .driver = { 179548a6092fSMaxime Coquelin .name = DRIVER_NAME, 1796270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 179748a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 179848a6092fSMaxime Coquelin }, 179948a6092fSMaxime Coquelin }; 180048a6092fSMaxime Coquelin 180156f9a76cSErwan Le Ray static int __init stm32_usart_init(void) 180248a6092fSMaxime Coquelin { 180348a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 180448a6092fSMaxime Coquelin int ret; 180548a6092fSMaxime Coquelin 180648a6092fSMaxime Coquelin pr_info("%s\n", banner); 180748a6092fSMaxime Coquelin 180848a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 180948a6092fSMaxime Coquelin if (ret) 181048a6092fSMaxime Coquelin return ret; 181148a6092fSMaxime Coquelin 181248a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 181348a6092fSMaxime Coquelin if (ret) 181448a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 181548a6092fSMaxime Coquelin 181648a6092fSMaxime Coquelin return ret; 181748a6092fSMaxime Coquelin } 181848a6092fSMaxime Coquelin 181956f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void) 182048a6092fSMaxime Coquelin { 182148a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 182248a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 182348a6092fSMaxime Coquelin } 182448a6092fSMaxime Coquelin 182556f9a76cSErwan Le Ray module_init(stm32_usart_init); 182656f9a76cSErwan Le Ray module_exit(stm32_usart_exit); 182748a6092fSMaxime Coquelin 182848a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 182948a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 183048a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1831