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 65*1bcda09dSBich HEMON static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, 66*1bcda09dSBich HEMON u32 delay_DDE, u32 baud) 67*1bcda09dSBich HEMON { 68*1bcda09dSBich HEMON u32 rs485_deat_dedt; 69*1bcda09dSBich HEMON u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); 70*1bcda09dSBich HEMON bool over8; 71*1bcda09dSBich HEMON 72*1bcda09dSBich HEMON *cr3 |= USART_CR3_DEM; 73*1bcda09dSBich HEMON over8 = *cr1 & USART_CR1_OVER8; 74*1bcda09dSBich HEMON 75*1bcda09dSBich HEMON if (over8) 76*1bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 8; 77*1bcda09dSBich HEMON else 78*1bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 16; 79*1bcda09dSBich HEMON 80*1bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 81*1bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 82*1bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 83*1bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & 84*1bcda09dSBich HEMON USART_CR1_DEAT_MASK; 85*1bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 86*1bcda09dSBich HEMON 87*1bcda09dSBich HEMON if (over8) 88*1bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 8; 89*1bcda09dSBich HEMON else 90*1bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 16; 91*1bcda09dSBich HEMON 92*1bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 93*1bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 94*1bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 95*1bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & 96*1bcda09dSBich HEMON USART_CR1_DEDT_MASK; 97*1bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 98*1bcda09dSBich HEMON } 99*1bcda09dSBich HEMON 100*1bcda09dSBich HEMON static int stm32_config_rs485(struct uart_port *port, 101*1bcda09dSBich HEMON struct serial_rs485 *rs485conf) 102*1bcda09dSBich HEMON { 103*1bcda09dSBich HEMON struct stm32_port *stm32_port = to_stm32_port(port); 104*1bcda09dSBich HEMON struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 105*1bcda09dSBich HEMON struct stm32_usart_config *cfg = &stm32_port->info->cfg; 106*1bcda09dSBich HEMON u32 usartdiv, baud, cr1, cr3; 107*1bcda09dSBich HEMON bool over8; 108*1bcda09dSBich HEMON unsigned long flags; 109*1bcda09dSBich HEMON 110*1bcda09dSBich HEMON spin_lock_irqsave(&port->lock, flags); 111*1bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 112*1bcda09dSBich HEMON 113*1bcda09dSBich HEMON port->rs485 = *rs485conf; 114*1bcda09dSBich HEMON 115*1bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RX_DURING_TX; 116*1bcda09dSBich HEMON 117*1bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 118*1bcda09dSBich HEMON cr1 = readl_relaxed(port->membase + ofs->cr1); 119*1bcda09dSBich HEMON cr3 = readl_relaxed(port->membase + ofs->cr3); 120*1bcda09dSBich HEMON usartdiv = readl_relaxed(port->membase + ofs->brr); 121*1bcda09dSBich HEMON usartdiv = usartdiv & GENMASK(15, 0); 122*1bcda09dSBich HEMON over8 = cr1 & USART_CR1_OVER8; 123*1bcda09dSBich HEMON 124*1bcda09dSBich HEMON if (over8) 125*1bcda09dSBich HEMON usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) 126*1bcda09dSBich HEMON << USART_BRR_04_R_SHIFT; 127*1bcda09dSBich HEMON 128*1bcda09dSBich HEMON baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); 129*1bcda09dSBich HEMON stm32_config_reg_rs485(&cr1, &cr3, 130*1bcda09dSBich HEMON rs485conf->delay_rts_before_send, 131*1bcda09dSBich HEMON rs485conf->delay_rts_after_send, baud); 132*1bcda09dSBich HEMON 133*1bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 134*1bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 135*1bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 136*1bcda09dSBich HEMON } else { 137*1bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 138*1bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 139*1bcda09dSBich HEMON } 140*1bcda09dSBich HEMON 141*1bcda09dSBich HEMON writel_relaxed(cr3, port->membase + ofs->cr3); 142*1bcda09dSBich HEMON writel_relaxed(cr1, port->membase + ofs->cr1); 143*1bcda09dSBich HEMON } else { 144*1bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP); 145*1bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr1, 146*1bcda09dSBich HEMON USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 147*1bcda09dSBich HEMON } 148*1bcda09dSBich HEMON 149*1bcda09dSBich HEMON stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 150*1bcda09dSBich HEMON spin_unlock_irqrestore(&port->lock, flags); 151*1bcda09dSBich HEMON 152*1bcda09dSBich HEMON return 0; 153*1bcda09dSBich HEMON } 154*1bcda09dSBich HEMON 155*1bcda09dSBich HEMON static int stm32_init_rs485(struct uart_port *port, 156*1bcda09dSBich HEMON struct platform_device *pdev) 157*1bcda09dSBich HEMON { 158*1bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 159*1bcda09dSBich HEMON 160*1bcda09dSBich HEMON rs485conf->flags = 0; 161*1bcda09dSBich HEMON rs485conf->delay_rts_before_send = 0; 162*1bcda09dSBich HEMON rs485conf->delay_rts_after_send = 0; 163*1bcda09dSBich HEMON 164*1bcda09dSBich HEMON if (!pdev->dev.of_node) 165*1bcda09dSBich HEMON return -ENODEV; 166*1bcda09dSBich HEMON 167*1bcda09dSBich HEMON uart_get_rs485_mode(&pdev->dev, rs485conf); 168*1bcda09dSBich HEMON 169*1bcda09dSBich HEMON return 0; 170*1bcda09dSBich HEMON } 171*1bcda09dSBich 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; 228e5707915SGerald Baeza c = stm32_get_char(port, &sr, &stm32_port->last_res); 22948a6092fSMaxime Coquelin flag = TTY_NORMAL; 23048a6092fSMaxime Coquelin port->icount.rx++; 23148a6092fSMaxime Coquelin 23248a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 23348a6092fSMaxime Coquelin if (sr & USART_SR_LBD) { 23448a6092fSMaxime Coquelin port->icount.brk++; 23548a6092fSMaxime Coquelin if (uart_handle_break(port)) 23648a6092fSMaxime Coquelin continue; 23748a6092fSMaxime Coquelin } else if (sr & USART_SR_ORE) { 238ada8618fSAlexandre TORGUE if (ofs->icr != UNDEF_REG) 239ada8618fSAlexandre TORGUE writel_relaxed(USART_ICR_ORECF, 240ada8618fSAlexandre TORGUE port->membase + 241ada8618fSAlexandre TORGUE ofs->icr); 24248a6092fSMaxime Coquelin port->icount.overrun++; 24348a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 24448a6092fSMaxime Coquelin port->icount.parity++; 24548a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 24648a6092fSMaxime Coquelin port->icount.frame++; 24748a6092fSMaxime Coquelin } 24848a6092fSMaxime Coquelin 24948a6092fSMaxime Coquelin sr &= port->read_status_mask; 25048a6092fSMaxime Coquelin 25148a6092fSMaxime Coquelin if (sr & USART_SR_LBD) 25248a6092fSMaxime Coquelin flag = TTY_BREAK; 25348a6092fSMaxime Coquelin else if (sr & USART_SR_PE) 25448a6092fSMaxime Coquelin flag = TTY_PARITY; 25548a6092fSMaxime Coquelin else if (sr & USART_SR_FE) 25648a6092fSMaxime Coquelin flag = TTY_FRAME; 25748a6092fSMaxime Coquelin } 25848a6092fSMaxime Coquelin 25948a6092fSMaxime Coquelin if (uart_handle_sysrq_char(port, c)) 26048a6092fSMaxime Coquelin continue; 26148a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 26248a6092fSMaxime Coquelin } 26348a6092fSMaxime Coquelin 26448a6092fSMaxime Coquelin spin_unlock(&port->lock); 26548a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 26648a6092fSMaxime Coquelin spin_lock(&port->lock); 26748a6092fSMaxime Coquelin } 26848a6092fSMaxime Coquelin 26934891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg) 27034891872SAlexandre TORGUE { 27134891872SAlexandre TORGUE struct uart_port *port = arg; 27234891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 27334891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 27434891872SAlexandre TORGUE unsigned int isr; 27534891872SAlexandre TORGUE int ret; 27634891872SAlexandre TORGUE 27734891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 27834891872SAlexandre TORGUE isr, 27934891872SAlexandre TORGUE (isr & USART_SR_TC), 28034891872SAlexandre TORGUE 10, 100000); 28134891872SAlexandre TORGUE 28234891872SAlexandre TORGUE if (ret) 28334891872SAlexandre TORGUE dev_err(port->dev, "terminal count not set\n"); 28434891872SAlexandre TORGUE 28534891872SAlexandre TORGUE if (ofs->icr == UNDEF_REG) 28634891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 28734891872SAlexandre TORGUE else 28834891872SAlexandre TORGUE stm32_set_bits(port, ofs->icr, USART_CR_TC); 28934891872SAlexandre TORGUE 29034891872SAlexandre TORGUE stm32_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 */ 29434891872SAlexandre TORGUE stm32_transmit_chars(port); 29534891872SAlexandre TORGUE } 29634891872SAlexandre TORGUE 29734891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port) 29834891872SAlexandre TORGUE { 29934891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 30034891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 30134891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 30234891872SAlexandre TORGUE unsigned int isr; 30334891872SAlexandre TORGUE int ret; 30434891872SAlexandre TORGUE 30534891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 30634891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 30734891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 30834891872SAlexandre TORGUE } 30934891872SAlexandre TORGUE 31034891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 31134891872SAlexandre TORGUE isr, 31234891872SAlexandre TORGUE (isr & USART_SR_TXE), 313a61d9e6eSGerald Baeza 10, 100000); 31434891872SAlexandre TORGUE 31534891872SAlexandre TORGUE if (ret) 31634891872SAlexandre TORGUE dev_err(port->dev, "tx empty not set\n"); 31734891872SAlexandre TORGUE 31834891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 31934891872SAlexandre TORGUE 32034891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 32134891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 32234891872SAlexandre TORGUE port->icount.tx++; 32334891872SAlexandre TORGUE } 32434891872SAlexandre TORGUE 32534891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port) 32634891872SAlexandre TORGUE { 32734891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 32834891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 32934891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 33034891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 33134891872SAlexandre TORGUE dma_cookie_t cookie; 33234891872SAlexandre TORGUE unsigned int count, i; 33334891872SAlexandre TORGUE 33434891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 33534891872SAlexandre TORGUE return; 33634891872SAlexandre TORGUE 33734891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 33834891872SAlexandre TORGUE 33934891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 34034891872SAlexandre TORGUE 34134891872SAlexandre TORGUE if (count > TX_BUF_L) 34234891872SAlexandre TORGUE count = TX_BUF_L; 34334891872SAlexandre TORGUE 34434891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 34534891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 34634891872SAlexandre TORGUE } else { 34734891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 34834891872SAlexandre TORGUE size_t two; 34934891872SAlexandre TORGUE 35034891872SAlexandre TORGUE if (one > count) 35134891872SAlexandre TORGUE one = count; 35234891872SAlexandre TORGUE two = count - one; 35334891872SAlexandre TORGUE 35434891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 35534891872SAlexandre TORGUE if (two) 35634891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 35734891872SAlexandre TORGUE } 35834891872SAlexandre TORGUE 35934891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 36034891872SAlexandre TORGUE stm32port->tx_dma_buf, 36134891872SAlexandre TORGUE count, 36234891872SAlexandre TORGUE DMA_MEM_TO_DEV, 36334891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 36434891872SAlexandre TORGUE 36534891872SAlexandre TORGUE if (!desc) { 36634891872SAlexandre TORGUE for (i = count; i > 0; i--) 36734891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 36834891872SAlexandre TORGUE return; 36934891872SAlexandre TORGUE } 37034891872SAlexandre TORGUE 37134891872SAlexandre TORGUE desc->callback = stm32_tx_dma_complete; 37234891872SAlexandre TORGUE desc->callback_param = port; 37334891872SAlexandre TORGUE 37434891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 37534891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 37634891872SAlexandre TORGUE 37734891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 37834891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 37934891872SAlexandre TORGUE 38034891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 38134891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 38234891872SAlexandre TORGUE 38334891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 38434891872SAlexandre TORGUE port->icount.tx += count; 38534891872SAlexandre TORGUE } 38634891872SAlexandre TORGUE 38748a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port) 38848a6092fSMaxime Coquelin { 389ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 390ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 39148a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 39248a6092fSMaxime Coquelin 39348a6092fSMaxime Coquelin if (port->x_char) { 39434891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 39534891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 396ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 39748a6092fSMaxime Coquelin port->x_char = 0; 39848a6092fSMaxime Coquelin port->icount.tx++; 39934891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 40034891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 40148a6092fSMaxime Coquelin return; 40248a6092fSMaxime Coquelin } 40348a6092fSMaxime Coquelin 40448a6092fSMaxime Coquelin if (uart_tx_stopped(port)) { 40548a6092fSMaxime Coquelin stm32_stop_tx(port); 40648a6092fSMaxime Coquelin return; 40748a6092fSMaxime Coquelin } 40848a6092fSMaxime Coquelin 40948a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) { 41048a6092fSMaxime Coquelin stm32_stop_tx(port); 41148a6092fSMaxime Coquelin return; 41248a6092fSMaxime Coquelin } 41348a6092fSMaxime Coquelin 41434891872SAlexandre TORGUE if (stm32_port->tx_ch) 41534891872SAlexandre TORGUE stm32_transmit_chars_dma(port); 41634891872SAlexandre TORGUE else 41734891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 41848a6092fSMaxime Coquelin 41948a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 42048a6092fSMaxime Coquelin uart_write_wakeup(port); 42148a6092fSMaxime Coquelin 42248a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 42348a6092fSMaxime Coquelin stm32_stop_tx(port); 42448a6092fSMaxime Coquelin } 42548a6092fSMaxime Coquelin 42648a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr) 42748a6092fSMaxime Coquelin { 42848a6092fSMaxime Coquelin struct uart_port *port = ptr; 429ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 430ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 43148a6092fSMaxime Coquelin u32 sr; 43248a6092fSMaxime Coquelin 43301d32d71SAlexandre TORGUE spin_lock(&port->lock); 43401d32d71SAlexandre TORGUE 435ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 43648a6092fSMaxime Coquelin 437270e5a74SFabrice Gasnier if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG)) 438270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 439270e5a74SFabrice Gasnier port->membase + ofs->icr); 440270e5a74SFabrice Gasnier 44134891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 44234891872SAlexandre TORGUE stm32_receive_chars(port, false); 44348a6092fSMaxime Coquelin 44434891872SAlexandre TORGUE if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) 44548a6092fSMaxime Coquelin stm32_transmit_chars(port); 44648a6092fSMaxime Coquelin 44701d32d71SAlexandre TORGUE spin_unlock(&port->lock); 44801d32d71SAlexandre TORGUE 44934891872SAlexandre TORGUE if (stm32_port->rx_ch) 45034891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 45134891872SAlexandre TORGUE else 45234891872SAlexandre TORGUE return IRQ_HANDLED; 45334891872SAlexandre TORGUE } 45434891872SAlexandre TORGUE 45534891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr) 45634891872SAlexandre TORGUE { 45734891872SAlexandre TORGUE struct uart_port *port = ptr; 45834891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 45934891872SAlexandre TORGUE 46034891872SAlexandre TORGUE spin_lock(&port->lock); 46134891872SAlexandre TORGUE 46234891872SAlexandre TORGUE if (stm32_port->rx_ch) 46334891872SAlexandre TORGUE stm32_receive_chars(port, true); 46434891872SAlexandre TORGUE 46548a6092fSMaxime Coquelin spin_unlock(&port->lock); 46648a6092fSMaxime Coquelin 46748a6092fSMaxime Coquelin return IRQ_HANDLED; 46848a6092fSMaxime Coquelin } 46948a6092fSMaxime Coquelin 47048a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port) 47148a6092fSMaxime Coquelin { 472ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 473ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 474ada8618fSAlexandre TORGUE 475ada8618fSAlexandre TORGUE return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; 47648a6092fSMaxime Coquelin } 47748a6092fSMaxime Coquelin 47848a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) 47948a6092fSMaxime Coquelin { 480ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 481ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 482ada8618fSAlexandre TORGUE 48348a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 484ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); 48548a6092fSMaxime Coquelin else 486ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 48748a6092fSMaxime Coquelin } 48848a6092fSMaxime Coquelin 48948a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port) 49048a6092fSMaxime Coquelin { 49148a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 49248a6092fSMaxime Coquelin return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 49348a6092fSMaxime Coquelin } 49448a6092fSMaxime Coquelin 49548a6092fSMaxime Coquelin /* Transmit stop */ 49648a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port) 49748a6092fSMaxime Coquelin { 498ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 499ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 500ada8618fSAlexandre TORGUE 501ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 50248a6092fSMaxime Coquelin } 50348a6092fSMaxime Coquelin 50448a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 50548a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port) 50648a6092fSMaxime Coquelin { 50748a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 50848a6092fSMaxime Coquelin 50948a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 51048a6092fSMaxime Coquelin return; 51148a6092fSMaxime Coquelin 51234891872SAlexandre TORGUE stm32_transmit_chars(port); 51348a6092fSMaxime Coquelin } 51448a6092fSMaxime Coquelin 51548a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 51648a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port) 51748a6092fSMaxime Coquelin { 518ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 519ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 52048a6092fSMaxime Coquelin unsigned long flags; 52148a6092fSMaxime Coquelin 52248a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 523ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 52448a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 52548a6092fSMaxime Coquelin } 52648a6092fSMaxime Coquelin 52748a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 52848a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port) 52948a6092fSMaxime Coquelin { 530ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 531ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 53248a6092fSMaxime Coquelin unsigned long flags; 53348a6092fSMaxime Coquelin 53448a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 535ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE); 53648a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 53748a6092fSMaxime Coquelin } 53848a6092fSMaxime Coquelin 53948a6092fSMaxime Coquelin /* Receive stop */ 54048a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port) 54148a6092fSMaxime Coquelin { 542ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 543ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 544ada8618fSAlexandre TORGUE 545ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 54648a6092fSMaxime Coquelin } 54748a6092fSMaxime Coquelin 54848a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 54948a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state) 55048a6092fSMaxime Coquelin { 55148a6092fSMaxime Coquelin } 55248a6092fSMaxime Coquelin 55348a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port) 55448a6092fSMaxime Coquelin { 555ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 556ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 557270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 55848a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 55948a6092fSMaxime Coquelin u32 val; 56048a6092fSMaxime Coquelin int ret; 56148a6092fSMaxime Coquelin 56234891872SAlexandre TORGUE ret = request_threaded_irq(port->irq, stm32_interrupt, 56334891872SAlexandre TORGUE stm32_threaded_interrupt, 56434891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 56548a6092fSMaxime Coquelin if (ret) 56648a6092fSMaxime Coquelin return ret; 56748a6092fSMaxime Coquelin 568270e5a74SFabrice Gasnier if (cfg->has_wakeup && stm32_port->wakeirq >= 0) { 569270e5a74SFabrice Gasnier ret = dev_pm_set_dedicated_wake_irq(port->dev, 570270e5a74SFabrice Gasnier stm32_port->wakeirq); 571270e5a74SFabrice Gasnier if (ret) { 572270e5a74SFabrice Gasnier free_irq(port->irq, port); 573270e5a74SFabrice Gasnier return ret; 574270e5a74SFabrice Gasnier } 575270e5a74SFabrice Gasnier } 576270e5a74SFabrice Gasnier 57748a6092fSMaxime Coquelin val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 578351a762aSGerald Baeza if (stm32_port->fifoen) 579351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 580ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, val); 58148a6092fSMaxime Coquelin 58248a6092fSMaxime Coquelin return 0; 58348a6092fSMaxime Coquelin } 58448a6092fSMaxime Coquelin 58548a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port) 58648a6092fSMaxime Coquelin { 587ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 588ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 58987f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 59048a6092fSMaxime Coquelin u32 val; 59148a6092fSMaxime Coquelin 59248a6092fSMaxime Coquelin val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 59387f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 594351a762aSGerald Baeza if (stm32_port->fifoen) 595351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 596a14f66a4SAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, val); 59748a6092fSMaxime Coquelin 598270e5a74SFabrice Gasnier dev_pm_clear_wake_irq(port->dev); 59948a6092fSMaxime Coquelin free_irq(port->irq, port); 60048a6092fSMaxime Coquelin } 60148a6092fSMaxime Coquelin 60248a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, 60348a6092fSMaxime Coquelin struct ktermios *old) 60448a6092fSMaxime Coquelin { 60548a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 606ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 607ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 608*1bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 60948a6092fSMaxime Coquelin unsigned int baud; 61048a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 61148a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 61248a6092fSMaxime Coquelin u32 cr1, cr2, cr3; 61348a6092fSMaxime Coquelin unsigned long flags; 61448a6092fSMaxime Coquelin 61548a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 61648a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 61748a6092fSMaxime Coquelin 61848a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 61948a6092fSMaxime Coquelin 62048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 62148a6092fSMaxime Coquelin 62248a6092fSMaxime Coquelin /* Stop serial port and reset value */ 623ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 62448a6092fSMaxime Coquelin 625ada8618fSAlexandre TORGUE cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; 626*1bcda09dSBich HEMON 627351a762aSGerald Baeza if (stm32_port->fifoen) 628351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 62948a6092fSMaxime Coquelin cr2 = 0; 63048a6092fSMaxime Coquelin cr3 = 0; 63148a6092fSMaxime Coquelin 63248a6092fSMaxime Coquelin if (cflag & CSTOPB) 63348a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 63448a6092fSMaxime Coquelin 63548a6092fSMaxime Coquelin if (cflag & PARENB) { 63648a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 637ada8618fSAlexandre TORGUE if ((cflag & CSIZE) == CS8) { 638ada8618fSAlexandre TORGUE if (cfg->has_7bits_data) 639ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 640ada8618fSAlexandre TORGUE else 64148a6092fSMaxime Coquelin cr1 |= USART_CR1_M; 64248a6092fSMaxime Coquelin } 643ada8618fSAlexandre TORGUE } 64448a6092fSMaxime Coquelin 64548a6092fSMaxime Coquelin if (cflag & PARODD) 64648a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 64748a6092fSMaxime Coquelin 64848a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 64948a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 65048a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 65135abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 65248a6092fSMaxime Coquelin } 65348a6092fSMaxime Coquelin 65448a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 65548a6092fSMaxime Coquelin 65648a6092fSMaxime Coquelin /* 65748a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 65848a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 65948a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 66048a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 66148a6092fSMaxime Coquelin */ 66248a6092fSMaxime Coquelin if (usartdiv < 16) { 66348a6092fSMaxime Coquelin oversampling = 8; 664*1bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 665ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); 66648a6092fSMaxime Coquelin } else { 66748a6092fSMaxime Coquelin oversampling = 16; 668*1bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 669ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 67048a6092fSMaxime Coquelin } 67148a6092fSMaxime Coquelin 67248a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 67348a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 674ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 67548a6092fSMaxime Coquelin 67648a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 67748a6092fSMaxime Coquelin 67848a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 67948a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 68048a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 68148a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 68248a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_LBD; 68348a6092fSMaxime Coquelin 68448a6092fSMaxime Coquelin /* Characters to ignore */ 68548a6092fSMaxime Coquelin port->ignore_status_mask = 0; 68648a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 68748a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 68848a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 68948a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_LBD; 69048a6092fSMaxime Coquelin /* 69148a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 69248a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 69348a6092fSMaxime Coquelin */ 69448a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 69548a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 69648a6092fSMaxime Coquelin } 69748a6092fSMaxime Coquelin 69848a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 69948a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 70048a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 70148a6092fSMaxime Coquelin 70234891872SAlexandre TORGUE if (stm32_port->rx_ch) 70334891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 70434891872SAlexandre TORGUE 705*1bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 706*1bcda09dSBich HEMON stm32_config_reg_rs485(&cr1, &cr3, 707*1bcda09dSBich HEMON rs485conf->delay_rts_before_send, 708*1bcda09dSBich HEMON rs485conf->delay_rts_after_send, baud); 709*1bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 710*1bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 711*1bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 712*1bcda09dSBich HEMON } else { 713*1bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 714*1bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 715*1bcda09dSBich HEMON } 716*1bcda09dSBich HEMON 717*1bcda09dSBich HEMON } else { 718*1bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 719*1bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 720*1bcda09dSBich HEMON } 721*1bcda09dSBich HEMON 722ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 723ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 724ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 72548a6092fSMaxime Coquelin 726*1bcda09dSBich HEMON stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 72748a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 72848a6092fSMaxime Coquelin } 72948a6092fSMaxime Coquelin 73048a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port) 73148a6092fSMaxime Coquelin { 73248a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 73348a6092fSMaxime Coquelin } 73448a6092fSMaxime Coquelin 73548a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port) 73648a6092fSMaxime Coquelin { 73748a6092fSMaxime Coquelin } 73848a6092fSMaxime Coquelin 73948a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port) 74048a6092fSMaxime Coquelin { 74148a6092fSMaxime Coquelin return 0; 74248a6092fSMaxime Coquelin } 74348a6092fSMaxime Coquelin 74448a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags) 74548a6092fSMaxime Coquelin { 74648a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 74748a6092fSMaxime Coquelin port->type = PORT_STM32; 74848a6092fSMaxime Coquelin } 74948a6092fSMaxime Coquelin 75048a6092fSMaxime Coquelin static int 75148a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser) 75248a6092fSMaxime Coquelin { 75348a6092fSMaxime Coquelin /* No user changeable parameters */ 75448a6092fSMaxime Coquelin return -EINVAL; 75548a6092fSMaxime Coquelin } 75648a6092fSMaxime Coquelin 75748a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state, 75848a6092fSMaxime Coquelin unsigned int oldstate) 75948a6092fSMaxime Coquelin { 76048a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 76148a6092fSMaxime Coquelin struct stm32_port, port); 762ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 763ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32port->info->cfg; 76448a6092fSMaxime Coquelin unsigned long flags = 0; 76548a6092fSMaxime Coquelin 76648a6092fSMaxime Coquelin switch (state) { 76748a6092fSMaxime Coquelin case UART_PM_STATE_ON: 76848a6092fSMaxime Coquelin clk_prepare_enable(stm32port->clk); 76948a6092fSMaxime Coquelin break; 77048a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 77148a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 772ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 77348a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 77448a6092fSMaxime Coquelin clk_disable_unprepare(stm32port->clk); 77548a6092fSMaxime Coquelin break; 77648a6092fSMaxime Coquelin } 77748a6092fSMaxime Coquelin } 77848a6092fSMaxime Coquelin 77948a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 78048a6092fSMaxime Coquelin .tx_empty = stm32_tx_empty, 78148a6092fSMaxime Coquelin .set_mctrl = stm32_set_mctrl, 78248a6092fSMaxime Coquelin .get_mctrl = stm32_get_mctrl, 78348a6092fSMaxime Coquelin .stop_tx = stm32_stop_tx, 78448a6092fSMaxime Coquelin .start_tx = stm32_start_tx, 78548a6092fSMaxime Coquelin .throttle = stm32_throttle, 78648a6092fSMaxime Coquelin .unthrottle = stm32_unthrottle, 78748a6092fSMaxime Coquelin .stop_rx = stm32_stop_rx, 78848a6092fSMaxime Coquelin .break_ctl = stm32_break_ctl, 78948a6092fSMaxime Coquelin .startup = stm32_startup, 79048a6092fSMaxime Coquelin .shutdown = stm32_shutdown, 79148a6092fSMaxime Coquelin .set_termios = stm32_set_termios, 79248a6092fSMaxime Coquelin .pm = stm32_pm, 79348a6092fSMaxime Coquelin .type = stm32_type, 79448a6092fSMaxime Coquelin .release_port = stm32_release_port, 79548a6092fSMaxime Coquelin .request_port = stm32_request_port, 79648a6092fSMaxime Coquelin .config_port = stm32_config_port, 79748a6092fSMaxime Coquelin .verify_port = stm32_verify_port, 79848a6092fSMaxime Coquelin }; 79948a6092fSMaxime Coquelin 80048a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port, 80148a6092fSMaxime Coquelin struct platform_device *pdev) 80248a6092fSMaxime Coquelin { 80348a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 80448a6092fSMaxime Coquelin struct resource *res; 80548a6092fSMaxime Coquelin int ret; 80648a6092fSMaxime Coquelin 80748a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 80848a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 80948a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 81048a6092fSMaxime Coquelin port->dev = &pdev->dev; 81148a6092fSMaxime Coquelin port->irq = platform_get_irq(pdev, 0); 812270e5a74SFabrice Gasnier stm32port->wakeirq = platform_get_irq(pdev, 1); 813351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 81448a6092fSMaxime Coquelin 81548a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 81648a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 81748a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 81848a6092fSMaxime Coquelin return PTR_ERR(port->membase); 81948a6092fSMaxime Coquelin port->mapbase = res->start; 82048a6092fSMaxime Coquelin 82148a6092fSMaxime Coquelin spin_lock_init(&port->lock); 82248a6092fSMaxime Coquelin 82348a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 82448a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 82548a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 82648a6092fSMaxime Coquelin 82748a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 82848a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 82948a6092fSMaxime Coquelin if (ret) 83048a6092fSMaxime Coquelin return ret; 83148a6092fSMaxime Coquelin 83248a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 833ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 834ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 83548a6092fSMaxime Coquelin ret = -EINVAL; 836ada80043SFabrice Gasnier } 83748a6092fSMaxime Coquelin 83848a6092fSMaxime Coquelin return ret; 83948a6092fSMaxime Coquelin } 84048a6092fSMaxime Coquelin 84148a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) 84248a6092fSMaxime Coquelin { 84348a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 84448a6092fSMaxime Coquelin int id; 84548a6092fSMaxime Coquelin 84648a6092fSMaxime Coquelin if (!np) 84748a6092fSMaxime Coquelin return NULL; 84848a6092fSMaxime Coquelin 84948a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 850e5707915SGerald Baeza if (id < 0) { 851e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 852e5707915SGerald Baeza return NULL; 853e5707915SGerald Baeza } 85448a6092fSMaxime Coquelin 85548a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 85648a6092fSMaxime Coquelin return NULL; 85748a6092fSMaxime Coquelin 85848a6092fSMaxime Coquelin stm32_ports[id].hw_flow_control = of_property_read_bool(np, 85959bed2dfSAlexandre TORGUE "st,hw-flow-ctrl"); 86048a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 861e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 86248a6092fSMaxime Coquelin return &stm32_ports[id]; 86348a6092fSMaxime Coquelin } 86448a6092fSMaxime Coquelin 86548a6092fSMaxime Coquelin #ifdef CONFIG_OF 86648a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 867ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 868ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 869270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 87048a6092fSMaxime Coquelin {}, 87148a6092fSMaxime Coquelin }; 87248a6092fSMaxime Coquelin 87348a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 87448a6092fSMaxime Coquelin #endif 87548a6092fSMaxime Coquelin 87634891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port, 87734891872SAlexandre TORGUE struct platform_device *pdev) 87834891872SAlexandre TORGUE { 87934891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 88034891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 88134891872SAlexandre TORGUE struct device *dev = &pdev->dev; 88234891872SAlexandre TORGUE struct dma_slave_config config; 88334891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 88434891872SAlexandre TORGUE dma_cookie_t cookie; 88534891872SAlexandre TORGUE int ret; 88634891872SAlexandre TORGUE 88734891872SAlexandre TORGUE /* Request DMA RX channel */ 88834891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 88934891872SAlexandre TORGUE if (!stm32port->rx_ch) { 89034891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 89134891872SAlexandre TORGUE return -ENODEV; 89234891872SAlexandre TORGUE } 89334891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 89434891872SAlexandre TORGUE &stm32port->rx_dma_buf, 89534891872SAlexandre TORGUE GFP_KERNEL); 89634891872SAlexandre TORGUE if (!stm32port->rx_buf) { 89734891872SAlexandre TORGUE ret = -ENOMEM; 89834891872SAlexandre TORGUE goto alloc_err; 89934891872SAlexandre TORGUE } 90034891872SAlexandre TORGUE 90134891872SAlexandre TORGUE /* Configure DMA channel */ 90234891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 9038e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 90434891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 90534891872SAlexandre TORGUE 90634891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 90734891872SAlexandre TORGUE if (ret < 0) { 90834891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 90934891872SAlexandre TORGUE ret = -ENODEV; 91034891872SAlexandre TORGUE goto config_err; 91134891872SAlexandre TORGUE } 91234891872SAlexandre TORGUE 91334891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 91434891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 91534891872SAlexandre TORGUE stm32port->rx_dma_buf, 91634891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 91734891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 91834891872SAlexandre TORGUE if (!desc) { 91934891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 92034891872SAlexandre TORGUE ret = -ENODEV; 92134891872SAlexandre TORGUE goto config_err; 92234891872SAlexandre TORGUE } 92334891872SAlexandre TORGUE 92434891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 92534891872SAlexandre TORGUE desc->callback = NULL; 92634891872SAlexandre TORGUE desc->callback_param = NULL; 92734891872SAlexandre TORGUE 92834891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 92934891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 93034891872SAlexandre TORGUE 93134891872SAlexandre TORGUE /* Issue pending DMA requests */ 93234891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 93334891872SAlexandre TORGUE 93434891872SAlexandre TORGUE return 0; 93534891872SAlexandre TORGUE 93634891872SAlexandre TORGUE config_err: 93734891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 93834891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 93934891872SAlexandre TORGUE stm32port->rx_dma_buf); 94034891872SAlexandre TORGUE 94134891872SAlexandre TORGUE alloc_err: 94234891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 94334891872SAlexandre TORGUE stm32port->rx_ch = NULL; 94434891872SAlexandre TORGUE 94534891872SAlexandre TORGUE return ret; 94634891872SAlexandre TORGUE } 94734891872SAlexandre TORGUE 94834891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port, 94934891872SAlexandre TORGUE struct platform_device *pdev) 95034891872SAlexandre TORGUE { 95134891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 95234891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 95334891872SAlexandre TORGUE struct device *dev = &pdev->dev; 95434891872SAlexandre TORGUE struct dma_slave_config config; 95534891872SAlexandre TORGUE int ret; 95634891872SAlexandre TORGUE 95734891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 95834891872SAlexandre TORGUE 95934891872SAlexandre TORGUE /* Request DMA TX channel */ 96034891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 96134891872SAlexandre TORGUE if (!stm32port->tx_ch) { 96234891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 96334891872SAlexandre TORGUE return -ENODEV; 96434891872SAlexandre TORGUE } 96534891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 96634891872SAlexandre TORGUE &stm32port->tx_dma_buf, 96734891872SAlexandre TORGUE GFP_KERNEL); 96834891872SAlexandre TORGUE if (!stm32port->tx_buf) { 96934891872SAlexandre TORGUE ret = -ENOMEM; 97034891872SAlexandre TORGUE goto alloc_err; 97134891872SAlexandre TORGUE } 97234891872SAlexandre TORGUE 97334891872SAlexandre TORGUE /* Configure DMA channel */ 97434891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 9758e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 97634891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 97734891872SAlexandre TORGUE 97834891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 97934891872SAlexandre TORGUE if (ret < 0) { 98034891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 98134891872SAlexandre TORGUE ret = -ENODEV; 98234891872SAlexandre TORGUE goto config_err; 98334891872SAlexandre TORGUE } 98434891872SAlexandre TORGUE 98534891872SAlexandre TORGUE return 0; 98634891872SAlexandre TORGUE 98734891872SAlexandre TORGUE config_err: 98834891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 98934891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 99034891872SAlexandre TORGUE stm32port->tx_dma_buf); 99134891872SAlexandre TORGUE 99234891872SAlexandre TORGUE alloc_err: 99334891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 99434891872SAlexandre TORGUE stm32port->tx_ch = NULL; 99534891872SAlexandre TORGUE 99634891872SAlexandre TORGUE return ret; 99734891872SAlexandre TORGUE } 99834891872SAlexandre TORGUE 99948a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev) 100048a6092fSMaxime Coquelin { 1001ada8618fSAlexandre TORGUE const struct of_device_id *match; 100248a6092fSMaxime Coquelin struct stm32_port *stm32port; 1003ada8618fSAlexandre TORGUE int ret; 100448a6092fSMaxime Coquelin 100548a6092fSMaxime Coquelin stm32port = stm32_of_get_stm32_port(pdev); 100648a6092fSMaxime Coquelin if (!stm32port) 100748a6092fSMaxime Coquelin return -ENODEV; 100848a6092fSMaxime Coquelin 1009ada8618fSAlexandre TORGUE match = of_match_device(stm32_match, &pdev->dev); 1010ada8618fSAlexandre TORGUE if (match && match->data) 1011ada8618fSAlexandre TORGUE stm32port->info = (struct stm32_usart_info *)match->data; 1012ada8618fSAlexandre TORGUE else 1013ada8618fSAlexandre TORGUE return -EINVAL; 1014ada8618fSAlexandre TORGUE 101548a6092fSMaxime Coquelin ret = stm32_init_port(stm32port, pdev); 101648a6092fSMaxime Coquelin if (ret) 101748a6092fSMaxime Coquelin return ret; 101848a6092fSMaxime Coquelin 1019270e5a74SFabrice Gasnier if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) { 1020270e5a74SFabrice Gasnier ret = device_init_wakeup(&pdev->dev, true); 102148a6092fSMaxime Coquelin if (ret) 1022ada80043SFabrice Gasnier goto err_uninit; 1023270e5a74SFabrice Gasnier } 1024270e5a74SFabrice Gasnier 1025270e5a74SFabrice Gasnier ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 1026270e5a74SFabrice Gasnier if (ret) 1027270e5a74SFabrice Gasnier goto err_nowup; 102848a6092fSMaxime Coquelin 102934891872SAlexandre TORGUE ret = stm32_of_dma_rx_probe(stm32port, pdev); 103034891872SAlexandre TORGUE if (ret) 103134891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 103234891872SAlexandre TORGUE 103334891872SAlexandre TORGUE ret = stm32_of_dma_tx_probe(stm32port, pdev); 103434891872SAlexandre TORGUE if (ret) 103534891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 103634891872SAlexandre TORGUE 103748a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 103848a6092fSMaxime Coquelin 103948a6092fSMaxime Coquelin return 0; 1040ada80043SFabrice Gasnier 1041270e5a74SFabrice Gasnier err_nowup: 1042270e5a74SFabrice Gasnier if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) 1043270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 1044270e5a74SFabrice Gasnier 1045ada80043SFabrice Gasnier err_uninit: 1046ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 1047ada80043SFabrice Gasnier 1048ada80043SFabrice Gasnier return ret; 104948a6092fSMaxime Coquelin } 105048a6092fSMaxime Coquelin 105148a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev) 105248a6092fSMaxime Coquelin { 105348a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1054511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 105534891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1056270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 105734891872SAlexandre TORGUE 105834891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 105934891872SAlexandre TORGUE 106034891872SAlexandre TORGUE if (stm32_port->rx_ch) 106134891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 106234891872SAlexandre TORGUE 106334891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 106434891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 106534891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 106634891872SAlexandre TORGUE stm32_port->rx_dma_buf); 106734891872SAlexandre TORGUE 106834891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 106934891872SAlexandre TORGUE 107034891872SAlexandre TORGUE if (stm32_port->tx_ch) 107134891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 107234891872SAlexandre TORGUE 107334891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 107434891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 107534891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 107634891872SAlexandre TORGUE stm32_port->tx_dma_buf); 1077511c7b1bSAlexandre TORGUE 1078270e5a74SFabrice Gasnier if (cfg->has_wakeup && stm32_port->wakeirq >= 0) 1079270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 1080270e5a74SFabrice Gasnier 1081511c7b1bSAlexandre TORGUE clk_disable_unprepare(stm32_port->clk); 108248a6092fSMaxime Coquelin 108348a6092fSMaxime Coquelin return uart_remove_one_port(&stm32_usart_driver, port); 108448a6092fSMaxime Coquelin } 108548a6092fSMaxime Coquelin 108648a6092fSMaxime Coquelin 108748a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 108848a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch) 108948a6092fSMaxime Coquelin { 1090ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1091ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1092ada8618fSAlexandre TORGUE 1093ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 109448a6092fSMaxime Coquelin cpu_relax(); 109548a6092fSMaxime Coquelin 1096ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 109748a6092fSMaxime Coquelin } 109848a6092fSMaxime Coquelin 109948a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt) 110048a6092fSMaxime Coquelin { 110148a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1102ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1103ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 110487f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 110548a6092fSMaxime Coquelin unsigned long flags; 110648a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 110748a6092fSMaxime Coquelin int locked = 1; 110848a6092fSMaxime Coquelin 110948a6092fSMaxime Coquelin local_irq_save(flags); 111048a6092fSMaxime Coquelin if (port->sysrq) 111148a6092fSMaxime Coquelin locked = 0; 111248a6092fSMaxime Coquelin else if (oops_in_progress) 111348a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 111448a6092fSMaxime Coquelin else 111548a6092fSMaxime Coquelin spin_lock(&port->lock); 111648a6092fSMaxime Coquelin 111787f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1118ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 111948a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 112087f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1121ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 112248a6092fSMaxime Coquelin 112348a6092fSMaxime Coquelin uart_console_write(port, s, cnt, stm32_console_putchar); 112448a6092fSMaxime Coquelin 112548a6092fSMaxime Coquelin /* Restore interrupt state */ 1126ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 112748a6092fSMaxime Coquelin 112848a6092fSMaxime Coquelin if (locked) 112948a6092fSMaxime Coquelin spin_unlock(&port->lock); 113048a6092fSMaxime Coquelin local_irq_restore(flags); 113148a6092fSMaxime Coquelin } 113248a6092fSMaxime Coquelin 113348a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options) 113448a6092fSMaxime Coquelin { 113548a6092fSMaxime Coquelin struct stm32_port *stm32port; 113648a6092fSMaxime Coquelin int baud = 9600; 113748a6092fSMaxime Coquelin int bits = 8; 113848a6092fSMaxime Coquelin int parity = 'n'; 113948a6092fSMaxime Coquelin int flow = 'n'; 114048a6092fSMaxime Coquelin 114148a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 114248a6092fSMaxime Coquelin return -ENODEV; 114348a6092fSMaxime Coquelin 114448a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 114548a6092fSMaxime Coquelin 114648a6092fSMaxime Coquelin /* 114748a6092fSMaxime Coquelin * This driver does not support early console initialization 114848a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 114948a6092fSMaxime Coquelin * this to be called during the uart port registration when the 115048a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 115148a6092fSMaxime Coquelin */ 115248a6092fSMaxime Coquelin if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) 115348a6092fSMaxime Coquelin return -ENXIO; 115448a6092fSMaxime Coquelin 115548a6092fSMaxime Coquelin if (options) 115648a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 115748a6092fSMaxime Coquelin 115848a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 115948a6092fSMaxime Coquelin } 116048a6092fSMaxime Coquelin 116148a6092fSMaxime Coquelin static struct console stm32_console = { 116248a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 116348a6092fSMaxime Coquelin .device = uart_console_device, 116448a6092fSMaxime Coquelin .write = stm32_console_write, 116548a6092fSMaxime Coquelin .setup = stm32_console_setup, 116648a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 116748a6092fSMaxime Coquelin .index = -1, 116848a6092fSMaxime Coquelin .data = &stm32_usart_driver, 116948a6092fSMaxime Coquelin }; 117048a6092fSMaxime Coquelin 117148a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 117248a6092fSMaxime Coquelin 117348a6092fSMaxime Coquelin #else 117448a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 117548a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 117648a6092fSMaxime Coquelin 117748a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 117848a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 117948a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 118048a6092fSMaxime Coquelin .major = 0, 118148a6092fSMaxime Coquelin .minor = 0, 118248a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 118348a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 118448a6092fSMaxime Coquelin }; 118548a6092fSMaxime Coquelin 1186270e5a74SFabrice Gasnier #ifdef CONFIG_PM_SLEEP 1187270e5a74SFabrice Gasnier static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable) 1188270e5a74SFabrice Gasnier { 1189270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1190270e5a74SFabrice Gasnier struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1191270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1192270e5a74SFabrice Gasnier u32 val; 1193270e5a74SFabrice Gasnier 1194270e5a74SFabrice Gasnier if (!cfg->has_wakeup || stm32_port->wakeirq < 0) 1195270e5a74SFabrice Gasnier return; 1196270e5a74SFabrice Gasnier 1197270e5a74SFabrice Gasnier if (enable) { 1198270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1199270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, USART_CR1_UESM); 1200270e5a74SFabrice Gasnier val = readl_relaxed(port->membase + ofs->cr3); 1201270e5a74SFabrice Gasnier val &= ~USART_CR3_WUS_MASK; 1202270e5a74SFabrice Gasnier /* Enable Wake up interrupt from low power on start bit */ 1203270e5a74SFabrice Gasnier val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE; 1204270e5a74SFabrice Gasnier writel_relaxed(val, port->membase + ofs->cr3); 1205270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1206270e5a74SFabrice Gasnier } else { 1207270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM); 1208270e5a74SFabrice Gasnier } 1209270e5a74SFabrice Gasnier } 1210270e5a74SFabrice Gasnier 1211270e5a74SFabrice Gasnier static int stm32_serial_suspend(struct device *dev) 1212270e5a74SFabrice Gasnier { 1213270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1214270e5a74SFabrice Gasnier 1215270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1216270e5a74SFabrice Gasnier 1217270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1218270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, true); 1219270e5a74SFabrice Gasnier else 1220270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1221270e5a74SFabrice Gasnier 1222270e5a74SFabrice Gasnier return 0; 1223270e5a74SFabrice Gasnier } 1224270e5a74SFabrice Gasnier 1225270e5a74SFabrice Gasnier static int stm32_serial_resume(struct device *dev) 1226270e5a74SFabrice Gasnier { 1227270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1228270e5a74SFabrice Gasnier 1229270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1230270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1231270e5a74SFabrice Gasnier 1232270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1233270e5a74SFabrice Gasnier } 1234270e5a74SFabrice Gasnier #endif /* CONFIG_PM_SLEEP */ 1235270e5a74SFabrice Gasnier 1236270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 1237270e5a74SFabrice Gasnier SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume) 1238270e5a74SFabrice Gasnier }; 1239270e5a74SFabrice Gasnier 124048a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 124148a6092fSMaxime Coquelin .probe = stm32_serial_probe, 124248a6092fSMaxime Coquelin .remove = stm32_serial_remove, 124348a6092fSMaxime Coquelin .driver = { 124448a6092fSMaxime Coquelin .name = DRIVER_NAME, 1245270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 124648a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 124748a6092fSMaxime Coquelin }, 124848a6092fSMaxime Coquelin }; 124948a6092fSMaxime Coquelin 125048a6092fSMaxime Coquelin static int __init usart_init(void) 125148a6092fSMaxime Coquelin { 125248a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 125348a6092fSMaxime Coquelin int ret; 125448a6092fSMaxime Coquelin 125548a6092fSMaxime Coquelin pr_info("%s\n", banner); 125648a6092fSMaxime Coquelin 125748a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 125848a6092fSMaxime Coquelin if (ret) 125948a6092fSMaxime Coquelin return ret; 126048a6092fSMaxime Coquelin 126148a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 126248a6092fSMaxime Coquelin if (ret) 126348a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 126448a6092fSMaxime Coquelin 126548a6092fSMaxime Coquelin return ret; 126648a6092fSMaxime Coquelin } 126748a6092fSMaxime Coquelin 126848a6092fSMaxime Coquelin static void __exit usart_exit(void) 126948a6092fSMaxime Coquelin { 127048a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 127148a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 127248a6092fSMaxime Coquelin } 127348a6092fSMaxime Coquelin 127448a6092fSMaxime Coquelin module_init(usart_init); 127548a6092fSMaxime Coquelin module_exit(usart_exit); 127648a6092fSMaxime Coquelin 127748a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 127848a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 127948a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1280