Greg Kroah-Hartman | e3b3d0f | 2017-11-06 18:11:51 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Maxime Coquelin 2015 |
Bich HEMON | 3e5fcba | 2017-07-13 15:08:26 +0000 | [diff] [blame] | 4 | * Copyright (C) STMicroelectronics SA 2017 |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 5 | * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com> |
| 6 | * Gerald Baeza <gerald.baeza@st.com> |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 7 | * |
| 8 | * Inspired by st-asc.c from STMicroelectronics (c) |
| 9 | */ |
| 10 | |
Maxime Coquelin | 6b596a8 | 2015-06-16 11:12:19 +0200 | [diff] [blame] | 11 | #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 12 | #define SUPPORT_SYSRQ |
| 13 | #endif |
| 14 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 15 | #include <linux/clk.h> |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 16 | #include <linux/console.h> |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 17 | #include <linux/delay.h> |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 18 | #include <linux/dma-direction.h> |
| 19 | #include <linux/dmaengine.h> |
| 20 | #include <linux/dma-mapping.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/iopoll.h> |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/module.h> |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 25 | #include <linux/of.h> |
| 26 | #include <linux/of_platform.h> |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/pm_runtime.h> |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 29 | #include <linux/pm_wakeirq.h> |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 30 | #include <linux/serial_core.h> |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 31 | #include <linux/serial.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/sysrq.h> |
| 34 | #include <linux/tty_flip.h> |
| 35 | #include <linux/tty.h> |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 36 | |
Alexandre TORGUE | bc5a0b5 | 2016-09-15 18:42:35 +0200 | [diff] [blame] | 37 | #include "stm32-usart.h" |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 38 | |
| 39 | static void stm32_stop_tx(struct uart_port *port); |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 40 | static void stm32_transmit_chars(struct uart_port *port); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 41 | |
| 42 | static inline struct stm32_port *to_stm32_port(struct uart_port *port) |
| 43 | { |
| 44 | return container_of(port, struct stm32_port, port); |
| 45 | } |
| 46 | |
| 47 | static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits) |
| 48 | { |
| 49 | u32 val; |
| 50 | |
| 51 | val = readl_relaxed(port->membase + reg); |
| 52 | val |= bits; |
| 53 | writel_relaxed(val, port->membase + reg); |
| 54 | } |
| 55 | |
| 56 | static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits) |
| 57 | { |
| 58 | u32 val; |
| 59 | |
| 60 | val = readl_relaxed(port->membase + reg); |
| 61 | val &= ~bits; |
| 62 | writel_relaxed(val, port->membase + reg); |
| 63 | } |
| 64 | |
Bich HEMON | 1bcda09 | 2018-03-12 09:50:05 +0000 | [diff] [blame] | 65 | static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE, |
| 66 | u32 delay_DDE, u32 baud) |
| 67 | { |
| 68 | u32 rs485_deat_dedt; |
| 69 | u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT); |
| 70 | bool over8; |
| 71 | |
| 72 | *cr3 |= USART_CR3_DEM; |
| 73 | over8 = *cr1 & USART_CR1_OVER8; |
| 74 | |
| 75 | if (over8) |
| 76 | rs485_deat_dedt = delay_ADE * baud * 8; |
| 77 | else |
| 78 | rs485_deat_dedt = delay_ADE * baud * 16; |
| 79 | |
| 80 | rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); |
| 81 | rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? |
| 82 | rs485_deat_dedt_max : rs485_deat_dedt; |
| 83 | rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) & |
| 84 | USART_CR1_DEAT_MASK; |
| 85 | *cr1 |= rs485_deat_dedt; |
| 86 | |
| 87 | if (over8) |
| 88 | rs485_deat_dedt = delay_DDE * baud * 8; |
| 89 | else |
| 90 | rs485_deat_dedt = delay_DDE * baud * 16; |
| 91 | |
| 92 | rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000); |
| 93 | rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ? |
| 94 | rs485_deat_dedt_max : rs485_deat_dedt; |
| 95 | rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) & |
| 96 | USART_CR1_DEDT_MASK; |
| 97 | *cr1 |= rs485_deat_dedt; |
| 98 | } |
| 99 | |
| 100 | static int stm32_config_rs485(struct uart_port *port, |
| 101 | struct serial_rs485 *rs485conf) |
| 102 | { |
| 103 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 104 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 105 | struct stm32_usart_config *cfg = &stm32_port->info->cfg; |
| 106 | u32 usartdiv, baud, cr1, cr3; |
| 107 | bool over8; |
| 108 | unsigned long flags; |
| 109 | |
| 110 | spin_lock_irqsave(&port->lock, flags); |
| 111 | stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); |
| 112 | |
| 113 | port->rs485 = *rs485conf; |
| 114 | |
| 115 | rs485conf->flags |= SER_RS485_RX_DURING_TX; |
| 116 | |
| 117 | if (rs485conf->flags & SER_RS485_ENABLED) { |
| 118 | cr1 = readl_relaxed(port->membase + ofs->cr1); |
| 119 | cr3 = readl_relaxed(port->membase + ofs->cr3); |
| 120 | usartdiv = readl_relaxed(port->membase + ofs->brr); |
| 121 | usartdiv = usartdiv & GENMASK(15, 0); |
| 122 | over8 = cr1 & USART_CR1_OVER8; |
| 123 | |
| 124 | if (over8) |
| 125 | usartdiv = usartdiv | (usartdiv & GENMASK(4, 0)) |
| 126 | << USART_BRR_04_R_SHIFT; |
| 127 | |
| 128 | baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv); |
| 129 | stm32_config_reg_rs485(&cr1, &cr3, |
| 130 | rs485conf->delay_rts_before_send, |
| 131 | rs485conf->delay_rts_after_send, baud); |
| 132 | |
| 133 | if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { |
| 134 | cr3 &= ~USART_CR3_DEP; |
| 135 | rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; |
| 136 | } else { |
| 137 | cr3 |= USART_CR3_DEP; |
| 138 | rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; |
| 139 | } |
| 140 | |
| 141 | writel_relaxed(cr3, port->membase + ofs->cr3); |
| 142 | writel_relaxed(cr1, port->membase + ofs->cr1); |
| 143 | } else { |
| 144 | stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP); |
| 145 | stm32_clr_bits(port, ofs->cr1, |
| 146 | USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); |
| 147 | } |
| 148 | |
| 149 | stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); |
| 150 | spin_unlock_irqrestore(&port->lock, flags); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int stm32_init_rs485(struct uart_port *port, |
| 156 | struct platform_device *pdev) |
| 157 | { |
| 158 | struct serial_rs485 *rs485conf = &port->rs485; |
| 159 | |
| 160 | rs485conf->flags = 0; |
| 161 | rs485conf->delay_rts_before_send = 0; |
| 162 | rs485conf->delay_rts_after_send = 0; |
| 163 | |
| 164 | if (!pdev->dev.of_node) |
| 165 | return -ENODEV; |
| 166 | |
| 167 | uart_get_rs485_mode(&pdev->dev, rs485conf); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Baoyou Xie | b97055b | 2016-09-26 19:58:56 +0800 | [diff] [blame] | 172 | static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res, |
| 173 | bool threaded) |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 174 | { |
| 175 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 176 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 177 | enum dma_status status; |
| 178 | struct dma_tx_state state; |
| 179 | |
| 180 | *sr = readl_relaxed(port->membase + ofs->isr); |
| 181 | |
| 182 | if (threaded && stm32_port->rx_ch) { |
| 183 | status = dmaengine_tx_status(stm32_port->rx_ch, |
| 184 | stm32_port->rx_ch->cookie, |
| 185 | &state); |
| 186 | if ((status == DMA_IN_PROGRESS) && |
| 187 | (*last_res != state.residue)) |
| 188 | return 1; |
| 189 | else |
| 190 | return 0; |
| 191 | } else if (*sr & USART_SR_RXNE) { |
| 192 | return 1; |
| 193 | } |
| 194 | return 0; |
| 195 | } |
| 196 | |
Erwan Le Ray | 6c5962f | 2019-05-21 17:45:43 +0200 | [diff] [blame] | 197 | static unsigned long stm32_get_char(struct uart_port *port, u32 *sr, |
| 198 | int *last_res) |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 199 | { |
| 200 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 201 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 202 | unsigned long c; |
| 203 | |
| 204 | if (stm32_port->rx_ch) { |
| 205 | c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--]; |
| 206 | if ((*last_res) == 0) |
| 207 | *last_res = RX_BUF_L; |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 208 | } else { |
Erwan Le Ray | 6c5962f | 2019-05-21 17:45:43 +0200 | [diff] [blame] | 209 | c = readl_relaxed(port->membase + ofs->rdr); |
| 210 | /* apply RDR data mask */ |
| 211 | c &= stm32_port->rdr_mask; |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 212 | } |
Erwan Le Ray | 6c5962f | 2019-05-21 17:45:43 +0200 | [diff] [blame] | 213 | |
| 214 | return c; |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static void stm32_receive_chars(struct uart_port *port, bool threaded) |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 218 | { |
| 219 | struct tty_port *tport = &port->state->port; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 220 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 221 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 222 | unsigned long c; |
| 223 | u32 sr; |
| 224 | char flag; |
| 225 | |
Andy Shevchenko | 29d6098 | 2017-08-13 17:47:41 +0300 | [diff] [blame] | 226 | if (irqd_is_wakeup_set(irq_get_irq_data(port->irq))) |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 227 | pm_wakeup_event(tport->tty->dev, 0); |
| 228 | |
Gerald Baeza | e570791 | 2017-07-13 15:08:27 +0000 | [diff] [blame] | 229 | while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) { |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 230 | sr |= USART_SR_DUMMY_RX; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 231 | flag = TTY_NORMAL; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 232 | |
Erwan Le Ray | 4f01d83 | 2019-05-21 17:45:42 +0200 | [diff] [blame] | 233 | /* |
| 234 | * Status bits has to be cleared before reading the RDR: |
| 235 | * In FIFO mode, reading the RDR will pop the next data |
| 236 | * (if any) along with its status bits into the SR. |
| 237 | * Not doing so leads to misalignement between RDR and SR, |
| 238 | * and clear status bits of the next rx data. |
| 239 | * |
| 240 | * Clear errors flags for stm32f7 and stm32h7 compatible |
| 241 | * devices. On stm32f4 compatible devices, the error bit is |
| 242 | * cleared by the sequence [read SR - read DR]. |
| 243 | */ |
| 244 | if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG) |
| 245 | stm32_clr_bits(port, ofs->icr, USART_ICR_ORECF | |
| 246 | USART_ICR_PECF | USART_ICR_FECF); |
| 247 | |
| 248 | c = stm32_get_char(port, &sr, &stm32_port->last_res); |
| 249 | port->icount.rx++; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 250 | if (sr & USART_SR_ERR_MASK) { |
Erwan Le Ray | 4f01d83 | 2019-05-21 17:45:42 +0200 | [diff] [blame] | 251 | if (sr & USART_SR_ORE) { |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 252 | port->icount.overrun++; |
| 253 | } else if (sr & USART_SR_PE) { |
| 254 | port->icount.parity++; |
| 255 | } else if (sr & USART_SR_FE) { |
Erwan Le Ray | 4f01d83 | 2019-05-21 17:45:42 +0200 | [diff] [blame] | 256 | /* Break detection if character is null */ |
| 257 | if (!c) { |
| 258 | port->icount.brk++; |
| 259 | if (uart_handle_break(port)) |
| 260 | continue; |
| 261 | } else { |
| 262 | port->icount.frame++; |
| 263 | } |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | sr &= port->read_status_mask; |
| 267 | |
Erwan Le Ray | 4f01d83 | 2019-05-21 17:45:42 +0200 | [diff] [blame] | 268 | if (sr & USART_SR_PE) { |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 269 | flag = TTY_PARITY; |
Erwan Le Ray | 4f01d83 | 2019-05-21 17:45:42 +0200 | [diff] [blame] | 270 | } else if (sr & USART_SR_FE) { |
| 271 | if (!c) |
| 272 | flag = TTY_BREAK; |
| 273 | else |
| 274 | flag = TTY_FRAME; |
| 275 | } |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (uart_handle_sysrq_char(port, c)) |
| 279 | continue; |
| 280 | uart_insert_char(port, sr, USART_SR_ORE, c, flag); |
| 281 | } |
| 282 | |
| 283 | spin_unlock(&port->lock); |
| 284 | tty_flip_buffer_push(tport); |
| 285 | spin_lock(&port->lock); |
| 286 | } |
| 287 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 288 | static void stm32_tx_dma_complete(void *arg) |
| 289 | { |
| 290 | struct uart_port *port = arg; |
| 291 | struct stm32_port *stm32port = to_stm32_port(port); |
| 292 | struct stm32_usart_offsets *ofs = &stm32port->info->ofs; |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 293 | |
| 294 | stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); |
| 295 | stm32port->tx_dma_busy = false; |
| 296 | |
| 297 | /* Let's see if we have pending data to send */ |
| 298 | stm32_transmit_chars(port); |
| 299 | } |
| 300 | |
| 301 | static void stm32_transmit_chars_pio(struct uart_port *port) |
| 302 | { |
| 303 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 304 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 305 | struct circ_buf *xmit = &port->state->xmit; |
| 306 | unsigned int isr; |
| 307 | int ret; |
| 308 | |
| 309 | if (stm32_port->tx_dma_busy) { |
| 310 | stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); |
| 311 | stm32_port->tx_dma_busy = false; |
| 312 | } |
| 313 | |
| 314 | ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, |
| 315 | isr, |
| 316 | (isr & USART_SR_TXE), |
Gerald Baeza | a61d9e6 | 2017-07-31 09:31:52 +0000 | [diff] [blame] | 317 | 10, 100000); |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 318 | |
| 319 | if (ret) |
| 320 | dev_err(port->dev, "tx empty not set\n"); |
| 321 | |
| 322 | stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE); |
| 323 | |
| 324 | writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr); |
| 325 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 326 | port->icount.tx++; |
| 327 | } |
| 328 | |
| 329 | static void stm32_transmit_chars_dma(struct uart_port *port) |
| 330 | { |
| 331 | struct stm32_port *stm32port = to_stm32_port(port); |
| 332 | struct stm32_usart_offsets *ofs = &stm32port->info->ofs; |
| 333 | struct circ_buf *xmit = &port->state->xmit; |
| 334 | struct dma_async_tx_descriptor *desc = NULL; |
| 335 | dma_cookie_t cookie; |
| 336 | unsigned int count, i; |
| 337 | |
| 338 | if (stm32port->tx_dma_busy) |
| 339 | return; |
| 340 | |
| 341 | stm32port->tx_dma_busy = true; |
| 342 | |
| 343 | count = uart_circ_chars_pending(xmit); |
| 344 | |
| 345 | if (count > TX_BUF_L) |
| 346 | count = TX_BUF_L; |
| 347 | |
| 348 | if (xmit->tail < xmit->head) { |
| 349 | memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count); |
| 350 | } else { |
| 351 | size_t one = UART_XMIT_SIZE - xmit->tail; |
| 352 | size_t two; |
| 353 | |
| 354 | if (one > count) |
| 355 | one = count; |
| 356 | two = count - one; |
| 357 | |
| 358 | memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one); |
| 359 | if (two) |
| 360 | memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two); |
| 361 | } |
| 362 | |
| 363 | desc = dmaengine_prep_slave_single(stm32port->tx_ch, |
| 364 | stm32port->tx_dma_buf, |
| 365 | count, |
| 366 | DMA_MEM_TO_DEV, |
| 367 | DMA_PREP_INTERRUPT); |
| 368 | |
| 369 | if (!desc) { |
| 370 | for (i = count; i > 0; i--) |
| 371 | stm32_transmit_chars_pio(port); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | desc->callback = stm32_tx_dma_complete; |
| 376 | desc->callback_param = port; |
| 377 | |
| 378 | /* Push current DMA TX transaction in the pending queue */ |
| 379 | cookie = dmaengine_submit(desc); |
| 380 | |
| 381 | /* Issue pending DMA TX requests */ |
| 382 | dma_async_issue_pending(stm32port->tx_ch); |
| 383 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 384 | stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); |
| 385 | |
| 386 | xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1); |
| 387 | port->icount.tx += count; |
| 388 | } |
| 389 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 390 | static void stm32_transmit_chars(struct uart_port *port) |
| 391 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 392 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 393 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 394 | struct circ_buf *xmit = &port->state->xmit; |
| 395 | |
| 396 | if (port->x_char) { |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 397 | if (stm32_port->tx_dma_busy) |
| 398 | stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 399 | writel_relaxed(port->x_char, port->membase + ofs->tdr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 400 | port->x_char = 0; |
| 401 | port->icount.tx++; |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 402 | if (stm32_port->tx_dma_busy) |
| 403 | stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 404 | return; |
| 405 | } |
| 406 | |
Erwan Le Ray | b83b957 | 2019-05-21 17:45:44 +0200 | [diff] [blame] | 407 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { |
| 408 | stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | |
Erwan Le Ray | 64c32ea | 2019-05-21 17:45:45 +0200 | [diff] [blame^] | 412 | if (ofs->icr == UNDEF_REG) |
| 413 | stm32_clr_bits(port, ofs->isr, USART_SR_TC); |
| 414 | else |
| 415 | stm32_set_bits(port, ofs->icr, USART_ICR_TCCF); |
| 416 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 417 | if (stm32_port->tx_ch) |
| 418 | stm32_transmit_chars_dma(port); |
| 419 | else |
| 420 | stm32_transmit_chars_pio(port); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 421 | |
| 422 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 423 | uart_write_wakeup(port); |
| 424 | |
| 425 | if (uart_circ_empty(xmit)) |
Erwan Le Ray | b83b957 | 2019-05-21 17:45:44 +0200 | [diff] [blame] | 426 | stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static irqreturn_t stm32_interrupt(int irq, void *ptr) |
| 430 | { |
| 431 | struct uart_port *port = ptr; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 432 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 433 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 434 | u32 sr; |
| 435 | |
Alexandre TORGUE | 01d32d7 | 2016-09-15 18:42:41 +0200 | [diff] [blame] | 436 | spin_lock(&port->lock); |
| 437 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 438 | sr = readl_relaxed(port->membase + ofs->isr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 439 | |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 440 | if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG)) |
| 441 | writel_relaxed(USART_ICR_WUCF, |
| 442 | port->membase + ofs->icr); |
| 443 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 444 | if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch)) |
| 445 | stm32_receive_chars(port, false); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 446 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 447 | if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 448 | stm32_transmit_chars(port); |
| 449 | |
Alexandre TORGUE | 01d32d7 | 2016-09-15 18:42:41 +0200 | [diff] [blame] | 450 | spin_unlock(&port->lock); |
| 451 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 452 | if (stm32_port->rx_ch) |
| 453 | return IRQ_WAKE_THREAD; |
| 454 | else |
| 455 | return IRQ_HANDLED; |
| 456 | } |
| 457 | |
| 458 | static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr) |
| 459 | { |
| 460 | struct uart_port *port = ptr; |
| 461 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 462 | |
| 463 | spin_lock(&port->lock); |
| 464 | |
| 465 | if (stm32_port->rx_ch) |
| 466 | stm32_receive_chars(port, true); |
| 467 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 468 | spin_unlock(&port->lock); |
| 469 | |
| 470 | return IRQ_HANDLED; |
| 471 | } |
| 472 | |
| 473 | static unsigned int stm32_tx_empty(struct uart_port *port) |
| 474 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 475 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 476 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 477 | |
| 478 | return readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 482 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 483 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 484 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 485 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 486 | if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS)) |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 487 | stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 488 | else |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 489 | stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static unsigned int stm32_get_mctrl(struct uart_port *port) |
| 493 | { |
| 494 | /* This routine is used to get signals of: DCD, DSR, RI, and CTS */ |
| 495 | return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS; |
| 496 | } |
| 497 | |
| 498 | /* Transmit stop */ |
| 499 | static void stm32_stop_tx(struct uart_port *port) |
| 500 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 501 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 502 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 503 | |
| 504 | stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /* There are probably characters waiting to be transmitted. */ |
| 508 | static void stm32_start_tx(struct uart_port *port) |
| 509 | { |
| 510 | struct circ_buf *xmit = &port->state->xmit; |
| 511 | |
| 512 | if (uart_circ_empty(xmit)) |
| 513 | return; |
| 514 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 515 | stm32_transmit_chars(port); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* Throttle the remote when input buffer is about to overflow. */ |
| 519 | static void stm32_throttle(struct uart_port *port) |
| 520 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 521 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 522 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 523 | unsigned long flags; |
| 524 | |
| 525 | spin_lock_irqsave(&port->lock, flags); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 526 | stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 527 | spin_unlock_irqrestore(&port->lock, flags); |
| 528 | } |
| 529 | |
| 530 | /* Unthrottle the remote, the input buffer can now accept data. */ |
| 531 | static void stm32_unthrottle(struct uart_port *port) |
| 532 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 533 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 534 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 535 | unsigned long flags; |
| 536 | |
| 537 | spin_lock_irqsave(&port->lock, flags); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 538 | stm32_set_bits(port, ofs->cr1, USART_CR1_RXNEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 539 | spin_unlock_irqrestore(&port->lock, flags); |
| 540 | } |
| 541 | |
| 542 | /* Receive stop */ |
| 543 | static void stm32_stop_rx(struct uart_port *port) |
| 544 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 545 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 546 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 547 | |
| 548 | stm32_clr_bits(port, ofs->cr1, USART_CR1_RXNEIE); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* Handle breaks - ignored by us */ |
| 552 | static void stm32_break_ctl(struct uart_port *port, int break_state) |
| 553 | { |
| 554 | } |
| 555 | |
| 556 | static int stm32_startup(struct uart_port *port) |
| 557 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 558 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 559 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 560 | struct stm32_usart_config *cfg = &stm32_port->info->cfg; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 561 | const char *name = to_platform_device(port->dev)->name; |
| 562 | u32 val; |
| 563 | int ret; |
| 564 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 565 | ret = request_threaded_irq(port->irq, stm32_interrupt, |
| 566 | stm32_threaded_interrupt, |
| 567 | IRQF_NO_SUSPEND, name, port); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 568 | if (ret) |
| 569 | return ret; |
| 570 | |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 571 | if (cfg->has_wakeup && stm32_port->wakeirq >= 0) { |
| 572 | ret = dev_pm_set_dedicated_wake_irq(port->dev, |
| 573 | stm32_port->wakeirq); |
| 574 | if (ret) { |
| 575 | free_irq(port->irq, port); |
| 576 | return ret; |
| 577 | } |
| 578 | } |
| 579 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 580 | val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; |
Gerald Baeza | 351a762 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 581 | if (stm32_port->fifoen) |
| 582 | val |= USART_CR1_FIFOEN; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 583 | stm32_set_bits(port, ofs->cr1, val); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static void stm32_shutdown(struct uart_port *port) |
| 589 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 590 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 591 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Alexandre TORGUE | 87f1f80 | 2016-09-15 18:42:42 +0200 | [diff] [blame] | 592 | struct stm32_usart_config *cfg = &stm32_port->info->cfg; |
Erwan Le Ray | 64c32ea | 2019-05-21 17:45:45 +0200 | [diff] [blame^] | 593 | u32 val, isr; |
| 594 | int ret; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 595 | |
| 596 | val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE; |
Alexandre TORGUE | 87f1f80 | 2016-09-15 18:42:42 +0200 | [diff] [blame] | 597 | val |= BIT(cfg->uart_enable_bit); |
Gerald Baeza | 351a762 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 598 | if (stm32_port->fifoen) |
| 599 | val |= USART_CR1_FIFOEN; |
Erwan Le Ray | 64c32ea | 2019-05-21 17:45:45 +0200 | [diff] [blame^] | 600 | |
| 601 | ret = readl_relaxed_poll_timeout(port->membase + ofs->isr, |
| 602 | isr, (isr & USART_SR_TC), |
| 603 | 10, 100000); |
| 604 | |
| 605 | if (ret) |
| 606 | dev_err(port->dev, "transmission complete not set\n"); |
| 607 | |
Alexandre TORGUE | a14f66a | 2016-09-15 18:42:36 +0200 | [diff] [blame] | 608 | stm32_clr_bits(port, ofs->cr1, val); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 609 | |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 610 | dev_pm_clear_wake_irq(port->dev); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 611 | free_irq(port->irq, port); |
| 612 | } |
| 613 | |
Erwan Le Ray | c8a9d04 | 2019-05-21 17:45:41 +0200 | [diff] [blame] | 614 | unsigned int stm32_get_databits(struct ktermios *termios) |
| 615 | { |
| 616 | unsigned int bits; |
| 617 | |
| 618 | tcflag_t cflag = termios->c_cflag; |
| 619 | |
| 620 | switch (cflag & CSIZE) { |
| 621 | /* |
| 622 | * CSIZE settings are not necessarily supported in hardware. |
| 623 | * CSIZE unsupported configurations are handled here to set word length |
| 624 | * to 8 bits word as default configuration and to print debug message. |
| 625 | */ |
| 626 | case CS5: |
| 627 | bits = 5; |
| 628 | break; |
| 629 | case CS6: |
| 630 | bits = 6; |
| 631 | break; |
| 632 | case CS7: |
| 633 | bits = 7; |
| 634 | break; |
| 635 | /* default including CS8 */ |
| 636 | default: |
| 637 | bits = 8; |
| 638 | break; |
| 639 | } |
| 640 | |
| 641 | return bits; |
| 642 | } |
| 643 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 644 | static void stm32_set_termios(struct uart_port *port, struct ktermios *termios, |
| 645 | struct ktermios *old) |
| 646 | { |
| 647 | struct stm32_port *stm32_port = to_stm32_port(port); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 648 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 649 | struct stm32_usart_config *cfg = &stm32_port->info->cfg; |
Bich HEMON | 1bcda09 | 2018-03-12 09:50:05 +0000 | [diff] [blame] | 650 | struct serial_rs485 *rs485conf = &port->rs485; |
Erwan Le Ray | c8a9d04 | 2019-05-21 17:45:41 +0200 | [diff] [blame] | 651 | unsigned int baud, bits; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 652 | u32 usartdiv, mantissa, fraction, oversampling; |
| 653 | tcflag_t cflag = termios->c_cflag; |
| 654 | u32 cr1, cr2, cr3; |
| 655 | unsigned long flags; |
| 656 | |
| 657 | if (!stm32_port->hw_flow_control) |
| 658 | cflag &= ~CRTSCTS; |
| 659 | |
| 660 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8); |
| 661 | |
| 662 | spin_lock_irqsave(&port->lock, flags); |
| 663 | |
| 664 | /* Stop serial port and reset value */ |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 665 | writel_relaxed(0, port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 666 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 667 | cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; |
Bich HEMON | 1bcda09 | 2018-03-12 09:50:05 +0000 | [diff] [blame] | 668 | |
Gerald Baeza | 351a762 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 669 | if (stm32_port->fifoen) |
| 670 | cr1 |= USART_CR1_FIFOEN; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 671 | cr2 = 0; |
| 672 | cr3 = 0; |
| 673 | |
| 674 | if (cflag & CSTOPB) |
| 675 | cr2 |= USART_CR2_STOP_2B; |
| 676 | |
Erwan Le Ray | c8a9d04 | 2019-05-21 17:45:41 +0200 | [diff] [blame] | 677 | bits = stm32_get_databits(termios); |
Erwan Le Ray | 6c5962f | 2019-05-21 17:45:43 +0200 | [diff] [blame] | 678 | stm32_port->rdr_mask = (BIT(bits) - 1); |
Erwan Le Ray | c8a9d04 | 2019-05-21 17:45:41 +0200 | [diff] [blame] | 679 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 680 | if (cflag & PARENB) { |
Erwan Le Ray | c8a9d04 | 2019-05-21 17:45:41 +0200 | [diff] [blame] | 681 | bits++; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 682 | cr1 |= USART_CR1_PCE; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 683 | } |
| 684 | |
Erwan Le Ray | c8a9d04 | 2019-05-21 17:45:41 +0200 | [diff] [blame] | 685 | /* |
| 686 | * Word length configuration: |
| 687 | * CS8 + parity, 9 bits word aka [M1:M0] = 0b01 |
| 688 | * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10 |
| 689 | * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00 |
| 690 | * M0 and M1 already cleared by cr1 initialization. |
| 691 | */ |
| 692 | if (bits == 9) |
| 693 | cr1 |= USART_CR1_M0; |
| 694 | else if ((bits == 7) && cfg->has_7bits_data) |
| 695 | cr1 |= USART_CR1_M1; |
| 696 | else if (bits != 8) |
| 697 | dev_dbg(port->dev, "Unsupported data bits config: %u bits\n" |
| 698 | , bits); |
| 699 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 700 | if (cflag & PARODD) |
| 701 | cr1 |= USART_CR1_PS; |
| 702 | |
| 703 | port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS); |
| 704 | if (cflag & CRTSCTS) { |
| 705 | port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; |
Bich HEMON | 35abe98 | 2017-07-13 15:08:28 +0000 | [diff] [blame] | 706 | cr3 |= USART_CR3_CTSE | USART_CR3_RTSE; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud); |
| 710 | |
| 711 | /* |
| 712 | * The USART supports 16 or 8 times oversampling. |
| 713 | * By default we prefer 16 times oversampling, so that the receiver |
| 714 | * has a better tolerance to clock deviations. |
| 715 | * 8 times oversampling is only used to achieve higher speeds. |
| 716 | */ |
| 717 | if (usartdiv < 16) { |
| 718 | oversampling = 8; |
Bich HEMON | 1bcda09 | 2018-03-12 09:50:05 +0000 | [diff] [blame] | 719 | cr1 |= USART_CR1_OVER8; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 720 | stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 721 | } else { |
| 722 | oversampling = 16; |
Bich HEMON | 1bcda09 | 2018-03-12 09:50:05 +0000 | [diff] [blame] | 723 | cr1 &= ~USART_CR1_OVER8; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 724 | stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT; |
| 728 | fraction = usartdiv % oversampling; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 729 | writel_relaxed(mantissa | fraction, port->membase + ofs->brr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 730 | |
| 731 | uart_update_timeout(port, cflag, baud); |
| 732 | |
| 733 | port->read_status_mask = USART_SR_ORE; |
| 734 | if (termios->c_iflag & INPCK) |
| 735 | port->read_status_mask |= USART_SR_PE | USART_SR_FE; |
| 736 | if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) |
Erwan Le Ray | 4f01d83 | 2019-05-21 17:45:42 +0200 | [diff] [blame] | 737 | port->read_status_mask |= USART_SR_FE; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 738 | |
| 739 | /* Characters to ignore */ |
| 740 | port->ignore_status_mask = 0; |
| 741 | if (termios->c_iflag & IGNPAR) |
| 742 | port->ignore_status_mask = USART_SR_PE | USART_SR_FE; |
| 743 | if (termios->c_iflag & IGNBRK) { |
Erwan Le Ray | 4f01d83 | 2019-05-21 17:45:42 +0200 | [diff] [blame] | 744 | port->ignore_status_mask |= USART_SR_FE; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 745 | /* |
| 746 | * If we're ignoring parity and break indicators, |
| 747 | * ignore overruns too (for real raw support). |
| 748 | */ |
| 749 | if (termios->c_iflag & IGNPAR) |
| 750 | port->ignore_status_mask |= USART_SR_ORE; |
| 751 | } |
| 752 | |
| 753 | /* Ignore all characters if CREAD is not set */ |
| 754 | if ((termios->c_cflag & CREAD) == 0) |
| 755 | port->ignore_status_mask |= USART_SR_DUMMY_RX; |
| 756 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 757 | if (stm32_port->rx_ch) |
| 758 | cr3 |= USART_CR3_DMAR; |
| 759 | |
Bich HEMON | 1bcda09 | 2018-03-12 09:50:05 +0000 | [diff] [blame] | 760 | if (rs485conf->flags & SER_RS485_ENABLED) { |
| 761 | stm32_config_reg_rs485(&cr1, &cr3, |
| 762 | rs485conf->delay_rts_before_send, |
| 763 | rs485conf->delay_rts_after_send, baud); |
| 764 | if (rs485conf->flags & SER_RS485_RTS_ON_SEND) { |
| 765 | cr3 &= ~USART_CR3_DEP; |
| 766 | rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND; |
| 767 | } else { |
| 768 | cr3 |= USART_CR3_DEP; |
| 769 | rs485conf->flags |= SER_RS485_RTS_AFTER_SEND; |
| 770 | } |
| 771 | |
| 772 | } else { |
| 773 | cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP); |
| 774 | cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK); |
| 775 | } |
| 776 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 777 | writel_relaxed(cr3, port->membase + ofs->cr3); |
| 778 | writel_relaxed(cr2, port->membase + ofs->cr2); |
| 779 | writel_relaxed(cr1, port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 780 | |
Bich HEMON | 1bcda09 | 2018-03-12 09:50:05 +0000 | [diff] [blame] | 781 | stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 782 | spin_unlock_irqrestore(&port->lock, flags); |
| 783 | } |
| 784 | |
| 785 | static const char *stm32_type(struct uart_port *port) |
| 786 | { |
| 787 | return (port->type == PORT_STM32) ? DRIVER_NAME : NULL; |
| 788 | } |
| 789 | |
| 790 | static void stm32_release_port(struct uart_port *port) |
| 791 | { |
| 792 | } |
| 793 | |
| 794 | static int stm32_request_port(struct uart_port *port) |
| 795 | { |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | static void stm32_config_port(struct uart_port *port, int flags) |
| 800 | { |
| 801 | if (flags & UART_CONFIG_TYPE) |
| 802 | port->type = PORT_STM32; |
| 803 | } |
| 804 | |
| 805 | static int |
| 806 | stm32_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 807 | { |
| 808 | /* No user changeable parameters */ |
| 809 | return -EINVAL; |
| 810 | } |
| 811 | |
| 812 | static void stm32_pm(struct uart_port *port, unsigned int state, |
| 813 | unsigned int oldstate) |
| 814 | { |
| 815 | struct stm32_port *stm32port = container_of(port, |
| 816 | struct stm32_port, port); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 817 | struct stm32_usart_offsets *ofs = &stm32port->info->ofs; |
| 818 | struct stm32_usart_config *cfg = &stm32port->info->cfg; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 819 | unsigned long flags = 0; |
| 820 | |
| 821 | switch (state) { |
| 822 | case UART_PM_STATE_ON: |
| 823 | clk_prepare_enable(stm32port->clk); |
| 824 | break; |
| 825 | case UART_PM_STATE_OFF: |
| 826 | spin_lock_irqsave(&port->lock, flags); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 827 | stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 828 | spin_unlock_irqrestore(&port->lock, flags); |
| 829 | clk_disable_unprepare(stm32port->clk); |
| 830 | break; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | static const struct uart_ops stm32_uart_ops = { |
| 835 | .tx_empty = stm32_tx_empty, |
| 836 | .set_mctrl = stm32_set_mctrl, |
| 837 | .get_mctrl = stm32_get_mctrl, |
| 838 | .stop_tx = stm32_stop_tx, |
| 839 | .start_tx = stm32_start_tx, |
| 840 | .throttle = stm32_throttle, |
| 841 | .unthrottle = stm32_unthrottle, |
| 842 | .stop_rx = stm32_stop_rx, |
| 843 | .break_ctl = stm32_break_ctl, |
| 844 | .startup = stm32_startup, |
| 845 | .shutdown = stm32_shutdown, |
| 846 | .set_termios = stm32_set_termios, |
| 847 | .pm = stm32_pm, |
| 848 | .type = stm32_type, |
| 849 | .release_port = stm32_release_port, |
| 850 | .request_port = stm32_request_port, |
| 851 | .config_port = stm32_config_port, |
| 852 | .verify_port = stm32_verify_port, |
| 853 | }; |
| 854 | |
| 855 | static int stm32_init_port(struct stm32_port *stm32port, |
| 856 | struct platform_device *pdev) |
| 857 | { |
| 858 | struct uart_port *port = &stm32port->port; |
| 859 | struct resource *res; |
| 860 | int ret; |
| 861 | |
| 862 | port->iotype = UPIO_MEM; |
| 863 | port->flags = UPF_BOOT_AUTOCONF; |
| 864 | port->ops = &stm32_uart_ops; |
| 865 | port->dev = &pdev->dev; |
| 866 | port->irq = platform_get_irq(pdev, 0); |
Bich HEMON | 7d8f686 | 2018-03-15 08:44:46 +0000 | [diff] [blame] | 867 | port->rs485_config = stm32_config_rs485; |
| 868 | |
| 869 | stm32_init_rs485(port, pdev); |
| 870 | |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 871 | stm32port->wakeirq = platform_get_irq(pdev, 1); |
Gerald Baeza | 351a762 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 872 | stm32port->fifoen = stm32port->info->cfg.has_fifo; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 873 | |
| 874 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 875 | port->membase = devm_ioremap_resource(&pdev->dev, res); |
| 876 | if (IS_ERR(port->membase)) |
| 877 | return PTR_ERR(port->membase); |
| 878 | port->mapbase = res->start; |
| 879 | |
| 880 | spin_lock_init(&port->lock); |
| 881 | |
| 882 | stm32port->clk = devm_clk_get(&pdev->dev, NULL); |
| 883 | if (IS_ERR(stm32port->clk)) |
| 884 | return PTR_ERR(stm32port->clk); |
| 885 | |
| 886 | /* Ensure that clk rate is correct by enabling the clk */ |
| 887 | ret = clk_prepare_enable(stm32port->clk); |
| 888 | if (ret) |
| 889 | return ret; |
| 890 | |
| 891 | stm32port->port.uartclk = clk_get_rate(stm32port->clk); |
Fabrice Gasnier | ada8004 | 2017-07-13 15:08:29 +0000 | [diff] [blame] | 892 | if (!stm32port->port.uartclk) { |
| 893 | clk_disable_unprepare(stm32port->clk); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 894 | ret = -EINVAL; |
Fabrice Gasnier | ada8004 | 2017-07-13 15:08:29 +0000 | [diff] [blame] | 895 | } |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 896 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 897 | return ret; |
| 898 | } |
| 899 | |
| 900 | static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev) |
| 901 | { |
| 902 | struct device_node *np = pdev->dev.of_node; |
| 903 | int id; |
| 904 | |
| 905 | if (!np) |
| 906 | return NULL; |
| 907 | |
| 908 | id = of_alias_get_id(np, "serial"); |
Gerald Baeza | e570791 | 2017-07-13 15:08:27 +0000 | [diff] [blame] | 909 | if (id < 0) { |
| 910 | dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id); |
| 911 | return NULL; |
| 912 | } |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 913 | |
| 914 | if (WARN_ON(id >= STM32_MAX_PORTS)) |
| 915 | return NULL; |
| 916 | |
| 917 | stm32_ports[id].hw_flow_control = of_property_read_bool(np, |
Alexandre TORGUE | 59bed2d | 2016-09-15 18:42:37 +0200 | [diff] [blame] | 918 | "st,hw-flow-ctrl"); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 919 | stm32_ports[id].port.line = id; |
Gerald Baeza | e570791 | 2017-07-13 15:08:27 +0000 | [diff] [blame] | 920 | stm32_ports[id].last_res = RX_BUF_L; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 921 | return &stm32_ports[id]; |
| 922 | } |
| 923 | |
| 924 | #ifdef CONFIG_OF |
| 925 | static const struct of_device_id stm32_match[] = { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 926 | { .compatible = "st,stm32-uart", .data = &stm32f4_info}, |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 927 | { .compatible = "st,stm32f7-uart", .data = &stm32f7_info}, |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 928 | { .compatible = "st,stm32h7-uart", .data = &stm32h7_info}, |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 929 | {}, |
| 930 | }; |
| 931 | |
| 932 | MODULE_DEVICE_TABLE(of, stm32_match); |
| 933 | #endif |
| 934 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 935 | static int stm32_of_dma_rx_probe(struct stm32_port *stm32port, |
| 936 | struct platform_device *pdev) |
| 937 | { |
| 938 | struct stm32_usart_offsets *ofs = &stm32port->info->ofs; |
| 939 | struct uart_port *port = &stm32port->port; |
| 940 | struct device *dev = &pdev->dev; |
| 941 | struct dma_slave_config config; |
| 942 | struct dma_async_tx_descriptor *desc = NULL; |
| 943 | dma_cookie_t cookie; |
| 944 | int ret; |
| 945 | |
| 946 | /* Request DMA RX channel */ |
| 947 | stm32port->rx_ch = dma_request_slave_channel(dev, "rx"); |
| 948 | if (!stm32port->rx_ch) { |
| 949 | dev_info(dev, "rx dma alloc failed\n"); |
| 950 | return -ENODEV; |
| 951 | } |
| 952 | stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L, |
| 953 | &stm32port->rx_dma_buf, |
| 954 | GFP_KERNEL); |
| 955 | if (!stm32port->rx_buf) { |
| 956 | ret = -ENOMEM; |
| 957 | goto alloc_err; |
| 958 | } |
| 959 | |
| 960 | /* Configure DMA channel */ |
| 961 | memset(&config, 0, sizeof(config)); |
Arnd Bergmann | 8e5481d | 2016-09-23 21:38:51 +0200 | [diff] [blame] | 962 | config.src_addr = port->mapbase + ofs->rdr; |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 963 | config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 964 | |
| 965 | ret = dmaengine_slave_config(stm32port->rx_ch, &config); |
| 966 | if (ret < 0) { |
| 967 | dev_err(dev, "rx dma channel config failed\n"); |
| 968 | ret = -ENODEV; |
| 969 | goto config_err; |
| 970 | } |
| 971 | |
| 972 | /* Prepare a DMA cyclic transaction */ |
| 973 | desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch, |
| 974 | stm32port->rx_dma_buf, |
| 975 | RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM, |
| 976 | DMA_PREP_INTERRUPT); |
| 977 | if (!desc) { |
| 978 | dev_err(dev, "rx dma prep cyclic failed\n"); |
| 979 | ret = -ENODEV; |
| 980 | goto config_err; |
| 981 | } |
| 982 | |
| 983 | /* No callback as dma buffer is drained on usart interrupt */ |
| 984 | desc->callback = NULL; |
| 985 | desc->callback_param = NULL; |
| 986 | |
| 987 | /* Push current DMA transaction in the pending queue */ |
| 988 | cookie = dmaengine_submit(desc); |
| 989 | |
| 990 | /* Issue pending DMA requests */ |
| 991 | dma_async_issue_pending(stm32port->rx_ch); |
| 992 | |
| 993 | return 0; |
| 994 | |
| 995 | config_err: |
| 996 | dma_free_coherent(&pdev->dev, |
| 997 | RX_BUF_L, stm32port->rx_buf, |
| 998 | stm32port->rx_dma_buf); |
| 999 | |
| 1000 | alloc_err: |
| 1001 | dma_release_channel(stm32port->rx_ch); |
| 1002 | stm32port->rx_ch = NULL; |
| 1003 | |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | static int stm32_of_dma_tx_probe(struct stm32_port *stm32port, |
| 1008 | struct platform_device *pdev) |
| 1009 | { |
| 1010 | struct stm32_usart_offsets *ofs = &stm32port->info->ofs; |
| 1011 | struct uart_port *port = &stm32port->port; |
| 1012 | struct device *dev = &pdev->dev; |
| 1013 | struct dma_slave_config config; |
| 1014 | int ret; |
| 1015 | |
| 1016 | stm32port->tx_dma_busy = false; |
| 1017 | |
| 1018 | /* Request DMA TX channel */ |
| 1019 | stm32port->tx_ch = dma_request_slave_channel(dev, "tx"); |
| 1020 | if (!stm32port->tx_ch) { |
| 1021 | dev_info(dev, "tx dma alloc failed\n"); |
| 1022 | return -ENODEV; |
| 1023 | } |
| 1024 | stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L, |
| 1025 | &stm32port->tx_dma_buf, |
| 1026 | GFP_KERNEL); |
| 1027 | if (!stm32port->tx_buf) { |
| 1028 | ret = -ENOMEM; |
| 1029 | goto alloc_err; |
| 1030 | } |
| 1031 | |
| 1032 | /* Configure DMA channel */ |
| 1033 | memset(&config, 0, sizeof(config)); |
Arnd Bergmann | 8e5481d | 2016-09-23 21:38:51 +0200 | [diff] [blame] | 1034 | config.dst_addr = port->mapbase + ofs->tdr; |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 1035 | config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 1036 | |
| 1037 | ret = dmaengine_slave_config(stm32port->tx_ch, &config); |
| 1038 | if (ret < 0) { |
| 1039 | dev_err(dev, "tx dma channel config failed\n"); |
| 1040 | ret = -ENODEV; |
| 1041 | goto config_err; |
| 1042 | } |
| 1043 | |
| 1044 | return 0; |
| 1045 | |
| 1046 | config_err: |
| 1047 | dma_free_coherent(&pdev->dev, |
| 1048 | TX_BUF_L, stm32port->tx_buf, |
| 1049 | stm32port->tx_dma_buf); |
| 1050 | |
| 1051 | alloc_err: |
| 1052 | dma_release_channel(stm32port->tx_ch); |
| 1053 | stm32port->tx_ch = NULL; |
| 1054 | |
| 1055 | return ret; |
| 1056 | } |
| 1057 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1058 | static int stm32_serial_probe(struct platform_device *pdev) |
| 1059 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1060 | const struct of_device_id *match; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1061 | struct stm32_port *stm32port; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1062 | int ret; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1063 | |
| 1064 | stm32port = stm32_of_get_stm32_port(pdev); |
| 1065 | if (!stm32port) |
| 1066 | return -ENODEV; |
| 1067 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1068 | match = of_match_device(stm32_match, &pdev->dev); |
| 1069 | if (match && match->data) |
| 1070 | stm32port->info = (struct stm32_usart_info *)match->data; |
| 1071 | else |
| 1072 | return -EINVAL; |
| 1073 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1074 | ret = stm32_init_port(stm32port, pdev); |
| 1075 | if (ret) |
| 1076 | return ret; |
| 1077 | |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 1078 | if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) { |
| 1079 | ret = device_init_wakeup(&pdev->dev, true); |
| 1080 | if (ret) |
| 1081 | goto err_uninit; |
| 1082 | } |
| 1083 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1084 | ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); |
| 1085 | if (ret) |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 1086 | goto err_nowup; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1087 | |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 1088 | ret = stm32_of_dma_rx_probe(stm32port, pdev); |
| 1089 | if (ret) |
| 1090 | dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); |
| 1091 | |
| 1092 | ret = stm32_of_dma_tx_probe(stm32port, pdev); |
| 1093 | if (ret) |
| 1094 | dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n"); |
| 1095 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1096 | platform_set_drvdata(pdev, &stm32port->port); |
| 1097 | |
| 1098 | return 0; |
Fabrice Gasnier | ada8004 | 2017-07-13 15:08:29 +0000 | [diff] [blame] | 1099 | |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 1100 | err_nowup: |
| 1101 | if (stm32port->info->cfg.has_wakeup && stm32port->wakeirq >= 0) |
| 1102 | device_init_wakeup(&pdev->dev, false); |
| 1103 | |
Fabrice Gasnier | ada8004 | 2017-07-13 15:08:29 +0000 | [diff] [blame] | 1104 | err_uninit: |
| 1105 | clk_disable_unprepare(stm32port->clk); |
| 1106 | |
| 1107 | return ret; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | static int stm32_serial_remove(struct platform_device *pdev) |
| 1111 | { |
| 1112 | struct uart_port *port = platform_get_drvdata(pdev); |
Alexandre TORGUE | 511c7b1 | 2016-09-15 18:42:38 +0200 | [diff] [blame] | 1113 | struct stm32_port *stm32_port = to_stm32_port(port); |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 1114 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 1115 | struct stm32_usart_config *cfg = &stm32_port->info->cfg; |
Alexandre TORGUE | 3489187 | 2016-09-15 18:42:40 +0200 | [diff] [blame] | 1116 | |
| 1117 | stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR); |
| 1118 | |
| 1119 | if (stm32_port->rx_ch) |
| 1120 | dma_release_channel(stm32_port->rx_ch); |
| 1121 | |
| 1122 | if (stm32_port->rx_dma_buf) |
| 1123 | dma_free_coherent(&pdev->dev, |
| 1124 | RX_BUF_L, stm32_port->rx_buf, |
| 1125 | stm32_port->rx_dma_buf); |
| 1126 | |
| 1127 | stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT); |
| 1128 | |
| 1129 | if (stm32_port->tx_ch) |
| 1130 | dma_release_channel(stm32_port->tx_ch); |
| 1131 | |
| 1132 | if (stm32_port->tx_dma_buf) |
| 1133 | dma_free_coherent(&pdev->dev, |
| 1134 | TX_BUF_L, stm32_port->tx_buf, |
| 1135 | stm32_port->tx_dma_buf); |
Alexandre TORGUE | 511c7b1 | 2016-09-15 18:42:38 +0200 | [diff] [blame] | 1136 | |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 1137 | if (cfg->has_wakeup && stm32_port->wakeirq >= 0) |
| 1138 | device_init_wakeup(&pdev->dev, false); |
| 1139 | |
Alexandre TORGUE | 511c7b1 | 2016-09-15 18:42:38 +0200 | [diff] [blame] | 1140 | clk_disable_unprepare(stm32_port->clk); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1141 | |
| 1142 | return uart_remove_one_port(&stm32_usart_driver, port); |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | #ifdef CONFIG_SERIAL_STM32_CONSOLE |
| 1147 | static void stm32_console_putchar(struct uart_port *port, int ch) |
| 1148 | { |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1149 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 1150 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 1151 | |
| 1152 | while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE)) |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1153 | cpu_relax(); |
| 1154 | |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1155 | writel_relaxed(ch, port->membase + ofs->tdr); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | static void stm32_console_write(struct console *co, const char *s, unsigned cnt) |
| 1159 | { |
| 1160 | struct uart_port *port = &stm32_ports[co->index].port; |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1161 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 1162 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
Alexandre TORGUE | 87f1f80 | 2016-09-15 18:42:42 +0200 | [diff] [blame] | 1163 | struct stm32_usart_config *cfg = &stm32_port->info->cfg; |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1164 | unsigned long flags; |
| 1165 | u32 old_cr1, new_cr1; |
| 1166 | int locked = 1; |
| 1167 | |
| 1168 | local_irq_save(flags); |
| 1169 | if (port->sysrq) |
| 1170 | locked = 0; |
| 1171 | else if (oops_in_progress) |
| 1172 | locked = spin_trylock(&port->lock); |
| 1173 | else |
| 1174 | spin_lock(&port->lock); |
| 1175 | |
Alexandre TORGUE | 87f1f80 | 2016-09-15 18:42:42 +0200 | [diff] [blame] | 1176 | /* Save and disable interrupts, enable the transmitter */ |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1177 | old_cr1 = readl_relaxed(port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1178 | new_cr1 = old_cr1 & ~USART_CR1_IE_MASK; |
Alexandre TORGUE | 87f1f80 | 2016-09-15 18:42:42 +0200 | [diff] [blame] | 1179 | new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit); |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1180 | writel_relaxed(new_cr1, port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1181 | |
| 1182 | uart_console_write(port, s, cnt, stm32_console_putchar); |
| 1183 | |
| 1184 | /* Restore interrupt state */ |
Alexandre TORGUE | ada8618 | 2016-09-15 18:42:33 +0200 | [diff] [blame] | 1185 | writel_relaxed(old_cr1, port->membase + ofs->cr1); |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1186 | |
| 1187 | if (locked) |
| 1188 | spin_unlock(&port->lock); |
| 1189 | local_irq_restore(flags); |
| 1190 | } |
| 1191 | |
| 1192 | static int stm32_console_setup(struct console *co, char *options) |
| 1193 | { |
| 1194 | struct stm32_port *stm32port; |
| 1195 | int baud = 9600; |
| 1196 | int bits = 8; |
| 1197 | int parity = 'n'; |
| 1198 | int flow = 'n'; |
| 1199 | |
| 1200 | if (co->index >= STM32_MAX_PORTS) |
| 1201 | return -ENODEV; |
| 1202 | |
| 1203 | stm32port = &stm32_ports[co->index]; |
| 1204 | |
| 1205 | /* |
| 1206 | * This driver does not support early console initialization |
| 1207 | * (use ARM early printk support instead), so we only expect |
| 1208 | * this to be called during the uart port registration when the |
| 1209 | * driver gets probed and the port should be mapped at that point. |
| 1210 | */ |
| 1211 | if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL) |
| 1212 | return -ENXIO; |
| 1213 | |
| 1214 | if (options) |
| 1215 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 1216 | |
| 1217 | return uart_set_options(&stm32port->port, co, baud, parity, bits, flow); |
| 1218 | } |
| 1219 | |
| 1220 | static struct console stm32_console = { |
| 1221 | .name = STM32_SERIAL_NAME, |
| 1222 | .device = uart_console_device, |
| 1223 | .write = stm32_console_write, |
| 1224 | .setup = stm32_console_setup, |
| 1225 | .flags = CON_PRINTBUFFER, |
| 1226 | .index = -1, |
| 1227 | .data = &stm32_usart_driver, |
| 1228 | }; |
| 1229 | |
| 1230 | #define STM32_SERIAL_CONSOLE (&stm32_console) |
| 1231 | |
| 1232 | #else |
| 1233 | #define STM32_SERIAL_CONSOLE NULL |
| 1234 | #endif /* CONFIG_SERIAL_STM32_CONSOLE */ |
| 1235 | |
| 1236 | static struct uart_driver stm32_usart_driver = { |
| 1237 | .driver_name = DRIVER_NAME, |
| 1238 | .dev_name = STM32_SERIAL_NAME, |
| 1239 | .major = 0, |
| 1240 | .minor = 0, |
| 1241 | .nr = STM32_MAX_PORTS, |
| 1242 | .cons = STM32_SERIAL_CONSOLE, |
| 1243 | }; |
| 1244 | |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 1245 | #ifdef CONFIG_PM_SLEEP |
| 1246 | static void stm32_serial_enable_wakeup(struct uart_port *port, bool enable) |
| 1247 | { |
| 1248 | struct stm32_port *stm32_port = to_stm32_port(port); |
| 1249 | struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; |
| 1250 | struct stm32_usart_config *cfg = &stm32_port->info->cfg; |
| 1251 | u32 val; |
| 1252 | |
| 1253 | if (!cfg->has_wakeup || stm32_port->wakeirq < 0) |
| 1254 | return; |
| 1255 | |
| 1256 | if (enable) { |
| 1257 | stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); |
| 1258 | stm32_set_bits(port, ofs->cr1, USART_CR1_UESM); |
| 1259 | val = readl_relaxed(port->membase + ofs->cr3); |
| 1260 | val &= ~USART_CR3_WUS_MASK; |
| 1261 | /* Enable Wake up interrupt from low power on start bit */ |
| 1262 | val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE; |
| 1263 | writel_relaxed(val, port->membase + ofs->cr3); |
| 1264 | stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit)); |
| 1265 | } else { |
| 1266 | stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | static int stm32_serial_suspend(struct device *dev) |
| 1271 | { |
| 1272 | struct uart_port *port = dev_get_drvdata(dev); |
| 1273 | |
| 1274 | uart_suspend_port(&stm32_usart_driver, port); |
| 1275 | |
| 1276 | if (device_may_wakeup(dev)) |
| 1277 | stm32_serial_enable_wakeup(port, true); |
| 1278 | else |
| 1279 | stm32_serial_enable_wakeup(port, false); |
| 1280 | |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | static int stm32_serial_resume(struct device *dev) |
| 1285 | { |
| 1286 | struct uart_port *port = dev_get_drvdata(dev); |
| 1287 | |
| 1288 | if (device_may_wakeup(dev)) |
| 1289 | stm32_serial_enable_wakeup(port, false); |
| 1290 | |
| 1291 | return uart_resume_port(&stm32_usart_driver, port); |
| 1292 | } |
| 1293 | #endif /* CONFIG_PM_SLEEP */ |
| 1294 | |
| 1295 | static const struct dev_pm_ops stm32_serial_pm_ops = { |
| 1296 | SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume) |
| 1297 | }; |
| 1298 | |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1299 | static struct platform_driver stm32_serial_driver = { |
| 1300 | .probe = stm32_serial_probe, |
| 1301 | .remove = stm32_serial_remove, |
| 1302 | .driver = { |
| 1303 | .name = DRIVER_NAME, |
Fabrice Gasnier | 270e5a7 | 2017-07-13 15:08:30 +0000 | [diff] [blame] | 1304 | .pm = &stm32_serial_pm_ops, |
Maxime Coquelin | 48a6092 | 2015-06-10 21:19:36 +0200 | [diff] [blame] | 1305 | .of_match_table = of_match_ptr(stm32_match), |
| 1306 | }, |
| 1307 | }; |
| 1308 | |
| 1309 | static int __init usart_init(void) |
| 1310 | { |
| 1311 | static char banner[] __initdata = "STM32 USART driver initialized"; |
| 1312 | int ret; |
| 1313 | |
| 1314 | pr_info("%s\n", banner); |
| 1315 | |
| 1316 | ret = uart_register_driver(&stm32_usart_driver); |
| 1317 | if (ret) |
| 1318 | return ret; |
| 1319 | |
| 1320 | ret = platform_driver_register(&stm32_serial_driver); |
| 1321 | if (ret) |
| 1322 | uart_unregister_driver(&stm32_usart_driver); |
| 1323 | |
| 1324 | return ret; |
| 1325 | } |
| 1326 | |
| 1327 | static void __exit usart_exit(void) |
| 1328 | { |
| 1329 | platform_driver_unregister(&stm32_serial_driver); |
| 1330 | uart_unregister_driver(&stm32_usart_driver); |
| 1331 | } |
| 1332 | |
| 1333 | module_init(usart_init); |
| 1334 | module_exit(usart_exit); |
| 1335 | |
| 1336 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 1337 | MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver"); |
| 1338 | MODULE_LICENSE("GPL v2"); |