148a6092fSMaxime Coquelin /* 248a6092fSMaxime Coquelin * Copyright (C) Maxime Coquelin 2015 3ada8618fSAlexandre TORGUE * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com> 4ada8618fSAlexandre TORGUE * Gerald Baeza <gerald.baeza@st.com> 548a6092fSMaxime Coquelin * License terms: GNU General Public License (GPL), version 2 648a6092fSMaxime Coquelin * 748a6092fSMaxime Coquelin * Inspired by st-asc.c from STMicroelectronics (c) 848a6092fSMaxime Coquelin */ 948a6092fSMaxime Coquelin 106b596a83SMaxime Coquelin #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 1148a6092fSMaxime Coquelin #define SUPPORT_SYSRQ 1248a6092fSMaxime Coquelin #endif 1348a6092fSMaxime Coquelin 14*34891872SAlexandre TORGUE #include <linux/clk.h> 1548a6092fSMaxime Coquelin #include <linux/console.h> 1648a6092fSMaxime Coquelin #include <linux/delay.h> 17*34891872SAlexandre TORGUE #include <linux/dma-direction.h> 18*34891872SAlexandre TORGUE #include <linux/dmaengine.h> 19*34891872SAlexandre TORGUE #include <linux/dma-mapping.h> 20*34891872SAlexandre TORGUE #include <linux/io.h> 21*34891872SAlexandre TORGUE #include <linux/iopoll.h> 22*34891872SAlexandre TORGUE #include <linux/irq.h> 23*34891872SAlexandre TORGUE #include <linux/module.h> 2448a6092fSMaxime Coquelin #include <linux/of.h> 2548a6092fSMaxime Coquelin #include <linux/of_platform.h> 26*34891872SAlexandre TORGUE #include <linux/platform_device.h> 27*34891872SAlexandre TORGUE #include <linux/pm_runtime.h> 2848a6092fSMaxime Coquelin #include <linux/serial_core.h> 29*34891872SAlexandre TORGUE #include <linux/serial.h> 30*34891872SAlexandre TORGUE #include <linux/spinlock.h> 31*34891872SAlexandre TORGUE #include <linux/sysrq.h> 32*34891872SAlexandre TORGUE #include <linux/tty_flip.h> 33*34891872SAlexandre TORGUE #include <linux/tty.h> 3448a6092fSMaxime Coquelin 35bc5a0b55SAlexandre TORGUE #include "stm32-usart.h" 3648a6092fSMaxime Coquelin 3748a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port); 38*34891872SAlexandre 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 63*34891872SAlexandre TORGUE int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res, 64*34891872SAlexandre TORGUE bool threaded) 65*34891872SAlexandre TORGUE { 66*34891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 67*34891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 68*34891872SAlexandre TORGUE enum dma_status status; 69*34891872SAlexandre TORGUE struct dma_tx_state state; 70*34891872SAlexandre TORGUE 71*34891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 72*34891872SAlexandre TORGUE 73*34891872SAlexandre TORGUE if (threaded && stm32_port->rx_ch) { 74*34891872SAlexandre TORGUE status = dmaengine_tx_status(stm32_port->rx_ch, 75*34891872SAlexandre TORGUE stm32_port->rx_ch->cookie, 76*34891872SAlexandre TORGUE &state); 77*34891872SAlexandre TORGUE if ((status == DMA_IN_PROGRESS) && 78*34891872SAlexandre TORGUE (*last_res != state.residue)) 79*34891872SAlexandre TORGUE return 1; 80*34891872SAlexandre TORGUE else 81*34891872SAlexandre TORGUE return 0; 82*34891872SAlexandre TORGUE } else if (*sr & USART_SR_RXNE) { 83*34891872SAlexandre TORGUE return 1; 84*34891872SAlexandre TORGUE } 85*34891872SAlexandre TORGUE return 0; 86*34891872SAlexandre TORGUE } 87*34891872SAlexandre TORGUE 88*34891872SAlexandre TORGUE unsigned long stm32_get_char(struct uart_port *port, u32 *sr, int *last_res) 89*34891872SAlexandre TORGUE { 90*34891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 91*34891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 92*34891872SAlexandre TORGUE unsigned long c; 93*34891872SAlexandre TORGUE 94*34891872SAlexandre TORGUE if (stm32_port->rx_ch) { 95*34891872SAlexandre TORGUE c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; 96*34891872SAlexandre TORGUE if ((*last_res) == 0) 97*34891872SAlexandre TORGUE *last_res = RX_BUF_L; 98*34891872SAlexandre TORGUE return c; 99*34891872SAlexandre TORGUE } else { 100*34891872SAlexandre TORGUE return readl_relaxed(port->membase + ofs->rdr); 101*34891872SAlexandre TORGUE } 102*34891872SAlexandre TORGUE } 103*34891872SAlexandre TORGUE 104*34891872SAlexandre TORGUE static void stm32_receive_chars(struct uart_port *port, bool threaded) 10548a6092fSMaxime Coquelin { 10648a6092fSMaxime Coquelin struct tty_port *tport = &port->state->port; 107ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 108ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 10948a6092fSMaxime Coquelin unsigned long c; 11048a6092fSMaxime Coquelin u32 sr; 11148a6092fSMaxime Coquelin char flag; 112*34891872SAlexandre TORGUE static int last_res = RX_BUF_L; 11348a6092fSMaxime Coquelin 11448a6092fSMaxime Coquelin if (port->irq_wake) 11548a6092fSMaxime Coquelin pm_wakeup_event(tport->tty->dev, 0); 11648a6092fSMaxime Coquelin 117*34891872SAlexandre TORGUE while (stm32_pending_rx(port, &sr, &last_res, threaded)) { 11848a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 119*34891872SAlexandre TORGUE c = stm32_get_char(port, &sr, &last_res); 12048a6092fSMaxime Coquelin flag = TTY_NORMAL; 12148a6092fSMaxime Coquelin port->icount.rx++; 12248a6092fSMaxime Coquelin 12348a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 12448a6092fSMaxime Coquelin if (sr & USART_SR_LBD) { 12548a6092fSMaxime Coquelin port->icount.brk++; 12648a6092fSMaxime Coquelin if (uart_handle_break(port)) 12748a6092fSMaxime Coquelin continue; 12848a6092fSMaxime Coquelin } else if (sr & USART_SR_ORE) { 129ada8618fSAlexandre TORGUE if (ofs->icr != UNDEF_REG) 130ada8618fSAlexandre TORGUE writel_relaxed(USART_ICR_ORECF, 131ada8618fSAlexandre TORGUE port->membase + 132ada8618fSAlexandre TORGUE ofs->icr); 13348a6092fSMaxime Coquelin port->icount.overrun++; 13448a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 13548a6092fSMaxime Coquelin port->icount.parity++; 13648a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 13748a6092fSMaxime Coquelin port->icount.frame++; 13848a6092fSMaxime Coquelin } 13948a6092fSMaxime Coquelin 14048a6092fSMaxime Coquelin sr &= port->read_status_mask; 14148a6092fSMaxime Coquelin 14248a6092fSMaxime Coquelin if (sr & USART_SR_LBD) 14348a6092fSMaxime Coquelin flag = TTY_BREAK; 14448a6092fSMaxime Coquelin else if (sr & USART_SR_PE) 14548a6092fSMaxime Coquelin flag = TTY_PARITY; 14648a6092fSMaxime Coquelin else if (sr & USART_SR_FE) 14748a6092fSMaxime Coquelin flag = TTY_FRAME; 14848a6092fSMaxime Coquelin } 14948a6092fSMaxime Coquelin 15048a6092fSMaxime Coquelin if (uart_handle_sysrq_char(port, c)) 15148a6092fSMaxime Coquelin continue; 15248a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 15348a6092fSMaxime Coquelin } 15448a6092fSMaxime Coquelin 15548a6092fSMaxime Coquelin spin_unlock(&port->lock); 15648a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 15748a6092fSMaxime Coquelin spin_lock(&port->lock); 15848a6092fSMaxime Coquelin } 15948a6092fSMaxime Coquelin 160*34891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg) 161*34891872SAlexandre TORGUE { 162*34891872SAlexandre TORGUE struct uart_port *port = arg; 163*34891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 164*34891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 165*34891872SAlexandre TORGUE unsigned int isr; 166*34891872SAlexandre TORGUE int ret; 167*34891872SAlexandre TORGUE 168*34891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 169*34891872SAlexandre TORGUE isr, 170*34891872SAlexandre TORGUE (isr & USART_SR_TC), 171*34891872SAlexandre TORGUE 10, 100000); 172*34891872SAlexandre TORGUE 173*34891872SAlexandre TORGUE if (ret) 174*34891872SAlexandre TORGUE dev_err(port->dev, "terminal count not set\n"); 175*34891872SAlexandre TORGUE 176*34891872SAlexandre TORGUE if (ofs->icr == UNDEF_REG) 177*34891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 178*34891872SAlexandre TORGUE else 179*34891872SAlexandre TORGUE stm32_set_bits(port, ofs->icr, USART_CR_TC); 180*34891872SAlexandre TORGUE 181*34891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 182*34891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 183*34891872SAlexandre TORGUE 184*34891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 185*34891872SAlexandre TORGUE stm32_transmit_chars(port); 186*34891872SAlexandre TORGUE } 187*34891872SAlexandre TORGUE 188*34891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port) 189*34891872SAlexandre TORGUE { 190*34891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 191*34891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 192*34891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 193*34891872SAlexandre TORGUE unsigned int isr; 194*34891872SAlexandre TORGUE int ret; 195*34891872SAlexandre TORGUE 196*34891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 197*34891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 198*34891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 199*34891872SAlexandre TORGUE } 200*34891872SAlexandre TORGUE 201*34891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 202*34891872SAlexandre TORGUE isr, 203*34891872SAlexandre TORGUE (isr & USART_SR_TXE), 204*34891872SAlexandre TORGUE 10, 100); 205*34891872SAlexandre TORGUE 206*34891872SAlexandre TORGUE if (ret) 207*34891872SAlexandre TORGUE dev_err(port->dev, "tx empty not set\n"); 208*34891872SAlexandre TORGUE 209*34891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 210*34891872SAlexandre TORGUE 211*34891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 212*34891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 213*34891872SAlexandre TORGUE port->icount.tx++; 214*34891872SAlexandre TORGUE } 215*34891872SAlexandre TORGUE 216*34891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port) 217*34891872SAlexandre TORGUE { 218*34891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 219*34891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 220*34891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 221*34891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 222*34891872SAlexandre TORGUE dma_cookie_t cookie; 223*34891872SAlexandre TORGUE unsigned int count, i; 224*34891872SAlexandre TORGUE 225*34891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 226*34891872SAlexandre TORGUE return; 227*34891872SAlexandre TORGUE 228*34891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 229*34891872SAlexandre TORGUE 230*34891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 231*34891872SAlexandre TORGUE 232*34891872SAlexandre TORGUE if (count > TX_BUF_L) 233*34891872SAlexandre TORGUE count = TX_BUF_L; 234*34891872SAlexandre TORGUE 235*34891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 236*34891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 237*34891872SAlexandre TORGUE } else { 238*34891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 239*34891872SAlexandre TORGUE size_t two; 240*34891872SAlexandre TORGUE 241*34891872SAlexandre TORGUE if (one > count) 242*34891872SAlexandre TORGUE one = count; 243*34891872SAlexandre TORGUE two = count - one; 244*34891872SAlexandre TORGUE 245*34891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 246*34891872SAlexandre TORGUE if (two) 247*34891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 248*34891872SAlexandre TORGUE } 249*34891872SAlexandre TORGUE 250*34891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 251*34891872SAlexandre TORGUE stm32port->tx_dma_buf, 252*34891872SAlexandre TORGUE count, 253*34891872SAlexandre TORGUE DMA_MEM_TO_DEV, 254*34891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 255*34891872SAlexandre TORGUE 256*34891872SAlexandre TORGUE if (!desc) { 257*34891872SAlexandre TORGUE for (i = count; i > 0; i--) 258*34891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 259*34891872SAlexandre TORGUE return; 260*34891872SAlexandre TORGUE } 261*34891872SAlexandre TORGUE 262*34891872SAlexandre TORGUE desc->callback = stm32_tx_dma_complete; 263*34891872SAlexandre TORGUE desc->callback_param = port; 264*34891872SAlexandre TORGUE 265*34891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 266*34891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 267*34891872SAlexandre TORGUE 268*34891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 269*34891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 270*34891872SAlexandre TORGUE 271*34891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 272*34891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 273*34891872SAlexandre TORGUE 274*34891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 275*34891872SAlexandre TORGUE port->icount.tx += count; 276*34891872SAlexandre TORGUE } 277*34891872SAlexandre TORGUE 27848a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port) 27948a6092fSMaxime Coquelin { 280ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 281ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 28248a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 28348a6092fSMaxime Coquelin 28448a6092fSMaxime Coquelin if (port->x_char) { 285*34891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 286*34891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 287ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 28848a6092fSMaxime Coquelin port->x_char = 0; 28948a6092fSMaxime Coquelin port->icount.tx++; 290*34891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 291*34891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 29248a6092fSMaxime Coquelin return; 29348a6092fSMaxime Coquelin } 29448a6092fSMaxime Coquelin 29548a6092fSMaxime Coquelin if (uart_tx_stopped(port)) { 29648a6092fSMaxime Coquelin stm32_stop_tx(port); 29748a6092fSMaxime Coquelin return; 29848a6092fSMaxime Coquelin } 29948a6092fSMaxime Coquelin 30048a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) { 30148a6092fSMaxime Coquelin stm32_stop_tx(port); 30248a6092fSMaxime Coquelin return; 30348a6092fSMaxime Coquelin } 30448a6092fSMaxime Coquelin 305*34891872SAlexandre TORGUE if (stm32_port->tx_ch) 306*34891872SAlexandre TORGUE stm32_transmit_chars_dma(port); 307*34891872SAlexandre TORGUE else 308*34891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 30948a6092fSMaxime Coquelin 31048a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 31148a6092fSMaxime Coquelin uart_write_wakeup(port); 31248a6092fSMaxime Coquelin 31348a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 31448a6092fSMaxime Coquelin stm32_stop_tx(port); 31548a6092fSMaxime Coquelin } 31648a6092fSMaxime Coquelin 31748a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr) 31848a6092fSMaxime Coquelin { 31948a6092fSMaxime Coquelin struct uart_port *port = ptr; 320ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 321ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 32248a6092fSMaxime Coquelin u32 sr; 32348a6092fSMaxime Coquelin 324ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 32548a6092fSMaxime Coquelin 326*34891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 327*34891872SAlexandre TORGUE stm32_receive_chars(port, false); 32848a6092fSMaxime Coquelin 329*34891872SAlexandre TORGUE if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) 33048a6092fSMaxime Coquelin stm32_transmit_chars(port); 33148a6092fSMaxime Coquelin 332*34891872SAlexandre TORGUE if (stm32_port->rx_ch) 333*34891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 334*34891872SAlexandre TORGUE else 335*34891872SAlexandre TORGUE return IRQ_HANDLED; 336*34891872SAlexandre TORGUE } 337*34891872SAlexandre TORGUE 338*34891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr) 339*34891872SAlexandre TORGUE { 340*34891872SAlexandre TORGUE struct uart_port *port = ptr; 341*34891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 342*34891872SAlexandre TORGUE 343*34891872SAlexandre TORGUE spin_lock(&port->lock); 344*34891872SAlexandre TORGUE 345*34891872SAlexandre TORGUE if (stm32_port->rx_ch) 346*34891872SAlexandre TORGUE stm32_receive_chars(port, true); 347*34891872SAlexandre TORGUE 34848a6092fSMaxime Coquelin spin_unlock(&port->lock); 34948a6092fSMaxime Coquelin 35048a6092fSMaxime Coquelin return IRQ_HANDLED; 35148a6092fSMaxime Coquelin } 35248a6092fSMaxime Coquelin 35348a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port) 35448a6092fSMaxime Coquelin { 355ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 356ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 357ada8618fSAlexandre TORGUE 358ada8618fSAlexandre TORGUE return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; 35948a6092fSMaxime Coquelin } 36048a6092fSMaxime Coquelin 36148a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) 36248a6092fSMaxime Coquelin { 363ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 364ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 365ada8618fSAlexandre TORGUE 36648a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 367ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); 36848a6092fSMaxime Coquelin else 369ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 37048a6092fSMaxime Coquelin } 37148a6092fSMaxime Coquelin 37248a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port) 37348a6092fSMaxime Coquelin { 37448a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 37548a6092fSMaxime Coquelin return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 37648a6092fSMaxime Coquelin } 37748a6092fSMaxime Coquelin 37848a6092fSMaxime Coquelin /* Transmit stop */ 37948a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port) 38048a6092fSMaxime Coquelin { 381ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 382ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 383ada8618fSAlexandre TORGUE 384ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 38548a6092fSMaxime Coquelin } 38648a6092fSMaxime Coquelin 38748a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 38848a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port) 38948a6092fSMaxime Coquelin { 39048a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 39148a6092fSMaxime Coquelin 39248a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 39348a6092fSMaxime Coquelin return; 39448a6092fSMaxime Coquelin 395*34891872SAlexandre TORGUE stm32_transmit_chars(port); 39648a6092fSMaxime Coquelin } 39748a6092fSMaxime Coquelin 39848a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 39948a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port) 40048a6092fSMaxime Coquelin { 401ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 402ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 40348a6092fSMaxime Coquelin unsigned long flags; 40448a6092fSMaxime Coquelin 40548a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 406ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 40748a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 40848a6092fSMaxime Coquelin } 40948a6092fSMaxime Coquelin 41048a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 41148a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port) 41248a6092fSMaxime Coquelin { 413ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 414ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 41548a6092fSMaxime Coquelin unsigned long flags; 41648a6092fSMaxime Coquelin 41748a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 418ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE); 41948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 42048a6092fSMaxime Coquelin } 42148a6092fSMaxime Coquelin 42248a6092fSMaxime Coquelin /* Receive stop */ 42348a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port) 42448a6092fSMaxime Coquelin { 425ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 426ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 427ada8618fSAlexandre TORGUE 428ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 42948a6092fSMaxime Coquelin } 43048a6092fSMaxime Coquelin 43148a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 43248a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state) 43348a6092fSMaxime Coquelin { 43448a6092fSMaxime Coquelin } 43548a6092fSMaxime Coquelin 43648a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port) 43748a6092fSMaxime Coquelin { 438ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 439ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 44048a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 44148a6092fSMaxime Coquelin u32 val; 44248a6092fSMaxime Coquelin int ret; 44348a6092fSMaxime Coquelin 444*34891872SAlexandre TORGUE ret = request_threaded_irq(port->irq, stm32_interrupt, 445*34891872SAlexandre TORGUE stm32_threaded_interrupt, 446*34891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 44748a6092fSMaxime Coquelin if (ret) 44848a6092fSMaxime Coquelin return ret; 44948a6092fSMaxime Coquelin 45048a6092fSMaxime Coquelin val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 451ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, val); 45248a6092fSMaxime Coquelin 45348a6092fSMaxime Coquelin return 0; 45448a6092fSMaxime Coquelin } 45548a6092fSMaxime Coquelin 45648a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port) 45748a6092fSMaxime Coquelin { 458ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 459ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 46048a6092fSMaxime Coquelin u32 val; 46148a6092fSMaxime Coquelin 46248a6092fSMaxime Coquelin val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 463a14f66a4SAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, val); 46448a6092fSMaxime Coquelin 46548a6092fSMaxime Coquelin free_irq(port->irq, port); 46648a6092fSMaxime Coquelin } 46748a6092fSMaxime Coquelin 46848a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, 46948a6092fSMaxime Coquelin struct ktermios *old) 47048a6092fSMaxime Coquelin { 47148a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 472ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 473ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 47448a6092fSMaxime Coquelin unsigned int baud; 47548a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 47648a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 47748a6092fSMaxime Coquelin u32 cr1, cr2, cr3; 47848a6092fSMaxime Coquelin unsigned long flags; 47948a6092fSMaxime Coquelin 48048a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 48148a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 48248a6092fSMaxime Coquelin 48348a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 48448a6092fSMaxime Coquelin 48548a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 48648a6092fSMaxime Coquelin 48748a6092fSMaxime Coquelin /* Stop serial port and reset value */ 488ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 48948a6092fSMaxime Coquelin 490ada8618fSAlexandre TORGUE cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; 491ada8618fSAlexandre TORGUE cr1 |= BIT(cfg->uart_enable_bit); 49248a6092fSMaxime Coquelin cr2 = 0; 49348a6092fSMaxime Coquelin cr3 = 0; 49448a6092fSMaxime Coquelin 49548a6092fSMaxime Coquelin if (cflag & CSTOPB) 49648a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 49748a6092fSMaxime Coquelin 49848a6092fSMaxime Coquelin if (cflag & PARENB) { 49948a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 500ada8618fSAlexandre TORGUE if ((cflag & CSIZE) == CS8) { 501ada8618fSAlexandre TORGUE if (cfg->has_7bits_data) 502ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 503ada8618fSAlexandre TORGUE else 50448a6092fSMaxime Coquelin cr1 |= USART_CR1_M; 50548a6092fSMaxime Coquelin } 506ada8618fSAlexandre TORGUE } 50748a6092fSMaxime Coquelin 50848a6092fSMaxime Coquelin if (cflag & PARODD) 50948a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 51048a6092fSMaxime Coquelin 51148a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 51248a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 51348a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 51448a6092fSMaxime Coquelin cr3 |= USART_CR3_CTSE; 51548a6092fSMaxime Coquelin } 51648a6092fSMaxime Coquelin 51748a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 51848a6092fSMaxime Coquelin 51948a6092fSMaxime Coquelin /* 52048a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 52148a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 52248a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 52348a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 52448a6092fSMaxime Coquelin */ 52548a6092fSMaxime Coquelin if (usartdiv < 16) { 52648a6092fSMaxime Coquelin oversampling = 8; 527ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); 52848a6092fSMaxime Coquelin } else { 52948a6092fSMaxime Coquelin oversampling = 16; 530ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 53148a6092fSMaxime Coquelin } 53248a6092fSMaxime Coquelin 53348a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 53448a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 535ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 53648a6092fSMaxime Coquelin 53748a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 53848a6092fSMaxime Coquelin 53948a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 54048a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 54148a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 54248a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 54348a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_LBD; 54448a6092fSMaxime Coquelin 54548a6092fSMaxime Coquelin /* Characters to ignore */ 54648a6092fSMaxime Coquelin port->ignore_status_mask = 0; 54748a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 54848a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 54948a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 55048a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_LBD; 55148a6092fSMaxime Coquelin /* 55248a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 55348a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 55448a6092fSMaxime Coquelin */ 55548a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 55648a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 55748a6092fSMaxime Coquelin } 55848a6092fSMaxime Coquelin 55948a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 56048a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 56148a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 56248a6092fSMaxime Coquelin 563*34891872SAlexandre TORGUE if (stm32_port->rx_ch) 564*34891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 565*34891872SAlexandre TORGUE 566ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 567ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 568ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 56948a6092fSMaxime Coquelin 57048a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 57148a6092fSMaxime Coquelin } 57248a6092fSMaxime Coquelin 57348a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port) 57448a6092fSMaxime Coquelin { 57548a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 57648a6092fSMaxime Coquelin } 57748a6092fSMaxime Coquelin 57848a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port) 57948a6092fSMaxime Coquelin { 58048a6092fSMaxime Coquelin } 58148a6092fSMaxime Coquelin 58248a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port) 58348a6092fSMaxime Coquelin { 58448a6092fSMaxime Coquelin return 0; 58548a6092fSMaxime Coquelin } 58648a6092fSMaxime Coquelin 58748a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags) 58848a6092fSMaxime Coquelin { 58948a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 59048a6092fSMaxime Coquelin port->type = PORT_STM32; 59148a6092fSMaxime Coquelin } 59248a6092fSMaxime Coquelin 59348a6092fSMaxime Coquelin static int 59448a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser) 59548a6092fSMaxime Coquelin { 59648a6092fSMaxime Coquelin /* No user changeable parameters */ 59748a6092fSMaxime Coquelin return -EINVAL; 59848a6092fSMaxime Coquelin } 59948a6092fSMaxime Coquelin 60048a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state, 60148a6092fSMaxime Coquelin unsigned int oldstate) 60248a6092fSMaxime Coquelin { 60348a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 60448a6092fSMaxime Coquelin struct stm32_port, port); 605ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 606ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32port->info->cfg; 60748a6092fSMaxime Coquelin unsigned long flags = 0; 60848a6092fSMaxime Coquelin 60948a6092fSMaxime Coquelin switch (state) { 61048a6092fSMaxime Coquelin case UART_PM_STATE_ON: 61148a6092fSMaxime Coquelin clk_prepare_enable(stm32port->clk); 61248a6092fSMaxime Coquelin break; 61348a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 61448a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 615ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 61648a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 61748a6092fSMaxime Coquelin clk_disable_unprepare(stm32port->clk); 61848a6092fSMaxime Coquelin break; 61948a6092fSMaxime Coquelin } 62048a6092fSMaxime Coquelin } 62148a6092fSMaxime Coquelin 62248a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 62348a6092fSMaxime Coquelin .tx_empty = stm32_tx_empty, 62448a6092fSMaxime Coquelin .set_mctrl = stm32_set_mctrl, 62548a6092fSMaxime Coquelin .get_mctrl = stm32_get_mctrl, 62648a6092fSMaxime Coquelin .stop_tx = stm32_stop_tx, 62748a6092fSMaxime Coquelin .start_tx = stm32_start_tx, 62848a6092fSMaxime Coquelin .throttle = stm32_throttle, 62948a6092fSMaxime Coquelin .unthrottle = stm32_unthrottle, 63048a6092fSMaxime Coquelin .stop_rx = stm32_stop_rx, 63148a6092fSMaxime Coquelin .break_ctl = stm32_break_ctl, 63248a6092fSMaxime Coquelin .startup = stm32_startup, 63348a6092fSMaxime Coquelin .shutdown = stm32_shutdown, 63448a6092fSMaxime Coquelin .set_termios = stm32_set_termios, 63548a6092fSMaxime Coquelin .pm = stm32_pm, 63648a6092fSMaxime Coquelin .type = stm32_type, 63748a6092fSMaxime Coquelin .release_port = stm32_release_port, 63848a6092fSMaxime Coquelin .request_port = stm32_request_port, 63948a6092fSMaxime Coquelin .config_port = stm32_config_port, 64048a6092fSMaxime Coquelin .verify_port = stm32_verify_port, 64148a6092fSMaxime Coquelin }; 64248a6092fSMaxime Coquelin 64348a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port, 64448a6092fSMaxime Coquelin struct platform_device *pdev) 64548a6092fSMaxime Coquelin { 64648a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 64748a6092fSMaxime Coquelin struct resource *res; 64848a6092fSMaxime Coquelin int ret; 64948a6092fSMaxime Coquelin 65048a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 65148a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 65248a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 65348a6092fSMaxime Coquelin port->dev = &pdev->dev; 65448a6092fSMaxime Coquelin port->irq = platform_get_irq(pdev, 0); 65548a6092fSMaxime Coquelin 65648a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 65748a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 65848a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 65948a6092fSMaxime Coquelin return PTR_ERR(port->membase); 66048a6092fSMaxime Coquelin port->mapbase = res->start; 66148a6092fSMaxime Coquelin 66248a6092fSMaxime Coquelin spin_lock_init(&port->lock); 66348a6092fSMaxime Coquelin 66448a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 66548a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 66648a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 66748a6092fSMaxime Coquelin 66848a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 66948a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 67048a6092fSMaxime Coquelin if (ret) 67148a6092fSMaxime Coquelin return ret; 67248a6092fSMaxime Coquelin 67348a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 67448a6092fSMaxime Coquelin if (!stm32port->port.uartclk) 67548a6092fSMaxime Coquelin ret = -EINVAL; 67648a6092fSMaxime Coquelin 67748a6092fSMaxime Coquelin return ret; 67848a6092fSMaxime Coquelin } 67948a6092fSMaxime Coquelin 68048a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) 68148a6092fSMaxime Coquelin { 68248a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 68348a6092fSMaxime Coquelin int id; 68448a6092fSMaxime Coquelin 68548a6092fSMaxime Coquelin if (!np) 68648a6092fSMaxime Coquelin return NULL; 68748a6092fSMaxime Coquelin 68848a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 68948a6092fSMaxime Coquelin if (id < 0) 69048a6092fSMaxime Coquelin id = 0; 69148a6092fSMaxime Coquelin 69248a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 69348a6092fSMaxime Coquelin return NULL; 69448a6092fSMaxime Coquelin 69548a6092fSMaxime Coquelin stm32_ports[id].hw_flow_control = of_property_read_bool(np, 69659bed2dfSAlexandre TORGUE "st,hw-flow-ctrl"); 69748a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 69848a6092fSMaxime Coquelin return &stm32_ports[id]; 69948a6092fSMaxime Coquelin } 70048a6092fSMaxime Coquelin 70148a6092fSMaxime Coquelin #ifdef CONFIG_OF 70248a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 703ada8618fSAlexandre TORGUE { .compatible = "st,stm32-usart", .data = &stm32f4_info}, 704ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 705ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-usart", .data = &stm32f7_info}, 706ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 70748a6092fSMaxime Coquelin {}, 70848a6092fSMaxime Coquelin }; 70948a6092fSMaxime Coquelin 71048a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 71148a6092fSMaxime Coquelin #endif 71248a6092fSMaxime Coquelin 713*34891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port, 714*34891872SAlexandre TORGUE struct platform_device *pdev) 715*34891872SAlexandre TORGUE { 716*34891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 717*34891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 718*34891872SAlexandre TORGUE struct device *dev = &pdev->dev; 719*34891872SAlexandre TORGUE struct dma_slave_config config; 720*34891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 721*34891872SAlexandre TORGUE dma_cookie_t cookie; 722*34891872SAlexandre TORGUE int ret; 723*34891872SAlexandre TORGUE 724*34891872SAlexandre TORGUE /* Request DMA RX channel */ 725*34891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 726*34891872SAlexandre TORGUE if (!stm32port->rx_ch) { 727*34891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 728*34891872SAlexandre TORGUE return -ENODEV; 729*34891872SAlexandre TORGUE } 730*34891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 731*34891872SAlexandre TORGUE &stm32port->rx_dma_buf, 732*34891872SAlexandre TORGUE GFP_KERNEL); 733*34891872SAlexandre TORGUE if (!stm32port->rx_buf) { 734*34891872SAlexandre TORGUE ret = -ENOMEM; 735*34891872SAlexandre TORGUE goto alloc_err; 736*34891872SAlexandre TORGUE } 737*34891872SAlexandre TORGUE 738*34891872SAlexandre TORGUE /* Configure DMA channel */ 739*34891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 740*34891872SAlexandre TORGUE config.src_addr = (dma_addr_t)port->membase + ofs->rdr; 741*34891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 742*34891872SAlexandre TORGUE 743*34891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 744*34891872SAlexandre TORGUE if (ret < 0) { 745*34891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 746*34891872SAlexandre TORGUE ret = -ENODEV; 747*34891872SAlexandre TORGUE goto config_err; 748*34891872SAlexandre TORGUE } 749*34891872SAlexandre TORGUE 750*34891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 751*34891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 752*34891872SAlexandre TORGUE stm32port->rx_dma_buf, 753*34891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 754*34891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 755*34891872SAlexandre TORGUE if (!desc) { 756*34891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 757*34891872SAlexandre TORGUE ret = -ENODEV; 758*34891872SAlexandre TORGUE goto config_err; 759*34891872SAlexandre TORGUE } 760*34891872SAlexandre TORGUE 761*34891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 762*34891872SAlexandre TORGUE desc->callback = NULL; 763*34891872SAlexandre TORGUE desc->callback_param = NULL; 764*34891872SAlexandre TORGUE 765*34891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 766*34891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 767*34891872SAlexandre TORGUE 768*34891872SAlexandre TORGUE /* Issue pending DMA requests */ 769*34891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 770*34891872SAlexandre TORGUE 771*34891872SAlexandre TORGUE return 0; 772*34891872SAlexandre TORGUE 773*34891872SAlexandre TORGUE config_err: 774*34891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 775*34891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 776*34891872SAlexandre TORGUE stm32port->rx_dma_buf); 777*34891872SAlexandre TORGUE 778*34891872SAlexandre TORGUE alloc_err: 779*34891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 780*34891872SAlexandre TORGUE stm32port->rx_ch = NULL; 781*34891872SAlexandre TORGUE 782*34891872SAlexandre TORGUE return ret; 783*34891872SAlexandre TORGUE } 784*34891872SAlexandre TORGUE 785*34891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port, 786*34891872SAlexandre TORGUE struct platform_device *pdev) 787*34891872SAlexandre TORGUE { 788*34891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 789*34891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 790*34891872SAlexandre TORGUE struct device *dev = &pdev->dev; 791*34891872SAlexandre TORGUE struct dma_slave_config config; 792*34891872SAlexandre TORGUE int ret; 793*34891872SAlexandre TORGUE 794*34891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 795*34891872SAlexandre TORGUE 796*34891872SAlexandre TORGUE /* Request DMA TX channel */ 797*34891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 798*34891872SAlexandre TORGUE if (!stm32port->tx_ch) { 799*34891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 800*34891872SAlexandre TORGUE return -ENODEV; 801*34891872SAlexandre TORGUE } 802*34891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 803*34891872SAlexandre TORGUE &stm32port->tx_dma_buf, 804*34891872SAlexandre TORGUE GFP_KERNEL); 805*34891872SAlexandre TORGUE if (!stm32port->tx_buf) { 806*34891872SAlexandre TORGUE ret = -ENOMEM; 807*34891872SAlexandre TORGUE goto alloc_err; 808*34891872SAlexandre TORGUE } 809*34891872SAlexandre TORGUE 810*34891872SAlexandre TORGUE /* Configure DMA channel */ 811*34891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 812*34891872SAlexandre TORGUE config.dst_addr = (dma_addr_t)port->membase + ofs->tdr; 813*34891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 814*34891872SAlexandre TORGUE 815*34891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 816*34891872SAlexandre TORGUE if (ret < 0) { 817*34891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 818*34891872SAlexandre TORGUE ret = -ENODEV; 819*34891872SAlexandre TORGUE goto config_err; 820*34891872SAlexandre TORGUE } 821*34891872SAlexandre TORGUE 822*34891872SAlexandre TORGUE return 0; 823*34891872SAlexandre TORGUE 824*34891872SAlexandre TORGUE config_err: 825*34891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 826*34891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 827*34891872SAlexandre TORGUE stm32port->tx_dma_buf); 828*34891872SAlexandre TORGUE 829*34891872SAlexandre TORGUE alloc_err: 830*34891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 831*34891872SAlexandre TORGUE stm32port->tx_ch = NULL; 832*34891872SAlexandre TORGUE 833*34891872SAlexandre TORGUE return ret; 834*34891872SAlexandre TORGUE } 835*34891872SAlexandre TORGUE 83648a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev) 83748a6092fSMaxime Coquelin { 838ada8618fSAlexandre TORGUE const struct of_device_id *match; 83948a6092fSMaxime Coquelin struct stm32_port *stm32port; 840ada8618fSAlexandre TORGUE int ret; 84148a6092fSMaxime Coquelin 84248a6092fSMaxime Coquelin stm32port = stm32_of_get_stm32_port(pdev); 84348a6092fSMaxime Coquelin if (!stm32port) 84448a6092fSMaxime Coquelin return -ENODEV; 84548a6092fSMaxime Coquelin 846ada8618fSAlexandre TORGUE match = of_match_device(stm32_match, &pdev->dev); 847ada8618fSAlexandre TORGUE if (match && match->data) 848ada8618fSAlexandre TORGUE stm32port->info = (struct stm32_usart_info *)match->data; 849ada8618fSAlexandre TORGUE else 850ada8618fSAlexandre TORGUE return -EINVAL; 851ada8618fSAlexandre TORGUE 85248a6092fSMaxime Coquelin ret = stm32_init_port(stm32port, pdev); 85348a6092fSMaxime Coquelin if (ret) 85448a6092fSMaxime Coquelin return ret; 85548a6092fSMaxime Coquelin 85648a6092fSMaxime Coquelin ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 85748a6092fSMaxime Coquelin if (ret) 85848a6092fSMaxime Coquelin return ret; 85948a6092fSMaxime Coquelin 860*34891872SAlexandre TORGUE ret = stm32_of_dma_rx_probe(stm32port, pdev); 861*34891872SAlexandre TORGUE if (ret) 862*34891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 863*34891872SAlexandre TORGUE 864*34891872SAlexandre TORGUE ret = stm32_of_dma_tx_probe(stm32port, pdev); 865*34891872SAlexandre TORGUE if (ret) 866*34891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 867*34891872SAlexandre TORGUE 86848a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 86948a6092fSMaxime Coquelin 87048a6092fSMaxime Coquelin return 0; 87148a6092fSMaxime Coquelin } 87248a6092fSMaxime Coquelin 87348a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev) 87448a6092fSMaxime Coquelin { 87548a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 876511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 877*34891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 878*34891872SAlexandre TORGUE 879*34891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 880*34891872SAlexandre TORGUE 881*34891872SAlexandre TORGUE if (stm32_port->rx_ch) 882*34891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 883*34891872SAlexandre TORGUE 884*34891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 885*34891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 886*34891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 887*34891872SAlexandre TORGUE stm32_port->rx_dma_buf); 888*34891872SAlexandre TORGUE 889*34891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 890*34891872SAlexandre TORGUE 891*34891872SAlexandre TORGUE if (stm32_port->tx_ch) 892*34891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 893*34891872SAlexandre TORGUE 894*34891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 895*34891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 896*34891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 897*34891872SAlexandre TORGUE stm32_port->tx_dma_buf); 898511c7b1bSAlexandre TORGUE 899511c7b1bSAlexandre TORGUE clk_disable_unprepare(stm32_port->clk); 90048a6092fSMaxime Coquelin 90148a6092fSMaxime Coquelin return uart_remove_one_port(&stm32_usart_driver, port); 90248a6092fSMaxime Coquelin } 90348a6092fSMaxime Coquelin 90448a6092fSMaxime Coquelin 90548a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 90648a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch) 90748a6092fSMaxime Coquelin { 908ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 909ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 910ada8618fSAlexandre TORGUE 911ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 91248a6092fSMaxime Coquelin cpu_relax(); 91348a6092fSMaxime Coquelin 914ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 91548a6092fSMaxime Coquelin } 91648a6092fSMaxime Coquelin 91748a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt) 91848a6092fSMaxime Coquelin { 91948a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 920ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 921ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 92248a6092fSMaxime Coquelin unsigned long flags; 92348a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 92448a6092fSMaxime Coquelin int locked = 1; 92548a6092fSMaxime Coquelin 92648a6092fSMaxime Coquelin local_irq_save(flags); 92748a6092fSMaxime Coquelin if (port->sysrq) 92848a6092fSMaxime Coquelin locked = 0; 92948a6092fSMaxime Coquelin else if (oops_in_progress) 93048a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 93148a6092fSMaxime Coquelin else 93248a6092fSMaxime Coquelin spin_lock(&port->lock); 93348a6092fSMaxime Coquelin 93448a6092fSMaxime Coquelin /* Save and disable interrupts */ 935ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 93648a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 937ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 93848a6092fSMaxime Coquelin 93948a6092fSMaxime Coquelin uart_console_write(port, s, cnt, stm32_console_putchar); 94048a6092fSMaxime Coquelin 94148a6092fSMaxime Coquelin /* Restore interrupt state */ 942ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 94348a6092fSMaxime Coquelin 94448a6092fSMaxime Coquelin if (locked) 94548a6092fSMaxime Coquelin spin_unlock(&port->lock); 94648a6092fSMaxime Coquelin local_irq_restore(flags); 94748a6092fSMaxime Coquelin } 94848a6092fSMaxime Coquelin 94948a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options) 95048a6092fSMaxime Coquelin { 95148a6092fSMaxime Coquelin struct stm32_port *stm32port; 95248a6092fSMaxime Coquelin int baud = 9600; 95348a6092fSMaxime Coquelin int bits = 8; 95448a6092fSMaxime Coquelin int parity = 'n'; 95548a6092fSMaxime Coquelin int flow = 'n'; 95648a6092fSMaxime Coquelin 95748a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 95848a6092fSMaxime Coquelin return -ENODEV; 95948a6092fSMaxime Coquelin 96048a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 96148a6092fSMaxime Coquelin 96248a6092fSMaxime Coquelin /* 96348a6092fSMaxime Coquelin * This driver does not support early console initialization 96448a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 96548a6092fSMaxime Coquelin * this to be called during the uart port registration when the 96648a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 96748a6092fSMaxime Coquelin */ 96848a6092fSMaxime Coquelin if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) 96948a6092fSMaxime Coquelin return -ENXIO; 97048a6092fSMaxime Coquelin 97148a6092fSMaxime Coquelin if (options) 97248a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 97348a6092fSMaxime Coquelin 97448a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 97548a6092fSMaxime Coquelin } 97648a6092fSMaxime Coquelin 97748a6092fSMaxime Coquelin static struct console stm32_console = { 97848a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 97948a6092fSMaxime Coquelin .device = uart_console_device, 98048a6092fSMaxime Coquelin .write = stm32_console_write, 98148a6092fSMaxime Coquelin .setup = stm32_console_setup, 98248a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 98348a6092fSMaxime Coquelin .index = -1, 98448a6092fSMaxime Coquelin .data = &stm32_usart_driver, 98548a6092fSMaxime Coquelin }; 98648a6092fSMaxime Coquelin 98748a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 98848a6092fSMaxime Coquelin 98948a6092fSMaxime Coquelin #else 99048a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 99148a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 99248a6092fSMaxime Coquelin 99348a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 99448a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 99548a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 99648a6092fSMaxime Coquelin .major = 0, 99748a6092fSMaxime Coquelin .minor = 0, 99848a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 99948a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 100048a6092fSMaxime Coquelin }; 100148a6092fSMaxime Coquelin 100248a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 100348a6092fSMaxime Coquelin .probe = stm32_serial_probe, 100448a6092fSMaxime Coquelin .remove = stm32_serial_remove, 100548a6092fSMaxime Coquelin .driver = { 100648a6092fSMaxime Coquelin .name = DRIVER_NAME, 100748a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 100848a6092fSMaxime Coquelin }, 100948a6092fSMaxime Coquelin }; 101048a6092fSMaxime Coquelin 101148a6092fSMaxime Coquelin static int __init usart_init(void) 101248a6092fSMaxime Coquelin { 101348a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 101448a6092fSMaxime Coquelin int ret; 101548a6092fSMaxime Coquelin 101648a6092fSMaxime Coquelin pr_info("%s\n", banner); 101748a6092fSMaxime Coquelin 101848a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 101948a6092fSMaxime Coquelin if (ret) 102048a6092fSMaxime Coquelin return ret; 102148a6092fSMaxime Coquelin 102248a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 102348a6092fSMaxime Coquelin if (ret) 102448a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 102548a6092fSMaxime Coquelin 102648a6092fSMaxime Coquelin return ret; 102748a6092fSMaxime Coquelin } 102848a6092fSMaxime Coquelin 102948a6092fSMaxime Coquelin static void __exit usart_exit(void) 103048a6092fSMaxime Coquelin { 103148a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 103248a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 103348a6092fSMaxime Coquelin } 103448a6092fSMaxime Coquelin 103548a6092fSMaxime Coquelin module_init(usart_init); 103648a6092fSMaxime Coquelin module_exit(usart_exit); 103748a6092fSMaxime Coquelin 103848a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 103948a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 104048a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1041