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