1 /* 2 * Driver for Atmel AT91 Serial ports 3 * Copyright (C) 2003 Rick Bronson 4 * 5 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd. 6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. 7 * 8 * DMA support added by Chip Coldwell. 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23 * 24 */ 25 #include <linux/tty.h> 26 #include <linux/ioport.h> 27 #include <linux/slab.h> 28 #include <linux/init.h> 29 #include <linux/serial.h> 30 #include <linux/clk.h> 31 #include <linux/console.h> 32 #include <linux/sysrq.h> 33 #include <linux/tty_flip.h> 34 #include <linux/platform_device.h> 35 #include <linux/of.h> 36 #include <linux/of_device.h> 37 #include <linux/of_gpio.h> 38 #include <linux/dma-mapping.h> 39 #include <linux/dmaengine.h> 40 #include <linux/atmel_pdc.h> 41 #include <linux/uaccess.h> 42 #include <linux/platform_data/atmel.h> 43 #include <linux/timer.h> 44 #include <linux/gpio.h> 45 #include <linux/gpio/consumer.h> 46 #include <linux/err.h> 47 #include <linux/irq.h> 48 #include <linux/suspend.h> 49 #include <linux/mm.h> 50 51 #include <asm/io.h> 52 #include <asm/ioctls.h> 53 54 #define PDC_BUFFER_SIZE 512 55 /* Revisit: We should calculate this based on the actual port settings */ 56 #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */ 57 58 /* The minium number of data FIFOs should be able to contain */ 59 #define ATMEL_MIN_FIFO_SIZE 8 60 /* 61 * These two offsets are substracted from the RX FIFO size to define the RTS 62 * high and low thresholds 63 */ 64 #define ATMEL_RTS_HIGH_OFFSET 16 65 #define ATMEL_RTS_LOW_OFFSET 20 66 67 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 68 #define SUPPORT_SYSRQ 69 #endif 70 71 #include <linux/serial_core.h> 72 73 #include "serial_mctrl_gpio.h" 74 #include "atmel_serial.h" 75 76 static void atmel_start_rx(struct uart_port *port); 77 static void atmel_stop_rx(struct uart_port *port); 78 79 #ifdef CONFIG_SERIAL_ATMEL_TTYAT 80 81 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we 82 * should coexist with the 8250 driver, such as if we have an external 16C550 83 * UART. */ 84 #define SERIAL_ATMEL_MAJOR 204 85 #define MINOR_START 154 86 #define ATMEL_DEVICENAME "ttyAT" 87 88 #else 89 90 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port 91 * name, but it is legally reserved for the 8250 driver. */ 92 #define SERIAL_ATMEL_MAJOR TTY_MAJOR 93 #define MINOR_START 64 94 #define ATMEL_DEVICENAME "ttyS" 95 96 #endif 97 98 #define ATMEL_ISR_PASS_LIMIT 256 99 100 struct atmel_dma_buffer { 101 unsigned char *buf; 102 dma_addr_t dma_addr; 103 unsigned int dma_size; 104 unsigned int ofs; 105 }; 106 107 struct atmel_uart_char { 108 u16 status; 109 u16 ch; 110 }; 111 112 /* 113 * Be careful, the real size of the ring buffer is 114 * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer 115 * can contain up to 1024 characters in PIO mode and up to 4096 characters in 116 * DMA mode. 117 */ 118 #define ATMEL_SERIAL_RINGSIZE 1024 119 120 /* 121 * at91: 6 USARTs and one DBGU port (SAM9260) 122 * samx7: 3 USARTs and 5 UARTs 123 */ 124 #define ATMEL_MAX_UART 8 125 126 /* 127 * We wrap our port structure around the generic uart_port. 128 */ 129 struct atmel_uart_port { 130 struct uart_port uart; /* uart */ 131 struct clk *clk; /* uart clock */ 132 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */ 133 u32 backup_imr; /* IMR saved during suspend */ 134 int break_active; /* break being received */ 135 136 bool use_dma_rx; /* enable DMA receiver */ 137 bool use_pdc_rx; /* enable PDC receiver */ 138 short pdc_rx_idx; /* current PDC RX buffer */ 139 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ 140 141 bool use_dma_tx; /* enable DMA transmitter */ 142 bool use_pdc_tx; /* enable PDC transmitter */ 143 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ 144 145 spinlock_t lock_tx; /* port lock */ 146 spinlock_t lock_rx; /* port lock */ 147 struct dma_chan *chan_tx; 148 struct dma_chan *chan_rx; 149 struct dma_async_tx_descriptor *desc_tx; 150 struct dma_async_tx_descriptor *desc_rx; 151 dma_cookie_t cookie_tx; 152 dma_cookie_t cookie_rx; 153 struct scatterlist sg_tx; 154 struct scatterlist sg_rx; 155 struct tasklet_struct tasklet_rx; 156 struct tasklet_struct tasklet_tx; 157 atomic_t tasklet_shutdown; 158 unsigned int irq_status_prev; 159 unsigned int tx_len; 160 161 struct circ_buf rx_ring; 162 163 struct mctrl_gpios *gpios; 164 unsigned int tx_done_mask; 165 u32 fifo_size; 166 u32 rts_high; 167 u32 rts_low; 168 bool ms_irq_enabled; 169 u32 rtor; /* address of receiver timeout register if it exists */ 170 bool has_frac_baudrate; 171 bool has_hw_timer; 172 struct timer_list uart_timer; 173 174 bool suspended; 175 unsigned int pending; 176 unsigned int pending_status; 177 spinlock_t lock_suspended; 178 179 #ifdef CONFIG_PM 180 struct { 181 u32 cr; 182 u32 mr; 183 u32 imr; 184 u32 brgr; 185 u32 rtor; 186 u32 ttgr; 187 u32 fmr; 188 u32 fimr; 189 } cache; 190 #endif 191 192 int (*prepare_rx)(struct uart_port *port); 193 int (*prepare_tx)(struct uart_port *port); 194 void (*schedule_rx)(struct uart_port *port); 195 void (*schedule_tx)(struct uart_port *port); 196 void (*release_rx)(struct uart_port *port); 197 void (*release_tx)(struct uart_port *port); 198 }; 199 200 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; 201 static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART); 202 203 #ifdef SUPPORT_SYSRQ 204 static struct console atmel_console; 205 #endif 206 207 #if defined(CONFIG_OF) 208 static const struct of_device_id atmel_serial_dt_ids[] = { 209 { .compatible = "atmel,at91rm9200-usart" }, 210 { .compatible = "atmel,at91sam9260-usart" }, 211 { /* sentinel */ } 212 }; 213 #endif 214 215 static inline struct atmel_uart_port * 216 to_atmel_uart_port(struct uart_port *uart) 217 { 218 return container_of(uart, struct atmel_uart_port, uart); 219 } 220 221 static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg) 222 { 223 return __raw_readl(port->membase + reg); 224 } 225 226 static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value) 227 { 228 __raw_writel(value, port->membase + reg); 229 } 230 231 static inline u8 atmel_uart_read_char(struct uart_port *port) 232 { 233 return __raw_readb(port->membase + ATMEL_US_RHR); 234 } 235 236 static inline void atmel_uart_write_char(struct uart_port *port, u8 value) 237 { 238 __raw_writeb(value, port->membase + ATMEL_US_THR); 239 } 240 241 #ifdef CONFIG_SERIAL_ATMEL_PDC 242 static bool atmel_use_pdc_rx(struct uart_port *port) 243 { 244 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 245 246 return atmel_port->use_pdc_rx; 247 } 248 249 static bool atmel_use_pdc_tx(struct uart_port *port) 250 { 251 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 252 253 return atmel_port->use_pdc_tx; 254 } 255 #else 256 static bool atmel_use_pdc_rx(struct uart_port *port) 257 { 258 return false; 259 } 260 261 static bool atmel_use_pdc_tx(struct uart_port *port) 262 { 263 return false; 264 } 265 #endif 266 267 static bool atmel_use_dma_tx(struct uart_port *port) 268 { 269 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 270 271 return atmel_port->use_dma_tx; 272 } 273 274 static bool atmel_use_dma_rx(struct uart_port *port) 275 { 276 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 277 278 return atmel_port->use_dma_rx; 279 } 280 281 static bool atmel_use_fifo(struct uart_port *port) 282 { 283 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 284 285 return atmel_port->fifo_size; 286 } 287 288 static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port, 289 struct tasklet_struct *t) 290 { 291 if (!atomic_read(&atmel_port->tasklet_shutdown)) 292 tasklet_schedule(t); 293 } 294 295 static unsigned int atmel_get_lines_status(struct uart_port *port) 296 { 297 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 298 unsigned int status, ret = 0; 299 300 status = atmel_uart_readl(port, ATMEL_US_CSR); 301 302 mctrl_gpio_get(atmel_port->gpios, &ret); 303 304 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 305 UART_GPIO_CTS))) { 306 if (ret & TIOCM_CTS) 307 status &= ~ATMEL_US_CTS; 308 else 309 status |= ATMEL_US_CTS; 310 } 311 312 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 313 UART_GPIO_DSR))) { 314 if (ret & TIOCM_DSR) 315 status &= ~ATMEL_US_DSR; 316 else 317 status |= ATMEL_US_DSR; 318 } 319 320 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 321 UART_GPIO_RI))) { 322 if (ret & TIOCM_RI) 323 status &= ~ATMEL_US_RI; 324 else 325 status |= ATMEL_US_RI; 326 } 327 328 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios, 329 UART_GPIO_DCD))) { 330 if (ret & TIOCM_CD) 331 status &= ~ATMEL_US_DCD; 332 else 333 status |= ATMEL_US_DCD; 334 } 335 336 return status; 337 } 338 339 /* Enable or disable the rs485 support */ 340 static int atmel_config_rs485(struct uart_port *port, 341 struct serial_rs485 *rs485conf) 342 { 343 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 344 unsigned int mode; 345 346 /* Disable interrupts */ 347 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 348 349 mode = atmel_uart_readl(port, ATMEL_US_MR); 350 351 /* Resetting serial mode to RS232 (0x0) */ 352 mode &= ~ATMEL_US_USMODE; 353 354 port->rs485 = *rs485conf; 355 356 if (rs485conf->flags & SER_RS485_ENABLED) { 357 dev_dbg(port->dev, "Setting UART to RS485\n"); 358 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 359 atmel_uart_writel(port, ATMEL_US_TTGR, 360 rs485conf->delay_rts_after_send); 361 mode |= ATMEL_US_USMODE_RS485; 362 } else { 363 dev_dbg(port->dev, "Setting UART to RS232\n"); 364 if (atmel_use_pdc_tx(port)) 365 atmel_port->tx_done_mask = ATMEL_US_ENDTX | 366 ATMEL_US_TXBUFE; 367 else 368 atmel_port->tx_done_mask = ATMEL_US_TXRDY; 369 } 370 atmel_uart_writel(port, ATMEL_US_MR, mode); 371 372 /* Enable interrupts */ 373 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 374 375 return 0; 376 } 377 378 /* 379 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty. 380 */ 381 static u_int atmel_tx_empty(struct uart_port *port) 382 { 383 return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ? 384 TIOCSER_TEMT : 385 0; 386 } 387 388 /* 389 * Set state of the modem control output lines 390 */ 391 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl) 392 { 393 unsigned int control = 0; 394 unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR); 395 unsigned int rts_paused, rts_ready; 396 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 397 398 /* override mode to RS485 if needed, otherwise keep the current mode */ 399 if (port->rs485.flags & SER_RS485_ENABLED) { 400 atmel_uart_writel(port, ATMEL_US_TTGR, 401 port->rs485.delay_rts_after_send); 402 mode &= ~ATMEL_US_USMODE; 403 mode |= ATMEL_US_USMODE_RS485; 404 } 405 406 /* set the RTS line state according to the mode */ 407 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 408 /* force RTS line to high level */ 409 rts_paused = ATMEL_US_RTSEN; 410 411 /* give the control of the RTS line back to the hardware */ 412 rts_ready = ATMEL_US_RTSDIS; 413 } else { 414 /* force RTS line to high level */ 415 rts_paused = ATMEL_US_RTSDIS; 416 417 /* force RTS line to low level */ 418 rts_ready = ATMEL_US_RTSEN; 419 } 420 421 if (mctrl & TIOCM_RTS) 422 control |= rts_ready; 423 else 424 control |= rts_paused; 425 426 if (mctrl & TIOCM_DTR) 427 control |= ATMEL_US_DTREN; 428 else 429 control |= ATMEL_US_DTRDIS; 430 431 atmel_uart_writel(port, ATMEL_US_CR, control); 432 433 mctrl_gpio_set(atmel_port->gpios, mctrl); 434 435 /* Local loopback mode? */ 436 mode &= ~ATMEL_US_CHMODE; 437 if (mctrl & TIOCM_LOOP) 438 mode |= ATMEL_US_CHMODE_LOC_LOOP; 439 else 440 mode |= ATMEL_US_CHMODE_NORMAL; 441 442 atmel_uart_writel(port, ATMEL_US_MR, mode); 443 } 444 445 /* 446 * Get state of the modem control input lines 447 */ 448 static u_int atmel_get_mctrl(struct uart_port *port) 449 { 450 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 451 unsigned int ret = 0, status; 452 453 status = atmel_uart_readl(port, ATMEL_US_CSR); 454 455 /* 456 * The control signals are active low. 457 */ 458 if (!(status & ATMEL_US_DCD)) 459 ret |= TIOCM_CD; 460 if (!(status & ATMEL_US_CTS)) 461 ret |= TIOCM_CTS; 462 if (!(status & ATMEL_US_DSR)) 463 ret |= TIOCM_DSR; 464 if (!(status & ATMEL_US_RI)) 465 ret |= TIOCM_RI; 466 467 return mctrl_gpio_get(atmel_port->gpios, &ret); 468 } 469 470 /* 471 * Stop transmitting. 472 */ 473 static void atmel_stop_tx(struct uart_port *port) 474 { 475 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 476 477 if (atmel_use_pdc_tx(port)) { 478 /* disable PDC transmit */ 479 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 480 } 481 482 /* 483 * Disable the transmitter. 484 * This is mandatory when DMA is used, otherwise the DMA buffer 485 * is fully transmitted. 486 */ 487 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS); 488 489 /* Disable interrupts */ 490 atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask); 491 492 if ((port->rs485.flags & SER_RS485_ENABLED) && 493 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 494 atmel_start_rx(port); 495 } 496 497 /* 498 * Start transmitting. 499 */ 500 static void atmel_start_tx(struct uart_port *port) 501 { 502 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 503 504 if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR) 505 & ATMEL_PDC_TXTEN)) 506 /* The transmitter is already running. Yes, we 507 really need this.*/ 508 return; 509 510 if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port)) 511 if ((port->rs485.flags & SER_RS485_ENABLED) && 512 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) 513 atmel_stop_rx(port); 514 515 if (atmel_use_pdc_tx(port)) 516 /* re-enable PDC transmit */ 517 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 518 519 /* Enable interrupts */ 520 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask); 521 522 /* re-enable the transmitter */ 523 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 524 } 525 526 /* 527 * start receiving - port is in process of being opened. 528 */ 529 static void atmel_start_rx(struct uart_port *port) 530 { 531 /* reset status and receiver */ 532 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 533 534 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN); 535 536 if (atmel_use_pdc_rx(port)) { 537 /* enable PDC controller */ 538 atmel_uart_writel(port, ATMEL_US_IER, 539 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 540 port->read_status_mask); 541 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 542 } else { 543 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 544 } 545 } 546 547 /* 548 * Stop receiving - port is in process of being closed. 549 */ 550 static void atmel_stop_rx(struct uart_port *port) 551 { 552 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS); 553 554 if (atmel_use_pdc_rx(port)) { 555 /* disable PDC receive */ 556 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS); 557 atmel_uart_writel(port, ATMEL_US_IDR, 558 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | 559 port->read_status_mask); 560 } else { 561 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY); 562 } 563 } 564 565 /* 566 * Enable modem status interrupts 567 */ 568 static void atmel_enable_ms(struct uart_port *port) 569 { 570 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 571 uint32_t ier = 0; 572 573 /* 574 * Interrupt should not be enabled twice 575 */ 576 if (atmel_port->ms_irq_enabled) 577 return; 578 579 atmel_port->ms_irq_enabled = true; 580 581 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 582 ier |= ATMEL_US_CTSIC; 583 584 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 585 ier |= ATMEL_US_DSRIC; 586 587 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 588 ier |= ATMEL_US_RIIC; 589 590 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 591 ier |= ATMEL_US_DCDIC; 592 593 atmel_uart_writel(port, ATMEL_US_IER, ier); 594 595 mctrl_gpio_enable_ms(atmel_port->gpios); 596 } 597 598 /* 599 * Disable modem status interrupts 600 */ 601 static void atmel_disable_ms(struct uart_port *port) 602 { 603 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 604 uint32_t idr = 0; 605 606 /* 607 * Interrupt should not be disabled twice 608 */ 609 if (!atmel_port->ms_irq_enabled) 610 return; 611 612 atmel_port->ms_irq_enabled = false; 613 614 mctrl_gpio_disable_ms(atmel_port->gpios); 615 616 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) 617 idr |= ATMEL_US_CTSIC; 618 619 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR)) 620 idr |= ATMEL_US_DSRIC; 621 622 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI)) 623 idr |= ATMEL_US_RIIC; 624 625 if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD)) 626 idr |= ATMEL_US_DCDIC; 627 628 atmel_uart_writel(port, ATMEL_US_IDR, idr); 629 } 630 631 /* 632 * Control the transmission of a break signal 633 */ 634 static void atmel_break_ctl(struct uart_port *port, int break_state) 635 { 636 if (break_state != 0) 637 /* start break */ 638 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK); 639 else 640 /* stop break */ 641 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK); 642 } 643 644 /* 645 * Stores the incoming character in the ring buffer 646 */ 647 static void 648 atmel_buffer_rx_char(struct uart_port *port, unsigned int status, 649 unsigned int ch) 650 { 651 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 652 struct circ_buf *ring = &atmel_port->rx_ring; 653 struct atmel_uart_char *c; 654 655 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE)) 656 /* Buffer overflow, ignore char */ 657 return; 658 659 c = &((struct atmel_uart_char *)ring->buf)[ring->head]; 660 c->status = status; 661 c->ch = ch; 662 663 /* Make sure the character is stored before we update head. */ 664 smp_wmb(); 665 666 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 667 } 668 669 /* 670 * Deal with parity, framing and overrun errors. 671 */ 672 static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status) 673 { 674 /* clear error */ 675 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 676 677 if (status & ATMEL_US_RXBRK) { 678 /* ignore side-effect */ 679 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 680 port->icount.brk++; 681 } 682 if (status & ATMEL_US_PARE) 683 port->icount.parity++; 684 if (status & ATMEL_US_FRAME) 685 port->icount.frame++; 686 if (status & ATMEL_US_OVRE) 687 port->icount.overrun++; 688 } 689 690 /* 691 * Characters received (called from interrupt handler) 692 */ 693 static void atmel_rx_chars(struct uart_port *port) 694 { 695 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 696 unsigned int status, ch; 697 698 status = atmel_uart_readl(port, ATMEL_US_CSR); 699 while (status & ATMEL_US_RXRDY) { 700 ch = atmel_uart_read_char(port); 701 702 /* 703 * note that the error handling code is 704 * out of the main execution path 705 */ 706 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 707 | ATMEL_US_OVRE | ATMEL_US_RXBRK) 708 || atmel_port->break_active)) { 709 710 /* clear error */ 711 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 712 713 if (status & ATMEL_US_RXBRK 714 && !atmel_port->break_active) { 715 atmel_port->break_active = 1; 716 atmel_uart_writel(port, ATMEL_US_IER, 717 ATMEL_US_RXBRK); 718 } else { 719 /* 720 * This is either the end-of-break 721 * condition or we've received at 722 * least one character without RXBRK 723 * being set. In both cases, the next 724 * RXBRK will indicate start-of-break. 725 */ 726 atmel_uart_writel(port, ATMEL_US_IDR, 727 ATMEL_US_RXBRK); 728 status &= ~ATMEL_US_RXBRK; 729 atmel_port->break_active = 0; 730 } 731 } 732 733 atmel_buffer_rx_char(port, status, ch); 734 status = atmel_uart_readl(port, ATMEL_US_CSR); 735 } 736 737 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 738 } 739 740 /* 741 * Transmit characters (called from tasklet with TXRDY interrupt 742 * disabled) 743 */ 744 static void atmel_tx_chars(struct uart_port *port) 745 { 746 struct circ_buf *xmit = &port->state->xmit; 747 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 748 749 if (port->x_char && 750 (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) { 751 atmel_uart_write_char(port, port->x_char); 752 port->icount.tx++; 753 port->x_char = 0; 754 } 755 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 756 return; 757 758 while (atmel_uart_readl(port, ATMEL_US_CSR) & 759 atmel_port->tx_done_mask) { 760 atmel_uart_write_char(port, xmit->buf[xmit->tail]); 761 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 762 port->icount.tx++; 763 if (uart_circ_empty(xmit)) 764 break; 765 } 766 767 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 768 uart_write_wakeup(port); 769 770 if (!uart_circ_empty(xmit)) 771 /* Enable interrupts */ 772 atmel_uart_writel(port, ATMEL_US_IER, 773 atmel_port->tx_done_mask); 774 } 775 776 static void atmel_complete_tx_dma(void *arg) 777 { 778 struct atmel_uart_port *atmel_port = arg; 779 struct uart_port *port = &atmel_port->uart; 780 struct circ_buf *xmit = &port->state->xmit; 781 struct dma_chan *chan = atmel_port->chan_tx; 782 unsigned long flags; 783 784 spin_lock_irqsave(&port->lock, flags); 785 786 if (chan) 787 dmaengine_terminate_all(chan); 788 xmit->tail += atmel_port->tx_len; 789 xmit->tail &= UART_XMIT_SIZE - 1; 790 791 port->icount.tx += atmel_port->tx_len; 792 793 spin_lock_irq(&atmel_port->lock_tx); 794 async_tx_ack(atmel_port->desc_tx); 795 atmel_port->cookie_tx = -EINVAL; 796 atmel_port->desc_tx = NULL; 797 spin_unlock_irq(&atmel_port->lock_tx); 798 799 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 800 uart_write_wakeup(port); 801 802 /* 803 * xmit is a circular buffer so, if we have just send data from 804 * xmit->tail to the end of xmit->buf, now we have to transmit the 805 * remaining data from the beginning of xmit->buf to xmit->head. 806 */ 807 if (!uart_circ_empty(xmit)) 808 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 809 else if ((port->rs485.flags & SER_RS485_ENABLED) && 810 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) { 811 /* DMA done, stop TX, start RX for RS485 */ 812 atmel_start_rx(port); 813 } 814 815 spin_unlock_irqrestore(&port->lock, flags); 816 } 817 818 static void atmel_release_tx_dma(struct uart_port *port) 819 { 820 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 821 struct dma_chan *chan = atmel_port->chan_tx; 822 823 if (chan) { 824 dmaengine_terminate_all(chan); 825 dma_release_channel(chan); 826 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, 827 DMA_TO_DEVICE); 828 } 829 830 atmel_port->desc_tx = NULL; 831 atmel_port->chan_tx = NULL; 832 atmel_port->cookie_tx = -EINVAL; 833 } 834 835 /* 836 * Called from tasklet with TXRDY interrupt is disabled. 837 */ 838 static void atmel_tx_dma(struct uart_port *port) 839 { 840 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 841 struct circ_buf *xmit = &port->state->xmit; 842 struct dma_chan *chan = atmel_port->chan_tx; 843 struct dma_async_tx_descriptor *desc; 844 struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx; 845 unsigned int tx_len, part1_len, part2_len, sg_len; 846 dma_addr_t phys_addr; 847 848 /* Make sure we have an idle channel */ 849 if (atmel_port->desc_tx != NULL) 850 return; 851 852 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 853 /* 854 * DMA is idle now. 855 * Port xmit buffer is already mapped, 856 * and it is one page... Just adjust 857 * offsets and lengths. Since it is a circular buffer, 858 * we have to transmit till the end, and then the rest. 859 * Take the port lock to get a 860 * consistent xmit buffer state. 861 */ 862 tx_len = CIRC_CNT_TO_END(xmit->head, 863 xmit->tail, 864 UART_XMIT_SIZE); 865 866 if (atmel_port->fifo_size) { 867 /* multi data mode */ 868 part1_len = (tx_len & ~0x3); /* DWORD access */ 869 part2_len = (tx_len & 0x3); /* BYTE access */ 870 } else { 871 /* single data (legacy) mode */ 872 part1_len = 0; 873 part2_len = tx_len; /* BYTE access only */ 874 } 875 876 sg_init_table(sgl, 2); 877 sg_len = 0; 878 phys_addr = sg_dma_address(sg_tx) + xmit->tail; 879 if (part1_len) { 880 sg = &sgl[sg_len++]; 881 sg_dma_address(sg) = phys_addr; 882 sg_dma_len(sg) = part1_len; 883 884 phys_addr += part1_len; 885 } 886 887 if (part2_len) { 888 sg = &sgl[sg_len++]; 889 sg_dma_address(sg) = phys_addr; 890 sg_dma_len(sg) = part2_len; 891 } 892 893 /* 894 * save tx_len so atmel_complete_tx_dma() will increase 895 * xmit->tail correctly 896 */ 897 atmel_port->tx_len = tx_len; 898 899 desc = dmaengine_prep_slave_sg(chan, 900 sgl, 901 sg_len, 902 DMA_MEM_TO_DEV, 903 DMA_PREP_INTERRUPT | 904 DMA_CTRL_ACK); 905 if (!desc) { 906 dev_err(port->dev, "Failed to send via dma!\n"); 907 return; 908 } 909 910 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE); 911 912 atmel_port->desc_tx = desc; 913 desc->callback = atmel_complete_tx_dma; 914 desc->callback_param = atmel_port; 915 atmel_port->cookie_tx = dmaengine_submit(desc); 916 } 917 918 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 919 uart_write_wakeup(port); 920 } 921 922 static int atmel_prepare_tx_dma(struct uart_port *port) 923 { 924 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 925 dma_cap_mask_t mask; 926 struct dma_slave_config config; 927 int ret, nent; 928 929 dma_cap_zero(mask); 930 dma_cap_set(DMA_SLAVE, mask); 931 932 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx"); 933 if (atmel_port->chan_tx == NULL) 934 goto chan_err; 935 dev_info(port->dev, "using %s for tx DMA transfers\n", 936 dma_chan_name(atmel_port->chan_tx)); 937 938 spin_lock_init(&atmel_port->lock_tx); 939 sg_init_table(&atmel_port->sg_tx, 1); 940 /* UART circular tx buffer is an aligned page. */ 941 BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf)); 942 sg_set_page(&atmel_port->sg_tx, 943 virt_to_page(port->state->xmit.buf), 944 UART_XMIT_SIZE, 945 offset_in_page(port->state->xmit.buf)); 946 nent = dma_map_sg(port->dev, 947 &atmel_port->sg_tx, 948 1, 949 DMA_TO_DEVICE); 950 951 if (!nent) { 952 dev_dbg(port->dev, "need to release resource of dma\n"); 953 goto chan_err; 954 } else { 955 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 956 sg_dma_len(&atmel_port->sg_tx), 957 port->state->xmit.buf, 958 &sg_dma_address(&atmel_port->sg_tx)); 959 } 960 961 /* Configure the slave DMA */ 962 memset(&config, 0, sizeof(config)); 963 config.direction = DMA_MEM_TO_DEV; 964 config.dst_addr_width = (atmel_port->fifo_size) ? 965 DMA_SLAVE_BUSWIDTH_4_BYTES : 966 DMA_SLAVE_BUSWIDTH_1_BYTE; 967 config.dst_addr = port->mapbase + ATMEL_US_THR; 968 config.dst_maxburst = 1; 969 970 ret = dmaengine_slave_config(atmel_port->chan_tx, 971 &config); 972 if (ret) { 973 dev_err(port->dev, "DMA tx slave configuration failed\n"); 974 goto chan_err; 975 } 976 977 return 0; 978 979 chan_err: 980 dev_err(port->dev, "TX channel not available, switch to pio\n"); 981 atmel_port->use_dma_tx = 0; 982 if (atmel_port->chan_tx) 983 atmel_release_tx_dma(port); 984 return -EINVAL; 985 } 986 987 static void atmel_complete_rx_dma(void *arg) 988 { 989 struct uart_port *port = arg; 990 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 991 992 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 993 } 994 995 static void atmel_release_rx_dma(struct uart_port *port) 996 { 997 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 998 struct dma_chan *chan = atmel_port->chan_rx; 999 1000 if (chan) { 1001 dmaengine_terminate_all(chan); 1002 dma_release_channel(chan); 1003 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, 1004 DMA_FROM_DEVICE); 1005 } 1006 1007 atmel_port->desc_rx = NULL; 1008 atmel_port->chan_rx = NULL; 1009 atmel_port->cookie_rx = -EINVAL; 1010 } 1011 1012 static void atmel_rx_from_dma(struct uart_port *port) 1013 { 1014 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1015 struct tty_port *tport = &port->state->port; 1016 struct circ_buf *ring = &atmel_port->rx_ring; 1017 struct dma_chan *chan = atmel_port->chan_rx; 1018 struct dma_tx_state state; 1019 enum dma_status dmastat; 1020 size_t count; 1021 1022 1023 /* Reset the UART timeout early so that we don't miss one */ 1024 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1025 dmastat = dmaengine_tx_status(chan, 1026 atmel_port->cookie_rx, 1027 &state); 1028 /* Restart a new tasklet if DMA status is error */ 1029 if (dmastat == DMA_ERROR) { 1030 dev_dbg(port->dev, "Get residue error, restart tasklet\n"); 1031 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 1032 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx); 1033 return; 1034 } 1035 1036 /* CPU claims ownership of RX DMA buffer */ 1037 dma_sync_sg_for_cpu(port->dev, 1038 &atmel_port->sg_rx, 1039 1, 1040 DMA_FROM_DEVICE); 1041 1042 /* 1043 * ring->head points to the end of data already written by the DMA. 1044 * ring->tail points to the beginning of data to be read by the 1045 * framework. 1046 * The current transfer size should not be larger than the dma buffer 1047 * length. 1048 */ 1049 ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue; 1050 BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx)); 1051 /* 1052 * At this point ring->head may point to the first byte right after the 1053 * last byte of the dma buffer: 1054 * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx) 1055 * 1056 * However ring->tail must always points inside the dma buffer: 1057 * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1 1058 * 1059 * Since we use a ring buffer, we have to handle the case 1060 * where head is lower than tail. In such a case, we first read from 1061 * tail to the end of the buffer then reset tail. 1062 */ 1063 if (ring->head < ring->tail) { 1064 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail; 1065 1066 tty_insert_flip_string(tport, ring->buf + ring->tail, count); 1067 ring->tail = 0; 1068 port->icount.rx += count; 1069 } 1070 1071 /* Finally we read data from tail to head */ 1072 if (ring->tail < ring->head) { 1073 count = ring->head - ring->tail; 1074 1075 tty_insert_flip_string(tport, ring->buf + ring->tail, count); 1076 /* Wrap ring->head if needed */ 1077 if (ring->head >= sg_dma_len(&atmel_port->sg_rx)) 1078 ring->head = 0; 1079 ring->tail = ring->head; 1080 port->icount.rx += count; 1081 } 1082 1083 /* USART retreives ownership of RX DMA buffer */ 1084 dma_sync_sg_for_device(port->dev, 1085 &atmel_port->sg_rx, 1086 1, 1087 DMA_FROM_DEVICE); 1088 1089 /* 1090 * Drop the lock here since it might end up calling 1091 * uart_start(), which takes the lock. 1092 */ 1093 spin_unlock(&port->lock); 1094 tty_flip_buffer_push(tport); 1095 spin_lock(&port->lock); 1096 1097 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT); 1098 } 1099 1100 static int atmel_prepare_rx_dma(struct uart_port *port) 1101 { 1102 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1103 struct dma_async_tx_descriptor *desc; 1104 dma_cap_mask_t mask; 1105 struct dma_slave_config config; 1106 struct circ_buf *ring; 1107 int ret, nent; 1108 1109 ring = &atmel_port->rx_ring; 1110 1111 dma_cap_zero(mask); 1112 dma_cap_set(DMA_CYCLIC, mask); 1113 1114 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx"); 1115 if (atmel_port->chan_rx == NULL) 1116 goto chan_err; 1117 dev_info(port->dev, "using %s for rx DMA transfers\n", 1118 dma_chan_name(atmel_port->chan_rx)); 1119 1120 spin_lock_init(&atmel_port->lock_rx); 1121 sg_init_table(&atmel_port->sg_rx, 1); 1122 /* UART circular rx buffer is an aligned page. */ 1123 BUG_ON(!PAGE_ALIGNED(ring->buf)); 1124 sg_set_page(&atmel_port->sg_rx, 1125 virt_to_page(ring->buf), 1126 sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE, 1127 offset_in_page(ring->buf)); 1128 nent = dma_map_sg(port->dev, 1129 &atmel_port->sg_rx, 1130 1, 1131 DMA_FROM_DEVICE); 1132 1133 if (!nent) { 1134 dev_dbg(port->dev, "need to release resource of dma\n"); 1135 goto chan_err; 1136 } else { 1137 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__, 1138 sg_dma_len(&atmel_port->sg_rx), 1139 ring->buf, 1140 &sg_dma_address(&atmel_port->sg_rx)); 1141 } 1142 1143 /* Configure the slave DMA */ 1144 memset(&config, 0, sizeof(config)); 1145 config.direction = DMA_DEV_TO_MEM; 1146 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 1147 config.src_addr = port->mapbase + ATMEL_US_RHR; 1148 config.src_maxburst = 1; 1149 1150 ret = dmaengine_slave_config(atmel_port->chan_rx, 1151 &config); 1152 if (ret) { 1153 dev_err(port->dev, "DMA rx slave configuration failed\n"); 1154 goto chan_err; 1155 } 1156 /* 1157 * Prepare a cyclic dma transfer, assign 2 descriptors, 1158 * each one is half ring buffer size 1159 */ 1160 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, 1161 sg_dma_address(&atmel_port->sg_rx), 1162 sg_dma_len(&atmel_port->sg_rx), 1163 sg_dma_len(&atmel_port->sg_rx)/2, 1164 DMA_DEV_TO_MEM, 1165 DMA_PREP_INTERRUPT); 1166 desc->callback = atmel_complete_rx_dma; 1167 desc->callback_param = port; 1168 atmel_port->desc_rx = desc; 1169 atmel_port->cookie_rx = dmaengine_submit(desc); 1170 1171 return 0; 1172 1173 chan_err: 1174 dev_err(port->dev, "RX channel not available, switch to pio\n"); 1175 atmel_port->use_dma_rx = 0; 1176 if (atmel_port->chan_rx) 1177 atmel_release_rx_dma(port); 1178 return -EINVAL; 1179 } 1180 1181 static void atmel_uart_timer_callback(unsigned long data) 1182 { 1183 struct uart_port *port = (void *)data; 1184 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1185 1186 if (!atomic_read(&atmel_port->tasklet_shutdown)) { 1187 tasklet_schedule(&atmel_port->tasklet_rx); 1188 mod_timer(&atmel_port->uart_timer, 1189 jiffies + uart_poll_timeout(port)); 1190 } 1191 } 1192 1193 /* 1194 * receive interrupt handler. 1195 */ 1196 static void 1197 atmel_handle_receive(struct uart_port *port, unsigned int pending) 1198 { 1199 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1200 1201 if (atmel_use_pdc_rx(port)) { 1202 /* 1203 * PDC receive. Just schedule the tasklet and let it 1204 * figure out the details. 1205 * 1206 * TODO: We're not handling error flags correctly at 1207 * the moment. 1208 */ 1209 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) { 1210 atmel_uart_writel(port, ATMEL_US_IDR, 1211 (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)); 1212 atmel_tasklet_schedule(atmel_port, 1213 &atmel_port->tasklet_rx); 1214 } 1215 1216 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE | 1217 ATMEL_US_FRAME | ATMEL_US_PARE)) 1218 atmel_pdc_rxerr(port, pending); 1219 } 1220 1221 if (atmel_use_dma_rx(port)) { 1222 if (pending & ATMEL_US_TIMEOUT) { 1223 atmel_uart_writel(port, ATMEL_US_IDR, 1224 ATMEL_US_TIMEOUT); 1225 atmel_tasklet_schedule(atmel_port, 1226 &atmel_port->tasklet_rx); 1227 } 1228 } 1229 1230 /* Interrupt receive */ 1231 if (pending & ATMEL_US_RXRDY) 1232 atmel_rx_chars(port); 1233 else if (pending & ATMEL_US_RXBRK) { 1234 /* 1235 * End of break detected. If it came along with a 1236 * character, atmel_rx_chars will handle it. 1237 */ 1238 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 1239 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK); 1240 atmel_port->break_active = 0; 1241 } 1242 } 1243 1244 /* 1245 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe) 1246 */ 1247 static void 1248 atmel_handle_transmit(struct uart_port *port, unsigned int pending) 1249 { 1250 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1251 1252 if (pending & atmel_port->tx_done_mask) { 1253 /* Either PDC or interrupt transmission */ 1254 atmel_uart_writel(port, ATMEL_US_IDR, 1255 atmel_port->tx_done_mask); 1256 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx); 1257 } 1258 } 1259 1260 /* 1261 * status flags interrupt handler. 1262 */ 1263 static void 1264 atmel_handle_status(struct uart_port *port, unsigned int pending, 1265 unsigned int status) 1266 { 1267 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1268 unsigned int status_change; 1269 1270 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC 1271 | ATMEL_US_CTSIC)) { 1272 status_change = status ^ atmel_port->irq_status_prev; 1273 atmel_port->irq_status_prev = status; 1274 1275 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR 1276 | ATMEL_US_DCD | ATMEL_US_CTS)) { 1277 /* TODO: All reads to CSR will clear these interrupts! */ 1278 if (status_change & ATMEL_US_RI) 1279 port->icount.rng++; 1280 if (status_change & ATMEL_US_DSR) 1281 port->icount.dsr++; 1282 if (status_change & ATMEL_US_DCD) 1283 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD)); 1284 if (status_change & ATMEL_US_CTS) 1285 uart_handle_cts_change(port, !(status & ATMEL_US_CTS)); 1286 1287 wake_up_interruptible(&port->state->port.delta_msr_wait); 1288 } 1289 } 1290 } 1291 1292 /* 1293 * Interrupt handler 1294 */ 1295 static irqreturn_t atmel_interrupt(int irq, void *dev_id) 1296 { 1297 struct uart_port *port = dev_id; 1298 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1299 unsigned int status, pending, mask, pass_counter = 0; 1300 1301 spin_lock(&atmel_port->lock_suspended); 1302 1303 do { 1304 status = atmel_get_lines_status(port); 1305 mask = atmel_uart_readl(port, ATMEL_US_IMR); 1306 pending = status & mask; 1307 if (!pending) 1308 break; 1309 1310 if (atmel_port->suspended) { 1311 atmel_port->pending |= pending; 1312 atmel_port->pending_status = status; 1313 atmel_uart_writel(port, ATMEL_US_IDR, mask); 1314 pm_system_wakeup(); 1315 break; 1316 } 1317 1318 atmel_handle_receive(port, pending); 1319 atmel_handle_status(port, pending, status); 1320 atmel_handle_transmit(port, pending); 1321 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT); 1322 1323 spin_unlock(&atmel_port->lock_suspended); 1324 1325 return pass_counter ? IRQ_HANDLED : IRQ_NONE; 1326 } 1327 1328 static void atmel_release_tx_pdc(struct uart_port *port) 1329 { 1330 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1331 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1332 1333 dma_unmap_single(port->dev, 1334 pdc->dma_addr, 1335 pdc->dma_size, 1336 DMA_TO_DEVICE); 1337 } 1338 1339 /* 1340 * Called from tasklet with ENDTX and TXBUFE interrupts disabled. 1341 */ 1342 static void atmel_tx_pdc(struct uart_port *port) 1343 { 1344 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1345 struct circ_buf *xmit = &port->state->xmit; 1346 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1347 int count; 1348 1349 /* nothing left to transmit? */ 1350 if (atmel_uart_readl(port, ATMEL_PDC_TCR)) 1351 return; 1352 1353 xmit->tail += pdc->ofs; 1354 xmit->tail &= UART_XMIT_SIZE - 1; 1355 1356 port->icount.tx += pdc->ofs; 1357 pdc->ofs = 0; 1358 1359 /* more to transmit - setup next transfer */ 1360 1361 /* disable PDC transmit */ 1362 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 1363 1364 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { 1365 dma_sync_single_for_device(port->dev, 1366 pdc->dma_addr, 1367 pdc->dma_size, 1368 DMA_TO_DEVICE); 1369 1370 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 1371 pdc->ofs = count; 1372 1373 atmel_uart_writel(port, ATMEL_PDC_TPR, 1374 pdc->dma_addr + xmit->tail); 1375 atmel_uart_writel(port, ATMEL_PDC_TCR, count); 1376 /* re-enable PDC transmit */ 1377 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 1378 /* Enable interrupts */ 1379 atmel_uart_writel(port, ATMEL_US_IER, 1380 atmel_port->tx_done_mask); 1381 } else { 1382 if ((port->rs485.flags & SER_RS485_ENABLED) && 1383 !(port->rs485.flags & SER_RS485_RX_DURING_TX)) { 1384 /* DMA done, stop TX, start RX for RS485 */ 1385 atmel_start_rx(port); 1386 } 1387 } 1388 1389 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 1390 uart_write_wakeup(port); 1391 } 1392 1393 static int atmel_prepare_tx_pdc(struct uart_port *port) 1394 { 1395 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1396 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; 1397 struct circ_buf *xmit = &port->state->xmit; 1398 1399 pdc->buf = xmit->buf; 1400 pdc->dma_addr = dma_map_single(port->dev, 1401 pdc->buf, 1402 UART_XMIT_SIZE, 1403 DMA_TO_DEVICE); 1404 pdc->dma_size = UART_XMIT_SIZE; 1405 pdc->ofs = 0; 1406 1407 return 0; 1408 } 1409 1410 static void atmel_rx_from_ring(struct uart_port *port) 1411 { 1412 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1413 struct circ_buf *ring = &atmel_port->rx_ring; 1414 unsigned int flg; 1415 unsigned int status; 1416 1417 while (ring->head != ring->tail) { 1418 struct atmel_uart_char c; 1419 1420 /* Make sure c is loaded after head. */ 1421 smp_rmb(); 1422 1423 c = ((struct atmel_uart_char *)ring->buf)[ring->tail]; 1424 1425 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1); 1426 1427 port->icount.rx++; 1428 status = c.status; 1429 flg = TTY_NORMAL; 1430 1431 /* 1432 * note that the error handling code is 1433 * out of the main execution path 1434 */ 1435 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME 1436 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) { 1437 if (status & ATMEL_US_RXBRK) { 1438 /* ignore side-effect */ 1439 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); 1440 1441 port->icount.brk++; 1442 if (uart_handle_break(port)) 1443 continue; 1444 } 1445 if (status & ATMEL_US_PARE) 1446 port->icount.parity++; 1447 if (status & ATMEL_US_FRAME) 1448 port->icount.frame++; 1449 if (status & ATMEL_US_OVRE) 1450 port->icount.overrun++; 1451 1452 status &= port->read_status_mask; 1453 1454 if (status & ATMEL_US_RXBRK) 1455 flg = TTY_BREAK; 1456 else if (status & ATMEL_US_PARE) 1457 flg = TTY_PARITY; 1458 else if (status & ATMEL_US_FRAME) 1459 flg = TTY_FRAME; 1460 } 1461 1462 1463 if (uart_handle_sysrq_char(port, c.ch)) 1464 continue; 1465 1466 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg); 1467 } 1468 1469 /* 1470 * Drop the lock here since it might end up calling 1471 * uart_start(), which takes the lock. 1472 */ 1473 spin_unlock(&port->lock); 1474 tty_flip_buffer_push(&port->state->port); 1475 spin_lock(&port->lock); 1476 } 1477 1478 static void atmel_release_rx_pdc(struct uart_port *port) 1479 { 1480 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1481 int i; 1482 1483 for (i = 0; i < 2; i++) { 1484 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1485 1486 dma_unmap_single(port->dev, 1487 pdc->dma_addr, 1488 pdc->dma_size, 1489 DMA_FROM_DEVICE); 1490 kfree(pdc->buf); 1491 } 1492 } 1493 1494 static void atmel_rx_from_pdc(struct uart_port *port) 1495 { 1496 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1497 struct tty_port *tport = &port->state->port; 1498 struct atmel_dma_buffer *pdc; 1499 int rx_idx = atmel_port->pdc_rx_idx; 1500 unsigned int head; 1501 unsigned int tail; 1502 unsigned int count; 1503 1504 do { 1505 /* Reset the UART timeout early so that we don't miss one */ 1506 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1507 1508 pdc = &atmel_port->pdc_rx[rx_idx]; 1509 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr; 1510 tail = pdc->ofs; 1511 1512 /* If the PDC has switched buffers, RPR won't contain 1513 * any address within the current buffer. Since head 1514 * is unsigned, we just need a one-way comparison to 1515 * find out. 1516 * 1517 * In this case, we just need to consume the entire 1518 * buffer and resubmit it for DMA. This will clear the 1519 * ENDRX bit as well, so that we can safely re-enable 1520 * all interrupts below. 1521 */ 1522 head = min(head, pdc->dma_size); 1523 1524 if (likely(head != tail)) { 1525 dma_sync_single_for_cpu(port->dev, pdc->dma_addr, 1526 pdc->dma_size, DMA_FROM_DEVICE); 1527 1528 /* 1529 * head will only wrap around when we recycle 1530 * the DMA buffer, and when that happens, we 1531 * explicitly set tail to 0. So head will 1532 * always be greater than tail. 1533 */ 1534 count = head - tail; 1535 1536 tty_insert_flip_string(tport, pdc->buf + pdc->ofs, 1537 count); 1538 1539 dma_sync_single_for_device(port->dev, pdc->dma_addr, 1540 pdc->dma_size, DMA_FROM_DEVICE); 1541 1542 port->icount.rx += count; 1543 pdc->ofs = head; 1544 } 1545 1546 /* 1547 * If the current buffer is full, we need to check if 1548 * the next one contains any additional data. 1549 */ 1550 if (head >= pdc->dma_size) { 1551 pdc->ofs = 0; 1552 atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr); 1553 atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size); 1554 1555 rx_idx = !rx_idx; 1556 atmel_port->pdc_rx_idx = rx_idx; 1557 } 1558 } while (head >= pdc->dma_size); 1559 1560 /* 1561 * Drop the lock here since it might end up calling 1562 * uart_start(), which takes the lock. 1563 */ 1564 spin_unlock(&port->lock); 1565 tty_flip_buffer_push(tport); 1566 spin_lock(&port->lock); 1567 1568 atmel_uart_writel(port, ATMEL_US_IER, 1569 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1570 } 1571 1572 static int atmel_prepare_rx_pdc(struct uart_port *port) 1573 { 1574 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1575 int i; 1576 1577 for (i = 0; i < 2; i++) { 1578 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; 1579 1580 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); 1581 if (pdc->buf == NULL) { 1582 if (i != 0) { 1583 dma_unmap_single(port->dev, 1584 atmel_port->pdc_rx[0].dma_addr, 1585 PDC_BUFFER_SIZE, 1586 DMA_FROM_DEVICE); 1587 kfree(atmel_port->pdc_rx[0].buf); 1588 } 1589 atmel_port->use_pdc_rx = 0; 1590 return -ENOMEM; 1591 } 1592 pdc->dma_addr = dma_map_single(port->dev, 1593 pdc->buf, 1594 PDC_BUFFER_SIZE, 1595 DMA_FROM_DEVICE); 1596 pdc->dma_size = PDC_BUFFER_SIZE; 1597 pdc->ofs = 0; 1598 } 1599 1600 atmel_port->pdc_rx_idx = 0; 1601 1602 atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr); 1603 atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE); 1604 1605 atmel_uart_writel(port, ATMEL_PDC_RNPR, 1606 atmel_port->pdc_rx[1].dma_addr); 1607 atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE); 1608 1609 return 0; 1610 } 1611 1612 /* 1613 * tasklet handling tty stuff outside the interrupt handler. 1614 */ 1615 static void atmel_tasklet_rx_func(unsigned long data) 1616 { 1617 struct uart_port *port = (struct uart_port *)data; 1618 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1619 1620 /* The interrupt handler does not take the lock */ 1621 spin_lock(&port->lock); 1622 atmel_port->schedule_rx(port); 1623 spin_unlock(&port->lock); 1624 } 1625 1626 static void atmel_tasklet_tx_func(unsigned long data) 1627 { 1628 struct uart_port *port = (struct uart_port *)data; 1629 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1630 1631 /* The interrupt handler does not take the lock */ 1632 spin_lock(&port->lock); 1633 atmel_port->schedule_tx(port); 1634 spin_unlock(&port->lock); 1635 } 1636 1637 static void atmel_init_property(struct atmel_uart_port *atmel_port, 1638 struct platform_device *pdev) 1639 { 1640 struct device_node *np = pdev->dev.of_node; 1641 1642 /* DMA/PDC usage specification */ 1643 if (of_property_read_bool(np, "atmel,use-dma-rx")) { 1644 if (of_property_read_bool(np, "dmas")) { 1645 atmel_port->use_dma_rx = true; 1646 atmel_port->use_pdc_rx = false; 1647 } else { 1648 atmel_port->use_dma_rx = false; 1649 atmel_port->use_pdc_rx = true; 1650 } 1651 } else { 1652 atmel_port->use_dma_rx = false; 1653 atmel_port->use_pdc_rx = false; 1654 } 1655 1656 if (of_property_read_bool(np, "atmel,use-dma-tx")) { 1657 if (of_property_read_bool(np, "dmas")) { 1658 atmel_port->use_dma_tx = true; 1659 atmel_port->use_pdc_tx = false; 1660 } else { 1661 atmel_port->use_dma_tx = false; 1662 atmel_port->use_pdc_tx = true; 1663 } 1664 } else { 1665 atmel_port->use_dma_tx = false; 1666 atmel_port->use_pdc_tx = false; 1667 } 1668 } 1669 1670 static void atmel_init_rs485(struct uart_port *port, 1671 struct platform_device *pdev) 1672 { 1673 struct device_node *np = pdev->dev.of_node; 1674 1675 struct serial_rs485 *rs485conf = &port->rs485; 1676 u32 rs485_delay[2]; 1677 1678 /* rs485 properties */ 1679 if (of_property_read_u32_array(np, "rs485-rts-delay", 1680 rs485_delay, 2) == 0) { 1681 rs485conf->delay_rts_before_send = rs485_delay[0]; 1682 rs485conf->delay_rts_after_send = rs485_delay[1]; 1683 rs485conf->flags = 0; 1684 } 1685 1686 if (of_get_property(np, "rs485-rx-during-tx", NULL)) 1687 rs485conf->flags |= SER_RS485_RX_DURING_TX; 1688 1689 if (of_get_property(np, "linux,rs485-enabled-at-boot-time", NULL)) 1690 rs485conf->flags |= SER_RS485_ENABLED; 1691 } 1692 1693 static void atmel_set_ops(struct uart_port *port) 1694 { 1695 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1696 1697 if (atmel_use_dma_rx(port)) { 1698 atmel_port->prepare_rx = &atmel_prepare_rx_dma; 1699 atmel_port->schedule_rx = &atmel_rx_from_dma; 1700 atmel_port->release_rx = &atmel_release_rx_dma; 1701 } else if (atmel_use_pdc_rx(port)) { 1702 atmel_port->prepare_rx = &atmel_prepare_rx_pdc; 1703 atmel_port->schedule_rx = &atmel_rx_from_pdc; 1704 atmel_port->release_rx = &atmel_release_rx_pdc; 1705 } else { 1706 atmel_port->prepare_rx = NULL; 1707 atmel_port->schedule_rx = &atmel_rx_from_ring; 1708 atmel_port->release_rx = NULL; 1709 } 1710 1711 if (atmel_use_dma_tx(port)) { 1712 atmel_port->prepare_tx = &atmel_prepare_tx_dma; 1713 atmel_port->schedule_tx = &atmel_tx_dma; 1714 atmel_port->release_tx = &atmel_release_tx_dma; 1715 } else if (atmel_use_pdc_tx(port)) { 1716 atmel_port->prepare_tx = &atmel_prepare_tx_pdc; 1717 atmel_port->schedule_tx = &atmel_tx_pdc; 1718 atmel_port->release_tx = &atmel_release_tx_pdc; 1719 } else { 1720 atmel_port->prepare_tx = NULL; 1721 atmel_port->schedule_tx = &atmel_tx_chars; 1722 atmel_port->release_tx = NULL; 1723 } 1724 } 1725 1726 /* 1727 * Get ip name usart or uart 1728 */ 1729 static void atmel_get_ip_name(struct uart_port *port) 1730 { 1731 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1732 int name = atmel_uart_readl(port, ATMEL_US_NAME); 1733 u32 version; 1734 u32 usart, dbgu_uart, new_uart; 1735 /* ASCII decoding for IP version */ 1736 usart = 0x55534152; /* USAR(T) */ 1737 dbgu_uart = 0x44424755; /* DBGU */ 1738 new_uart = 0x55415254; /* UART */ 1739 1740 /* 1741 * Only USART devices from at91sam9260 SOC implement fractional 1742 * baudrate. It is available for all asynchronous modes, with the 1743 * following restriction: the sampling clock's duty cycle is not 1744 * constant. 1745 */ 1746 atmel_port->has_frac_baudrate = false; 1747 atmel_port->has_hw_timer = false; 1748 1749 if (name == new_uart) { 1750 dev_dbg(port->dev, "Uart with hw timer"); 1751 atmel_port->has_hw_timer = true; 1752 atmel_port->rtor = ATMEL_UA_RTOR; 1753 } else if (name == usart) { 1754 dev_dbg(port->dev, "Usart\n"); 1755 atmel_port->has_frac_baudrate = true; 1756 atmel_port->has_hw_timer = true; 1757 atmel_port->rtor = ATMEL_US_RTOR; 1758 } else if (name == dbgu_uart) { 1759 dev_dbg(port->dev, "Dbgu or uart without hw timer\n"); 1760 } else { 1761 /* fallback for older SoCs: use version field */ 1762 version = atmel_uart_readl(port, ATMEL_US_VERSION); 1763 switch (version) { 1764 case 0x302: 1765 case 0x10213: 1766 dev_dbg(port->dev, "This version is usart\n"); 1767 atmel_port->has_frac_baudrate = true; 1768 atmel_port->has_hw_timer = true; 1769 atmel_port->rtor = ATMEL_US_RTOR; 1770 break; 1771 case 0x203: 1772 case 0x10202: 1773 dev_dbg(port->dev, "This version is uart\n"); 1774 break; 1775 default: 1776 dev_err(port->dev, "Not supported ip name nor version, set to uart\n"); 1777 } 1778 } 1779 } 1780 1781 /* 1782 * Perform initialization and enable port for reception 1783 */ 1784 static int atmel_startup(struct uart_port *port) 1785 { 1786 struct platform_device *pdev = to_platform_device(port->dev); 1787 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1788 struct tty_struct *tty = port->state->port.tty; 1789 int retval; 1790 1791 /* 1792 * Ensure that no interrupts are enabled otherwise when 1793 * request_irq() is called we could get stuck trying to 1794 * handle an unexpected interrupt 1795 */ 1796 atmel_uart_writel(port, ATMEL_US_IDR, -1); 1797 atmel_port->ms_irq_enabled = false; 1798 1799 /* 1800 * Allocate the IRQ 1801 */ 1802 retval = request_irq(port->irq, atmel_interrupt, 1803 IRQF_SHARED | IRQF_COND_SUSPEND, 1804 tty ? tty->name : "atmel_serial", port); 1805 if (retval) { 1806 dev_err(port->dev, "atmel_startup - Can't get irq\n"); 1807 return retval; 1808 } 1809 1810 atomic_set(&atmel_port->tasklet_shutdown, 0); 1811 tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func, 1812 (unsigned long)port); 1813 tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func, 1814 (unsigned long)port); 1815 1816 /* 1817 * Initialize DMA (if necessary) 1818 */ 1819 atmel_init_property(atmel_port, pdev); 1820 atmel_set_ops(port); 1821 1822 if (atmel_port->prepare_rx) { 1823 retval = atmel_port->prepare_rx(port); 1824 if (retval < 0) 1825 atmel_set_ops(port); 1826 } 1827 1828 if (atmel_port->prepare_tx) { 1829 retval = atmel_port->prepare_tx(port); 1830 if (retval < 0) 1831 atmel_set_ops(port); 1832 } 1833 1834 /* 1835 * Enable FIFO when available 1836 */ 1837 if (atmel_port->fifo_size) { 1838 unsigned int txrdym = ATMEL_US_ONE_DATA; 1839 unsigned int rxrdym = ATMEL_US_ONE_DATA; 1840 unsigned int fmr; 1841 1842 atmel_uart_writel(port, ATMEL_US_CR, 1843 ATMEL_US_FIFOEN | 1844 ATMEL_US_RXFCLR | 1845 ATMEL_US_TXFLCLR); 1846 1847 if (atmel_use_dma_tx(port)) 1848 txrdym = ATMEL_US_FOUR_DATA; 1849 1850 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym); 1851 if (atmel_port->rts_high && 1852 atmel_port->rts_low) 1853 fmr |= ATMEL_US_FRTSC | 1854 ATMEL_US_RXFTHRES(atmel_port->rts_high) | 1855 ATMEL_US_RXFTHRES2(atmel_port->rts_low); 1856 1857 atmel_uart_writel(port, ATMEL_US_FMR, fmr); 1858 } 1859 1860 /* Save current CSR for comparison in atmel_tasklet_func() */ 1861 atmel_port->irq_status_prev = atmel_get_lines_status(port); 1862 1863 /* 1864 * Finally, enable the serial port 1865 */ 1866 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 1867 /* enable xmit & rcvr */ 1868 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 1869 1870 setup_timer(&atmel_port->uart_timer, 1871 atmel_uart_timer_callback, 1872 (unsigned long)port); 1873 1874 if (atmel_use_pdc_rx(port)) { 1875 /* set UART timeout */ 1876 if (!atmel_port->has_hw_timer) { 1877 mod_timer(&atmel_port->uart_timer, 1878 jiffies + uart_poll_timeout(port)); 1879 /* set USART timeout */ 1880 } else { 1881 atmel_uart_writel(port, atmel_port->rtor, 1882 PDC_RX_TIMEOUT); 1883 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1884 1885 atmel_uart_writel(port, ATMEL_US_IER, 1886 ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); 1887 } 1888 /* enable PDC controller */ 1889 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN); 1890 } else if (atmel_use_dma_rx(port)) { 1891 /* set UART timeout */ 1892 if (!atmel_port->has_hw_timer) { 1893 mod_timer(&atmel_port->uart_timer, 1894 jiffies + uart_poll_timeout(port)); 1895 /* set USART timeout */ 1896 } else { 1897 atmel_uart_writel(port, atmel_port->rtor, 1898 PDC_RX_TIMEOUT); 1899 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO); 1900 1901 atmel_uart_writel(port, ATMEL_US_IER, 1902 ATMEL_US_TIMEOUT); 1903 } 1904 } else { 1905 /* enable receive only */ 1906 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY); 1907 } 1908 1909 return 0; 1910 } 1911 1912 /* 1913 * Flush any TX data submitted for DMA. Called when the TX circular 1914 * buffer is reset. 1915 */ 1916 static void atmel_flush_buffer(struct uart_port *port) 1917 { 1918 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1919 1920 if (atmel_use_pdc_tx(port)) { 1921 atmel_uart_writel(port, ATMEL_PDC_TCR, 0); 1922 atmel_port->pdc_tx.ofs = 0; 1923 } 1924 /* 1925 * in uart_flush_buffer(), the xmit circular buffer has just 1926 * been cleared, so we have to reset tx_len accordingly. 1927 */ 1928 atmel_port->tx_len = 0; 1929 } 1930 1931 /* 1932 * Disable the port 1933 */ 1934 static void atmel_shutdown(struct uart_port *port) 1935 { 1936 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 1937 1938 /* Disable modem control lines interrupts */ 1939 atmel_disable_ms(port); 1940 1941 /* Disable interrupts at device level */ 1942 atmel_uart_writel(port, ATMEL_US_IDR, -1); 1943 1944 /* Prevent spurious interrupts from scheduling the tasklet */ 1945 atomic_inc(&atmel_port->tasklet_shutdown); 1946 1947 /* 1948 * Prevent any tasklets being scheduled during 1949 * cleanup 1950 */ 1951 del_timer_sync(&atmel_port->uart_timer); 1952 1953 /* Make sure that no interrupt is on the fly */ 1954 synchronize_irq(port->irq); 1955 1956 /* 1957 * Clear out any scheduled tasklets before 1958 * we destroy the buffers 1959 */ 1960 tasklet_kill(&atmel_port->tasklet_rx); 1961 tasklet_kill(&atmel_port->tasklet_tx); 1962 1963 /* 1964 * Ensure everything is stopped and 1965 * disable port and break condition. 1966 */ 1967 atmel_stop_rx(port); 1968 atmel_stop_tx(port); 1969 1970 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA); 1971 1972 /* 1973 * Shut-down the DMA. 1974 */ 1975 if (atmel_port->release_rx) 1976 atmel_port->release_rx(port); 1977 if (atmel_port->release_tx) 1978 atmel_port->release_tx(port); 1979 1980 /* 1981 * Reset ring buffer pointers 1982 */ 1983 atmel_port->rx_ring.head = 0; 1984 atmel_port->rx_ring.tail = 0; 1985 1986 /* 1987 * Free the interrupts 1988 */ 1989 free_irq(port->irq, port); 1990 1991 atmel_flush_buffer(port); 1992 } 1993 1994 /* 1995 * Power / Clock management. 1996 */ 1997 static void atmel_serial_pm(struct uart_port *port, unsigned int state, 1998 unsigned int oldstate) 1999 { 2000 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2001 2002 switch (state) { 2003 case 0: 2004 /* 2005 * Enable the peripheral clock for this serial port. 2006 * This is called on uart_open() or a resume event. 2007 */ 2008 clk_prepare_enable(atmel_port->clk); 2009 2010 /* re-enable interrupts if we disabled some on suspend */ 2011 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr); 2012 break; 2013 case 3: 2014 /* Back up the interrupt mask and disable all interrupts */ 2015 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR); 2016 atmel_uart_writel(port, ATMEL_US_IDR, -1); 2017 2018 /* 2019 * Disable the peripheral clock for this serial port. 2020 * This is called on uart_close() or a suspend event. 2021 */ 2022 clk_disable_unprepare(atmel_port->clk); 2023 break; 2024 default: 2025 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state); 2026 } 2027 } 2028 2029 /* 2030 * Change the port parameters 2031 */ 2032 static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, 2033 struct ktermios *old) 2034 { 2035 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2036 unsigned long flags; 2037 unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0; 2038 2039 /* save the current mode register */ 2040 mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR); 2041 2042 /* reset the mode, clock divisor, parity, stop bits and data size */ 2043 mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | 2044 ATMEL_US_PAR | ATMEL_US_USMODE); 2045 2046 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16); 2047 2048 /* byte size */ 2049 switch (termios->c_cflag & CSIZE) { 2050 case CS5: 2051 mode |= ATMEL_US_CHRL_5; 2052 break; 2053 case CS6: 2054 mode |= ATMEL_US_CHRL_6; 2055 break; 2056 case CS7: 2057 mode |= ATMEL_US_CHRL_7; 2058 break; 2059 default: 2060 mode |= ATMEL_US_CHRL_8; 2061 break; 2062 } 2063 2064 /* stop bits */ 2065 if (termios->c_cflag & CSTOPB) 2066 mode |= ATMEL_US_NBSTOP_2; 2067 2068 /* parity */ 2069 if (termios->c_cflag & PARENB) { 2070 /* Mark or Space parity */ 2071 if (termios->c_cflag & CMSPAR) { 2072 if (termios->c_cflag & PARODD) 2073 mode |= ATMEL_US_PAR_MARK; 2074 else 2075 mode |= ATMEL_US_PAR_SPACE; 2076 } else if (termios->c_cflag & PARODD) 2077 mode |= ATMEL_US_PAR_ODD; 2078 else 2079 mode |= ATMEL_US_PAR_EVEN; 2080 } else 2081 mode |= ATMEL_US_PAR_NONE; 2082 2083 spin_lock_irqsave(&port->lock, flags); 2084 2085 port->read_status_mask = ATMEL_US_OVRE; 2086 if (termios->c_iflag & INPCK) 2087 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2088 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 2089 port->read_status_mask |= ATMEL_US_RXBRK; 2090 2091 if (atmel_use_pdc_rx(port)) 2092 /* need to enable error interrupts */ 2093 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask); 2094 2095 /* 2096 * Characters to ignore 2097 */ 2098 port->ignore_status_mask = 0; 2099 if (termios->c_iflag & IGNPAR) 2100 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE); 2101 if (termios->c_iflag & IGNBRK) { 2102 port->ignore_status_mask |= ATMEL_US_RXBRK; 2103 /* 2104 * If we're ignoring parity and break indicators, 2105 * ignore overruns too (for real raw support). 2106 */ 2107 if (termios->c_iflag & IGNPAR) 2108 port->ignore_status_mask |= ATMEL_US_OVRE; 2109 } 2110 /* TODO: Ignore all characters if CREAD is set.*/ 2111 2112 /* update the per-port timeout */ 2113 uart_update_timeout(port, termios->c_cflag, baud); 2114 2115 /* 2116 * save/disable interrupts. The tty layer will ensure that the 2117 * transmitter is empty if requested by the caller, so there's 2118 * no need to wait for it here. 2119 */ 2120 imr = atmel_uart_readl(port, ATMEL_US_IMR); 2121 atmel_uart_writel(port, ATMEL_US_IDR, -1); 2122 2123 /* disable receiver and transmitter */ 2124 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS); 2125 2126 /* mode */ 2127 if (port->rs485.flags & SER_RS485_ENABLED) { 2128 atmel_uart_writel(port, ATMEL_US_TTGR, 2129 port->rs485.delay_rts_after_send); 2130 mode |= ATMEL_US_USMODE_RS485; 2131 } else if (termios->c_cflag & CRTSCTS) { 2132 /* RS232 with hardware handshake (RTS/CTS) */ 2133 if (atmel_use_fifo(port) && 2134 !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) { 2135 /* 2136 * with ATMEL_US_USMODE_HWHS set, the controller will 2137 * be able to drive the RTS pin high/low when the RX 2138 * FIFO is above RXFTHRES/below RXFTHRES2. 2139 * It will also disable the transmitter when the CTS 2140 * pin is high. 2141 * This mode is not activated if CTS pin is a GPIO 2142 * because in this case, the transmitter is always 2143 * disabled (there must be an internal pull-up 2144 * responsible for this behaviour). 2145 * If the RTS pin is a GPIO, the controller won't be 2146 * able to drive it according to the FIFO thresholds, 2147 * but it will be handled by the driver. 2148 */ 2149 mode |= ATMEL_US_USMODE_HWHS; 2150 } else { 2151 /* 2152 * For platforms without FIFO, the flow control is 2153 * handled by the driver. 2154 */ 2155 mode |= ATMEL_US_USMODE_NORMAL; 2156 } 2157 } else { 2158 /* RS232 without hadware handshake */ 2159 mode |= ATMEL_US_USMODE_NORMAL; 2160 } 2161 2162 /* set the mode, clock divisor, parity, stop bits and data size */ 2163 atmel_uart_writel(port, ATMEL_US_MR, mode); 2164 2165 /* 2166 * when switching the mode, set the RTS line state according to the 2167 * new mode, otherwise keep the former state 2168 */ 2169 if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) { 2170 unsigned int rts_state; 2171 2172 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) { 2173 /* let the hardware control the RTS line */ 2174 rts_state = ATMEL_US_RTSDIS; 2175 } else { 2176 /* force RTS line to low level */ 2177 rts_state = ATMEL_US_RTSEN; 2178 } 2179 2180 atmel_uart_writel(port, ATMEL_US_CR, rts_state); 2181 } 2182 2183 /* 2184 * Set the baud rate: 2185 * Fractional baudrate allows to setup output frequency more 2186 * accurately. This feature is enabled only when using normal mode. 2187 * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8)) 2188 * Currently, OVER is always set to 0 so we get 2189 * baudrate = selected clock / (16 * (CD + FP / 8)) 2190 * then 2191 * 8 CD + FP = selected clock / (2 * baudrate) 2192 */ 2193 if (atmel_port->has_frac_baudrate) { 2194 div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2); 2195 cd = div >> 3; 2196 fp = div & ATMEL_US_FP_MASK; 2197 } else { 2198 cd = uart_get_divisor(port, baud); 2199 } 2200 2201 if (cd > 65535) { /* BRGR is 16-bit, so switch to slower clock */ 2202 cd /= 8; 2203 mode |= ATMEL_US_USCLKS_MCK_DIV8; 2204 } 2205 quot = cd | fp << ATMEL_US_FP_OFFSET; 2206 2207 atmel_uart_writel(port, ATMEL_US_BRGR, quot); 2208 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 2209 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2210 2211 /* restore interrupts */ 2212 atmel_uart_writel(port, ATMEL_US_IER, imr); 2213 2214 /* CTS flow-control and modem-status interrupts */ 2215 if (UART_ENABLE_MS(port, termios->c_cflag)) 2216 atmel_enable_ms(port); 2217 else 2218 atmel_disable_ms(port); 2219 2220 spin_unlock_irqrestore(&port->lock, flags); 2221 } 2222 2223 static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios) 2224 { 2225 if (termios->c_line == N_PPS) { 2226 port->flags |= UPF_HARDPPS_CD; 2227 spin_lock_irq(&port->lock); 2228 atmel_enable_ms(port); 2229 spin_unlock_irq(&port->lock); 2230 } else { 2231 port->flags &= ~UPF_HARDPPS_CD; 2232 if (!UART_ENABLE_MS(port, termios->c_cflag)) { 2233 spin_lock_irq(&port->lock); 2234 atmel_disable_ms(port); 2235 spin_unlock_irq(&port->lock); 2236 } 2237 } 2238 } 2239 2240 /* 2241 * Return string describing the specified port 2242 */ 2243 static const char *atmel_type(struct uart_port *port) 2244 { 2245 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL; 2246 } 2247 2248 /* 2249 * Release the memory region(s) being used by 'port'. 2250 */ 2251 static void atmel_release_port(struct uart_port *port) 2252 { 2253 struct platform_device *pdev = to_platform_device(port->dev); 2254 int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2255 2256 release_mem_region(port->mapbase, size); 2257 2258 if (port->flags & UPF_IOREMAP) { 2259 iounmap(port->membase); 2260 port->membase = NULL; 2261 } 2262 } 2263 2264 /* 2265 * Request the memory region(s) being used by 'port'. 2266 */ 2267 static int atmel_request_port(struct uart_port *port) 2268 { 2269 struct platform_device *pdev = to_platform_device(port->dev); 2270 int size = pdev->resource[0].end - pdev->resource[0].start + 1; 2271 2272 if (!request_mem_region(port->mapbase, size, "atmel_serial")) 2273 return -EBUSY; 2274 2275 if (port->flags & UPF_IOREMAP) { 2276 port->membase = ioremap(port->mapbase, size); 2277 if (port->membase == NULL) { 2278 release_mem_region(port->mapbase, size); 2279 return -ENOMEM; 2280 } 2281 } 2282 2283 return 0; 2284 } 2285 2286 /* 2287 * Configure/autoconfigure the port. 2288 */ 2289 static void atmel_config_port(struct uart_port *port, int flags) 2290 { 2291 if (flags & UART_CONFIG_TYPE) { 2292 port->type = PORT_ATMEL; 2293 atmel_request_port(port); 2294 } 2295 } 2296 2297 /* 2298 * Verify the new serial_struct (for TIOCSSERIAL). 2299 */ 2300 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser) 2301 { 2302 int ret = 0; 2303 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL) 2304 ret = -EINVAL; 2305 if (port->irq != ser->irq) 2306 ret = -EINVAL; 2307 if (ser->io_type != SERIAL_IO_MEM) 2308 ret = -EINVAL; 2309 if (port->uartclk / 16 != ser->baud_base) 2310 ret = -EINVAL; 2311 if (port->mapbase != (unsigned long)ser->iomem_base) 2312 ret = -EINVAL; 2313 if (port->iobase != ser->port) 2314 ret = -EINVAL; 2315 if (ser->hub6 != 0) 2316 ret = -EINVAL; 2317 return ret; 2318 } 2319 2320 #ifdef CONFIG_CONSOLE_POLL 2321 static int atmel_poll_get_char(struct uart_port *port) 2322 { 2323 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY)) 2324 cpu_relax(); 2325 2326 return atmel_uart_read_char(port); 2327 } 2328 2329 static void atmel_poll_put_char(struct uart_port *port, unsigned char ch) 2330 { 2331 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2332 cpu_relax(); 2333 2334 atmel_uart_write_char(port, ch); 2335 } 2336 #endif 2337 2338 static const struct uart_ops atmel_pops = { 2339 .tx_empty = atmel_tx_empty, 2340 .set_mctrl = atmel_set_mctrl, 2341 .get_mctrl = atmel_get_mctrl, 2342 .stop_tx = atmel_stop_tx, 2343 .start_tx = atmel_start_tx, 2344 .stop_rx = atmel_stop_rx, 2345 .enable_ms = atmel_enable_ms, 2346 .break_ctl = atmel_break_ctl, 2347 .startup = atmel_startup, 2348 .shutdown = atmel_shutdown, 2349 .flush_buffer = atmel_flush_buffer, 2350 .set_termios = atmel_set_termios, 2351 .set_ldisc = atmel_set_ldisc, 2352 .type = atmel_type, 2353 .release_port = atmel_release_port, 2354 .request_port = atmel_request_port, 2355 .config_port = atmel_config_port, 2356 .verify_port = atmel_verify_port, 2357 .pm = atmel_serial_pm, 2358 #ifdef CONFIG_CONSOLE_POLL 2359 .poll_get_char = atmel_poll_get_char, 2360 .poll_put_char = atmel_poll_put_char, 2361 #endif 2362 }; 2363 2364 /* 2365 * Configure the port from the platform device resource info. 2366 */ 2367 static int atmel_init_port(struct atmel_uart_port *atmel_port, 2368 struct platform_device *pdev) 2369 { 2370 int ret; 2371 struct uart_port *port = &atmel_port->uart; 2372 2373 atmel_init_property(atmel_port, pdev); 2374 atmel_set_ops(port); 2375 2376 atmel_init_rs485(port, pdev); 2377 2378 port->iotype = UPIO_MEM; 2379 port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP; 2380 port->ops = &atmel_pops; 2381 port->fifosize = 1; 2382 port->dev = &pdev->dev; 2383 port->mapbase = pdev->resource[0].start; 2384 port->irq = pdev->resource[1].start; 2385 port->rs485_config = atmel_config_rs485; 2386 port->membase = NULL; 2387 2388 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring)); 2389 2390 /* for console, the clock could already be configured */ 2391 if (!atmel_port->clk) { 2392 atmel_port->clk = clk_get(&pdev->dev, "usart"); 2393 if (IS_ERR(atmel_port->clk)) { 2394 ret = PTR_ERR(atmel_port->clk); 2395 atmel_port->clk = NULL; 2396 return ret; 2397 } 2398 ret = clk_prepare_enable(atmel_port->clk); 2399 if (ret) { 2400 clk_put(atmel_port->clk); 2401 atmel_port->clk = NULL; 2402 return ret; 2403 } 2404 port->uartclk = clk_get_rate(atmel_port->clk); 2405 clk_disable_unprepare(atmel_port->clk); 2406 /* only enable clock when USART is in use */ 2407 } 2408 2409 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ 2410 if (port->rs485.flags & SER_RS485_ENABLED) 2411 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; 2412 else if (atmel_use_pdc_tx(port)) { 2413 port->fifosize = PDC_BUFFER_SIZE; 2414 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; 2415 } else { 2416 atmel_port->tx_done_mask = ATMEL_US_TXRDY; 2417 } 2418 2419 return 0; 2420 } 2421 2422 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2423 static void atmel_console_putchar(struct uart_port *port, int ch) 2424 { 2425 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) 2426 cpu_relax(); 2427 atmel_uart_write_char(port, ch); 2428 } 2429 2430 /* 2431 * Interrupts are disabled on entering 2432 */ 2433 static void atmel_console_write(struct console *co, const char *s, u_int count) 2434 { 2435 struct uart_port *port = &atmel_ports[co->index].uart; 2436 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2437 unsigned int status, imr; 2438 unsigned int pdc_tx; 2439 2440 /* 2441 * First, save IMR and then disable interrupts 2442 */ 2443 imr = atmel_uart_readl(port, ATMEL_US_IMR); 2444 atmel_uart_writel(port, ATMEL_US_IDR, 2445 ATMEL_US_RXRDY | atmel_port->tx_done_mask); 2446 2447 /* Store PDC transmit status and disable it */ 2448 pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN; 2449 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS); 2450 2451 /* Make sure that tx path is actually able to send characters */ 2452 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN); 2453 2454 uart_console_write(port, s, count, atmel_console_putchar); 2455 2456 /* 2457 * Finally, wait for transmitter to become empty 2458 * and restore IMR 2459 */ 2460 do { 2461 status = atmel_uart_readl(port, ATMEL_US_CSR); 2462 } while (!(status & ATMEL_US_TXRDY)); 2463 2464 /* Restore PDC transmit status */ 2465 if (pdc_tx) 2466 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN); 2467 2468 /* set interrupts back the way they were */ 2469 atmel_uart_writel(port, ATMEL_US_IER, imr); 2470 } 2471 2472 /* 2473 * If the port was already initialised (eg, by a boot loader), 2474 * try to determine the current setup. 2475 */ 2476 static void __init atmel_console_get_options(struct uart_port *port, int *baud, 2477 int *parity, int *bits) 2478 { 2479 unsigned int mr, quot; 2480 2481 /* 2482 * If the baud rate generator isn't running, the port wasn't 2483 * initialized by the boot loader. 2484 */ 2485 quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD; 2486 if (!quot) 2487 return; 2488 2489 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL; 2490 if (mr == ATMEL_US_CHRL_8) 2491 *bits = 8; 2492 else 2493 *bits = 7; 2494 2495 mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR; 2496 if (mr == ATMEL_US_PAR_EVEN) 2497 *parity = 'e'; 2498 else if (mr == ATMEL_US_PAR_ODD) 2499 *parity = 'o'; 2500 2501 /* 2502 * The serial core only rounds down when matching this to a 2503 * supported baud rate. Make sure we don't end up slightly 2504 * lower than one of those, as it would make us fall through 2505 * to a much lower baud rate than we really want. 2506 */ 2507 *baud = port->uartclk / (16 * (quot - 1)); 2508 } 2509 2510 static int __init atmel_console_setup(struct console *co, char *options) 2511 { 2512 int ret; 2513 struct uart_port *port = &atmel_ports[co->index].uart; 2514 int baud = 115200; 2515 int bits = 8; 2516 int parity = 'n'; 2517 int flow = 'n'; 2518 2519 if (port->membase == NULL) { 2520 /* Port not initialized yet - delay setup */ 2521 return -ENODEV; 2522 } 2523 2524 ret = clk_prepare_enable(atmel_ports[co->index].clk); 2525 if (ret) 2526 return ret; 2527 2528 atmel_uart_writel(port, ATMEL_US_IDR, -1); 2529 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX); 2530 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN); 2531 2532 if (options) 2533 uart_parse_options(options, &baud, &parity, &bits, &flow); 2534 else 2535 atmel_console_get_options(port, &baud, &parity, &bits); 2536 2537 return uart_set_options(port, co, baud, parity, bits, flow); 2538 } 2539 2540 static struct uart_driver atmel_uart; 2541 2542 static struct console atmel_console = { 2543 .name = ATMEL_DEVICENAME, 2544 .write = atmel_console_write, 2545 .device = uart_console_device, 2546 .setup = atmel_console_setup, 2547 .flags = CON_PRINTBUFFER, 2548 .index = -1, 2549 .data = &atmel_uart, 2550 }; 2551 2552 #define ATMEL_CONSOLE_DEVICE (&atmel_console) 2553 2554 static inline bool atmel_is_console_port(struct uart_port *port) 2555 { 2556 return port->cons && port->cons->index == port->line; 2557 } 2558 2559 #else 2560 #define ATMEL_CONSOLE_DEVICE NULL 2561 2562 static inline bool atmel_is_console_port(struct uart_port *port) 2563 { 2564 return false; 2565 } 2566 #endif 2567 2568 static struct uart_driver atmel_uart = { 2569 .owner = THIS_MODULE, 2570 .driver_name = "atmel_serial", 2571 .dev_name = ATMEL_DEVICENAME, 2572 .major = SERIAL_ATMEL_MAJOR, 2573 .minor = MINOR_START, 2574 .nr = ATMEL_MAX_UART, 2575 .cons = ATMEL_CONSOLE_DEVICE, 2576 }; 2577 2578 #ifdef CONFIG_PM 2579 static bool atmel_serial_clk_will_stop(void) 2580 { 2581 #ifdef CONFIG_ARCH_AT91 2582 return at91_suspend_entering_slow_clock(); 2583 #else 2584 return false; 2585 #endif 2586 } 2587 2588 static int atmel_serial_suspend(struct platform_device *pdev, 2589 pm_message_t state) 2590 { 2591 struct uart_port *port = platform_get_drvdata(pdev); 2592 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2593 2594 if (atmel_is_console_port(port) && console_suspend_enabled) { 2595 /* Drain the TX shifter */ 2596 while (!(atmel_uart_readl(port, ATMEL_US_CSR) & 2597 ATMEL_US_TXEMPTY)) 2598 cpu_relax(); 2599 } 2600 2601 if (atmel_is_console_port(port) && !console_suspend_enabled) { 2602 /* Cache register values as we won't get a full shutdown/startup 2603 * cycle 2604 */ 2605 atmel_port->cache.mr = atmel_uart_readl(port, ATMEL_US_MR); 2606 atmel_port->cache.imr = atmel_uart_readl(port, ATMEL_US_IMR); 2607 atmel_port->cache.brgr = atmel_uart_readl(port, ATMEL_US_BRGR); 2608 atmel_port->cache.rtor = atmel_uart_readl(port, 2609 atmel_port->rtor); 2610 atmel_port->cache.ttgr = atmel_uart_readl(port, ATMEL_US_TTGR); 2611 atmel_port->cache.fmr = atmel_uart_readl(port, ATMEL_US_FMR); 2612 atmel_port->cache.fimr = atmel_uart_readl(port, ATMEL_US_FIMR); 2613 } 2614 2615 /* we can not wake up if we're running on slow clock */ 2616 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev); 2617 if (atmel_serial_clk_will_stop()) { 2618 unsigned long flags; 2619 2620 spin_lock_irqsave(&atmel_port->lock_suspended, flags); 2621 atmel_port->suspended = true; 2622 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2623 device_set_wakeup_enable(&pdev->dev, 0); 2624 } 2625 2626 uart_suspend_port(&atmel_uart, port); 2627 2628 return 0; 2629 } 2630 2631 static int atmel_serial_resume(struct platform_device *pdev) 2632 { 2633 struct uart_port *port = platform_get_drvdata(pdev); 2634 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2635 unsigned long flags; 2636 2637 if (atmel_is_console_port(port) && !console_suspend_enabled) { 2638 atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr); 2639 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr); 2640 atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr); 2641 atmel_uart_writel(port, atmel_port->rtor, 2642 atmel_port->cache.rtor); 2643 atmel_uart_writel(port, ATMEL_US_TTGR, atmel_port->cache.ttgr); 2644 2645 if (atmel_port->fifo_size) { 2646 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_FIFOEN | 2647 ATMEL_US_RXFCLR | ATMEL_US_TXFLCLR); 2648 atmel_uart_writel(port, ATMEL_US_FMR, 2649 atmel_port->cache.fmr); 2650 atmel_uart_writel(port, ATMEL_US_FIER, 2651 atmel_port->cache.fimr); 2652 } 2653 atmel_start_rx(port); 2654 } 2655 2656 spin_lock_irqsave(&atmel_port->lock_suspended, flags); 2657 if (atmel_port->pending) { 2658 atmel_handle_receive(port, atmel_port->pending); 2659 atmel_handle_status(port, atmel_port->pending, 2660 atmel_port->pending_status); 2661 atmel_handle_transmit(port, atmel_port->pending); 2662 atmel_port->pending = 0; 2663 } 2664 atmel_port->suspended = false; 2665 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags); 2666 2667 uart_resume_port(&atmel_uart, port); 2668 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup); 2669 2670 return 0; 2671 } 2672 #else 2673 #define atmel_serial_suspend NULL 2674 #define atmel_serial_resume NULL 2675 #endif 2676 2677 static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port, 2678 struct platform_device *pdev) 2679 { 2680 atmel_port->fifo_size = 0; 2681 atmel_port->rts_low = 0; 2682 atmel_port->rts_high = 0; 2683 2684 if (of_property_read_u32(pdev->dev.of_node, 2685 "atmel,fifo-size", 2686 &atmel_port->fifo_size)) 2687 return; 2688 2689 if (!atmel_port->fifo_size) 2690 return; 2691 2692 if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) { 2693 atmel_port->fifo_size = 0; 2694 dev_err(&pdev->dev, "Invalid FIFO size\n"); 2695 return; 2696 } 2697 2698 /* 2699 * 0 <= rts_low <= rts_high <= fifo_size 2700 * Once their CTS line asserted by the remote peer, some x86 UARTs tend 2701 * to flush their internal TX FIFO, commonly up to 16 data, before 2702 * actually stopping to send new data. So we try to set the RTS High 2703 * Threshold to a reasonably high value respecting this 16 data 2704 * empirical rule when possible. 2705 */ 2706 atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1, 2707 atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET); 2708 atmel_port->rts_low = max_t(int, atmel_port->fifo_size >> 2, 2709 atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET); 2710 2711 dev_info(&pdev->dev, "Using FIFO (%u data)\n", 2712 atmel_port->fifo_size); 2713 dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n", 2714 atmel_port->rts_high); 2715 dev_dbg(&pdev->dev, "RTS Low Threshold : %2u data\n", 2716 atmel_port->rts_low); 2717 } 2718 2719 static int atmel_serial_probe(struct platform_device *pdev) 2720 { 2721 struct atmel_uart_port *atmel_port; 2722 struct device_node *np = pdev->dev.of_node; 2723 void *data; 2724 int ret = -ENODEV; 2725 bool rs485_enabled; 2726 2727 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); 2728 2729 ret = of_alias_get_id(np, "serial"); 2730 if (ret < 0) 2731 /* port id not found in platform data nor device-tree aliases: 2732 * auto-enumerate it */ 2733 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART); 2734 2735 if (ret >= ATMEL_MAX_UART) { 2736 ret = -ENODEV; 2737 goto err; 2738 } 2739 2740 if (test_and_set_bit(ret, atmel_ports_in_use)) { 2741 /* port already in use */ 2742 ret = -EBUSY; 2743 goto err; 2744 } 2745 2746 atmel_port = &atmel_ports[ret]; 2747 atmel_port->backup_imr = 0; 2748 atmel_port->uart.line = ret; 2749 atmel_serial_probe_fifos(atmel_port, pdev); 2750 2751 atomic_set(&atmel_port->tasklet_shutdown, 0); 2752 spin_lock_init(&atmel_port->lock_suspended); 2753 2754 ret = atmel_init_port(atmel_port, pdev); 2755 if (ret) 2756 goto err_clear_bit; 2757 2758 atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0); 2759 if (IS_ERR(atmel_port->gpios)) { 2760 ret = PTR_ERR(atmel_port->gpios); 2761 goto err_clear_bit; 2762 } 2763 2764 if (!atmel_use_pdc_rx(&atmel_port->uart)) { 2765 ret = -ENOMEM; 2766 data = kmalloc(sizeof(struct atmel_uart_char) 2767 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); 2768 if (!data) 2769 goto err_alloc_ring; 2770 atmel_port->rx_ring.buf = data; 2771 } 2772 2773 rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED; 2774 2775 ret = uart_add_one_port(&atmel_uart, &atmel_port->uart); 2776 if (ret) 2777 goto err_add_port; 2778 2779 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE 2780 if (atmel_is_console_port(&atmel_port->uart) 2781 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) { 2782 /* 2783 * The serial core enabled the clock for us, so undo 2784 * the clk_prepare_enable() in atmel_console_setup() 2785 */ 2786 clk_disable_unprepare(atmel_port->clk); 2787 } 2788 #endif 2789 2790 device_init_wakeup(&pdev->dev, 1); 2791 platform_set_drvdata(pdev, atmel_port); 2792 2793 /* 2794 * The peripheral clock has been disabled by atmel_init_port(): 2795 * enable it before accessing I/O registers 2796 */ 2797 clk_prepare_enable(atmel_port->clk); 2798 2799 if (rs485_enabled) { 2800 atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR, 2801 ATMEL_US_USMODE_NORMAL); 2802 atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR, 2803 ATMEL_US_RTSEN); 2804 } 2805 2806 /* 2807 * Get port name of usart or uart 2808 */ 2809 atmel_get_ip_name(&atmel_port->uart); 2810 2811 /* 2812 * The peripheral clock can now safely be disabled till the port 2813 * is used 2814 */ 2815 clk_disable_unprepare(atmel_port->clk); 2816 2817 return 0; 2818 2819 err_add_port: 2820 kfree(atmel_port->rx_ring.buf); 2821 atmel_port->rx_ring.buf = NULL; 2822 err_alloc_ring: 2823 if (!atmel_is_console_port(&atmel_port->uart)) { 2824 clk_put(atmel_port->clk); 2825 atmel_port->clk = NULL; 2826 } 2827 err_clear_bit: 2828 clear_bit(atmel_port->uart.line, atmel_ports_in_use); 2829 err: 2830 return ret; 2831 } 2832 2833 /* 2834 * Even if the driver is not modular, it makes sense to be able to 2835 * unbind a device: there can be many bound devices, and there are 2836 * situations where dynamic binding and unbinding can be useful. 2837 * 2838 * For example, a connected device can require a specific firmware update 2839 * protocol that needs bitbanging on IO lines, but use the regular serial 2840 * port in the normal case. 2841 */ 2842 static int atmel_serial_remove(struct platform_device *pdev) 2843 { 2844 struct uart_port *port = platform_get_drvdata(pdev); 2845 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); 2846 int ret = 0; 2847 2848 tasklet_kill(&atmel_port->tasklet_rx); 2849 tasklet_kill(&atmel_port->tasklet_tx); 2850 2851 device_init_wakeup(&pdev->dev, 0); 2852 2853 ret = uart_remove_one_port(&atmel_uart, port); 2854 2855 kfree(atmel_port->rx_ring.buf); 2856 2857 /* "port" is allocated statically, so we shouldn't free it */ 2858 2859 clear_bit(port->line, atmel_ports_in_use); 2860 2861 clk_put(atmel_port->clk); 2862 atmel_port->clk = NULL; 2863 2864 return ret; 2865 } 2866 2867 static struct platform_driver atmel_serial_driver = { 2868 .probe = atmel_serial_probe, 2869 .remove = atmel_serial_remove, 2870 .suspend = atmel_serial_suspend, 2871 .resume = atmel_serial_resume, 2872 .driver = { 2873 .name = "atmel_usart", 2874 .of_match_table = of_match_ptr(atmel_serial_dt_ids), 2875 }, 2876 }; 2877 2878 static int __init atmel_serial_init(void) 2879 { 2880 int ret; 2881 2882 ret = uart_register_driver(&atmel_uart); 2883 if (ret) 2884 return ret; 2885 2886 ret = platform_driver_register(&atmel_serial_driver); 2887 if (ret) 2888 uart_unregister_driver(&atmel_uart); 2889 2890 return ret; 2891 } 2892 device_initcall(atmel_serial_init); 2893