2 * linux/drivers/char/atmel_serial.c
4 * Driver for Atmel AT91 / AT32 Serial ports
5 * Copyright (C) 2003 Rick Bronson
7 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
8 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
10 * DMA support added by Chip Coldwell.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
28 #include <linux/tty.h>
29 #include <linux/ioport.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/serial.h>
33 #include <linux/clk.h>
34 #include <linux/console.h>
35 #include <linux/sysrq.h>
36 #include <linux/tty_flip.h>
37 #include <linux/platform_device.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/atmel_pdc.h>
40 #include <linux/atmel_serial.h>
44 #include <asm/mach/serial_at91.h>
45 #include <mach/board.h>
49 #include <mach/gpio.h>
52 #define PDC_BUFFER_SIZE 512
53 /* Revisit: We should calculate this based on the actual port settings */
54 #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
56 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
60 #include <linux/serial_core.h>
62 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
64 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
65 * should coexist with the 8250 driver, such as if we have an external 16C550
67 #define SERIAL_ATMEL_MAJOR 204
68 #define MINOR_START 154
69 #define ATMEL_DEVICENAME "ttyAT"
73 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
74 * name, but it is legally reserved for the 8250 driver. */
75 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
76 #define MINOR_START 64
77 #define ATMEL_DEVICENAME "ttyS"
81 #define ATMEL_ISR_PASS_LIMIT 256
83 /* UART registers. CR is write-only, hence no GET macro */
84 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
85 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
86 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
87 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
88 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
89 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
90 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
91 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
92 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
93 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
94 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
95 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
98 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
99 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
101 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
102 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
103 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
104 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
105 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
107 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
108 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
109 #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
111 static int (*atmel_open_hook)(struct uart_port *);
112 static void (*atmel_close_hook)(struct uart_port *);
114 struct atmel_dma_buffer {
117 unsigned int dma_size;
121 struct atmel_uart_char {
126 #define ATMEL_SERIAL_RINGSIZE 1024
129 * We wrap our port structure around the generic uart_port.
131 struct atmel_uart_port {
132 struct uart_port uart; /* uart */
133 struct clk *clk; /* uart clock */
134 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
135 u32 backup_imr; /* IMR saved during suspend */
136 int break_active; /* break being received */
138 short use_dma_rx; /* enable PDC receiver */
139 short pdc_rx_idx; /* current PDC RX buffer */
140 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
142 short use_dma_tx; /* enable PDC transmitter */
143 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
145 struct tasklet_struct tasklet;
146 unsigned int irq_status;
147 unsigned int irq_status_prev;
149 struct circ_buf rx_ring;
152 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
155 static struct console atmel_console;
158 static inline struct atmel_uart_port *
159 to_atmel_uart_port(struct uart_port *uart)
161 return container_of(uart, struct atmel_uart_port, uart);
164 #ifdef CONFIG_SERIAL_ATMEL_PDC
165 static bool atmel_use_dma_rx(struct uart_port *port)
167 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
169 return atmel_port->use_dma_rx;
172 static bool atmel_use_dma_tx(struct uart_port *port)
174 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
176 return atmel_port->use_dma_tx;
179 static bool atmel_use_dma_rx(struct uart_port *port)
184 static bool atmel_use_dma_tx(struct uart_port *port)
191 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
193 static u_int atmel_tx_empty(struct uart_port *port)
195 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
199 * Set state of the modem control output lines
201 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
203 unsigned int control = 0;
206 #ifdef CONFIG_ARCH_AT91RM9200
207 if (cpu_is_at91rm9200()) {
209 * AT91RM9200 Errata #39: RTS0 is not internally connected
210 * to PA21. We need to drive the pin manually.
212 if (port->mapbase == AT91RM9200_BASE_US0) {
213 if (mctrl & TIOCM_RTS)
214 at91_set_gpio_value(AT91_PIN_PA21, 0);
216 at91_set_gpio_value(AT91_PIN_PA21, 1);
221 if (mctrl & TIOCM_RTS)
222 control |= ATMEL_US_RTSEN;
224 control |= ATMEL_US_RTSDIS;
226 if (mctrl & TIOCM_DTR)
227 control |= ATMEL_US_DTREN;
229 control |= ATMEL_US_DTRDIS;
231 UART_PUT_CR(port, control);
233 /* Local loopback mode? */
234 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
235 if (mctrl & TIOCM_LOOP)
236 mode |= ATMEL_US_CHMODE_LOC_LOOP;
238 mode |= ATMEL_US_CHMODE_NORMAL;
239 UART_PUT_MR(port, mode);
243 * Get state of the modem control input lines
245 static u_int atmel_get_mctrl(struct uart_port *port)
247 unsigned int status, ret = 0;
249 status = UART_GET_CSR(port);
252 * The control signals are active low.
254 if (!(status & ATMEL_US_DCD))
256 if (!(status & ATMEL_US_CTS))
258 if (!(status & ATMEL_US_DSR))
260 if (!(status & ATMEL_US_RI))
269 static void atmel_stop_tx(struct uart_port *port)
271 if (atmel_use_dma_tx(port)) {
272 /* disable PDC transmit */
273 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
274 UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
276 UART_PUT_IDR(port, ATMEL_US_TXRDY);
280 * Start transmitting.
282 static void atmel_start_tx(struct uart_port *port)
284 if (atmel_use_dma_tx(port)) {
285 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
286 /* The transmitter is already running. Yes, we
290 UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
291 /* re-enable PDC transmit */
292 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
294 UART_PUT_IER(port, ATMEL_US_TXRDY);
298 * Stop receiving - port is in process of being closed.
300 static void atmel_stop_rx(struct uart_port *port)
302 if (atmel_use_dma_rx(port)) {
303 /* disable PDC receive */
304 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
305 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
307 UART_PUT_IDR(port, ATMEL_US_RXRDY);
311 * Enable modem status interrupts
313 static void atmel_enable_ms(struct uart_port *port)
315 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
316 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
320 * Control the transmission of a break signal
322 static void atmel_break_ctl(struct uart_port *port, int break_state)
324 if (break_state != 0)
325 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
327 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
331 * Stores the incoming character in the ring buffer
334 atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
337 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
338 struct circ_buf *ring = &atmel_port->rx_ring;
339 struct atmel_uart_char *c;
341 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
342 /* Buffer overflow, ignore char */
345 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
349 /* Make sure the character is stored before we update head. */
352 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
356 * Deal with parity, framing and overrun errors.
358 static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
361 UART_PUT_CR(port, ATMEL_US_RSTSTA);
363 if (status & ATMEL_US_RXBRK) {
364 /* ignore side-effect */
365 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
368 if (status & ATMEL_US_PARE)
369 port->icount.parity++;
370 if (status & ATMEL_US_FRAME)
371 port->icount.frame++;
372 if (status & ATMEL_US_OVRE)
373 port->icount.overrun++;
377 * Characters received (called from interrupt handler)
379 static void atmel_rx_chars(struct uart_port *port)
381 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
382 unsigned int status, ch;
384 status = UART_GET_CSR(port);
385 while (status & ATMEL_US_RXRDY) {
386 ch = UART_GET_CHAR(port);
389 * note that the error handling code is
390 * out of the main execution path
392 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
393 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
394 || atmel_port->break_active)) {
397 UART_PUT_CR(port, ATMEL_US_RSTSTA);
399 if (status & ATMEL_US_RXBRK
400 && !atmel_port->break_active) {
401 atmel_port->break_active = 1;
402 UART_PUT_IER(port, ATMEL_US_RXBRK);
405 * This is either the end-of-break
406 * condition or we've received at
407 * least one character without RXBRK
408 * being set. In both cases, the next
409 * RXBRK will indicate start-of-break.
411 UART_PUT_IDR(port, ATMEL_US_RXBRK);
412 status &= ~ATMEL_US_RXBRK;
413 atmel_port->break_active = 0;
417 atmel_buffer_rx_char(port, status, ch);
418 status = UART_GET_CSR(port);
421 tasklet_schedule(&atmel_port->tasklet);
425 * Transmit characters (called from tasklet with TXRDY interrupt
428 static void atmel_tx_chars(struct uart_port *port)
430 struct circ_buf *xmit = &port->info->xmit;
432 if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
433 UART_PUT_CHAR(port, port->x_char);
437 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
440 while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
441 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
442 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
444 if (uart_circ_empty(xmit))
448 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
449 uart_write_wakeup(port);
451 if (!uart_circ_empty(xmit))
452 UART_PUT_IER(port, ATMEL_US_TXRDY);
456 * receive interrupt handler.
459 atmel_handle_receive(struct uart_port *port, unsigned int pending)
461 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
463 if (atmel_use_dma_rx(port)) {
465 * PDC receive. Just schedule the tasklet and let it
466 * figure out the details.
468 * TODO: We're not handling error flags correctly at
471 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
472 UART_PUT_IDR(port, (ATMEL_US_ENDRX
473 | ATMEL_US_TIMEOUT));
474 tasklet_schedule(&atmel_port->tasklet);
477 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
478 ATMEL_US_FRAME | ATMEL_US_PARE))
479 atmel_pdc_rxerr(port, pending);
482 /* Interrupt receive */
483 if (pending & ATMEL_US_RXRDY)
484 atmel_rx_chars(port);
485 else if (pending & ATMEL_US_RXBRK) {
487 * End of break detected. If it came along with a
488 * character, atmel_rx_chars will handle it.
490 UART_PUT_CR(port, ATMEL_US_RSTSTA);
491 UART_PUT_IDR(port, ATMEL_US_RXBRK);
492 atmel_port->break_active = 0;
497 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
500 atmel_handle_transmit(struct uart_port *port, unsigned int pending)
502 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
504 if (atmel_use_dma_tx(port)) {
506 if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) {
507 UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
508 tasklet_schedule(&atmel_port->tasklet);
511 /* Interrupt transmit */
512 if (pending & ATMEL_US_TXRDY) {
513 UART_PUT_IDR(port, ATMEL_US_TXRDY);
514 tasklet_schedule(&atmel_port->tasklet);
520 * status flags interrupt handler.
523 atmel_handle_status(struct uart_port *port, unsigned int pending,
526 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
528 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
530 atmel_port->irq_status = status;
531 tasklet_schedule(&atmel_port->tasklet);
538 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
540 struct uart_port *port = dev_id;
541 unsigned int status, pending, pass_counter = 0;
544 status = UART_GET_CSR(port);
545 pending = status & UART_GET_IMR(port);
549 atmel_handle_receive(port, pending);
550 atmel_handle_status(port, pending, status);
551 atmel_handle_transmit(port, pending);
552 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
554 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
558 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
560 static void atmel_tx_dma(struct uart_port *port)
562 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
563 struct circ_buf *xmit = &port->info->xmit;
564 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
567 /* nothing left to transmit? */
568 if (UART_GET_TCR(port))
571 xmit->tail += pdc->ofs;
572 xmit->tail &= UART_XMIT_SIZE - 1;
574 port->icount.tx += pdc->ofs;
577 /* more to transmit - setup next transfer */
579 /* disable PDC transmit */
580 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
582 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
583 dma_sync_single_for_device(port->dev,
588 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
591 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
592 UART_PUT_TCR(port, count);
593 /* re-enable PDC transmit and interrupts */
594 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
595 UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
598 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
599 uart_write_wakeup(port);
602 static void atmel_rx_from_ring(struct uart_port *port)
604 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
605 struct circ_buf *ring = &atmel_port->rx_ring;
609 while (ring->head != ring->tail) {
610 struct atmel_uart_char c;
612 /* Make sure c is loaded after head. */
615 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
617 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
624 * note that the error handling code is
625 * out of the main execution path
627 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
628 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
629 if (status & ATMEL_US_RXBRK) {
630 /* ignore side-effect */
631 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
634 if (uart_handle_break(port))
637 if (status & ATMEL_US_PARE)
638 port->icount.parity++;
639 if (status & ATMEL_US_FRAME)
640 port->icount.frame++;
641 if (status & ATMEL_US_OVRE)
642 port->icount.overrun++;
644 status &= port->read_status_mask;
646 if (status & ATMEL_US_RXBRK)
648 else if (status & ATMEL_US_PARE)
650 else if (status & ATMEL_US_FRAME)
655 if (uart_handle_sysrq_char(port, c.ch))
658 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
662 * Drop the lock here since it might end up calling
663 * uart_start(), which takes the lock.
665 spin_unlock(&port->lock);
666 tty_flip_buffer_push(port->info->port.tty);
667 spin_lock(&port->lock);
670 static void atmel_rx_from_dma(struct uart_port *port)
672 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
673 struct tty_struct *tty = port->info->port.tty;
674 struct atmel_dma_buffer *pdc;
675 int rx_idx = atmel_port->pdc_rx_idx;
681 /* Reset the UART timeout early so that we don't miss one */
682 UART_PUT_CR(port, ATMEL_US_STTTO);
684 pdc = &atmel_port->pdc_rx[rx_idx];
685 head = UART_GET_RPR(port) - pdc->dma_addr;
688 /* If the PDC has switched buffers, RPR won't contain
689 * any address within the current buffer. Since head
690 * is unsigned, we just need a one-way comparison to
693 * In this case, we just need to consume the entire
694 * buffer and resubmit it for DMA. This will clear the
695 * ENDRX bit as well, so that we can safely re-enable
696 * all interrupts below.
698 head = min(head, pdc->dma_size);
700 if (likely(head != tail)) {
701 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
702 pdc->dma_size, DMA_FROM_DEVICE);
705 * head will only wrap around when we recycle
706 * the DMA buffer, and when that happens, we
707 * explicitly set tail to 0. So head will
708 * always be greater than tail.
712 tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
714 dma_sync_single_for_device(port->dev, pdc->dma_addr,
715 pdc->dma_size, DMA_FROM_DEVICE);
717 port->icount.rx += count;
722 * If the current buffer is full, we need to check if
723 * the next one contains any additional data.
725 if (head >= pdc->dma_size) {
727 UART_PUT_RNPR(port, pdc->dma_addr);
728 UART_PUT_RNCR(port, pdc->dma_size);
731 atmel_port->pdc_rx_idx = rx_idx;
733 } while (head >= pdc->dma_size);
736 * Drop the lock here since it might end up calling
737 * uart_start(), which takes the lock.
739 spin_unlock(&port->lock);
740 tty_flip_buffer_push(tty);
741 spin_lock(&port->lock);
743 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
747 * tasklet handling tty stuff outside the interrupt handler.
749 static void atmel_tasklet_func(unsigned long data)
751 struct uart_port *port = (struct uart_port *)data;
752 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
754 unsigned int status_change;
756 /* The interrupt handler does not take the lock */
757 spin_lock(&port->lock);
759 if (atmel_use_dma_tx(port))
762 atmel_tx_chars(port);
764 status = atmel_port->irq_status;
765 status_change = status ^ atmel_port->irq_status_prev;
767 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
768 | ATMEL_US_DCD | ATMEL_US_CTS)) {
769 /* TODO: All reads to CSR will clear these interrupts! */
770 if (status_change & ATMEL_US_RI)
772 if (status_change & ATMEL_US_DSR)
774 if (status_change & ATMEL_US_DCD)
775 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
776 if (status_change & ATMEL_US_CTS)
777 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
779 wake_up_interruptible(&port->info->delta_msr_wait);
781 atmel_port->irq_status_prev = status;
784 if (atmel_use_dma_rx(port))
785 atmel_rx_from_dma(port);
787 atmel_rx_from_ring(port);
789 spin_unlock(&port->lock);
793 * Perform initialization and enable port for reception
795 static int atmel_startup(struct uart_port *port)
797 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
798 struct tty_struct *tty = port->info->port.tty;
802 * Ensure that no interrupts are enabled otherwise when
803 * request_irq() is called we could get stuck trying to
804 * handle an unexpected interrupt
806 UART_PUT_IDR(port, -1);
811 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
812 tty ? tty->name : "atmel_serial", port);
814 printk("atmel_serial: atmel_startup - Can't get irq\n");
819 * Initialize DMA (if necessary)
821 if (atmel_use_dma_rx(port)) {
824 for (i = 0; i < 2; i++) {
825 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
827 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
828 if (pdc->buf == NULL) {
830 dma_unmap_single(port->dev,
831 atmel_port->pdc_rx[0].dma_addr,
834 kfree(atmel_port->pdc_rx[0].buf);
836 free_irq(port->irq, port);
839 pdc->dma_addr = dma_map_single(port->dev,
843 pdc->dma_size = PDC_BUFFER_SIZE;
847 atmel_port->pdc_rx_idx = 0;
849 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
850 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
852 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
853 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
855 if (atmel_use_dma_tx(port)) {
856 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
857 struct circ_buf *xmit = &port->info->xmit;
859 pdc->buf = xmit->buf;
860 pdc->dma_addr = dma_map_single(port->dev,
864 pdc->dma_size = UART_XMIT_SIZE;
869 * If there is a specific "open" function (to register
870 * control line interrupts)
872 if (atmel_open_hook) {
873 retval = atmel_open_hook(port);
875 free_irq(port->irq, port);
880 /* Save current CSR for comparison in atmel_tasklet_func() */
881 atmel_port->irq_status_prev = UART_GET_CSR(port);
882 atmel_port->irq_status = atmel_port->irq_status_prev;
885 * Finally, enable the serial port
887 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
888 /* enable xmit & rcvr */
889 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
891 if (atmel_use_dma_rx(port)) {
892 /* set UART timeout */
893 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
894 UART_PUT_CR(port, ATMEL_US_STTTO);
896 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
897 /* enable PDC controller */
898 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
900 /* enable receive only */
901 UART_PUT_IER(port, ATMEL_US_RXRDY);
910 static void atmel_shutdown(struct uart_port *port)
912 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
914 * Ensure everything is stopped.
922 if (atmel_use_dma_rx(port)) {
925 for (i = 0; i < 2; i++) {
926 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
928 dma_unmap_single(port->dev,
935 if (atmel_use_dma_tx(port)) {
936 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
938 dma_unmap_single(port->dev,
945 * Disable all interrupts, port and break condition.
947 UART_PUT_CR(port, ATMEL_US_RSTSTA);
948 UART_PUT_IDR(port, -1);
953 free_irq(port->irq, port);
956 * If there is a specific "close" function (to unregister
957 * control line interrupts)
959 if (atmel_close_hook)
960 atmel_close_hook(port);
964 * Flush any TX data submitted for DMA. Called when the TX circular
967 static void atmel_flush_buffer(struct uart_port *port)
969 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
971 if (atmel_use_dma_tx(port)) {
972 UART_PUT_TCR(port, 0);
973 atmel_port->pdc_tx.ofs = 0;
978 * Power / Clock management.
980 static void atmel_serial_pm(struct uart_port *port, unsigned int state,
981 unsigned int oldstate)
983 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
988 * Enable the peripheral clock for this serial port.
989 * This is called on uart_open() or a resume event.
991 clk_enable(atmel_port->clk);
993 /* re-enable interrupts if we disabled some on suspend */
994 UART_PUT_IER(port, atmel_port->backup_imr);
997 /* Back up the interrupt mask and disable all interrupts */
998 atmel_port->backup_imr = UART_GET_IMR(port);
999 UART_PUT_IDR(port, -1);
1002 * Disable the peripheral clock for this serial port.
1003 * This is called on uart_close() or a suspend event.
1005 clk_disable(atmel_port->clk);
1008 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1013 * Change the port parameters
1015 static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1016 struct ktermios *old)
1018 unsigned long flags;
1019 unsigned int mode, imr, quot, baud;
1021 /* Get current mode register */
1022 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1023 | ATMEL_US_NBSTOP | ATMEL_US_PAR);
1025 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1026 quot = uart_get_divisor(port, baud);
1028 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
1030 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1034 switch (termios->c_cflag & CSIZE) {
1036 mode |= ATMEL_US_CHRL_5;
1039 mode |= ATMEL_US_CHRL_6;
1042 mode |= ATMEL_US_CHRL_7;
1045 mode |= ATMEL_US_CHRL_8;
1050 if (termios->c_cflag & CSTOPB)
1051 mode |= ATMEL_US_NBSTOP_2;
1054 if (termios->c_cflag & PARENB) {
1055 /* Mark or Space parity */
1056 if (termios->c_cflag & CMSPAR) {
1057 if (termios->c_cflag & PARODD)
1058 mode |= ATMEL_US_PAR_MARK;
1060 mode |= ATMEL_US_PAR_SPACE;
1061 } else if (termios->c_cflag & PARODD)
1062 mode |= ATMEL_US_PAR_ODD;
1064 mode |= ATMEL_US_PAR_EVEN;
1066 mode |= ATMEL_US_PAR_NONE;
1068 spin_lock_irqsave(&port->lock, flags);
1070 port->read_status_mask = ATMEL_US_OVRE;
1071 if (termios->c_iflag & INPCK)
1072 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1073 if (termios->c_iflag & (BRKINT | PARMRK))
1074 port->read_status_mask |= ATMEL_US_RXBRK;
1076 if (atmel_use_dma_rx(port))
1077 /* need to enable error interrupts */
1078 UART_PUT_IER(port, port->read_status_mask);
1081 * Characters to ignore
1083 port->ignore_status_mask = 0;
1084 if (termios->c_iflag & IGNPAR)
1085 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1086 if (termios->c_iflag & IGNBRK) {
1087 port->ignore_status_mask |= ATMEL_US_RXBRK;
1089 * If we're ignoring parity and break indicators,
1090 * ignore overruns too (for real raw support).
1092 if (termios->c_iflag & IGNPAR)
1093 port->ignore_status_mask |= ATMEL_US_OVRE;
1095 /* TODO: Ignore all characters if CREAD is set.*/
1097 /* update the per-port timeout */
1098 uart_update_timeout(port, termios->c_cflag, baud);
1100 /* save/disable interrupts and drain transmitter */
1101 imr = UART_GET_IMR(port);
1102 UART_PUT_IDR(port, -1);
1103 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1106 /* disable receiver and transmitter */
1107 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1109 /* set the parity, stop bits and data size */
1110 UART_PUT_MR(port, mode);
1112 /* set the baud rate */
1113 UART_PUT_BRGR(port, quot);
1114 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1115 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1117 /* restore interrupts */
1118 UART_PUT_IER(port, imr);
1120 /* CTS flow-control and modem-status interrupts */
1121 if (UART_ENABLE_MS(port, termios->c_cflag))
1122 port->ops->enable_ms(port);
1124 spin_unlock_irqrestore(&port->lock, flags);
1128 * Return string describing the specified port
1130 static const char *atmel_type(struct uart_port *port)
1132 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1136 * Release the memory region(s) being used by 'port'.
1138 static void atmel_release_port(struct uart_port *port)
1140 struct platform_device *pdev = to_platform_device(port->dev);
1141 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1143 release_mem_region(port->mapbase, size);
1145 if (port->flags & UPF_IOREMAP) {
1146 iounmap(port->membase);
1147 port->membase = NULL;
1152 * Request the memory region(s) being used by 'port'.
1154 static int atmel_request_port(struct uart_port *port)
1156 struct platform_device *pdev = to_platform_device(port->dev);
1157 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1159 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1162 if (port->flags & UPF_IOREMAP) {
1163 port->membase = ioremap(port->mapbase, size);
1164 if (port->membase == NULL) {
1165 release_mem_region(port->mapbase, size);
1174 * Configure/autoconfigure the port.
1176 static void atmel_config_port(struct uart_port *port, int flags)
1178 if (flags & UART_CONFIG_TYPE) {
1179 port->type = PORT_ATMEL;
1180 atmel_request_port(port);
1185 * Verify the new serial_struct (for TIOCSSERIAL).
1187 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1190 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1192 if (port->irq != ser->irq)
1194 if (ser->io_type != SERIAL_IO_MEM)
1196 if (port->uartclk / 16 != ser->baud_base)
1198 if ((void *)port->mapbase != ser->iomem_base)
1200 if (port->iobase != ser->port)
1207 static struct uart_ops atmel_pops = {
1208 .tx_empty = atmel_tx_empty,
1209 .set_mctrl = atmel_set_mctrl,
1210 .get_mctrl = atmel_get_mctrl,
1211 .stop_tx = atmel_stop_tx,
1212 .start_tx = atmel_start_tx,
1213 .stop_rx = atmel_stop_rx,
1214 .enable_ms = atmel_enable_ms,
1215 .break_ctl = atmel_break_ctl,
1216 .startup = atmel_startup,
1217 .shutdown = atmel_shutdown,
1218 .flush_buffer = atmel_flush_buffer,
1219 .set_termios = atmel_set_termios,
1221 .release_port = atmel_release_port,
1222 .request_port = atmel_request_port,
1223 .config_port = atmel_config_port,
1224 .verify_port = atmel_verify_port,
1225 .pm = atmel_serial_pm,
1229 * Configure the port from the platform device resource info.
1231 static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1232 struct platform_device *pdev)
1234 struct uart_port *port = &atmel_port->uart;
1235 struct atmel_uart_data *data = pdev->dev.platform_data;
1237 port->iotype = UPIO_MEM;
1238 port->flags = UPF_BOOT_AUTOCONF;
1239 port->ops = &atmel_pops;
1241 port->line = pdev->id;
1242 port->dev = &pdev->dev;
1244 port->mapbase = pdev->resource[0].start;
1245 port->irq = pdev->resource[1].start;
1247 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1248 (unsigned long)port);
1250 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1253 /* Already mapped by setup code */
1254 port->membase = data->regs;
1256 port->flags |= UPF_IOREMAP;
1257 port->membase = NULL;
1260 /* for console, the clock could already be configured */
1261 if (!atmel_port->clk) {
1262 atmel_port->clk = clk_get(&pdev->dev, "usart");
1263 clk_enable(atmel_port->clk);
1264 port->uartclk = clk_get_rate(atmel_port->clk);
1265 clk_disable(atmel_port->clk);
1266 /* only enable clock when USART is in use */
1269 atmel_port->use_dma_rx = data->use_dma_rx;
1270 atmel_port->use_dma_tx = data->use_dma_tx;
1271 if (atmel_use_dma_tx(port))
1272 port->fifosize = PDC_BUFFER_SIZE;
1276 * Register board-specific modem-control line handlers.
1278 void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
1281 atmel_pops.enable_ms = fns->enable_ms;
1283 atmel_pops.get_mctrl = fns->get_mctrl;
1285 atmel_pops.set_mctrl = fns->set_mctrl;
1286 atmel_open_hook = fns->open;
1287 atmel_close_hook = fns->close;
1288 atmel_pops.pm = fns->pm;
1289 atmel_pops.set_wake = fns->set_wake;
1292 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1293 static void atmel_console_putchar(struct uart_port *port, int ch)
1295 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1297 UART_PUT_CHAR(port, ch);
1301 * Interrupts are disabled on entering
1303 static void atmel_console_write(struct console *co, const char *s, u_int count)
1305 struct uart_port *port = &atmel_ports[co->index].uart;
1306 unsigned int status, imr;
1307 unsigned int pdc_tx;
1310 * First, save IMR and then disable interrupts
1312 imr = UART_GET_IMR(port);
1313 UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
1315 /* Store PDC transmit status and disable it */
1316 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1317 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1319 uart_console_write(port, s, count, atmel_console_putchar);
1322 * Finally, wait for transmitter to become empty
1326 status = UART_GET_CSR(port);
1327 } while (!(status & ATMEL_US_TXRDY));
1329 /* Restore PDC transmit status */
1331 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1333 /* set interrupts back the way they were */
1334 UART_PUT_IER(port, imr);
1338 * If the port was already initialised (eg, by a boot loader),
1339 * try to determine the current setup.
1341 static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1342 int *parity, int *bits)
1344 unsigned int mr, quot;
1347 * If the baud rate generator isn't running, the port wasn't
1348 * initialized by the boot loader.
1350 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
1354 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1355 if (mr == ATMEL_US_CHRL_8)
1360 mr = UART_GET_MR(port) & ATMEL_US_PAR;
1361 if (mr == ATMEL_US_PAR_EVEN)
1363 else if (mr == ATMEL_US_PAR_ODD)
1367 * The serial core only rounds down when matching this to a
1368 * supported baud rate. Make sure we don't end up slightly
1369 * lower than one of those, as it would make us fall through
1370 * to a much lower baud rate than we really want.
1372 *baud = port->uartclk / (16 * (quot - 1));
1375 static int __init atmel_console_setup(struct console *co, char *options)
1377 struct uart_port *port = &atmel_ports[co->index].uart;
1383 if (port->membase == NULL) {
1384 /* Port not initialized yet - delay setup */
1388 clk_enable(atmel_ports[co->index].clk);
1390 UART_PUT_IDR(port, -1);
1391 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1392 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1395 uart_parse_options(options, &baud, &parity, &bits, &flow);
1397 atmel_console_get_options(port, &baud, &parity, &bits);
1399 return uart_set_options(port, co, baud, parity, bits, flow);
1402 static struct uart_driver atmel_uart;
1404 static struct console atmel_console = {
1405 .name = ATMEL_DEVICENAME,
1406 .write = atmel_console_write,
1407 .device = uart_console_device,
1408 .setup = atmel_console_setup,
1409 .flags = CON_PRINTBUFFER,
1411 .data = &atmel_uart,
1414 #define ATMEL_CONSOLE_DEVICE (&atmel_console)
1417 * Early console initialization (before VM subsystem initialized).
1419 static int __init atmel_console_init(void)
1421 if (atmel_default_console_device) {
1422 add_preferred_console(ATMEL_DEVICENAME,
1423 atmel_default_console_device->id, NULL);
1424 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1425 atmel_default_console_device);
1426 register_console(&atmel_console);
1432 console_initcall(atmel_console_init);
1435 * Late console initialization.
1437 static int __init atmel_late_console_init(void)
1439 if (atmel_default_console_device
1440 && !(atmel_console.flags & CON_ENABLED))
1441 register_console(&atmel_console);
1446 core_initcall(atmel_late_console_init);
1448 static inline bool atmel_is_console_port(struct uart_port *port)
1450 return port->cons && port->cons->index == port->line;
1454 #define ATMEL_CONSOLE_DEVICE NULL
1456 static inline bool atmel_is_console_port(struct uart_port *port)
1462 static struct uart_driver atmel_uart = {
1463 .owner = THIS_MODULE,
1464 .driver_name = "atmel_serial",
1465 .dev_name = ATMEL_DEVICENAME,
1466 .major = SERIAL_ATMEL_MAJOR,
1467 .minor = MINOR_START,
1468 .nr = ATMEL_MAX_UART,
1469 .cons = ATMEL_CONSOLE_DEVICE,
1473 static bool atmel_serial_clk_will_stop(void)
1475 #ifdef CONFIG_ARCH_AT91
1476 return at91_suspend_entering_slow_clock();
1482 static int atmel_serial_suspend(struct platform_device *pdev,
1485 struct uart_port *port = platform_get_drvdata(pdev);
1486 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1488 if (atmel_is_console_port(port) && console_suspend_enabled) {
1489 /* Drain the TX shifter */
1490 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1494 /* we can not wake up if we're running on slow clock */
1495 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
1496 if (atmel_serial_clk_will_stop())
1497 device_set_wakeup_enable(&pdev->dev, 0);
1499 uart_suspend_port(&atmel_uart, port);
1504 static int atmel_serial_resume(struct platform_device *pdev)
1506 struct uart_port *port = platform_get_drvdata(pdev);
1507 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1509 uart_resume_port(&atmel_uart, port);
1510 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
1515 #define atmel_serial_suspend NULL
1516 #define atmel_serial_resume NULL
1519 static int __devinit atmel_serial_probe(struct platform_device *pdev)
1521 struct atmel_uart_port *port;
1525 BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE));
1527 port = &atmel_ports[pdev->id];
1528 port->backup_imr = 0;
1530 atmel_init_port(port, pdev);
1532 if (!atmel_use_dma_rx(&port->uart)) {
1534 data = kmalloc(sizeof(struct atmel_uart_char)
1535 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1537 goto err_alloc_ring;
1538 port->rx_ring.buf = data;
1541 ret = uart_add_one_port(&atmel_uart, &port->uart);
1545 if (atmel_is_console_port(&port->uart)
1546 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
1548 * The serial core enabled the clock for us, so undo
1549 * the clk_enable() in atmel_console_setup()
1551 clk_disable(port->clk);
1554 device_init_wakeup(&pdev->dev, 1);
1555 platform_set_drvdata(pdev, port);
1560 kfree(port->rx_ring.buf);
1561 port->rx_ring.buf = NULL;
1563 if (!atmel_is_console_port(&port->uart)) {
1571 static int __devexit atmel_serial_remove(struct platform_device *pdev)
1573 struct uart_port *port = platform_get_drvdata(pdev);
1574 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1577 device_init_wakeup(&pdev->dev, 0);
1578 platform_set_drvdata(pdev, NULL);
1580 ret = uart_remove_one_port(&atmel_uart, port);
1582 tasklet_kill(&atmel_port->tasklet);
1583 kfree(atmel_port->rx_ring.buf);
1585 /* "port" is allocated statically, so we shouldn't free it */
1587 clk_put(atmel_port->clk);
1592 static struct platform_driver atmel_serial_driver = {
1593 .probe = atmel_serial_probe,
1594 .remove = __devexit_p(atmel_serial_remove),
1595 .suspend = atmel_serial_suspend,
1596 .resume = atmel_serial_resume,
1598 .name = "atmel_usart",
1599 .owner = THIS_MODULE,
1603 static int __init atmel_serial_init(void)
1607 ret = uart_register_driver(&atmel_uart);
1611 ret = platform_driver_register(&atmel_serial_driver);
1613 uart_unregister_driver(&atmel_uart);
1618 static void __exit atmel_serial_exit(void)
1620 platform_driver_unregister(&atmel_serial_driver);
1621 uart_unregister_driver(&atmel_uart);
1624 module_init(atmel_serial_init);
1625 module_exit(atmel_serial_exit);
1627 MODULE_AUTHOR("Rick Bronson");
1628 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1629 MODULE_LICENSE("GPL");
1630 MODULE_ALIAS("platform:atmel_usart");