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 1434891872SAlexandre TORGUE #include <linux/clk.h> 1548a6092fSMaxime Coquelin #include <linux/console.h> 1648a6092fSMaxime Coquelin #include <linux/delay.h> 1734891872SAlexandre TORGUE #include <linux/dma-direction.h> 1834891872SAlexandre TORGUE #include <linux/dmaengine.h> 1934891872SAlexandre TORGUE #include <linux/dma-mapping.h> 2034891872SAlexandre TORGUE #include <linux/io.h> 2134891872SAlexandre TORGUE #include <linux/iopoll.h> 2234891872SAlexandre TORGUE #include <linux/irq.h> 2334891872SAlexandre TORGUE #include <linux/module.h> 2448a6092fSMaxime Coquelin #include <linux/of.h> 2548a6092fSMaxime Coquelin #include <linux/of_platform.h> 2634891872SAlexandre TORGUE #include <linux/platform_device.h> 2734891872SAlexandre TORGUE #include <linux/pm_runtime.h> 2848a6092fSMaxime Coquelin #include <linux/serial_core.h> 2934891872SAlexandre TORGUE #include <linux/serial.h> 3034891872SAlexandre TORGUE #include <linux/spinlock.h> 3134891872SAlexandre TORGUE #include <linux/sysrq.h> 3234891872SAlexandre TORGUE #include <linux/tty_flip.h> 3334891872SAlexandre 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); 3834891872SAlexandre TORGUE static void stm32_transmit_chars(struct uart_port *port); 3948a6092fSMaxime Coquelin 4048a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 4148a6092fSMaxime Coquelin { 4248a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 4348a6092fSMaxime Coquelin } 4448a6092fSMaxime Coquelin 4548a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits) 4648a6092fSMaxime Coquelin { 4748a6092fSMaxime Coquelin u32 val; 4848a6092fSMaxime Coquelin 4948a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5048a6092fSMaxime Coquelin val |= bits; 5148a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 5248a6092fSMaxime Coquelin } 5348a6092fSMaxime Coquelin 5448a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits) 5548a6092fSMaxime Coquelin { 5648a6092fSMaxime Coquelin u32 val; 5748a6092fSMaxime Coquelin 5848a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5948a6092fSMaxime Coquelin val &= ~bits; 6048a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 6148a6092fSMaxime Coquelin } 6248a6092fSMaxime Coquelin 6334891872SAlexandre TORGUE int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res, 6434891872SAlexandre TORGUE bool threaded) 6534891872SAlexandre TORGUE { 6634891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 6734891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 6834891872SAlexandre TORGUE enum dma_status status; 6934891872SAlexandre TORGUE struct dma_tx_state state; 7034891872SAlexandre TORGUE 7134891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 7234891872SAlexandre TORGUE 7334891872SAlexandre TORGUE if (threaded && stm32_port->rx_ch) { 7434891872SAlexandre TORGUE status = dmaengine_tx_status(stm32_port->rx_ch, 7534891872SAlexandre TORGUE stm32_port->rx_ch->cookie, 7634891872SAlexandre TORGUE &state); 7734891872SAlexandre TORGUE if ((status == DMA_IN_PROGRESS) && 7834891872SAlexandre TORGUE (*last_res != state.residue)) 7934891872SAlexandre TORGUE return 1; 8034891872SAlexandre TORGUE else 8134891872SAlexandre TORGUE return 0; 8234891872SAlexandre TORGUE } else if (*sr & USART_SR_RXNE) { 8334891872SAlexandre TORGUE return 1; 8434891872SAlexandre TORGUE } 8534891872SAlexandre TORGUE return 0; 8634891872SAlexandre TORGUE } 8734891872SAlexandre TORGUE 8834891872SAlexandre TORGUE unsigned long stm32_get_char(struct uart_port *port, u32 *sr, int *last_res) 8934891872SAlexandre TORGUE { 9034891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 9134891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 9234891872SAlexandre TORGUE unsigned long c; 9334891872SAlexandre TORGUE 9434891872SAlexandre TORGUE if (stm32_port->rx_ch) { 9534891872SAlexandre TORGUE c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; 9634891872SAlexandre TORGUE if ((*last_res) == 0) 9734891872SAlexandre TORGUE *last_res = RX_BUF_L; 9834891872SAlexandre TORGUE return c; 9934891872SAlexandre TORGUE } else { 10034891872SAlexandre TORGUE return readl_relaxed(port->membase + ofs->rdr); 10134891872SAlexandre TORGUE } 10234891872SAlexandre TORGUE } 10334891872SAlexandre TORGUE 10434891872SAlexandre 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; 11234891872SAlexandre 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 11734891872SAlexandre TORGUE while (stm32_pending_rx(port, &sr, &last_res, threaded)) { 11848a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 11934891872SAlexandre 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 16034891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg) 16134891872SAlexandre TORGUE { 16234891872SAlexandre TORGUE struct uart_port *port = arg; 16334891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 16434891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 16534891872SAlexandre TORGUE unsigned int isr; 16634891872SAlexandre TORGUE int ret; 16734891872SAlexandre TORGUE 16834891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 16934891872SAlexandre TORGUE isr, 17034891872SAlexandre TORGUE (isr & USART_SR_TC), 17134891872SAlexandre TORGUE 10, 100000); 17234891872SAlexandre TORGUE 17334891872SAlexandre TORGUE if (ret) 17434891872SAlexandre TORGUE dev_err(port->dev, "terminal count not set\n"); 17534891872SAlexandre TORGUE 17634891872SAlexandre TORGUE if (ofs->icr == UNDEF_REG) 17734891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 17834891872SAlexandre TORGUE else 17934891872SAlexandre TORGUE stm32_set_bits(port, ofs->icr, USART_CR_TC); 18034891872SAlexandre TORGUE 18134891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 18234891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 18334891872SAlexandre TORGUE 18434891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 18534891872SAlexandre TORGUE stm32_transmit_chars(port); 18634891872SAlexandre TORGUE } 18734891872SAlexandre TORGUE 18834891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port) 18934891872SAlexandre TORGUE { 19034891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 19134891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 19234891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 19334891872SAlexandre TORGUE unsigned int isr; 19434891872SAlexandre TORGUE int ret; 19534891872SAlexandre TORGUE 19634891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 19734891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 19834891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 19934891872SAlexandre TORGUE } 20034891872SAlexandre TORGUE 20134891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 20234891872SAlexandre TORGUE isr, 20334891872SAlexandre TORGUE (isr & USART_SR_TXE), 20434891872SAlexandre TORGUE 10, 100); 20534891872SAlexandre TORGUE 20634891872SAlexandre TORGUE if (ret) 20734891872SAlexandre TORGUE dev_err(port->dev, "tx empty not set\n"); 20834891872SAlexandre TORGUE 20934891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 21034891872SAlexandre TORGUE 21134891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 21234891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 21334891872SAlexandre TORGUE port->icount.tx++; 21434891872SAlexandre TORGUE } 21534891872SAlexandre TORGUE 21634891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port) 21734891872SAlexandre TORGUE { 21834891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 21934891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 22034891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 22134891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 22234891872SAlexandre TORGUE dma_cookie_t cookie; 22334891872SAlexandre TORGUE unsigned int count, i; 22434891872SAlexandre TORGUE 22534891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 22634891872SAlexandre TORGUE return; 22734891872SAlexandre TORGUE 22834891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 22934891872SAlexandre TORGUE 23034891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 23134891872SAlexandre TORGUE 23234891872SAlexandre TORGUE if (count > TX_BUF_L) 23334891872SAlexandre TORGUE count = TX_BUF_L; 23434891872SAlexandre TORGUE 23534891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 23634891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 23734891872SAlexandre TORGUE } else { 23834891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 23934891872SAlexandre TORGUE size_t two; 24034891872SAlexandre TORGUE 24134891872SAlexandre TORGUE if (one > count) 24234891872SAlexandre TORGUE one = count; 24334891872SAlexandre TORGUE two = count - one; 24434891872SAlexandre TORGUE 24534891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 24634891872SAlexandre TORGUE if (two) 24734891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 24834891872SAlexandre TORGUE } 24934891872SAlexandre TORGUE 25034891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 25134891872SAlexandre TORGUE stm32port->tx_dma_buf, 25234891872SAlexandre TORGUE count, 25334891872SAlexandre TORGUE DMA_MEM_TO_DEV, 25434891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 25534891872SAlexandre TORGUE 25634891872SAlexandre TORGUE if (!desc) { 25734891872SAlexandre TORGUE for (i = count; i > 0; i--) 25834891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 25934891872SAlexandre TORGUE return; 26034891872SAlexandre TORGUE } 26134891872SAlexandre TORGUE 26234891872SAlexandre TORGUE desc->callback = stm32_tx_dma_complete; 26334891872SAlexandre TORGUE desc->callback_param = port; 26434891872SAlexandre TORGUE 26534891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 26634891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 26734891872SAlexandre TORGUE 26834891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 26934891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 27034891872SAlexandre TORGUE 27134891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 27234891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 27334891872SAlexandre TORGUE 27434891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 27534891872SAlexandre TORGUE port->icount.tx += count; 27634891872SAlexandre TORGUE } 27734891872SAlexandre 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) { 28534891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 28634891872SAlexandre 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++; 29034891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 29134891872SAlexandre 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 30534891872SAlexandre TORGUE if (stm32_port->tx_ch) 30634891872SAlexandre TORGUE stm32_transmit_chars_dma(port); 30734891872SAlexandre TORGUE else 30834891872SAlexandre 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 32401d32d71SAlexandre TORGUE spin_lock(&port->lock); 32501d32d71SAlexandre TORGUE 326ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 32748a6092fSMaxime Coquelin 32834891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 32934891872SAlexandre TORGUE stm32_receive_chars(port, false); 33048a6092fSMaxime Coquelin 33134891872SAlexandre TORGUE if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) 33248a6092fSMaxime Coquelin stm32_transmit_chars(port); 33348a6092fSMaxime Coquelin 33401d32d71SAlexandre TORGUE spin_unlock(&port->lock); 33501d32d71SAlexandre TORGUE 33634891872SAlexandre TORGUE if (stm32_port->rx_ch) 33734891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 33834891872SAlexandre TORGUE else 33934891872SAlexandre TORGUE return IRQ_HANDLED; 34034891872SAlexandre TORGUE } 34134891872SAlexandre TORGUE 34234891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr) 34334891872SAlexandre TORGUE { 34434891872SAlexandre TORGUE struct uart_port *port = ptr; 34534891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 34634891872SAlexandre TORGUE 34734891872SAlexandre TORGUE spin_lock(&port->lock); 34834891872SAlexandre TORGUE 34934891872SAlexandre TORGUE if (stm32_port->rx_ch) 35034891872SAlexandre TORGUE stm32_receive_chars(port, true); 35134891872SAlexandre TORGUE 35248a6092fSMaxime Coquelin spin_unlock(&port->lock); 35348a6092fSMaxime Coquelin 35448a6092fSMaxime Coquelin return IRQ_HANDLED; 35548a6092fSMaxime Coquelin } 35648a6092fSMaxime Coquelin 35748a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port) 35848a6092fSMaxime Coquelin { 359ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 360ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 361ada8618fSAlexandre TORGUE 362ada8618fSAlexandre TORGUE return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; 36348a6092fSMaxime Coquelin } 36448a6092fSMaxime Coquelin 36548a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) 36648a6092fSMaxime Coquelin { 367ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 368ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 369ada8618fSAlexandre TORGUE 37048a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 371ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); 37248a6092fSMaxime Coquelin else 373ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 37448a6092fSMaxime Coquelin } 37548a6092fSMaxime Coquelin 37648a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port) 37748a6092fSMaxime Coquelin { 37848a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 37948a6092fSMaxime Coquelin return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 38048a6092fSMaxime Coquelin } 38148a6092fSMaxime Coquelin 38248a6092fSMaxime Coquelin /* Transmit stop */ 38348a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port) 38448a6092fSMaxime Coquelin { 385ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 386ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 387ada8618fSAlexandre TORGUE 388ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 38948a6092fSMaxime Coquelin } 39048a6092fSMaxime Coquelin 39148a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 39248a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port) 39348a6092fSMaxime Coquelin { 39448a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 39548a6092fSMaxime Coquelin 39648a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 39748a6092fSMaxime Coquelin return; 39848a6092fSMaxime Coquelin 39934891872SAlexandre TORGUE stm32_transmit_chars(port); 40048a6092fSMaxime Coquelin } 40148a6092fSMaxime Coquelin 40248a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 40348a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port) 40448a6092fSMaxime Coquelin { 405ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 406ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 40748a6092fSMaxime Coquelin unsigned long flags; 40848a6092fSMaxime Coquelin 40948a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 410ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 41148a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 41248a6092fSMaxime Coquelin } 41348a6092fSMaxime Coquelin 41448a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 41548a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port) 41648a6092fSMaxime Coquelin { 417ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 418ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 41948a6092fSMaxime Coquelin unsigned long flags; 42048a6092fSMaxime Coquelin 42148a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 422ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE); 42348a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 42448a6092fSMaxime Coquelin } 42548a6092fSMaxime Coquelin 42648a6092fSMaxime Coquelin /* Receive stop */ 42748a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port) 42848a6092fSMaxime Coquelin { 429ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 430ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 431ada8618fSAlexandre TORGUE 432ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 43348a6092fSMaxime Coquelin } 43448a6092fSMaxime Coquelin 43548a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 43648a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state) 43748a6092fSMaxime Coquelin { 43848a6092fSMaxime Coquelin } 43948a6092fSMaxime Coquelin 44048a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port) 44148a6092fSMaxime Coquelin { 442ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 443ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 44448a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 44548a6092fSMaxime Coquelin u32 val; 44648a6092fSMaxime Coquelin int ret; 44748a6092fSMaxime Coquelin 44834891872SAlexandre TORGUE ret = request_threaded_irq(port->irq, stm32_interrupt, 44934891872SAlexandre TORGUE stm32_threaded_interrupt, 45034891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 45148a6092fSMaxime Coquelin if (ret) 45248a6092fSMaxime Coquelin return ret; 45348a6092fSMaxime Coquelin 45448a6092fSMaxime Coquelin val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 455ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, val); 45648a6092fSMaxime Coquelin 45748a6092fSMaxime Coquelin return 0; 45848a6092fSMaxime Coquelin } 45948a6092fSMaxime Coquelin 46048a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port) 46148a6092fSMaxime Coquelin { 462ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 463ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 46487f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 46548a6092fSMaxime Coquelin u32 val; 46648a6092fSMaxime Coquelin 46748a6092fSMaxime Coquelin val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 46887f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 469a14f66a4SAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, val); 47048a6092fSMaxime Coquelin 47148a6092fSMaxime Coquelin free_irq(port->irq, port); 47248a6092fSMaxime Coquelin } 47348a6092fSMaxime Coquelin 47448a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, 47548a6092fSMaxime Coquelin struct ktermios *old) 47648a6092fSMaxime Coquelin { 47748a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 478ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 479ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 48048a6092fSMaxime Coquelin unsigned int baud; 48148a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 48248a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 48348a6092fSMaxime Coquelin u32 cr1, cr2, cr3; 48448a6092fSMaxime Coquelin unsigned long flags; 48548a6092fSMaxime Coquelin 48648a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 48748a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 48848a6092fSMaxime Coquelin 48948a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 49048a6092fSMaxime Coquelin 49148a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 49248a6092fSMaxime Coquelin 49348a6092fSMaxime Coquelin /* Stop serial port and reset value */ 494ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 49548a6092fSMaxime Coquelin 496ada8618fSAlexandre TORGUE cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; 497ada8618fSAlexandre TORGUE cr1 |= BIT(cfg->uart_enable_bit); 49848a6092fSMaxime Coquelin cr2 = 0; 49948a6092fSMaxime Coquelin cr3 = 0; 50048a6092fSMaxime Coquelin 50148a6092fSMaxime Coquelin if (cflag & CSTOPB) 50248a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 50348a6092fSMaxime Coquelin 50448a6092fSMaxime Coquelin if (cflag & PARENB) { 50548a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 506ada8618fSAlexandre TORGUE if ((cflag & CSIZE) == CS8) { 507ada8618fSAlexandre TORGUE if (cfg->has_7bits_data) 508ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 509ada8618fSAlexandre TORGUE else 51048a6092fSMaxime Coquelin cr1 |= USART_CR1_M; 51148a6092fSMaxime Coquelin } 512ada8618fSAlexandre TORGUE } 51348a6092fSMaxime Coquelin 51448a6092fSMaxime Coquelin if (cflag & PARODD) 51548a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 51648a6092fSMaxime Coquelin 51748a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 51848a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 51948a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 52048a6092fSMaxime Coquelin cr3 |= USART_CR3_CTSE; 52148a6092fSMaxime Coquelin } 52248a6092fSMaxime Coquelin 52348a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 52448a6092fSMaxime Coquelin 52548a6092fSMaxime Coquelin /* 52648a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 52748a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 52848a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 52948a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 53048a6092fSMaxime Coquelin */ 53148a6092fSMaxime Coquelin if (usartdiv < 16) { 53248a6092fSMaxime Coquelin oversampling = 8; 533ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); 53448a6092fSMaxime Coquelin } else { 53548a6092fSMaxime Coquelin oversampling = 16; 536ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 53748a6092fSMaxime Coquelin } 53848a6092fSMaxime Coquelin 53948a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 54048a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 541ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 54248a6092fSMaxime Coquelin 54348a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 54448a6092fSMaxime Coquelin 54548a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 54648a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 54748a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 54848a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 54948a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_LBD; 55048a6092fSMaxime Coquelin 55148a6092fSMaxime Coquelin /* Characters to ignore */ 55248a6092fSMaxime Coquelin port->ignore_status_mask = 0; 55348a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 55448a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 55548a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 55648a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_LBD; 55748a6092fSMaxime Coquelin /* 55848a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 55948a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 56048a6092fSMaxime Coquelin */ 56148a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 56248a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 56348a6092fSMaxime Coquelin } 56448a6092fSMaxime Coquelin 56548a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 56648a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 56748a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 56848a6092fSMaxime Coquelin 56934891872SAlexandre TORGUE if (stm32_port->rx_ch) 57034891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 57134891872SAlexandre TORGUE 572ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 573ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 574ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 57548a6092fSMaxime Coquelin 57648a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 57748a6092fSMaxime Coquelin } 57848a6092fSMaxime Coquelin 57948a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port) 58048a6092fSMaxime Coquelin { 58148a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 58248a6092fSMaxime Coquelin } 58348a6092fSMaxime Coquelin 58448a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port) 58548a6092fSMaxime Coquelin { 58648a6092fSMaxime Coquelin } 58748a6092fSMaxime Coquelin 58848a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port) 58948a6092fSMaxime Coquelin { 59048a6092fSMaxime Coquelin return 0; 59148a6092fSMaxime Coquelin } 59248a6092fSMaxime Coquelin 59348a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags) 59448a6092fSMaxime Coquelin { 59548a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 59648a6092fSMaxime Coquelin port->type = PORT_STM32; 59748a6092fSMaxime Coquelin } 59848a6092fSMaxime Coquelin 59948a6092fSMaxime Coquelin static int 60048a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser) 60148a6092fSMaxime Coquelin { 60248a6092fSMaxime Coquelin /* No user changeable parameters */ 60348a6092fSMaxime Coquelin return -EINVAL; 60448a6092fSMaxime Coquelin } 60548a6092fSMaxime Coquelin 60648a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state, 60748a6092fSMaxime Coquelin unsigned int oldstate) 60848a6092fSMaxime Coquelin { 60948a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 61048a6092fSMaxime Coquelin struct stm32_port, port); 611ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 612ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32port->info->cfg; 61348a6092fSMaxime Coquelin unsigned long flags = 0; 61448a6092fSMaxime Coquelin 61548a6092fSMaxime Coquelin switch (state) { 61648a6092fSMaxime Coquelin case UART_PM_STATE_ON: 61748a6092fSMaxime Coquelin clk_prepare_enable(stm32port->clk); 61848a6092fSMaxime Coquelin break; 61948a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 62048a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 621ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 62248a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 62348a6092fSMaxime Coquelin clk_disable_unprepare(stm32port->clk); 62448a6092fSMaxime Coquelin break; 62548a6092fSMaxime Coquelin } 62648a6092fSMaxime Coquelin } 62748a6092fSMaxime Coquelin 62848a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 62948a6092fSMaxime Coquelin .tx_empty = stm32_tx_empty, 63048a6092fSMaxime Coquelin .set_mctrl = stm32_set_mctrl, 63148a6092fSMaxime Coquelin .get_mctrl = stm32_get_mctrl, 63248a6092fSMaxime Coquelin .stop_tx = stm32_stop_tx, 63348a6092fSMaxime Coquelin .start_tx = stm32_start_tx, 63448a6092fSMaxime Coquelin .throttle = stm32_throttle, 63548a6092fSMaxime Coquelin .unthrottle = stm32_unthrottle, 63648a6092fSMaxime Coquelin .stop_rx = stm32_stop_rx, 63748a6092fSMaxime Coquelin .break_ctl = stm32_break_ctl, 63848a6092fSMaxime Coquelin .startup = stm32_startup, 63948a6092fSMaxime Coquelin .shutdown = stm32_shutdown, 64048a6092fSMaxime Coquelin .set_termios = stm32_set_termios, 64148a6092fSMaxime Coquelin .pm = stm32_pm, 64248a6092fSMaxime Coquelin .type = stm32_type, 64348a6092fSMaxime Coquelin .release_port = stm32_release_port, 64448a6092fSMaxime Coquelin .request_port = stm32_request_port, 64548a6092fSMaxime Coquelin .config_port = stm32_config_port, 64648a6092fSMaxime Coquelin .verify_port = stm32_verify_port, 64748a6092fSMaxime Coquelin }; 64848a6092fSMaxime Coquelin 64948a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port, 65048a6092fSMaxime Coquelin struct platform_device *pdev) 65148a6092fSMaxime Coquelin { 65248a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 65348a6092fSMaxime Coquelin struct resource *res; 65448a6092fSMaxime Coquelin int ret; 65548a6092fSMaxime Coquelin 65648a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 65748a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 65848a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 65948a6092fSMaxime Coquelin port->dev = &pdev->dev; 66048a6092fSMaxime Coquelin port->irq = platform_get_irq(pdev, 0); 66148a6092fSMaxime Coquelin 66248a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 66348a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 66448a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 66548a6092fSMaxime Coquelin return PTR_ERR(port->membase); 66648a6092fSMaxime Coquelin port->mapbase = res->start; 66748a6092fSMaxime Coquelin 66848a6092fSMaxime Coquelin spin_lock_init(&port->lock); 66948a6092fSMaxime Coquelin 67048a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 67148a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 67248a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 67348a6092fSMaxime Coquelin 67448a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 67548a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 67648a6092fSMaxime Coquelin if (ret) 67748a6092fSMaxime Coquelin return ret; 67848a6092fSMaxime Coquelin 67948a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 68048a6092fSMaxime Coquelin if (!stm32port->port.uartclk) 68148a6092fSMaxime Coquelin ret = -EINVAL; 68248a6092fSMaxime Coquelin 68348a6092fSMaxime Coquelin return ret; 68448a6092fSMaxime Coquelin } 68548a6092fSMaxime Coquelin 68648a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) 68748a6092fSMaxime Coquelin { 68848a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 68948a6092fSMaxime Coquelin int id; 69048a6092fSMaxime Coquelin 69148a6092fSMaxime Coquelin if (!np) 69248a6092fSMaxime Coquelin return NULL; 69348a6092fSMaxime Coquelin 69448a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 69548a6092fSMaxime Coquelin if (id < 0) 69648a6092fSMaxime Coquelin id = 0; 69748a6092fSMaxime Coquelin 69848a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 69948a6092fSMaxime Coquelin return NULL; 70048a6092fSMaxime Coquelin 70148a6092fSMaxime Coquelin stm32_ports[id].hw_flow_control = of_property_read_bool(np, 70259bed2dfSAlexandre TORGUE "st,hw-flow-ctrl"); 70348a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 70448a6092fSMaxime Coquelin return &stm32_ports[id]; 70548a6092fSMaxime Coquelin } 70648a6092fSMaxime Coquelin 70748a6092fSMaxime Coquelin #ifdef CONFIG_OF 70848a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 709ada8618fSAlexandre TORGUE { .compatible = "st,stm32-usart", .data = &stm32f4_info}, 710ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 711ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-usart", .data = &stm32f7_info}, 712ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 71348a6092fSMaxime Coquelin {}, 71448a6092fSMaxime Coquelin }; 71548a6092fSMaxime Coquelin 71648a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 71748a6092fSMaxime Coquelin #endif 71848a6092fSMaxime Coquelin 71934891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port, 72034891872SAlexandre TORGUE struct platform_device *pdev) 72134891872SAlexandre TORGUE { 72234891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 72334891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 72434891872SAlexandre TORGUE struct device *dev = &pdev->dev; 72534891872SAlexandre TORGUE struct dma_slave_config config; 72634891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 72734891872SAlexandre TORGUE dma_cookie_t cookie; 72834891872SAlexandre TORGUE int ret; 72934891872SAlexandre TORGUE 73034891872SAlexandre TORGUE /* Request DMA RX channel */ 73134891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 73234891872SAlexandre TORGUE if (!stm32port->rx_ch) { 73334891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 73434891872SAlexandre TORGUE return -ENODEV; 73534891872SAlexandre TORGUE } 73634891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 73734891872SAlexandre TORGUE &stm32port->rx_dma_buf, 73834891872SAlexandre TORGUE GFP_KERNEL); 73934891872SAlexandre TORGUE if (!stm32port->rx_buf) { 74034891872SAlexandre TORGUE ret = -ENOMEM; 74134891872SAlexandre TORGUE goto alloc_err; 74234891872SAlexandre TORGUE } 74334891872SAlexandre TORGUE 74434891872SAlexandre TORGUE /* Configure DMA channel */ 74534891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 746*8e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 74734891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 74834891872SAlexandre TORGUE 74934891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 75034891872SAlexandre TORGUE if (ret < 0) { 75134891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 75234891872SAlexandre TORGUE ret = -ENODEV; 75334891872SAlexandre TORGUE goto config_err; 75434891872SAlexandre TORGUE } 75534891872SAlexandre TORGUE 75634891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 75734891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 75834891872SAlexandre TORGUE stm32port->rx_dma_buf, 75934891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 76034891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 76134891872SAlexandre TORGUE if (!desc) { 76234891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 76334891872SAlexandre TORGUE ret = -ENODEV; 76434891872SAlexandre TORGUE goto config_err; 76534891872SAlexandre TORGUE } 76634891872SAlexandre TORGUE 76734891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 76834891872SAlexandre TORGUE desc->callback = NULL; 76934891872SAlexandre TORGUE desc->callback_param = NULL; 77034891872SAlexandre TORGUE 77134891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 77234891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 77334891872SAlexandre TORGUE 77434891872SAlexandre TORGUE /* Issue pending DMA requests */ 77534891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 77634891872SAlexandre TORGUE 77734891872SAlexandre TORGUE return 0; 77834891872SAlexandre TORGUE 77934891872SAlexandre TORGUE config_err: 78034891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 78134891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 78234891872SAlexandre TORGUE stm32port->rx_dma_buf); 78334891872SAlexandre TORGUE 78434891872SAlexandre TORGUE alloc_err: 78534891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 78634891872SAlexandre TORGUE stm32port->rx_ch = NULL; 78734891872SAlexandre TORGUE 78834891872SAlexandre TORGUE return ret; 78934891872SAlexandre TORGUE } 79034891872SAlexandre TORGUE 79134891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port, 79234891872SAlexandre TORGUE struct platform_device *pdev) 79334891872SAlexandre TORGUE { 79434891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 79534891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 79634891872SAlexandre TORGUE struct device *dev = &pdev->dev; 79734891872SAlexandre TORGUE struct dma_slave_config config; 79834891872SAlexandre TORGUE int ret; 79934891872SAlexandre TORGUE 80034891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 80134891872SAlexandre TORGUE 80234891872SAlexandre TORGUE /* Request DMA TX channel */ 80334891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 80434891872SAlexandre TORGUE if (!stm32port->tx_ch) { 80534891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 80634891872SAlexandre TORGUE return -ENODEV; 80734891872SAlexandre TORGUE } 80834891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 80934891872SAlexandre TORGUE &stm32port->tx_dma_buf, 81034891872SAlexandre TORGUE GFP_KERNEL); 81134891872SAlexandre TORGUE if (!stm32port->tx_buf) { 81234891872SAlexandre TORGUE ret = -ENOMEM; 81334891872SAlexandre TORGUE goto alloc_err; 81434891872SAlexandre TORGUE } 81534891872SAlexandre TORGUE 81634891872SAlexandre TORGUE /* Configure DMA channel */ 81734891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 818*8e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 81934891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 82034891872SAlexandre TORGUE 82134891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 82234891872SAlexandre TORGUE if (ret < 0) { 82334891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 82434891872SAlexandre TORGUE ret = -ENODEV; 82534891872SAlexandre TORGUE goto config_err; 82634891872SAlexandre TORGUE } 82734891872SAlexandre TORGUE 82834891872SAlexandre TORGUE return 0; 82934891872SAlexandre TORGUE 83034891872SAlexandre TORGUE config_err: 83134891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 83234891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 83334891872SAlexandre TORGUE stm32port->tx_dma_buf); 83434891872SAlexandre TORGUE 83534891872SAlexandre TORGUE alloc_err: 83634891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 83734891872SAlexandre TORGUE stm32port->tx_ch = NULL; 83834891872SAlexandre TORGUE 83934891872SAlexandre TORGUE return ret; 84034891872SAlexandre TORGUE } 84134891872SAlexandre TORGUE 84248a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev) 84348a6092fSMaxime Coquelin { 844ada8618fSAlexandre TORGUE const struct of_device_id *match; 84548a6092fSMaxime Coquelin struct stm32_port *stm32port; 846ada8618fSAlexandre TORGUE int ret; 84748a6092fSMaxime Coquelin 84848a6092fSMaxime Coquelin stm32port = stm32_of_get_stm32_port(pdev); 84948a6092fSMaxime Coquelin if (!stm32port) 85048a6092fSMaxime Coquelin return -ENODEV; 85148a6092fSMaxime Coquelin 852ada8618fSAlexandre TORGUE match = of_match_device(stm32_match, &pdev->dev); 853ada8618fSAlexandre TORGUE if (match && match->data) 854ada8618fSAlexandre TORGUE stm32port->info = (struct stm32_usart_info *)match->data; 855ada8618fSAlexandre TORGUE else 856ada8618fSAlexandre TORGUE return -EINVAL; 857ada8618fSAlexandre TORGUE 85848a6092fSMaxime Coquelin ret = stm32_init_port(stm32port, pdev); 85948a6092fSMaxime Coquelin if (ret) 86048a6092fSMaxime Coquelin return ret; 86148a6092fSMaxime Coquelin 86248a6092fSMaxime Coquelin ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 86348a6092fSMaxime Coquelin if (ret) 86448a6092fSMaxime Coquelin return ret; 86548a6092fSMaxime Coquelin 86634891872SAlexandre TORGUE ret = stm32_of_dma_rx_probe(stm32port, pdev); 86734891872SAlexandre TORGUE if (ret) 86834891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 86934891872SAlexandre TORGUE 87034891872SAlexandre TORGUE ret = stm32_of_dma_tx_probe(stm32port, pdev); 87134891872SAlexandre TORGUE if (ret) 87234891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 87334891872SAlexandre TORGUE 87448a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 87548a6092fSMaxime Coquelin 87648a6092fSMaxime Coquelin return 0; 87748a6092fSMaxime Coquelin } 87848a6092fSMaxime Coquelin 87948a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev) 88048a6092fSMaxime Coquelin { 88148a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 882511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 88334891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 88434891872SAlexandre TORGUE 88534891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 88634891872SAlexandre TORGUE 88734891872SAlexandre TORGUE if (stm32_port->rx_ch) 88834891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 88934891872SAlexandre TORGUE 89034891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 89134891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 89234891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 89334891872SAlexandre TORGUE stm32_port->rx_dma_buf); 89434891872SAlexandre TORGUE 89534891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 89634891872SAlexandre TORGUE 89734891872SAlexandre TORGUE if (stm32_port->tx_ch) 89834891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 89934891872SAlexandre TORGUE 90034891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 90134891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 90234891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 90334891872SAlexandre TORGUE stm32_port->tx_dma_buf); 904511c7b1bSAlexandre TORGUE 905511c7b1bSAlexandre TORGUE clk_disable_unprepare(stm32_port->clk); 90648a6092fSMaxime Coquelin 90748a6092fSMaxime Coquelin return uart_remove_one_port(&stm32_usart_driver, port); 90848a6092fSMaxime Coquelin } 90948a6092fSMaxime Coquelin 91048a6092fSMaxime Coquelin 91148a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 91248a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch) 91348a6092fSMaxime Coquelin { 914ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 915ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 916ada8618fSAlexandre TORGUE 917ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 91848a6092fSMaxime Coquelin cpu_relax(); 91948a6092fSMaxime Coquelin 920ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 92148a6092fSMaxime Coquelin } 92248a6092fSMaxime Coquelin 92348a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt) 92448a6092fSMaxime Coquelin { 92548a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 926ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 927ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 92887f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 92948a6092fSMaxime Coquelin unsigned long flags; 93048a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 93148a6092fSMaxime Coquelin int locked = 1; 93248a6092fSMaxime Coquelin 93348a6092fSMaxime Coquelin local_irq_save(flags); 93448a6092fSMaxime Coquelin if (port->sysrq) 93548a6092fSMaxime Coquelin locked = 0; 93648a6092fSMaxime Coquelin else if (oops_in_progress) 93748a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 93848a6092fSMaxime Coquelin else 93948a6092fSMaxime Coquelin spin_lock(&port->lock); 94048a6092fSMaxime Coquelin 94187f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 942ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 94348a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 94487f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 945ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 94648a6092fSMaxime Coquelin 94748a6092fSMaxime Coquelin uart_console_write(port, s, cnt, stm32_console_putchar); 94848a6092fSMaxime Coquelin 94948a6092fSMaxime Coquelin /* Restore interrupt state */ 950ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 95148a6092fSMaxime Coquelin 95248a6092fSMaxime Coquelin if (locked) 95348a6092fSMaxime Coquelin spin_unlock(&port->lock); 95448a6092fSMaxime Coquelin local_irq_restore(flags); 95548a6092fSMaxime Coquelin } 95648a6092fSMaxime Coquelin 95748a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options) 95848a6092fSMaxime Coquelin { 95948a6092fSMaxime Coquelin struct stm32_port *stm32port; 96048a6092fSMaxime Coquelin int baud = 9600; 96148a6092fSMaxime Coquelin int bits = 8; 96248a6092fSMaxime Coquelin int parity = 'n'; 96348a6092fSMaxime Coquelin int flow = 'n'; 96448a6092fSMaxime Coquelin 96548a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 96648a6092fSMaxime Coquelin return -ENODEV; 96748a6092fSMaxime Coquelin 96848a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 96948a6092fSMaxime Coquelin 97048a6092fSMaxime Coquelin /* 97148a6092fSMaxime Coquelin * This driver does not support early console initialization 97248a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 97348a6092fSMaxime Coquelin * this to be called during the uart port registration when the 97448a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 97548a6092fSMaxime Coquelin */ 97648a6092fSMaxime Coquelin if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) 97748a6092fSMaxime Coquelin return -ENXIO; 97848a6092fSMaxime Coquelin 97948a6092fSMaxime Coquelin if (options) 98048a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 98148a6092fSMaxime Coquelin 98248a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 98348a6092fSMaxime Coquelin } 98448a6092fSMaxime Coquelin 98548a6092fSMaxime Coquelin static struct console stm32_console = { 98648a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 98748a6092fSMaxime Coquelin .device = uart_console_device, 98848a6092fSMaxime Coquelin .write = stm32_console_write, 98948a6092fSMaxime Coquelin .setup = stm32_console_setup, 99048a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 99148a6092fSMaxime Coquelin .index = -1, 99248a6092fSMaxime Coquelin .data = &stm32_usart_driver, 99348a6092fSMaxime Coquelin }; 99448a6092fSMaxime Coquelin 99548a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 99648a6092fSMaxime Coquelin 99748a6092fSMaxime Coquelin #else 99848a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 99948a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 100048a6092fSMaxime Coquelin 100148a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 100248a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 100348a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 100448a6092fSMaxime Coquelin .major = 0, 100548a6092fSMaxime Coquelin .minor = 0, 100648a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 100748a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 100848a6092fSMaxime Coquelin }; 100948a6092fSMaxime Coquelin 101048a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 101148a6092fSMaxime Coquelin .probe = stm32_serial_probe, 101248a6092fSMaxime Coquelin .remove = stm32_serial_remove, 101348a6092fSMaxime Coquelin .driver = { 101448a6092fSMaxime Coquelin .name = DRIVER_NAME, 101548a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 101648a6092fSMaxime Coquelin }, 101748a6092fSMaxime Coquelin }; 101848a6092fSMaxime Coquelin 101948a6092fSMaxime Coquelin static int __init usart_init(void) 102048a6092fSMaxime Coquelin { 102148a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 102248a6092fSMaxime Coquelin int ret; 102348a6092fSMaxime Coquelin 102448a6092fSMaxime Coquelin pr_info("%s\n", banner); 102548a6092fSMaxime Coquelin 102648a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 102748a6092fSMaxime Coquelin if (ret) 102848a6092fSMaxime Coquelin return ret; 102948a6092fSMaxime Coquelin 103048a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 103148a6092fSMaxime Coquelin if (ret) 103248a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 103348a6092fSMaxime Coquelin 103448a6092fSMaxime Coquelin return ret; 103548a6092fSMaxime Coquelin } 103648a6092fSMaxime Coquelin 103748a6092fSMaxime Coquelin static void __exit usart_exit(void) 103848a6092fSMaxime Coquelin { 103948a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 104048a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 104148a6092fSMaxime Coquelin } 104248a6092fSMaxime Coquelin 104348a6092fSMaxime Coquelin module_init(usart_init); 104448a6092fSMaxime Coquelin module_exit(usart_exit); 104548a6092fSMaxime Coquelin 104648a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 104748a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 104848a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1049