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; 21748a6092fSMaxime Coquelin unsigned long c; 21848a6092fSMaxime Coquelin u32 sr; 21948a6092fSMaxime Coquelin char flag; 22048a6092fSMaxime Coquelin 22129d60981SAndy Shevchenko if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 22248a6092fSMaxime Coquelin pm_wakeup_event(tport->tty->dev, 0); 22348a6092fSMaxime Coquelin 22456f9a76cSErwan Le Ray while (stm32_usart_pending_rx(port, &sr, &stm32_port->last_res, 22556f9a76cSErwan Le Ray threaded)) { 22648a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22748a6092fSMaxime Coquelin flag = TTY_NORMAL; 22848a6092fSMaxime Coquelin 2294f01d833SErwan Le Ray /* 2304f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2314f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2324f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2334f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2344f01d833SErwan Le Ray * and clear status bits of the next rx data. 2354f01d833SErwan Le Ray * 2364f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2374f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 2384f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 2394f01d833SErwan Le Ray */ 2404f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 2411250ed71SFabrice Gasnier writel_relaxed(sr & USART_SR_ERR_MASK, 2421250ed71SFabrice Gasnier port->membase + ofs->icr); 2434f01d833SErwan Le Ray 24456f9a76cSErwan Le Ray c = stm32_usart_get_char(port, &sr, &stm32_port->last_res); 2454f01d833SErwan Le Ray port->icount.rx++; 24648a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 2474f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 24848a6092fSMaxime Coquelin port->icount.overrun++; 24948a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 25048a6092fSMaxime Coquelin port->icount.parity++; 25148a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 2524f01d833SErwan Le Ray /* Break detection if character is null */ 2534f01d833SErwan Le Ray if (!c) { 2544f01d833SErwan Le Ray port->icount.brk++; 2554f01d833SErwan Le Ray if (uart_handle_break(port)) 2564f01d833SErwan Le Ray continue; 2574f01d833SErwan Le Ray } else { 25848a6092fSMaxime Coquelin port->icount.frame++; 25948a6092fSMaxime Coquelin } 2604f01d833SErwan Le Ray } 26148a6092fSMaxime Coquelin 26248a6092fSMaxime Coquelin sr &= port->read_status_mask; 26348a6092fSMaxime Coquelin 2644f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26548a6092fSMaxime Coquelin flag = TTY_PARITY; 2664f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 2674f01d833SErwan Le Ray if (!c) 2684f01d833SErwan Le Ray flag = TTY_BREAK; 2694f01d833SErwan Le Ray else 27048a6092fSMaxime Coquelin flag = TTY_FRAME; 27148a6092fSMaxime Coquelin } 2724f01d833SErwan Le Ray } 27348a6092fSMaxime Coquelin 27448a6092fSMaxime Coquelin if (uart_handle_sysrq_char(port, c)) 27548a6092fSMaxime Coquelin continue; 27648a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27748a6092fSMaxime Coquelin } 27848a6092fSMaxime Coquelin 27948a6092fSMaxime Coquelin spin_unlock(&port->lock); 28048a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 28148a6092fSMaxime Coquelin spin_lock(&port->lock); 28248a6092fSMaxime Coquelin } 28348a6092fSMaxime Coquelin 28456f9a76cSErwan Le Ray static void stm32_usart_tx_dma_complete(void *arg) 28534891872SAlexandre TORGUE { 28634891872SAlexandre TORGUE struct uart_port *port = arg; 28734891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 288d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 28934891872SAlexandre TORGUE 29056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 29134891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 29234891872SAlexandre TORGUE 29334891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 29456f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 29534891872SAlexandre TORGUE } 29634891872SAlexandre TORGUE 29756f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_enable(struct uart_port *port) 298d075719eSErwan Le Ray { 299d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 300d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 301d075719eSErwan Le Ray 302d075719eSErwan Le Ray /* 303d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 304d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 305d075719eSErwan Le Ray */ 306d075719eSErwan Le Ray if (stm32_port->fifoen) 30756f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 308d075719eSErwan Le Ray else 30956f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 310d075719eSErwan Le Ray } 311d075719eSErwan Le Ray 31256f9a76cSErwan Le Ray static void stm32_usart_tx_interrupt_disable(struct uart_port *port) 313d075719eSErwan Le Ray { 314d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 315d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 316d075719eSErwan Le Ray 317d075719eSErwan Le Ray if (stm32_port->fifoen) 31856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 319d075719eSErwan Le Ray else 32056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 321d075719eSErwan Le Ray } 322d075719eSErwan Le Ray 32356f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_pio(struct uart_port *port) 32434891872SAlexandre TORGUE { 32534891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 326d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 32734891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 32834891872SAlexandre TORGUE 32934891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 33056f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 33134891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 33234891872SAlexandre TORGUE } 33334891872SAlexandre TORGUE 3345d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 3355d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 3365d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 3375d9176edSErwan Le Ray break; 33834891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 33934891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 34034891872SAlexandre TORGUE port->icount.tx++; 34134891872SAlexandre TORGUE } 34234891872SAlexandre TORGUE 3435d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 3445d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 34556f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 3465d9176edSErwan Le Ray else 34756f9a76cSErwan Le Ray stm32_usart_tx_interrupt_enable(port); 3485d9176edSErwan Le Ray } 3495d9176edSErwan Le Ray 35056f9a76cSErwan Le Ray static void stm32_usart_transmit_chars_dma(struct uart_port *port) 35134891872SAlexandre TORGUE { 35234891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 353d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 35434891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 35534891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 35634891872SAlexandre TORGUE unsigned int count, i; 35734891872SAlexandre TORGUE 35834891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 35934891872SAlexandre TORGUE return; 36034891872SAlexandre TORGUE 36134891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 36234891872SAlexandre TORGUE 36334891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 36434891872SAlexandre TORGUE 36534891872SAlexandre TORGUE if (count > TX_BUF_L) 36634891872SAlexandre TORGUE count = TX_BUF_L; 36734891872SAlexandre TORGUE 36834891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 36934891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 37034891872SAlexandre TORGUE } else { 37134891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 37234891872SAlexandre TORGUE size_t two; 37334891872SAlexandre TORGUE 37434891872SAlexandre TORGUE if (one > count) 37534891872SAlexandre TORGUE one = count; 37634891872SAlexandre TORGUE two = count - one; 37734891872SAlexandre TORGUE 37834891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 37934891872SAlexandre TORGUE if (two) 38034891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 38134891872SAlexandre TORGUE } 38234891872SAlexandre TORGUE 38334891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 38434891872SAlexandre TORGUE stm32port->tx_dma_buf, 38534891872SAlexandre TORGUE count, 38634891872SAlexandre TORGUE DMA_MEM_TO_DEV, 38734891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 38834891872SAlexandre TORGUE 389e7997f7fSErwan Le Ray if (!desc) 390e7997f7fSErwan Le Ray goto fallback_err; 39134891872SAlexandre TORGUE 39256f9a76cSErwan Le Ray desc->callback = stm32_usart_tx_dma_complete; 39334891872SAlexandre TORGUE desc->callback_param = port; 39434891872SAlexandre TORGUE 39534891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 396e7997f7fSErwan Le Ray if (dma_submit_error(dmaengine_submit(desc))) { 397e7997f7fSErwan Le Ray /* dma no yet started, safe to free resources */ 398e7997f7fSErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 399e7997f7fSErwan Le Ray goto fallback_err; 400e7997f7fSErwan Le Ray } 40134891872SAlexandre TORGUE 40234891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 40334891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 40434891872SAlexandre TORGUE 40556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 40634891872SAlexandre TORGUE 40734891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 40834891872SAlexandre TORGUE port->icount.tx += count; 409e7997f7fSErwan Le Ray return; 410e7997f7fSErwan Le Ray 411e7997f7fSErwan Le Ray fallback_err: 412e7997f7fSErwan Le Ray for (i = count; i > 0; i--) 41356f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 41434891872SAlexandre TORGUE } 41534891872SAlexandre TORGUE 41656f9a76cSErwan Le Ray static void stm32_usart_transmit_chars(struct uart_port *port) 41748a6092fSMaxime Coquelin { 418ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 419d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 42048a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 42148a6092fSMaxime Coquelin 42248a6092fSMaxime Coquelin if (port->x_char) { 42334891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 42456f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 425ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 42648a6092fSMaxime Coquelin port->x_char = 0; 42748a6092fSMaxime Coquelin port->icount.tx++; 42834891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 42956f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAT); 43048a6092fSMaxime Coquelin return; 43148a6092fSMaxime Coquelin } 43248a6092fSMaxime Coquelin 433b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 43456f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 43548a6092fSMaxime Coquelin return; 43648a6092fSMaxime Coquelin } 43748a6092fSMaxime Coquelin 43864c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 43956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC); 44064c32eabSErwan Le Ray else 4411250ed71SFabrice Gasnier writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr); 44264c32eabSErwan Le Ray 44334891872SAlexandre TORGUE if (stm32_port->tx_ch) 44456f9a76cSErwan Le Ray stm32_usart_transmit_chars_dma(port); 44534891872SAlexandre TORGUE else 44656f9a76cSErwan Le Ray stm32_usart_transmit_chars_pio(port); 44748a6092fSMaxime Coquelin 44848a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 44948a6092fSMaxime Coquelin uart_write_wakeup(port); 45048a6092fSMaxime Coquelin 45148a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 45256f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 45348a6092fSMaxime Coquelin } 45448a6092fSMaxime Coquelin 45556f9a76cSErwan Le Ray static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) 45648a6092fSMaxime Coquelin { 45748a6092fSMaxime Coquelin struct uart_port *port = ptr; 458ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 459d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 46048a6092fSMaxime Coquelin u32 sr; 46148a6092fSMaxime Coquelin 46201d32d71SAlexandre TORGUE spin_lock(&port->lock); 46301d32d71SAlexandre TORGUE 464ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 46548a6092fSMaxime Coquelin 4664cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 4674cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 4684cc0ed62SErwan Le Ray port->membase + ofs->icr); 4694cc0ed62SErwan Le Ray 47092fc0023SErwan Le Ray if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) 471270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 472270e5a74SFabrice Gasnier port->membase + ofs->icr); 473270e5a74SFabrice Gasnier 47434891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 47556f9a76cSErwan Le Ray stm32_usart_receive_chars(port, false); 47648a6092fSMaxime Coquelin 47734891872SAlexandre TORGUE if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) 47856f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 47948a6092fSMaxime Coquelin 48001d32d71SAlexandre TORGUE spin_unlock(&port->lock); 48101d32d71SAlexandre TORGUE 48234891872SAlexandre TORGUE if (stm32_port->rx_ch) 48334891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 48434891872SAlexandre TORGUE else 48534891872SAlexandre TORGUE return IRQ_HANDLED; 48634891872SAlexandre TORGUE } 48734891872SAlexandre TORGUE 48856f9a76cSErwan Le Ray static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr) 48934891872SAlexandre TORGUE { 49034891872SAlexandre TORGUE struct uart_port *port = ptr; 49134891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 49234891872SAlexandre TORGUE 49334891872SAlexandre TORGUE spin_lock(&port->lock); 49434891872SAlexandre TORGUE 49534891872SAlexandre TORGUE if (stm32_port->rx_ch) 49656f9a76cSErwan Le Ray stm32_usart_receive_chars(port, true); 49734891872SAlexandre TORGUE 49848a6092fSMaxime Coquelin spin_unlock(&port->lock); 49948a6092fSMaxime Coquelin 50048a6092fSMaxime Coquelin return IRQ_HANDLED; 50148a6092fSMaxime Coquelin } 50248a6092fSMaxime Coquelin 50356f9a76cSErwan Le Ray static unsigned int stm32_usart_tx_empty(struct uart_port *port) 50448a6092fSMaxime Coquelin { 505ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 506d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 507ada8618fSAlexandre TORGUE 508ada8618fSAlexandre TORGUE return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; 50948a6092fSMaxime Coquelin } 51048a6092fSMaxime Coquelin 51156f9a76cSErwan Le Ray static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl) 51248a6092fSMaxime Coquelin { 513ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 514d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 515ada8618fSAlexandre TORGUE 51648a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 51756f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE); 51848a6092fSMaxime Coquelin else 51956f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 5206cf61b9bSManivannan Sadhasivam 5216cf61b9bSManivannan Sadhasivam mctrl_gpio_set(stm32_port->gpios, mctrl); 52248a6092fSMaxime Coquelin } 52348a6092fSMaxime Coquelin 52456f9a76cSErwan Le Ray static unsigned int stm32_usart_get_mctrl(struct uart_port *port) 52548a6092fSMaxime Coquelin { 5266cf61b9bSManivannan Sadhasivam struct stm32_port *stm32_port = to_stm32_port(port); 5276cf61b9bSManivannan Sadhasivam unsigned int ret; 5286cf61b9bSManivannan Sadhasivam 52948a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 5306cf61b9bSManivannan Sadhasivam ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 5316cf61b9bSManivannan Sadhasivam 5326cf61b9bSManivannan Sadhasivam return mctrl_gpio_get(stm32_port->gpios, &ret); 5336cf61b9bSManivannan Sadhasivam } 5346cf61b9bSManivannan Sadhasivam 53556f9a76cSErwan Le Ray static void stm32_usart_enable_ms(struct uart_port *port) 5366cf61b9bSManivannan Sadhasivam { 5376cf61b9bSManivannan Sadhasivam mctrl_gpio_enable_ms(to_stm32_port(port)->gpios); 5386cf61b9bSManivannan Sadhasivam } 5396cf61b9bSManivannan Sadhasivam 54056f9a76cSErwan Le Ray static void stm32_usart_disable_ms(struct uart_port *port) 5416cf61b9bSManivannan Sadhasivam { 5426cf61b9bSManivannan Sadhasivam mctrl_gpio_disable_ms(to_stm32_port(port)->gpios); 54348a6092fSMaxime Coquelin } 54448a6092fSMaxime Coquelin 54548a6092fSMaxime Coquelin /* Transmit stop */ 54656f9a76cSErwan Le Ray static void stm32_usart_stop_tx(struct uart_port *port) 54748a6092fSMaxime Coquelin { 548ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 549ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 550ad0c2748SMarek Vasut 55156f9a76cSErwan Le Ray stm32_usart_tx_interrupt_disable(port); 552ad0c2748SMarek Vasut 553ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 554ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 555ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 556ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 557ad0c2748SMarek Vasut } else { 558ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 559ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 560ad0c2748SMarek Vasut } 561ad0c2748SMarek Vasut } 56248a6092fSMaxime Coquelin } 56348a6092fSMaxime Coquelin 56448a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 56556f9a76cSErwan Le Ray static void stm32_usart_start_tx(struct uart_port *port) 56648a6092fSMaxime Coquelin { 567ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 568ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 56948a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 57048a6092fSMaxime Coquelin 57148a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 57248a6092fSMaxime Coquelin return; 57348a6092fSMaxime Coquelin 574ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 575ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 576ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 577ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 578ad0c2748SMarek Vasut } else { 579ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 580ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 581ad0c2748SMarek Vasut } 582ad0c2748SMarek Vasut } 583ad0c2748SMarek Vasut 58456f9a76cSErwan Le Ray stm32_usart_transmit_chars(port); 58548a6092fSMaxime Coquelin } 58648a6092fSMaxime Coquelin 58748a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 58856f9a76cSErwan Le Ray static void stm32_usart_throttle(struct uart_port *port) 58948a6092fSMaxime Coquelin { 590ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 591d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 59248a6092fSMaxime Coquelin unsigned long flags; 59348a6092fSMaxime Coquelin 59448a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 59556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 596d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 59756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 598d0a6a7bcSErwan Le Ray 59948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 60048a6092fSMaxime Coquelin } 60148a6092fSMaxime Coquelin 60248a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 60356f9a76cSErwan Le Ray static void stm32_usart_unthrottle(struct uart_port *port) 60448a6092fSMaxime Coquelin { 605ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 606d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 60748a6092fSMaxime Coquelin unsigned long flags; 60848a6092fSMaxime Coquelin 60948a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 61056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 611d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 61256f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq); 613d0a6a7bcSErwan Le Ray 61448a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 61548a6092fSMaxime Coquelin } 61648a6092fSMaxime Coquelin 61748a6092fSMaxime Coquelin /* Receive stop */ 61856f9a76cSErwan Le Ray static void stm32_usart_stop_rx(struct uart_port *port) 61948a6092fSMaxime Coquelin { 620ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 621d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 622ada8618fSAlexandre TORGUE 62356f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 624d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 62556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 62648a6092fSMaxime Coquelin } 62748a6092fSMaxime Coquelin 62848a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 62956f9a76cSErwan Le Ray static void stm32_usart_break_ctl(struct uart_port *port, int break_state) 63048a6092fSMaxime Coquelin { 63148a6092fSMaxime Coquelin } 63248a6092fSMaxime Coquelin 63356f9a76cSErwan Le Ray static int stm32_usart_startup(struct uart_port *port) 63448a6092fSMaxime Coquelin { 635ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 636d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 637f4518a8aSErwan Le Ray const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 63848a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 63948a6092fSMaxime Coquelin u32 val; 64048a6092fSMaxime Coquelin int ret; 64148a6092fSMaxime Coquelin 64256f9a76cSErwan Le Ray ret = request_threaded_irq(port->irq, stm32_usart_interrupt, 64356f9a76cSErwan Le Ray stm32_usart_threaded_interrupt, 64434891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 64548a6092fSMaxime Coquelin if (ret) 64648a6092fSMaxime Coquelin return ret; 64748a6092fSMaxime Coquelin 64884872dc4SErwan Le Ray /* RX FIFO Flush */ 64984872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 65056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->rqr, USART_RQR_RXFRQ); 65148a6092fSMaxime Coquelin 65284872dc4SErwan Le Ray /* Tx and RX FIFO configuration */ 653d075719eSErwan Le Ray if (stm32_port->fifoen) { 654d075719eSErwan Le Ray val = readl_relaxed(port->membase + ofs->cr3); 655d0a6a7bcSErwan Le Ray val &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK); 656d075719eSErwan Le Ray val |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT; 657d0a6a7bcSErwan Le Ray val |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT; 658d075719eSErwan Le Ray writel_relaxed(val, port->membase + ofs->cr3); 659d075719eSErwan Le Ray } 660d075719eSErwan Le Ray 66184872dc4SErwan Le Ray /* RX FIFO enabling */ 662f4518a8aSErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit); 66384872dc4SErwan Le Ray if (stm32_port->fifoen) 66484872dc4SErwan Le Ray val |= USART_CR1_FIFOEN; 66556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, val); 66684872dc4SErwan Le Ray 66748a6092fSMaxime Coquelin return 0; 66848a6092fSMaxime Coquelin } 66948a6092fSMaxime Coquelin 67056f9a76cSErwan Le Ray static void stm32_usart_shutdown(struct uart_port *port) 67148a6092fSMaxime Coquelin { 672ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 673d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 674d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 67564c32eabSErwan Le Ray u32 val, isr; 67664c32eabSErwan Le Ray int ret; 67748a6092fSMaxime Coquelin 6786cf61b9bSManivannan Sadhasivam /* Disable modem control interrupts */ 67956f9a76cSErwan Le Ray stm32_usart_disable_ms(port); 6806cf61b9bSManivannan Sadhasivam 6814cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 6824cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 68387f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 684351a762aSGerald Baeza if (stm32_port->fifoen) 685351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 68664c32eabSErwan Le Ray 68764c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 68864c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 68964c32eabSErwan Le Ray 10, 100000); 69064c32eabSErwan Le Ray 691c31c3ea0SErwan Le Ray /* Send the TC error message only when ISR_TC is not set */ 69264c32eabSErwan Le Ray if (ret) 693c31c3ea0SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 69464c32eabSErwan Le Ray 69556f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, val); 69648a6092fSMaxime Coquelin 69748a6092fSMaxime Coquelin free_irq(port->irq, port); 69848a6092fSMaxime Coquelin } 69948a6092fSMaxime Coquelin 70056f9a76cSErwan Le Ray static unsigned int stm32_usart_get_databits(struct ktermios *termios) 701c8a9d043SErwan Le Ray { 702c8a9d043SErwan Le Ray unsigned int bits; 703c8a9d043SErwan Le Ray 704c8a9d043SErwan Le Ray tcflag_t cflag = termios->c_cflag; 705c8a9d043SErwan Le Ray 706c8a9d043SErwan Le Ray switch (cflag & CSIZE) { 707c8a9d043SErwan Le Ray /* 708c8a9d043SErwan Le Ray * CSIZE settings are not necessarily supported in hardware. 709c8a9d043SErwan Le Ray * CSIZE unsupported configurations are handled here to set word length 710c8a9d043SErwan Le Ray * to 8 bits word as default configuration and to print debug message. 711c8a9d043SErwan Le Ray */ 712c8a9d043SErwan Le Ray case CS5: 713c8a9d043SErwan Le Ray bits = 5; 714c8a9d043SErwan Le Ray break; 715c8a9d043SErwan Le Ray case CS6: 716c8a9d043SErwan Le Ray bits = 6; 717c8a9d043SErwan Le Ray break; 718c8a9d043SErwan Le Ray case CS7: 719c8a9d043SErwan Le Ray bits = 7; 720c8a9d043SErwan Le Ray break; 721c8a9d043SErwan Le Ray /* default including CS8 */ 722c8a9d043SErwan Le Ray default: 723c8a9d043SErwan Le Ray bits = 8; 724c8a9d043SErwan Le Ray break; 725c8a9d043SErwan Le Ray } 726c8a9d043SErwan Le Ray 727c8a9d043SErwan Le Ray return bits; 728c8a9d043SErwan Le Ray } 729c8a9d043SErwan Le Ray 73056f9a76cSErwan Le Ray static void stm32_usart_set_termios(struct uart_port *port, 73156f9a76cSErwan Le Ray struct ktermios *termios, 73248a6092fSMaxime Coquelin struct ktermios *old) 73348a6092fSMaxime Coquelin { 73448a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 735d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 736d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 7371bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 738c8a9d043SErwan Le Ray unsigned int baud, bits; 73948a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 74048a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 741*f264c6f6SErwan Le Ray u32 cr1, cr2, cr3, isr; 74248a6092fSMaxime Coquelin unsigned long flags; 743*f264c6f6SErwan Le Ray int ret; 74448a6092fSMaxime Coquelin 74548a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 74648a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 74748a6092fSMaxime Coquelin 74848a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 74948a6092fSMaxime Coquelin 75048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 75148a6092fSMaxime Coquelin 752*f264c6f6SErwan Le Ray ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 753*f264c6f6SErwan Le Ray isr, 754*f264c6f6SErwan Le Ray (isr & USART_SR_TC), 755*f264c6f6SErwan Le Ray 10, 100000); 756*f264c6f6SErwan Le Ray 757*f264c6f6SErwan Le Ray /* Send the TC error message only when ISR_TC is not set. */ 758*f264c6f6SErwan Le Ray if (ret) 759*f264c6f6SErwan Le Ray dev_err(port->dev, "Transmission is not complete\n"); 760*f264c6f6SErwan Le Ray 76148a6092fSMaxime Coquelin /* Stop serial port and reset value */ 762ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 76348a6092fSMaxime Coquelin 76484872dc4SErwan Le Ray /* flush RX & TX FIFO */ 76584872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 76656f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->rqr, 76784872dc4SErwan Le Ray USART_RQR_TXFRQ | USART_RQR_RXFRQ); 7681bcda09dSBich HEMON 76984872dc4SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 770351a762aSGerald Baeza if (stm32_port->fifoen) 771351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 77248a6092fSMaxime Coquelin cr2 = 0; 773d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 774d0a6a7bcSErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTCFG_MASK | USART_CR3_RXFTIE 775d075719eSErwan Le Ray | USART_CR3_TXFTCFG_MASK; 77648a6092fSMaxime Coquelin 77748a6092fSMaxime Coquelin if (cflag & CSTOPB) 77848a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 77948a6092fSMaxime Coquelin 78056f9a76cSErwan Le Ray bits = stm32_usart_get_databits(termios); 7816c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 782c8a9d043SErwan Le Ray 78348a6092fSMaxime Coquelin if (cflag & PARENB) { 784c8a9d043SErwan Le Ray bits++; 78548a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 786c8a9d043SErwan Le Ray } 787c8a9d043SErwan Le Ray 788c8a9d043SErwan Le Ray /* 789c8a9d043SErwan Le Ray * Word length configuration: 790c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 791c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 792c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 793c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 794c8a9d043SErwan Le Ray */ 795c8a9d043SErwan Le Ray if (bits == 9) 796ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 797c8a9d043SErwan Le Ray else if ((bits == 7) && cfg->has_7bits_data) 798c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 799c8a9d043SErwan Le Ray else if (bits != 8) 800c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 801c8a9d043SErwan Le Ray , bits); 80248a6092fSMaxime Coquelin 8034cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 8044cc0ed62SErwan Le Ray stm32_port->fifoen)) { 8054cc0ed62SErwan Le Ray if (cflag & CSTOPB) 8064cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 8074cc0ed62SErwan Le Ray else 8084cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 8094cc0ed62SErwan Le Ray 8104cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 8114cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 8124cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 8134cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 814d0a6a7bcSErwan Le Ray /* Not using dma, enable fifo threshold irq */ 815d0a6a7bcSErwan Le Ray if (!stm32_port->rx_ch) 816d0a6a7bcSErwan Le Ray stm32_port->cr3_irq = USART_CR3_RXFTIE; 8174cc0ed62SErwan Le Ray } 8184cc0ed62SErwan Le Ray 819d0a6a7bcSErwan Le Ray cr1 |= stm32_port->cr1_irq; 820d0a6a7bcSErwan Le Ray cr3 |= stm32_port->cr3_irq; 821d0a6a7bcSErwan Le Ray 82248a6092fSMaxime Coquelin if (cflag & PARODD) 82348a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 82448a6092fSMaxime Coquelin 82548a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 82648a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 82748a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 82835abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 82948a6092fSMaxime Coquelin } 83048a6092fSMaxime Coquelin 8316cf61b9bSManivannan Sadhasivam /* Handle modem control interrupts */ 8326cf61b9bSManivannan Sadhasivam if (UART_ENABLE_MS(port, termios->c_cflag)) 83356f9a76cSErwan Le Ray stm32_usart_enable_ms(port); 8346cf61b9bSManivannan Sadhasivam else 83556f9a76cSErwan Le Ray stm32_usart_disable_ms(port); 8366cf61b9bSManivannan Sadhasivam 83748a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 83848a6092fSMaxime Coquelin 83948a6092fSMaxime Coquelin /* 84048a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 84148a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 84248a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 84348a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 84448a6092fSMaxime Coquelin */ 84548a6092fSMaxime Coquelin if (usartdiv < 16) { 84648a6092fSMaxime Coquelin oversampling = 8; 8471bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 84856f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8); 84948a6092fSMaxime Coquelin } else { 85048a6092fSMaxime Coquelin oversampling = 16; 8511bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 85256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 85348a6092fSMaxime Coquelin } 85448a6092fSMaxime Coquelin 85548a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 85648a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 857ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 85848a6092fSMaxime Coquelin 85948a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 86048a6092fSMaxime Coquelin 86148a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 86248a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 86348a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 86448a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 8654f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 86648a6092fSMaxime Coquelin 86748a6092fSMaxime Coquelin /* Characters to ignore */ 86848a6092fSMaxime Coquelin port->ignore_status_mask = 0; 86948a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 87048a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 87148a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 8724f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 87348a6092fSMaxime Coquelin /* 87448a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 87548a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 87648a6092fSMaxime Coquelin */ 87748a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 87848a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 87948a6092fSMaxime Coquelin } 88048a6092fSMaxime Coquelin 88148a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 88248a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 88348a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 88448a6092fSMaxime Coquelin 88534891872SAlexandre TORGUE if (stm32_port->rx_ch) 88634891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 88734891872SAlexandre TORGUE 8881bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 88956f9a76cSErwan Le Ray stm32_usart_config_reg_rs485(&cr1, &cr3, 8901bcda09dSBich HEMON rs485conf->delay_rts_before_send, 89156f9a76cSErwan Le Ray rs485conf->delay_rts_after_send, 89256f9a76cSErwan Le Ray baud); 8931bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 8941bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 8951bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 8961bcda09dSBich HEMON } else { 8971bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 8981bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 8991bcda09dSBich HEMON } 9001bcda09dSBich HEMON 9011bcda09dSBich HEMON } else { 9021bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 9031bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 9041bcda09dSBich HEMON } 9051bcda09dSBich HEMON 906ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 907ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 908ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 90948a6092fSMaxime Coquelin 91056f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 91148a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 91248a6092fSMaxime Coquelin } 91348a6092fSMaxime Coquelin 91456f9a76cSErwan Le Ray static const char *stm32_usart_type(struct uart_port *port) 91548a6092fSMaxime Coquelin { 91648a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 91748a6092fSMaxime Coquelin } 91848a6092fSMaxime Coquelin 91956f9a76cSErwan Le Ray static void stm32_usart_release_port(struct uart_port *port) 92048a6092fSMaxime Coquelin { 92148a6092fSMaxime Coquelin } 92248a6092fSMaxime Coquelin 92356f9a76cSErwan Le Ray static int stm32_usart_request_port(struct uart_port *port) 92448a6092fSMaxime Coquelin { 92548a6092fSMaxime Coquelin return 0; 92648a6092fSMaxime Coquelin } 92748a6092fSMaxime Coquelin 92856f9a76cSErwan Le Ray static void stm32_usart_config_port(struct uart_port *port, int flags) 92948a6092fSMaxime Coquelin { 93048a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 93148a6092fSMaxime Coquelin port->type = PORT_STM32; 93248a6092fSMaxime Coquelin } 93348a6092fSMaxime Coquelin 93448a6092fSMaxime Coquelin static int 93556f9a76cSErwan Le Ray stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser) 93648a6092fSMaxime Coquelin { 93748a6092fSMaxime Coquelin /* No user changeable parameters */ 93848a6092fSMaxime Coquelin return -EINVAL; 93948a6092fSMaxime Coquelin } 94048a6092fSMaxime Coquelin 94156f9a76cSErwan Le Ray static void stm32_usart_pm(struct uart_port *port, unsigned int state, 94248a6092fSMaxime Coquelin unsigned int oldstate) 94348a6092fSMaxime Coquelin { 94448a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 94548a6092fSMaxime Coquelin struct stm32_port, port); 946d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 947d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32port->info->cfg; 94848a6092fSMaxime Coquelin unsigned long flags = 0; 94948a6092fSMaxime Coquelin 95048a6092fSMaxime Coquelin switch (state) { 95148a6092fSMaxime Coquelin case UART_PM_STATE_ON: 952fb6dcef6SErwan Le Ray pm_runtime_get_sync(port->dev); 95348a6092fSMaxime Coquelin break; 95448a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 95548a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 95656f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 95748a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 958fb6dcef6SErwan Le Ray pm_runtime_put_sync(port->dev); 95948a6092fSMaxime Coquelin break; 96048a6092fSMaxime Coquelin } 96148a6092fSMaxime Coquelin } 96248a6092fSMaxime Coquelin 96348a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 96456f9a76cSErwan Le Ray .tx_empty = stm32_usart_tx_empty, 96556f9a76cSErwan Le Ray .set_mctrl = stm32_usart_set_mctrl, 96656f9a76cSErwan Le Ray .get_mctrl = stm32_usart_get_mctrl, 96756f9a76cSErwan Le Ray .stop_tx = stm32_usart_stop_tx, 96856f9a76cSErwan Le Ray .start_tx = stm32_usart_start_tx, 96956f9a76cSErwan Le Ray .throttle = stm32_usart_throttle, 97056f9a76cSErwan Le Ray .unthrottle = stm32_usart_unthrottle, 97156f9a76cSErwan Le Ray .stop_rx = stm32_usart_stop_rx, 97256f9a76cSErwan Le Ray .enable_ms = stm32_usart_enable_ms, 97356f9a76cSErwan Le Ray .break_ctl = stm32_usart_break_ctl, 97456f9a76cSErwan Le Ray .startup = stm32_usart_startup, 97556f9a76cSErwan Le Ray .shutdown = stm32_usart_shutdown, 97656f9a76cSErwan Le Ray .set_termios = stm32_usart_set_termios, 97756f9a76cSErwan Le Ray .pm = stm32_usart_pm, 97856f9a76cSErwan Le Ray .type = stm32_usart_type, 97956f9a76cSErwan Le Ray .release_port = stm32_usart_release_port, 98056f9a76cSErwan Le Ray .request_port = stm32_usart_request_port, 98156f9a76cSErwan Le Ray .config_port = stm32_usart_config_port, 98256f9a76cSErwan Le Ray .verify_port = stm32_usart_verify_port, 98348a6092fSMaxime Coquelin }; 98448a6092fSMaxime Coquelin 98597f3a085SErwan Le Ray static void stm32_usart_deinit_port(struct stm32_port *stm32port) 98697f3a085SErwan Le Ray { 98797f3a085SErwan Le Ray clk_disable_unprepare(stm32port->clk); 98897f3a085SErwan Le Ray } 98997f3a085SErwan Le Ray 99056f9a76cSErwan Le Ray static int stm32_usart_init_port(struct stm32_port *stm32port, 99148a6092fSMaxime Coquelin struct platform_device *pdev) 99248a6092fSMaxime Coquelin { 99348a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 99448a6092fSMaxime Coquelin struct resource *res; 995e0f2a902SErwan Le Ray int ret, irq; 99648a6092fSMaxime Coquelin 997e0f2a902SErwan Le Ray irq = platform_get_irq(pdev, 0); 998e0f2a902SErwan Le Ray if (irq <= 0) 999e0f2a902SErwan Le Ray return irq ? : -ENODEV; 100092fc0023SErwan Le Ray 100148a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 100248a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 100348a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 100448a6092fSMaxime Coquelin port->dev = &pdev->dev; 1005d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 10069feedaa7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE); 1007e0f2a902SErwan Le Ray port->irq = irq; 100856f9a76cSErwan Le Ray port->rs485_config = stm32_usart_config_rs485; 10097d8f6861SBich HEMON 101056f9a76cSErwan Le Ray ret = stm32_usart_init_rs485(port, pdev); 1011c150c0f3SLukas Wunner if (ret) 1012c150c0f3SLukas Wunner return ret; 10137d8f6861SBich HEMON 10142c58e560SErwan Le Ray if (stm32port->info->cfg.has_wakeup) { 1015fdf16d78SHolger Assmann stm32port->wakeirq = platform_get_irq_optional(pdev, 1); 10161df21786SStephen Boyd if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO) 10171df21786SStephen Boyd return stm32port->wakeirq ? : -ENODEV; 10182c58e560SErwan Le Ray } 10192c58e560SErwan Le Ray 1020351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 102148a6092fSMaxime Coquelin 102248a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 102348a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 102448a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 102548a6092fSMaxime Coquelin return PTR_ERR(port->membase); 102648a6092fSMaxime Coquelin port->mapbase = res->start; 102748a6092fSMaxime Coquelin 102848a6092fSMaxime Coquelin spin_lock_init(&port->lock); 102948a6092fSMaxime Coquelin 103048a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 103148a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 103248a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 103348a6092fSMaxime Coquelin 103448a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 103548a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 103648a6092fSMaxime Coquelin if (ret) 103748a6092fSMaxime Coquelin return ret; 103848a6092fSMaxime Coquelin 103948a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 1040ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 104148a6092fSMaxime Coquelin ret = -EINVAL; 10426cf61b9bSManivannan Sadhasivam goto err_clk; 1043ada80043SFabrice Gasnier } 104448a6092fSMaxime Coquelin 10456cf61b9bSManivannan Sadhasivam stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0); 10466cf61b9bSManivannan Sadhasivam if (IS_ERR(stm32port->gpios)) { 10476cf61b9bSManivannan Sadhasivam ret = PTR_ERR(stm32port->gpios); 10486cf61b9bSManivannan Sadhasivam goto err_clk; 10496cf61b9bSManivannan Sadhasivam } 10506cf61b9bSManivannan Sadhasivam 10519359369aSErwan Le Ray /* 10529359369aSErwan Le Ray * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts" 10539359369aSErwan Le Ray * properties should not be specified. 10549359369aSErwan Le Ray */ 10556cf61b9bSManivannan Sadhasivam if (stm32port->hw_flow_control) { 10566cf61b9bSManivannan Sadhasivam if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) || 10576cf61b9bSManivannan Sadhasivam mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) { 10586cf61b9bSManivannan Sadhasivam dev_err(&pdev->dev, "Conflicting RTS/CTS config\n"); 10596cf61b9bSManivannan Sadhasivam ret = -EINVAL; 10606cf61b9bSManivannan Sadhasivam goto err_clk; 10616cf61b9bSManivannan Sadhasivam } 10626cf61b9bSManivannan Sadhasivam } 10636cf61b9bSManivannan Sadhasivam 10646cf61b9bSManivannan Sadhasivam return ret; 10656cf61b9bSManivannan Sadhasivam 10666cf61b9bSManivannan Sadhasivam err_clk: 10676cf61b9bSManivannan Sadhasivam clk_disable_unprepare(stm32port->clk); 10686cf61b9bSManivannan Sadhasivam 106948a6092fSMaxime Coquelin return ret; 107048a6092fSMaxime Coquelin } 107148a6092fSMaxime Coquelin 107256f9a76cSErwan Le Ray static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev) 107348a6092fSMaxime Coquelin { 107448a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 107548a6092fSMaxime Coquelin int id; 107648a6092fSMaxime Coquelin 107748a6092fSMaxime Coquelin if (!np) 107848a6092fSMaxime Coquelin return NULL; 107948a6092fSMaxime Coquelin 108048a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 1081e5707915SGerald Baeza if (id < 0) { 1082e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 1083e5707915SGerald Baeza return NULL; 1084e5707915SGerald Baeza } 108548a6092fSMaxime Coquelin 108648a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 108748a6092fSMaxime Coquelin return NULL; 108848a6092fSMaxime Coquelin 10896fd9fffbSErwan Le Ray stm32_ports[id].hw_flow_control = 10906fd9fffbSErwan Le Ray of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ || 10916fd9fffbSErwan Le Ray of_property_read_bool (np, "uart-has-rtscts"); 109248a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 10934cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 1094d0a6a7bcSErwan Le Ray stm32_ports[id].cr3_irq = 0; 1095e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 109648a6092fSMaxime Coquelin return &stm32_ports[id]; 109748a6092fSMaxime Coquelin } 109848a6092fSMaxime Coquelin 109948a6092fSMaxime Coquelin #ifdef CONFIG_OF 110048a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 1101ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 1102ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 1103270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 110448a6092fSMaxime Coquelin {}, 110548a6092fSMaxime Coquelin }; 110648a6092fSMaxime Coquelin 110748a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 110848a6092fSMaxime Coquelin #endif 110948a6092fSMaxime Coquelin 111056f9a76cSErwan Le Ray static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port, 111134891872SAlexandre TORGUE struct platform_device *pdev) 111234891872SAlexandre TORGUE { 1113d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 111434891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 111534891872SAlexandre TORGUE struct device *dev = &pdev->dev; 111634891872SAlexandre TORGUE struct dma_slave_config config; 111734891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 111834891872SAlexandre TORGUE int ret; 111934891872SAlexandre TORGUE 112034891872SAlexandre TORGUE /* Request DMA RX channel */ 112134891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 112234891872SAlexandre TORGUE if (!stm32port->rx_ch) { 112334891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 112434891872SAlexandre TORGUE return -ENODEV; 112534891872SAlexandre TORGUE } 112634891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 112734891872SAlexandre TORGUE &stm32port->rx_dma_buf, 112834891872SAlexandre TORGUE GFP_KERNEL); 112934891872SAlexandre TORGUE if (!stm32port->rx_buf) { 113034891872SAlexandre TORGUE ret = -ENOMEM; 113134891872SAlexandre TORGUE goto alloc_err; 113234891872SAlexandre TORGUE } 113334891872SAlexandre TORGUE 113434891872SAlexandre TORGUE /* Configure DMA channel */ 113534891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 11368e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 113734891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 113834891872SAlexandre TORGUE 113934891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 114034891872SAlexandre TORGUE if (ret < 0) { 114134891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 114234891872SAlexandre TORGUE ret = -ENODEV; 114334891872SAlexandre TORGUE goto config_err; 114434891872SAlexandre TORGUE } 114534891872SAlexandre TORGUE 114634891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 114734891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 114834891872SAlexandre TORGUE stm32port->rx_dma_buf, 114934891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 115034891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 115134891872SAlexandre TORGUE if (!desc) { 115234891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 115334891872SAlexandre TORGUE ret = -ENODEV; 115434891872SAlexandre TORGUE goto config_err; 115534891872SAlexandre TORGUE } 115634891872SAlexandre TORGUE 115734891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 115834891872SAlexandre TORGUE desc->callback = NULL; 115934891872SAlexandre TORGUE desc->callback_param = NULL; 116034891872SAlexandre TORGUE 116134891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 1162e7997f7fSErwan Le Ray ret = dma_submit_error(dmaengine_submit(desc)); 1163e7997f7fSErwan Le Ray if (ret) { 1164e7997f7fSErwan Le Ray dmaengine_terminate_sync(stm32port->rx_ch); 1165e7997f7fSErwan Le Ray goto config_err; 1166e7997f7fSErwan Le Ray } 116734891872SAlexandre TORGUE 116834891872SAlexandre TORGUE /* Issue pending DMA requests */ 116934891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 117034891872SAlexandre TORGUE 117134891872SAlexandre TORGUE return 0; 117234891872SAlexandre TORGUE 117334891872SAlexandre TORGUE config_err: 117434891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 117534891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 117634891872SAlexandre TORGUE stm32port->rx_dma_buf); 117734891872SAlexandre TORGUE 117834891872SAlexandre TORGUE alloc_err: 117934891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 118034891872SAlexandre TORGUE stm32port->rx_ch = NULL; 118134891872SAlexandre TORGUE 118234891872SAlexandre TORGUE return ret; 118334891872SAlexandre TORGUE } 118434891872SAlexandre TORGUE 118556f9a76cSErwan Le Ray static int stm32_usart_of_dma_tx_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 int ret; 119334891872SAlexandre TORGUE 119434891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 119534891872SAlexandre TORGUE 119634891872SAlexandre TORGUE /* Request DMA TX channel */ 119734891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 119834891872SAlexandre TORGUE if (!stm32port->tx_ch) { 119934891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 120034891872SAlexandre TORGUE return -ENODEV; 120134891872SAlexandre TORGUE } 120234891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 120334891872SAlexandre TORGUE &stm32port->tx_dma_buf, 120434891872SAlexandre TORGUE GFP_KERNEL); 120534891872SAlexandre TORGUE if (!stm32port->tx_buf) { 120634891872SAlexandre TORGUE ret = -ENOMEM; 120734891872SAlexandre TORGUE goto alloc_err; 120834891872SAlexandre TORGUE } 120934891872SAlexandre TORGUE 121034891872SAlexandre TORGUE /* Configure DMA channel */ 121134891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 12128e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 121334891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 121434891872SAlexandre TORGUE 121534891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 121634891872SAlexandre TORGUE if (ret < 0) { 121734891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 121834891872SAlexandre TORGUE ret = -ENODEV; 121934891872SAlexandre TORGUE goto config_err; 122034891872SAlexandre TORGUE } 122134891872SAlexandre TORGUE 122234891872SAlexandre TORGUE return 0; 122334891872SAlexandre TORGUE 122434891872SAlexandre TORGUE config_err: 122534891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 122634891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 122734891872SAlexandre TORGUE stm32port->tx_dma_buf); 122834891872SAlexandre TORGUE 122934891872SAlexandre TORGUE alloc_err: 123034891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 123134891872SAlexandre TORGUE stm32port->tx_ch = NULL; 123234891872SAlexandre TORGUE 123334891872SAlexandre TORGUE return ret; 123434891872SAlexandre TORGUE } 123534891872SAlexandre TORGUE 123656f9a76cSErwan Le Ray static int stm32_usart_serial_probe(struct platform_device *pdev) 123748a6092fSMaxime Coquelin { 123848a6092fSMaxime Coquelin struct stm32_port *stm32port; 1239ada8618fSAlexandre TORGUE int ret; 124048a6092fSMaxime Coquelin 124156f9a76cSErwan Le Ray stm32port = stm32_usart_of_get_port(pdev); 124248a6092fSMaxime Coquelin if (!stm32port) 124348a6092fSMaxime Coquelin return -ENODEV; 124448a6092fSMaxime Coquelin 1245d825f0beSStephen Boyd stm32port->info = of_device_get_match_data(&pdev->dev); 1246d825f0beSStephen Boyd if (!stm32port->info) 1247ada8618fSAlexandre TORGUE return -EINVAL; 1248ada8618fSAlexandre TORGUE 124956f9a76cSErwan Le Ray ret = stm32_usart_init_port(stm32port, pdev); 125048a6092fSMaxime Coquelin if (ret) 125148a6092fSMaxime Coquelin return ret; 125248a6092fSMaxime Coquelin 12532c58e560SErwan Le Ray if (stm32port->wakeirq > 0) { 1254270e5a74SFabrice Gasnier ret = device_init_wakeup(&pdev->dev, true); 125548a6092fSMaxime Coquelin if (ret) 1256ada80043SFabrice Gasnier goto err_uninit; 12575297f274SErwan Le Ray 12585297f274SErwan Le Ray ret = dev_pm_set_dedicated_wake_irq(&pdev->dev, 12595297f274SErwan Le Ray stm32port->wakeirq); 12605297f274SErwan Le Ray if (ret) 12615297f274SErwan Le Ray goto err_nowup; 12625297f274SErwan Le Ray 12635297f274SErwan Le Ray device_set_wakeup_enable(&pdev->dev, false); 1264270e5a74SFabrice Gasnier } 1265270e5a74SFabrice Gasnier 126656f9a76cSErwan Le Ray ret = stm32_usart_of_dma_rx_probe(stm32port, pdev); 126734891872SAlexandre TORGUE if (ret) 126834891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 126934891872SAlexandre TORGUE 127056f9a76cSErwan Le Ray ret = stm32_usart_of_dma_tx_probe(stm32port, pdev); 127134891872SAlexandre TORGUE if (ret) 127234891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 127334891872SAlexandre TORGUE 127448a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 127548a6092fSMaxime Coquelin 1276fb6dcef6SErwan Le Ray pm_runtime_get_noresume(&pdev->dev); 1277fb6dcef6SErwan Le Ray pm_runtime_set_active(&pdev->dev); 1278fb6dcef6SErwan Le Ray pm_runtime_enable(&pdev->dev); 127987fd0741SErwan Le Ray 128087fd0741SErwan Le Ray ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 128187fd0741SErwan Le Ray if (ret) 128287fd0741SErwan Le Ray goto err_port; 128387fd0741SErwan Le Ray 1284fb6dcef6SErwan Le Ray pm_runtime_put_sync(&pdev->dev); 1285fb6dcef6SErwan Le Ray 128648a6092fSMaxime Coquelin return 0; 1287ada80043SFabrice Gasnier 128887fd0741SErwan Le Ray err_port: 128987fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 129087fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 129187fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 129287fd0741SErwan Le Ray 129387fd0741SErwan Le Ray if (stm32port->rx_ch) { 129487fd0741SErwan Le Ray dmaengine_terminate_async(stm32port->rx_ch); 129587fd0741SErwan Le Ray dma_release_channel(stm32port->rx_ch); 129687fd0741SErwan Le Ray } 129787fd0741SErwan Le Ray 129887fd0741SErwan Le Ray if (stm32port->rx_dma_buf) 129987fd0741SErwan Le Ray dma_free_coherent(&pdev->dev, 130087fd0741SErwan Le Ray RX_BUF_L, stm32port->rx_buf, 130187fd0741SErwan Le Ray stm32port->rx_dma_buf); 130287fd0741SErwan Le Ray 130387fd0741SErwan Le Ray if (stm32port->tx_ch) { 130487fd0741SErwan Le Ray dmaengine_terminate_async(stm32port->tx_ch); 130587fd0741SErwan Le Ray dma_release_channel(stm32port->tx_ch); 130687fd0741SErwan Le Ray } 130787fd0741SErwan Le Ray 130887fd0741SErwan Le Ray if (stm32port->tx_dma_buf) 130987fd0741SErwan Le Ray dma_free_coherent(&pdev->dev, 131087fd0741SErwan Le Ray TX_BUF_L, stm32port->tx_buf, 131187fd0741SErwan Le Ray stm32port->tx_dma_buf); 131287fd0741SErwan Le Ray 13132c58e560SErwan Le Ray if (stm32port->wakeirq > 0) 13145297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 13155297f274SErwan Le Ray 1316270e5a74SFabrice Gasnier err_nowup: 13172c58e560SErwan Le Ray if (stm32port->wakeirq > 0) 1318270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 1319270e5a74SFabrice Gasnier 1320ada80043SFabrice Gasnier err_uninit: 132197f3a085SErwan Le Ray stm32_usart_deinit_port(stm32port); 1322ada80043SFabrice Gasnier 1323ada80043SFabrice Gasnier return ret; 132448a6092fSMaxime Coquelin } 132548a6092fSMaxime Coquelin 132656f9a76cSErwan Le Ray static int stm32_usart_serial_remove(struct platform_device *pdev) 132748a6092fSMaxime Coquelin { 132848a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1329511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1330d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1331fb6dcef6SErwan Le Ray int err; 1332fb6dcef6SErwan Le Ray 1333fb6dcef6SErwan Le Ray pm_runtime_get_sync(&pdev->dev); 133487fd0741SErwan Le Ray err = uart_remove_one_port(&stm32_usart_driver, port); 133587fd0741SErwan Le Ray if (err) 133687fd0741SErwan Le Ray return(err); 133787fd0741SErwan Le Ray 133887fd0741SErwan Le Ray pm_runtime_disable(&pdev->dev); 133987fd0741SErwan Le Ray pm_runtime_set_suspended(&pdev->dev); 134087fd0741SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 134134891872SAlexandre TORGUE 134256f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 134334891872SAlexandre TORGUE 134487fd0741SErwan Le Ray if (stm32_port->rx_ch) { 134587fd0741SErwan Le Ray dmaengine_terminate_async(stm32_port->rx_ch); 134634891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 134787fd0741SErwan Le Ray } 134834891872SAlexandre TORGUE 134934891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 135034891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 135134891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 135234891872SAlexandre TORGUE stm32_port->rx_dma_buf); 135334891872SAlexandre TORGUE 135456f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 135534891872SAlexandre TORGUE 135687fd0741SErwan Le Ray if (stm32_port->tx_ch) { 135787fd0741SErwan Le Ray dmaengine_terminate_async(stm32_port->tx_ch); 135834891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 135987fd0741SErwan Le Ray } 136034891872SAlexandre TORGUE 136134891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 136234891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 136334891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 136434891872SAlexandre TORGUE stm32_port->tx_dma_buf); 1365511c7b1bSAlexandre TORGUE 13662c58e560SErwan Le Ray if (stm32_port->wakeirq > 0) { 13675297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1368270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 13695297f274SErwan Le Ray } 1370270e5a74SFabrice Gasnier 137197f3a085SErwan Le Ray stm32_usart_deinit_port(stm32_port); 137248a6092fSMaxime Coquelin 137387fd0741SErwan Le Ray return 0; 137448a6092fSMaxime Coquelin } 137548a6092fSMaxime Coquelin 137648a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 137756f9a76cSErwan Le Ray static void stm32_usart_console_putchar(struct uart_port *port, int ch) 137848a6092fSMaxime Coquelin { 1379ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1380d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1381ada8618fSAlexandre TORGUE 1382ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 138348a6092fSMaxime Coquelin cpu_relax(); 138448a6092fSMaxime Coquelin 1385ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 138648a6092fSMaxime Coquelin } 138748a6092fSMaxime Coquelin 138856f9a76cSErwan Le Ray static void stm32_usart_console_write(struct console *co, const char *s, 138992fc0023SErwan Le Ray unsigned int cnt) 139048a6092fSMaxime Coquelin { 139148a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1392ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1393d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1394d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 139548a6092fSMaxime Coquelin unsigned long flags; 139648a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 139748a6092fSMaxime Coquelin int locked = 1; 139848a6092fSMaxime Coquelin 139948a6092fSMaxime Coquelin local_irq_save(flags); 140048a6092fSMaxime Coquelin if (port->sysrq) 140148a6092fSMaxime Coquelin locked = 0; 140248a6092fSMaxime Coquelin else if (oops_in_progress) 140348a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 140448a6092fSMaxime Coquelin else 140548a6092fSMaxime Coquelin spin_lock(&port->lock); 140648a6092fSMaxime Coquelin 140787f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1408ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 140948a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 141087f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1411ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 141248a6092fSMaxime Coquelin 141356f9a76cSErwan Le Ray uart_console_write(port, s, cnt, stm32_usart_console_putchar); 141448a6092fSMaxime Coquelin 141548a6092fSMaxime Coquelin /* Restore interrupt state */ 1416ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 141748a6092fSMaxime Coquelin 141848a6092fSMaxime Coquelin if (locked) 141948a6092fSMaxime Coquelin spin_unlock(&port->lock); 142048a6092fSMaxime Coquelin local_irq_restore(flags); 142148a6092fSMaxime Coquelin } 142248a6092fSMaxime Coquelin 142356f9a76cSErwan Le Ray static int stm32_usart_console_setup(struct console *co, char *options) 142448a6092fSMaxime Coquelin { 142548a6092fSMaxime Coquelin struct stm32_port *stm32port; 142648a6092fSMaxime Coquelin int baud = 9600; 142748a6092fSMaxime Coquelin int bits = 8; 142848a6092fSMaxime Coquelin int parity = 'n'; 142948a6092fSMaxime Coquelin int flow = 'n'; 143048a6092fSMaxime Coquelin 143148a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 143248a6092fSMaxime Coquelin return -ENODEV; 143348a6092fSMaxime Coquelin 143448a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 143548a6092fSMaxime Coquelin 143648a6092fSMaxime Coquelin /* 143748a6092fSMaxime Coquelin * This driver does not support early console initialization 143848a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 143948a6092fSMaxime Coquelin * this to be called during the uart port registration when the 144048a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 144148a6092fSMaxime Coquelin */ 144292fc0023SErwan Le Ray if (stm32port->port.mapbase == 0 || !stm32port->port.membase) 144348a6092fSMaxime Coquelin return -ENXIO; 144448a6092fSMaxime Coquelin 144548a6092fSMaxime Coquelin if (options) 144648a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 144748a6092fSMaxime Coquelin 144848a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 144948a6092fSMaxime Coquelin } 145048a6092fSMaxime Coquelin 145148a6092fSMaxime Coquelin static struct console stm32_console = { 145248a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 145348a6092fSMaxime Coquelin .device = uart_console_device, 145456f9a76cSErwan Le Ray .write = stm32_usart_console_write, 145556f9a76cSErwan Le Ray .setup = stm32_usart_console_setup, 145648a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 145748a6092fSMaxime Coquelin .index = -1, 145848a6092fSMaxime Coquelin .data = &stm32_usart_driver, 145948a6092fSMaxime Coquelin }; 146048a6092fSMaxime Coquelin 146148a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 146248a6092fSMaxime Coquelin 146348a6092fSMaxime Coquelin #else 146448a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 146548a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 146648a6092fSMaxime Coquelin 146748a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 146848a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 146948a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 147048a6092fSMaxime Coquelin .major = 0, 147148a6092fSMaxime Coquelin .minor = 0, 147248a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 147348a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 147448a6092fSMaxime Coquelin }; 147548a6092fSMaxime Coquelin 147656f9a76cSErwan Le Ray static void __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port, 1477fe94347dSErwan Le Ray bool enable) 1478270e5a74SFabrice Gasnier { 1479270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1480d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1481d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1482270e5a74SFabrice Gasnier u32 val; 1483270e5a74SFabrice Gasnier 14842c58e560SErwan Le Ray if (stm32_port->wakeirq <= 0) 1485270e5a74SFabrice Gasnier return; 1486270e5a74SFabrice Gasnier 1487270e5a74SFabrice Gasnier if (enable) { 148856f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 148956f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM); 1490270e5a74SFabrice Gasnier val = readl_relaxed(port->membase + ofs->cr3); 1491270e5a74SFabrice Gasnier val &= ~USART_CR3_WUS_MASK; 1492270e5a74SFabrice Gasnier /* Enable Wake up interrupt from low power on start bit */ 1493270e5a74SFabrice Gasnier val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE; 1494270e5a74SFabrice Gasnier writel_relaxed(val, port->membase + ofs->cr3); 149556f9a76cSErwan Le Ray stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1496270e5a74SFabrice Gasnier } else { 149756f9a76cSErwan Le Ray stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM); 1498270e5a74SFabrice Gasnier } 1499270e5a74SFabrice Gasnier } 1500270e5a74SFabrice Gasnier 150156f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_suspend(struct device *dev) 1502270e5a74SFabrice Gasnier { 1503270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1504270e5a74SFabrice Gasnier 1505270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1506270e5a74SFabrice Gasnier 1507270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 150856f9a76cSErwan Le Ray stm32_usart_serial_en_wakeup(port, true); 1509270e5a74SFabrice Gasnier else 151056f9a76cSErwan Le Ray stm32_usart_serial_en_wakeup(port, false); 1511270e5a74SFabrice Gasnier 151255484fccSErwan Le Ray /* 151355484fccSErwan Le Ray * When "no_console_suspend" is enabled, keep the pinctrl default state 151455484fccSErwan Le Ray * and rely on bootloader stage to restore this state upon resume. 151555484fccSErwan Le Ray * Otherwise, apply the idle or sleep states depending on wakeup 151655484fccSErwan Le Ray * capabilities. 151755484fccSErwan Le Ray */ 151855484fccSErwan Le Ray if (console_suspend_enabled || !uart_console(port)) { 151955484fccSErwan Le Ray if (device_may_wakeup(dev)) 152055484fccSErwan Le Ray pinctrl_pm_select_idle_state(dev); 152155484fccSErwan Le Ray else 152294616d9aSErwan Le Ray pinctrl_pm_select_sleep_state(dev); 152355484fccSErwan Le Ray } 152494616d9aSErwan Le Ray 1525270e5a74SFabrice Gasnier return 0; 1526270e5a74SFabrice Gasnier } 1527270e5a74SFabrice Gasnier 152856f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_serial_resume(struct device *dev) 1529270e5a74SFabrice Gasnier { 1530270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1531270e5a74SFabrice Gasnier 153294616d9aSErwan Le Ray pinctrl_pm_select_default_state(dev); 153394616d9aSErwan Le Ray 1534270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 153556f9a76cSErwan Le Ray stm32_usart_serial_en_wakeup(port, false); 1536270e5a74SFabrice Gasnier 1537270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1538270e5a74SFabrice Gasnier } 1539270e5a74SFabrice Gasnier 154056f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev) 1541fb6dcef6SErwan Le Ray { 1542fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1543fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1544fb6dcef6SErwan Le Ray struct stm32_port, port); 1545fb6dcef6SErwan Le Ray 1546fb6dcef6SErwan Le Ray clk_disable_unprepare(stm32port->clk); 1547fb6dcef6SErwan Le Ray 1548fb6dcef6SErwan Le Ray return 0; 1549fb6dcef6SErwan Le Ray } 1550fb6dcef6SErwan Le Ray 155156f9a76cSErwan Le Ray static int __maybe_unused stm32_usart_runtime_resume(struct device *dev) 1552fb6dcef6SErwan Le Ray { 1553fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1554fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1555fb6dcef6SErwan Le Ray struct stm32_port, port); 1556fb6dcef6SErwan Le Ray 1557fb6dcef6SErwan Le Ray return clk_prepare_enable(stm32port->clk); 1558fb6dcef6SErwan Le Ray } 1559fb6dcef6SErwan Le Ray 1560270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 156156f9a76cSErwan Le Ray SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend, 156256f9a76cSErwan Le Ray stm32_usart_runtime_resume, NULL) 156356f9a76cSErwan Le Ray SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend, 156456f9a76cSErwan Le Ray stm32_usart_serial_resume) 1565270e5a74SFabrice Gasnier }; 1566270e5a74SFabrice Gasnier 156748a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 156856f9a76cSErwan Le Ray .probe = stm32_usart_serial_probe, 156956f9a76cSErwan Le Ray .remove = stm32_usart_serial_remove, 157048a6092fSMaxime Coquelin .driver = { 157148a6092fSMaxime Coquelin .name = DRIVER_NAME, 1572270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 157348a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 157448a6092fSMaxime Coquelin }, 157548a6092fSMaxime Coquelin }; 157648a6092fSMaxime Coquelin 157756f9a76cSErwan Le Ray static int __init stm32_usart_init(void) 157848a6092fSMaxime Coquelin { 157948a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 158048a6092fSMaxime Coquelin int ret; 158148a6092fSMaxime Coquelin 158248a6092fSMaxime Coquelin pr_info("%s\n", banner); 158348a6092fSMaxime Coquelin 158448a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 158548a6092fSMaxime Coquelin if (ret) 158648a6092fSMaxime Coquelin return ret; 158748a6092fSMaxime Coquelin 158848a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 158948a6092fSMaxime Coquelin if (ret) 159048a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 159148a6092fSMaxime Coquelin 159248a6092fSMaxime Coquelin return ret; 159348a6092fSMaxime Coquelin } 159448a6092fSMaxime Coquelin 159556f9a76cSErwan Le Ray static void __exit stm32_usart_exit(void) 159648a6092fSMaxime Coquelin { 159748a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 159848a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 159948a6092fSMaxime Coquelin } 160048a6092fSMaxime Coquelin 160156f9a76cSErwan Le Ray module_init(stm32_usart_init); 160256f9a76cSErwan Le Ray module_exit(stm32_usart_exit); 160348a6092fSMaxime Coquelin 160448a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 160548a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 160648a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1607