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 1134891872SAlexandre TORGUE #include <linux/clk.h> 1248a6092fSMaxime Coquelin #include <linux/console.h> 1348a6092fSMaxime Coquelin #include <linux/delay.h> 1434891872SAlexandre TORGUE #include <linux/dma-direction.h> 1534891872SAlexandre TORGUE #include <linux/dmaengine.h> 1634891872SAlexandre TORGUE #include <linux/dma-mapping.h> 1734891872SAlexandre TORGUE #include <linux/io.h> 1834891872SAlexandre TORGUE #include <linux/iopoll.h> 1934891872SAlexandre TORGUE #include <linux/irq.h> 2034891872SAlexandre TORGUE #include <linux/module.h> 2148a6092fSMaxime Coquelin #include <linux/of.h> 2248a6092fSMaxime Coquelin #include <linux/of_platform.h> 2394616d9aSErwan Le Ray #include <linux/pinctrl/consumer.h> 2434891872SAlexandre TORGUE #include <linux/platform_device.h> 2534891872SAlexandre TORGUE #include <linux/pm_runtime.h> 26270e5a74SFabrice Gasnier #include <linux/pm_wakeirq.h> 2748a6092fSMaxime Coquelin #include <linux/serial_core.h> 2834891872SAlexandre TORGUE #include <linux/serial.h> 2934891872SAlexandre TORGUE #include <linux/spinlock.h> 3034891872SAlexandre TORGUE #include <linux/sysrq.h> 3134891872SAlexandre TORGUE #include <linux/tty_flip.h> 3234891872SAlexandre TORGUE #include <linux/tty.h> 3348a6092fSMaxime Coquelin 346cf61b9bSManivannan Sadhasivam #include "serial_mctrl_gpio.h" 35bc5a0b55SAlexandre TORGUE #include "stm32-usart.h" 3648a6092fSMaxime Coquelin 3748a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port); 3834891872SAlexandre TORGUE static void stm32_transmit_chars(struct uart_port *port); 3948a6092fSMaxime Coquelin 4048a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 4148a6092fSMaxime Coquelin { 4248a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 4348a6092fSMaxime Coquelin } 4448a6092fSMaxime Coquelin 4548a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits) 4648a6092fSMaxime Coquelin { 4748a6092fSMaxime Coquelin u32 val; 4848a6092fSMaxime Coquelin 4948a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5048a6092fSMaxime Coquelin val |= bits; 5148a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 5248a6092fSMaxime Coquelin } 5348a6092fSMaxime Coquelin 5448a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits) 5548a6092fSMaxime Coquelin { 5648a6092fSMaxime Coquelin u32 val; 5748a6092fSMaxime Coquelin 5848a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5948a6092fSMaxime Coquelin val &= ~bits; 6048a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 6148a6092fSMaxime Coquelin } 6248a6092fSMaxime Coquelin 631bcda09dSBich HEMON static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, 641bcda09dSBich HEMON u32 delay_DDE, u32 baud) 651bcda09dSBich HEMON { 661bcda09dSBich HEMON u32 rs485_deat_dedt; 671bcda09dSBich HEMON u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); 681bcda09dSBich HEMON bool over8; 691bcda09dSBich HEMON 701bcda09dSBich HEMON *cr3 |= USART_CR3_DEM; 711bcda09dSBich HEMON over8 = *cr1 & USART_CR1_OVER8; 721bcda09dSBich HEMON 731bcda09dSBich HEMON if (over8) 741bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 8; 751bcda09dSBich HEMON else 761bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 16; 771bcda09dSBich HEMON 781bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 791bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 801bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 811bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & 821bcda09dSBich HEMON USART_CR1_DEAT_MASK; 831bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 841bcda09dSBich HEMON 851bcda09dSBich HEMON if (over8) 861bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 8; 871bcda09dSBich HEMON else 881bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 16; 891bcda09dSBich HEMON 901bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 911bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 921bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 931bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & 941bcda09dSBich HEMON USART_CR1_DEDT_MASK; 951bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 961bcda09dSBich HEMON } 971bcda09dSBich HEMON 981bcda09dSBich HEMON static int stm32_config_rs485(struct uart_port *port, 991bcda09dSBich HEMON struct serial_rs485 *rs485conf) 1001bcda09dSBich HEMON { 1011bcda09dSBich HEMON struct stm32_port *stm32_port = to_stm32_port(port); 102*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 103*d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1041bcda09dSBich HEMON u32 usartdiv, baud, cr1, cr3; 1051bcda09dSBich HEMON bool over8; 1061bcda09dSBich HEMON 1071bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1081bcda09dSBich HEMON 1091bcda09dSBich HEMON port->rs485 = *rs485conf; 1101bcda09dSBich HEMON 1111bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RX_DURING_TX; 1121bcda09dSBich HEMON 1131bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 1141bcda09dSBich HEMON cr1 = readl_relaxed(port->membase + ofs->cr1); 1151bcda09dSBich HEMON cr3 = readl_relaxed(port->membase + ofs->cr3); 1161bcda09dSBich HEMON usartdiv = readl_relaxed(port->membase + ofs->brr); 1171bcda09dSBich HEMON usartdiv = usartdiv & GENMASK(15, 0); 1181bcda09dSBich HEMON over8 = cr1 & USART_CR1_OVER8; 1191bcda09dSBich HEMON 1201bcda09dSBich HEMON if (over8) 1211bcda09dSBich HEMON usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) 1221bcda09dSBich HEMON << USART_BRR_04_R_SHIFT; 1231bcda09dSBich HEMON 1241bcda09dSBich HEMON baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); 1251bcda09dSBich HEMON stm32_config_reg_rs485(&cr1, &cr3, 1261bcda09dSBich HEMON rs485conf->delay_rts_before_send, 1271bcda09dSBich HEMON rs485conf->delay_rts_after_send, baud); 1281bcda09dSBich HEMON 1291bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 1301bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 1311bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 1321bcda09dSBich HEMON } else { 1331bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 1341bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 1351bcda09dSBich HEMON } 1361bcda09dSBich HEMON 1371bcda09dSBich HEMON writel_relaxed(cr3, port->membase + ofs->cr3); 1381bcda09dSBich HEMON writel_relaxed(cr1, port->membase + ofs->cr1); 1391bcda09dSBich HEMON } else { 1401bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP); 1411bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr1, 1421bcda09dSBich HEMON USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 1431bcda09dSBich HEMON } 1441bcda09dSBich HEMON 1451bcda09dSBich HEMON stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1461bcda09dSBich HEMON 1471bcda09dSBich HEMON return 0; 1481bcda09dSBich HEMON } 1491bcda09dSBich HEMON 1501bcda09dSBich HEMON static int stm32_init_rs485(struct uart_port *port, 1511bcda09dSBich HEMON struct platform_device *pdev) 1521bcda09dSBich HEMON { 1531bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 1541bcda09dSBich HEMON 1551bcda09dSBich HEMON rs485conf->flags = 0; 1561bcda09dSBich HEMON rs485conf->delay_rts_before_send = 0; 1571bcda09dSBich HEMON rs485conf->delay_rts_after_send = 0; 1581bcda09dSBich HEMON 1591bcda09dSBich HEMON if (!pdev->dev.of_node) 1601bcda09dSBich HEMON return -ENODEV; 1611bcda09dSBich HEMON 162c150c0f3SLukas Wunner return uart_get_rs485_mode(port); 1631bcda09dSBich HEMON } 1641bcda09dSBich HEMON 165b97055bcSBaoyou Xie static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res, 16634891872SAlexandre TORGUE bool threaded) 16734891872SAlexandre TORGUE { 16834891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 169*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 17034891872SAlexandre TORGUE enum dma_status status; 17134891872SAlexandre TORGUE struct dma_tx_state state; 17234891872SAlexandre TORGUE 17334891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 17434891872SAlexandre TORGUE 17534891872SAlexandre TORGUE if (threaded && stm32_port->rx_ch) { 17634891872SAlexandre TORGUE status = dmaengine_tx_status(stm32_port->rx_ch, 17734891872SAlexandre TORGUE stm32_port->rx_ch->cookie, 17834891872SAlexandre TORGUE &state); 17934891872SAlexandre TORGUE if ((status == DMA_IN_PROGRESS) && 18034891872SAlexandre TORGUE (*last_res != state.residue)) 18134891872SAlexandre TORGUE return 1; 18234891872SAlexandre TORGUE else 18334891872SAlexandre TORGUE return 0; 18434891872SAlexandre TORGUE } else if (*sr & USART_SR_RXNE) { 18534891872SAlexandre TORGUE return 1; 18634891872SAlexandre TORGUE } 18734891872SAlexandre TORGUE return 0; 18834891872SAlexandre TORGUE } 18934891872SAlexandre TORGUE 1906c5962f3SErwan Le Ray static unsigned long stm32_get_char(struct uart_port *port, u32 *sr, 1916c5962f3SErwan Le Ray int *last_res) 19234891872SAlexandre TORGUE { 19334891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 194*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 19534891872SAlexandre TORGUE unsigned long c; 19634891872SAlexandre TORGUE 19734891872SAlexandre TORGUE if (stm32_port->rx_ch) { 19834891872SAlexandre TORGUE c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; 19934891872SAlexandre TORGUE if ((*last_res) == 0) 20034891872SAlexandre TORGUE *last_res = RX_BUF_L; 20134891872SAlexandre TORGUE } else { 2026c5962f3SErwan Le Ray c = readl_relaxed(port->membase + ofs->rdr); 2036c5962f3SErwan Le Ray /* apply RDR data mask */ 2046c5962f3SErwan Le Ray c &= stm32_port->rdr_mask; 20534891872SAlexandre TORGUE } 2066c5962f3SErwan Le Ray 2076c5962f3SErwan Le Ray return c; 20834891872SAlexandre TORGUE } 20934891872SAlexandre TORGUE 21034891872SAlexandre TORGUE static void stm32_receive_chars(struct uart_port *port, bool threaded) 21148a6092fSMaxime Coquelin { 21248a6092fSMaxime Coquelin struct tty_port *tport = &port->state->port; 213ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 214*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 21548a6092fSMaxime Coquelin unsigned long c; 21648a6092fSMaxime Coquelin u32 sr; 21748a6092fSMaxime Coquelin char flag; 21848a6092fSMaxime Coquelin 21929d60981SAndy Shevchenko if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 22048a6092fSMaxime Coquelin pm_wakeup_event(tport->tty->dev, 0); 22148a6092fSMaxime Coquelin 222e5707915SGerald Baeza while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) { 22348a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22448a6092fSMaxime Coquelin flag = TTY_NORMAL; 22548a6092fSMaxime Coquelin 2264f01d833SErwan Le Ray /* 2274f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2284f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2294f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2304f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2314f01d833SErwan Le Ray * and clear status bits of the next rx data. 2324f01d833SErwan Le Ray * 2334f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2344f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 2354f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 2364f01d833SErwan Le Ray */ 2374f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 2381250ed71SFabrice Gasnier writel_relaxed(sr & USART_SR_ERR_MASK, 2391250ed71SFabrice Gasnier port->membase + ofs->icr); 2404f01d833SErwan Le Ray 2414f01d833SErwan Le Ray c = stm32_get_char(port, &sr, &stm32_port->last_res); 2424f01d833SErwan Le Ray port->icount.rx++; 24348a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 2444f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 24548a6092fSMaxime Coquelin port->icount.overrun++; 24648a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 24748a6092fSMaxime Coquelin port->icount.parity++; 24848a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 2494f01d833SErwan Le Ray /* Break detection if character is null */ 2504f01d833SErwan Le Ray if (!c) { 2514f01d833SErwan Le Ray port->icount.brk++; 2524f01d833SErwan Le Ray if (uart_handle_break(port)) 2534f01d833SErwan Le Ray continue; 2544f01d833SErwan Le Ray } else { 25548a6092fSMaxime Coquelin port->icount.frame++; 25648a6092fSMaxime Coquelin } 2574f01d833SErwan Le Ray } 25848a6092fSMaxime Coquelin 25948a6092fSMaxime Coquelin sr &= port->read_status_mask; 26048a6092fSMaxime Coquelin 2614f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26248a6092fSMaxime Coquelin flag = TTY_PARITY; 2634f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 2644f01d833SErwan Le Ray if (!c) 2654f01d833SErwan Le Ray flag = TTY_BREAK; 2664f01d833SErwan Le Ray else 26748a6092fSMaxime Coquelin flag = TTY_FRAME; 26848a6092fSMaxime Coquelin } 2694f01d833SErwan Le Ray } 27048a6092fSMaxime Coquelin 27148a6092fSMaxime Coquelin if (uart_handle_sysrq_char(port, c)) 27248a6092fSMaxime Coquelin continue; 27348a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27448a6092fSMaxime Coquelin } 27548a6092fSMaxime Coquelin 27648a6092fSMaxime Coquelin spin_unlock(&port->lock); 27748a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 27848a6092fSMaxime Coquelin spin_lock(&port->lock); 27948a6092fSMaxime Coquelin } 28048a6092fSMaxime Coquelin 28134891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg) 28234891872SAlexandre TORGUE { 28334891872SAlexandre TORGUE struct uart_port *port = arg; 28434891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 285*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 28634891872SAlexandre TORGUE 28734891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 28834891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 28934891872SAlexandre TORGUE 29034891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 29134891872SAlexandre TORGUE stm32_transmit_chars(port); 29234891872SAlexandre TORGUE } 29334891872SAlexandre TORGUE 294d075719eSErwan Le Ray static void stm32_tx_interrupt_enable(struct uart_port *port) 295d075719eSErwan Le Ray { 296d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 297*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 298d075719eSErwan Le Ray 299d075719eSErwan Le Ray /* 300d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 301d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 302d075719eSErwan Le Ray */ 303d075719eSErwan Le Ray if (stm32_port->fifoen) 304d075719eSErwan Le Ray stm32_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 305d075719eSErwan Le Ray else 306d075719eSErwan Le Ray stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 307d075719eSErwan Le Ray } 308d075719eSErwan Le Ray 309d075719eSErwan Le Ray static void stm32_tx_interrupt_disable(struct uart_port *port) 310d075719eSErwan Le Ray { 311d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 312*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 313d075719eSErwan Le Ray 314d075719eSErwan Le Ray if (stm32_port->fifoen) 315d075719eSErwan Le Ray stm32_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 316d075719eSErwan Le Ray else 317d075719eSErwan Le Ray stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 318d075719eSErwan Le Ray } 319d075719eSErwan Le Ray 32034891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port) 32134891872SAlexandre TORGUE { 32234891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 323*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 32434891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 32534891872SAlexandre TORGUE 32634891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 32734891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 32834891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 32934891872SAlexandre TORGUE } 33034891872SAlexandre TORGUE 3315d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 3325d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 3335d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 3345d9176edSErwan Le Ray break; 33534891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 33634891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 33734891872SAlexandre TORGUE port->icount.tx++; 33834891872SAlexandre TORGUE } 33934891872SAlexandre TORGUE 3405d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 3415d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 342d075719eSErwan Le Ray stm32_tx_interrupt_disable(port); 3435d9176edSErwan Le Ray else 344d075719eSErwan Le Ray stm32_tx_interrupt_enable(port); 3455d9176edSErwan Le Ray } 3465d9176edSErwan Le Ray 34734891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port) 34834891872SAlexandre TORGUE { 34934891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 350*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 35134891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 35234891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 35334891872SAlexandre TORGUE unsigned int count, i; 35434891872SAlexandre TORGUE 35534891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 35634891872SAlexandre TORGUE return; 35734891872SAlexandre TORGUE 35834891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 35934891872SAlexandre TORGUE 36034891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 36134891872SAlexandre TORGUE 36234891872SAlexandre TORGUE if (count > TX_BUF_L) 36334891872SAlexandre TORGUE count = TX_BUF_L; 36434891872SAlexandre TORGUE 36534891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 36634891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 36734891872SAlexandre TORGUE } else { 36834891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 36934891872SAlexandre TORGUE size_t two; 37034891872SAlexandre TORGUE 37134891872SAlexandre TORGUE if (one > count) 37234891872SAlexandre TORGUE one = count; 37334891872SAlexandre TORGUE two = count - one; 37434891872SAlexandre TORGUE 37534891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 37634891872SAlexandre TORGUE if (two) 37734891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 37834891872SAlexandre TORGUE } 37934891872SAlexandre TORGUE 38034891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 38134891872SAlexandre TORGUE stm32port->tx_dma_buf, 38234891872SAlexandre TORGUE count, 38334891872SAlexandre TORGUE DMA_MEM_TO_DEV, 38434891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 38534891872SAlexandre TORGUE 38634891872SAlexandre TORGUE if (!desc) { 38734891872SAlexandre TORGUE for (i = count; i > 0; i--) 38834891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 38934891872SAlexandre TORGUE return; 39034891872SAlexandre TORGUE } 39134891872SAlexandre TORGUE 39234891872SAlexandre TORGUE desc->callback = stm32_tx_dma_complete; 39334891872SAlexandre TORGUE desc->callback_param = port; 39434891872SAlexandre TORGUE 39534891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 39624832ca3SLee Jones dmaengine_submit(desc); 39734891872SAlexandre TORGUE 39834891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 39934891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 40034891872SAlexandre TORGUE 40134891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 40234891872SAlexandre TORGUE 40334891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 40434891872SAlexandre TORGUE port->icount.tx += count; 40534891872SAlexandre TORGUE } 40634891872SAlexandre TORGUE 40748a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port) 40848a6092fSMaxime Coquelin { 409ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 410*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 41148a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 41248a6092fSMaxime Coquelin 41348a6092fSMaxime Coquelin if (port->x_char) { 41434891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 41534891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 416ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 41748a6092fSMaxime Coquelin port->x_char = 0; 41848a6092fSMaxime Coquelin port->icount.tx++; 41934891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 42034891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 42148a6092fSMaxime Coquelin return; 42248a6092fSMaxime Coquelin } 42348a6092fSMaxime Coquelin 424b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 425d075719eSErwan Le Ray stm32_tx_interrupt_disable(port); 42648a6092fSMaxime Coquelin return; 42748a6092fSMaxime Coquelin } 42848a6092fSMaxime Coquelin 42964c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 43064c32eabSErwan Le Ray stm32_clr_bits(port, ofs->isr, USART_SR_TC); 43164c32eabSErwan Le Ray else 4321250ed71SFabrice Gasnier writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr); 43364c32eabSErwan Le Ray 43434891872SAlexandre TORGUE if (stm32_port->tx_ch) 43534891872SAlexandre TORGUE stm32_transmit_chars_dma(port); 43634891872SAlexandre TORGUE else 43734891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 43848a6092fSMaxime Coquelin 43948a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 44048a6092fSMaxime Coquelin uart_write_wakeup(port); 44148a6092fSMaxime Coquelin 44248a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 443d075719eSErwan Le Ray stm32_tx_interrupt_disable(port); 44448a6092fSMaxime Coquelin } 44548a6092fSMaxime Coquelin 44648a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr) 44748a6092fSMaxime Coquelin { 44848a6092fSMaxime Coquelin struct uart_port *port = ptr; 449ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 450*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 45148a6092fSMaxime Coquelin u32 sr; 45248a6092fSMaxime Coquelin 45301d32d71SAlexandre TORGUE spin_lock(&port->lock); 45401d32d71SAlexandre TORGUE 455ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 45648a6092fSMaxime Coquelin 4574cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 4584cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 4594cc0ed62SErwan Le Ray port->membase + ofs->icr); 4604cc0ed62SErwan Le Ray 461270e5a74SFabrice Gasnier if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG)) 462270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 463270e5a74SFabrice Gasnier port->membase + ofs->icr); 464270e5a74SFabrice Gasnier 46534891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 46634891872SAlexandre TORGUE stm32_receive_chars(port, false); 46748a6092fSMaxime Coquelin 46834891872SAlexandre TORGUE if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) 46948a6092fSMaxime Coquelin stm32_transmit_chars(port); 47048a6092fSMaxime Coquelin 47101d32d71SAlexandre TORGUE spin_unlock(&port->lock); 47201d32d71SAlexandre TORGUE 47334891872SAlexandre TORGUE if (stm32_port->rx_ch) 47434891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 47534891872SAlexandre TORGUE else 47634891872SAlexandre TORGUE return IRQ_HANDLED; 47734891872SAlexandre TORGUE } 47834891872SAlexandre TORGUE 47934891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr) 48034891872SAlexandre TORGUE { 48134891872SAlexandre TORGUE struct uart_port *port = ptr; 48234891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 48334891872SAlexandre TORGUE 48434891872SAlexandre TORGUE spin_lock(&port->lock); 48534891872SAlexandre TORGUE 48634891872SAlexandre TORGUE if (stm32_port->rx_ch) 48734891872SAlexandre TORGUE stm32_receive_chars(port, true); 48834891872SAlexandre TORGUE 48948a6092fSMaxime Coquelin spin_unlock(&port->lock); 49048a6092fSMaxime Coquelin 49148a6092fSMaxime Coquelin return IRQ_HANDLED; 49248a6092fSMaxime Coquelin } 49348a6092fSMaxime Coquelin 49448a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port) 49548a6092fSMaxime Coquelin { 496ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 497*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 498ada8618fSAlexandre TORGUE 499ada8618fSAlexandre TORGUE return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; 50048a6092fSMaxime Coquelin } 50148a6092fSMaxime Coquelin 50248a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) 50348a6092fSMaxime Coquelin { 504ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 505*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 506ada8618fSAlexandre TORGUE 50748a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 508ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); 50948a6092fSMaxime Coquelin else 510ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 5116cf61b9bSManivannan Sadhasivam 5126cf61b9bSManivannan Sadhasivam mctrl_gpio_set(stm32_port->gpios, mctrl); 51348a6092fSMaxime Coquelin } 51448a6092fSMaxime Coquelin 51548a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port) 51648a6092fSMaxime Coquelin { 5176cf61b9bSManivannan Sadhasivam struct stm32_port *stm32_port = to_stm32_port(port); 5186cf61b9bSManivannan Sadhasivam unsigned int ret; 5196cf61b9bSManivannan Sadhasivam 52048a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 5216cf61b9bSManivannan Sadhasivam ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 5226cf61b9bSManivannan Sadhasivam 5236cf61b9bSManivannan Sadhasivam return mctrl_gpio_get(stm32_port->gpios, &ret); 5246cf61b9bSManivannan Sadhasivam } 5256cf61b9bSManivannan Sadhasivam 5266cf61b9bSManivannan Sadhasivam static void stm32_enable_ms(struct uart_port *port) 5276cf61b9bSManivannan Sadhasivam { 5286cf61b9bSManivannan Sadhasivam mctrl_gpio_enable_ms(to_stm32_port(port)->gpios); 5296cf61b9bSManivannan Sadhasivam } 5306cf61b9bSManivannan Sadhasivam 5316cf61b9bSManivannan Sadhasivam static void stm32_disable_ms(struct uart_port *port) 5326cf61b9bSManivannan Sadhasivam { 5336cf61b9bSManivannan Sadhasivam mctrl_gpio_disable_ms(to_stm32_port(port)->gpios); 53448a6092fSMaxime Coquelin } 53548a6092fSMaxime Coquelin 53648a6092fSMaxime Coquelin /* Transmit stop */ 53748a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port) 53848a6092fSMaxime Coquelin { 539ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 540ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 541ad0c2748SMarek Vasut 542d075719eSErwan Le Ray stm32_tx_interrupt_disable(port); 543ad0c2748SMarek Vasut 544ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 545ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 546ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 547ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 548ad0c2748SMarek Vasut } else { 549ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 550ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 551ad0c2748SMarek Vasut } 552ad0c2748SMarek Vasut } 55348a6092fSMaxime Coquelin } 55448a6092fSMaxime Coquelin 55548a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 55648a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port) 55748a6092fSMaxime Coquelin { 558ad0c2748SMarek Vasut struct stm32_port *stm32_port = to_stm32_port(port); 559ad0c2748SMarek Vasut struct serial_rs485 *rs485conf = &port->rs485; 56048a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 56148a6092fSMaxime Coquelin 56248a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 56348a6092fSMaxime Coquelin return; 56448a6092fSMaxime Coquelin 565ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_ENABLED) { 566ad0c2748SMarek Vasut if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 567ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 568ad0c2748SMarek Vasut stm32_port->port.mctrl | TIOCM_RTS); 569ad0c2748SMarek Vasut } else { 570ad0c2748SMarek Vasut mctrl_gpio_set(stm32_port->gpios, 571ad0c2748SMarek Vasut stm32_port->port.mctrl & ~TIOCM_RTS); 572ad0c2748SMarek Vasut } 573ad0c2748SMarek Vasut } 574ad0c2748SMarek Vasut 57534891872SAlexandre TORGUE stm32_transmit_chars(port); 57648a6092fSMaxime Coquelin } 57748a6092fSMaxime Coquelin 57848a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 57948a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port) 58048a6092fSMaxime Coquelin { 581ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 582*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 58348a6092fSMaxime Coquelin unsigned long flags; 58448a6092fSMaxime Coquelin 58548a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 5864cc0ed62SErwan Le Ray stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 587d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 588d0a6a7bcSErwan Le Ray stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 589d0a6a7bcSErwan Le Ray 59048a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 59148a6092fSMaxime Coquelin } 59248a6092fSMaxime Coquelin 59348a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 59448a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port) 59548a6092fSMaxime Coquelin { 596ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 597*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 59848a6092fSMaxime Coquelin unsigned long flags; 59948a6092fSMaxime Coquelin 60048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 6014cc0ed62SErwan Le Ray stm32_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 602d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 603d0a6a7bcSErwan Le Ray stm32_set_bits(port, ofs->cr3, stm32_port->cr3_irq); 604d0a6a7bcSErwan Le Ray 60548a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 60648a6092fSMaxime Coquelin } 60748a6092fSMaxime Coquelin 60848a6092fSMaxime Coquelin /* Receive stop */ 60948a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port) 61048a6092fSMaxime Coquelin { 611ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 612*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 613ada8618fSAlexandre TORGUE 6144cc0ed62SErwan Le Ray stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 615d0a6a7bcSErwan Le Ray if (stm32_port->cr3_irq) 616d0a6a7bcSErwan Le Ray stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq); 617d0a6a7bcSErwan Le Ray 61848a6092fSMaxime Coquelin } 61948a6092fSMaxime Coquelin 62048a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 62148a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state) 62248a6092fSMaxime Coquelin { 62348a6092fSMaxime Coquelin } 62448a6092fSMaxime Coquelin 62548a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port) 62648a6092fSMaxime Coquelin { 627ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 628*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 62948a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 63048a6092fSMaxime Coquelin u32 val; 63148a6092fSMaxime Coquelin int ret; 63248a6092fSMaxime Coquelin 63334891872SAlexandre TORGUE ret = request_threaded_irq(port->irq, stm32_interrupt, 63434891872SAlexandre TORGUE stm32_threaded_interrupt, 63534891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 63648a6092fSMaxime Coquelin if (ret) 63748a6092fSMaxime Coquelin return ret; 63848a6092fSMaxime Coquelin 63984872dc4SErwan Le Ray /* RX FIFO Flush */ 64084872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 64184872dc4SErwan Le Ray stm32_set_bits(port, ofs->rqr, USART_RQR_RXFRQ); 64248a6092fSMaxime Coquelin 64384872dc4SErwan Le Ray /* Tx and RX FIFO configuration */ 644d075719eSErwan Le Ray if (stm32_port->fifoen) { 645d075719eSErwan Le Ray val = readl_relaxed(port->membase + ofs->cr3); 646d0a6a7bcSErwan Le Ray val &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK); 647d075719eSErwan Le Ray val |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT; 648d0a6a7bcSErwan Le Ray val |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT; 649d075719eSErwan Le Ray writel_relaxed(val, port->membase + ofs->cr3); 650d075719eSErwan Le Ray } 651d075719eSErwan Le Ray 65284872dc4SErwan Le Ray /* RX FIFO enabling */ 65384872dc4SErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_RE; 65484872dc4SErwan Le Ray if (stm32_port->fifoen) 65584872dc4SErwan Le Ray val |= USART_CR1_FIFOEN; 65684872dc4SErwan Le Ray stm32_set_bits(port, ofs->cr1, val); 65784872dc4SErwan Le Ray 65848a6092fSMaxime Coquelin return 0; 65948a6092fSMaxime Coquelin } 66048a6092fSMaxime Coquelin 66148a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port) 66248a6092fSMaxime Coquelin { 663ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 664*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 665*d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 66664c32eabSErwan Le Ray u32 val, isr; 66764c32eabSErwan Le Ray int ret; 66848a6092fSMaxime Coquelin 6696cf61b9bSManivannan Sadhasivam /* Disable modem control interrupts */ 6706cf61b9bSManivannan Sadhasivam stm32_disable_ms(port); 6716cf61b9bSManivannan Sadhasivam 6724cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 6734cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 67487f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 675351a762aSGerald Baeza if (stm32_port->fifoen) 676351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 67764c32eabSErwan Le Ray 67864c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 67964c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 68064c32eabSErwan Le Ray 10, 100000); 68164c32eabSErwan Le Ray 68264c32eabSErwan Le Ray if (ret) 68364c32eabSErwan Le Ray dev_err(port->dev, "transmission complete not set\n"); 68464c32eabSErwan Le Ray 685a14f66a4SAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, val); 68648a6092fSMaxime Coquelin 68748a6092fSMaxime Coquelin free_irq(port->irq, port); 68848a6092fSMaxime Coquelin } 68948a6092fSMaxime Coquelin 690929ffa4aSYueHaibing static unsigned int stm32_get_databits(struct ktermios *termios) 691c8a9d043SErwan Le Ray { 692c8a9d043SErwan Le Ray unsigned int bits; 693c8a9d043SErwan Le Ray 694c8a9d043SErwan Le Ray tcflag_t cflag = termios->c_cflag; 695c8a9d043SErwan Le Ray 696c8a9d043SErwan Le Ray switch (cflag & CSIZE) { 697c8a9d043SErwan Le Ray /* 698c8a9d043SErwan Le Ray * CSIZE settings are not necessarily supported in hardware. 699c8a9d043SErwan Le Ray * CSIZE unsupported configurations are handled here to set word length 700c8a9d043SErwan Le Ray * to 8 bits word as default configuration and to print debug message. 701c8a9d043SErwan Le Ray */ 702c8a9d043SErwan Le Ray case CS5: 703c8a9d043SErwan Le Ray bits = 5; 704c8a9d043SErwan Le Ray break; 705c8a9d043SErwan Le Ray case CS6: 706c8a9d043SErwan Le Ray bits = 6; 707c8a9d043SErwan Le Ray break; 708c8a9d043SErwan Le Ray case CS7: 709c8a9d043SErwan Le Ray bits = 7; 710c8a9d043SErwan Le Ray break; 711c8a9d043SErwan Le Ray /* default including CS8 */ 712c8a9d043SErwan Le Ray default: 713c8a9d043SErwan Le Ray bits = 8; 714c8a9d043SErwan Le Ray break; 715c8a9d043SErwan Le Ray } 716c8a9d043SErwan Le Ray 717c8a9d043SErwan Le Ray return bits; 718c8a9d043SErwan Le Ray } 719c8a9d043SErwan Le Ray 72048a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, 72148a6092fSMaxime Coquelin struct ktermios *old) 72248a6092fSMaxime Coquelin { 72348a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 724*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 725*d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 7261bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 727c8a9d043SErwan Le Ray unsigned int baud, bits; 72848a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 72948a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 73048a6092fSMaxime Coquelin u32 cr1, cr2, cr3; 73148a6092fSMaxime Coquelin unsigned long flags; 73248a6092fSMaxime Coquelin 73348a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 73448a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 73548a6092fSMaxime Coquelin 73648a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 73748a6092fSMaxime Coquelin 73848a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 73948a6092fSMaxime Coquelin 74048a6092fSMaxime Coquelin /* Stop serial port and reset value */ 741ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 74248a6092fSMaxime Coquelin 74384872dc4SErwan Le Ray /* flush RX & TX FIFO */ 74484872dc4SErwan Le Ray if (ofs->rqr != UNDEF_REG) 74584872dc4SErwan Le Ray stm32_set_bits(port, ofs->rqr, 74684872dc4SErwan Le Ray USART_RQR_TXFRQ | USART_RQR_RXFRQ); 7471bcda09dSBich HEMON 74884872dc4SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 749351a762aSGerald Baeza if (stm32_port->fifoen) 750351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 75148a6092fSMaxime Coquelin cr2 = 0; 752d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 753d0a6a7bcSErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTCFG_MASK | USART_CR3_RXFTIE 754d075719eSErwan Le Ray | USART_CR3_TXFTCFG_MASK; 75548a6092fSMaxime Coquelin 75648a6092fSMaxime Coquelin if (cflag & CSTOPB) 75748a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 75848a6092fSMaxime Coquelin 759c8a9d043SErwan Le Ray bits = stm32_get_databits(termios); 7606c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 761c8a9d043SErwan Le Ray 76248a6092fSMaxime Coquelin if (cflag & PARENB) { 763c8a9d043SErwan Le Ray bits++; 76448a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 765c8a9d043SErwan Le Ray } 766c8a9d043SErwan Le Ray 767c8a9d043SErwan Le Ray /* 768c8a9d043SErwan Le Ray * Word length configuration: 769c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 770c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 771c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 772c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 773c8a9d043SErwan Le Ray */ 774c8a9d043SErwan Le Ray if (bits == 9) 775ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 776c8a9d043SErwan Le Ray else if ((bits == 7) && cfg->has_7bits_data) 777c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 778c8a9d043SErwan Le Ray else if (bits != 8) 779c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 780c8a9d043SErwan Le Ray , bits); 78148a6092fSMaxime Coquelin 7824cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 7834cc0ed62SErwan Le Ray stm32_port->fifoen)) { 7844cc0ed62SErwan Le Ray if (cflag & CSTOPB) 7854cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 7864cc0ed62SErwan Le Ray else 7874cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 7884cc0ed62SErwan Le Ray 7894cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 7904cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 7914cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 7924cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 793d0a6a7bcSErwan Le Ray /* Not using dma, enable fifo threshold irq */ 794d0a6a7bcSErwan Le Ray if (!stm32_port->rx_ch) 795d0a6a7bcSErwan Le Ray stm32_port->cr3_irq = USART_CR3_RXFTIE; 7964cc0ed62SErwan Le Ray } 7974cc0ed62SErwan Le Ray 798d0a6a7bcSErwan Le Ray cr1 |= stm32_port->cr1_irq; 799d0a6a7bcSErwan Le Ray cr3 |= stm32_port->cr3_irq; 800d0a6a7bcSErwan Le Ray 80148a6092fSMaxime Coquelin if (cflag & PARODD) 80248a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 80348a6092fSMaxime Coquelin 80448a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 80548a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 80648a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 80735abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 80848a6092fSMaxime Coquelin } 80948a6092fSMaxime Coquelin 8106cf61b9bSManivannan Sadhasivam /* Handle modem control interrupts */ 8116cf61b9bSManivannan Sadhasivam if (UART_ENABLE_MS(port, termios->c_cflag)) 8126cf61b9bSManivannan Sadhasivam stm32_enable_ms(port); 8136cf61b9bSManivannan Sadhasivam else 8146cf61b9bSManivannan Sadhasivam stm32_disable_ms(port); 8156cf61b9bSManivannan Sadhasivam 81648a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 81748a6092fSMaxime Coquelin 81848a6092fSMaxime Coquelin /* 81948a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 82048a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 82148a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 82248a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 82348a6092fSMaxime Coquelin */ 82448a6092fSMaxime Coquelin if (usartdiv < 16) { 82548a6092fSMaxime Coquelin oversampling = 8; 8261bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 827ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); 82848a6092fSMaxime Coquelin } else { 82948a6092fSMaxime Coquelin oversampling = 16; 8301bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 831ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 83248a6092fSMaxime Coquelin } 83348a6092fSMaxime Coquelin 83448a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 83548a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 836ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 83748a6092fSMaxime Coquelin 83848a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 83948a6092fSMaxime Coquelin 84048a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 84148a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 84248a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 84348a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 8444f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 84548a6092fSMaxime Coquelin 84648a6092fSMaxime Coquelin /* Characters to ignore */ 84748a6092fSMaxime Coquelin port->ignore_status_mask = 0; 84848a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 84948a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 85048a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 8514f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 85248a6092fSMaxime Coquelin /* 85348a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 85448a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 85548a6092fSMaxime Coquelin */ 85648a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 85748a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 85848a6092fSMaxime Coquelin } 85948a6092fSMaxime Coquelin 86048a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 86148a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 86248a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 86348a6092fSMaxime Coquelin 86434891872SAlexandre TORGUE if (stm32_port->rx_ch) 86534891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 86634891872SAlexandre TORGUE 8671bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 8681bcda09dSBich HEMON stm32_config_reg_rs485(&cr1, &cr3, 8691bcda09dSBich HEMON rs485conf->delay_rts_before_send, 8701bcda09dSBich HEMON rs485conf->delay_rts_after_send, baud); 8711bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 8721bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 8731bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 8741bcda09dSBich HEMON } else { 8751bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 8761bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 8771bcda09dSBich HEMON } 8781bcda09dSBich HEMON 8791bcda09dSBich HEMON } else { 8801bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 8811bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 8821bcda09dSBich HEMON } 8831bcda09dSBich HEMON 884ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 885ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 886ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 88748a6092fSMaxime Coquelin 8881bcda09dSBich HEMON stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 88948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 89048a6092fSMaxime Coquelin } 89148a6092fSMaxime Coquelin 89248a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port) 89348a6092fSMaxime Coquelin { 89448a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 89548a6092fSMaxime Coquelin } 89648a6092fSMaxime Coquelin 89748a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port) 89848a6092fSMaxime Coquelin { 89948a6092fSMaxime Coquelin } 90048a6092fSMaxime Coquelin 90148a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port) 90248a6092fSMaxime Coquelin { 90348a6092fSMaxime Coquelin return 0; 90448a6092fSMaxime Coquelin } 90548a6092fSMaxime Coquelin 90648a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags) 90748a6092fSMaxime Coquelin { 90848a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 90948a6092fSMaxime Coquelin port->type = PORT_STM32; 91048a6092fSMaxime Coquelin } 91148a6092fSMaxime Coquelin 91248a6092fSMaxime Coquelin static int 91348a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser) 91448a6092fSMaxime Coquelin { 91548a6092fSMaxime Coquelin /* No user changeable parameters */ 91648a6092fSMaxime Coquelin return -EINVAL; 91748a6092fSMaxime Coquelin } 91848a6092fSMaxime Coquelin 91948a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state, 92048a6092fSMaxime Coquelin unsigned int oldstate) 92148a6092fSMaxime Coquelin { 92248a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 92348a6092fSMaxime Coquelin struct stm32_port, port); 924*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 925*d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32port->info->cfg; 92648a6092fSMaxime Coquelin unsigned long flags = 0; 92748a6092fSMaxime Coquelin 92848a6092fSMaxime Coquelin switch (state) { 92948a6092fSMaxime Coquelin case UART_PM_STATE_ON: 930fb6dcef6SErwan Le Ray pm_runtime_get_sync(port->dev); 93148a6092fSMaxime Coquelin break; 93248a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 93348a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 934ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 93548a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 936fb6dcef6SErwan Le Ray pm_runtime_put_sync(port->dev); 93748a6092fSMaxime Coquelin break; 93848a6092fSMaxime Coquelin } 93948a6092fSMaxime Coquelin } 94048a6092fSMaxime Coquelin 94148a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 94248a6092fSMaxime Coquelin .tx_empty = stm32_tx_empty, 94348a6092fSMaxime Coquelin .set_mctrl = stm32_set_mctrl, 94448a6092fSMaxime Coquelin .get_mctrl = stm32_get_mctrl, 94548a6092fSMaxime Coquelin .stop_tx = stm32_stop_tx, 94648a6092fSMaxime Coquelin .start_tx = stm32_start_tx, 94748a6092fSMaxime Coquelin .throttle = stm32_throttle, 94848a6092fSMaxime Coquelin .unthrottle = stm32_unthrottle, 94948a6092fSMaxime Coquelin .stop_rx = stm32_stop_rx, 9506cf61b9bSManivannan Sadhasivam .enable_ms = stm32_enable_ms, 95148a6092fSMaxime Coquelin .break_ctl = stm32_break_ctl, 95248a6092fSMaxime Coquelin .startup = stm32_startup, 95348a6092fSMaxime Coquelin .shutdown = stm32_shutdown, 95448a6092fSMaxime Coquelin .set_termios = stm32_set_termios, 95548a6092fSMaxime Coquelin .pm = stm32_pm, 95648a6092fSMaxime Coquelin .type = stm32_type, 95748a6092fSMaxime Coquelin .release_port = stm32_release_port, 95848a6092fSMaxime Coquelin .request_port = stm32_request_port, 95948a6092fSMaxime Coquelin .config_port = stm32_config_port, 96048a6092fSMaxime Coquelin .verify_port = stm32_verify_port, 96148a6092fSMaxime Coquelin }; 96248a6092fSMaxime Coquelin 96348a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port, 96448a6092fSMaxime Coquelin struct platform_device *pdev) 96548a6092fSMaxime Coquelin { 96648a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 96748a6092fSMaxime Coquelin struct resource *res; 96848a6092fSMaxime Coquelin int ret; 96948a6092fSMaxime Coquelin 97048a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 97148a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 97248a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 97348a6092fSMaxime Coquelin port->dev = &pdev->dev; 974d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 9759feedaa7SDmitry Safonov port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE); 9762c58e560SErwan Le Ray 9772c58e560SErwan Le Ray ret = platform_get_irq(pdev, 0); 9781df21786SStephen Boyd if (ret <= 0) 9791df21786SStephen Boyd return ret ? : -ENODEV; 9802c58e560SErwan Le Ray port->irq = ret; 9812c58e560SErwan Le Ray 9827d8f6861SBich HEMON port->rs485_config = stm32_config_rs485; 9837d8f6861SBich HEMON 984c150c0f3SLukas Wunner ret = stm32_init_rs485(port, pdev); 985c150c0f3SLukas Wunner if (ret) 986c150c0f3SLukas Wunner return ret; 9877d8f6861SBich HEMON 9882c58e560SErwan Le Ray if (stm32port->info->cfg.has_wakeup) { 989fdf16d78SHolger Assmann stm32port->wakeirq = platform_get_irq_optional(pdev, 1); 9901df21786SStephen Boyd if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO) 9911df21786SStephen Boyd return stm32port->wakeirq ? : -ENODEV; 9922c58e560SErwan Le Ray } 9932c58e560SErwan Le Ray 994351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 99548a6092fSMaxime Coquelin 99648a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 99748a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 99848a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 99948a6092fSMaxime Coquelin return PTR_ERR(port->membase); 100048a6092fSMaxime Coquelin port->mapbase = res->start; 100148a6092fSMaxime Coquelin 100248a6092fSMaxime Coquelin spin_lock_init(&port->lock); 100348a6092fSMaxime Coquelin 100448a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 100548a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 100648a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 100748a6092fSMaxime Coquelin 100848a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 100948a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 101048a6092fSMaxime Coquelin if (ret) 101148a6092fSMaxime Coquelin return ret; 101248a6092fSMaxime Coquelin 101348a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 1014ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 101548a6092fSMaxime Coquelin ret = -EINVAL; 10166cf61b9bSManivannan Sadhasivam goto err_clk; 1017ada80043SFabrice Gasnier } 101848a6092fSMaxime Coquelin 10196cf61b9bSManivannan Sadhasivam stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0); 10206cf61b9bSManivannan Sadhasivam if (IS_ERR(stm32port->gpios)) { 10216cf61b9bSManivannan Sadhasivam ret = PTR_ERR(stm32port->gpios); 10226cf61b9bSManivannan Sadhasivam goto err_clk; 10236cf61b9bSManivannan Sadhasivam } 10246cf61b9bSManivannan Sadhasivam 10256cf61b9bSManivannan Sadhasivam /* Both CTS/RTS gpios and "st,hw-flow-ctrl" should not be specified */ 10266cf61b9bSManivannan Sadhasivam if (stm32port->hw_flow_control) { 10276cf61b9bSManivannan Sadhasivam if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) || 10286cf61b9bSManivannan Sadhasivam mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) { 10296cf61b9bSManivannan Sadhasivam dev_err(&pdev->dev, "Conflicting RTS/CTS config\n"); 10306cf61b9bSManivannan Sadhasivam ret = -EINVAL; 10316cf61b9bSManivannan Sadhasivam goto err_clk; 10326cf61b9bSManivannan Sadhasivam } 10336cf61b9bSManivannan Sadhasivam } 10346cf61b9bSManivannan Sadhasivam 10356cf61b9bSManivannan Sadhasivam return ret; 10366cf61b9bSManivannan Sadhasivam 10376cf61b9bSManivannan Sadhasivam err_clk: 10386cf61b9bSManivannan Sadhasivam clk_disable_unprepare(stm32port->clk); 10396cf61b9bSManivannan Sadhasivam 104048a6092fSMaxime Coquelin return ret; 104148a6092fSMaxime Coquelin } 104248a6092fSMaxime Coquelin 104348a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) 104448a6092fSMaxime Coquelin { 104548a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 104648a6092fSMaxime Coquelin int id; 104748a6092fSMaxime Coquelin 104848a6092fSMaxime Coquelin if (!np) 104948a6092fSMaxime Coquelin return NULL; 105048a6092fSMaxime Coquelin 105148a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 1052e5707915SGerald Baeza if (id < 0) { 1053e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 1054e5707915SGerald Baeza return NULL; 1055e5707915SGerald Baeza } 105648a6092fSMaxime Coquelin 105748a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 105848a6092fSMaxime Coquelin return NULL; 105948a6092fSMaxime Coquelin 10606fd9fffbSErwan Le Ray stm32_ports[id].hw_flow_control = 10616fd9fffbSErwan Le Ray of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ || 10626fd9fffbSErwan Le Ray of_property_read_bool (np, "uart-has-rtscts"); 106348a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 10644cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 1065d0a6a7bcSErwan Le Ray stm32_ports[id].cr3_irq = 0; 1066e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 106748a6092fSMaxime Coquelin return &stm32_ports[id]; 106848a6092fSMaxime Coquelin } 106948a6092fSMaxime Coquelin 107048a6092fSMaxime Coquelin #ifdef CONFIG_OF 107148a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 1072ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 1073ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 1074270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 107548a6092fSMaxime Coquelin {}, 107648a6092fSMaxime Coquelin }; 107748a6092fSMaxime Coquelin 107848a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 107948a6092fSMaxime Coquelin #endif 108048a6092fSMaxime Coquelin 108134891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port, 108234891872SAlexandre TORGUE struct platform_device *pdev) 108334891872SAlexandre TORGUE { 1084*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 108534891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 108634891872SAlexandre TORGUE struct device *dev = &pdev->dev; 108734891872SAlexandre TORGUE struct dma_slave_config config; 108834891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 108934891872SAlexandre TORGUE int ret; 109034891872SAlexandre TORGUE 109134891872SAlexandre TORGUE /* Request DMA RX channel */ 109234891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 109334891872SAlexandre TORGUE if (!stm32port->rx_ch) { 109434891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 109534891872SAlexandre TORGUE return -ENODEV; 109634891872SAlexandre TORGUE } 109734891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 109834891872SAlexandre TORGUE &stm32port->rx_dma_buf, 109934891872SAlexandre TORGUE GFP_KERNEL); 110034891872SAlexandre TORGUE if (!stm32port->rx_buf) { 110134891872SAlexandre TORGUE ret = -ENOMEM; 110234891872SAlexandre TORGUE goto alloc_err; 110334891872SAlexandre TORGUE } 110434891872SAlexandre TORGUE 110534891872SAlexandre TORGUE /* Configure DMA channel */ 110634891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 11078e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 110834891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 110934891872SAlexandre TORGUE 111034891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 111134891872SAlexandre TORGUE if (ret < 0) { 111234891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 111334891872SAlexandre TORGUE ret = -ENODEV; 111434891872SAlexandre TORGUE goto config_err; 111534891872SAlexandre TORGUE } 111634891872SAlexandre TORGUE 111734891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 111834891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 111934891872SAlexandre TORGUE stm32port->rx_dma_buf, 112034891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 112134891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 112234891872SAlexandre TORGUE if (!desc) { 112334891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 112434891872SAlexandre TORGUE ret = -ENODEV; 112534891872SAlexandre TORGUE goto config_err; 112634891872SAlexandre TORGUE } 112734891872SAlexandre TORGUE 112834891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 112934891872SAlexandre TORGUE desc->callback = NULL; 113034891872SAlexandre TORGUE desc->callback_param = NULL; 113134891872SAlexandre TORGUE 113234891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 113324832ca3SLee Jones dmaengine_submit(desc); 113434891872SAlexandre TORGUE 113534891872SAlexandre TORGUE /* Issue pending DMA requests */ 113634891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 113734891872SAlexandre TORGUE 113834891872SAlexandre TORGUE return 0; 113934891872SAlexandre TORGUE 114034891872SAlexandre TORGUE config_err: 114134891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 114234891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 114334891872SAlexandre TORGUE stm32port->rx_dma_buf); 114434891872SAlexandre TORGUE 114534891872SAlexandre TORGUE alloc_err: 114634891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 114734891872SAlexandre TORGUE stm32port->rx_ch = NULL; 114834891872SAlexandre TORGUE 114934891872SAlexandre TORGUE return ret; 115034891872SAlexandre TORGUE } 115134891872SAlexandre TORGUE 115234891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port, 115334891872SAlexandre TORGUE struct platform_device *pdev) 115434891872SAlexandre TORGUE { 1155*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 115634891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 115734891872SAlexandre TORGUE struct device *dev = &pdev->dev; 115834891872SAlexandre TORGUE struct dma_slave_config config; 115934891872SAlexandre TORGUE int ret; 116034891872SAlexandre TORGUE 116134891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 116234891872SAlexandre TORGUE 116334891872SAlexandre TORGUE /* Request DMA TX channel */ 116434891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 116534891872SAlexandre TORGUE if (!stm32port->tx_ch) { 116634891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 116734891872SAlexandre TORGUE return -ENODEV; 116834891872SAlexandre TORGUE } 116934891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 117034891872SAlexandre TORGUE &stm32port->tx_dma_buf, 117134891872SAlexandre TORGUE GFP_KERNEL); 117234891872SAlexandre TORGUE if (!stm32port->tx_buf) { 117334891872SAlexandre TORGUE ret = -ENOMEM; 117434891872SAlexandre TORGUE goto alloc_err; 117534891872SAlexandre TORGUE } 117634891872SAlexandre TORGUE 117734891872SAlexandre TORGUE /* Configure DMA channel */ 117834891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 11798e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 118034891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 118134891872SAlexandre TORGUE 118234891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 118334891872SAlexandre TORGUE if (ret < 0) { 118434891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 118534891872SAlexandre TORGUE ret = -ENODEV; 118634891872SAlexandre TORGUE goto config_err; 118734891872SAlexandre TORGUE } 118834891872SAlexandre TORGUE 118934891872SAlexandre TORGUE return 0; 119034891872SAlexandre TORGUE 119134891872SAlexandre TORGUE config_err: 119234891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 119334891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 119434891872SAlexandre TORGUE stm32port->tx_dma_buf); 119534891872SAlexandre TORGUE 119634891872SAlexandre TORGUE alloc_err: 119734891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 119834891872SAlexandre TORGUE stm32port->tx_ch = NULL; 119934891872SAlexandre TORGUE 120034891872SAlexandre TORGUE return ret; 120134891872SAlexandre TORGUE } 120234891872SAlexandre TORGUE 120348a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev) 120448a6092fSMaxime Coquelin { 120548a6092fSMaxime Coquelin struct stm32_port *stm32port; 1206ada8618fSAlexandre TORGUE int ret; 120748a6092fSMaxime Coquelin 120848a6092fSMaxime Coquelin stm32port = stm32_of_get_stm32_port(pdev); 120948a6092fSMaxime Coquelin if (!stm32port) 121048a6092fSMaxime Coquelin return -ENODEV; 121148a6092fSMaxime Coquelin 1212*d825f0beSStephen Boyd stm32port->info = of_device_get_match_data(&pdev->dev); 1213*d825f0beSStephen Boyd if (!stm32port->info) 1214ada8618fSAlexandre TORGUE return -EINVAL; 1215ada8618fSAlexandre TORGUE 121648a6092fSMaxime Coquelin ret = stm32_init_port(stm32port, pdev); 121748a6092fSMaxime Coquelin if (ret) 121848a6092fSMaxime Coquelin return ret; 121948a6092fSMaxime Coquelin 12202c58e560SErwan Le Ray if (stm32port->wakeirq > 0) { 1221270e5a74SFabrice Gasnier ret = device_init_wakeup(&pdev->dev, true); 122248a6092fSMaxime Coquelin if (ret) 1223ada80043SFabrice Gasnier goto err_uninit; 12245297f274SErwan Le Ray 12255297f274SErwan Le Ray ret = dev_pm_set_dedicated_wake_irq(&pdev->dev, 12265297f274SErwan Le Ray stm32port->wakeirq); 12275297f274SErwan Le Ray if (ret) 12285297f274SErwan Le Ray goto err_nowup; 12295297f274SErwan Le Ray 12305297f274SErwan Le Ray device_set_wakeup_enable(&pdev->dev, false); 1231270e5a74SFabrice Gasnier } 1232270e5a74SFabrice Gasnier 1233270e5a74SFabrice Gasnier ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 1234270e5a74SFabrice Gasnier if (ret) 12355297f274SErwan Le Ray goto err_wirq; 123648a6092fSMaxime Coquelin 123734891872SAlexandre TORGUE ret = stm32_of_dma_rx_probe(stm32port, pdev); 123834891872SAlexandre TORGUE if (ret) 123934891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 124034891872SAlexandre TORGUE 124134891872SAlexandre TORGUE ret = stm32_of_dma_tx_probe(stm32port, pdev); 124234891872SAlexandre TORGUE if (ret) 124334891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 124434891872SAlexandre TORGUE 124548a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 124648a6092fSMaxime Coquelin 1247fb6dcef6SErwan Le Ray pm_runtime_get_noresume(&pdev->dev); 1248fb6dcef6SErwan Le Ray pm_runtime_set_active(&pdev->dev); 1249fb6dcef6SErwan Le Ray pm_runtime_enable(&pdev->dev); 1250fb6dcef6SErwan Le Ray pm_runtime_put_sync(&pdev->dev); 1251fb6dcef6SErwan Le Ray 125248a6092fSMaxime Coquelin return 0; 1253ada80043SFabrice Gasnier 12545297f274SErwan Le Ray err_wirq: 12552c58e560SErwan Le Ray if (stm32port->wakeirq > 0) 12565297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 12575297f274SErwan Le Ray 1258270e5a74SFabrice Gasnier err_nowup: 12592c58e560SErwan Le Ray if (stm32port->wakeirq > 0) 1260270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 1261270e5a74SFabrice Gasnier 1262ada80043SFabrice Gasnier err_uninit: 1263ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 1264ada80043SFabrice Gasnier 1265ada80043SFabrice Gasnier return ret; 126648a6092fSMaxime Coquelin } 126748a6092fSMaxime Coquelin 126848a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev) 126948a6092fSMaxime Coquelin { 127048a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1271511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1272*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1273fb6dcef6SErwan Le Ray int err; 1274fb6dcef6SErwan Le Ray 1275fb6dcef6SErwan Le Ray pm_runtime_get_sync(&pdev->dev); 127634891872SAlexandre TORGUE 127734891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 127834891872SAlexandre TORGUE 127934891872SAlexandre TORGUE if (stm32_port->rx_ch) 128034891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 128134891872SAlexandre TORGUE 128234891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 128334891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 128434891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 128534891872SAlexandre TORGUE stm32_port->rx_dma_buf); 128634891872SAlexandre TORGUE 128734891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 128834891872SAlexandre TORGUE 128934891872SAlexandre TORGUE if (stm32_port->tx_ch) 129034891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 129134891872SAlexandre TORGUE 129234891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 129334891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 129434891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 129534891872SAlexandre TORGUE stm32_port->tx_dma_buf); 1296511c7b1bSAlexandre TORGUE 12972c58e560SErwan Le Ray if (stm32_port->wakeirq > 0) { 12985297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1299270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 13005297f274SErwan Le Ray } 1301270e5a74SFabrice Gasnier 1302511c7b1bSAlexandre TORGUE clk_disable_unprepare(stm32_port->clk); 130348a6092fSMaxime Coquelin 1304fb6dcef6SErwan Le Ray err = uart_remove_one_port(&stm32_usart_driver, port); 1305fb6dcef6SErwan Le Ray 1306fb6dcef6SErwan Le Ray pm_runtime_disable(&pdev->dev); 1307fb6dcef6SErwan Le Ray pm_runtime_put_noidle(&pdev->dev); 1308fb6dcef6SErwan Le Ray 1309fb6dcef6SErwan Le Ray return err; 131048a6092fSMaxime Coquelin } 131148a6092fSMaxime Coquelin 131248a6092fSMaxime Coquelin 131348a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 131448a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch) 131548a6092fSMaxime Coquelin { 1316ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1317*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1318ada8618fSAlexandre TORGUE 1319ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 132048a6092fSMaxime Coquelin cpu_relax(); 132148a6092fSMaxime Coquelin 1322ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 132348a6092fSMaxime Coquelin } 132448a6092fSMaxime Coquelin 132548a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt) 132648a6092fSMaxime Coquelin { 132748a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1328ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1329*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1330*d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 133148a6092fSMaxime Coquelin unsigned long flags; 133248a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 133348a6092fSMaxime Coquelin int locked = 1; 133448a6092fSMaxime Coquelin 133548a6092fSMaxime Coquelin local_irq_save(flags); 133648a6092fSMaxime Coquelin if (port->sysrq) 133748a6092fSMaxime Coquelin locked = 0; 133848a6092fSMaxime Coquelin else if (oops_in_progress) 133948a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 134048a6092fSMaxime Coquelin else 134148a6092fSMaxime Coquelin spin_lock(&port->lock); 134248a6092fSMaxime Coquelin 134387f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1344ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 134548a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 134687f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1347ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 134848a6092fSMaxime Coquelin 134948a6092fSMaxime Coquelin uart_console_write(port, s, cnt, stm32_console_putchar); 135048a6092fSMaxime Coquelin 135148a6092fSMaxime Coquelin /* Restore interrupt state */ 1352ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 135348a6092fSMaxime Coquelin 135448a6092fSMaxime Coquelin if (locked) 135548a6092fSMaxime Coquelin spin_unlock(&port->lock); 135648a6092fSMaxime Coquelin local_irq_restore(flags); 135748a6092fSMaxime Coquelin } 135848a6092fSMaxime Coquelin 135948a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options) 136048a6092fSMaxime Coquelin { 136148a6092fSMaxime Coquelin struct stm32_port *stm32port; 136248a6092fSMaxime Coquelin int baud = 9600; 136348a6092fSMaxime Coquelin int bits = 8; 136448a6092fSMaxime Coquelin int parity = 'n'; 136548a6092fSMaxime Coquelin int flow = 'n'; 136648a6092fSMaxime Coquelin 136748a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 136848a6092fSMaxime Coquelin return -ENODEV; 136948a6092fSMaxime Coquelin 137048a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 137148a6092fSMaxime Coquelin 137248a6092fSMaxime Coquelin /* 137348a6092fSMaxime Coquelin * This driver does not support early console initialization 137448a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 137548a6092fSMaxime Coquelin * this to be called during the uart port registration when the 137648a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 137748a6092fSMaxime Coquelin */ 137848a6092fSMaxime Coquelin if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) 137948a6092fSMaxime Coquelin return -ENXIO; 138048a6092fSMaxime Coquelin 138148a6092fSMaxime Coquelin if (options) 138248a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 138348a6092fSMaxime Coquelin 138448a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 138548a6092fSMaxime Coquelin } 138648a6092fSMaxime Coquelin 138748a6092fSMaxime Coquelin static struct console stm32_console = { 138848a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 138948a6092fSMaxime Coquelin .device = uart_console_device, 139048a6092fSMaxime Coquelin .write = stm32_console_write, 139148a6092fSMaxime Coquelin .setup = stm32_console_setup, 139248a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 139348a6092fSMaxime Coquelin .index = -1, 139448a6092fSMaxime Coquelin .data = &stm32_usart_driver, 139548a6092fSMaxime Coquelin }; 139648a6092fSMaxime Coquelin 139748a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 139848a6092fSMaxime Coquelin 139948a6092fSMaxime Coquelin #else 140048a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 140148a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 140248a6092fSMaxime Coquelin 140348a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 140448a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 140548a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 140648a6092fSMaxime Coquelin .major = 0, 140748a6092fSMaxime Coquelin .minor = 0, 140848a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 140948a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 141048a6092fSMaxime Coquelin }; 141148a6092fSMaxime Coquelin 1412fe94347dSErwan Le Ray static void __maybe_unused stm32_serial_enable_wakeup(struct uart_port *port, 1413fe94347dSErwan Le Ray bool enable) 1414270e5a74SFabrice Gasnier { 1415270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1416*d825f0beSStephen Boyd const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1417*d825f0beSStephen Boyd const struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1418270e5a74SFabrice Gasnier u32 val; 1419270e5a74SFabrice Gasnier 14202c58e560SErwan Le Ray if (stm32_port->wakeirq <= 0) 1421270e5a74SFabrice Gasnier return; 1422270e5a74SFabrice Gasnier 1423270e5a74SFabrice Gasnier if (enable) { 1424270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1425270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, USART_CR1_UESM); 1426270e5a74SFabrice Gasnier val = readl_relaxed(port->membase + ofs->cr3); 1427270e5a74SFabrice Gasnier val &= ~USART_CR3_WUS_MASK; 1428270e5a74SFabrice Gasnier /* Enable Wake up interrupt from low power on start bit */ 1429270e5a74SFabrice Gasnier val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE; 1430270e5a74SFabrice Gasnier writel_relaxed(val, port->membase + ofs->cr3); 1431270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1432270e5a74SFabrice Gasnier } else { 1433270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM); 1434270e5a74SFabrice Gasnier } 1435270e5a74SFabrice Gasnier } 1436270e5a74SFabrice Gasnier 1437fe94347dSErwan Le Ray static int __maybe_unused stm32_serial_suspend(struct device *dev) 1438270e5a74SFabrice Gasnier { 1439270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1440270e5a74SFabrice Gasnier 1441270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1442270e5a74SFabrice Gasnier 1443270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1444270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, true); 1445270e5a74SFabrice Gasnier else 1446270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1447270e5a74SFabrice Gasnier 144855484fccSErwan Le Ray /* 144955484fccSErwan Le Ray * When "no_console_suspend" is enabled, keep the pinctrl default state 145055484fccSErwan Le Ray * and rely on bootloader stage to restore this state upon resume. 145155484fccSErwan Le Ray * Otherwise, apply the idle or sleep states depending on wakeup 145255484fccSErwan Le Ray * capabilities. 145355484fccSErwan Le Ray */ 145455484fccSErwan Le Ray if (console_suspend_enabled || !uart_console(port)) { 145555484fccSErwan Le Ray if (device_may_wakeup(dev)) 145655484fccSErwan Le Ray pinctrl_pm_select_idle_state(dev); 145755484fccSErwan Le Ray else 145894616d9aSErwan Le Ray pinctrl_pm_select_sleep_state(dev); 145955484fccSErwan Le Ray } 146094616d9aSErwan Le Ray 1461270e5a74SFabrice Gasnier return 0; 1462270e5a74SFabrice Gasnier } 1463270e5a74SFabrice Gasnier 1464fe94347dSErwan Le Ray static int __maybe_unused stm32_serial_resume(struct device *dev) 1465270e5a74SFabrice Gasnier { 1466270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1467270e5a74SFabrice Gasnier 146894616d9aSErwan Le Ray pinctrl_pm_select_default_state(dev); 146994616d9aSErwan Le Ray 1470270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1471270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1472270e5a74SFabrice Gasnier 1473270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1474270e5a74SFabrice Gasnier } 1475270e5a74SFabrice Gasnier 1476fb6dcef6SErwan Le Ray static int __maybe_unused stm32_serial_runtime_suspend(struct device *dev) 1477fb6dcef6SErwan Le Ray { 1478fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1479fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1480fb6dcef6SErwan Le Ray struct stm32_port, port); 1481fb6dcef6SErwan Le Ray 1482fb6dcef6SErwan Le Ray clk_disable_unprepare(stm32port->clk); 1483fb6dcef6SErwan Le Ray 1484fb6dcef6SErwan Le Ray return 0; 1485fb6dcef6SErwan Le Ray } 1486fb6dcef6SErwan Le Ray 1487fb6dcef6SErwan Le Ray static int __maybe_unused stm32_serial_runtime_resume(struct device *dev) 1488fb6dcef6SErwan Le Ray { 1489fb6dcef6SErwan Le Ray struct uart_port *port = dev_get_drvdata(dev); 1490fb6dcef6SErwan Le Ray struct stm32_port *stm32port = container_of(port, 1491fb6dcef6SErwan Le Ray struct stm32_port, port); 1492fb6dcef6SErwan Le Ray 1493fb6dcef6SErwan Le Ray return clk_prepare_enable(stm32port->clk); 1494fb6dcef6SErwan Le Ray } 1495fb6dcef6SErwan Le Ray 1496270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 1497fb6dcef6SErwan Le Ray SET_RUNTIME_PM_OPS(stm32_serial_runtime_suspend, 1498fb6dcef6SErwan Le Ray stm32_serial_runtime_resume, NULL) 1499270e5a74SFabrice Gasnier SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume) 1500270e5a74SFabrice Gasnier }; 1501270e5a74SFabrice Gasnier 150248a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 150348a6092fSMaxime Coquelin .probe = stm32_serial_probe, 150448a6092fSMaxime Coquelin .remove = stm32_serial_remove, 150548a6092fSMaxime Coquelin .driver = { 150648a6092fSMaxime Coquelin .name = DRIVER_NAME, 1507270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 150848a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 150948a6092fSMaxime Coquelin }, 151048a6092fSMaxime Coquelin }; 151148a6092fSMaxime Coquelin 151248a6092fSMaxime Coquelin static int __init usart_init(void) 151348a6092fSMaxime Coquelin { 151448a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 151548a6092fSMaxime Coquelin int ret; 151648a6092fSMaxime Coquelin 151748a6092fSMaxime Coquelin pr_info("%s\n", banner); 151848a6092fSMaxime Coquelin 151948a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 152048a6092fSMaxime Coquelin if (ret) 152148a6092fSMaxime Coquelin return ret; 152248a6092fSMaxime Coquelin 152348a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 152448a6092fSMaxime Coquelin if (ret) 152548a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 152648a6092fSMaxime Coquelin 152748a6092fSMaxime Coquelin return ret; 152848a6092fSMaxime Coquelin } 152948a6092fSMaxime Coquelin 153048a6092fSMaxime Coquelin static void __exit usart_exit(void) 153148a6092fSMaxime Coquelin { 153248a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 153348a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 153448a6092fSMaxime Coquelin } 153548a6092fSMaxime Coquelin 153648a6092fSMaxime Coquelin module_init(usart_init); 153748a6092fSMaxime Coquelin module_exit(usart_exit); 153848a6092fSMaxime Coquelin 153948a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 154048a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 154148a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1542