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 38*c7039ce9SBen Dooks 39*c7039ce9SBen Dooks /* Register offsets */ 40*c7039ce9SBen Dooks static struct stm32_usart_info stm32f4_info = { 41*c7039ce9SBen Dooks .ofs = { 42*c7039ce9SBen Dooks .isr = 0x00, 43*c7039ce9SBen Dooks .rdr = 0x04, 44*c7039ce9SBen Dooks .tdr = 0x04, 45*c7039ce9SBen Dooks .brr = 0x08, 46*c7039ce9SBen Dooks .cr1 = 0x0c, 47*c7039ce9SBen Dooks .cr2 = 0x10, 48*c7039ce9SBen Dooks .cr3 = 0x14, 49*c7039ce9SBen Dooks .gtpr = 0x18, 50*c7039ce9SBen Dooks .rtor = UNDEF_REG, 51*c7039ce9SBen Dooks .rqr = UNDEF_REG, 52*c7039ce9SBen Dooks .icr = UNDEF_REG, 53*c7039ce9SBen Dooks }, 54*c7039ce9SBen Dooks .cfg = { 55*c7039ce9SBen Dooks .uart_enable_bit = 13, 56*c7039ce9SBen Dooks .has_7bits_data = false, 57*c7039ce9SBen Dooks .fifosize = 1, 58*c7039ce9SBen Dooks } 59*c7039ce9SBen Dooks }; 60*c7039ce9SBen Dooks 61*c7039ce9SBen Dooks static struct stm32_usart_info stm32f7_info = { 62*c7039ce9SBen Dooks .ofs = { 63*c7039ce9SBen Dooks .cr1 = 0x00, 64*c7039ce9SBen Dooks .cr2 = 0x04, 65*c7039ce9SBen Dooks .cr3 = 0x08, 66*c7039ce9SBen Dooks .brr = 0x0c, 67*c7039ce9SBen Dooks .gtpr = 0x10, 68*c7039ce9SBen Dooks .rtor = 0x14, 69*c7039ce9SBen Dooks .rqr = 0x18, 70*c7039ce9SBen Dooks .isr = 0x1c, 71*c7039ce9SBen Dooks .icr = 0x20, 72*c7039ce9SBen Dooks .rdr = 0x24, 73*c7039ce9SBen Dooks .tdr = 0x28, 74*c7039ce9SBen Dooks }, 75*c7039ce9SBen Dooks .cfg = { 76*c7039ce9SBen Dooks .uart_enable_bit = 0, 77*c7039ce9SBen Dooks .has_7bits_data = true, 78*c7039ce9SBen Dooks .has_swap = true, 79*c7039ce9SBen Dooks .fifosize = 1, 80*c7039ce9SBen Dooks } 81*c7039ce9SBen Dooks }; 82*c7039ce9SBen Dooks 83*c7039ce9SBen Dooks static struct stm32_usart_info stm32h7_info = { 84*c7039ce9SBen Dooks .ofs = { 85*c7039ce9SBen Dooks .cr1 = 0x00, 86*c7039ce9SBen Dooks .cr2 = 0x04, 87*c7039ce9SBen Dooks .cr3 = 0x08, 88*c7039ce9SBen Dooks .brr = 0x0c, 89*c7039ce9SBen Dooks .gtpr = 0x10, 90*c7039ce9SBen Dooks .rtor = 0x14, 91*c7039ce9SBen Dooks .rqr = 0x18, 92*c7039ce9SBen Dooks .isr = 0x1c, 93*c7039ce9SBen Dooks .icr = 0x20, 94*c7039ce9SBen Dooks .rdr = 0x24, 95*c7039ce9SBen Dooks .tdr = 0x28, 96*c7039ce9SBen Dooks }, 97*c7039ce9SBen Dooks .cfg = { 98*c7039ce9SBen Dooks .uart_enable_bit = 0, 99*c7039ce9SBen Dooks .has_7bits_data = true, 100*c7039ce9SBen Dooks .has_swap = true, 101*c7039ce9SBen Dooks .has_wakeup = true, 102*c7039ce9SBen Dooks .has_fifo = true, 103*c7039ce9SBen Dooks .fifosize = 16, 104*c7039ce9SBen Dooks } 105*c7039ce9SBen Dooks }; 106*c7039ce9SBen Dooks 10756f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port); 10856f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port); 1091f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch); 11048a6092fSMaxime Coquelin 11148a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 11248a6092fSMaxime Coquelin { 11348a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 11448a6092fSMaxime Coquelin } 11548a6092fSMaxime Coquelin 11656f9a76cSErwan Le Ray static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits) 11748a6092fSMaxime Coquelin { 11848a6092fSMaxime Coquelin u32 val; 11948a6092fSMaxime Coquelin 12048a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 12148a6092fSMaxime Coquelin val |= bits; 12248a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 12348a6092fSMaxime Coquelin } 12448a6092fSMaxime Coquelin 12556f9a76cSErwan Le Ray static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits) 12648a6092fSMaxime Coquelin { 12748a6092fSMaxime Coquelin u32 val; 12848a6092fSMaxime Coquelin 12948a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 13048a6092fSMaxime Coquelin val &= ~bits; 13148a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 13248a6092fSMaxime Coquelin } 13348a6092fSMaxime Coquelin 13456f9a76cSErwan Le Ray static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, 1351bcda09dSBich HEMON u32 delay_DDE, u32 baud) 1361bcda09dSBich HEMON { 1371bcda09dSBich HEMON u32 rs485_deat_dedt; 1381bcda09dSBich HEMON u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); 1391bcda09dSBich HEMON bool over8; 1401bcda09dSBich HEMON 1411bcda09dSBich HEMON *cr3 |= USART_CR3_DEM; 1421bcda09dSBich HEMON over8 = *cr1 & USART_CR1_OVER8; 1431bcda09dSBich HEMON 1441bcda09dSBich HEMON if (over8) 1451bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 8; 1461bcda09dSBich HEMON else 1471bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 16; 1481bcda09dSBich HEMON 1491bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 1501bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 1511bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 1521bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & 1531bcda09dSBich HEMON USART_CR1_DEAT_MASK; 1541bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 1551bcda09dSBich HEMON 1561bcda09dSBich HEMON if (over8) 1571bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 8; 1581bcda09dSBich HEMON else 1591bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 16; 1601bcda09dSBich HEMON 1611bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 1621bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 1631bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 1641bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & 1651bcda09dSBich HEMON USART_CR1_DEDT_MASK; 1661bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 1671bcda09dSBich HEMON } 1681bcda09dSBich HEMON 169ae50bb27SIlpo Järvinen static int stm32_usart_config_rs485(struct uart_port *port, struct ktermios *termios, 1701bcda09dSBich HEMON struct serial_rs485 *rs485conf) 1711bcda09dSBich HEMON { 1721bcda09dSBich HEMON struct stm32_port *stm32_port = to_stm32_port(port); 173d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 174d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1751bcda09dSBich HEMON u32 usartdiv, baud, cr1, cr3; 1761bcda09dSBich HEMON bool over8; 1771bcda09dSBich HEMON 17856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1791bcda09dSBich HEMON 1801bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RX_DURING_TX; 1811bcda09dSBich HEMON 1821bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 1831bcda09dSBich HEMON cr1 = readl_relaxed(port->membase + ofs->cr1); 1841bcda09dSBich HEMON cr3 = readl_relaxed(port->membase + ofs->cr3); 1851bcda09dSBich HEMON usartdiv = readl_relaxed(port->membase + ofs->brr); 1861bcda09dSBich HEMON usartdiv = usartdiv & GENMASK(15, 0); 1871bcda09dSBich HEMON over8 = cr1 & USART_CR1_OVER8; 1881bcda09dSBich HEMON 1891bcda09dSBich HEMON if (over8) 1901bcda09dSBich HEMON usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) 1911bcda09dSBich HEMON << USART_BRR_04_R_SHIFT; 1921bcda09dSBich HEMON 1931bcda09dSBich HEMON baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); 19456f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 1951bcda09dSBich HEMON rs485conf->delay_rts_before_send, 19656f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 19756f9a76cSErwan Le Ray baud); 1981bcda09dSBich HEMON 199f633eb29SLino Sanfilippo if (rs485conf->flags & SER_RS485_RTS_ON_SEND) 2001bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 201f633eb29SLino Sanfilippo else 2021bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 2031bcda09dSBich HEMON 2041bcda09dSBich HEMON writel_relaxed(cr3, port->membase + ofs->cr3); 2051bcda09dSBich HEMON writel_relaxed(cr1, port->membase + ofs->cr1); 2061bcda09dSBich HEMON } else { 20756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, 20856f9a76cSErwan Le Ray USART_CR3_DEM | USART_CR3_DEP); 20956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, 2101bcda09dSBich HEMON USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 2111bcda09dSBich HEMON } 2121bcda09dSBich HEMON 21356f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 2141bcda09dSBich HEMON 2151bcda09dSBich HEMON return 0; 2161bcda09dSBich HEMON } 2171bcda09dSBich HEMON 21856f9a76cSErwan Le Ray static int stm32_usart_init_rs485(struct uart_port *port, 2191bcda09dSBich HEMON struct platform_device *pdev) 2201bcda09dSBich HEMON { 2211bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 2221bcda09dSBich HEMON 2231bcda09dSBich HEMON rs485conf->flags = 0; 2241bcda09dSBich HEMON rs485conf->delay_rts_before_send = 0; 2251bcda09dSBich HEMON rs485conf->delay_rts_after_send = 0; 2261bcda09dSBich HEMON 2271bcda09dSBich HEMON if (!pdev->dev.of_node) 2281bcda09dSBich HEMON return -ENODEV; 2291bcda09dSBich HEMON 230c150c0f3SLukas Wunner return uart_get_rs485_mode(port); 2311bcda09dSBich HEMON } 2321bcda09dSBich HEMON 23333bb2f6aSErwan Le Ray static bool stm32_usart_rx_dma_enabled(struct uart_port *port) 23434891872SAlexandre TORGUE { 23534891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 236d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 23733bb2f6aSErwan Le Ray 23833bb2f6aSErwan Le Ray if (!stm32_port->rx_ch) 23933bb2f6aSErwan Le Ray return false; 24033bb2f6aSErwan Le Ray 24133bb2f6aSErwan Le Ray return !!(readl_relaxed(port->membase + ofs->cr3) & USART_CR3_DMAR); 24233bb2f6aSErwan Le Ray } 24333bb2f6aSErwan Le Ray 24433bb2f6aSErwan Le Ray /* Return true when data is pending (in pio mode), and false when no data is pending. */ 24533bb2f6aSErwan Le Ray static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr) 24633bb2f6aSErwan Le Ray { 24733bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 24833bb2f6aSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 24934891872SAlexandre TORGUE 25034891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 25133bb2f6aSErwan Le Ray /* Get pending characters in RDR or FIFO */ 25233bb2f6aSErwan Le Ray if (*sr & USART_SR_RXNE) { 25333bb2f6aSErwan Le Ray /* Get all pending characters from the RDR or the FIFO when using interrupts */ 25433bb2f6aSErwan Le Ray if (!stm32_usart_rx_dma_enabled(port)) 25533bb2f6aSErwan Le Ray return true; 25634891872SAlexandre TORGUE 25733bb2f6aSErwan Le Ray /* Handle only RX data errors when using DMA */ 25833bb2f6aSErwan Le Ray if (*sr & USART_SR_ERR_MASK) 25933bb2f6aSErwan Le Ray return true; 26034891872SAlexandre TORGUE } 26134891872SAlexandre TORGUE 26233bb2f6aSErwan Le Ray return false; 26333bb2f6aSErwan Le Ray } 26433bb2f6aSErwan Le Ray 26533bb2f6aSErwan Le Ray static unsigned long stm32_usart_get_char_pio(struct uart_port *port) 26634891872SAlexandre TORGUE { 26734891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 268d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 26934891872SAlexandre TORGUE unsigned long c; 27034891872SAlexandre TORGUE 2716c5962f3SErwan Le Ray c = readl_relaxed(port->membase + ofs->rdr); 27233bb2f6aSErwan Le Ray /* Apply RDR data mask */ 2736c5962f3SErwan Le Ray c &= stm32_port->rdr_mask; 2746c5962f3SErwan Le Ray 2756c5962f3SErwan Le Ray return c; 27634891872SAlexandre TORGUE } 27734891872SAlexandre TORGUE 2786333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port) 27948a6092fSMaxime Coquelin { 280ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 281d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 28233bb2f6aSErwan Le Ray unsigned long c; 2836333a485SErwan Le Ray unsigned int size = 0; 28448a6092fSMaxime Coquelin u32 sr; 28548a6092fSMaxime Coquelin char flag; 28648a6092fSMaxime Coquelin 28733bb2f6aSErwan Le Ray while (stm32_usart_pending_rx_pio(port, &sr)) { 28848a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 28948a6092fSMaxime Coquelin flag = TTY_NORMAL; 29048a6092fSMaxime Coquelin 2914f01d833SErwan Le Ray /* 2924f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2934f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2944f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2954f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2964f01d833SErwan Le Ray * and clear status bits of the next rx data. 2974f01d833SErwan Le Ray * 2984f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2994f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 3004f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 3014f01d833SErwan Le Ray */ 3024f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 3031250ed71SFabrice Gasnier writel_relaxed(sr & USART_SR_ERR_MASK, 3041250ed71SFabrice Gasnier port->membase + ofs->icr); 3054f01d833SErwan Le Ray 30633bb2f6aSErwan Le Ray c = stm32_usart_get_char_pio(port); 3074f01d833SErwan Le Ray port->icount.rx++; 3086333a485SErwan Le Ray size++; 30948a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 3104f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 31148a6092fSMaxime Coquelin port->icount.overrun++; 31248a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 31348a6092fSMaxime Coquelin port->icount.parity++; 31448a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 3154f01d833SErwan Le Ray /* Break detection if character is null */ 3164f01d833SErwan Le Ray if (!c) { 3174f01d833SErwan Le Ray port->icount.brk++; 3184f01d833SErwan Le Ray if (uart_handle_break(port)) 3194f01d833SErwan Le Ray continue; 3204f01d833SErwan Le Ray } else { 32148a6092fSMaxime Coquelin port->icount.frame++; 32248a6092fSMaxime Coquelin } 3234f01d833SErwan Le Ray } 32448a6092fSMaxime Coquelin 32548a6092fSMaxime Coquelin sr &= port->read_status_mask; 32648a6092fSMaxime Coquelin 3274f01d833SErwan Le Ray if (sr & USART_SR_PE) { 32848a6092fSMaxime Coquelin flag = TTY_PARITY; 3294f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 3304f01d833SErwan Le Ray if (!c) 3314f01d833SErwan Le Ray flag = TTY_BREAK; 3324f01d833SErwan Le Ray else 33348a6092fSMaxime Coquelin flag = TTY_FRAME; 33448a6092fSMaxime Coquelin } 3354f01d833SErwan Le Ray } 33648a6092fSMaxime Coquelin 337cea37afdSJohan Hovold if (uart_prepare_sysrq_char(port, c)) 33848a6092fSMaxime Coquelin continue; 33948a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 34048a6092fSMaxime Coquelin } 3416333a485SErwan Le Ray 3426333a485SErwan Le Ray return size; 34333bb2f6aSErwan Le Ray } 34433bb2f6aSErwan Le Ray 34533bb2f6aSErwan Le Ray static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size) 34633bb2f6aSErwan Le Ray { 34733bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 34833bb2f6aSErwan Le Ray struct tty_port *ttyport = &stm32_port->port.state->port; 34933bb2f6aSErwan Le Ray unsigned char *dma_start; 35033bb2f6aSErwan Le Ray int dma_count, i; 35133bb2f6aSErwan Le Ray 35233bb2f6aSErwan Le Ray dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res); 35333bb2f6aSErwan Le Ray 35433bb2f6aSErwan Le Ray /* 35533bb2f6aSErwan Le Ray * Apply rdr_mask on buffer in order to mask parity bit. 35633bb2f6aSErwan Le Ray * This loop is useless in cs8 mode because DMA copies only 35733bb2f6aSErwan Le Ray * 8 bits and already ignores parity bit. 35833bb2f6aSErwan Le Ray */ 35933bb2f6aSErwan Le Ray if (!(stm32_port->rdr_mask == (BIT(8) - 1))) 36033bb2f6aSErwan Le Ray for (i = 0; i < dma_size; i++) 36133bb2f6aSErwan Le Ray *(dma_start + i) &= stm32_port->rdr_mask; 36233bb2f6aSErwan Le Ray 36333bb2f6aSErwan Le Ray dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size); 36433bb2f6aSErwan Le Ray port->icount.rx += dma_count; 36533bb2f6aSErwan Le Ray if (dma_count != dma_size) 36633bb2f6aSErwan Le Ray port->icount.buf_overrun++; 36733bb2f6aSErwan Le Ray stm32_port->last_res -= dma_count; 36833bb2f6aSErwan Le Ray if (stm32_port->last_res == 0) 36933bb2f6aSErwan Le Ray stm32_port->last_res = RX_BUF_L; 37033bb2f6aSErwan Le Ray } 37133bb2f6aSErwan Le Ray 3726333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port) 37333bb2f6aSErwan Le Ray { 37433bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 3756333a485SErwan Le Ray unsigned int dma_size, size = 0; 37633bb2f6aSErwan Le Ray 37733bb2f6aSErwan Le Ray /* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */ 37833bb2f6aSErwan Le Ray if (stm32_port->rx_dma_state.residue > stm32_port->last_res) { 37933bb2f6aSErwan Le Ray /* Conditional first part: from last_res to end of DMA buffer */ 38033bb2f6aSErwan Le Ray dma_size = stm32_port->last_res; 38133bb2f6aSErwan Le Ray stm32_usart_push_buffer_dma(port, dma_size); 3826333a485SErwan Le Ray size = dma_size; 38333bb2f6aSErwan Le Ray } 38433bb2f6aSErwan Le Ray 38533bb2f6aSErwan Le Ray dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue; 38633bb2f6aSErwan Le Ray stm32_usart_push_buffer_dma(port, dma_size); 3876333a485SErwan Le Ray size += dma_size; 3886333a485SErwan Le Ray 3896333a485SErwan Le Ray return size; 39033bb2f6aSErwan Le Ray } 39133bb2f6aSErwan Le Ray 3926333a485SErwan Le Ray static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush) 39333bb2f6aSErwan Le Ray { 39433bb2f6aSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 39533bb2f6aSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 39633bb2f6aSErwan Le Ray enum dma_status rx_dma_status; 39733bb2f6aSErwan Le Ray u32 sr; 3986333a485SErwan Le Ray unsigned int size = 0; 39933bb2f6aSErwan Le Ray 4006333a485SErwan Le Ray if (stm32_usart_rx_dma_enabled(port) || force_dma_flush) { 40133bb2f6aSErwan Le Ray rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch, 40233bb2f6aSErwan Le Ray stm32_port->rx_ch->cookie, 40333bb2f6aSErwan Le Ray &stm32_port->rx_dma_state); 40433bb2f6aSErwan Le Ray if (rx_dma_status == DMA_IN_PROGRESS) { 40533bb2f6aSErwan Le Ray /* Empty DMA buffer */ 4066333a485SErwan Le Ray size = stm32_usart_receive_chars_dma(port); 40733bb2f6aSErwan Le Ray sr = readl_relaxed(port->membase + ofs->isr); 40833bb2f6aSErwan Le Ray if (sr & USART_SR_ERR_MASK) { 40933bb2f6aSErwan Le Ray /* Disable DMA request line */ 41033bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 41133bb2f6aSErwan Le Ray 41233bb2f6aSErwan Le Ray /* Switch to PIO mode to handle the errors */ 4136333a485SErwan Le Ray size += stm32_usart_receive_chars_pio(port); 41433bb2f6aSErwan Le Ray 41533bb2f6aSErwan Le Ray /* Switch back to DMA mode */ 41633bb2f6aSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 41733bb2f6aSErwan Le Ray } 41833bb2f6aSErwan Le Ray } else { 41933bb2f6aSErwan Le Ray /* Disable RX DMA */ 42033bb2f6aSErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 42133bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 42233bb2f6aSErwan Le Ray /* Fall back to interrupt mode */ 42333bb2f6aSErwan Le Ray dev_dbg(port->dev, "DMA error, fallback to irq mode\n"); 4246333a485SErwan Le Ray size = stm32_usart_receive_chars_pio(port); 42533bb2f6aSErwan Le Ray } 42633bb2f6aSErwan Le Ray } else { 4276333a485SErwan Le Ray size = stm32_usart_receive_chars_pio(port); 42833bb2f6aSErwan Le Ray } 42948a6092fSMaxime Coquelin 4306333a485SErwan Le Ray return size; 43148a6092fSMaxime Coquelin } 43248a6092fSMaxime Coquelin 4339a135f16SValentin Caron static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port) 4349a135f16SValentin Caron { 4359a135f16SValentin Caron dmaengine_terminate_async(stm32_port->tx_ch); 4369a135f16SValentin Caron stm32_port->tx_dma_busy = false; 4379a135f16SValentin Caron } 4389a135f16SValentin Caron 4399a135f16SValentin Caron static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port) 4409a135f16SValentin Caron { 4419a135f16SValentin Caron /* 4429a135f16SValentin Caron * We cannot use the function "dmaengine_tx_status" to know the 4439a135f16SValentin Caron * status of DMA. This function does not show if the "dma complete" 4449a135f16SValentin Caron * callback of the DMA transaction has been called. So we prefer 4459a135f16SValentin Caron * to use "tx_dma_busy" flag to prevent dual DMA transaction at the 4469a135f16SValentin Caron * same time. 4479a135f16SValentin Caron */ 4489a135f16SValentin Caron return stm32_port->tx_dma_busy; 4499a135f16SValentin Caron } 4509a135f16SValentin Caron 4519a135f16SValentin Caron static bool stm32_usart_tx_dma_enabled(struct stm32_port *stm32_port) 4529a135f16SValentin Caron { 4539a135f16SValentin Caron const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 4549a135f16SValentin Caron 4559a135f16SValentin Caron return !!(readl_relaxed(stm32_port->port.membase + ofs->cr3) & USART_CR3_DMAT); 4569a135f16SValentin Caron } 4579a135f16SValentin Caron 45856f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg) 45934891872SAlexandre TORGUE { 46034891872SAlexandre TORGUE struct uart_port *port = arg; 46134891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 462d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 463f16b90c2SErwan Le Ray unsigned long flags; 46434891872SAlexandre TORGUE 46556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 4669a135f16SValentin Caron stm32_usart_tx_dma_terminate(stm32port); 46734891872SAlexandre TORGUE 46834891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 469f16b90c2SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 47056f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 471f16b90c2SErwan Le Ray spin_unlock_irqrestore(&port->lock, flags); 47234891872SAlexandre TORGUE } 47334891872SAlexandre TORGUE 47456f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port) 475d075719eSErwan Le Ray { 476d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 477d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 478d075719eSErwan Le Ray 479d075719eSErwan Le Ray /* 480d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 481d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 482d075719eSErwan Le Ray */ 4832aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 48456f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 485d075719eSErwan Le Ray else 48656f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 487d075719eSErwan Le Ray } 488d075719eSErwan Le Ray 489d7c76716SMarek Vasut static void stm32_usart_tc_interrupt_enable(struct uart_port *port) 490d7c76716SMarek Vasut { 491d7c76716SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 492d7c76716SMarek Vasut const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 493d7c76716SMarek Vasut 494d7c76716SMarek Vasut stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TCIE); 495d7c76716SMarek Vasut } 496d7c76716SMarek Vasut 49733bb2f6aSErwan Le Ray static void stm32_usart_rx_dma_complete(void *arg) 49833bb2f6aSErwan Le Ray { 49933bb2f6aSErwan Le Ray struct uart_port *port = arg; 5006333a485SErwan Le Ray struct tty_port *tport = &port->state->port; 5016333a485SErwan Le Ray unsigned int size; 5026333a485SErwan Le Ray unsigned long flags; 50333bb2f6aSErwan Le Ray 5046333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 5056333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 5066333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 5076333a485SErwan Le Ray if (size) 5086333a485SErwan Le Ray tty_flip_buffer_push(tport); 50933bb2f6aSErwan Le Ray } 51033bb2f6aSErwan Le Ray 51156f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port) 512d075719eSErwan Le Ray { 513d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 514d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 515d075719eSErwan Le Ray 5162aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 51756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 518d075719eSErwan Le Ray else 51956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 520d075719eSErwan Le Ray } 521d075719eSErwan Le Ray 522d7c76716SMarek Vasut static void stm32_usart_tc_interrupt_disable(struct uart_port *port) 523d7c76716SMarek Vasut { 524d7c76716SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 525d7c76716SMarek Vasut const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 526d7c76716SMarek Vasut 527d7c76716SMarek Vasut stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TCIE); 528d7c76716SMarek Vasut } 529d7c76716SMarek Vasut 5303bcea529SMarek Vasut static void stm32_usart_rs485_rts_enable(struct uart_port *port) 5313bcea529SMarek Vasut { 5323bcea529SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 5333bcea529SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 5343bcea529SMarek Vasut 5353bcea529SMarek Vasut if (stm32_port->hw_flow_control || 5363bcea529SMarek Vasut !(rs485conf->flags & SER_RS485_ENABLED)) 5373bcea529SMarek Vasut return; 5383bcea529SMarek Vasut 5393bcea529SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 5403bcea529SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 5413bcea529SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 5423bcea529SMarek Vasut } else { 5433bcea529SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 5443bcea529SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 5453bcea529SMarek Vasut } 5463bcea529SMarek Vasut } 5473bcea529SMarek Vasut 5483bcea529SMarek Vasut static void stm32_usart_rs485_rts_disable(struct uart_port *port) 5493bcea529SMarek Vasut { 5503bcea529SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 5513bcea529SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 5523bcea529SMarek Vasut 5533bcea529SMarek Vasut if (stm32_port->hw_flow_control || 5543bcea529SMarek Vasut !(rs485conf->flags & SER_RS485_ENABLED)) 5553bcea529SMarek Vasut return; 5563bcea529SMarek Vasut 5573bcea529SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 5583bcea529SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 5593bcea529SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 5603bcea529SMarek Vasut } else { 5613bcea529SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 5623bcea529SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 5633bcea529SMarek Vasut } 5643bcea529SMarek Vasut } 5653bcea529SMarek Vasut 56656f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port) 56734891872SAlexandre TORGUE { 56834891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 569d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 57034891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 57134891872SAlexandre TORGUE 5729a135f16SValentin Caron if (stm32_usart_tx_dma_enabled(stm32_port)) 57356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 57434891872SAlexandre TORGUE 5755d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 5765d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 5775d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 5785d9176edSErwan Le Ray break; 57934891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 58034891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 58134891872SAlexandre TORGUE port->icount.tx++; 58234891872SAlexandre TORGUE } 58334891872SAlexandre TORGUE 5845d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 5855d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 58656f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 5875d9176edSErwan Le Ray else 58856f9a76cSErwan Le Ray stm32_usart_tx_interrupt_enable(port); 5895d9176edSErwan Le Ray } 5905d9176edSErwan Le Ray 59156f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port) 59234891872SAlexandre TORGUE { 59334891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 594d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 59534891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 59634891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 597195437d1SValentin Caron unsigned int count; 59834891872SAlexandre TORGUE 5999a135f16SValentin Caron if (stm32_usart_tx_dma_started(stm32port)) { 6009a135f16SValentin Caron if (!stm32_usart_tx_dma_enabled(stm32port)) 6019a135f16SValentin Caron stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 60234891872SAlexandre TORGUE return; 6039a135f16SValentin Caron } 60434891872SAlexandre TORGUE 60534891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 60634891872SAlexandre TORGUE 60734891872SAlexandre TORGUE if (count > TX_BUF_L) 60834891872SAlexandre TORGUE count = TX_BUF_L; 60934891872SAlexandre TORGUE 61034891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 61134891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 61234891872SAlexandre TORGUE } else { 61334891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 61434891872SAlexandre TORGUE size_t two; 61534891872SAlexandre TORGUE 61634891872SAlexandre TORGUE if (one > count) 61734891872SAlexandre TORGUE one = count; 61834891872SAlexandre TORGUE two = count - one; 61934891872SAlexandre TORGUE 62034891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 62134891872SAlexandre TORGUE if (two) 62234891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 62334891872SAlexandre TORGUE } 62434891872SAlexandre TORGUE 62534891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 62634891872SAlexandre TORGUE stm32port->tx_dma_buf, 62734891872SAlexandre TORGUE count, 62834891872SAlexandre TORGUE DMA_MEM_TO_DEV, 62934891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 63034891872SAlexandre TORGUE 631e7997f7fSErwan Le Ray if (!desc) 632e7997f7fSErwan Le Ray goto fallback_err; 63334891872SAlexandre TORGUE 6349a135f16SValentin Caron /* 6359a135f16SValentin Caron * Set "tx_dma_busy" flag. This flag will be released when 6369a135f16SValentin Caron * dmaengine_terminate_async will be called. This flag helps 6379a135f16SValentin Caron * transmit_chars_dma not to start another DMA transaction 6389a135f16SValentin Caron * if the callback of the previous is not yet called. 6399a135f16SValentin Caron */ 6409a135f16SValentin Caron stm32port->tx_dma_busy = true; 6419a135f16SValentin Caron 64256f9a76cSErwan Le Ray desc->callback = stm32_usart_tx_dma_complete; 64334891872SAlexandre TORGUE desc->callback_param = port; 64434891872SAlexandre TORGUE 64534891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 646e7997f7fSErwan Le Ray if (dma_submit_error(dmaengine_submit(desc))) { 647e7997f7fSErwan Le Ray /* dma no yet started, safe to free resources */ 6489a135f16SValentin Caron stm32_usart_tx_dma_terminate(stm32port); 649e7997f7fSErwan Le Ray goto fallback_err; 650e7997f7fSErwan Le Ray } 65134891872SAlexandre TORGUE 65234891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 65334891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 65434891872SAlexandre TORGUE 65556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 65634891872SAlexandre TORGUE 65734891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 65834891872SAlexandre TORGUE port->icount.tx += count; 659e7997f7fSErwan Le Ray return; 660e7997f7fSErwan Le Ray 661e7997f7fSErwan Le Ray fallback_err: 66256f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 66334891872SAlexandre TORGUE } 66434891872SAlexandre TORGUE 66556f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port) 66648a6092fSMaxime Coquelin { 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 struct circ_buf *xmit = &port->state->xmit; 670d3d079bdSValentin Caron u32 isr; 671d3d079bdSValentin Caron int ret; 67248a6092fSMaxime Coquelin 673d7c76716SMarek Vasut if (!stm32_port->hw_flow_control && 674d7c76716SMarek Vasut port->rs485.flags & SER_RS485_ENABLED) { 675d7c76716SMarek Vasut stm32_port->txdone = false; 676d7c76716SMarek Vasut stm32_usart_tc_interrupt_disable(port); 677d7c76716SMarek Vasut stm32_usart_rs485_rts_enable(port); 678d7c76716SMarek Vasut } 679d7c76716SMarek Vasut 68048a6092fSMaxime Coquelin if (port->x_char) { 6819a135f16SValentin Caron if (stm32_usart_tx_dma_started(stm32_port) && 6829a135f16SValentin Caron stm32_usart_tx_dma_enabled(stm32_port)) 68356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 684d3d079bdSValentin Caron 685d3d079bdSValentin Caron /* Check that TDR is empty before filling FIFO */ 686d3d079bdSValentin Caron ret = 687d3d079bdSValentin Caron readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 688d3d079bdSValentin Caron isr, 689d3d079bdSValentin Caron (isr & USART_SR_TXE), 690d3d079bdSValentin Caron 10, 1000); 691d3d079bdSValentin Caron if (ret) 692d3d079bdSValentin Caron dev_warn(port->dev, "1 character may be erased\n"); 693d3d079bdSValentin Caron 694ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 69548a6092fSMaxime Coquelin port->x_char = 0; 69648a6092fSMaxime Coquelin port->icount.tx++; 6979a135f16SValentin Caron if (stm32_usart_tx_dma_started(stm32_port)) 69856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 69948a6092fSMaxime Coquelin return; 70048a6092fSMaxime Coquelin } 70148a6092fSMaxime Coquelin 702b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 70356f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 70448a6092fSMaxime Coquelin return; 70548a6092fSMaxime Coquelin } 70648a6092fSMaxime Coquelin 70764c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 70856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC); 70964c32eabSErwan Le Ray else 7101250ed71SFabrice Gasnier writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr); 71164c32eabSErwan Le Ray 71234891872SAlexandre TORGUE if (stm32_port->tx_ch) 71356f9a76cSErwan Le Ray stm32_usart_transmit_chars_dma(port); 71434891872SAlexandre TORGUE else 71556f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 71648a6092fSMaxime Coquelin 71748a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 71848a6092fSMaxime Coquelin uart_write_wakeup(port); 71948a6092fSMaxime Coquelin 720d7c76716SMarek Vasut if (uart_circ_empty(xmit)) { 72156f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 722d7c76716SMarek Vasut if (!stm32_port->hw_flow_control && 723d7c76716SMarek Vasut port->rs485.flags & SER_RS485_ENABLED) { 724d7c76716SMarek Vasut stm32_port->txdone = true; 725d7c76716SMarek Vasut stm32_usart_tc_interrupt_enable(port); 726d7c76716SMarek Vasut } 727d7c76716SMarek Vasut } 72848a6092fSMaxime Coquelin } 72948a6092fSMaxime Coquelin 73056f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) 73148a6092fSMaxime Coquelin { 73248a6092fSMaxime Coquelin struct uart_port *port = ptr; 73312761869SErwan Le Ray struct tty_port *tport = &port->state->port; 734ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 735d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 73648a6092fSMaxime Coquelin u32 sr; 7376333a485SErwan Le Ray unsigned int size; 73848a6092fSMaxime Coquelin 739ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 74048a6092fSMaxime Coquelin 741d7c76716SMarek Vasut if (!stm32_port->hw_flow_control && 742d7c76716SMarek Vasut port->rs485.flags & SER_RS485_ENABLED && 743d7c76716SMarek Vasut (sr & USART_SR_TC)) { 744d7c76716SMarek Vasut stm32_usart_tc_interrupt_disable(port); 745d7c76716SMarek Vasut stm32_usart_rs485_rts_disable(port); 746d7c76716SMarek Vasut } 747d7c76716SMarek Vasut 7484cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 7494cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 7504cc0ed62SErwan Le Ray port->membase + ofs->icr); 7514cc0ed62SErwan Le Ray 75212761869SErwan Le Ray if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) { 75312761869SErwan Le Ray /* Clear wake up flag and disable wake up interrupt */ 754270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 755270e5a74SFabrice Gasnier port->membase + ofs->icr); 75612761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 75712761869SErwan Le Ray if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 75812761869SErwan Le Ray pm_wakeup_event(tport->tty->dev, 0); 75912761869SErwan Le Ray } 760270e5a74SFabrice Gasnier 76133bb2f6aSErwan Le Ray /* 76233bb2f6aSErwan Le Ray * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request 76333bb2f6aSErwan Le Ray * line has been masked by HW and rx data are stacking in FIFO. 76433bb2f6aSErwan Le Ray */ 765d1ec8a2eSErwan Le Ray if (!stm32_port->throttled) { 76633bb2f6aSErwan Le Ray if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_enabled(port)) || 767d1ec8a2eSErwan Le Ray ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_enabled(port))) { 7686333a485SErwan Le Ray spin_lock(&port->lock); 7696333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 7706333a485SErwan Le Ray uart_unlock_and_check_sysrq(port); 7716333a485SErwan Le Ray if (size) 7726333a485SErwan Le Ray tty_flip_buffer_push(tport); 773d1ec8a2eSErwan Le Ray } 774d1ec8a2eSErwan Le Ray } 77548a6092fSMaxime Coquelin 776ad767681SErwan Le Ray if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) { 777ad767681SErwan Le Ray spin_lock(&port->lock); 77856f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 77901d32d71SAlexandre TORGUE spin_unlock(&port->lock); 780ad767681SErwan Le Ray } 78101d32d71SAlexandre TORGUE 78233bb2f6aSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) 78334891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 78434891872SAlexandre TORGUE else 78534891872SAlexandre TORGUE return IRQ_HANDLED; 78634891872SAlexandre TORGUE } 78734891872SAlexandre TORGUE 78856f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr) 78934891872SAlexandre TORGUE { 79034891872SAlexandre TORGUE struct uart_port *port = ptr; 7916333a485SErwan Le Ray struct tty_port *tport = &port->state->port; 792d1ec8a2eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 7936333a485SErwan Le Ray unsigned int size; 7946333a485SErwan Le Ray unsigned long flags; 79534891872SAlexandre TORGUE 796cc58d0a3SErwan Le Ray /* Receiver timeout irq for DMA RX */ 7976333a485SErwan Le Ray if (!stm32_port->throttled) { 7986333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 7996333a485SErwan Le Ray size = stm32_usart_receive_chars(port, false); 8006333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 8016333a485SErwan Le Ray if (size) 8026333a485SErwan Le Ray tty_flip_buffer_push(tport); 8036333a485SErwan Le Ray } 80434891872SAlexandre TORGUE 80548a6092fSMaxime Coquelin return IRQ_HANDLED; 80648a6092fSMaxime Coquelin } 80748a6092fSMaxime Coquelin 80856f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port) 80948a6092fSMaxime Coquelin { 810ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 811d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 812ada8618fSAlexandre TORGUE 8133db1d524SErwan Le Ray if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC) 8143db1d524SErwan Le Ray return TIOCSER_TEMT; 8153db1d524SErwan Le Ray 8163db1d524SErwan Le Ray return 0; 81748a6092fSMaxime Coquelin } 81848a6092fSMaxime Coquelin 81956f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl) 82048a6092fSMaxime Coquelin { 821ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 822d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 823ada8618fSAlexandre TORGUE 82448a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 82556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE); 82648a6092fSMaxime Coquelin else 82756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 8286cf61b9bSManivannan Sadhasivam 8296cf61b9bSManivannan Sadhasivam mctrl_gpio_set(stm32_port->gpios, mctrl); 83048a6092fSMaxime Coquelin } 83148a6092fSMaxime Coquelin 83256f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port) 83348a6092fSMaxime Coquelin { 8346cf61b9bSManivannan Sadhasivam struct stm32_port *stm32_port = to_stm32_port(port); 8356cf61b9bSManivannan Sadhasivam unsigned int ret; 8366cf61b9bSManivannan Sadhasivam 83748a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 8386cf61b9bSManivannan Sadhasivam ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 8396cf61b9bSManivannan Sadhasivam 8406cf61b9bSManivannan Sadhasivam return mctrl_gpio_get(stm32_port->gpios, &ret); 8416cf61b9bSManivannan Sadhasivam } 8426cf61b9bSManivannan Sadhasivam 84356f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port) 8446cf61b9bSManivannan Sadhasivam { 8456cf61b9bSManivannan Sadhasivam mctrl_gpio_enable_ms(to_stm32_port(port)->gpios); 8466cf61b9bSManivannan Sadhasivam } 8476cf61b9bSManivannan Sadhasivam 84856f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port) 8496cf61b9bSManivannan Sadhasivam { 8506cf61b9bSManivannan Sadhasivam mctrl_gpio_disable_ms(to_stm32_port(port)->gpios); 85148a6092fSMaxime Coquelin } 85248a6092fSMaxime Coquelin 85348a6092fSMaxime Coquelin /* Transmit stop */ 85456f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port) 85548a6092fSMaxime Coquelin { 856ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 8572a3bcfe0SValentin Caron const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 858ad0c2748SMarek Vasut 85956f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 8602a3bcfe0SValentin Caron if (stm32_usart_tx_dma_started(stm32_port) && stm32_usart_tx_dma_enabled(stm32_port)) 8612a3bcfe0SValentin Caron stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 862ad0c2748SMarek Vasut 8633bcea529SMarek Vasut stm32_usart_rs485_rts_disable(port); 86448a6092fSMaxime Coquelin } 86548a6092fSMaxime Coquelin 86648a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 86756f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port) 86848a6092fSMaxime Coquelin { 86948a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 87048a6092fSMaxime Coquelin 871d7c76716SMarek Vasut if (uart_circ_empty(xmit) && !port->x_char) { 872d7c76716SMarek Vasut stm32_usart_rs485_rts_disable(port); 87348a6092fSMaxime Coquelin return; 874d7c76716SMarek Vasut } 87548a6092fSMaxime Coquelin 8763bcea529SMarek Vasut stm32_usart_rs485_rts_enable(port); 877ad0c2748SMarek Vasut 87856f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 87948a6092fSMaxime Coquelin } 88048a6092fSMaxime Coquelin 8813d82be8bSErwan Le Ray /* Flush the transmit buffer. */ 8823d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port) 8833d82be8bSErwan Le Ray { 8843d82be8bSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 8853d82be8bSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 8863d82be8bSErwan Le Ray 8873d82be8bSErwan Le Ray if (stm32_port->tx_ch) { 8889a135f16SValentin Caron stm32_usart_tx_dma_terminate(stm32_port); 8893d82be8bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 8903d82be8bSErwan Le Ray } 8913d82be8bSErwan Le Ray } 8923d82be8bSErwan Le Ray 89348a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 89456f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port) 89548a6092fSMaxime Coquelin { 896ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 897d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 89848a6092fSMaxime Coquelin unsigned long flags; 89948a6092fSMaxime Coquelin 90048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 901d1ec8a2eSErwan Le Ray 902d1ec8a2eSErwan Le Ray /* 903d1ec8a2eSErwan Le Ray * Disable DMA request line if enabled, so the RX data gets queued into the FIFO. 904d1ec8a2eSErwan Le Ray * Hardware flow control is triggered when RX FIFO is full. 905d1ec8a2eSErwan Le Ray */ 906d1ec8a2eSErwan Le Ray if (stm32_usart_rx_dma_enabled(port)) 907d1ec8a2eSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 908d1ec8a2eSErwan Le Ray 90956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 910d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 91156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 912d0a6a7bcSErwan Le Ray 913d1ec8a2eSErwan Le Ray stm32_port->throttled = true; 91448a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 91548a6092fSMaxime Coquelin } 91648a6092fSMaxime Coquelin 91748a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 91856f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port) 91948a6092fSMaxime Coquelin { 920ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 921d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 92248a6092fSMaxime Coquelin unsigned long flags; 92348a6092fSMaxime Coquelin 92448a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 92556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 926d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 92756f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq); 928d0a6a7bcSErwan Le Ray 929d1ec8a2eSErwan Le Ray /* 930d1ec8a2eSErwan Le Ray * Switch back to DMA mode (re-enable DMA request line). 931d1ec8a2eSErwan Le Ray * Hardware flow control is stopped when FIFO is not full any more. 932d1ec8a2eSErwan Le Ray */ 933d1ec8a2eSErwan Le Ray if (stm32_port->rx_ch) 934d1ec8a2eSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 935d1ec8a2eSErwan Le Ray 936d1ec8a2eSErwan Le Ray stm32_port->throttled = false; 93748a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 93848a6092fSMaxime Coquelin } 93948a6092fSMaxime Coquelin 94048a6092fSMaxime Coquelin /* Receive stop */ 94156f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port) 94248a6092fSMaxime Coquelin { 943ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 944d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 945ada8618fSAlexandre TORGUE 946e0abc903SErwan Le Ray /* Disable DMA request line. */ 947e0abc903SErwan Le Ray if (stm32_port->rx_ch) 948e0abc903SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 949e0abc903SErwan Le Ray 95056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 951d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 95256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 95348a6092fSMaxime Coquelin } 95448a6092fSMaxime Coquelin 95548a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 95656f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state) 95748a6092fSMaxime Coquelin { 95848a6092fSMaxime Coquelin } 95948a6092fSMaxime Coquelin 9606eeb348cSErwan Le Ray static int stm32_usart_start_rx_dma_cyclic(struct uart_port *port) 9616eeb348cSErwan Le Ray { 9626eeb348cSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 9636eeb348cSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 9646eeb348cSErwan Le Ray struct dma_async_tx_descriptor *desc; 9656eeb348cSErwan Le Ray int ret; 9666eeb348cSErwan Le Ray 9676eeb348cSErwan Le Ray stm32_port->last_res = RX_BUF_L; 9686eeb348cSErwan Le Ray /* Prepare a DMA cyclic transaction */ 9696eeb348cSErwan Le Ray desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch, 9706eeb348cSErwan Le Ray stm32_port->rx_dma_buf, 9716eeb348cSErwan Le Ray RX_BUF_L, RX_BUF_P, 9726eeb348cSErwan Le Ray DMA_DEV_TO_MEM, 9736eeb348cSErwan Le Ray DMA_PREP_INTERRUPT); 9746eeb348cSErwan Le Ray if (!desc) { 9756eeb348cSErwan Le Ray dev_err(port->dev, "rx dma prep cyclic failed\n"); 9766eeb348cSErwan Le Ray return -ENODEV; 9776eeb348cSErwan Le Ray } 9786eeb348cSErwan Le Ray 9796eeb348cSErwan Le Ray desc->callback = stm32_usart_rx_dma_complete; 9806eeb348cSErwan Le Ray desc->callback_param = port; 9816eeb348cSErwan Le Ray 9826eeb348cSErwan Le Ray /* Push current DMA transaction in the pending queue */ 9836eeb348cSErwan Le Ray ret = dma_submit_error(dmaengine_submit(desc)); 9846eeb348cSErwan Le Ray if (ret) { 9856eeb348cSErwan Le Ray dmaengine_terminate_sync(stm32_port->rx_ch); 9866eeb348cSErwan Le Ray return ret; 9876eeb348cSErwan Le Ray } 9886eeb348cSErwan Le Ray 9896eeb348cSErwan Le Ray /* Issue pending DMA requests */ 9906eeb348cSErwan Le Ray dma_async_issue_pending(stm32_port->rx_ch); 9916eeb348cSErwan Le Ray 9926eeb348cSErwan Le Ray /* 9936eeb348cSErwan Le Ray * DMA request line not re-enabled at resume when port is throttled. 9946eeb348cSErwan Le Ray * It will be re-enabled by unthrottle ops. 9956eeb348cSErwan Le Ray */ 9966eeb348cSErwan Le Ray if (!stm32_port->throttled) 9976eeb348cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR); 9986eeb348cSErwan Le Ray 9996eeb348cSErwan Le Ray return 0; 10006eeb348cSErwan Le Ray } 10016eeb348cSErwan Le Ray 100256f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port) 100348a6092fSMaxime Coquelin { 1004ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1005d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1006f4518a8aSErwan Le Ray const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 100748a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 100848a6092fSMaxime Coquelin u32 val; 100948a6092fSMaxime Coquelin int ret; 101048a6092fSMaxime Coquelin 101156f9a76cSErwan Le Ray ret = request_threaded_irq(port->irq, stm32_usart_interrupt, 101256f9a76cSErwan Le Ray stm32_usart_threaded_interrupt, 1013e359b441SJohan Hovold IRQF_ONESHOT | IRQF_NO_SUSPEND, 1014e359b441SJohan Hovold name, port); 101548a6092fSMaxime Coquelin if (ret) 101648a6092fSMaxime Coquelin return ret; 101748a6092fSMaxime Coquelin 10183cd66593SMartin Devera if (stm32_port->swap) { 10193cd66593SMartin Devera val = readl_relaxed(port->membase + ofs->cr2); 10203cd66593SMartin Devera val |= USART_CR2_SWAP; 10213cd66593SMartin Devera writel_relaxed(val, port->membase + ofs->cr2); 10223cd66593SMartin Devera } 10233cd66593SMartin Devera 102484872dc4SErwan Le Ray /* RX FIFO Flush */ 102584872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 1026315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr); 102748a6092fSMaxime Coquelin 1028e0abc903SErwan Le Ray if (stm32_port->rx_ch) { 10296eeb348cSErwan Le Ray ret = stm32_usart_start_rx_dma_cyclic(port); 1030e0abc903SErwan Le Ray if (ret) { 10316eeb348cSErwan Le Ray free_irq(port->irq, port); 10326eeb348cSErwan Le Ray return ret; 1033e0abc903SErwan Le Ray } 1034e0abc903SErwan Le Ray } 1035d1ec8a2eSErwan Le Ray 103625a8e761SErwan Le Ray /* RX enabling */ 1037f4518a8aSErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit); 103856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, val); 103984872dc4SErwan Le Ray 104048a6092fSMaxime Coquelin return 0; 104148a6092fSMaxime Coquelin } 104248a6092fSMaxime Coquelin 104356f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port) 104448a6092fSMaxime Coquelin { 1045ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1046d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1047d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 104864c32eabSErwan Le Ray u32 val, isr; 104964c32eabSErwan Le Ray int ret; 105048a6092fSMaxime Coquelin 10519a135f16SValentin Caron if (stm32_usart_tx_dma_enabled(stm32_port)) 105256a23f93SValentin Caron stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 10539a135f16SValentin Caron 10549a135f16SValentin Caron if (stm32_usart_tx_dma_started(stm32_port)) 10559a135f16SValentin Caron stm32_usart_tx_dma_terminate(stm32_port); 105656a23f93SValentin Caron 10576cf61b9bSManivannan Sadhasivam /* Disable modem control interrupts */ 105856f9a76cSErwan Le Ray stm32_usart_disable_ms(port); 10596cf61b9bSManivannan Sadhasivam 10604cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 10614cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 106287f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 1063351a762aSGerald Baeza if (stm32_port->fifoen) 1064351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 106564c32eabSErwan Le Ray 106664c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 106764c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 106864c32eabSErwan Le Ray 10, 100000); 106964c32eabSErwan Le Ray 1070c31c3ea0SErwan Le Ray /* Send the TC error message only when ISR_TC is not set */ 107164c32eabSErwan Le Ray if (ret) 1072c31c3ea0SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 107364c32eabSErwan Le Ray 1074e0abc903SErwan Le Ray /* Disable RX DMA. */ 1075e0abc903SErwan Le Ray if (stm32_port->rx_ch) 1076e0abc903SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 1077e0abc903SErwan Le Ray 10789f77d192SErwan Le Ray /* flush RX & TX FIFO */ 10799f77d192SErwan Le Ray if (ofs->rqr != UNDEF_REG) 10809f77d192SErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 10819f77d192SErwan Le Ray port->membase + ofs->rqr); 10829f77d192SErwan Le Ray 108356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, val); 108448a6092fSMaxime Coquelin 108548a6092fSMaxime Coquelin free_irq(port->irq, port); 108648a6092fSMaxime Coquelin } 108748a6092fSMaxime Coquelin 108856f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port, 108956f9a76cSErwan Le Ray struct ktermios *termios, 109048a6092fSMaxime Coquelin struct ktermios *old) 109148a6092fSMaxime Coquelin { 109248a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 1093d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1094d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 10951bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 1096c8a9d043SErwan Le Ray unsigned int baud, bits; 109748a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 109848a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 1099f264c6f6SErwan Le Ray u32 cr1, cr2, cr3, isr; 110048a6092fSMaxime Coquelin unsigned long flags; 1101f264c6f6SErwan Le Ray int ret; 110248a6092fSMaxime Coquelin 110348a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 110448a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 110548a6092fSMaxime Coquelin 110648a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 110748a6092fSMaxime Coquelin 110848a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 110948a6092fSMaxime Coquelin 1110f264c6f6SErwan Le Ray ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 1111f264c6f6SErwan Le Ray isr, 1112f264c6f6SErwan Le Ray (isr & USART_SR_TC), 1113f264c6f6SErwan Le Ray 10, 100000); 1114f264c6f6SErwan Le Ray 1115f264c6f6SErwan Le Ray /* Send the TC error message only when ISR_TC is not set. */ 1116f264c6f6SErwan Le Ray if (ret) 1117f264c6f6SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 1118f264c6f6SErwan Le Ray 111948a6092fSMaxime Coquelin /* Stop serial port and reset value */ 1120ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 112148a6092fSMaxime Coquelin 112284872dc4SErwan Le Ray /* flush RX & TX FIFO */ 112384872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 1124315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 1125315e2d8aSErwan Le Ray port->membase + ofs->rqr); 11261bcda09dSBich HEMON 112784872dc4SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 1128351a762aSGerald Baeza if (stm32_port->fifoen) 1129351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 11303cd66593SMartin Devera cr2 = stm32_port->swap ? USART_CR2_SWAP : 0; 113125a8e761SErwan Le Ray 113225a8e761SErwan Le Ray /* Tx and RX FIFO configuration */ 1133d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 113425a8e761SErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE; 113525a8e761SErwan Le Ray if (stm32_port->fifoen) { 11362aa1bbb2SFabrice Gasnier if (stm32_port->txftcfg >= 0) 11372aa1bbb2SFabrice Gasnier cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT; 11382aa1bbb2SFabrice Gasnier if (stm32_port->rxftcfg >= 0) 11392aa1bbb2SFabrice Gasnier cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT; 114025a8e761SErwan Le Ray } 114148a6092fSMaxime Coquelin 114248a6092fSMaxime Coquelin if (cflag & CSTOPB) 114348a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 114448a6092fSMaxime Coquelin 11453ec2ff37SJiri Slaby bits = tty_get_char_size(cflag); 11466c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 1147c8a9d043SErwan Le Ray 114848a6092fSMaxime Coquelin if (cflag & PARENB) { 1149c8a9d043SErwan Le Ray bits++; 115048a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 1151c8a9d043SErwan Le Ray } 1152c8a9d043SErwan Le Ray 1153c8a9d043SErwan Le Ray /* 1154c8a9d043SErwan Le Ray * Word length configuration: 1155c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 1156c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 1157c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 1158c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 1159c8a9d043SErwan Le Ray */ 11601deeda8dSIlpo Järvinen if (bits == 9) { 1161ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 11621deeda8dSIlpo Järvinen } else if ((bits == 7) && cfg->has_7bits_data) { 1163c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 11641deeda8dSIlpo Järvinen } else if (bits != 8) { 1165c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 1166c8a9d043SErwan Le Ray , bits); 11671deeda8dSIlpo Järvinen cflag &= ~CSIZE; 11681deeda8dSIlpo Järvinen cflag |= CS8; 11691deeda8dSIlpo Järvinen termios->c_cflag = cflag; 11701deeda8dSIlpo Järvinen bits = 8; 11711deeda8dSIlpo Järvinen if (cflag & PARENB) { 11721deeda8dSIlpo Järvinen bits++; 11731deeda8dSIlpo Järvinen cr1 |= USART_CR1_M0; 11741deeda8dSIlpo Järvinen } 11751deeda8dSIlpo Järvinen } 117648a6092fSMaxime Coquelin 11774cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 11782aa1bbb2SFabrice Gasnier (stm32_port->fifoen && 11792aa1bbb2SFabrice Gasnier stm32_port->rxftcfg >= 0))) { 11804cc0ed62SErwan Le Ray if (cflag & CSTOPB) 11814cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 11824cc0ed62SErwan Le Ray else 11834cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 11844cc0ed62SErwan Le Ray 11854cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 11864cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 11874cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 11884cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 118933bb2f6aSErwan Le Ray /* 119033bb2f6aSErwan Le Ray * Enable fifo threshold irq in two cases, either when there is no DMA, or when 119133bb2f6aSErwan Le Ray * wake up over usart, from low power until the DMA gets re-enabled by resume. 119233bb2f6aSErwan Le Ray */ 1193d0a6a7bcSErwan Le Ray stm32_port->cr3_irq = USART_CR3_RXFTIE; 11944cc0ed62SErwan Le Ray } 11954cc0ed62SErwan Le Ray 1196d0a6a7bcSErwan Le Ray cr1 |= stm32_port->cr1_irq; 1197d0a6a7bcSErwan Le Ray cr3 |= stm32_port->cr3_irq; 1198d0a6a7bcSErwan Le Ray 119948a6092fSMaxime Coquelin if (cflag & PARODD) 120048a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 120148a6092fSMaxime Coquelin 120248a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 120348a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 120448a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 120535abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 120648a6092fSMaxime Coquelin } 120748a6092fSMaxime Coquelin 120848a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 120948a6092fSMaxime Coquelin 121048a6092fSMaxime Coquelin /* 121148a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 121248a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 121348a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 121448a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 121548a6092fSMaxime Coquelin */ 121648a6092fSMaxime Coquelin if (usartdiv < 16) { 121748a6092fSMaxime Coquelin oversampling = 8; 12181bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 121956f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8); 122048a6092fSMaxime Coquelin } else { 122148a6092fSMaxime Coquelin oversampling = 16; 12221bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 122356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 122448a6092fSMaxime Coquelin } 122548a6092fSMaxime Coquelin 122648a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 122748a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 1228ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 122948a6092fSMaxime Coquelin 123048a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 123148a6092fSMaxime Coquelin 123248a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 123348a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 123448a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 123548a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 12364f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 123748a6092fSMaxime Coquelin 123848a6092fSMaxime Coquelin /* Characters to ignore */ 123948a6092fSMaxime Coquelin port->ignore_status_mask = 0; 124048a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 124148a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 124248a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 12434f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 124448a6092fSMaxime Coquelin /* 124548a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 124648a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 124748a6092fSMaxime Coquelin */ 124848a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 124948a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 125048a6092fSMaxime Coquelin } 125148a6092fSMaxime Coquelin 125248a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 125348a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 125448a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 125548a6092fSMaxime Coquelin 125633bb2f6aSErwan Le Ray if (stm32_port->rx_ch) { 125733bb2f6aSErwan Le Ray /* 125833bb2f6aSErwan Le Ray * Setup DMA to collect only valid data and enable error irqs. 125933bb2f6aSErwan Le Ray * This also enables break reception when using DMA. 126033bb2f6aSErwan Le Ray */ 126133bb2f6aSErwan Le Ray cr1 |= USART_CR1_PEIE; 126233bb2f6aSErwan Le Ray cr3 |= USART_CR3_EIE; 126334891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 126433bb2f6aSErwan Le Ray cr3 |= USART_CR3_DDRE; 126533bb2f6aSErwan Le Ray } 126634891872SAlexandre TORGUE 12671bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 126856f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 12691bcda09dSBich HEMON rs485conf->delay_rts_before_send, 127056f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 127156f9a76cSErwan Le Ray baud); 12721bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 12731bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 12741bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 12751bcda09dSBich HEMON } else { 12761bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 12771bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 12781bcda09dSBich HEMON } 12791bcda09dSBich HEMON 12801bcda09dSBich HEMON } else { 12811bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 12821bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 12831bcda09dSBich HEMON } 12841bcda09dSBich HEMON 128512761869SErwan Le Ray /* Configure wake up from low power on start bit detection */ 12863d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 128712761869SErwan Le Ray cr3 &= ~USART_CR3_WUS_MASK; 128812761869SErwan Le Ray cr3 |= USART_CR3_WUS_START_BIT; 128912761869SErwan Le Ray } 129012761869SErwan Le Ray 1291ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 1292ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 1293ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 129448a6092fSMaxime Coquelin 129556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 129648a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 1297436c9793SErwan Le Ray 1298436c9793SErwan Le Ray /* Handle modem control interrupts */ 1299436c9793SErwan Le Ray if (UART_ENABLE_MS(port, termios->c_cflag)) 1300436c9793SErwan Le Ray stm32_usart_enable_ms(port); 1301436c9793SErwan Le Ray else 1302436c9793SErwan Le Ray stm32_usart_disable_ms(port); 130348a6092fSMaxime Coquelin } 130448a6092fSMaxime Coquelin 130556f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port) 130648a6092fSMaxime Coquelin { 130748a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 130848a6092fSMaxime Coquelin } 130948a6092fSMaxime Coquelin 131056f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port) 131148a6092fSMaxime Coquelin { 131248a6092fSMaxime Coquelin } 131348a6092fSMaxime Coquelin 131456f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port) 131548a6092fSMaxime Coquelin { 131648a6092fSMaxime Coquelin return 0; 131748a6092fSMaxime Coquelin } 131848a6092fSMaxime Coquelin 131956f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags) 132048a6092fSMaxime Coquelin { 132148a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 132248a6092fSMaxime Coquelin port->type = PORT_STM32; 132348a6092fSMaxime Coquelin } 132448a6092fSMaxime Coquelin 132548a6092fSMaxime Coquelin static int 132656f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser) 132748a6092fSMaxime Coquelin { 132848a6092fSMaxime Coquelin /* No user changeable parameters */ 132948a6092fSMaxime Coquelin return -EINVAL; 133048a6092fSMaxime Coquelin } 133148a6092fSMaxime Coquelin 133256f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state, 133348a6092fSMaxime Coquelin unsigned int oldstate) 133448a6092fSMaxime Coquelin { 133548a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 133648a6092fSMaxime Coquelin struct stm32_port, port); 1337d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 1338d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32port->info->cfg; 133918ee37e1SJohan Hovold unsigned long flags; 134048a6092fSMaxime Coquelin 134148a6092fSMaxime Coquelin switch (state) { 134248a6092fSMaxime Coquelin case UART_PM_STATE_ON: 1343fb6dcef6SErwan Le Ray pm_runtime_get_sync(port->dev); 134448a6092fSMaxime Coquelin break; 134548a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 134648a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 134756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 134848a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 1349fb6dcef6SErwan Le Ray pm_runtime_put_sync(port->dev); 135048a6092fSMaxime Coquelin break; 135148a6092fSMaxime Coquelin } 135248a6092fSMaxime Coquelin } 135348a6092fSMaxime Coquelin 13541f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL) 13551f507b3aSValentin Caron 13561f507b3aSValentin Caron /* Callbacks for characters polling in debug context (i.e. KGDB). */ 13571f507b3aSValentin Caron static int stm32_usart_poll_init(struct uart_port *port) 13581f507b3aSValentin Caron { 13591f507b3aSValentin Caron struct stm32_port *stm32_port = to_stm32_port(port); 13601f507b3aSValentin Caron 13611f507b3aSValentin Caron return clk_prepare_enable(stm32_port->clk); 13621f507b3aSValentin Caron } 13631f507b3aSValentin Caron 13641f507b3aSValentin Caron static int stm32_usart_poll_get_char(struct uart_port *port) 13651f507b3aSValentin Caron { 13661f507b3aSValentin Caron struct stm32_port *stm32_port = to_stm32_port(port); 13671f507b3aSValentin Caron const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 13681f507b3aSValentin Caron 13691f507b3aSValentin Caron if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE)) 13701f507b3aSValentin Caron return NO_POLL_CHAR; 13711f507b3aSValentin Caron 13721f507b3aSValentin Caron return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask; 13731f507b3aSValentin Caron } 13741f507b3aSValentin Caron 13751f507b3aSValentin Caron static void stm32_usart_poll_put_char(struct uart_port *port, unsigned char ch) 13761f507b3aSValentin Caron { 13771f507b3aSValentin Caron stm32_usart_console_putchar(port, ch); 13781f507b3aSValentin Caron } 13791f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */ 13801f507b3aSValentin Caron 138148a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 138256f9a76cSErwan Le Ray .tx_empty = stm32_usart_tx_empty, 138356f9a76cSErwan Le Ray .set_mctrl = stm32_usart_set_mctrl, 138456f9a76cSErwan Le Ray .get_mctrl = stm32_usart_get_mctrl, 138556f9a76cSErwan Le Ray .stop_tx = stm32_usart_stop_tx, 138656f9a76cSErwan Le Ray .start_tx = stm32_usart_start_tx, 138756f9a76cSErwan Le Ray .throttle = stm32_usart_throttle, 138856f9a76cSErwan Le Ray .unthrottle = stm32_usart_unthrottle, 138956f9a76cSErwan Le Ray .stop_rx = stm32_usart_stop_rx, 139056f9a76cSErwan Le Ray .enable_ms = stm32_usart_enable_ms, 139156f9a76cSErwan Le Ray .break_ctl = stm32_usart_break_ctl, 139256f9a76cSErwan Le Ray .startup = stm32_usart_startup, 139356f9a76cSErwan Le Ray .shutdown = stm32_usart_shutdown, 13943d82be8bSErwan Le Ray .flush_buffer = stm32_usart_flush_buffer, 139556f9a76cSErwan Le Ray .set_termios = stm32_usart_set_termios, 139656f9a76cSErwan Le Ray .pm = stm32_usart_pm, 139756f9a76cSErwan Le Ray .type = stm32_usart_type, 139856f9a76cSErwan Le Ray .release_port = stm32_usart_release_port, 139956f9a76cSErwan Le Ray .request_port = stm32_usart_request_port, 140056f9a76cSErwan Le Ray .config_port = stm32_usart_config_port, 140156f9a76cSErwan Le Ray .verify_port = stm32_usart_verify_port, 14021f507b3aSValentin Caron #if defined(CONFIG_CONSOLE_POLL) 14031f507b3aSValentin Caron .poll_init = stm32_usart_poll_init, 14041f507b3aSValentin Caron .poll_get_char = stm32_usart_poll_get_char, 14051f507b3aSValentin Caron .poll_put_char = stm32_usart_poll_put_char, 14061f507b3aSValentin Caron #endif /* CONFIG_CONSOLE_POLL */ 140748a6092fSMaxime Coquelin }; 140848a6092fSMaxime Coquelin 14092aa1bbb2SFabrice Gasnier /* 14102aa1bbb2SFabrice Gasnier * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG) 14112aa1bbb2SFabrice Gasnier * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case, 14122aa1bbb2SFabrice Gasnier * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE. 14132aa1bbb2SFabrice Gasnier * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1. 14142aa1bbb2SFabrice Gasnier */ 14152aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 }; 14162aa1bbb2SFabrice Gasnier 14172aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p, 14182aa1bbb2SFabrice Gasnier int *ftcfg) 14192aa1bbb2SFabrice Gasnier { 14202aa1bbb2SFabrice Gasnier u32 bytes, i; 14212aa1bbb2SFabrice Gasnier 14222aa1bbb2SFabrice Gasnier /* DT option to get RX & TX FIFO threshold (default to 8 bytes) */ 14232aa1bbb2SFabrice Gasnier if (of_property_read_u32(pdev->dev.of_node, p, &bytes)) 14242aa1bbb2SFabrice Gasnier bytes = 8; 14252aa1bbb2SFabrice Gasnier 14262aa1bbb2SFabrice Gasnier for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++) 14272aa1bbb2SFabrice Gasnier if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes) 14282aa1bbb2SFabrice Gasnier break; 14292aa1bbb2SFabrice Gasnier if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg)) 14302aa1bbb2SFabrice Gasnier i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1; 14312aa1bbb2SFabrice Gasnier 14322aa1bbb2SFabrice Gasnier dev_dbg(&pdev->dev, "%s set to %d bytes\n", p, 14332aa1bbb2SFabrice Gasnier stm32h7_usart_fifo_thresh_cfg[i]); 14342aa1bbb2SFabrice Gasnier 14352aa1bbb2SFabrice Gasnier /* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */ 14362aa1bbb2SFabrice Gasnier if (i) 14372aa1bbb2SFabrice Gasnier *ftcfg = i - 1; 14382aa1bbb2SFabrice Gasnier else 14392aa1bbb2SFabrice Gasnier *ftcfg = -EINVAL; 14402aa1bbb2SFabrice Gasnier } 14412aa1bbb2SFabrice Gasnier 144297f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port) 144397f3a085SErwan Le Ray { 144497f3a085SErwan Le Ray clk_disable_unprepare(stm32port->clk); 144597f3a085SErwan Le Ray } 144697f3a085SErwan Le Ray 1447aeae8f22SIlpo Järvinen static const struct serial_rs485 stm32_rs485_supported = { 1448aeae8f22SIlpo Järvinen .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND | 1449aeae8f22SIlpo Järvinen SER_RS485_RX_DURING_TX, 1450aeae8f22SIlpo Järvinen .delay_rts_before_send = 1, 1451aeae8f22SIlpo Järvinen .delay_rts_after_send = 1, 1452aeae8f22SIlpo Järvinen }; 1453aeae8f22SIlpo Järvinen 145456f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port, 145548a6092fSMaxime Coquelin struct platform_device *pdev) 145648a6092fSMaxime Coquelin { 145748a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 145848a6092fSMaxime Coquelin struct resource *res; 1459e0f2a902SErwan Le Ray int ret, irq; 146048a6092fSMaxime Coquelin 1461e0f2a902SErwan Le Ray irq = platform_get_irq(pdev, 0); 1462217b04c6STang Bin if (irq < 0) 1463217b04c6STang Bin return irq; 146492fc0023SErwan Le Ray 146548a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 146648a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 146748a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 146848a6092fSMaxime Coquelin port->dev = &pdev->dev; 1469d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 14709feedaa7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE); 1471e0f2a902SErwan Le Ray port->irq = irq; 147256f9a76cSErwan Le Ray port->rs485_config = stm32_usart_config_rs485; 14730139da50SIlpo Järvinen port->rs485_supported = stm32_rs485_supported; 14747d8f6861SBich HEMON 147556f9a76cSErwan Le Ray ret = stm32_usart_init_rs485(port, pdev); 1476c150c0f3SLukas Wunner if (ret) 1477c150c0f3SLukas Wunner return ret; 14787d8f6861SBich HEMON 14793d530017SAlexandre Torgue stm32port->wakeup_src = stm32port->info->cfg.has_wakeup && 14803d530017SAlexandre Torgue of_property_read_bool(pdev->dev.of_node, "wakeup-source"); 14812c58e560SErwan Le Ray 14823cd66593SMartin Devera stm32port->swap = stm32port->info->cfg.has_swap && 14833cd66593SMartin Devera of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"); 14843cd66593SMartin Devera 1485351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 14862aa1bbb2SFabrice Gasnier if (stm32port->fifoen) { 14872aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "rx-threshold", 14882aa1bbb2SFabrice Gasnier &stm32port->rxftcfg); 14892aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "tx-threshold", 14902aa1bbb2SFabrice Gasnier &stm32port->txftcfg); 14912aa1bbb2SFabrice Gasnier } 149248a6092fSMaxime Coquelin 14933d881e32STang Bin port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 149448a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 149548a6092fSMaxime Coquelin return PTR_ERR(port->membase); 149648a6092fSMaxime Coquelin port->mapbase = res->start; 149748a6092fSMaxime Coquelin 149848a6092fSMaxime Coquelin spin_lock_init(&port->lock); 149948a6092fSMaxime Coquelin 150048a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 150148a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 150248a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 150348a6092fSMaxime Coquelin 150448a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 150548a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 150648a6092fSMaxime Coquelin if (ret) 150748a6092fSMaxime Coquelin return ret; 150848a6092fSMaxime Coquelin 150948a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 1510ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 151148a6092fSMaxime Coquelin ret = -EINVAL; 15126cf61b9bSManivannan Sadhasivam goto err_clk; 1513ada80043SFabrice Gasnier } 151448a6092fSMaxime Coquelin 15156cf61b9bSManivannan Sadhasivam stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0); 15166cf61b9bSManivannan Sadhasivam if (IS_ERR(stm32port->gpios)) { 15176cf61b9bSManivannan Sadhasivam ret = PTR_ERR(stm32port->gpios); 15186cf61b9bSManivannan Sadhasivam goto err_clk; 15196cf61b9bSManivannan Sadhasivam } 15206cf61b9bSManivannan Sadhasivam 15219359369aSErwan Le Ray /* 15229359369aSErwan Le Ray * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts" 15239359369aSErwan Le Ray * properties should not be specified. 15249359369aSErwan Le Ray */ 15256cf61b9bSManivannan Sadhasivam if (stm32port->hw_flow_control) { 15266cf61b9bSManivannan Sadhasivam if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) || 15276cf61b9bSManivannan Sadhasivam mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) { 15286cf61b9bSManivannan Sadhasivam dev_err(&pdev->dev, "Conflicting RTS/CTS config\n"); 15296cf61b9bSManivannan Sadhasivam ret = -EINVAL; 15306cf61b9bSManivannan Sadhasivam goto err_clk; 15316cf61b9bSManivannan Sadhasivam } 15326cf61b9bSManivannan Sadhasivam } 15336cf61b9bSManivannan Sadhasivam 15346cf61b9bSManivannan Sadhasivam return ret; 15356cf61b9bSManivannan Sadhasivam 15366cf61b9bSManivannan Sadhasivam err_clk: 15376cf61b9bSManivannan Sadhasivam clk_disable_unprepare(stm32port->clk); 15386cf61b9bSManivannan Sadhasivam 153948a6092fSMaxime Coquelin return ret; 154048a6092fSMaxime Coquelin } 154148a6092fSMaxime Coquelin 154256f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev) 154348a6092fSMaxime Coquelin { 154448a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 154548a6092fSMaxime Coquelin int id; 154648a6092fSMaxime Coquelin 154748a6092fSMaxime Coquelin if (!np) 154848a6092fSMaxime Coquelin return NULL; 154948a6092fSMaxime Coquelin 155048a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 1551e5707915SGerald Baeza if (id < 0) { 1552e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 1553e5707915SGerald Baeza return NULL; 1554e5707915SGerald Baeza } 155548a6092fSMaxime Coquelin 155648a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 155748a6092fSMaxime Coquelin return NULL; 155848a6092fSMaxime Coquelin 15596fd9fffbSErwan Le Ray stm32_ports[id].hw_flow_control = 15606fd9fffbSErwan Le Ray of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ || 15616fd9fffbSErwan Le Ray of_property_read_bool (np, "uart-has-rtscts"); 156248a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 15634cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 1564d0a6a7bcSErwan Le Ray stm32_ports[id].cr3_irq = 0; 1565e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 156648a6092fSMaxime Coquelin return &stm32_ports[id]; 156748a6092fSMaxime Coquelin } 156848a6092fSMaxime Coquelin 156948a6092fSMaxime Coquelin #ifdef CONFIG_OF 157048a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 1571ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 1572ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 1573270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 157448a6092fSMaxime Coquelin {}, 157548a6092fSMaxime Coquelin }; 157648a6092fSMaxime Coquelin 157748a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 157848a6092fSMaxime Coquelin #endif 157948a6092fSMaxime Coquelin 1580a7770a4bSErwan Le Ray static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port, 1581a7770a4bSErwan Le Ray struct platform_device *pdev) 1582a7770a4bSErwan Le Ray { 1583a7770a4bSErwan Le Ray if (stm32port->rx_buf) 1584a7770a4bSErwan Le Ray dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf, 1585a7770a4bSErwan Le Ray stm32port->rx_dma_buf); 1586a7770a4bSErwan Le Ray } 1587a7770a4bSErwan Le Ray 158856f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port, 158934891872SAlexandre TORGUE struct platform_device *pdev) 159034891872SAlexandre TORGUE { 1591d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 159234891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 159334891872SAlexandre TORGUE struct device *dev = &pdev->dev; 159434891872SAlexandre TORGUE struct dma_slave_config config; 159534891872SAlexandre TORGUE int ret; 159634891872SAlexandre TORGUE 1597e359b441SJohan Hovold /* 1598e359b441SJohan Hovold * Using DMA and threaded handler for the console could lead to 1599e359b441SJohan Hovold * deadlocks. 1600e359b441SJohan Hovold */ 1601e359b441SJohan Hovold if (uart_console(port)) 1602e359b441SJohan Hovold return -ENODEV; 1603e359b441SJohan Hovold 160459bd4eedSTang Bin stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L, 160534891872SAlexandre TORGUE &stm32port->rx_dma_buf, 160634891872SAlexandre TORGUE GFP_KERNEL); 1607a7770a4bSErwan Le Ray if (!stm32port->rx_buf) 1608a7770a4bSErwan Le Ray return -ENOMEM; 160934891872SAlexandre TORGUE 161034891872SAlexandre TORGUE /* Configure DMA channel */ 161134891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 16128e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 161334891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 161434891872SAlexandre TORGUE 161534891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 161634891872SAlexandre TORGUE if (ret < 0) { 161734891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 1618a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32port, pdev); 1619a7770a4bSErwan Le Ray return ret; 162034891872SAlexandre TORGUE } 162134891872SAlexandre TORGUE 162234891872SAlexandre TORGUE return 0; 1623a7770a4bSErwan Le Ray } 162434891872SAlexandre TORGUE 1625a7770a4bSErwan Le Ray static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port, 1626a7770a4bSErwan Le Ray struct platform_device *pdev) 1627a7770a4bSErwan Le Ray { 1628a7770a4bSErwan Le Ray if (stm32port->tx_buf) 1629a7770a4bSErwan Le Ray dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf, 1630a7770a4bSErwan Le Ray stm32port->tx_dma_buf); 163134891872SAlexandre TORGUE } 163234891872SAlexandre TORGUE 163356f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port, 163434891872SAlexandre TORGUE struct platform_device *pdev) 163534891872SAlexandre TORGUE { 1636d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 163734891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 163834891872SAlexandre TORGUE struct device *dev = &pdev->dev; 163934891872SAlexandre TORGUE struct dma_slave_config config; 164034891872SAlexandre TORGUE int ret; 164134891872SAlexandre TORGUE 164259bd4eedSTang Bin stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L, 164334891872SAlexandre TORGUE &stm32port->tx_dma_buf, 164434891872SAlexandre TORGUE GFP_KERNEL); 1645a7770a4bSErwan Le Ray if (!stm32port->tx_buf) 1646a7770a4bSErwan Le Ray return -ENOMEM; 164734891872SAlexandre TORGUE 164834891872SAlexandre TORGUE /* Configure DMA channel */ 164934891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 16508e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 165134891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 165234891872SAlexandre TORGUE 165334891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 165434891872SAlexandre TORGUE if (ret < 0) { 165534891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 1656a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32port, pdev); 1657a7770a4bSErwan Le Ray return ret; 165834891872SAlexandre TORGUE } 165934891872SAlexandre TORGUE 166034891872SAlexandre TORGUE return 0; 166134891872SAlexandre TORGUE } 166234891872SAlexandre TORGUE 166356f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev) 166448a6092fSMaxime Coquelin { 166548a6092fSMaxime Coquelin struct stm32_port *stm32port; 1666ada8618fSAlexandre TORGUE int ret; 166748a6092fSMaxime Coquelin 166856f9a76cSErwan Le Ray stm32port = stm32_usart_of_get_port(pdev); 166948a6092fSMaxime Coquelin if (!stm32port) 167048a6092fSMaxime Coquelin return -ENODEV; 167148a6092fSMaxime Coquelin 1672d825f0beSStephen Boyd stm32port->info = of_device_get_match_data(&pdev->dev); 1673d825f0beSStephen Boyd if (!stm32port->info) 1674ada8618fSAlexandre TORGUE return -EINVAL; 1675ada8618fSAlexandre TORGUE 167656f9a76cSErwan Le Ray ret = stm32_usart_init_port(stm32port, pdev); 167748a6092fSMaxime Coquelin if (ret) 167848a6092fSMaxime Coquelin return ret; 167948a6092fSMaxime Coquelin 16803d530017SAlexandre Torgue if (stm32port->wakeup_src) { 16813d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, true); 16823d530017SAlexandre Torgue ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq); 16835297f274SErwan Le Ray if (ret) 1684a7770a4bSErwan Le Ray goto err_deinit_port; 1685270e5a74SFabrice Gasnier } 1686270e5a74SFabrice Gasnier 1687a7770a4bSErwan Le Ray stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx"); 1688a7770a4bSErwan Le Ray if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER) { 1689a7770a4bSErwan Le Ray ret = -EPROBE_DEFER; 1690a7770a4bSErwan Le Ray goto err_wakeirq; 1691a7770a4bSErwan Le Ray } 1692a7770a4bSErwan Le Ray /* Fall back in interrupt mode for any non-deferral error */ 1693a7770a4bSErwan Le Ray if (IS_ERR(stm32port->rx_ch)) 1694a7770a4bSErwan Le Ray stm32port->rx_ch = NULL; 169534891872SAlexandre TORGUE 1696a7770a4bSErwan Le Ray stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx"); 1697a7770a4bSErwan Le Ray if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) { 1698a7770a4bSErwan Le Ray ret = -EPROBE_DEFER; 1699a7770a4bSErwan Le Ray goto err_dma_rx; 1700a7770a4bSErwan Le Ray } 1701a7770a4bSErwan Le Ray /* Fall back in interrupt mode for any non-deferral error */ 1702a7770a4bSErwan Le Ray if (IS_ERR(stm32port->tx_ch)) 1703a7770a4bSErwan Le Ray stm32port->tx_ch = NULL; 1704a7770a4bSErwan Le Ray 1705a7770a4bSErwan Le Ray if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) { 1706a7770a4bSErwan Le Ray /* Fall back in interrupt mode */ 1707a7770a4bSErwan Le Ray dma_release_channel(stm32port->rx_ch); 1708a7770a4bSErwan Le Ray stm32port->rx_ch = NULL; 1709a7770a4bSErwan Le Ray } 1710a7770a4bSErwan Le Ray 1711a7770a4bSErwan Le Ray if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) { 1712a7770a4bSErwan Le Ray /* Fall back in interrupt mode */ 1713a7770a4bSErwan Le Ray dma_release_channel(stm32port->tx_ch); 1714a7770a4bSErwan Le Ray stm32port->tx_ch = NULL; 1715a7770a4bSErwan Le Ray } 1716a7770a4bSErwan Le Ray 1717a7770a4bSErwan Le Ray if (!stm32port->rx_ch) 1718a7770a4bSErwan Le Ray dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n"); 1719a7770a4bSErwan Le Ray if (!stm32port->tx_ch) 1720a7770a4bSErwan Le Ray dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n"); 172134891872SAlexandre TORGUE 172248a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 172348a6092fSMaxime Coquelin 1724fb6dcef6SErwan Le Ray pm_runtime_get_noresume(&pdev->dev); 1725fb6dcef6SErwan Le Ray pm_runtime_set_active(&pdev->dev); 1726fb6dcef6SErwan Le Ray pm_runtime_enable(&pdev->dev); 172787fd0741SErwan Le Ray 172887fd0741SErwan Le Ray ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 172987fd0741SErwan Le Ray if (ret) 173087fd0741SErwan Le Ray goto err_port; 173187fd0741SErwan Le Ray 1732fb6dcef6SErwan Le Ray pm_runtime_put_sync(&pdev->dev); 1733fb6dcef6SErwan Le Ray 173448a6092fSMaxime Coquelin return 0; 1735ada80043SFabrice Gasnier 173687fd0741SErwan Le Ray err_port: 173787fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 173887fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 173987fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 174087fd0741SErwan Le Ray 174187fd0741SErwan Le Ray if (stm32port->tx_ch) { 1742a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32port, pdev); 174387fd0741SErwan Le Ray dma_release_channel(stm32port->tx_ch); 174487fd0741SErwan Le Ray } 174587fd0741SErwan Le Ray 1746a7770a4bSErwan Le Ray if (stm32port->rx_ch) 1747a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32port, pdev); 174887fd0741SErwan Le Ray 1749a7770a4bSErwan Le Ray err_dma_rx: 1750a7770a4bSErwan Le Ray if (stm32port->rx_ch) 1751a7770a4bSErwan Le Ray dma_release_channel(stm32port->rx_ch); 1752a7770a4bSErwan Le Ray 1753a7770a4bSErwan Le Ray err_wakeirq: 17543d530017SAlexandre Torgue if (stm32port->wakeup_src) 17555297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 17565297f274SErwan Le Ray 1757a7770a4bSErwan Le Ray err_deinit_port: 17583d530017SAlexandre Torgue if (stm32port->wakeup_src) 17593d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, false); 1760270e5a74SFabrice Gasnier 176197f3a085SErwan Le Ray stm32_usart_deinit_port(stm32port); 1762ada80043SFabrice Gasnier 1763ada80043SFabrice Gasnier return ret; 176448a6092fSMaxime Coquelin } 176548a6092fSMaxime Coquelin 176656f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev) 176748a6092fSMaxime Coquelin { 176848a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1769511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1770d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1771fb6dcef6SErwan Le Ray int err; 177233bb2f6aSErwan Le Ray u32 cr3; 1773fb6dcef6SErwan Le Ray 1774fb6dcef6SErwan Le Ray pm_runtime_get_sync(&pdev->dev); 177587fd0741SErwan Le Ray err = uart_remove_one_port(&stm32_usart_driver, port); 177687fd0741SErwan Le Ray if (err) 177787fd0741SErwan Le Ray return(err); 177887fd0741SErwan Le Ray 177987fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 178087fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 178187fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 178234891872SAlexandre TORGUE 178333bb2f6aSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE); 178433bb2f6aSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 178533bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_EIE; 178633bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_DMAR; 178733bb2f6aSErwan Le Ray cr3 &= ~USART_CR3_DDRE; 178833bb2f6aSErwan Le Ray writel_relaxed(cr3, port->membase + ofs->cr3); 178934891872SAlexandre TORGUE 179087fd0741SErwan Le Ray if (stm32_port->tx_ch) { 1791a7770a4bSErwan Le Ray stm32_usart_of_dma_tx_remove(stm32_port, pdev); 179234891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 179387fd0741SErwan Le Ray } 179434891872SAlexandre TORGUE 1795a7770a4bSErwan Le Ray if (stm32_port->rx_ch) { 1796a7770a4bSErwan Le Ray stm32_usart_of_dma_rx_remove(stm32_port, pdev); 1797a7770a4bSErwan Le Ray dma_release_channel(stm32_port->rx_ch); 1798a7770a4bSErwan Le Ray } 1799a7770a4bSErwan Le Ray 1800a7770a4bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 1801511c7b1bSAlexandre TORGUE 18023d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 18035297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1804270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 18055297f274SErwan Le Ray } 1806270e5a74SFabrice Gasnier 180797f3a085SErwan Le Ray stm32_usart_deinit_port(stm32_port); 180848a6092fSMaxime Coquelin 180987fd0741SErwan Le Ray return 0; 181048a6092fSMaxime Coquelin } 181148a6092fSMaxime Coquelin 18121f507b3aSValentin Caron static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch) 181348a6092fSMaxime Coquelin { 1814ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1815d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 181628fb1a92SValentin Caron u32 isr; 181728fb1a92SValentin Caron int ret; 1818ada8618fSAlexandre TORGUE 181928fb1a92SValentin Caron ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr, 182028fb1a92SValentin Caron (isr & USART_SR_TXE), 100, 182128fb1a92SValentin Caron STM32_USART_TIMEOUT_USEC); 182228fb1a92SValentin Caron if (ret != 0) { 182328fb1a92SValentin Caron dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret); 182428fb1a92SValentin Caron return; 182528fb1a92SValentin Caron } 1826ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 182748a6092fSMaxime Coquelin } 182848a6092fSMaxime Coquelin 18291f507b3aSValentin Caron #ifdef CONFIG_SERIAL_STM32_CONSOLE 183056f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s, 183192fc0023SErwan Le Ray unsigned int cnt) 183248a6092fSMaxime Coquelin { 183348a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1834ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1835d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1836d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 183748a6092fSMaxime Coquelin unsigned long flags; 183848a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 183948a6092fSMaxime Coquelin int locked = 1; 184048a6092fSMaxime Coquelin 1841cea37afdSJohan Hovold if (oops_in_progress) 1842cea37afdSJohan Hovold locked = spin_trylock_irqsave(&port->lock, flags); 184348a6092fSMaxime Coquelin else 1844cea37afdSJohan Hovold spin_lock_irqsave(&port->lock, flags); 184548a6092fSMaxime Coquelin 184687f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1847ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 184848a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 184987f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1850ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 185148a6092fSMaxime Coquelin 185256f9a76cSErwan Le Ray uart_console_write(port, s, cnt, stm32_usart_console_putchar); 185348a6092fSMaxime Coquelin 185448a6092fSMaxime Coquelin /* Restore interrupt state */ 1855ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 185648a6092fSMaxime Coquelin 185748a6092fSMaxime Coquelin if (locked) 1858cea37afdSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 185948a6092fSMaxime Coquelin } 186048a6092fSMaxime Coquelin 186156f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options) 186248a6092fSMaxime Coquelin { 186348a6092fSMaxime Coquelin struct stm32_port *stm32port; 186448a6092fSMaxime Coquelin int baud = 9600; 186548a6092fSMaxime Coquelin int bits = 8; 186648a6092fSMaxime Coquelin int parity = 'n'; 186748a6092fSMaxime Coquelin int flow = 'n'; 186848a6092fSMaxime Coquelin 186948a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 187048a6092fSMaxime Coquelin return -ENODEV; 187148a6092fSMaxime Coquelin 187248a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 187348a6092fSMaxime Coquelin 187448a6092fSMaxime Coquelin /* 187548a6092fSMaxime Coquelin * This driver does not support early console initialization 187648a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 187748a6092fSMaxime Coquelin * this to be called during the uart port registration when the 187848a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 187948a6092fSMaxime Coquelin */ 188092fc0023SErwan Le Ray if (stm32port->port.mapbase == 0 || !stm32port->port.membase) 188148a6092fSMaxime Coquelin return -ENXIO; 188248a6092fSMaxime Coquelin 188348a6092fSMaxime Coquelin if (options) 188448a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 188548a6092fSMaxime Coquelin 188648a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 188748a6092fSMaxime Coquelin } 188848a6092fSMaxime Coquelin 188948a6092fSMaxime Coquelin static struct console stm32_console = { 189048a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 189148a6092fSMaxime Coquelin .device = uart_console_device, 189256f9a76cSErwan Le Ray .write = stm32_usart_console_write, 189356f9a76cSErwan Le Ray .setup = stm32_usart_console_setup, 189448a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 189548a6092fSMaxime Coquelin .index = -1, 189648a6092fSMaxime Coquelin .data = &stm32_usart_driver, 189748a6092fSMaxime Coquelin }; 189848a6092fSMaxime Coquelin 189948a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 190048a6092fSMaxime Coquelin 190148a6092fSMaxime Coquelin #else 190248a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 190348a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 190448a6092fSMaxime Coquelin 19058043b16fSValentin Caron #ifdef CONFIG_SERIAL_EARLYCON 19068043b16fSValentin Caron static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch) 19078043b16fSValentin Caron { 19088043b16fSValentin Caron struct stm32_usart_info *info = port->private_data; 19098043b16fSValentin Caron 19108043b16fSValentin Caron while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE)) 19118043b16fSValentin Caron cpu_relax(); 19128043b16fSValentin Caron 19138043b16fSValentin Caron writel_relaxed(ch, port->membase + info->ofs.tdr); 19148043b16fSValentin Caron } 19158043b16fSValentin Caron 19168043b16fSValentin Caron static void early_stm32_serial_write(struct console *console, const char *s, unsigned int count) 19178043b16fSValentin Caron { 19188043b16fSValentin Caron struct earlycon_device *device = console->data; 19198043b16fSValentin Caron struct uart_port *port = &device->port; 19208043b16fSValentin Caron 19218043b16fSValentin Caron uart_console_write(port, s, count, early_stm32_usart_console_putchar); 19228043b16fSValentin Caron } 19238043b16fSValentin Caron 19248043b16fSValentin Caron static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options) 19258043b16fSValentin Caron { 19268043b16fSValentin Caron if (!(device->port.membase || device->port.iobase)) 19278043b16fSValentin Caron return -ENODEV; 19288043b16fSValentin Caron device->port.private_data = &stm32h7_info; 19298043b16fSValentin Caron device->con->write = early_stm32_serial_write; 19308043b16fSValentin Caron return 0; 19318043b16fSValentin Caron } 19328043b16fSValentin Caron 19338043b16fSValentin Caron static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options) 19348043b16fSValentin Caron { 19358043b16fSValentin Caron if (!(device->port.membase || device->port.iobase)) 19368043b16fSValentin Caron return -ENODEV; 19378043b16fSValentin Caron device->port.private_data = &stm32f7_info; 19388043b16fSValentin Caron device->con->write = early_stm32_serial_write; 19398043b16fSValentin Caron return 0; 19408043b16fSValentin Caron } 19418043b16fSValentin Caron 19428043b16fSValentin Caron static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options) 19438043b16fSValentin Caron { 19448043b16fSValentin Caron if (!(device->port.membase || device->port.iobase)) 19458043b16fSValentin Caron return -ENODEV; 19468043b16fSValentin Caron device->port.private_data = &stm32f4_info; 19478043b16fSValentin Caron device->con->write = early_stm32_serial_write; 19488043b16fSValentin Caron return 0; 19498043b16fSValentin Caron } 19508043b16fSValentin Caron 19518043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup); 19528043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup); 19538043b16fSValentin Caron OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup); 19548043b16fSValentin Caron #endif /* CONFIG_SERIAL_EARLYCON */ 19558043b16fSValentin Caron 195648a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 195748a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 195848a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 195948a6092fSMaxime Coquelin .major = 0, 196048a6092fSMaxime Coquelin .minor = 0, 196148a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 196248a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 196348a6092fSMaxime Coquelin }; 196448a6092fSMaxime Coquelin 19656eeb348cSErwan Le Ray static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port, 1966fe94347dSErwan Le Ray bool enable) 1967270e5a74SFabrice Gasnier { 1968270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1969d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 19706eeb348cSErwan Le Ray struct tty_port *tport = &port->state->port; 19716eeb348cSErwan Le Ray int ret; 19726333a485SErwan Le Ray unsigned int size; 19736333a485SErwan Le Ray unsigned long flags; 1974270e5a74SFabrice Gasnier 19756eeb348cSErwan Le Ray if (!stm32_port->wakeup_src || !tty_port_initialized(tport)) 19766eeb348cSErwan Le Ray return 0; 1977270e5a74SFabrice Gasnier 197812761869SErwan Le Ray /* 197912761869SErwan Le Ray * Enable low-power wake-up and wake-up irq if argument is set to 198012761869SErwan Le Ray * "enable", disable low-power wake-up and wake-up irq otherwise 198112761869SErwan Le Ray */ 1982270e5a74SFabrice Gasnier if (enable) { 198356f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM); 198412761869SErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE); 19857547d9abSErwan Le Ray mctrl_gpio_enable_irq_wake(stm32_port->gpios); 19866eeb348cSErwan Le Ray 19876eeb348cSErwan Le Ray /* 19886eeb348cSErwan Le Ray * When DMA is used for reception, it must be disabled before 19896eeb348cSErwan Le Ray * entering low-power mode and re-enabled when exiting from 19906eeb348cSErwan Le Ray * low-power mode. 19916eeb348cSErwan Le Ray */ 19926eeb348cSErwan Le Ray if (stm32_port->rx_ch) { 19936333a485SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 19946333a485SErwan Le Ray /* Avoid race with RX IRQ when DMAR is cleared */ 19956eeb348cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 19966333a485SErwan Le Ray /* Poll data from DMA RX buffer if any */ 19976333a485SErwan Le Ray size = stm32_usart_receive_chars(port, true); 19986333a485SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 19996333a485SErwan Le Ray uart_unlock_and_check_sysrq_irqrestore(port, flags); 20006333a485SErwan Le Ray if (size) 20016333a485SErwan Le Ray tty_flip_buffer_push(tport); 20026eeb348cSErwan Le Ray } 20036eeb348cSErwan Le Ray 20046eeb348cSErwan Le Ray /* Poll data from RX FIFO if any */ 20056eeb348cSErwan Le Ray stm32_usart_receive_chars(port, false); 2006270e5a74SFabrice Gasnier } else { 20076eeb348cSErwan Le Ray if (stm32_port->rx_ch) { 20086eeb348cSErwan Le Ray ret = stm32_usart_start_rx_dma_cyclic(port); 20096eeb348cSErwan Le Ray if (ret) 20106eeb348cSErwan Le Ray return ret; 20116eeb348cSErwan Le Ray } 20127547d9abSErwan Le Ray mctrl_gpio_disable_irq_wake(stm32_port->gpios); 201356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM); 201412761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 2015270e5a74SFabrice Gasnier } 20166eeb348cSErwan Le Ray 20176eeb348cSErwan Le Ray return 0; 2018270e5a74SFabrice Gasnier } 2019270e5a74SFabrice Gasnier 202056f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev) 2021270e5a74SFabrice Gasnier { 2022270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 20236eeb348cSErwan Le Ray int ret; 2024270e5a74SFabrice Gasnier 2025270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 2026270e5a74SFabrice Gasnier 20276eeb348cSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) { 20286eeb348cSErwan Le Ray ret = stm32_usart_serial_en_wakeup(port, true); 20296eeb348cSErwan Le Ray if (ret) 20306eeb348cSErwan Le Ray return ret; 20316eeb348cSErwan Le Ray } 2032270e5a74SFabrice Gasnier 203355484fccSErwan Le Ray /* 203455484fccSErwan Le Ray * When "no_console_suspend" is enabled, keep the pinctrl default state 203555484fccSErwan Le Ray * and rely on bootloader stage to restore this state upon resume. 203655484fccSErwan Le Ray * Otherwise, apply the idle or sleep states depending on wakeup 203755484fccSErwan Le Ray * capabilities. 203855484fccSErwan Le Ray */ 203955484fccSErwan Le Ray if (console_suspend_enabled || !uart_console(port)) { 20401631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 204155484fccSErwan Le Ray pinctrl_pm_select_idle_state(dev); 204255484fccSErwan Le Ray else 204394616d9aSErwan Le Ray pinctrl_pm_select_sleep_state(dev); 204455484fccSErwan Le Ray } 204594616d9aSErwan Le Ray 2046270e5a74SFabrice Gasnier return 0; 2047270e5a74SFabrice Gasnier } 2048270e5a74SFabrice Gasnier 204956f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev) 2050270e5a74SFabrice Gasnier { 2051270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 20526eeb348cSErwan Le Ray int ret; 2053270e5a74SFabrice Gasnier 205494616d9aSErwan Le Ray pinctrl_pm_select_default_state(dev); 205594616d9aSErwan Le Ray 20566eeb348cSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) { 20576eeb348cSErwan Le Ray ret = stm32_usart_serial_en_wakeup(port, false); 20586eeb348cSErwan Le Ray if (ret) 20596eeb348cSErwan Le Ray return ret; 20606eeb348cSErwan Le Ray } 2061270e5a74SFabrice Gasnier 2062270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 2063270e5a74SFabrice Gasnier } 2064270e5a74SFabrice Gasnier 206556f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev) 2066fb6dcef6SErwan Le Ray { 2067fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 2068fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 2069fb6dcef6SErwan Le Ray struct stm32_port, port); 2070fb6dcef6SErwan Le Ray 2071fb6dcef6SErwan Le Ray clk_disable_unprepare(stm32port->clk); 2072fb6dcef6SErwan Le Ray 2073fb6dcef6SErwan Le Ray return 0; 2074fb6dcef6SErwan Le Ray } 2075fb6dcef6SErwan Le Ray 207656f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev) 2077fb6dcef6SErwan Le Ray { 2078fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 2079fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 2080fb6dcef6SErwan Le Ray struct stm32_port, port); 2081fb6dcef6SErwan Le Ray 2082fb6dcef6SErwan Le Ray return clk_prepare_enable(stm32port->clk); 2083fb6dcef6SErwan Le Ray } 2084fb6dcef6SErwan Le Ray 2085270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 208656f9a76cSErwan Le Ray SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend, 208756f9a76cSErwan Le Ray stm32_usart_runtime_resume, NULL) 208856f9a76cSErwan Le Ray SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend, 208956f9a76cSErwan Le Ray stm32_usart_serial_resume) 2090270e5a74SFabrice Gasnier }; 2091270e5a74SFabrice Gasnier 209248a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 209356f9a76cSErwan Le Ray .probe = stm32_usart_serial_probe, 209456f9a76cSErwan Le Ray .remove = stm32_usart_serial_remove, 209548a6092fSMaxime Coquelin .driver = { 209648a6092fSMaxime Coquelin .name = DRIVER_NAME, 2097270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 209848a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 209948a6092fSMaxime Coquelin }, 210048a6092fSMaxime Coquelin }; 210148a6092fSMaxime Coquelin 210256f9a76cSErwan Le Ray static int __init stm32_usart_init(void) 210348a6092fSMaxime Coquelin { 210448a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 210548a6092fSMaxime Coquelin int ret; 210648a6092fSMaxime Coquelin 210748a6092fSMaxime Coquelin pr_info("%s\n", banner); 210848a6092fSMaxime Coquelin 210948a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 211048a6092fSMaxime Coquelin if (ret) 211148a6092fSMaxime Coquelin return ret; 211248a6092fSMaxime Coquelin 211348a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 211448a6092fSMaxime Coquelin if (ret) 211548a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 211648a6092fSMaxime Coquelin 211748a6092fSMaxime Coquelin return ret; 211848a6092fSMaxime Coquelin } 211948a6092fSMaxime Coquelin 212056f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void) 212148a6092fSMaxime Coquelin { 212248a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 212348a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 212448a6092fSMaxime Coquelin } 212548a6092fSMaxime Coquelin 212656f9a76cSErwan Le Ray module_init(stm32_usart_init); 212756f9a76cSErwan Le Ray module_exit(stm32_usart_exit); 212848a6092fSMaxime Coquelin 212948a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 213048a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 213148a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 2132