1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 248a6092fSMaxime Coquelin /* 348a6092fSMaxime Coquelin * Copyright (C) Maxime Coquelin 2015 43e5fcbacSBich HEMON * Copyright (C) STMicroelectronics SA 2017 5ada8618fSAlexandre TORGUE * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com> 6ada8618fSAlexandre TORGUE * Gerald Baeza <gerald.baeza@st.com> 748a6092fSMaxime Coquelin * 848a6092fSMaxime Coquelin * Inspired by st-asc.c from STMicroelectronics (c) 948a6092fSMaxime Coquelin */ 1048a6092fSMaxime Coquelin 116b596a83SMaxime Coquelin #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 1248a6092fSMaxime Coquelin #define SUPPORT_SYSRQ 1348a6092fSMaxime Coquelin #endif 1448a6092fSMaxime Coquelin 1534891872SAlexandre TORGUE #include <linux/clk.h> 1648a6092fSMaxime Coquelin #include <linux/console.h> 1748a6092fSMaxime Coquelin #include <linux/delay.h> 1834891872SAlexandre TORGUE #include <linux/dma-direction.h> 1934891872SAlexandre TORGUE #include <linux/dmaengine.h> 2034891872SAlexandre TORGUE #include <linux/dma-mapping.h> 2134891872SAlexandre TORGUE #include <linux/io.h> 2234891872SAlexandre TORGUE #include <linux/iopoll.h> 2334891872SAlexandre TORGUE #include <linux/irq.h> 2434891872SAlexandre TORGUE #include <linux/module.h> 2548a6092fSMaxime Coquelin #include <linux/of.h> 2648a6092fSMaxime Coquelin #include <linux/of_platform.h> 2734891872SAlexandre TORGUE #include <linux/platform_device.h> 2834891872SAlexandre TORGUE #include <linux/pm_runtime.h> 29270e5a74SFabrice Gasnier #include <linux/pm_wakeirq.h> 3048a6092fSMaxime Coquelin #include <linux/serial_core.h> 3134891872SAlexandre TORGUE #include <linux/serial.h> 3234891872SAlexandre TORGUE #include <linux/spinlock.h> 3334891872SAlexandre TORGUE #include <linux/sysrq.h> 3434891872SAlexandre TORGUE #include <linux/tty_flip.h> 3534891872SAlexandre TORGUE #include <linux/tty.h> 3648a6092fSMaxime Coquelin 37bc5a0b55SAlexandre TORGUE #include "stm32-usart.h" 3848a6092fSMaxime Coquelin 3948a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port); 4034891872SAlexandre TORGUE static void stm32_transmit_chars(struct uart_port *port); 4148a6092fSMaxime Coquelin 4248a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 4348a6092fSMaxime Coquelin { 4448a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 4548a6092fSMaxime Coquelin } 4648a6092fSMaxime Coquelin 4748a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits) 4848a6092fSMaxime Coquelin { 4948a6092fSMaxime Coquelin u32 val; 5048a6092fSMaxime Coquelin 5148a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5248a6092fSMaxime Coquelin val |= bits; 5348a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 5448a6092fSMaxime Coquelin } 5548a6092fSMaxime Coquelin 5648a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits) 5748a6092fSMaxime Coquelin { 5848a6092fSMaxime Coquelin u32 val; 5948a6092fSMaxime Coquelin 6048a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 6148a6092fSMaxime Coquelin val &= ~bits; 6248a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 6348a6092fSMaxime Coquelin } 6448a6092fSMaxime Coquelin 651bcda09dSBich HEMON static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, 661bcda09dSBich HEMON u32 delay_DDE, u32 baud) 671bcda09dSBich HEMON { 681bcda09dSBich HEMON u32 rs485_deat_dedt; 691bcda09dSBich HEMON u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); 701bcda09dSBich HEMON bool over8; 711bcda09dSBich HEMON 721bcda09dSBich HEMON *cr3 |= USART_CR3_DEM; 731bcda09dSBich HEMON over8 = *cr1 & USART_CR1_OVER8; 741bcda09dSBich HEMON 751bcda09dSBich HEMON if (over8) 761bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 8; 771bcda09dSBich HEMON else 781bcda09dSBich HEMON rs485_deat_dedt = delay_ADE * baud * 16; 791bcda09dSBich HEMON 801bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 811bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 821bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 831bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & 841bcda09dSBich HEMON USART_CR1_DEAT_MASK; 851bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 861bcda09dSBich HEMON 871bcda09dSBich HEMON if (over8) 881bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 8; 891bcda09dSBich HEMON else 901bcda09dSBich HEMON rs485_deat_dedt = delay_DDE * baud * 16; 911bcda09dSBich HEMON 921bcda09dSBich HEMON rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); 931bcda09dSBich HEMON rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? 941bcda09dSBich HEMON rs485_deat_dedt_max : rs485_deat_dedt; 951bcda09dSBich HEMON rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & 961bcda09dSBich HEMON USART_CR1_DEDT_MASK; 971bcda09dSBich HEMON *cr1 |= rs485_deat_dedt; 981bcda09dSBich HEMON } 991bcda09dSBich HEMON 1001bcda09dSBich HEMON static int stm32_config_rs485(struct uart_port *port, 1011bcda09dSBich HEMON struct serial_rs485 *rs485conf) 1021bcda09dSBich HEMON { 1031bcda09dSBich HEMON struct stm32_port *stm32_port = to_stm32_port(port); 1041bcda09dSBich HEMON struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1051bcda09dSBich HEMON struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1061bcda09dSBich HEMON u32 usartdiv, baud, cr1, cr3; 1071bcda09dSBich HEMON bool over8; 1081bcda09dSBich HEMON 1091bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1101bcda09dSBich HEMON 1111bcda09dSBich HEMON port->rs485 = *rs485conf; 1121bcda09dSBich HEMON 1131bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RX_DURING_TX; 1141bcda09dSBich HEMON 1151bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 1161bcda09dSBich HEMON cr1 = readl_relaxed(port->membase + ofs->cr1); 1171bcda09dSBich HEMON cr3 = readl_relaxed(port->membase + ofs->cr3); 1181bcda09dSBich HEMON usartdiv = readl_relaxed(port->membase + ofs->brr); 1191bcda09dSBich HEMON usartdiv = usartdiv & GENMASK(15, 0); 1201bcda09dSBich HEMON over8 = cr1 & USART_CR1_OVER8; 1211bcda09dSBich HEMON 1221bcda09dSBich HEMON if (over8) 1231bcda09dSBich HEMON usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) 1241bcda09dSBich HEMON << USART_BRR_04_R_SHIFT; 1251bcda09dSBich HEMON 1261bcda09dSBich HEMON baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); 1271bcda09dSBich HEMON stm32_config_reg_rs485(&cr1, &cr3, 1281bcda09dSBich HEMON rs485conf->delay_rts_before_send, 1291bcda09dSBich HEMON rs485conf->delay_rts_after_send, baud); 1301bcda09dSBich HEMON 1311bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 1321bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 1331bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 1341bcda09dSBich HEMON } else { 1351bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 1361bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 1371bcda09dSBich HEMON } 1381bcda09dSBich HEMON 1391bcda09dSBich HEMON writel_relaxed(cr3, port->membase + ofs->cr3); 1401bcda09dSBich HEMON writel_relaxed(cr1, port->membase + ofs->cr1); 1411bcda09dSBich HEMON } else { 1421bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP); 1431bcda09dSBich HEMON stm32_clr_bits(port, ofs->cr1, 1441bcda09dSBich HEMON USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 1451bcda09dSBich HEMON } 1461bcda09dSBich HEMON 1471bcda09dSBich HEMON stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1481bcda09dSBich HEMON 1491bcda09dSBich HEMON return 0; 1501bcda09dSBich HEMON } 1511bcda09dSBich HEMON 1521bcda09dSBich HEMON static int stm32_init_rs485(struct uart_port *port, 1531bcda09dSBich HEMON struct platform_device *pdev) 1541bcda09dSBich HEMON { 1551bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 1561bcda09dSBich HEMON 1571bcda09dSBich HEMON rs485conf->flags = 0; 1581bcda09dSBich HEMON rs485conf->delay_rts_before_send = 0; 1591bcda09dSBich HEMON rs485conf->delay_rts_after_send = 0; 1601bcda09dSBich HEMON 1611bcda09dSBich HEMON if (!pdev->dev.of_node) 1621bcda09dSBich HEMON return -ENODEV; 1631bcda09dSBich HEMON 1641bcda09dSBich HEMON uart_get_rs485_mode(&pdev->dev, rs485conf); 1651bcda09dSBich HEMON 1661bcda09dSBich HEMON return 0; 1671bcda09dSBich HEMON } 1681bcda09dSBich HEMON 169b97055bcSBaoyou Xie static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res, 17034891872SAlexandre TORGUE bool threaded) 17134891872SAlexandre TORGUE { 17234891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 17334891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 17434891872SAlexandre TORGUE enum dma_status status; 17534891872SAlexandre TORGUE struct dma_tx_state state; 17634891872SAlexandre TORGUE 17734891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 17834891872SAlexandre TORGUE 17934891872SAlexandre TORGUE if (threaded && stm32_port->rx_ch) { 18034891872SAlexandre TORGUE status = dmaengine_tx_status(stm32_port->rx_ch, 18134891872SAlexandre TORGUE stm32_port->rx_ch->cookie, 18234891872SAlexandre TORGUE &state); 18334891872SAlexandre TORGUE if ((status == DMA_IN_PROGRESS) && 18434891872SAlexandre TORGUE (*last_res != state.residue)) 18534891872SAlexandre TORGUE return 1; 18634891872SAlexandre TORGUE else 18734891872SAlexandre TORGUE return 0; 18834891872SAlexandre TORGUE } else if (*sr & USART_SR_RXNE) { 18934891872SAlexandre TORGUE return 1; 19034891872SAlexandre TORGUE } 19134891872SAlexandre TORGUE return 0; 19234891872SAlexandre TORGUE } 19334891872SAlexandre TORGUE 1946c5962f3SErwan Le Ray static unsigned long stm32_get_char(struct uart_port *port, u32 *sr, 1956c5962f3SErwan Le Ray int *last_res) 19634891872SAlexandre TORGUE { 19734891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 19834891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 19934891872SAlexandre TORGUE unsigned long c; 20034891872SAlexandre TORGUE 20134891872SAlexandre TORGUE if (stm32_port->rx_ch) { 20234891872SAlexandre TORGUE c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; 20334891872SAlexandre TORGUE if ((*last_res) == 0) 20434891872SAlexandre TORGUE *last_res = RX_BUF_L; 20534891872SAlexandre TORGUE } else { 2066c5962f3SErwan Le Ray c = readl_relaxed(port->membase + ofs->rdr); 2076c5962f3SErwan Le Ray /* apply RDR data mask */ 2086c5962f3SErwan Le Ray c &= stm32_port->rdr_mask; 20934891872SAlexandre TORGUE } 2106c5962f3SErwan Le Ray 2116c5962f3SErwan Le Ray return c; 21234891872SAlexandre TORGUE } 21334891872SAlexandre TORGUE 21434891872SAlexandre TORGUE static void stm32_receive_chars(struct uart_port *port, bool threaded) 21548a6092fSMaxime Coquelin { 21648a6092fSMaxime Coquelin struct tty_port *tport = &port->state->port; 217ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 218ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 21948a6092fSMaxime Coquelin unsigned long c; 22048a6092fSMaxime Coquelin u32 sr; 22148a6092fSMaxime Coquelin char flag; 22248a6092fSMaxime Coquelin 22329d60981SAndy Shevchenko if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 22448a6092fSMaxime Coquelin pm_wakeup_event(tport->tty->dev, 0); 22548a6092fSMaxime Coquelin 226e5707915SGerald Baeza while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) { 22748a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 22848a6092fSMaxime Coquelin flag = TTY_NORMAL; 22948a6092fSMaxime Coquelin 2304f01d833SErwan Le Ray /* 2314f01d833SErwan Le Ray * Status bits has to be cleared before reading the RDR: 2324f01d833SErwan Le Ray * In FIFO mode, reading the RDR will pop the next data 2334f01d833SErwan Le Ray * (if any) along with its status bits into the SR. 2344f01d833SErwan Le Ray * Not doing so leads to misalignement between RDR and SR, 2354f01d833SErwan Le Ray * and clear status bits of the next rx data. 2364f01d833SErwan Le Ray * 2374f01d833SErwan Le Ray * Clear errors flags for stm32f7 and stm32h7 compatible 2384f01d833SErwan Le Ray * devices. On stm32f4 compatible devices, the error bit is 2394f01d833SErwan Le Ray * cleared by the sequence [read SR - read DR]. 2404f01d833SErwan Le Ray */ 2414f01d833SErwan Le Ray if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) 2424f01d833SErwan Le Ray stm32_clr_bits(port, ofs->icr, USART_ICR_ORECF | 2434f01d833SErwan Le Ray USART_ICR_PECF | USART_ICR_FECF); 2444f01d833SErwan Le Ray 2454f01d833SErwan Le Ray c = stm32_get_char(port, &sr, &stm32_port->last_res); 2464f01d833SErwan Le Ray port->icount.rx++; 24748a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 2484f01d833SErwan Le Ray if (sr & USART_SR_ORE) { 24948a6092fSMaxime Coquelin port->icount.overrun++; 25048a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 25148a6092fSMaxime Coquelin port->icount.parity++; 25248a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 2534f01d833SErwan Le Ray /* Break detection if character is null */ 2544f01d833SErwan Le Ray if (!c) { 2554f01d833SErwan Le Ray port->icount.brk++; 2564f01d833SErwan Le Ray if (uart_handle_break(port)) 2574f01d833SErwan Le Ray continue; 2584f01d833SErwan Le Ray } else { 25948a6092fSMaxime Coquelin port->icount.frame++; 26048a6092fSMaxime Coquelin } 2614f01d833SErwan Le Ray } 26248a6092fSMaxime Coquelin 26348a6092fSMaxime Coquelin sr &= port->read_status_mask; 26448a6092fSMaxime Coquelin 2654f01d833SErwan Le Ray if (sr & USART_SR_PE) { 26648a6092fSMaxime Coquelin flag = TTY_PARITY; 2674f01d833SErwan Le Ray } else if (sr & USART_SR_FE) { 2684f01d833SErwan Le Ray if (!c) 2694f01d833SErwan Le Ray flag = TTY_BREAK; 2704f01d833SErwan Le Ray else 27148a6092fSMaxime Coquelin flag = TTY_FRAME; 27248a6092fSMaxime Coquelin } 2734f01d833SErwan Le Ray } 27448a6092fSMaxime Coquelin 27548a6092fSMaxime Coquelin if (uart_handle_sysrq_char(port, c)) 27648a6092fSMaxime Coquelin continue; 27748a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 27848a6092fSMaxime Coquelin } 27948a6092fSMaxime Coquelin 28048a6092fSMaxime Coquelin spin_unlock(&port->lock); 28148a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 28248a6092fSMaxime Coquelin spin_lock(&port->lock); 28348a6092fSMaxime Coquelin } 28448a6092fSMaxime Coquelin 28534891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg) 28634891872SAlexandre TORGUE { 28734891872SAlexandre TORGUE struct uart_port *port = arg; 28834891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 28934891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 29034891872SAlexandre TORGUE 29134891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 29234891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 29334891872SAlexandre TORGUE 29434891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 29534891872SAlexandre TORGUE stm32_transmit_chars(port); 29634891872SAlexandre TORGUE } 29734891872SAlexandre TORGUE 298*d075719eSErwan Le Ray static void stm32_tx_interrupt_enable(struct uart_port *port) 299*d075719eSErwan Le Ray { 300*d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 301*d075719eSErwan Le Ray struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 302*d075719eSErwan Le Ray 303*d075719eSErwan Le Ray /* 304*d075719eSErwan Le Ray * Enables TX FIFO threashold irq when FIFO is enabled, 305*d075719eSErwan Le Ray * or TX empty irq when FIFO is disabled 306*d075719eSErwan Le Ray */ 307*d075719eSErwan Le Ray if (stm32_port->fifoen) 308*d075719eSErwan Le Ray stm32_set_bits(port, ofs->cr3, USART_CR3_TXFTIE); 309*d075719eSErwan Le Ray else 310*d075719eSErwan Le Ray stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 311*d075719eSErwan Le Ray } 312*d075719eSErwan Le Ray 313*d075719eSErwan Le Ray static void stm32_tx_interrupt_disable(struct uart_port *port) 314*d075719eSErwan Le Ray { 315*d075719eSErwan Le Ray struct stm32_port *stm32_port = to_stm32_port(port); 316*d075719eSErwan Le Ray struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 317*d075719eSErwan Le Ray 318*d075719eSErwan Le Ray if (stm32_port->fifoen) 319*d075719eSErwan Le Ray stm32_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE); 320*d075719eSErwan Le Ray else 321*d075719eSErwan Le Ray stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 322*d075719eSErwan Le Ray } 323*d075719eSErwan Le Ray 32434891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port) 32534891872SAlexandre TORGUE { 32634891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 32734891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 32834891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 32934891872SAlexandre TORGUE 33034891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 33134891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 33234891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 33334891872SAlexandre TORGUE } 33434891872SAlexandre TORGUE 3355d9176edSErwan Le Ray while (!uart_circ_empty(xmit)) { 3365d9176edSErwan Le Ray /* Check that TDR is empty before filling FIFO */ 3375d9176edSErwan Le Ray if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 3385d9176edSErwan Le Ray break; 33934891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 34034891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 34134891872SAlexandre TORGUE port->icount.tx++; 34234891872SAlexandre TORGUE } 34334891872SAlexandre TORGUE 3445d9176edSErwan Le Ray /* rely on TXE irq (mask or unmask) for sending remaining data */ 3455d9176edSErwan Le Ray if (uart_circ_empty(xmit)) 346*d075719eSErwan Le Ray stm32_tx_interrupt_disable(port); 3475d9176edSErwan Le Ray else 348*d075719eSErwan Le Ray stm32_tx_interrupt_enable(port); 3495d9176edSErwan Le Ray } 3505d9176edSErwan Le Ray 35134891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port) 35234891872SAlexandre TORGUE { 35334891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 35434891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 35534891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 35634891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 35734891872SAlexandre TORGUE dma_cookie_t cookie; 35834891872SAlexandre TORGUE unsigned int count, i; 35934891872SAlexandre TORGUE 36034891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 36134891872SAlexandre TORGUE return; 36234891872SAlexandre TORGUE 36334891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 36434891872SAlexandre TORGUE 36534891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 36634891872SAlexandre TORGUE 36734891872SAlexandre TORGUE if (count > TX_BUF_L) 36834891872SAlexandre TORGUE count = TX_BUF_L; 36934891872SAlexandre TORGUE 37034891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 37134891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 37234891872SAlexandre TORGUE } else { 37334891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 37434891872SAlexandre TORGUE size_t two; 37534891872SAlexandre TORGUE 37634891872SAlexandre TORGUE if (one > count) 37734891872SAlexandre TORGUE one = count; 37834891872SAlexandre TORGUE two = count - one; 37934891872SAlexandre TORGUE 38034891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 38134891872SAlexandre TORGUE if (two) 38234891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 38334891872SAlexandre TORGUE } 38434891872SAlexandre TORGUE 38534891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 38634891872SAlexandre TORGUE stm32port->tx_dma_buf, 38734891872SAlexandre TORGUE count, 38834891872SAlexandre TORGUE DMA_MEM_TO_DEV, 38934891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 39034891872SAlexandre TORGUE 39134891872SAlexandre TORGUE if (!desc) { 39234891872SAlexandre TORGUE for (i = count; i > 0; i--) 39334891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 39434891872SAlexandre TORGUE return; 39534891872SAlexandre TORGUE } 39634891872SAlexandre TORGUE 39734891872SAlexandre TORGUE desc->callback = stm32_tx_dma_complete; 39834891872SAlexandre TORGUE desc->callback_param = port; 39934891872SAlexandre TORGUE 40034891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 40134891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 40234891872SAlexandre TORGUE 40334891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 40434891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 40534891872SAlexandre TORGUE 40634891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 40734891872SAlexandre TORGUE 40834891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 40934891872SAlexandre TORGUE port->icount.tx += count; 41034891872SAlexandre TORGUE } 41134891872SAlexandre TORGUE 41248a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port) 41348a6092fSMaxime Coquelin { 414ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 415ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 41648a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 41748a6092fSMaxime Coquelin 41848a6092fSMaxime Coquelin if (port->x_char) { 41934891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 42034891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 421ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 42248a6092fSMaxime Coquelin port->x_char = 0; 42348a6092fSMaxime Coquelin port->icount.tx++; 42434891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 42534891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 42648a6092fSMaxime Coquelin return; 42748a6092fSMaxime Coquelin } 42848a6092fSMaxime Coquelin 429b83b957cSErwan Le Ray if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 430*d075719eSErwan Le Ray stm32_tx_interrupt_disable(port); 43148a6092fSMaxime Coquelin return; 43248a6092fSMaxime Coquelin } 43348a6092fSMaxime Coquelin 43464c32eabSErwan Le Ray if (ofs->icr == UNDEF_REG) 43564c32eabSErwan Le Ray stm32_clr_bits(port, ofs->isr, USART_SR_TC); 43664c32eabSErwan Le Ray else 43764c32eabSErwan Le Ray stm32_set_bits(port, ofs->icr, USART_ICR_TCCF); 43864c32eabSErwan Le Ray 43934891872SAlexandre TORGUE if (stm32_port->tx_ch) 44034891872SAlexandre TORGUE stm32_transmit_chars_dma(port); 44134891872SAlexandre TORGUE else 44234891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 44348a6092fSMaxime Coquelin 44448a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 44548a6092fSMaxime Coquelin uart_write_wakeup(port); 44648a6092fSMaxime Coquelin 44748a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 448*d075719eSErwan Le Ray stm32_tx_interrupt_disable(port); 44948a6092fSMaxime Coquelin } 45048a6092fSMaxime Coquelin 45148a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr) 45248a6092fSMaxime Coquelin { 45348a6092fSMaxime Coquelin struct uart_port *port = ptr; 454ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 455ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 45648a6092fSMaxime Coquelin u32 sr; 45748a6092fSMaxime Coquelin 45801d32d71SAlexandre TORGUE spin_lock(&port->lock); 45901d32d71SAlexandre TORGUE 460ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 46148a6092fSMaxime Coquelin 4624cc0ed62SErwan Le Ray if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) 4634cc0ed62SErwan Le Ray writel_relaxed(USART_ICR_RTOCF, 4644cc0ed62SErwan Le Ray port->membase + ofs->icr); 4654cc0ed62SErwan Le Ray 466270e5a74SFabrice Gasnier if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG)) 467270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 468270e5a74SFabrice Gasnier port->membase + ofs->icr); 469270e5a74SFabrice Gasnier 47034891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 47134891872SAlexandre TORGUE stm32_receive_chars(port, false); 47248a6092fSMaxime Coquelin 47334891872SAlexandre TORGUE if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) 47448a6092fSMaxime Coquelin stm32_transmit_chars(port); 47548a6092fSMaxime Coquelin 47601d32d71SAlexandre TORGUE spin_unlock(&port->lock); 47701d32d71SAlexandre TORGUE 47834891872SAlexandre TORGUE if (stm32_port->rx_ch) 47934891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 48034891872SAlexandre TORGUE else 48134891872SAlexandre TORGUE return IRQ_HANDLED; 48234891872SAlexandre TORGUE } 48334891872SAlexandre TORGUE 48434891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr) 48534891872SAlexandre TORGUE { 48634891872SAlexandre TORGUE struct uart_port *port = ptr; 48734891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 48834891872SAlexandre TORGUE 48934891872SAlexandre TORGUE spin_lock(&port->lock); 49034891872SAlexandre TORGUE 49134891872SAlexandre TORGUE if (stm32_port->rx_ch) 49234891872SAlexandre TORGUE stm32_receive_chars(port, true); 49334891872SAlexandre TORGUE 49448a6092fSMaxime Coquelin spin_unlock(&port->lock); 49548a6092fSMaxime Coquelin 49648a6092fSMaxime Coquelin return IRQ_HANDLED; 49748a6092fSMaxime Coquelin } 49848a6092fSMaxime Coquelin 49948a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port) 50048a6092fSMaxime Coquelin { 501ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 502ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 503ada8618fSAlexandre TORGUE 504ada8618fSAlexandre TORGUE return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; 50548a6092fSMaxime Coquelin } 50648a6092fSMaxime Coquelin 50748a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) 50848a6092fSMaxime Coquelin { 509ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 510ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 511ada8618fSAlexandre TORGUE 51248a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 513ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); 51448a6092fSMaxime Coquelin else 515ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 51648a6092fSMaxime Coquelin } 51748a6092fSMaxime Coquelin 51848a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port) 51948a6092fSMaxime Coquelin { 52048a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 52148a6092fSMaxime Coquelin return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 52248a6092fSMaxime Coquelin } 52348a6092fSMaxime Coquelin 52448a6092fSMaxime Coquelin /* Transmit stop */ 52548a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port) 52648a6092fSMaxime Coquelin { 527*d075719eSErwan Le Ray stm32_tx_interrupt_disable(port); 52848a6092fSMaxime Coquelin } 52948a6092fSMaxime Coquelin 53048a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 53148a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port) 53248a6092fSMaxime Coquelin { 53348a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 53448a6092fSMaxime Coquelin 53548a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 53648a6092fSMaxime Coquelin return; 53748a6092fSMaxime Coquelin 53834891872SAlexandre TORGUE stm32_transmit_chars(port); 53948a6092fSMaxime Coquelin } 54048a6092fSMaxime Coquelin 54148a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 54248a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port) 54348a6092fSMaxime Coquelin { 544ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 545ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 54648a6092fSMaxime Coquelin unsigned long flags; 54748a6092fSMaxime Coquelin 54848a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 5494cc0ed62SErwan Le Ray stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 55048a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 55148a6092fSMaxime Coquelin } 55248a6092fSMaxime Coquelin 55348a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 55448a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port) 55548a6092fSMaxime Coquelin { 556ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 557ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 55848a6092fSMaxime Coquelin unsigned long flags; 55948a6092fSMaxime Coquelin 56048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 5614cc0ed62SErwan Le Ray stm32_set_bits(port, ofs->cr1, stm32_port->cr1_irq); 56248a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 56348a6092fSMaxime Coquelin } 56448a6092fSMaxime Coquelin 56548a6092fSMaxime Coquelin /* Receive stop */ 56648a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port) 56748a6092fSMaxime Coquelin { 568ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 569ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 570ada8618fSAlexandre TORGUE 5714cc0ed62SErwan Le Ray stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq); 57248a6092fSMaxime Coquelin } 57348a6092fSMaxime Coquelin 57448a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 57548a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state) 57648a6092fSMaxime Coquelin { 57748a6092fSMaxime Coquelin } 57848a6092fSMaxime Coquelin 57948a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port) 58048a6092fSMaxime Coquelin { 581ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 582ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 58348a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 58448a6092fSMaxime Coquelin u32 val; 58548a6092fSMaxime Coquelin int ret; 58648a6092fSMaxime Coquelin 58734891872SAlexandre TORGUE ret = request_threaded_irq(port->irq, stm32_interrupt, 58834891872SAlexandre TORGUE stm32_threaded_interrupt, 58934891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 59048a6092fSMaxime Coquelin if (ret) 59148a6092fSMaxime Coquelin return ret; 59248a6092fSMaxime Coquelin 5934cc0ed62SErwan Le Ray val = stm32_port->cr1_irq | USART_CR1_TE | USART_CR1_RE; 594351a762aSGerald Baeza if (stm32_port->fifoen) 595351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 596ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, val); 59748a6092fSMaxime Coquelin 598*d075719eSErwan Le Ray if (stm32_port->fifoen) { 599*d075719eSErwan Le Ray val = readl_relaxed(port->membase + ofs->cr3); 600*d075719eSErwan Le Ray val &= ~USART_CR3_TXFTCFG_MASK; 601*d075719eSErwan Le Ray val |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT; 602*d075719eSErwan Le Ray writel_relaxed(val, port->membase + ofs->cr3); 603*d075719eSErwan Le Ray } 604*d075719eSErwan Le Ray 60548a6092fSMaxime Coquelin return 0; 60648a6092fSMaxime Coquelin } 60748a6092fSMaxime Coquelin 60848a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port) 60948a6092fSMaxime Coquelin { 610ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 611ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 61287f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 61364c32eabSErwan Le Ray u32 val, isr; 61464c32eabSErwan Le Ray int ret; 61548a6092fSMaxime Coquelin 6164cc0ed62SErwan Le Ray val = USART_CR1_TXEIE | USART_CR1_TE; 6174cc0ed62SErwan Le Ray val |= stm32_port->cr1_irq | USART_CR1_RE; 61887f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 619351a762aSGerald Baeza if (stm32_port->fifoen) 620351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 62164c32eabSErwan Le Ray 62264c32eabSErwan Le Ray ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, 62364c32eabSErwan Le Ray isr, (isr & USART_SR_TC), 62464c32eabSErwan Le Ray 10, 100000); 62564c32eabSErwan Le Ray 62664c32eabSErwan Le Ray if (ret) 62764c32eabSErwan Le Ray dev_err(port->dev, "transmission complete not set\n"); 62864c32eabSErwan Le Ray 629a14f66a4SAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, val); 63048a6092fSMaxime Coquelin 63148a6092fSMaxime Coquelin free_irq(port->irq, port); 63248a6092fSMaxime Coquelin } 63348a6092fSMaxime Coquelin 634929ffa4aSYueHaibing static unsigned int stm32_get_databits(struct ktermios *termios) 635c8a9d043SErwan Le Ray { 636c8a9d043SErwan Le Ray unsigned int bits; 637c8a9d043SErwan Le Ray 638c8a9d043SErwan Le Ray tcflag_t cflag = termios->c_cflag; 639c8a9d043SErwan Le Ray 640c8a9d043SErwan Le Ray switch (cflag & CSIZE) { 641c8a9d043SErwan Le Ray /* 642c8a9d043SErwan Le Ray * CSIZE settings are not necessarily supported in hardware. 643c8a9d043SErwan Le Ray * CSIZE unsupported configurations are handled here to set word length 644c8a9d043SErwan Le Ray * to 8 bits word as default configuration and to print debug message. 645c8a9d043SErwan Le Ray */ 646c8a9d043SErwan Le Ray case CS5: 647c8a9d043SErwan Le Ray bits = 5; 648c8a9d043SErwan Le Ray break; 649c8a9d043SErwan Le Ray case CS6: 650c8a9d043SErwan Le Ray bits = 6; 651c8a9d043SErwan Le Ray break; 652c8a9d043SErwan Le Ray case CS7: 653c8a9d043SErwan Le Ray bits = 7; 654c8a9d043SErwan Le Ray break; 655c8a9d043SErwan Le Ray /* default including CS8 */ 656c8a9d043SErwan Le Ray default: 657c8a9d043SErwan Le Ray bits = 8; 658c8a9d043SErwan Le Ray break; 659c8a9d043SErwan Le Ray } 660c8a9d043SErwan Le Ray 661c8a9d043SErwan Le Ray return bits; 662c8a9d043SErwan Le Ray } 663c8a9d043SErwan Le Ray 66448a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, 66548a6092fSMaxime Coquelin struct ktermios *old) 66648a6092fSMaxime Coquelin { 66748a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 668ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 669ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 6701bcda09dSBich HEMON struct serial_rs485 *rs485conf = &port->rs485; 671c8a9d043SErwan Le Ray unsigned int baud, bits; 67248a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 67348a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 67448a6092fSMaxime Coquelin u32 cr1, cr2, cr3; 67548a6092fSMaxime Coquelin unsigned long flags; 67648a6092fSMaxime Coquelin 67748a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 67848a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 67948a6092fSMaxime Coquelin 68048a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 68148a6092fSMaxime Coquelin 68248a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 68348a6092fSMaxime Coquelin 68448a6092fSMaxime Coquelin /* Stop serial port and reset value */ 685ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 68648a6092fSMaxime Coquelin 6874cc0ed62SErwan Le Ray cr1 = USART_CR1_TE | USART_CR1_RE; 6881bcda09dSBich HEMON 689351a762aSGerald Baeza if (stm32_port->fifoen) 690351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 69148a6092fSMaxime Coquelin cr2 = 0; 692*d075719eSErwan Le Ray cr3 = readl_relaxed(port->membase + ofs->cr3); 693*d075719eSErwan Le Ray cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTCFG | USART_CR3_RXFTIE 694*d075719eSErwan Le Ray | USART_CR3_TXFTCFG_MASK; 69548a6092fSMaxime Coquelin 69648a6092fSMaxime Coquelin if (cflag & CSTOPB) 69748a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 69848a6092fSMaxime Coquelin 699c8a9d043SErwan Le Ray bits = stm32_get_databits(termios); 7006c5962f3SErwan Le Ray stm32_port->rdr_mask = (BIT(bits) - 1); 701c8a9d043SErwan Le Ray 70248a6092fSMaxime Coquelin if (cflag & PARENB) { 703c8a9d043SErwan Le Ray bits++; 70448a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 705c8a9d043SErwan Le Ray } 706c8a9d043SErwan Le Ray 707c8a9d043SErwan Le Ray /* 708c8a9d043SErwan Le Ray * Word length configuration: 709c8a9d043SErwan Le Ray * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 710c8a9d043SErwan Le Ray * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 711c8a9d043SErwan Le Ray * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 712c8a9d043SErwan Le Ray * M0 and M1 already cleared by cr1 initialization. 713c8a9d043SErwan Le Ray */ 714c8a9d043SErwan Le Ray if (bits == 9) 715ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 716c8a9d043SErwan Le Ray else if ((bits == 7) && cfg->has_7bits_data) 717c8a9d043SErwan Le Ray cr1 |= USART_CR1_M1; 718c8a9d043SErwan Le Ray else if (bits != 8) 719c8a9d043SErwan Le Ray dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" 720c8a9d043SErwan Le Ray , bits); 72148a6092fSMaxime Coquelin 7224cc0ed62SErwan Le Ray if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch || 7234cc0ed62SErwan Le Ray stm32_port->fifoen)) { 7244cc0ed62SErwan Le Ray if (cflag & CSTOPB) 7254cc0ed62SErwan Le Ray bits = bits + 3; /* 1 start bit + 2 stop bits */ 7264cc0ed62SErwan Le Ray else 7274cc0ed62SErwan Le Ray bits = bits + 2; /* 1 start bit + 1 stop bit */ 7284cc0ed62SErwan Le Ray 7294cc0ed62SErwan Le Ray /* RX timeout irq to occur after last stop bit + bits */ 7304cc0ed62SErwan Le Ray stm32_port->cr1_irq = USART_CR1_RTOIE; 7314cc0ed62SErwan Le Ray writel_relaxed(bits, port->membase + ofs->rtor); 7324cc0ed62SErwan Le Ray cr2 |= USART_CR2_RTOEN; 7334cc0ed62SErwan Le Ray } 7344cc0ed62SErwan Le Ray 73548a6092fSMaxime Coquelin if (cflag & PARODD) 73648a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 73748a6092fSMaxime Coquelin 73848a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 73948a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 74048a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 74135abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 74248a6092fSMaxime Coquelin } 74348a6092fSMaxime Coquelin 74448a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 74548a6092fSMaxime Coquelin 74648a6092fSMaxime Coquelin /* 74748a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 74848a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 74948a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 75048a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 75148a6092fSMaxime Coquelin */ 75248a6092fSMaxime Coquelin if (usartdiv < 16) { 75348a6092fSMaxime Coquelin oversampling = 8; 7541bcda09dSBich HEMON cr1 |= USART_CR1_OVER8; 755ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); 75648a6092fSMaxime Coquelin } else { 75748a6092fSMaxime Coquelin oversampling = 16; 7581bcda09dSBich HEMON cr1 &= ~USART_CR1_OVER8; 759ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 76048a6092fSMaxime Coquelin } 76148a6092fSMaxime Coquelin 76248a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 76348a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 764ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 76548a6092fSMaxime Coquelin 76648a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 76748a6092fSMaxime Coquelin 76848a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 76948a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 77048a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 77148a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 7724f01d833SErwan Le Ray port->read_status_mask |= USART_SR_FE; 77348a6092fSMaxime Coquelin 77448a6092fSMaxime Coquelin /* Characters to ignore */ 77548a6092fSMaxime Coquelin port->ignore_status_mask = 0; 77648a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 77748a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 77848a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 7794f01d833SErwan Le Ray port->ignore_status_mask |= USART_SR_FE; 78048a6092fSMaxime Coquelin /* 78148a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 78248a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 78348a6092fSMaxime Coquelin */ 78448a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 78548a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 78648a6092fSMaxime Coquelin } 78748a6092fSMaxime Coquelin 78848a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 78948a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 79048a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 79148a6092fSMaxime Coquelin 79234891872SAlexandre TORGUE if (stm32_port->rx_ch) 79334891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 79434891872SAlexandre TORGUE 7951bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_ENABLED) { 7961bcda09dSBich HEMON stm32_config_reg_rs485(&cr1, &cr3, 7971bcda09dSBich HEMON rs485conf->delay_rts_before_send, 7981bcda09dSBich HEMON rs485conf->delay_rts_after_send, baud); 7991bcda09dSBich HEMON if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { 8001bcda09dSBich HEMON cr3 &= ~USART_CR3_DEP; 8011bcda09dSBich HEMON rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; 8021bcda09dSBich HEMON } else { 8031bcda09dSBich HEMON cr3 |= USART_CR3_DEP; 8041bcda09dSBich HEMON rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; 8051bcda09dSBich HEMON } 8061bcda09dSBich HEMON 8071bcda09dSBich HEMON } else { 8081bcda09dSBich HEMON cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); 8091bcda09dSBich HEMON cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); 8101bcda09dSBich HEMON } 8111bcda09dSBich HEMON 812ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 813ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 814ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 81548a6092fSMaxime Coquelin 8161bcda09dSBich HEMON stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 81748a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 81848a6092fSMaxime Coquelin } 81948a6092fSMaxime Coquelin 82048a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port) 82148a6092fSMaxime Coquelin { 82248a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 82348a6092fSMaxime Coquelin } 82448a6092fSMaxime Coquelin 82548a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port) 82648a6092fSMaxime Coquelin { 82748a6092fSMaxime Coquelin } 82848a6092fSMaxime Coquelin 82948a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port) 83048a6092fSMaxime Coquelin { 83148a6092fSMaxime Coquelin return 0; 83248a6092fSMaxime Coquelin } 83348a6092fSMaxime Coquelin 83448a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags) 83548a6092fSMaxime Coquelin { 83648a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 83748a6092fSMaxime Coquelin port->type = PORT_STM32; 83848a6092fSMaxime Coquelin } 83948a6092fSMaxime Coquelin 84048a6092fSMaxime Coquelin static int 84148a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser) 84248a6092fSMaxime Coquelin { 84348a6092fSMaxime Coquelin /* No user changeable parameters */ 84448a6092fSMaxime Coquelin return -EINVAL; 84548a6092fSMaxime Coquelin } 84648a6092fSMaxime Coquelin 84748a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state, 84848a6092fSMaxime Coquelin unsigned int oldstate) 84948a6092fSMaxime Coquelin { 85048a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 85148a6092fSMaxime Coquelin struct stm32_port, port); 852ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 853ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32port->info->cfg; 85448a6092fSMaxime Coquelin unsigned long flags = 0; 85548a6092fSMaxime Coquelin 85648a6092fSMaxime Coquelin switch (state) { 85748a6092fSMaxime Coquelin case UART_PM_STATE_ON: 85848a6092fSMaxime Coquelin clk_prepare_enable(stm32port->clk); 85948a6092fSMaxime Coquelin break; 86048a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 86148a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 862ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 86348a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 86448a6092fSMaxime Coquelin clk_disable_unprepare(stm32port->clk); 86548a6092fSMaxime Coquelin break; 86648a6092fSMaxime Coquelin } 86748a6092fSMaxime Coquelin } 86848a6092fSMaxime Coquelin 86948a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 87048a6092fSMaxime Coquelin .tx_empty = stm32_tx_empty, 87148a6092fSMaxime Coquelin .set_mctrl = stm32_set_mctrl, 87248a6092fSMaxime Coquelin .get_mctrl = stm32_get_mctrl, 87348a6092fSMaxime Coquelin .stop_tx = stm32_stop_tx, 87448a6092fSMaxime Coquelin .start_tx = stm32_start_tx, 87548a6092fSMaxime Coquelin .throttle = stm32_throttle, 87648a6092fSMaxime Coquelin .unthrottle = stm32_unthrottle, 87748a6092fSMaxime Coquelin .stop_rx = stm32_stop_rx, 87848a6092fSMaxime Coquelin .break_ctl = stm32_break_ctl, 87948a6092fSMaxime Coquelin .startup = stm32_startup, 88048a6092fSMaxime Coquelin .shutdown = stm32_shutdown, 88148a6092fSMaxime Coquelin .set_termios = stm32_set_termios, 88248a6092fSMaxime Coquelin .pm = stm32_pm, 88348a6092fSMaxime Coquelin .type = stm32_type, 88448a6092fSMaxime Coquelin .release_port = stm32_release_port, 88548a6092fSMaxime Coquelin .request_port = stm32_request_port, 88648a6092fSMaxime Coquelin .config_port = stm32_config_port, 88748a6092fSMaxime Coquelin .verify_port = stm32_verify_port, 88848a6092fSMaxime Coquelin }; 88948a6092fSMaxime Coquelin 89048a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port, 89148a6092fSMaxime Coquelin struct platform_device *pdev) 89248a6092fSMaxime Coquelin { 89348a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 89448a6092fSMaxime Coquelin struct resource *res; 89548a6092fSMaxime Coquelin int ret; 89648a6092fSMaxime Coquelin 89748a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 89848a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 89948a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 90048a6092fSMaxime Coquelin port->dev = &pdev->dev; 901*d075719eSErwan Le Ray port->fifosize = stm32port->info->cfg.fifosize; 9022c58e560SErwan Le Ray 9032c58e560SErwan Le Ray ret = platform_get_irq(pdev, 0); 9042c58e560SErwan Le Ray if (ret <= 0) { 9052c58e560SErwan Le Ray if (ret != -EPROBE_DEFER) 9062c58e560SErwan Le Ray dev_err(&pdev->dev, "Can't get event IRQ: %d\n", ret); 9072c58e560SErwan Le Ray return ret ? ret : -ENODEV; 9082c58e560SErwan Le Ray } 9092c58e560SErwan Le Ray port->irq = ret; 9102c58e560SErwan Le Ray 9117d8f6861SBich HEMON port->rs485_config = stm32_config_rs485; 9127d8f6861SBich HEMON 9137d8f6861SBich HEMON stm32_init_rs485(port, pdev); 9147d8f6861SBich HEMON 9152c58e560SErwan Le Ray if (stm32port->info->cfg.has_wakeup) { 916270e5a74SFabrice Gasnier stm32port->wakeirq = platform_get_irq(pdev, 1); 9172c58e560SErwan Le Ray if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO) { 9182c58e560SErwan Le Ray if (stm32port->wakeirq != -EPROBE_DEFER) 9192c58e560SErwan Le Ray dev_err(&pdev->dev, 9202c58e560SErwan Le Ray "Can't get event wake IRQ: %d\n", 9212c58e560SErwan Le Ray stm32port->wakeirq); 9222c58e560SErwan Le Ray return stm32port->wakeirq ? stm32port->wakeirq : 9232c58e560SErwan Le Ray -ENODEV; 9242c58e560SErwan Le Ray } 9252c58e560SErwan Le Ray } 9262c58e560SErwan Le Ray 927351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 92848a6092fSMaxime Coquelin 92948a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 93048a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 93148a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 93248a6092fSMaxime Coquelin return PTR_ERR(port->membase); 93348a6092fSMaxime Coquelin port->mapbase = res->start; 93448a6092fSMaxime Coquelin 93548a6092fSMaxime Coquelin spin_lock_init(&port->lock); 93648a6092fSMaxime Coquelin 93748a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 93848a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 93948a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 94048a6092fSMaxime Coquelin 94148a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 94248a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 94348a6092fSMaxime Coquelin if (ret) 94448a6092fSMaxime Coquelin return ret; 94548a6092fSMaxime Coquelin 94648a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 947ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 948ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 94948a6092fSMaxime Coquelin ret = -EINVAL; 950ada80043SFabrice Gasnier } 95148a6092fSMaxime Coquelin 95248a6092fSMaxime Coquelin return ret; 95348a6092fSMaxime Coquelin } 95448a6092fSMaxime Coquelin 95548a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) 95648a6092fSMaxime Coquelin { 95748a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 95848a6092fSMaxime Coquelin int id; 95948a6092fSMaxime Coquelin 96048a6092fSMaxime Coquelin if (!np) 96148a6092fSMaxime Coquelin return NULL; 96248a6092fSMaxime Coquelin 96348a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 964e5707915SGerald Baeza if (id < 0) { 965e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 966e5707915SGerald Baeza return NULL; 967e5707915SGerald Baeza } 96848a6092fSMaxime Coquelin 96948a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 97048a6092fSMaxime Coquelin return NULL; 97148a6092fSMaxime Coquelin 97248a6092fSMaxime Coquelin stm32_ports[id].hw_flow_control = of_property_read_bool(np, 97359bed2dfSAlexandre TORGUE "st,hw-flow-ctrl"); 97448a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 9754cc0ed62SErwan Le Ray stm32_ports[id].cr1_irq = USART_CR1_RXNEIE; 976e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 97748a6092fSMaxime Coquelin return &stm32_ports[id]; 97848a6092fSMaxime Coquelin } 97948a6092fSMaxime Coquelin 98048a6092fSMaxime Coquelin #ifdef CONFIG_OF 98148a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 982ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 983ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 984270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 98548a6092fSMaxime Coquelin {}, 98648a6092fSMaxime Coquelin }; 98748a6092fSMaxime Coquelin 98848a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 98948a6092fSMaxime Coquelin #endif 99048a6092fSMaxime Coquelin 99134891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port, 99234891872SAlexandre TORGUE struct platform_device *pdev) 99334891872SAlexandre TORGUE { 99434891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 99534891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 99634891872SAlexandre TORGUE struct device *dev = &pdev->dev; 99734891872SAlexandre TORGUE struct dma_slave_config config; 99834891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 99934891872SAlexandre TORGUE dma_cookie_t cookie; 100034891872SAlexandre TORGUE int ret; 100134891872SAlexandre TORGUE 100234891872SAlexandre TORGUE /* Request DMA RX channel */ 100334891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 100434891872SAlexandre TORGUE if (!stm32port->rx_ch) { 100534891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 100634891872SAlexandre TORGUE return -ENODEV; 100734891872SAlexandre TORGUE } 100834891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 100934891872SAlexandre TORGUE &stm32port->rx_dma_buf, 101034891872SAlexandre TORGUE GFP_KERNEL); 101134891872SAlexandre TORGUE if (!stm32port->rx_buf) { 101234891872SAlexandre TORGUE ret = -ENOMEM; 101334891872SAlexandre TORGUE goto alloc_err; 101434891872SAlexandre TORGUE } 101534891872SAlexandre TORGUE 101634891872SAlexandre TORGUE /* Configure DMA channel */ 101734891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 10188e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 101934891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 102034891872SAlexandre TORGUE 102134891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 102234891872SAlexandre TORGUE if (ret < 0) { 102334891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 102434891872SAlexandre TORGUE ret = -ENODEV; 102534891872SAlexandre TORGUE goto config_err; 102634891872SAlexandre TORGUE } 102734891872SAlexandre TORGUE 102834891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 102934891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 103034891872SAlexandre TORGUE stm32port->rx_dma_buf, 103134891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 103234891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 103334891872SAlexandre TORGUE if (!desc) { 103434891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 103534891872SAlexandre TORGUE ret = -ENODEV; 103634891872SAlexandre TORGUE goto config_err; 103734891872SAlexandre TORGUE } 103834891872SAlexandre TORGUE 103934891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 104034891872SAlexandre TORGUE desc->callback = NULL; 104134891872SAlexandre TORGUE desc->callback_param = NULL; 104234891872SAlexandre TORGUE 104334891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 104434891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 104534891872SAlexandre TORGUE 104634891872SAlexandre TORGUE /* Issue pending DMA requests */ 104734891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 104834891872SAlexandre TORGUE 104934891872SAlexandre TORGUE return 0; 105034891872SAlexandre TORGUE 105134891872SAlexandre TORGUE config_err: 105234891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 105334891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 105434891872SAlexandre TORGUE stm32port->rx_dma_buf); 105534891872SAlexandre TORGUE 105634891872SAlexandre TORGUE alloc_err: 105734891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 105834891872SAlexandre TORGUE stm32port->rx_ch = NULL; 105934891872SAlexandre TORGUE 106034891872SAlexandre TORGUE return ret; 106134891872SAlexandre TORGUE } 106234891872SAlexandre TORGUE 106334891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port, 106434891872SAlexandre TORGUE struct platform_device *pdev) 106534891872SAlexandre TORGUE { 106634891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 106734891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 106834891872SAlexandre TORGUE struct device *dev = &pdev->dev; 106934891872SAlexandre TORGUE struct dma_slave_config config; 107034891872SAlexandre TORGUE int ret; 107134891872SAlexandre TORGUE 107234891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 107334891872SAlexandre TORGUE 107434891872SAlexandre TORGUE /* Request DMA TX channel */ 107534891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 107634891872SAlexandre TORGUE if (!stm32port->tx_ch) { 107734891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 107834891872SAlexandre TORGUE return -ENODEV; 107934891872SAlexandre TORGUE } 108034891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 108134891872SAlexandre TORGUE &stm32port->tx_dma_buf, 108234891872SAlexandre TORGUE GFP_KERNEL); 108334891872SAlexandre TORGUE if (!stm32port->tx_buf) { 108434891872SAlexandre TORGUE ret = -ENOMEM; 108534891872SAlexandre TORGUE goto alloc_err; 108634891872SAlexandre TORGUE } 108734891872SAlexandre TORGUE 108834891872SAlexandre TORGUE /* Configure DMA channel */ 108934891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 10908e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 109134891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 109234891872SAlexandre TORGUE 109334891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 109434891872SAlexandre TORGUE if (ret < 0) { 109534891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 109634891872SAlexandre TORGUE ret = -ENODEV; 109734891872SAlexandre TORGUE goto config_err; 109834891872SAlexandre TORGUE } 109934891872SAlexandre TORGUE 110034891872SAlexandre TORGUE return 0; 110134891872SAlexandre TORGUE 110234891872SAlexandre TORGUE config_err: 110334891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 110434891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 110534891872SAlexandre TORGUE stm32port->tx_dma_buf); 110634891872SAlexandre TORGUE 110734891872SAlexandre TORGUE alloc_err: 110834891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 110934891872SAlexandre TORGUE stm32port->tx_ch = NULL; 111034891872SAlexandre TORGUE 111134891872SAlexandre TORGUE return ret; 111234891872SAlexandre TORGUE } 111334891872SAlexandre TORGUE 111448a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev) 111548a6092fSMaxime Coquelin { 1116ada8618fSAlexandre TORGUE const struct of_device_id *match; 111748a6092fSMaxime Coquelin struct stm32_port *stm32port; 1118ada8618fSAlexandre TORGUE int ret; 111948a6092fSMaxime Coquelin 112048a6092fSMaxime Coquelin stm32port = stm32_of_get_stm32_port(pdev); 112148a6092fSMaxime Coquelin if (!stm32port) 112248a6092fSMaxime Coquelin return -ENODEV; 112348a6092fSMaxime Coquelin 1124ada8618fSAlexandre TORGUE match = of_match_device(stm32_match, &pdev->dev); 1125ada8618fSAlexandre TORGUE if (match && match->data) 1126ada8618fSAlexandre TORGUE stm32port->info = (struct stm32_usart_info *)match->data; 1127ada8618fSAlexandre TORGUE else 1128ada8618fSAlexandre TORGUE return -EINVAL; 1129ada8618fSAlexandre TORGUE 113048a6092fSMaxime Coquelin ret = stm32_init_port(stm32port, pdev); 113148a6092fSMaxime Coquelin if (ret) 113248a6092fSMaxime Coquelin return ret; 113348a6092fSMaxime Coquelin 11342c58e560SErwan Le Ray if (stm32port->wakeirq > 0) { 1135270e5a74SFabrice Gasnier ret = device_init_wakeup(&pdev->dev, true); 113648a6092fSMaxime Coquelin if (ret) 1137ada80043SFabrice Gasnier goto err_uninit; 11385297f274SErwan Le Ray 11395297f274SErwan Le Ray ret = dev_pm_set_dedicated_wake_irq(&pdev->dev, 11405297f274SErwan Le Ray stm32port->wakeirq); 11415297f274SErwan Le Ray if (ret) 11425297f274SErwan Le Ray goto err_nowup; 11435297f274SErwan Le Ray 11445297f274SErwan Le Ray device_set_wakeup_enable(&pdev->dev, false); 1145270e5a74SFabrice Gasnier } 1146270e5a74SFabrice Gasnier 1147270e5a74SFabrice Gasnier ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 1148270e5a74SFabrice Gasnier if (ret) 11495297f274SErwan Le Ray goto err_wirq; 115048a6092fSMaxime Coquelin 115134891872SAlexandre TORGUE ret = stm32_of_dma_rx_probe(stm32port, pdev); 115234891872SAlexandre TORGUE if (ret) 115334891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 115434891872SAlexandre TORGUE 115534891872SAlexandre TORGUE ret = stm32_of_dma_tx_probe(stm32port, pdev); 115634891872SAlexandre TORGUE if (ret) 115734891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 115834891872SAlexandre TORGUE 115948a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 116048a6092fSMaxime Coquelin 116148a6092fSMaxime Coquelin return 0; 1162ada80043SFabrice Gasnier 11635297f274SErwan Le Ray err_wirq: 11642c58e560SErwan Le Ray if (stm32port->wakeirq > 0) 11655297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 11665297f274SErwan Le Ray 1167270e5a74SFabrice Gasnier err_nowup: 11682c58e560SErwan Le Ray if (stm32port->wakeirq > 0) 1169270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 1170270e5a74SFabrice Gasnier 1171ada80043SFabrice Gasnier err_uninit: 1172ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 1173ada80043SFabrice Gasnier 1174ada80043SFabrice Gasnier return ret; 117548a6092fSMaxime Coquelin } 117648a6092fSMaxime Coquelin 117748a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev) 117848a6092fSMaxime Coquelin { 117948a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 1180511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 118134891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 118234891872SAlexandre TORGUE 118334891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 118434891872SAlexandre TORGUE 118534891872SAlexandre TORGUE if (stm32_port->rx_ch) 118634891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 118734891872SAlexandre TORGUE 118834891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 118934891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 119034891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 119134891872SAlexandre TORGUE stm32_port->rx_dma_buf); 119234891872SAlexandre TORGUE 119334891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 119434891872SAlexandre TORGUE 119534891872SAlexandre TORGUE if (stm32_port->tx_ch) 119634891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 119734891872SAlexandre TORGUE 119834891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 119934891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 120034891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 120134891872SAlexandre TORGUE stm32_port->tx_dma_buf); 1202511c7b1bSAlexandre TORGUE 12032c58e560SErwan Le Ray if (stm32_port->wakeirq > 0) { 12045297f274SErwan Le Ray dev_pm_clear_wake_irq(&pdev->dev); 1205270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 12065297f274SErwan Le Ray } 1207270e5a74SFabrice Gasnier 1208511c7b1bSAlexandre TORGUE clk_disable_unprepare(stm32_port->clk); 120948a6092fSMaxime Coquelin 121048a6092fSMaxime Coquelin return uart_remove_one_port(&stm32_usart_driver, port); 121148a6092fSMaxime Coquelin } 121248a6092fSMaxime Coquelin 121348a6092fSMaxime Coquelin 121448a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 121548a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch) 121648a6092fSMaxime Coquelin { 1217ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1218ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1219ada8618fSAlexandre TORGUE 1220ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 122148a6092fSMaxime Coquelin cpu_relax(); 122248a6092fSMaxime Coquelin 1223ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 122448a6092fSMaxime Coquelin } 122548a6092fSMaxime Coquelin 122648a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt) 122748a6092fSMaxime Coquelin { 122848a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 1229ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 1230ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 123187f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 123248a6092fSMaxime Coquelin unsigned long flags; 123348a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 123448a6092fSMaxime Coquelin int locked = 1; 123548a6092fSMaxime Coquelin 123648a6092fSMaxime Coquelin local_irq_save(flags); 123748a6092fSMaxime Coquelin if (port->sysrq) 123848a6092fSMaxime Coquelin locked = 0; 123948a6092fSMaxime Coquelin else if (oops_in_progress) 124048a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 124148a6092fSMaxime Coquelin else 124248a6092fSMaxime Coquelin spin_lock(&port->lock); 124348a6092fSMaxime Coquelin 124487f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 1245ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 124648a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 124787f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 1248ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 124948a6092fSMaxime Coquelin 125048a6092fSMaxime Coquelin uart_console_write(port, s, cnt, stm32_console_putchar); 125148a6092fSMaxime Coquelin 125248a6092fSMaxime Coquelin /* Restore interrupt state */ 1253ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 125448a6092fSMaxime Coquelin 125548a6092fSMaxime Coquelin if (locked) 125648a6092fSMaxime Coquelin spin_unlock(&port->lock); 125748a6092fSMaxime Coquelin local_irq_restore(flags); 125848a6092fSMaxime Coquelin } 125948a6092fSMaxime Coquelin 126048a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options) 126148a6092fSMaxime Coquelin { 126248a6092fSMaxime Coquelin struct stm32_port *stm32port; 126348a6092fSMaxime Coquelin int baud = 9600; 126448a6092fSMaxime Coquelin int bits = 8; 126548a6092fSMaxime Coquelin int parity = 'n'; 126648a6092fSMaxime Coquelin int flow = 'n'; 126748a6092fSMaxime Coquelin 126848a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 126948a6092fSMaxime Coquelin return -ENODEV; 127048a6092fSMaxime Coquelin 127148a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 127248a6092fSMaxime Coquelin 127348a6092fSMaxime Coquelin /* 127448a6092fSMaxime Coquelin * This driver does not support early console initialization 127548a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 127648a6092fSMaxime Coquelin * this to be called during the uart port registration when the 127748a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 127848a6092fSMaxime Coquelin */ 127948a6092fSMaxime Coquelin if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) 128048a6092fSMaxime Coquelin return -ENXIO; 128148a6092fSMaxime Coquelin 128248a6092fSMaxime Coquelin if (options) 128348a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 128448a6092fSMaxime Coquelin 128548a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 128648a6092fSMaxime Coquelin } 128748a6092fSMaxime Coquelin 128848a6092fSMaxime Coquelin static struct console stm32_console = { 128948a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 129048a6092fSMaxime Coquelin .device = uart_console_device, 129148a6092fSMaxime Coquelin .write = stm32_console_write, 129248a6092fSMaxime Coquelin .setup = stm32_console_setup, 129348a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 129448a6092fSMaxime Coquelin .index = -1, 129548a6092fSMaxime Coquelin .data = &stm32_usart_driver, 129648a6092fSMaxime Coquelin }; 129748a6092fSMaxime Coquelin 129848a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 129948a6092fSMaxime Coquelin 130048a6092fSMaxime Coquelin #else 130148a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 130248a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 130348a6092fSMaxime Coquelin 130448a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 130548a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 130648a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 130748a6092fSMaxime Coquelin .major = 0, 130848a6092fSMaxime Coquelin .minor = 0, 130948a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 131048a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 131148a6092fSMaxime Coquelin }; 131248a6092fSMaxime Coquelin 1313270e5a74SFabrice Gasnier #ifdef CONFIG_PM_SLEEP 1314270e5a74SFabrice Gasnier static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable) 1315270e5a74SFabrice Gasnier { 1316270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1317270e5a74SFabrice Gasnier struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1318270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1319270e5a74SFabrice Gasnier u32 val; 1320270e5a74SFabrice Gasnier 13212c58e560SErwan Le Ray if (stm32_port->wakeirq <= 0) 1322270e5a74SFabrice Gasnier return; 1323270e5a74SFabrice Gasnier 1324270e5a74SFabrice Gasnier if (enable) { 1325270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1326270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, USART_CR1_UESM); 1327270e5a74SFabrice Gasnier val = readl_relaxed(port->membase + ofs->cr3); 1328270e5a74SFabrice Gasnier val &= ~USART_CR3_WUS_MASK; 1329270e5a74SFabrice Gasnier /* Enable Wake up interrupt from low power on start bit */ 1330270e5a74SFabrice Gasnier val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE; 1331270e5a74SFabrice Gasnier writel_relaxed(val, port->membase + ofs->cr3); 1332270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1333270e5a74SFabrice Gasnier } else { 1334270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM); 1335270e5a74SFabrice Gasnier } 1336270e5a74SFabrice Gasnier } 1337270e5a74SFabrice Gasnier 1338270e5a74SFabrice Gasnier static int stm32_serial_suspend(struct device *dev) 1339270e5a74SFabrice Gasnier { 1340270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1341270e5a74SFabrice Gasnier 1342270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1343270e5a74SFabrice Gasnier 1344270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1345270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, true); 1346270e5a74SFabrice Gasnier else 1347270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1348270e5a74SFabrice Gasnier 1349270e5a74SFabrice Gasnier return 0; 1350270e5a74SFabrice Gasnier } 1351270e5a74SFabrice Gasnier 1352270e5a74SFabrice Gasnier static int stm32_serial_resume(struct device *dev) 1353270e5a74SFabrice Gasnier { 1354270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1355270e5a74SFabrice Gasnier 1356270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1357270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1358270e5a74SFabrice Gasnier 1359270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1360270e5a74SFabrice Gasnier } 1361270e5a74SFabrice Gasnier #endif /* CONFIG_PM_SLEEP */ 1362270e5a74SFabrice Gasnier 1363270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 1364270e5a74SFabrice Gasnier SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume) 1365270e5a74SFabrice Gasnier }; 1366270e5a74SFabrice Gasnier 136748a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 136848a6092fSMaxime Coquelin .probe = stm32_serial_probe, 136948a6092fSMaxime Coquelin .remove = stm32_serial_remove, 137048a6092fSMaxime Coquelin .driver = { 137148a6092fSMaxime Coquelin .name = DRIVER_NAME, 1372270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 137348a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 137448a6092fSMaxime Coquelin }, 137548a6092fSMaxime Coquelin }; 137648a6092fSMaxime Coquelin 137748a6092fSMaxime Coquelin static int __init usart_init(void) 137848a6092fSMaxime Coquelin { 137948a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 138048a6092fSMaxime Coquelin int ret; 138148a6092fSMaxime Coquelin 138248a6092fSMaxime Coquelin pr_info("%s\n", banner); 138348a6092fSMaxime Coquelin 138448a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 138548a6092fSMaxime Coquelin if (ret) 138648a6092fSMaxime Coquelin return ret; 138748a6092fSMaxime Coquelin 138848a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 138948a6092fSMaxime Coquelin if (ret) 139048a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 139148a6092fSMaxime Coquelin 139248a6092fSMaxime Coquelin return ret; 139348a6092fSMaxime Coquelin } 139448a6092fSMaxime Coquelin 139548a6092fSMaxime Coquelin static void __exit usart_exit(void) 139648a6092fSMaxime Coquelin { 139748a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 139848a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 139948a6092fSMaxime Coquelin } 140048a6092fSMaxime Coquelin 140148a6092fSMaxime Coquelin module_init(usart_init); 140248a6092fSMaxime Coquelin module_exit(usart_exit); 140348a6092fSMaxime Coquelin 140448a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 140548a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 140648a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1407