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