drivers/usb/class/cdc-acm.c: clear dangling pointer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / tty / serial / atmel_serial.c
blobb989495c763eb15fd548167610555227b3c01d62
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/dma-mapping.h>
37 #include <linux/atmel_pdc.h>
38 #include <linux/atmel_serial.h>
39 #include <linux/uaccess.h>
41 #include <asm/io.h>
42 #include <asm/ioctls.h>
44 #include <asm/mach/serial_at91.h>
45 #include <mach/board.h>
47 #ifdef CONFIG_ARM
48 #include <mach/cpu.h>
49 #include <mach/gpio.h>
50 #endif
52 #define PDC_BUFFER_SIZE 512
53 /* Revisit: We should calculate this based on the actual port settings */
54 #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
56 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
57 #define SUPPORT_SYSRQ
58 #endif
60 #include <linux/serial_core.h>
62 static void atmel_start_rx(struct uart_port *port);
63 static void atmel_stop_rx(struct uart_port *port);
65 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
67 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
68 * should coexist with the 8250 driver, such as if we have an external 16C550
69 * UART. */
70 #define SERIAL_ATMEL_MAJOR 204
71 #define MINOR_START 154
72 #define ATMEL_DEVICENAME "ttyAT"
74 #else
76 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
77 * name, but it is legally reserved for the 8250 driver. */
78 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
79 #define MINOR_START 64
80 #define ATMEL_DEVICENAME "ttyS"
82 #endif
84 #define ATMEL_ISR_PASS_LIMIT 256
86 /* UART registers. CR is write-only, hence no GET macro */
87 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
88 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
89 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
90 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
91 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
92 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
93 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
94 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
95 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
96 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
97 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
98 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
99 #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
101 /* PDC registers */
102 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
103 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
105 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
106 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
107 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
108 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
109 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
111 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
112 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
113 #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
115 static int (*atmel_open_hook)(struct uart_port *);
116 static void (*atmel_close_hook)(struct uart_port *);
118 struct atmel_dma_buffer {
119 unsigned char *buf;
120 dma_addr_t dma_addr;
121 unsigned int dma_size;
122 unsigned int ofs;
125 struct atmel_uart_char {
126 u16 status;
127 u16 ch;
130 #define ATMEL_SERIAL_RINGSIZE 1024
133 * We wrap our port structure around the generic uart_port.
135 struct atmel_uart_port {
136 struct uart_port uart; /* uart */
137 struct clk *clk; /* uart clock */
138 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
139 u32 backup_imr; /* IMR saved during suspend */
140 int break_active; /* break being received */
142 short use_dma_rx; /* enable PDC receiver */
143 short pdc_rx_idx; /* current PDC RX buffer */
144 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
146 short use_dma_tx; /* enable PDC transmitter */
147 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
149 struct tasklet_struct tasklet;
150 unsigned int irq_status;
151 unsigned int irq_status_prev;
153 struct circ_buf rx_ring;
155 struct serial_rs485 rs485; /* rs485 settings */
156 unsigned int tx_done_mask;
159 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
161 #ifdef SUPPORT_SYSRQ
162 static struct console atmel_console;
163 #endif
165 static inline struct atmel_uart_port *
166 to_atmel_uart_port(struct uart_port *uart)
168 return container_of(uart, struct atmel_uart_port, uart);
171 #ifdef CONFIG_SERIAL_ATMEL_PDC
172 static bool atmel_use_dma_rx(struct uart_port *port)
174 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
176 return atmel_port->use_dma_rx;
179 static bool atmel_use_dma_tx(struct uart_port *port)
181 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
183 return atmel_port->use_dma_tx;
185 #else
186 static bool atmel_use_dma_rx(struct uart_port *port)
188 return false;
191 static bool atmel_use_dma_tx(struct uart_port *port)
193 return false;
195 #endif
197 /* Enable or disable the rs485 support */
198 void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
200 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
201 unsigned int mode;
202 unsigned long flags;
204 spin_lock_irqsave(&port->lock, flags);
206 /* Disable interrupts */
207 UART_PUT_IDR(port, atmel_port->tx_done_mask);
209 mode = UART_GET_MR(port);
211 /* Resetting serial mode to RS232 (0x0) */
212 mode &= ~ATMEL_US_USMODE;
214 atmel_port->rs485 = *rs485conf;
216 if (rs485conf->flags & SER_RS485_ENABLED) {
217 dev_dbg(port->dev, "Setting UART to RS485\n");
218 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
219 if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
220 UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
221 mode |= ATMEL_US_USMODE_RS485;
222 } else {
223 dev_dbg(port->dev, "Setting UART to RS232\n");
224 if (atmel_use_dma_tx(port))
225 atmel_port->tx_done_mask = ATMEL_US_ENDTX |
226 ATMEL_US_TXBUFE;
227 else
228 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
230 UART_PUT_MR(port, mode);
232 /* Enable interrupts */
233 UART_PUT_IER(port, atmel_port->tx_done_mask);
235 spin_unlock_irqrestore(&port->lock, flags);
240 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
242 static u_int atmel_tx_empty(struct uart_port *port)
244 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
248 * Set state of the modem control output lines
250 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
252 unsigned int control = 0;
253 unsigned int mode;
254 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
256 #ifdef CONFIG_ARCH_AT91RM9200
257 if (cpu_is_at91rm9200()) {
259 * AT91RM9200 Errata #39: RTS0 is not internally connected
260 * to PA21. We need to drive the pin manually.
262 if (port->mapbase == AT91RM9200_BASE_US0) {
263 if (mctrl & TIOCM_RTS)
264 at91_set_gpio_value(AT91_PIN_PA21, 0);
265 else
266 at91_set_gpio_value(AT91_PIN_PA21, 1);
269 #endif
271 if (mctrl & TIOCM_RTS)
272 control |= ATMEL_US_RTSEN;
273 else
274 control |= ATMEL_US_RTSDIS;
276 if (mctrl & TIOCM_DTR)
277 control |= ATMEL_US_DTREN;
278 else
279 control |= ATMEL_US_DTRDIS;
281 UART_PUT_CR(port, control);
283 /* Local loopback mode? */
284 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
285 if (mctrl & TIOCM_LOOP)
286 mode |= ATMEL_US_CHMODE_LOC_LOOP;
287 else
288 mode |= ATMEL_US_CHMODE_NORMAL;
290 /* Resetting serial mode to RS232 (0x0) */
291 mode &= ~ATMEL_US_USMODE;
293 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
294 dev_dbg(port->dev, "Setting UART to RS485\n");
295 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
296 UART_PUT_TTGR(port,
297 atmel_port->rs485.delay_rts_after_send);
298 mode |= ATMEL_US_USMODE_RS485;
299 } else {
300 dev_dbg(port->dev, "Setting UART to RS232\n");
302 UART_PUT_MR(port, mode);
306 * Get state of the modem control input lines
308 static u_int atmel_get_mctrl(struct uart_port *port)
310 unsigned int status, ret = 0;
312 status = UART_GET_CSR(port);
315 * The control signals are active low.
317 if (!(status & ATMEL_US_DCD))
318 ret |= TIOCM_CD;
319 if (!(status & ATMEL_US_CTS))
320 ret |= TIOCM_CTS;
321 if (!(status & ATMEL_US_DSR))
322 ret |= TIOCM_DSR;
323 if (!(status & ATMEL_US_RI))
324 ret |= TIOCM_RI;
326 return ret;
330 * Stop transmitting.
332 static void atmel_stop_tx(struct uart_port *port)
334 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
336 if (atmel_use_dma_tx(port)) {
337 /* disable PDC transmit */
338 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
340 /* Disable interrupts */
341 UART_PUT_IDR(port, atmel_port->tx_done_mask);
343 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
344 atmel_start_rx(port);
348 * Start transmitting.
350 static void atmel_start_tx(struct uart_port *port)
352 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
354 if (atmel_use_dma_tx(port)) {
355 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
356 /* The transmitter is already running. Yes, we
357 really need this.*/
358 return;
360 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
361 atmel_stop_rx(port);
363 /* re-enable PDC transmit */
364 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
366 /* Enable interrupts */
367 UART_PUT_IER(port, atmel_port->tx_done_mask);
371 * start receiving - port is in process of being opened.
373 static void atmel_start_rx(struct uart_port *port)
375 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
377 if (atmel_use_dma_rx(port)) {
378 /* enable PDC controller */
379 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
380 port->read_status_mask);
381 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
382 } else {
383 UART_PUT_IER(port, ATMEL_US_RXRDY);
388 * Stop receiving - port is in process of being closed.
390 static void atmel_stop_rx(struct uart_port *port)
392 if (atmel_use_dma_rx(port)) {
393 /* disable PDC receive */
394 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
395 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
396 port->read_status_mask);
397 } else {
398 UART_PUT_IDR(port, ATMEL_US_RXRDY);
403 * Enable modem status interrupts
405 static void atmel_enable_ms(struct uart_port *port)
407 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
408 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
412 * Control the transmission of a break signal
414 static void atmel_break_ctl(struct uart_port *port, int break_state)
416 if (break_state != 0)
417 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
418 else
419 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
423 * Stores the incoming character in the ring buffer
425 static void
426 atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
427 unsigned int ch)
429 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
430 struct circ_buf *ring = &atmel_port->rx_ring;
431 struct atmel_uart_char *c;
433 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
434 /* Buffer overflow, ignore char */
435 return;
437 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
438 c->status = status;
439 c->ch = ch;
441 /* Make sure the character is stored before we update head. */
442 smp_wmb();
444 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
448 * Deal with parity, framing and overrun errors.
450 static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
452 /* clear error */
453 UART_PUT_CR(port, ATMEL_US_RSTSTA);
455 if (status & ATMEL_US_RXBRK) {
456 /* ignore side-effect */
457 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
458 port->icount.brk++;
460 if (status & ATMEL_US_PARE)
461 port->icount.parity++;
462 if (status & ATMEL_US_FRAME)
463 port->icount.frame++;
464 if (status & ATMEL_US_OVRE)
465 port->icount.overrun++;
469 * Characters received (called from interrupt handler)
471 static void atmel_rx_chars(struct uart_port *port)
473 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
474 unsigned int status, ch;
476 status = UART_GET_CSR(port);
477 while (status & ATMEL_US_RXRDY) {
478 ch = UART_GET_CHAR(port);
481 * note that the error handling code is
482 * out of the main execution path
484 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
485 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
486 || atmel_port->break_active)) {
488 /* clear error */
489 UART_PUT_CR(port, ATMEL_US_RSTSTA);
491 if (status & ATMEL_US_RXBRK
492 && !atmel_port->break_active) {
493 atmel_port->break_active = 1;
494 UART_PUT_IER(port, ATMEL_US_RXBRK);
495 } else {
497 * This is either the end-of-break
498 * condition or we've received at
499 * least one character without RXBRK
500 * being set. In both cases, the next
501 * RXBRK will indicate start-of-break.
503 UART_PUT_IDR(port, ATMEL_US_RXBRK);
504 status &= ~ATMEL_US_RXBRK;
505 atmel_port->break_active = 0;
509 atmel_buffer_rx_char(port, status, ch);
510 status = UART_GET_CSR(port);
513 tasklet_schedule(&atmel_port->tasklet);
517 * Transmit characters (called from tasklet with TXRDY interrupt
518 * disabled)
520 static void atmel_tx_chars(struct uart_port *port)
522 struct circ_buf *xmit = &port->state->xmit;
523 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
525 if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
526 UART_PUT_CHAR(port, port->x_char);
527 port->icount.tx++;
528 port->x_char = 0;
530 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
531 return;
533 while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
534 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
535 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
536 port->icount.tx++;
537 if (uart_circ_empty(xmit))
538 break;
541 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
542 uart_write_wakeup(port);
544 if (!uart_circ_empty(xmit))
545 /* Enable interrupts */
546 UART_PUT_IER(port, atmel_port->tx_done_mask);
550 * receive interrupt handler.
552 static void
553 atmel_handle_receive(struct uart_port *port, unsigned int pending)
555 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
557 if (atmel_use_dma_rx(port)) {
559 * PDC receive. Just schedule the tasklet and let it
560 * figure out the details.
562 * TODO: We're not handling error flags correctly at
563 * the moment.
565 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
566 UART_PUT_IDR(port, (ATMEL_US_ENDRX
567 | ATMEL_US_TIMEOUT));
568 tasklet_schedule(&atmel_port->tasklet);
571 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
572 ATMEL_US_FRAME | ATMEL_US_PARE))
573 atmel_pdc_rxerr(port, pending);
576 /* Interrupt receive */
577 if (pending & ATMEL_US_RXRDY)
578 atmel_rx_chars(port);
579 else if (pending & ATMEL_US_RXBRK) {
581 * End of break detected. If it came along with a
582 * character, atmel_rx_chars will handle it.
584 UART_PUT_CR(port, ATMEL_US_RSTSTA);
585 UART_PUT_IDR(port, ATMEL_US_RXBRK);
586 atmel_port->break_active = 0;
591 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
593 static void
594 atmel_handle_transmit(struct uart_port *port, unsigned int pending)
596 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
598 if (pending & atmel_port->tx_done_mask) {
599 /* Either PDC or interrupt transmission */
600 UART_PUT_IDR(port, atmel_port->tx_done_mask);
601 tasklet_schedule(&atmel_port->tasklet);
606 * status flags interrupt handler.
608 static void
609 atmel_handle_status(struct uart_port *port, unsigned int pending,
610 unsigned int status)
612 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
614 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
615 | ATMEL_US_CTSIC)) {
616 atmel_port->irq_status = status;
617 tasklet_schedule(&atmel_port->tasklet);
622 * Interrupt handler
624 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
626 struct uart_port *port = dev_id;
627 unsigned int status, pending, pass_counter = 0;
629 do {
630 status = UART_GET_CSR(port);
631 pending = status & UART_GET_IMR(port);
632 if (!pending)
633 break;
635 atmel_handle_receive(port, pending);
636 atmel_handle_status(port, pending, status);
637 atmel_handle_transmit(port, pending);
638 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
640 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
644 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
646 static void atmel_tx_dma(struct uart_port *port)
648 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
649 struct circ_buf *xmit = &port->state->xmit;
650 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
651 int count;
653 /* nothing left to transmit? */
654 if (UART_GET_TCR(port))
655 return;
657 xmit->tail += pdc->ofs;
658 xmit->tail &= UART_XMIT_SIZE - 1;
660 port->icount.tx += pdc->ofs;
661 pdc->ofs = 0;
663 /* more to transmit - setup next transfer */
665 /* disable PDC transmit */
666 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
668 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
669 dma_sync_single_for_device(port->dev,
670 pdc->dma_addr,
671 pdc->dma_size,
672 DMA_TO_DEVICE);
674 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
675 pdc->ofs = count;
677 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
678 UART_PUT_TCR(port, count);
679 /* re-enable PDC transmit */
680 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
681 /* Enable interrupts */
682 UART_PUT_IER(port, atmel_port->tx_done_mask);
683 } else {
684 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
685 /* DMA done, stop TX, start RX for RS485 */
686 atmel_start_rx(port);
690 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
691 uart_write_wakeup(port);
694 static void atmel_rx_from_ring(struct uart_port *port)
696 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
697 struct circ_buf *ring = &atmel_port->rx_ring;
698 unsigned int flg;
699 unsigned int status;
701 while (ring->head != ring->tail) {
702 struct atmel_uart_char c;
704 /* Make sure c is loaded after head. */
705 smp_rmb();
707 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
709 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
711 port->icount.rx++;
712 status = c.status;
713 flg = TTY_NORMAL;
716 * note that the error handling code is
717 * out of the main execution path
719 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
720 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
721 if (status & ATMEL_US_RXBRK) {
722 /* ignore side-effect */
723 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
725 port->icount.brk++;
726 if (uart_handle_break(port))
727 continue;
729 if (status & ATMEL_US_PARE)
730 port->icount.parity++;
731 if (status & ATMEL_US_FRAME)
732 port->icount.frame++;
733 if (status & ATMEL_US_OVRE)
734 port->icount.overrun++;
736 status &= port->read_status_mask;
738 if (status & ATMEL_US_RXBRK)
739 flg = TTY_BREAK;
740 else if (status & ATMEL_US_PARE)
741 flg = TTY_PARITY;
742 else if (status & ATMEL_US_FRAME)
743 flg = TTY_FRAME;
747 if (uart_handle_sysrq_char(port, c.ch))
748 continue;
750 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
754 * Drop the lock here since it might end up calling
755 * uart_start(), which takes the lock.
757 spin_unlock(&port->lock);
758 tty_flip_buffer_push(port->state->port.tty);
759 spin_lock(&port->lock);
762 static void atmel_rx_from_dma(struct uart_port *port)
764 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
765 struct tty_struct *tty = port->state->port.tty;
766 struct atmel_dma_buffer *pdc;
767 int rx_idx = atmel_port->pdc_rx_idx;
768 unsigned int head;
769 unsigned int tail;
770 unsigned int count;
772 do {
773 /* Reset the UART timeout early so that we don't miss one */
774 UART_PUT_CR(port, ATMEL_US_STTTO);
776 pdc = &atmel_port->pdc_rx[rx_idx];
777 head = UART_GET_RPR(port) - pdc->dma_addr;
778 tail = pdc->ofs;
780 /* If the PDC has switched buffers, RPR won't contain
781 * any address within the current buffer. Since head
782 * is unsigned, we just need a one-way comparison to
783 * find out.
785 * In this case, we just need to consume the entire
786 * buffer and resubmit it for DMA. This will clear the
787 * ENDRX bit as well, so that we can safely re-enable
788 * all interrupts below.
790 head = min(head, pdc->dma_size);
792 if (likely(head != tail)) {
793 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
794 pdc->dma_size, DMA_FROM_DEVICE);
797 * head will only wrap around when we recycle
798 * the DMA buffer, and when that happens, we
799 * explicitly set tail to 0. So head will
800 * always be greater than tail.
802 count = head - tail;
804 tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
806 dma_sync_single_for_device(port->dev, pdc->dma_addr,
807 pdc->dma_size, DMA_FROM_DEVICE);
809 port->icount.rx += count;
810 pdc->ofs = head;
814 * If the current buffer is full, we need to check if
815 * the next one contains any additional data.
817 if (head >= pdc->dma_size) {
818 pdc->ofs = 0;
819 UART_PUT_RNPR(port, pdc->dma_addr);
820 UART_PUT_RNCR(port, pdc->dma_size);
822 rx_idx = !rx_idx;
823 atmel_port->pdc_rx_idx = rx_idx;
825 } while (head >= pdc->dma_size);
828 * Drop the lock here since it might end up calling
829 * uart_start(), which takes the lock.
831 spin_unlock(&port->lock);
832 tty_flip_buffer_push(tty);
833 spin_lock(&port->lock);
835 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
839 * tasklet handling tty stuff outside the interrupt handler.
841 static void atmel_tasklet_func(unsigned long data)
843 struct uart_port *port = (struct uart_port *)data;
844 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
845 unsigned int status;
846 unsigned int status_change;
848 /* The interrupt handler does not take the lock */
849 spin_lock(&port->lock);
851 if (atmel_use_dma_tx(port))
852 atmel_tx_dma(port);
853 else
854 atmel_tx_chars(port);
856 status = atmel_port->irq_status;
857 status_change = status ^ atmel_port->irq_status_prev;
859 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
860 | ATMEL_US_DCD | ATMEL_US_CTS)) {
861 /* TODO: All reads to CSR will clear these interrupts! */
862 if (status_change & ATMEL_US_RI)
863 port->icount.rng++;
864 if (status_change & ATMEL_US_DSR)
865 port->icount.dsr++;
866 if (status_change & ATMEL_US_DCD)
867 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
868 if (status_change & ATMEL_US_CTS)
869 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
871 wake_up_interruptible(&port->state->port.delta_msr_wait);
873 atmel_port->irq_status_prev = status;
876 if (atmel_use_dma_rx(port))
877 atmel_rx_from_dma(port);
878 else
879 atmel_rx_from_ring(port);
881 spin_unlock(&port->lock);
885 * Perform initialization and enable port for reception
887 static int atmel_startup(struct uart_port *port)
889 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
890 struct tty_struct *tty = port->state->port.tty;
891 int retval;
894 * Ensure that no interrupts are enabled otherwise when
895 * request_irq() is called we could get stuck trying to
896 * handle an unexpected interrupt
898 UART_PUT_IDR(port, -1);
901 * Allocate the IRQ
903 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
904 tty ? tty->name : "atmel_serial", port);
905 if (retval) {
906 printk("atmel_serial: atmel_startup - Can't get irq\n");
907 return retval;
911 * Initialize DMA (if necessary)
913 if (atmel_use_dma_rx(port)) {
914 int i;
916 for (i = 0; i < 2; i++) {
917 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
919 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
920 if (pdc->buf == NULL) {
921 if (i != 0) {
922 dma_unmap_single(port->dev,
923 atmel_port->pdc_rx[0].dma_addr,
924 PDC_BUFFER_SIZE,
925 DMA_FROM_DEVICE);
926 kfree(atmel_port->pdc_rx[0].buf);
928 free_irq(port->irq, port);
929 return -ENOMEM;
931 pdc->dma_addr = dma_map_single(port->dev,
932 pdc->buf,
933 PDC_BUFFER_SIZE,
934 DMA_FROM_DEVICE);
935 pdc->dma_size = PDC_BUFFER_SIZE;
936 pdc->ofs = 0;
939 atmel_port->pdc_rx_idx = 0;
941 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
942 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
944 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
945 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
947 if (atmel_use_dma_tx(port)) {
948 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
949 struct circ_buf *xmit = &port->state->xmit;
951 pdc->buf = xmit->buf;
952 pdc->dma_addr = dma_map_single(port->dev,
953 pdc->buf,
954 UART_XMIT_SIZE,
955 DMA_TO_DEVICE);
956 pdc->dma_size = UART_XMIT_SIZE;
957 pdc->ofs = 0;
961 * If there is a specific "open" function (to register
962 * control line interrupts)
964 if (atmel_open_hook) {
965 retval = atmel_open_hook(port);
966 if (retval) {
967 free_irq(port->irq, port);
968 return retval;
972 /* Save current CSR for comparison in atmel_tasklet_func() */
973 atmel_port->irq_status_prev = UART_GET_CSR(port);
974 atmel_port->irq_status = atmel_port->irq_status_prev;
977 * Finally, enable the serial port
979 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
980 /* enable xmit & rcvr */
981 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
983 if (atmel_use_dma_rx(port)) {
984 /* set UART timeout */
985 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
986 UART_PUT_CR(port, ATMEL_US_STTTO);
988 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
989 /* enable PDC controller */
990 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
991 } else {
992 /* enable receive only */
993 UART_PUT_IER(port, ATMEL_US_RXRDY);
996 return 0;
1000 * Disable the port
1002 static void atmel_shutdown(struct uart_port *port)
1004 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1006 * Ensure everything is stopped.
1008 atmel_stop_rx(port);
1009 atmel_stop_tx(port);
1012 * Shut-down the DMA.
1014 if (atmel_use_dma_rx(port)) {
1015 int i;
1017 for (i = 0; i < 2; i++) {
1018 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1020 dma_unmap_single(port->dev,
1021 pdc->dma_addr,
1022 pdc->dma_size,
1023 DMA_FROM_DEVICE);
1024 kfree(pdc->buf);
1027 if (atmel_use_dma_tx(port)) {
1028 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1030 dma_unmap_single(port->dev,
1031 pdc->dma_addr,
1032 pdc->dma_size,
1033 DMA_TO_DEVICE);
1037 * Disable all interrupts, port and break condition.
1039 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1040 UART_PUT_IDR(port, -1);
1043 * Free the interrupt
1045 free_irq(port->irq, port);
1048 * If there is a specific "close" function (to unregister
1049 * control line interrupts)
1051 if (atmel_close_hook)
1052 atmel_close_hook(port);
1056 * Flush any TX data submitted for DMA. Called when the TX circular
1057 * buffer is reset.
1059 static void atmel_flush_buffer(struct uart_port *port)
1061 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1063 if (atmel_use_dma_tx(port)) {
1064 UART_PUT_TCR(port, 0);
1065 atmel_port->pdc_tx.ofs = 0;
1070 * Power / Clock management.
1072 static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1073 unsigned int oldstate)
1075 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1077 switch (state) {
1078 case 0:
1080 * Enable the peripheral clock for this serial port.
1081 * This is called on uart_open() or a resume event.
1083 clk_enable(atmel_port->clk);
1085 /* re-enable interrupts if we disabled some on suspend */
1086 UART_PUT_IER(port, atmel_port->backup_imr);
1087 break;
1088 case 3:
1089 /* Back up the interrupt mask and disable all interrupts */
1090 atmel_port->backup_imr = UART_GET_IMR(port);
1091 UART_PUT_IDR(port, -1);
1094 * Disable the peripheral clock for this serial port.
1095 * This is called on uart_close() or a suspend event.
1097 clk_disable(atmel_port->clk);
1098 break;
1099 default:
1100 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1105 * Change the port parameters
1107 static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1108 struct ktermios *old)
1110 unsigned long flags;
1111 unsigned int mode, imr, quot, baud;
1112 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1114 /* Get current mode register */
1115 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1116 | ATMEL_US_NBSTOP | ATMEL_US_PAR
1117 | ATMEL_US_USMODE);
1119 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1120 quot = uart_get_divisor(port, baud);
1122 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
1123 quot /= 8;
1124 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1127 /* byte size */
1128 switch (termios->c_cflag & CSIZE) {
1129 case CS5:
1130 mode |= ATMEL_US_CHRL_5;
1131 break;
1132 case CS6:
1133 mode |= ATMEL_US_CHRL_6;
1134 break;
1135 case CS7:
1136 mode |= ATMEL_US_CHRL_7;
1137 break;
1138 default:
1139 mode |= ATMEL_US_CHRL_8;
1140 break;
1143 /* stop bits */
1144 if (termios->c_cflag & CSTOPB)
1145 mode |= ATMEL_US_NBSTOP_2;
1147 /* parity */
1148 if (termios->c_cflag & PARENB) {
1149 /* Mark or Space parity */
1150 if (termios->c_cflag & CMSPAR) {
1151 if (termios->c_cflag & PARODD)
1152 mode |= ATMEL_US_PAR_MARK;
1153 else
1154 mode |= ATMEL_US_PAR_SPACE;
1155 } else if (termios->c_cflag & PARODD)
1156 mode |= ATMEL_US_PAR_ODD;
1157 else
1158 mode |= ATMEL_US_PAR_EVEN;
1159 } else
1160 mode |= ATMEL_US_PAR_NONE;
1162 /* hardware handshake (RTS/CTS) */
1163 if (termios->c_cflag & CRTSCTS)
1164 mode |= ATMEL_US_USMODE_HWHS;
1165 else
1166 mode |= ATMEL_US_USMODE_NORMAL;
1168 spin_lock_irqsave(&port->lock, flags);
1170 port->read_status_mask = ATMEL_US_OVRE;
1171 if (termios->c_iflag & INPCK)
1172 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1173 if (termios->c_iflag & (BRKINT | PARMRK))
1174 port->read_status_mask |= ATMEL_US_RXBRK;
1176 if (atmel_use_dma_rx(port))
1177 /* need to enable error interrupts */
1178 UART_PUT_IER(port, port->read_status_mask);
1181 * Characters to ignore
1183 port->ignore_status_mask = 0;
1184 if (termios->c_iflag & IGNPAR)
1185 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1186 if (termios->c_iflag & IGNBRK) {
1187 port->ignore_status_mask |= ATMEL_US_RXBRK;
1189 * If we're ignoring parity and break indicators,
1190 * ignore overruns too (for real raw support).
1192 if (termios->c_iflag & IGNPAR)
1193 port->ignore_status_mask |= ATMEL_US_OVRE;
1195 /* TODO: Ignore all characters if CREAD is set.*/
1197 /* update the per-port timeout */
1198 uart_update_timeout(port, termios->c_cflag, baud);
1201 * save/disable interrupts. The tty layer will ensure that the
1202 * transmitter is empty if requested by the caller, so there's
1203 * no need to wait for it here.
1205 imr = UART_GET_IMR(port);
1206 UART_PUT_IDR(port, -1);
1208 /* disable receiver and transmitter */
1209 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1211 /* Resetting serial mode to RS232 (0x0) */
1212 mode &= ~ATMEL_US_USMODE;
1214 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1215 dev_dbg(port->dev, "Setting UART to RS485\n");
1216 if (atmel_port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
1217 UART_PUT_TTGR(port,
1218 atmel_port->rs485.delay_rts_after_send);
1219 mode |= ATMEL_US_USMODE_RS485;
1220 } else {
1221 dev_dbg(port->dev, "Setting UART to RS232\n");
1224 /* set the parity, stop bits and data size */
1225 UART_PUT_MR(port, mode);
1227 /* set the baud rate */
1228 UART_PUT_BRGR(port, quot);
1229 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1230 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1232 /* restore interrupts */
1233 UART_PUT_IER(port, imr);
1235 /* CTS flow-control and modem-status interrupts */
1236 if (UART_ENABLE_MS(port, termios->c_cflag))
1237 port->ops->enable_ms(port);
1239 spin_unlock_irqrestore(&port->lock, flags);
1242 static void atmel_set_ldisc(struct uart_port *port, int new)
1244 int line = port->line;
1246 if (line >= port->state->port.tty->driver->num)
1247 return;
1249 if (port->state->port.tty->ldisc->ops->num == N_PPS) {
1250 port->flags |= UPF_HARDPPS_CD;
1251 atmel_enable_ms(port);
1252 } else {
1253 port->flags &= ~UPF_HARDPPS_CD;
1258 * Return string describing the specified port
1260 static const char *atmel_type(struct uart_port *port)
1262 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1266 * Release the memory region(s) being used by 'port'.
1268 static void atmel_release_port(struct uart_port *port)
1270 struct platform_device *pdev = to_platform_device(port->dev);
1271 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1273 release_mem_region(port->mapbase, size);
1275 if (port->flags & UPF_IOREMAP) {
1276 iounmap(port->membase);
1277 port->membase = NULL;
1282 * Request the memory region(s) being used by 'port'.
1284 static int atmel_request_port(struct uart_port *port)
1286 struct platform_device *pdev = to_platform_device(port->dev);
1287 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1289 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1290 return -EBUSY;
1292 if (port->flags & UPF_IOREMAP) {
1293 port->membase = ioremap(port->mapbase, size);
1294 if (port->membase == NULL) {
1295 release_mem_region(port->mapbase, size);
1296 return -ENOMEM;
1300 return 0;
1304 * Configure/autoconfigure the port.
1306 static void atmel_config_port(struct uart_port *port, int flags)
1308 if (flags & UART_CONFIG_TYPE) {
1309 port->type = PORT_ATMEL;
1310 atmel_request_port(port);
1315 * Verify the new serial_struct (for TIOCSSERIAL).
1317 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1319 int ret = 0;
1320 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1321 ret = -EINVAL;
1322 if (port->irq != ser->irq)
1323 ret = -EINVAL;
1324 if (ser->io_type != SERIAL_IO_MEM)
1325 ret = -EINVAL;
1326 if (port->uartclk / 16 != ser->baud_base)
1327 ret = -EINVAL;
1328 if ((void *)port->mapbase != ser->iomem_base)
1329 ret = -EINVAL;
1330 if (port->iobase != ser->port)
1331 ret = -EINVAL;
1332 if (ser->hub6 != 0)
1333 ret = -EINVAL;
1334 return ret;
1337 #ifdef CONFIG_CONSOLE_POLL
1338 static int atmel_poll_get_char(struct uart_port *port)
1340 while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1341 cpu_relax();
1343 return UART_GET_CHAR(port);
1346 static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1348 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1349 cpu_relax();
1351 UART_PUT_CHAR(port, ch);
1353 #endif
1355 static int
1356 atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1358 struct serial_rs485 rs485conf;
1360 switch (cmd) {
1361 case TIOCSRS485:
1362 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1363 sizeof(rs485conf)))
1364 return -EFAULT;
1366 atmel_config_rs485(port, &rs485conf);
1367 break;
1369 case TIOCGRS485:
1370 if (copy_to_user((struct serial_rs485 *) arg,
1371 &(to_atmel_uart_port(port)->rs485),
1372 sizeof(rs485conf)))
1373 return -EFAULT;
1374 break;
1376 default:
1377 return -ENOIOCTLCMD;
1379 return 0;
1384 static struct uart_ops atmel_pops = {
1385 .tx_empty = atmel_tx_empty,
1386 .set_mctrl = atmel_set_mctrl,
1387 .get_mctrl = atmel_get_mctrl,
1388 .stop_tx = atmel_stop_tx,
1389 .start_tx = atmel_start_tx,
1390 .stop_rx = atmel_stop_rx,
1391 .enable_ms = atmel_enable_ms,
1392 .break_ctl = atmel_break_ctl,
1393 .startup = atmel_startup,
1394 .shutdown = atmel_shutdown,
1395 .flush_buffer = atmel_flush_buffer,
1396 .set_termios = atmel_set_termios,
1397 .set_ldisc = atmel_set_ldisc,
1398 .type = atmel_type,
1399 .release_port = atmel_release_port,
1400 .request_port = atmel_request_port,
1401 .config_port = atmel_config_port,
1402 .verify_port = atmel_verify_port,
1403 .pm = atmel_serial_pm,
1404 .ioctl = atmel_ioctl,
1405 #ifdef CONFIG_CONSOLE_POLL
1406 .poll_get_char = atmel_poll_get_char,
1407 .poll_put_char = atmel_poll_put_char,
1408 #endif
1412 * Configure the port from the platform device resource info.
1414 static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1415 struct platform_device *pdev)
1417 struct uart_port *port = &atmel_port->uart;
1418 struct atmel_uart_data *data = pdev->dev.platform_data;
1420 port->iotype = UPIO_MEM;
1421 port->flags = UPF_BOOT_AUTOCONF;
1422 port->ops = &atmel_pops;
1423 port->fifosize = 1;
1424 port->line = data->num;
1425 port->dev = &pdev->dev;
1426 port->mapbase = pdev->resource[0].start;
1427 port->irq = pdev->resource[1].start;
1429 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1430 (unsigned long)port);
1432 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1434 if (data->regs)
1435 /* Already mapped by setup code */
1436 port->membase = data->regs;
1437 else {
1438 port->flags |= UPF_IOREMAP;
1439 port->membase = NULL;
1442 /* for console, the clock could already be configured */
1443 if (!atmel_port->clk) {
1444 atmel_port->clk = clk_get(&pdev->dev, "usart");
1445 clk_enable(atmel_port->clk);
1446 port->uartclk = clk_get_rate(atmel_port->clk);
1447 clk_disable(atmel_port->clk);
1448 /* only enable clock when USART is in use */
1451 atmel_port->use_dma_rx = data->use_dma_rx;
1452 atmel_port->use_dma_tx = data->use_dma_tx;
1453 atmel_port->rs485 = data->rs485;
1454 /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
1455 if (atmel_port->rs485.flags & SER_RS485_ENABLED)
1456 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
1457 else if (atmel_use_dma_tx(port)) {
1458 port->fifosize = PDC_BUFFER_SIZE;
1459 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
1460 } else {
1461 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
1466 * Register board-specific modem-control line handlers.
1468 void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
1470 if (fns->enable_ms)
1471 atmel_pops.enable_ms = fns->enable_ms;
1472 if (fns->get_mctrl)
1473 atmel_pops.get_mctrl = fns->get_mctrl;
1474 if (fns->set_mctrl)
1475 atmel_pops.set_mctrl = fns->set_mctrl;
1476 atmel_open_hook = fns->open;
1477 atmel_close_hook = fns->close;
1478 atmel_pops.pm = fns->pm;
1479 atmel_pops.set_wake = fns->set_wake;
1482 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1483 static void atmel_console_putchar(struct uart_port *port, int ch)
1485 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1486 cpu_relax();
1487 UART_PUT_CHAR(port, ch);
1491 * Interrupts are disabled on entering
1493 static void atmel_console_write(struct console *co, const char *s, u_int count)
1495 struct uart_port *port = &atmel_ports[co->index].uart;
1496 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1497 unsigned int status, imr;
1498 unsigned int pdc_tx;
1501 * First, save IMR and then disable interrupts
1503 imr = UART_GET_IMR(port);
1504 UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
1506 /* Store PDC transmit status and disable it */
1507 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1508 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1510 uart_console_write(port, s, count, atmel_console_putchar);
1513 * Finally, wait for transmitter to become empty
1514 * and restore IMR
1516 do {
1517 status = UART_GET_CSR(port);
1518 } while (!(status & ATMEL_US_TXRDY));
1520 /* Restore PDC transmit status */
1521 if (pdc_tx)
1522 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1524 /* set interrupts back the way they were */
1525 UART_PUT_IER(port, imr);
1529 * If the port was already initialised (eg, by a boot loader),
1530 * try to determine the current setup.
1532 static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1533 int *parity, int *bits)
1535 unsigned int mr, quot;
1538 * If the baud rate generator isn't running, the port wasn't
1539 * initialized by the boot loader.
1541 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
1542 if (!quot)
1543 return;
1545 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1546 if (mr == ATMEL_US_CHRL_8)
1547 *bits = 8;
1548 else
1549 *bits = 7;
1551 mr = UART_GET_MR(port) & ATMEL_US_PAR;
1552 if (mr == ATMEL_US_PAR_EVEN)
1553 *parity = 'e';
1554 else if (mr == ATMEL_US_PAR_ODD)
1555 *parity = 'o';
1558 * The serial core only rounds down when matching this to a
1559 * supported baud rate. Make sure we don't end up slightly
1560 * lower than one of those, as it would make us fall through
1561 * to a much lower baud rate than we really want.
1563 *baud = port->uartclk / (16 * (quot - 1));
1566 static int __init atmel_console_setup(struct console *co, char *options)
1568 struct uart_port *port = &atmel_ports[co->index].uart;
1569 int baud = 115200;
1570 int bits = 8;
1571 int parity = 'n';
1572 int flow = 'n';
1574 if (port->membase == NULL) {
1575 /* Port not initialized yet - delay setup */
1576 return -ENODEV;
1579 clk_enable(atmel_ports[co->index].clk);
1581 UART_PUT_IDR(port, -1);
1582 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1583 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1585 if (options)
1586 uart_parse_options(options, &baud, &parity, &bits, &flow);
1587 else
1588 atmel_console_get_options(port, &baud, &parity, &bits);
1590 return uart_set_options(port, co, baud, parity, bits, flow);
1593 static struct uart_driver atmel_uart;
1595 static struct console atmel_console = {
1596 .name = ATMEL_DEVICENAME,
1597 .write = atmel_console_write,
1598 .device = uart_console_device,
1599 .setup = atmel_console_setup,
1600 .flags = CON_PRINTBUFFER,
1601 .index = -1,
1602 .data = &atmel_uart,
1605 #define ATMEL_CONSOLE_DEVICE (&atmel_console)
1608 * Early console initialization (before VM subsystem initialized).
1610 static int __init atmel_console_init(void)
1612 if (atmel_default_console_device) {
1613 add_preferred_console(ATMEL_DEVICENAME,
1614 atmel_default_console_device->id, NULL);
1615 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1616 atmel_default_console_device);
1617 register_console(&atmel_console);
1620 return 0;
1623 console_initcall(atmel_console_init);
1626 * Late console initialization.
1628 static int __init atmel_late_console_init(void)
1630 if (atmel_default_console_device
1631 && !(atmel_console.flags & CON_ENABLED))
1632 register_console(&atmel_console);
1634 return 0;
1637 core_initcall(atmel_late_console_init);
1639 static inline bool atmel_is_console_port(struct uart_port *port)
1641 return port->cons && port->cons->index == port->line;
1644 #else
1645 #define ATMEL_CONSOLE_DEVICE NULL
1647 static inline bool atmel_is_console_port(struct uart_port *port)
1649 return false;
1651 #endif
1653 static struct uart_driver atmel_uart = {
1654 .owner = THIS_MODULE,
1655 .driver_name = "atmel_serial",
1656 .dev_name = ATMEL_DEVICENAME,
1657 .major = SERIAL_ATMEL_MAJOR,
1658 .minor = MINOR_START,
1659 .nr = ATMEL_MAX_UART,
1660 .cons = ATMEL_CONSOLE_DEVICE,
1663 #ifdef CONFIG_PM
1664 static bool atmel_serial_clk_will_stop(void)
1666 #ifdef CONFIG_ARCH_AT91
1667 return at91_suspend_entering_slow_clock();
1668 #else
1669 return false;
1670 #endif
1673 static int atmel_serial_suspend(struct platform_device *pdev,
1674 pm_message_t state)
1676 struct uart_port *port = platform_get_drvdata(pdev);
1677 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1679 if (atmel_is_console_port(port) && console_suspend_enabled) {
1680 /* Drain the TX shifter */
1681 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1682 cpu_relax();
1685 /* we can not wake up if we're running on slow clock */
1686 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
1687 if (atmel_serial_clk_will_stop())
1688 device_set_wakeup_enable(&pdev->dev, 0);
1690 uart_suspend_port(&atmel_uart, port);
1692 return 0;
1695 static int atmel_serial_resume(struct platform_device *pdev)
1697 struct uart_port *port = platform_get_drvdata(pdev);
1698 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1700 uart_resume_port(&atmel_uart, port);
1701 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
1703 return 0;
1705 #else
1706 #define atmel_serial_suspend NULL
1707 #define atmel_serial_resume NULL
1708 #endif
1710 static int __devinit atmel_serial_probe(struct platform_device *pdev)
1712 struct atmel_uart_port *port;
1713 struct atmel_uart_data *pdata = pdev->dev.platform_data;
1714 void *data;
1715 int ret;
1717 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
1719 port = &atmel_ports[pdata->num];
1720 port->backup_imr = 0;
1722 atmel_init_port(port, pdev);
1724 if (!atmel_use_dma_rx(&port->uart)) {
1725 ret = -ENOMEM;
1726 data = kmalloc(sizeof(struct atmel_uart_char)
1727 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1728 if (!data)
1729 goto err_alloc_ring;
1730 port->rx_ring.buf = data;
1733 ret = uart_add_one_port(&atmel_uart, &port->uart);
1734 if (ret)
1735 goto err_add_port;
1737 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1738 if (atmel_is_console_port(&port->uart)
1739 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
1741 * The serial core enabled the clock for us, so undo
1742 * the clk_enable() in atmel_console_setup()
1744 clk_disable(port->clk);
1746 #endif
1748 device_init_wakeup(&pdev->dev, 1);
1749 platform_set_drvdata(pdev, port);
1751 if (port->rs485.flags & SER_RS485_ENABLED) {
1752 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
1753 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
1756 return 0;
1758 err_add_port:
1759 kfree(port->rx_ring.buf);
1760 port->rx_ring.buf = NULL;
1761 err_alloc_ring:
1762 if (!atmel_is_console_port(&port->uart)) {
1763 clk_put(port->clk);
1764 port->clk = NULL;
1767 return ret;
1770 static int __devexit atmel_serial_remove(struct platform_device *pdev)
1772 struct uart_port *port = platform_get_drvdata(pdev);
1773 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1774 int ret = 0;
1776 device_init_wakeup(&pdev->dev, 0);
1777 platform_set_drvdata(pdev, NULL);
1779 ret = uart_remove_one_port(&atmel_uart, port);
1781 tasklet_kill(&atmel_port->tasklet);
1782 kfree(atmel_port->rx_ring.buf);
1784 /* "port" is allocated statically, so we shouldn't free it */
1786 clk_put(atmel_port->clk);
1788 return ret;
1791 static struct platform_driver atmel_serial_driver = {
1792 .probe = atmel_serial_probe,
1793 .remove = __devexit_p(atmel_serial_remove),
1794 .suspend = atmel_serial_suspend,
1795 .resume = atmel_serial_resume,
1796 .driver = {
1797 .name = "atmel_usart",
1798 .owner = THIS_MODULE,
1802 static int __init atmel_serial_init(void)
1804 int ret;
1806 ret = uart_register_driver(&atmel_uart);
1807 if (ret)
1808 return ret;
1810 ret = platform_driver_register(&atmel_serial_driver);
1811 if (ret)
1812 uart_unregister_driver(&atmel_uart);
1814 return ret;
1817 static void __exit atmel_serial_exit(void)
1819 platform_driver_unregister(&atmel_serial_driver);
1820 uart_unregister_driver(&atmel_uart);
1823 module_init(atmel_serial_init);
1824 module_exit(atmel_serial_exit);
1826 MODULE_AUTHOR("Rick Bronson");
1827 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1828 MODULE_LICENSE("GPL");
1829 MODULE_ALIAS("platform:atmel_usart");