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> 6ada8618fSAlexandre TORGUE * Gerald Baeza <gerald.baeza@st.com> 748a6092fSMaxime Coquelin * 848a6092fSMaxime Coquelin * Inspired by st-asc.c from STMicroelectronics (c) 948a6092fSMaxime Coquelin */ 1048a6092fSMaxime Coquelin 116b596a83SMaxime Coquelin #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 1248a6092fSMaxime Coquelin #define SUPPORT_SYSRQ 1348a6092fSMaxime Coquelin #endif 1448a6092fSMaxime Coquelin 1534891872SAlexandre TORGUE #include <linux/clk.h> 1648a6092fSMaxime Coquelin #include <linux/console.h> 1748a6092fSMaxime Coquelin #include <linux/delay.h> 1834891872SAlexandre TORGUE #include <linux/dma-direction.h> 1934891872SAlexandre TORGUE #include <linux/dmaengine.h> 2034891872SAlexandre TORGUE #include <linux/dma-mapping.h> 2134891872SAlexandre TORGUE #include <linux/io.h> 2234891872SAlexandre TORGUE #include <linux/iopoll.h> 2334891872SAlexandre TORGUE #include <linux/irq.h> 2434891872SAlexandre TORGUE #include <linux/module.h> 2548a6092fSMaxime Coquelin #include <linux/of.h> 2648a6092fSMaxime Coquelin #include <linux/of_platform.h> 2734891872SAlexandre TORGUE #include <linux/platform_device.h> 2834891872SAlexandre TORGUE #include <linux/pm_runtime.h> 29270e5a74SFabrice Gasnier #include <linux/pm_wakeirq.h> 3048a6092fSMaxime Coquelin #include <linux/serial_core.h> 3134891872SAlexandre TORGUE #include <linux/serial.h> 3234891872SAlexandre TORGUE #include <linux/spinlock.h> 3334891872SAlexandre TORGUE #include <linux/sysrq.h> 3434891872SAlexandre TORGUE #include <linux/tty_flip.h> 3534891872SAlexandre TORGUE #include <linux/tty.h> 3648a6092fSMaxime Coquelin 37bc5a0b55SAlexandre TORGUE #include "stm32-usart.h" 3848a6092fSMaxime Coquelin 3948a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port); 4034891872SAlexandre TORGUE static void stm32_transmit_chars(struct uart_port *port); 4148a6092fSMaxime Coquelin 4248a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 4348a6092fSMaxime Coquelin { 4448a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 4548a6092fSMaxime Coquelin } 4648a6092fSMaxime Coquelin 4748a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits) 4848a6092fSMaxime Coquelin { 4948a6092fSMaxime Coquelin u32 val; 5048a6092fSMaxime Coquelin 5148a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5248a6092fSMaxime Coquelin val |= bits; 5348a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 5448a6092fSMaxime Coquelin } 5548a6092fSMaxime Coquelin 5648a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits) 5748a6092fSMaxime Coquelin { 5848a6092fSMaxime Coquelin u32 val; 5948a6092fSMaxime Coquelin 6048a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 6148a6092fSMaxime Coquelin val &= ~bits; 6248a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 6348a6092fSMaxime Coquelin } 6448a6092fSMaxime Coquelin 651bcda09dSBich HEMON static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, 661bcda09dSBich HEMON u32 delay_DDE, u32 baud) 671bcda09dSBich HEMON { 681bcda09dSBich HEMON u32 rs485_deat_dedt; 691bcda09dSBich HEMON u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); 701bcda09dSBich HEMON bool over8; 711bcda09dSBich HEMON 721bcda09dSBich HEMON *cr3 |= USART_CR3_DEM; 731bcda09dSBich HEMON over8 = *cr1 & USART_CR1_OVER8; 741bcda09dSBich HEMON 751bcda09dSBich HEMON if (over8) 761bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 8; 771bcda09dSBich HEMON else 781bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 16; 791bcda09dSBich HEMON 801bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 811bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 821bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 831bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & 841bcda09dSBich HEMON USART_CR1_DEAT_MASK; 851bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 861bcda09dSBich HEMON 871bcda09dSBich HEMON if (over8) 881bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 8; 891bcda09dSBich HEMON else 901bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 16; 911bcda09dSBich HEMON 921bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 931bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 941bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 951bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & 961bcda09dSBich HEMON USART_CR1_DEDT_MASK; 971bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 981bcda09dSBich HEMON } 991bcda09dSBich HEMON 1001bcda09dSBich HEMON static int stm32_config_rs485(struct uart_port *port, 1011bcda09dSBich HEMON struct serial_rs485 *rs485conf) 1021bcda09dSBich HEMON { 1031bcda09dSBich HEMON struct stm32_port *stm32_port = to_stm32_port(port); 1041bcda09dSBich HEMON struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1051bcda09dSBich HEMON struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1061bcda09dSBich HEMON u32 usartdiv, baud, cr1, cr3; 1071bcda09dSBich HEMON bool over8; 1081bcda09dSBich HEMON unsigned long flags; 1091bcda09dSBich HEMON 1101bcda09dSBich HEMON spin_lock_irqsave(&port->lock, flags); 1111bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1121bcda09dSBich HEMON 1131bcda09dSBich HEMON port->rs485 = *rs485conf; 1141bcda09dSBich HEMON 1151bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RX_DURING_TX; 1161bcda09dSBich HEMON 1171bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 1181bcda09dSBich HEMON cr1 = readl_relaxed(port->membase + ofs->cr1); 1191bcda09dSBich HEMON cr3 = readl_relaxed(port->membase + ofs->cr3); 1201bcda09dSBich HEMON usartdiv = readl_relaxed(port->membase + ofs->brr); 1211bcda09dSBich HEMON usartdiv = usartdiv & GENMASK(15, 0); 1221bcda09dSBich HEMON over8 = cr1 & USART_CR1_OVER8; 1231bcda09dSBich HEMON 1241bcda09dSBich HEMON if (over8) 1251bcda09dSBich HEMON usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) 1261bcda09dSBich HEMON << USART_BRR_04_R_SHIFT; 1271bcda09dSBich HEMON 1281bcda09dSBich HEMON baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); 1291bcda09dSBich HEMON stm32_config_reg_rs485(&cr1, &cr3, 1301bcda09dSBich HEMON rs485conf->delay_rts_before_send, 1311bcda09dSBich HEMON rs485conf->delay_rts_after_send, baud); 1321bcda09dSBich HEMON 1331bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 1341bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 1351bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 1361bcda09dSBich HEMON } else { 1371bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 1381bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 1391bcda09dSBich HEMON } 1401bcda09dSBich HEMON 1411bcda09dSBich HEMON writel_relaxed(cr3, port->membase + ofs->cr3); 1421bcda09dSBich HEMON writel_relaxed(cr1, port->membase + ofs->cr1); 1431bcda09dSBich HEMON } else { 1441bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP); 1451bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr1, 1461bcda09dSBich HEMON USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 1471bcda09dSBich HEMON } 1481bcda09dSBich HEMON 1491bcda09dSBich HEMON stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1501bcda09dSBich HEMON spin_unlock_irqrestore(&port->lock, flags); 1511bcda09dSBich HEMON 1521bcda09dSBich HEMON return 0; 1531bcda09dSBich HEMON } 1541bcda09dSBich HEMON 1551bcda09dSBich HEMON static int stm32_init_rs485(struct uart_port *port, 1561bcda09dSBich HEMON struct platform_device *pdev) 1571bcda09dSBich HEMON { 1581bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 1591bcda09dSBich HEMON 1601bcda09dSBich HEMON rs485conf->flags = 0; 1611bcda09dSBich HEMON rs485conf->delay_rts_before_send = 0; 1621bcda09dSBich HEMON rs485conf->delay_rts_after_send = 0; 1631bcda09dSBich HEMON 1641bcda09dSBich HEMON if (!pdev->dev.of_node) 1651bcda09dSBich HEMON return -ENODEV; 1661bcda09dSBich HEMON 1671bcda09dSBich HEMON uart_get_rs485_mode(&pdev->dev, rs485conf); 1681bcda09dSBich HEMON 1691bcda09dSBich HEMON return 0; 1701bcda09dSBich HEMON } 1711bcda09dSBich HEMON 172b97055bcSBaoyou Xie static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res, 17334891872SAlexandre TORGUE bool threaded) 17434891872SAlexandre TORGUE { 17534891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 17634891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 17734891872SAlexandre TORGUE enum dma_status status; 17834891872SAlexandre TORGUE struct dma_tx_state state; 17934891872SAlexandre TORGUE 18034891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 18134891872SAlexandre TORGUE 18234891872SAlexandre TORGUE if (threaded && stm32_port->rx_ch) { 18334891872SAlexandre TORGUE status = dmaengine_tx_status(stm32_port->rx_ch, 18434891872SAlexandre TORGUE stm32_port->rx_ch->cookie, 18534891872SAlexandre TORGUE &state); 18634891872SAlexandre TORGUE if ((status == DMA_IN_PROGRESS) && 18734891872SAlexandre TORGUE (*last_res != state.residue)) 18834891872SAlexandre TORGUE return 1; 18934891872SAlexandre TORGUE else 19034891872SAlexandre TORGUE return 0; 19134891872SAlexandre TORGUE } else if (*sr & USART_SR_RXNE) { 19234891872SAlexandre TORGUE return 1; 19334891872SAlexandre TORGUE } 19434891872SAlexandre TORGUE return 0; 19534891872SAlexandre TORGUE } 19634891872SAlexandre TORGUE 197b97055bcSBaoyou Xie static unsigned long 198b97055bcSBaoyou Xie stm32_get_char(struct uart_port *port, u32 *sr, int *last_res) 19934891872SAlexandre TORGUE { 20034891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 20134891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 20234891872SAlexandre TORGUE unsigned long c; 20334891872SAlexandre TORGUE 20434891872SAlexandre TORGUE if (stm32_port->rx_ch) { 20534891872SAlexandre TORGUE c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; 20634891872SAlexandre TORGUE if ((*last_res) == 0) 20734891872SAlexandre TORGUE *last_res = RX_BUF_L; 20834891872SAlexandre TORGUE return c; 20934891872SAlexandre TORGUE } else { 21034891872SAlexandre TORGUE return readl_relaxed(port->membase + ofs->rdr); 21134891872SAlexandre TORGUE } 21234891872SAlexandre TORGUE } 21334891872SAlexandre TORGUE 21434891872SAlexandre TORGUE static void stm32_receive_chars(struct uart_port *port, bool threaded) 21548a6092fSMaxime Coquelin { 21648a6092fSMaxime Coquelin struct tty_port *tport = &port->state->port; 217ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 218ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 21948a6092fSMaxime Coquelin unsigned long c; 22048a6092fSMaxime Coquelin u32 sr; 22148a6092fSMaxime Coquelin char flag; 22248a6092fSMaxime Coquelin 22329d60981SAndy Shevchenko if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 22448a6092fSMaxime Coquelin pm_wakeup_event(tport->tty->dev, 0); 22548a6092fSMaxime Coquelin 226e5707915SGerald Baeza while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) { 22748a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22848a6092fSMaxime Coquelin flag = TTY_NORMAL; 22948a6092fSMaxime Coquelin 230*4f01d833SErwan Le Ray /* 231*4f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 232*4f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 233*4f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 234*4f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 235*4f01d833SErwan Le Ray * and clear status bits of the next rx data. 236*4f01d833SErwan Le Ray * 237*4f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 238*4f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 239*4f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 240*4f01d833SErwan Le Ray */ 241*4f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 242*4f01d833SErwan Le Ray stm32_clr_bits(port, ofs->icr, USART_ICR_ORECF | 243*4f01d833SErwan Le Ray USART_ICR_PECF | USART_ICR_FECF); 244*4f01d833SErwan Le Ray 245*4f01d833SErwan Le Ray c = stm32_get_char(port, &sr, &stm32_port->last_res); 246*4f01d833SErwan Le Ray port->icount.rx++; 24748a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 248*4f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 24948a6092fSMaxime Coquelin port->icount.overrun++; 25048a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 25148a6092fSMaxime Coquelin port->icount.parity++; 25248a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 253*4f01d833SErwan Le Ray /* Break detection if character is null */ 254*4f01d833SErwan Le Ray if (!c) { 255*4f01d833SErwan Le Ray port->icount.brk++; 256*4f01d833SErwan Le Ray if (uart_handle_break(port)) 257*4f01d833SErwan Le Ray continue; 258*4f01d833SErwan Le Ray } else { 25948a6092fSMaxime Coquelin port->icount.frame++; 26048a6092fSMaxime Coquelin } 261*4f01d833SErwan Le Ray } 26248a6092fSMaxime Coquelin 26348a6092fSMaxime Coquelin sr &= port->read_status_mask; 26448a6092fSMaxime Coquelin 265*4f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26648a6092fSMaxime Coquelin flag = TTY_PARITY; 267*4f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 268*4f01d833SErwan Le Ray if (!c) 269*4f01d833SErwan Le Ray flag = TTY_BREAK; 270*4f01d833SErwan Le Ray else 27148a6092fSMaxime Coquelin flag = TTY_FRAME; 27248a6092fSMaxime Coquelin } 273*4f01d833SErwan Le Ray } 27448a6092fSMaxime Coquelin 27548a6092fSMaxime Coquelin if (uart_handle_sysrq_char(port, c)) 27648a6092fSMaxime Coquelin continue; 27748a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27848a6092fSMaxime Coquelin } 27948a6092fSMaxime Coquelin 28048a6092fSMaxime Coquelin spin_unlock(&port->lock); 28148a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 28248a6092fSMaxime Coquelin spin_lock(&port->lock); 28348a6092fSMaxime Coquelin } 28448a6092fSMaxime Coquelin 28534891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg) 28634891872SAlexandre TORGUE { 28734891872SAlexandre TORGUE struct uart_port *port = arg; 28834891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 28934891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 29034891872SAlexandre TORGUE unsigned int isr; 29134891872SAlexandre TORGUE int ret; 29234891872SAlexandre TORGUE 29334891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 29434891872SAlexandre TORGUE isr, 29534891872SAlexandre TORGUE (isr & USART_SR_TC), 29634891872SAlexandre TORGUE 10, 100000); 29734891872SAlexandre TORGUE 29834891872SAlexandre TORGUE if (ret) 29934891872SAlexandre TORGUE dev_err(port->dev, "terminal count not set\n"); 30034891872SAlexandre TORGUE 30134891872SAlexandre TORGUE if (ofs->icr == UNDEF_REG) 30234891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 30334891872SAlexandre TORGUE else 30434891872SAlexandre TORGUE stm32_set_bits(port, ofs->icr, USART_CR_TC); 30534891872SAlexandre TORGUE 30634891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 30734891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 30834891872SAlexandre TORGUE 30934891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 31034891872SAlexandre TORGUE stm32_transmit_chars(port); 31134891872SAlexandre TORGUE } 31234891872SAlexandre TORGUE 31334891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port) 31434891872SAlexandre TORGUE { 31534891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 31634891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 31734891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 31834891872SAlexandre TORGUE unsigned int isr; 31934891872SAlexandre TORGUE int ret; 32034891872SAlexandre TORGUE 32134891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 32234891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 32334891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 32434891872SAlexandre TORGUE } 32534891872SAlexandre TORGUE 32634891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 32734891872SAlexandre TORGUE isr, 32834891872SAlexandre TORGUE (isr & USART_SR_TXE), 329a61d9e6eSGerald Baeza 10, 100000); 33034891872SAlexandre TORGUE 33134891872SAlexandre TORGUE if (ret) 33234891872SAlexandre TORGUE dev_err(port->dev, "tx empty not set\n"); 33334891872SAlexandre TORGUE 33434891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 33534891872SAlexandre TORGUE 33634891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 33734891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 33834891872SAlexandre TORGUE port->icount.tx++; 33934891872SAlexandre TORGUE } 34034891872SAlexandre TORGUE 34134891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port) 34234891872SAlexandre TORGUE { 34334891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 34434891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 34534891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 34634891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 34734891872SAlexandre TORGUE dma_cookie_t cookie; 34834891872SAlexandre TORGUE unsigned int count, i; 34934891872SAlexandre TORGUE 35034891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 35134891872SAlexandre TORGUE return; 35234891872SAlexandre TORGUE 35334891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 35434891872SAlexandre TORGUE 35534891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 35634891872SAlexandre TORGUE 35734891872SAlexandre TORGUE if (count > TX_BUF_L) 35834891872SAlexandre TORGUE count = TX_BUF_L; 35934891872SAlexandre TORGUE 36034891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 36134891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 36234891872SAlexandre TORGUE } else { 36334891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 36434891872SAlexandre TORGUE size_t two; 36534891872SAlexandre TORGUE 36634891872SAlexandre TORGUE if (one > count) 36734891872SAlexandre TORGUE one = count; 36834891872SAlexandre TORGUE two = count - one; 36934891872SAlexandre TORGUE 37034891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 37134891872SAlexandre TORGUE if (two) 37234891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 37334891872SAlexandre TORGUE } 37434891872SAlexandre TORGUE 37534891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 37634891872SAlexandre TORGUE stm32port->tx_dma_buf, 37734891872SAlexandre TORGUE count, 37834891872SAlexandre TORGUE DMA_MEM_TO_DEV, 37934891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 38034891872SAlexandre TORGUE 38134891872SAlexandre TORGUE if (!desc) { 38234891872SAlexandre TORGUE for (i = count; i > 0; i--) 38334891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 38434891872SAlexandre TORGUE return; 38534891872SAlexandre TORGUE } 38634891872SAlexandre TORGUE 38734891872SAlexandre TORGUE desc->callback = stm32_tx_dma_complete; 38834891872SAlexandre TORGUE desc->callback_param = port; 38934891872SAlexandre TORGUE 39034891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 39134891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 39234891872SAlexandre TORGUE 39334891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 39434891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 39534891872SAlexandre TORGUE 39634891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 39734891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 39834891872SAlexandre TORGUE 39934891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 40034891872SAlexandre TORGUE port->icount.tx += count; 40134891872SAlexandre TORGUE } 40234891872SAlexandre TORGUE 40348a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port) 40448a6092fSMaxime Coquelin { 405ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 406ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 40748a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 40848a6092fSMaxime Coquelin 40948a6092fSMaxime Coquelin if (port->x_char) { 41034891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 41134891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 412ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 41348a6092fSMaxime Coquelin port->x_char = 0; 41448a6092fSMaxime Coquelin port->icount.tx++; 41534891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 41634891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 41748a6092fSMaxime Coquelin return; 41848a6092fSMaxime Coquelin } 41948a6092fSMaxime Coquelin 42048a6092fSMaxime Coquelin if (uart_tx_stopped(port)) { 42148a6092fSMaxime Coquelin stm32_stop_tx(port); 42248a6092fSMaxime Coquelin return; 42348a6092fSMaxime Coquelin } 42448a6092fSMaxime Coquelin 42548a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) { 42648a6092fSMaxime Coquelin stm32_stop_tx(port); 42748a6092fSMaxime Coquelin return; 42848a6092fSMaxime Coquelin } 42948a6092fSMaxime Coquelin 43034891872SAlexandre TORGUE if (stm32_port->tx_ch) 43134891872SAlexandre TORGUE stm32_transmit_chars_dma(port); 43234891872SAlexandre TORGUE else 43334891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 43448a6092fSMaxime Coquelin 43548a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 43648a6092fSMaxime Coquelin uart_write_wakeup(port); 43748a6092fSMaxime Coquelin 43848a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 43948a6092fSMaxime Coquelin stm32_stop_tx(port); 44048a6092fSMaxime Coquelin } 44148a6092fSMaxime Coquelin 44248a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr) 44348a6092fSMaxime Coquelin { 44448a6092fSMaxime Coquelin struct uart_port *port = ptr; 445ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 446ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 44748a6092fSMaxime Coquelin u32 sr; 44848a6092fSMaxime Coquelin 44901d32d71SAlexandre TORGUE spin_lock(&port->lock); 45001d32d71SAlexandre TORGUE 451ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 45248a6092fSMaxime Coquelin 453270e5a74SFabrice Gasnier if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG)) 454270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 455270e5a74SFabrice Gasnier port->membase + ofs->icr); 456270e5a74SFabrice Gasnier 45734891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 45834891872SAlexandre TORGUE stm32_receive_chars(port, false); 45948a6092fSMaxime Coquelin 46034891872SAlexandre TORGUE if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) 46148a6092fSMaxime Coquelin stm32_transmit_chars(port); 46248a6092fSMaxime Coquelin 46301d32d71SAlexandre TORGUE spin_unlock(&port->lock); 46401d32d71SAlexandre TORGUE 46534891872SAlexandre TORGUE if (stm32_port->rx_ch) 46634891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 46734891872SAlexandre TORGUE else 46834891872SAlexandre TORGUE return IRQ_HANDLED; 46934891872SAlexandre TORGUE } 47034891872SAlexandre TORGUE 47134891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr) 47234891872SAlexandre TORGUE { 47334891872SAlexandre TORGUE struct uart_port *port = ptr; 47434891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 47534891872SAlexandre TORGUE 47634891872SAlexandre TORGUE spin_lock(&port->lock); 47734891872SAlexandre TORGUE 47834891872SAlexandre TORGUE if (stm32_port->rx_ch) 47934891872SAlexandre TORGUE stm32_receive_chars(port, true); 48034891872SAlexandre TORGUE 48148a6092fSMaxime Coquelin spin_unlock(&port->lock); 48248a6092fSMaxime Coquelin 48348a6092fSMaxime Coquelin return IRQ_HANDLED; 48448a6092fSMaxime Coquelin } 48548a6092fSMaxime Coquelin 48648a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port) 48748a6092fSMaxime Coquelin { 488ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 489ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 490ada8618fSAlexandre TORGUE 491ada8618fSAlexandre TORGUE return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; 49248a6092fSMaxime Coquelin } 49348a6092fSMaxime Coquelin 49448a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) 49548a6092fSMaxime Coquelin { 496ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 497ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 498ada8618fSAlexandre TORGUE 49948a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 500ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); 50148a6092fSMaxime Coquelin else 502ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 50348a6092fSMaxime Coquelin } 50448a6092fSMaxime Coquelin 50548a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port) 50648a6092fSMaxime Coquelin { 50748a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 50848a6092fSMaxime Coquelin return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 50948a6092fSMaxime Coquelin } 51048a6092fSMaxime Coquelin 51148a6092fSMaxime Coquelin /* Transmit stop */ 51248a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port) 51348a6092fSMaxime Coquelin { 514ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 515ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 516ada8618fSAlexandre TORGUE 517ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 51848a6092fSMaxime Coquelin } 51948a6092fSMaxime Coquelin 52048a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 52148a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port) 52248a6092fSMaxime Coquelin { 52348a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 52448a6092fSMaxime Coquelin 52548a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 52648a6092fSMaxime Coquelin return; 52748a6092fSMaxime Coquelin 52834891872SAlexandre TORGUE stm32_transmit_chars(port); 52948a6092fSMaxime Coquelin } 53048a6092fSMaxime Coquelin 53148a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 53248a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port) 53348a6092fSMaxime Coquelin { 534ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 535ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 53648a6092fSMaxime Coquelin unsigned long flags; 53748a6092fSMaxime Coquelin 53848a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 539ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 54048a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 54148a6092fSMaxime Coquelin } 54248a6092fSMaxime Coquelin 54348a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 54448a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port) 54548a6092fSMaxime Coquelin { 546ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 547ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 54848a6092fSMaxime Coquelin unsigned long flags; 54948a6092fSMaxime Coquelin 55048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 551ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE); 55248a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 55348a6092fSMaxime Coquelin } 55448a6092fSMaxime Coquelin 55548a6092fSMaxime Coquelin /* Receive stop */ 55648a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port) 55748a6092fSMaxime Coquelin { 558ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 559ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 560ada8618fSAlexandre TORGUE 561ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 56248a6092fSMaxime Coquelin } 56348a6092fSMaxime Coquelin 56448a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 56548a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state) 56648a6092fSMaxime Coquelin { 56748a6092fSMaxime Coquelin } 56848a6092fSMaxime Coquelin 56948a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port) 57048a6092fSMaxime Coquelin { 571ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 572ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 573270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 57448a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 57548a6092fSMaxime Coquelin u32 val; 57648a6092fSMaxime Coquelin int ret; 57748a6092fSMaxime Coquelin 57834891872SAlexandre TORGUE ret = request_threaded_irq(port->irq, stm32_interrupt, 57934891872SAlexandre TORGUE stm32_threaded_interrupt, 58034891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 58148a6092fSMaxime Coquelin if (ret) 58248a6092fSMaxime Coquelin return ret; 58348a6092fSMaxime Coquelin 584270e5a74SFabrice Gasnier if (cfg->has_wakeup && stm32_port->wakeirq >= 0) { 585270e5a74SFabrice Gasnier ret = dev_pm_set_dedicated_wake_irq(port->dev, 586270e5a74SFabrice Gasnier stm32_port->wakeirq); 587270e5a74SFabrice Gasnier if (ret) { 588270e5a74SFabrice Gasnier free_irq(port->irq, port); 589270e5a74SFabrice Gasnier return ret; 590270e5a74SFabrice Gasnier } 591270e5a74SFabrice Gasnier } 592270e5a74SFabrice Gasnier 59348a6092fSMaxime Coquelin val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 594351a762aSGerald Baeza if (stm32_port->fifoen) 595351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 596ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, val); 59748a6092fSMaxime Coquelin 59848a6092fSMaxime Coquelin return 0; 59948a6092fSMaxime Coquelin } 60048a6092fSMaxime Coquelin 60148a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port) 60248a6092fSMaxime Coquelin { 603ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 604ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 60587f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 60648a6092fSMaxime Coquelin u32 val; 60748a6092fSMaxime Coquelin 60848a6092fSMaxime Coquelin val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 60987f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 610351a762aSGerald Baeza if (stm32_port->fifoen) 611351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 612a14f66a4SAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, val); 61348a6092fSMaxime Coquelin 614270e5a74SFabrice Gasnier dev_pm_clear_wake_irq(port->dev); 61548a6092fSMaxime Coquelin free_irq(port->irq, port); 61648a6092fSMaxime Coquelin } 61748a6092fSMaxime Coquelin 618c8a9d043SErwan Le Ray unsigned int stm32_get_databits(struct ktermios *termios) 619c8a9d043SErwan Le Ray { 620c8a9d043SErwan Le Ray unsigned int bits; 621c8a9d043SErwan Le Ray 622c8a9d043SErwan Le Ray tcflag_t cflag = termios->c_cflag; 623c8a9d043SErwan Le Ray 624c8a9d043SErwan Le Ray switch (cflag & CSIZE) { 625c8a9d043SErwan Le Ray /* 626c8a9d043SErwan Le Ray * CSIZE settings are not necessarily supported in hardware. 627c8a9d043SErwan Le Ray * CSIZE unsupported configurations are handled here to set word length 628c8a9d043SErwan Le Ray * to 8 bits word as default configuration and to print debug message. 629c8a9d043SErwan Le Ray */ 630c8a9d043SErwan Le Ray case CS5: 631c8a9d043SErwan Le Ray bits = 5; 632c8a9d043SErwan Le Ray break; 633c8a9d043SErwan Le Ray case CS6: 634c8a9d043SErwan Le Ray bits = 6; 635c8a9d043SErwan Le Ray break; 636c8a9d043SErwan Le Ray case CS7: 637c8a9d043SErwan Le Ray bits = 7; 638c8a9d043SErwan Le Ray break; 639c8a9d043SErwan Le Ray /* default including CS8 */ 640c8a9d043SErwan Le Ray default: 641c8a9d043SErwan Le Ray bits = 8; 642c8a9d043SErwan Le Ray break; 643c8a9d043SErwan Le Ray } 644c8a9d043SErwan Le Ray 645c8a9d043SErwan Le Ray return bits; 646c8a9d043SErwan Le Ray } 647c8a9d043SErwan Le Ray 64848a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, 64948a6092fSMaxime Coquelin struct ktermios *old) 65048a6092fSMaxime Coquelin { 65148a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 652ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 653ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 6541bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 655c8a9d043SErwan Le Ray unsigned int baud, bits; 65648a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 65748a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 65848a6092fSMaxime Coquelin u32 cr1, cr2, cr3; 65948a6092fSMaxime Coquelin unsigned long flags; 66048a6092fSMaxime Coquelin 66148a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 66248a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 66348a6092fSMaxime Coquelin 66448a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 66548a6092fSMaxime Coquelin 66648a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 66748a6092fSMaxime Coquelin 66848a6092fSMaxime Coquelin /* Stop serial port and reset value */ 669ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 67048a6092fSMaxime Coquelin 671ada8618fSAlexandre TORGUE cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; 6721bcda09dSBich HEMON 673351a762aSGerald Baeza if (stm32_port->fifoen) 674351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 67548a6092fSMaxime Coquelin cr2 = 0; 67648a6092fSMaxime Coquelin cr3 = 0; 67748a6092fSMaxime Coquelin 67848a6092fSMaxime Coquelin if (cflag & CSTOPB) 67948a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 68048a6092fSMaxime Coquelin 681c8a9d043SErwan Le Ray bits = stm32_get_databits(termios); 682c8a9d043SErwan Le Ray 68348a6092fSMaxime Coquelin if (cflag & PARENB) { 684c8a9d043SErwan Le Ray bits++; 68548a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 686c8a9d043SErwan Le Ray } 687c8a9d043SErwan Le Ray 688c8a9d043SErwan Le Ray /* 689c8a9d043SErwan Le Ray * Word length configuration: 690c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 691c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 692c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 693c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 694c8a9d043SErwan Le Ray */ 695c8a9d043SErwan Le Ray if (bits == 9) 696ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 697c8a9d043SErwan Le Ray else if ((bits == 7) && cfg->has_7bits_data) 698c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 699c8a9d043SErwan Le Ray else if (bits != 8) 700c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 701c8a9d043SErwan Le Ray , bits); 70248a6092fSMaxime Coquelin 70348a6092fSMaxime Coquelin if (cflag & PARODD) 70448a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 70548a6092fSMaxime Coquelin 70648a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 70748a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 70848a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 70935abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 71048a6092fSMaxime Coquelin } 71148a6092fSMaxime Coquelin 71248a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 71348a6092fSMaxime Coquelin 71448a6092fSMaxime Coquelin /* 71548a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 71648a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 71748a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 71848a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 71948a6092fSMaxime Coquelin */ 72048a6092fSMaxime Coquelin if (usartdiv < 16) { 72148a6092fSMaxime Coquelin oversampling = 8; 7221bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 723ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); 72448a6092fSMaxime Coquelin } else { 72548a6092fSMaxime Coquelin oversampling = 16; 7261bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 727ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 72848a6092fSMaxime Coquelin } 72948a6092fSMaxime Coquelin 73048a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 73148a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 732ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 73348a6092fSMaxime Coquelin 73448a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 73548a6092fSMaxime Coquelin 73648a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 73748a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 73848a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 73948a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 740*4f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 74148a6092fSMaxime Coquelin 74248a6092fSMaxime Coquelin /* Characters to ignore */ 74348a6092fSMaxime Coquelin port->ignore_status_mask = 0; 74448a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 74548a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 74648a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 747*4f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 74848a6092fSMaxime Coquelin /* 74948a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 75048a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 75148a6092fSMaxime Coquelin */ 75248a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 75348a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 75448a6092fSMaxime Coquelin } 75548a6092fSMaxime Coquelin 75648a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 75748a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 75848a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 75948a6092fSMaxime Coquelin 76034891872SAlexandre TORGUE if (stm32_port->rx_ch) 76134891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 76234891872SAlexandre TORGUE 7631bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 7641bcda09dSBich HEMON stm32_config_reg_rs485(&cr1, &cr3, 7651bcda09dSBich HEMON rs485conf->delay_rts_before_send, 7661bcda09dSBich HEMON rs485conf->delay_rts_after_send, baud); 7671bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 7681bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 7691bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 7701bcda09dSBich HEMON } else { 7711bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 7721bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 7731bcda09dSBich HEMON } 7741bcda09dSBich HEMON 7751bcda09dSBich HEMON } else { 7761bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 7771bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 7781bcda09dSBich HEMON } 7791bcda09dSBich HEMON 780ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 781ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 782ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 78348a6092fSMaxime Coquelin 7841bcda09dSBich HEMON stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 78548a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 78648a6092fSMaxime Coquelin } 78748a6092fSMaxime Coquelin 78848a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port) 78948a6092fSMaxime Coquelin { 79048a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 79148a6092fSMaxime Coquelin } 79248a6092fSMaxime Coquelin 79348a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port) 79448a6092fSMaxime Coquelin { 79548a6092fSMaxime Coquelin } 79648a6092fSMaxime Coquelin 79748a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port) 79848a6092fSMaxime Coquelin { 79948a6092fSMaxime Coquelin return 0; 80048a6092fSMaxime Coquelin } 80148a6092fSMaxime Coquelin 80248a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags) 80348a6092fSMaxime Coquelin { 80448a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 80548a6092fSMaxime Coquelin port->type = PORT_STM32; 80648a6092fSMaxime Coquelin } 80748a6092fSMaxime Coquelin 80848a6092fSMaxime Coquelin static int 80948a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser) 81048a6092fSMaxime Coquelin { 81148a6092fSMaxime Coquelin /* No user changeable parameters */ 81248a6092fSMaxime Coquelin return -EINVAL; 81348a6092fSMaxime Coquelin } 81448a6092fSMaxime Coquelin 81548a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state, 81648a6092fSMaxime Coquelin unsigned int oldstate) 81748a6092fSMaxime Coquelin { 81848a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 81948a6092fSMaxime Coquelin struct stm32_port, port); 820ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 821ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32port->info->cfg; 82248a6092fSMaxime Coquelin unsigned long flags = 0; 82348a6092fSMaxime Coquelin 82448a6092fSMaxime Coquelin switch (state) { 82548a6092fSMaxime Coquelin case UART_PM_STATE_ON: 82648a6092fSMaxime Coquelin clk_prepare_enable(stm32port->clk); 82748a6092fSMaxime Coquelin break; 82848a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 82948a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 830ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 83148a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 83248a6092fSMaxime Coquelin clk_disable_unprepare(stm32port->clk); 83348a6092fSMaxime Coquelin break; 83448a6092fSMaxime Coquelin } 83548a6092fSMaxime Coquelin } 83648a6092fSMaxime Coquelin 83748a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 83848a6092fSMaxime Coquelin .tx_empty = stm32_tx_empty, 83948a6092fSMaxime Coquelin .set_mctrl = stm32_set_mctrl, 84048a6092fSMaxime Coquelin .get_mctrl = stm32_get_mctrl, 84148a6092fSMaxime Coquelin .stop_tx = stm32_stop_tx, 84248a6092fSMaxime Coquelin .start_tx = stm32_start_tx, 84348a6092fSMaxime Coquelin .throttle = stm32_throttle, 84448a6092fSMaxime Coquelin .unthrottle = stm32_unthrottle, 84548a6092fSMaxime Coquelin .stop_rx = stm32_stop_rx, 84648a6092fSMaxime Coquelin .break_ctl = stm32_break_ctl, 84748a6092fSMaxime Coquelin .startup = stm32_startup, 84848a6092fSMaxime Coquelin .shutdown = stm32_shutdown, 84948a6092fSMaxime Coquelin .set_termios = stm32_set_termios, 85048a6092fSMaxime Coquelin .pm = stm32_pm, 85148a6092fSMaxime Coquelin .type = stm32_type, 85248a6092fSMaxime Coquelin .release_port = stm32_release_port, 85348a6092fSMaxime Coquelin .request_port = stm32_request_port, 85448a6092fSMaxime Coquelin .config_port = stm32_config_port, 85548a6092fSMaxime Coquelin .verify_port = stm32_verify_port, 85648a6092fSMaxime Coquelin }; 85748a6092fSMaxime Coquelin 85848a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port, 85948a6092fSMaxime Coquelin struct platform_device *pdev) 86048a6092fSMaxime Coquelin { 86148a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 86248a6092fSMaxime Coquelin struct resource *res; 86348a6092fSMaxime Coquelin int ret; 86448a6092fSMaxime Coquelin 86548a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 86648a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 86748a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 86848a6092fSMaxime Coquelin port->dev = &pdev->dev; 86948a6092fSMaxime Coquelin port->irq = platform_get_irq(pdev, 0); 8707d8f6861SBich HEMON port->rs485_config = stm32_config_rs485; 8717d8f6861SBich HEMON 8727d8f6861SBich HEMON stm32_init_rs485(port, pdev); 8737d8f6861SBich HEMON 874270e5a74SFabrice Gasnier stm32port->wakeirq = platform_get_irq(pdev, 1); 875351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 87648a6092fSMaxime Coquelin 87748a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 87848a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 87948a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 88048a6092fSMaxime Coquelin return PTR_ERR(port->membase); 88148a6092fSMaxime Coquelin port->mapbase = res->start; 88248a6092fSMaxime Coquelin 88348a6092fSMaxime Coquelin spin_lock_init(&port->lock); 88448a6092fSMaxime Coquelin 88548a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 88648a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 88748a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 88848a6092fSMaxime Coquelin 88948a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 89048a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 89148a6092fSMaxime Coquelin if (ret) 89248a6092fSMaxime Coquelin return ret; 89348a6092fSMaxime Coquelin 89448a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 895ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 896ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 89748a6092fSMaxime Coquelin ret = -EINVAL; 898ada80043SFabrice Gasnier } 89948a6092fSMaxime Coquelin 90048a6092fSMaxime Coquelin return ret; 90148a6092fSMaxime Coquelin } 90248a6092fSMaxime Coquelin 90348a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) 90448a6092fSMaxime Coquelin { 90548a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 90648a6092fSMaxime Coquelin int id; 90748a6092fSMaxime Coquelin 90848a6092fSMaxime Coquelin if (!np) 90948a6092fSMaxime Coquelin return NULL; 91048a6092fSMaxime Coquelin 91148a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 912e5707915SGerald Baeza if (id < 0) { 913e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 914e5707915SGerald Baeza return NULL; 915e5707915SGerald Baeza } 91648a6092fSMaxime Coquelin 91748a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 91848a6092fSMaxime Coquelin return NULL; 91948a6092fSMaxime Coquelin 92048a6092fSMaxime Coquelin stm32_ports[id].hw_flow_control = of_property_read_bool(np, 92159bed2dfSAlexandre TORGUE "st,hw-flow-ctrl"); 92248a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 923e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 92448a6092fSMaxime Coquelin return &stm32_ports[id]; 92548a6092fSMaxime Coquelin } 92648a6092fSMaxime Coquelin 92748a6092fSMaxime Coquelin #ifdef CONFIG_OF 92848a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 929ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 930ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 931270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 93248a6092fSMaxime Coquelin {}, 93348a6092fSMaxime Coquelin }; 93448a6092fSMaxime Coquelin 93548a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 93648a6092fSMaxime Coquelin #endif 93748a6092fSMaxime Coquelin 93834891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port, 93934891872SAlexandre TORGUE struct platform_device *pdev) 94034891872SAlexandre TORGUE { 94134891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 94234891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 94334891872SAlexandre TORGUE struct device *dev = &pdev->dev; 94434891872SAlexandre TORGUE struct dma_slave_config config; 94534891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 94634891872SAlexandre TORGUE dma_cookie_t cookie; 94734891872SAlexandre TORGUE int ret; 94834891872SAlexandre TORGUE 94934891872SAlexandre TORGUE /* Request DMA RX channel */ 95034891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 95134891872SAlexandre TORGUE if (!stm32port->rx_ch) { 95234891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 95334891872SAlexandre TORGUE return -ENODEV; 95434891872SAlexandre TORGUE } 95534891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 95634891872SAlexandre TORGUE &stm32port->rx_dma_buf, 95734891872SAlexandre TORGUE GFP_KERNEL); 95834891872SAlexandre TORGUE if (!stm32port->rx_buf) { 95934891872SAlexandre TORGUE ret = -ENOMEM; 96034891872SAlexandre TORGUE goto alloc_err; 96134891872SAlexandre TORGUE } 96234891872SAlexandre TORGUE 96334891872SAlexandre TORGUE /* Configure DMA channel */ 96434891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 9658e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 96634891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 96734891872SAlexandre TORGUE 96834891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 96934891872SAlexandre TORGUE if (ret < 0) { 97034891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 97134891872SAlexandre TORGUE ret = -ENODEV; 97234891872SAlexandre TORGUE goto config_err; 97334891872SAlexandre TORGUE } 97434891872SAlexandre TORGUE 97534891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 97634891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 97734891872SAlexandre TORGUE stm32port->rx_dma_buf, 97834891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 97934891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 98034891872SAlexandre TORGUE if (!desc) { 98134891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 98234891872SAlexandre TORGUE ret = -ENODEV; 98334891872SAlexandre TORGUE goto config_err; 98434891872SAlexandre TORGUE } 98534891872SAlexandre TORGUE 98634891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 98734891872SAlexandre TORGUE desc->callback = NULL; 98834891872SAlexandre TORGUE desc->callback_param = NULL; 98934891872SAlexandre TORGUE 99034891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 99134891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 99234891872SAlexandre TORGUE 99334891872SAlexandre TORGUE /* Issue pending DMA requests */ 99434891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 99534891872SAlexandre TORGUE 99634891872SAlexandre TORGUE return 0; 99734891872SAlexandre TORGUE 99834891872SAlexandre TORGUE config_err: 99934891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 100034891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 100134891872SAlexandre TORGUE stm32port->rx_dma_buf); 100234891872SAlexandre TORGUE 100334891872SAlexandre TORGUE alloc_err: 100434891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 100534891872SAlexandre TORGUE stm32port->rx_ch = NULL; 100634891872SAlexandre TORGUE 100734891872SAlexandre TORGUE return ret; 100834891872SAlexandre TORGUE } 100934891872SAlexandre TORGUE 101034891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port, 101134891872SAlexandre TORGUE struct platform_device *pdev) 101234891872SAlexandre TORGUE { 101334891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 101434891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 101534891872SAlexandre TORGUE struct device *dev = &pdev->dev; 101634891872SAlexandre TORGUE struct dma_slave_config config; 101734891872SAlexandre TORGUE int ret; 101834891872SAlexandre TORGUE 101934891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 102034891872SAlexandre TORGUE 102134891872SAlexandre TORGUE /* Request DMA TX channel */ 102234891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 102334891872SAlexandre TORGUE if (!stm32port->tx_ch) { 102434891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 102534891872SAlexandre TORGUE return -ENODEV; 102634891872SAlexandre TORGUE } 102734891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 102834891872SAlexandre TORGUE &stm32port->tx_dma_buf, 102934891872SAlexandre TORGUE GFP_KERNEL); 103034891872SAlexandre TORGUE if (!stm32port->tx_buf) { 103134891872SAlexandre TORGUE ret = -ENOMEM; 103234891872SAlexandre TORGUE goto alloc_err; 103334891872SAlexandre TORGUE } 103434891872SAlexandre TORGUE 103534891872SAlexandre TORGUE /* Configure DMA channel */ 103634891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 10378e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 103834891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 103934891872SAlexandre TORGUE 104034891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 104134891872SAlexandre TORGUE if (ret < 0) { 104234891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 104334891872SAlexandre TORGUE ret = -ENODEV; 104434891872SAlexandre TORGUE goto config_err; 104534891872SAlexandre TORGUE } 104634891872SAlexandre TORGUE 104734891872SAlexandre TORGUE return 0; 104834891872SAlexandre TORGUE 104934891872SAlexandre TORGUE config_err: 105034891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 105134891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 105234891872SAlexandre TORGUE stm32port->tx_dma_buf); 105334891872SAlexandre TORGUE 105434891872SAlexandre TORGUE alloc_err: 105534891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 105634891872SAlexandre TORGUE stm32port->tx_ch = NULL; 105734891872SAlexandre TORGUE 105834891872SAlexandre TORGUE return ret; 105934891872SAlexandre TORGUE } 106034891872SAlexandre TORGUE 106148a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev) 106248a6092fSMaxime Coquelin { 1063ada8618fSAlexandre TORGUE const struct of_device_id *match; 106448a6092fSMaxime Coquelin struct stm32_port *stm32port; 1065ada8618fSAlexandre TORGUE int ret; 106648a6092fSMaxime Coquelin 106748a6092fSMaxime Coquelin stm32port = stm32_of_get_stm32_port(pdev); 106848a6092fSMaxime Coquelin if (!stm32port) 106948a6092fSMaxime Coquelin return -ENODEV; 107048a6092fSMaxime Coquelin 1071ada8618fSAlexandre TORGUE match = of_match_device(stm32_match, &pdev->dev); 1072ada8618fSAlexandre TORGUE if (match && match->data) 1073ada8618fSAlexandre TORGUE stm32port->info = (struct stm32_usart_info *)match->data; 1074ada8618fSAlexandre TORGUE else 1075ada8618fSAlexandre TORGUE return -EINVAL; 1076ada8618fSAlexandre TORGUE 107748a6092fSMaxime Coquelin ret = stm32_init_port(stm32port, pdev); 107848a6092fSMaxime Coquelin if (ret) 107948a6092fSMaxime Coquelin return ret; 108048a6092fSMaxime Coquelin 1081270e5a74SFabrice Gasnier if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) { 1082270e5a74SFabrice Gasnier ret = device_init_wakeup(&pdev->dev, true); 108348a6092fSMaxime Coquelin if (ret) 1084ada80043SFabrice Gasnier goto err_uninit; 1085270e5a74SFabrice Gasnier } 1086270e5a74SFabrice Gasnier 1087270e5a74SFabrice Gasnier ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 1088270e5a74SFabrice Gasnier if (ret) 1089270e5a74SFabrice Gasnier goto err_nowup; 109048a6092fSMaxime Coquelin 109134891872SAlexandre TORGUE ret = stm32_of_dma_rx_probe(stm32port, pdev); 109234891872SAlexandre TORGUE if (ret) 109334891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 109434891872SAlexandre TORGUE 109534891872SAlexandre TORGUE ret = stm32_of_dma_tx_probe(stm32port, pdev); 109634891872SAlexandre TORGUE if (ret) 109734891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 109834891872SAlexandre TORGUE 109948a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 110048a6092fSMaxime Coquelin 110148a6092fSMaxime Coquelin return 0; 1102ada80043SFabrice Gasnier 1103270e5a74SFabrice Gasnier err_nowup: 1104270e5a74SFabrice Gasnier if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) 1105270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 1106270e5a74SFabrice Gasnier 1107ada80043SFabrice Gasnier err_uninit: 1108ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 1109ada80043SFabrice Gasnier 1110ada80043SFabrice Gasnier return ret; 111148a6092fSMaxime Coquelin } 111248a6092fSMaxime Coquelin 111348a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev) 111448a6092fSMaxime Coquelin { 111548a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1116511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 111734891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1118270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 111934891872SAlexandre TORGUE 112034891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 112134891872SAlexandre TORGUE 112234891872SAlexandre TORGUE if (stm32_port->rx_ch) 112334891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 112434891872SAlexandre TORGUE 112534891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 112634891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 112734891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 112834891872SAlexandre TORGUE stm32_port->rx_dma_buf); 112934891872SAlexandre TORGUE 113034891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 113134891872SAlexandre TORGUE 113234891872SAlexandre TORGUE if (stm32_port->tx_ch) 113334891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 113434891872SAlexandre TORGUE 113534891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 113634891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 113734891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 113834891872SAlexandre TORGUE stm32_port->tx_dma_buf); 1139511c7b1bSAlexandre TORGUE 1140270e5a74SFabrice Gasnier if (cfg->has_wakeup && stm32_port->wakeirq >= 0) 1141270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 1142270e5a74SFabrice Gasnier 1143511c7b1bSAlexandre TORGUE clk_disable_unprepare(stm32_port->clk); 114448a6092fSMaxime Coquelin 114548a6092fSMaxime Coquelin return uart_remove_one_port(&stm32_usart_driver, port); 114648a6092fSMaxime Coquelin } 114748a6092fSMaxime Coquelin 114848a6092fSMaxime Coquelin 114948a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 115048a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch) 115148a6092fSMaxime Coquelin { 1152ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1153ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1154ada8618fSAlexandre TORGUE 1155ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 115648a6092fSMaxime Coquelin cpu_relax(); 115748a6092fSMaxime Coquelin 1158ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 115948a6092fSMaxime Coquelin } 116048a6092fSMaxime Coquelin 116148a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt) 116248a6092fSMaxime Coquelin { 116348a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1164ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1165ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 116687f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 116748a6092fSMaxime Coquelin unsigned long flags; 116848a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 116948a6092fSMaxime Coquelin int locked = 1; 117048a6092fSMaxime Coquelin 117148a6092fSMaxime Coquelin local_irq_save(flags); 117248a6092fSMaxime Coquelin if (port->sysrq) 117348a6092fSMaxime Coquelin locked = 0; 117448a6092fSMaxime Coquelin else if (oops_in_progress) 117548a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 117648a6092fSMaxime Coquelin else 117748a6092fSMaxime Coquelin spin_lock(&port->lock); 117848a6092fSMaxime Coquelin 117987f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1180ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 118148a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 118287f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1183ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 118448a6092fSMaxime Coquelin 118548a6092fSMaxime Coquelin uart_console_write(port, s, cnt, stm32_console_putchar); 118648a6092fSMaxime Coquelin 118748a6092fSMaxime Coquelin /* Restore interrupt state */ 1188ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 118948a6092fSMaxime Coquelin 119048a6092fSMaxime Coquelin if (locked) 119148a6092fSMaxime Coquelin spin_unlock(&port->lock); 119248a6092fSMaxime Coquelin local_irq_restore(flags); 119348a6092fSMaxime Coquelin } 119448a6092fSMaxime Coquelin 119548a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options) 119648a6092fSMaxime Coquelin { 119748a6092fSMaxime Coquelin struct stm32_port *stm32port; 119848a6092fSMaxime Coquelin int baud = 9600; 119948a6092fSMaxime Coquelin int bits = 8; 120048a6092fSMaxime Coquelin int parity = 'n'; 120148a6092fSMaxime Coquelin int flow = 'n'; 120248a6092fSMaxime Coquelin 120348a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 120448a6092fSMaxime Coquelin return -ENODEV; 120548a6092fSMaxime Coquelin 120648a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 120748a6092fSMaxime Coquelin 120848a6092fSMaxime Coquelin /* 120948a6092fSMaxime Coquelin * This driver does not support early console initialization 121048a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 121148a6092fSMaxime Coquelin * this to be called during the uart port registration when the 121248a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 121348a6092fSMaxime Coquelin */ 121448a6092fSMaxime Coquelin if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) 121548a6092fSMaxime Coquelin return -ENXIO; 121648a6092fSMaxime Coquelin 121748a6092fSMaxime Coquelin if (options) 121848a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 121948a6092fSMaxime Coquelin 122048a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 122148a6092fSMaxime Coquelin } 122248a6092fSMaxime Coquelin 122348a6092fSMaxime Coquelin static struct console stm32_console = { 122448a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 122548a6092fSMaxime Coquelin .device = uart_console_device, 122648a6092fSMaxime Coquelin .write = stm32_console_write, 122748a6092fSMaxime Coquelin .setup = stm32_console_setup, 122848a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 122948a6092fSMaxime Coquelin .index = -1, 123048a6092fSMaxime Coquelin .data = &stm32_usart_driver, 123148a6092fSMaxime Coquelin }; 123248a6092fSMaxime Coquelin 123348a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 123448a6092fSMaxime Coquelin 123548a6092fSMaxime Coquelin #else 123648a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 123748a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 123848a6092fSMaxime Coquelin 123948a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 124048a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 124148a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 124248a6092fSMaxime Coquelin .major = 0, 124348a6092fSMaxime Coquelin .minor = 0, 124448a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 124548a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 124648a6092fSMaxime Coquelin }; 124748a6092fSMaxime Coquelin 1248270e5a74SFabrice Gasnier #ifdef CONFIG_PM_SLEEP 1249270e5a74SFabrice Gasnier static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable) 1250270e5a74SFabrice Gasnier { 1251270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1252270e5a74SFabrice Gasnier struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1253270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1254270e5a74SFabrice Gasnier u32 val; 1255270e5a74SFabrice Gasnier 1256270e5a74SFabrice Gasnier if (!cfg->has_wakeup || stm32_port->wakeirq < 0) 1257270e5a74SFabrice Gasnier return; 1258270e5a74SFabrice Gasnier 1259270e5a74SFabrice Gasnier if (enable) { 1260270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1261270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, USART_CR1_UESM); 1262270e5a74SFabrice Gasnier val = readl_relaxed(port->membase + ofs->cr3); 1263270e5a74SFabrice Gasnier val &= ~USART_CR3_WUS_MASK; 1264270e5a74SFabrice Gasnier /* Enable Wake up interrupt from low power on start bit */ 1265270e5a74SFabrice Gasnier val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE; 1266270e5a74SFabrice Gasnier writel_relaxed(val, port->membase + ofs->cr3); 1267270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1268270e5a74SFabrice Gasnier } else { 1269270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM); 1270270e5a74SFabrice Gasnier } 1271270e5a74SFabrice Gasnier } 1272270e5a74SFabrice Gasnier 1273270e5a74SFabrice Gasnier static int stm32_serial_suspend(struct device *dev) 1274270e5a74SFabrice Gasnier { 1275270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1276270e5a74SFabrice Gasnier 1277270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1278270e5a74SFabrice Gasnier 1279270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1280270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, true); 1281270e5a74SFabrice Gasnier else 1282270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1283270e5a74SFabrice Gasnier 1284270e5a74SFabrice Gasnier return 0; 1285270e5a74SFabrice Gasnier } 1286270e5a74SFabrice Gasnier 1287270e5a74SFabrice Gasnier static int stm32_serial_resume(struct device *dev) 1288270e5a74SFabrice Gasnier { 1289270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1290270e5a74SFabrice Gasnier 1291270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1292270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1293270e5a74SFabrice Gasnier 1294270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1295270e5a74SFabrice Gasnier } 1296270e5a74SFabrice Gasnier #endif /* CONFIG_PM_SLEEP */ 1297270e5a74SFabrice Gasnier 1298270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 1299270e5a74SFabrice Gasnier SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume) 1300270e5a74SFabrice Gasnier }; 1301270e5a74SFabrice Gasnier 130248a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 130348a6092fSMaxime Coquelin .probe = stm32_serial_probe, 130448a6092fSMaxime Coquelin .remove = stm32_serial_remove, 130548a6092fSMaxime Coquelin .driver = { 130648a6092fSMaxime Coquelin .name = DRIVER_NAME, 1307270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 130848a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 130948a6092fSMaxime Coquelin }, 131048a6092fSMaxime Coquelin }; 131148a6092fSMaxime Coquelin 131248a6092fSMaxime Coquelin static int __init usart_init(void) 131348a6092fSMaxime Coquelin { 131448a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 131548a6092fSMaxime Coquelin int ret; 131648a6092fSMaxime Coquelin 131748a6092fSMaxime Coquelin pr_info("%s\n", banner); 131848a6092fSMaxime Coquelin 131948a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 132048a6092fSMaxime Coquelin if (ret) 132148a6092fSMaxime Coquelin return ret; 132248a6092fSMaxime Coquelin 132348a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 132448a6092fSMaxime Coquelin if (ret) 132548a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 132648a6092fSMaxime Coquelin 132748a6092fSMaxime Coquelin return ret; 132848a6092fSMaxime Coquelin } 132948a6092fSMaxime Coquelin 133048a6092fSMaxime Coquelin static void __exit usart_exit(void) 133148a6092fSMaxime Coquelin { 133248a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 133348a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 133448a6092fSMaxime Coquelin } 133548a6092fSMaxime Coquelin 133648a6092fSMaxime Coquelin module_init(usart_init); 133748a6092fSMaxime Coquelin module_exit(usart_exit); 133848a6092fSMaxime Coquelin 133948a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 134048a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 134148a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1342