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>
41 #include <linux/uaccess.h>
44 #include <asm/ioctls.h>
46 #include <asm/mach/serial_at91.h>
47 #include <mach/board.h>
51 #include <mach/gpio.h>
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 */
58 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
62 #include <linux/serial_core.h>
64 static void atmel_start_rx(struct uart_port
*port
);
65 static void atmel_stop_rx(struct uart_port
*port
);
67 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
69 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
70 * should coexist with the 8250 driver, such as if we have an external 16C550
72 #define SERIAL_ATMEL_MAJOR 204
73 #define MINOR_START 154
74 #define ATMEL_DEVICENAME "ttyAT"
78 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
79 * name, but it is legally reserved for the 8250 driver. */
80 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
81 #define MINOR_START 64
82 #define ATMEL_DEVICENAME "ttyS"
86 #define ATMEL_ISR_PASS_LIMIT 256
88 /* UART registers. CR is write-only, hence no GET macro */
89 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
90 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
91 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
92 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
93 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
94 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
95 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
96 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
97 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
98 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
99 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
100 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
101 #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
104 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
105 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
107 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
108 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
109 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
110 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
111 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
113 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
114 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
115 #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
117 static int (*atmel_open_hook
)(struct uart_port
*);
118 static void (*atmel_close_hook
)(struct uart_port
*);
120 struct atmel_dma_buffer
{
123 unsigned int dma_size
;
127 struct atmel_uart_char
{
132 #define ATMEL_SERIAL_RINGSIZE 1024
135 * We wrap our port structure around the generic uart_port.
137 struct atmel_uart_port
{
138 struct uart_port uart
; /* uart */
139 struct clk
*clk
; /* uart clock */
140 int may_wakeup
; /* cached value of device_may_wakeup for times we need to disable it */
141 u32 backup_imr
; /* IMR saved during suspend */
142 int break_active
; /* break being received */
144 short use_dma_rx
; /* enable PDC receiver */
145 short pdc_rx_idx
; /* current PDC RX buffer */
146 struct atmel_dma_buffer pdc_rx
[2]; /* PDC receier */
148 short use_dma_tx
; /* enable PDC transmitter */
149 struct atmel_dma_buffer pdc_tx
; /* PDC transmitter */
151 struct tasklet_struct tasklet
;
152 unsigned int irq_status
;
153 unsigned int irq_status_prev
;
155 struct circ_buf rx_ring
;
157 struct serial_rs485 rs485
; /* rs485 settings */
158 unsigned int tx_done_mask
;
161 static struct atmel_uart_port atmel_ports
[ATMEL_MAX_UART
];
164 static struct console atmel_console
;
167 static inline struct atmel_uart_port
*
168 to_atmel_uart_port(struct uart_port
*uart
)
170 return container_of(uart
, struct atmel_uart_port
, uart
);
173 #ifdef CONFIG_SERIAL_ATMEL_PDC
174 static bool atmel_use_dma_rx(struct uart_port
*port
)
176 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
178 return atmel_port
->use_dma_rx
;
181 static bool atmel_use_dma_tx(struct uart_port
*port
)
183 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
185 return atmel_port
->use_dma_tx
;
188 static bool atmel_use_dma_rx(struct uart_port
*port
)
193 static bool atmel_use_dma_tx(struct uart_port
*port
)
199 /* Enable or disable the rs485 support */
200 void atmel_config_rs485(struct uart_port
*port
, struct serial_rs485
*rs485conf
)
202 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
205 spin_lock(&port
->lock
);
207 /* Disable interrupts */
208 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
210 mode
= UART_GET_MR(port
);
212 /* Resetting serial mode to RS232 (0x0) */
213 mode
&= ~ATMEL_US_USMODE
;
215 atmel_port
->rs485
= *rs485conf
;
217 if (rs485conf
->flags
& SER_RS485_ENABLED
) {
218 dev_dbg(port
->dev
, "Setting UART to RS485\n");
219 atmel_port
->tx_done_mask
= ATMEL_US_TXEMPTY
;
220 UART_PUT_TTGR(port
, rs485conf
->delay_rts_before_send
);
221 mode
|= ATMEL_US_USMODE_RS485
;
223 dev_dbg(port
->dev
, "Setting UART to RS232\n");
224 if (atmel_use_dma_tx(port
))
225 atmel_port
->tx_done_mask
= ATMEL_US_ENDTX
|
228 atmel_port
->tx_done_mask
= ATMEL_US_TXRDY
;
230 UART_PUT_MR(port
, mode
);
232 /* Enable interrupts */
233 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
235 spin_unlock(&port
->lock
);
240 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
242 static u_int
atmel_tx_empty(struct uart_port
*port
)
244 return (UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
) ? TIOCSER_TEMT
: 0;
248 * Set state of the modem control output lines
250 static void atmel_set_mctrl(struct uart_port
*port
, u_int mctrl
)
252 unsigned int control
= 0;
254 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
256 #ifdef CONFIG_ARCH_AT91RM9200
257 if (cpu_is_at91rm9200()) {
259 * AT91RM9200 Errata #39: RTS0 is not internally connected
260 * to PA21. We need to drive the pin manually.
262 if (port
->mapbase
== AT91RM9200_BASE_US0
) {
263 if (mctrl
& TIOCM_RTS
)
264 at91_set_gpio_value(AT91_PIN_PA21
, 0);
266 at91_set_gpio_value(AT91_PIN_PA21
, 1);
271 if (mctrl
& TIOCM_RTS
)
272 control
|= ATMEL_US_RTSEN
;
274 control
|= ATMEL_US_RTSDIS
;
276 if (mctrl
& TIOCM_DTR
)
277 control
|= ATMEL_US_DTREN
;
279 control
|= ATMEL_US_DTRDIS
;
281 UART_PUT_CR(port
, control
);
283 /* Local loopback mode? */
284 mode
= UART_GET_MR(port
) & ~ATMEL_US_CHMODE
;
285 if (mctrl
& TIOCM_LOOP
)
286 mode
|= ATMEL_US_CHMODE_LOC_LOOP
;
288 mode
|= ATMEL_US_CHMODE_NORMAL
;
290 /* Resetting serial mode to RS232 (0x0) */
291 mode
&= ~ATMEL_US_USMODE
;
293 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
294 dev_dbg(port
->dev
, "Setting UART to RS485\n");
295 UART_PUT_TTGR(port
, atmel_port
->rs485
.delay_rts_before_send
);
296 mode
|= ATMEL_US_USMODE_RS485
;
298 dev_dbg(port
->dev
, "Setting UART to RS232\n");
300 UART_PUT_MR(port
, mode
);
304 * Get state of the modem control input lines
306 static u_int
atmel_get_mctrl(struct uart_port
*port
)
308 unsigned int status
, ret
= 0;
310 status
= UART_GET_CSR(port
);
313 * The control signals are active low.
315 if (!(status
& ATMEL_US_DCD
))
317 if (!(status
& ATMEL_US_CTS
))
319 if (!(status
& ATMEL_US_DSR
))
321 if (!(status
& ATMEL_US_RI
))
330 static void atmel_stop_tx(struct uart_port
*port
)
332 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
334 if (atmel_use_dma_tx(port
)) {
335 /* disable PDC transmit */
336 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
338 /* Disable interrupts */
339 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
341 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
)
342 atmel_start_rx(port
);
346 * Start transmitting.
348 static void atmel_start_tx(struct uart_port
*port
)
350 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
352 if (atmel_use_dma_tx(port
)) {
353 if (UART_GET_PTSR(port
) & ATMEL_PDC_TXTEN
)
354 /* The transmitter is already running. Yes, we
358 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
)
361 /* re-enable PDC transmit */
362 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
364 /* Enable interrupts */
365 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
369 * start receiving - port is in process of being opened.
371 static void atmel_start_rx(struct uart_port
*port
)
373 UART_PUT_CR(port
, ATMEL_US_RSTSTA
); /* reset status and receiver */
375 if (atmel_use_dma_rx(port
)) {
376 /* enable PDC controller */
377 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
|
378 port
->read_status_mask
);
379 UART_PUT_PTCR(port
, ATMEL_PDC_RXTEN
);
381 UART_PUT_IER(port
, ATMEL_US_RXRDY
);
386 * Stop receiving - port is in process of being closed.
388 static void atmel_stop_rx(struct uart_port
*port
)
390 if (atmel_use_dma_rx(port
)) {
391 /* disable PDC receive */
392 UART_PUT_PTCR(port
, ATMEL_PDC_RXTDIS
);
393 UART_PUT_IDR(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
|
394 port
->read_status_mask
);
396 UART_PUT_IDR(port
, ATMEL_US_RXRDY
);
401 * Enable modem status interrupts
403 static void atmel_enable_ms(struct uart_port
*port
)
405 UART_PUT_IER(port
, ATMEL_US_RIIC
| ATMEL_US_DSRIC
406 | ATMEL_US_DCDIC
| ATMEL_US_CTSIC
);
410 * Control the transmission of a break signal
412 static void atmel_break_ctl(struct uart_port
*port
, int break_state
)
414 if (break_state
!= 0)
415 UART_PUT_CR(port
, ATMEL_US_STTBRK
); /* start break */
417 UART_PUT_CR(port
, ATMEL_US_STPBRK
); /* stop break */
421 * Stores the incoming character in the ring buffer
424 atmel_buffer_rx_char(struct uart_port
*port
, unsigned int status
,
427 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
428 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
429 struct atmel_uart_char
*c
;
431 if (!CIRC_SPACE(ring
->head
, ring
->tail
, ATMEL_SERIAL_RINGSIZE
))
432 /* Buffer overflow, ignore char */
435 c
= &((struct atmel_uart_char
*)ring
->buf
)[ring
->head
];
439 /* Make sure the character is stored before we update head. */
442 ring
->head
= (ring
->head
+ 1) & (ATMEL_SERIAL_RINGSIZE
- 1);
446 * Deal with parity, framing and overrun errors.
448 static void atmel_pdc_rxerr(struct uart_port
*port
, unsigned int status
)
451 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
453 if (status
& ATMEL_US_RXBRK
) {
454 /* ignore side-effect */
455 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
458 if (status
& ATMEL_US_PARE
)
459 port
->icount
.parity
++;
460 if (status
& ATMEL_US_FRAME
)
461 port
->icount
.frame
++;
462 if (status
& ATMEL_US_OVRE
)
463 port
->icount
.overrun
++;
467 * Characters received (called from interrupt handler)
469 static void atmel_rx_chars(struct uart_port
*port
)
471 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
472 unsigned int status
, ch
;
474 status
= UART_GET_CSR(port
);
475 while (status
& ATMEL_US_RXRDY
) {
476 ch
= UART_GET_CHAR(port
);
479 * note that the error handling code is
480 * out of the main execution path
482 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
483 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
)
484 || atmel_port
->break_active
)) {
487 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
489 if (status
& ATMEL_US_RXBRK
490 && !atmel_port
->break_active
) {
491 atmel_port
->break_active
= 1;
492 UART_PUT_IER(port
, ATMEL_US_RXBRK
);
495 * This is either the end-of-break
496 * condition or we've received at
497 * least one character without RXBRK
498 * being set. In both cases, the next
499 * RXBRK will indicate start-of-break.
501 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
502 status
&= ~ATMEL_US_RXBRK
;
503 atmel_port
->break_active
= 0;
507 atmel_buffer_rx_char(port
, status
, ch
);
508 status
= UART_GET_CSR(port
);
511 tasklet_schedule(&atmel_port
->tasklet
);
515 * Transmit characters (called from tasklet with TXRDY interrupt
518 static void atmel_tx_chars(struct uart_port
*port
)
520 struct circ_buf
*xmit
= &port
->state
->xmit
;
521 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
523 if (port
->x_char
&& UART_GET_CSR(port
) & atmel_port
->tx_done_mask
) {
524 UART_PUT_CHAR(port
, port
->x_char
);
528 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
531 while (UART_GET_CSR(port
) & atmel_port
->tx_done_mask
) {
532 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
533 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
535 if (uart_circ_empty(xmit
))
539 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
540 uart_write_wakeup(port
);
542 if (!uart_circ_empty(xmit
))
543 /* Enable interrupts */
544 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
548 * receive interrupt handler.
551 atmel_handle_receive(struct uart_port
*port
, unsigned int pending
)
553 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
555 if (atmel_use_dma_rx(port
)) {
557 * PDC receive. Just schedule the tasklet and let it
558 * figure out the details.
560 * TODO: We're not handling error flags correctly at
563 if (pending
& (ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
)) {
564 UART_PUT_IDR(port
, (ATMEL_US_ENDRX
565 | ATMEL_US_TIMEOUT
));
566 tasklet_schedule(&atmel_port
->tasklet
);
569 if (pending
& (ATMEL_US_RXBRK
| ATMEL_US_OVRE
|
570 ATMEL_US_FRAME
| ATMEL_US_PARE
))
571 atmel_pdc_rxerr(port
, pending
);
574 /* Interrupt receive */
575 if (pending
& ATMEL_US_RXRDY
)
576 atmel_rx_chars(port
);
577 else if (pending
& ATMEL_US_RXBRK
) {
579 * End of break detected. If it came along with a
580 * character, atmel_rx_chars will handle it.
582 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
583 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
584 atmel_port
->break_active
= 0;
589 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
592 atmel_handle_transmit(struct uart_port
*port
, unsigned int pending
)
594 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
596 if (pending
& atmel_port
->tx_done_mask
) {
597 /* Either PDC or interrupt transmission */
598 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
599 tasklet_schedule(&atmel_port
->tasklet
);
604 * status flags interrupt handler.
607 atmel_handle_status(struct uart_port
*port
, unsigned int pending
,
610 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
612 if (pending
& (ATMEL_US_RIIC
| ATMEL_US_DSRIC
| ATMEL_US_DCDIC
614 atmel_port
->irq_status
= status
;
615 tasklet_schedule(&atmel_port
->tasklet
);
622 static irqreturn_t
atmel_interrupt(int irq
, void *dev_id
)
624 struct uart_port
*port
= dev_id
;
625 unsigned int status
, pending
, pass_counter
= 0;
628 status
= UART_GET_CSR(port
);
629 pending
= status
& UART_GET_IMR(port
);
633 atmel_handle_receive(port
, pending
);
634 atmel_handle_status(port
, pending
, status
);
635 atmel_handle_transmit(port
, pending
);
636 } while (pass_counter
++ < ATMEL_ISR_PASS_LIMIT
);
638 return pass_counter
? IRQ_HANDLED
: IRQ_NONE
;
642 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
644 static void atmel_tx_dma(struct uart_port
*port
)
646 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
647 struct circ_buf
*xmit
= &port
->state
->xmit
;
648 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
651 /* nothing left to transmit? */
652 if (UART_GET_TCR(port
))
655 xmit
->tail
+= pdc
->ofs
;
656 xmit
->tail
&= UART_XMIT_SIZE
- 1;
658 port
->icount
.tx
+= pdc
->ofs
;
661 /* more to transmit - setup next transfer */
663 /* disable PDC transmit */
664 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
666 if (!uart_circ_empty(xmit
) && !uart_tx_stopped(port
)) {
667 dma_sync_single_for_device(port
->dev
,
672 count
= CIRC_CNT_TO_END(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
675 UART_PUT_TPR(port
, pdc
->dma_addr
+ xmit
->tail
);
676 UART_PUT_TCR(port
, count
);
677 /* re-enable PDC transmit */
678 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
679 /* Enable interrupts */
680 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
682 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
683 /* DMA done, stop TX, start RX for RS485 */
684 atmel_start_rx(port
);
688 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
689 uart_write_wakeup(port
);
692 static void atmel_rx_from_ring(struct uart_port
*port
)
694 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
695 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
699 while (ring
->head
!= ring
->tail
) {
700 struct atmel_uart_char c
;
702 /* Make sure c is loaded after head. */
705 c
= ((struct atmel_uart_char
*)ring
->buf
)[ring
->tail
];
707 ring
->tail
= (ring
->tail
+ 1) & (ATMEL_SERIAL_RINGSIZE
- 1);
714 * note that the error handling code is
715 * out of the main execution path
717 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
718 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
))) {
719 if (status
& ATMEL_US_RXBRK
) {
720 /* ignore side-effect */
721 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
724 if (uart_handle_break(port
))
727 if (status
& ATMEL_US_PARE
)
728 port
->icount
.parity
++;
729 if (status
& ATMEL_US_FRAME
)
730 port
->icount
.frame
++;
731 if (status
& ATMEL_US_OVRE
)
732 port
->icount
.overrun
++;
734 status
&= port
->read_status_mask
;
736 if (status
& ATMEL_US_RXBRK
)
738 else if (status
& ATMEL_US_PARE
)
740 else if (status
& ATMEL_US_FRAME
)
745 if (uart_handle_sysrq_char(port
, c
.ch
))
748 uart_insert_char(port
, status
, ATMEL_US_OVRE
, c
.ch
, flg
);
752 * Drop the lock here since it might end up calling
753 * uart_start(), which takes the lock.
755 spin_unlock(&port
->lock
);
756 tty_flip_buffer_push(port
->state
->port
.tty
);
757 spin_lock(&port
->lock
);
760 static void atmel_rx_from_dma(struct uart_port
*port
)
762 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
763 struct tty_struct
*tty
= port
->state
->port
.tty
;
764 struct atmel_dma_buffer
*pdc
;
765 int rx_idx
= atmel_port
->pdc_rx_idx
;
771 /* Reset the UART timeout early so that we don't miss one */
772 UART_PUT_CR(port
, ATMEL_US_STTTO
);
774 pdc
= &atmel_port
->pdc_rx
[rx_idx
];
775 head
= UART_GET_RPR(port
) - pdc
->dma_addr
;
778 /* If the PDC has switched buffers, RPR won't contain
779 * any address within the current buffer. Since head
780 * is unsigned, we just need a one-way comparison to
783 * In this case, we just need to consume the entire
784 * buffer and resubmit it for DMA. This will clear the
785 * ENDRX bit as well, so that we can safely re-enable
786 * all interrupts below.
788 head
= min(head
, pdc
->dma_size
);
790 if (likely(head
!= tail
)) {
791 dma_sync_single_for_cpu(port
->dev
, pdc
->dma_addr
,
792 pdc
->dma_size
, DMA_FROM_DEVICE
);
795 * head will only wrap around when we recycle
796 * the DMA buffer, and when that happens, we
797 * explicitly set tail to 0. So head will
798 * always be greater than tail.
802 tty_insert_flip_string(tty
, pdc
->buf
+ pdc
->ofs
, count
);
804 dma_sync_single_for_device(port
->dev
, pdc
->dma_addr
,
805 pdc
->dma_size
, DMA_FROM_DEVICE
);
807 port
->icount
.rx
+= count
;
812 * If the current buffer is full, we need to check if
813 * the next one contains any additional data.
815 if (head
>= pdc
->dma_size
) {
817 UART_PUT_RNPR(port
, pdc
->dma_addr
);
818 UART_PUT_RNCR(port
, pdc
->dma_size
);
821 atmel_port
->pdc_rx_idx
= rx_idx
;
823 } while (head
>= pdc
->dma_size
);
826 * Drop the lock here since it might end up calling
827 * uart_start(), which takes the lock.
829 spin_unlock(&port
->lock
);
830 tty_flip_buffer_push(tty
);
831 spin_lock(&port
->lock
);
833 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
837 * tasklet handling tty stuff outside the interrupt handler.
839 static void atmel_tasklet_func(unsigned long data
)
841 struct uart_port
*port
= (struct uart_port
*)data
;
842 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
844 unsigned int status_change
;
846 /* The interrupt handler does not take the lock */
847 spin_lock(&port
->lock
);
849 if (atmel_use_dma_tx(port
))
852 atmel_tx_chars(port
);
854 status
= atmel_port
->irq_status
;
855 status_change
= status
^ atmel_port
->irq_status_prev
;
857 if (status_change
& (ATMEL_US_RI
| ATMEL_US_DSR
858 | ATMEL_US_DCD
| ATMEL_US_CTS
)) {
859 /* TODO: All reads to CSR will clear these interrupts! */
860 if (status_change
& ATMEL_US_RI
)
862 if (status_change
& ATMEL_US_DSR
)
864 if (status_change
& ATMEL_US_DCD
)
865 uart_handle_dcd_change(port
, !(status
& ATMEL_US_DCD
));
866 if (status_change
& ATMEL_US_CTS
)
867 uart_handle_cts_change(port
, !(status
& ATMEL_US_CTS
));
869 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
871 atmel_port
->irq_status_prev
= status
;
874 if (atmel_use_dma_rx(port
))
875 atmel_rx_from_dma(port
);
877 atmel_rx_from_ring(port
);
879 spin_unlock(&port
->lock
);
883 * Perform initialization and enable port for reception
885 static int atmel_startup(struct uart_port
*port
)
887 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
888 struct tty_struct
*tty
= port
->state
->port
.tty
;
892 * Ensure that no interrupts are enabled otherwise when
893 * request_irq() is called we could get stuck trying to
894 * handle an unexpected interrupt
896 UART_PUT_IDR(port
, -1);
901 retval
= request_irq(port
->irq
, atmel_interrupt
, IRQF_SHARED
,
902 tty
? tty
->name
: "atmel_serial", port
);
904 printk("atmel_serial: atmel_startup - Can't get irq\n");
909 * Initialize DMA (if necessary)
911 if (atmel_use_dma_rx(port
)) {
914 for (i
= 0; i
< 2; i
++) {
915 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_rx
[i
];
917 pdc
->buf
= kmalloc(PDC_BUFFER_SIZE
, GFP_KERNEL
);
918 if (pdc
->buf
== NULL
) {
920 dma_unmap_single(port
->dev
,
921 atmel_port
->pdc_rx
[0].dma_addr
,
924 kfree(atmel_port
->pdc_rx
[0].buf
);
926 free_irq(port
->irq
, port
);
929 pdc
->dma_addr
= dma_map_single(port
->dev
,
933 pdc
->dma_size
= PDC_BUFFER_SIZE
;
937 atmel_port
->pdc_rx_idx
= 0;
939 UART_PUT_RPR(port
, atmel_port
->pdc_rx
[0].dma_addr
);
940 UART_PUT_RCR(port
, PDC_BUFFER_SIZE
);
942 UART_PUT_RNPR(port
, atmel_port
->pdc_rx
[1].dma_addr
);
943 UART_PUT_RNCR(port
, PDC_BUFFER_SIZE
);
945 if (atmel_use_dma_tx(port
)) {
946 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
947 struct circ_buf
*xmit
= &port
->state
->xmit
;
949 pdc
->buf
= xmit
->buf
;
950 pdc
->dma_addr
= dma_map_single(port
->dev
,
954 pdc
->dma_size
= UART_XMIT_SIZE
;
959 * If there is a specific "open" function (to register
960 * control line interrupts)
962 if (atmel_open_hook
) {
963 retval
= atmel_open_hook(port
);
965 free_irq(port
->irq
, port
);
970 /* Save current CSR for comparison in atmel_tasklet_func() */
971 atmel_port
->irq_status_prev
= UART_GET_CSR(port
);
972 atmel_port
->irq_status
= atmel_port
->irq_status_prev
;
975 * Finally, enable the serial port
977 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
978 /* enable xmit & rcvr */
979 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
981 if (atmel_use_dma_rx(port
)) {
982 /* set UART timeout */
983 UART_PUT_RTOR(port
, PDC_RX_TIMEOUT
);
984 UART_PUT_CR(port
, ATMEL_US_STTTO
);
986 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
987 /* enable PDC controller */
988 UART_PUT_PTCR(port
, ATMEL_PDC_RXTEN
);
990 /* enable receive only */
991 UART_PUT_IER(port
, ATMEL_US_RXRDY
);
1000 static void atmel_shutdown(struct uart_port
*port
)
1002 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1004 * Ensure everything is stopped.
1006 atmel_stop_rx(port
);
1007 atmel_stop_tx(port
);
1010 * Shut-down the DMA.
1012 if (atmel_use_dma_rx(port
)) {
1015 for (i
= 0; i
< 2; i
++) {
1016 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_rx
[i
];
1018 dma_unmap_single(port
->dev
,
1025 if (atmel_use_dma_tx(port
)) {
1026 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
1028 dma_unmap_single(port
->dev
,
1035 * Disable all interrupts, port and break condition.
1037 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
1038 UART_PUT_IDR(port
, -1);
1041 * Free the interrupt
1043 free_irq(port
->irq
, port
);
1046 * If there is a specific "close" function (to unregister
1047 * control line interrupts)
1049 if (atmel_close_hook
)
1050 atmel_close_hook(port
);
1054 * Flush any TX data submitted for DMA. Called when the TX circular
1057 static void atmel_flush_buffer(struct uart_port
*port
)
1059 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1061 if (atmel_use_dma_tx(port
)) {
1062 UART_PUT_TCR(port
, 0);
1063 atmel_port
->pdc_tx
.ofs
= 0;
1068 * Power / Clock management.
1070 static void atmel_serial_pm(struct uart_port
*port
, unsigned int state
,
1071 unsigned int oldstate
)
1073 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1078 * Enable the peripheral clock for this serial port.
1079 * This is called on uart_open() or a resume event.
1081 clk_enable(atmel_port
->clk
);
1083 /* re-enable interrupts if we disabled some on suspend */
1084 UART_PUT_IER(port
, atmel_port
->backup_imr
);
1087 /* Back up the interrupt mask and disable all interrupts */
1088 atmel_port
->backup_imr
= UART_GET_IMR(port
);
1089 UART_PUT_IDR(port
, -1);
1092 * Disable the peripheral clock for this serial port.
1093 * This is called on uart_close() or a suspend event.
1095 clk_disable(atmel_port
->clk
);
1098 printk(KERN_ERR
"atmel_serial: unknown pm %d\n", state
);
1103 * Change the port parameters
1105 static void atmel_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1106 struct ktermios
*old
)
1108 unsigned long flags
;
1109 unsigned int mode
, imr
, quot
, baud
;
1110 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1112 /* Get current mode register */
1113 mode
= UART_GET_MR(port
) & ~(ATMEL_US_USCLKS
| ATMEL_US_CHRL
1114 | ATMEL_US_NBSTOP
| ATMEL_US_PAR
1117 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
1118 quot
= uart_get_divisor(port
, baud
);
1120 if (quot
> 65535) { /* BRGR is 16-bit, so switch to slower clock */
1122 mode
|= ATMEL_US_USCLKS_MCK_DIV8
;
1126 switch (termios
->c_cflag
& CSIZE
) {
1128 mode
|= ATMEL_US_CHRL_5
;
1131 mode
|= ATMEL_US_CHRL_6
;
1134 mode
|= ATMEL_US_CHRL_7
;
1137 mode
|= ATMEL_US_CHRL_8
;
1142 if (termios
->c_cflag
& CSTOPB
)
1143 mode
|= ATMEL_US_NBSTOP_2
;
1146 if (termios
->c_cflag
& PARENB
) {
1147 /* Mark or Space parity */
1148 if (termios
->c_cflag
& CMSPAR
) {
1149 if (termios
->c_cflag
& PARODD
)
1150 mode
|= ATMEL_US_PAR_MARK
;
1152 mode
|= ATMEL_US_PAR_SPACE
;
1153 } else if (termios
->c_cflag
& PARODD
)
1154 mode
|= ATMEL_US_PAR_ODD
;
1156 mode
|= ATMEL_US_PAR_EVEN
;
1158 mode
|= ATMEL_US_PAR_NONE
;
1160 /* hardware handshake (RTS/CTS) */
1161 if (termios
->c_cflag
& CRTSCTS
)
1162 mode
|= ATMEL_US_USMODE_HWHS
;
1164 mode
|= ATMEL_US_USMODE_NORMAL
;
1166 spin_lock_irqsave(&port
->lock
, flags
);
1168 port
->read_status_mask
= ATMEL_US_OVRE
;
1169 if (termios
->c_iflag
& INPCK
)
1170 port
->read_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
1171 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
1172 port
->read_status_mask
|= ATMEL_US_RXBRK
;
1174 if (atmel_use_dma_rx(port
))
1175 /* need to enable error interrupts */
1176 UART_PUT_IER(port
, port
->read_status_mask
);
1179 * Characters to ignore
1181 port
->ignore_status_mask
= 0;
1182 if (termios
->c_iflag
& IGNPAR
)
1183 port
->ignore_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
1184 if (termios
->c_iflag
& IGNBRK
) {
1185 port
->ignore_status_mask
|= ATMEL_US_RXBRK
;
1187 * If we're ignoring parity and break indicators,
1188 * ignore overruns too (for real raw support).
1190 if (termios
->c_iflag
& IGNPAR
)
1191 port
->ignore_status_mask
|= ATMEL_US_OVRE
;
1193 /* TODO: Ignore all characters if CREAD is set.*/
1195 /* update the per-port timeout */
1196 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1199 * save/disable interrupts. The tty layer will ensure that the
1200 * transmitter is empty if requested by the caller, so there's
1201 * no need to wait for it here.
1203 imr
= UART_GET_IMR(port
);
1204 UART_PUT_IDR(port
, -1);
1206 /* disable receiver and transmitter */
1207 UART_PUT_CR(port
, ATMEL_US_TXDIS
| ATMEL_US_RXDIS
);
1209 /* Resetting serial mode to RS232 (0x0) */
1210 mode
&= ~ATMEL_US_USMODE
;
1212 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
1213 dev_dbg(port
->dev
, "Setting UART to RS485\n");
1214 UART_PUT_TTGR(port
, atmel_port
->rs485
.delay_rts_before_send
);
1215 mode
|= ATMEL_US_USMODE_RS485
;
1217 dev_dbg(port
->dev
, "Setting UART to RS232\n");
1220 /* set the parity, stop bits and data size */
1221 UART_PUT_MR(port
, mode
);
1223 /* set the baud rate */
1224 UART_PUT_BRGR(port
, quot
);
1225 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
1226 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
1228 /* restore interrupts */
1229 UART_PUT_IER(port
, imr
);
1231 /* CTS flow-control and modem-status interrupts */
1232 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
1233 port
->ops
->enable_ms(port
);
1235 spin_unlock_irqrestore(&port
->lock
, flags
);
1239 * Return string describing the specified port
1241 static const char *atmel_type(struct uart_port
*port
)
1243 return (port
->type
== PORT_ATMEL
) ? "ATMEL_SERIAL" : NULL
;
1247 * Release the memory region(s) being used by 'port'.
1249 static void atmel_release_port(struct uart_port
*port
)
1251 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1252 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
1254 release_mem_region(port
->mapbase
, size
);
1256 if (port
->flags
& UPF_IOREMAP
) {
1257 iounmap(port
->membase
);
1258 port
->membase
= NULL
;
1263 * Request the memory region(s) being used by 'port'.
1265 static int atmel_request_port(struct uart_port
*port
)
1267 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1268 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
1270 if (!request_mem_region(port
->mapbase
, size
, "atmel_serial"))
1273 if (port
->flags
& UPF_IOREMAP
) {
1274 port
->membase
= ioremap(port
->mapbase
, size
);
1275 if (port
->membase
== NULL
) {
1276 release_mem_region(port
->mapbase
, size
);
1285 * Configure/autoconfigure the port.
1287 static void atmel_config_port(struct uart_port
*port
, int flags
)
1289 if (flags
& UART_CONFIG_TYPE
) {
1290 port
->type
= PORT_ATMEL
;
1291 atmel_request_port(port
);
1296 * Verify the new serial_struct (for TIOCSSERIAL).
1298 static int atmel_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1301 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_ATMEL
)
1303 if (port
->irq
!= ser
->irq
)
1305 if (ser
->io_type
!= SERIAL_IO_MEM
)
1307 if (port
->uartclk
/ 16 != ser
->baud_base
)
1309 if ((void *)port
->mapbase
!= ser
->iomem_base
)
1311 if (port
->iobase
!= ser
->port
)
1318 #ifdef CONFIG_CONSOLE_POLL
1319 static int atmel_poll_get_char(struct uart_port
*port
)
1321 while (!(UART_GET_CSR(port
) & ATMEL_US_RXRDY
))
1324 return UART_GET_CHAR(port
);
1327 static void atmel_poll_put_char(struct uart_port
*port
, unsigned char ch
)
1329 while (!(UART_GET_CSR(port
) & ATMEL_US_TXRDY
))
1332 UART_PUT_CHAR(port
, ch
);
1337 atmel_ioctl(struct uart_port
*port
, unsigned int cmd
, unsigned long arg
)
1339 struct serial_rs485 rs485conf
;
1343 if (copy_from_user(&rs485conf
, (struct serial_rs485
*) arg
,
1347 atmel_config_rs485(port
, &rs485conf
);
1351 if (copy_to_user((struct serial_rs485
*) arg
,
1352 &(to_atmel_uart_port(port
)->rs485
),
1358 return -ENOIOCTLCMD
;
1365 static struct uart_ops atmel_pops
= {
1366 .tx_empty
= atmel_tx_empty
,
1367 .set_mctrl
= atmel_set_mctrl
,
1368 .get_mctrl
= atmel_get_mctrl
,
1369 .stop_tx
= atmel_stop_tx
,
1370 .start_tx
= atmel_start_tx
,
1371 .stop_rx
= atmel_stop_rx
,
1372 .enable_ms
= atmel_enable_ms
,
1373 .break_ctl
= atmel_break_ctl
,
1374 .startup
= atmel_startup
,
1375 .shutdown
= atmel_shutdown
,
1376 .flush_buffer
= atmel_flush_buffer
,
1377 .set_termios
= atmel_set_termios
,
1379 .release_port
= atmel_release_port
,
1380 .request_port
= atmel_request_port
,
1381 .config_port
= atmel_config_port
,
1382 .verify_port
= atmel_verify_port
,
1383 .pm
= atmel_serial_pm
,
1384 .ioctl
= atmel_ioctl
,
1385 #ifdef CONFIG_CONSOLE_POLL
1386 .poll_get_char
= atmel_poll_get_char
,
1387 .poll_put_char
= atmel_poll_put_char
,
1392 * Configure the port from the platform device resource info.
1394 static void __devinit
atmel_init_port(struct atmel_uart_port
*atmel_port
,
1395 struct platform_device
*pdev
)
1397 struct uart_port
*port
= &atmel_port
->uart
;
1398 struct atmel_uart_data
*data
= pdev
->dev
.platform_data
;
1400 port
->iotype
= UPIO_MEM
;
1401 port
->flags
= UPF_BOOT_AUTOCONF
;
1402 port
->ops
= &atmel_pops
;
1404 port
->line
= pdev
->id
;
1405 port
->dev
= &pdev
->dev
;
1406 port
->mapbase
= pdev
->resource
[0].start
;
1407 port
->irq
= pdev
->resource
[1].start
;
1409 tasklet_init(&atmel_port
->tasklet
, atmel_tasklet_func
,
1410 (unsigned long)port
);
1412 memset(&atmel_port
->rx_ring
, 0, sizeof(atmel_port
->rx_ring
));
1415 /* Already mapped by setup code */
1416 port
->membase
= data
->regs
;
1418 port
->flags
|= UPF_IOREMAP
;
1419 port
->membase
= NULL
;
1422 /* for console, the clock could already be configured */
1423 if (!atmel_port
->clk
) {
1424 atmel_port
->clk
= clk_get(&pdev
->dev
, "usart");
1425 clk_enable(atmel_port
->clk
);
1426 port
->uartclk
= clk_get_rate(atmel_port
->clk
);
1427 clk_disable(atmel_port
->clk
);
1428 /* only enable clock when USART is in use */
1431 atmel_port
->use_dma_rx
= data
->use_dma_rx
;
1432 atmel_port
->use_dma_tx
= data
->use_dma_tx
;
1433 atmel_port
->rs485
= data
->rs485
;
1434 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1435 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
)
1436 atmel_port
->tx_done_mask
= ATMEL_US_TXEMPTY
;
1437 else if (atmel_use_dma_tx(port
)) {
1438 port
->fifosize
= PDC_BUFFER_SIZE
;
1439 atmel_port
->tx_done_mask
= ATMEL_US_ENDTX
| ATMEL_US_TXBUFE
;
1441 atmel_port
->tx_done_mask
= ATMEL_US_TXRDY
;
1446 * Register board-specific modem-control line handlers.
1448 void __init
atmel_register_uart_fns(struct atmel_port_fns
*fns
)
1451 atmel_pops
.enable_ms
= fns
->enable_ms
;
1453 atmel_pops
.get_mctrl
= fns
->get_mctrl
;
1455 atmel_pops
.set_mctrl
= fns
->set_mctrl
;
1456 atmel_open_hook
= fns
->open
;
1457 atmel_close_hook
= fns
->close
;
1458 atmel_pops
.pm
= fns
->pm
;
1459 atmel_pops
.set_wake
= fns
->set_wake
;
1462 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1463 static void atmel_console_putchar(struct uart_port
*port
, int ch
)
1465 while (!(UART_GET_CSR(port
) & ATMEL_US_TXRDY
))
1467 UART_PUT_CHAR(port
, ch
);
1471 * Interrupts are disabled on entering
1473 static void atmel_console_write(struct console
*co
, const char *s
, u_int count
)
1475 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
1476 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1477 unsigned int status
, imr
;
1478 unsigned int pdc_tx
;
1481 * First, save IMR and then disable interrupts
1483 imr
= UART_GET_IMR(port
);
1484 UART_PUT_IDR(port
, ATMEL_US_RXRDY
| atmel_port
->tx_done_mask
);
1486 /* Store PDC transmit status and disable it */
1487 pdc_tx
= UART_GET_PTSR(port
) & ATMEL_PDC_TXTEN
;
1488 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
1490 uart_console_write(port
, s
, count
, atmel_console_putchar
);
1493 * Finally, wait for transmitter to become empty
1497 status
= UART_GET_CSR(port
);
1498 } while (!(status
& ATMEL_US_TXRDY
));
1500 /* Restore PDC transmit status */
1502 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
1504 /* set interrupts back the way they were */
1505 UART_PUT_IER(port
, imr
);
1509 * If the port was already initialised (eg, by a boot loader),
1510 * try to determine the current setup.
1512 static void __init
atmel_console_get_options(struct uart_port
*port
, int *baud
,
1513 int *parity
, int *bits
)
1515 unsigned int mr
, quot
;
1518 * If the baud rate generator isn't running, the port wasn't
1519 * initialized by the boot loader.
1521 quot
= UART_GET_BRGR(port
) & ATMEL_US_CD
;
1525 mr
= UART_GET_MR(port
) & ATMEL_US_CHRL
;
1526 if (mr
== ATMEL_US_CHRL_8
)
1531 mr
= UART_GET_MR(port
) & ATMEL_US_PAR
;
1532 if (mr
== ATMEL_US_PAR_EVEN
)
1534 else if (mr
== ATMEL_US_PAR_ODD
)
1538 * The serial core only rounds down when matching this to a
1539 * supported baud rate. Make sure we don't end up slightly
1540 * lower than one of those, as it would make us fall through
1541 * to a much lower baud rate than we really want.
1543 *baud
= port
->uartclk
/ (16 * (quot
- 1));
1546 static int __init
atmel_console_setup(struct console
*co
, char *options
)
1548 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
1554 if (port
->membase
== NULL
) {
1555 /* Port not initialized yet - delay setup */
1559 clk_enable(atmel_ports
[co
->index
].clk
);
1561 UART_PUT_IDR(port
, -1);
1562 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
1563 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
1566 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1568 atmel_console_get_options(port
, &baud
, &parity
, &bits
);
1570 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1573 static struct uart_driver atmel_uart
;
1575 static struct console atmel_console
= {
1576 .name
= ATMEL_DEVICENAME
,
1577 .write
= atmel_console_write
,
1578 .device
= uart_console_device
,
1579 .setup
= atmel_console_setup
,
1580 .flags
= CON_PRINTBUFFER
,
1582 .data
= &atmel_uart
,
1585 #define ATMEL_CONSOLE_DEVICE (&atmel_console)
1588 * Early console initialization (before VM subsystem initialized).
1590 static int __init
atmel_console_init(void)
1592 if (atmel_default_console_device
) {
1593 add_preferred_console(ATMEL_DEVICENAME
,
1594 atmel_default_console_device
->id
, NULL
);
1595 atmel_init_port(&atmel_ports
[atmel_default_console_device
->id
],
1596 atmel_default_console_device
);
1597 register_console(&atmel_console
);
1603 console_initcall(atmel_console_init
);
1606 * Late console initialization.
1608 static int __init
atmel_late_console_init(void)
1610 if (atmel_default_console_device
1611 && !(atmel_console
.flags
& CON_ENABLED
))
1612 register_console(&atmel_console
);
1617 core_initcall(atmel_late_console_init
);
1619 static inline bool atmel_is_console_port(struct uart_port
*port
)
1621 return port
->cons
&& port
->cons
->index
== port
->line
;
1625 #define ATMEL_CONSOLE_DEVICE NULL
1627 static inline bool atmel_is_console_port(struct uart_port
*port
)
1633 static struct uart_driver atmel_uart
= {
1634 .owner
= THIS_MODULE
,
1635 .driver_name
= "atmel_serial",
1636 .dev_name
= ATMEL_DEVICENAME
,
1637 .major
= SERIAL_ATMEL_MAJOR
,
1638 .minor
= MINOR_START
,
1639 .nr
= ATMEL_MAX_UART
,
1640 .cons
= ATMEL_CONSOLE_DEVICE
,
1644 static bool atmel_serial_clk_will_stop(void)
1646 #ifdef CONFIG_ARCH_AT91
1647 return at91_suspend_entering_slow_clock();
1653 static int atmel_serial_suspend(struct platform_device
*pdev
,
1656 struct uart_port
*port
= platform_get_drvdata(pdev
);
1657 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1659 if (atmel_is_console_port(port
) && console_suspend_enabled
) {
1660 /* Drain the TX shifter */
1661 while (!(UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
))
1665 /* we can not wake up if we're running on slow clock */
1666 atmel_port
->may_wakeup
= device_may_wakeup(&pdev
->dev
);
1667 if (atmel_serial_clk_will_stop())
1668 device_set_wakeup_enable(&pdev
->dev
, 0);
1670 uart_suspend_port(&atmel_uart
, port
);
1675 static int atmel_serial_resume(struct platform_device
*pdev
)
1677 struct uart_port
*port
= platform_get_drvdata(pdev
);
1678 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1680 uart_resume_port(&atmel_uart
, port
);
1681 device_set_wakeup_enable(&pdev
->dev
, atmel_port
->may_wakeup
);
1686 #define atmel_serial_suspend NULL
1687 #define atmel_serial_resume NULL
1690 static int __devinit
atmel_serial_probe(struct platform_device
*pdev
)
1692 struct atmel_uart_port
*port
;
1696 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE
& (ATMEL_SERIAL_RINGSIZE
- 1));
1698 port
= &atmel_ports
[pdev
->id
];
1699 port
->backup_imr
= 0;
1701 atmel_init_port(port
, pdev
);
1703 if (!atmel_use_dma_rx(&port
->uart
)) {
1705 data
= kmalloc(sizeof(struct atmel_uart_char
)
1706 * ATMEL_SERIAL_RINGSIZE
, GFP_KERNEL
);
1708 goto err_alloc_ring
;
1709 port
->rx_ring
.buf
= data
;
1712 ret
= uart_add_one_port(&atmel_uart
, &port
->uart
);
1716 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1717 if (atmel_is_console_port(&port
->uart
)
1718 && ATMEL_CONSOLE_DEVICE
->flags
& CON_ENABLED
) {
1720 * The serial core enabled the clock for us, so undo
1721 * the clk_enable() in atmel_console_setup()
1723 clk_disable(port
->clk
);
1727 device_init_wakeup(&pdev
->dev
, 1);
1728 platform_set_drvdata(pdev
, port
);
1733 kfree(port
->rx_ring
.buf
);
1734 port
->rx_ring
.buf
= NULL
;
1736 if (!atmel_is_console_port(&port
->uart
)) {
1744 static int __devexit
atmel_serial_remove(struct platform_device
*pdev
)
1746 struct uart_port
*port
= platform_get_drvdata(pdev
);
1747 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1750 device_init_wakeup(&pdev
->dev
, 0);
1751 platform_set_drvdata(pdev
, NULL
);
1753 ret
= uart_remove_one_port(&atmel_uart
, port
);
1755 tasklet_kill(&atmel_port
->tasklet
);
1756 kfree(atmel_port
->rx_ring
.buf
);
1758 /* "port" is allocated statically, so we shouldn't free it */
1760 clk_put(atmel_port
->clk
);
1765 static struct platform_driver atmel_serial_driver
= {
1766 .probe
= atmel_serial_probe
,
1767 .remove
= __devexit_p(atmel_serial_remove
),
1768 .suspend
= atmel_serial_suspend
,
1769 .resume
= atmel_serial_resume
,
1771 .name
= "atmel_usart",
1772 .owner
= THIS_MODULE
,
1776 static int __init
atmel_serial_init(void)
1780 ret
= uart_register_driver(&atmel_uart
);
1784 ret
= platform_driver_register(&atmel_serial_driver
);
1786 uart_unregister_driver(&atmel_uart
);
1791 static void __exit
atmel_serial_exit(void)
1793 platform_driver_unregister(&atmel_serial_driver
);
1794 uart_unregister_driver(&atmel_uart
);
1797 module_init(atmel_serial_init
);
1798 module_exit(atmel_serial_exit
);
1800 MODULE_AUTHOR("Rick Bronson");
1801 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1802 MODULE_LICENSE("GPL");
1803 MODULE_ALIAS("platform:atmel_usart");