1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 248a6092fSMaxime Coquelin /* 348a6092fSMaxime Coquelin * Copyright (C) Maxime Coquelin 2015 43e5fcbacSBich HEMON * Copyright (C) STMicroelectronics SA 2017 5ada8618fSAlexandre TORGUE * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com> 68ebd9665SErwan Le Ray * Gerald Baeza <gerald.baeza@foss.st.com> 78ebd9665SErwan Le Ray * Erwan Le Ray <erwan.leray@foss.st.com> 848a6092fSMaxime Coquelin * 948a6092fSMaxime Coquelin * Inspired by st-asc.c from STMicroelectronics (c) 1048a6092fSMaxime Coquelin */ 1148a6092fSMaxime Coquelin 1234891872SAlexandre TORGUE #include <linux/clk.h> 1348a6092fSMaxime Coquelin #include <linux/console.h> 1448a6092fSMaxime Coquelin #include <linux/delay.h> 1534891872SAlexandre TORGUE #include <linux/dma-direction.h> 1634891872SAlexandre TORGUE #include <linux/dmaengine.h> 1734891872SAlexandre TORGUE #include <linux/dma-mapping.h> 1834891872SAlexandre TORGUE #include <linux/io.h> 1934891872SAlexandre TORGUE #include <linux/iopoll.h> 2034891872SAlexandre TORGUE #include <linux/irq.h> 2134891872SAlexandre TORGUE #include <linux/module.h> 2248a6092fSMaxime Coquelin #include <linux/of.h> 2348a6092fSMaxime Coquelin #include <linux/of_platform.h> 2494616d9aSErwan Le Ray #include <linux/pinctrl/consumer.h> 2534891872SAlexandre TORGUE #include <linux/platform_device.h> 2634891872SAlexandre TORGUE #include <linux/pm_runtime.h> 27270e5a74SFabrice Gasnier #include <linux/pm_wakeirq.h> 2848a6092fSMaxime Coquelin #include <linux/serial_core.h> 2934891872SAlexandre TORGUE #include <linux/serial.h> 3034891872SAlexandre TORGUE #include <linux/spinlock.h> 3134891872SAlexandre TORGUE #include <linux/sysrq.h> 3234891872SAlexandre TORGUE #include <linux/tty_flip.h> 3334891872SAlexandre TORGUE #include <linux/tty.h> 3448a6092fSMaxime Coquelin 356cf61b9bSManivannan Sadhasivam #include "serial_mctrl_gpio.h" 36bc5a0b55SAlexandre TORGUE #include "stm32-usart.h" 3748a6092fSMaxime Coquelin 3856f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port); 3956f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port); 4048a6092fSMaxime Coquelin 4148a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 4248a6092fSMaxime Coquelin { 4348a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 4448a6092fSMaxime Coquelin } 4548a6092fSMaxime Coquelin 4656f9a76cSErwan Le Ray static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits) 4748a6092fSMaxime Coquelin { 4848a6092fSMaxime Coquelin u32 val; 4948a6092fSMaxime Coquelin 5048a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5148a6092fSMaxime Coquelin val |= bits; 5248a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 5348a6092fSMaxime Coquelin } 5448a6092fSMaxime Coquelin 5556f9a76cSErwan Le Ray static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits) 5648a6092fSMaxime Coquelin { 5748a6092fSMaxime Coquelin u32 val; 5848a6092fSMaxime Coquelin 5948a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 6048a6092fSMaxime Coquelin val &= ~bits; 6148a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 6248a6092fSMaxime Coquelin } 6348a6092fSMaxime Coquelin 6456f9a76cSErwan Le Ray static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, 651bcda09dSBich HEMON u32 delay_DDE, u32 baud) 661bcda09dSBich HEMON { 671bcda09dSBich HEMON u32 rs485_deat_dedt; 681bcda09dSBich HEMON u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); 691bcda09dSBich HEMON bool over8; 701bcda09dSBich HEMON 711bcda09dSBich HEMON *cr3 |= USART_CR3_DEM; 721bcda09dSBich HEMON over8 = *cr1 & USART_CR1_OVER8; 731bcda09dSBich HEMON 741bcda09dSBich HEMON if (over8) 751bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 8; 761bcda09dSBich HEMON else 771bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 16; 781bcda09dSBich HEMON 791bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 801bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 811bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 821bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & 831bcda09dSBich HEMON USART_CR1_DEAT_MASK; 841bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 851bcda09dSBich HEMON 861bcda09dSBich HEMON if (over8) 871bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 8; 881bcda09dSBich HEMON else 891bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 16; 901bcda09dSBich HEMON 911bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 921bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 931bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 941bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & 951bcda09dSBich HEMON USART_CR1_DEDT_MASK; 961bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 971bcda09dSBich HEMON } 981bcda09dSBich HEMON 9956f9a76cSErwan Le Ray static int stm32_usart_config_rs485(struct uart_port *port, 1001bcda09dSBich HEMON struct serial_rs485 *rs485conf) 1011bcda09dSBich HEMON { 1021bcda09dSBich HEMON struct stm32_port *stm32_port = to_stm32_port(port); 103d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 104d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1051bcda09dSBich HEMON u32 usartdiv, baud, cr1, cr3; 1061bcda09dSBich HEMON bool over8; 1071bcda09dSBich HEMON 10856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1091bcda09dSBich HEMON 1101bcda09dSBich HEMON port->rs485 = *rs485conf; 1111bcda09dSBich HEMON 1121bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RX_DURING_TX; 1131bcda09dSBich HEMON 1141bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 1151bcda09dSBich HEMON cr1 = readl_relaxed(port->membase + ofs->cr1); 1161bcda09dSBich HEMON cr3 = readl_relaxed(port->membase + ofs->cr3); 1171bcda09dSBich HEMON usartdiv = readl_relaxed(port->membase + ofs->brr); 1181bcda09dSBich HEMON usartdiv = usartdiv & GENMASK(15, 0); 1191bcda09dSBich HEMON over8 = cr1 & USART_CR1_OVER8; 1201bcda09dSBich HEMON 1211bcda09dSBich HEMON if (over8) 1221bcda09dSBich HEMON usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) 1231bcda09dSBich HEMON << USART_BRR_04_R_SHIFT; 1241bcda09dSBich HEMON 1251bcda09dSBich HEMON baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); 12656f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 1271bcda09dSBich HEMON rs485conf->delay_rts_before_send, 12856f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 12956f9a76cSErwan Le Ray baud); 1301bcda09dSBich HEMON 1311bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 1321bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 1331bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 1341bcda09dSBich HEMON } else { 1351bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 1361bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 1371bcda09dSBich HEMON } 1381bcda09dSBich HEMON 1391bcda09dSBich HEMON writel_relaxed(cr3, port->membase + ofs->cr3); 1401bcda09dSBich HEMON writel_relaxed(cr1, port->membase + ofs->cr1); 1411bcda09dSBich HEMON } else { 14256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, 14356f9a76cSErwan Le Ray USART_CR3_DEM | USART_CR3_DEP); 14456f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, 1451bcda09dSBich HEMON USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 1461bcda09dSBich HEMON } 1471bcda09dSBich HEMON 14856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1491bcda09dSBich HEMON 1501bcda09dSBich HEMON return 0; 1511bcda09dSBich HEMON } 1521bcda09dSBich HEMON 15356f9a76cSErwan Le Ray static int stm32_usart_init_rs485(struct uart_port *port, 1541bcda09dSBich HEMON struct platform_device *pdev) 1551bcda09dSBich HEMON { 1561bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 1571bcda09dSBich HEMON 1581bcda09dSBich HEMON rs485conf->flags = 0; 1591bcda09dSBich HEMON rs485conf->delay_rts_before_send = 0; 1601bcda09dSBich HEMON rs485conf->delay_rts_after_send = 0; 1611bcda09dSBich HEMON 1621bcda09dSBich HEMON if (!pdev->dev.of_node) 1631bcda09dSBich HEMON return -ENODEV; 1641bcda09dSBich HEMON 165c150c0f3SLukas Wunner return uart_get_rs485_mode(port); 1661bcda09dSBich HEMON } 1671bcda09dSBich HEMON 16856f9a76cSErwan Le Ray static int stm32_usart_pending_rx(struct uart_port *port, u32 *sr, 16956f9a76cSErwan Le Ray int *last_res, bool threaded) 17034891872SAlexandre TORGUE { 17134891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 172d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 17334891872SAlexandre TORGUE enum dma_status status; 17434891872SAlexandre TORGUE struct dma_tx_state state; 17534891872SAlexandre TORGUE 17634891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 17734891872SAlexandre TORGUE 17834891872SAlexandre TORGUE if (threaded && stm32_port->rx_ch) { 17934891872SAlexandre TORGUE status = dmaengine_tx_status(stm32_port->rx_ch, 18034891872SAlexandre TORGUE stm32_port->rx_ch->cookie, 18134891872SAlexandre TORGUE &state); 18292fc0023SErwan Le Ray if (status == DMA_IN_PROGRESS && (*last_res != state.residue)) 18334891872SAlexandre TORGUE return 1; 18434891872SAlexandre TORGUE else 18534891872SAlexandre TORGUE return 0; 18634891872SAlexandre TORGUE } else if (*sr & USART_SR_RXNE) { 18734891872SAlexandre TORGUE return 1; 18834891872SAlexandre TORGUE } 18934891872SAlexandre TORGUE return 0; 19034891872SAlexandre TORGUE } 19134891872SAlexandre TORGUE 19256f9a76cSErwan Le Ray static unsigned long stm32_usart_get_char(struct uart_port *port, u32 *sr, 1936c5962f3SErwan Le Ray int *last_res) 19434891872SAlexandre TORGUE { 19534891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 196d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 19734891872SAlexandre TORGUE unsigned long c; 19834891872SAlexandre TORGUE 19934891872SAlexandre TORGUE if (stm32_port->rx_ch) { 20034891872SAlexandre TORGUE c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; 20134891872SAlexandre TORGUE if ((*last_res) == 0) 20234891872SAlexandre TORGUE *last_res = RX_BUF_L; 20334891872SAlexandre TORGUE } else { 2046c5962f3SErwan Le Ray c = readl_relaxed(port->membase + ofs->rdr); 2056c5962f3SErwan Le Ray /* apply RDR data mask */ 2066c5962f3SErwan Le Ray c &= stm32_port->rdr_mask; 20734891872SAlexandre TORGUE } 2086c5962f3SErwan Le Ray 2096c5962f3SErwan Le Ray return c; 21034891872SAlexandre TORGUE } 21134891872SAlexandre TORGUE 21256f9a76cSErwan Le Ray static void stm32_usart_receive_chars(struct uart_port *port, bool threaded) 21348a6092fSMaxime Coquelin { 21448a6092fSMaxime Coquelin struct tty_port *tport = &port->state->port; 215ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 216d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 217e359b441SJohan Hovold unsigned long c; 21848a6092fSMaxime Coquelin u32 sr; 21948a6092fSMaxime Coquelin char flag; 22048a6092fSMaxime Coquelin 221ad767681SErwan Le Ray spin_lock(&port->lock); 222ad767681SErwan Le Ray 22356f9a76cSErwan Le Ray while (stm32_usart_pending_rx(port, &sr, &stm32_port->last_res, 22456f9a76cSErwan Le Ray threaded)) { 22548a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22648a6092fSMaxime Coquelin flag = TTY_NORMAL; 22748a6092fSMaxime Coquelin 2284f01d833SErwan Le Ray /* 2294f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2304f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2314f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2324f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2334f01d833SErwan Le Ray * and clear status bits of the next rx data. 2344f01d833SErwan Le Ray * 2354f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2364f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 2374f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 2384f01d833SErwan Le Ray */ 2394f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 2401250ed71SFabrice Gasnier writel_relaxed(sr & USART_SR_ERR_MASK, 2411250ed71SFabrice Gasnier port->membase + ofs->icr); 2424f01d833SErwan Le Ray 24356f9a76cSErwan Le Ray c = stm32_usart_get_char(port, &sr, &stm32_port->last_res); 2444f01d833SErwan Le Ray port->icount.rx++; 24548a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 2464f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 24748a6092fSMaxime Coquelin port->icount.overrun++; 24848a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 24948a6092fSMaxime Coquelin port->icount.parity++; 25048a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 2514f01d833SErwan Le Ray /* Break detection if character is null */ 2524f01d833SErwan Le Ray if (!c) { 2534f01d833SErwan Le Ray port->icount.brk++; 2544f01d833SErwan Le Ray if (uart_handle_break(port)) 2554f01d833SErwan Le Ray continue; 2564f01d833SErwan Le Ray } else { 25748a6092fSMaxime Coquelin port->icount.frame++; 25848a6092fSMaxime Coquelin } 2594f01d833SErwan Le Ray } 26048a6092fSMaxime Coquelin 26148a6092fSMaxime Coquelin sr &= port->read_status_mask; 26248a6092fSMaxime Coquelin 2634f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26448a6092fSMaxime Coquelin flag = TTY_PARITY; 2654f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 2664f01d833SErwan Le Ray if (!c) 2674f01d833SErwan Le Ray flag = TTY_BREAK; 2684f01d833SErwan Le Ray else 26948a6092fSMaxime Coquelin flag = TTY_FRAME; 27048a6092fSMaxime Coquelin } 2714f01d833SErwan Le Ray } 27248a6092fSMaxime Coquelin 273cea37afdSJohan Hovold if (uart_prepare_sysrq_char(port, c)) 27448a6092fSMaxime Coquelin continue; 27548a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27648a6092fSMaxime Coquelin } 27748a6092fSMaxime Coquelin 278cea37afdSJohan Hovold uart_unlock_and_check_sysrq(port); 279ad767681SErwan Le Ray 28048a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 28148a6092fSMaxime Coquelin } 28248a6092fSMaxime Coquelin 28356f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg) 28434891872SAlexandre TORGUE { 28534891872SAlexandre TORGUE struct uart_port *port = arg; 28634891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 287d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 288f16b90c2SErwan Le Ray unsigned long flags; 28934891872SAlexandre TORGUE 290fb4f2e04SErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 29156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 29234891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 29334891872SAlexandre TORGUE 29434891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 295f16b90c2SErwan Le Ray spin_lock_irqsave(&port->lock, flags); 29656f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 297f16b90c2SErwan Le Ray spin_unlock_irqrestore(&port->lock, flags); 29834891872SAlexandre TORGUE } 29934891872SAlexandre TORGUE 30056f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port) 301d075719eSErwan Le Ray { 302d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 303d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 304d075719eSErwan Le Ray 305d075719eSErwan Le Ray /* 306d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 307d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 308d075719eSErwan Le Ray */ 3092aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 31056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 311d075719eSErwan Le Ray else 31256f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 313d075719eSErwan Le Ray } 314d075719eSErwan Le Ray 31556f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port) 316d075719eSErwan Le Ray { 317d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 318d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 319d075719eSErwan Le Ray 3202aa1bbb2SFabrice Gasnier if (stm32_port->fifoen && stm32_port->txftcfg >= 0) 32156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 322d075719eSErwan Le Ray else 32356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 324d075719eSErwan Le Ray } 325d075719eSErwan Le Ray 32656f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port) 32734891872SAlexandre TORGUE { 32834891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 329d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 33034891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 33134891872SAlexandre TORGUE 33234891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 33356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 33434891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 33534891872SAlexandre TORGUE } 33634891872SAlexandre TORGUE 3375d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 3385d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 3395d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 3405d9176edSErwan Le Ray break; 34134891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 34234891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 34334891872SAlexandre TORGUE port->icount.tx++; 34434891872SAlexandre TORGUE } 34534891872SAlexandre TORGUE 3465d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 3475d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 34856f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 3495d9176edSErwan Le Ray else 35056f9a76cSErwan Le Ray stm32_usart_tx_interrupt_enable(port); 3515d9176edSErwan Le Ray } 3525d9176edSErwan Le Ray 35356f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port) 35434891872SAlexandre TORGUE { 35534891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 356d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 35734891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 35834891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 35934891872SAlexandre TORGUE unsigned int count, i; 36034891872SAlexandre TORGUE 36134891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 36234891872SAlexandre TORGUE return; 36334891872SAlexandre TORGUE 36434891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 36534891872SAlexandre TORGUE 36634891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 36734891872SAlexandre TORGUE 36834891872SAlexandre TORGUE if (count > TX_BUF_L) 36934891872SAlexandre TORGUE count = TX_BUF_L; 37034891872SAlexandre TORGUE 37134891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 37234891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 37334891872SAlexandre TORGUE } else { 37434891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 37534891872SAlexandre TORGUE size_t two; 37634891872SAlexandre TORGUE 37734891872SAlexandre TORGUE if (one > count) 37834891872SAlexandre TORGUE one = count; 37934891872SAlexandre TORGUE two = count - one; 38034891872SAlexandre TORGUE 38134891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 38234891872SAlexandre TORGUE if (two) 38334891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 38434891872SAlexandre TORGUE } 38534891872SAlexandre TORGUE 38634891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 38734891872SAlexandre TORGUE stm32port->tx_dma_buf, 38834891872SAlexandre TORGUE count, 38934891872SAlexandre TORGUE DMA_MEM_TO_DEV, 39034891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 39134891872SAlexandre TORGUE 392e7997f7fSErwan Le Ray if (!desc) 393e7997f7fSErwan Le Ray goto fallback_err; 39434891872SAlexandre TORGUE 39556f9a76cSErwan Le Ray desc->callback = stm32_usart_tx_dma_complete; 39634891872SAlexandre TORGUE desc->callback_param = port; 39734891872SAlexandre TORGUE 39834891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 399e7997f7fSErwan Le Ray if (dma_submit_error(dmaengine_submit(desc))) { 400e7997f7fSErwan Le Ray /* dma no yet started, safe to free resources */ 401e7997f7fSErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 402e7997f7fSErwan Le Ray goto fallback_err; 403e7997f7fSErwan Le Ray } 40434891872SAlexandre TORGUE 40534891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 40634891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 40734891872SAlexandre TORGUE 40856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 40934891872SAlexandre TORGUE 41034891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 41134891872SAlexandre TORGUE port->icount.tx += count; 412e7997f7fSErwan Le Ray return; 413e7997f7fSErwan Le Ray 414e7997f7fSErwan Le Ray fallback_err: 415e7997f7fSErwan Le Ray for (i = count; i > 0; i--) 41656f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 41734891872SAlexandre TORGUE } 41834891872SAlexandre TORGUE 41956f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port) 42048a6092fSMaxime Coquelin { 421ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 422d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 42348a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 42448a6092fSMaxime Coquelin 42548a6092fSMaxime Coquelin if (port->x_char) { 42634891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 42756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 428ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 42948a6092fSMaxime Coquelin port->x_char = 0; 43048a6092fSMaxime Coquelin port->icount.tx++; 43134891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 43256f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 43348a6092fSMaxime Coquelin return; 43448a6092fSMaxime Coquelin } 43548a6092fSMaxime Coquelin 436b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 43756f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 43848a6092fSMaxime Coquelin return; 43948a6092fSMaxime Coquelin } 44048a6092fSMaxime Coquelin 44164c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 44256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC); 44364c32eabSErwan Le Ray else 4441250ed71SFabrice Gasnier writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr); 44564c32eabSErwan Le Ray 44634891872SAlexandre TORGUE if (stm32_port->tx_ch) 44756f9a76cSErwan Le Ray stm32_usart_transmit_chars_dma(port); 44834891872SAlexandre TORGUE else 44956f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 45048a6092fSMaxime Coquelin 45148a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 45248a6092fSMaxime Coquelin uart_write_wakeup(port); 45348a6092fSMaxime Coquelin 45448a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 45556f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 45648a6092fSMaxime Coquelin } 45748a6092fSMaxime Coquelin 45856f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) 45948a6092fSMaxime Coquelin { 46048a6092fSMaxime Coquelin struct uart_port *port = ptr; 46112761869SErwan Le Ray struct tty_port *tport = &port->state->port; 462ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 463d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 46448a6092fSMaxime Coquelin u32 sr; 46548a6092fSMaxime Coquelin 466ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 46748a6092fSMaxime Coquelin 4684cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 4694cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 4704cc0ed62SErwan Le Ray port->membase + ofs->icr); 4714cc0ed62SErwan Le Ray 47212761869SErwan Le Ray if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) { 47312761869SErwan Le Ray /* Clear wake up flag and disable wake up interrupt */ 474270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 475270e5a74SFabrice Gasnier port->membase + ofs->icr); 47612761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 47712761869SErwan Le Ray if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 47812761869SErwan Le Ray pm_wakeup_event(tport->tty->dev, 0); 47912761869SErwan Le Ray } 480270e5a74SFabrice Gasnier 48134891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 48256f9a76cSErwan Le Ray stm32_usart_receive_chars(port, false); 48348a6092fSMaxime Coquelin 484ad767681SErwan Le Ray if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) { 485ad767681SErwan Le Ray spin_lock(&port->lock); 48656f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 48701d32d71SAlexandre TORGUE spin_unlock(&port->lock); 488ad767681SErwan Le Ray } 48901d32d71SAlexandre TORGUE 49034891872SAlexandre TORGUE if (stm32_port->rx_ch) 49134891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 49234891872SAlexandre TORGUE else 49334891872SAlexandre TORGUE return IRQ_HANDLED; 49434891872SAlexandre TORGUE } 49534891872SAlexandre TORGUE 49656f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr) 49734891872SAlexandre TORGUE { 49834891872SAlexandre TORGUE struct uart_port *port = ptr; 49934891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 50034891872SAlexandre TORGUE 50134891872SAlexandre TORGUE if (stm32_port->rx_ch) 50256f9a76cSErwan Le Ray stm32_usart_receive_chars(port, true); 50334891872SAlexandre TORGUE 50448a6092fSMaxime Coquelin return IRQ_HANDLED; 50548a6092fSMaxime Coquelin } 50648a6092fSMaxime Coquelin 50756f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port) 50848a6092fSMaxime Coquelin { 509ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 510d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 511ada8618fSAlexandre TORGUE 5123db1d524SErwan Le Ray if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC) 5133db1d524SErwan Le Ray return TIOCSER_TEMT; 5143db1d524SErwan Le Ray 5153db1d524SErwan Le Ray return 0; 51648a6092fSMaxime Coquelin } 51748a6092fSMaxime Coquelin 51856f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl) 51948a6092fSMaxime Coquelin { 520ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 521d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 522ada8618fSAlexandre TORGUE 52348a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 52456f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE); 52548a6092fSMaxime Coquelin else 52656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 5276cf61b9bSManivannan Sadhasivam 5286cf61b9bSManivannan Sadhasivam mctrl_gpio_set(stm32_port->gpios, mctrl); 52948a6092fSMaxime Coquelin } 53048a6092fSMaxime Coquelin 53156f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port) 53248a6092fSMaxime Coquelin { 5336cf61b9bSManivannan Sadhasivam struct stm32_port *stm32_port = to_stm32_port(port); 5346cf61b9bSManivannan Sadhasivam unsigned int ret; 5356cf61b9bSManivannan Sadhasivam 53648a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 5376cf61b9bSManivannan Sadhasivam ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 5386cf61b9bSManivannan Sadhasivam 5396cf61b9bSManivannan Sadhasivam return mctrl_gpio_get(stm32_port->gpios, &ret); 5406cf61b9bSManivannan Sadhasivam } 5416cf61b9bSManivannan Sadhasivam 54256f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port) 5436cf61b9bSManivannan Sadhasivam { 5446cf61b9bSManivannan Sadhasivam mctrl_gpio_enable_ms(to_stm32_port(port)->gpios); 5456cf61b9bSManivannan Sadhasivam } 5466cf61b9bSManivannan Sadhasivam 54756f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port) 5486cf61b9bSManivannan Sadhasivam { 5496cf61b9bSManivannan Sadhasivam mctrl_gpio_disable_ms(to_stm32_port(port)->gpios); 55048a6092fSMaxime Coquelin } 55148a6092fSMaxime Coquelin 55248a6092fSMaxime Coquelin /* Transmit stop */ 55356f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port) 55448a6092fSMaxime Coquelin { 555ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 556ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 557ad0c2748SMarek Vasut 55856f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 559ad0c2748SMarek Vasut 560ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 561ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 562ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 563ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 564ad0c2748SMarek Vasut } else { 565ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 566ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 567ad0c2748SMarek Vasut } 568ad0c2748SMarek Vasut } 56948a6092fSMaxime Coquelin } 57048a6092fSMaxime Coquelin 57148a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 57256f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port) 57348a6092fSMaxime Coquelin { 574ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 575ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 57648a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 57748a6092fSMaxime Coquelin 57848a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 57948a6092fSMaxime Coquelin return; 58048a6092fSMaxime Coquelin 581ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 582ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 583ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 584ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 585ad0c2748SMarek Vasut } else { 586ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 587ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 588ad0c2748SMarek Vasut } 589ad0c2748SMarek Vasut } 590ad0c2748SMarek Vasut 59156f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 59248a6092fSMaxime Coquelin } 59348a6092fSMaxime Coquelin 5943d82be8bSErwan Le Ray /* Flush the transmit buffer. */ 5953d82be8bSErwan Le Ray static void stm32_usart_flush_buffer(struct uart_port *port) 5963d82be8bSErwan Le Ray { 5973d82be8bSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 5983d82be8bSErwan Le Ray const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 5993d82be8bSErwan Le Ray 6003d82be8bSErwan Le Ray if (stm32_port->tx_ch) { 6013d82be8bSErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 6023d82be8bSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 6033d82be8bSErwan Le Ray stm32_port->tx_dma_busy = false; 6043d82be8bSErwan Le Ray } 6053d82be8bSErwan Le Ray } 6063d82be8bSErwan Le Ray 60748a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 60856f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port) 60948a6092fSMaxime Coquelin { 610ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 611d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 61248a6092fSMaxime Coquelin unsigned long flags; 61348a6092fSMaxime Coquelin 61448a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 61556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 616d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 61756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 618d0a6a7bcSErwan Le Ray 61948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 62048a6092fSMaxime Coquelin } 62148a6092fSMaxime Coquelin 62248a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 62356f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port) 62448a6092fSMaxime Coquelin { 625ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 626d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 62748a6092fSMaxime Coquelin unsigned long flags; 62848a6092fSMaxime Coquelin 62948a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 63056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 631d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 63256f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq); 633d0a6a7bcSErwan Le Ray 63448a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 63548a6092fSMaxime Coquelin } 63648a6092fSMaxime Coquelin 63748a6092fSMaxime Coquelin /* Receive stop */ 63856f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port) 63948a6092fSMaxime Coquelin { 640ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 641d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 642ada8618fSAlexandre TORGUE 64356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 644d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 64556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 64648a6092fSMaxime Coquelin } 64748a6092fSMaxime Coquelin 64848a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 64956f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state) 65048a6092fSMaxime Coquelin { 65148a6092fSMaxime Coquelin } 65248a6092fSMaxime Coquelin 65356f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port) 65448a6092fSMaxime Coquelin { 655ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 656d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 657f4518a8aSErwan Le Ray const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 65848a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 65948a6092fSMaxime Coquelin u32 val; 66048a6092fSMaxime Coquelin int ret; 66148a6092fSMaxime Coquelin 66256f9a76cSErwan Le Ray ret = request_threaded_irq(port->irq, stm32_usart_interrupt, 66356f9a76cSErwan Le Ray stm32_usart_threaded_interrupt, 664e359b441SJohan Hovold IRQF_ONESHOT | IRQF_NO_SUSPEND, 665e359b441SJohan Hovold name, port); 66648a6092fSMaxime Coquelin if (ret) 66748a6092fSMaxime Coquelin return ret; 66848a6092fSMaxime Coquelin 6693cd66593SMartin Devera if (stm32_port->swap) { 6703cd66593SMartin Devera val = readl_relaxed(port->membase + ofs->cr2); 6713cd66593SMartin Devera val |= USART_CR2_SWAP; 6723cd66593SMartin Devera writel_relaxed(val, port->membase + ofs->cr2); 6733cd66593SMartin Devera } 6743cd66593SMartin Devera 67584872dc4SErwan Le Ray /* RX FIFO Flush */ 67684872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 677315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr); 67848a6092fSMaxime Coquelin 67925a8e761SErwan Le Ray /* RX enabling */ 680f4518a8aSErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit); 68156f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, val); 68284872dc4SErwan Le Ray 68348a6092fSMaxime Coquelin return 0; 68448a6092fSMaxime Coquelin } 68548a6092fSMaxime Coquelin 68656f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port) 68748a6092fSMaxime Coquelin { 688ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 689d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 690d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 69164c32eabSErwan Le Ray u32 val, isr; 69264c32eabSErwan Le Ray int ret; 69348a6092fSMaxime Coquelin 6946cf61b9bSManivannan Sadhasivam /* Disable modem control interrupts */ 69556f9a76cSErwan Le Ray stm32_usart_disable_ms(port); 6966cf61b9bSManivannan Sadhasivam 6974cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 6984cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 69987f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 700351a762aSGerald Baeza if (stm32_port->fifoen) 701351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 70264c32eabSErwan Le Ray 70364c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 70464c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 70564c32eabSErwan Le Ray 10, 100000); 70664c32eabSErwan Le Ray 707c31c3ea0SErwan Le Ray /* Send the TC error message only when ISR_TC is not set */ 70864c32eabSErwan Le Ray if (ret) 709c31c3ea0SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 71064c32eabSErwan Le Ray 7119f77d192SErwan Le Ray /* flush RX & TX FIFO */ 7129f77d192SErwan Le Ray if (ofs->rqr != UNDEF_REG) 7139f77d192SErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 7149f77d192SErwan Le Ray port->membase + ofs->rqr); 7159f77d192SErwan Le Ray 71656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, val); 71748a6092fSMaxime Coquelin 71848a6092fSMaxime Coquelin free_irq(port->irq, port); 71948a6092fSMaxime Coquelin } 72048a6092fSMaxime Coquelin 72156f9a76cSErwan Le Ray static unsigned int stm32_usart_get_databits(struct ktermios *termios) 722c8a9d043SErwan Le Ray { 723c8a9d043SErwan Le Ray unsigned int bits; 724c8a9d043SErwan Le Ray 725c8a9d043SErwan Le Ray tcflag_t cflag = termios->c_cflag; 726c8a9d043SErwan Le Ray 727c8a9d043SErwan Le Ray switch (cflag & CSIZE) { 728c8a9d043SErwan Le Ray /* 729c8a9d043SErwan Le Ray * CSIZE settings are not necessarily supported in hardware. 730c8a9d043SErwan Le Ray * CSIZE unsupported configurations are handled here to set word length 731c8a9d043SErwan Le Ray * to 8 bits word as default configuration and to print debug message. 732c8a9d043SErwan Le Ray */ 733c8a9d043SErwan Le Ray case CS5: 734c8a9d043SErwan Le Ray bits = 5; 735c8a9d043SErwan Le Ray break; 736c8a9d043SErwan Le Ray case CS6: 737c8a9d043SErwan Le Ray bits = 6; 738c8a9d043SErwan Le Ray break; 739c8a9d043SErwan Le Ray case CS7: 740c8a9d043SErwan Le Ray bits = 7; 741c8a9d043SErwan Le Ray break; 742c8a9d043SErwan Le Ray /* default including CS8 */ 743c8a9d043SErwan Le Ray default: 744c8a9d043SErwan Le Ray bits = 8; 745c8a9d043SErwan Le Ray break; 746c8a9d043SErwan Le Ray } 747c8a9d043SErwan Le Ray 748c8a9d043SErwan Le Ray return bits; 749c8a9d043SErwan Le Ray } 750c8a9d043SErwan Le Ray 75156f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port, 75256f9a76cSErwan Le Ray struct ktermios *termios, 75348a6092fSMaxime Coquelin struct ktermios *old) 75448a6092fSMaxime Coquelin { 75548a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 756d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 757d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 7581bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 759c8a9d043SErwan Le Ray unsigned int baud, bits; 76048a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 76148a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 762f264c6f6SErwan Le Ray u32 cr1, cr2, cr3, isr; 76348a6092fSMaxime Coquelin unsigned long flags; 764f264c6f6SErwan Le Ray int ret; 76548a6092fSMaxime Coquelin 76648a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 76748a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 76848a6092fSMaxime Coquelin 76948a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 77048a6092fSMaxime Coquelin 77148a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 77248a6092fSMaxime Coquelin 773f264c6f6SErwan Le Ray ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 774f264c6f6SErwan Le Ray isr, 775f264c6f6SErwan Le Ray (isr & USART_SR_TC), 776f264c6f6SErwan Le Ray 10, 100000); 777f264c6f6SErwan Le Ray 778f264c6f6SErwan Le Ray /* Send the TC error message only when ISR_TC is not set. */ 779f264c6f6SErwan Le Ray if (ret) 780f264c6f6SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 781f264c6f6SErwan Le Ray 78248a6092fSMaxime Coquelin /* Stop serial port and reset value */ 783ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 78448a6092fSMaxime Coquelin 78584872dc4SErwan Le Ray /* flush RX & TX FIFO */ 78684872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 787315e2d8aSErwan Le Ray writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ, 788315e2d8aSErwan Le Ray port->membase + ofs->rqr); 7891bcda09dSBich HEMON 79084872dc4SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 791351a762aSGerald Baeza if (stm32_port->fifoen) 792351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 7933cd66593SMartin Devera cr2 = stm32_port->swap ? USART_CR2_SWAP : 0; 79425a8e761SErwan Le Ray 79525a8e761SErwan Le Ray /* Tx and RX FIFO configuration */ 796d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 79725a8e761SErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE; 79825a8e761SErwan Le Ray if (stm32_port->fifoen) { 7992aa1bbb2SFabrice Gasnier if (stm32_port->txftcfg >= 0) 8002aa1bbb2SFabrice Gasnier cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT; 8012aa1bbb2SFabrice Gasnier if (stm32_port->rxftcfg >= 0) 8022aa1bbb2SFabrice Gasnier cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT; 80325a8e761SErwan Le Ray } 80448a6092fSMaxime Coquelin 80548a6092fSMaxime Coquelin if (cflag & CSTOPB) 80648a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 80748a6092fSMaxime Coquelin 80856f9a76cSErwan Le Ray bits = stm32_usart_get_databits(termios); 8096c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 810c8a9d043SErwan Le Ray 81148a6092fSMaxime Coquelin if (cflag & PARENB) { 812c8a9d043SErwan Le Ray bits++; 81348a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 814c8a9d043SErwan Le Ray } 815c8a9d043SErwan Le Ray 816c8a9d043SErwan Le Ray /* 817c8a9d043SErwan Le Ray * Word length configuration: 818c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 819c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 820c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 821c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 822c8a9d043SErwan Le Ray */ 823c8a9d043SErwan Le Ray if (bits == 9) 824ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 825c8a9d043SErwan Le Ray else if ((bits == 7) && cfg->has_7bits_data) 826c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 827c8a9d043SErwan Le Ray else if (bits != 8) 828c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 829c8a9d043SErwan Le Ray , bits); 83048a6092fSMaxime Coquelin 8314cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 8322aa1bbb2SFabrice Gasnier (stm32_port->fifoen && 8332aa1bbb2SFabrice Gasnier stm32_port->rxftcfg >= 0))) { 8344cc0ed62SErwan Le Ray if (cflag & CSTOPB) 8354cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 8364cc0ed62SErwan Le Ray else 8374cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 8384cc0ed62SErwan Le Ray 8394cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 8404cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 8414cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 8424cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 843d0a6a7bcSErwan Le Ray /* Not using dma, enable fifo threshold irq */ 844d0a6a7bcSErwan Le Ray if (!stm32_port->rx_ch) 845d0a6a7bcSErwan Le Ray stm32_port->cr3_irq = USART_CR3_RXFTIE; 8464cc0ed62SErwan Le Ray } 8474cc0ed62SErwan Le Ray 848d0a6a7bcSErwan Le Ray cr1 |= stm32_port->cr1_irq; 849d0a6a7bcSErwan Le Ray cr3 |= stm32_port->cr3_irq; 850d0a6a7bcSErwan Le Ray 85148a6092fSMaxime Coquelin if (cflag & PARODD) 85248a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 85348a6092fSMaxime Coquelin 85448a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 85548a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 85648a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 85735abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 85848a6092fSMaxime Coquelin } 85948a6092fSMaxime Coquelin 86048a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 86148a6092fSMaxime Coquelin 86248a6092fSMaxime Coquelin /* 86348a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 86448a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 86548a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 86648a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 86748a6092fSMaxime Coquelin */ 86848a6092fSMaxime Coquelin if (usartdiv < 16) { 86948a6092fSMaxime Coquelin oversampling = 8; 8701bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 87156f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8); 87248a6092fSMaxime Coquelin } else { 87348a6092fSMaxime Coquelin oversampling = 16; 8741bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 87556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 87648a6092fSMaxime Coquelin } 87748a6092fSMaxime Coquelin 87848a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 87948a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 880ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 88148a6092fSMaxime Coquelin 88248a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 88348a6092fSMaxime Coquelin 88448a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 88548a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 88648a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 88748a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 8884f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 88948a6092fSMaxime Coquelin 89048a6092fSMaxime Coquelin /* Characters to ignore */ 89148a6092fSMaxime Coquelin port->ignore_status_mask = 0; 89248a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 89348a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 89448a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 8954f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 89648a6092fSMaxime Coquelin /* 89748a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 89848a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 89948a6092fSMaxime Coquelin */ 90048a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 90148a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 90248a6092fSMaxime Coquelin } 90348a6092fSMaxime Coquelin 90448a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 90548a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 90648a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 90748a6092fSMaxime Coquelin 90834891872SAlexandre TORGUE if (stm32_port->rx_ch) 90934891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 91034891872SAlexandre TORGUE 9111bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 91256f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 9131bcda09dSBich HEMON rs485conf->delay_rts_before_send, 91456f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 91556f9a76cSErwan Le Ray baud); 9161bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 9171bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 9181bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 9191bcda09dSBich HEMON } else { 9201bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 9211bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 9221bcda09dSBich HEMON } 9231bcda09dSBich HEMON 9241bcda09dSBich HEMON } else { 9251bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 9261bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 9271bcda09dSBich HEMON } 9281bcda09dSBich HEMON 92912761869SErwan Le Ray /* Configure wake up from low power on start bit detection */ 9303d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 93112761869SErwan Le Ray cr3 &= ~USART_CR3_WUS_MASK; 93212761869SErwan Le Ray cr3 |= USART_CR3_WUS_START_BIT; 93312761869SErwan Le Ray } 93412761869SErwan Le Ray 935ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 936ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 937ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 93848a6092fSMaxime Coquelin 93956f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 94048a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 941436c9793SErwan Le Ray 942436c9793SErwan Le Ray /* Handle modem control interrupts */ 943436c9793SErwan Le Ray if (UART_ENABLE_MS(port, termios->c_cflag)) 944436c9793SErwan Le Ray stm32_usart_enable_ms(port); 945436c9793SErwan Le Ray else 946436c9793SErwan Le Ray stm32_usart_disable_ms(port); 94748a6092fSMaxime Coquelin } 94848a6092fSMaxime Coquelin 94956f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port) 95048a6092fSMaxime Coquelin { 95148a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 95248a6092fSMaxime Coquelin } 95348a6092fSMaxime Coquelin 95456f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port) 95548a6092fSMaxime Coquelin { 95648a6092fSMaxime Coquelin } 95748a6092fSMaxime Coquelin 95856f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port) 95948a6092fSMaxime Coquelin { 96048a6092fSMaxime Coquelin return 0; 96148a6092fSMaxime Coquelin } 96248a6092fSMaxime Coquelin 96356f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags) 96448a6092fSMaxime Coquelin { 96548a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 96648a6092fSMaxime Coquelin port->type = PORT_STM32; 96748a6092fSMaxime Coquelin } 96848a6092fSMaxime Coquelin 96948a6092fSMaxime Coquelin static int 97056f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser) 97148a6092fSMaxime Coquelin { 97248a6092fSMaxime Coquelin /* No user changeable parameters */ 97348a6092fSMaxime Coquelin return -EINVAL; 97448a6092fSMaxime Coquelin } 97548a6092fSMaxime Coquelin 97656f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state, 97748a6092fSMaxime Coquelin unsigned int oldstate) 97848a6092fSMaxime Coquelin { 97948a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 98048a6092fSMaxime Coquelin struct stm32_port, port); 981d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 982d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32port->info->cfg; 983*18ee37e1SJohan Hovold unsigned long flags; 98448a6092fSMaxime Coquelin 98548a6092fSMaxime Coquelin switch (state) { 98648a6092fSMaxime Coquelin case UART_PM_STATE_ON: 987fb6dcef6SErwan Le Ray pm_runtime_get_sync(port->dev); 98848a6092fSMaxime Coquelin break; 98948a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 99048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 99156f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 99248a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 993fb6dcef6SErwan Le Ray pm_runtime_put_sync(port->dev); 99448a6092fSMaxime Coquelin break; 99548a6092fSMaxime Coquelin } 99648a6092fSMaxime Coquelin } 99748a6092fSMaxime Coquelin 99848a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 99956f9a76cSErwan Le Ray .tx_empty = stm32_usart_tx_empty, 100056f9a76cSErwan Le Ray .set_mctrl = stm32_usart_set_mctrl, 100156f9a76cSErwan Le Ray .get_mctrl = stm32_usart_get_mctrl, 100256f9a76cSErwan Le Ray .stop_tx = stm32_usart_stop_tx, 100356f9a76cSErwan Le Ray .start_tx = stm32_usart_start_tx, 100456f9a76cSErwan Le Ray .throttle = stm32_usart_throttle, 100556f9a76cSErwan Le Ray .unthrottle = stm32_usart_unthrottle, 100656f9a76cSErwan Le Ray .stop_rx = stm32_usart_stop_rx, 100756f9a76cSErwan Le Ray .enable_ms = stm32_usart_enable_ms, 100856f9a76cSErwan Le Ray .break_ctl = stm32_usart_break_ctl, 100956f9a76cSErwan Le Ray .startup = stm32_usart_startup, 101056f9a76cSErwan Le Ray .shutdown = stm32_usart_shutdown, 10113d82be8bSErwan Le Ray .flush_buffer = stm32_usart_flush_buffer, 101256f9a76cSErwan Le Ray .set_termios = stm32_usart_set_termios, 101356f9a76cSErwan Le Ray .pm = stm32_usart_pm, 101456f9a76cSErwan Le Ray .type = stm32_usart_type, 101556f9a76cSErwan Le Ray .release_port = stm32_usart_release_port, 101656f9a76cSErwan Le Ray .request_port = stm32_usart_request_port, 101756f9a76cSErwan Le Ray .config_port = stm32_usart_config_port, 101856f9a76cSErwan Le Ray .verify_port = stm32_usart_verify_port, 101948a6092fSMaxime Coquelin }; 102048a6092fSMaxime Coquelin 10212aa1bbb2SFabrice Gasnier /* 10222aa1bbb2SFabrice Gasnier * STM32H7 RX & TX FIFO threshold configuration (CR3 RXFTCFG / TXFTCFG) 10232aa1bbb2SFabrice Gasnier * Note: 1 isn't a valid value in RXFTCFG / TXFTCFG. In this case, 10242aa1bbb2SFabrice Gasnier * RXNEIE / TXEIE can be used instead of threshold irqs: RXFTIE / TXFTIE. 10252aa1bbb2SFabrice Gasnier * So, RXFTCFG / TXFTCFG bitfields values are encoded as array index + 1. 10262aa1bbb2SFabrice Gasnier */ 10272aa1bbb2SFabrice Gasnier static const u32 stm32h7_usart_fifo_thresh_cfg[] = { 1, 2, 4, 8, 12, 14, 16 }; 10282aa1bbb2SFabrice Gasnier 10292aa1bbb2SFabrice Gasnier static void stm32_usart_get_ftcfg(struct platform_device *pdev, const char *p, 10302aa1bbb2SFabrice Gasnier int *ftcfg) 10312aa1bbb2SFabrice Gasnier { 10322aa1bbb2SFabrice Gasnier u32 bytes, i; 10332aa1bbb2SFabrice Gasnier 10342aa1bbb2SFabrice Gasnier /* DT option to get RX & TX FIFO threshold (default to 8 bytes) */ 10352aa1bbb2SFabrice Gasnier if (of_property_read_u32(pdev->dev.of_node, p, &bytes)) 10362aa1bbb2SFabrice Gasnier bytes = 8; 10372aa1bbb2SFabrice Gasnier 10382aa1bbb2SFabrice Gasnier for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++) 10392aa1bbb2SFabrice Gasnier if (stm32h7_usart_fifo_thresh_cfg[i] >= bytes) 10402aa1bbb2SFabrice Gasnier break; 10412aa1bbb2SFabrice Gasnier if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg)) 10422aa1bbb2SFabrice Gasnier i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1; 10432aa1bbb2SFabrice Gasnier 10442aa1bbb2SFabrice Gasnier dev_dbg(&pdev->dev, "%s set to %d bytes\n", p, 10452aa1bbb2SFabrice Gasnier stm32h7_usart_fifo_thresh_cfg[i]); 10462aa1bbb2SFabrice Gasnier 10472aa1bbb2SFabrice Gasnier /* Provide FIFO threshold ftcfg (1 is invalid: threshold irq unused) */ 10482aa1bbb2SFabrice Gasnier if (i) 10492aa1bbb2SFabrice Gasnier *ftcfg = i - 1; 10502aa1bbb2SFabrice Gasnier else 10512aa1bbb2SFabrice Gasnier *ftcfg = -EINVAL; 10522aa1bbb2SFabrice Gasnier } 10532aa1bbb2SFabrice Gasnier 105497f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port) 105597f3a085SErwan Le Ray { 105697f3a085SErwan Le Ray clk_disable_unprepare(stm32port->clk); 105797f3a085SErwan Le Ray } 105897f3a085SErwan Le Ray 105956f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port, 106048a6092fSMaxime Coquelin struct platform_device *pdev) 106148a6092fSMaxime Coquelin { 106248a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 106348a6092fSMaxime Coquelin struct resource *res; 1064e0f2a902SErwan Le Ray int ret, irq; 106548a6092fSMaxime Coquelin 1066e0f2a902SErwan Le Ray irq = platform_get_irq(pdev, 0); 1067e0f2a902SErwan Le Ray if (irq <= 0) 1068e0f2a902SErwan Le Ray return irq ? : -ENODEV; 106992fc0023SErwan Le Ray 107048a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 107148a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 107248a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 107348a6092fSMaxime Coquelin port->dev = &pdev->dev; 1074d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 10759feedaa7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE); 1076e0f2a902SErwan Le Ray port->irq = irq; 107756f9a76cSErwan Le Ray port->rs485_config = stm32_usart_config_rs485; 10787d8f6861SBich HEMON 107956f9a76cSErwan Le Ray ret = stm32_usart_init_rs485(port, pdev); 1080c150c0f3SLukas Wunner if (ret) 1081c150c0f3SLukas Wunner return ret; 10827d8f6861SBich HEMON 10833d530017SAlexandre Torgue stm32port->wakeup_src = stm32port->info->cfg.has_wakeup && 10843d530017SAlexandre Torgue of_property_read_bool(pdev->dev.of_node, "wakeup-source"); 10852c58e560SErwan Le Ray 10863cd66593SMartin Devera stm32port->swap = stm32port->info->cfg.has_swap && 10873cd66593SMartin Devera of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"); 10883cd66593SMartin Devera 1089351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 10902aa1bbb2SFabrice Gasnier if (stm32port->fifoen) { 10912aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "rx-threshold", 10922aa1bbb2SFabrice Gasnier &stm32port->rxftcfg); 10932aa1bbb2SFabrice Gasnier stm32_usart_get_ftcfg(pdev, "tx-threshold", 10942aa1bbb2SFabrice Gasnier &stm32port->txftcfg); 10952aa1bbb2SFabrice Gasnier } 109648a6092fSMaxime Coquelin 109748a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 109848a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 109948a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 110048a6092fSMaxime Coquelin return PTR_ERR(port->membase); 110148a6092fSMaxime Coquelin port->mapbase = res->start; 110248a6092fSMaxime Coquelin 110348a6092fSMaxime Coquelin spin_lock_init(&port->lock); 110448a6092fSMaxime Coquelin 110548a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 110648a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 110748a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 110848a6092fSMaxime Coquelin 110948a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 111048a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 111148a6092fSMaxime Coquelin if (ret) 111248a6092fSMaxime Coquelin return ret; 111348a6092fSMaxime Coquelin 111448a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 1115ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 111648a6092fSMaxime Coquelin ret = -EINVAL; 11176cf61b9bSManivannan Sadhasivam goto err_clk; 1118ada80043SFabrice Gasnier } 111948a6092fSMaxime Coquelin 11206cf61b9bSManivannan Sadhasivam stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0); 11216cf61b9bSManivannan Sadhasivam if (IS_ERR(stm32port->gpios)) { 11226cf61b9bSManivannan Sadhasivam ret = PTR_ERR(stm32port->gpios); 11236cf61b9bSManivannan Sadhasivam goto err_clk; 11246cf61b9bSManivannan Sadhasivam } 11256cf61b9bSManivannan Sadhasivam 11269359369aSErwan Le Ray /* 11279359369aSErwan Le Ray * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts" 11289359369aSErwan Le Ray * properties should not be specified. 11299359369aSErwan Le Ray */ 11306cf61b9bSManivannan Sadhasivam if (stm32port->hw_flow_control) { 11316cf61b9bSManivannan Sadhasivam if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) || 11326cf61b9bSManivannan Sadhasivam mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) { 11336cf61b9bSManivannan Sadhasivam dev_err(&pdev->dev, "Conflicting RTS/CTS config\n"); 11346cf61b9bSManivannan Sadhasivam ret = -EINVAL; 11356cf61b9bSManivannan Sadhasivam goto err_clk; 11366cf61b9bSManivannan Sadhasivam } 11376cf61b9bSManivannan Sadhasivam } 11386cf61b9bSManivannan Sadhasivam 11396cf61b9bSManivannan Sadhasivam return ret; 11406cf61b9bSManivannan Sadhasivam 11416cf61b9bSManivannan Sadhasivam err_clk: 11426cf61b9bSManivannan Sadhasivam clk_disable_unprepare(stm32port->clk); 11436cf61b9bSManivannan Sadhasivam 114448a6092fSMaxime Coquelin return ret; 114548a6092fSMaxime Coquelin } 114648a6092fSMaxime Coquelin 114756f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev) 114848a6092fSMaxime Coquelin { 114948a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 115048a6092fSMaxime Coquelin int id; 115148a6092fSMaxime Coquelin 115248a6092fSMaxime Coquelin if (!np) 115348a6092fSMaxime Coquelin return NULL; 115448a6092fSMaxime Coquelin 115548a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 1156e5707915SGerald Baeza if (id < 0) { 1157e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 1158e5707915SGerald Baeza return NULL; 1159e5707915SGerald Baeza } 116048a6092fSMaxime Coquelin 116148a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 116248a6092fSMaxime Coquelin return NULL; 116348a6092fSMaxime Coquelin 11646fd9fffbSErwan Le Ray stm32_ports[id].hw_flow_control = 11656fd9fffbSErwan Le Ray of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ || 11666fd9fffbSErwan Le Ray of_property_read_bool (np, "uart-has-rtscts"); 116748a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 11684cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 1169d0a6a7bcSErwan Le Ray stm32_ports[id].cr3_irq = 0; 1170e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 117148a6092fSMaxime Coquelin return &stm32_ports[id]; 117248a6092fSMaxime Coquelin } 117348a6092fSMaxime Coquelin 117448a6092fSMaxime Coquelin #ifdef CONFIG_OF 117548a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 1176ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 1177ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 1178270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 117948a6092fSMaxime Coquelin {}, 118048a6092fSMaxime Coquelin }; 118148a6092fSMaxime Coquelin 118248a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 118348a6092fSMaxime Coquelin #endif 118448a6092fSMaxime Coquelin 118556f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port, 118634891872SAlexandre TORGUE struct platform_device *pdev) 118734891872SAlexandre TORGUE { 1188d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 118934891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 119034891872SAlexandre TORGUE struct device *dev = &pdev->dev; 119134891872SAlexandre TORGUE struct dma_slave_config config; 119234891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 119334891872SAlexandre TORGUE int ret; 119434891872SAlexandre TORGUE 1195e359b441SJohan Hovold /* 1196e359b441SJohan Hovold * Using DMA and threaded handler for the console could lead to 1197e359b441SJohan Hovold * deadlocks. 1198e359b441SJohan Hovold */ 1199e359b441SJohan Hovold if (uart_console(port)) 1200e359b441SJohan Hovold return -ENODEV; 1201e359b441SJohan Hovold 120234891872SAlexandre TORGUE /* Request DMA RX channel */ 120334891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 120434891872SAlexandre TORGUE if (!stm32port->rx_ch) { 120534891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 120634891872SAlexandre TORGUE return -ENODEV; 120734891872SAlexandre TORGUE } 120834891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 120934891872SAlexandre TORGUE &stm32port->rx_dma_buf, 121034891872SAlexandre TORGUE GFP_KERNEL); 121134891872SAlexandre TORGUE if (!stm32port->rx_buf) { 121234891872SAlexandre TORGUE ret = -ENOMEM; 121334891872SAlexandre TORGUE goto alloc_err; 121434891872SAlexandre TORGUE } 121534891872SAlexandre TORGUE 121634891872SAlexandre TORGUE /* Configure DMA channel */ 121734891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 12188e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 121934891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 122034891872SAlexandre TORGUE 122134891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 122234891872SAlexandre TORGUE if (ret < 0) { 122334891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 122434891872SAlexandre TORGUE ret = -ENODEV; 122534891872SAlexandre TORGUE goto config_err; 122634891872SAlexandre TORGUE } 122734891872SAlexandre TORGUE 122834891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 122934891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 123034891872SAlexandre TORGUE stm32port->rx_dma_buf, 123134891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 123234891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 123334891872SAlexandre TORGUE if (!desc) { 123434891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 123534891872SAlexandre TORGUE ret = -ENODEV; 123634891872SAlexandre TORGUE goto config_err; 123734891872SAlexandre TORGUE } 123834891872SAlexandre TORGUE 123934891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 124034891872SAlexandre TORGUE desc->callback = NULL; 124134891872SAlexandre TORGUE desc->callback_param = NULL; 124234891872SAlexandre TORGUE 124334891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 1244e7997f7fSErwan Le Ray ret = dma_submit_error(dmaengine_submit(desc)); 1245e7997f7fSErwan Le Ray if (ret) { 1246e7997f7fSErwan Le Ray dmaengine_terminate_sync(stm32port->rx_ch); 1247e7997f7fSErwan Le Ray goto config_err; 1248e7997f7fSErwan Le Ray } 124934891872SAlexandre TORGUE 125034891872SAlexandre TORGUE /* Issue pending DMA requests */ 125134891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 125234891872SAlexandre TORGUE 125334891872SAlexandre TORGUE return 0; 125434891872SAlexandre TORGUE 125534891872SAlexandre TORGUE config_err: 125634891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 125734891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 125834891872SAlexandre TORGUE stm32port->rx_dma_buf); 125934891872SAlexandre TORGUE 126034891872SAlexandre TORGUE alloc_err: 126134891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 126234891872SAlexandre TORGUE stm32port->rx_ch = NULL; 126334891872SAlexandre TORGUE 126434891872SAlexandre TORGUE return ret; 126534891872SAlexandre TORGUE } 126634891872SAlexandre TORGUE 126756f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port, 126834891872SAlexandre TORGUE struct platform_device *pdev) 126934891872SAlexandre TORGUE { 1270d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 127134891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 127234891872SAlexandre TORGUE struct device *dev = &pdev->dev; 127334891872SAlexandre TORGUE struct dma_slave_config config; 127434891872SAlexandre TORGUE int ret; 127534891872SAlexandre TORGUE 127634891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 127734891872SAlexandre TORGUE 127834891872SAlexandre TORGUE /* Request DMA TX channel */ 127934891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 128034891872SAlexandre TORGUE if (!stm32port->tx_ch) { 128134891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 128234891872SAlexandre TORGUE return -ENODEV; 128334891872SAlexandre TORGUE } 128434891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 128534891872SAlexandre TORGUE &stm32port->tx_dma_buf, 128634891872SAlexandre TORGUE GFP_KERNEL); 128734891872SAlexandre TORGUE if (!stm32port->tx_buf) { 128834891872SAlexandre TORGUE ret = -ENOMEM; 128934891872SAlexandre TORGUE goto alloc_err; 129034891872SAlexandre TORGUE } 129134891872SAlexandre TORGUE 129234891872SAlexandre TORGUE /* Configure DMA channel */ 129334891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 12948e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 129534891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 129634891872SAlexandre TORGUE 129734891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 129834891872SAlexandre TORGUE if (ret < 0) { 129934891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 130034891872SAlexandre TORGUE ret = -ENODEV; 130134891872SAlexandre TORGUE goto config_err; 130234891872SAlexandre TORGUE } 130334891872SAlexandre TORGUE 130434891872SAlexandre TORGUE return 0; 130534891872SAlexandre TORGUE 130634891872SAlexandre TORGUE config_err: 130734891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 130834891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 130934891872SAlexandre TORGUE stm32port->tx_dma_buf); 131034891872SAlexandre TORGUE 131134891872SAlexandre TORGUE alloc_err: 131234891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 131334891872SAlexandre TORGUE stm32port->tx_ch = NULL; 131434891872SAlexandre TORGUE 131534891872SAlexandre TORGUE return ret; 131634891872SAlexandre TORGUE } 131734891872SAlexandre TORGUE 131856f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev) 131948a6092fSMaxime Coquelin { 132048a6092fSMaxime Coquelin struct stm32_port *stm32port; 1321ada8618fSAlexandre TORGUE int ret; 132248a6092fSMaxime Coquelin 132356f9a76cSErwan Le Ray stm32port = stm32_usart_of_get_port(pdev); 132448a6092fSMaxime Coquelin if (!stm32port) 132548a6092fSMaxime Coquelin return -ENODEV; 132648a6092fSMaxime Coquelin 1327d825f0beSStephen Boyd stm32port->info = of_device_get_match_data(&pdev->dev); 1328d825f0beSStephen Boyd if (!stm32port->info) 1329ada8618fSAlexandre TORGUE return -EINVAL; 1330ada8618fSAlexandre TORGUE 133156f9a76cSErwan Le Ray ret = stm32_usart_init_port(stm32port, pdev); 133248a6092fSMaxime Coquelin if (ret) 133348a6092fSMaxime Coquelin return ret; 133448a6092fSMaxime Coquelin 13353d530017SAlexandre Torgue if (stm32port->wakeup_src) { 13363d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, true); 13373d530017SAlexandre Torgue ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq); 13385297f274SErwan Le Ray if (ret) 13395297f274SErwan Le Ray goto err_nowup; 1340270e5a74SFabrice Gasnier } 1341270e5a74SFabrice Gasnier 134256f9a76cSErwan Le Ray ret = stm32_usart_of_dma_rx_probe(stm32port, pdev); 134334891872SAlexandre TORGUE if (ret) 134434891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 134534891872SAlexandre TORGUE 134656f9a76cSErwan Le Ray ret = stm32_usart_of_dma_tx_probe(stm32port, pdev); 134734891872SAlexandre TORGUE if (ret) 134834891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 134934891872SAlexandre TORGUE 135048a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 135148a6092fSMaxime Coquelin 1352fb6dcef6SErwan Le Ray pm_runtime_get_noresume(&pdev->dev); 1353fb6dcef6SErwan Le Ray pm_runtime_set_active(&pdev->dev); 1354fb6dcef6SErwan Le Ray pm_runtime_enable(&pdev->dev); 135587fd0741SErwan Le Ray 135687fd0741SErwan Le Ray ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 135787fd0741SErwan Le Ray if (ret) 135887fd0741SErwan Le Ray goto err_port; 135987fd0741SErwan Le Ray 1360fb6dcef6SErwan Le Ray pm_runtime_put_sync(&pdev->dev); 1361fb6dcef6SErwan Le Ray 136248a6092fSMaxime Coquelin return 0; 1363ada80043SFabrice Gasnier 136487fd0741SErwan Le Ray err_port: 136587fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 136687fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 136787fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 136887fd0741SErwan Le Ray 136987fd0741SErwan Le Ray if (stm32port->rx_ch) { 137087fd0741SErwan Le Ray dmaengine_terminate_async(stm32port->rx_ch); 137187fd0741SErwan Le Ray dma_release_channel(stm32port->rx_ch); 137287fd0741SErwan Le Ray } 137387fd0741SErwan Le Ray 137487fd0741SErwan Le Ray if (stm32port->rx_dma_buf) 137587fd0741SErwan Le Ray dma_free_coherent(&pdev->dev, 137687fd0741SErwan Le Ray RX_BUF_L, stm32port->rx_buf, 137787fd0741SErwan Le Ray stm32port->rx_dma_buf); 137887fd0741SErwan Le Ray 137987fd0741SErwan Le Ray if (stm32port->tx_ch) { 138087fd0741SErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 138187fd0741SErwan Le Ray dma_release_channel(stm32port->tx_ch); 138287fd0741SErwan Le Ray } 138387fd0741SErwan Le Ray 138487fd0741SErwan Le Ray if (stm32port->tx_dma_buf) 138587fd0741SErwan Le Ray dma_free_coherent(&pdev->dev, 138687fd0741SErwan Le Ray TX_BUF_L, stm32port->tx_buf, 138787fd0741SErwan Le Ray stm32port->tx_dma_buf); 138887fd0741SErwan Le Ray 13893d530017SAlexandre Torgue if (stm32port->wakeup_src) 13905297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 13915297f274SErwan Le Ray 1392270e5a74SFabrice Gasnier err_nowup: 13933d530017SAlexandre Torgue if (stm32port->wakeup_src) 13943d530017SAlexandre Torgue device_set_wakeup_capable(&pdev->dev, false); 1395270e5a74SFabrice Gasnier 139697f3a085SErwan Le Ray stm32_usart_deinit_port(stm32port); 1397ada80043SFabrice Gasnier 1398ada80043SFabrice Gasnier return ret; 139948a6092fSMaxime Coquelin } 140048a6092fSMaxime Coquelin 140156f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev) 140248a6092fSMaxime Coquelin { 140348a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1404511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1405d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1406fb6dcef6SErwan Le Ray int err; 1407fb6dcef6SErwan Le Ray 1408fb6dcef6SErwan Le Ray pm_runtime_get_sync(&pdev->dev); 140987fd0741SErwan Le Ray err = uart_remove_one_port(&stm32_usart_driver, port); 141087fd0741SErwan Le Ray if (err) 141187fd0741SErwan Le Ray return(err); 141287fd0741SErwan Le Ray 141387fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 141487fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 141587fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 141634891872SAlexandre TORGUE 141756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 141834891872SAlexandre TORGUE 141987fd0741SErwan Le Ray if (stm32_port->rx_ch) { 142087fd0741SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 142134891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 142287fd0741SErwan Le Ray } 142334891872SAlexandre TORGUE 142434891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 142534891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 142634891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 142734891872SAlexandre TORGUE stm32_port->rx_dma_buf); 142834891872SAlexandre TORGUE 142956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 143034891872SAlexandre TORGUE 143187fd0741SErwan Le Ray if (stm32_port->tx_ch) { 143287fd0741SErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 143334891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 143487fd0741SErwan Le Ray } 143534891872SAlexandre TORGUE 143634891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 143734891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 143834891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 143934891872SAlexandre TORGUE stm32_port->tx_dma_buf); 1440511c7b1bSAlexandre TORGUE 14413d530017SAlexandre Torgue if (stm32_port->wakeup_src) { 14425297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1443270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 14445297f274SErwan Le Ray } 1445270e5a74SFabrice Gasnier 144697f3a085SErwan Le Ray stm32_usart_deinit_port(stm32_port); 144748a6092fSMaxime Coquelin 144887fd0741SErwan Le Ray return 0; 144948a6092fSMaxime Coquelin } 145048a6092fSMaxime Coquelin 145148a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 145256f9a76cSErwan Le Ray static void stm32_usart_console_putchar(struct uart_port *port, int ch) 145348a6092fSMaxime Coquelin { 1454ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1455d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1456ada8618fSAlexandre TORGUE 1457ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 145848a6092fSMaxime Coquelin cpu_relax(); 145948a6092fSMaxime Coquelin 1460ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 146148a6092fSMaxime Coquelin } 146248a6092fSMaxime Coquelin 146356f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s, 146492fc0023SErwan Le Ray unsigned int cnt) 146548a6092fSMaxime Coquelin { 146648a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1467ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1468d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1469d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 147048a6092fSMaxime Coquelin unsigned long flags; 147148a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 147248a6092fSMaxime Coquelin int locked = 1; 147348a6092fSMaxime Coquelin 1474cea37afdSJohan Hovold if (oops_in_progress) 1475cea37afdSJohan Hovold locked = spin_trylock_irqsave(&port->lock, flags); 147648a6092fSMaxime Coquelin else 1477cea37afdSJohan Hovold spin_lock_irqsave(&port->lock, flags); 147848a6092fSMaxime Coquelin 147987f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1480ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 148148a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 148287f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1483ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 148448a6092fSMaxime Coquelin 148556f9a76cSErwan Le Ray uart_console_write(port, s, cnt, stm32_usart_console_putchar); 148648a6092fSMaxime Coquelin 148748a6092fSMaxime Coquelin /* Restore interrupt state */ 1488ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 148948a6092fSMaxime Coquelin 149048a6092fSMaxime Coquelin if (locked) 1491cea37afdSJohan Hovold spin_unlock_irqrestore(&port->lock, flags); 149248a6092fSMaxime Coquelin } 149348a6092fSMaxime Coquelin 149456f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options) 149548a6092fSMaxime Coquelin { 149648a6092fSMaxime Coquelin struct stm32_port *stm32port; 149748a6092fSMaxime Coquelin int baud = 9600; 149848a6092fSMaxime Coquelin int bits = 8; 149948a6092fSMaxime Coquelin int parity = 'n'; 150048a6092fSMaxime Coquelin int flow = 'n'; 150148a6092fSMaxime Coquelin 150248a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 150348a6092fSMaxime Coquelin return -ENODEV; 150448a6092fSMaxime Coquelin 150548a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 150648a6092fSMaxime Coquelin 150748a6092fSMaxime Coquelin /* 150848a6092fSMaxime Coquelin * This driver does not support early console initialization 150948a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 151048a6092fSMaxime Coquelin * this to be called during the uart port registration when the 151148a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 151248a6092fSMaxime Coquelin */ 151392fc0023SErwan Le Ray if (stm32port->port.mapbase == 0 || !stm32port->port.membase) 151448a6092fSMaxime Coquelin return -ENXIO; 151548a6092fSMaxime Coquelin 151648a6092fSMaxime Coquelin if (options) 151748a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 151848a6092fSMaxime Coquelin 151948a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 152048a6092fSMaxime Coquelin } 152148a6092fSMaxime Coquelin 152248a6092fSMaxime Coquelin static struct console stm32_console = { 152348a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 152448a6092fSMaxime Coquelin .device = uart_console_device, 152556f9a76cSErwan Le Ray .write = stm32_usart_console_write, 152656f9a76cSErwan Le Ray .setup = stm32_usart_console_setup, 152748a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 152848a6092fSMaxime Coquelin .index = -1, 152948a6092fSMaxime Coquelin .data = &stm32_usart_driver, 153048a6092fSMaxime Coquelin }; 153148a6092fSMaxime Coquelin 153248a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 153348a6092fSMaxime Coquelin 153448a6092fSMaxime Coquelin #else 153548a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 153648a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 153748a6092fSMaxime Coquelin 153848a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 153948a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 154048a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 154148a6092fSMaxime Coquelin .major = 0, 154248a6092fSMaxime Coquelin .minor = 0, 154348a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 154448a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 154548a6092fSMaxime Coquelin }; 154648a6092fSMaxime Coquelin 154756f9a76cSErwan Le Ray static void __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port, 1548fe94347dSErwan Le Ray bool enable) 1549270e5a74SFabrice Gasnier { 1550270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1551d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1552270e5a74SFabrice Gasnier 15533d530017SAlexandre Torgue if (!stm32_port->wakeup_src) 1554270e5a74SFabrice Gasnier return; 1555270e5a74SFabrice Gasnier 155612761869SErwan Le Ray /* 155712761869SErwan Le Ray * Enable low-power wake-up and wake-up irq if argument is set to 155812761869SErwan Le Ray * "enable", disable low-power wake-up and wake-up irq otherwise 155912761869SErwan Le Ray */ 1560270e5a74SFabrice Gasnier if (enable) { 156156f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM); 156212761869SErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE); 1563270e5a74SFabrice Gasnier } else { 156456f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM); 156512761869SErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE); 1566270e5a74SFabrice Gasnier } 1567270e5a74SFabrice Gasnier } 1568270e5a74SFabrice Gasnier 156956f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev) 1570270e5a74SFabrice Gasnier { 1571270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1572270e5a74SFabrice Gasnier 1573270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1574270e5a74SFabrice Gasnier 15751631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 157656f9a76cSErwan Le Ray stm32_usart_serial_en_wakeup(port, true); 1577270e5a74SFabrice Gasnier 157855484fccSErwan Le Ray /* 157955484fccSErwan Le Ray * When "no_console_suspend" is enabled, keep the pinctrl default state 158055484fccSErwan Le Ray * and rely on bootloader stage to restore this state upon resume. 158155484fccSErwan Le Ray * Otherwise, apply the idle or sleep states depending on wakeup 158255484fccSErwan Le Ray * capabilities. 158355484fccSErwan Le Ray */ 158455484fccSErwan Le Ray if (console_suspend_enabled || !uart_console(port)) { 15851631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 158655484fccSErwan Le Ray pinctrl_pm_select_idle_state(dev); 158755484fccSErwan Le Ray else 158894616d9aSErwan Le Ray pinctrl_pm_select_sleep_state(dev); 158955484fccSErwan Le Ray } 159094616d9aSErwan Le Ray 1591270e5a74SFabrice Gasnier return 0; 1592270e5a74SFabrice Gasnier } 1593270e5a74SFabrice Gasnier 159456f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev) 1595270e5a74SFabrice Gasnier { 1596270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1597270e5a74SFabrice Gasnier 159894616d9aSErwan Le Ray pinctrl_pm_select_default_state(dev); 159994616d9aSErwan Le Ray 16001631eeeaSErwan Le Ray if (device_may_wakeup(dev) || device_wakeup_path(dev)) 160156f9a76cSErwan Le Ray stm32_usart_serial_en_wakeup(port, false); 1602270e5a74SFabrice Gasnier 1603270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1604270e5a74SFabrice Gasnier } 1605270e5a74SFabrice Gasnier 160656f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev) 1607fb6dcef6SErwan Le Ray { 1608fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1609fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1610fb6dcef6SErwan Le Ray struct stm32_port, port); 1611fb6dcef6SErwan Le Ray 1612fb6dcef6SErwan Le Ray clk_disable_unprepare(stm32port->clk); 1613fb6dcef6SErwan Le Ray 1614fb6dcef6SErwan Le Ray return 0; 1615fb6dcef6SErwan Le Ray } 1616fb6dcef6SErwan Le Ray 161756f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev) 1618fb6dcef6SErwan Le Ray { 1619fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1620fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1621fb6dcef6SErwan Le Ray struct stm32_port, port); 1622fb6dcef6SErwan Le Ray 1623fb6dcef6SErwan Le Ray return clk_prepare_enable(stm32port->clk); 1624fb6dcef6SErwan Le Ray } 1625fb6dcef6SErwan Le Ray 1626270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 162756f9a76cSErwan Le Ray SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend, 162856f9a76cSErwan Le Ray stm32_usart_runtime_resume, NULL) 162956f9a76cSErwan Le Ray SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend, 163056f9a76cSErwan Le Ray stm32_usart_serial_resume) 1631270e5a74SFabrice Gasnier }; 1632270e5a74SFabrice Gasnier 163348a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 163456f9a76cSErwan Le Ray .probe = stm32_usart_serial_probe, 163556f9a76cSErwan Le Ray .remove = stm32_usart_serial_remove, 163648a6092fSMaxime Coquelin .driver = { 163748a6092fSMaxime Coquelin .name = DRIVER_NAME, 1638270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 163948a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 164048a6092fSMaxime Coquelin }, 164148a6092fSMaxime Coquelin }; 164248a6092fSMaxime Coquelin 164356f9a76cSErwan Le Ray static int __init stm32_usart_init(void) 164448a6092fSMaxime Coquelin { 164548a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 164648a6092fSMaxime Coquelin int ret; 164748a6092fSMaxime Coquelin 164848a6092fSMaxime Coquelin pr_info("%s\n", banner); 164948a6092fSMaxime Coquelin 165048a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 165148a6092fSMaxime Coquelin if (ret) 165248a6092fSMaxime Coquelin return ret; 165348a6092fSMaxime Coquelin 165448a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 165548a6092fSMaxime Coquelin if (ret) 165648a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 165748a6092fSMaxime Coquelin 165848a6092fSMaxime Coquelin return ret; 165948a6092fSMaxime Coquelin } 166048a6092fSMaxime Coquelin 166156f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void) 166248a6092fSMaxime Coquelin { 166348a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 166448a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 166548a6092fSMaxime Coquelin } 166648a6092fSMaxime Coquelin 166756f9a76cSErwan Le Ray module_init(stm32_usart_init); 166856f9a76cSErwan Le Ray module_exit(stm32_usart_exit); 166948a6092fSMaxime Coquelin 167048a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 167148a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 167248a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1673