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 213*6333a485SErwan Le Ray static unsigned int 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; 218*6333a485SErwan Le Ray unsigned int size = 0; 21948a6092fSMaxime Coquelin u32 sr; 22048a6092fSMaxime Coquelin char flag; 22148a6092fSMaxime Coquelin 22233bb2f6aSErwan Le Ray while (stm32_usart_pending_rx_pio(port, &sr)) { 22348a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22448a6092fSMaxime Coquelin flag = TTY_NORMAL; 22548a6092fSMaxime Coquelin 2264f01d833SErwan Le Ray /* 2274f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2284f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2294f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2304f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2314f01d833SErwan Le Ray * and clear status bits of the next rx data. 2324f01d833SErwan Le Ray * 2334f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2344f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 2354f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 2364f01d833SErwan Le Ray */ 2374f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 2381250ed71SFabrice Gasnier writel_relaxed(sr & USART_SR_ERR_MASK, 2391250ed71SFabrice Gasnier port->membase + ofs->icr); 2404f01d833SErwan Le Ray 24133bb2f6aSErwan Le Ray c = stm32_usart_get_char_pio(port); 2424f01d833SErwan Le Ray port->icount.rx++; 243*6333a485SErwan Le Ray size++; 24448a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 2454f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 24648a6092fSMaxime Coquelin port->icount.overrun++; 24748a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 24848a6092fSMaxime Coquelin port->icount.parity++; 24948a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 2504f01d833SErwan Le Ray /* Break detection if character is null */ 2514f01d833SErwan Le Ray if (!c) { 2524f01d833SErwan Le Ray port->icount.brk++; 2534f01d833SErwan Le Ray if (uart_handle_break(port)) 2544f01d833SErwan Le Ray continue; 2554f01d833SErwan Le Ray } else { 25648a6092fSMaxime Coquelin port->icount.frame++; 25748a6092fSMaxime Coquelin } 2584f01d833SErwan Le Ray } 25948a6092fSMaxime Coquelin 26048a6092fSMaxime Coquelin sr &= port->read_status_mask; 26148a6092fSMaxime Coquelin 2624f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26348a6092fSMaxime Coquelin flag = TTY_PARITY; 2644f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 2654f01d833SErwan Le Ray if (!c) 2664f01d833SErwan Le Ray flag = TTY_BREAK; 2674f01d833SErwan Le Ray else 26848a6092fSMaxime Coquelin flag = TTY_FRAME; 26948a6092fSMaxime Coquelin } 2704f01d833SErwan Le Ray } 27148a6092fSMaxime Coquelin 272cea37afdSJohan Hovold if (uart_prepare_sysrq_char(port, c)) 27348a6092fSMaxime Coquelin continue; 27448a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27548a6092fSMaxime Coquelin } 276*6333a485SErwan Le Ray 277*6333a485SErwan Le Ray return size; 27833bb2f6aSErwan Le Ray } 27933bb2f6aSErwan Le Ray 28033bb2f6aSErwan Le Ray static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size) 28133bb2f6aSErwan Le Ray { 28233bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 28333bb2f6aSErwan Le Ray struct tty_port *ttyport = &stm32_port->port.state->port; 28433bb2f6aSErwan Le Ray unsigned char *dma_start; 28533bb2f6aSErwan Le Ray int dma_count, i; 28633bb2f6aSErwan Le Ray 28733bb2f6aSErwan Le Ray dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res); 28833bb2f6aSErwan Le Ray 28933bb2f6aSErwan Le Ray /* 29033bb2f6aSErwan Le Ray * Apply rdr_mask on buffer in order to mask parity bit. 29133bb2f6aSErwan Le Ray * This loop is useless in cs8 mode because DMA copies only 29233bb2f6aSErwan Le Ray * 8 bits and already ignores parity bit. 29333bb2f6aSErwan Le Ray */ 29433bb2f6aSErwan Le Ray if (!(stm32_port->rdr_mask == (BIT(8) - 1))) 29533bb2f6aSErwan Le Ray for (i = 0; i < dma_size; i++) 29633bb2f6aSErwan Le Ray *(dma_start + i) &= stm32_port->rdr_mask; 29733bb2f6aSErwan Le Ray 29833bb2f6aSErwan Le Ray dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size); 29933bb2f6aSErwan Le Ray port->icount.rx += dma_count; 30033bb2f6aSErwan Le Ray if (dma_count != dma_size) 30133bb2f6aSErwan Le Ray port->icount.buf_overrun++; 30233bb2f6aSErwan Le Ray stm32_port->last_res -= dma_count; 30333bb2f6aSErwan Le Ray if (stm32_port->last_res == 0) 30433bb2f6aSErwan Le Ray stm32_port->last_res = RX_BUF_L; 30533bb2f6aSErwan Le Ray } 30633bb2f6aSErwan Le Ray 307*6333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port) 30833bb2f6aSErwan Le Ray { 30933bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 310*6333a485SErwan Le Ray unsigned int dma_size, size = 0; 31133bb2f6aSErwan Le Ray 31233bb2f6aSErwan Le Ray /* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */ 31333bb2f6aSErwan Le Ray if (stm32_port->rx_dma_state.residue > stm32_port->last_res) { 31433bb2f6aSErwan Le Ray /* Conditional first part: from last_res to end of DMA buffer */ 31533bb2f6aSErwan Le Ray dma_size = stm32_port->last_res; 31633bb2f6aSErwan Le Ray stm32_usart_push_buffer_dma(port, dma_size); 317*6333a485SErwan Le Ray size = dma_size; 31833bb2f6aSErwan Le Ray } 31933bb2f6aSErwan Le Ray 32033bb2f6aSErwan Le Ray dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue; 32133bb2f6aSErwan Le Ray stm32_usart_push_buffer_dma(port, dma_size); 322*6333a485SErwan Le Ray size += dma_size; 323*6333a485SErwan Le Ray 324*6333a485SErwan Le Ray return size; 32533bb2f6aSErwan Le Ray } 32633bb2f6aSErwan Le Ray 327*6333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush) 32833bb2f6aSErwan Le Ray { 32933bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 33033bb2f6aSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 33133bb2f6aSErwan Le Ray enum dma_status rx_dma_status; 33233bb2f6aSErwan Le Ray u32 sr; 333*6333a485SErwan Le Ray unsigned int size = 0; 33433bb2f6aSErwan Le Ray 335*6333a485SErwan Le Ray if (stm32_usart_rx_dma_enabled(port) || force_dma_flush) { 33633bb2f6aSErwan Le Ray rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch, 33733bb2f6aSErwan Le Ray stm32_port->rx_ch->cookie, 33833bb2f6aSErwan Le Ray &stm32_port->rx_dma_state); 33933bb2f6aSErwan Le Ray if (rx_dma_status == DMA_IN_PROGRESS) { 34033bb2f6aSErwan Le Ray /* Empty DMA buffer */ 341*6333a485SErwan Le Ray size = stm32_usart_receive_chars_dma(port); 34233bb2f6aSErwan Le Ray sr = readl_relaxed(port->membase + ofs->isr); 34333bb2f6aSErwan Le Ray if (sr & USART_SR_ERR_MASK) { 34433bb2f6aSErwan Le Ray /* Disable DMA request line */ 34533bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 34633bb2f6aSErwan Le Ray 34733bb2f6aSErwan Le Ray /* Switch to PIO mode to handle the errors */ 348*6333a485SErwan Le Ray size += stm32_usart_receive_chars_pio(port); 34933bb2f6aSErwan Le Ray 35033bb2f6aSErwan Le Ray /* Switch back to DMA mode */ 35133bb2f6aSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 35233bb2f6aSErwan Le Ray } 35333bb2f6aSErwan Le Ray } else { 35433bb2f6aSErwan Le Ray /* Disable RX DMA */ 35533bb2f6aSErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 35633bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 35733bb2f6aSErwan Le Ray /* Fall back to interrupt mode */ 35833bb2f6aSErwan Le Ray dev_dbg(port->dev, "DMA error, fallback to irq mode\n"); 359*6333a485SErwan Le Ray size = stm32_usart_receive_chars_pio(port); 36033bb2f6aSErwan Le Ray } 36133bb2f6aSErwan Le Ray } else { 362*6333a485SErwan Le Ray size = stm32_usart_receive_chars_pio(port); 36333bb2f6aSErwan Le Ray } 36448a6092fSMaxime Coquelin 365*6333a485SErwan Le Ray return size; 36648a6092fSMaxime Coquelin } 36748a6092fSMaxime Coquelin 36856f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg) 36934891872SAlexandre TORGUE { 37034891872SAlexandre TORGUE struct uart_port *port = arg; 37134891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 372d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 373f16b90c2SErwan Le Ray unsigned long flags; 37434891872SAlexandre TORGUE 375fb4f2e04SErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 37656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 37734891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 37834891872SAlexandre TORGUE 37934891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 380f16b90c2SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 38156f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 382f16b90c2SErwan Le Ray spin_unlock_irqrestore(&port->lock, flags); 38334891872SAlexandre TORGUE } 38434891872SAlexandre TORGUE 38556f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port) 386d075719eSErwan Le Ray { 387d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 388d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 389d075719eSErwan Le Ray 390d075719eSErwan Le Ray /* 391d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 392d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 393d075719eSErwan Le Ray */ 3942aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 39556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 396d075719eSErwan Le Ray else 39756f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 398d075719eSErwan Le Ray } 399d075719eSErwan Le Ray 40033bb2f6aSErwan Le Ray static void stm32_usart_rx_dma_complete(void *arg) 40133bb2f6aSErwan Le Ray { 40233bb2f6aSErwan Le Ray struct uart_port *port = arg; 403*6333a485SErwan Le Ray struct tty_port *tport = &port->state->port; 404*6333a485SErwan Le Ray unsigned int size; 405*6333a485SErwan Le Ray unsigned long flags; 40633bb2f6aSErwan Le Ray 407*6333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 408*6333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 409*6333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 410*6333a485SErwan Le Ray if (size) 411*6333a485SErwan Le Ray tty_flip_buffer_push(tport); 41233bb2f6aSErwan Le Ray } 41333bb2f6aSErwan Le Ray 41456f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port) 415d075719eSErwan Le Ray { 416d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 417d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 418d075719eSErwan Le Ray 4192aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 42056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 421d075719eSErwan Le Ray else 42256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 423d075719eSErwan Le Ray } 424d075719eSErwan Le Ray 42556f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port) 42634891872SAlexandre TORGUE { 42734891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 428d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 42934891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 43034891872SAlexandre TORGUE 43134891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 43256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 43334891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 43434891872SAlexandre TORGUE } 43534891872SAlexandre TORGUE 4365d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 4375d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 4385d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 4395d9176edSErwan Le Ray break; 44034891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 44134891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 44234891872SAlexandre TORGUE port->icount.tx++; 44334891872SAlexandre TORGUE } 44434891872SAlexandre TORGUE 4455d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 4465d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 44756f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 4485d9176edSErwan Le Ray else 44956f9a76cSErwan Le Ray stm32_usart_tx_interrupt_enable(port); 4505d9176edSErwan Le Ray } 4515d9176edSErwan Le Ray 45256f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port) 45334891872SAlexandre TORGUE { 45434891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 455d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 45634891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 45734891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 45834891872SAlexandre TORGUE unsigned int count, i; 45934891872SAlexandre TORGUE 46034891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 46134891872SAlexandre TORGUE return; 46234891872SAlexandre TORGUE 46334891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 46434891872SAlexandre TORGUE 46534891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 46634891872SAlexandre TORGUE 46734891872SAlexandre TORGUE if (count > TX_BUF_L) 46834891872SAlexandre TORGUE count = TX_BUF_L; 46934891872SAlexandre TORGUE 47034891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 47134891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 47234891872SAlexandre TORGUE } else { 47334891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 47434891872SAlexandre TORGUE size_t two; 47534891872SAlexandre TORGUE 47634891872SAlexandre TORGUE if (one > count) 47734891872SAlexandre TORGUE one = count; 47834891872SAlexandre TORGUE two = count - one; 47934891872SAlexandre TORGUE 48034891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 48134891872SAlexandre TORGUE if (two) 48234891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 48334891872SAlexandre TORGUE } 48434891872SAlexandre TORGUE 48534891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 48634891872SAlexandre TORGUE stm32port->tx_dma_buf, 48734891872SAlexandre TORGUE count, 48834891872SAlexandre TORGUE DMA_MEM_TO_DEV, 48934891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 49034891872SAlexandre TORGUE 491e7997f7fSErwan Le Ray if (!desc) 492e7997f7fSErwan Le Ray goto fallback_err; 49334891872SAlexandre TORGUE 49456f9a76cSErwan Le Ray desc->callback = stm32_usart_tx_dma_complete; 49534891872SAlexandre TORGUE desc->callback_param = port; 49634891872SAlexandre TORGUE 49734891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 498e7997f7fSErwan Le Ray if (dma_submit_error(dmaengine_submit(desc))) { 499e7997f7fSErwan Le Ray /* dma no yet started, safe to free resources */ 500e7997f7fSErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 501e7997f7fSErwan Le Ray goto fallback_err; 502e7997f7fSErwan Le Ray } 50334891872SAlexandre TORGUE 50434891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 50534891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 50634891872SAlexandre TORGUE 50756f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 50834891872SAlexandre TORGUE 50934891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 51034891872SAlexandre TORGUE port->icount.tx += count; 511e7997f7fSErwan Le Ray return; 512e7997f7fSErwan Le Ray 513e7997f7fSErwan Le Ray fallback_err: 514e7997f7fSErwan Le Ray for (i = count; i > 0; i--) 51556f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 51634891872SAlexandre TORGUE } 51734891872SAlexandre TORGUE 51856f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port) 51948a6092fSMaxime Coquelin { 520ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 521d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 52248a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 52348a6092fSMaxime Coquelin 52448a6092fSMaxime Coquelin if (port->x_char) { 52534891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 52656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 527ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 52848a6092fSMaxime Coquelin port->x_char = 0; 52948a6092fSMaxime Coquelin port->icount.tx++; 53034891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 53156f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 53248a6092fSMaxime Coquelin return; 53348a6092fSMaxime Coquelin } 53448a6092fSMaxime Coquelin 535b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 53656f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 53748a6092fSMaxime Coquelin return; 53848a6092fSMaxime Coquelin } 53948a6092fSMaxime Coquelin 54064c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 54156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC); 54264c32eabSErwan Le Ray else 5431250ed71SFabrice Gasnier writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr); 54464c32eabSErwan Le Ray 54534891872SAlexandre TORGUE if (stm32_port->tx_ch) 54656f9a76cSErwan Le Ray stm32_usart_transmit_chars_dma(port); 54734891872SAlexandre TORGUE else 54856f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 54948a6092fSMaxime Coquelin 55048a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 55148a6092fSMaxime Coquelin uart_write_wakeup(port); 55248a6092fSMaxime Coquelin 55348a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 55456f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 55548a6092fSMaxime Coquelin } 55648a6092fSMaxime Coquelin 55756f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) 55848a6092fSMaxime Coquelin { 55948a6092fSMaxime Coquelin struct uart_port *port = ptr; 56012761869SErwan Le Ray struct tty_port *tport = &port->state->port; 561ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 562d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 56348a6092fSMaxime Coquelin u32 sr; 564*6333a485SErwan Le Ray unsigned int size; 56548a6092fSMaxime Coquelin 566ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 56748a6092fSMaxime Coquelin 5684cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 5694cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 5704cc0ed62SErwan Le Ray port->membase + ofs->icr); 5714cc0ed62SErwan Le Ray 57212761869SErwan Le Ray if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) { 57312761869SErwan Le Ray /* Clear wake up flag and disable wake up interrupt */ 574270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 575270e5a74SFabrice Gasnier port->membase + ofs->icr); 57612761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 57712761869SErwan Le Ray if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 57812761869SErwan Le Ray pm_wakeup_event(tport->tty->dev, 0); 57912761869SErwan Le Ray } 580270e5a74SFabrice Gasnier 58133bb2f6aSErwan Le Ray /* 58233bb2f6aSErwan Le Ray * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request 58333bb2f6aSErwan Le Ray * line has been masked by HW and rx data are stacking in FIFO. 58433bb2f6aSErwan Le Ray */ 585d1ec8a2eSErwan Le Ray if (!stm32_port->throttled) { 58633bb2f6aSErwan Le Ray if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_enabled(port)) || 587d1ec8a2eSErwan Le Ray ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_enabled(port))) { 588*6333a485SErwan Le Ray spin_lock(&port->lock); 589*6333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 590*6333a485SErwan Le Ray uart_unlock_and_check_sysrq(port); 591*6333a485SErwan Le Ray if (size) 592*6333a485SErwan Le Ray tty_flip_buffer_push(tport); 593d1ec8a2eSErwan Le Ray } 594d1ec8a2eSErwan Le Ray } 59548a6092fSMaxime Coquelin 596ad767681SErwan Le Ray if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) { 597ad767681SErwan Le Ray spin_lock(&port->lock); 59856f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 59901d32d71SAlexandre TORGUE spin_unlock(&port->lock); 600ad767681SErwan Le Ray } 60101d32d71SAlexandre TORGUE 60233bb2f6aSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) 60334891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 60434891872SAlexandre TORGUE else 60534891872SAlexandre TORGUE return IRQ_HANDLED; 60634891872SAlexandre TORGUE } 60734891872SAlexandre TORGUE 60856f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr) 60934891872SAlexandre TORGUE { 61034891872SAlexandre TORGUE struct uart_port *port = ptr; 611*6333a485SErwan Le Ray struct tty_port *tport = &port->state->port; 612d1ec8a2eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 613*6333a485SErwan Le Ray unsigned int size; 614*6333a485SErwan Le Ray unsigned long flags; 61534891872SAlexandre TORGUE 616cc58d0a3SErwan Le Ray /* Receiver timeout irq for DMA RX */ 617*6333a485SErwan Le Ray if (!stm32_port->throttled) { 618*6333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 619*6333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 620*6333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 621*6333a485SErwan Le Ray if (size) 622*6333a485SErwan Le Ray tty_flip_buffer_push(tport); 623*6333a485SErwan Le Ray } 62434891872SAlexandre TORGUE 62548a6092fSMaxime Coquelin return IRQ_HANDLED; 62648a6092fSMaxime Coquelin } 62748a6092fSMaxime Coquelin 62856f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port) 62948a6092fSMaxime Coquelin { 630ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 631d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 632ada8618fSAlexandre TORGUE 6333db1d524SErwan Le Ray if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC) 6343db1d524SErwan Le Ray return TIOCSER_TEMT; 6353db1d524SErwan Le Ray 6363db1d524SErwan Le Ray return 0; 63748a6092fSMaxime Coquelin } 63848a6092fSMaxime Coquelin 63956f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl) 64048a6092fSMaxime Coquelin { 641ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 642d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 643ada8618fSAlexandre TORGUE 64448a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 64556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE); 64648a6092fSMaxime Coquelin else 64756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 6486cf61b9bSManivannan Sadhasivam 6496cf61b9bSManivannan Sadhasivam mctrl_gpio_set(stm32_port->gpios, mctrl); 65048a6092fSMaxime Coquelin } 65148a6092fSMaxime Coquelin 65256f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port) 65348a6092fSMaxime Coquelin { 6546cf61b9bSManivannan Sadhasivam struct stm32_port *stm32_port = to_stm32_port(port); 6556cf61b9bSManivannan Sadhasivam unsigned int ret; 6566cf61b9bSManivannan Sadhasivam 65748a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 6586cf61b9bSManivannan Sadhasivam ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 6596cf61b9bSManivannan Sadhasivam 6606cf61b9bSManivannan Sadhasivam return mctrl_gpio_get(stm32_port->gpios, &ret); 6616cf61b9bSManivannan Sadhasivam } 6626cf61b9bSManivannan Sadhasivam 66356f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port) 6646cf61b9bSManivannan Sadhasivam { 6656cf61b9bSManivannan Sadhasivam mctrl_gpio_enable_ms(to_stm32_port(port)->gpios); 6666cf61b9bSManivannan Sadhasivam } 6676cf61b9bSManivannan Sadhasivam 66856f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port) 6696cf61b9bSManivannan Sadhasivam { 6706cf61b9bSManivannan Sadhasivam mctrl_gpio_disable_ms(to_stm32_port(port)->gpios); 67148a6092fSMaxime Coquelin } 67248a6092fSMaxime Coquelin 67348a6092fSMaxime Coquelin /* Transmit stop */ 67456f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port) 67548a6092fSMaxime Coquelin { 676ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 677ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 678ad0c2748SMarek Vasut 67956f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 680ad0c2748SMarek Vasut 681ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 682ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 683ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 684ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 685ad0c2748SMarek Vasut } else { 686ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 687ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 688ad0c2748SMarek Vasut } 689ad0c2748SMarek Vasut } 69048a6092fSMaxime Coquelin } 69148a6092fSMaxime Coquelin 69248a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 69356f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port) 69448a6092fSMaxime Coquelin { 695ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 696ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 69748a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 69848a6092fSMaxime Coquelin 69948a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 70048a6092fSMaxime Coquelin return; 70148a6092fSMaxime Coquelin 702ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 703ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 704ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 705ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 706ad0c2748SMarek Vasut } else { 707ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 708ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 709ad0c2748SMarek Vasut } 710ad0c2748SMarek Vasut } 711ad0c2748SMarek Vasut 71256f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 71348a6092fSMaxime Coquelin } 71448a6092fSMaxime Coquelin 7153d82be8bSErwan Le Ray /* Flush the transmit buffer. */ 7163d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port) 7173d82be8bSErwan Le Ray { 7183d82be8bSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 7193d82be8bSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 7203d82be8bSErwan Le Ray 7213d82be8bSErwan Le Ray if (stm32_port->tx_ch) { 7223d82be8bSErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 7233d82be8bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 7243d82be8bSErwan Le Ray stm32_port->tx_dma_busy = false; 7253d82be8bSErwan Le Ray } 7263d82be8bSErwan Le Ray } 7273d82be8bSErwan Le Ray 72848a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 72956f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port) 73048a6092fSMaxime Coquelin { 731ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 732d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 73348a6092fSMaxime Coquelin unsigned long flags; 73448a6092fSMaxime Coquelin 73548a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 736d1ec8a2eSErwan Le Ray 737d1ec8a2eSErwan Le Ray /* 738d1ec8a2eSErwan Le Ray * Disable DMA request line if enabled, so the RX data gets queued into the FIFO. 739d1ec8a2eSErwan Le Ray * Hardware flow control is triggered when RX FIFO is full. 740d1ec8a2eSErwan Le Ray */ 741d1ec8a2eSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) 742d1ec8a2eSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 743d1ec8a2eSErwan Le Ray 74456f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 745d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 74656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 747d0a6a7bcSErwan Le Ray 748d1ec8a2eSErwan Le Ray stm32_port->throttled = true; 74948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 75048a6092fSMaxime Coquelin } 75148a6092fSMaxime Coquelin 75248a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 75356f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port) 75448a6092fSMaxime Coquelin { 755ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 756d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 75748a6092fSMaxime Coquelin unsigned long flags; 75848a6092fSMaxime Coquelin 75948a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 76056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 761d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 76256f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq); 763d0a6a7bcSErwan Le Ray 764d1ec8a2eSErwan Le Ray /* 765d1ec8a2eSErwan Le Ray * Switch back to DMA mode (re-enable DMA request line). 766d1ec8a2eSErwan Le Ray * Hardware flow control is stopped when FIFO is not full any more. 767d1ec8a2eSErwan Le Ray */ 768d1ec8a2eSErwan Le Ray if (stm32_port->rx_ch) 769d1ec8a2eSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 770d1ec8a2eSErwan Le Ray 771d1ec8a2eSErwan Le Ray stm32_port->throttled = false; 77248a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 77348a6092fSMaxime Coquelin } 77448a6092fSMaxime Coquelin 77548a6092fSMaxime Coquelin /* Receive stop */ 77656f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port) 77748a6092fSMaxime Coquelin { 778ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 779d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 780ada8618fSAlexandre TORGUE 781e0abc903SErwan Le Ray /* Disable DMA request line. */ 782e0abc903SErwan Le Ray if (stm32_port->rx_ch) 783e0abc903SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 784e0abc903SErwan Le Ray 78556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 786d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 78756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 78848a6092fSMaxime Coquelin } 78948a6092fSMaxime Coquelin 79048a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 79156f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state) 79248a6092fSMaxime Coquelin { 79348a6092fSMaxime Coquelin } 79448a6092fSMaxime Coquelin 7956eeb348cSErwan Le Ray static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port) 7966eeb348cSErwan Le Ray { 7976eeb348cSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 7986eeb348cSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 7996eeb348cSErwan Le Ray struct dma_async_tx_descriptor *desc; 8006eeb348cSErwan Le Ray int ret; 8016eeb348cSErwan Le Ray 8026eeb348cSErwan Le Ray stm32_port->last_res = RX_BUF_L; 8036eeb348cSErwan Le Ray /* Prepare a DMA cyclic transaction */ 8046eeb348cSErwan Le Ray desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch, 8056eeb348cSErwan Le Ray stm32_port->rx_dma_buf, 8066eeb348cSErwan Le Ray RX_BUF_L, RX_BUF_P, 8076eeb348cSErwan Le Ray DMA_DEV_TO_MEM, 8086eeb348cSErwan Le Ray DMA_PREP_INTERRUPT); 8096eeb348cSErwan Le Ray if (!desc) { 8106eeb348cSErwan Le Ray dev_err(port->dev, "rx dma prep cyclic failed\n"); 8116eeb348cSErwan Le Ray return -ENODEV; 8126eeb348cSErwan Le Ray } 8136eeb348cSErwan Le Ray 8146eeb348cSErwan Le Ray desc->callback = stm32_usart_rx_dma_complete; 8156eeb348cSErwan Le Ray desc->callback_param = port; 8166eeb348cSErwan Le Ray 8176eeb348cSErwan Le Ray /* Push current DMA transaction in the pending queue */ 8186eeb348cSErwan Le Ray ret = dma_submit_error(dmaengine_submit(desc)); 8196eeb348cSErwan Le Ray if (ret) { 8206eeb348cSErwan Le Ray dmaengine_terminate_sync(stm32_port->rx_ch); 8216eeb348cSErwan Le Ray return ret; 8226eeb348cSErwan Le Ray } 8236eeb348cSErwan Le Ray 8246eeb348cSErwan Le Ray /* Issue pending DMA requests */ 8256eeb348cSErwan Le Ray dma_async_issue_pending(stm32_port->rx_ch); 8266eeb348cSErwan Le Ray 8276eeb348cSErwan Le Ray /* 8286eeb348cSErwan Le Ray * DMA request line not re-enabled at resume when port is throttled. 8296eeb348cSErwan Le Ray * It will be re-enabled by unthrottle ops. 8306eeb348cSErwan Le Ray */ 8316eeb348cSErwan Le Ray if (!stm32_port->throttled) 8326eeb348cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 8336eeb348cSErwan Le Ray 8346eeb348cSErwan Le Ray return 0; 8356eeb348cSErwan Le Ray } 8366eeb348cSErwan Le Ray 83756f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port) 83848a6092fSMaxime Coquelin { 839ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 840d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 841f4518a8aSErwan Le Ray const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 84248a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 84348a6092fSMaxime Coquelin u32 val; 84448a6092fSMaxime Coquelin int ret; 84548a6092fSMaxime Coquelin 84656f9a76cSErwan Le Ray ret = request_threaded_irq(port->irq, stm32_usart_interrupt, 84756f9a76cSErwan Le Ray stm32_usart_threaded_interrupt, 848e359b441SJohan Hovold IRQF_ONESHOT | IRQF_NO_SUSPEND, 849e359b441SJohan Hovold name, port); 85048a6092fSMaxime Coquelin if (ret) 85148a6092fSMaxime Coquelin return ret; 85248a6092fSMaxime Coquelin 8533cd66593SMartin Devera if (stm32_port->swap) { 8543cd66593SMartin Devera val = readl_relaxed(port->membase + ofs->cr2); 8553cd66593SMartin Devera val |= USART_CR2_SWAP; 8563cd66593SMartin Devera writel_relaxed(val, port->membase + ofs->cr2); 8573cd66593SMartin Devera } 8583cd66593SMartin Devera 85984872dc4SErwan Le Ray /* RX FIFO Flush */ 86084872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 861315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr); 86248a6092fSMaxime Coquelin 863e0abc903SErwan Le Ray if (stm32_port->rx_ch) { 8646eeb348cSErwan Le Ray ret = stm32_usart_start_rx_dma_cyclic(port); 865e0abc903SErwan Le Ray if (ret) { 8666eeb348cSErwan Le Ray free_irq(port->irq, port); 8676eeb348cSErwan Le Ray return ret; 868e0abc903SErwan Le Ray } 869e0abc903SErwan Le Ray } 870d1ec8a2eSErwan Le Ray 87125a8e761SErwan Le Ray /* RX enabling */ 872f4518a8aSErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit); 87356f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, val); 87484872dc4SErwan Le Ray 87548a6092fSMaxime Coquelin return 0; 87648a6092fSMaxime Coquelin } 87748a6092fSMaxime Coquelin 87856f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port) 87948a6092fSMaxime Coquelin { 880ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 881d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 882d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 88364c32eabSErwan Le Ray u32 val, isr; 88464c32eabSErwan Le Ray int ret; 88548a6092fSMaxime Coquelin 8866cf61b9bSManivannan Sadhasivam /* Disable modem control interrupts */ 88756f9a76cSErwan Le Ray stm32_usart_disable_ms(port); 8886cf61b9bSManivannan Sadhasivam 8894cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 8904cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 89187f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 892351a762aSGerald Baeza if (stm32_port->fifoen) 893351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 89464c32eabSErwan Le Ray 89564c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 89664c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 89764c32eabSErwan Le Ray 10, 100000); 89864c32eabSErwan Le Ray 899c31c3ea0SErwan Le Ray /* Send the TC error message only when ISR_TC is not set */ 90064c32eabSErwan Le Ray if (ret) 901c31c3ea0SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 90264c32eabSErwan Le Ray 903e0abc903SErwan Le Ray /* Disable RX DMA. */ 904e0abc903SErwan Le Ray if (stm32_port->rx_ch) 905e0abc903SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 906e0abc903SErwan Le Ray 9079f77d192SErwan Le Ray /* flush RX & TX FIFO */ 9089f77d192SErwan Le Ray if (ofs->rqr != UNDEF_REG) 9099f77d192SErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 9109f77d192SErwan Le Ray port->membase + ofs->rqr); 9119f77d192SErwan Le Ray 91256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, val); 91348a6092fSMaxime Coquelin 91448a6092fSMaxime Coquelin free_irq(port->irq, port); 91548a6092fSMaxime Coquelin } 91648a6092fSMaxime Coquelin 91756f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port, 91856f9a76cSErwan Le Ray struct ktermios *termios, 91948a6092fSMaxime Coquelin struct ktermios *old) 92048a6092fSMaxime Coquelin { 92148a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 922d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 923d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 9241bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 925c8a9d043SErwan Le Ray unsigned int baud, bits; 92648a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 92748a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 928f264c6f6SErwan Le Ray u32 cr1, cr2, cr3, isr; 92948a6092fSMaxime Coquelin unsigned long flags; 930f264c6f6SErwan Le Ray int ret; 93148a6092fSMaxime Coquelin 93248a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 93348a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 93448a6092fSMaxime Coquelin 93548a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 93648a6092fSMaxime Coquelin 93748a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 93848a6092fSMaxime Coquelin 939f264c6f6SErwan Le Ray ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 940f264c6f6SErwan Le Ray isr, 941f264c6f6SErwan Le Ray (isr & USART_SR_TC), 942f264c6f6SErwan Le Ray 10, 100000); 943f264c6f6SErwan Le Ray 944f264c6f6SErwan Le Ray /* Send the TC error message only when ISR_TC is not set. */ 945f264c6f6SErwan Le Ray if (ret) 946f264c6f6SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 947f264c6f6SErwan Le Ray 94848a6092fSMaxime Coquelin /* Stop serial port and reset value */ 949ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 95048a6092fSMaxime Coquelin 95184872dc4SErwan Le Ray /* flush RX & TX FIFO */ 95284872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 953315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 954315e2d8aSErwan Le Ray port->membase + ofs->rqr); 9551bcda09dSBich HEMON 95684872dc4SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 957351a762aSGerald Baeza if (stm32_port->fifoen) 958351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 9593cd66593SMartin Devera cr2 = stm32_port->swap ? USART_CR2_SWAP : 0; 96025a8e761SErwan Le Ray 96125a8e761SErwan Le Ray /* Tx and RX FIFO configuration */ 962d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 96325a8e761SErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE; 96425a8e761SErwan Le Ray if (stm32_port->fifoen) { 9652aa1bbb2SFabrice Gasnier if (stm32_port->txftcfg >= 0) 9662aa1bbb2SFabrice Gasnier cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT; 9672aa1bbb2SFabrice Gasnier if (stm32_port->rxftcfg >= 0) 9682aa1bbb2SFabrice Gasnier cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT; 96925a8e761SErwan Le Ray } 97048a6092fSMaxime Coquelin 97148a6092fSMaxime Coquelin if (cflag & CSTOPB) 97248a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 97348a6092fSMaxime Coquelin 9743ec2ff37SJiri Slaby bits = tty_get_char_size(cflag); 9756c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 976c8a9d043SErwan Le Ray 97748a6092fSMaxime Coquelin if (cflag & PARENB) { 978c8a9d043SErwan Le Ray bits++; 97948a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 980c8a9d043SErwan Le Ray } 981c8a9d043SErwan Le Ray 982c8a9d043SErwan Le Ray /* 983c8a9d043SErwan Le Ray * Word length configuration: 984c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 985c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 986c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 987c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 988c8a9d043SErwan Le Ray */ 989c8a9d043SErwan Le Ray if (bits == 9) 990ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 991c8a9d043SErwan Le Ray else if ((bits == 7) && cfg->has_7bits_data) 992c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 993c8a9d043SErwan Le Ray else if (bits != 8) 994c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 995c8a9d043SErwan Le Ray , bits); 99648a6092fSMaxime Coquelin 9974cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 9982aa1bbb2SFabrice Gasnier (stm32_port->fifoen && 9992aa1bbb2SFabrice Gasnier stm32_port->rxftcfg >= 0))) { 10004cc0ed62SErwan Le Ray if (cflag & CSTOPB) 10014cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 10024cc0ed62SErwan Le Ray else 10034cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 10044cc0ed62SErwan Le Ray 10054cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 10064cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 10074cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 10084cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 100933bb2f6aSErwan Le Ray /* 101033bb2f6aSErwan Le Ray * Enable fifo threshold irq in two cases, either when there is no DMA, or when 101133bb2f6aSErwan Le Ray * wake up over usart, from low power until the DMA gets re-enabled by resume. 101233bb2f6aSErwan Le Ray */ 1013d0a6a7bcSErwan Le Ray stm32_port->cr3_irq = USART_CR3_RXFTIE; 10144cc0ed62SErwan Le Ray } 10154cc0ed62SErwan Le Ray 1016d0a6a7bcSErwan Le Ray cr1 |= stm32_port->cr1_irq; 1017d0a6a7bcSErwan Le Ray cr3 |= stm32_port->cr3_irq; 1018d0a6a7bcSErwan Le Ray 101948a6092fSMaxime Coquelin if (cflag & PARODD) 102048a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 102148a6092fSMaxime Coquelin 102248a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 102348a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 102448a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 102535abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 102648a6092fSMaxime Coquelin } 102748a6092fSMaxime Coquelin 102848a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 102948a6092fSMaxime Coquelin 103048a6092fSMaxime Coquelin /* 103148a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 103248a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 103348a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 103448a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 103548a6092fSMaxime Coquelin */ 103648a6092fSMaxime Coquelin if (usartdiv < 16) { 103748a6092fSMaxime Coquelin oversampling = 8; 10381bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 103956f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8); 104048a6092fSMaxime Coquelin } else { 104148a6092fSMaxime Coquelin oversampling = 16; 10421bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 104356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 104448a6092fSMaxime Coquelin } 104548a6092fSMaxime Coquelin 104648a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 104748a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 1048ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 104948a6092fSMaxime Coquelin 105048a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 105148a6092fSMaxime Coquelin 105248a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 105348a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 105448a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 105548a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 10564f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 105748a6092fSMaxime Coquelin 105848a6092fSMaxime Coquelin /* Characters to ignore */ 105948a6092fSMaxime Coquelin port->ignore_status_mask = 0; 106048a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 106148a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 106248a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 10634f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 106448a6092fSMaxime Coquelin /* 106548a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 106648a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 106748a6092fSMaxime Coquelin */ 106848a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 106948a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 107048a6092fSMaxime Coquelin } 107148a6092fSMaxime Coquelin 107248a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 107348a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 107448a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 107548a6092fSMaxime Coquelin 107633bb2f6aSErwan Le Ray if (stm32_port->rx_ch) { 107733bb2f6aSErwan Le Ray /* 107833bb2f6aSErwan Le Ray * Setup DMA to collect only valid data and enable error irqs. 107933bb2f6aSErwan Le Ray * This also enables break reception when using DMA. 108033bb2f6aSErwan Le Ray */ 108133bb2f6aSErwan Le Ray cr1 |= USART_CR1_PEIE; 108233bb2f6aSErwan Le Ray cr3 |= USART_CR3_EIE; 108334891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 108433bb2f6aSErwan Le Ray cr3 |= USART_CR3_DDRE; 108533bb2f6aSErwan Le Ray } 108634891872SAlexandre TORGUE 10871bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 108856f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 10891bcda09dSBich HEMON rs485conf->delay_rts_before_send, 109056f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 109156f9a76cSErwan Le Ray baud); 10921bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 10931bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 10941bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 10951bcda09dSBich HEMON } else { 10961bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 10971bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 10981bcda09dSBich HEMON } 10991bcda09dSBich HEMON 11001bcda09dSBich HEMON } else { 11011bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 11021bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 11031bcda09dSBich HEMON } 11041bcda09dSBich HEMON 110512761869SErwan Le Ray /* Configure wake up from low power on start bit detection */ 11063d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 110712761869SErwan Le Ray cr3 &= ~USART_CR3_WUS_MASK; 110812761869SErwan Le Ray cr3 |= USART_CR3_WUS_START_BIT; 110912761869SErwan Le Ray } 111012761869SErwan Le Ray 1111ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 1112ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 1113ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 111448a6092fSMaxime Coquelin 111556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 111648a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 1117436c9793SErwan Le Ray 1118436c9793SErwan Le Ray /* Handle modem control interrupts */ 1119436c9793SErwan Le Ray if (UART_ENABLE_MS(port, termios->c_cflag)) 1120436c9793SErwan Le Ray stm32_usart_enable_ms(port); 1121436c9793SErwan Le Ray else 1122436c9793SErwan Le Ray stm32_usart_disable_ms(port); 112348a6092fSMaxime Coquelin } 112448a6092fSMaxime Coquelin 112556f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port) 112648a6092fSMaxime Coquelin { 112748a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 112848a6092fSMaxime Coquelin } 112948a6092fSMaxime Coquelin 113056f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port) 113148a6092fSMaxime Coquelin { 113248a6092fSMaxime Coquelin } 113348a6092fSMaxime Coquelin 113456f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port) 113548a6092fSMaxime Coquelin { 113648a6092fSMaxime Coquelin return 0; 113748a6092fSMaxime Coquelin } 113848a6092fSMaxime Coquelin 113956f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags) 114048a6092fSMaxime Coquelin { 114148a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 114248a6092fSMaxime Coquelin port->type = PORT_STM32; 114348a6092fSMaxime Coquelin } 114448a6092fSMaxime Coquelin 114548a6092fSMaxime Coquelin static int 114656f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser) 114748a6092fSMaxime Coquelin { 114848a6092fSMaxime Coquelin /* No user changeable parameters */ 114948a6092fSMaxime Coquelin return -EINVAL; 115048a6092fSMaxime Coquelin } 115148a6092fSMaxime Coquelin 115256f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state, 115348a6092fSMaxime Coquelin unsigned int oldstate) 115448a6092fSMaxime Coquelin { 115548a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 115648a6092fSMaxime Coquelin struct stm32_port, port); 1157d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 1158d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32port->info->cfg; 115918ee37e1SJohan Hovold unsigned long flags; 116048a6092fSMaxime Coquelin 116148a6092fSMaxime Coquelin switch (state) { 116248a6092fSMaxime Coquelin case UART_PM_STATE_ON: 1163fb6dcef6SErwan Le Ray pm_runtime_get_sync(port->dev); 116448a6092fSMaxime Coquelin break; 116548a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 116648a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 116756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 116848a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 1169fb6dcef6SErwan Le Ray pm_runtime_put_sync(port->dev); 117048a6092fSMaxime Coquelin break; 117148a6092fSMaxime Coquelin } 117248a6092fSMaxime Coquelin } 117348a6092fSMaxime Coquelin 117448a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 117556f9a76cSErwan Le Ray .tx_empty = stm32_usart_tx_empty, 117656f9a76cSErwan Le Ray .set_mctrl = stm32_usart_set_mctrl, 117756f9a76cSErwan Le Ray .get_mctrl = stm32_usart_get_mctrl, 117856f9a76cSErwan Le Ray .stop_tx = stm32_usart_stop_tx, 117956f9a76cSErwan Le Ray .start_tx = stm32_usart_start_tx, 118056f9a76cSErwan Le Ray .throttle = stm32_usart_throttle, 118156f9a76cSErwan Le Ray .unthrottle = stm32_usart_unthrottle, 118256f9a76cSErwan Le Ray .stop_rx = stm32_usart_stop_rx, 118356f9a76cSErwan Le Ray .enable_ms = stm32_usart_enable_ms, 118456f9a76cSErwan Le Ray .break_ctl = stm32_usart_break_ctl, 118556f9a76cSErwan Le Ray .startup = stm32_usart_startup, 118656f9a76cSErwan Le Ray .shutdown = stm32_usart_shutdown, 11873d82be8bSErwan Le Ray .flush_buffer = stm32_usart_flush_buffer, 118856f9a76cSErwan Le Ray .set_termios = stm32_usart_set_termios, 118956f9a76cSErwan Le Ray .pm = stm32_usart_pm, 119056f9a76cSErwan Le Ray .type = stm32_usart_type, 119156f9a76cSErwan Le Ray .release_port = stm32_usart_release_port, 119256f9a76cSErwan Le Ray .request_port = stm32_usart_request_port, 119356f9a76cSErwan Le Ray .config_port = stm32_usart_config_port, 119456f9a76cSErwan Le Ray .verify_port = stm32_usart_verify_port, 119548a6092fSMaxime Coquelin }; 119648a6092fSMaxime Coquelin 11972aa1bbb2SFabrice Gasnier /* 11982aa1bbb2SFabrice Gasnier * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG) 11992aa1bbb2SFabrice Gasnier * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case, 12002aa1bbb2SFabrice Gasnier * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE. 12012aa1bbb2SFabrice Gasnier * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1. 12022aa1bbb2SFabrice Gasnier */ 12032aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 }; 12042aa1bbb2SFabrice Gasnier 12052aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p, 12062aa1bbb2SFabrice Gasnier int *ftcfg) 12072aa1bbb2SFabrice Gasnier { 12082aa1bbb2SFabrice Gasnier u32 bytes, i; 12092aa1bbb2SFabrice Gasnier 12102aa1bbb2SFabrice Gasnier /* DT option to get RX & TX FIFO threshold (default to 8 bytes) */ 12112aa1bbb2SFabrice Gasnier if (of_property_read_u32(pdev->dev.of_node, p, &bytes)) 12122aa1bbb2SFabrice Gasnier bytes = 8; 12132aa1bbb2SFabrice Gasnier 12142aa1bbb2SFabrice Gasnier for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++) 12152aa1bbb2SFabrice Gasnier if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes) 12162aa1bbb2SFabrice Gasnier break; 12172aa1bbb2SFabrice Gasnier if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg)) 12182aa1bbb2SFabrice Gasnier i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1; 12192aa1bbb2SFabrice Gasnier 12202aa1bbb2SFabrice Gasnier dev_dbg(&pdev->dev, "%s set to %d bytes\n", p, 12212aa1bbb2SFabrice Gasnier stm32h7_usart_fifo_thresh_cfg[i]); 12222aa1bbb2SFabrice Gasnier 12232aa1bbb2SFabrice Gasnier /* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */ 12242aa1bbb2SFabrice Gasnier if (i) 12252aa1bbb2SFabrice Gasnier *ftcfg = i - 1; 12262aa1bbb2SFabrice Gasnier else 12272aa1bbb2SFabrice Gasnier *ftcfg = -EINVAL; 12282aa1bbb2SFabrice Gasnier } 12292aa1bbb2SFabrice Gasnier 123097f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port) 123197f3a085SErwan Le Ray { 123297f3a085SErwan Le Ray clk_disable_unprepare(stm32port->clk); 123397f3a085SErwan Le Ray } 123497f3a085SErwan Le Ray 123556f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port, 123648a6092fSMaxime Coquelin struct platform_device *pdev) 123748a6092fSMaxime Coquelin { 123848a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 123948a6092fSMaxime Coquelin struct resource *res; 1240e0f2a902SErwan Le Ray int ret, irq; 124148a6092fSMaxime Coquelin 1242e0f2a902SErwan Le Ray irq = platform_get_irq(pdev, 0); 1243217b04c6STang Bin if (irq < 0) 1244217b04c6STang Bin return irq; 124592fc0023SErwan Le Ray 124648a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 124748a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 124848a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 124948a6092fSMaxime Coquelin port->dev = &pdev->dev; 1250d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 12519feedaa7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE); 1252e0f2a902SErwan Le Ray port->irq = irq; 125356f9a76cSErwan Le Ray port->rs485_config = stm32_usart_config_rs485; 12547d8f6861SBich HEMON 125556f9a76cSErwan Le Ray ret = stm32_usart_init_rs485(port, pdev); 1256c150c0f3SLukas Wunner if (ret) 1257c150c0f3SLukas Wunner return ret; 12587d8f6861SBich HEMON 12593d530017SAlexandre Torgue stm32port->wakeup_src = stm32port->info->cfg.has_wakeup && 12603d530017SAlexandre Torgue of_property_read_bool(pdev->dev.of_node, "wakeup-source"); 12612c58e560SErwan Le Ray 12623cd66593SMartin Devera stm32port->swap = stm32port->info->cfg.has_swap && 12633cd66593SMartin Devera of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"); 12643cd66593SMartin Devera 1265351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 12662aa1bbb2SFabrice Gasnier if (stm32port->fifoen) { 12672aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "rx-threshold", 12682aa1bbb2SFabrice Gasnier &stm32port->rxftcfg); 12692aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "tx-threshold", 12702aa1bbb2SFabrice Gasnier &stm32port->txftcfg); 12712aa1bbb2SFabrice Gasnier } 127248a6092fSMaxime Coquelin 12733d881e32STang Bin port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 127448a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 127548a6092fSMaxime Coquelin return PTR_ERR(port->membase); 127648a6092fSMaxime Coquelin port->mapbase = res->start; 127748a6092fSMaxime Coquelin 127848a6092fSMaxime Coquelin spin_lock_init(&port->lock); 127948a6092fSMaxime Coquelin 128048a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 128148a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 128248a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 128348a6092fSMaxime Coquelin 128448a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 128548a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 128648a6092fSMaxime Coquelin if (ret) 128748a6092fSMaxime Coquelin return ret; 128848a6092fSMaxime Coquelin 128948a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 1290ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 129148a6092fSMaxime Coquelin ret = -EINVAL; 12926cf61b9bSManivannan Sadhasivam goto err_clk; 1293ada80043SFabrice Gasnier } 129448a6092fSMaxime Coquelin 12956cf61b9bSManivannan Sadhasivam stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0); 12966cf61b9bSManivannan Sadhasivam if (IS_ERR(stm32port->gpios)) { 12976cf61b9bSManivannan Sadhasivam ret = PTR_ERR(stm32port->gpios); 12986cf61b9bSManivannan Sadhasivam goto err_clk; 12996cf61b9bSManivannan Sadhasivam } 13006cf61b9bSManivannan Sadhasivam 13019359369aSErwan Le Ray /* 13029359369aSErwan Le Ray * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts" 13039359369aSErwan Le Ray * properties should not be specified. 13049359369aSErwan Le Ray */ 13056cf61b9bSManivannan Sadhasivam if (stm32port->hw_flow_control) { 13066cf61b9bSManivannan Sadhasivam if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) || 13076cf61b9bSManivannan Sadhasivam mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) { 13086cf61b9bSManivannan Sadhasivam dev_err(&pdev->dev, "Conflicting RTS/CTS config\n"); 13096cf61b9bSManivannan Sadhasivam ret = -EINVAL; 13106cf61b9bSManivannan Sadhasivam goto err_clk; 13116cf61b9bSManivannan Sadhasivam } 13126cf61b9bSManivannan Sadhasivam } 13136cf61b9bSManivannan Sadhasivam 13146cf61b9bSManivannan Sadhasivam return ret; 13156cf61b9bSManivannan Sadhasivam 13166cf61b9bSManivannan Sadhasivam err_clk: 13176cf61b9bSManivannan Sadhasivam clk_disable_unprepare(stm32port->clk); 13186cf61b9bSManivannan Sadhasivam 131948a6092fSMaxime Coquelin return ret; 132048a6092fSMaxime Coquelin } 132148a6092fSMaxime Coquelin 132256f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev) 132348a6092fSMaxime Coquelin { 132448a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 132548a6092fSMaxime Coquelin int id; 132648a6092fSMaxime Coquelin 132748a6092fSMaxime Coquelin if (!np) 132848a6092fSMaxime Coquelin return NULL; 132948a6092fSMaxime Coquelin 133048a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 1331e5707915SGerald Baeza if (id < 0) { 1332e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 1333e5707915SGerald Baeza return NULL; 1334e5707915SGerald Baeza } 133548a6092fSMaxime Coquelin 133648a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 133748a6092fSMaxime Coquelin return NULL; 133848a6092fSMaxime Coquelin 13396fd9fffbSErwan Le Ray stm32_ports[id].hw_flow_control = 13406fd9fffbSErwan Le Ray of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ || 13416fd9fffbSErwan Le Ray of_property_read_bool (np, "uart-has-rtscts"); 134248a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 13434cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 1344d0a6a7bcSErwan Le Ray stm32_ports[id].cr3_irq = 0; 1345e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 134648a6092fSMaxime Coquelin return &stm32_ports[id]; 134748a6092fSMaxime Coquelin } 134848a6092fSMaxime Coquelin 134948a6092fSMaxime Coquelin #ifdef CONFIG_OF 135048a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 1351ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 1352ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 1353270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 135448a6092fSMaxime Coquelin {}, 135548a6092fSMaxime Coquelin }; 135648a6092fSMaxime Coquelin 135748a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 135848a6092fSMaxime Coquelin #endif 135948a6092fSMaxime Coquelin 1360a7770a4bSErwan Le Ray static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port, 1361a7770a4bSErwan Le Ray struct platform_device *pdev) 1362a7770a4bSErwan Le Ray { 1363a7770a4bSErwan Le Ray if (stm32port->rx_buf) 1364a7770a4bSErwan Le Ray dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf, 1365a7770a4bSErwan Le Ray stm32port->rx_dma_buf); 1366a7770a4bSErwan Le Ray } 1367a7770a4bSErwan Le Ray 136856f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port, 136934891872SAlexandre TORGUE struct platform_device *pdev) 137034891872SAlexandre TORGUE { 1371d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 137234891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 137334891872SAlexandre TORGUE struct device *dev = &pdev->dev; 137434891872SAlexandre TORGUE struct dma_slave_config config; 137534891872SAlexandre TORGUE int ret; 137634891872SAlexandre TORGUE 1377e359b441SJohan Hovold /* 1378e359b441SJohan Hovold * Using DMA and threaded handler for the console could lead to 1379e359b441SJohan Hovold * deadlocks. 1380e359b441SJohan Hovold */ 1381e359b441SJohan Hovold if (uart_console(port)) 1382e359b441SJohan Hovold return -ENODEV; 1383e359b441SJohan Hovold 138459bd4eedSTang Bin stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L, 138534891872SAlexandre TORGUE &stm32port->rx_dma_buf, 138634891872SAlexandre TORGUE GFP_KERNEL); 1387a7770a4bSErwan Le Ray if (!stm32port->rx_buf) 1388a7770a4bSErwan Le Ray return -ENOMEM; 138934891872SAlexandre TORGUE 139034891872SAlexandre TORGUE /* Configure DMA channel */ 139134891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 13928e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 139334891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 139434891872SAlexandre TORGUE 139534891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 139634891872SAlexandre TORGUE if (ret < 0) { 139734891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 1398a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32port, pdev); 1399a7770a4bSErwan Le Ray return ret; 140034891872SAlexandre TORGUE } 140134891872SAlexandre TORGUE 140234891872SAlexandre TORGUE return 0; 1403a7770a4bSErwan Le Ray } 140434891872SAlexandre TORGUE 1405a7770a4bSErwan Le Ray static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port, 1406a7770a4bSErwan Le Ray struct platform_device *pdev) 1407a7770a4bSErwan Le Ray { 1408a7770a4bSErwan Le Ray if (stm32port->tx_buf) 1409a7770a4bSErwan Le Ray dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf, 1410a7770a4bSErwan Le Ray stm32port->tx_dma_buf); 141134891872SAlexandre TORGUE } 141234891872SAlexandre TORGUE 141356f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port, 141434891872SAlexandre TORGUE struct platform_device *pdev) 141534891872SAlexandre TORGUE { 1416d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 141734891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 141834891872SAlexandre TORGUE struct device *dev = &pdev->dev; 141934891872SAlexandre TORGUE struct dma_slave_config config; 142034891872SAlexandre TORGUE int ret; 142134891872SAlexandre TORGUE 142234891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 142334891872SAlexandre TORGUE 142459bd4eedSTang Bin stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L, 142534891872SAlexandre TORGUE &stm32port->tx_dma_buf, 142634891872SAlexandre TORGUE GFP_KERNEL); 1427a7770a4bSErwan Le Ray if (!stm32port->tx_buf) 1428a7770a4bSErwan Le Ray return -ENOMEM; 142934891872SAlexandre TORGUE 143034891872SAlexandre TORGUE /* Configure DMA channel */ 143134891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 14328e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 143334891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 143434891872SAlexandre TORGUE 143534891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 143634891872SAlexandre TORGUE if (ret < 0) { 143734891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 1438a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32port, pdev); 1439a7770a4bSErwan Le Ray return ret; 144034891872SAlexandre TORGUE } 144134891872SAlexandre TORGUE 144234891872SAlexandre TORGUE return 0; 144334891872SAlexandre TORGUE } 144434891872SAlexandre TORGUE 144556f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev) 144648a6092fSMaxime Coquelin { 144748a6092fSMaxime Coquelin struct stm32_port *stm32port; 1448ada8618fSAlexandre TORGUE int ret; 144948a6092fSMaxime Coquelin 145056f9a76cSErwan Le Ray stm32port = stm32_usart_of_get_port(pdev); 145148a6092fSMaxime Coquelin if (!stm32port) 145248a6092fSMaxime Coquelin return -ENODEV; 145348a6092fSMaxime Coquelin 1454d825f0beSStephen Boyd stm32port->info = of_device_get_match_data(&pdev->dev); 1455d825f0beSStephen Boyd if (!stm32port->info) 1456ada8618fSAlexandre TORGUE return -EINVAL; 1457ada8618fSAlexandre TORGUE 145856f9a76cSErwan Le Ray ret = stm32_usart_init_port(stm32port, pdev); 145948a6092fSMaxime Coquelin if (ret) 146048a6092fSMaxime Coquelin return ret; 146148a6092fSMaxime Coquelin 14623d530017SAlexandre Torgue if (stm32port->wakeup_src) { 14633d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, true); 14643d530017SAlexandre Torgue ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq); 14655297f274SErwan Le Ray if (ret) 1466a7770a4bSErwan Le Ray goto err_deinit_port; 1467270e5a74SFabrice Gasnier } 1468270e5a74SFabrice Gasnier 1469a7770a4bSErwan Le Ray stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx"); 1470a7770a4bSErwan Le Ray if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER) { 1471a7770a4bSErwan Le Ray ret = -EPROBE_DEFER; 1472a7770a4bSErwan Le Ray goto err_wakeirq; 1473a7770a4bSErwan Le Ray } 1474a7770a4bSErwan Le Ray /* Fall back in interrupt mode for any non-deferral error */ 1475a7770a4bSErwan Le Ray if (IS_ERR(stm32port->rx_ch)) 1476a7770a4bSErwan Le Ray stm32port->rx_ch = NULL; 147734891872SAlexandre TORGUE 1478a7770a4bSErwan Le Ray stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx"); 1479a7770a4bSErwan Le Ray if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) { 1480a7770a4bSErwan Le Ray ret = -EPROBE_DEFER; 1481a7770a4bSErwan Le Ray goto err_dma_rx; 1482a7770a4bSErwan Le Ray } 1483a7770a4bSErwan Le Ray /* Fall back in interrupt mode for any non-deferral error */ 1484a7770a4bSErwan Le Ray if (IS_ERR(stm32port->tx_ch)) 1485a7770a4bSErwan Le Ray stm32port->tx_ch = NULL; 1486a7770a4bSErwan Le Ray 1487a7770a4bSErwan Le Ray if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) { 1488a7770a4bSErwan Le Ray /* Fall back in interrupt mode */ 1489a7770a4bSErwan Le Ray dma_release_channel(stm32port->rx_ch); 1490a7770a4bSErwan Le Ray stm32port->rx_ch = NULL; 1491a7770a4bSErwan Le Ray } 1492a7770a4bSErwan Le Ray 1493a7770a4bSErwan Le Ray if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) { 1494a7770a4bSErwan Le Ray /* Fall back in interrupt mode */ 1495a7770a4bSErwan Le Ray dma_release_channel(stm32port->tx_ch); 1496a7770a4bSErwan Le Ray stm32port->tx_ch = NULL; 1497a7770a4bSErwan Le Ray } 1498a7770a4bSErwan Le Ray 1499a7770a4bSErwan Le Ray if (!stm32port->rx_ch) 1500a7770a4bSErwan Le Ray dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n"); 1501a7770a4bSErwan Le Ray if (!stm32port->tx_ch) 1502a7770a4bSErwan Le Ray dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n"); 150334891872SAlexandre TORGUE 150448a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 150548a6092fSMaxime Coquelin 1506fb6dcef6SErwan Le Ray pm_runtime_get_noresume(&pdev->dev); 1507fb6dcef6SErwan Le Ray pm_runtime_set_active(&pdev->dev); 1508fb6dcef6SErwan Le Ray pm_runtime_enable(&pdev->dev); 150987fd0741SErwan Le Ray 151087fd0741SErwan Le Ray ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 151187fd0741SErwan Le Ray if (ret) 151287fd0741SErwan Le Ray goto err_port; 151387fd0741SErwan Le Ray 1514fb6dcef6SErwan Le Ray pm_runtime_put_sync(&pdev->dev); 1515fb6dcef6SErwan Le Ray 151648a6092fSMaxime Coquelin return 0; 1517ada80043SFabrice Gasnier 151887fd0741SErwan Le Ray err_port: 151987fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 152087fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 152187fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 152287fd0741SErwan Le Ray 152387fd0741SErwan Le Ray if (stm32port->tx_ch) { 1524a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32port, pdev); 152587fd0741SErwan Le Ray dma_release_channel(stm32port->tx_ch); 152687fd0741SErwan Le Ray } 152787fd0741SErwan Le Ray 1528a7770a4bSErwan Le Ray if (stm32port->rx_ch) 1529a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32port, pdev); 153087fd0741SErwan Le Ray 1531a7770a4bSErwan Le Ray err_dma_rx: 1532a7770a4bSErwan Le Ray if (stm32port->rx_ch) 1533a7770a4bSErwan Le Ray dma_release_channel(stm32port->rx_ch); 1534a7770a4bSErwan Le Ray 1535a7770a4bSErwan Le Ray err_wakeirq: 15363d530017SAlexandre Torgue if (stm32port->wakeup_src) 15375297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 15385297f274SErwan Le Ray 1539a7770a4bSErwan Le Ray err_deinit_port: 15403d530017SAlexandre Torgue if (stm32port->wakeup_src) 15413d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, false); 1542270e5a74SFabrice Gasnier 154397f3a085SErwan Le Ray stm32_usart_deinit_port(stm32port); 1544ada80043SFabrice Gasnier 1545ada80043SFabrice Gasnier return ret; 154648a6092fSMaxime Coquelin } 154748a6092fSMaxime Coquelin 154856f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev) 154948a6092fSMaxime Coquelin { 155048a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1551511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1552d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1553fb6dcef6SErwan Le Ray int err; 155433bb2f6aSErwan Le Ray u32 cr3; 1555fb6dcef6SErwan Le Ray 1556fb6dcef6SErwan Le Ray pm_runtime_get_sync(&pdev->dev); 155787fd0741SErwan Le Ray err = uart_remove_one_port(&stm32_usart_driver, port); 155887fd0741SErwan Le Ray if (err) 155987fd0741SErwan Le Ray return(err); 156087fd0741SErwan Le Ray 156187fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 156287fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 156387fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 156434891872SAlexandre TORGUE 156533bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE); 156633bb2f6aSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 156733bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_EIE; 156833bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_DMAR; 156933bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_DDRE; 157033bb2f6aSErwan Le Ray writel_relaxed(cr3, port->membase + ofs->cr3); 157134891872SAlexandre TORGUE 157287fd0741SErwan Le Ray if (stm32_port->tx_ch) { 157387fd0741SErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 1574a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32_port, pdev); 157534891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 157687fd0741SErwan Le Ray } 157734891872SAlexandre TORGUE 1578a7770a4bSErwan Le Ray if (stm32_port->rx_ch) { 1579a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32_port, pdev); 1580a7770a4bSErwan Le Ray dma_release_channel(stm32_port->rx_ch); 1581a7770a4bSErwan Le Ray } 1582a7770a4bSErwan Le Ray 1583a7770a4bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 1584511c7b1bSAlexandre TORGUE 15853d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 15865297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1587270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 15885297f274SErwan Le Ray } 1589270e5a74SFabrice Gasnier 159097f3a085SErwan Le Ray stm32_usart_deinit_port(stm32_port); 159148a6092fSMaxime Coquelin 159287fd0741SErwan Le Ray return 0; 159348a6092fSMaxime Coquelin } 159448a6092fSMaxime Coquelin 159548a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 159656f9a76cSErwan Le Ray static void stm32_usart_console_putchar(struct uart_port *port, int ch) 159748a6092fSMaxime Coquelin { 1598ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1599d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1600ada8618fSAlexandre TORGUE 1601ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 160248a6092fSMaxime Coquelin cpu_relax(); 160348a6092fSMaxime Coquelin 1604ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 160548a6092fSMaxime Coquelin } 160648a6092fSMaxime Coquelin 160756f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s, 160892fc0023SErwan Le Ray unsigned int cnt) 160948a6092fSMaxime Coquelin { 161048a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1611ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1612d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1613d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 161448a6092fSMaxime Coquelin unsigned long flags; 161548a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 161648a6092fSMaxime Coquelin int locked = 1; 161748a6092fSMaxime Coquelin 1618cea37afdSJohan Hovold if (oops_in_progress) 1619cea37afdSJohan Hovold locked = spin_trylock_irqsave(&port->lock, flags); 162048a6092fSMaxime Coquelin else 1621cea37afdSJohan Hovold spin_lock_irqsave(&port->lock, flags); 162248a6092fSMaxime Coquelin 162387f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1624ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 162548a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 162687f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1627ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 162848a6092fSMaxime Coquelin 162956f9a76cSErwan Le Ray uart_console_write(port, s, cnt, stm32_usart_console_putchar); 163048a6092fSMaxime Coquelin 163148a6092fSMaxime Coquelin /* Restore interrupt state */ 1632ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 163348a6092fSMaxime Coquelin 163448a6092fSMaxime Coquelin if (locked) 1635cea37afdSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 163648a6092fSMaxime Coquelin } 163748a6092fSMaxime Coquelin 163856f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options) 163948a6092fSMaxime Coquelin { 164048a6092fSMaxime Coquelin struct stm32_port *stm32port; 164148a6092fSMaxime Coquelin int baud = 9600; 164248a6092fSMaxime Coquelin int bits = 8; 164348a6092fSMaxime Coquelin int parity = 'n'; 164448a6092fSMaxime Coquelin int flow = 'n'; 164548a6092fSMaxime Coquelin 164648a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 164748a6092fSMaxime Coquelin return -ENODEV; 164848a6092fSMaxime Coquelin 164948a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 165048a6092fSMaxime Coquelin 165148a6092fSMaxime Coquelin /* 165248a6092fSMaxime Coquelin * This driver does not support early console initialization 165348a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 165448a6092fSMaxime Coquelin * this to be called during the uart port registration when the 165548a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 165648a6092fSMaxime Coquelin */ 165792fc0023SErwan Le Ray if (stm32port->port.mapbase == 0 || !stm32port->port.membase) 165848a6092fSMaxime Coquelin return -ENXIO; 165948a6092fSMaxime Coquelin 166048a6092fSMaxime Coquelin if (options) 166148a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 166248a6092fSMaxime Coquelin 166348a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 166448a6092fSMaxime Coquelin } 166548a6092fSMaxime Coquelin 166648a6092fSMaxime Coquelin static struct console stm32_console = { 166748a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 166848a6092fSMaxime Coquelin .device = uart_console_device, 166956f9a76cSErwan Le Ray .write = stm32_usart_console_write, 167056f9a76cSErwan Le Ray .setup = stm32_usart_console_setup, 167148a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 167248a6092fSMaxime Coquelin .index = -1, 167348a6092fSMaxime Coquelin .data = &stm32_usart_driver, 167448a6092fSMaxime Coquelin }; 167548a6092fSMaxime Coquelin 167648a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 167748a6092fSMaxime Coquelin 167848a6092fSMaxime Coquelin #else 167948a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 168048a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 168148a6092fSMaxime Coquelin 168248a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 168348a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 168448a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 168548a6092fSMaxime Coquelin .major = 0, 168648a6092fSMaxime Coquelin .minor = 0, 168748a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 168848a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 168948a6092fSMaxime Coquelin }; 169048a6092fSMaxime Coquelin 16916eeb348cSErwan Le Ray static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port, 1692fe94347dSErwan Le Ray bool enable) 1693270e5a74SFabrice Gasnier { 1694270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1695d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 16966eeb348cSErwan Le Ray struct tty_port *tport = &port->state->port; 16976eeb348cSErwan Le Ray int ret; 1698*6333a485SErwan Le Ray unsigned int size; 1699*6333a485SErwan Le Ray unsigned long flags; 1700270e5a74SFabrice Gasnier 17016eeb348cSErwan Le Ray if (!stm32_port->wakeup_src || !tty_port_initialized(tport)) 17026eeb348cSErwan Le Ray return 0; 1703270e5a74SFabrice Gasnier 170412761869SErwan Le Ray /* 170512761869SErwan Le Ray * Enable low-power wake-up and wake-up irq if argument is set to 170612761869SErwan Le Ray * "enable", disable low-power wake-up and wake-up irq otherwise 170712761869SErwan Le Ray */ 1708270e5a74SFabrice Gasnier if (enable) { 170956f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM); 171012761869SErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE); 17116eeb348cSErwan Le Ray 17126eeb348cSErwan Le Ray /* 17136eeb348cSErwan Le Ray * When DMA is used for reception, it must be disabled before 17146eeb348cSErwan Le Ray * entering low-power mode and re-enabled when exiting from 17156eeb348cSErwan Le Ray * low-power mode. 17166eeb348cSErwan Le Ray */ 17176eeb348cSErwan Le Ray if (stm32_port->rx_ch) { 1718*6333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 1719*6333a485SErwan Le Ray /* Avoid race with RX IRQ when DMAR is cleared */ 17206eeb348cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 1721*6333a485SErwan Le Ray /* Poll data from DMA RX buffer if any */ 1722*6333a485SErwan Le Ray size = stm32_usart_receive_chars(port, true); 1723*6333a485SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 1724*6333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 1725*6333a485SErwan Le Ray if (size) 1726*6333a485SErwan Le Ray tty_flip_buffer_push(tport); 17276eeb348cSErwan Le Ray } 17286eeb348cSErwan Le Ray 17296eeb348cSErwan Le Ray /* Poll data from RX FIFO if any */ 17306eeb348cSErwan Le Ray stm32_usart_receive_chars(port, false); 1731270e5a74SFabrice Gasnier } else { 17326eeb348cSErwan Le Ray if (stm32_port->rx_ch) { 17336eeb348cSErwan Le Ray ret = stm32_usart_start_rx_dma_cyclic(port); 17346eeb348cSErwan Le Ray if (ret) 17356eeb348cSErwan Le Ray return ret; 17366eeb348cSErwan Le Ray } 17376eeb348cSErwan Le Ray 173856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM); 173912761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 1740270e5a74SFabrice Gasnier } 17416eeb348cSErwan Le Ray 17426eeb348cSErwan Le Ray return 0; 1743270e5a74SFabrice Gasnier } 1744270e5a74SFabrice Gasnier 174556f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev) 1746270e5a74SFabrice Gasnier { 1747270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 17486eeb348cSErwan Le Ray int ret; 1749270e5a74SFabrice Gasnier 1750270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1751270e5a74SFabrice Gasnier 17526eeb348cSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) { 17536eeb348cSErwan Le Ray ret = stm32_usart_serial_en_wakeup(port, true); 17546eeb348cSErwan Le Ray if (ret) 17556eeb348cSErwan Le Ray return ret; 17566eeb348cSErwan Le Ray } 1757270e5a74SFabrice Gasnier 175855484fccSErwan Le Ray /* 175955484fccSErwan Le Ray * When "no_console_suspend" is enabled, keep the pinctrl default state 176055484fccSErwan Le Ray * and rely on bootloader stage to restore this state upon resume. 176155484fccSErwan Le Ray * Otherwise, apply the idle or sleep states depending on wakeup 176255484fccSErwan Le Ray * capabilities. 176355484fccSErwan Le Ray */ 176455484fccSErwan Le Ray if (console_suspend_enabled || !uart_console(port)) { 17651631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 176655484fccSErwan Le Ray pinctrl_pm_select_idle_state(dev); 176755484fccSErwan Le Ray else 176894616d9aSErwan Le Ray pinctrl_pm_select_sleep_state(dev); 176955484fccSErwan Le Ray } 177094616d9aSErwan Le Ray 1771270e5a74SFabrice Gasnier return 0; 1772270e5a74SFabrice Gasnier } 1773270e5a74SFabrice Gasnier 177456f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev) 1775270e5a74SFabrice Gasnier { 1776270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 17776eeb348cSErwan Le Ray int ret; 1778270e5a74SFabrice Gasnier 177994616d9aSErwan Le Ray pinctrl_pm_select_default_state(dev); 178094616d9aSErwan Le Ray 17816eeb348cSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) { 17826eeb348cSErwan Le Ray ret = stm32_usart_serial_en_wakeup(port, false); 17836eeb348cSErwan Le Ray if (ret) 17846eeb348cSErwan Le Ray return ret; 17856eeb348cSErwan Le Ray } 1786270e5a74SFabrice Gasnier 1787270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1788270e5a74SFabrice Gasnier } 1789270e5a74SFabrice Gasnier 179056f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev) 1791fb6dcef6SErwan Le Ray { 1792fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1793fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1794fb6dcef6SErwan Le Ray struct stm32_port, port); 1795fb6dcef6SErwan Le Ray 1796fb6dcef6SErwan Le Ray clk_disable_unprepare(stm32port->clk); 1797fb6dcef6SErwan Le Ray 1798fb6dcef6SErwan Le Ray return 0; 1799fb6dcef6SErwan Le Ray } 1800fb6dcef6SErwan Le Ray 180156f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev) 1802fb6dcef6SErwan Le Ray { 1803fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1804fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1805fb6dcef6SErwan Le Ray struct stm32_port, port); 1806fb6dcef6SErwan Le Ray 1807fb6dcef6SErwan Le Ray return clk_prepare_enable(stm32port->clk); 1808fb6dcef6SErwan Le Ray } 1809fb6dcef6SErwan Le Ray 1810270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 181156f9a76cSErwan Le Ray SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend, 181256f9a76cSErwan Le Ray stm32_usart_runtime_resume, NULL) 181356f9a76cSErwan Le Ray SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend, 181456f9a76cSErwan Le Ray stm32_usart_serial_resume) 1815270e5a74SFabrice Gasnier }; 1816270e5a74SFabrice Gasnier 181748a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 181856f9a76cSErwan Le Ray .probe = stm32_usart_serial_probe, 181956f9a76cSErwan Le Ray .remove = stm32_usart_serial_remove, 182048a6092fSMaxime Coquelin .driver = { 182148a6092fSMaxime Coquelin .name = DRIVER_NAME, 1822270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 182348a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 182448a6092fSMaxime Coquelin }, 182548a6092fSMaxime Coquelin }; 182648a6092fSMaxime Coquelin 182756f9a76cSErwan Le Ray static int __init stm32_usart_init(void) 182848a6092fSMaxime Coquelin { 182948a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 183048a6092fSMaxime Coquelin int ret; 183148a6092fSMaxime Coquelin 183248a6092fSMaxime Coquelin pr_info("%s\n", banner); 183348a6092fSMaxime Coquelin 183448a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 183548a6092fSMaxime Coquelin if (ret) 183648a6092fSMaxime Coquelin return ret; 183748a6092fSMaxime Coquelin 183848a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 183948a6092fSMaxime Coquelin if (ret) 184048a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 184148a6092fSMaxime Coquelin 184248a6092fSMaxime Coquelin return ret; 184348a6092fSMaxime Coquelin } 184448a6092fSMaxime Coquelin 184556f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void) 184648a6092fSMaxime Coquelin { 184748a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 184848a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 184948a6092fSMaxime Coquelin } 185048a6092fSMaxime Coquelin 185156f9a76cSErwan Le Ray module_init(stm32_usart_init); 185256f9a76cSErwan Le Ray module_exit(stm32_usart_exit); 185348a6092fSMaxime Coquelin 185448a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 185548a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 185648a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1857