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); 401f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch); 4148a6092fSMaxime Coquelin 4248a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 4348a6092fSMaxime Coquelin { 4448a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 4548a6092fSMaxime Coquelin } 4648a6092fSMaxime Coquelin 4756f9a76cSErwan Le Ray static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits) 4848a6092fSMaxime Coquelin { 4948a6092fSMaxime Coquelin u32 val; 5048a6092fSMaxime Coquelin 5148a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5248a6092fSMaxime Coquelin val |= bits; 5348a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 5448a6092fSMaxime Coquelin } 5548a6092fSMaxime Coquelin 5656f9a76cSErwan Le Ray static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits) 5748a6092fSMaxime Coquelin { 5848a6092fSMaxime Coquelin u32 val; 5948a6092fSMaxime Coquelin 6048a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 6148a6092fSMaxime Coquelin val &= ~bits; 6248a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 6348a6092fSMaxime Coquelin } 6448a6092fSMaxime Coquelin 6556f9a76cSErwan Le Ray static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, 661bcda09dSBich HEMON u32 delay_DDE, u32 baud) 671bcda09dSBich HEMON { 681bcda09dSBich HEMON u32 rs485_deat_dedt; 691bcda09dSBich HEMON u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); 701bcda09dSBich HEMON bool over8; 711bcda09dSBich HEMON 721bcda09dSBich HEMON *cr3 |= USART_CR3_DEM; 731bcda09dSBich HEMON over8 = *cr1 & USART_CR1_OVER8; 741bcda09dSBich HEMON 75*5c5f44e3SIlpo Järvinen *cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 76*5c5f44e3SIlpo Järvinen 771bcda09dSBich HEMON if (over8) 781bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 8; 791bcda09dSBich HEMON else 801bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 16; 811bcda09dSBich HEMON 821bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 831bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 841bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 851bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & 861bcda09dSBich HEMON USART_CR1_DEAT_MASK; 871bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 881bcda09dSBich HEMON 891bcda09dSBich HEMON if (over8) 901bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 8; 911bcda09dSBich HEMON else 921bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 16; 931bcda09dSBich HEMON 941bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 951bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 961bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 971bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & 981bcda09dSBich HEMON USART_CR1_DEDT_MASK; 991bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 1001bcda09dSBich HEMON } 1011bcda09dSBich HEMON 10256f9a76cSErwan Le Ray static int stm32_usart_config_rs485(struct uart_port *port, 1031bcda09dSBich HEMON struct serial_rs485 *rs485conf) 1041bcda09dSBich HEMON { 1051bcda09dSBich HEMON struct stm32_port *stm32_port = to_stm32_port(port); 106d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 107d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1081bcda09dSBich HEMON u32 usartdiv, baud, cr1, cr3; 1091bcda09dSBich HEMON bool over8; 1101bcda09dSBich HEMON 11156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1121bcda09dSBich HEMON 1131bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RX_DURING_TX; 1141bcda09dSBich HEMON 1151bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 1161bcda09dSBich HEMON cr1 = readl_relaxed(port->membase + ofs->cr1); 1171bcda09dSBich HEMON cr3 = readl_relaxed(port->membase + ofs->cr3); 1181bcda09dSBich HEMON usartdiv = readl_relaxed(port->membase + ofs->brr); 1191bcda09dSBich HEMON usartdiv = usartdiv & GENMASK(15, 0); 1201bcda09dSBich HEMON over8 = cr1 & USART_CR1_OVER8; 1211bcda09dSBich HEMON 1221bcda09dSBich HEMON if (over8) 1231bcda09dSBich HEMON usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) 1241bcda09dSBich HEMON << USART_BRR_04_R_SHIFT; 1251bcda09dSBich HEMON 1261bcda09dSBich HEMON baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); 12756f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 1281bcda09dSBich HEMON rs485conf->delay_rts_before_send, 12956f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 13056f9a76cSErwan Le Ray baud); 1311bcda09dSBich HEMON 132f633eb29SLino Sanfilippo if (rs485conf->flags & SER_RS485_RTS_ON_SEND) 1331bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 134f633eb29SLino Sanfilippo else 1351bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 1361bcda09dSBich HEMON 1371bcda09dSBich HEMON writel_relaxed(cr3, port->membase + ofs->cr3); 1381bcda09dSBich HEMON writel_relaxed(cr1, port->membase + ofs->cr1); 1391bcda09dSBich HEMON } else { 14056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, 14156f9a76cSErwan Le Ray USART_CR3_DEM | USART_CR3_DEP); 14256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, 1431bcda09dSBich HEMON USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 1441bcda09dSBich HEMON } 1451bcda09dSBich HEMON 14656f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1471bcda09dSBich HEMON 1481bcda09dSBich HEMON return 0; 1491bcda09dSBich HEMON } 1501bcda09dSBich HEMON 15156f9a76cSErwan Le Ray static int stm32_usart_init_rs485(struct uart_port *port, 1521bcda09dSBich HEMON struct platform_device *pdev) 1531bcda09dSBich HEMON { 1541bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 1551bcda09dSBich HEMON 1561bcda09dSBich HEMON rs485conf->flags = 0; 1571bcda09dSBich HEMON rs485conf->delay_rts_before_send = 0; 1581bcda09dSBich HEMON rs485conf->delay_rts_after_send = 0; 1591bcda09dSBich HEMON 1601bcda09dSBich HEMON if (!pdev->dev.of_node) 1611bcda09dSBich HEMON return -ENODEV; 1621bcda09dSBich HEMON 163c150c0f3SLukas Wunner return uart_get_rs485_mode(port); 1641bcda09dSBich HEMON } 1651bcda09dSBich HEMON 16633bb2f6aSErwan Le Ray static bool stm32_usart_rx_dma_enabled(struct uart_port *port) 16734891872SAlexandre TORGUE { 16834891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 169d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 17033bb2f6aSErwan Le Ray 17133bb2f6aSErwan Le Ray if (!stm32_port->rx_ch) 17233bb2f6aSErwan Le Ray return false; 17333bb2f6aSErwan Le Ray 17433bb2f6aSErwan Le Ray return !!(readl_relaxed(port->membase + ofs->cr3) & USART_CR3_DMAR); 17533bb2f6aSErwan Le Ray } 17633bb2f6aSErwan Le Ray 17733bb2f6aSErwan Le Ray /* Return true when data is pending (in pio mode), and false when no data is pending. */ 17833bb2f6aSErwan Le Ray static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr) 17933bb2f6aSErwan Le Ray { 18033bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 18133bb2f6aSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 18234891872SAlexandre TORGUE 18334891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 18433bb2f6aSErwan Le Ray /* Get pending characters in RDR or FIFO */ 18533bb2f6aSErwan Le Ray if (*sr & USART_SR_RXNE) { 18633bb2f6aSErwan Le Ray /* Get all pending characters from the RDR or the FIFO when using interrupts */ 18733bb2f6aSErwan Le Ray if (!stm32_usart_rx_dma_enabled(port)) 18833bb2f6aSErwan Le Ray return true; 18934891872SAlexandre TORGUE 19033bb2f6aSErwan Le Ray /* Handle only RX data errors when using DMA */ 19133bb2f6aSErwan Le Ray if (*sr & USART_SR_ERR_MASK) 19233bb2f6aSErwan Le Ray return true; 19334891872SAlexandre TORGUE } 19434891872SAlexandre TORGUE 19533bb2f6aSErwan Le Ray return false; 19633bb2f6aSErwan Le Ray } 19733bb2f6aSErwan Le Ray 19833bb2f6aSErwan Le Ray static unsigned long stm32_usart_get_char_pio(struct uart_port *port) 19934891872SAlexandre TORGUE { 20034891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 201d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 20234891872SAlexandre TORGUE unsigned long c; 20334891872SAlexandre TORGUE 2046c5962f3SErwan Le Ray c = readl_relaxed(port->membase + ofs->rdr); 20533bb2f6aSErwan Le Ray /* Apply RDR data mask */ 2066c5962f3SErwan Le Ray c &= stm32_port->rdr_mask; 2076c5962f3SErwan Le Ray 2086c5962f3SErwan Le Ray return c; 20934891872SAlexandre TORGUE } 21034891872SAlexandre TORGUE 2116333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port) 21248a6092fSMaxime Coquelin { 213ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 214d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 21533bb2f6aSErwan Le Ray unsigned long c; 2166333a485SErwan Le Ray unsigned int size = 0; 21748a6092fSMaxime Coquelin u32 sr; 21848a6092fSMaxime Coquelin char flag; 21948a6092fSMaxime Coquelin 22033bb2f6aSErwan Le Ray while (stm32_usart_pending_rx_pio(port, &sr)) { 22148a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22248a6092fSMaxime Coquelin flag = TTY_NORMAL; 22348a6092fSMaxime Coquelin 2244f01d833SErwan Le Ray /* 2254f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2264f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2274f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2284f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2294f01d833SErwan Le Ray * and clear status bits of the next rx data. 2304f01d833SErwan Le Ray * 2314f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2324f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 2334f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 2344f01d833SErwan Le Ray */ 2354f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 2361250ed71SFabrice Gasnier writel_relaxed(sr & USART_SR_ERR_MASK, 2371250ed71SFabrice Gasnier port->membase + ofs->icr); 2384f01d833SErwan Le Ray 23933bb2f6aSErwan Le Ray c = stm32_usart_get_char_pio(port); 2404f01d833SErwan Le Ray port->icount.rx++; 2416333a485SErwan Le Ray size++; 24248a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 2434f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 24448a6092fSMaxime Coquelin port->icount.overrun++; 24548a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 24648a6092fSMaxime Coquelin port->icount.parity++; 24748a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 2484f01d833SErwan Le Ray /* Break detection if character is null */ 2494f01d833SErwan Le Ray if (!c) { 2504f01d833SErwan Le Ray port->icount.brk++; 2514f01d833SErwan Le Ray if (uart_handle_break(port)) 2524f01d833SErwan Le Ray continue; 2534f01d833SErwan Le Ray } else { 25448a6092fSMaxime Coquelin port->icount.frame++; 25548a6092fSMaxime Coquelin } 2564f01d833SErwan Le Ray } 25748a6092fSMaxime Coquelin 25848a6092fSMaxime Coquelin sr &= port->read_status_mask; 25948a6092fSMaxime Coquelin 2604f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26148a6092fSMaxime Coquelin flag = TTY_PARITY; 2624f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 2634f01d833SErwan Le Ray if (!c) 2644f01d833SErwan Le Ray flag = TTY_BREAK; 2654f01d833SErwan Le Ray else 26648a6092fSMaxime Coquelin flag = TTY_FRAME; 26748a6092fSMaxime Coquelin } 2684f01d833SErwan Le Ray } 26948a6092fSMaxime Coquelin 270cea37afdSJohan Hovold if (uart_prepare_sysrq_char(port, c)) 27148a6092fSMaxime Coquelin continue; 27248a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27348a6092fSMaxime Coquelin } 2746333a485SErwan Le Ray 2756333a485SErwan Le Ray return size; 27633bb2f6aSErwan Le Ray } 27733bb2f6aSErwan Le Ray 27833bb2f6aSErwan Le Ray static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size) 27933bb2f6aSErwan Le Ray { 28033bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 28133bb2f6aSErwan Le Ray struct tty_port *ttyport = &stm32_port->port.state->port; 28233bb2f6aSErwan Le Ray unsigned char *dma_start; 28333bb2f6aSErwan Le Ray int dma_count, i; 28433bb2f6aSErwan Le Ray 28533bb2f6aSErwan Le Ray dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res); 28633bb2f6aSErwan Le Ray 28733bb2f6aSErwan Le Ray /* 28833bb2f6aSErwan Le Ray * Apply rdr_mask on buffer in order to mask parity bit. 28933bb2f6aSErwan Le Ray * This loop is useless in cs8 mode because DMA copies only 29033bb2f6aSErwan Le Ray * 8 bits and already ignores parity bit. 29133bb2f6aSErwan Le Ray */ 29233bb2f6aSErwan Le Ray if (!(stm32_port->rdr_mask == (BIT(8) - 1))) 29333bb2f6aSErwan Le Ray for (i = 0; i < dma_size; i++) 29433bb2f6aSErwan Le Ray *(dma_start + i) &= stm32_port->rdr_mask; 29533bb2f6aSErwan Le Ray 29633bb2f6aSErwan Le Ray dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size); 29733bb2f6aSErwan Le Ray port->icount.rx += dma_count; 29833bb2f6aSErwan Le Ray if (dma_count != dma_size) 29933bb2f6aSErwan Le Ray port->icount.buf_overrun++; 30033bb2f6aSErwan Le Ray stm32_port->last_res -= dma_count; 30133bb2f6aSErwan Le Ray if (stm32_port->last_res == 0) 30233bb2f6aSErwan Le Ray stm32_port->last_res = RX_BUF_L; 30333bb2f6aSErwan Le Ray } 30433bb2f6aSErwan Le Ray 3056333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port) 30633bb2f6aSErwan Le Ray { 30733bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 3086333a485SErwan Le Ray unsigned int dma_size, size = 0; 30933bb2f6aSErwan Le Ray 31033bb2f6aSErwan Le Ray /* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */ 31133bb2f6aSErwan Le Ray if (stm32_port->rx_dma_state.residue > stm32_port->last_res) { 31233bb2f6aSErwan Le Ray /* Conditional first part: from last_res to end of DMA buffer */ 31333bb2f6aSErwan Le Ray dma_size = stm32_port->last_res; 31433bb2f6aSErwan Le Ray stm32_usart_push_buffer_dma(port, dma_size); 3156333a485SErwan Le Ray size = dma_size; 31633bb2f6aSErwan Le Ray } 31733bb2f6aSErwan Le Ray 31833bb2f6aSErwan Le Ray dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue; 31933bb2f6aSErwan Le Ray stm32_usart_push_buffer_dma(port, dma_size); 3206333a485SErwan Le Ray size += dma_size; 3216333a485SErwan Le Ray 3226333a485SErwan Le Ray return size; 32333bb2f6aSErwan Le Ray } 32433bb2f6aSErwan Le Ray 3256333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush) 32633bb2f6aSErwan Le Ray { 32733bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 32833bb2f6aSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 32933bb2f6aSErwan Le Ray enum dma_status rx_dma_status; 33033bb2f6aSErwan Le Ray u32 sr; 3316333a485SErwan Le Ray unsigned int size = 0; 33233bb2f6aSErwan Le Ray 3336333a485SErwan Le Ray if (stm32_usart_rx_dma_enabled(port) || force_dma_flush) { 33433bb2f6aSErwan Le Ray rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch, 33533bb2f6aSErwan Le Ray stm32_port->rx_ch->cookie, 33633bb2f6aSErwan Le Ray &stm32_port->rx_dma_state); 33733bb2f6aSErwan Le Ray if (rx_dma_status == DMA_IN_PROGRESS) { 33833bb2f6aSErwan Le Ray /* Empty DMA buffer */ 3396333a485SErwan Le Ray size = stm32_usart_receive_chars_dma(port); 34033bb2f6aSErwan Le Ray sr = readl_relaxed(port->membase + ofs->isr); 34133bb2f6aSErwan Le Ray if (sr & USART_SR_ERR_MASK) { 34233bb2f6aSErwan Le Ray /* Disable DMA request line */ 34333bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 34433bb2f6aSErwan Le Ray 34533bb2f6aSErwan Le Ray /* Switch to PIO mode to handle the errors */ 3466333a485SErwan Le Ray size += stm32_usart_receive_chars_pio(port); 34733bb2f6aSErwan Le Ray 34833bb2f6aSErwan Le Ray /* Switch back to DMA mode */ 34933bb2f6aSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 35033bb2f6aSErwan Le Ray } 35133bb2f6aSErwan Le Ray } else { 35233bb2f6aSErwan Le Ray /* Disable RX DMA */ 35333bb2f6aSErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 35433bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 35533bb2f6aSErwan Le Ray /* Fall back to interrupt mode */ 35633bb2f6aSErwan Le Ray dev_dbg(port->dev, "DMA error, fallback to irq mode\n"); 3576333a485SErwan Le Ray size = stm32_usart_receive_chars_pio(port); 35833bb2f6aSErwan Le Ray } 35933bb2f6aSErwan Le Ray } else { 3606333a485SErwan Le Ray size = stm32_usart_receive_chars_pio(port); 36133bb2f6aSErwan Le Ray } 36248a6092fSMaxime Coquelin 3636333a485SErwan Le Ray return size; 36448a6092fSMaxime Coquelin } 36548a6092fSMaxime Coquelin 3669a135f16SValentin Caron static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port) 3679a135f16SValentin Caron { 3689a135f16SValentin Caron dmaengine_terminate_async(stm32_port->tx_ch); 3699a135f16SValentin Caron stm32_port->tx_dma_busy = false; 3709a135f16SValentin Caron } 3719a135f16SValentin Caron 3729a135f16SValentin Caron static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port) 3739a135f16SValentin Caron { 3749a135f16SValentin Caron /* 3759a135f16SValentin Caron * We cannot use the function "dmaengine_tx_status" to know the 3769a135f16SValentin Caron * status of DMA. This function does not show if the "dma complete" 3779a135f16SValentin Caron * callback of the DMA transaction has been called. So we prefer 3789a135f16SValentin Caron * to use "tx_dma_busy" flag to prevent dual DMA transaction at the 3799a135f16SValentin Caron * same time. 3809a135f16SValentin Caron */ 3819a135f16SValentin Caron return stm32_port->tx_dma_busy; 3829a135f16SValentin Caron } 3839a135f16SValentin Caron 3849a135f16SValentin Caron static bool stm32_usart_tx_dma_enabled(struct stm32_port *stm32_port) 3859a135f16SValentin Caron { 3869a135f16SValentin Caron const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 3879a135f16SValentin Caron 3889a135f16SValentin Caron return !!(readl_relaxed(stm32_port->port.membase + ofs->cr3) & USART_CR3_DMAT); 3899a135f16SValentin Caron } 3909a135f16SValentin Caron 39156f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg) 39234891872SAlexandre TORGUE { 39334891872SAlexandre TORGUE struct uart_port *port = arg; 39434891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 395d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 396f16b90c2SErwan Le Ray unsigned long flags; 39734891872SAlexandre TORGUE 39856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 3999a135f16SValentin Caron stm32_usart_tx_dma_terminate(stm32port); 40034891872SAlexandre TORGUE 40134891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 402f16b90c2SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 40356f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 404f16b90c2SErwan Le Ray spin_unlock_irqrestore(&port->lock, flags); 40534891872SAlexandre TORGUE } 40634891872SAlexandre TORGUE 40756f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port) 408d075719eSErwan Le Ray { 409d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 410d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 411d075719eSErwan Le Ray 412d075719eSErwan Le Ray /* 413d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 414d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 415d075719eSErwan Le Ray */ 4162aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 41756f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 418d075719eSErwan Le Ray else 41956f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 420d075719eSErwan Le Ray } 421d075719eSErwan Le Ray 422d7c76716SMarek Vasut static void stm32_usart_tc_interrupt_enable(struct uart_port *port) 423d7c76716SMarek Vasut { 424d7c76716SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 425d7c76716SMarek Vasut const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 426d7c76716SMarek Vasut 427d7c76716SMarek Vasut stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TCIE); 428d7c76716SMarek Vasut } 429d7c76716SMarek Vasut 43033bb2f6aSErwan Le Ray static void stm32_usart_rx_dma_complete(void *arg) 43133bb2f6aSErwan Le Ray { 43233bb2f6aSErwan Le Ray struct uart_port *port = arg; 4336333a485SErwan Le Ray struct tty_port *tport = &port->state->port; 4346333a485SErwan Le Ray unsigned int size; 4356333a485SErwan Le Ray unsigned long flags; 43633bb2f6aSErwan Le Ray 4376333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 4386333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 4396333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 4406333a485SErwan Le Ray if (size) 4416333a485SErwan Le Ray tty_flip_buffer_push(tport); 44233bb2f6aSErwan Le Ray } 44333bb2f6aSErwan Le Ray 44456f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port) 445d075719eSErwan Le Ray { 446d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 447d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 448d075719eSErwan Le Ray 4492aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 45056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 451d075719eSErwan Le Ray else 45256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 453d075719eSErwan Le Ray } 454d075719eSErwan Le Ray 455d7c76716SMarek Vasut static void stm32_usart_tc_interrupt_disable(struct uart_port *port) 456d7c76716SMarek Vasut { 457d7c76716SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 458d7c76716SMarek Vasut const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 459d7c76716SMarek Vasut 460d7c76716SMarek Vasut stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TCIE); 461d7c76716SMarek Vasut } 462d7c76716SMarek Vasut 4633bcea529SMarek Vasut static void stm32_usart_rs485_rts_enable(struct uart_port *port) 4643bcea529SMarek Vasut { 4653bcea529SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 4663bcea529SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 4673bcea529SMarek Vasut 4683bcea529SMarek Vasut if (stm32_port->hw_flow_control || 4693bcea529SMarek Vasut !(rs485conf->flags & SER_RS485_ENABLED)) 4703bcea529SMarek Vasut return; 4713bcea529SMarek Vasut 4723bcea529SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 4733bcea529SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 4743bcea529SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 4753bcea529SMarek Vasut } else { 4763bcea529SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 4773bcea529SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 4783bcea529SMarek Vasut } 4793bcea529SMarek Vasut } 4803bcea529SMarek Vasut 4813bcea529SMarek Vasut static void stm32_usart_rs485_rts_disable(struct uart_port *port) 4823bcea529SMarek Vasut { 4833bcea529SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 4843bcea529SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 4853bcea529SMarek Vasut 4863bcea529SMarek Vasut if (stm32_port->hw_flow_control || 4873bcea529SMarek Vasut !(rs485conf->flags & SER_RS485_ENABLED)) 4883bcea529SMarek Vasut return; 4893bcea529SMarek Vasut 4903bcea529SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 4913bcea529SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 4923bcea529SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 4933bcea529SMarek Vasut } else { 4943bcea529SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 4953bcea529SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 4963bcea529SMarek Vasut } 4973bcea529SMarek Vasut } 4983bcea529SMarek Vasut 49956f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port) 50034891872SAlexandre TORGUE { 50134891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 502d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 50334891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 50434891872SAlexandre TORGUE 5059a135f16SValentin Caron if (stm32_usart_tx_dma_enabled(stm32_port)) 50656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 50734891872SAlexandre TORGUE 5085d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 5095d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 5105d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 5115d9176edSErwan Le Ray break; 51234891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 51334891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 51434891872SAlexandre TORGUE port->icount.tx++; 51534891872SAlexandre TORGUE } 51634891872SAlexandre TORGUE 5175d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 5185d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 51956f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 5205d9176edSErwan Le Ray else 52156f9a76cSErwan Le Ray stm32_usart_tx_interrupt_enable(port); 5225d9176edSErwan Le Ray } 5235d9176edSErwan Le Ray 52456f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port) 52534891872SAlexandre TORGUE { 52634891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 527d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 52834891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 52934891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 530195437d1SValentin Caron unsigned int count; 53134891872SAlexandre TORGUE 5329a135f16SValentin Caron if (stm32_usart_tx_dma_started(stm32port)) { 5339a135f16SValentin Caron if (!stm32_usart_tx_dma_enabled(stm32port)) 5349a135f16SValentin Caron stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 53534891872SAlexandre TORGUE return; 5369a135f16SValentin Caron } 53734891872SAlexandre TORGUE 53834891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 53934891872SAlexandre TORGUE 54034891872SAlexandre TORGUE if (count > TX_BUF_L) 54134891872SAlexandre TORGUE count = TX_BUF_L; 54234891872SAlexandre TORGUE 54334891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 54434891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 54534891872SAlexandre TORGUE } else { 54634891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 54734891872SAlexandre TORGUE size_t two; 54834891872SAlexandre TORGUE 54934891872SAlexandre TORGUE if (one > count) 55034891872SAlexandre TORGUE one = count; 55134891872SAlexandre TORGUE two = count - one; 55234891872SAlexandre TORGUE 55334891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 55434891872SAlexandre TORGUE if (two) 55534891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 55634891872SAlexandre TORGUE } 55734891872SAlexandre TORGUE 55834891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 55934891872SAlexandre TORGUE stm32port->tx_dma_buf, 56034891872SAlexandre TORGUE count, 56134891872SAlexandre TORGUE DMA_MEM_TO_DEV, 56234891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 56334891872SAlexandre TORGUE 564e7997f7fSErwan Le Ray if (!desc) 565e7997f7fSErwan Le Ray goto fallback_err; 56634891872SAlexandre TORGUE 5679a135f16SValentin Caron /* 5689a135f16SValentin Caron * Set "tx_dma_busy" flag. This flag will be released when 5699a135f16SValentin Caron * dmaengine_terminate_async will be called. This flag helps 5709a135f16SValentin Caron * transmit_chars_dma not to start another DMA transaction 5719a135f16SValentin Caron * if the callback of the previous is not yet called. 5729a135f16SValentin Caron */ 5739a135f16SValentin Caron stm32port->tx_dma_busy = true; 5749a135f16SValentin Caron 57556f9a76cSErwan Le Ray desc->callback = stm32_usart_tx_dma_complete; 57634891872SAlexandre TORGUE desc->callback_param = port; 57734891872SAlexandre TORGUE 57834891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 579e7997f7fSErwan Le Ray if (dma_submit_error(dmaengine_submit(desc))) { 580e7997f7fSErwan Le Ray /* dma no yet started, safe to free resources */ 5819a135f16SValentin Caron stm32_usart_tx_dma_terminate(stm32port); 582e7997f7fSErwan Le Ray goto fallback_err; 583e7997f7fSErwan Le Ray } 58434891872SAlexandre TORGUE 58534891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 58634891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 58734891872SAlexandre TORGUE 58856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 58934891872SAlexandre TORGUE 59034891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 59134891872SAlexandre TORGUE port->icount.tx += count; 592e7997f7fSErwan Le Ray return; 593e7997f7fSErwan Le Ray 594e7997f7fSErwan Le Ray fallback_err: 59556f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 59634891872SAlexandre TORGUE } 59734891872SAlexandre TORGUE 59856f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port) 59948a6092fSMaxime Coquelin { 600ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 601d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 60248a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 603d3d079bdSValentin Caron u32 isr; 604d3d079bdSValentin Caron int ret; 60548a6092fSMaxime Coquelin 606d7c76716SMarek Vasut if (!stm32_port->hw_flow_control && 607d7c76716SMarek Vasut port->rs485.flags & SER_RS485_ENABLED) { 608d7c76716SMarek Vasut stm32_port->txdone = false; 609d7c76716SMarek Vasut stm32_usart_tc_interrupt_disable(port); 610d7c76716SMarek Vasut stm32_usart_rs485_rts_enable(port); 611d7c76716SMarek Vasut } 612d7c76716SMarek Vasut 61348a6092fSMaxime Coquelin if (port->x_char) { 6149a135f16SValentin Caron if (stm32_usart_tx_dma_started(stm32_port) && 6159a135f16SValentin Caron stm32_usart_tx_dma_enabled(stm32_port)) 61656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 617d3d079bdSValentin Caron 618d3d079bdSValentin Caron /* Check that TDR is empty before filling FIFO */ 619d3d079bdSValentin Caron ret = 620d3d079bdSValentin Caron readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 621d3d079bdSValentin Caron isr, 622d3d079bdSValentin Caron (isr & USART_SR_TXE), 623d3d079bdSValentin Caron 10, 1000); 624d3d079bdSValentin Caron if (ret) 625d3d079bdSValentin Caron dev_warn(port->dev, "1 character may be erased\n"); 626d3d079bdSValentin Caron 627ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 62848a6092fSMaxime Coquelin port->x_char = 0; 62948a6092fSMaxime Coquelin port->icount.tx++; 6309a135f16SValentin Caron if (stm32_usart_tx_dma_started(stm32_port)) 63156f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 63248a6092fSMaxime Coquelin return; 63348a6092fSMaxime Coquelin } 63448a6092fSMaxime Coquelin 635b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 63656f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 63748a6092fSMaxime Coquelin return; 63848a6092fSMaxime Coquelin } 63948a6092fSMaxime Coquelin 64064c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 64156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC); 64264c32eabSErwan Le Ray else 6431250ed71SFabrice Gasnier writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr); 64464c32eabSErwan Le Ray 64534891872SAlexandre TORGUE if (stm32_port->tx_ch) 64656f9a76cSErwan Le Ray stm32_usart_transmit_chars_dma(port); 64734891872SAlexandre TORGUE else 64856f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 64948a6092fSMaxime Coquelin 65048a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 65148a6092fSMaxime Coquelin uart_write_wakeup(port); 65248a6092fSMaxime Coquelin 653d7c76716SMarek Vasut if (uart_circ_empty(xmit)) { 65456f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 655d7c76716SMarek Vasut if (!stm32_port->hw_flow_control && 656d7c76716SMarek Vasut port->rs485.flags & SER_RS485_ENABLED) { 657d7c76716SMarek Vasut stm32_port->txdone = true; 658d7c76716SMarek Vasut stm32_usart_tc_interrupt_enable(port); 659d7c76716SMarek Vasut } 660d7c76716SMarek Vasut } 66148a6092fSMaxime Coquelin } 66248a6092fSMaxime Coquelin 66356f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) 66448a6092fSMaxime Coquelin { 66548a6092fSMaxime Coquelin struct uart_port *port = ptr; 66612761869SErwan Le Ray struct tty_port *tport = &port->state->port; 667ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 668d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 66948a6092fSMaxime Coquelin u32 sr; 6706333a485SErwan Le Ray unsigned int size; 67148a6092fSMaxime Coquelin 672ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 67348a6092fSMaxime Coquelin 674d7c76716SMarek Vasut if (!stm32_port->hw_flow_control && 675d7c76716SMarek Vasut port->rs485.flags & SER_RS485_ENABLED && 676d7c76716SMarek Vasut (sr & USART_SR_TC)) { 677d7c76716SMarek Vasut stm32_usart_tc_interrupt_disable(port); 678d7c76716SMarek Vasut stm32_usart_rs485_rts_disable(port); 679d7c76716SMarek Vasut } 680d7c76716SMarek Vasut 6814cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 6824cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 6834cc0ed62SErwan Le Ray port->membase + ofs->icr); 6844cc0ed62SErwan Le Ray 68512761869SErwan Le Ray if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) { 68612761869SErwan Le Ray /* Clear wake up flag and disable wake up interrupt */ 687270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 688270e5a74SFabrice Gasnier port->membase + ofs->icr); 68912761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 69012761869SErwan Le Ray if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 69112761869SErwan Le Ray pm_wakeup_event(tport->tty->dev, 0); 69212761869SErwan Le Ray } 693270e5a74SFabrice Gasnier 69433bb2f6aSErwan Le Ray /* 69533bb2f6aSErwan Le Ray * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request 69633bb2f6aSErwan Le Ray * line has been masked by HW and rx data are stacking in FIFO. 69733bb2f6aSErwan Le Ray */ 698d1ec8a2eSErwan Le Ray if (!stm32_port->throttled) { 69933bb2f6aSErwan Le Ray if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_enabled(port)) || 700d1ec8a2eSErwan Le Ray ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_enabled(port))) { 7016333a485SErwan Le Ray spin_lock(&port->lock); 7026333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 7036333a485SErwan Le Ray uart_unlock_and_check_sysrq(port); 7046333a485SErwan Le Ray if (size) 7056333a485SErwan Le Ray tty_flip_buffer_push(tport); 706d1ec8a2eSErwan Le Ray } 707d1ec8a2eSErwan Le Ray } 70848a6092fSMaxime Coquelin 709ad767681SErwan Le Ray if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) { 710ad767681SErwan Le Ray spin_lock(&port->lock); 71156f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 71201d32d71SAlexandre TORGUE spin_unlock(&port->lock); 713ad767681SErwan Le Ray } 71401d32d71SAlexandre TORGUE 71533bb2f6aSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) 71634891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 71734891872SAlexandre TORGUE else 71834891872SAlexandre TORGUE return IRQ_HANDLED; 71934891872SAlexandre TORGUE } 72034891872SAlexandre TORGUE 72156f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr) 72234891872SAlexandre TORGUE { 72334891872SAlexandre TORGUE struct uart_port *port = ptr; 7246333a485SErwan Le Ray struct tty_port *tport = &port->state->port; 725d1ec8a2eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 7266333a485SErwan Le Ray unsigned int size; 7276333a485SErwan Le Ray unsigned long flags; 72834891872SAlexandre TORGUE 729cc58d0a3SErwan Le Ray /* Receiver timeout irq for DMA RX */ 7306333a485SErwan Le Ray if (!stm32_port->throttled) { 7316333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 7326333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 7336333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 7346333a485SErwan Le Ray if (size) 7356333a485SErwan Le Ray tty_flip_buffer_push(tport); 7366333a485SErwan Le Ray } 73734891872SAlexandre TORGUE 73848a6092fSMaxime Coquelin return IRQ_HANDLED; 73948a6092fSMaxime Coquelin } 74048a6092fSMaxime Coquelin 74156f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port) 74248a6092fSMaxime Coquelin { 743ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 744d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 745ada8618fSAlexandre TORGUE 7463db1d524SErwan Le Ray if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC) 7473db1d524SErwan Le Ray return TIOCSER_TEMT; 7483db1d524SErwan Le Ray 7493db1d524SErwan Le Ray return 0; 75048a6092fSMaxime Coquelin } 75148a6092fSMaxime Coquelin 75256f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl) 75348a6092fSMaxime Coquelin { 754ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 755d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 756ada8618fSAlexandre TORGUE 75748a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 75856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE); 75948a6092fSMaxime Coquelin else 76056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 7616cf61b9bSManivannan Sadhasivam 7626cf61b9bSManivannan Sadhasivam mctrl_gpio_set(stm32_port->gpios, mctrl); 76348a6092fSMaxime Coquelin } 76448a6092fSMaxime Coquelin 76556f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port) 76648a6092fSMaxime Coquelin { 7676cf61b9bSManivannan Sadhasivam struct stm32_port *stm32_port = to_stm32_port(port); 7686cf61b9bSManivannan Sadhasivam unsigned int ret; 7696cf61b9bSManivannan Sadhasivam 77048a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 7716cf61b9bSManivannan Sadhasivam ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 7726cf61b9bSManivannan Sadhasivam 7736cf61b9bSManivannan Sadhasivam return mctrl_gpio_get(stm32_port->gpios, &ret); 7746cf61b9bSManivannan Sadhasivam } 7756cf61b9bSManivannan Sadhasivam 77656f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port) 7776cf61b9bSManivannan Sadhasivam { 7786cf61b9bSManivannan Sadhasivam mctrl_gpio_enable_ms(to_stm32_port(port)->gpios); 7796cf61b9bSManivannan Sadhasivam } 7806cf61b9bSManivannan Sadhasivam 78156f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port) 7826cf61b9bSManivannan Sadhasivam { 7836cf61b9bSManivannan Sadhasivam mctrl_gpio_disable_ms(to_stm32_port(port)->gpios); 78448a6092fSMaxime Coquelin } 78548a6092fSMaxime Coquelin 78648a6092fSMaxime Coquelin /* Transmit stop */ 78756f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port) 78848a6092fSMaxime Coquelin { 789ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 7902a3bcfe0SValentin Caron const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 791ad0c2748SMarek Vasut 79256f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 7932a3bcfe0SValentin Caron if (stm32_usart_tx_dma_started(stm32_port) && stm32_usart_tx_dma_enabled(stm32_port)) 7942a3bcfe0SValentin Caron stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 795ad0c2748SMarek Vasut 7963bcea529SMarek Vasut stm32_usart_rs485_rts_disable(port); 79748a6092fSMaxime Coquelin } 79848a6092fSMaxime Coquelin 79948a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 80056f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port) 80148a6092fSMaxime Coquelin { 80248a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 80348a6092fSMaxime Coquelin 804d7c76716SMarek Vasut if (uart_circ_empty(xmit) && !port->x_char) { 805d7c76716SMarek Vasut stm32_usart_rs485_rts_disable(port); 80648a6092fSMaxime Coquelin return; 807d7c76716SMarek Vasut } 80848a6092fSMaxime Coquelin 8093bcea529SMarek Vasut stm32_usart_rs485_rts_enable(port); 810ad0c2748SMarek Vasut 81156f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 81248a6092fSMaxime Coquelin } 81348a6092fSMaxime Coquelin 8143d82be8bSErwan Le Ray /* Flush the transmit buffer. */ 8153d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port) 8163d82be8bSErwan Le Ray { 8173d82be8bSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 8183d82be8bSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 8193d82be8bSErwan Le Ray 8203d82be8bSErwan Le Ray if (stm32_port->tx_ch) { 8219a135f16SValentin Caron stm32_usart_tx_dma_terminate(stm32_port); 8223d82be8bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 8233d82be8bSErwan Le Ray } 8243d82be8bSErwan Le Ray } 8253d82be8bSErwan Le Ray 82648a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 82756f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port) 82848a6092fSMaxime Coquelin { 829ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 830d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 83148a6092fSMaxime Coquelin unsigned long flags; 83248a6092fSMaxime Coquelin 83348a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 834d1ec8a2eSErwan Le Ray 835d1ec8a2eSErwan Le Ray /* 836d1ec8a2eSErwan Le Ray * Disable DMA request line if enabled, so the RX data gets queued into the FIFO. 837d1ec8a2eSErwan Le Ray * Hardware flow control is triggered when RX FIFO is full. 838d1ec8a2eSErwan Le Ray */ 839d1ec8a2eSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) 840d1ec8a2eSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 841d1ec8a2eSErwan Le Ray 84256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 843d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 84456f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 845d0a6a7bcSErwan Le Ray 846d1ec8a2eSErwan Le Ray stm32_port->throttled = true; 84748a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 84848a6092fSMaxime Coquelin } 84948a6092fSMaxime Coquelin 85048a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 85156f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port) 85248a6092fSMaxime Coquelin { 853ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 854d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 85548a6092fSMaxime Coquelin unsigned long flags; 85648a6092fSMaxime Coquelin 85748a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 85856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 859d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 86056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq); 861d0a6a7bcSErwan Le Ray 862d1ec8a2eSErwan Le Ray /* 863d1ec8a2eSErwan Le Ray * Switch back to DMA mode (re-enable DMA request line). 864d1ec8a2eSErwan Le Ray * Hardware flow control is stopped when FIFO is not full any more. 865d1ec8a2eSErwan Le Ray */ 866d1ec8a2eSErwan Le Ray if (stm32_port->rx_ch) 867d1ec8a2eSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 868d1ec8a2eSErwan Le Ray 869d1ec8a2eSErwan Le Ray stm32_port->throttled = false; 87048a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 87148a6092fSMaxime Coquelin } 87248a6092fSMaxime Coquelin 87348a6092fSMaxime Coquelin /* Receive stop */ 87456f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port) 87548a6092fSMaxime Coquelin { 876ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 877d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 878ada8618fSAlexandre TORGUE 879e0abc903SErwan Le Ray /* Disable DMA request line. */ 880e0abc903SErwan Le Ray if (stm32_port->rx_ch) 881e0abc903SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 882e0abc903SErwan Le Ray 88356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 884d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 88556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 88648a6092fSMaxime Coquelin } 88748a6092fSMaxime Coquelin 88848a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 88956f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state) 89048a6092fSMaxime Coquelin { 89148a6092fSMaxime Coquelin } 89248a6092fSMaxime Coquelin 8936eeb348cSErwan Le Ray static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port) 8946eeb348cSErwan Le Ray { 8956eeb348cSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 8966eeb348cSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 8976eeb348cSErwan Le Ray struct dma_async_tx_descriptor *desc; 8986eeb348cSErwan Le Ray int ret; 8996eeb348cSErwan Le Ray 9006eeb348cSErwan Le Ray stm32_port->last_res = RX_BUF_L; 9016eeb348cSErwan Le Ray /* Prepare a DMA cyclic transaction */ 9026eeb348cSErwan Le Ray desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch, 9036eeb348cSErwan Le Ray stm32_port->rx_dma_buf, 9046eeb348cSErwan Le Ray RX_BUF_L, RX_BUF_P, 9056eeb348cSErwan Le Ray DMA_DEV_TO_MEM, 9066eeb348cSErwan Le Ray DMA_PREP_INTERRUPT); 9076eeb348cSErwan Le Ray if (!desc) { 9086eeb348cSErwan Le Ray dev_err(port->dev, "rx dma prep cyclic failed\n"); 9096eeb348cSErwan Le Ray return -ENODEV; 9106eeb348cSErwan Le Ray } 9116eeb348cSErwan Le Ray 9126eeb348cSErwan Le Ray desc->callback = stm32_usart_rx_dma_complete; 9136eeb348cSErwan Le Ray desc->callback_param = port; 9146eeb348cSErwan Le Ray 9156eeb348cSErwan Le Ray /* Push current DMA transaction in the pending queue */ 9166eeb348cSErwan Le Ray ret = dma_submit_error(dmaengine_submit(desc)); 9176eeb348cSErwan Le Ray if (ret) { 9186eeb348cSErwan Le Ray dmaengine_terminate_sync(stm32_port->rx_ch); 9196eeb348cSErwan Le Ray return ret; 9206eeb348cSErwan Le Ray } 9216eeb348cSErwan Le Ray 9226eeb348cSErwan Le Ray /* Issue pending DMA requests */ 9236eeb348cSErwan Le Ray dma_async_issue_pending(stm32_port->rx_ch); 9246eeb348cSErwan Le Ray 9256eeb348cSErwan Le Ray /* 9266eeb348cSErwan Le Ray * DMA request line not re-enabled at resume when port is throttled. 9276eeb348cSErwan Le Ray * It will be re-enabled by unthrottle ops. 9286eeb348cSErwan Le Ray */ 9296eeb348cSErwan Le Ray if (!stm32_port->throttled) 9306eeb348cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 9316eeb348cSErwan Le Ray 9326eeb348cSErwan Le Ray return 0; 9336eeb348cSErwan Le Ray } 9346eeb348cSErwan Le Ray 93556f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port) 93648a6092fSMaxime Coquelin { 937ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 938d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 939f4518a8aSErwan Le Ray const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 94048a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 94148a6092fSMaxime Coquelin u32 val; 94248a6092fSMaxime Coquelin int ret; 94348a6092fSMaxime Coquelin 94456f9a76cSErwan Le Ray ret = request_threaded_irq(port->irq, stm32_usart_interrupt, 94556f9a76cSErwan Le Ray stm32_usart_threaded_interrupt, 946e359b441SJohan Hovold IRQF_ONESHOT | IRQF_NO_SUSPEND, 947e359b441SJohan Hovold name, port); 94848a6092fSMaxime Coquelin if (ret) 94948a6092fSMaxime Coquelin return ret; 95048a6092fSMaxime Coquelin 9513cd66593SMartin Devera if (stm32_port->swap) { 9523cd66593SMartin Devera val = readl_relaxed(port->membase + ofs->cr2); 9533cd66593SMartin Devera val |= USART_CR2_SWAP; 9543cd66593SMartin Devera writel_relaxed(val, port->membase + ofs->cr2); 9553cd66593SMartin Devera } 9563cd66593SMartin Devera 95784872dc4SErwan Le Ray /* RX FIFO Flush */ 95884872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 959315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr); 96048a6092fSMaxime Coquelin 961e0abc903SErwan Le Ray if (stm32_port->rx_ch) { 9626eeb348cSErwan Le Ray ret = stm32_usart_start_rx_dma_cyclic(port); 963e0abc903SErwan Le Ray if (ret) { 9646eeb348cSErwan Le Ray free_irq(port->irq, port); 9656eeb348cSErwan Le Ray return ret; 966e0abc903SErwan Le Ray } 967e0abc903SErwan Le Ray } 968d1ec8a2eSErwan Le Ray 96925a8e761SErwan Le Ray /* RX enabling */ 970f4518a8aSErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit); 97156f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, val); 97284872dc4SErwan Le Ray 97348a6092fSMaxime Coquelin return 0; 97448a6092fSMaxime Coquelin } 97548a6092fSMaxime Coquelin 97656f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port) 97748a6092fSMaxime Coquelin { 978ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 979d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 980d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 98164c32eabSErwan Le Ray u32 val, isr; 98264c32eabSErwan Le Ray int ret; 98348a6092fSMaxime Coquelin 9849a135f16SValentin Caron if (stm32_usart_tx_dma_enabled(stm32_port)) 98556a23f93SValentin Caron stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 9869a135f16SValentin Caron 9879a135f16SValentin Caron if (stm32_usart_tx_dma_started(stm32_port)) 9889a135f16SValentin Caron stm32_usart_tx_dma_terminate(stm32_port); 98956a23f93SValentin Caron 9906cf61b9bSManivannan Sadhasivam /* Disable modem control interrupts */ 99156f9a76cSErwan Le Ray stm32_usart_disable_ms(port); 9926cf61b9bSManivannan Sadhasivam 9934cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 9944cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 99587f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 996351a762aSGerald Baeza if (stm32_port->fifoen) 997351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 99864c32eabSErwan Le Ray 99964c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 100064c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 100164c32eabSErwan Le Ray 10, 100000); 100264c32eabSErwan Le Ray 1003c31c3ea0SErwan Le Ray /* Send the TC error message only when ISR_TC is not set */ 100464c32eabSErwan Le Ray if (ret) 1005c31c3ea0SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 100664c32eabSErwan Le Ray 1007e0abc903SErwan Le Ray /* Disable RX DMA. */ 1008e0abc903SErwan Le Ray if (stm32_port->rx_ch) 1009e0abc903SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 1010e0abc903SErwan Le Ray 10119f77d192SErwan Le Ray /* flush RX & TX FIFO */ 10129f77d192SErwan Le Ray if (ofs->rqr != UNDEF_REG) 10139f77d192SErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 10149f77d192SErwan Le Ray port->membase + ofs->rqr); 10159f77d192SErwan Le Ray 101656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, val); 101748a6092fSMaxime Coquelin 101848a6092fSMaxime Coquelin free_irq(port->irq, port); 101948a6092fSMaxime Coquelin } 102048a6092fSMaxime Coquelin 102156f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port, 102256f9a76cSErwan Le Ray struct ktermios *termios, 102348a6092fSMaxime Coquelin struct ktermios *old) 102448a6092fSMaxime Coquelin { 102548a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 1026d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1027d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 10281bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 1029c8a9d043SErwan Le Ray unsigned int baud, bits; 103048a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 103148a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 1032f264c6f6SErwan Le Ray u32 cr1, cr2, cr3, isr; 103348a6092fSMaxime Coquelin unsigned long flags; 1034f264c6f6SErwan Le Ray int ret; 103548a6092fSMaxime Coquelin 103648a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 103748a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 103848a6092fSMaxime Coquelin 103948a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 104048a6092fSMaxime Coquelin 104148a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 104248a6092fSMaxime Coquelin 1043f264c6f6SErwan Le Ray ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 1044f264c6f6SErwan Le Ray isr, 1045f264c6f6SErwan Le Ray (isr & USART_SR_TC), 1046f264c6f6SErwan Le Ray 10, 100000); 1047f264c6f6SErwan Le Ray 1048f264c6f6SErwan Le Ray /* Send the TC error message only when ISR_TC is not set. */ 1049f264c6f6SErwan Le Ray if (ret) 1050f264c6f6SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 1051f264c6f6SErwan Le Ray 105248a6092fSMaxime Coquelin /* Stop serial port and reset value */ 1053ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 105448a6092fSMaxime Coquelin 105584872dc4SErwan Le Ray /* flush RX & TX FIFO */ 105684872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 1057315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 1058315e2d8aSErwan Le Ray port->membase + ofs->rqr); 10591bcda09dSBich HEMON 106084872dc4SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 1061351a762aSGerald Baeza if (stm32_port->fifoen) 1062351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 10633cd66593SMartin Devera cr2 = stm32_port->swap ? USART_CR2_SWAP : 0; 106425a8e761SErwan Le Ray 106525a8e761SErwan Le Ray /* Tx and RX FIFO configuration */ 1066d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 106725a8e761SErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE; 106825a8e761SErwan Le Ray if (stm32_port->fifoen) { 10692aa1bbb2SFabrice Gasnier if (stm32_port->txftcfg >= 0) 10702aa1bbb2SFabrice Gasnier cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT; 10712aa1bbb2SFabrice Gasnier if (stm32_port->rxftcfg >= 0) 10722aa1bbb2SFabrice Gasnier cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT; 107325a8e761SErwan Le Ray } 107448a6092fSMaxime Coquelin 107548a6092fSMaxime Coquelin if (cflag & CSTOPB) 107648a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 107748a6092fSMaxime Coquelin 10783ec2ff37SJiri Slaby bits = tty_get_char_size(cflag); 10796c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 1080c8a9d043SErwan Le Ray 108148a6092fSMaxime Coquelin if (cflag & PARENB) { 1082c8a9d043SErwan Le Ray bits++; 108348a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 1084c8a9d043SErwan Le Ray } 1085c8a9d043SErwan Le Ray 1086c8a9d043SErwan Le Ray /* 1087c8a9d043SErwan Le Ray * Word length configuration: 1088c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 1089c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 1090c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 1091c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 1092c8a9d043SErwan Le Ray */ 10931deeda8dSIlpo Järvinen if (bits == 9) { 1094ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 10951deeda8dSIlpo Järvinen } else if ((bits == 7) && cfg->has_7bits_data) { 1096c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 10971deeda8dSIlpo Järvinen } else if (bits != 8) { 1098c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 1099c8a9d043SErwan Le Ray , bits); 11001deeda8dSIlpo Järvinen cflag &= ~CSIZE; 11011deeda8dSIlpo Järvinen cflag |= CS8; 11021deeda8dSIlpo Järvinen termios->c_cflag = cflag; 11031deeda8dSIlpo Järvinen bits = 8; 11041deeda8dSIlpo Järvinen if (cflag & PARENB) { 11051deeda8dSIlpo Järvinen bits++; 11061deeda8dSIlpo Järvinen cr1 |= USART_CR1_M0; 11071deeda8dSIlpo Järvinen } 11081deeda8dSIlpo Järvinen } 110948a6092fSMaxime Coquelin 11104cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 11112aa1bbb2SFabrice Gasnier (stm32_port->fifoen && 11122aa1bbb2SFabrice Gasnier stm32_port->rxftcfg >= 0))) { 11134cc0ed62SErwan Le Ray if (cflag & CSTOPB) 11144cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 11154cc0ed62SErwan Le Ray else 11164cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 11174cc0ed62SErwan Le Ray 11184cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 11194cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 11204cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 11214cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 112233bb2f6aSErwan Le Ray /* 112333bb2f6aSErwan Le Ray * Enable fifo threshold irq in two cases, either when there is no DMA, or when 112433bb2f6aSErwan Le Ray * wake up over usart, from low power until the DMA gets re-enabled by resume. 112533bb2f6aSErwan Le Ray */ 1126d0a6a7bcSErwan Le Ray stm32_port->cr3_irq = USART_CR3_RXFTIE; 11274cc0ed62SErwan Le Ray } 11284cc0ed62SErwan Le Ray 1129d0a6a7bcSErwan Le Ray cr1 |= stm32_port->cr1_irq; 1130d0a6a7bcSErwan Le Ray cr3 |= stm32_port->cr3_irq; 1131d0a6a7bcSErwan Le Ray 113248a6092fSMaxime Coquelin if (cflag & PARODD) 113348a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 113448a6092fSMaxime Coquelin 113548a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 113648a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 113748a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 113835abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 113948a6092fSMaxime Coquelin } 114048a6092fSMaxime Coquelin 114148a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 114248a6092fSMaxime Coquelin 114348a6092fSMaxime Coquelin /* 114448a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 114548a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 114648a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 114748a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 114848a6092fSMaxime Coquelin */ 114948a6092fSMaxime Coquelin if (usartdiv < 16) { 115048a6092fSMaxime Coquelin oversampling = 8; 11511bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 115256f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8); 115348a6092fSMaxime Coquelin } else { 115448a6092fSMaxime Coquelin oversampling = 16; 11551bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 115656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 115748a6092fSMaxime Coquelin } 115848a6092fSMaxime Coquelin 115948a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 116048a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 1161ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 116248a6092fSMaxime Coquelin 116348a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 116448a6092fSMaxime Coquelin 116548a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 116648a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 116748a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 116848a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 11694f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 117048a6092fSMaxime Coquelin 117148a6092fSMaxime Coquelin /* Characters to ignore */ 117248a6092fSMaxime Coquelin port->ignore_status_mask = 0; 117348a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 117448a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 117548a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 11764f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 117748a6092fSMaxime Coquelin /* 117848a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 117948a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 118048a6092fSMaxime Coquelin */ 118148a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 118248a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 118348a6092fSMaxime Coquelin } 118448a6092fSMaxime Coquelin 118548a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 118648a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 118748a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 118848a6092fSMaxime Coquelin 118933bb2f6aSErwan Le Ray if (stm32_port->rx_ch) { 119033bb2f6aSErwan Le Ray /* 119133bb2f6aSErwan Le Ray * Setup DMA to collect only valid data and enable error irqs. 119233bb2f6aSErwan Le Ray * This also enables break reception when using DMA. 119333bb2f6aSErwan Le Ray */ 119433bb2f6aSErwan Le Ray cr1 |= USART_CR1_PEIE; 119533bb2f6aSErwan Le Ray cr3 |= USART_CR3_EIE; 119634891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 119733bb2f6aSErwan Le Ray cr3 |= USART_CR3_DDRE; 119833bb2f6aSErwan Le Ray } 119934891872SAlexandre TORGUE 12001bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 120156f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 12021bcda09dSBich HEMON rs485conf->delay_rts_before_send, 120356f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 120456f9a76cSErwan Le Ray baud); 12051bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 12061bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 12071bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 12081bcda09dSBich HEMON } else { 12091bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 12101bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 12111bcda09dSBich HEMON } 12121bcda09dSBich HEMON 12131bcda09dSBich HEMON } else { 12141bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 12151bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 12161bcda09dSBich HEMON } 12171bcda09dSBich HEMON 121812761869SErwan Le Ray /* Configure wake up from low power on start bit detection */ 12193d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 122012761869SErwan Le Ray cr3 &= ~USART_CR3_WUS_MASK; 122112761869SErwan Le Ray cr3 |= USART_CR3_WUS_START_BIT; 122212761869SErwan Le Ray } 122312761869SErwan Le Ray 1224ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 1225ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 1226ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 122748a6092fSMaxime Coquelin 122856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 122948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 1230436c9793SErwan Le Ray 1231436c9793SErwan Le Ray /* Handle modem control interrupts */ 1232436c9793SErwan Le Ray if (UART_ENABLE_MS(port, termios->c_cflag)) 1233436c9793SErwan Le Ray stm32_usart_enable_ms(port); 1234436c9793SErwan Le Ray else 1235436c9793SErwan Le Ray stm32_usart_disable_ms(port); 123648a6092fSMaxime Coquelin } 123748a6092fSMaxime Coquelin 123856f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port) 123948a6092fSMaxime Coquelin { 124048a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 124148a6092fSMaxime Coquelin } 124248a6092fSMaxime Coquelin 124356f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port) 124448a6092fSMaxime Coquelin { 124548a6092fSMaxime Coquelin } 124648a6092fSMaxime Coquelin 124756f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port) 124848a6092fSMaxime Coquelin { 124948a6092fSMaxime Coquelin return 0; 125048a6092fSMaxime Coquelin } 125148a6092fSMaxime Coquelin 125256f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags) 125348a6092fSMaxime Coquelin { 125448a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 125548a6092fSMaxime Coquelin port->type = PORT_STM32; 125648a6092fSMaxime Coquelin } 125748a6092fSMaxime Coquelin 125848a6092fSMaxime Coquelin static int 125956f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser) 126048a6092fSMaxime Coquelin { 126148a6092fSMaxime Coquelin /* No user changeable parameters */ 126248a6092fSMaxime Coquelin return -EINVAL; 126348a6092fSMaxime Coquelin } 126448a6092fSMaxime Coquelin 126556f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state, 126648a6092fSMaxime Coquelin unsigned int oldstate) 126748a6092fSMaxime Coquelin { 126848a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 126948a6092fSMaxime Coquelin struct stm32_port, port); 1270d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 1271d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32port->info->cfg; 127218ee37e1SJohan Hovold unsigned long flags; 127348a6092fSMaxime Coquelin 127448a6092fSMaxime Coquelin switch (state) { 127548a6092fSMaxime Coquelin case UART_PM_STATE_ON: 1276fb6dcef6SErwan Le Ray pm_runtime_get_sync(port->dev); 127748a6092fSMaxime Coquelin break; 127848a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 127948a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 128056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 128148a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 1282fb6dcef6SErwan Le Ray pm_runtime_put_sync(port->dev); 128348a6092fSMaxime Coquelin break; 128448a6092fSMaxime Coquelin } 128548a6092fSMaxime Coquelin } 128648a6092fSMaxime Coquelin 12871f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL) 12881f507b3aSValentin Caron 12891f507b3aSValentin Caron /* Callbacks for characters polling in debug context (i.e. KGDB). */ 12901f507b3aSValentin Caron static int stm32_usart_poll_init(struct uart_port *port) 12911f507b3aSValentin Caron { 12921f507b3aSValentin Caron struct stm32_port *stm32_port = to_stm32_port(port); 12931f507b3aSValentin Caron 12941f507b3aSValentin Caron return clk_prepare_enable(stm32_port->clk); 12951f507b3aSValentin Caron } 12961f507b3aSValentin Caron 12971f507b3aSValentin Caron static int stm32_usart_poll_get_char(struct uart_port *port) 12981f507b3aSValentin Caron { 12991f507b3aSValentin Caron struct stm32_port *stm32_port = to_stm32_port(port); 13001f507b3aSValentin Caron const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 13011f507b3aSValentin Caron 13021f507b3aSValentin Caron if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE)) 13031f507b3aSValentin Caron return NO_POLL_CHAR; 13041f507b3aSValentin Caron 13051f507b3aSValentin Caron return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask; 13061f507b3aSValentin Caron } 13071f507b3aSValentin Caron 13081f507b3aSValentin Caron static void stm32_usart_poll_put_char(struct uart_port *port, unsigned char ch) 13091f507b3aSValentin Caron { 13101f507b3aSValentin Caron stm32_usart_console_putchar(port, ch); 13111f507b3aSValentin Caron } 13121f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */ 13131f507b3aSValentin Caron 131448a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 131556f9a76cSErwan Le Ray .tx_empty = stm32_usart_tx_empty, 131656f9a76cSErwan Le Ray .set_mctrl = stm32_usart_set_mctrl, 131756f9a76cSErwan Le Ray .get_mctrl = stm32_usart_get_mctrl, 131856f9a76cSErwan Le Ray .stop_tx = stm32_usart_stop_tx, 131956f9a76cSErwan Le Ray .start_tx = stm32_usart_start_tx, 132056f9a76cSErwan Le Ray .throttle = stm32_usart_throttle, 132156f9a76cSErwan Le Ray .unthrottle = stm32_usart_unthrottle, 132256f9a76cSErwan Le Ray .stop_rx = stm32_usart_stop_rx, 132356f9a76cSErwan Le Ray .enable_ms = stm32_usart_enable_ms, 132456f9a76cSErwan Le Ray .break_ctl = stm32_usart_break_ctl, 132556f9a76cSErwan Le Ray .startup = stm32_usart_startup, 132656f9a76cSErwan Le Ray .shutdown = stm32_usart_shutdown, 13273d82be8bSErwan Le Ray .flush_buffer = stm32_usart_flush_buffer, 132856f9a76cSErwan Le Ray .set_termios = stm32_usart_set_termios, 132956f9a76cSErwan Le Ray .pm = stm32_usart_pm, 133056f9a76cSErwan Le Ray .type = stm32_usart_type, 133156f9a76cSErwan Le Ray .release_port = stm32_usart_release_port, 133256f9a76cSErwan Le Ray .request_port = stm32_usart_request_port, 133356f9a76cSErwan Le Ray .config_port = stm32_usart_config_port, 133456f9a76cSErwan Le Ray .verify_port = stm32_usart_verify_port, 13351f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL) 13361f507b3aSValentin Caron .poll_init = stm32_usart_poll_init, 13371f507b3aSValentin Caron .poll_get_char = stm32_usart_poll_get_char, 13381f507b3aSValentin Caron .poll_put_char = stm32_usart_poll_put_char, 13391f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */ 134048a6092fSMaxime Coquelin }; 134148a6092fSMaxime Coquelin 13422aa1bbb2SFabrice Gasnier /* 13432aa1bbb2SFabrice Gasnier * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG) 13442aa1bbb2SFabrice Gasnier * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case, 13452aa1bbb2SFabrice Gasnier * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE. 13462aa1bbb2SFabrice Gasnier * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1. 13472aa1bbb2SFabrice Gasnier */ 13482aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 }; 13492aa1bbb2SFabrice Gasnier 13502aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p, 13512aa1bbb2SFabrice Gasnier int *ftcfg) 13522aa1bbb2SFabrice Gasnier { 13532aa1bbb2SFabrice Gasnier u32 bytes, i; 13542aa1bbb2SFabrice Gasnier 13552aa1bbb2SFabrice Gasnier /* DT option to get RX & TX FIFO threshold (default to 8 bytes) */ 13562aa1bbb2SFabrice Gasnier if (of_property_read_u32(pdev->dev.of_node, p, &bytes)) 13572aa1bbb2SFabrice Gasnier bytes = 8; 13582aa1bbb2SFabrice Gasnier 13592aa1bbb2SFabrice Gasnier for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++) 13602aa1bbb2SFabrice Gasnier if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes) 13612aa1bbb2SFabrice Gasnier break; 13622aa1bbb2SFabrice Gasnier if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg)) 13632aa1bbb2SFabrice Gasnier i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1; 13642aa1bbb2SFabrice Gasnier 13652aa1bbb2SFabrice Gasnier dev_dbg(&pdev->dev, "%s set to %d bytes\n", p, 13662aa1bbb2SFabrice Gasnier stm32h7_usart_fifo_thresh_cfg[i]); 13672aa1bbb2SFabrice Gasnier 13682aa1bbb2SFabrice Gasnier /* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */ 13692aa1bbb2SFabrice Gasnier if (i) 13702aa1bbb2SFabrice Gasnier *ftcfg = i - 1; 13712aa1bbb2SFabrice Gasnier else 13722aa1bbb2SFabrice Gasnier *ftcfg = -EINVAL; 13732aa1bbb2SFabrice Gasnier } 13742aa1bbb2SFabrice Gasnier 137597f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port) 137697f3a085SErwan Le Ray { 137797f3a085SErwan Le Ray clk_disable_unprepare(stm32port->clk); 137897f3a085SErwan Le Ray } 137997f3a085SErwan Le Ray 138056f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port, 138148a6092fSMaxime Coquelin struct platform_device *pdev) 138248a6092fSMaxime Coquelin { 138348a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 138448a6092fSMaxime Coquelin struct resource *res; 1385e0f2a902SErwan Le Ray int ret, irq; 138648a6092fSMaxime Coquelin 1387e0f2a902SErwan Le Ray irq = platform_get_irq(pdev, 0); 1388217b04c6STang Bin if (irq < 0) 1389217b04c6STang Bin return irq; 139092fc0023SErwan Le Ray 139148a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 139248a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 139348a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 139448a6092fSMaxime Coquelin port->dev = &pdev->dev; 1395d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 13969feedaa7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE); 1397e0f2a902SErwan Le Ray port->irq = irq; 139856f9a76cSErwan Le Ray port->rs485_config = stm32_usart_config_rs485; 13997d8f6861SBich HEMON 140056f9a76cSErwan Le Ray ret = stm32_usart_init_rs485(port, pdev); 1401c150c0f3SLukas Wunner if (ret) 1402c150c0f3SLukas Wunner return ret; 14037d8f6861SBich HEMON 14043d530017SAlexandre Torgue stm32port->wakeup_src = stm32port->info->cfg.has_wakeup && 14053d530017SAlexandre Torgue of_property_read_bool(pdev->dev.of_node, "wakeup-source"); 14062c58e560SErwan Le Ray 14073cd66593SMartin Devera stm32port->swap = stm32port->info->cfg.has_swap && 14083cd66593SMartin Devera of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"); 14093cd66593SMartin Devera 1410351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 14112aa1bbb2SFabrice Gasnier if (stm32port->fifoen) { 14122aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "rx-threshold", 14132aa1bbb2SFabrice Gasnier &stm32port->rxftcfg); 14142aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "tx-threshold", 14152aa1bbb2SFabrice Gasnier &stm32port->txftcfg); 14162aa1bbb2SFabrice Gasnier } 141748a6092fSMaxime Coquelin 14183d881e32STang Bin port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 141948a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 142048a6092fSMaxime Coquelin return PTR_ERR(port->membase); 142148a6092fSMaxime Coquelin port->mapbase = res->start; 142248a6092fSMaxime Coquelin 142348a6092fSMaxime Coquelin spin_lock_init(&port->lock); 142448a6092fSMaxime Coquelin 142548a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 142648a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 142748a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 142848a6092fSMaxime Coquelin 142948a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 143048a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 143148a6092fSMaxime Coquelin if (ret) 143248a6092fSMaxime Coquelin return ret; 143348a6092fSMaxime Coquelin 143448a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 1435ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 143648a6092fSMaxime Coquelin ret = -EINVAL; 14376cf61b9bSManivannan Sadhasivam goto err_clk; 1438ada80043SFabrice Gasnier } 143948a6092fSMaxime Coquelin 14406cf61b9bSManivannan Sadhasivam stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0); 14416cf61b9bSManivannan Sadhasivam if (IS_ERR(stm32port->gpios)) { 14426cf61b9bSManivannan Sadhasivam ret = PTR_ERR(stm32port->gpios); 14436cf61b9bSManivannan Sadhasivam goto err_clk; 14446cf61b9bSManivannan Sadhasivam } 14456cf61b9bSManivannan Sadhasivam 14469359369aSErwan Le Ray /* 14479359369aSErwan Le Ray * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts" 14489359369aSErwan Le Ray * properties should not be specified. 14499359369aSErwan Le Ray */ 14506cf61b9bSManivannan Sadhasivam if (stm32port->hw_flow_control) { 14516cf61b9bSManivannan Sadhasivam if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) || 14526cf61b9bSManivannan Sadhasivam mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) { 14536cf61b9bSManivannan Sadhasivam dev_err(&pdev->dev, "Conflicting RTS/CTS config\n"); 14546cf61b9bSManivannan Sadhasivam ret = -EINVAL; 14556cf61b9bSManivannan Sadhasivam goto err_clk; 14566cf61b9bSManivannan Sadhasivam } 14576cf61b9bSManivannan Sadhasivam } 14586cf61b9bSManivannan Sadhasivam 14596cf61b9bSManivannan Sadhasivam return ret; 14606cf61b9bSManivannan Sadhasivam 14616cf61b9bSManivannan Sadhasivam err_clk: 14626cf61b9bSManivannan Sadhasivam clk_disable_unprepare(stm32port->clk); 14636cf61b9bSManivannan Sadhasivam 146448a6092fSMaxime Coquelin return ret; 146548a6092fSMaxime Coquelin } 146648a6092fSMaxime Coquelin 146756f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev) 146848a6092fSMaxime Coquelin { 146948a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 147048a6092fSMaxime Coquelin int id; 147148a6092fSMaxime Coquelin 147248a6092fSMaxime Coquelin if (!np) 147348a6092fSMaxime Coquelin return NULL; 147448a6092fSMaxime Coquelin 147548a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 1476e5707915SGerald Baeza if (id < 0) { 1477e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 1478e5707915SGerald Baeza return NULL; 1479e5707915SGerald Baeza } 148048a6092fSMaxime Coquelin 148148a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 148248a6092fSMaxime Coquelin return NULL; 148348a6092fSMaxime Coquelin 14846fd9fffbSErwan Le Ray stm32_ports[id].hw_flow_control = 14856fd9fffbSErwan Le Ray of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ || 14866fd9fffbSErwan Le Ray of_property_read_bool (np, "uart-has-rtscts"); 148748a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 14884cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 1489d0a6a7bcSErwan Le Ray stm32_ports[id].cr3_irq = 0; 1490e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 149148a6092fSMaxime Coquelin return &stm32_ports[id]; 149248a6092fSMaxime Coquelin } 149348a6092fSMaxime Coquelin 149448a6092fSMaxime Coquelin #ifdef CONFIG_OF 149548a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 1496ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 1497ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 1498270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 149948a6092fSMaxime Coquelin {}, 150048a6092fSMaxime Coquelin }; 150148a6092fSMaxime Coquelin 150248a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 150348a6092fSMaxime Coquelin #endif 150448a6092fSMaxime Coquelin 1505a7770a4bSErwan Le Ray static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port, 1506a7770a4bSErwan Le Ray struct platform_device *pdev) 1507a7770a4bSErwan Le Ray { 1508a7770a4bSErwan Le Ray if (stm32port->rx_buf) 1509a7770a4bSErwan Le Ray dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf, 1510a7770a4bSErwan Le Ray stm32port->rx_dma_buf); 1511a7770a4bSErwan Le Ray } 1512a7770a4bSErwan Le Ray 151356f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port, 151434891872SAlexandre TORGUE struct platform_device *pdev) 151534891872SAlexandre TORGUE { 1516d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 151734891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 151834891872SAlexandre TORGUE struct device *dev = &pdev->dev; 151934891872SAlexandre TORGUE struct dma_slave_config config; 152034891872SAlexandre TORGUE int ret; 152134891872SAlexandre TORGUE 1522e359b441SJohan Hovold /* 1523e359b441SJohan Hovold * Using DMA and threaded handler for the console could lead to 1524e359b441SJohan Hovold * deadlocks. 1525e359b441SJohan Hovold */ 1526e359b441SJohan Hovold if (uart_console(port)) 1527e359b441SJohan Hovold return -ENODEV; 1528e359b441SJohan Hovold 152959bd4eedSTang Bin stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L, 153034891872SAlexandre TORGUE &stm32port->rx_dma_buf, 153134891872SAlexandre TORGUE GFP_KERNEL); 1532a7770a4bSErwan Le Ray if (!stm32port->rx_buf) 1533a7770a4bSErwan Le Ray return -ENOMEM; 153434891872SAlexandre TORGUE 153534891872SAlexandre TORGUE /* Configure DMA channel */ 153634891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 15378e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 153834891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 153934891872SAlexandre TORGUE 154034891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 154134891872SAlexandre TORGUE if (ret < 0) { 154234891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 1543a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32port, pdev); 1544a7770a4bSErwan Le Ray return ret; 154534891872SAlexandre TORGUE } 154634891872SAlexandre TORGUE 154734891872SAlexandre TORGUE return 0; 1548a7770a4bSErwan Le Ray } 154934891872SAlexandre TORGUE 1550a7770a4bSErwan Le Ray static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port, 1551a7770a4bSErwan Le Ray struct platform_device *pdev) 1552a7770a4bSErwan Le Ray { 1553a7770a4bSErwan Le Ray if (stm32port->tx_buf) 1554a7770a4bSErwan Le Ray dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf, 1555a7770a4bSErwan Le Ray stm32port->tx_dma_buf); 155634891872SAlexandre TORGUE } 155734891872SAlexandre TORGUE 155856f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port, 155934891872SAlexandre TORGUE struct platform_device *pdev) 156034891872SAlexandre TORGUE { 1561d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 156234891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 156334891872SAlexandre TORGUE struct device *dev = &pdev->dev; 156434891872SAlexandre TORGUE struct dma_slave_config config; 156534891872SAlexandre TORGUE int ret; 156634891872SAlexandre TORGUE 156759bd4eedSTang Bin stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L, 156834891872SAlexandre TORGUE &stm32port->tx_dma_buf, 156934891872SAlexandre TORGUE GFP_KERNEL); 1570a7770a4bSErwan Le Ray if (!stm32port->tx_buf) 1571a7770a4bSErwan Le Ray return -ENOMEM; 157234891872SAlexandre TORGUE 157334891872SAlexandre TORGUE /* Configure DMA channel */ 157434891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 15758e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 157634891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 157734891872SAlexandre TORGUE 157834891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 157934891872SAlexandre TORGUE if (ret < 0) { 158034891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 1581a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32port, pdev); 1582a7770a4bSErwan Le Ray return ret; 158334891872SAlexandre TORGUE } 158434891872SAlexandre TORGUE 158534891872SAlexandre TORGUE return 0; 158634891872SAlexandre TORGUE } 158734891872SAlexandre TORGUE 158856f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev) 158948a6092fSMaxime Coquelin { 159048a6092fSMaxime Coquelin struct stm32_port *stm32port; 1591ada8618fSAlexandre TORGUE int ret; 159248a6092fSMaxime Coquelin 159356f9a76cSErwan Le Ray stm32port = stm32_usart_of_get_port(pdev); 159448a6092fSMaxime Coquelin if (!stm32port) 159548a6092fSMaxime Coquelin return -ENODEV; 159648a6092fSMaxime Coquelin 1597d825f0beSStephen Boyd stm32port->info = of_device_get_match_data(&pdev->dev); 1598d825f0beSStephen Boyd if (!stm32port->info) 1599ada8618fSAlexandre TORGUE return -EINVAL; 1600ada8618fSAlexandre TORGUE 160156f9a76cSErwan Le Ray ret = stm32_usart_init_port(stm32port, pdev); 160248a6092fSMaxime Coquelin if (ret) 160348a6092fSMaxime Coquelin return ret; 160448a6092fSMaxime Coquelin 16053d530017SAlexandre Torgue if (stm32port->wakeup_src) { 16063d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, true); 16073d530017SAlexandre Torgue ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq); 16085297f274SErwan Le Ray if (ret) 1609a7770a4bSErwan Le Ray goto err_deinit_port; 1610270e5a74SFabrice Gasnier } 1611270e5a74SFabrice Gasnier 1612a7770a4bSErwan Le Ray stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx"); 1613a7770a4bSErwan Le Ray if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER) { 1614a7770a4bSErwan Le Ray ret = -EPROBE_DEFER; 1615a7770a4bSErwan Le Ray goto err_wakeirq; 1616a7770a4bSErwan Le Ray } 1617a7770a4bSErwan Le Ray /* Fall back in interrupt mode for any non-deferral error */ 1618a7770a4bSErwan Le Ray if (IS_ERR(stm32port->rx_ch)) 1619a7770a4bSErwan Le Ray stm32port->rx_ch = NULL; 162034891872SAlexandre TORGUE 1621a7770a4bSErwan Le Ray stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx"); 1622a7770a4bSErwan Le Ray if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) { 1623a7770a4bSErwan Le Ray ret = -EPROBE_DEFER; 1624a7770a4bSErwan Le Ray goto err_dma_rx; 1625a7770a4bSErwan Le Ray } 1626a7770a4bSErwan Le Ray /* Fall back in interrupt mode for any non-deferral error */ 1627a7770a4bSErwan Le Ray if (IS_ERR(stm32port->tx_ch)) 1628a7770a4bSErwan Le Ray stm32port->tx_ch = NULL; 1629a7770a4bSErwan Le Ray 1630a7770a4bSErwan Le Ray if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) { 1631a7770a4bSErwan Le Ray /* Fall back in interrupt mode */ 1632a7770a4bSErwan Le Ray dma_release_channel(stm32port->rx_ch); 1633a7770a4bSErwan Le Ray stm32port->rx_ch = NULL; 1634a7770a4bSErwan Le Ray } 1635a7770a4bSErwan Le Ray 1636a7770a4bSErwan Le Ray if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) { 1637a7770a4bSErwan Le Ray /* Fall back in interrupt mode */ 1638a7770a4bSErwan Le Ray dma_release_channel(stm32port->tx_ch); 1639a7770a4bSErwan Le Ray stm32port->tx_ch = NULL; 1640a7770a4bSErwan Le Ray } 1641a7770a4bSErwan Le Ray 1642a7770a4bSErwan Le Ray if (!stm32port->rx_ch) 1643a7770a4bSErwan Le Ray dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n"); 1644a7770a4bSErwan Le Ray if (!stm32port->tx_ch) 1645a7770a4bSErwan Le Ray dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n"); 164634891872SAlexandre TORGUE 164748a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 164848a6092fSMaxime Coquelin 1649fb6dcef6SErwan Le Ray pm_runtime_get_noresume(&pdev->dev); 1650fb6dcef6SErwan Le Ray pm_runtime_set_active(&pdev->dev); 1651fb6dcef6SErwan Le Ray pm_runtime_enable(&pdev->dev); 165287fd0741SErwan Le Ray 165387fd0741SErwan Le Ray ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 165487fd0741SErwan Le Ray if (ret) 165587fd0741SErwan Le Ray goto err_port; 165687fd0741SErwan Le Ray 1657fb6dcef6SErwan Le Ray pm_runtime_put_sync(&pdev->dev); 1658fb6dcef6SErwan Le Ray 165948a6092fSMaxime Coquelin return 0; 1660ada80043SFabrice Gasnier 166187fd0741SErwan Le Ray err_port: 166287fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 166387fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 166487fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 166587fd0741SErwan Le Ray 166687fd0741SErwan Le Ray if (stm32port->tx_ch) { 1667a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32port, pdev); 166887fd0741SErwan Le Ray dma_release_channel(stm32port->tx_ch); 166987fd0741SErwan Le Ray } 167087fd0741SErwan Le Ray 1671a7770a4bSErwan Le Ray if (stm32port->rx_ch) 1672a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32port, pdev); 167387fd0741SErwan Le Ray 1674a7770a4bSErwan Le Ray err_dma_rx: 1675a7770a4bSErwan Le Ray if (stm32port->rx_ch) 1676a7770a4bSErwan Le Ray dma_release_channel(stm32port->rx_ch); 1677a7770a4bSErwan Le Ray 1678a7770a4bSErwan Le Ray err_wakeirq: 16793d530017SAlexandre Torgue if (stm32port->wakeup_src) 16805297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 16815297f274SErwan Le Ray 1682a7770a4bSErwan Le Ray err_deinit_port: 16833d530017SAlexandre Torgue if (stm32port->wakeup_src) 16843d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, false); 1685270e5a74SFabrice Gasnier 168697f3a085SErwan Le Ray stm32_usart_deinit_port(stm32port); 1687ada80043SFabrice Gasnier 1688ada80043SFabrice Gasnier return ret; 168948a6092fSMaxime Coquelin } 169048a6092fSMaxime Coquelin 169156f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev) 169248a6092fSMaxime Coquelin { 169348a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1694511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1695d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1696fb6dcef6SErwan Le Ray int err; 169733bb2f6aSErwan Le Ray u32 cr3; 1698fb6dcef6SErwan Le Ray 1699fb6dcef6SErwan Le Ray pm_runtime_get_sync(&pdev->dev); 170087fd0741SErwan Le Ray err = uart_remove_one_port(&stm32_usart_driver, port); 170187fd0741SErwan Le Ray if (err) 170287fd0741SErwan Le Ray return(err); 170387fd0741SErwan Le Ray 170487fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 170587fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 170687fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 170734891872SAlexandre TORGUE 170833bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE); 170933bb2f6aSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 171033bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_EIE; 171133bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_DMAR; 171233bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_DDRE; 171333bb2f6aSErwan Le Ray writel_relaxed(cr3, port->membase + ofs->cr3); 171434891872SAlexandre TORGUE 171587fd0741SErwan Le Ray if (stm32_port->tx_ch) { 1716a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32_port, pdev); 171734891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 171887fd0741SErwan Le Ray } 171934891872SAlexandre TORGUE 1720a7770a4bSErwan Le Ray if (stm32_port->rx_ch) { 1721a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32_port, pdev); 1722a7770a4bSErwan Le Ray dma_release_channel(stm32_port->rx_ch); 1723a7770a4bSErwan Le Ray } 1724a7770a4bSErwan Le Ray 1725a7770a4bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 1726511c7b1bSAlexandre TORGUE 17273d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 17285297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1729270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 17305297f274SErwan Le Ray } 1731270e5a74SFabrice Gasnier 173297f3a085SErwan Le Ray stm32_usart_deinit_port(stm32_port); 173348a6092fSMaxime Coquelin 173487fd0741SErwan Le Ray return 0; 173548a6092fSMaxime Coquelin } 173648a6092fSMaxime Coquelin 17371f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch) 173848a6092fSMaxime Coquelin { 1739ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1740d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 174128fb1a92SValentin Caron u32 isr; 174228fb1a92SValentin Caron int ret; 1743ada8618fSAlexandre TORGUE 174428fb1a92SValentin Caron ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr, 174528fb1a92SValentin Caron (isr & USART_SR_TXE), 100, 174628fb1a92SValentin Caron STM32_USART_TIMEOUT_USEC); 174728fb1a92SValentin Caron if (ret != 0) { 174828fb1a92SValentin Caron dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret); 174928fb1a92SValentin Caron return; 175028fb1a92SValentin Caron } 1751ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 175248a6092fSMaxime Coquelin } 175348a6092fSMaxime Coquelin 17541f507b3aSValentin Caron #ifdef CONFIG_SERIAL_STM32_CONSOLE 175556f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s, 175692fc0023SErwan Le Ray unsigned int cnt) 175748a6092fSMaxime Coquelin { 175848a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1759ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1760d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1761d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 176248a6092fSMaxime Coquelin unsigned long flags; 176348a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 176448a6092fSMaxime Coquelin int locked = 1; 176548a6092fSMaxime Coquelin 1766cea37afdSJohan Hovold if (oops_in_progress) 1767cea37afdSJohan Hovold locked = spin_trylock_irqsave(&port->lock, flags); 176848a6092fSMaxime Coquelin else 1769cea37afdSJohan Hovold spin_lock_irqsave(&port->lock, flags); 177048a6092fSMaxime Coquelin 177187f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1772ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 177348a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 177487f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1775ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 177648a6092fSMaxime Coquelin 177756f9a76cSErwan Le Ray uart_console_write(port, s, cnt, stm32_usart_console_putchar); 177848a6092fSMaxime Coquelin 177948a6092fSMaxime Coquelin /* Restore interrupt state */ 1780ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 178148a6092fSMaxime Coquelin 178248a6092fSMaxime Coquelin if (locked) 1783cea37afdSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 178448a6092fSMaxime Coquelin } 178548a6092fSMaxime Coquelin 178656f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options) 178748a6092fSMaxime Coquelin { 178848a6092fSMaxime Coquelin struct stm32_port *stm32port; 178948a6092fSMaxime Coquelin int baud = 9600; 179048a6092fSMaxime Coquelin int bits = 8; 179148a6092fSMaxime Coquelin int parity = 'n'; 179248a6092fSMaxime Coquelin int flow = 'n'; 179348a6092fSMaxime Coquelin 179448a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 179548a6092fSMaxime Coquelin return -ENODEV; 179648a6092fSMaxime Coquelin 179748a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 179848a6092fSMaxime Coquelin 179948a6092fSMaxime Coquelin /* 180048a6092fSMaxime Coquelin * This driver does not support early console initialization 180148a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 180248a6092fSMaxime Coquelin * this to be called during the uart port registration when the 180348a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 180448a6092fSMaxime Coquelin */ 180592fc0023SErwan Le Ray if (stm32port->port.mapbase == 0 || !stm32port->port.membase) 180648a6092fSMaxime Coquelin return -ENXIO; 180748a6092fSMaxime Coquelin 180848a6092fSMaxime Coquelin if (options) 180948a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 181048a6092fSMaxime Coquelin 181148a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 181248a6092fSMaxime Coquelin } 181348a6092fSMaxime Coquelin 181448a6092fSMaxime Coquelin static struct console stm32_console = { 181548a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 181648a6092fSMaxime Coquelin .device = uart_console_device, 181756f9a76cSErwan Le Ray .write = stm32_usart_console_write, 181856f9a76cSErwan Le Ray .setup = stm32_usart_console_setup, 181948a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 182048a6092fSMaxime Coquelin .index = -1, 182148a6092fSMaxime Coquelin .data = &stm32_usart_driver, 182248a6092fSMaxime Coquelin }; 182348a6092fSMaxime Coquelin 182448a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 182548a6092fSMaxime Coquelin 182648a6092fSMaxime Coquelin #else 182748a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 182848a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 182948a6092fSMaxime Coquelin 18308043b16fSValentin Caron #ifdef CONFIG_SERIAL_EARLYCON 18318043b16fSValentin Caron static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch) 18328043b16fSValentin Caron { 18338043b16fSValentin Caron struct stm32_usart_info *info = port->private_data; 18348043b16fSValentin Caron 18358043b16fSValentin Caron while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE)) 18368043b16fSValentin Caron cpu_relax(); 18378043b16fSValentin Caron 18388043b16fSValentin Caron writel_relaxed(ch, port->membase + info->ofs.tdr); 18398043b16fSValentin Caron } 18408043b16fSValentin Caron 18418043b16fSValentin Caron static void early_stm32_serial_write(struct console *console, const char *s, unsigned int count) 18428043b16fSValentin Caron { 18438043b16fSValentin Caron struct earlycon_device *device = console->data; 18448043b16fSValentin Caron struct uart_port *port = &device->port; 18458043b16fSValentin Caron 18468043b16fSValentin Caron uart_console_write(port, s, count, early_stm32_usart_console_putchar); 18478043b16fSValentin Caron } 18488043b16fSValentin Caron 18498043b16fSValentin Caron static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options) 18508043b16fSValentin Caron { 18518043b16fSValentin Caron if (!(device->port.membase || device->port.iobase)) 18528043b16fSValentin Caron return -ENODEV; 18538043b16fSValentin Caron device->port.private_data = &stm32h7_info; 18548043b16fSValentin Caron device->con->write = early_stm32_serial_write; 18558043b16fSValentin Caron return 0; 18568043b16fSValentin Caron } 18578043b16fSValentin Caron 18588043b16fSValentin Caron static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options) 18598043b16fSValentin Caron { 18608043b16fSValentin Caron if (!(device->port.membase || device->port.iobase)) 18618043b16fSValentin Caron return -ENODEV; 18628043b16fSValentin Caron device->port.private_data = &stm32f7_info; 18638043b16fSValentin Caron device->con->write = early_stm32_serial_write; 18648043b16fSValentin Caron return 0; 18658043b16fSValentin Caron } 18668043b16fSValentin Caron 18678043b16fSValentin Caron static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options) 18688043b16fSValentin Caron { 18698043b16fSValentin Caron if (!(device->port.membase || device->port.iobase)) 18708043b16fSValentin Caron return -ENODEV; 18718043b16fSValentin Caron device->port.private_data = &stm32f4_info; 18728043b16fSValentin Caron device->con->write = early_stm32_serial_write; 18738043b16fSValentin Caron return 0; 18748043b16fSValentin Caron } 18758043b16fSValentin Caron 18768043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup); 18778043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup); 18788043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup); 18798043b16fSValentin Caron #endif /* CONFIG_SERIAL_EARLYCON */ 18808043b16fSValentin Caron 188148a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 188248a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 188348a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 188448a6092fSMaxime Coquelin .major = 0, 188548a6092fSMaxime Coquelin .minor = 0, 188648a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 188748a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 188848a6092fSMaxime Coquelin }; 188948a6092fSMaxime Coquelin 18906eeb348cSErwan Le Ray static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port, 1891fe94347dSErwan Le Ray bool enable) 1892270e5a74SFabrice Gasnier { 1893270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1894d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 18956eeb348cSErwan Le Ray struct tty_port *tport = &port->state->port; 18966eeb348cSErwan Le Ray int ret; 18976333a485SErwan Le Ray unsigned int size; 18986333a485SErwan Le Ray unsigned long flags; 1899270e5a74SFabrice Gasnier 19006eeb348cSErwan Le Ray if (!stm32_port->wakeup_src || !tty_port_initialized(tport)) 19016eeb348cSErwan Le Ray return 0; 1902270e5a74SFabrice Gasnier 190312761869SErwan Le Ray /* 190412761869SErwan Le Ray * Enable low-power wake-up and wake-up irq if argument is set to 190512761869SErwan Le Ray * "enable", disable low-power wake-up and wake-up irq otherwise 190612761869SErwan Le Ray */ 1907270e5a74SFabrice Gasnier if (enable) { 190856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM); 190912761869SErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE); 19107547d9abSErwan Le Ray mctrl_gpio_enable_irq_wake(stm32_port->gpios); 19116eeb348cSErwan Le Ray 19126eeb348cSErwan Le Ray /* 19136eeb348cSErwan Le Ray * When DMA is used for reception, it must be disabled before 19146eeb348cSErwan Le Ray * entering low-power mode and re-enabled when exiting from 19156eeb348cSErwan Le Ray * low-power mode. 19166eeb348cSErwan Le Ray */ 19176eeb348cSErwan Le Ray if (stm32_port->rx_ch) { 19186333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 19196333a485SErwan Le Ray /* Avoid race with RX IRQ when DMAR is cleared */ 19206eeb348cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 19216333a485SErwan Le Ray /* Poll data from DMA RX buffer if any */ 19226333a485SErwan Le Ray size = stm32_usart_receive_chars(port, true); 19236333a485SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 19246333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 19256333a485SErwan Le Ray if (size) 19266333a485SErwan Le Ray tty_flip_buffer_push(tport); 19276eeb348cSErwan Le Ray } 19286eeb348cSErwan Le Ray 19296eeb348cSErwan Le Ray /* Poll data from RX FIFO if any */ 19306eeb348cSErwan Le Ray stm32_usart_receive_chars(port, false); 1931270e5a74SFabrice Gasnier } else { 19326eeb348cSErwan Le Ray if (stm32_port->rx_ch) { 19336eeb348cSErwan Le Ray ret = stm32_usart_start_rx_dma_cyclic(port); 19346eeb348cSErwan Le Ray if (ret) 19356eeb348cSErwan Le Ray return ret; 19366eeb348cSErwan Le Ray } 19377547d9abSErwan Le Ray mctrl_gpio_disable_irq_wake(stm32_port->gpios); 193856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM); 193912761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 1940270e5a74SFabrice Gasnier } 19416eeb348cSErwan Le Ray 19426eeb348cSErwan Le Ray return 0; 1943270e5a74SFabrice Gasnier } 1944270e5a74SFabrice Gasnier 194556f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev) 1946270e5a74SFabrice Gasnier { 1947270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 19486eeb348cSErwan Le Ray int ret; 1949270e5a74SFabrice Gasnier 1950270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1951270e5a74SFabrice Gasnier 19526eeb348cSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) { 19536eeb348cSErwan Le Ray ret = stm32_usart_serial_en_wakeup(port, true); 19546eeb348cSErwan Le Ray if (ret) 19556eeb348cSErwan Le Ray return ret; 19566eeb348cSErwan Le Ray } 1957270e5a74SFabrice Gasnier 195855484fccSErwan Le Ray /* 195955484fccSErwan Le Ray * When "no_console_suspend" is enabled, keep the pinctrl default state 196055484fccSErwan Le Ray * and rely on bootloader stage to restore this state upon resume. 196155484fccSErwan Le Ray * Otherwise, apply the idle or sleep states depending on wakeup 196255484fccSErwan Le Ray * capabilities. 196355484fccSErwan Le Ray */ 196455484fccSErwan Le Ray if (console_suspend_enabled || !uart_console(port)) { 19651631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 196655484fccSErwan Le Ray pinctrl_pm_select_idle_state(dev); 196755484fccSErwan Le Ray else 196894616d9aSErwan Le Ray pinctrl_pm_select_sleep_state(dev); 196955484fccSErwan Le Ray } 197094616d9aSErwan Le Ray 1971270e5a74SFabrice Gasnier return 0; 1972270e5a74SFabrice Gasnier } 1973270e5a74SFabrice Gasnier 197456f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev) 1975270e5a74SFabrice Gasnier { 1976270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 19776eeb348cSErwan Le Ray int ret; 1978270e5a74SFabrice Gasnier 197994616d9aSErwan Le Ray pinctrl_pm_select_default_state(dev); 198094616d9aSErwan Le Ray 19816eeb348cSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) { 19826eeb348cSErwan Le Ray ret = stm32_usart_serial_en_wakeup(port, false); 19836eeb348cSErwan Le Ray if (ret) 19846eeb348cSErwan Le Ray return ret; 19856eeb348cSErwan Le Ray } 1986270e5a74SFabrice Gasnier 1987270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1988270e5a74SFabrice Gasnier } 1989270e5a74SFabrice Gasnier 199056f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev) 1991fb6dcef6SErwan Le Ray { 1992fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1993fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1994fb6dcef6SErwan Le Ray struct stm32_port, port); 1995fb6dcef6SErwan Le Ray 1996fb6dcef6SErwan Le Ray clk_disable_unprepare(stm32port->clk); 1997fb6dcef6SErwan Le Ray 1998fb6dcef6SErwan Le Ray return 0; 1999fb6dcef6SErwan Le Ray } 2000fb6dcef6SErwan Le Ray 200156f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev) 2002fb6dcef6SErwan Le Ray { 2003fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 2004fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 2005fb6dcef6SErwan Le Ray struct stm32_port, port); 2006fb6dcef6SErwan Le Ray 2007fb6dcef6SErwan Le Ray return clk_prepare_enable(stm32port->clk); 2008fb6dcef6SErwan Le Ray } 2009fb6dcef6SErwan Le Ray 2010270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 201156f9a76cSErwan Le Ray SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend, 201256f9a76cSErwan Le Ray stm32_usart_runtime_resume, NULL) 201356f9a76cSErwan Le Ray SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend, 201456f9a76cSErwan Le Ray stm32_usart_serial_resume) 2015270e5a74SFabrice Gasnier }; 2016270e5a74SFabrice Gasnier 201748a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 201856f9a76cSErwan Le Ray .probe = stm32_usart_serial_probe, 201956f9a76cSErwan Le Ray .remove = stm32_usart_serial_remove, 202048a6092fSMaxime Coquelin .driver = { 202148a6092fSMaxime Coquelin .name = DRIVER_NAME, 2022270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 202348a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 202448a6092fSMaxime Coquelin }, 202548a6092fSMaxime Coquelin }; 202648a6092fSMaxime Coquelin 202756f9a76cSErwan Le Ray static int __init stm32_usart_init(void) 202848a6092fSMaxime Coquelin { 202948a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 203048a6092fSMaxime Coquelin int ret; 203148a6092fSMaxime Coquelin 203248a6092fSMaxime Coquelin pr_info("%s\n", banner); 203348a6092fSMaxime Coquelin 203448a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 203548a6092fSMaxime Coquelin if (ret) 203648a6092fSMaxime Coquelin return ret; 203748a6092fSMaxime Coquelin 203848a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 203948a6092fSMaxime Coquelin if (ret) 204048a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 204148a6092fSMaxime Coquelin 204248a6092fSMaxime Coquelin return ret; 204348a6092fSMaxime Coquelin } 204448a6092fSMaxime Coquelin 204556f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void) 204648a6092fSMaxime Coquelin { 204748a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 204848a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 204948a6092fSMaxime Coquelin } 205048a6092fSMaxime Coquelin 205156f9a76cSErwan Le Ray module_init(stm32_usart_init); 205256f9a76cSErwan Le Ray module_exit(stm32_usart_exit); 205348a6092fSMaxime Coquelin 205448a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 205548a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 205648a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 2057