2 * Driver for Atmel AT91 / AT32 Serial ports
3 * Copyright (C) 2003 Rick Bronson
5 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * DMA support added by Chip Coldwell.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/tty.h>
27 #include <linux/ioport.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/serial.h>
31 #include <linux/clk.h>
32 #include <linux/console.h>
33 #include <linux/sysrq.h>
34 #include <linux/tty_flip.h>
35 #include <linux/platform_device.h>
37 #include <linux/of_device.h>
38 #include <linux/of_gpio.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/atmel_pdc.h>
41 #include <linux/atmel_serial.h>
42 #include <linux/uaccess.h>
43 #include <linux/platform_data/atmel.h>
44 #include <linux/timer.h>
45 #include <linux/gpio.h>
48 #include <asm/ioctls.h>
50 #define PDC_BUFFER_SIZE 512
51 /* Revisit: We should calculate this based on the actual port settings */
52 #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
54 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
58 #include <linux/serial_core.h>
60 static void atmel_start_rx(struct uart_port
*port
);
61 static void atmel_stop_rx(struct uart_port
*port
);
63 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
65 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
66 * should coexist with the 8250 driver, such as if we have an external 16C550
68 #define SERIAL_ATMEL_MAJOR 204
69 #define MINOR_START 154
70 #define ATMEL_DEVICENAME "ttyAT"
74 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
75 * name, but it is legally reserved for the 8250 driver. */
76 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
77 #define MINOR_START 64
78 #define ATMEL_DEVICENAME "ttyS"
82 #define ATMEL_ISR_PASS_LIMIT 256
84 /* UART registers. CR is write-only, hence no GET macro */
85 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
86 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
87 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
88 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
89 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
90 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
91 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
92 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
93 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
94 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
95 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
96 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
97 #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
98 #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
99 #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
102 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
103 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
105 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
106 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
107 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
108 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
109 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
111 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
112 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
113 #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
115 static int (*atmel_open_hook
)(struct uart_port
*);
116 static void (*atmel_close_hook
)(struct uart_port
*);
118 struct atmel_dma_buffer
{
121 unsigned int dma_size
;
125 struct atmel_uart_char
{
130 #define ATMEL_SERIAL_RINGSIZE 1024
133 * We wrap our port structure around the generic uart_port.
135 struct atmel_uart_port
{
136 struct uart_port uart
; /* uart */
137 struct clk
*clk
; /* uart clock */
138 int may_wakeup
; /* cached value of device_may_wakeup for times we need to disable it */
139 u32 backup_imr
; /* IMR saved during suspend */
140 int break_active
; /* break being received */
142 bool use_dma_rx
; /* enable DMA receiver */
143 bool use_pdc_rx
; /* enable PDC receiver */
144 short pdc_rx_idx
; /* current PDC RX buffer */
145 struct atmel_dma_buffer pdc_rx
[2]; /* PDC receier */
147 bool use_dma_tx
; /* enable DMA transmitter */
148 bool use_pdc_tx
; /* enable PDC transmitter */
149 struct atmel_dma_buffer pdc_tx
; /* PDC transmitter */
151 spinlock_t lock_tx
; /* port lock */
152 spinlock_t lock_rx
; /* port lock */
153 struct dma_chan
*chan_tx
;
154 struct dma_chan
*chan_rx
;
155 struct dma_async_tx_descriptor
*desc_tx
;
156 struct dma_async_tx_descriptor
*desc_rx
;
157 dma_cookie_t cookie_tx
;
158 dma_cookie_t cookie_rx
;
159 struct scatterlist sg_tx
;
160 struct scatterlist sg_rx
;
161 struct tasklet_struct tasklet
;
162 unsigned int irq_status
;
163 unsigned int irq_status_prev
;
165 struct circ_buf rx_ring
;
167 struct serial_rs485 rs485
; /* rs485 settings */
168 int rts_gpio
; /* optional RTS GPIO */
169 unsigned int tx_done_mask
;
170 bool is_usart
; /* usart or uart */
171 struct timer_list uart_timer
; /* uart timer */
172 int (*prepare_rx
)(struct uart_port
*port
);
173 int (*prepare_tx
)(struct uart_port
*port
);
174 void (*schedule_rx
)(struct uart_port
*port
);
175 void (*schedule_tx
)(struct uart_port
*port
);
176 void (*release_rx
)(struct uart_port
*port
);
177 void (*release_tx
)(struct uart_port
*port
);
180 static struct atmel_uart_port atmel_ports
[ATMEL_MAX_UART
];
181 static DECLARE_BITMAP(atmel_ports_in_use
, ATMEL_MAX_UART
);
184 static struct console atmel_console
;
187 #if defined(CONFIG_OF)
188 static const struct of_device_id atmel_serial_dt_ids
[] = {
189 { .compatible
= "atmel,at91rm9200-usart" },
190 { .compatible
= "atmel,at91sam9260-usart" },
194 MODULE_DEVICE_TABLE(of
, atmel_serial_dt_ids
);
197 static inline struct atmel_uart_port
*
198 to_atmel_uart_port(struct uart_port
*uart
)
200 return container_of(uart
, struct atmel_uart_port
, uart
);
203 #ifdef CONFIG_SERIAL_ATMEL_PDC
204 static bool atmel_use_pdc_rx(struct uart_port
*port
)
206 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
208 return atmel_port
->use_pdc_rx
;
211 static bool atmel_use_pdc_tx(struct uart_port
*port
)
213 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
215 return atmel_port
->use_pdc_tx
;
218 static bool atmel_use_pdc_rx(struct uart_port
*port
)
223 static bool atmel_use_pdc_tx(struct uart_port
*port
)
229 static bool atmel_use_dma_tx(struct uart_port
*port
)
231 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
233 return atmel_port
->use_dma_tx
;
236 static bool atmel_use_dma_rx(struct uart_port
*port
)
238 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
240 return atmel_port
->use_dma_rx
;
243 /* Enable or disable the rs485 support */
244 void atmel_config_rs485(struct uart_port
*port
, struct serial_rs485
*rs485conf
)
246 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
250 spin_lock_irqsave(&port
->lock
, flags
);
252 /* Disable interrupts */
253 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
255 mode
= UART_GET_MR(port
);
257 /* Resetting serial mode to RS232 (0x0) */
258 mode
&= ~ATMEL_US_USMODE
;
260 atmel_port
->rs485
= *rs485conf
;
262 if (rs485conf
->flags
& SER_RS485_ENABLED
) {
263 dev_dbg(port
->dev
, "Setting UART to RS485\n");
264 atmel_port
->tx_done_mask
= ATMEL_US_TXEMPTY
;
265 if ((rs485conf
->delay_rts_after_send
) > 0)
266 UART_PUT_TTGR(port
, rs485conf
->delay_rts_after_send
);
267 mode
|= ATMEL_US_USMODE_RS485
;
269 dev_dbg(port
->dev
, "Setting UART to RS232\n");
270 if (atmel_use_pdc_tx(port
))
271 atmel_port
->tx_done_mask
= ATMEL_US_ENDTX
|
274 atmel_port
->tx_done_mask
= ATMEL_US_TXRDY
;
276 UART_PUT_MR(port
, mode
);
278 /* Enable interrupts */
279 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
281 spin_unlock_irqrestore(&port
->lock
, flags
);
286 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
288 static u_int
atmel_tx_empty(struct uart_port
*port
)
290 return (UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
) ? TIOCSER_TEMT
: 0;
294 * Set state of the modem control output lines
296 static void atmel_set_mctrl(struct uart_port
*port
, u_int mctrl
)
298 unsigned int control
= 0;
300 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
303 * AT91RM9200 Errata #39: RTS0 is not internally connected
304 * to PA21. We need to drive the pin as a GPIO.
306 if (gpio_is_valid(atmel_port
->rts_gpio
)) {
307 if (mctrl
& TIOCM_RTS
)
308 gpio_set_value(atmel_port
->rts_gpio
, 0);
310 gpio_set_value(atmel_port
->rts_gpio
, 1);
313 if (mctrl
& TIOCM_RTS
)
314 control
|= ATMEL_US_RTSEN
;
316 control
|= ATMEL_US_RTSDIS
;
318 if (mctrl
& TIOCM_DTR
)
319 control
|= ATMEL_US_DTREN
;
321 control
|= ATMEL_US_DTRDIS
;
323 UART_PUT_CR(port
, control
);
325 /* Local loopback mode? */
326 mode
= UART_GET_MR(port
) & ~ATMEL_US_CHMODE
;
327 if (mctrl
& TIOCM_LOOP
)
328 mode
|= ATMEL_US_CHMODE_LOC_LOOP
;
330 mode
|= ATMEL_US_CHMODE_NORMAL
;
332 /* Resetting serial mode to RS232 (0x0) */
333 mode
&= ~ATMEL_US_USMODE
;
335 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
336 dev_dbg(port
->dev
, "Setting UART to RS485\n");
337 if ((atmel_port
->rs485
.delay_rts_after_send
) > 0)
339 atmel_port
->rs485
.delay_rts_after_send
);
340 mode
|= ATMEL_US_USMODE_RS485
;
342 dev_dbg(port
->dev
, "Setting UART to RS232\n");
344 UART_PUT_MR(port
, mode
);
348 * Get state of the modem control input lines
350 static u_int
atmel_get_mctrl(struct uart_port
*port
)
352 unsigned int status
, ret
= 0;
354 status
= UART_GET_CSR(port
);
357 * The control signals are active low.
359 if (!(status
& ATMEL_US_DCD
))
361 if (!(status
& ATMEL_US_CTS
))
363 if (!(status
& ATMEL_US_DSR
))
365 if (!(status
& ATMEL_US_RI
))
374 static void atmel_stop_tx(struct uart_port
*port
)
376 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
378 if (atmel_use_pdc_tx(port
)) {
379 /* disable PDC transmit */
380 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
382 /* Disable interrupts */
383 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
385 if ((atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) &&
386 !(atmel_port
->rs485
.flags
& SER_RS485_RX_DURING_TX
))
387 atmel_start_rx(port
);
391 * Start transmitting.
393 static void atmel_start_tx(struct uart_port
*port
)
395 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
397 if (atmel_use_pdc_tx(port
)) {
398 if (UART_GET_PTSR(port
) & ATMEL_PDC_TXTEN
)
399 /* The transmitter is already running. Yes, we
403 if ((atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) &&
404 !(atmel_port
->rs485
.flags
& SER_RS485_RX_DURING_TX
))
407 /* re-enable PDC transmit */
408 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
410 /* Enable interrupts */
411 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
415 * start receiving - port is in process of being opened.
417 static void atmel_start_rx(struct uart_port
*port
)
419 UART_PUT_CR(port
, ATMEL_US_RSTSTA
); /* reset status and receiver */
421 UART_PUT_CR(port
, ATMEL_US_RXEN
);
423 if (atmel_use_pdc_rx(port
)) {
424 /* enable PDC controller */
425 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
|
426 port
->read_status_mask
);
427 UART_PUT_PTCR(port
, ATMEL_PDC_RXTEN
);
429 UART_PUT_IER(port
, ATMEL_US_RXRDY
);
434 * Stop receiving - port is in process of being closed.
436 static void atmel_stop_rx(struct uart_port
*port
)
438 UART_PUT_CR(port
, ATMEL_US_RXDIS
);
440 if (atmel_use_pdc_rx(port
)) {
441 /* disable PDC receive */
442 UART_PUT_PTCR(port
, ATMEL_PDC_RXTDIS
);
443 UART_PUT_IDR(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
|
444 port
->read_status_mask
);
446 UART_PUT_IDR(port
, ATMEL_US_RXRDY
);
451 * Enable modem status interrupts
453 static void atmel_enable_ms(struct uart_port
*port
)
455 UART_PUT_IER(port
, ATMEL_US_RIIC
| ATMEL_US_DSRIC
456 | ATMEL_US_DCDIC
| ATMEL_US_CTSIC
);
460 * Control the transmission of a break signal
462 static void atmel_break_ctl(struct uart_port
*port
, int break_state
)
464 if (break_state
!= 0)
465 UART_PUT_CR(port
, ATMEL_US_STTBRK
); /* start break */
467 UART_PUT_CR(port
, ATMEL_US_STPBRK
); /* stop break */
471 * Stores the incoming character in the ring buffer
474 atmel_buffer_rx_char(struct uart_port
*port
, unsigned int status
,
477 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
478 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
479 struct atmel_uart_char
*c
;
481 if (!CIRC_SPACE(ring
->head
, ring
->tail
, ATMEL_SERIAL_RINGSIZE
))
482 /* Buffer overflow, ignore char */
485 c
= &((struct atmel_uart_char
*)ring
->buf
)[ring
->head
];
489 /* Make sure the character is stored before we update head. */
492 ring
->head
= (ring
->head
+ 1) & (ATMEL_SERIAL_RINGSIZE
- 1);
496 * Deal with parity, framing and overrun errors.
498 static void atmel_pdc_rxerr(struct uart_port
*port
, unsigned int status
)
501 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
503 if (status
& ATMEL_US_RXBRK
) {
504 /* ignore side-effect */
505 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
508 if (status
& ATMEL_US_PARE
)
509 port
->icount
.parity
++;
510 if (status
& ATMEL_US_FRAME
)
511 port
->icount
.frame
++;
512 if (status
& ATMEL_US_OVRE
)
513 port
->icount
.overrun
++;
517 * Characters received (called from interrupt handler)
519 static void atmel_rx_chars(struct uart_port
*port
)
521 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
522 unsigned int status
, ch
;
524 status
= UART_GET_CSR(port
);
525 while (status
& ATMEL_US_RXRDY
) {
526 ch
= UART_GET_CHAR(port
);
529 * note that the error handling code is
530 * out of the main execution path
532 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
533 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
)
534 || atmel_port
->break_active
)) {
537 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
539 if (status
& ATMEL_US_RXBRK
540 && !atmel_port
->break_active
) {
541 atmel_port
->break_active
= 1;
542 UART_PUT_IER(port
, ATMEL_US_RXBRK
);
545 * This is either the end-of-break
546 * condition or we've received at
547 * least one character without RXBRK
548 * being set. In both cases, the next
549 * RXBRK will indicate start-of-break.
551 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
552 status
&= ~ATMEL_US_RXBRK
;
553 atmel_port
->break_active
= 0;
557 atmel_buffer_rx_char(port
, status
, ch
);
558 status
= UART_GET_CSR(port
);
561 tasklet_schedule(&atmel_port
->tasklet
);
565 * Transmit characters (called from tasklet with TXRDY interrupt
568 static void atmel_tx_chars(struct uart_port
*port
)
570 struct circ_buf
*xmit
= &port
->state
->xmit
;
571 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
573 if (port
->x_char
&& UART_GET_CSR(port
) & atmel_port
->tx_done_mask
) {
574 UART_PUT_CHAR(port
, port
->x_char
);
578 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
581 while (UART_GET_CSR(port
) & atmel_port
->tx_done_mask
) {
582 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
583 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
585 if (uart_circ_empty(xmit
))
589 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
590 uart_write_wakeup(port
);
592 if (!uart_circ_empty(xmit
))
593 /* Enable interrupts */
594 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
597 static void atmel_complete_tx_dma(void *arg
)
599 struct atmel_uart_port
*atmel_port
= arg
;
600 struct uart_port
*port
= &atmel_port
->uart
;
601 struct circ_buf
*xmit
= &port
->state
->xmit
;
602 struct dma_chan
*chan
= atmel_port
->chan_tx
;
605 spin_lock_irqsave(&port
->lock
, flags
);
608 dmaengine_terminate_all(chan
);
609 xmit
->tail
+= sg_dma_len(&atmel_port
->sg_tx
);
610 xmit
->tail
&= UART_XMIT_SIZE
- 1;
612 port
->icount
.tx
+= sg_dma_len(&atmel_port
->sg_tx
);
614 spin_lock_irq(&atmel_port
->lock_tx
);
615 async_tx_ack(atmel_port
->desc_tx
);
616 atmel_port
->cookie_tx
= -EINVAL
;
617 atmel_port
->desc_tx
= NULL
;
618 spin_unlock_irq(&atmel_port
->lock_tx
);
620 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
621 uart_write_wakeup(port
);
623 /* Do we really need this? */
624 if (!uart_circ_empty(xmit
))
625 tasklet_schedule(&atmel_port
->tasklet
);
627 spin_unlock_irqrestore(&port
->lock
, flags
);
630 static void atmel_release_tx_dma(struct uart_port
*port
)
632 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
633 struct dma_chan
*chan
= atmel_port
->chan_tx
;
636 dmaengine_terminate_all(chan
);
637 dma_release_channel(chan
);
638 dma_unmap_sg(port
->dev
, &atmel_port
->sg_tx
, 1,
642 atmel_port
->desc_tx
= NULL
;
643 atmel_port
->chan_tx
= NULL
;
644 atmel_port
->cookie_tx
= -EINVAL
;
648 * Called from tasklet with TXRDY interrupt is disabled.
650 static void atmel_tx_dma(struct uart_port
*port
)
652 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
653 struct circ_buf
*xmit
= &port
->state
->xmit
;
654 struct dma_chan
*chan
= atmel_port
->chan_tx
;
655 struct dma_async_tx_descriptor
*desc
;
656 struct scatterlist
*sg
= &atmel_port
->sg_tx
;
658 /* Make sure we have an idle channel */
659 if (atmel_port
->desc_tx
!= NULL
)
662 if (!uart_circ_empty(xmit
) && !uart_tx_stopped(port
)) {
665 * Port xmit buffer is already mapped,
666 * and it is one page... Just adjust
667 * offsets and lengths. Since it is a circular buffer,
668 * we have to transmit till the end, and then the rest.
669 * Take the port lock to get a
670 * consistent xmit buffer state.
672 sg
->offset
= xmit
->tail
& (UART_XMIT_SIZE
- 1);
673 sg_dma_address(sg
) = (sg_dma_address(sg
) &
674 ~(UART_XMIT_SIZE
- 1))
676 sg_dma_len(sg
) = CIRC_CNT_TO_END(xmit
->head
,
679 BUG_ON(!sg_dma_len(sg
));
681 desc
= dmaengine_prep_slave_sg(chan
,
688 dev_err(port
->dev
, "Failed to send via dma!\n");
692 dma_sync_sg_for_device(port
->dev
, sg
, 1, DMA_MEM_TO_DEV
);
694 atmel_port
->desc_tx
= desc
;
695 desc
->callback
= atmel_complete_tx_dma
;
696 desc
->callback_param
= atmel_port
;
697 atmel_port
->cookie_tx
= dmaengine_submit(desc
);
700 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
701 /* DMA done, stop TX, start RX for RS485 */
702 atmel_start_rx(port
);
706 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
707 uart_write_wakeup(port
);
710 static int atmel_prepare_tx_dma(struct uart_port
*port
)
712 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
714 struct dma_slave_config config
;
718 dma_cap_set(DMA_SLAVE
, mask
);
720 atmel_port
->chan_tx
= dma_request_slave_channel(port
->dev
, "tx");
721 if (atmel_port
->chan_tx
== NULL
)
723 dev_info(port
->dev
, "using %s for tx DMA transfers\n",
724 dma_chan_name(atmel_port
->chan_tx
));
726 spin_lock_init(&atmel_port
->lock_tx
);
727 sg_init_table(&atmel_port
->sg_tx
, 1);
728 /* UART circular tx buffer is an aligned page. */
729 BUG_ON((int)port
->state
->xmit
.buf
& ~PAGE_MASK
);
730 sg_set_page(&atmel_port
->sg_tx
,
731 virt_to_page(port
->state
->xmit
.buf
),
733 (int)port
->state
->xmit
.buf
& ~PAGE_MASK
);
734 nent
= dma_map_sg(port
->dev
,
740 dev_dbg(port
->dev
, "need to release resource of dma\n");
743 dev_dbg(port
->dev
, "%s: mapped %d@%p to %x\n", __func__
,
744 sg_dma_len(&atmel_port
->sg_tx
),
745 port
->state
->xmit
.buf
,
746 sg_dma_address(&atmel_port
->sg_tx
));
749 /* Configure the slave DMA */
750 memset(&config
, 0, sizeof(config
));
751 config
.direction
= DMA_MEM_TO_DEV
;
752 config
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
753 config
.dst_addr
= port
->mapbase
+ ATMEL_US_THR
;
755 ret
= dmaengine_device_control(atmel_port
->chan_tx
,
757 (unsigned long)&config
);
759 dev_err(port
->dev
, "DMA tx slave configuration failed\n");
766 dev_err(port
->dev
, "TX channel not available, switch to pio\n");
767 atmel_port
->use_dma_tx
= 0;
768 if (atmel_port
->chan_tx
)
769 atmel_release_tx_dma(port
);
773 static void atmel_flip_buffer_rx_dma(struct uart_port
*port
,
774 char *buf
, size_t count
)
776 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
777 struct tty_port
*tport
= &port
->state
->port
;
779 dma_sync_sg_for_cpu(port
->dev
,
784 tty_insert_flip_string(tport
, buf
, count
);
786 dma_sync_sg_for_device(port
->dev
,
791 * Drop the lock here since it might end up calling
792 * uart_start(), which takes the lock.
794 spin_unlock(&port
->lock
);
795 tty_flip_buffer_push(tport
);
796 spin_lock(&port
->lock
);
799 static void atmel_complete_rx_dma(void *arg
)
801 struct uart_port
*port
= arg
;
802 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
804 tasklet_schedule(&atmel_port
->tasklet
);
807 static void atmel_release_rx_dma(struct uart_port
*port
)
809 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
810 struct dma_chan
*chan
= atmel_port
->chan_rx
;
813 dmaengine_terminate_all(chan
);
814 dma_release_channel(chan
);
815 dma_unmap_sg(port
->dev
, &atmel_port
->sg_rx
, 1,
819 atmel_port
->desc_rx
= NULL
;
820 atmel_port
->chan_rx
= NULL
;
821 atmel_port
->cookie_rx
= -EINVAL
;
824 static void atmel_rx_from_dma(struct uart_port
*port
)
826 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
827 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
828 struct dma_chan
*chan
= atmel_port
->chan_rx
;
829 struct dma_tx_state state
;
830 enum dma_status dmastat
;
831 size_t pending
, count
;
834 /* Reset the UART timeout early so that we don't miss one */
835 UART_PUT_CR(port
, ATMEL_US_STTTO
);
836 dmastat
= dmaengine_tx_status(chan
,
837 atmel_port
->cookie_rx
,
839 /* Restart a new tasklet if DMA status is error */
840 if (dmastat
== DMA_ERROR
) {
841 dev_dbg(port
->dev
, "Get residue error, restart tasklet\n");
842 UART_PUT_IER(port
, ATMEL_US_TIMEOUT
);
843 tasklet_schedule(&atmel_port
->tasklet
);
846 /* current transfer size should no larger than dma buffer */
847 pending
= sg_dma_len(&atmel_port
->sg_rx
) - state
.residue
;
848 BUG_ON(pending
> sg_dma_len(&atmel_port
->sg_rx
));
851 * This will take the chars we have so far,
852 * ring->head will record the transfer size, only new bytes come
853 * will insert into the framework.
855 if (pending
> ring
->head
) {
856 count
= pending
- ring
->head
;
858 atmel_flip_buffer_rx_dma(port
, ring
->buf
+ ring
->head
, count
);
861 if (ring
->head
== sg_dma_len(&atmel_port
->sg_rx
))
864 port
->icount
.rx
+= count
;
867 UART_PUT_IER(port
, ATMEL_US_TIMEOUT
);
870 static int atmel_prepare_rx_dma(struct uart_port
*port
)
872 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
873 struct dma_async_tx_descriptor
*desc
;
875 struct dma_slave_config config
;
876 struct circ_buf
*ring
;
879 ring
= &atmel_port
->rx_ring
;
882 dma_cap_set(DMA_CYCLIC
, mask
);
884 atmel_port
->chan_rx
= dma_request_slave_channel(port
->dev
, "rx");
885 if (atmel_port
->chan_rx
== NULL
)
887 dev_info(port
->dev
, "using %s for rx DMA transfers\n",
888 dma_chan_name(atmel_port
->chan_rx
));
890 spin_lock_init(&atmel_port
->lock_rx
);
891 sg_init_table(&atmel_port
->sg_rx
, 1);
892 /* UART circular rx buffer is an aligned page. */
893 BUG_ON((int)port
->state
->xmit
.buf
& ~PAGE_MASK
);
894 sg_set_page(&atmel_port
->sg_rx
,
895 virt_to_page(ring
->buf
),
896 ATMEL_SERIAL_RINGSIZE
,
897 (int)ring
->buf
& ~PAGE_MASK
);
898 nent
= dma_map_sg(port
->dev
,
904 dev_dbg(port
->dev
, "need to release resource of dma\n");
907 dev_dbg(port
->dev
, "%s: mapped %d@%p to %x\n", __func__
,
908 sg_dma_len(&atmel_port
->sg_rx
),
910 sg_dma_address(&atmel_port
->sg_rx
));
913 /* Configure the slave DMA */
914 memset(&config
, 0, sizeof(config
));
915 config
.direction
= DMA_DEV_TO_MEM
;
916 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
917 config
.src_addr
= port
->mapbase
+ ATMEL_US_RHR
;
919 ret
= dmaengine_device_control(atmel_port
->chan_rx
,
921 (unsigned long)&config
);
923 dev_err(port
->dev
, "DMA rx slave configuration failed\n");
927 * Prepare a cyclic dma transfer, assign 2 descriptors,
928 * each one is half ring buffer size
930 desc
= dmaengine_prep_dma_cyclic(atmel_port
->chan_rx
,
931 sg_dma_address(&atmel_port
->sg_rx
),
932 sg_dma_len(&atmel_port
->sg_rx
),
933 sg_dma_len(&atmel_port
->sg_rx
)/2,
936 desc
->callback
= atmel_complete_rx_dma
;
937 desc
->callback_param
= port
;
938 atmel_port
->desc_rx
= desc
;
939 atmel_port
->cookie_rx
= dmaengine_submit(desc
);
944 dev_err(port
->dev
, "RX channel not available, switch to pio\n");
945 atmel_port
->use_dma_rx
= 0;
946 if (atmel_port
->chan_rx
)
947 atmel_release_rx_dma(port
);
951 static void atmel_uart_timer_callback(unsigned long data
)
953 struct uart_port
*port
= (void *)data
;
954 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
956 tasklet_schedule(&atmel_port
->tasklet
);
957 mod_timer(&atmel_port
->uart_timer
, jiffies
+ uart_poll_timeout(port
));
961 * receive interrupt handler.
964 atmel_handle_receive(struct uart_port
*port
, unsigned int pending
)
966 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
968 if (atmel_use_pdc_rx(port
)) {
970 * PDC receive. Just schedule the tasklet and let it
971 * figure out the details.
973 * TODO: We're not handling error flags correctly at
976 if (pending
& (ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
)) {
977 UART_PUT_IDR(port
, (ATMEL_US_ENDRX
978 | ATMEL_US_TIMEOUT
));
979 tasklet_schedule(&atmel_port
->tasklet
);
982 if (pending
& (ATMEL_US_RXBRK
| ATMEL_US_OVRE
|
983 ATMEL_US_FRAME
| ATMEL_US_PARE
))
984 atmel_pdc_rxerr(port
, pending
);
987 if (atmel_use_dma_rx(port
)) {
988 if (pending
& ATMEL_US_TIMEOUT
) {
989 UART_PUT_IDR(port
, ATMEL_US_TIMEOUT
);
990 tasklet_schedule(&atmel_port
->tasklet
);
994 /* Interrupt receive */
995 if (pending
& ATMEL_US_RXRDY
)
996 atmel_rx_chars(port
);
997 else if (pending
& ATMEL_US_RXBRK
) {
999 * End of break detected. If it came along with a
1000 * character, atmel_rx_chars will handle it.
1002 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
1003 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
1004 atmel_port
->break_active
= 0;
1009 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1012 atmel_handle_transmit(struct uart_port
*port
, unsigned int pending
)
1014 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1016 if (pending
& atmel_port
->tx_done_mask
) {
1017 /* Either PDC or interrupt transmission */
1018 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
1019 tasklet_schedule(&atmel_port
->tasklet
);
1024 * status flags interrupt handler.
1027 atmel_handle_status(struct uart_port
*port
, unsigned int pending
,
1028 unsigned int status
)
1030 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1032 if (pending
& (ATMEL_US_RIIC
| ATMEL_US_DSRIC
| ATMEL_US_DCDIC
1033 | ATMEL_US_CTSIC
)) {
1034 atmel_port
->irq_status
= status
;
1035 tasklet_schedule(&atmel_port
->tasklet
);
1042 static irqreturn_t
atmel_interrupt(int irq
, void *dev_id
)
1044 struct uart_port
*port
= dev_id
;
1045 unsigned int status
, pending
, pass_counter
= 0;
1048 status
= UART_GET_CSR(port
);
1049 pending
= status
& UART_GET_IMR(port
);
1053 atmel_handle_receive(port
, pending
);
1054 atmel_handle_status(port
, pending
, status
);
1055 atmel_handle_transmit(port
, pending
);
1056 } while (pass_counter
++ < ATMEL_ISR_PASS_LIMIT
);
1058 return pass_counter
? IRQ_HANDLED
: IRQ_NONE
;
1061 static void atmel_release_tx_pdc(struct uart_port
*port
)
1063 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1064 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
1066 dma_unmap_single(port
->dev
,
1073 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1075 static void atmel_tx_pdc(struct uart_port
*port
)
1077 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1078 struct circ_buf
*xmit
= &port
->state
->xmit
;
1079 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
1082 /* nothing left to transmit? */
1083 if (UART_GET_TCR(port
))
1086 xmit
->tail
+= pdc
->ofs
;
1087 xmit
->tail
&= UART_XMIT_SIZE
- 1;
1089 port
->icount
.tx
+= pdc
->ofs
;
1092 /* more to transmit - setup next transfer */
1094 /* disable PDC transmit */
1095 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
1097 if (!uart_circ_empty(xmit
) && !uart_tx_stopped(port
)) {
1098 dma_sync_single_for_device(port
->dev
,
1103 count
= CIRC_CNT_TO_END(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
1106 UART_PUT_TPR(port
, pdc
->dma_addr
+ xmit
->tail
);
1107 UART_PUT_TCR(port
, count
);
1108 /* re-enable PDC transmit */
1109 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
1110 /* Enable interrupts */
1111 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
1113 if ((atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) &&
1114 !(atmel_port
->rs485
.flags
& SER_RS485_RX_DURING_TX
)) {
1115 /* DMA done, stop TX, start RX for RS485 */
1116 atmel_start_rx(port
);
1120 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
1121 uart_write_wakeup(port
);
1124 static int atmel_prepare_tx_pdc(struct uart_port
*port
)
1126 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1127 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
1128 struct circ_buf
*xmit
= &port
->state
->xmit
;
1130 pdc
->buf
= xmit
->buf
;
1131 pdc
->dma_addr
= dma_map_single(port
->dev
,
1135 pdc
->dma_size
= UART_XMIT_SIZE
;
1141 static void atmel_rx_from_ring(struct uart_port
*port
)
1143 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1144 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
1146 unsigned int status
;
1148 while (ring
->head
!= ring
->tail
) {
1149 struct atmel_uart_char c
;
1151 /* Make sure c is loaded after head. */
1154 c
= ((struct atmel_uart_char
*)ring
->buf
)[ring
->tail
];
1156 ring
->tail
= (ring
->tail
+ 1) & (ATMEL_SERIAL_RINGSIZE
- 1);
1163 * note that the error handling code is
1164 * out of the main execution path
1166 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
1167 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
))) {
1168 if (status
& ATMEL_US_RXBRK
) {
1169 /* ignore side-effect */
1170 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
1173 if (uart_handle_break(port
))
1176 if (status
& ATMEL_US_PARE
)
1177 port
->icount
.parity
++;
1178 if (status
& ATMEL_US_FRAME
)
1179 port
->icount
.frame
++;
1180 if (status
& ATMEL_US_OVRE
)
1181 port
->icount
.overrun
++;
1183 status
&= port
->read_status_mask
;
1185 if (status
& ATMEL_US_RXBRK
)
1187 else if (status
& ATMEL_US_PARE
)
1189 else if (status
& ATMEL_US_FRAME
)
1194 if (uart_handle_sysrq_char(port
, c
.ch
))
1197 uart_insert_char(port
, status
, ATMEL_US_OVRE
, c
.ch
, flg
);
1201 * Drop the lock here since it might end up calling
1202 * uart_start(), which takes the lock.
1204 spin_unlock(&port
->lock
);
1205 tty_flip_buffer_push(&port
->state
->port
);
1206 spin_lock(&port
->lock
);
1209 static void atmel_release_rx_pdc(struct uart_port
*port
)
1211 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1214 for (i
= 0; i
< 2; i
++) {
1215 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_rx
[i
];
1217 dma_unmap_single(port
->dev
,
1225 static void atmel_rx_from_pdc(struct uart_port
*port
)
1227 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1228 struct tty_port
*tport
= &port
->state
->port
;
1229 struct atmel_dma_buffer
*pdc
;
1230 int rx_idx
= atmel_port
->pdc_rx_idx
;
1236 /* Reset the UART timeout early so that we don't miss one */
1237 UART_PUT_CR(port
, ATMEL_US_STTTO
);
1239 pdc
= &atmel_port
->pdc_rx
[rx_idx
];
1240 head
= UART_GET_RPR(port
) - pdc
->dma_addr
;
1243 /* If the PDC has switched buffers, RPR won't contain
1244 * any address within the current buffer. Since head
1245 * is unsigned, we just need a one-way comparison to
1248 * In this case, we just need to consume the entire
1249 * buffer and resubmit it for DMA. This will clear the
1250 * ENDRX bit as well, so that we can safely re-enable
1251 * all interrupts below.
1253 head
= min(head
, pdc
->dma_size
);
1255 if (likely(head
!= tail
)) {
1256 dma_sync_single_for_cpu(port
->dev
, pdc
->dma_addr
,
1257 pdc
->dma_size
, DMA_FROM_DEVICE
);
1260 * head will only wrap around when we recycle
1261 * the DMA buffer, and when that happens, we
1262 * explicitly set tail to 0. So head will
1263 * always be greater than tail.
1265 count
= head
- tail
;
1267 tty_insert_flip_string(tport
, pdc
->buf
+ pdc
->ofs
,
1270 dma_sync_single_for_device(port
->dev
, pdc
->dma_addr
,
1271 pdc
->dma_size
, DMA_FROM_DEVICE
);
1273 port
->icount
.rx
+= count
;
1278 * If the current buffer is full, we need to check if
1279 * the next one contains any additional data.
1281 if (head
>= pdc
->dma_size
) {
1283 UART_PUT_RNPR(port
, pdc
->dma_addr
);
1284 UART_PUT_RNCR(port
, pdc
->dma_size
);
1287 atmel_port
->pdc_rx_idx
= rx_idx
;
1289 } while (head
>= pdc
->dma_size
);
1292 * Drop the lock here since it might end up calling
1293 * uart_start(), which takes the lock.
1295 spin_unlock(&port
->lock
);
1296 tty_flip_buffer_push(tport
);
1297 spin_lock(&port
->lock
);
1299 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
1302 static int atmel_prepare_rx_pdc(struct uart_port
*port
)
1304 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1307 for (i
= 0; i
< 2; i
++) {
1308 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_rx
[i
];
1310 pdc
->buf
= kmalloc(PDC_BUFFER_SIZE
, GFP_KERNEL
);
1311 if (pdc
->buf
== NULL
) {
1313 dma_unmap_single(port
->dev
,
1314 atmel_port
->pdc_rx
[0].dma_addr
,
1317 kfree(atmel_port
->pdc_rx
[0].buf
);
1319 atmel_port
->use_pdc_rx
= 0;
1322 pdc
->dma_addr
= dma_map_single(port
->dev
,
1326 pdc
->dma_size
= PDC_BUFFER_SIZE
;
1330 atmel_port
->pdc_rx_idx
= 0;
1332 UART_PUT_RPR(port
, atmel_port
->pdc_rx
[0].dma_addr
);
1333 UART_PUT_RCR(port
, PDC_BUFFER_SIZE
);
1335 UART_PUT_RNPR(port
, atmel_port
->pdc_rx
[1].dma_addr
);
1336 UART_PUT_RNCR(port
, PDC_BUFFER_SIZE
);
1342 * tasklet handling tty stuff outside the interrupt handler.
1344 static void atmel_tasklet_func(unsigned long data
)
1346 struct uart_port
*port
= (struct uart_port
*)data
;
1347 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1348 unsigned int status
;
1349 unsigned int status_change
;
1351 /* The interrupt handler does not take the lock */
1352 spin_lock(&port
->lock
);
1354 atmel_port
->schedule_tx(port
);
1356 status
= atmel_port
->irq_status
;
1357 status_change
= status
^ atmel_port
->irq_status_prev
;
1359 if (status_change
& (ATMEL_US_RI
| ATMEL_US_DSR
1360 | ATMEL_US_DCD
| ATMEL_US_CTS
)) {
1361 /* TODO: All reads to CSR will clear these interrupts! */
1362 if (status_change
& ATMEL_US_RI
)
1364 if (status_change
& ATMEL_US_DSR
)
1366 if (status_change
& ATMEL_US_DCD
)
1367 uart_handle_dcd_change(port
, !(status
& ATMEL_US_DCD
));
1368 if (status_change
& ATMEL_US_CTS
)
1369 uart_handle_cts_change(port
, !(status
& ATMEL_US_CTS
));
1371 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
1373 atmel_port
->irq_status_prev
= status
;
1376 atmel_port
->schedule_rx(port
);
1378 spin_unlock(&port
->lock
);
1381 static int atmel_init_property(struct atmel_uart_port
*atmel_port
,
1382 struct platform_device
*pdev
)
1384 struct device_node
*np
= pdev
->dev
.of_node
;
1385 struct atmel_uart_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1388 /* DMA/PDC usage specification */
1389 if (of_get_property(np
, "atmel,use-dma-rx", NULL
)) {
1390 if (of_get_property(np
, "dmas", NULL
)) {
1391 atmel_port
->use_dma_rx
= true;
1392 atmel_port
->use_pdc_rx
= false;
1394 atmel_port
->use_dma_rx
= false;
1395 atmel_port
->use_pdc_rx
= true;
1398 atmel_port
->use_dma_rx
= false;
1399 atmel_port
->use_pdc_rx
= false;
1402 if (of_get_property(np
, "atmel,use-dma-tx", NULL
)) {
1403 if (of_get_property(np
, "dmas", NULL
)) {
1404 atmel_port
->use_dma_tx
= true;
1405 atmel_port
->use_pdc_tx
= false;
1407 atmel_port
->use_dma_tx
= false;
1408 atmel_port
->use_pdc_tx
= true;
1411 atmel_port
->use_dma_tx
= false;
1412 atmel_port
->use_pdc_tx
= false;
1416 atmel_port
->use_pdc_rx
= pdata
->use_dma_rx
;
1417 atmel_port
->use_pdc_tx
= pdata
->use_dma_tx
;
1418 atmel_port
->use_dma_rx
= false;
1419 atmel_port
->use_dma_tx
= false;
1425 static void atmel_init_rs485(struct atmel_uart_port
*atmel_port
,
1426 struct platform_device
*pdev
)
1428 struct device_node
*np
= pdev
->dev
.of_node
;
1429 struct atmel_uart_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1433 /* rs485 properties */
1434 if (of_property_read_u32_array(np
, "rs485-rts-delay",
1435 rs485_delay
, 2) == 0) {
1436 struct serial_rs485
*rs485conf
= &atmel_port
->rs485
;
1438 rs485conf
->delay_rts_before_send
= rs485_delay
[0];
1439 rs485conf
->delay_rts_after_send
= rs485_delay
[1];
1440 rs485conf
->flags
= 0;
1442 if (of_get_property(np
, "rs485-rx-during-tx", NULL
))
1443 rs485conf
->flags
|= SER_RS485_RX_DURING_TX
;
1445 if (of_get_property(np
, "linux,rs485-enabled-at-boot-time",
1447 rs485conf
->flags
|= SER_RS485_ENABLED
;
1450 atmel_port
->rs485
= pdata
->rs485
;
1455 static void atmel_set_ops(struct uart_port
*port
)
1457 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1459 if (atmel_use_dma_rx(port
)) {
1460 atmel_port
->prepare_rx
= &atmel_prepare_rx_dma
;
1461 atmel_port
->schedule_rx
= &atmel_rx_from_dma
;
1462 atmel_port
->release_rx
= &atmel_release_rx_dma
;
1463 } else if (atmel_use_pdc_rx(port
)) {
1464 atmel_port
->prepare_rx
= &atmel_prepare_rx_pdc
;
1465 atmel_port
->schedule_rx
= &atmel_rx_from_pdc
;
1466 atmel_port
->release_rx
= &atmel_release_rx_pdc
;
1468 atmel_port
->prepare_rx
= NULL
;
1469 atmel_port
->schedule_rx
= &atmel_rx_from_ring
;
1470 atmel_port
->release_rx
= NULL
;
1473 if (atmel_use_dma_tx(port
)) {
1474 atmel_port
->prepare_tx
= &atmel_prepare_tx_dma
;
1475 atmel_port
->schedule_tx
= &atmel_tx_dma
;
1476 atmel_port
->release_tx
= &atmel_release_tx_dma
;
1477 } else if (atmel_use_pdc_tx(port
)) {
1478 atmel_port
->prepare_tx
= &atmel_prepare_tx_pdc
;
1479 atmel_port
->schedule_tx
= &atmel_tx_pdc
;
1480 atmel_port
->release_tx
= &atmel_release_tx_pdc
;
1482 atmel_port
->prepare_tx
= NULL
;
1483 atmel_port
->schedule_tx
= &atmel_tx_chars
;
1484 atmel_port
->release_tx
= NULL
;
1489 * Get ip name usart or uart
1491 static void atmel_get_ip_name(struct uart_port
*port
)
1493 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1494 int name
= UART_GET_IP_NAME(port
);
1497 /* usart and uart ascii */
1501 atmel_port
->is_usart
= false;
1503 if (name
== usart
) {
1504 dev_dbg(port
->dev
, "This is usart\n");
1505 atmel_port
->is_usart
= true;
1506 } else if (name
== uart
) {
1507 dev_dbg(port
->dev
, "This is uart\n");
1508 atmel_port
->is_usart
= false;
1510 /* fallback for older SoCs: use version field */
1511 version
= UART_GET_IP_VERSION(port
);
1515 dev_dbg(port
->dev
, "This version is usart\n");
1516 atmel_port
->is_usart
= true;
1520 dev_dbg(port
->dev
, "This version is uart\n");
1521 atmel_port
->is_usart
= false;
1524 dev_err(port
->dev
, "Not supported ip name nor version, set to uart\n");
1530 * Perform initialization and enable port for reception
1532 static int atmel_startup(struct uart_port
*port
)
1534 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1535 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1536 struct tty_struct
*tty
= port
->state
->port
.tty
;
1540 * Ensure that no interrupts are enabled otherwise when
1541 * request_irq() is called we could get stuck trying to
1542 * handle an unexpected interrupt
1544 UART_PUT_IDR(port
, -1);
1549 retval
= request_irq(port
->irq
, atmel_interrupt
, IRQF_SHARED
,
1550 tty
? tty
->name
: "atmel_serial", port
);
1552 printk("atmel_serial: atmel_startup - Can't get irq\n");
1557 * Initialize DMA (if necessary)
1559 atmel_init_property(atmel_port
, pdev
);
1561 if (atmel_port
->prepare_rx
) {
1562 retval
= atmel_port
->prepare_rx(port
);
1564 atmel_set_ops(port
);
1567 if (atmel_port
->prepare_tx
) {
1568 retval
= atmel_port
->prepare_tx(port
);
1570 atmel_set_ops(port
);
1573 * If there is a specific "open" function (to register
1574 * control line interrupts)
1576 if (atmel_open_hook
) {
1577 retval
= atmel_open_hook(port
);
1579 free_irq(port
->irq
, port
);
1584 /* Save current CSR for comparison in atmel_tasklet_func() */
1585 atmel_port
->irq_status_prev
= UART_GET_CSR(port
);
1586 atmel_port
->irq_status
= atmel_port
->irq_status_prev
;
1589 * Finally, enable the serial port
1591 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
1592 /* enable xmit & rcvr */
1593 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
1595 setup_timer(&atmel_port
->uart_timer
,
1596 atmel_uart_timer_callback
,
1597 (unsigned long)port
);
1599 if (atmel_use_pdc_rx(port
)) {
1600 /* set UART timeout */
1601 if (!atmel_port
->is_usart
) {
1602 mod_timer(&atmel_port
->uart_timer
,
1603 jiffies
+ uart_poll_timeout(port
));
1604 /* set USART timeout */
1606 UART_PUT_RTOR(port
, PDC_RX_TIMEOUT
);
1607 UART_PUT_CR(port
, ATMEL_US_STTTO
);
1609 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
1611 /* enable PDC controller */
1612 UART_PUT_PTCR(port
, ATMEL_PDC_RXTEN
);
1613 } else if (atmel_use_dma_rx(port
)) {
1614 /* set UART timeout */
1615 if (!atmel_port
->is_usart
) {
1616 mod_timer(&atmel_port
->uart_timer
,
1617 jiffies
+ uart_poll_timeout(port
));
1618 /* set USART timeout */
1620 UART_PUT_RTOR(port
, PDC_RX_TIMEOUT
);
1621 UART_PUT_CR(port
, ATMEL_US_STTTO
);
1623 UART_PUT_IER(port
, ATMEL_US_TIMEOUT
);
1626 /* enable receive only */
1627 UART_PUT_IER(port
, ATMEL_US_RXRDY
);
1636 static void atmel_shutdown(struct uart_port
*port
)
1638 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1641 * Prevent any tasklets being scheduled during
1644 del_timer_sync(&atmel_port
->uart_timer
);
1647 * Clear out any scheduled tasklets before
1648 * we destroy the buffers
1650 tasklet_kill(&atmel_port
->tasklet
);
1653 * Ensure everything is stopped and
1654 * disable all interrupts, port and break condition.
1656 atmel_stop_rx(port
);
1657 atmel_stop_tx(port
);
1659 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
1660 UART_PUT_IDR(port
, -1);
1664 * Shut-down the DMA.
1666 if (atmel_port
->release_rx
)
1667 atmel_port
->release_rx(port
);
1668 if (atmel_port
->release_tx
)
1669 atmel_port
->release_tx(port
);
1672 * Reset ring buffer pointers
1674 atmel_port
->rx_ring
.head
= 0;
1675 atmel_port
->rx_ring
.tail
= 0;
1678 * Free the interrupt
1680 free_irq(port
->irq
, port
);
1683 * If there is a specific "close" function (to unregister
1684 * control line interrupts)
1686 if (atmel_close_hook
)
1687 atmel_close_hook(port
);
1691 * Flush any TX data submitted for DMA. Called when the TX circular
1694 static void atmel_flush_buffer(struct uart_port
*port
)
1696 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1698 if (atmel_use_pdc_tx(port
)) {
1699 UART_PUT_TCR(port
, 0);
1700 atmel_port
->pdc_tx
.ofs
= 0;
1705 * Power / Clock management.
1707 static void atmel_serial_pm(struct uart_port
*port
, unsigned int state
,
1708 unsigned int oldstate
)
1710 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1715 * Enable the peripheral clock for this serial port.
1716 * This is called on uart_open() or a resume event.
1718 clk_prepare_enable(atmel_port
->clk
);
1720 /* re-enable interrupts if we disabled some on suspend */
1721 UART_PUT_IER(port
, atmel_port
->backup_imr
);
1724 /* Back up the interrupt mask and disable all interrupts */
1725 atmel_port
->backup_imr
= UART_GET_IMR(port
);
1726 UART_PUT_IDR(port
, -1);
1729 * Disable the peripheral clock for this serial port.
1730 * This is called on uart_close() or a suspend event.
1732 clk_disable_unprepare(atmel_port
->clk
);
1735 printk(KERN_ERR
"atmel_serial: unknown pm %d\n", state
);
1740 * Change the port parameters
1742 static void atmel_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1743 struct ktermios
*old
)
1745 unsigned long flags
;
1746 unsigned int mode
, imr
, quot
, baud
;
1747 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1749 /* Get current mode register */
1750 mode
= UART_GET_MR(port
) & ~(ATMEL_US_USCLKS
| ATMEL_US_CHRL
1751 | ATMEL_US_NBSTOP
| ATMEL_US_PAR
1754 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
1755 quot
= uart_get_divisor(port
, baud
);
1757 if (quot
> 65535) { /* BRGR is 16-bit, so switch to slower clock */
1759 mode
|= ATMEL_US_USCLKS_MCK_DIV8
;
1763 switch (termios
->c_cflag
& CSIZE
) {
1765 mode
|= ATMEL_US_CHRL_5
;
1768 mode
|= ATMEL_US_CHRL_6
;
1771 mode
|= ATMEL_US_CHRL_7
;
1774 mode
|= ATMEL_US_CHRL_8
;
1779 if (termios
->c_cflag
& CSTOPB
)
1780 mode
|= ATMEL_US_NBSTOP_2
;
1783 if (termios
->c_cflag
& PARENB
) {
1784 /* Mark or Space parity */
1785 if (termios
->c_cflag
& CMSPAR
) {
1786 if (termios
->c_cflag
& PARODD
)
1787 mode
|= ATMEL_US_PAR_MARK
;
1789 mode
|= ATMEL_US_PAR_SPACE
;
1790 } else if (termios
->c_cflag
& PARODD
)
1791 mode
|= ATMEL_US_PAR_ODD
;
1793 mode
|= ATMEL_US_PAR_EVEN
;
1795 mode
|= ATMEL_US_PAR_NONE
;
1797 /* hardware handshake (RTS/CTS) */
1798 if (termios
->c_cflag
& CRTSCTS
)
1799 mode
|= ATMEL_US_USMODE_HWHS
;
1801 mode
|= ATMEL_US_USMODE_NORMAL
;
1803 spin_lock_irqsave(&port
->lock
, flags
);
1805 port
->read_status_mask
= ATMEL_US_OVRE
;
1806 if (termios
->c_iflag
& INPCK
)
1807 port
->read_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
1808 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
1809 port
->read_status_mask
|= ATMEL_US_RXBRK
;
1811 if (atmel_use_pdc_rx(port
))
1812 /* need to enable error interrupts */
1813 UART_PUT_IER(port
, port
->read_status_mask
);
1816 * Characters to ignore
1818 port
->ignore_status_mask
= 0;
1819 if (termios
->c_iflag
& IGNPAR
)
1820 port
->ignore_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
1821 if (termios
->c_iflag
& IGNBRK
) {
1822 port
->ignore_status_mask
|= ATMEL_US_RXBRK
;
1824 * If we're ignoring parity and break indicators,
1825 * ignore overruns too (for real raw support).
1827 if (termios
->c_iflag
& IGNPAR
)
1828 port
->ignore_status_mask
|= ATMEL_US_OVRE
;
1830 /* TODO: Ignore all characters if CREAD is set.*/
1832 /* update the per-port timeout */
1833 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1836 * save/disable interrupts. The tty layer will ensure that the
1837 * transmitter is empty if requested by the caller, so there's
1838 * no need to wait for it here.
1840 imr
= UART_GET_IMR(port
);
1841 UART_PUT_IDR(port
, -1);
1843 /* disable receiver and transmitter */
1844 UART_PUT_CR(port
, ATMEL_US_TXDIS
| ATMEL_US_RXDIS
);
1846 /* Resetting serial mode to RS232 (0x0) */
1847 mode
&= ~ATMEL_US_USMODE
;
1849 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
1850 dev_dbg(port
->dev
, "Setting UART to RS485\n");
1851 if ((atmel_port
->rs485
.delay_rts_after_send
) > 0)
1853 atmel_port
->rs485
.delay_rts_after_send
);
1854 mode
|= ATMEL_US_USMODE_RS485
;
1856 dev_dbg(port
->dev
, "Setting UART to RS232\n");
1859 /* set the parity, stop bits and data size */
1860 UART_PUT_MR(port
, mode
);
1862 /* set the baud rate */
1863 UART_PUT_BRGR(port
, quot
);
1864 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
1865 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
1867 /* restore interrupts */
1868 UART_PUT_IER(port
, imr
);
1870 /* CTS flow-control and modem-status interrupts */
1871 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
1872 port
->ops
->enable_ms(port
);
1874 spin_unlock_irqrestore(&port
->lock
, flags
);
1877 static void atmel_set_ldisc(struct uart_port
*port
, int new)
1880 port
->flags
|= UPF_HARDPPS_CD
;
1881 atmel_enable_ms(port
);
1883 port
->flags
&= ~UPF_HARDPPS_CD
;
1888 * Return string describing the specified port
1890 static const char *atmel_type(struct uart_port
*port
)
1892 return (port
->type
== PORT_ATMEL
) ? "ATMEL_SERIAL" : NULL
;
1896 * Release the memory region(s) being used by 'port'.
1898 static void atmel_release_port(struct uart_port
*port
)
1900 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1901 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
1903 release_mem_region(port
->mapbase
, size
);
1905 if (port
->flags
& UPF_IOREMAP
) {
1906 iounmap(port
->membase
);
1907 port
->membase
= NULL
;
1912 * Request the memory region(s) being used by 'port'.
1914 static int atmel_request_port(struct uart_port
*port
)
1916 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1917 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
1919 if (!request_mem_region(port
->mapbase
, size
, "atmel_serial"))
1922 if (port
->flags
& UPF_IOREMAP
) {
1923 port
->membase
= ioremap(port
->mapbase
, size
);
1924 if (port
->membase
== NULL
) {
1925 release_mem_region(port
->mapbase
, size
);
1934 * Configure/autoconfigure the port.
1936 static void atmel_config_port(struct uart_port
*port
, int flags
)
1938 if (flags
& UART_CONFIG_TYPE
) {
1939 port
->type
= PORT_ATMEL
;
1940 atmel_request_port(port
);
1945 * Verify the new serial_struct (for TIOCSSERIAL).
1947 static int atmel_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1950 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_ATMEL
)
1952 if (port
->irq
!= ser
->irq
)
1954 if (ser
->io_type
!= SERIAL_IO_MEM
)
1956 if (port
->uartclk
/ 16 != ser
->baud_base
)
1958 if ((void *)port
->mapbase
!= ser
->iomem_base
)
1960 if (port
->iobase
!= ser
->port
)
1967 #ifdef CONFIG_CONSOLE_POLL
1968 static int atmel_poll_get_char(struct uart_port
*port
)
1970 while (!(UART_GET_CSR(port
) & ATMEL_US_RXRDY
))
1973 return UART_GET_CHAR(port
);
1976 static void atmel_poll_put_char(struct uart_port
*port
, unsigned char ch
)
1978 while (!(UART_GET_CSR(port
) & ATMEL_US_TXRDY
))
1981 UART_PUT_CHAR(port
, ch
);
1986 atmel_ioctl(struct uart_port
*port
, unsigned int cmd
, unsigned long arg
)
1988 struct serial_rs485 rs485conf
;
1992 if (copy_from_user(&rs485conf
, (struct serial_rs485
*) arg
,
1996 atmel_config_rs485(port
, &rs485conf
);
2000 if (copy_to_user((struct serial_rs485
*) arg
,
2001 &(to_atmel_uart_port(port
)->rs485
),
2007 return -ENOIOCTLCMD
;
2014 static struct uart_ops atmel_pops
= {
2015 .tx_empty
= atmel_tx_empty
,
2016 .set_mctrl
= atmel_set_mctrl
,
2017 .get_mctrl
= atmel_get_mctrl
,
2018 .stop_tx
= atmel_stop_tx
,
2019 .start_tx
= atmel_start_tx
,
2020 .stop_rx
= atmel_stop_rx
,
2021 .enable_ms
= atmel_enable_ms
,
2022 .break_ctl
= atmel_break_ctl
,
2023 .startup
= atmel_startup
,
2024 .shutdown
= atmel_shutdown
,
2025 .flush_buffer
= atmel_flush_buffer
,
2026 .set_termios
= atmel_set_termios
,
2027 .set_ldisc
= atmel_set_ldisc
,
2029 .release_port
= atmel_release_port
,
2030 .request_port
= atmel_request_port
,
2031 .config_port
= atmel_config_port
,
2032 .verify_port
= atmel_verify_port
,
2033 .pm
= atmel_serial_pm
,
2034 .ioctl
= atmel_ioctl
,
2035 #ifdef CONFIG_CONSOLE_POLL
2036 .poll_get_char
= atmel_poll_get_char
,
2037 .poll_put_char
= atmel_poll_put_char
,
2042 * Configure the port from the platform device resource info.
2044 static int atmel_init_port(struct atmel_uart_port
*atmel_port
,
2045 struct platform_device
*pdev
)
2048 struct uart_port
*port
= &atmel_port
->uart
;
2049 struct atmel_uart_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2051 if (!atmel_init_property(atmel_port
, pdev
))
2052 atmel_set_ops(port
);
2054 atmel_init_rs485(atmel_port
, pdev
);
2056 port
->iotype
= UPIO_MEM
;
2057 port
->flags
= UPF_BOOT_AUTOCONF
;
2058 port
->ops
= &atmel_pops
;
2060 port
->dev
= &pdev
->dev
;
2061 port
->mapbase
= pdev
->resource
[0].start
;
2062 port
->irq
= pdev
->resource
[1].start
;
2064 tasklet_init(&atmel_port
->tasklet
, atmel_tasklet_func
,
2065 (unsigned long)port
);
2067 memset(&atmel_port
->rx_ring
, 0, sizeof(atmel_port
->rx_ring
));
2069 if (pdata
&& pdata
->regs
) {
2070 /* Already mapped by setup code */
2071 port
->membase
= pdata
->regs
;
2073 port
->flags
|= UPF_IOREMAP
;
2074 port
->membase
= NULL
;
2077 /* for console, the clock could already be configured */
2078 if (!atmel_port
->clk
) {
2079 atmel_port
->clk
= clk_get(&pdev
->dev
, "usart");
2080 if (IS_ERR(atmel_port
->clk
)) {
2081 ret
= PTR_ERR(atmel_port
->clk
);
2082 atmel_port
->clk
= NULL
;
2085 ret
= clk_prepare_enable(atmel_port
->clk
);
2087 clk_put(atmel_port
->clk
);
2088 atmel_port
->clk
= NULL
;
2091 port
->uartclk
= clk_get_rate(atmel_port
->clk
);
2092 clk_disable_unprepare(atmel_port
->clk
);
2093 /* only enable clock when USART is in use */
2096 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2097 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
)
2098 atmel_port
->tx_done_mask
= ATMEL_US_TXEMPTY
;
2099 else if (atmel_use_pdc_tx(port
)) {
2100 port
->fifosize
= PDC_BUFFER_SIZE
;
2101 atmel_port
->tx_done_mask
= ATMEL_US_ENDTX
| ATMEL_US_TXBUFE
;
2103 atmel_port
->tx_done_mask
= ATMEL_US_TXRDY
;
2109 struct platform_device
*atmel_default_console_device
; /* the serial console device */
2111 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2112 static void atmel_console_putchar(struct uart_port
*port
, int ch
)
2114 while (!(UART_GET_CSR(port
) & ATMEL_US_TXRDY
))
2116 UART_PUT_CHAR(port
, ch
);
2120 * Interrupts are disabled on entering
2122 static void atmel_console_write(struct console
*co
, const char *s
, u_int count
)
2124 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
2125 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
2126 unsigned int status
, imr
;
2127 unsigned int pdc_tx
;
2130 * First, save IMR and then disable interrupts
2132 imr
= UART_GET_IMR(port
);
2133 UART_PUT_IDR(port
, ATMEL_US_RXRDY
| atmel_port
->tx_done_mask
);
2135 /* Store PDC transmit status and disable it */
2136 pdc_tx
= UART_GET_PTSR(port
) & ATMEL_PDC_TXTEN
;
2137 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
2139 uart_console_write(port
, s
, count
, atmel_console_putchar
);
2142 * Finally, wait for transmitter to become empty
2146 status
= UART_GET_CSR(port
);
2147 } while (!(status
& ATMEL_US_TXRDY
));
2149 /* Restore PDC transmit status */
2151 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
2153 /* set interrupts back the way they were */
2154 UART_PUT_IER(port
, imr
);
2158 * If the port was already initialised (eg, by a boot loader),
2159 * try to determine the current setup.
2161 static void __init
atmel_console_get_options(struct uart_port
*port
, int *baud
,
2162 int *parity
, int *bits
)
2164 unsigned int mr
, quot
;
2167 * If the baud rate generator isn't running, the port wasn't
2168 * initialized by the boot loader.
2170 quot
= UART_GET_BRGR(port
) & ATMEL_US_CD
;
2174 mr
= UART_GET_MR(port
) & ATMEL_US_CHRL
;
2175 if (mr
== ATMEL_US_CHRL_8
)
2180 mr
= UART_GET_MR(port
) & ATMEL_US_PAR
;
2181 if (mr
== ATMEL_US_PAR_EVEN
)
2183 else if (mr
== ATMEL_US_PAR_ODD
)
2187 * The serial core only rounds down when matching this to a
2188 * supported baud rate. Make sure we don't end up slightly
2189 * lower than one of those, as it would make us fall through
2190 * to a much lower baud rate than we really want.
2192 *baud
= port
->uartclk
/ (16 * (quot
- 1));
2195 static int __init
atmel_console_setup(struct console
*co
, char *options
)
2198 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
2204 if (port
->membase
== NULL
) {
2205 /* Port not initialized yet - delay setup */
2209 ret
= clk_prepare_enable(atmel_ports
[co
->index
].clk
);
2213 UART_PUT_IDR(port
, -1);
2214 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
2215 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
2218 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2220 atmel_console_get_options(port
, &baud
, &parity
, &bits
);
2222 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
2225 static struct uart_driver atmel_uart
;
2227 static struct console atmel_console
= {
2228 .name
= ATMEL_DEVICENAME
,
2229 .write
= atmel_console_write
,
2230 .device
= uart_console_device
,
2231 .setup
= atmel_console_setup
,
2232 .flags
= CON_PRINTBUFFER
,
2234 .data
= &atmel_uart
,
2237 #define ATMEL_CONSOLE_DEVICE (&atmel_console)
2240 * Early console initialization (before VM subsystem initialized).
2242 static int __init
atmel_console_init(void)
2245 if (atmel_default_console_device
) {
2246 struct atmel_uart_data
*pdata
=
2247 dev_get_platdata(&atmel_default_console_device
->dev
);
2248 int id
= pdata
->num
;
2249 struct atmel_uart_port
*port
= &atmel_ports
[id
];
2251 port
->backup_imr
= 0;
2252 port
->uart
.line
= id
;
2254 add_preferred_console(ATMEL_DEVICENAME
, id
, NULL
);
2255 ret
= atmel_init_port(port
, atmel_default_console_device
);
2258 register_console(&atmel_console
);
2264 console_initcall(atmel_console_init
);
2267 * Late console initialization.
2269 static int __init
atmel_late_console_init(void)
2271 if (atmel_default_console_device
2272 && !(atmel_console
.flags
& CON_ENABLED
))
2273 register_console(&atmel_console
);
2278 core_initcall(atmel_late_console_init
);
2280 static inline bool atmel_is_console_port(struct uart_port
*port
)
2282 return port
->cons
&& port
->cons
->index
== port
->line
;
2286 #define ATMEL_CONSOLE_DEVICE NULL
2288 static inline bool atmel_is_console_port(struct uart_port
*port
)
2294 static struct uart_driver atmel_uart
= {
2295 .owner
= THIS_MODULE
,
2296 .driver_name
= "atmel_serial",
2297 .dev_name
= ATMEL_DEVICENAME
,
2298 .major
= SERIAL_ATMEL_MAJOR
,
2299 .minor
= MINOR_START
,
2300 .nr
= ATMEL_MAX_UART
,
2301 .cons
= ATMEL_CONSOLE_DEVICE
,
2305 static bool atmel_serial_clk_will_stop(void)
2307 #ifdef CONFIG_ARCH_AT91
2308 return at91_suspend_entering_slow_clock();
2314 static int atmel_serial_suspend(struct platform_device
*pdev
,
2317 struct uart_port
*port
= platform_get_drvdata(pdev
);
2318 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
2320 if (atmel_is_console_port(port
) && console_suspend_enabled
) {
2321 /* Drain the TX shifter */
2322 while (!(UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
))
2326 /* we can not wake up if we're running on slow clock */
2327 atmel_port
->may_wakeup
= device_may_wakeup(&pdev
->dev
);
2328 if (atmel_serial_clk_will_stop())
2329 device_set_wakeup_enable(&pdev
->dev
, 0);
2331 uart_suspend_port(&atmel_uart
, port
);
2336 static int atmel_serial_resume(struct platform_device
*pdev
)
2338 struct uart_port
*port
= platform_get_drvdata(pdev
);
2339 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
2341 uart_resume_port(&atmel_uart
, port
);
2342 device_set_wakeup_enable(&pdev
->dev
, atmel_port
->may_wakeup
);
2347 #define atmel_serial_suspend NULL
2348 #define atmel_serial_resume NULL
2351 static int atmel_serial_probe(struct platform_device
*pdev
)
2353 struct atmel_uart_port
*port
;
2354 struct device_node
*np
= pdev
->dev
.of_node
;
2355 struct atmel_uart_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2359 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE
& (ATMEL_SERIAL_RINGSIZE
- 1));
2362 ret
= of_alias_get_id(np
, "serial");
2368 /* port id not found in platform data nor device-tree aliases:
2369 * auto-enumerate it */
2370 ret
= find_first_zero_bit(atmel_ports_in_use
, ATMEL_MAX_UART
);
2372 if (ret
>= ATMEL_MAX_UART
) {
2377 if (test_and_set_bit(ret
, atmel_ports_in_use
)) {
2378 /* port already in use */
2383 port
= &atmel_ports
[ret
];
2384 port
->backup_imr
= 0;
2385 port
->uart
.line
= ret
;
2386 port
->rts_gpio
= -EINVAL
; /* Invalid, zero could be valid */
2388 port
->rts_gpio
= pdata
->rts_gpio
;
2390 port
->rts_gpio
= of_get_named_gpio(np
, "rts-gpios", 0);
2392 if (gpio_is_valid(port
->rts_gpio
)) {
2393 ret
= devm_gpio_request(&pdev
->dev
, port
->rts_gpio
, "RTS");
2395 dev_err(&pdev
->dev
, "error requesting RTS GPIO\n");
2398 /* Default to 1 as RTS is active low */
2399 ret
= gpio_direction_output(port
->rts_gpio
, 1);
2401 dev_err(&pdev
->dev
, "error setting up RTS GPIO\n");
2406 ret
= atmel_init_port(port
, pdev
);
2410 if (!atmel_use_pdc_rx(&port
->uart
)) {
2412 data
= kmalloc(sizeof(struct atmel_uart_char
)
2413 * ATMEL_SERIAL_RINGSIZE
, GFP_KERNEL
);
2415 goto err_alloc_ring
;
2416 port
->rx_ring
.buf
= data
;
2419 ret
= uart_add_one_port(&atmel_uart
, &port
->uart
);
2423 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2424 if (atmel_is_console_port(&port
->uart
)
2425 && ATMEL_CONSOLE_DEVICE
->flags
& CON_ENABLED
) {
2427 * The serial core enabled the clock for us, so undo
2428 * the clk_prepare_enable() in atmel_console_setup()
2430 clk_disable_unprepare(port
->clk
);
2434 device_init_wakeup(&pdev
->dev
, 1);
2435 platform_set_drvdata(pdev
, port
);
2437 if (port
->rs485
.flags
& SER_RS485_ENABLED
) {
2438 UART_PUT_MR(&port
->uart
, ATMEL_US_USMODE_NORMAL
);
2439 UART_PUT_CR(&port
->uart
, ATMEL_US_RTSEN
);
2443 * Get port name of usart or uart
2445 atmel_get_ip_name(&port
->uart
);
2450 kfree(port
->rx_ring
.buf
);
2451 port
->rx_ring
.buf
= NULL
;
2453 if (!atmel_is_console_port(&port
->uart
)) {
2461 static int atmel_serial_remove(struct platform_device
*pdev
)
2463 struct uart_port
*port
= platform_get_drvdata(pdev
);
2464 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
2467 tasklet_kill(&atmel_port
->tasklet
);
2469 device_init_wakeup(&pdev
->dev
, 0);
2471 ret
= uart_remove_one_port(&atmel_uart
, port
);
2473 kfree(atmel_port
->rx_ring
.buf
);
2475 /* "port" is allocated statically, so we shouldn't free it */
2477 clear_bit(port
->line
, atmel_ports_in_use
);
2479 clk_put(atmel_port
->clk
);
2484 static struct platform_driver atmel_serial_driver
= {
2485 .probe
= atmel_serial_probe
,
2486 .remove
= atmel_serial_remove
,
2487 .suspend
= atmel_serial_suspend
,
2488 .resume
= atmel_serial_resume
,
2490 .name
= "atmel_usart",
2491 .owner
= THIS_MODULE
,
2492 .of_match_table
= of_match_ptr(atmel_serial_dt_ids
),
2496 static int __init
atmel_serial_init(void)
2500 ret
= uart_register_driver(&atmel_uart
);
2504 ret
= platform_driver_register(&atmel_serial_driver
);
2506 uart_unregister_driver(&atmel_uart
);
2511 static void __exit
atmel_serial_exit(void)
2513 platform_driver_unregister(&atmel_serial_driver
);
2514 uart_unregister_driver(&atmel_uart
);
2517 module_init(atmel_serial_init
);
2518 module_exit(atmel_serial_exit
);
2520 MODULE_AUTHOR("Rick Bronson");
2521 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2522 MODULE_LICENSE("GPL");
2523 MODULE_ALIAS("platform:atmel_usart");