Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / serial / atmel_serial.c
blob5e2a00640b2e6a89fda57a9bff9d8b05df46285c
1 /*
2 * linux/drivers/char/atmel_serial.c
4 * Driver for Atmel AT91 / AT32 Serial ports
5 * Copyright (C) 2003 Rick Bronson
7 * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
8 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
10 * DMA support added by Chip Coldwell.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/module.h>
28 #include <linux/tty.h>
29 #include <linux/ioport.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/serial.h>
33 #include <linux/clk.h>
34 #include <linux/console.h>
35 #include <linux/sysrq.h>
36 #include <linux/tty_flip.h>
37 #include <linux/platform_device.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/atmel_pdc.h>
40 #include <linux/atmel_serial.h>
42 #include <asm/io.h>
44 #include <asm/mach/serial_at91.h>
45 #include <asm/arch/board.h>
47 #ifdef CONFIG_ARM
48 #include <asm/arch/cpu.h>
49 #include <asm/arch/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 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
64 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
65 * should coexist with the 8250 driver, such as if we have an external 16C550
66 * UART. */
67 #define SERIAL_ATMEL_MAJOR 204
68 #define MINOR_START 154
69 #define ATMEL_DEVICENAME "ttyAT"
71 #else
73 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
74 * name, but it is legally reserved for the 8250 driver. */
75 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
76 #define MINOR_START 64
77 #define ATMEL_DEVICENAME "ttyS"
79 #endif
81 #define ATMEL_ISR_PASS_LIMIT 256
83 /* UART registers. CR is write-only, hence no GET macro */
84 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
85 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
86 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
87 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
88 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
89 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
90 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
91 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
92 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
93 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
94 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
95 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
97 /* PDC registers */
98 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
99 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
101 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
102 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
103 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
104 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
105 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
107 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
108 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
110 static int (*atmel_open_hook)(struct uart_port *);
111 static void (*atmel_close_hook)(struct uart_port *);
113 struct atmel_dma_buffer {
114 unsigned char *buf;
115 dma_addr_t dma_addr;
116 unsigned int dma_size;
117 unsigned int ofs;
120 struct atmel_uart_char {
121 u16 status;
122 u16 ch;
125 #define ATMEL_SERIAL_RINGSIZE 1024
128 * We wrap our port structure around the generic uart_port.
130 struct atmel_uart_port {
131 struct uart_port uart; /* uart */
132 struct clk *clk; /* uart clock */
133 unsigned short suspended; /* is port suspended? */
134 int break_active; /* break being received */
136 short use_dma_rx; /* enable PDC receiver */
137 short pdc_rx_idx; /* current PDC RX buffer */
138 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
140 short use_dma_tx; /* enable PDC transmitter */
141 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
143 struct tasklet_struct tasklet;
144 unsigned int irq_status;
145 unsigned int irq_status_prev;
147 struct circ_buf rx_ring;
150 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
152 #ifdef SUPPORT_SYSRQ
153 static struct console atmel_console;
154 #endif
156 static inline struct atmel_uart_port *
157 to_atmel_uart_port(struct uart_port *uart)
159 return container_of(uart, struct atmel_uart_port, uart);
162 #ifdef CONFIG_SERIAL_ATMEL_PDC
163 static bool atmel_use_dma_rx(struct uart_port *port)
165 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
167 return atmel_port->use_dma_rx;
170 static bool atmel_use_dma_tx(struct uart_port *port)
172 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
174 return atmel_port->use_dma_tx;
176 #else
177 static bool atmel_use_dma_rx(struct uart_port *port)
179 return false;
182 static bool atmel_use_dma_tx(struct uart_port *port)
184 return false;
186 #endif
189 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
191 static u_int atmel_tx_empty(struct uart_port *port)
193 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
197 * Set state of the modem control output lines
199 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
201 unsigned int control = 0;
202 unsigned int mode;
204 #ifdef CONFIG_ARCH_AT91RM9200
205 if (cpu_is_at91rm9200()) {
207 * AT91RM9200 Errata #39: RTS0 is not internally connected
208 * to PA21. We need to drive the pin manually.
210 if (port->mapbase == AT91RM9200_BASE_US0) {
211 if (mctrl & TIOCM_RTS)
212 at91_set_gpio_value(AT91_PIN_PA21, 0);
213 else
214 at91_set_gpio_value(AT91_PIN_PA21, 1);
217 #endif
219 if (mctrl & TIOCM_RTS)
220 control |= ATMEL_US_RTSEN;
221 else
222 control |= ATMEL_US_RTSDIS;
224 if (mctrl & TIOCM_DTR)
225 control |= ATMEL_US_DTREN;
226 else
227 control |= ATMEL_US_DTRDIS;
229 UART_PUT_CR(port, control);
231 /* Local loopback mode? */
232 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
233 if (mctrl & TIOCM_LOOP)
234 mode |= ATMEL_US_CHMODE_LOC_LOOP;
235 else
236 mode |= ATMEL_US_CHMODE_NORMAL;
237 UART_PUT_MR(port, mode);
241 * Get state of the modem control input lines
243 static u_int atmel_get_mctrl(struct uart_port *port)
245 unsigned int status, ret = 0;
247 status = UART_GET_CSR(port);
250 * The control signals are active low.
252 if (!(status & ATMEL_US_DCD))
253 ret |= TIOCM_CD;
254 if (!(status & ATMEL_US_CTS))
255 ret |= TIOCM_CTS;
256 if (!(status & ATMEL_US_DSR))
257 ret |= TIOCM_DSR;
258 if (!(status & ATMEL_US_RI))
259 ret |= TIOCM_RI;
261 return ret;
265 * Stop transmitting.
267 static void atmel_stop_tx(struct uart_port *port)
269 if (atmel_use_dma_tx(port)) {
270 /* disable PDC transmit */
271 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
272 UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
273 } else
274 UART_PUT_IDR(port, ATMEL_US_TXRDY);
278 * Start transmitting.
280 static void atmel_start_tx(struct uart_port *port)
282 if (atmel_use_dma_tx(port)) {
283 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
284 /* The transmitter is already running. Yes, we
285 really need this.*/
286 return;
288 UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
289 /* re-enable PDC transmit */
290 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
291 } else
292 UART_PUT_IER(port, ATMEL_US_TXRDY);
296 * Stop receiving - port is in process of being closed.
298 static void atmel_stop_rx(struct uart_port *port)
300 if (atmel_use_dma_rx(port)) {
301 /* disable PDC receive */
302 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
303 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
304 } else
305 UART_PUT_IDR(port, ATMEL_US_RXRDY);
309 * Enable modem status interrupts
311 static void atmel_enable_ms(struct uart_port *port)
313 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
314 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
318 * Control the transmission of a break signal
320 static void atmel_break_ctl(struct uart_port *port, int break_state)
322 if (break_state != 0)
323 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
324 else
325 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
329 * Stores the incoming character in the ring buffer
331 static void
332 atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
333 unsigned int ch)
335 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
336 struct circ_buf *ring = &atmel_port->rx_ring;
337 struct atmel_uart_char *c;
339 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
340 /* Buffer overflow, ignore char */
341 return;
343 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
344 c->status = status;
345 c->ch = ch;
347 /* Make sure the character is stored before we update head. */
348 smp_wmb();
350 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
354 * Deal with parity, framing and overrun errors.
356 static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
358 /* clear error */
359 UART_PUT_CR(port, ATMEL_US_RSTSTA);
361 if (status & ATMEL_US_RXBRK) {
362 /* ignore side-effect */
363 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
364 port->icount.brk++;
366 if (status & ATMEL_US_PARE)
367 port->icount.parity++;
368 if (status & ATMEL_US_FRAME)
369 port->icount.frame++;
370 if (status & ATMEL_US_OVRE)
371 port->icount.overrun++;
375 * Characters received (called from interrupt handler)
377 static void atmel_rx_chars(struct uart_port *port)
379 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
380 unsigned int status, ch;
382 status = UART_GET_CSR(port);
383 while (status & ATMEL_US_RXRDY) {
384 ch = UART_GET_CHAR(port);
387 * note that the error handling code is
388 * out of the main execution path
390 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
391 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
392 || atmel_port->break_active)) {
394 /* clear error */
395 UART_PUT_CR(port, ATMEL_US_RSTSTA);
397 if (status & ATMEL_US_RXBRK
398 && !atmel_port->break_active) {
399 atmel_port->break_active = 1;
400 UART_PUT_IER(port, ATMEL_US_RXBRK);
401 } else {
403 * This is either the end-of-break
404 * condition or we've received at
405 * least one character without RXBRK
406 * being set. In both cases, the next
407 * RXBRK will indicate start-of-break.
409 UART_PUT_IDR(port, ATMEL_US_RXBRK);
410 status &= ~ATMEL_US_RXBRK;
411 atmel_port->break_active = 0;
415 atmel_buffer_rx_char(port, status, ch);
416 status = UART_GET_CSR(port);
419 tasklet_schedule(&atmel_port->tasklet);
423 * Transmit characters (called from tasklet with TXRDY interrupt
424 * disabled)
426 static void atmel_tx_chars(struct uart_port *port)
428 struct circ_buf *xmit = &port->info->xmit;
430 if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
431 UART_PUT_CHAR(port, port->x_char);
432 port->icount.tx++;
433 port->x_char = 0;
435 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
436 return;
438 while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
439 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
440 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
441 port->icount.tx++;
442 if (uart_circ_empty(xmit))
443 break;
446 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
447 uart_write_wakeup(port);
449 if (!uart_circ_empty(xmit))
450 UART_PUT_IER(port, ATMEL_US_TXRDY);
454 * receive interrupt handler.
456 static void
457 atmel_handle_receive(struct uart_port *port, unsigned int pending)
459 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
461 if (atmel_use_dma_rx(port)) {
463 * PDC receive. Just schedule the tasklet and let it
464 * figure out the details.
466 * TODO: We're not handling error flags correctly at
467 * the moment.
469 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
470 UART_PUT_IDR(port, (ATMEL_US_ENDRX
471 | ATMEL_US_TIMEOUT));
472 tasklet_schedule(&atmel_port->tasklet);
475 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
476 ATMEL_US_FRAME | ATMEL_US_PARE))
477 atmel_pdc_rxerr(port, pending);
480 /* Interrupt receive */
481 if (pending & ATMEL_US_RXRDY)
482 atmel_rx_chars(port);
483 else if (pending & ATMEL_US_RXBRK) {
485 * End of break detected. If it came along with a
486 * character, atmel_rx_chars will handle it.
488 UART_PUT_CR(port, ATMEL_US_RSTSTA);
489 UART_PUT_IDR(port, ATMEL_US_RXBRK);
490 atmel_port->break_active = 0;
495 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
497 static void
498 atmel_handle_transmit(struct uart_port *port, unsigned int pending)
500 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
502 if (atmel_use_dma_tx(port)) {
503 /* PDC transmit */
504 if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) {
505 UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
506 tasklet_schedule(&atmel_port->tasklet);
508 } else {
509 /* Interrupt transmit */
510 if (pending & ATMEL_US_TXRDY) {
511 UART_PUT_IDR(port, ATMEL_US_TXRDY);
512 tasklet_schedule(&atmel_port->tasklet);
518 * status flags interrupt handler.
520 static void
521 atmel_handle_status(struct uart_port *port, unsigned int pending,
522 unsigned int status)
524 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
526 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
527 | ATMEL_US_CTSIC)) {
528 atmel_port->irq_status = status;
529 tasklet_schedule(&atmel_port->tasklet);
534 * Interrupt handler
536 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
538 struct uart_port *port = dev_id;
539 unsigned int status, pending, pass_counter = 0;
541 do {
542 status = UART_GET_CSR(port);
543 pending = status & UART_GET_IMR(port);
544 if (!pending)
545 break;
547 atmel_handle_receive(port, pending);
548 atmel_handle_status(port, pending, status);
549 atmel_handle_transmit(port, pending);
550 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
552 <<<<<<< HEAD:drivers/serial/atmel_serial.c
553 return IRQ_HANDLED;
554 =======
555 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
556 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/serial/atmel_serial.c
560 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
562 static void atmel_tx_dma(struct uart_port *port)
564 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
565 struct circ_buf *xmit = &port->info->xmit;
566 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
567 int count;
569 xmit->tail += pdc->ofs;
570 xmit->tail &= UART_XMIT_SIZE - 1;
572 port->icount.tx += pdc->ofs;
573 pdc->ofs = 0;
575 if (!uart_circ_empty(xmit)) {
576 /* more to transmit - setup next transfer */
578 /* disable PDC transmit */
579 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
580 dma_sync_single_for_device(port->dev,
581 pdc->dma_addr,
582 pdc->dma_size,
583 DMA_TO_DEVICE);
585 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
586 pdc->ofs = count;
588 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
589 UART_PUT_TCR(port, count);
590 /* re-enable PDC transmit and interrupts */
591 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
592 UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
593 } else {
594 /* nothing left to transmit - disable the transmitter */
596 /* disable PDC transmit */
597 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
600 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
601 uart_write_wakeup(port);
604 static void atmel_rx_from_ring(struct uart_port *port)
606 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
607 struct circ_buf *ring = &atmel_port->rx_ring;
608 unsigned int flg;
609 unsigned int status;
611 while (ring->head != ring->tail) {
612 struct atmel_uart_char c;
614 /* Make sure c is loaded after head. */
615 smp_rmb();
617 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
619 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
621 port->icount.rx++;
622 status = c.status;
623 flg = TTY_NORMAL;
626 * note that the error handling code is
627 * out of the main execution path
629 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
630 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
631 if (status & ATMEL_US_RXBRK) {
632 /* ignore side-effect */
633 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
635 port->icount.brk++;
636 if (uart_handle_break(port))
637 continue;
639 if (status & ATMEL_US_PARE)
640 port->icount.parity++;
641 if (status & ATMEL_US_FRAME)
642 port->icount.frame++;
643 if (status & ATMEL_US_OVRE)
644 port->icount.overrun++;
646 status &= port->read_status_mask;
648 if (status & ATMEL_US_RXBRK)
649 flg = TTY_BREAK;
650 else if (status & ATMEL_US_PARE)
651 flg = TTY_PARITY;
652 else if (status & ATMEL_US_FRAME)
653 flg = TTY_FRAME;
657 if (uart_handle_sysrq_char(port, c.ch))
658 continue;
660 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
664 * Drop the lock here since it might end up calling
665 * uart_start(), which takes the lock.
667 spin_unlock(&port->lock);
668 tty_flip_buffer_push(port->info->tty);
669 spin_lock(&port->lock);
672 static void atmel_rx_from_dma(struct uart_port *port)
674 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
675 struct tty_struct *tty = port->info->tty;
676 struct atmel_dma_buffer *pdc;
677 int rx_idx = atmel_port->pdc_rx_idx;
678 unsigned int head;
679 unsigned int tail;
680 unsigned int count;
682 do {
683 /* Reset the UART timeout early so that we don't miss one */
684 UART_PUT_CR(port, ATMEL_US_STTTO);
686 pdc = &atmel_port->pdc_rx[rx_idx];
687 head = UART_GET_RPR(port) - pdc->dma_addr;
688 tail = pdc->ofs;
690 /* If the PDC has switched buffers, RPR won't contain
691 * any address within the current buffer. Since head
692 * is unsigned, we just need a one-way comparison to
693 * find out.
695 * In this case, we just need to consume the entire
696 * buffer and resubmit it for DMA. This will clear the
697 * ENDRX bit as well, so that we can safely re-enable
698 * all interrupts below.
700 head = min(head, pdc->dma_size);
702 if (likely(head != tail)) {
703 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
704 pdc->dma_size, DMA_FROM_DEVICE);
707 * head will only wrap around when we recycle
708 * the DMA buffer, and when that happens, we
709 * explicitly set tail to 0. So head will
710 * always be greater than tail.
712 count = head - tail;
714 tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
716 dma_sync_single_for_device(port->dev, pdc->dma_addr,
717 pdc->dma_size, DMA_FROM_DEVICE);
719 port->icount.rx += count;
720 pdc->ofs = head;
724 * If the current buffer is full, we need to check if
725 * the next one contains any additional data.
727 if (head >= pdc->dma_size) {
728 pdc->ofs = 0;
729 UART_PUT_RNPR(port, pdc->dma_addr);
730 UART_PUT_RNCR(port, pdc->dma_size);
732 rx_idx = !rx_idx;
733 atmel_port->pdc_rx_idx = rx_idx;
735 } while (head >= pdc->dma_size);
738 * Drop the lock here since it might end up calling
739 * uart_start(), which takes the lock.
741 spin_unlock(&port->lock);
742 tty_flip_buffer_push(tty);
743 spin_lock(&port->lock);
745 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
749 * tasklet handling tty stuff outside the interrupt handler.
751 static void atmel_tasklet_func(unsigned long data)
753 struct uart_port *port = (struct uart_port *)data;
754 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
755 unsigned int status;
756 unsigned int status_change;
758 /* The interrupt handler does not take the lock */
759 spin_lock(&port->lock);
761 if (atmel_use_dma_tx(port))
762 atmel_tx_dma(port);
763 else
764 atmel_tx_chars(port);
766 status = atmel_port->irq_status;
767 status_change = status ^ atmel_port->irq_status_prev;
769 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
770 | ATMEL_US_DCD | ATMEL_US_CTS)) {
771 /* TODO: All reads to CSR will clear these interrupts! */
772 if (status_change & ATMEL_US_RI)
773 port->icount.rng++;
774 if (status_change & ATMEL_US_DSR)
775 port->icount.dsr++;
776 if (status_change & ATMEL_US_DCD)
777 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
778 if (status_change & ATMEL_US_CTS)
779 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
781 wake_up_interruptible(&port->info->delta_msr_wait);
783 atmel_port->irq_status_prev = status;
786 if (atmel_use_dma_rx(port))
787 atmel_rx_from_dma(port);
788 else
789 atmel_rx_from_ring(port);
791 spin_unlock(&port->lock);
795 * Perform initialization and enable port for reception
797 static int atmel_startup(struct uart_port *port)
799 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
800 struct tty_struct *tty = port->info->tty;
801 int retval;
804 * Ensure that no interrupts are enabled otherwise when
805 * request_irq() is called we could get stuck trying to
806 * handle an unexpected interrupt
808 UART_PUT_IDR(port, -1);
811 * Allocate the IRQ
813 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
814 tty ? tty->name : "atmel_serial", port);
815 if (retval) {
816 printk("atmel_serial: atmel_startup - Can't get irq\n");
817 return retval;
821 * Initialize DMA (if necessary)
823 if (atmel_use_dma_rx(port)) {
824 int i;
826 for (i = 0; i < 2; i++) {
827 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
829 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
830 if (pdc->buf == NULL) {
831 if (i != 0) {
832 dma_unmap_single(port->dev,
833 atmel_port->pdc_rx[0].dma_addr,
834 PDC_BUFFER_SIZE,
835 DMA_FROM_DEVICE);
836 kfree(atmel_port->pdc_rx[0].buf);
838 free_irq(port->irq, port);
839 return -ENOMEM;
841 pdc->dma_addr = dma_map_single(port->dev,
842 pdc->buf,
843 PDC_BUFFER_SIZE,
844 DMA_FROM_DEVICE);
845 pdc->dma_size = PDC_BUFFER_SIZE;
846 pdc->ofs = 0;
849 atmel_port->pdc_rx_idx = 0;
851 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
852 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
854 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
855 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
857 if (atmel_use_dma_tx(port)) {
858 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
859 struct circ_buf *xmit = &port->info->xmit;
861 pdc->buf = xmit->buf;
862 pdc->dma_addr = dma_map_single(port->dev,
863 pdc->buf,
864 UART_XMIT_SIZE,
865 DMA_TO_DEVICE);
866 pdc->dma_size = UART_XMIT_SIZE;
867 pdc->ofs = 0;
871 * If there is a specific "open" function (to register
872 * control line interrupts)
874 if (atmel_open_hook) {
875 retval = atmel_open_hook(port);
876 if (retval) {
877 free_irq(port->irq, port);
878 return retval;
883 * Finally, enable the serial port
885 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
886 /* enable xmit & rcvr */
887 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
889 if (atmel_use_dma_rx(port)) {
890 /* set UART timeout */
891 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
892 UART_PUT_CR(port, ATMEL_US_STTTO);
894 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
895 /* enable PDC controller */
896 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
897 } else {
898 /* enable receive only */
899 UART_PUT_IER(port, ATMEL_US_RXRDY);
902 return 0;
906 * Disable the port
908 static void atmel_shutdown(struct uart_port *port)
910 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
912 * Ensure everything is stopped.
914 atmel_stop_rx(port);
915 atmel_stop_tx(port);
918 * Shut-down the DMA.
920 if (atmel_use_dma_rx(port)) {
921 int i;
923 for (i = 0; i < 2; i++) {
924 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
926 dma_unmap_single(port->dev,
927 pdc->dma_addr,
928 pdc->dma_size,
929 DMA_FROM_DEVICE);
930 kfree(pdc->buf);
933 if (atmel_use_dma_tx(port)) {
934 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
936 dma_unmap_single(port->dev,
937 pdc->dma_addr,
938 pdc->dma_size,
939 DMA_TO_DEVICE);
943 * Disable all interrupts, port and break condition.
945 UART_PUT_CR(port, ATMEL_US_RSTSTA);
946 UART_PUT_IDR(port, -1);
949 * Free the interrupt
951 free_irq(port->irq, port);
954 * If there is a specific "close" function (to unregister
955 * control line interrupts)
957 if (atmel_close_hook)
958 atmel_close_hook(port);
962 * Power / Clock management.
964 static void atmel_serial_pm(struct uart_port *port, unsigned int state,
965 unsigned int oldstate)
967 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
969 switch (state) {
970 case 0:
972 * Enable the peripheral clock for this serial port.
973 * This is called on uart_open() or a resume event.
975 clk_enable(atmel_port->clk);
976 break;
977 case 3:
979 * Disable the peripheral clock for this serial port.
980 * This is called on uart_close() or a suspend event.
982 clk_disable(atmel_port->clk);
983 break;
984 default:
985 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
990 * Change the port parameters
992 static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
993 struct ktermios *old)
995 unsigned long flags;
996 unsigned int mode, imr, quot, baud;
998 /* Get current mode register */
999 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1000 | ATMEL_US_NBSTOP | ATMEL_US_PAR);
1002 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1003 quot = uart_get_divisor(port, baud);
1005 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
1006 quot /= 8;
1007 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1010 /* byte size */
1011 switch (termios->c_cflag & CSIZE) {
1012 case CS5:
1013 mode |= ATMEL_US_CHRL_5;
1014 break;
1015 case CS6:
1016 mode |= ATMEL_US_CHRL_6;
1017 break;
1018 case CS7:
1019 mode |= ATMEL_US_CHRL_7;
1020 break;
1021 default:
1022 mode |= ATMEL_US_CHRL_8;
1023 break;
1026 /* stop bits */
1027 if (termios->c_cflag & CSTOPB)
1028 mode |= ATMEL_US_NBSTOP_2;
1030 /* parity */
1031 if (termios->c_cflag & PARENB) {
1032 /* Mark or Space parity */
1033 if (termios->c_cflag & CMSPAR) {
1034 if (termios->c_cflag & PARODD)
1035 mode |= ATMEL_US_PAR_MARK;
1036 else
1037 mode |= ATMEL_US_PAR_SPACE;
1038 } else if (termios->c_cflag & PARODD)
1039 mode |= ATMEL_US_PAR_ODD;
1040 else
1041 mode |= ATMEL_US_PAR_EVEN;
1042 } else
1043 mode |= ATMEL_US_PAR_NONE;
1045 spin_lock_irqsave(&port->lock, flags);
1047 port->read_status_mask = ATMEL_US_OVRE;
1048 if (termios->c_iflag & INPCK)
1049 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1050 if (termios->c_iflag & (BRKINT | PARMRK))
1051 port->read_status_mask |= ATMEL_US_RXBRK;
1053 if (atmel_use_dma_rx(port))
1054 /* need to enable error interrupts */
1055 UART_PUT_IER(port, port->read_status_mask);
1058 * Characters to ignore
1060 port->ignore_status_mask = 0;
1061 if (termios->c_iflag & IGNPAR)
1062 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1063 if (termios->c_iflag & IGNBRK) {
1064 port->ignore_status_mask |= ATMEL_US_RXBRK;
1066 * If we're ignoring parity and break indicators,
1067 * ignore overruns too (for real raw support).
1069 if (termios->c_iflag & IGNPAR)
1070 port->ignore_status_mask |= ATMEL_US_OVRE;
1072 /* TODO: Ignore all characters if CREAD is set.*/
1074 /* update the per-port timeout */
1075 uart_update_timeout(port, termios->c_cflag, baud);
1077 /* save/disable interrupts and drain transmitter */
1078 imr = UART_GET_IMR(port);
1079 UART_PUT_IDR(port, -1);
1080 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1081 cpu_relax();
1083 /* disable receiver and transmitter */
1084 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1086 /* set the parity, stop bits and data size */
1087 UART_PUT_MR(port, mode);
1089 /* set the baud rate */
1090 UART_PUT_BRGR(port, quot);
1091 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1092 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1094 /* restore interrupts */
1095 UART_PUT_IER(port, imr);
1097 /* CTS flow-control and modem-status interrupts */
1098 if (UART_ENABLE_MS(port, termios->c_cflag))
1099 port->ops->enable_ms(port);
1101 spin_unlock_irqrestore(&port->lock, flags);
1105 * Return string describing the specified port
1107 static const char *atmel_type(struct uart_port *port)
1109 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1113 * Release the memory region(s) being used by 'port'.
1115 static void atmel_release_port(struct uart_port *port)
1117 struct platform_device *pdev = to_platform_device(port->dev);
1118 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1120 release_mem_region(port->mapbase, size);
1122 if (port->flags & UPF_IOREMAP) {
1123 iounmap(port->membase);
1124 port->membase = NULL;
1129 * Request the memory region(s) being used by 'port'.
1131 static int atmel_request_port(struct uart_port *port)
1133 struct platform_device *pdev = to_platform_device(port->dev);
1134 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1136 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1137 return -EBUSY;
1139 if (port->flags & UPF_IOREMAP) {
1140 port->membase = ioremap(port->mapbase, size);
1141 if (port->membase == NULL) {
1142 release_mem_region(port->mapbase, size);
1143 return -ENOMEM;
1147 return 0;
1151 * Configure/autoconfigure the port.
1153 static void atmel_config_port(struct uart_port *port, int flags)
1155 if (flags & UART_CONFIG_TYPE) {
1156 port->type = PORT_ATMEL;
1157 atmel_request_port(port);
1162 * Verify the new serial_struct (for TIOCSSERIAL).
1164 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1166 int ret = 0;
1167 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1168 ret = -EINVAL;
1169 if (port->irq != ser->irq)
1170 ret = -EINVAL;
1171 if (ser->io_type != SERIAL_IO_MEM)
1172 ret = -EINVAL;
1173 if (port->uartclk / 16 != ser->baud_base)
1174 ret = -EINVAL;
1175 if ((void *)port->mapbase != ser->iomem_base)
1176 ret = -EINVAL;
1177 if (port->iobase != ser->port)
1178 ret = -EINVAL;
1179 if (ser->hub6 != 0)
1180 ret = -EINVAL;
1181 return ret;
1184 static struct uart_ops atmel_pops = {
1185 .tx_empty = atmel_tx_empty,
1186 .set_mctrl = atmel_set_mctrl,
1187 .get_mctrl = atmel_get_mctrl,
1188 .stop_tx = atmel_stop_tx,
1189 .start_tx = atmel_start_tx,
1190 .stop_rx = atmel_stop_rx,
1191 .enable_ms = atmel_enable_ms,
1192 .break_ctl = atmel_break_ctl,
1193 .startup = atmel_startup,
1194 .shutdown = atmel_shutdown,
1195 .set_termios = atmel_set_termios,
1196 .type = atmel_type,
1197 .release_port = atmel_release_port,
1198 .request_port = atmel_request_port,
1199 .config_port = atmel_config_port,
1200 .verify_port = atmel_verify_port,
1201 .pm = atmel_serial_pm,
1205 * Configure the port from the platform device resource info.
1207 static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1208 struct platform_device *pdev)
1210 struct uart_port *port = &atmel_port->uart;
1211 struct atmel_uart_data *data = pdev->dev.platform_data;
1213 port->iotype = UPIO_MEM;
1214 port->flags = UPF_BOOT_AUTOCONF;
1215 port->ops = &atmel_pops;
1216 port->fifosize = 1;
1217 port->line = pdev->id;
1218 port->dev = &pdev->dev;
1220 port->mapbase = pdev->resource[0].start;
1221 port->irq = pdev->resource[1].start;
1223 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1224 (unsigned long)port);
1226 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1228 if (data->regs)
1229 /* Already mapped by setup code */
1230 port->membase = data->regs;
1231 else {
1232 port->flags |= UPF_IOREMAP;
1233 port->membase = NULL;
1236 /* for console, the clock could already be configured */
1237 if (!atmel_port->clk) {
1238 atmel_port->clk = clk_get(&pdev->dev, "usart");
1239 clk_enable(atmel_port->clk);
1240 port->uartclk = clk_get_rate(atmel_port->clk);
1243 atmel_port->use_dma_rx = data->use_dma_rx;
1244 atmel_port->use_dma_tx = data->use_dma_tx;
1245 if (atmel_use_dma_tx(port))
1246 port->fifosize = PDC_BUFFER_SIZE;
1250 * Register board-specific modem-control line handlers.
1252 void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
1254 if (fns->enable_ms)
1255 atmel_pops.enable_ms = fns->enable_ms;
1256 if (fns->get_mctrl)
1257 atmel_pops.get_mctrl = fns->get_mctrl;
1258 if (fns->set_mctrl)
1259 atmel_pops.set_mctrl = fns->set_mctrl;
1260 atmel_open_hook = fns->open;
1261 atmel_close_hook = fns->close;
1262 atmel_pops.pm = fns->pm;
1263 atmel_pops.set_wake = fns->set_wake;
1266 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
1267 static void atmel_console_putchar(struct uart_port *port, int ch)
1269 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1270 cpu_relax();
1271 UART_PUT_CHAR(port, ch);
1275 * Interrupts are disabled on entering
1277 static void atmel_console_write(struct console *co, const char *s, u_int count)
1279 struct uart_port *port = &atmel_ports[co->index].uart;
1280 unsigned int status, imr;
1283 * First, save IMR and then disable interrupts
1285 imr = UART_GET_IMR(port);
1286 UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
1288 uart_console_write(port, s, count, atmel_console_putchar);
1291 * Finally, wait for transmitter to become empty
1292 * and restore IMR
1294 do {
1295 status = UART_GET_CSR(port);
1296 } while (!(status & ATMEL_US_TXRDY));
1297 /* set interrupts back the way they were */
1298 UART_PUT_IER(port, imr);
1302 * If the port was already initialised (eg, by a boot loader),
1303 * try to determine the current setup.
1305 static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1306 int *parity, int *bits)
1308 unsigned int mr, quot;
1311 * If the baud rate generator isn't running, the port wasn't
1312 * initialized by the boot loader.
1314 quot = UART_GET_BRGR(port);
1315 if (!quot)
1316 return;
1318 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1319 if (mr == ATMEL_US_CHRL_8)
1320 *bits = 8;
1321 else
1322 *bits = 7;
1324 mr = UART_GET_MR(port) & ATMEL_US_PAR;
1325 if (mr == ATMEL_US_PAR_EVEN)
1326 *parity = 'e';
1327 else if (mr == ATMEL_US_PAR_ODD)
1328 *parity = 'o';
1331 * The serial core only rounds down when matching this to a
1332 * supported baud rate. Make sure we don't end up slightly
1333 * lower than one of those, as it would make us fall through
1334 * to a much lower baud rate than we really want.
1336 *baud = port->uartclk / (16 * (quot - 1));
1339 static int __init atmel_console_setup(struct console *co, char *options)
1341 struct uart_port *port = &atmel_ports[co->index].uart;
1342 int baud = 115200;
1343 int bits = 8;
1344 int parity = 'n';
1345 int flow = 'n';
1347 if (port->membase == NULL) {
1348 /* Port not initialized yet - delay setup */
1349 return -ENODEV;
1352 UART_PUT_IDR(port, -1);
1353 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1354 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1356 if (options)
1357 uart_parse_options(options, &baud, &parity, &bits, &flow);
1358 else
1359 atmel_console_get_options(port, &baud, &parity, &bits);
1361 return uart_set_options(port, co, baud, parity, bits, flow);
1364 static struct uart_driver atmel_uart;
1366 static struct console atmel_console = {
1367 .name = ATMEL_DEVICENAME,
1368 .write = atmel_console_write,
1369 .device = uart_console_device,
1370 .setup = atmel_console_setup,
1371 .flags = CON_PRINTBUFFER,
1372 .index = -1,
1373 .data = &atmel_uart,
1376 #define ATMEL_CONSOLE_DEVICE &atmel_console
1379 * Early console initialization (before VM subsystem initialized).
1381 static int __init atmel_console_init(void)
1383 if (atmel_default_console_device) {
1384 add_preferred_console(ATMEL_DEVICENAME,
1385 atmel_default_console_device->id, NULL);
1386 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1387 atmel_default_console_device);
1388 register_console(&atmel_console);
1391 return 0;
1394 console_initcall(atmel_console_init);
1397 * Late console initialization.
1399 static int __init atmel_late_console_init(void)
1401 if (atmel_default_console_device
1402 && !(atmel_console.flags & CON_ENABLED))
1403 register_console(&atmel_console);
1405 return 0;
1408 core_initcall(atmel_late_console_init);
1410 static inline bool atmel_is_console_port(struct uart_port *port)
1412 return port->cons && port->cons->index == port->line;
1415 #else
1416 #define ATMEL_CONSOLE_DEVICE NULL
1418 static inline bool atmel_is_console_port(struct uart_port *port)
1420 return false;
1422 #endif
1424 static struct uart_driver atmel_uart = {
1425 .owner = THIS_MODULE,
1426 .driver_name = "atmel_serial",
1427 .dev_name = ATMEL_DEVICENAME,
1428 .major = SERIAL_ATMEL_MAJOR,
1429 .minor = MINOR_START,
1430 .nr = ATMEL_MAX_UART,
1431 .cons = ATMEL_CONSOLE_DEVICE,
1434 #ifdef CONFIG_PM
1435 static int atmel_serial_suspend(struct platform_device *pdev,
1436 pm_message_t state)
1438 struct uart_port *port = platform_get_drvdata(pdev);
1439 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1441 if (device_may_wakeup(&pdev->dev)
1442 && !at91_suspend_entering_slow_clock())
1443 enable_irq_wake(port->irq);
1444 else {
1445 uart_suspend_port(&atmel_uart, port);
1446 atmel_port->suspended = 1;
1449 return 0;
1452 static int atmel_serial_resume(struct platform_device *pdev)
1454 struct uart_port *port = platform_get_drvdata(pdev);
1455 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1457 if (atmel_port->suspended) {
1458 uart_resume_port(&atmel_uart, port);
1459 atmel_port->suspended = 0;
1460 } else
1461 disable_irq_wake(port->irq);
1463 return 0;
1465 #else
1466 #define atmel_serial_suspend NULL
1467 #define atmel_serial_resume NULL
1468 #endif
1470 static int __devinit atmel_serial_probe(struct platform_device *pdev)
1472 struct atmel_uart_port *port;
1473 void *data;
1474 int ret;
1476 BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE));
1478 port = &atmel_ports[pdev->id];
1479 atmel_init_port(port, pdev);
1481 if (!atmel_use_dma_rx(&port->uart)) {
1482 ret = -ENOMEM;
1483 data = kmalloc(sizeof(struct atmel_uart_char)
1484 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1485 if (!data)
1486 goto err_alloc_ring;
1487 port->rx_ring.buf = data;
1490 ret = uart_add_one_port(&atmel_uart, &port->uart);
1491 if (ret)
1492 goto err_add_port;
1494 device_init_wakeup(&pdev->dev, 1);
1495 platform_set_drvdata(pdev, port);
1497 return 0;
1499 err_add_port:
1500 kfree(port->rx_ring.buf);
1501 port->rx_ring.buf = NULL;
1502 err_alloc_ring:
1503 if (!atmel_is_console_port(&port->uart)) {
1504 clk_disable(port->clk);
1505 clk_put(port->clk);
1506 port->clk = NULL;
1509 return ret;
1512 static int __devexit atmel_serial_remove(struct platform_device *pdev)
1514 struct uart_port *port = platform_get_drvdata(pdev);
1515 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1516 int ret = 0;
1518 device_init_wakeup(&pdev->dev, 0);
1519 platform_set_drvdata(pdev, NULL);
1521 ret = uart_remove_one_port(&atmel_uart, port);
1523 tasklet_kill(&atmel_port->tasklet);
1524 kfree(atmel_port->rx_ring.buf);
1526 /* "port" is allocated statically, so we shouldn't free it */
1528 clk_disable(atmel_port->clk);
1529 clk_put(atmel_port->clk);
1531 return ret;
1534 static struct platform_driver atmel_serial_driver = {
1535 .probe = atmel_serial_probe,
1536 .remove = __devexit_p(atmel_serial_remove),
1537 .suspend = atmel_serial_suspend,
1538 .resume = atmel_serial_resume,
1539 .driver = {
1540 .name = "atmel_usart",
1541 .owner = THIS_MODULE,
1545 static int __init atmel_serial_init(void)
1547 int ret;
1549 ret = uart_register_driver(&atmel_uart);
1550 if (ret)
1551 return ret;
1553 ret = platform_driver_register(&atmel_serial_driver);
1554 if (ret)
1555 uart_unregister_driver(&atmel_uart);
1557 return ret;
1560 static void __exit atmel_serial_exit(void)
1562 platform_driver_unregister(&atmel_serial_driver);
1563 uart_unregister_driver(&atmel_uart);
1566 module_init(atmel_serial_init);
1567 module_exit(atmel_serial_exit);
1569 MODULE_AUTHOR("Rick Bronson");
1570 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1571 MODULE_LICENSE("GPL");