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>
46 #include <linux/gpio/consumer.h>
47 #include <linux/err.h>
48 #include <linux/irq.h>
51 #include <asm/ioctls.h>
53 #define PDC_BUFFER_SIZE 512
54 /* Revisit: We should calculate this based on the actual port settings */
55 #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
57 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
61 #include <linux/serial_core.h>
63 #include "serial_mctrl_gpio.h"
65 static void atmel_start_rx(struct uart_port
*port
);
66 static void atmel_stop_rx(struct uart_port
*port
);
68 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
70 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
71 * should coexist with the 8250 driver, such as if we have an external 16C550
73 #define SERIAL_ATMEL_MAJOR 204
74 #define MINOR_START 154
75 #define ATMEL_DEVICENAME "ttyAT"
79 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
80 * name, but it is legally reserved for the 8250 driver. */
81 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
82 #define MINOR_START 64
83 #define ATMEL_DEVICENAME "ttyS"
87 #define ATMEL_ISR_PASS_LIMIT 256
89 /* UART registers. CR is write-only, hence no GET macro */
90 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
91 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
92 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
93 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
94 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
95 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
96 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
97 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
98 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
99 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
100 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
101 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
102 #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
103 #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
104 #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
107 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
108 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
110 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
111 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
112 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
113 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
114 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
116 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
117 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
118 #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
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 bool use_dma_rx
; /* enable DMA receiver */
145 bool use_pdc_rx
; /* enable PDC receiver */
146 short pdc_rx_idx
; /* current PDC RX buffer */
147 struct atmel_dma_buffer pdc_rx
[2]; /* PDC receier */
149 bool use_dma_tx
; /* enable DMA transmitter */
150 bool use_pdc_tx
; /* enable PDC transmitter */
151 struct atmel_dma_buffer pdc_tx
; /* PDC transmitter */
153 spinlock_t lock_tx
; /* port lock */
154 spinlock_t lock_rx
; /* port lock */
155 struct dma_chan
*chan_tx
;
156 struct dma_chan
*chan_rx
;
157 struct dma_async_tx_descriptor
*desc_tx
;
158 struct dma_async_tx_descriptor
*desc_rx
;
159 dma_cookie_t cookie_tx
;
160 dma_cookie_t cookie_rx
;
161 struct scatterlist sg_tx
;
162 struct scatterlist sg_rx
;
163 struct tasklet_struct tasklet
;
164 unsigned int irq_status
;
165 unsigned int irq_status_prev
;
167 struct circ_buf rx_ring
;
169 struct serial_rs485 rs485
; /* rs485 settings */
170 struct mctrl_gpios
*gpios
;
171 int gpio_irq
[UART_GPIO_MAX
];
172 unsigned int tx_done_mask
;
174 bool is_usart
; /* usart or uart */
175 struct timer_list uart_timer
; /* uart timer */
176 int (*prepare_rx
)(struct uart_port
*port
);
177 int (*prepare_tx
)(struct uart_port
*port
);
178 void (*schedule_rx
)(struct uart_port
*port
);
179 void (*schedule_tx
)(struct uart_port
*port
);
180 void (*release_rx
)(struct uart_port
*port
);
181 void (*release_tx
)(struct uart_port
*port
);
184 static struct atmel_uart_port atmel_ports
[ATMEL_MAX_UART
];
185 static DECLARE_BITMAP(atmel_ports_in_use
, ATMEL_MAX_UART
);
188 static struct console atmel_console
;
191 #if defined(CONFIG_OF)
192 static const struct of_device_id atmel_serial_dt_ids
[] = {
193 { .compatible
= "atmel,at91rm9200-usart" },
194 { .compatible
= "atmel,at91sam9260-usart" },
198 MODULE_DEVICE_TABLE(of
, atmel_serial_dt_ids
);
201 static inline struct atmel_uart_port
*
202 to_atmel_uart_port(struct uart_port
*uart
)
204 return container_of(uart
, struct atmel_uart_port
, uart
);
207 #ifdef CONFIG_SERIAL_ATMEL_PDC
208 static bool atmel_use_pdc_rx(struct uart_port
*port
)
210 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
212 return atmel_port
->use_pdc_rx
;
215 static bool atmel_use_pdc_tx(struct uart_port
*port
)
217 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
219 return atmel_port
->use_pdc_tx
;
222 static bool atmel_use_pdc_rx(struct uart_port
*port
)
227 static bool atmel_use_pdc_tx(struct uart_port
*port
)
233 static bool atmel_use_dma_tx(struct uart_port
*port
)
235 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
237 return atmel_port
->use_dma_tx
;
240 static bool atmel_use_dma_rx(struct uart_port
*port
)
242 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
244 return atmel_port
->use_dma_rx
;
247 static unsigned int atmel_get_lines_status(struct uart_port
*port
)
249 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
250 unsigned int status
, ret
= 0;
252 status
= UART_GET_CSR(port
);
254 mctrl_gpio_get(atmel_port
->gpios
, &ret
);
256 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port
->gpios
,
259 status
&= ~ATMEL_US_CTS
;
261 status
|= ATMEL_US_CTS
;
264 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port
->gpios
,
267 status
&= ~ATMEL_US_DSR
;
269 status
|= ATMEL_US_DSR
;
272 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port
->gpios
,
275 status
&= ~ATMEL_US_RI
;
277 status
|= ATMEL_US_RI
;
280 if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port
->gpios
,
283 status
&= ~ATMEL_US_DCD
;
285 status
|= ATMEL_US_DCD
;
291 /* Enable or disable the rs485 support */
292 void atmel_config_rs485(struct uart_port
*port
, struct serial_rs485
*rs485conf
)
294 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
298 spin_lock_irqsave(&port
->lock
, flags
);
300 /* Disable interrupts */
301 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
303 mode
= UART_GET_MR(port
);
305 /* Resetting serial mode to RS232 (0x0) */
306 mode
&= ~ATMEL_US_USMODE
;
308 atmel_port
->rs485
= *rs485conf
;
310 if (rs485conf
->flags
& SER_RS485_ENABLED
) {
311 dev_dbg(port
->dev
, "Setting UART to RS485\n");
312 atmel_port
->tx_done_mask
= ATMEL_US_TXEMPTY
;
313 if ((rs485conf
->delay_rts_after_send
) > 0)
314 UART_PUT_TTGR(port
, rs485conf
->delay_rts_after_send
);
315 mode
|= ATMEL_US_USMODE_RS485
;
317 dev_dbg(port
->dev
, "Setting UART to RS232\n");
318 if (atmel_use_pdc_tx(port
))
319 atmel_port
->tx_done_mask
= ATMEL_US_ENDTX
|
322 atmel_port
->tx_done_mask
= ATMEL_US_TXRDY
;
324 UART_PUT_MR(port
, mode
);
326 /* Enable interrupts */
327 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
329 spin_unlock_irqrestore(&port
->lock
, flags
);
334 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
336 static u_int
atmel_tx_empty(struct uart_port
*port
)
338 return (UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
) ? TIOCSER_TEMT
: 0;
342 * Set state of the modem control output lines
344 static void atmel_set_mctrl(struct uart_port
*port
, u_int mctrl
)
346 unsigned int control
= 0;
348 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
350 if (mctrl
& TIOCM_RTS
)
351 control
|= ATMEL_US_RTSEN
;
353 control
|= ATMEL_US_RTSDIS
;
355 if (mctrl
& TIOCM_DTR
)
356 control
|= ATMEL_US_DTREN
;
358 control
|= ATMEL_US_DTRDIS
;
360 UART_PUT_CR(port
, control
);
362 mctrl_gpio_set(atmel_port
->gpios
, mctrl
);
364 /* Local loopback mode? */
365 mode
= UART_GET_MR(port
) & ~ATMEL_US_CHMODE
;
366 if (mctrl
& TIOCM_LOOP
)
367 mode
|= ATMEL_US_CHMODE_LOC_LOOP
;
369 mode
|= ATMEL_US_CHMODE_NORMAL
;
371 /* Resetting serial mode to RS232 (0x0) */
372 mode
&= ~ATMEL_US_USMODE
;
374 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
375 dev_dbg(port
->dev
, "Setting UART to RS485\n");
376 if ((atmel_port
->rs485
.delay_rts_after_send
) > 0)
378 atmel_port
->rs485
.delay_rts_after_send
);
379 mode
|= ATMEL_US_USMODE_RS485
;
381 dev_dbg(port
->dev
, "Setting UART to RS232\n");
383 UART_PUT_MR(port
, mode
);
387 * Get state of the modem control input lines
389 static u_int
atmel_get_mctrl(struct uart_port
*port
)
391 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
392 unsigned int ret
= 0, status
;
394 status
= UART_GET_CSR(port
);
397 * The control signals are active low.
399 if (!(status
& ATMEL_US_DCD
))
401 if (!(status
& ATMEL_US_CTS
))
403 if (!(status
& ATMEL_US_DSR
))
405 if (!(status
& ATMEL_US_RI
))
408 return mctrl_gpio_get(atmel_port
->gpios
, &ret
);
414 static void atmel_stop_tx(struct uart_port
*port
)
416 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
418 if (atmel_use_pdc_tx(port
)) {
419 /* disable PDC transmit */
420 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
422 /* Disable interrupts */
423 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
425 if ((atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) &&
426 !(atmel_port
->rs485
.flags
& SER_RS485_RX_DURING_TX
))
427 atmel_start_rx(port
);
431 * Start transmitting.
433 static void atmel_start_tx(struct uart_port
*port
)
435 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
437 if (atmel_use_pdc_tx(port
)) {
438 if (UART_GET_PTSR(port
) & ATMEL_PDC_TXTEN
)
439 /* The transmitter is already running. Yes, we
443 if ((atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) &&
444 !(atmel_port
->rs485
.flags
& SER_RS485_RX_DURING_TX
))
447 /* re-enable PDC transmit */
448 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
450 /* Enable interrupts */
451 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
455 * start receiving - port is in process of being opened.
457 static void atmel_start_rx(struct uart_port
*port
)
459 UART_PUT_CR(port
, ATMEL_US_RSTSTA
); /* reset status and receiver */
461 UART_PUT_CR(port
, ATMEL_US_RXEN
);
463 if (atmel_use_pdc_rx(port
)) {
464 /* enable PDC controller */
465 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
|
466 port
->read_status_mask
);
467 UART_PUT_PTCR(port
, ATMEL_PDC_RXTEN
);
469 UART_PUT_IER(port
, ATMEL_US_RXRDY
);
474 * Stop receiving - port is in process of being closed.
476 static void atmel_stop_rx(struct uart_port
*port
)
478 UART_PUT_CR(port
, ATMEL_US_RXDIS
);
480 if (atmel_use_pdc_rx(port
)) {
481 /* disable PDC receive */
482 UART_PUT_PTCR(port
, ATMEL_PDC_RXTDIS
);
483 UART_PUT_IDR(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
|
484 port
->read_status_mask
);
486 UART_PUT_IDR(port
, ATMEL_US_RXRDY
);
491 * Enable modem status interrupts
493 static void atmel_enable_ms(struct uart_port
*port
)
495 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
499 * Interrupt should not be enabled twice
501 if (atmel_port
->ms_irq_enabled
)
504 atmel_port
->ms_irq_enabled
= true;
506 if (atmel_port
->gpio_irq
[UART_GPIO_CTS
] >= 0)
507 enable_irq(atmel_port
->gpio_irq
[UART_GPIO_CTS
]);
509 ier
|= ATMEL_US_CTSIC
;
511 if (atmel_port
->gpio_irq
[UART_GPIO_DSR
] >= 0)
512 enable_irq(atmel_port
->gpio_irq
[UART_GPIO_DSR
]);
514 ier
|= ATMEL_US_DSRIC
;
516 if (atmel_port
->gpio_irq
[UART_GPIO_RI
] >= 0)
517 enable_irq(atmel_port
->gpio_irq
[UART_GPIO_RI
]);
519 ier
|= ATMEL_US_RIIC
;
521 if (atmel_port
->gpio_irq
[UART_GPIO_DCD
] >= 0)
522 enable_irq(atmel_port
->gpio_irq
[UART_GPIO_DCD
]);
524 ier
|= ATMEL_US_DCDIC
;
526 UART_PUT_IER(port
, ier
);
530 * Control the transmission of a break signal
532 static void atmel_break_ctl(struct uart_port
*port
, int break_state
)
534 if (break_state
!= 0)
535 UART_PUT_CR(port
, ATMEL_US_STTBRK
); /* start break */
537 UART_PUT_CR(port
, ATMEL_US_STPBRK
); /* stop break */
541 * Stores the incoming character in the ring buffer
544 atmel_buffer_rx_char(struct uart_port
*port
, unsigned int status
,
547 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
548 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
549 struct atmel_uart_char
*c
;
551 if (!CIRC_SPACE(ring
->head
, ring
->tail
, ATMEL_SERIAL_RINGSIZE
))
552 /* Buffer overflow, ignore char */
555 c
= &((struct atmel_uart_char
*)ring
->buf
)[ring
->head
];
559 /* Make sure the character is stored before we update head. */
562 ring
->head
= (ring
->head
+ 1) & (ATMEL_SERIAL_RINGSIZE
- 1);
566 * Deal with parity, framing and overrun errors.
568 static void atmel_pdc_rxerr(struct uart_port
*port
, unsigned int status
)
571 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
573 if (status
& ATMEL_US_RXBRK
) {
574 /* ignore side-effect */
575 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
578 if (status
& ATMEL_US_PARE
)
579 port
->icount
.parity
++;
580 if (status
& ATMEL_US_FRAME
)
581 port
->icount
.frame
++;
582 if (status
& ATMEL_US_OVRE
)
583 port
->icount
.overrun
++;
587 * Characters received (called from interrupt handler)
589 static void atmel_rx_chars(struct uart_port
*port
)
591 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
592 unsigned int status
, ch
;
594 status
= UART_GET_CSR(port
);
595 while (status
& ATMEL_US_RXRDY
) {
596 ch
= UART_GET_CHAR(port
);
599 * note that the error handling code is
600 * out of the main execution path
602 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
603 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
)
604 || atmel_port
->break_active
)) {
607 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
609 if (status
& ATMEL_US_RXBRK
610 && !atmel_port
->break_active
) {
611 atmel_port
->break_active
= 1;
612 UART_PUT_IER(port
, ATMEL_US_RXBRK
);
615 * This is either the end-of-break
616 * condition or we've received at
617 * least one character without RXBRK
618 * being set. In both cases, the next
619 * RXBRK will indicate start-of-break.
621 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
622 status
&= ~ATMEL_US_RXBRK
;
623 atmel_port
->break_active
= 0;
627 atmel_buffer_rx_char(port
, status
, ch
);
628 status
= UART_GET_CSR(port
);
631 tasklet_schedule(&atmel_port
->tasklet
);
635 * Transmit characters (called from tasklet with TXRDY interrupt
638 static void atmel_tx_chars(struct uart_port
*port
)
640 struct circ_buf
*xmit
= &port
->state
->xmit
;
641 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
643 if (port
->x_char
&& UART_GET_CSR(port
) & atmel_port
->tx_done_mask
) {
644 UART_PUT_CHAR(port
, port
->x_char
);
648 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
))
651 while (UART_GET_CSR(port
) & atmel_port
->tx_done_mask
) {
652 UART_PUT_CHAR(port
, xmit
->buf
[xmit
->tail
]);
653 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
655 if (uart_circ_empty(xmit
))
659 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
660 uart_write_wakeup(port
);
662 if (!uart_circ_empty(xmit
))
663 /* Enable interrupts */
664 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
667 static void atmel_complete_tx_dma(void *arg
)
669 struct atmel_uart_port
*atmel_port
= arg
;
670 struct uart_port
*port
= &atmel_port
->uart
;
671 struct circ_buf
*xmit
= &port
->state
->xmit
;
672 struct dma_chan
*chan
= atmel_port
->chan_tx
;
675 spin_lock_irqsave(&port
->lock
, flags
);
678 dmaengine_terminate_all(chan
);
679 xmit
->tail
+= sg_dma_len(&atmel_port
->sg_tx
);
680 xmit
->tail
&= UART_XMIT_SIZE
- 1;
682 port
->icount
.tx
+= sg_dma_len(&atmel_port
->sg_tx
);
684 spin_lock_irq(&atmel_port
->lock_tx
);
685 async_tx_ack(atmel_port
->desc_tx
);
686 atmel_port
->cookie_tx
= -EINVAL
;
687 atmel_port
->desc_tx
= NULL
;
688 spin_unlock_irq(&atmel_port
->lock_tx
);
690 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
691 uart_write_wakeup(port
);
693 /* Do we really need this? */
694 if (!uart_circ_empty(xmit
))
695 tasklet_schedule(&atmel_port
->tasklet
);
697 spin_unlock_irqrestore(&port
->lock
, flags
);
700 static void atmel_release_tx_dma(struct uart_port
*port
)
702 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
703 struct dma_chan
*chan
= atmel_port
->chan_tx
;
706 dmaengine_terminate_all(chan
);
707 dma_release_channel(chan
);
708 dma_unmap_sg(port
->dev
, &atmel_port
->sg_tx
, 1,
712 atmel_port
->desc_tx
= NULL
;
713 atmel_port
->chan_tx
= NULL
;
714 atmel_port
->cookie_tx
= -EINVAL
;
718 * Called from tasklet with TXRDY interrupt is disabled.
720 static void atmel_tx_dma(struct uart_port
*port
)
722 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
723 struct circ_buf
*xmit
= &port
->state
->xmit
;
724 struct dma_chan
*chan
= atmel_port
->chan_tx
;
725 struct dma_async_tx_descriptor
*desc
;
726 struct scatterlist
*sg
= &atmel_port
->sg_tx
;
728 /* Make sure we have an idle channel */
729 if (atmel_port
->desc_tx
!= NULL
)
732 if (!uart_circ_empty(xmit
) && !uart_tx_stopped(port
)) {
735 * Port xmit buffer is already mapped,
736 * and it is one page... Just adjust
737 * offsets and lengths. Since it is a circular buffer,
738 * we have to transmit till the end, and then the rest.
739 * Take the port lock to get a
740 * consistent xmit buffer state.
742 sg
->offset
= xmit
->tail
& (UART_XMIT_SIZE
- 1);
743 sg_dma_address(sg
) = (sg_dma_address(sg
) &
744 ~(UART_XMIT_SIZE
- 1))
746 sg_dma_len(sg
) = CIRC_CNT_TO_END(xmit
->head
,
749 BUG_ON(!sg_dma_len(sg
));
751 desc
= dmaengine_prep_slave_sg(chan
,
758 dev_err(port
->dev
, "Failed to send via dma!\n");
762 dma_sync_sg_for_device(port
->dev
, sg
, 1, DMA_MEM_TO_DEV
);
764 atmel_port
->desc_tx
= desc
;
765 desc
->callback
= atmel_complete_tx_dma
;
766 desc
->callback_param
= atmel_port
;
767 atmel_port
->cookie_tx
= dmaengine_submit(desc
);
770 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
771 /* DMA done, stop TX, start RX for RS485 */
772 atmel_start_rx(port
);
776 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
777 uart_write_wakeup(port
);
780 static int atmel_prepare_tx_dma(struct uart_port
*port
)
782 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
784 struct dma_slave_config config
;
788 dma_cap_set(DMA_SLAVE
, mask
);
790 atmel_port
->chan_tx
= dma_request_slave_channel(port
->dev
, "tx");
791 if (atmel_port
->chan_tx
== NULL
)
793 dev_info(port
->dev
, "using %s for tx DMA transfers\n",
794 dma_chan_name(atmel_port
->chan_tx
));
796 spin_lock_init(&atmel_port
->lock_tx
);
797 sg_init_table(&atmel_port
->sg_tx
, 1);
798 /* UART circular tx buffer is an aligned page. */
799 BUG_ON((int)port
->state
->xmit
.buf
& ~PAGE_MASK
);
800 sg_set_page(&atmel_port
->sg_tx
,
801 virt_to_page(port
->state
->xmit
.buf
),
803 (int)port
->state
->xmit
.buf
& ~PAGE_MASK
);
804 nent
= dma_map_sg(port
->dev
,
810 dev_dbg(port
->dev
, "need to release resource of dma\n");
813 dev_dbg(port
->dev
, "%s: mapped %d@%p to %x\n", __func__
,
814 sg_dma_len(&atmel_port
->sg_tx
),
815 port
->state
->xmit
.buf
,
816 sg_dma_address(&atmel_port
->sg_tx
));
819 /* Configure the slave DMA */
820 memset(&config
, 0, sizeof(config
));
821 config
.direction
= DMA_MEM_TO_DEV
;
822 config
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
823 config
.dst_addr
= port
->mapbase
+ ATMEL_US_THR
;
825 ret
= dmaengine_device_control(atmel_port
->chan_tx
,
827 (unsigned long)&config
);
829 dev_err(port
->dev
, "DMA tx slave configuration failed\n");
836 dev_err(port
->dev
, "TX channel not available, switch to pio\n");
837 atmel_port
->use_dma_tx
= 0;
838 if (atmel_port
->chan_tx
)
839 atmel_release_tx_dma(port
);
843 static void atmel_flip_buffer_rx_dma(struct uart_port
*port
,
844 char *buf
, size_t count
)
846 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
847 struct tty_port
*tport
= &port
->state
->port
;
849 dma_sync_sg_for_cpu(port
->dev
,
854 tty_insert_flip_string(tport
, buf
, count
);
856 dma_sync_sg_for_device(port
->dev
,
861 * Drop the lock here since it might end up calling
862 * uart_start(), which takes the lock.
864 spin_unlock(&port
->lock
);
865 tty_flip_buffer_push(tport
);
866 spin_lock(&port
->lock
);
869 static void atmel_complete_rx_dma(void *arg
)
871 struct uart_port
*port
= arg
;
872 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
874 tasklet_schedule(&atmel_port
->tasklet
);
877 static void atmel_release_rx_dma(struct uart_port
*port
)
879 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
880 struct dma_chan
*chan
= atmel_port
->chan_rx
;
883 dmaengine_terminate_all(chan
);
884 dma_release_channel(chan
);
885 dma_unmap_sg(port
->dev
, &atmel_port
->sg_rx
, 1,
889 atmel_port
->desc_rx
= NULL
;
890 atmel_port
->chan_rx
= NULL
;
891 atmel_port
->cookie_rx
= -EINVAL
;
894 static void atmel_rx_from_dma(struct uart_port
*port
)
896 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
897 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
898 struct dma_chan
*chan
= atmel_port
->chan_rx
;
899 struct dma_tx_state state
;
900 enum dma_status dmastat
;
901 size_t pending
, count
;
904 /* Reset the UART timeout early so that we don't miss one */
905 UART_PUT_CR(port
, ATMEL_US_STTTO
);
906 dmastat
= dmaengine_tx_status(chan
,
907 atmel_port
->cookie_rx
,
909 /* Restart a new tasklet if DMA status is error */
910 if (dmastat
== DMA_ERROR
) {
911 dev_dbg(port
->dev
, "Get residue error, restart tasklet\n");
912 UART_PUT_IER(port
, ATMEL_US_TIMEOUT
);
913 tasklet_schedule(&atmel_port
->tasklet
);
916 /* current transfer size should no larger than dma buffer */
917 pending
= sg_dma_len(&atmel_port
->sg_rx
) - state
.residue
;
918 BUG_ON(pending
> sg_dma_len(&atmel_port
->sg_rx
));
921 * This will take the chars we have so far,
922 * ring->head will record the transfer size, only new bytes come
923 * will insert into the framework.
925 if (pending
> ring
->head
) {
926 count
= pending
- ring
->head
;
928 atmel_flip_buffer_rx_dma(port
, ring
->buf
+ ring
->head
, count
);
931 if (ring
->head
== sg_dma_len(&atmel_port
->sg_rx
))
934 port
->icount
.rx
+= count
;
937 UART_PUT_IER(port
, ATMEL_US_TIMEOUT
);
940 static int atmel_prepare_rx_dma(struct uart_port
*port
)
942 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
943 struct dma_async_tx_descriptor
*desc
;
945 struct dma_slave_config config
;
946 struct circ_buf
*ring
;
949 ring
= &atmel_port
->rx_ring
;
952 dma_cap_set(DMA_CYCLIC
, mask
);
954 atmel_port
->chan_rx
= dma_request_slave_channel(port
->dev
, "rx");
955 if (atmel_port
->chan_rx
== NULL
)
957 dev_info(port
->dev
, "using %s for rx DMA transfers\n",
958 dma_chan_name(atmel_port
->chan_rx
));
960 spin_lock_init(&atmel_port
->lock_rx
);
961 sg_init_table(&atmel_port
->sg_rx
, 1);
962 /* UART circular rx buffer is an aligned page. */
963 BUG_ON((int)port
->state
->xmit
.buf
& ~PAGE_MASK
);
964 sg_set_page(&atmel_port
->sg_rx
,
965 virt_to_page(ring
->buf
),
966 ATMEL_SERIAL_RINGSIZE
,
967 (int)ring
->buf
& ~PAGE_MASK
);
968 nent
= dma_map_sg(port
->dev
,
974 dev_dbg(port
->dev
, "need to release resource of dma\n");
977 dev_dbg(port
->dev
, "%s: mapped %d@%p to %x\n", __func__
,
978 sg_dma_len(&atmel_port
->sg_rx
),
980 sg_dma_address(&atmel_port
->sg_rx
));
983 /* Configure the slave DMA */
984 memset(&config
, 0, sizeof(config
));
985 config
.direction
= DMA_DEV_TO_MEM
;
986 config
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
987 config
.src_addr
= port
->mapbase
+ ATMEL_US_RHR
;
989 ret
= dmaengine_device_control(atmel_port
->chan_rx
,
991 (unsigned long)&config
);
993 dev_err(port
->dev
, "DMA rx slave configuration failed\n");
997 * Prepare a cyclic dma transfer, assign 2 descriptors,
998 * each one is half ring buffer size
1000 desc
= dmaengine_prep_dma_cyclic(atmel_port
->chan_rx
,
1001 sg_dma_address(&atmel_port
->sg_rx
),
1002 sg_dma_len(&atmel_port
->sg_rx
),
1003 sg_dma_len(&atmel_port
->sg_rx
)/2,
1005 DMA_PREP_INTERRUPT
);
1006 desc
->callback
= atmel_complete_rx_dma
;
1007 desc
->callback_param
= port
;
1008 atmel_port
->desc_rx
= desc
;
1009 atmel_port
->cookie_rx
= dmaengine_submit(desc
);
1014 dev_err(port
->dev
, "RX channel not available, switch to pio\n");
1015 atmel_port
->use_dma_rx
= 0;
1016 if (atmel_port
->chan_rx
)
1017 atmel_release_rx_dma(port
);
1021 static void atmel_uart_timer_callback(unsigned long data
)
1023 struct uart_port
*port
= (void *)data
;
1024 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1026 tasklet_schedule(&atmel_port
->tasklet
);
1027 mod_timer(&atmel_port
->uart_timer
, jiffies
+ uart_poll_timeout(port
));
1031 * receive interrupt handler.
1034 atmel_handle_receive(struct uart_port
*port
, unsigned int pending
)
1036 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1038 if (atmel_use_pdc_rx(port
)) {
1040 * PDC receive. Just schedule the tasklet and let it
1041 * figure out the details.
1043 * TODO: We're not handling error flags correctly at
1046 if (pending
& (ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
)) {
1047 UART_PUT_IDR(port
, (ATMEL_US_ENDRX
1048 | ATMEL_US_TIMEOUT
));
1049 tasklet_schedule(&atmel_port
->tasklet
);
1052 if (pending
& (ATMEL_US_RXBRK
| ATMEL_US_OVRE
|
1053 ATMEL_US_FRAME
| ATMEL_US_PARE
))
1054 atmel_pdc_rxerr(port
, pending
);
1057 if (atmel_use_dma_rx(port
)) {
1058 if (pending
& ATMEL_US_TIMEOUT
) {
1059 UART_PUT_IDR(port
, ATMEL_US_TIMEOUT
);
1060 tasklet_schedule(&atmel_port
->tasklet
);
1064 /* Interrupt receive */
1065 if (pending
& ATMEL_US_RXRDY
)
1066 atmel_rx_chars(port
);
1067 else if (pending
& ATMEL_US_RXBRK
) {
1069 * End of break detected. If it came along with a
1070 * character, atmel_rx_chars will handle it.
1072 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
1073 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
1074 atmel_port
->break_active
= 0;
1079 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1082 atmel_handle_transmit(struct uart_port
*port
, unsigned int pending
)
1084 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1086 if (pending
& atmel_port
->tx_done_mask
) {
1087 /* Either PDC or interrupt transmission */
1088 UART_PUT_IDR(port
, atmel_port
->tx_done_mask
);
1089 tasklet_schedule(&atmel_port
->tasklet
);
1094 * status flags interrupt handler.
1097 atmel_handle_status(struct uart_port
*port
, unsigned int pending
,
1098 unsigned int status
)
1100 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1102 if (pending
& (ATMEL_US_RIIC
| ATMEL_US_DSRIC
| ATMEL_US_DCDIC
1103 | ATMEL_US_CTSIC
)) {
1104 atmel_port
->irq_status
= status
;
1105 tasklet_schedule(&atmel_port
->tasklet
);
1112 static irqreturn_t
atmel_interrupt(int irq
, void *dev_id
)
1114 struct uart_port
*port
= dev_id
;
1115 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1116 unsigned int status
, pending
, pass_counter
= 0;
1117 bool gpio_handled
= false;
1120 status
= atmel_get_lines_status(port
);
1121 pending
= status
& UART_GET_IMR(port
);
1122 if (!gpio_handled
) {
1124 * Dealing with GPIO interrupt
1126 if (irq
== atmel_port
->gpio_irq
[UART_GPIO_CTS
])
1127 pending
|= ATMEL_US_CTSIC
;
1129 if (irq
== atmel_port
->gpio_irq
[UART_GPIO_DSR
])
1130 pending
|= ATMEL_US_DSRIC
;
1132 if (irq
== atmel_port
->gpio_irq
[UART_GPIO_RI
])
1133 pending
|= ATMEL_US_RIIC
;
1135 if (irq
== atmel_port
->gpio_irq
[UART_GPIO_DCD
])
1136 pending
|= ATMEL_US_DCDIC
;
1138 gpio_handled
= true;
1143 atmel_handle_receive(port
, pending
);
1144 atmel_handle_status(port
, pending
, status
);
1145 atmel_handle_transmit(port
, pending
);
1146 } while (pass_counter
++ < ATMEL_ISR_PASS_LIMIT
);
1148 return pass_counter
? IRQ_HANDLED
: IRQ_NONE
;
1151 static void atmel_release_tx_pdc(struct uart_port
*port
)
1153 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1154 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
1156 dma_unmap_single(port
->dev
,
1163 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1165 static void atmel_tx_pdc(struct uart_port
*port
)
1167 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1168 struct circ_buf
*xmit
= &port
->state
->xmit
;
1169 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
1172 /* nothing left to transmit? */
1173 if (UART_GET_TCR(port
))
1176 xmit
->tail
+= pdc
->ofs
;
1177 xmit
->tail
&= UART_XMIT_SIZE
- 1;
1179 port
->icount
.tx
+= pdc
->ofs
;
1182 /* more to transmit - setup next transfer */
1184 /* disable PDC transmit */
1185 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
1187 if (!uart_circ_empty(xmit
) && !uart_tx_stopped(port
)) {
1188 dma_sync_single_for_device(port
->dev
,
1193 count
= CIRC_CNT_TO_END(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
1196 UART_PUT_TPR(port
, pdc
->dma_addr
+ xmit
->tail
);
1197 UART_PUT_TCR(port
, count
);
1198 /* re-enable PDC transmit */
1199 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
1200 /* Enable interrupts */
1201 UART_PUT_IER(port
, atmel_port
->tx_done_mask
);
1203 if ((atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) &&
1204 !(atmel_port
->rs485
.flags
& SER_RS485_RX_DURING_TX
)) {
1205 /* DMA done, stop TX, start RX for RS485 */
1206 atmel_start_rx(port
);
1210 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
1211 uart_write_wakeup(port
);
1214 static int atmel_prepare_tx_pdc(struct uart_port
*port
)
1216 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1217 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_tx
;
1218 struct circ_buf
*xmit
= &port
->state
->xmit
;
1220 pdc
->buf
= xmit
->buf
;
1221 pdc
->dma_addr
= dma_map_single(port
->dev
,
1225 pdc
->dma_size
= UART_XMIT_SIZE
;
1231 static void atmel_rx_from_ring(struct uart_port
*port
)
1233 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1234 struct circ_buf
*ring
= &atmel_port
->rx_ring
;
1236 unsigned int status
;
1238 while (ring
->head
!= ring
->tail
) {
1239 struct atmel_uart_char c
;
1241 /* Make sure c is loaded after head. */
1244 c
= ((struct atmel_uart_char
*)ring
->buf
)[ring
->tail
];
1246 ring
->tail
= (ring
->tail
+ 1) & (ATMEL_SERIAL_RINGSIZE
- 1);
1253 * note that the error handling code is
1254 * out of the main execution path
1256 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
1257 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
))) {
1258 if (status
& ATMEL_US_RXBRK
) {
1259 /* ignore side-effect */
1260 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
1263 if (uart_handle_break(port
))
1266 if (status
& ATMEL_US_PARE
)
1267 port
->icount
.parity
++;
1268 if (status
& ATMEL_US_FRAME
)
1269 port
->icount
.frame
++;
1270 if (status
& ATMEL_US_OVRE
)
1271 port
->icount
.overrun
++;
1273 status
&= port
->read_status_mask
;
1275 if (status
& ATMEL_US_RXBRK
)
1277 else if (status
& ATMEL_US_PARE
)
1279 else if (status
& ATMEL_US_FRAME
)
1284 if (uart_handle_sysrq_char(port
, c
.ch
))
1287 uart_insert_char(port
, status
, ATMEL_US_OVRE
, c
.ch
, flg
);
1291 * Drop the lock here since it might end up calling
1292 * uart_start(), which takes the lock.
1294 spin_unlock(&port
->lock
);
1295 tty_flip_buffer_push(&port
->state
->port
);
1296 spin_lock(&port
->lock
);
1299 static void atmel_release_rx_pdc(struct uart_port
*port
)
1301 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1304 for (i
= 0; i
< 2; i
++) {
1305 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_rx
[i
];
1307 dma_unmap_single(port
->dev
,
1315 static void atmel_rx_from_pdc(struct uart_port
*port
)
1317 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1318 struct tty_port
*tport
= &port
->state
->port
;
1319 struct atmel_dma_buffer
*pdc
;
1320 int rx_idx
= atmel_port
->pdc_rx_idx
;
1326 /* Reset the UART timeout early so that we don't miss one */
1327 UART_PUT_CR(port
, ATMEL_US_STTTO
);
1329 pdc
= &atmel_port
->pdc_rx
[rx_idx
];
1330 head
= UART_GET_RPR(port
) - pdc
->dma_addr
;
1333 /* If the PDC has switched buffers, RPR won't contain
1334 * any address within the current buffer. Since head
1335 * is unsigned, we just need a one-way comparison to
1338 * In this case, we just need to consume the entire
1339 * buffer and resubmit it for DMA. This will clear the
1340 * ENDRX bit as well, so that we can safely re-enable
1341 * all interrupts below.
1343 head
= min(head
, pdc
->dma_size
);
1345 if (likely(head
!= tail
)) {
1346 dma_sync_single_for_cpu(port
->dev
, pdc
->dma_addr
,
1347 pdc
->dma_size
, DMA_FROM_DEVICE
);
1350 * head will only wrap around when we recycle
1351 * the DMA buffer, and when that happens, we
1352 * explicitly set tail to 0. So head will
1353 * always be greater than tail.
1355 count
= head
- tail
;
1357 tty_insert_flip_string(tport
, pdc
->buf
+ pdc
->ofs
,
1360 dma_sync_single_for_device(port
->dev
, pdc
->dma_addr
,
1361 pdc
->dma_size
, DMA_FROM_DEVICE
);
1363 port
->icount
.rx
+= count
;
1368 * If the current buffer is full, we need to check if
1369 * the next one contains any additional data.
1371 if (head
>= pdc
->dma_size
) {
1373 UART_PUT_RNPR(port
, pdc
->dma_addr
);
1374 UART_PUT_RNCR(port
, pdc
->dma_size
);
1377 atmel_port
->pdc_rx_idx
= rx_idx
;
1379 } while (head
>= pdc
->dma_size
);
1382 * Drop the lock here since it might end up calling
1383 * uart_start(), which takes the lock.
1385 spin_unlock(&port
->lock
);
1386 tty_flip_buffer_push(tport
);
1387 spin_lock(&port
->lock
);
1389 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
1392 static int atmel_prepare_rx_pdc(struct uart_port
*port
)
1394 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1397 for (i
= 0; i
< 2; i
++) {
1398 struct atmel_dma_buffer
*pdc
= &atmel_port
->pdc_rx
[i
];
1400 pdc
->buf
= kmalloc(PDC_BUFFER_SIZE
, GFP_KERNEL
);
1401 if (pdc
->buf
== NULL
) {
1403 dma_unmap_single(port
->dev
,
1404 atmel_port
->pdc_rx
[0].dma_addr
,
1407 kfree(atmel_port
->pdc_rx
[0].buf
);
1409 atmel_port
->use_pdc_rx
= 0;
1412 pdc
->dma_addr
= dma_map_single(port
->dev
,
1416 pdc
->dma_size
= PDC_BUFFER_SIZE
;
1420 atmel_port
->pdc_rx_idx
= 0;
1422 UART_PUT_RPR(port
, atmel_port
->pdc_rx
[0].dma_addr
);
1423 UART_PUT_RCR(port
, PDC_BUFFER_SIZE
);
1425 UART_PUT_RNPR(port
, atmel_port
->pdc_rx
[1].dma_addr
);
1426 UART_PUT_RNCR(port
, PDC_BUFFER_SIZE
);
1432 * tasklet handling tty stuff outside the interrupt handler.
1434 static void atmel_tasklet_func(unsigned long data
)
1436 struct uart_port
*port
= (struct uart_port
*)data
;
1437 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1438 unsigned int status
;
1439 unsigned int status_change
;
1441 /* The interrupt handler does not take the lock */
1442 spin_lock(&port
->lock
);
1444 atmel_port
->schedule_tx(port
);
1446 status
= atmel_port
->irq_status
;
1447 status_change
= status
^ atmel_port
->irq_status_prev
;
1449 if (status_change
& (ATMEL_US_RI
| ATMEL_US_DSR
1450 | ATMEL_US_DCD
| ATMEL_US_CTS
)) {
1451 /* TODO: All reads to CSR will clear these interrupts! */
1452 if (status_change
& ATMEL_US_RI
)
1454 if (status_change
& ATMEL_US_DSR
)
1456 if (status_change
& ATMEL_US_DCD
)
1457 uart_handle_dcd_change(port
, !(status
& ATMEL_US_DCD
));
1458 if (status_change
& ATMEL_US_CTS
)
1459 uart_handle_cts_change(port
, !(status
& ATMEL_US_CTS
));
1461 wake_up_interruptible(&port
->state
->port
.delta_msr_wait
);
1463 atmel_port
->irq_status_prev
= status
;
1466 atmel_port
->schedule_rx(port
);
1468 spin_unlock(&port
->lock
);
1471 static int atmel_init_property(struct atmel_uart_port
*atmel_port
,
1472 struct platform_device
*pdev
)
1474 struct device_node
*np
= pdev
->dev
.of_node
;
1475 struct atmel_uart_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1478 /* DMA/PDC usage specification */
1479 if (of_get_property(np
, "atmel,use-dma-rx", NULL
)) {
1480 if (of_get_property(np
, "dmas", NULL
)) {
1481 atmel_port
->use_dma_rx
= true;
1482 atmel_port
->use_pdc_rx
= false;
1484 atmel_port
->use_dma_rx
= false;
1485 atmel_port
->use_pdc_rx
= true;
1488 atmel_port
->use_dma_rx
= false;
1489 atmel_port
->use_pdc_rx
= false;
1492 if (of_get_property(np
, "atmel,use-dma-tx", NULL
)) {
1493 if (of_get_property(np
, "dmas", NULL
)) {
1494 atmel_port
->use_dma_tx
= true;
1495 atmel_port
->use_pdc_tx
= false;
1497 atmel_port
->use_dma_tx
= false;
1498 atmel_port
->use_pdc_tx
= true;
1501 atmel_port
->use_dma_tx
= false;
1502 atmel_port
->use_pdc_tx
= false;
1506 atmel_port
->use_pdc_rx
= pdata
->use_dma_rx
;
1507 atmel_port
->use_pdc_tx
= pdata
->use_dma_tx
;
1508 atmel_port
->use_dma_rx
= false;
1509 atmel_port
->use_dma_tx
= false;
1515 static void atmel_init_rs485(struct atmel_uart_port
*atmel_port
,
1516 struct platform_device
*pdev
)
1518 struct device_node
*np
= pdev
->dev
.of_node
;
1519 struct atmel_uart_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1523 /* rs485 properties */
1524 if (of_property_read_u32_array(np
, "rs485-rts-delay",
1525 rs485_delay
, 2) == 0) {
1526 struct serial_rs485
*rs485conf
= &atmel_port
->rs485
;
1528 rs485conf
->delay_rts_before_send
= rs485_delay
[0];
1529 rs485conf
->delay_rts_after_send
= rs485_delay
[1];
1530 rs485conf
->flags
= 0;
1532 if (of_get_property(np
, "rs485-rx-during-tx", NULL
))
1533 rs485conf
->flags
|= SER_RS485_RX_DURING_TX
;
1535 if (of_get_property(np
, "linux,rs485-enabled-at-boot-time",
1537 rs485conf
->flags
|= SER_RS485_ENABLED
;
1540 atmel_port
->rs485
= pdata
->rs485
;
1545 static void atmel_set_ops(struct uart_port
*port
)
1547 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1549 if (atmel_use_dma_rx(port
)) {
1550 atmel_port
->prepare_rx
= &atmel_prepare_rx_dma
;
1551 atmel_port
->schedule_rx
= &atmel_rx_from_dma
;
1552 atmel_port
->release_rx
= &atmel_release_rx_dma
;
1553 } else if (atmel_use_pdc_rx(port
)) {
1554 atmel_port
->prepare_rx
= &atmel_prepare_rx_pdc
;
1555 atmel_port
->schedule_rx
= &atmel_rx_from_pdc
;
1556 atmel_port
->release_rx
= &atmel_release_rx_pdc
;
1558 atmel_port
->prepare_rx
= NULL
;
1559 atmel_port
->schedule_rx
= &atmel_rx_from_ring
;
1560 atmel_port
->release_rx
= NULL
;
1563 if (atmel_use_dma_tx(port
)) {
1564 atmel_port
->prepare_tx
= &atmel_prepare_tx_dma
;
1565 atmel_port
->schedule_tx
= &atmel_tx_dma
;
1566 atmel_port
->release_tx
= &atmel_release_tx_dma
;
1567 } else if (atmel_use_pdc_tx(port
)) {
1568 atmel_port
->prepare_tx
= &atmel_prepare_tx_pdc
;
1569 atmel_port
->schedule_tx
= &atmel_tx_pdc
;
1570 atmel_port
->release_tx
= &atmel_release_tx_pdc
;
1572 atmel_port
->prepare_tx
= NULL
;
1573 atmel_port
->schedule_tx
= &atmel_tx_chars
;
1574 atmel_port
->release_tx
= NULL
;
1579 * Get ip name usart or uart
1581 static void atmel_get_ip_name(struct uart_port
*port
)
1583 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1584 int name
= UART_GET_IP_NAME(port
);
1587 /* usart and uart ascii */
1591 atmel_port
->is_usart
= false;
1593 if (name
== usart
) {
1594 dev_dbg(port
->dev
, "This is usart\n");
1595 atmel_port
->is_usart
= true;
1596 } else if (name
== uart
) {
1597 dev_dbg(port
->dev
, "This is uart\n");
1598 atmel_port
->is_usart
= false;
1600 /* fallback for older SoCs: use version field */
1601 version
= UART_GET_IP_VERSION(port
);
1605 dev_dbg(port
->dev
, "This version is usart\n");
1606 atmel_port
->is_usart
= true;
1610 dev_dbg(port
->dev
, "This version is uart\n");
1611 atmel_port
->is_usart
= false;
1614 dev_err(port
->dev
, "Not supported ip name nor version, set to uart\n");
1619 static void atmel_free_gpio_irq(struct uart_port
*port
)
1621 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1622 enum mctrl_gpio_idx i
;
1624 for (i
= 0; i
< UART_GPIO_MAX
; i
++)
1625 if (atmel_port
->gpio_irq
[i
] >= 0)
1626 free_irq(atmel_port
->gpio_irq
[i
], port
);
1629 static int atmel_request_gpio_irq(struct uart_port
*port
)
1631 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1632 int *irq
= atmel_port
->gpio_irq
;
1633 enum mctrl_gpio_idx i
;
1636 for (i
= 0; (i
< UART_GPIO_MAX
) && !err
; i
++) {
1640 irq_set_status_flags(irq
[i
], IRQ_NOAUTOEN
);
1641 err
= request_irq(irq
[i
], atmel_interrupt
, IRQ_TYPE_EDGE_BOTH
,
1642 "atmel_serial", port
);
1644 dev_err(port
->dev
, "atmel_startup - Can't get %d irq\n",
1649 * If something went wrong, rollback.
1651 while (err
&& (--i
>= 0))
1653 free_irq(irq
[i
], port
);
1659 * Perform initialization and enable port for reception
1661 static int atmel_startup(struct uart_port
*port
)
1663 struct platform_device
*pdev
= to_platform_device(port
->dev
);
1664 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1665 struct tty_struct
*tty
= port
->state
->port
.tty
;
1669 * Ensure that no interrupts are enabled otherwise when
1670 * request_irq() is called we could get stuck trying to
1671 * handle an unexpected interrupt
1673 UART_PUT_IDR(port
, -1);
1674 atmel_port
->ms_irq_enabled
= false;
1679 retval
= request_irq(port
->irq
, atmel_interrupt
, IRQF_SHARED
,
1680 tty
? tty
->name
: "atmel_serial", port
);
1682 dev_err(port
->dev
, "atmel_startup - Can't get irq\n");
1687 * Get the GPIO lines IRQ
1689 retval
= atmel_request_gpio_irq(port
);
1694 * Initialize DMA (if necessary)
1696 atmel_init_property(atmel_port
, pdev
);
1698 if (atmel_port
->prepare_rx
) {
1699 retval
= atmel_port
->prepare_rx(port
);
1701 atmel_set_ops(port
);
1704 if (atmel_port
->prepare_tx
) {
1705 retval
= atmel_port
->prepare_tx(port
);
1707 atmel_set_ops(port
);
1710 /* Save current CSR for comparison in atmel_tasklet_func() */
1711 atmel_port
->irq_status_prev
= atmel_get_lines_status(port
);
1712 atmel_port
->irq_status
= atmel_port
->irq_status_prev
;
1715 * Finally, enable the serial port
1717 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
1718 /* enable xmit & rcvr */
1719 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
1721 setup_timer(&atmel_port
->uart_timer
,
1722 atmel_uart_timer_callback
,
1723 (unsigned long)port
);
1725 if (atmel_use_pdc_rx(port
)) {
1726 /* set UART timeout */
1727 if (!atmel_port
->is_usart
) {
1728 mod_timer(&atmel_port
->uart_timer
,
1729 jiffies
+ uart_poll_timeout(port
));
1730 /* set USART timeout */
1732 UART_PUT_RTOR(port
, PDC_RX_TIMEOUT
);
1733 UART_PUT_CR(port
, ATMEL_US_STTTO
);
1735 UART_PUT_IER(port
, ATMEL_US_ENDRX
| ATMEL_US_TIMEOUT
);
1737 /* enable PDC controller */
1738 UART_PUT_PTCR(port
, ATMEL_PDC_RXTEN
);
1739 } else if (atmel_use_dma_rx(port
)) {
1740 /* set UART timeout */
1741 if (!atmel_port
->is_usart
) {
1742 mod_timer(&atmel_port
->uart_timer
,
1743 jiffies
+ uart_poll_timeout(port
));
1744 /* set USART timeout */
1746 UART_PUT_RTOR(port
, PDC_RX_TIMEOUT
);
1747 UART_PUT_CR(port
, ATMEL_US_STTTO
);
1749 UART_PUT_IER(port
, ATMEL_US_TIMEOUT
);
1752 /* enable receive only */
1753 UART_PUT_IER(port
, ATMEL_US_RXRDY
);
1759 free_irq(port
->irq
, port
);
1767 static void atmel_shutdown(struct uart_port
*port
)
1769 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1772 * Prevent any tasklets being scheduled during
1775 del_timer_sync(&atmel_port
->uart_timer
);
1778 * Clear out any scheduled tasklets before
1779 * we destroy the buffers
1781 tasklet_kill(&atmel_port
->tasklet
);
1784 * Ensure everything is stopped and
1785 * disable all interrupts, port and break condition.
1787 atmel_stop_rx(port
);
1788 atmel_stop_tx(port
);
1790 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
1791 UART_PUT_IDR(port
, -1);
1795 * Shut-down the DMA.
1797 if (atmel_port
->release_rx
)
1798 atmel_port
->release_rx(port
);
1799 if (atmel_port
->release_tx
)
1800 atmel_port
->release_tx(port
);
1803 * Reset ring buffer pointers
1805 atmel_port
->rx_ring
.head
= 0;
1806 atmel_port
->rx_ring
.tail
= 0;
1809 * Free the interrupts
1811 free_irq(port
->irq
, port
);
1812 atmel_free_gpio_irq(port
);
1814 atmel_port
->ms_irq_enabled
= false;
1818 * Flush any TX data submitted for DMA. Called when the TX circular
1821 static void atmel_flush_buffer(struct uart_port
*port
)
1823 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1825 if (atmel_use_pdc_tx(port
)) {
1826 UART_PUT_TCR(port
, 0);
1827 atmel_port
->pdc_tx
.ofs
= 0;
1832 * Power / Clock management.
1834 static void atmel_serial_pm(struct uart_port
*port
, unsigned int state
,
1835 unsigned int oldstate
)
1837 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1842 * Enable the peripheral clock for this serial port.
1843 * This is called on uart_open() or a resume event.
1845 clk_prepare_enable(atmel_port
->clk
);
1847 /* re-enable interrupts if we disabled some on suspend */
1848 UART_PUT_IER(port
, atmel_port
->backup_imr
);
1851 /* Back up the interrupt mask and disable all interrupts */
1852 atmel_port
->backup_imr
= UART_GET_IMR(port
);
1853 UART_PUT_IDR(port
, -1);
1856 * Disable the peripheral clock for this serial port.
1857 * This is called on uart_close() or a suspend event.
1859 clk_disable_unprepare(atmel_port
->clk
);
1862 dev_err(port
->dev
, "atmel_serial: unknown pm %d\n", state
);
1867 * Change the port parameters
1869 static void atmel_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1870 struct ktermios
*old
)
1872 unsigned long flags
;
1873 unsigned int mode
, imr
, quot
, baud
;
1874 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
1876 /* Get current mode register */
1877 mode
= UART_GET_MR(port
) & ~(ATMEL_US_USCLKS
| ATMEL_US_CHRL
1878 | ATMEL_US_NBSTOP
| ATMEL_US_PAR
1881 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
1882 quot
= uart_get_divisor(port
, baud
);
1884 if (quot
> 65535) { /* BRGR is 16-bit, so switch to slower clock */
1886 mode
|= ATMEL_US_USCLKS_MCK_DIV8
;
1890 switch (termios
->c_cflag
& CSIZE
) {
1892 mode
|= ATMEL_US_CHRL_5
;
1895 mode
|= ATMEL_US_CHRL_6
;
1898 mode
|= ATMEL_US_CHRL_7
;
1901 mode
|= ATMEL_US_CHRL_8
;
1906 if (termios
->c_cflag
& CSTOPB
)
1907 mode
|= ATMEL_US_NBSTOP_2
;
1910 if (termios
->c_cflag
& PARENB
) {
1911 /* Mark or Space parity */
1912 if (termios
->c_cflag
& CMSPAR
) {
1913 if (termios
->c_cflag
& PARODD
)
1914 mode
|= ATMEL_US_PAR_MARK
;
1916 mode
|= ATMEL_US_PAR_SPACE
;
1917 } else if (termios
->c_cflag
& PARODD
)
1918 mode
|= ATMEL_US_PAR_ODD
;
1920 mode
|= ATMEL_US_PAR_EVEN
;
1922 mode
|= ATMEL_US_PAR_NONE
;
1924 /* hardware handshake (RTS/CTS) */
1925 if (termios
->c_cflag
& CRTSCTS
)
1926 mode
|= ATMEL_US_USMODE_HWHS
;
1928 mode
|= ATMEL_US_USMODE_NORMAL
;
1930 spin_lock_irqsave(&port
->lock
, flags
);
1932 port
->read_status_mask
= ATMEL_US_OVRE
;
1933 if (termios
->c_iflag
& INPCK
)
1934 port
->read_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
1935 if (termios
->c_iflag
& (IGNBRK
| BRKINT
| PARMRK
))
1936 port
->read_status_mask
|= ATMEL_US_RXBRK
;
1938 if (atmel_use_pdc_rx(port
))
1939 /* need to enable error interrupts */
1940 UART_PUT_IER(port
, port
->read_status_mask
);
1943 * Characters to ignore
1945 port
->ignore_status_mask
= 0;
1946 if (termios
->c_iflag
& IGNPAR
)
1947 port
->ignore_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
1948 if (termios
->c_iflag
& IGNBRK
) {
1949 port
->ignore_status_mask
|= ATMEL_US_RXBRK
;
1951 * If we're ignoring parity and break indicators,
1952 * ignore overruns too (for real raw support).
1954 if (termios
->c_iflag
& IGNPAR
)
1955 port
->ignore_status_mask
|= ATMEL_US_OVRE
;
1957 /* TODO: Ignore all characters if CREAD is set.*/
1959 /* update the per-port timeout */
1960 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1963 * save/disable interrupts. The tty layer will ensure that the
1964 * transmitter is empty if requested by the caller, so there's
1965 * no need to wait for it here.
1967 imr
= UART_GET_IMR(port
);
1968 UART_PUT_IDR(port
, -1);
1970 /* disable receiver and transmitter */
1971 UART_PUT_CR(port
, ATMEL_US_TXDIS
| ATMEL_US_RXDIS
);
1973 /* Resetting serial mode to RS232 (0x0) */
1974 mode
&= ~ATMEL_US_USMODE
;
1976 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
) {
1977 if ((atmel_port
->rs485
.delay_rts_after_send
) > 0)
1979 atmel_port
->rs485
.delay_rts_after_send
);
1980 mode
|= ATMEL_US_USMODE_RS485
;
1983 /* set the parity, stop bits and data size */
1984 UART_PUT_MR(port
, mode
);
1986 /* set the baud rate */
1987 UART_PUT_BRGR(port
, quot
);
1988 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
1989 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
1991 /* restore interrupts */
1992 UART_PUT_IER(port
, imr
);
1994 /* CTS flow-control and modem-status interrupts */
1995 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
1996 port
->ops
->enable_ms(port
);
1998 spin_unlock_irqrestore(&port
->lock
, flags
);
2001 static void atmel_set_ldisc(struct uart_port
*port
, int new)
2004 port
->flags
|= UPF_HARDPPS_CD
;
2005 atmel_enable_ms(port
);
2007 port
->flags
&= ~UPF_HARDPPS_CD
;
2012 * Return string describing the specified port
2014 static const char *atmel_type(struct uart_port
*port
)
2016 return (port
->type
== PORT_ATMEL
) ? "ATMEL_SERIAL" : NULL
;
2020 * Release the memory region(s) being used by 'port'.
2022 static void atmel_release_port(struct uart_port
*port
)
2024 struct platform_device
*pdev
= to_platform_device(port
->dev
);
2025 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
2027 release_mem_region(port
->mapbase
, size
);
2029 if (port
->flags
& UPF_IOREMAP
) {
2030 iounmap(port
->membase
);
2031 port
->membase
= NULL
;
2036 * Request the memory region(s) being used by 'port'.
2038 static int atmel_request_port(struct uart_port
*port
)
2040 struct platform_device
*pdev
= to_platform_device(port
->dev
);
2041 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
2043 if (!request_mem_region(port
->mapbase
, size
, "atmel_serial"))
2046 if (port
->flags
& UPF_IOREMAP
) {
2047 port
->membase
= ioremap(port
->mapbase
, size
);
2048 if (port
->membase
== NULL
) {
2049 release_mem_region(port
->mapbase
, size
);
2058 * Configure/autoconfigure the port.
2060 static void atmel_config_port(struct uart_port
*port
, int flags
)
2062 if (flags
& UART_CONFIG_TYPE
) {
2063 port
->type
= PORT_ATMEL
;
2064 atmel_request_port(port
);
2069 * Verify the new serial_struct (for TIOCSSERIAL).
2071 static int atmel_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
2074 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_ATMEL
)
2076 if (port
->irq
!= ser
->irq
)
2078 if (ser
->io_type
!= SERIAL_IO_MEM
)
2080 if (port
->uartclk
/ 16 != ser
->baud_base
)
2082 if ((void *)port
->mapbase
!= ser
->iomem_base
)
2084 if (port
->iobase
!= ser
->port
)
2091 #ifdef CONFIG_CONSOLE_POLL
2092 static int atmel_poll_get_char(struct uart_port
*port
)
2094 while (!(UART_GET_CSR(port
) & ATMEL_US_RXRDY
))
2097 return UART_GET_CHAR(port
);
2100 static void atmel_poll_put_char(struct uart_port
*port
, unsigned char ch
)
2102 while (!(UART_GET_CSR(port
) & ATMEL_US_TXRDY
))
2105 UART_PUT_CHAR(port
, ch
);
2110 atmel_ioctl(struct uart_port
*port
, unsigned int cmd
, unsigned long arg
)
2112 struct serial_rs485 rs485conf
;
2116 if (copy_from_user(&rs485conf
, (struct serial_rs485
*) arg
,
2120 atmel_config_rs485(port
, &rs485conf
);
2124 if (copy_to_user((struct serial_rs485
*) arg
,
2125 &(to_atmel_uart_port(port
)->rs485
),
2131 return -ENOIOCTLCMD
;
2138 static struct uart_ops atmel_pops
= {
2139 .tx_empty
= atmel_tx_empty
,
2140 .set_mctrl
= atmel_set_mctrl
,
2141 .get_mctrl
= atmel_get_mctrl
,
2142 .stop_tx
= atmel_stop_tx
,
2143 .start_tx
= atmel_start_tx
,
2144 .stop_rx
= atmel_stop_rx
,
2145 .enable_ms
= atmel_enable_ms
,
2146 .break_ctl
= atmel_break_ctl
,
2147 .startup
= atmel_startup
,
2148 .shutdown
= atmel_shutdown
,
2149 .flush_buffer
= atmel_flush_buffer
,
2150 .set_termios
= atmel_set_termios
,
2151 .set_ldisc
= atmel_set_ldisc
,
2153 .release_port
= atmel_release_port
,
2154 .request_port
= atmel_request_port
,
2155 .config_port
= atmel_config_port
,
2156 .verify_port
= atmel_verify_port
,
2157 .pm
= atmel_serial_pm
,
2158 .ioctl
= atmel_ioctl
,
2159 #ifdef CONFIG_CONSOLE_POLL
2160 .poll_get_char
= atmel_poll_get_char
,
2161 .poll_put_char
= atmel_poll_put_char
,
2166 * Configure the port from the platform device resource info.
2168 static int atmel_init_port(struct atmel_uart_port
*atmel_port
,
2169 struct platform_device
*pdev
)
2172 struct uart_port
*port
= &atmel_port
->uart
;
2173 struct atmel_uart_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2175 if (!atmel_init_property(atmel_port
, pdev
))
2176 atmel_set_ops(port
);
2178 atmel_init_rs485(atmel_port
, pdev
);
2180 port
->iotype
= UPIO_MEM
;
2181 port
->flags
= UPF_BOOT_AUTOCONF
;
2182 port
->ops
= &atmel_pops
;
2184 port
->dev
= &pdev
->dev
;
2185 port
->mapbase
= pdev
->resource
[0].start
;
2186 port
->irq
= pdev
->resource
[1].start
;
2188 tasklet_init(&atmel_port
->tasklet
, atmel_tasklet_func
,
2189 (unsigned long)port
);
2191 memset(&atmel_port
->rx_ring
, 0, sizeof(atmel_port
->rx_ring
));
2193 if (pdata
&& pdata
->regs
) {
2194 /* Already mapped by setup code */
2195 port
->membase
= pdata
->regs
;
2197 port
->flags
|= UPF_IOREMAP
;
2198 port
->membase
= NULL
;
2201 /* for console, the clock could already be configured */
2202 if (!atmel_port
->clk
) {
2203 atmel_port
->clk
= clk_get(&pdev
->dev
, "usart");
2204 if (IS_ERR(atmel_port
->clk
)) {
2205 ret
= PTR_ERR(atmel_port
->clk
);
2206 atmel_port
->clk
= NULL
;
2209 ret
= clk_prepare_enable(atmel_port
->clk
);
2211 clk_put(atmel_port
->clk
);
2212 atmel_port
->clk
= NULL
;
2215 port
->uartclk
= clk_get_rate(atmel_port
->clk
);
2216 clk_disable_unprepare(atmel_port
->clk
);
2217 /* only enable clock when USART is in use */
2220 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2221 if (atmel_port
->rs485
.flags
& SER_RS485_ENABLED
)
2222 atmel_port
->tx_done_mask
= ATMEL_US_TXEMPTY
;
2223 else if (atmel_use_pdc_tx(port
)) {
2224 port
->fifosize
= PDC_BUFFER_SIZE
;
2225 atmel_port
->tx_done_mask
= ATMEL_US_ENDTX
| ATMEL_US_TXBUFE
;
2227 atmel_port
->tx_done_mask
= ATMEL_US_TXRDY
;
2233 struct platform_device
*atmel_default_console_device
; /* the serial console device */
2235 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2236 static void atmel_console_putchar(struct uart_port
*port
, int ch
)
2238 while (!(UART_GET_CSR(port
) & ATMEL_US_TXRDY
))
2240 UART_PUT_CHAR(port
, ch
);
2244 * Interrupts are disabled on entering
2246 static void atmel_console_write(struct console
*co
, const char *s
, u_int count
)
2248 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
2249 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
2250 unsigned int status
, imr
;
2251 unsigned int pdc_tx
;
2254 * First, save IMR and then disable interrupts
2256 imr
= UART_GET_IMR(port
);
2257 UART_PUT_IDR(port
, ATMEL_US_RXRDY
| atmel_port
->tx_done_mask
);
2259 /* Store PDC transmit status and disable it */
2260 pdc_tx
= UART_GET_PTSR(port
) & ATMEL_PDC_TXTEN
;
2261 UART_PUT_PTCR(port
, ATMEL_PDC_TXTDIS
);
2263 uart_console_write(port
, s
, count
, atmel_console_putchar
);
2266 * Finally, wait for transmitter to become empty
2270 status
= UART_GET_CSR(port
);
2271 } while (!(status
& ATMEL_US_TXRDY
));
2273 /* Restore PDC transmit status */
2275 UART_PUT_PTCR(port
, ATMEL_PDC_TXTEN
);
2277 /* set interrupts back the way they were */
2278 UART_PUT_IER(port
, imr
);
2282 * If the port was already initialised (eg, by a boot loader),
2283 * try to determine the current setup.
2285 static void __init
atmel_console_get_options(struct uart_port
*port
, int *baud
,
2286 int *parity
, int *bits
)
2288 unsigned int mr
, quot
;
2291 * If the baud rate generator isn't running, the port wasn't
2292 * initialized by the boot loader.
2294 quot
= UART_GET_BRGR(port
) & ATMEL_US_CD
;
2298 mr
= UART_GET_MR(port
) & ATMEL_US_CHRL
;
2299 if (mr
== ATMEL_US_CHRL_8
)
2304 mr
= UART_GET_MR(port
) & ATMEL_US_PAR
;
2305 if (mr
== ATMEL_US_PAR_EVEN
)
2307 else if (mr
== ATMEL_US_PAR_ODD
)
2311 * The serial core only rounds down when matching this to a
2312 * supported baud rate. Make sure we don't end up slightly
2313 * lower than one of those, as it would make us fall through
2314 * to a much lower baud rate than we really want.
2316 *baud
= port
->uartclk
/ (16 * (quot
- 1));
2319 static int __init
atmel_console_setup(struct console
*co
, char *options
)
2322 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
2328 if (port
->membase
== NULL
) {
2329 /* Port not initialized yet - delay setup */
2333 ret
= clk_prepare_enable(atmel_ports
[co
->index
].clk
);
2337 UART_PUT_IDR(port
, -1);
2338 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
2339 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
2342 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
2344 atmel_console_get_options(port
, &baud
, &parity
, &bits
);
2346 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
2349 static struct uart_driver atmel_uart
;
2351 static struct console atmel_console
= {
2352 .name
= ATMEL_DEVICENAME
,
2353 .write
= atmel_console_write
,
2354 .device
= uart_console_device
,
2355 .setup
= atmel_console_setup
,
2356 .flags
= CON_PRINTBUFFER
,
2358 .data
= &atmel_uart
,
2361 #define ATMEL_CONSOLE_DEVICE (&atmel_console)
2364 * Early console initialization (before VM subsystem initialized).
2366 static int __init
atmel_console_init(void)
2369 if (atmel_default_console_device
) {
2370 struct atmel_uart_data
*pdata
=
2371 dev_get_platdata(&atmel_default_console_device
->dev
);
2372 int id
= pdata
->num
;
2373 struct atmel_uart_port
*port
= &atmel_ports
[id
];
2375 port
->backup_imr
= 0;
2376 port
->uart
.line
= id
;
2378 add_preferred_console(ATMEL_DEVICENAME
, id
, NULL
);
2379 ret
= atmel_init_port(port
, atmel_default_console_device
);
2382 register_console(&atmel_console
);
2388 console_initcall(atmel_console_init
);
2391 * Late console initialization.
2393 static int __init
atmel_late_console_init(void)
2395 if (atmel_default_console_device
2396 && !(atmel_console
.flags
& CON_ENABLED
))
2397 register_console(&atmel_console
);
2402 core_initcall(atmel_late_console_init
);
2404 static inline bool atmel_is_console_port(struct uart_port
*port
)
2406 return port
->cons
&& port
->cons
->index
== port
->line
;
2410 #define ATMEL_CONSOLE_DEVICE NULL
2412 static inline bool atmel_is_console_port(struct uart_port
*port
)
2418 static struct uart_driver atmel_uart
= {
2419 .owner
= THIS_MODULE
,
2420 .driver_name
= "atmel_serial",
2421 .dev_name
= ATMEL_DEVICENAME
,
2422 .major
= SERIAL_ATMEL_MAJOR
,
2423 .minor
= MINOR_START
,
2424 .nr
= ATMEL_MAX_UART
,
2425 .cons
= ATMEL_CONSOLE_DEVICE
,
2429 static bool atmel_serial_clk_will_stop(void)
2431 #ifdef CONFIG_ARCH_AT91
2432 return at91_suspend_entering_slow_clock();
2438 static int atmel_serial_suspend(struct platform_device
*pdev
,
2441 struct uart_port
*port
= platform_get_drvdata(pdev
);
2442 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
2444 if (atmel_is_console_port(port
) && console_suspend_enabled
) {
2445 /* Drain the TX shifter */
2446 while (!(UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
))
2450 /* we can not wake up if we're running on slow clock */
2451 atmel_port
->may_wakeup
= device_may_wakeup(&pdev
->dev
);
2452 if (atmel_serial_clk_will_stop())
2453 device_set_wakeup_enable(&pdev
->dev
, 0);
2455 uart_suspend_port(&atmel_uart
, port
);
2460 static int atmel_serial_resume(struct platform_device
*pdev
)
2462 struct uart_port
*port
= platform_get_drvdata(pdev
);
2463 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
2465 uart_resume_port(&atmel_uart
, port
);
2466 device_set_wakeup_enable(&pdev
->dev
, atmel_port
->may_wakeup
);
2471 #define atmel_serial_suspend NULL
2472 #define atmel_serial_resume NULL
2475 static int atmel_init_gpios(struct atmel_uart_port
*p
, struct device
*dev
)
2477 enum mctrl_gpio_idx i
;
2478 struct gpio_desc
*gpiod
;
2480 p
->gpios
= mctrl_gpio_init(dev
, 0);
2481 if (IS_ERR_OR_NULL(p
->gpios
))
2484 for (i
= 0; i
< UART_GPIO_MAX
; i
++) {
2485 gpiod
= mctrl_gpio_to_gpiod(p
->gpios
, i
);
2486 if (gpiod
&& (gpiod_get_direction(gpiod
) == GPIOF_DIR_IN
))
2487 p
->gpio_irq
[i
] = gpiod_to_irq(gpiod
);
2489 p
->gpio_irq
[i
] = -EINVAL
;
2495 static int atmel_serial_probe(struct platform_device
*pdev
)
2497 struct atmel_uart_port
*port
;
2498 struct device_node
*np
= pdev
->dev
.of_node
;
2499 struct atmel_uart_data
*pdata
= dev_get_platdata(&pdev
->dev
);
2503 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE
& (ATMEL_SERIAL_RINGSIZE
- 1));
2506 ret
= of_alias_get_id(np
, "serial");
2512 /* port id not found in platform data nor device-tree aliases:
2513 * auto-enumerate it */
2514 ret
= find_first_zero_bit(atmel_ports_in_use
, ATMEL_MAX_UART
);
2516 if (ret
>= ATMEL_MAX_UART
) {
2521 if (test_and_set_bit(ret
, atmel_ports_in_use
)) {
2522 /* port already in use */
2527 port
= &atmel_ports
[ret
];
2528 port
->backup_imr
= 0;
2529 port
->uart
.line
= ret
;
2531 ret
= atmel_init_gpios(port
, &pdev
->dev
);
2533 dev_err(&pdev
->dev
, "%s",
2534 "Failed to initialize GPIOs. The serial port may not work as expected");
2536 ret
= atmel_init_port(port
, pdev
);
2540 if (!atmel_use_pdc_rx(&port
->uart
)) {
2542 data
= kmalloc(sizeof(struct atmel_uart_char
)
2543 * ATMEL_SERIAL_RINGSIZE
, GFP_KERNEL
);
2545 goto err_alloc_ring
;
2546 port
->rx_ring
.buf
= data
;
2549 ret
= uart_add_one_port(&atmel_uart
, &port
->uart
);
2553 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2554 if (atmel_is_console_port(&port
->uart
)
2555 && ATMEL_CONSOLE_DEVICE
->flags
& CON_ENABLED
) {
2557 * The serial core enabled the clock for us, so undo
2558 * the clk_prepare_enable() in atmel_console_setup()
2560 clk_disable_unprepare(port
->clk
);
2564 device_init_wakeup(&pdev
->dev
, 1);
2565 platform_set_drvdata(pdev
, port
);
2567 if (port
->rs485
.flags
& SER_RS485_ENABLED
) {
2568 UART_PUT_MR(&port
->uart
, ATMEL_US_USMODE_NORMAL
);
2569 UART_PUT_CR(&port
->uart
, ATMEL_US_RTSEN
);
2573 * Get port name of usart or uart
2575 atmel_get_ip_name(&port
->uart
);
2580 kfree(port
->rx_ring
.buf
);
2581 port
->rx_ring
.buf
= NULL
;
2583 if (!atmel_is_console_port(&port
->uart
)) {
2591 static int atmel_serial_remove(struct platform_device
*pdev
)
2593 struct uart_port
*port
= platform_get_drvdata(pdev
);
2594 struct atmel_uart_port
*atmel_port
= to_atmel_uart_port(port
);
2597 tasklet_kill(&atmel_port
->tasklet
);
2599 device_init_wakeup(&pdev
->dev
, 0);
2601 ret
= uart_remove_one_port(&atmel_uart
, port
);
2603 kfree(atmel_port
->rx_ring
.buf
);
2605 /* "port" is allocated statically, so we shouldn't free it */
2607 clear_bit(port
->line
, atmel_ports_in_use
);
2609 clk_put(atmel_port
->clk
);
2614 static struct platform_driver atmel_serial_driver
= {
2615 .probe
= atmel_serial_probe
,
2616 .remove
= atmel_serial_remove
,
2617 .suspend
= atmel_serial_suspend
,
2618 .resume
= atmel_serial_resume
,
2620 .name
= "atmel_usart",
2621 .owner
= THIS_MODULE
,
2622 .of_match_table
= of_match_ptr(atmel_serial_dt_ids
),
2626 static int __init
atmel_serial_init(void)
2630 ret
= uart_register_driver(&atmel_uart
);
2634 ret
= platform_driver_register(&atmel_serial_driver
);
2636 uart_unregister_driver(&atmel_uart
);
2641 static void __exit
atmel_serial_exit(void)
2643 platform_driver_unregister(&atmel_serial_driver
);
2644 uart_unregister_driver(&atmel_uart
);
2647 module_init(atmel_serial_init
);
2648 module_exit(atmel_serial_exit
);
2650 MODULE_AUTHOR("Rick Bronson");
2651 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2652 MODULE_LICENSE("GPL");
2653 MODULE_ALIAS("platform:atmel_usart");