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 <asm/arch/board.h>
48 #include <asm/arch/cpu.h>
49 #include <asm/arch/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)
110 static int (*atmel_open_hook
)(struct uart_port
*);
111 static void (*atmel_close_hook
)(struct uart_port
*);
113 struct atmel_dma_buffer
{
116 unsigned int dma_size
;
120 struct atmel_uart_char
{
125 #define ATMEL_SERIAL_RINGSIZE 1024
128 * We wrap our port structure around the generic uart_port.
130 struct atmel_uart_port
{
131 struct uart_port uart
; /* uart */
132 struct clk
*clk
; /* uart clock */
133 unsigned short suspended
; /* is port suspended? */
134 int break_active
; /* break being received */
136 short use_dma_rx
; /* enable PDC receiver */
137 short pdc_rx_idx
; /* current PDC RX buffer */
138 struct atmel_dma_buffer pdc_rx
[2]; /* PDC receier */
140 short use_dma_tx
; /* enable PDC transmitter */
141 struct atmel_dma_buffer pdc_tx
; /* PDC transmitter */
143 struct tasklet_struct tasklet
;
144 unsigned int irq_status
;
145 unsigned int irq_status_prev
;
147 struct circ_buf rx_ring
;
150 static struct atmel_uart_port atmel_ports
[ATMEL_MAX_UART
];
153 static struct console atmel_console
;
156 #ifdef CONFIG_SERIAL_ATMEL_PDC
157 static bool atmel_use_dma_rx(struct uart_port
*port
)
159 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
161 return atmel_port
->use_dma_rx
;
164 static bool atmel_use_dma_tx(struct uart_port
*port
)
166 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
168 return atmel_port
->use_dma_tx
;
171 static bool atmel_use_dma_rx(struct uart_port
*port
)
176 static bool atmel_use_dma_tx(struct uart_port
*port
)
183 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
185 static u_int
atmel_tx_empty(struct uart_port
*port
)
187 return (UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
) ? TIOCSER_TEMT
: 0;
191 * Set state of the modem control output lines
193 static void atmel_set_mctrl(struct uart_port
*port
, u_int mctrl
)
195 unsigned int control
= 0;
198 #ifdef CONFIG_ARCH_AT91RM9200
199 if (cpu_is_at91rm9200()) {
201 * AT91RM9200 Errata #39: RTS0 is not internally connected
202 * to PA21. We need to drive the pin manually.
204 if (port
->mapbase
== AT91RM9200_BASE_US0
) {
205 if (mctrl
& TIOCM_RTS
)
206 at91_set_gpio_value(AT91_PIN_PA21
, 0);
208 at91_set_gpio_value(AT91_PIN_PA21
, 1);
213 if (mctrl
& TIOCM_RTS
)
214 control
|= ATMEL_US_RTSEN
;
216 control
|= ATMEL_US_RTSDIS
;
218 if (mctrl
& TIOCM_DTR
)
219 control
|= ATMEL_US_DTREN
;
221 control
|= ATMEL_US_DTRDIS
;
223 UART_PUT_CR(port
, control
);
225 /* Local loopback mode? */
226 mode
= UART_GET_MR(port
) & ~ATMEL_US_CHMODE
;
227 if (mctrl
& TIOCM_LOOP
)
228 mode
|= ATMEL_US_CHMODE_LOC_LOOP
;
230 mode
|= ATMEL_US_CHMODE_NORMAL
;
231 UART_PUT_MR(port
, mode
);
235 * Get state of the modem control input lines
237 static u_int
atmel_get_mctrl(struct uart_port
*port
)
239 unsigned int status
, ret
= 0;
241 status
= UART_GET_CSR(port
);
244 * The control signals are active low.
246 if (!(status
& ATMEL_US_DCD
))
248 if (!(status
& ATMEL_US_CTS
))
250 if (!(status
& ATMEL_US_DSR
))
252 if (!(status
& ATMEL_US_RI
))
261 static void atmel_stop_tx(struct uart_port
*port
)
263 if (atmel_use_dma_tx(port
)) {
264 /* disable PDC transmit */
265 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
266 UART_PUT_IDR(port
, ATMEL_US_ENDTX
| ATMEL_US_TXBUFE
);
268 UART_PUT_IDR(port
, ATMEL_US_TXRDY
);
272 * Start transmitting.
274 static void atmel_start_tx(struct uart_port
*port
)
276 if (atmel_use_dma_tx(port
)) {
277 if (UART_GET_PTSR(port
) & ATMEL_PDC_TXTEN
)
278 /* The transmitter is already running. Yes, we
282 UART_PUT_IER(port
, ATMEL_US_ENDTX
| ATMEL_US_TXBUFE
);
283 /* re-enable PDC transmit */
284 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
286 UART_PUT_IER(port
, ATMEL_US_TXRDY
);
290 * Stop receiving - port is in process of being closed.
292 static void atmel_stop_rx(struct uart_port
*port
)
294 if (atmel_use_dma_rx(port
)) {
295 /* disable PDC receive */
296 UART_PUT_PTCR(port
, ATMEL_PDC_RXTDIS
);
297 UART_PUT_IDR(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
299 UART_PUT_IDR(port
, ATMEL_US_RXRDY
);
303 * Enable modem status interrupts
305 static void atmel_enable_ms(struct uart_port
*port
)
307 UART_PUT_IER(port
, ATMEL_US_RIIC
| ATMEL_US_DSRIC
308 | ATMEL_US_DCDIC
| ATMEL_US_CTSIC
);
312 * Control the transmission of a break signal
314 static void atmel_break_ctl(struct uart_port
*port
, int break_state
)
316 if (break_state
!= 0)
317 UART_PUT_CR(port
, ATMEL_US_STTBRK
); /* start break */
319 UART_PUT_CR(port
, ATMEL_US_STPBRK
); /* stop break */
323 * Stores the incoming character in the ring buffer
326 atmel_buffer_rx_char(struct uart_port
*port
, unsigned int status
,
329 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
330 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
331 struct atmel_uart_char
*c
;
333 if (!CIRC_SPACE(ring
->head
, ring
->tail
, ATMEL_SERIAL_RINGSIZE
))
334 /* Buffer overflow, ignore char */
337 c
= &((struct atmel_uart_char
*)ring
->buf
)[ring
->head
];
341 /* Make sure the character is stored before we update head. */
344 ring
->head
= (ring
->head
+ 1) & (ATMEL_SERIAL_RINGSIZE
- 1);
348 * Deal with parity, framing and overrun errors.
350 static void atmel_pdc_rxerr(struct uart_port
*port
, unsigned int status
)
353 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
355 if (status
& ATMEL_US_RXBRK
) {
356 /* ignore side-effect */
357 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
360 if (status
& ATMEL_US_PARE
)
361 port
->icount
.parity
++;
362 if (status
& ATMEL_US_FRAME
)
363 port
->icount
.frame
++;
364 if (status
& ATMEL_US_OVRE
)
365 port
->icount
.overrun
++;
369 * Characters received (called from interrupt handler)
371 static void atmel_rx_chars(struct uart_port
*port
)
373 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
374 unsigned int status
, ch
;
376 status
= UART_GET_CSR(port
);
377 while (status
& ATMEL_US_RXRDY
) {
378 ch
= UART_GET_CHAR(port
);
381 * note that the error handling code is
382 * out of the main execution path
384 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
385 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
)
386 || atmel_port
->break_active
)) {
389 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
391 if (status
& ATMEL_US_RXBRK
392 && !atmel_port
->break_active
) {
393 atmel_port
->break_active
= 1;
394 UART_PUT_IER(port
, ATMEL_US_RXBRK
);
397 * This is either the end-of-break
398 * condition or we've received at
399 * least one character without RXBRK
400 * being set. In both cases, the next
401 * RXBRK will indicate start-of-break.
403 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
404 status
&= ~ATMEL_US_RXBRK
;
405 atmel_port
->break_active
= 0;
409 atmel_buffer_rx_char(port
, status
, ch
);
410 status
= UART_GET_CSR(port
);
413 tasklet_schedule(&atmel_port
->tasklet
);
417 * Transmit characters (called from tasklet with TXRDY interrupt
420 static void atmel_tx_chars(struct uart_port
*port
)
422 struct circ_buf
*xmit
= &port
->info
->xmit
;
424 if (port
->x_char
&& UART_GET_CSR(port
) & ATMEL_US_TXRDY
) {
425 UART_PUT_CHAR(port
, port
->x_char
);
429 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
432 while (UART_GET_CSR(port
) & ATMEL_US_TXRDY
) {
433 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
434 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
436 if (uart_circ_empty(xmit
))
440 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
441 uart_write_wakeup(port
);
443 if (!uart_circ_empty(xmit
))
444 UART_PUT_IER(port
, ATMEL_US_TXRDY
);
448 * receive interrupt handler.
451 atmel_handle_receive(struct uart_port
*port
, unsigned int pending
)
453 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
455 if (atmel_use_dma_rx(port
)) {
457 * PDC receive. Just schedule the tasklet and let it
458 * figure out the details.
460 * TODO: We're not handling error flags correctly at
463 if (pending
& (ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
)) {
464 UART_PUT_IDR(port
, (ATMEL_US_ENDRX
465 | ATMEL_US_TIMEOUT
));
466 tasklet_schedule(&atmel_port
->tasklet
);
469 if (pending
& (ATMEL_US_RXBRK
| ATMEL_US_OVRE
|
470 ATMEL_US_FRAME
| ATMEL_US_PARE
))
471 atmel_pdc_rxerr(port
, pending
);
474 /* Interrupt receive */
475 if (pending
& ATMEL_US_RXRDY
)
476 atmel_rx_chars(port
);
477 else if (pending
& ATMEL_US_RXBRK
) {
479 * End of break detected. If it came along with a
480 * character, atmel_rx_chars will handle it.
482 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
483 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
484 atmel_port
->break_active
= 0;
489 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
492 atmel_handle_transmit(struct uart_port
*port
, unsigned int pending
)
494 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
496 if (atmel_use_dma_tx(port
)) {
498 if (pending
& (ATMEL_US_ENDTX
| ATMEL_US_TXBUFE
)) {
499 UART_PUT_IDR(port
, ATMEL_US_ENDTX
| ATMEL_US_TXBUFE
);
500 tasklet_schedule(&atmel_port
->tasklet
);
503 /* Interrupt transmit */
504 if (pending
& ATMEL_US_TXRDY
) {
505 UART_PUT_IDR(port
, ATMEL_US_TXRDY
);
506 tasklet_schedule(&atmel_port
->tasklet
);
512 * status flags interrupt handler.
515 atmel_handle_status(struct uart_port
*port
, unsigned int pending
,
518 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
520 if (pending
& (ATMEL_US_RIIC
| ATMEL_US_DSRIC
| ATMEL_US_DCDIC
522 atmel_port
->irq_status
= status
;
523 tasklet_schedule(&atmel_port
->tasklet
);
530 static irqreturn_t
atmel_interrupt(int irq
, void *dev_id
)
532 struct uart_port
*port
= dev_id
;
533 unsigned int status
, pending
, pass_counter
= 0;
536 status
= UART_GET_CSR(port
);
537 pending
= status
& UART_GET_IMR(port
);
541 atmel_handle_receive(port
, pending
);
542 atmel_handle_status(port
, pending
, status
);
543 atmel_handle_transmit(port
, pending
);
544 } while (pass_counter
++ < ATMEL_ISR_PASS_LIMIT
);
550 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
552 static void atmel_tx_dma(struct uart_port
*port
)
554 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
555 struct circ_buf
*xmit
= &port
->info
->xmit
;
556 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
559 xmit
->tail
+= pdc
->ofs
;
560 xmit
->tail
&= UART_XMIT_SIZE
- 1;
562 port
->icount
.tx
+= pdc
->ofs
;
565 if (!uart_circ_empty(xmit
)) {
566 /* more to transmit - setup next transfer */
568 /* disable PDC transmit */
569 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
570 dma_sync_single_for_device(port
->dev
,
575 count
= CIRC_CNT_TO_END(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
578 UART_PUT_TPR(port
, pdc
->dma_addr
+ xmit
->tail
);
579 UART_PUT_TCR(port
, count
);
580 /* re-enable PDC transmit and interrupts */
581 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
582 UART_PUT_IER(port
, ATMEL_US_ENDTX
| ATMEL_US_TXBUFE
);
584 /* nothing left to transmit - disable the transmitter */
586 /* disable PDC transmit */
587 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
590 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
591 uart_write_wakeup(port
);
594 static void atmel_rx_from_ring(struct uart_port
*port
)
596 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
597 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
601 while (ring
->head
!= ring
->tail
) {
602 struct atmel_uart_char c
;
604 /* Make sure c is loaded after head. */
607 c
= ((struct atmel_uart_char
*)ring
->buf
)[ring
->tail
];
609 ring
->tail
= (ring
->tail
+ 1) & (ATMEL_SERIAL_RINGSIZE
- 1);
616 * note that the error handling code is
617 * out of the main execution path
619 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
620 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
))) {
621 if (status
& ATMEL_US_RXBRK
) {
622 /* ignore side-effect */
623 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
626 if (uart_handle_break(port
))
629 if (status
& ATMEL_US_PARE
)
630 port
->icount
.parity
++;
631 if (status
& ATMEL_US_FRAME
)
632 port
->icount
.frame
++;
633 if (status
& ATMEL_US_OVRE
)
634 port
->icount
.overrun
++;
636 status
&= port
->read_status_mask
;
638 if (status
& ATMEL_US_RXBRK
)
640 else if (status
& ATMEL_US_PARE
)
642 else if (status
& ATMEL_US_FRAME
)
647 if (uart_handle_sysrq_char(port
, c
.ch
))
650 uart_insert_char(port
, status
, ATMEL_US_OVRE
, c
.ch
, flg
);
654 * Drop the lock here since it might end up calling
655 * uart_start(), which takes the lock.
657 spin_unlock(&port
->lock
);
658 tty_flip_buffer_push(port
->info
->tty
);
659 spin_lock(&port
->lock
);
662 static void atmel_rx_from_dma(struct uart_port
*port
)
664 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
665 struct tty_struct
*tty
= port
->info
->tty
;
666 struct atmel_dma_buffer
*pdc
;
667 int rx_idx
= atmel_port
->pdc_rx_idx
;
673 /* Reset the UART timeout early so that we don't miss one */
674 UART_PUT_CR(port
, ATMEL_US_STTTO
);
676 pdc
= &atmel_port
->pdc_rx
[rx_idx
];
677 head
= UART_GET_RPR(port
) - pdc
->dma_addr
;
680 /* If the PDC has switched buffers, RPR won't contain
681 * any address within the current buffer. Since head
682 * is unsigned, we just need a one-way comparison to
685 * In this case, we just need to consume the entire
686 * buffer and resubmit it for DMA. This will clear the
687 * ENDRX bit as well, so that we can safely re-enable
688 * all interrupts below.
690 head
= min(head
, pdc
->dma_size
);
692 if (likely(head
!= tail
)) {
693 dma_sync_single_for_cpu(port
->dev
, pdc
->dma_addr
,
694 pdc
->dma_size
, DMA_FROM_DEVICE
);
697 * head will only wrap around when we recycle
698 * the DMA buffer, and when that happens, we
699 * explicitly set tail to 0. So head will
700 * always be greater than tail.
704 tty_insert_flip_string(tty
, pdc
->buf
+ pdc
->ofs
, count
);
706 dma_sync_single_for_device(port
->dev
, pdc
->dma_addr
,
707 pdc
->dma_size
, DMA_FROM_DEVICE
);
709 port
->icount
.rx
+= count
;
714 * If the current buffer is full, we need to check if
715 * the next one contains any additional data.
717 if (head
>= pdc
->dma_size
) {
719 UART_PUT_RNPR(port
, pdc
->dma_addr
);
720 UART_PUT_RNCR(port
, pdc
->dma_size
);
723 atmel_port
->pdc_rx_idx
= rx_idx
;
725 } while (head
>= pdc
->dma_size
);
728 * Drop the lock here since it might end up calling
729 * uart_start(), which takes the lock.
731 spin_unlock(&port
->lock
);
732 tty_flip_buffer_push(tty
);
733 spin_lock(&port
->lock
);
735 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
739 * tasklet handling tty stuff outside the interrupt handler.
741 static void atmel_tasklet_func(unsigned long data
)
743 struct uart_port
*port
= (struct uart_port
*)data
;
744 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
746 unsigned int status_change
;
748 /* The interrupt handler does not take the lock */
749 spin_lock(&port
->lock
);
751 if (atmel_use_dma_tx(port
))
754 atmel_tx_chars(port
);
756 status
= atmel_port
->irq_status
;
757 status_change
= status
^ atmel_port
->irq_status_prev
;
759 if (status_change
& (ATMEL_US_RI
| ATMEL_US_DSR
760 | ATMEL_US_DCD
| ATMEL_US_CTS
)) {
761 /* TODO: All reads to CSR will clear these interrupts! */
762 if (status_change
& ATMEL_US_RI
)
764 if (status_change
& ATMEL_US_DSR
)
766 if (status_change
& ATMEL_US_DCD
)
767 uart_handle_dcd_change(port
, !(status
& ATMEL_US_DCD
));
768 if (status_change
& ATMEL_US_CTS
)
769 uart_handle_cts_change(port
, !(status
& ATMEL_US_CTS
));
771 wake_up_interruptible(&port
->info
->delta_msr_wait
);
773 atmel_port
->irq_status_prev
= status
;
776 if (atmel_use_dma_rx(port
))
777 atmel_rx_from_dma(port
);
779 atmel_rx_from_ring(port
);
781 spin_unlock(&port
->lock
);
785 * Perform initialization and enable port for reception
787 static int atmel_startup(struct uart_port
*port
)
789 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
793 * Ensure that no interrupts are enabled otherwise when
794 * request_irq() is called we could get stuck trying to
795 * handle an unexpected interrupt
797 UART_PUT_IDR(port
, -1);
802 retval
= request_irq(port
->irq
, atmel_interrupt
, IRQF_SHARED
,
803 "atmel_serial", port
);
805 printk("atmel_serial: atmel_startup - Can't get irq\n");
810 * Initialize DMA (if necessary)
812 if (atmel_use_dma_rx(port
)) {
815 for (i
= 0; i
< 2; i
++) {
816 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_rx
[i
];
818 pdc
->buf
= kmalloc(PDC_BUFFER_SIZE
, GFP_KERNEL
);
819 if (pdc
->buf
== NULL
) {
821 dma_unmap_single(port
->dev
,
822 atmel_port
->pdc_rx
[0].dma_addr
,
825 kfree(atmel_port
->pdc_rx
[0].buf
);
827 free_irq(port
->irq
, port
);
830 pdc
->dma_addr
= dma_map_single(port
->dev
,
834 pdc
->dma_size
= PDC_BUFFER_SIZE
;
838 atmel_port
->pdc_rx_idx
= 0;
840 UART_PUT_RPR(port
, atmel_port
->pdc_rx
[0].dma_addr
);
841 UART_PUT_RCR(port
, PDC_BUFFER_SIZE
);
843 UART_PUT_RNPR(port
, atmel_port
->pdc_rx
[1].dma_addr
);
844 UART_PUT_RNCR(port
, PDC_BUFFER_SIZE
);
846 if (atmel_use_dma_tx(port
)) {
847 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
848 struct circ_buf
*xmit
= &port
->info
->xmit
;
850 pdc
->buf
= xmit
->buf
;
851 pdc
->dma_addr
= dma_map_single(port
->dev
,
855 pdc
->dma_size
= UART_XMIT_SIZE
;
860 * If there is a specific "open" function (to register
861 * control line interrupts)
863 if (atmel_open_hook
) {
864 retval
= atmel_open_hook(port
);
866 free_irq(port
->irq
, port
);
872 * Finally, enable the serial port
874 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
875 /* enable xmit & rcvr */
876 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
878 if (atmel_use_dma_rx(port
)) {
879 /* set UART timeout */
880 UART_PUT_RTOR(port
, PDC_RX_TIMEOUT
);
881 UART_PUT_CR(port
, ATMEL_US_STTTO
);
883 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
884 /* enable PDC controller */
885 UART_PUT_PTCR(port
, ATMEL_PDC_RXTEN
);
887 /* enable receive only */
888 UART_PUT_IER(port
, ATMEL_US_RXRDY
);
897 static void atmel_shutdown(struct uart_port
*port
)
899 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
901 * Ensure everything is stopped.
909 if (atmel_use_dma_rx(port
)) {
912 for (i
= 0; i
< 2; i
++) {
913 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_rx
[i
];
915 dma_unmap_single(port
->dev
,
922 if (atmel_use_dma_tx(port
)) {
923 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
925 dma_unmap_single(port
->dev
,
932 * Disable all interrupts, port and break condition.
934 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
935 UART_PUT_IDR(port
, -1);
940 free_irq(port
->irq
, port
);
943 * If there is a specific "close" function (to unregister
944 * control line interrupts)
946 if (atmel_close_hook
)
947 atmel_close_hook(port
);
951 * Power / Clock management.
953 static void atmel_serial_pm(struct uart_port
*port
, unsigned int state
,
954 unsigned int oldstate
)
956 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
961 * Enable the peripheral clock for this serial port.
962 * This is called on uart_open() or a resume event.
964 clk_enable(atmel_port
->clk
);
968 * Disable the peripheral clock for this serial port.
969 * This is called on uart_close() or a suspend event.
971 clk_disable(atmel_port
->clk
);
974 printk(KERN_ERR
"atmel_serial: unknown pm %d\n", state
);
979 * Change the port parameters
981 static void atmel_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
982 struct ktermios
*old
)
985 unsigned int mode
, imr
, quot
, baud
;
987 /* Get current mode register */
988 mode
= UART_GET_MR(port
) & ~(ATMEL_US_USCLKS
| ATMEL_US_CHRL
989 | ATMEL_US_NBSTOP
| ATMEL_US_PAR
);
991 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
992 quot
= uart_get_divisor(port
, baud
);
994 if (quot
> 65535) { /* BRGR is 16-bit, so switch to slower clock */
996 mode
|= ATMEL_US_USCLKS_MCK_DIV8
;
1000 switch (termios
->c_cflag
& CSIZE
) {
1002 mode
|= ATMEL_US_CHRL_5
;
1005 mode
|= ATMEL_US_CHRL_6
;
1008 mode
|= ATMEL_US_CHRL_7
;
1011 mode
|= ATMEL_US_CHRL_8
;
1016 if (termios
->c_cflag
& CSTOPB
)
1017 mode
|= ATMEL_US_NBSTOP_2
;
1020 if (termios
->c_cflag
& PARENB
) {
1021 /* Mark or Space parity */
1022 if (termios
->c_cflag
& CMSPAR
) {
1023 if (termios
->c_cflag
& PARODD
)
1024 mode
|= ATMEL_US_PAR_MARK
;
1026 mode
|= ATMEL_US_PAR_SPACE
;
1027 } else if (termios
->c_cflag
& PARODD
)
1028 mode
|= ATMEL_US_PAR_ODD
;
1030 mode
|= ATMEL_US_PAR_EVEN
;
1032 mode
|= ATMEL_US_PAR_NONE
;
1034 spin_lock_irqsave(&port
->lock
, flags
);
1036 port
->read_status_mask
= ATMEL_US_OVRE
;
1037 if (termios
->c_iflag
& INPCK
)
1038 port
->read_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
1039 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
1040 port
->read_status_mask
|= ATMEL_US_RXBRK
;
1042 if (atmel_use_dma_rx(port
))
1043 /* need to enable error interrupts */
1044 UART_PUT_IER(port
, port
->read_status_mask
);
1047 * Characters to ignore
1049 port
->ignore_status_mask
= 0;
1050 if (termios
->c_iflag
& IGNPAR
)
1051 port
->ignore_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
1052 if (termios
->c_iflag
& IGNBRK
) {
1053 port
->ignore_status_mask
|= ATMEL_US_RXBRK
;
1055 * If we're ignoring parity and break indicators,
1056 * ignore overruns too (for real raw support).
1058 if (termios
->c_iflag
& IGNPAR
)
1059 port
->ignore_status_mask
|= ATMEL_US_OVRE
;
1061 /* TODO: Ignore all characters if CREAD is set.*/
1063 /* update the per-port timeout */
1064 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1066 /* save/disable interrupts and drain transmitter */
1067 imr
= UART_GET_IMR(port
);
1068 UART_PUT_IDR(port
, -1);
1069 while (!(UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
))
1072 /* disable receiver and transmitter */
1073 UART_PUT_CR(port
, ATMEL_US_TXDIS
| ATMEL_US_RXDIS
);
1075 /* set the parity, stop bits and data size */
1076 UART_PUT_MR(port
, mode
);
1078 /* set the baud rate */
1079 UART_PUT_BRGR(port
, quot
);
1080 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
1081 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
1083 /* restore interrupts */
1084 UART_PUT_IER(port
, imr
);
1086 /* CTS flow-control and modem-status interrupts */
1087 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
1088 port
->ops
->enable_ms(port
);
1090 spin_unlock_irqrestore(&port
->lock
, flags
);
1094 * Return string describing the specified port
1096 static const char *atmel_type(struct uart_port
*port
)
1098 return (port
->type
== PORT_ATMEL
) ? "ATMEL_SERIAL" : NULL
;
1102 * Release the memory region(s) being used by 'port'.
1104 static void atmel_release_port(struct uart_port
*port
)
1106 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1107 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
1109 release_mem_region(port
->mapbase
, size
);
1111 if (port
->flags
& UPF_IOREMAP
) {
1112 iounmap(port
->membase
);
1113 port
->membase
= NULL
;
1118 * Request the memory region(s) being used by 'port'.
1120 static int atmel_request_port(struct uart_port
*port
)
1122 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1123 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
1125 if (!request_mem_region(port
->mapbase
, size
, "atmel_serial"))
1128 if (port
->flags
& UPF_IOREMAP
) {
1129 port
->membase
= ioremap(port
->mapbase
, size
);
1130 if (port
->membase
== NULL
) {
1131 release_mem_region(port
->mapbase
, size
);
1140 * Configure/autoconfigure the port.
1142 static void atmel_config_port(struct uart_port
*port
, int flags
)
1144 if (flags
& UART_CONFIG_TYPE
) {
1145 port
->type
= PORT_ATMEL
;
1146 atmel_request_port(port
);
1151 * Verify the new serial_struct (for TIOCSSERIAL).
1153 static int atmel_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1156 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_ATMEL
)
1158 if (port
->irq
!= ser
->irq
)
1160 if (ser
->io_type
!= SERIAL_IO_MEM
)
1162 if (port
->uartclk
/ 16 != ser
->baud_base
)
1164 if ((void *)port
->mapbase
!= ser
->iomem_base
)
1166 if (port
->iobase
!= ser
->port
)
1173 static struct uart_ops atmel_pops
= {
1174 .tx_empty
= atmel_tx_empty
,
1175 .set_mctrl
= atmel_set_mctrl
,
1176 .get_mctrl
= atmel_get_mctrl
,
1177 .stop_tx
= atmel_stop_tx
,
1178 .start_tx
= atmel_start_tx
,
1179 .stop_rx
= atmel_stop_rx
,
1180 .enable_ms
= atmel_enable_ms
,
1181 .break_ctl
= atmel_break_ctl
,
1182 .startup
= atmel_startup
,
1183 .shutdown
= atmel_shutdown
,
1184 .set_termios
= atmel_set_termios
,
1186 .release_port
= atmel_release_port
,
1187 .request_port
= atmel_request_port
,
1188 .config_port
= atmel_config_port
,
1189 .verify_port
= atmel_verify_port
,
1190 .pm
= atmel_serial_pm
,
1194 * Configure the port from the platform device resource info.
1196 static void __devinit
atmel_init_port(struct atmel_uart_port
*atmel_port
,
1197 struct platform_device
*pdev
)
1199 struct uart_port
*port
= &atmel_port
->uart
;
1200 struct atmel_uart_data
*data
= pdev
->dev
.platform_data
;
1202 port
->iotype
= UPIO_MEM
;
1203 port
->flags
= UPF_BOOT_AUTOCONF
;
1204 port
->ops
= &atmel_pops
;
1206 port
->line
= pdev
->id
;
1207 port
->dev
= &pdev
->dev
;
1209 port
->mapbase
= pdev
->resource
[0].start
;
1210 port
->irq
= pdev
->resource
[1].start
;
1212 tasklet_init(&atmel_port
->tasklet
, atmel_tasklet_func
,
1213 (unsigned long)port
);
1215 memset(&atmel_port
->rx_ring
, 0, sizeof(atmel_port
->rx_ring
));
1218 /* Already mapped by setup code */
1219 port
->membase
= data
->regs
;
1221 port
->flags
|= UPF_IOREMAP
;
1222 port
->membase
= NULL
;
1225 /* for console, the clock could already be configured */
1226 if (!atmel_port
->clk
) {
1227 atmel_port
->clk
= clk_get(&pdev
->dev
, "usart");
1228 clk_enable(atmel_port
->clk
);
1229 port
->uartclk
= clk_get_rate(atmel_port
->clk
);
1232 atmel_port
->use_dma_rx
= data
->use_dma_rx
;
1233 atmel_port
->use_dma_tx
= data
->use_dma_tx
;
1234 if (atmel_use_dma_tx(port
))
1235 port
->fifosize
= PDC_BUFFER_SIZE
;
1239 * Register board-specific modem-control line handlers.
1241 void __init
atmel_register_uart_fns(struct atmel_port_fns
*fns
)
1244 atmel_pops
.enable_ms
= fns
->enable_ms
;
1246 atmel_pops
.get_mctrl
= fns
->get_mctrl
;
1248 atmel_pops
.set_mctrl
= fns
->set_mctrl
;
1249 atmel_open_hook
= fns
->open
;
1250 atmel_close_hook
= fns
->close
;
1251 atmel_pops
.pm
= fns
->pm
;
1252 atmel_pops
.set_wake
= fns
->set_wake
;
1255 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1256 static void atmel_console_putchar(struct uart_port
*port
, int ch
)
1258 while (!(UART_GET_CSR(port
) & ATMEL_US_TXRDY
))
1260 UART_PUT_CHAR(port
, ch
);
1264 * Interrupts are disabled on entering
1266 static void atmel_console_write(struct console
*co
, const char *s
, u_int count
)
1268 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
1269 unsigned int status
, imr
;
1272 * First, save IMR and then disable interrupts
1274 imr
= UART_GET_IMR(port
);
1275 UART_PUT_IDR(port
, ATMEL_US_RXRDY
| ATMEL_US_TXRDY
);
1277 uart_console_write(port
, s
, count
, atmel_console_putchar
);
1280 * Finally, wait for transmitter to become empty
1284 status
= UART_GET_CSR(port
);
1285 } while (!(status
& ATMEL_US_TXRDY
));
1286 /* set interrupts back the way they were */
1287 UART_PUT_IER(port
, imr
);
1291 * If the port was already initialised (eg, by a boot loader),
1292 * try to determine the current setup.
1294 static void __init
atmel_console_get_options(struct uart_port
*port
, int *baud
,
1295 int *parity
, int *bits
)
1297 unsigned int mr
, quot
;
1300 * If the baud rate generator isn't running, the port wasn't
1301 * initialized by the boot loader.
1303 quot
= UART_GET_BRGR(port
);
1307 mr
= UART_GET_MR(port
) & ATMEL_US_CHRL
;
1308 if (mr
== ATMEL_US_CHRL_8
)
1313 mr
= UART_GET_MR(port
) & ATMEL_US_PAR
;
1314 if (mr
== ATMEL_US_PAR_EVEN
)
1316 else if (mr
== ATMEL_US_PAR_ODD
)
1320 * The serial core only rounds down when matching this to a
1321 * supported baud rate. Make sure we don't end up slightly
1322 * lower than one of those, as it would make us fall through
1323 * to a much lower baud rate than we really want.
1325 *baud
= port
->uartclk
/ (16 * (quot
- 1));
1328 static int __init
atmel_console_setup(struct console
*co
, char *options
)
1330 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
1336 if (port
->membase
== NULL
) {
1337 /* Port not initialized yet - delay setup */
1341 UART_PUT_IDR(port
, -1);
1342 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
1343 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
1346 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1348 atmel_console_get_options(port
, &baud
, &parity
, &bits
);
1350 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1353 static struct uart_driver atmel_uart
;
1355 static struct console atmel_console
= {
1356 .name
= ATMEL_DEVICENAME
,
1357 .write
= atmel_console_write
,
1358 .device
= uart_console_device
,
1359 .setup
= atmel_console_setup
,
1360 .flags
= CON_PRINTBUFFER
,
1362 .data
= &atmel_uart
,
1365 #define ATMEL_CONSOLE_DEVICE &atmel_console
1368 * Early console initialization (before VM subsystem initialized).
1370 static int __init
atmel_console_init(void)
1372 if (atmel_default_console_device
) {
1373 add_preferred_console(ATMEL_DEVICENAME
,
1374 atmel_default_console_device
->id
, NULL
);
1375 atmel_init_port(&atmel_ports
[atmel_default_console_device
->id
],
1376 atmel_default_console_device
);
1377 register_console(&atmel_console
);
1383 console_initcall(atmel_console_init
);
1386 * Late console initialization.
1388 static int __init
atmel_late_console_init(void)
1390 if (atmel_default_console_device
1391 && !(atmel_console
.flags
& CON_ENABLED
))
1392 register_console(&atmel_console
);
1397 core_initcall(atmel_late_console_init
);
1399 static inline bool atmel_is_console_port(struct uart_port
*port
)
1401 return port
->cons
&& port
->cons
->index
== port
->line
;
1405 #define ATMEL_CONSOLE_DEVICE NULL
1407 static inline bool atmel_is_console_port(struct uart_port
*port
)
1413 static struct uart_driver atmel_uart
= {
1414 .owner
= THIS_MODULE
,
1415 .driver_name
= "atmel_serial",
1416 .dev_name
= ATMEL_DEVICENAME
,
1417 .major
= SERIAL_ATMEL_MAJOR
,
1418 .minor
= MINOR_START
,
1419 .nr
= ATMEL_MAX_UART
,
1420 .cons
= ATMEL_CONSOLE_DEVICE
,
1424 static int atmel_serial_suspend(struct platform_device
*pdev
,
1427 struct uart_port
*port
= platform_get_drvdata(pdev
);
1428 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
1430 if (device_may_wakeup(&pdev
->dev
)
1431 && !at91_suspend_entering_slow_clock())
1432 enable_irq_wake(port
->irq
);
1434 uart_suspend_port(&atmel_uart
, port
);
1435 atmel_port
->suspended
= 1;
1441 static int atmel_serial_resume(struct platform_device
*pdev
)
1443 struct uart_port
*port
= platform_get_drvdata(pdev
);
1444 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
1446 if (atmel_port
->suspended
) {
1447 uart_resume_port(&atmel_uart
, port
);
1448 atmel_port
->suspended
= 0;
1450 disable_irq_wake(port
->irq
);
1455 #define atmel_serial_suspend NULL
1456 #define atmel_serial_resume NULL
1459 static int __devinit
atmel_serial_probe(struct platform_device
*pdev
)
1461 struct atmel_uart_port
*port
;
1465 BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE
));
1467 port
= &atmel_ports
[pdev
->id
];
1468 atmel_init_port(port
, pdev
);
1470 if (!atmel_use_dma_rx(&port
->uart
)) {
1472 data
= kmalloc(sizeof(struct atmel_uart_char
)
1473 * ATMEL_SERIAL_RINGSIZE
, GFP_KERNEL
);
1475 goto err_alloc_ring
;
1476 port
->rx_ring
.buf
= data
;
1479 ret
= uart_add_one_port(&atmel_uart
, &port
->uart
);
1483 device_init_wakeup(&pdev
->dev
, 1);
1484 platform_set_drvdata(pdev
, port
);
1489 kfree(port
->rx_ring
.buf
);
1490 port
->rx_ring
.buf
= NULL
;
1492 if (!atmel_is_console_port(&port
->uart
)) {
1493 clk_disable(port
->clk
);
1501 static int __devexit
atmel_serial_remove(struct platform_device
*pdev
)
1503 struct uart_port
*port
= platform_get_drvdata(pdev
);
1504 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
1507 device_init_wakeup(&pdev
->dev
, 0);
1508 platform_set_drvdata(pdev
, NULL
);
1510 ret
= uart_remove_one_port(&atmel_uart
, port
);
1512 tasklet_kill(&atmel_port
->tasklet
);
1513 kfree(atmel_port
->rx_ring
.buf
);
1515 /* "port" is allocated statically, so we shouldn't free it */
1517 clk_disable(atmel_port
->clk
);
1518 clk_put(atmel_port
->clk
);
1523 static struct platform_driver atmel_serial_driver
= {
1524 .probe
= atmel_serial_probe
,
1525 .remove
= __devexit_p(atmel_serial_remove
),
1526 .suspend
= atmel_serial_suspend
,
1527 .resume
= atmel_serial_resume
,
1529 .name
= "atmel_usart",
1530 .owner
= THIS_MODULE
,
1534 static int __init
atmel_serial_init(void)
1538 ret
= uart_register_driver(&atmel_uart
);
1542 ret
= platform_driver_register(&atmel_serial_driver
);
1544 uart_unregister_driver(&atmel_uart
);
1549 static void __exit
atmel_serial_exit(void)
1551 platform_driver_unregister(&atmel_serial_driver
);
1552 uart_unregister_driver(&atmel_uart
);
1555 module_init(atmel_serial_init
);
1556 module_exit(atmel_serial_exit
);
1558 MODULE_AUTHOR("Rick Bronson");
1559 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1560 MODULE_LICENSE("GPL");