Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / tty / serial / atmel_serial.c
bloba49f10d269b20afb2e8d2a1579af8f8987ed2a16
1 /*
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>
36 #include <linux/of.h>
37 #include <linux/of_device.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/atmel_pdc.h>
40 #include <linux/atmel_serial.h>
41 #include <linux/uaccess.h>
42 #include <linux/platform_data/atmel.h>
43 #include <linux/timer.h>
45 #include <asm/io.h>
46 #include <asm/ioctls.h>
48 #ifdef CONFIG_ARM
49 #include <mach/cpu.h>
50 #include <asm/gpio.h>
51 #endif
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)
58 #define SUPPORT_SYSRQ
59 #endif
61 #include <linux/serial_core.h>
63 static void atmel_start_rx(struct uart_port *port);
64 static void atmel_stop_rx(struct uart_port *port);
66 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
68 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
69 * should coexist with the 8250 driver, such as if we have an external 16C550
70 * UART. */
71 #define SERIAL_ATMEL_MAJOR 204
72 #define MINOR_START 154
73 #define ATMEL_DEVICENAME "ttyAT"
75 #else
77 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
78 * name, but it is legally reserved for the 8250 driver. */
79 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
80 #define MINOR_START 64
81 #define ATMEL_DEVICENAME "ttyS"
83 #endif
85 #define ATMEL_ISR_PASS_LIMIT 256
87 /* UART registers. CR is write-only, hence no GET macro */
88 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
89 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
90 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
91 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
92 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
93 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
94 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
95 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
96 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
97 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
98 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
99 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
100 #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
101 #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME)
102 #define UART_GET_IP_VERSION(port) __raw_readl((port)->membase + ATMEL_US_VERSION)
104 /* PDC registers */
105 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
106 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
108 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
109 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
110 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
111 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
112 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
114 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
115 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
116 #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
118 static int (*atmel_open_hook)(struct uart_port *);
119 static void (*atmel_close_hook)(struct uart_port *);
121 struct atmel_dma_buffer {
122 unsigned char *buf;
123 dma_addr_t dma_addr;
124 unsigned int dma_size;
125 unsigned int ofs;
128 struct atmel_uart_char {
129 u16 status;
130 u16 ch;
133 #define ATMEL_SERIAL_RINGSIZE 1024
136 * We wrap our port structure around the generic uart_port.
138 struct atmel_uart_port {
139 struct uart_port uart; /* uart */
140 struct clk *clk; /* uart clock */
141 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
142 u32 backup_imr; /* IMR saved during suspend */
143 int break_active; /* break being received */
145 bool use_dma_rx; /* enable DMA receiver */
146 bool use_pdc_rx; /* enable PDC receiver */
147 short pdc_rx_idx; /* current PDC RX buffer */
148 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
150 bool use_dma_tx; /* enable DMA transmitter */
151 bool use_pdc_tx; /* enable PDC transmitter */
152 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
154 spinlock_t lock_tx; /* port lock */
155 spinlock_t lock_rx; /* port lock */
156 struct dma_chan *chan_tx;
157 struct dma_chan *chan_rx;
158 struct dma_async_tx_descriptor *desc_tx;
159 struct dma_async_tx_descriptor *desc_rx;
160 dma_cookie_t cookie_tx;
161 dma_cookie_t cookie_rx;
162 struct scatterlist sg_tx;
163 struct scatterlist sg_rx;
164 struct tasklet_struct tasklet;
165 unsigned int irq_status;
166 unsigned int irq_status_prev;
168 struct circ_buf rx_ring;
170 struct serial_rs485 rs485; /* rs485 settings */
171 unsigned int tx_done_mask;
172 bool is_usart; /* usart or uart */
173 struct timer_list uart_timer; /* uart timer */
174 int (*prepare_rx)(struct uart_port *port);
175 int (*prepare_tx)(struct uart_port *port);
176 void (*schedule_rx)(struct uart_port *port);
177 void (*schedule_tx)(struct uart_port *port);
178 void (*release_rx)(struct uart_port *port);
179 void (*release_tx)(struct uart_port *port);
182 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
183 static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
185 #ifdef SUPPORT_SYSRQ
186 static struct console atmel_console;
187 #endif
189 #if defined(CONFIG_OF)
190 static const struct of_device_id atmel_serial_dt_ids[] = {
191 { .compatible = "atmel,at91rm9200-usart" },
192 { .compatible = "atmel,at91sam9260-usart" },
193 { /* sentinel */ }
196 MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
197 #endif
199 static inline struct atmel_uart_port *
200 to_atmel_uart_port(struct uart_port *uart)
202 return container_of(uart, struct atmel_uart_port, uart);
205 #ifdef CONFIG_SERIAL_ATMEL_PDC
206 static bool atmel_use_pdc_rx(struct uart_port *port)
208 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
210 return atmel_port->use_pdc_rx;
213 static bool atmel_use_pdc_tx(struct uart_port *port)
215 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
217 return atmel_port->use_pdc_tx;
219 #else
220 static bool atmel_use_pdc_rx(struct uart_port *port)
222 return false;
225 static bool atmel_use_pdc_tx(struct uart_port *port)
227 return false;
229 #endif
231 static bool atmel_use_dma_tx(struct uart_port *port)
233 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
235 return atmel_port->use_dma_tx;
238 static bool atmel_use_dma_rx(struct uart_port *port)
240 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
242 return atmel_port->use_dma_rx;
245 /* Enable or disable the rs485 support */
246 void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
248 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
249 unsigned int mode;
250 unsigned long flags;
252 spin_lock_irqsave(&port->lock, flags);
254 /* Disable interrupts */
255 UART_PUT_IDR(port, atmel_port->tx_done_mask);
257 mode = UART_GET_MR(port);
259 /* Resetting serial mode to RS232 (0x0) */
260 mode &= ~ATMEL_US_USMODE;
262 atmel_port->rs485 = *rs485conf;
264 if (rs485conf->flags & SER_RS485_ENABLED) {
265 dev_dbg(port->dev, "Setting UART to RS485\n");
266 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
267 if ((rs485conf->delay_rts_after_send) > 0)
268 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
269 mode |= ATMEL_US_USMODE_RS485;
270 } else {
271 dev_dbg(port->dev, "Setting UART to RS232\n");
272 if (atmel_use_pdc_tx(port))
273 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
274 ATMEL_US_TXBUFE;
275 else
276 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
278 UART_PUT_MR(port, mode);
280 /* Enable interrupts */
281 UART_PUT_IER(port, atmel_port->tx_done_mask);
283 spin_unlock_irqrestore(&port->lock, flags);
288 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
290 static u_int atmel_tx_empty(struct uart_port *port)
292 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
296 * Set state of the modem control output lines
298 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
300 unsigned int control = 0;
301 unsigned int mode;
302 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
304 #ifdef CONFIG_ARCH_AT91RM9200
305 if (cpu_is_at91rm9200()) {
307 * AT91RM9200 Errata #39: RTS0 is not internally connected
308 * to PA21. We need to drive the pin manually.
310 if (port->mapbase == AT91RM9200_BASE_US0) {
311 if (mctrl & TIOCM_RTS)
312 at91_set_gpio_value(AT91_PIN_PA21, 0);
313 else
314 at91_set_gpio_value(AT91_PIN_PA21, 1);
317 #endif
319 if (mctrl & TIOCM_RTS)
320 control |= ATMEL_US_RTSEN;
321 else
322 control |= ATMEL_US_RTSDIS;
324 if (mctrl & TIOCM_DTR)
325 control |= ATMEL_US_DTREN;
326 else
327 control |= ATMEL_US_DTRDIS;
329 UART_PUT_CR(port, control);
331 /* Local loopback mode? */
332 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
333 if (mctrl & TIOCM_LOOP)
334 mode |= ATMEL_US_CHMODE_LOC_LOOP;
335 else
336 mode |= ATMEL_US_CHMODE_NORMAL;
338 /* Resetting serial mode to RS232 (0x0) */
339 mode &= ~ATMEL_US_USMODE;
341 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
342 dev_dbg(port->dev, "Setting UART to RS485\n");
343 if ((atmel_port->rs485.delay_rts_after_send) > 0)
344 UART_PUT_TTGR(port,
345 atmel_port->rs485.delay_rts_after_send);
346 mode |= ATMEL_US_USMODE_RS485;
347 } else {
348 dev_dbg(port->dev, "Setting UART to RS232\n");
350 UART_PUT_MR(port, mode);
354 * Get state of the modem control input lines
356 static u_int atmel_get_mctrl(struct uart_port *port)
358 unsigned int status, ret = 0;
360 status = UART_GET_CSR(port);
363 * The control signals are active low.
365 if (!(status & ATMEL_US_DCD))
366 ret |= TIOCM_CD;
367 if (!(status & ATMEL_US_CTS))
368 ret |= TIOCM_CTS;
369 if (!(status & ATMEL_US_DSR))
370 ret |= TIOCM_DSR;
371 if (!(status & ATMEL_US_RI))
372 ret |= TIOCM_RI;
374 return ret;
378 * Stop transmitting.
380 static void atmel_stop_tx(struct uart_port *port)
382 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
384 if (atmel_use_pdc_tx(port)) {
385 /* disable PDC transmit */
386 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
388 /* Disable interrupts */
389 UART_PUT_IDR(port, atmel_port->tx_done_mask);
391 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
392 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
393 atmel_start_rx(port);
397 * Start transmitting.
399 static void atmel_start_tx(struct uart_port *port)
401 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
403 if (atmel_use_pdc_tx(port)) {
404 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
405 /* The transmitter is already running. Yes, we
406 really need this.*/
407 return;
409 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
410 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
411 atmel_stop_rx(port);
413 /* re-enable PDC transmit */
414 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
416 /* Enable interrupts */
417 UART_PUT_IER(port, atmel_port->tx_done_mask);
421 * start receiving - port is in process of being opened.
423 static void atmel_start_rx(struct uart_port *port)
425 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
427 UART_PUT_CR(port, ATMEL_US_RXEN);
429 if (atmel_use_pdc_rx(port)) {
430 /* enable PDC controller */
431 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
432 port->read_status_mask);
433 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
434 } else {
435 UART_PUT_IER(port, ATMEL_US_RXRDY);
440 * Stop receiving - port is in process of being closed.
442 static void atmel_stop_rx(struct uart_port *port)
444 UART_PUT_CR(port, ATMEL_US_RXDIS);
446 if (atmel_use_pdc_rx(port)) {
447 /* disable PDC receive */
448 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
449 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
450 port->read_status_mask);
451 } else {
452 UART_PUT_IDR(port, ATMEL_US_RXRDY);
457 * Enable modem status interrupts
459 static void atmel_enable_ms(struct uart_port *port)
461 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
462 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
466 * Control the transmission of a break signal
468 static void atmel_break_ctl(struct uart_port *port, int break_state)
470 if (break_state != 0)
471 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
472 else
473 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
477 * Stores the incoming character in the ring buffer
479 static void
480 atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
481 unsigned int ch)
483 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
484 struct circ_buf *ring = &atmel_port->rx_ring;
485 struct atmel_uart_char *c;
487 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
488 /* Buffer overflow, ignore char */
489 return;
491 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
492 c->status = status;
493 c->ch = ch;
495 /* Make sure the character is stored before we update head. */
496 smp_wmb();
498 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
502 * Deal with parity, framing and overrun errors.
504 static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
506 /* clear error */
507 UART_PUT_CR(port, ATMEL_US_RSTSTA);
509 if (status & ATMEL_US_RXBRK) {
510 /* ignore side-effect */
511 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
512 port->icount.brk++;
514 if (status & ATMEL_US_PARE)
515 port->icount.parity++;
516 if (status & ATMEL_US_FRAME)
517 port->icount.frame++;
518 if (status & ATMEL_US_OVRE)
519 port->icount.overrun++;
523 * Characters received (called from interrupt handler)
525 static void atmel_rx_chars(struct uart_port *port)
527 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
528 unsigned int status, ch;
530 status = UART_GET_CSR(port);
531 while (status & ATMEL_US_RXRDY) {
532 ch = UART_GET_CHAR(port);
535 * note that the error handling code is
536 * out of the main execution path
538 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
539 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
540 || atmel_port->break_active)) {
542 /* clear error */
543 UART_PUT_CR(port, ATMEL_US_RSTSTA);
545 if (status & ATMEL_US_RXBRK
546 && !atmel_port->break_active) {
547 atmel_port->break_active = 1;
548 UART_PUT_IER(port, ATMEL_US_RXBRK);
549 } else {
551 * This is either the end-of-break
552 * condition or we've received at
553 * least one character without RXBRK
554 * being set. In both cases, the next
555 * RXBRK will indicate start-of-break.
557 UART_PUT_IDR(port, ATMEL_US_RXBRK);
558 status &= ~ATMEL_US_RXBRK;
559 atmel_port->break_active = 0;
563 atmel_buffer_rx_char(port, status, ch);
564 status = UART_GET_CSR(port);
567 tasklet_schedule(&atmel_port->tasklet);
571 * Transmit characters (called from tasklet with TXRDY interrupt
572 * disabled)
574 static void atmel_tx_chars(struct uart_port *port)
576 struct circ_buf *xmit = &port->state->xmit;
577 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
579 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
580 UART_PUT_CHAR(port, port->x_char);
581 port->icount.tx++;
582 port->x_char = 0;
584 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
585 return;
587 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
588 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
589 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
590 port->icount.tx++;
591 if (uart_circ_empty(xmit))
592 break;
595 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
596 uart_write_wakeup(port);
598 if (!uart_circ_empty(xmit))
599 /* Enable interrupts */
600 UART_PUT_IER(port, atmel_port->tx_done_mask);
603 static void atmel_complete_tx_dma(void *arg)
605 struct atmel_uart_port *atmel_port = arg;
606 struct uart_port *port = &atmel_port->uart;
607 struct circ_buf *xmit = &port->state->xmit;
608 struct dma_chan *chan = atmel_port->chan_tx;
609 unsigned long flags;
611 spin_lock_irqsave(&port->lock, flags);
613 if (chan)
614 dmaengine_terminate_all(chan);
615 xmit->tail += sg_dma_len(&atmel_port->sg_tx);
616 xmit->tail &= UART_XMIT_SIZE - 1;
618 port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
620 spin_lock_irq(&atmel_port->lock_tx);
621 async_tx_ack(atmel_port->desc_tx);
622 atmel_port->cookie_tx = -EINVAL;
623 atmel_port->desc_tx = NULL;
624 spin_unlock_irq(&atmel_port->lock_tx);
626 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
627 uart_write_wakeup(port);
629 /* Do we really need this? */
630 if (!uart_circ_empty(xmit))
631 tasklet_schedule(&atmel_port->tasklet);
633 spin_unlock_irqrestore(&port->lock, flags);
636 static void atmel_release_tx_dma(struct uart_port *port)
638 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
639 struct dma_chan *chan = atmel_port->chan_tx;
641 if (chan) {
642 dmaengine_terminate_all(chan);
643 dma_release_channel(chan);
644 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
645 DMA_MEM_TO_DEV);
648 atmel_port->desc_tx = NULL;
649 atmel_port->chan_tx = NULL;
650 atmel_port->cookie_tx = -EINVAL;
654 * Called from tasklet with TXRDY interrupt is disabled.
656 static void atmel_tx_dma(struct uart_port *port)
658 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
659 struct circ_buf *xmit = &port->state->xmit;
660 struct dma_chan *chan = atmel_port->chan_tx;
661 struct dma_async_tx_descriptor *desc;
662 struct scatterlist *sg = &atmel_port->sg_tx;
664 /* Make sure we have an idle channel */
665 if (atmel_port->desc_tx != NULL)
666 return;
668 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
670 * DMA is idle now.
671 * Port xmit buffer is already mapped,
672 * and it is one page... Just adjust
673 * offsets and lengths. Since it is a circular buffer,
674 * we have to transmit till the end, and then the rest.
675 * Take the port lock to get a
676 * consistent xmit buffer state.
678 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
679 sg_dma_address(sg) = (sg_dma_address(sg) &
680 ~(UART_XMIT_SIZE - 1))
681 + sg->offset;
682 sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
683 xmit->tail,
684 UART_XMIT_SIZE);
685 BUG_ON(!sg_dma_len(sg));
687 desc = dmaengine_prep_slave_sg(chan,
690 DMA_MEM_TO_DEV,
691 DMA_PREP_INTERRUPT |
692 DMA_CTRL_ACK);
693 if (!desc) {
694 dev_err(port->dev, "Failed to send via dma!\n");
695 return;
698 dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
700 atmel_port->desc_tx = desc;
701 desc->callback = atmel_complete_tx_dma;
702 desc->callback_param = atmel_port;
703 atmel_port->cookie_tx = dmaengine_submit(desc);
705 } else {
706 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
707 /* DMA done, stop TX, start RX for RS485 */
708 atmel_start_rx(port);
712 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
713 uart_write_wakeup(port);
716 static int atmel_prepare_tx_dma(struct uart_port *port)
718 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
719 dma_cap_mask_t mask;
720 struct dma_slave_config config;
721 int ret, nent;
723 dma_cap_zero(mask);
724 dma_cap_set(DMA_SLAVE, mask);
726 atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
727 if (atmel_port->chan_tx == NULL)
728 goto chan_err;
729 dev_info(port->dev, "using %s for tx DMA transfers\n",
730 dma_chan_name(atmel_port->chan_tx));
732 spin_lock_init(&atmel_port->lock_tx);
733 sg_init_table(&atmel_port->sg_tx, 1);
734 /* UART circular tx buffer is an aligned page. */
735 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
736 sg_set_page(&atmel_port->sg_tx,
737 virt_to_page(port->state->xmit.buf),
738 UART_XMIT_SIZE,
739 (int)port->state->xmit.buf & ~PAGE_MASK);
740 nent = dma_map_sg(port->dev,
741 &atmel_port->sg_tx,
743 DMA_MEM_TO_DEV);
745 if (!nent) {
746 dev_dbg(port->dev, "need to release resource of dma\n");
747 goto chan_err;
748 } else {
749 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
750 sg_dma_len(&atmel_port->sg_tx),
751 port->state->xmit.buf,
752 sg_dma_address(&atmel_port->sg_tx));
755 /* Configure the slave DMA */
756 memset(&config, 0, sizeof(config));
757 config.direction = DMA_MEM_TO_DEV;
758 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
759 config.dst_addr = port->mapbase + ATMEL_US_THR;
761 ret = dmaengine_device_control(atmel_port->chan_tx,
762 DMA_SLAVE_CONFIG,
763 (unsigned long)&config);
764 if (ret) {
765 dev_err(port->dev, "DMA tx slave configuration failed\n");
766 goto chan_err;
769 return 0;
771 chan_err:
772 dev_err(port->dev, "TX channel not available, switch to pio\n");
773 atmel_port->use_dma_tx = 0;
774 if (atmel_port->chan_tx)
775 atmel_release_tx_dma(port);
776 return -EINVAL;
779 static void atmel_flip_buffer_rx_dma(struct uart_port *port,
780 char *buf, size_t count)
782 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
783 struct tty_port *tport = &port->state->port;
785 dma_sync_sg_for_cpu(port->dev,
786 &atmel_port->sg_rx,
788 DMA_DEV_TO_MEM);
790 tty_insert_flip_string(tport, buf, count);
792 dma_sync_sg_for_device(port->dev,
793 &atmel_port->sg_rx,
795 DMA_DEV_TO_MEM);
797 * Drop the lock here since it might end up calling
798 * uart_start(), which takes the lock.
800 spin_unlock(&port->lock);
801 tty_flip_buffer_push(tport);
802 spin_lock(&port->lock);
805 static void atmel_complete_rx_dma(void *arg)
807 struct uart_port *port = arg;
808 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
810 tasklet_schedule(&atmel_port->tasklet);
813 static void atmel_release_rx_dma(struct uart_port *port)
815 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
816 struct dma_chan *chan = atmel_port->chan_rx;
818 if (chan) {
819 dmaengine_terminate_all(chan);
820 dma_release_channel(chan);
821 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
822 DMA_DEV_TO_MEM);
825 atmel_port->desc_rx = NULL;
826 atmel_port->chan_rx = NULL;
827 atmel_port->cookie_rx = -EINVAL;
830 static void atmel_rx_from_dma(struct uart_port *port)
832 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
833 struct circ_buf *ring = &atmel_port->rx_ring;
834 struct dma_chan *chan = atmel_port->chan_rx;
835 struct dma_tx_state state;
836 enum dma_status dmastat;
837 size_t pending, count;
840 /* Reset the UART timeout early so that we don't miss one */
841 UART_PUT_CR(port, ATMEL_US_STTTO);
842 dmastat = dmaengine_tx_status(chan,
843 atmel_port->cookie_rx,
844 &state);
845 /* Restart a new tasklet if DMA status is error */
846 if (dmastat == DMA_ERROR) {
847 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
848 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
849 tasklet_schedule(&atmel_port->tasklet);
850 return;
852 /* current transfer size should no larger than dma buffer */
853 pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
854 BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
857 * This will take the chars we have so far,
858 * ring->head will record the transfer size, only new bytes come
859 * will insert into the framework.
861 if (pending > ring->head) {
862 count = pending - ring->head;
864 atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
866 ring->head += count;
867 if (ring->head == sg_dma_len(&atmel_port->sg_rx))
868 ring->head = 0;
870 port->icount.rx += count;
873 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
876 static int atmel_prepare_rx_dma(struct uart_port *port)
878 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
879 struct dma_async_tx_descriptor *desc;
880 dma_cap_mask_t mask;
881 struct dma_slave_config config;
882 struct circ_buf *ring;
883 int ret, nent;
885 ring = &atmel_port->rx_ring;
887 dma_cap_zero(mask);
888 dma_cap_set(DMA_CYCLIC, mask);
890 atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
891 if (atmel_port->chan_rx == NULL)
892 goto chan_err;
893 dev_info(port->dev, "using %s for rx DMA transfers\n",
894 dma_chan_name(atmel_port->chan_rx));
896 spin_lock_init(&atmel_port->lock_rx);
897 sg_init_table(&atmel_port->sg_rx, 1);
898 /* UART circular rx buffer is an aligned page. */
899 BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
900 sg_set_page(&atmel_port->sg_rx,
901 virt_to_page(ring->buf),
902 ATMEL_SERIAL_RINGSIZE,
903 (int)ring->buf & ~PAGE_MASK);
904 nent = dma_map_sg(port->dev,
905 &atmel_port->sg_rx,
907 DMA_DEV_TO_MEM);
909 if (!nent) {
910 dev_dbg(port->dev, "need to release resource of dma\n");
911 goto chan_err;
912 } else {
913 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
914 sg_dma_len(&atmel_port->sg_rx),
915 ring->buf,
916 sg_dma_address(&atmel_port->sg_rx));
919 /* Configure the slave DMA */
920 memset(&config, 0, sizeof(config));
921 config.direction = DMA_DEV_TO_MEM;
922 config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
923 config.src_addr = port->mapbase + ATMEL_US_RHR;
925 ret = dmaengine_device_control(atmel_port->chan_rx,
926 DMA_SLAVE_CONFIG,
927 (unsigned long)&config);
928 if (ret) {
929 dev_err(port->dev, "DMA rx slave configuration failed\n");
930 goto chan_err;
933 * Prepare a cyclic dma transfer, assign 2 descriptors,
934 * each one is half ring buffer size
936 desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
937 sg_dma_address(&atmel_port->sg_rx),
938 sg_dma_len(&atmel_port->sg_rx),
939 sg_dma_len(&atmel_port->sg_rx)/2,
940 DMA_DEV_TO_MEM,
941 DMA_PREP_INTERRUPT);
942 desc->callback = atmel_complete_rx_dma;
943 desc->callback_param = port;
944 atmel_port->desc_rx = desc;
945 atmel_port->cookie_rx = dmaengine_submit(desc);
947 return 0;
949 chan_err:
950 dev_err(port->dev, "RX channel not available, switch to pio\n");
951 atmel_port->use_dma_rx = 0;
952 if (atmel_port->chan_rx)
953 atmel_release_rx_dma(port);
954 return -EINVAL;
957 static void atmel_uart_timer_callback(unsigned long data)
959 struct uart_port *port = (void *)data;
960 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
962 tasklet_schedule(&atmel_port->tasklet);
963 mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
967 * receive interrupt handler.
969 static void
970 atmel_handle_receive(struct uart_port *port, unsigned int pending)
972 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
974 if (atmel_use_pdc_rx(port)) {
976 * PDC receive. Just schedule the tasklet and let it
977 * figure out the details.
979 * TODO: We're not handling error flags correctly at
980 * the moment.
982 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
983 UART_PUT_IDR(port, (ATMEL_US_ENDRX
984 | ATMEL_US_TIMEOUT));
985 tasklet_schedule(&atmel_port->tasklet);
988 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
989 ATMEL_US_FRAME | ATMEL_US_PARE))
990 atmel_pdc_rxerr(port, pending);
993 if (atmel_use_dma_rx(port)) {
994 if (pending & ATMEL_US_TIMEOUT) {
995 UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
996 tasklet_schedule(&atmel_port->tasklet);
1000 /* Interrupt receive */
1001 if (pending & ATMEL_US_RXRDY)
1002 atmel_rx_chars(port);
1003 else if (pending & ATMEL_US_RXBRK) {
1005 * End of break detected. If it came along with a
1006 * character, atmel_rx_chars will handle it.
1008 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1009 UART_PUT_IDR(port, ATMEL_US_RXBRK);
1010 atmel_port->break_active = 0;
1015 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1017 static void
1018 atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1020 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1022 if (pending & atmel_port->tx_done_mask) {
1023 /* Either PDC or interrupt transmission */
1024 UART_PUT_IDR(port, atmel_port->tx_done_mask);
1025 tasklet_schedule(&atmel_port->tasklet);
1030 * status flags interrupt handler.
1032 static void
1033 atmel_handle_status(struct uart_port *port, unsigned int pending,
1034 unsigned int status)
1036 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1038 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1039 | ATMEL_US_CTSIC)) {
1040 atmel_port->irq_status = status;
1041 tasklet_schedule(&atmel_port->tasklet);
1046 * Interrupt handler
1048 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1050 struct uart_port *port = dev_id;
1051 unsigned int status, pending, pass_counter = 0;
1053 do {
1054 status = UART_GET_CSR(port);
1055 pending = status & UART_GET_IMR(port);
1056 if (!pending)
1057 break;
1059 atmel_handle_receive(port, pending);
1060 atmel_handle_status(port, pending, status);
1061 atmel_handle_transmit(port, pending);
1062 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1064 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1067 static void atmel_release_tx_pdc(struct uart_port *port)
1069 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1070 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1072 dma_unmap_single(port->dev,
1073 pdc->dma_addr,
1074 pdc->dma_size,
1075 DMA_TO_DEVICE);
1079 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1081 static void atmel_tx_pdc(struct uart_port *port)
1083 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1084 struct circ_buf *xmit = &port->state->xmit;
1085 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1086 int count;
1088 /* nothing left to transmit? */
1089 if (UART_GET_TCR(port))
1090 return;
1092 xmit->tail += pdc->ofs;
1093 xmit->tail &= UART_XMIT_SIZE - 1;
1095 port->icount.tx += pdc->ofs;
1096 pdc->ofs = 0;
1098 /* more to transmit - setup next transfer */
1100 /* disable PDC transmit */
1101 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1103 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1104 dma_sync_single_for_device(port->dev,
1105 pdc->dma_addr,
1106 pdc->dma_size,
1107 DMA_TO_DEVICE);
1109 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1110 pdc->ofs = count;
1112 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1113 UART_PUT_TCR(port, count);
1114 /* re-enable PDC transmit */
1115 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1116 /* Enable interrupts */
1117 UART_PUT_IER(port, atmel_port->tx_done_mask);
1118 } else {
1119 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
1120 !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1121 /* DMA done, stop TX, start RX for RS485 */
1122 atmel_start_rx(port);
1126 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1127 uart_write_wakeup(port);
1130 static int atmel_prepare_tx_pdc(struct uart_port *port)
1132 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1133 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1134 struct circ_buf *xmit = &port->state->xmit;
1136 pdc->buf = xmit->buf;
1137 pdc->dma_addr = dma_map_single(port->dev,
1138 pdc->buf,
1139 UART_XMIT_SIZE,
1140 DMA_TO_DEVICE);
1141 pdc->dma_size = UART_XMIT_SIZE;
1142 pdc->ofs = 0;
1144 return 0;
1147 static void atmel_rx_from_ring(struct uart_port *port)
1149 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1150 struct circ_buf *ring = &atmel_port->rx_ring;
1151 unsigned int flg;
1152 unsigned int status;
1154 while (ring->head != ring->tail) {
1155 struct atmel_uart_char c;
1157 /* Make sure c is loaded after head. */
1158 smp_rmb();
1160 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1162 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1164 port->icount.rx++;
1165 status = c.status;
1166 flg = TTY_NORMAL;
1169 * note that the error handling code is
1170 * out of the main execution path
1172 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1173 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1174 if (status & ATMEL_US_RXBRK) {
1175 /* ignore side-effect */
1176 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1178 port->icount.brk++;
1179 if (uart_handle_break(port))
1180 continue;
1182 if (status & ATMEL_US_PARE)
1183 port->icount.parity++;
1184 if (status & ATMEL_US_FRAME)
1185 port->icount.frame++;
1186 if (status & ATMEL_US_OVRE)
1187 port->icount.overrun++;
1189 status &= port->read_status_mask;
1191 if (status & ATMEL_US_RXBRK)
1192 flg = TTY_BREAK;
1193 else if (status & ATMEL_US_PARE)
1194 flg = TTY_PARITY;
1195 else if (status & ATMEL_US_FRAME)
1196 flg = TTY_FRAME;
1200 if (uart_handle_sysrq_char(port, c.ch))
1201 continue;
1203 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1207 * Drop the lock here since it might end up calling
1208 * uart_start(), which takes the lock.
1210 spin_unlock(&port->lock);
1211 tty_flip_buffer_push(&port->state->port);
1212 spin_lock(&port->lock);
1215 static void atmel_release_rx_pdc(struct uart_port *port)
1217 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1218 int i;
1220 for (i = 0; i < 2; i++) {
1221 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1223 dma_unmap_single(port->dev,
1224 pdc->dma_addr,
1225 pdc->dma_size,
1226 DMA_FROM_DEVICE);
1227 kfree(pdc->buf);
1231 static void atmel_rx_from_pdc(struct uart_port *port)
1233 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1234 struct tty_port *tport = &port->state->port;
1235 struct atmel_dma_buffer *pdc;
1236 int rx_idx = atmel_port->pdc_rx_idx;
1237 unsigned int head;
1238 unsigned int tail;
1239 unsigned int count;
1241 do {
1242 /* Reset the UART timeout early so that we don't miss one */
1243 UART_PUT_CR(port, ATMEL_US_STTTO);
1245 pdc = &atmel_port->pdc_rx[rx_idx];
1246 head = UART_GET_RPR(port) - pdc->dma_addr;
1247 tail = pdc->ofs;
1249 /* If the PDC has switched buffers, RPR won't contain
1250 * any address within the current buffer. Since head
1251 * is unsigned, we just need a one-way comparison to
1252 * find out.
1254 * In this case, we just need to consume the entire
1255 * buffer and resubmit it for DMA. This will clear the
1256 * ENDRX bit as well, so that we can safely re-enable
1257 * all interrupts below.
1259 head = min(head, pdc->dma_size);
1261 if (likely(head != tail)) {
1262 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1263 pdc->dma_size, DMA_FROM_DEVICE);
1266 * head will only wrap around when we recycle
1267 * the DMA buffer, and when that happens, we
1268 * explicitly set tail to 0. So head will
1269 * always be greater than tail.
1271 count = head - tail;
1273 tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1274 count);
1276 dma_sync_single_for_device(port->dev, pdc->dma_addr,
1277 pdc->dma_size, DMA_FROM_DEVICE);
1279 port->icount.rx += count;
1280 pdc->ofs = head;
1284 * If the current buffer is full, we need to check if
1285 * the next one contains any additional data.
1287 if (head >= pdc->dma_size) {
1288 pdc->ofs = 0;
1289 UART_PUT_RNPR(port, pdc->dma_addr);
1290 UART_PUT_RNCR(port, pdc->dma_size);
1292 rx_idx = !rx_idx;
1293 atmel_port->pdc_rx_idx = rx_idx;
1295 } while (head >= pdc->dma_size);
1298 * Drop the lock here since it might end up calling
1299 * uart_start(), which takes the lock.
1301 spin_unlock(&port->lock);
1302 tty_flip_buffer_push(tport);
1303 spin_lock(&port->lock);
1305 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1308 static int atmel_prepare_rx_pdc(struct uart_port *port)
1310 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1311 int i;
1313 for (i = 0; i < 2; i++) {
1314 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1316 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1317 if (pdc->buf == NULL) {
1318 if (i != 0) {
1319 dma_unmap_single(port->dev,
1320 atmel_port->pdc_rx[0].dma_addr,
1321 PDC_BUFFER_SIZE,
1322 DMA_FROM_DEVICE);
1323 kfree(atmel_port->pdc_rx[0].buf);
1325 atmel_port->use_pdc_rx = 0;
1326 return -ENOMEM;
1328 pdc->dma_addr = dma_map_single(port->dev,
1329 pdc->buf,
1330 PDC_BUFFER_SIZE,
1331 DMA_FROM_DEVICE);
1332 pdc->dma_size = PDC_BUFFER_SIZE;
1333 pdc->ofs = 0;
1336 atmel_port->pdc_rx_idx = 0;
1338 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1339 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1341 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1342 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1344 return 0;
1348 * tasklet handling tty stuff outside the interrupt handler.
1350 static void atmel_tasklet_func(unsigned long data)
1352 struct uart_port *port = (struct uart_port *)data;
1353 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1354 unsigned int status;
1355 unsigned int status_change;
1357 /* The interrupt handler does not take the lock */
1358 spin_lock(&port->lock);
1360 atmel_port->schedule_tx(port);
1362 status = atmel_port->irq_status;
1363 status_change = status ^ atmel_port->irq_status_prev;
1365 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1366 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1367 /* TODO: All reads to CSR will clear these interrupts! */
1368 if (status_change & ATMEL_US_RI)
1369 port->icount.rng++;
1370 if (status_change & ATMEL_US_DSR)
1371 port->icount.dsr++;
1372 if (status_change & ATMEL_US_DCD)
1373 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1374 if (status_change & ATMEL_US_CTS)
1375 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1377 wake_up_interruptible(&port->state->port.delta_msr_wait);
1379 atmel_port->irq_status_prev = status;
1382 atmel_port->schedule_rx(port);
1384 spin_unlock(&port->lock);
1387 static int atmel_init_property(struct atmel_uart_port *atmel_port,
1388 struct platform_device *pdev)
1390 struct device_node *np = pdev->dev.of_node;
1391 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1393 if (np) {
1394 /* DMA/PDC usage specification */
1395 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1396 if (of_get_property(np, "dmas", NULL)) {
1397 atmel_port->use_dma_rx = true;
1398 atmel_port->use_pdc_rx = false;
1399 } else {
1400 atmel_port->use_dma_rx = false;
1401 atmel_port->use_pdc_rx = true;
1403 } else {
1404 atmel_port->use_dma_rx = false;
1405 atmel_port->use_pdc_rx = false;
1408 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1409 if (of_get_property(np, "dmas", NULL)) {
1410 atmel_port->use_dma_tx = true;
1411 atmel_port->use_pdc_tx = false;
1412 } else {
1413 atmel_port->use_dma_tx = false;
1414 atmel_port->use_pdc_tx = true;
1416 } else {
1417 atmel_port->use_dma_tx = false;
1418 atmel_port->use_pdc_tx = false;
1421 } else {
1422 atmel_port->use_pdc_rx = pdata->use_dma_rx;
1423 atmel_port->use_pdc_tx = pdata->use_dma_tx;
1424 atmel_port->use_dma_rx = false;
1425 atmel_port->use_dma_tx = false;
1428 return 0;
1431 static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
1432 struct platform_device *pdev)
1434 struct device_node *np = pdev->dev.of_node;
1435 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1437 if (np) {
1438 u32 rs485_delay[2];
1439 /* rs485 properties */
1440 if (of_property_read_u32_array(np, "rs485-rts-delay",
1441 rs485_delay, 2) == 0) {
1442 struct serial_rs485 *rs485conf = &atmel_port->rs485;
1444 rs485conf->delay_rts_before_send = rs485_delay[0];
1445 rs485conf->delay_rts_after_send = rs485_delay[1];
1446 rs485conf->flags = 0;
1448 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1449 rs485conf->flags |= SER_RS485_RX_DURING_TX;
1451 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1452 NULL))
1453 rs485conf->flags |= SER_RS485_ENABLED;
1455 } else {
1456 atmel_port->rs485 = pdata->rs485;
1461 static void atmel_set_ops(struct uart_port *port)
1463 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1465 if (atmel_use_dma_rx(port)) {
1466 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1467 atmel_port->schedule_rx = &atmel_rx_from_dma;
1468 atmel_port->release_rx = &atmel_release_rx_dma;
1469 } else if (atmel_use_pdc_rx(port)) {
1470 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1471 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1472 atmel_port->release_rx = &atmel_release_rx_pdc;
1473 } else {
1474 atmel_port->prepare_rx = NULL;
1475 atmel_port->schedule_rx = &atmel_rx_from_ring;
1476 atmel_port->release_rx = NULL;
1479 if (atmel_use_dma_tx(port)) {
1480 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1481 atmel_port->schedule_tx = &atmel_tx_dma;
1482 atmel_port->release_tx = &atmel_release_tx_dma;
1483 } else if (atmel_use_pdc_tx(port)) {
1484 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1485 atmel_port->schedule_tx = &atmel_tx_pdc;
1486 atmel_port->release_tx = &atmel_release_tx_pdc;
1487 } else {
1488 atmel_port->prepare_tx = NULL;
1489 atmel_port->schedule_tx = &atmel_tx_chars;
1490 atmel_port->release_tx = NULL;
1495 * Get ip name usart or uart
1497 static void atmel_get_ip_name(struct uart_port *port)
1499 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1500 int name = UART_GET_IP_NAME(port);
1501 u32 version;
1502 int usart, uart;
1503 /* usart and uart ascii */
1504 usart = 0x55534152;
1505 uart = 0x44424755;
1507 atmel_port->is_usart = false;
1509 if (name == usart) {
1510 dev_dbg(port->dev, "This is usart\n");
1511 atmel_port->is_usart = true;
1512 } else if (name == uart) {
1513 dev_dbg(port->dev, "This is uart\n");
1514 atmel_port->is_usart = false;
1515 } else {
1516 /* fallback for older SoCs: use version field */
1517 version = UART_GET_IP_VERSION(port);
1518 switch (version) {
1519 case 0x302:
1520 case 0x10213:
1521 dev_dbg(port->dev, "This version is usart\n");
1522 atmel_port->is_usart = true;
1523 break;
1524 case 0x203:
1525 case 0x10202:
1526 dev_dbg(port->dev, "This version is uart\n");
1527 atmel_port->is_usart = false;
1528 break;
1529 default:
1530 dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1536 * Perform initialization and enable port for reception
1538 static int atmel_startup(struct uart_port *port)
1540 struct platform_device *pdev = to_platform_device(port->dev);
1541 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1542 struct tty_struct *tty = port->state->port.tty;
1543 int retval;
1546 * Ensure that no interrupts are enabled otherwise when
1547 * request_irq() is called we could get stuck trying to
1548 * handle an unexpected interrupt
1550 UART_PUT_IDR(port, -1);
1553 * Allocate the IRQ
1555 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
1556 tty ? tty->name : "atmel_serial", port);
1557 if (retval) {
1558 printk("atmel_serial: atmel_startup - Can't get irq\n");
1559 return retval;
1563 * Initialize DMA (if necessary)
1565 atmel_init_property(atmel_port, pdev);
1567 if (atmel_port->prepare_rx) {
1568 retval = atmel_port->prepare_rx(port);
1569 if (retval < 0)
1570 atmel_set_ops(port);
1573 if (atmel_port->prepare_tx) {
1574 retval = atmel_port->prepare_tx(port);
1575 if (retval < 0)
1576 atmel_set_ops(port);
1579 * If there is a specific "open" function (to register
1580 * control line interrupts)
1582 if (atmel_open_hook) {
1583 retval = atmel_open_hook(port);
1584 if (retval) {
1585 free_irq(port->irq, port);
1586 return retval;
1590 /* Save current CSR for comparison in atmel_tasklet_func() */
1591 atmel_port->irq_status_prev = UART_GET_CSR(port);
1592 atmel_port->irq_status = atmel_port->irq_status_prev;
1595 * Finally, enable the serial port
1597 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1598 /* enable xmit & rcvr */
1599 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1601 setup_timer(&atmel_port->uart_timer,
1602 atmel_uart_timer_callback,
1603 (unsigned long)port);
1605 if (atmel_use_pdc_rx(port)) {
1606 /* set UART timeout */
1607 if (!atmel_port->is_usart) {
1608 mod_timer(&atmel_port->uart_timer,
1609 jiffies + uart_poll_timeout(port));
1610 /* set USART timeout */
1611 } else {
1612 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1613 UART_PUT_CR(port, ATMEL_US_STTTO);
1615 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1617 /* enable PDC controller */
1618 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
1619 } else if (atmel_use_dma_rx(port)) {
1620 /* set UART timeout */
1621 if (!atmel_port->is_usart) {
1622 mod_timer(&atmel_port->uart_timer,
1623 jiffies + uart_poll_timeout(port));
1624 /* set USART timeout */
1625 } else {
1626 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1627 UART_PUT_CR(port, ATMEL_US_STTTO);
1629 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
1631 } else {
1632 /* enable receive only */
1633 UART_PUT_IER(port, ATMEL_US_RXRDY);
1636 return 0;
1640 * Disable the port
1642 static void atmel_shutdown(struct uart_port *port)
1644 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1647 * Prevent any tasklets being scheduled during
1648 * cleanup
1650 del_timer_sync(&atmel_port->uart_timer);
1653 * Clear out any scheduled tasklets before
1654 * we destroy the buffers
1656 tasklet_kill(&atmel_port->tasklet);
1659 * Ensure everything is stopped and
1660 * disable all interrupts, port and break condition.
1662 atmel_stop_rx(port);
1663 atmel_stop_tx(port);
1665 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1666 UART_PUT_IDR(port, -1);
1670 * Shut-down the DMA.
1672 if (atmel_port->release_rx)
1673 atmel_port->release_rx(port);
1674 if (atmel_port->release_tx)
1675 atmel_port->release_tx(port);
1678 * Reset ring buffer pointers
1680 atmel_port->rx_ring.head = 0;
1681 atmel_port->rx_ring.tail = 0;
1684 * Free the interrupt
1686 free_irq(port->irq, port);
1689 * If there is a specific "close" function (to unregister
1690 * control line interrupts)
1692 if (atmel_close_hook)
1693 atmel_close_hook(port);
1697 * Flush any TX data submitted for DMA. Called when the TX circular
1698 * buffer is reset.
1700 static void atmel_flush_buffer(struct uart_port *port)
1702 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1704 if (atmel_use_pdc_tx(port)) {
1705 UART_PUT_TCR(port, 0);
1706 atmel_port->pdc_tx.ofs = 0;
1711 * Power / Clock management.
1713 static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1714 unsigned int oldstate)
1716 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1718 switch (state) {
1719 case 0:
1721 * Enable the peripheral clock for this serial port.
1722 * This is called on uart_open() or a resume event.
1724 clk_prepare_enable(atmel_port->clk);
1726 /* re-enable interrupts if we disabled some on suspend */
1727 UART_PUT_IER(port, atmel_port->backup_imr);
1728 break;
1729 case 3:
1730 /* Back up the interrupt mask and disable all interrupts */
1731 atmel_port->backup_imr = UART_GET_IMR(port);
1732 UART_PUT_IDR(port, -1);
1735 * Disable the peripheral clock for this serial port.
1736 * This is called on uart_close() or a suspend event.
1738 clk_disable_unprepare(atmel_port->clk);
1739 break;
1740 default:
1741 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1746 * Change the port parameters
1748 static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1749 struct ktermios *old)
1751 unsigned long flags;
1752 unsigned int mode, imr, quot, baud;
1753 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1755 /* Get current mode register */
1756 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1757 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1758 | ATMEL_US_USMODE);
1760 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1761 quot = uart_get_divisor(port, baud);
1763 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
1764 quot /= 8;
1765 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1768 /* byte size */
1769 switch (termios->c_cflag & CSIZE) {
1770 case CS5:
1771 mode |= ATMEL_US_CHRL_5;
1772 break;
1773 case CS6:
1774 mode |= ATMEL_US_CHRL_6;
1775 break;
1776 case CS7:
1777 mode |= ATMEL_US_CHRL_7;
1778 break;
1779 default:
1780 mode |= ATMEL_US_CHRL_8;
1781 break;
1784 /* stop bits */
1785 if (termios->c_cflag & CSTOPB)
1786 mode |= ATMEL_US_NBSTOP_2;
1788 /* parity */
1789 if (termios->c_cflag & PARENB) {
1790 /* Mark or Space parity */
1791 if (termios->c_cflag & CMSPAR) {
1792 if (termios->c_cflag & PARODD)
1793 mode |= ATMEL_US_PAR_MARK;
1794 else
1795 mode |= ATMEL_US_PAR_SPACE;
1796 } else if (termios->c_cflag & PARODD)
1797 mode |= ATMEL_US_PAR_ODD;
1798 else
1799 mode |= ATMEL_US_PAR_EVEN;
1800 } else
1801 mode |= ATMEL_US_PAR_NONE;
1803 /* hardware handshake (RTS/CTS) */
1804 if (termios->c_cflag & CRTSCTS)
1805 mode |= ATMEL_US_USMODE_HWHS;
1806 else
1807 mode |= ATMEL_US_USMODE_NORMAL;
1809 spin_lock_irqsave(&port->lock, flags);
1811 port->read_status_mask = ATMEL_US_OVRE;
1812 if (termios->c_iflag & INPCK)
1813 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1814 if (termios->c_iflag & (BRKINT | PARMRK))
1815 port->read_status_mask |= ATMEL_US_RXBRK;
1817 if (atmel_use_pdc_rx(port))
1818 /* need to enable error interrupts */
1819 UART_PUT_IER(port, port->read_status_mask);
1822 * Characters to ignore
1824 port->ignore_status_mask = 0;
1825 if (termios->c_iflag & IGNPAR)
1826 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1827 if (termios->c_iflag & IGNBRK) {
1828 port->ignore_status_mask |= ATMEL_US_RXBRK;
1830 * If we're ignoring parity and break indicators,
1831 * ignore overruns too (for real raw support).
1833 if (termios->c_iflag & IGNPAR)
1834 port->ignore_status_mask |= ATMEL_US_OVRE;
1836 /* TODO: Ignore all characters if CREAD is set.*/
1838 /* update the per-port timeout */
1839 uart_update_timeout(port, termios->c_cflag, baud);
1842 * save/disable interrupts. The tty layer will ensure that the
1843 * transmitter is empty if requested by the caller, so there's
1844 * no need to wait for it here.
1846 imr = UART_GET_IMR(port);
1847 UART_PUT_IDR(port, -1);
1849 /* disable receiver and transmitter */
1850 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1852 /* Resetting serial mode to RS232 (0x0) */
1853 mode &= ~ATMEL_US_USMODE;
1855 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1856 dev_dbg(port->dev, "Setting UART to RS485\n");
1857 if ((atmel_port->rs485.delay_rts_after_send) > 0)
1858 UART_PUT_TTGR(port,
1859 atmel_port->rs485.delay_rts_after_send);
1860 mode |= ATMEL_US_USMODE_RS485;
1861 } else {
1862 dev_dbg(port->dev, "Setting UART to RS232\n");
1865 /* set the parity, stop bits and data size */
1866 UART_PUT_MR(port, mode);
1868 /* set the baud rate */
1869 UART_PUT_BRGR(port, quot);
1870 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1871 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1873 /* restore interrupts */
1874 UART_PUT_IER(port, imr);
1876 /* CTS flow-control and modem-status interrupts */
1877 if (UART_ENABLE_MS(port, termios->c_cflag))
1878 port->ops->enable_ms(port);
1880 spin_unlock_irqrestore(&port->lock, flags);
1883 static void atmel_set_ldisc(struct uart_port *port, int new)
1885 if (new == N_PPS) {
1886 port->flags |= UPF_HARDPPS_CD;
1887 atmel_enable_ms(port);
1888 } else {
1889 port->flags &= ~UPF_HARDPPS_CD;
1894 * Return string describing the specified port
1896 static const char *atmel_type(struct uart_port *port)
1898 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1902 * Release the memory region(s) being used by 'port'.
1904 static void atmel_release_port(struct uart_port *port)
1906 struct platform_device *pdev = to_platform_device(port->dev);
1907 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1909 release_mem_region(port->mapbase, size);
1911 if (port->flags & UPF_IOREMAP) {
1912 iounmap(port->membase);
1913 port->membase = NULL;
1918 * Request the memory region(s) being used by 'port'.
1920 static int atmel_request_port(struct uart_port *port)
1922 struct platform_device *pdev = to_platform_device(port->dev);
1923 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1925 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1926 return -EBUSY;
1928 if (port->flags & UPF_IOREMAP) {
1929 port->membase = ioremap(port->mapbase, size);
1930 if (port->membase == NULL) {
1931 release_mem_region(port->mapbase, size);
1932 return -ENOMEM;
1936 return 0;
1940 * Configure/autoconfigure the port.
1942 static void atmel_config_port(struct uart_port *port, int flags)
1944 if (flags & UART_CONFIG_TYPE) {
1945 port->type = PORT_ATMEL;
1946 atmel_request_port(port);
1951 * Verify the new serial_struct (for TIOCSSERIAL).
1953 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1955 int ret = 0;
1956 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1957 ret = -EINVAL;
1958 if (port->irq != ser->irq)
1959 ret = -EINVAL;
1960 if (ser->io_type != SERIAL_IO_MEM)
1961 ret = -EINVAL;
1962 if (port->uartclk / 16 != ser->baud_base)
1963 ret = -EINVAL;
1964 if ((void *)port->mapbase != ser->iomem_base)
1965 ret = -EINVAL;
1966 if (port->iobase != ser->port)
1967 ret = -EINVAL;
1968 if (ser->hub6 != 0)
1969 ret = -EINVAL;
1970 return ret;
1973 #ifdef CONFIG_CONSOLE_POLL
1974 static int atmel_poll_get_char(struct uart_port *port)
1976 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1977 cpu_relax();
1979 return UART_GET_CHAR(port);
1982 static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1984 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1985 cpu_relax();
1987 UART_PUT_CHAR(port, ch);
1989 #endif
1991 static int
1992 atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1994 struct serial_rs485 rs485conf;
1996 switch (cmd) {
1997 case TIOCSRS485:
1998 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1999 sizeof(rs485conf)))
2000 return -EFAULT;
2002 atmel_config_rs485(port, &rs485conf);
2003 break;
2005 case TIOCGRS485:
2006 if (copy_to_user((struct serial_rs485 *) arg,
2007 &(to_atmel_uart_port(port)->rs485),
2008 sizeof(rs485conf)))
2009 return -EFAULT;
2010 break;
2012 default:
2013 return -ENOIOCTLCMD;
2015 return 0;
2020 static struct uart_ops atmel_pops = {
2021 .tx_empty = atmel_tx_empty,
2022 .set_mctrl = atmel_set_mctrl,
2023 .get_mctrl = atmel_get_mctrl,
2024 .stop_tx = atmel_stop_tx,
2025 .start_tx = atmel_start_tx,
2026 .stop_rx = atmel_stop_rx,
2027 .enable_ms = atmel_enable_ms,
2028 .break_ctl = atmel_break_ctl,
2029 .startup = atmel_startup,
2030 .shutdown = atmel_shutdown,
2031 .flush_buffer = atmel_flush_buffer,
2032 .set_termios = atmel_set_termios,
2033 .set_ldisc = atmel_set_ldisc,
2034 .type = atmel_type,
2035 .release_port = atmel_release_port,
2036 .request_port = atmel_request_port,
2037 .config_port = atmel_config_port,
2038 .verify_port = atmel_verify_port,
2039 .pm = atmel_serial_pm,
2040 .ioctl = atmel_ioctl,
2041 #ifdef CONFIG_CONSOLE_POLL
2042 .poll_get_char = atmel_poll_get_char,
2043 .poll_put_char = atmel_poll_put_char,
2044 #endif
2048 * Configure the port from the platform device resource info.
2050 static int atmel_init_port(struct atmel_uart_port *atmel_port,
2051 struct platform_device *pdev)
2053 int ret;
2054 struct uart_port *port = &atmel_port->uart;
2055 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2057 if (!atmel_init_property(atmel_port, pdev))
2058 atmel_set_ops(port);
2060 atmel_init_rs485(atmel_port, pdev);
2062 port->iotype = UPIO_MEM;
2063 port->flags = UPF_BOOT_AUTOCONF;
2064 port->ops = &atmel_pops;
2065 port->fifosize = 1;
2066 port->dev = &pdev->dev;
2067 port->mapbase = pdev->resource[0].start;
2068 port->irq = pdev->resource[1].start;
2070 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2071 (unsigned long)port);
2073 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2075 if (pdata && pdata->regs) {
2076 /* Already mapped by setup code */
2077 port->membase = pdata->regs;
2078 } else {
2079 port->flags |= UPF_IOREMAP;
2080 port->membase = NULL;
2083 /* for console, the clock could already be configured */
2084 if (!atmel_port->clk) {
2085 atmel_port->clk = clk_get(&pdev->dev, "usart");
2086 if (IS_ERR(atmel_port->clk)) {
2087 ret = PTR_ERR(atmel_port->clk);
2088 atmel_port->clk = NULL;
2089 return ret;
2091 ret = clk_prepare_enable(atmel_port->clk);
2092 if (ret) {
2093 clk_put(atmel_port->clk);
2094 atmel_port->clk = NULL;
2095 return ret;
2097 port->uartclk = clk_get_rate(atmel_port->clk);
2098 clk_disable_unprepare(atmel_port->clk);
2099 /* only enable clock when USART is in use */
2102 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2103 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2104 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
2105 else if (atmel_use_pdc_tx(port)) {
2106 port->fifosize = PDC_BUFFER_SIZE;
2107 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2108 } else {
2109 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2112 return 0;
2115 struct platform_device *atmel_default_console_device; /* the serial console device */
2117 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2118 static void atmel_console_putchar(struct uart_port *port, int ch)
2120 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2121 cpu_relax();
2122 UART_PUT_CHAR(port, ch);
2126 * Interrupts are disabled on entering
2128 static void atmel_console_write(struct console *co, const char *s, u_int count)
2130 struct uart_port *port = &atmel_ports[co->index].uart;
2131 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2132 unsigned int status, imr;
2133 unsigned int pdc_tx;
2136 * First, save IMR and then disable interrupts
2138 imr = UART_GET_IMR(port);
2139 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2141 /* Store PDC transmit status and disable it */
2142 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2143 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2145 uart_console_write(port, s, count, atmel_console_putchar);
2148 * Finally, wait for transmitter to become empty
2149 * and restore IMR
2151 do {
2152 status = UART_GET_CSR(port);
2153 } while (!(status & ATMEL_US_TXRDY));
2155 /* Restore PDC transmit status */
2156 if (pdc_tx)
2157 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2159 /* set interrupts back the way they were */
2160 UART_PUT_IER(port, imr);
2164 * If the port was already initialised (eg, by a boot loader),
2165 * try to determine the current setup.
2167 static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2168 int *parity, int *bits)
2170 unsigned int mr, quot;
2173 * If the baud rate generator isn't running, the port wasn't
2174 * initialized by the boot loader.
2176 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
2177 if (!quot)
2178 return;
2180 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2181 if (mr == ATMEL_US_CHRL_8)
2182 *bits = 8;
2183 else
2184 *bits = 7;
2186 mr = UART_GET_MR(port) & ATMEL_US_PAR;
2187 if (mr == ATMEL_US_PAR_EVEN)
2188 *parity = 'e';
2189 else if (mr == ATMEL_US_PAR_ODD)
2190 *parity = 'o';
2193 * The serial core only rounds down when matching this to a
2194 * supported baud rate. Make sure we don't end up slightly
2195 * lower than one of those, as it would make us fall through
2196 * to a much lower baud rate than we really want.
2198 *baud = port->uartclk / (16 * (quot - 1));
2201 static int __init atmel_console_setup(struct console *co, char *options)
2203 int ret;
2204 struct uart_port *port = &atmel_ports[co->index].uart;
2205 int baud = 115200;
2206 int bits = 8;
2207 int parity = 'n';
2208 int flow = 'n';
2210 if (port->membase == NULL) {
2211 /* Port not initialized yet - delay setup */
2212 return -ENODEV;
2215 ret = clk_prepare_enable(atmel_ports[co->index].clk);
2216 if (ret)
2217 return ret;
2219 UART_PUT_IDR(port, -1);
2220 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2221 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
2223 if (options)
2224 uart_parse_options(options, &baud, &parity, &bits, &flow);
2225 else
2226 atmel_console_get_options(port, &baud, &parity, &bits);
2228 return uart_set_options(port, co, baud, parity, bits, flow);
2231 static struct uart_driver atmel_uart;
2233 static struct console atmel_console = {
2234 .name = ATMEL_DEVICENAME,
2235 .write = atmel_console_write,
2236 .device = uart_console_device,
2237 .setup = atmel_console_setup,
2238 .flags = CON_PRINTBUFFER,
2239 .index = -1,
2240 .data = &atmel_uart,
2243 #define ATMEL_CONSOLE_DEVICE (&atmel_console)
2246 * Early console initialization (before VM subsystem initialized).
2248 static int __init atmel_console_init(void)
2250 int ret;
2251 if (atmel_default_console_device) {
2252 struct atmel_uart_data *pdata =
2253 dev_get_platdata(&atmel_default_console_device->dev);
2254 int id = pdata->num;
2255 struct atmel_uart_port *port = &atmel_ports[id];
2257 port->backup_imr = 0;
2258 port->uart.line = id;
2260 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
2261 ret = atmel_init_port(port, atmel_default_console_device);
2262 if (ret)
2263 return ret;
2264 register_console(&atmel_console);
2267 return 0;
2270 console_initcall(atmel_console_init);
2273 * Late console initialization.
2275 static int __init atmel_late_console_init(void)
2277 if (atmel_default_console_device
2278 && !(atmel_console.flags & CON_ENABLED))
2279 register_console(&atmel_console);
2281 return 0;
2284 core_initcall(atmel_late_console_init);
2286 static inline bool atmel_is_console_port(struct uart_port *port)
2288 return port->cons && port->cons->index == port->line;
2291 #else
2292 #define ATMEL_CONSOLE_DEVICE NULL
2294 static inline bool atmel_is_console_port(struct uart_port *port)
2296 return false;
2298 #endif
2300 static struct uart_driver atmel_uart = {
2301 .owner = THIS_MODULE,
2302 .driver_name = "atmel_serial",
2303 .dev_name = ATMEL_DEVICENAME,
2304 .major = SERIAL_ATMEL_MAJOR,
2305 .minor = MINOR_START,
2306 .nr = ATMEL_MAX_UART,
2307 .cons = ATMEL_CONSOLE_DEVICE,
2310 #ifdef CONFIG_PM
2311 static bool atmel_serial_clk_will_stop(void)
2313 #ifdef CONFIG_ARCH_AT91
2314 return at91_suspend_entering_slow_clock();
2315 #else
2316 return false;
2317 #endif
2320 static int atmel_serial_suspend(struct platform_device *pdev,
2321 pm_message_t state)
2323 struct uart_port *port = platform_get_drvdata(pdev);
2324 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2326 if (atmel_is_console_port(port) && console_suspend_enabled) {
2327 /* Drain the TX shifter */
2328 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2329 cpu_relax();
2332 /* we can not wake up if we're running on slow clock */
2333 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2334 if (atmel_serial_clk_will_stop())
2335 device_set_wakeup_enable(&pdev->dev, 0);
2337 uart_suspend_port(&atmel_uart, port);
2339 return 0;
2342 static int atmel_serial_resume(struct platform_device *pdev)
2344 struct uart_port *port = platform_get_drvdata(pdev);
2345 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2347 uart_resume_port(&atmel_uart, port);
2348 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2350 return 0;
2352 #else
2353 #define atmel_serial_suspend NULL
2354 #define atmel_serial_resume NULL
2355 #endif
2357 static int atmel_serial_probe(struct platform_device *pdev)
2359 struct atmel_uart_port *port;
2360 struct device_node *np = pdev->dev.of_node;
2361 struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2362 void *data;
2363 int ret = -ENODEV;
2365 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2367 if (np)
2368 ret = of_alias_get_id(np, "serial");
2369 else
2370 if (pdata)
2371 ret = pdata->num;
2373 if (ret < 0)
2374 /* port id not found in platform data nor device-tree aliases:
2375 * auto-enumerate it */
2376 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
2378 if (ret >= ATMEL_MAX_UART) {
2379 ret = -ENODEV;
2380 goto err;
2383 if (test_and_set_bit(ret, atmel_ports_in_use)) {
2384 /* port already in use */
2385 ret = -EBUSY;
2386 goto err;
2389 port = &atmel_ports[ret];
2390 port->backup_imr = 0;
2391 port->uart.line = ret;
2393 ret = atmel_init_port(port, pdev);
2394 if (ret)
2395 goto err;
2397 if (!atmel_use_pdc_rx(&port->uart)) {
2398 ret = -ENOMEM;
2399 data = kmalloc(sizeof(struct atmel_uart_char)
2400 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2401 if (!data)
2402 goto err_alloc_ring;
2403 port->rx_ring.buf = data;
2406 ret = uart_add_one_port(&atmel_uart, &port->uart);
2407 if (ret)
2408 goto err_add_port;
2410 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2411 if (atmel_is_console_port(&port->uart)
2412 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2414 * The serial core enabled the clock for us, so undo
2415 * the clk_prepare_enable() in atmel_console_setup()
2417 clk_disable_unprepare(port->clk);
2419 #endif
2421 device_init_wakeup(&pdev->dev, 1);
2422 platform_set_drvdata(pdev, port);
2424 if (port->rs485.flags & SER_RS485_ENABLED) {
2425 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2426 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2430 * Get port name of usart or uart
2432 atmel_get_ip_name(&port->uart);
2434 return 0;
2436 err_add_port:
2437 kfree(port->rx_ring.buf);
2438 port->rx_ring.buf = NULL;
2439 err_alloc_ring:
2440 if (!atmel_is_console_port(&port->uart)) {
2441 clk_put(port->clk);
2442 port->clk = NULL;
2444 err:
2445 return ret;
2448 static int atmel_serial_remove(struct platform_device *pdev)
2450 struct uart_port *port = platform_get_drvdata(pdev);
2451 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2452 int ret = 0;
2454 tasklet_kill(&atmel_port->tasklet);
2456 device_init_wakeup(&pdev->dev, 0);
2458 ret = uart_remove_one_port(&atmel_uart, port);
2460 kfree(atmel_port->rx_ring.buf);
2462 /* "port" is allocated statically, so we shouldn't free it */
2464 clear_bit(port->line, atmel_ports_in_use);
2466 clk_put(atmel_port->clk);
2468 return ret;
2471 static struct platform_driver atmel_serial_driver = {
2472 .probe = atmel_serial_probe,
2473 .remove = atmel_serial_remove,
2474 .suspend = atmel_serial_suspend,
2475 .resume = atmel_serial_resume,
2476 .driver = {
2477 .name = "atmel_usart",
2478 .owner = THIS_MODULE,
2479 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
2483 static int __init atmel_serial_init(void)
2485 int ret;
2487 ret = uart_register_driver(&atmel_uart);
2488 if (ret)
2489 return ret;
2491 ret = platform_driver_register(&atmel_serial_driver);
2492 if (ret)
2493 uart_unregister_driver(&atmel_uart);
2495 return ret;
2498 static void __exit atmel_serial_exit(void)
2500 platform_driver_unregister(&atmel_serial_driver);
2501 uart_unregister_driver(&atmel_uart);
2504 module_init(atmel_serial_init);
2505 module_exit(atmel_serial_exit);
2507 MODULE_AUTHOR("Rick Bronson");
2508 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2509 MODULE_LICENSE("GPL");
2510 MODULE_ALIAS("platform:atmel_usart");