148a6092fSMaxime Coquelin /* 248a6092fSMaxime Coquelin * Copyright (C) Maxime Coquelin 2015 33e5fcbacSBich HEMON * Copyright (C) STMicroelectronics SA 2017 4ada8618fSAlexandre TORGUE * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com> 5ada8618fSAlexandre TORGUE * Gerald Baeza <gerald.baeza@st.com> 648a6092fSMaxime Coquelin * License terms: GNU General Public License (GPL), version 2 748a6092fSMaxime Coquelin * 848a6092fSMaxime Coquelin * Inspired by st-asc.c from STMicroelectronics (c) 948a6092fSMaxime Coquelin */ 1048a6092fSMaxime Coquelin 116b596a83SMaxime Coquelin #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 1248a6092fSMaxime Coquelin #define SUPPORT_SYSRQ 1348a6092fSMaxime Coquelin #endif 1448a6092fSMaxime Coquelin 1534891872SAlexandre TORGUE #include <linux/clk.h> 1648a6092fSMaxime Coquelin #include <linux/console.h> 1748a6092fSMaxime Coquelin #include <linux/delay.h> 1834891872SAlexandre TORGUE #include <linux/dma-direction.h> 1934891872SAlexandre TORGUE #include <linux/dmaengine.h> 2034891872SAlexandre TORGUE #include <linux/dma-mapping.h> 2134891872SAlexandre TORGUE #include <linux/io.h> 2234891872SAlexandre TORGUE #include <linux/iopoll.h> 2334891872SAlexandre TORGUE #include <linux/irq.h> 2434891872SAlexandre TORGUE #include <linux/module.h> 2548a6092fSMaxime Coquelin #include <linux/of.h> 2648a6092fSMaxime Coquelin #include <linux/of_platform.h> 2734891872SAlexandre TORGUE #include <linux/platform_device.h> 2834891872SAlexandre TORGUE #include <linux/pm_runtime.h> 29270e5a74SFabrice Gasnier #include <linux/pm_wakeirq.h> 3048a6092fSMaxime Coquelin #include <linux/serial_core.h> 3134891872SAlexandre TORGUE #include <linux/serial.h> 3234891872SAlexandre TORGUE #include <linux/spinlock.h> 3334891872SAlexandre TORGUE #include <linux/sysrq.h> 3434891872SAlexandre TORGUE #include <linux/tty_flip.h> 3534891872SAlexandre TORGUE #include <linux/tty.h> 3648a6092fSMaxime Coquelin 37bc5a0b55SAlexandre TORGUE #include "stm32-usart.h" 3848a6092fSMaxime Coquelin 3948a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port); 4034891872SAlexandre TORGUE static void stm32_transmit_chars(struct uart_port *port); 4148a6092fSMaxime Coquelin 4248a6092fSMaxime Coquelin static inline struct stm32_port *to_stm32_port(struct uart_port *port) 4348a6092fSMaxime Coquelin { 4448a6092fSMaxime Coquelin return container_of(port, struct stm32_port, port); 4548a6092fSMaxime Coquelin } 4648a6092fSMaxime Coquelin 4748a6092fSMaxime Coquelin static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits) 4848a6092fSMaxime Coquelin { 4948a6092fSMaxime Coquelin u32 val; 5048a6092fSMaxime Coquelin 5148a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 5248a6092fSMaxime Coquelin val |= bits; 5348a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 5448a6092fSMaxime Coquelin } 5548a6092fSMaxime Coquelin 5648a6092fSMaxime Coquelin static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits) 5748a6092fSMaxime Coquelin { 5848a6092fSMaxime Coquelin u32 val; 5948a6092fSMaxime Coquelin 6048a6092fSMaxime Coquelin val = readl_relaxed(port->membase + reg); 6148a6092fSMaxime Coquelin val &= ~bits; 6248a6092fSMaxime Coquelin writel_relaxed(val, port->membase + reg); 6348a6092fSMaxime Coquelin } 6448a6092fSMaxime Coquelin 65b97055bcSBaoyou Xie static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res, 6634891872SAlexandre TORGUE bool threaded) 6734891872SAlexandre TORGUE { 6834891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 6934891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 7034891872SAlexandre TORGUE enum dma_status status; 7134891872SAlexandre TORGUE struct dma_tx_state state; 7234891872SAlexandre TORGUE 7334891872SAlexandre TORGUE *sr = readl_relaxed(port->membase + ofs->isr); 7434891872SAlexandre TORGUE 7534891872SAlexandre TORGUE if (threaded && stm32_port->rx_ch) { 7634891872SAlexandre TORGUE status = dmaengine_tx_status(stm32_port->rx_ch, 7734891872SAlexandre TORGUE stm32_port->rx_ch->cookie, 7834891872SAlexandre TORGUE &state); 7934891872SAlexandre TORGUE if ((status == DMA_IN_PROGRESS) && 8034891872SAlexandre TORGUE (*last_res != state.residue)) 8134891872SAlexandre TORGUE return 1; 8234891872SAlexandre TORGUE else 8334891872SAlexandre TORGUE return 0; 8434891872SAlexandre TORGUE } else if (*sr & USART_SR_RXNE) { 8534891872SAlexandre TORGUE return 1; 8634891872SAlexandre TORGUE } 8734891872SAlexandre TORGUE return 0; 8834891872SAlexandre TORGUE } 8934891872SAlexandre TORGUE 90b97055bcSBaoyou Xie static unsigned long 91b97055bcSBaoyou Xie stm32_get_char(struct uart_port *port, u32 *sr, int *last_res) 9234891872SAlexandre TORGUE { 9334891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 9434891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 9534891872SAlexandre TORGUE unsigned long c; 9634891872SAlexandre TORGUE 9734891872SAlexandre TORGUE if (stm32_port->rx_ch) { 9834891872SAlexandre TORGUE c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; 9934891872SAlexandre TORGUE if ((*last_res) == 0) 10034891872SAlexandre TORGUE *last_res = RX_BUF_L; 10134891872SAlexandre TORGUE return c; 10234891872SAlexandre TORGUE } else { 10334891872SAlexandre TORGUE return readl_relaxed(port->membase + ofs->rdr); 10434891872SAlexandre TORGUE } 10534891872SAlexandre TORGUE } 10634891872SAlexandre TORGUE 10734891872SAlexandre TORGUE static void stm32_receive_chars(struct uart_port *port, bool threaded) 10848a6092fSMaxime Coquelin { 10948a6092fSMaxime Coquelin struct tty_port *tport = &port->state->port; 110ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 111ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 11248a6092fSMaxime Coquelin unsigned long c; 11348a6092fSMaxime Coquelin u32 sr; 11448a6092fSMaxime Coquelin char flag; 11548a6092fSMaxime Coquelin 116*29d60981SAndy Shevchenko if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) 11748a6092fSMaxime Coquelin pm_wakeup_event(tport->tty->dev, 0); 11848a6092fSMaxime Coquelin 119e5707915SGerald Baeza while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) { 12048a6092fSMaxime Coquelin sr |= USART_SR_DUMMY_RX; 121e5707915SGerald Baeza c = stm32_get_char(port, &sr, &stm32_port->last_res); 12248a6092fSMaxime Coquelin flag = TTY_NORMAL; 12348a6092fSMaxime Coquelin port->icount.rx++; 12448a6092fSMaxime Coquelin 12548a6092fSMaxime Coquelin if (sr & USART_SR_ERR_MASK) { 12648a6092fSMaxime Coquelin if (sr & USART_SR_LBD) { 12748a6092fSMaxime Coquelin port->icount.brk++; 12848a6092fSMaxime Coquelin if (uart_handle_break(port)) 12948a6092fSMaxime Coquelin continue; 13048a6092fSMaxime Coquelin } else if (sr & USART_SR_ORE) { 131ada8618fSAlexandre TORGUE if (ofs->icr != UNDEF_REG) 132ada8618fSAlexandre TORGUE writel_relaxed(USART_ICR_ORECF, 133ada8618fSAlexandre TORGUE port->membase + 134ada8618fSAlexandre TORGUE ofs->icr); 13548a6092fSMaxime Coquelin port->icount.overrun++; 13648a6092fSMaxime Coquelin } else if (sr & USART_SR_PE) { 13748a6092fSMaxime Coquelin port->icount.parity++; 13848a6092fSMaxime Coquelin } else if (sr & USART_SR_FE) { 13948a6092fSMaxime Coquelin port->icount.frame++; 14048a6092fSMaxime Coquelin } 14148a6092fSMaxime Coquelin 14248a6092fSMaxime Coquelin sr &= port->read_status_mask; 14348a6092fSMaxime Coquelin 14448a6092fSMaxime Coquelin if (sr & USART_SR_LBD) 14548a6092fSMaxime Coquelin flag = TTY_BREAK; 14648a6092fSMaxime Coquelin else if (sr & USART_SR_PE) 14748a6092fSMaxime Coquelin flag = TTY_PARITY; 14848a6092fSMaxime Coquelin else if (sr & USART_SR_FE) 14948a6092fSMaxime Coquelin flag = TTY_FRAME; 15048a6092fSMaxime Coquelin } 15148a6092fSMaxime Coquelin 15248a6092fSMaxime Coquelin if (uart_handle_sysrq_char(port, c)) 15348a6092fSMaxime Coquelin continue; 15448a6092fSMaxime Coquelin uart_insert_char(port, sr, USART_SR_ORE, c, flag); 15548a6092fSMaxime Coquelin } 15648a6092fSMaxime Coquelin 15748a6092fSMaxime Coquelin spin_unlock(&port->lock); 15848a6092fSMaxime Coquelin tty_flip_buffer_push(tport); 15948a6092fSMaxime Coquelin spin_lock(&port->lock); 16048a6092fSMaxime Coquelin } 16148a6092fSMaxime Coquelin 16234891872SAlexandre TORGUE static void stm32_tx_dma_complete(void *arg) 16334891872SAlexandre TORGUE { 16434891872SAlexandre TORGUE struct uart_port *port = arg; 16534891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 16634891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 16734891872SAlexandre TORGUE unsigned int isr; 16834891872SAlexandre TORGUE int ret; 16934891872SAlexandre TORGUE 17034891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 17134891872SAlexandre TORGUE isr, 17234891872SAlexandre TORGUE (isr & USART_SR_TC), 17334891872SAlexandre TORGUE 10, 100000); 17434891872SAlexandre TORGUE 17534891872SAlexandre TORGUE if (ret) 17634891872SAlexandre TORGUE dev_err(port->dev, "terminal count not set\n"); 17734891872SAlexandre TORGUE 17834891872SAlexandre TORGUE if (ofs->icr == UNDEF_REG) 17934891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 18034891872SAlexandre TORGUE else 18134891872SAlexandre TORGUE stm32_set_bits(port, ofs->icr, USART_CR_TC); 18234891872SAlexandre TORGUE 18334891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 18434891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 18534891872SAlexandre TORGUE 18634891872SAlexandre TORGUE /* Let's see if we have pending data to send */ 18734891872SAlexandre TORGUE stm32_transmit_chars(port); 18834891872SAlexandre TORGUE } 18934891872SAlexandre TORGUE 19034891872SAlexandre TORGUE static void stm32_transmit_chars_pio(struct uart_port *port) 19134891872SAlexandre TORGUE { 19234891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 19334891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 19434891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 19534891872SAlexandre TORGUE unsigned int isr; 19634891872SAlexandre TORGUE int ret; 19734891872SAlexandre TORGUE 19834891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) { 19934891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 20034891872SAlexandre TORGUE stm32_port->tx_dma_busy = false; 20134891872SAlexandre TORGUE } 20234891872SAlexandre TORGUE 20334891872SAlexandre TORGUE ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, 20434891872SAlexandre TORGUE isr, 20534891872SAlexandre TORGUE (isr & USART_SR_TXE), 206a61d9e6eSGerald Baeza 10, 100000); 20734891872SAlexandre TORGUE 20834891872SAlexandre TORGUE if (ret) 20934891872SAlexandre TORGUE dev_err(port->dev, "tx empty not set\n"); 21034891872SAlexandre TORGUE 21134891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE); 21234891872SAlexandre TORGUE 21334891872SAlexandre TORGUE writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); 21434891872SAlexandre TORGUE xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 21534891872SAlexandre TORGUE port->icount.tx++; 21634891872SAlexandre TORGUE } 21734891872SAlexandre TORGUE 21834891872SAlexandre TORGUE static void stm32_transmit_chars_dma(struct uart_port *port) 21934891872SAlexandre TORGUE { 22034891872SAlexandre TORGUE struct stm32_port *stm32port = to_stm32_port(port); 22134891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 22234891872SAlexandre TORGUE struct circ_buf *xmit = &port->state->xmit; 22334891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 22434891872SAlexandre TORGUE dma_cookie_t cookie; 22534891872SAlexandre TORGUE unsigned int count, i; 22634891872SAlexandre TORGUE 22734891872SAlexandre TORGUE if (stm32port->tx_dma_busy) 22834891872SAlexandre TORGUE return; 22934891872SAlexandre TORGUE 23034891872SAlexandre TORGUE stm32port->tx_dma_busy = true; 23134891872SAlexandre TORGUE 23234891872SAlexandre TORGUE count = uart_circ_chars_pending(xmit); 23334891872SAlexandre TORGUE 23434891872SAlexandre TORGUE if (count > TX_BUF_L) 23534891872SAlexandre TORGUE count = TX_BUF_L; 23634891872SAlexandre TORGUE 23734891872SAlexandre TORGUE if (xmit->tail < xmit->head) { 23834891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); 23934891872SAlexandre TORGUE } else { 24034891872SAlexandre TORGUE size_t one = UART_XMIT_SIZE - xmit->tail; 24134891872SAlexandre TORGUE size_t two; 24234891872SAlexandre TORGUE 24334891872SAlexandre TORGUE if (one > count) 24434891872SAlexandre TORGUE one = count; 24534891872SAlexandre TORGUE two = count - one; 24634891872SAlexandre TORGUE 24734891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); 24834891872SAlexandre TORGUE if (two) 24934891872SAlexandre TORGUE memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); 25034891872SAlexandre TORGUE } 25134891872SAlexandre TORGUE 25234891872SAlexandre TORGUE desc = dmaengine_prep_slave_single(stm32port->tx_ch, 25334891872SAlexandre TORGUE stm32port->tx_dma_buf, 25434891872SAlexandre TORGUE count, 25534891872SAlexandre TORGUE DMA_MEM_TO_DEV, 25634891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 25734891872SAlexandre TORGUE 25834891872SAlexandre TORGUE if (!desc) { 25934891872SAlexandre TORGUE for (i = count; i > 0; i--) 26034891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 26134891872SAlexandre TORGUE return; 26234891872SAlexandre TORGUE } 26334891872SAlexandre TORGUE 26434891872SAlexandre TORGUE desc->callback = stm32_tx_dma_complete; 26534891872SAlexandre TORGUE desc->callback_param = port; 26634891872SAlexandre TORGUE 26734891872SAlexandre TORGUE /* Push current DMA TX transaction in the pending queue */ 26834891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 26934891872SAlexandre TORGUE 27034891872SAlexandre TORGUE /* Issue pending DMA TX requests */ 27134891872SAlexandre TORGUE dma_async_issue_pending(stm32port->tx_ch); 27234891872SAlexandre TORGUE 27334891872SAlexandre TORGUE stm32_clr_bits(port, ofs->isr, USART_SR_TC); 27434891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 27534891872SAlexandre TORGUE 27634891872SAlexandre TORGUE xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); 27734891872SAlexandre TORGUE port->icount.tx += count; 27834891872SAlexandre TORGUE } 27934891872SAlexandre TORGUE 28048a6092fSMaxime Coquelin static void stm32_transmit_chars(struct uart_port *port) 28148a6092fSMaxime Coquelin { 282ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 283ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 28448a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 28548a6092fSMaxime Coquelin 28648a6092fSMaxime Coquelin if (port->x_char) { 28734891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 28834891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 289ada8618fSAlexandre TORGUE writel_relaxed(port->x_char, port->membase + ofs->tdr); 29048a6092fSMaxime Coquelin port->x_char = 0; 29148a6092fSMaxime Coquelin port->icount.tx++; 29234891872SAlexandre TORGUE if (stm32_port->tx_dma_busy) 29334891872SAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); 29448a6092fSMaxime Coquelin return; 29548a6092fSMaxime Coquelin } 29648a6092fSMaxime Coquelin 29748a6092fSMaxime Coquelin if (uart_tx_stopped(port)) { 29848a6092fSMaxime Coquelin stm32_stop_tx(port); 29948a6092fSMaxime Coquelin return; 30048a6092fSMaxime Coquelin } 30148a6092fSMaxime Coquelin 30248a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) { 30348a6092fSMaxime Coquelin stm32_stop_tx(port); 30448a6092fSMaxime Coquelin return; 30548a6092fSMaxime Coquelin } 30648a6092fSMaxime Coquelin 30734891872SAlexandre TORGUE if (stm32_port->tx_ch) 30834891872SAlexandre TORGUE stm32_transmit_chars_dma(port); 30934891872SAlexandre TORGUE else 31034891872SAlexandre TORGUE stm32_transmit_chars_pio(port); 31148a6092fSMaxime Coquelin 31248a6092fSMaxime Coquelin if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 31348a6092fSMaxime Coquelin uart_write_wakeup(port); 31448a6092fSMaxime Coquelin 31548a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 31648a6092fSMaxime Coquelin stm32_stop_tx(port); 31748a6092fSMaxime Coquelin } 31848a6092fSMaxime Coquelin 31948a6092fSMaxime Coquelin static irqreturn_t stm32_interrupt(int irq, void *ptr) 32048a6092fSMaxime Coquelin { 32148a6092fSMaxime Coquelin struct uart_port *port = ptr; 322ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 323ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 32448a6092fSMaxime Coquelin u32 sr; 32548a6092fSMaxime Coquelin 32601d32d71SAlexandre TORGUE spin_lock(&port->lock); 32701d32d71SAlexandre TORGUE 328ada8618fSAlexandre TORGUE sr = readl_relaxed(port->membase + ofs->isr); 32948a6092fSMaxime Coquelin 330270e5a74SFabrice Gasnier if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG)) 331270e5a74SFabrice Gasnier writel_relaxed(USART_ICR_WUCF, 332270e5a74SFabrice Gasnier port->membase + ofs->icr); 333270e5a74SFabrice Gasnier 33434891872SAlexandre TORGUE if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) 33534891872SAlexandre TORGUE stm32_receive_chars(port, false); 33648a6092fSMaxime Coquelin 33734891872SAlexandre TORGUE if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) 33848a6092fSMaxime Coquelin stm32_transmit_chars(port); 33948a6092fSMaxime Coquelin 34001d32d71SAlexandre TORGUE spin_unlock(&port->lock); 34101d32d71SAlexandre TORGUE 34234891872SAlexandre TORGUE if (stm32_port->rx_ch) 34334891872SAlexandre TORGUE return IRQ_WAKE_THREAD; 34434891872SAlexandre TORGUE else 34534891872SAlexandre TORGUE return IRQ_HANDLED; 34634891872SAlexandre TORGUE } 34734891872SAlexandre TORGUE 34834891872SAlexandre TORGUE static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr) 34934891872SAlexandre TORGUE { 35034891872SAlexandre TORGUE struct uart_port *port = ptr; 35134891872SAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 35234891872SAlexandre TORGUE 35334891872SAlexandre TORGUE spin_lock(&port->lock); 35434891872SAlexandre TORGUE 35534891872SAlexandre TORGUE if (stm32_port->rx_ch) 35634891872SAlexandre TORGUE stm32_receive_chars(port, true); 35734891872SAlexandre TORGUE 35848a6092fSMaxime Coquelin spin_unlock(&port->lock); 35948a6092fSMaxime Coquelin 36048a6092fSMaxime Coquelin return IRQ_HANDLED; 36148a6092fSMaxime Coquelin } 36248a6092fSMaxime Coquelin 36348a6092fSMaxime Coquelin static unsigned int stm32_tx_empty(struct uart_port *port) 36448a6092fSMaxime Coquelin { 365ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 366ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 367ada8618fSAlexandre TORGUE 368ada8618fSAlexandre TORGUE return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; 36948a6092fSMaxime Coquelin } 37048a6092fSMaxime Coquelin 37148a6092fSMaxime Coquelin static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) 37248a6092fSMaxime Coquelin { 373ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 374ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 375ada8618fSAlexandre TORGUE 37648a6092fSMaxime Coquelin if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) 377ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); 37848a6092fSMaxime Coquelin else 379ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); 38048a6092fSMaxime Coquelin } 38148a6092fSMaxime Coquelin 38248a6092fSMaxime Coquelin static unsigned int stm32_get_mctrl(struct uart_port *port) 38348a6092fSMaxime Coquelin { 38448a6092fSMaxime Coquelin /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ 38548a6092fSMaxime Coquelin return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; 38648a6092fSMaxime Coquelin } 38748a6092fSMaxime Coquelin 38848a6092fSMaxime Coquelin /* Transmit stop */ 38948a6092fSMaxime Coquelin static void stm32_stop_tx(struct uart_port *port) 39048a6092fSMaxime Coquelin { 391ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 392ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 393ada8618fSAlexandre TORGUE 394ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); 39548a6092fSMaxime Coquelin } 39648a6092fSMaxime Coquelin 39748a6092fSMaxime Coquelin /* There are probably characters waiting to be transmitted. */ 39848a6092fSMaxime Coquelin static void stm32_start_tx(struct uart_port *port) 39948a6092fSMaxime Coquelin { 40048a6092fSMaxime Coquelin struct circ_buf *xmit = &port->state->xmit; 40148a6092fSMaxime Coquelin 40248a6092fSMaxime Coquelin if (uart_circ_empty(xmit)) 40348a6092fSMaxime Coquelin return; 40448a6092fSMaxime Coquelin 40534891872SAlexandre TORGUE stm32_transmit_chars(port); 40648a6092fSMaxime Coquelin } 40748a6092fSMaxime Coquelin 40848a6092fSMaxime Coquelin /* Throttle the remote when input buffer is about to overflow. */ 40948a6092fSMaxime Coquelin static void stm32_throttle(struct uart_port *port) 41048a6092fSMaxime Coquelin { 411ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 412ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 41348a6092fSMaxime Coquelin unsigned long flags; 41448a6092fSMaxime Coquelin 41548a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 416ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 41748a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 41848a6092fSMaxime Coquelin } 41948a6092fSMaxime Coquelin 42048a6092fSMaxime Coquelin /* Unthrottle the remote, the input buffer can now accept data. */ 42148a6092fSMaxime Coquelin static void stm32_unthrottle(struct uart_port *port) 42248a6092fSMaxime Coquelin { 423ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 424ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 42548a6092fSMaxime Coquelin unsigned long flags; 42648a6092fSMaxime Coquelin 42748a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 428ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE); 42948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 43048a6092fSMaxime Coquelin } 43148a6092fSMaxime Coquelin 43248a6092fSMaxime Coquelin /* Receive stop */ 43348a6092fSMaxime Coquelin static void stm32_stop_rx(struct uart_port *port) 43448a6092fSMaxime Coquelin { 435ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 436ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 437ada8618fSAlexandre TORGUE 438ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); 43948a6092fSMaxime Coquelin } 44048a6092fSMaxime Coquelin 44148a6092fSMaxime Coquelin /* Handle breaks - ignored by us */ 44248a6092fSMaxime Coquelin static void stm32_break_ctl(struct uart_port *port, int break_state) 44348a6092fSMaxime Coquelin { 44448a6092fSMaxime Coquelin } 44548a6092fSMaxime Coquelin 44648a6092fSMaxime Coquelin static int stm32_startup(struct uart_port *port) 44748a6092fSMaxime Coquelin { 448ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 449ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 450270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 45148a6092fSMaxime Coquelin const char *name = to_platform_device(port->dev)->name; 45248a6092fSMaxime Coquelin u32 val; 45348a6092fSMaxime Coquelin int ret; 45448a6092fSMaxime Coquelin 45534891872SAlexandre TORGUE ret = request_threaded_irq(port->irq, stm32_interrupt, 45634891872SAlexandre TORGUE stm32_threaded_interrupt, 45734891872SAlexandre TORGUE IRQF_NO_SUSPEND, name, port); 45848a6092fSMaxime Coquelin if (ret) 45948a6092fSMaxime Coquelin return ret; 46048a6092fSMaxime Coquelin 461270e5a74SFabrice Gasnier if (cfg->has_wakeup && stm32_port->wakeirq >= 0) { 462270e5a74SFabrice Gasnier ret = dev_pm_set_dedicated_wake_irq(port->dev, 463270e5a74SFabrice Gasnier stm32_port->wakeirq); 464270e5a74SFabrice Gasnier if (ret) { 465270e5a74SFabrice Gasnier free_irq(port->irq, port); 466270e5a74SFabrice Gasnier return ret; 467270e5a74SFabrice Gasnier } 468270e5a74SFabrice Gasnier } 469270e5a74SFabrice Gasnier 47048a6092fSMaxime Coquelin val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 471351a762aSGerald Baeza if (stm32_port->fifoen) 472351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 473ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, val); 47448a6092fSMaxime Coquelin 47548a6092fSMaxime Coquelin return 0; 47648a6092fSMaxime Coquelin } 47748a6092fSMaxime Coquelin 47848a6092fSMaxime Coquelin static void stm32_shutdown(struct uart_port *port) 47948a6092fSMaxime Coquelin { 480ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 481ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 48287f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 48348a6092fSMaxime Coquelin u32 val; 48448a6092fSMaxime Coquelin 48548a6092fSMaxime Coquelin val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; 48687f1f809SAlexandre TORGUE val |= BIT(cfg->uart_enable_bit); 487351a762aSGerald Baeza if (stm32_port->fifoen) 488351a762aSGerald Baeza val |= USART_CR1_FIFOEN; 489a14f66a4SAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, val); 49048a6092fSMaxime Coquelin 491270e5a74SFabrice Gasnier dev_pm_clear_wake_irq(port->dev); 49248a6092fSMaxime Coquelin free_irq(port->irq, port); 49348a6092fSMaxime Coquelin } 49448a6092fSMaxime Coquelin 49548a6092fSMaxime Coquelin static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, 49648a6092fSMaxime Coquelin struct ktermios *old) 49748a6092fSMaxime Coquelin { 49848a6092fSMaxime Coquelin struct stm32_port *stm32_port = to_stm32_port(port); 499ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 500ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 50148a6092fSMaxime Coquelin unsigned int baud; 50248a6092fSMaxime Coquelin u32 usartdiv, mantissa, fraction, oversampling; 50348a6092fSMaxime Coquelin tcflag_t cflag = termios->c_cflag; 50448a6092fSMaxime Coquelin u32 cr1, cr2, cr3; 50548a6092fSMaxime Coquelin unsigned long flags; 50648a6092fSMaxime Coquelin 50748a6092fSMaxime Coquelin if (!stm32_port->hw_flow_control) 50848a6092fSMaxime Coquelin cflag &= ~CRTSCTS; 50948a6092fSMaxime Coquelin 51048a6092fSMaxime Coquelin baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); 51148a6092fSMaxime Coquelin 51248a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 51348a6092fSMaxime Coquelin 51448a6092fSMaxime Coquelin /* Stop serial port and reset value */ 515ada8618fSAlexandre TORGUE writel_relaxed(0, port->membase + ofs->cr1); 51648a6092fSMaxime Coquelin 517ada8618fSAlexandre TORGUE cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; 518ada8618fSAlexandre TORGUE cr1 |= BIT(cfg->uart_enable_bit); 519351a762aSGerald Baeza if (stm32_port->fifoen) 520351a762aSGerald Baeza cr1 |= USART_CR1_FIFOEN; 52148a6092fSMaxime Coquelin cr2 = 0; 52248a6092fSMaxime Coquelin cr3 = 0; 52348a6092fSMaxime Coquelin 52448a6092fSMaxime Coquelin if (cflag & CSTOPB) 52548a6092fSMaxime Coquelin cr2 |= USART_CR2_STOP_2B; 52648a6092fSMaxime Coquelin 52748a6092fSMaxime Coquelin if (cflag & PARENB) { 52848a6092fSMaxime Coquelin cr1 |= USART_CR1_PCE; 529ada8618fSAlexandre TORGUE if ((cflag & CSIZE) == CS8) { 530ada8618fSAlexandre TORGUE if (cfg->has_7bits_data) 531ada8618fSAlexandre TORGUE cr1 |= USART_CR1_M0; 532ada8618fSAlexandre TORGUE else 53348a6092fSMaxime Coquelin cr1 |= USART_CR1_M; 53448a6092fSMaxime Coquelin } 535ada8618fSAlexandre TORGUE } 53648a6092fSMaxime Coquelin 53748a6092fSMaxime Coquelin if (cflag & PARODD) 53848a6092fSMaxime Coquelin cr1 |= USART_CR1_PS; 53948a6092fSMaxime Coquelin 54048a6092fSMaxime Coquelin port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); 54148a6092fSMaxime Coquelin if (cflag & CRTSCTS) { 54248a6092fSMaxime Coquelin port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; 54335abe98fSBich HEMON cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; 54448a6092fSMaxime Coquelin } 54548a6092fSMaxime Coquelin 54648a6092fSMaxime Coquelin usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); 54748a6092fSMaxime Coquelin 54848a6092fSMaxime Coquelin /* 54948a6092fSMaxime Coquelin * The USART supports 16 or 8 times oversampling. 55048a6092fSMaxime Coquelin * By default we prefer 16 times oversampling, so that the receiver 55148a6092fSMaxime Coquelin * has a better tolerance to clock deviations. 55248a6092fSMaxime Coquelin * 8 times oversampling is only used to achieve higher speeds. 55348a6092fSMaxime Coquelin */ 55448a6092fSMaxime Coquelin if (usartdiv < 16) { 55548a6092fSMaxime Coquelin oversampling = 8; 556ada8618fSAlexandre TORGUE stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); 55748a6092fSMaxime Coquelin } else { 55848a6092fSMaxime Coquelin oversampling = 16; 559ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); 56048a6092fSMaxime Coquelin } 56148a6092fSMaxime Coquelin 56248a6092fSMaxime Coquelin mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; 56348a6092fSMaxime Coquelin fraction = usartdiv % oversampling; 564ada8618fSAlexandre TORGUE writel_relaxed(mantissa | fraction, port->membase + ofs->brr); 56548a6092fSMaxime Coquelin 56648a6092fSMaxime Coquelin uart_update_timeout(port, cflag, baud); 56748a6092fSMaxime Coquelin 56848a6092fSMaxime Coquelin port->read_status_mask = USART_SR_ORE; 56948a6092fSMaxime Coquelin if (termios->c_iflag & INPCK) 57048a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_PE | USART_SR_FE; 57148a6092fSMaxime Coquelin if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 57248a6092fSMaxime Coquelin port->read_status_mask |= USART_SR_LBD; 57348a6092fSMaxime Coquelin 57448a6092fSMaxime Coquelin /* Characters to ignore */ 57548a6092fSMaxime Coquelin port->ignore_status_mask = 0; 57648a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 57748a6092fSMaxime Coquelin port->ignore_status_mask = USART_SR_PE | USART_SR_FE; 57848a6092fSMaxime Coquelin if (termios->c_iflag & IGNBRK) { 57948a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_LBD; 58048a6092fSMaxime Coquelin /* 58148a6092fSMaxime Coquelin * If we're ignoring parity and break indicators, 58248a6092fSMaxime Coquelin * ignore overruns too (for real raw support). 58348a6092fSMaxime Coquelin */ 58448a6092fSMaxime Coquelin if (termios->c_iflag & IGNPAR) 58548a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_ORE; 58648a6092fSMaxime Coquelin } 58748a6092fSMaxime Coquelin 58848a6092fSMaxime Coquelin /* Ignore all characters if CREAD is not set */ 58948a6092fSMaxime Coquelin if ((termios->c_cflag & CREAD) == 0) 59048a6092fSMaxime Coquelin port->ignore_status_mask |= USART_SR_DUMMY_RX; 59148a6092fSMaxime Coquelin 59234891872SAlexandre TORGUE if (stm32_port->rx_ch) 59334891872SAlexandre TORGUE cr3 |= USART_CR3_DMAR; 59434891872SAlexandre TORGUE 595ada8618fSAlexandre TORGUE writel_relaxed(cr3, port->membase + ofs->cr3); 596ada8618fSAlexandre TORGUE writel_relaxed(cr2, port->membase + ofs->cr2); 597ada8618fSAlexandre TORGUE writel_relaxed(cr1, port->membase + ofs->cr1); 59848a6092fSMaxime Coquelin 59948a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 60048a6092fSMaxime Coquelin } 60148a6092fSMaxime Coquelin 60248a6092fSMaxime Coquelin static const char *stm32_type(struct uart_port *port) 60348a6092fSMaxime Coquelin { 60448a6092fSMaxime Coquelin return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; 60548a6092fSMaxime Coquelin } 60648a6092fSMaxime Coquelin 60748a6092fSMaxime Coquelin static void stm32_release_port(struct uart_port *port) 60848a6092fSMaxime Coquelin { 60948a6092fSMaxime Coquelin } 61048a6092fSMaxime Coquelin 61148a6092fSMaxime Coquelin static int stm32_request_port(struct uart_port *port) 61248a6092fSMaxime Coquelin { 61348a6092fSMaxime Coquelin return 0; 61448a6092fSMaxime Coquelin } 61548a6092fSMaxime Coquelin 61648a6092fSMaxime Coquelin static void stm32_config_port(struct uart_port *port, int flags) 61748a6092fSMaxime Coquelin { 61848a6092fSMaxime Coquelin if (flags & UART_CONFIG_TYPE) 61948a6092fSMaxime Coquelin port->type = PORT_STM32; 62048a6092fSMaxime Coquelin } 62148a6092fSMaxime Coquelin 62248a6092fSMaxime Coquelin static int 62348a6092fSMaxime Coquelin stm32_verify_port(struct uart_port *port, struct serial_struct *ser) 62448a6092fSMaxime Coquelin { 62548a6092fSMaxime Coquelin /* No user changeable parameters */ 62648a6092fSMaxime Coquelin return -EINVAL; 62748a6092fSMaxime Coquelin } 62848a6092fSMaxime Coquelin 62948a6092fSMaxime Coquelin static void stm32_pm(struct uart_port *port, unsigned int state, 63048a6092fSMaxime Coquelin unsigned int oldstate) 63148a6092fSMaxime Coquelin { 63248a6092fSMaxime Coquelin struct stm32_port *stm32port = container_of(port, 63348a6092fSMaxime Coquelin struct stm32_port, port); 634ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 635ada8618fSAlexandre TORGUE struct stm32_usart_config *cfg = &stm32port->info->cfg; 63648a6092fSMaxime Coquelin unsigned long flags = 0; 63748a6092fSMaxime Coquelin 63848a6092fSMaxime Coquelin switch (state) { 63948a6092fSMaxime Coquelin case UART_PM_STATE_ON: 64048a6092fSMaxime Coquelin clk_prepare_enable(stm32port->clk); 64148a6092fSMaxime Coquelin break; 64248a6092fSMaxime Coquelin case UART_PM_STATE_OFF: 64348a6092fSMaxime Coquelin spin_lock_irqsave(&port->lock, flags); 644ada8618fSAlexandre TORGUE stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 64548a6092fSMaxime Coquelin spin_unlock_irqrestore(&port->lock, flags); 64648a6092fSMaxime Coquelin clk_disable_unprepare(stm32port->clk); 64748a6092fSMaxime Coquelin break; 64848a6092fSMaxime Coquelin } 64948a6092fSMaxime Coquelin } 65048a6092fSMaxime Coquelin 65148a6092fSMaxime Coquelin static const struct uart_ops stm32_uart_ops = { 65248a6092fSMaxime Coquelin .tx_empty = stm32_tx_empty, 65348a6092fSMaxime Coquelin .set_mctrl = stm32_set_mctrl, 65448a6092fSMaxime Coquelin .get_mctrl = stm32_get_mctrl, 65548a6092fSMaxime Coquelin .stop_tx = stm32_stop_tx, 65648a6092fSMaxime Coquelin .start_tx = stm32_start_tx, 65748a6092fSMaxime Coquelin .throttle = stm32_throttle, 65848a6092fSMaxime Coquelin .unthrottle = stm32_unthrottle, 65948a6092fSMaxime Coquelin .stop_rx = stm32_stop_rx, 66048a6092fSMaxime Coquelin .break_ctl = stm32_break_ctl, 66148a6092fSMaxime Coquelin .startup = stm32_startup, 66248a6092fSMaxime Coquelin .shutdown = stm32_shutdown, 66348a6092fSMaxime Coquelin .set_termios = stm32_set_termios, 66448a6092fSMaxime Coquelin .pm = stm32_pm, 66548a6092fSMaxime Coquelin .type = stm32_type, 66648a6092fSMaxime Coquelin .release_port = stm32_release_port, 66748a6092fSMaxime Coquelin .request_port = stm32_request_port, 66848a6092fSMaxime Coquelin .config_port = stm32_config_port, 66948a6092fSMaxime Coquelin .verify_port = stm32_verify_port, 67048a6092fSMaxime Coquelin }; 67148a6092fSMaxime Coquelin 67248a6092fSMaxime Coquelin static int stm32_init_port(struct stm32_port *stm32port, 67348a6092fSMaxime Coquelin struct platform_device *pdev) 67448a6092fSMaxime Coquelin { 67548a6092fSMaxime Coquelin struct uart_port *port = &stm32port->port; 67648a6092fSMaxime Coquelin struct resource *res; 67748a6092fSMaxime Coquelin int ret; 67848a6092fSMaxime Coquelin 67948a6092fSMaxime Coquelin port->iotype = UPIO_MEM; 68048a6092fSMaxime Coquelin port->flags = UPF_BOOT_AUTOCONF; 68148a6092fSMaxime Coquelin port->ops = &stm32_uart_ops; 68248a6092fSMaxime Coquelin port->dev = &pdev->dev; 68348a6092fSMaxime Coquelin port->irq = platform_get_irq(pdev, 0); 684270e5a74SFabrice Gasnier stm32port->wakeirq = platform_get_irq(pdev, 1); 685351a762aSGerald Baeza stm32port->fifoen = stm32port->info->cfg.has_fifo; 68648a6092fSMaxime Coquelin 68748a6092fSMaxime Coquelin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 68848a6092fSMaxime Coquelin port->membase = devm_ioremap_resource(&pdev->dev, res); 68948a6092fSMaxime Coquelin if (IS_ERR(port->membase)) 69048a6092fSMaxime Coquelin return PTR_ERR(port->membase); 69148a6092fSMaxime Coquelin port->mapbase = res->start; 69248a6092fSMaxime Coquelin 69348a6092fSMaxime Coquelin spin_lock_init(&port->lock); 69448a6092fSMaxime Coquelin 69548a6092fSMaxime Coquelin stm32port->clk = devm_clk_get(&pdev->dev, NULL); 69648a6092fSMaxime Coquelin if (IS_ERR(stm32port->clk)) 69748a6092fSMaxime Coquelin return PTR_ERR(stm32port->clk); 69848a6092fSMaxime Coquelin 69948a6092fSMaxime Coquelin /* Ensure that clk rate is correct by enabling the clk */ 70048a6092fSMaxime Coquelin ret = clk_prepare_enable(stm32port->clk); 70148a6092fSMaxime Coquelin if (ret) 70248a6092fSMaxime Coquelin return ret; 70348a6092fSMaxime Coquelin 70448a6092fSMaxime Coquelin stm32port->port.uartclk = clk_get_rate(stm32port->clk); 705ada80043SFabrice Gasnier if (!stm32port->port.uartclk) { 706ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 70748a6092fSMaxime Coquelin ret = -EINVAL; 708ada80043SFabrice Gasnier } 70948a6092fSMaxime Coquelin 71048a6092fSMaxime Coquelin return ret; 71148a6092fSMaxime Coquelin } 71248a6092fSMaxime Coquelin 71348a6092fSMaxime Coquelin static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) 71448a6092fSMaxime Coquelin { 71548a6092fSMaxime Coquelin struct device_node *np = pdev->dev.of_node; 71648a6092fSMaxime Coquelin int id; 71748a6092fSMaxime Coquelin 71848a6092fSMaxime Coquelin if (!np) 71948a6092fSMaxime Coquelin return NULL; 72048a6092fSMaxime Coquelin 72148a6092fSMaxime Coquelin id = of_alias_get_id(np, "serial"); 722e5707915SGerald Baeza if (id < 0) { 723e5707915SGerald Baeza dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); 724e5707915SGerald Baeza return NULL; 725e5707915SGerald Baeza } 72648a6092fSMaxime Coquelin 72748a6092fSMaxime Coquelin if (WARN_ON(id >= STM32_MAX_PORTS)) 72848a6092fSMaxime Coquelin return NULL; 72948a6092fSMaxime Coquelin 73048a6092fSMaxime Coquelin stm32_ports[id].hw_flow_control = of_property_read_bool(np, 73159bed2dfSAlexandre TORGUE "st,hw-flow-ctrl"); 73248a6092fSMaxime Coquelin stm32_ports[id].port.line = id; 733e5707915SGerald Baeza stm32_ports[id].last_res = RX_BUF_L; 73448a6092fSMaxime Coquelin return &stm32_ports[id]; 73548a6092fSMaxime Coquelin } 73648a6092fSMaxime Coquelin 73748a6092fSMaxime Coquelin #ifdef CONFIG_OF 73848a6092fSMaxime Coquelin static const struct of_device_id stm32_match[] = { 739ada8618fSAlexandre TORGUE { .compatible = "st,stm32-usart", .data = &stm32f4_info}, 740ada8618fSAlexandre TORGUE { .compatible = "st,stm32-uart", .data = &stm32f4_info}, 741ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-usart", .data = &stm32f7_info}, 742ada8618fSAlexandre TORGUE { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, 743270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-usart", .data = &stm32h7_info}, 744270e5a74SFabrice Gasnier { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, 74548a6092fSMaxime Coquelin {}, 74648a6092fSMaxime Coquelin }; 74748a6092fSMaxime Coquelin 74848a6092fSMaxime Coquelin MODULE_DEVICE_TABLE(of, stm32_match); 74948a6092fSMaxime Coquelin #endif 75048a6092fSMaxime Coquelin 75134891872SAlexandre TORGUE static int stm32_of_dma_rx_probe(struct stm32_port *stm32port, 75234891872SAlexandre TORGUE struct platform_device *pdev) 75334891872SAlexandre TORGUE { 75434891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 75534891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 75634891872SAlexandre TORGUE struct device *dev = &pdev->dev; 75734891872SAlexandre TORGUE struct dma_slave_config config; 75834891872SAlexandre TORGUE struct dma_async_tx_descriptor *desc = NULL; 75934891872SAlexandre TORGUE dma_cookie_t cookie; 76034891872SAlexandre TORGUE int ret; 76134891872SAlexandre TORGUE 76234891872SAlexandre TORGUE /* Request DMA RX channel */ 76334891872SAlexandre TORGUE stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); 76434891872SAlexandre TORGUE if (!stm32port->rx_ch) { 76534891872SAlexandre TORGUE dev_info(dev, "rx dma alloc failed\n"); 76634891872SAlexandre TORGUE return -ENODEV; 76734891872SAlexandre TORGUE } 76834891872SAlexandre TORGUE stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, 76934891872SAlexandre TORGUE &stm32port->rx_dma_buf, 77034891872SAlexandre TORGUE GFP_KERNEL); 77134891872SAlexandre TORGUE if (!stm32port->rx_buf) { 77234891872SAlexandre TORGUE ret = -ENOMEM; 77334891872SAlexandre TORGUE goto alloc_err; 77434891872SAlexandre TORGUE } 77534891872SAlexandre TORGUE 77634891872SAlexandre TORGUE /* Configure DMA channel */ 77734891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 7788e5481d9SArnd Bergmann config.src_addr = port->mapbase + ofs->rdr; 77934891872SAlexandre TORGUE config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 78034891872SAlexandre TORGUE 78134891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->rx_ch, &config); 78234891872SAlexandre TORGUE if (ret < 0) { 78334891872SAlexandre TORGUE dev_err(dev, "rx dma channel config failed\n"); 78434891872SAlexandre TORGUE ret = -ENODEV; 78534891872SAlexandre TORGUE goto config_err; 78634891872SAlexandre TORGUE } 78734891872SAlexandre TORGUE 78834891872SAlexandre TORGUE /* Prepare a DMA cyclic transaction */ 78934891872SAlexandre TORGUE desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, 79034891872SAlexandre TORGUE stm32port->rx_dma_buf, 79134891872SAlexandre TORGUE RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, 79234891872SAlexandre TORGUE DMA_PREP_INTERRUPT); 79334891872SAlexandre TORGUE if (!desc) { 79434891872SAlexandre TORGUE dev_err(dev, "rx dma prep cyclic failed\n"); 79534891872SAlexandre TORGUE ret = -ENODEV; 79634891872SAlexandre TORGUE goto config_err; 79734891872SAlexandre TORGUE } 79834891872SAlexandre TORGUE 79934891872SAlexandre TORGUE /* No callback as dma buffer is drained on usart interrupt */ 80034891872SAlexandre TORGUE desc->callback = NULL; 80134891872SAlexandre TORGUE desc->callback_param = NULL; 80234891872SAlexandre TORGUE 80334891872SAlexandre TORGUE /* Push current DMA transaction in the pending queue */ 80434891872SAlexandre TORGUE cookie = dmaengine_submit(desc); 80534891872SAlexandre TORGUE 80634891872SAlexandre TORGUE /* Issue pending DMA requests */ 80734891872SAlexandre TORGUE dma_async_issue_pending(stm32port->rx_ch); 80834891872SAlexandre TORGUE 80934891872SAlexandre TORGUE return 0; 81034891872SAlexandre TORGUE 81134891872SAlexandre TORGUE config_err: 81234891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 81334891872SAlexandre TORGUE RX_BUF_L, stm32port->rx_buf, 81434891872SAlexandre TORGUE stm32port->rx_dma_buf); 81534891872SAlexandre TORGUE 81634891872SAlexandre TORGUE alloc_err: 81734891872SAlexandre TORGUE dma_release_channel(stm32port->rx_ch); 81834891872SAlexandre TORGUE stm32port->rx_ch = NULL; 81934891872SAlexandre TORGUE 82034891872SAlexandre TORGUE return ret; 82134891872SAlexandre TORGUE } 82234891872SAlexandre TORGUE 82334891872SAlexandre TORGUE static int stm32_of_dma_tx_probe(struct stm32_port *stm32port, 82434891872SAlexandre TORGUE struct platform_device *pdev) 82534891872SAlexandre TORGUE { 82634891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32port->info->ofs; 82734891872SAlexandre TORGUE struct uart_port *port = &stm32port->port; 82834891872SAlexandre TORGUE struct device *dev = &pdev->dev; 82934891872SAlexandre TORGUE struct dma_slave_config config; 83034891872SAlexandre TORGUE int ret; 83134891872SAlexandre TORGUE 83234891872SAlexandre TORGUE stm32port->tx_dma_busy = false; 83334891872SAlexandre TORGUE 83434891872SAlexandre TORGUE /* Request DMA TX channel */ 83534891872SAlexandre TORGUE stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); 83634891872SAlexandre TORGUE if (!stm32port->tx_ch) { 83734891872SAlexandre TORGUE dev_info(dev, "tx dma alloc failed\n"); 83834891872SAlexandre TORGUE return -ENODEV; 83934891872SAlexandre TORGUE } 84034891872SAlexandre TORGUE stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, 84134891872SAlexandre TORGUE &stm32port->tx_dma_buf, 84234891872SAlexandre TORGUE GFP_KERNEL); 84334891872SAlexandre TORGUE if (!stm32port->tx_buf) { 84434891872SAlexandre TORGUE ret = -ENOMEM; 84534891872SAlexandre TORGUE goto alloc_err; 84634891872SAlexandre TORGUE } 84734891872SAlexandre TORGUE 84834891872SAlexandre TORGUE /* Configure DMA channel */ 84934891872SAlexandre TORGUE memset(&config, 0, sizeof(config)); 8508e5481d9SArnd Bergmann config.dst_addr = port->mapbase + ofs->tdr; 85134891872SAlexandre TORGUE config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 85234891872SAlexandre TORGUE 85334891872SAlexandre TORGUE ret = dmaengine_slave_config(stm32port->tx_ch, &config); 85434891872SAlexandre TORGUE if (ret < 0) { 85534891872SAlexandre TORGUE dev_err(dev, "tx dma channel config failed\n"); 85634891872SAlexandre TORGUE ret = -ENODEV; 85734891872SAlexandre TORGUE goto config_err; 85834891872SAlexandre TORGUE } 85934891872SAlexandre TORGUE 86034891872SAlexandre TORGUE return 0; 86134891872SAlexandre TORGUE 86234891872SAlexandre TORGUE config_err: 86334891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 86434891872SAlexandre TORGUE TX_BUF_L, stm32port->tx_buf, 86534891872SAlexandre TORGUE stm32port->tx_dma_buf); 86634891872SAlexandre TORGUE 86734891872SAlexandre TORGUE alloc_err: 86834891872SAlexandre TORGUE dma_release_channel(stm32port->tx_ch); 86934891872SAlexandre TORGUE stm32port->tx_ch = NULL; 87034891872SAlexandre TORGUE 87134891872SAlexandre TORGUE return ret; 87234891872SAlexandre TORGUE } 87334891872SAlexandre TORGUE 87448a6092fSMaxime Coquelin static int stm32_serial_probe(struct platform_device *pdev) 87548a6092fSMaxime Coquelin { 876ada8618fSAlexandre TORGUE const struct of_device_id *match; 87748a6092fSMaxime Coquelin struct stm32_port *stm32port; 878ada8618fSAlexandre TORGUE int ret; 87948a6092fSMaxime Coquelin 88048a6092fSMaxime Coquelin stm32port = stm32_of_get_stm32_port(pdev); 88148a6092fSMaxime Coquelin if (!stm32port) 88248a6092fSMaxime Coquelin return -ENODEV; 88348a6092fSMaxime Coquelin 884ada8618fSAlexandre TORGUE match = of_match_device(stm32_match, &pdev->dev); 885ada8618fSAlexandre TORGUE if (match && match->data) 886ada8618fSAlexandre TORGUE stm32port->info = (struct stm32_usart_info *)match->data; 887ada8618fSAlexandre TORGUE else 888ada8618fSAlexandre TORGUE return -EINVAL; 889ada8618fSAlexandre TORGUE 89048a6092fSMaxime Coquelin ret = stm32_init_port(stm32port, pdev); 89148a6092fSMaxime Coquelin if (ret) 89248a6092fSMaxime Coquelin return ret; 89348a6092fSMaxime Coquelin 894270e5a74SFabrice Gasnier if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) { 895270e5a74SFabrice Gasnier ret = device_init_wakeup(&pdev->dev, true); 89648a6092fSMaxime Coquelin if (ret) 897ada80043SFabrice Gasnier goto err_uninit; 898270e5a74SFabrice Gasnier } 899270e5a74SFabrice Gasnier 900270e5a74SFabrice Gasnier ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); 901270e5a74SFabrice Gasnier if (ret) 902270e5a74SFabrice Gasnier goto err_nowup; 90348a6092fSMaxime Coquelin 90434891872SAlexandre TORGUE ret = stm32_of_dma_rx_probe(stm32port, pdev); 90534891872SAlexandre TORGUE if (ret) 90634891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); 90734891872SAlexandre TORGUE 90834891872SAlexandre TORGUE ret = stm32_of_dma_tx_probe(stm32port, pdev); 90934891872SAlexandre TORGUE if (ret) 91034891872SAlexandre TORGUE dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); 91134891872SAlexandre TORGUE 91248a6092fSMaxime Coquelin platform_set_drvdata(pdev, &stm32port->port); 91348a6092fSMaxime Coquelin 91448a6092fSMaxime Coquelin return 0; 915ada80043SFabrice Gasnier 916270e5a74SFabrice Gasnier err_nowup: 917270e5a74SFabrice Gasnier if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) 918270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 919270e5a74SFabrice Gasnier 920ada80043SFabrice Gasnier err_uninit: 921ada80043SFabrice Gasnier clk_disable_unprepare(stm32port->clk); 922ada80043SFabrice Gasnier 923ada80043SFabrice Gasnier return ret; 92448a6092fSMaxime Coquelin } 92548a6092fSMaxime Coquelin 92648a6092fSMaxime Coquelin static int stm32_serial_remove(struct platform_device *pdev) 92748a6092fSMaxime Coquelin { 92848a6092fSMaxime Coquelin struct uart_port *port = platform_get_drvdata(pdev); 929511c7b1bSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 93034891872SAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 931270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 93234891872SAlexandre TORGUE 93334891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR); 93434891872SAlexandre TORGUE 93534891872SAlexandre TORGUE if (stm32_port->rx_ch) 93634891872SAlexandre TORGUE dma_release_channel(stm32_port->rx_ch); 93734891872SAlexandre TORGUE 93834891872SAlexandre TORGUE if (stm32_port->rx_dma_buf) 93934891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 94034891872SAlexandre TORGUE RX_BUF_L, stm32_port->rx_buf, 94134891872SAlexandre TORGUE stm32_port->rx_dma_buf); 94234891872SAlexandre TORGUE 94334891872SAlexandre TORGUE stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); 94434891872SAlexandre TORGUE 94534891872SAlexandre TORGUE if (stm32_port->tx_ch) 94634891872SAlexandre TORGUE dma_release_channel(stm32_port->tx_ch); 94734891872SAlexandre TORGUE 94834891872SAlexandre TORGUE if (stm32_port->tx_dma_buf) 94934891872SAlexandre TORGUE dma_free_coherent(&pdev->dev, 95034891872SAlexandre TORGUE TX_BUF_L, stm32_port->tx_buf, 95134891872SAlexandre TORGUE stm32_port->tx_dma_buf); 952511c7b1bSAlexandre TORGUE 953270e5a74SFabrice Gasnier if (cfg->has_wakeup && stm32_port->wakeirq >= 0) 954270e5a74SFabrice Gasnier device_init_wakeup(&pdev->dev, false); 955270e5a74SFabrice Gasnier 956511c7b1bSAlexandre TORGUE clk_disable_unprepare(stm32_port->clk); 95748a6092fSMaxime Coquelin 95848a6092fSMaxime Coquelin return uart_remove_one_port(&stm32_usart_driver, port); 95948a6092fSMaxime Coquelin } 96048a6092fSMaxime Coquelin 96148a6092fSMaxime Coquelin 96248a6092fSMaxime Coquelin #ifdef CONFIG_SERIAL_STM32_CONSOLE 96348a6092fSMaxime Coquelin static void stm32_console_putchar(struct uart_port *port, int ch) 96448a6092fSMaxime Coquelin { 965ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 966ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 967ada8618fSAlexandre TORGUE 968ada8618fSAlexandre TORGUE while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) 96948a6092fSMaxime Coquelin cpu_relax(); 97048a6092fSMaxime Coquelin 971ada8618fSAlexandre TORGUE writel_relaxed(ch, port->membase + ofs->tdr); 97248a6092fSMaxime Coquelin } 97348a6092fSMaxime Coquelin 97448a6092fSMaxime Coquelin static void stm32_console_write(struct console *co, const char *s, unsigned cnt) 97548a6092fSMaxime Coquelin { 97648a6092fSMaxime Coquelin struct uart_port *port = &stm32_ports[co->index].port; 977ada8618fSAlexandre TORGUE struct stm32_port *stm32_port = to_stm32_port(port); 978ada8618fSAlexandre TORGUE struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 97987f1f809SAlexandre TORGUE struct stm32_usart_config *cfg = &stm32_port->info->cfg; 98048a6092fSMaxime Coquelin unsigned long flags; 98148a6092fSMaxime Coquelin u32 old_cr1, new_cr1; 98248a6092fSMaxime Coquelin int locked = 1; 98348a6092fSMaxime Coquelin 98448a6092fSMaxime Coquelin local_irq_save(flags); 98548a6092fSMaxime Coquelin if (port->sysrq) 98648a6092fSMaxime Coquelin locked = 0; 98748a6092fSMaxime Coquelin else if (oops_in_progress) 98848a6092fSMaxime Coquelin locked = spin_trylock(&port->lock); 98948a6092fSMaxime Coquelin else 99048a6092fSMaxime Coquelin spin_lock(&port->lock); 99148a6092fSMaxime Coquelin 99287f1f809SAlexandre TORGUE /* Save and disable interrupts, enable the transmitter */ 993ada8618fSAlexandre TORGUE old_cr1 = readl_relaxed(port->membase + ofs->cr1); 99448a6092fSMaxime Coquelin new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; 99587f1f809SAlexandre TORGUE new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); 996ada8618fSAlexandre TORGUE writel_relaxed(new_cr1, port->membase + ofs->cr1); 99748a6092fSMaxime Coquelin 99848a6092fSMaxime Coquelin uart_console_write(port, s, cnt, stm32_console_putchar); 99948a6092fSMaxime Coquelin 100048a6092fSMaxime Coquelin /* Restore interrupt state */ 1001ada8618fSAlexandre TORGUE writel_relaxed(old_cr1, port->membase + ofs->cr1); 100248a6092fSMaxime Coquelin 100348a6092fSMaxime Coquelin if (locked) 100448a6092fSMaxime Coquelin spin_unlock(&port->lock); 100548a6092fSMaxime Coquelin local_irq_restore(flags); 100648a6092fSMaxime Coquelin } 100748a6092fSMaxime Coquelin 100848a6092fSMaxime Coquelin static int stm32_console_setup(struct console *co, char *options) 100948a6092fSMaxime Coquelin { 101048a6092fSMaxime Coquelin struct stm32_port *stm32port; 101148a6092fSMaxime Coquelin int baud = 9600; 101248a6092fSMaxime Coquelin int bits = 8; 101348a6092fSMaxime Coquelin int parity = 'n'; 101448a6092fSMaxime Coquelin int flow = 'n'; 101548a6092fSMaxime Coquelin 101648a6092fSMaxime Coquelin if (co->index >= STM32_MAX_PORTS) 101748a6092fSMaxime Coquelin return -ENODEV; 101848a6092fSMaxime Coquelin 101948a6092fSMaxime Coquelin stm32port = &stm32_ports[co->index]; 102048a6092fSMaxime Coquelin 102148a6092fSMaxime Coquelin /* 102248a6092fSMaxime Coquelin * This driver does not support early console initialization 102348a6092fSMaxime Coquelin * (use ARM early printk support instead), so we only expect 102448a6092fSMaxime Coquelin * this to be called during the uart port registration when the 102548a6092fSMaxime Coquelin * driver gets probed and the port should be mapped at that point. 102648a6092fSMaxime Coquelin */ 102748a6092fSMaxime Coquelin if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) 102848a6092fSMaxime Coquelin return -ENXIO; 102948a6092fSMaxime Coquelin 103048a6092fSMaxime Coquelin if (options) 103148a6092fSMaxime Coquelin uart_parse_options(options, &baud, &parity, &bits, &flow); 103248a6092fSMaxime Coquelin 103348a6092fSMaxime Coquelin return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); 103448a6092fSMaxime Coquelin } 103548a6092fSMaxime Coquelin 103648a6092fSMaxime Coquelin static struct console stm32_console = { 103748a6092fSMaxime Coquelin .name = STM32_SERIAL_NAME, 103848a6092fSMaxime Coquelin .device = uart_console_device, 103948a6092fSMaxime Coquelin .write = stm32_console_write, 104048a6092fSMaxime Coquelin .setup = stm32_console_setup, 104148a6092fSMaxime Coquelin .flags = CON_PRINTBUFFER, 104248a6092fSMaxime Coquelin .index = -1, 104348a6092fSMaxime Coquelin .data = &stm32_usart_driver, 104448a6092fSMaxime Coquelin }; 104548a6092fSMaxime Coquelin 104648a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE (&stm32_console) 104748a6092fSMaxime Coquelin 104848a6092fSMaxime Coquelin #else 104948a6092fSMaxime Coquelin #define STM32_SERIAL_CONSOLE NULL 105048a6092fSMaxime Coquelin #endif /* CONFIG_SERIAL_STM32_CONSOLE */ 105148a6092fSMaxime Coquelin 105248a6092fSMaxime Coquelin static struct uart_driver stm32_usart_driver = { 105348a6092fSMaxime Coquelin .driver_name = DRIVER_NAME, 105448a6092fSMaxime Coquelin .dev_name = STM32_SERIAL_NAME, 105548a6092fSMaxime Coquelin .major = 0, 105648a6092fSMaxime Coquelin .minor = 0, 105748a6092fSMaxime Coquelin .nr = STM32_MAX_PORTS, 105848a6092fSMaxime Coquelin .cons = STM32_SERIAL_CONSOLE, 105948a6092fSMaxime Coquelin }; 106048a6092fSMaxime Coquelin 1061270e5a74SFabrice Gasnier #ifdef CONFIG_PM_SLEEP 1062270e5a74SFabrice Gasnier static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable) 1063270e5a74SFabrice Gasnier { 1064270e5a74SFabrice Gasnier struct stm32_port *stm32_port = to_stm32_port(port); 1065270e5a74SFabrice Gasnier struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; 1066270e5a74SFabrice Gasnier struct stm32_usart_config *cfg = &stm32_port->info->cfg; 1067270e5a74SFabrice Gasnier u32 val; 1068270e5a74SFabrice Gasnier 1069270e5a74SFabrice Gasnier if (!cfg->has_wakeup || stm32_port->wakeirq < 0) 1070270e5a74SFabrice Gasnier return; 1071270e5a74SFabrice Gasnier 1072270e5a74SFabrice Gasnier if (enable) { 1073270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1074270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, USART_CR1_UESM); 1075270e5a74SFabrice Gasnier val = readl_relaxed(port->membase + ofs->cr3); 1076270e5a74SFabrice Gasnier val &= ~USART_CR3_WUS_MASK; 1077270e5a74SFabrice Gasnier /* Enable Wake up interrupt from low power on start bit */ 1078270e5a74SFabrice Gasnier val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE; 1079270e5a74SFabrice Gasnier writel_relaxed(val, port->membase + ofs->cr3); 1080270e5a74SFabrice Gasnier stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); 1081270e5a74SFabrice Gasnier } else { 1082270e5a74SFabrice Gasnier stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM); 1083270e5a74SFabrice Gasnier } 1084270e5a74SFabrice Gasnier } 1085270e5a74SFabrice Gasnier 1086270e5a74SFabrice Gasnier static int stm32_serial_suspend(struct device *dev) 1087270e5a74SFabrice Gasnier { 1088270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1089270e5a74SFabrice Gasnier 1090270e5a74SFabrice Gasnier uart_suspend_port(&stm32_usart_driver, port); 1091270e5a74SFabrice Gasnier 1092270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1093270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, true); 1094270e5a74SFabrice Gasnier else 1095270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1096270e5a74SFabrice Gasnier 1097270e5a74SFabrice Gasnier return 0; 1098270e5a74SFabrice Gasnier } 1099270e5a74SFabrice Gasnier 1100270e5a74SFabrice Gasnier static int stm32_serial_resume(struct device *dev) 1101270e5a74SFabrice Gasnier { 1102270e5a74SFabrice Gasnier struct uart_port *port = dev_get_drvdata(dev); 1103270e5a74SFabrice Gasnier 1104270e5a74SFabrice Gasnier if (device_may_wakeup(dev)) 1105270e5a74SFabrice Gasnier stm32_serial_enable_wakeup(port, false); 1106270e5a74SFabrice Gasnier 1107270e5a74SFabrice Gasnier return uart_resume_port(&stm32_usart_driver, port); 1108270e5a74SFabrice Gasnier } 1109270e5a74SFabrice Gasnier #endif /* CONFIG_PM_SLEEP */ 1110270e5a74SFabrice Gasnier 1111270e5a74SFabrice Gasnier static const struct dev_pm_ops stm32_serial_pm_ops = { 1112270e5a74SFabrice Gasnier SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume) 1113270e5a74SFabrice Gasnier }; 1114270e5a74SFabrice Gasnier 111548a6092fSMaxime Coquelin static struct platform_driver stm32_serial_driver = { 111648a6092fSMaxime Coquelin .probe = stm32_serial_probe, 111748a6092fSMaxime Coquelin .remove = stm32_serial_remove, 111848a6092fSMaxime Coquelin .driver = { 111948a6092fSMaxime Coquelin .name = DRIVER_NAME, 1120270e5a74SFabrice Gasnier .pm = &stm32_serial_pm_ops, 112148a6092fSMaxime Coquelin .of_match_table = of_match_ptr(stm32_match), 112248a6092fSMaxime Coquelin }, 112348a6092fSMaxime Coquelin }; 112448a6092fSMaxime Coquelin 112548a6092fSMaxime Coquelin static int __init usart_init(void) 112648a6092fSMaxime Coquelin { 112748a6092fSMaxime Coquelin static char banner[] __initdata = "STM32 USART driver initialized"; 112848a6092fSMaxime Coquelin int ret; 112948a6092fSMaxime Coquelin 113048a6092fSMaxime Coquelin pr_info("%s\n", banner); 113148a6092fSMaxime Coquelin 113248a6092fSMaxime Coquelin ret = uart_register_driver(&stm32_usart_driver); 113348a6092fSMaxime Coquelin if (ret) 113448a6092fSMaxime Coquelin return ret; 113548a6092fSMaxime Coquelin 113648a6092fSMaxime Coquelin ret = platform_driver_register(&stm32_serial_driver); 113748a6092fSMaxime Coquelin if (ret) 113848a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 113948a6092fSMaxime Coquelin 114048a6092fSMaxime Coquelin return ret; 114148a6092fSMaxime Coquelin } 114248a6092fSMaxime Coquelin 114348a6092fSMaxime Coquelin static void __exit usart_exit(void) 114448a6092fSMaxime Coquelin { 114548a6092fSMaxime Coquelin platform_driver_unregister(&stm32_serial_driver); 114648a6092fSMaxime Coquelin uart_unregister_driver(&stm32_usart_driver); 114748a6092fSMaxime Coquelin } 114848a6092fSMaxime Coquelin 114948a6092fSMaxime Coquelin module_init(usart_init); 115048a6092fSMaxime Coquelin module_exit(usart_exit); 115148a6092fSMaxime Coquelin 115248a6092fSMaxime Coquelin MODULE_ALIAS("platform:" DRIVER_NAME); 115348a6092fSMaxime Coquelin MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); 115448a6092fSMaxime Coquelin MODULE_LICENSE("GPL v2"); 1155