atmel_serial: split the interrupt handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / atmel_serial.c
blobf0f6ea3a9eed23f22f3f96060fc2e87a1f0107e6
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 * 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/atmel_pdc.h>
37 #include <linux/atmel_serial.h>
39 #include <asm/io.h>
41 #include <asm/mach/serial_at91.h>
42 #include <asm/arch/board.h>
44 #ifdef CONFIG_ARM
45 #include <asm/arch/cpu.h>
46 #include <asm/arch/gpio.h>
47 #endif
49 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
50 #define SUPPORT_SYSRQ
51 #endif
53 #include <linux/serial_core.h>
55 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
57 /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
58 * should coexist with the 8250 driver, such as if we have an external 16C550
59 * UART. */
60 #define SERIAL_ATMEL_MAJOR 204
61 #define MINOR_START 154
62 #define ATMEL_DEVICENAME "ttyAT"
64 #else
66 /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
67 * name, but it is legally reserved for the 8250 driver. */
68 #define SERIAL_ATMEL_MAJOR TTY_MAJOR
69 #define MINOR_START 64
70 #define ATMEL_DEVICENAME "ttyS"
72 #endif
74 #define ATMEL_ISR_PASS_LIMIT 256
76 /* UART registers. CR is write-only, hence no GET macro */
77 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
78 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
79 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
80 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
81 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
82 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
83 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
84 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
85 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
86 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
87 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
88 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
90 /* PDC registers */
91 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
92 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
94 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
95 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
96 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
97 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
98 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
100 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
101 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
103 static int (*atmel_open_hook)(struct uart_port *);
104 static void (*atmel_close_hook)(struct uart_port *);
106 struct atmel_uart_char {
107 u16 status;
108 u16 ch;
111 #define ATMEL_SERIAL_RINGSIZE 1024
114 * We wrap our port structure around the generic uart_port.
116 struct atmel_uart_port {
117 struct uart_port uart; /* uart */
118 struct clk *clk; /* uart clock */
119 unsigned short suspended; /* is port suspended? */
120 int break_active; /* break being received */
122 struct tasklet_struct tasklet;
123 unsigned int irq_status;
124 unsigned int irq_status_prev;
126 struct circ_buf rx_ring;
129 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
131 #ifdef SUPPORT_SYSRQ
132 static struct console atmel_console;
133 #endif
136 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
138 static u_int atmel_tx_empty(struct uart_port *port)
140 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
144 * Set state of the modem control output lines
146 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
148 unsigned int control = 0;
149 unsigned int mode;
151 #ifdef CONFIG_ARCH_AT91RM9200
152 if (cpu_is_at91rm9200()) {
154 * AT91RM9200 Errata #39: RTS0 is not internally connected
155 * to PA21. We need to drive the pin manually.
157 if (port->mapbase == AT91RM9200_BASE_US0) {
158 if (mctrl & TIOCM_RTS)
159 at91_set_gpio_value(AT91_PIN_PA21, 0);
160 else
161 at91_set_gpio_value(AT91_PIN_PA21, 1);
164 #endif
166 if (mctrl & TIOCM_RTS)
167 control |= ATMEL_US_RTSEN;
168 else
169 control |= ATMEL_US_RTSDIS;
171 if (mctrl & TIOCM_DTR)
172 control |= ATMEL_US_DTREN;
173 else
174 control |= ATMEL_US_DTRDIS;
176 UART_PUT_CR(port, control);
178 /* Local loopback mode? */
179 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
180 if (mctrl & TIOCM_LOOP)
181 mode |= ATMEL_US_CHMODE_LOC_LOOP;
182 else
183 mode |= ATMEL_US_CHMODE_NORMAL;
184 UART_PUT_MR(port, mode);
188 * Get state of the modem control input lines
190 static u_int atmel_get_mctrl(struct uart_port *port)
192 unsigned int status, ret = 0;
194 status = UART_GET_CSR(port);
197 * The control signals are active low.
199 if (!(status & ATMEL_US_DCD))
200 ret |= TIOCM_CD;
201 if (!(status & ATMEL_US_CTS))
202 ret |= TIOCM_CTS;
203 if (!(status & ATMEL_US_DSR))
204 ret |= TIOCM_DSR;
205 if (!(status & ATMEL_US_RI))
206 ret |= TIOCM_RI;
208 return ret;
212 * Stop transmitting.
214 static void atmel_stop_tx(struct uart_port *port)
216 UART_PUT_IDR(port, ATMEL_US_TXRDY);
220 * Start transmitting.
222 static void atmel_start_tx(struct uart_port *port)
224 UART_PUT_IER(port, ATMEL_US_TXRDY);
228 * Stop receiving - port is in process of being closed.
230 static void atmel_stop_rx(struct uart_port *port)
232 UART_PUT_IDR(port, ATMEL_US_RXRDY);
236 * Enable modem status interrupts
238 static void atmel_enable_ms(struct uart_port *port)
240 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
241 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
245 * Control the transmission of a break signal
247 static void atmel_break_ctl(struct uart_port *port, int break_state)
249 if (break_state != 0)
250 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
251 else
252 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
256 * Stores the incoming character in the ring buffer
258 static void
259 atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
260 unsigned int ch)
262 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
263 struct circ_buf *ring = &atmel_port->rx_ring;
264 struct atmel_uart_char *c;
266 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
267 /* Buffer overflow, ignore char */
268 return;
270 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
271 c->status = status;
272 c->ch = ch;
274 /* Make sure the character is stored before we update head. */
275 smp_wmb();
277 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
281 * Characters received (called from interrupt handler)
283 static void atmel_rx_chars(struct uart_port *port)
285 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
286 unsigned int status, ch;
288 status = UART_GET_CSR(port);
289 while (status & ATMEL_US_RXRDY) {
290 ch = UART_GET_CHAR(port);
293 * note that the error handling code is
294 * out of the main execution path
296 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
297 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
298 || atmel_port->break_active)) {
300 /* clear error */
301 UART_PUT_CR(port, ATMEL_US_RSTSTA);
303 if (status & ATMEL_US_RXBRK
304 && !atmel_port->break_active) {
305 atmel_port->break_active = 1;
306 UART_PUT_IER(port, ATMEL_US_RXBRK);
307 } else {
309 * This is either the end-of-break
310 * condition or we've received at
311 * least one character without RXBRK
312 * being set. In both cases, the next
313 * RXBRK will indicate start-of-break.
315 UART_PUT_IDR(port, ATMEL_US_RXBRK);
316 status &= ~ATMEL_US_RXBRK;
317 atmel_port->break_active = 0;
321 atmel_buffer_rx_char(port, status, ch);
322 status = UART_GET_CSR(port);
325 tasklet_schedule(&atmel_port->tasklet);
329 * Transmit characters (called from tasklet with TXRDY interrupt
330 * disabled)
332 static void atmel_tx_chars(struct uart_port *port)
334 struct circ_buf *xmit = &port->info->xmit;
336 if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
337 UART_PUT_CHAR(port, port->x_char);
338 port->icount.tx++;
339 port->x_char = 0;
341 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
342 return;
344 while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
345 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
346 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
347 port->icount.tx++;
348 if (uart_circ_empty(xmit))
349 break;
352 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
353 uart_write_wakeup(port);
355 if (!uart_circ_empty(xmit))
356 UART_PUT_IER(port, ATMEL_US_TXRDY);
360 * receive interrupt handler.
362 static void
363 atmel_handle_receive(struct uart_port *port, unsigned int pending)
365 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
367 /* Interrupt receive */
368 if (pending & ATMEL_US_RXRDY)
369 atmel_rx_chars(port);
370 else if (pending & ATMEL_US_RXBRK) {
372 * End of break detected. If it came along with a
373 * character, atmel_rx_chars will handle it.
375 UART_PUT_CR(port, ATMEL_US_RSTSTA);
376 UART_PUT_IDR(port, ATMEL_US_RXBRK);
377 atmel_port->break_active = 0;
382 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
384 static void
385 atmel_handle_transmit(struct uart_port *port, unsigned int pending)
387 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
389 /* Interrupt transmit */
390 if (pending & ATMEL_US_TXRDY) {
391 UART_PUT_IDR(port, ATMEL_US_TXRDY);
392 tasklet_schedule(&atmel_port->tasklet);
397 * status flags interrupt handler.
399 static void
400 atmel_handle_status(struct uart_port *port, unsigned int pending,
401 unsigned int status)
403 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
405 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
406 | ATMEL_US_CTSIC)) {
407 atmel_port->irq_status = status;
408 tasklet_schedule(&atmel_port->tasklet);
413 * Interrupt handler
415 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
417 struct uart_port *port = dev_id;
418 unsigned int status, pending, pass_counter = 0;
420 status = UART_GET_CSR(port);
421 pending = status & UART_GET_IMR(port);
422 while (pending) {
423 atmel_handle_receive(port, pending);
424 atmel_handle_status(port, pending, status);
425 atmel_handle_transmit(port, pending);
427 if (pass_counter++ > ATMEL_ISR_PASS_LIMIT)
428 break;
430 status = UART_GET_CSR(port);
431 pending = status & UART_GET_IMR(port);
433 return IRQ_HANDLED;
436 static void atmel_rx_from_ring(struct uart_port *port)
438 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
439 struct circ_buf *ring = &atmel_port->rx_ring;
440 unsigned int flg;
441 unsigned int status;
443 while (ring->head != ring->tail) {
444 struct atmel_uart_char c;
446 /* Make sure c is loaded after head. */
447 smp_rmb();
449 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
451 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
453 port->icount.rx++;
454 status = c.status;
455 flg = TTY_NORMAL;
458 * note that the error handling code is
459 * out of the main execution path
461 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
462 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
463 if (status & ATMEL_US_RXBRK) {
464 /* ignore side-effect */
465 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
467 port->icount.brk++;
468 if (uart_handle_break(port))
469 continue;
471 if (status & ATMEL_US_PARE)
472 port->icount.parity++;
473 if (status & ATMEL_US_FRAME)
474 port->icount.frame++;
475 if (status & ATMEL_US_OVRE)
476 port->icount.overrun++;
478 status &= port->read_status_mask;
480 if (status & ATMEL_US_RXBRK)
481 flg = TTY_BREAK;
482 else if (status & ATMEL_US_PARE)
483 flg = TTY_PARITY;
484 else if (status & ATMEL_US_FRAME)
485 flg = TTY_FRAME;
489 if (uart_handle_sysrq_char(port, c.ch))
490 continue;
492 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
496 * Drop the lock here since it might end up calling
497 * uart_start(), which takes the lock.
499 spin_unlock(&port->lock);
500 tty_flip_buffer_push(port->info->tty);
501 spin_lock(&port->lock);
505 * tasklet handling tty stuff outside the interrupt handler.
507 static void atmel_tasklet_func(unsigned long data)
509 struct uart_port *port = (struct uart_port *)data;
510 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
511 unsigned int status;
512 unsigned int status_change;
514 /* The interrupt handler does not take the lock */
515 spin_lock(&port->lock);
517 atmel_tx_chars(port);
519 status = atmel_port->irq_status;
520 status_change = status ^ atmel_port->irq_status_prev;
522 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
523 | ATMEL_US_DCD | ATMEL_US_CTS)) {
524 /* TODO: All reads to CSR will clear these interrupts! */
525 if (status_change & ATMEL_US_RI)
526 port->icount.rng++;
527 if (status_change & ATMEL_US_DSR)
528 port->icount.dsr++;
529 if (status_change & ATMEL_US_DCD)
530 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
531 if (status_change & ATMEL_US_CTS)
532 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
534 wake_up_interruptible(&port->info->delta_msr_wait);
536 atmel_port->irq_status_prev = status;
539 atmel_rx_from_ring(port);
541 spin_unlock(&port->lock);
545 * Perform initialization and enable port for reception
547 static int atmel_startup(struct uart_port *port)
549 int retval;
552 * Ensure that no interrupts are enabled otherwise when
553 * request_irq() is called we could get stuck trying to
554 * handle an unexpected interrupt
556 UART_PUT_IDR(port, -1);
559 * Allocate the IRQ
561 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
562 "atmel_serial", port);
563 if (retval) {
564 printk("atmel_serial: atmel_startup - Can't get irq\n");
565 return retval;
569 * If there is a specific "open" function (to register
570 * control line interrupts)
572 if (atmel_open_hook) {
573 retval = atmel_open_hook(port);
574 if (retval) {
575 free_irq(port->irq, port);
576 return retval;
581 * Finally, enable the serial port
583 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
584 /* enable xmit & rcvr */
585 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
587 /* enable receive only */
588 UART_PUT_IER(port, ATMEL_US_RXRDY);
590 return 0;
594 * Disable the port
596 static void atmel_shutdown(struct uart_port *port)
599 * Disable all interrupts, port and break condition.
601 UART_PUT_CR(port, ATMEL_US_RSTSTA);
602 UART_PUT_IDR(port, -1);
605 * Free the interrupt
607 free_irq(port->irq, port);
610 * If there is a specific "close" function (to unregister
611 * control line interrupts)
613 if (atmel_close_hook)
614 atmel_close_hook(port);
618 * Power / Clock management.
620 static void atmel_serial_pm(struct uart_port *port, unsigned int state,
621 unsigned int oldstate)
623 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
625 switch (state) {
626 case 0:
628 * Enable the peripheral clock for this serial port.
629 * This is called on uart_open() or a resume event.
631 clk_enable(atmel_port->clk);
632 break;
633 case 3:
635 * Disable the peripheral clock for this serial port.
636 * This is called on uart_close() or a suspend event.
638 clk_disable(atmel_port->clk);
639 break;
640 default:
641 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
646 * Change the port parameters
648 static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
649 struct ktermios *old)
651 unsigned long flags;
652 unsigned int mode, imr, quot, baud;
654 /* Get current mode register */
655 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
656 | ATMEL_US_NBSTOP | ATMEL_US_PAR);
658 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
659 quot = uart_get_divisor(port, baud);
661 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
662 quot /= 8;
663 mode |= ATMEL_US_USCLKS_MCK_DIV8;
666 /* byte size */
667 switch (termios->c_cflag & CSIZE) {
668 case CS5:
669 mode |= ATMEL_US_CHRL_5;
670 break;
671 case CS6:
672 mode |= ATMEL_US_CHRL_6;
673 break;
674 case CS7:
675 mode |= ATMEL_US_CHRL_7;
676 break;
677 default:
678 mode |= ATMEL_US_CHRL_8;
679 break;
682 /* stop bits */
683 if (termios->c_cflag & CSTOPB)
684 mode |= ATMEL_US_NBSTOP_2;
686 /* parity */
687 if (termios->c_cflag & PARENB) {
688 /* Mark or Space parity */
689 if (termios->c_cflag & CMSPAR) {
690 if (termios->c_cflag & PARODD)
691 mode |= ATMEL_US_PAR_MARK;
692 else
693 mode |= ATMEL_US_PAR_SPACE;
694 } else if (termios->c_cflag & PARODD)
695 mode |= ATMEL_US_PAR_ODD;
696 else
697 mode |= ATMEL_US_PAR_EVEN;
698 } else
699 mode |= ATMEL_US_PAR_NONE;
701 spin_lock_irqsave(&port->lock, flags);
703 port->read_status_mask = ATMEL_US_OVRE;
704 if (termios->c_iflag & INPCK)
705 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
706 if (termios->c_iflag & (BRKINT | PARMRK))
707 port->read_status_mask |= ATMEL_US_RXBRK;
710 * Characters to ignore
712 port->ignore_status_mask = 0;
713 if (termios->c_iflag & IGNPAR)
714 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
715 if (termios->c_iflag & IGNBRK) {
716 port->ignore_status_mask |= ATMEL_US_RXBRK;
718 * If we're ignoring parity and break indicators,
719 * ignore overruns too (for real raw support).
721 if (termios->c_iflag & IGNPAR)
722 port->ignore_status_mask |= ATMEL_US_OVRE;
724 /* TODO: Ignore all characters if CREAD is set.*/
726 /* update the per-port timeout */
727 uart_update_timeout(port, termios->c_cflag, baud);
729 /* save/disable interrupts and drain transmitter */
730 imr = UART_GET_IMR(port);
731 UART_PUT_IDR(port, -1);
732 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
733 cpu_relax();
735 /* disable receiver and transmitter */
736 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
738 /* set the parity, stop bits and data size */
739 UART_PUT_MR(port, mode);
741 /* set the baud rate */
742 UART_PUT_BRGR(port, quot);
743 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
744 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
746 /* restore interrupts */
747 UART_PUT_IER(port, imr);
749 /* CTS flow-control and modem-status interrupts */
750 if (UART_ENABLE_MS(port, termios->c_cflag))
751 port->ops->enable_ms(port);
753 spin_unlock_irqrestore(&port->lock, flags);
757 * Return string describing the specified port
759 static const char *atmel_type(struct uart_port *port)
761 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
765 * Release the memory region(s) being used by 'port'.
767 static void atmel_release_port(struct uart_port *port)
769 struct platform_device *pdev = to_platform_device(port->dev);
770 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
772 release_mem_region(port->mapbase, size);
774 if (port->flags & UPF_IOREMAP) {
775 iounmap(port->membase);
776 port->membase = NULL;
781 * Request the memory region(s) being used by 'port'.
783 static int atmel_request_port(struct uart_port *port)
785 struct platform_device *pdev = to_platform_device(port->dev);
786 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
788 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
789 return -EBUSY;
791 if (port->flags & UPF_IOREMAP) {
792 port->membase = ioremap(port->mapbase, size);
793 if (port->membase == NULL) {
794 release_mem_region(port->mapbase, size);
795 return -ENOMEM;
799 return 0;
803 * Configure/autoconfigure the port.
805 static void atmel_config_port(struct uart_port *port, int flags)
807 if (flags & UART_CONFIG_TYPE) {
808 port->type = PORT_ATMEL;
809 atmel_request_port(port);
814 * Verify the new serial_struct (for TIOCSSERIAL).
816 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
818 int ret = 0;
819 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
820 ret = -EINVAL;
821 if (port->irq != ser->irq)
822 ret = -EINVAL;
823 if (ser->io_type != SERIAL_IO_MEM)
824 ret = -EINVAL;
825 if (port->uartclk / 16 != ser->baud_base)
826 ret = -EINVAL;
827 if ((void *)port->mapbase != ser->iomem_base)
828 ret = -EINVAL;
829 if (port->iobase != ser->port)
830 ret = -EINVAL;
831 if (ser->hub6 != 0)
832 ret = -EINVAL;
833 return ret;
836 static struct uart_ops atmel_pops = {
837 .tx_empty = atmel_tx_empty,
838 .set_mctrl = atmel_set_mctrl,
839 .get_mctrl = atmel_get_mctrl,
840 .stop_tx = atmel_stop_tx,
841 .start_tx = atmel_start_tx,
842 .stop_rx = atmel_stop_rx,
843 .enable_ms = atmel_enable_ms,
844 .break_ctl = atmel_break_ctl,
845 .startup = atmel_startup,
846 .shutdown = atmel_shutdown,
847 .set_termios = atmel_set_termios,
848 .type = atmel_type,
849 .release_port = atmel_release_port,
850 .request_port = atmel_request_port,
851 .config_port = atmel_config_port,
852 .verify_port = atmel_verify_port,
853 .pm = atmel_serial_pm,
857 * Configure the port from the platform device resource info.
859 static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
860 struct platform_device *pdev)
862 struct uart_port *port = &atmel_port->uart;
863 struct atmel_uart_data *data = pdev->dev.platform_data;
865 port->iotype = UPIO_MEM;
866 port->flags = UPF_BOOT_AUTOCONF;
867 port->ops = &atmel_pops;
868 port->fifosize = 1;
869 port->line = pdev->id;
870 port->dev = &pdev->dev;
872 port->mapbase = pdev->resource[0].start;
873 port->irq = pdev->resource[1].start;
875 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
876 (unsigned long)port);
878 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
880 if (data->regs)
881 /* Already mapped by setup code */
882 port->membase = data->regs;
883 else {
884 port->flags |= UPF_IOREMAP;
885 port->membase = NULL;
888 /* for console, the clock could already be configured */
889 if (!atmel_port->clk) {
890 atmel_port->clk = clk_get(&pdev->dev, "usart");
891 clk_enable(atmel_port->clk);
892 port->uartclk = clk_get_rate(atmel_port->clk);
897 * Register board-specific modem-control line handlers.
899 void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
901 if (fns->enable_ms)
902 atmel_pops.enable_ms = fns->enable_ms;
903 if (fns->get_mctrl)
904 atmel_pops.get_mctrl = fns->get_mctrl;
905 if (fns->set_mctrl)
906 atmel_pops.set_mctrl = fns->set_mctrl;
907 atmel_open_hook = fns->open;
908 atmel_close_hook = fns->close;
909 atmel_pops.pm = fns->pm;
910 atmel_pops.set_wake = fns->set_wake;
913 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
914 static void atmel_console_putchar(struct uart_port *port, int ch)
916 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
917 cpu_relax();
918 UART_PUT_CHAR(port, ch);
922 * Interrupts are disabled on entering
924 static void atmel_console_write(struct console *co, const char *s, u_int count)
926 struct uart_port *port = &atmel_ports[co->index].uart;
927 unsigned int status, imr;
930 * First, save IMR and then disable interrupts
932 imr = UART_GET_IMR(port);
933 UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
935 uart_console_write(port, s, count, atmel_console_putchar);
938 * Finally, wait for transmitter to become empty
939 * and restore IMR
941 do {
942 status = UART_GET_CSR(port);
943 } while (!(status & ATMEL_US_TXRDY));
944 /* set interrupts back the way they were */
945 UART_PUT_IER(port, imr);
949 * If the port was already initialised (eg, by a boot loader),
950 * try to determine the current setup.
952 static void __init atmel_console_get_options(struct uart_port *port, int *baud,
953 int *parity, int *bits)
955 unsigned int mr, quot;
958 * If the baud rate generator isn't running, the port wasn't
959 * initialized by the boot loader.
961 quot = UART_GET_BRGR(port);
962 if (!quot)
963 return;
965 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
966 if (mr == ATMEL_US_CHRL_8)
967 *bits = 8;
968 else
969 *bits = 7;
971 mr = UART_GET_MR(port) & ATMEL_US_PAR;
972 if (mr == ATMEL_US_PAR_EVEN)
973 *parity = 'e';
974 else if (mr == ATMEL_US_PAR_ODD)
975 *parity = 'o';
978 * The serial core only rounds down when matching this to a
979 * supported baud rate. Make sure we don't end up slightly
980 * lower than one of those, as it would make us fall through
981 * to a much lower baud rate than we really want.
983 *baud = port->uartclk / (16 * (quot - 1));
986 static int __init atmel_console_setup(struct console *co, char *options)
988 struct uart_port *port = &atmel_ports[co->index].uart;
989 int baud = 115200;
990 int bits = 8;
991 int parity = 'n';
992 int flow = 'n';
994 if (port->membase == NULL) {
995 /* Port not initialized yet - delay setup */
996 return -ENODEV;
999 UART_PUT_IDR(port, -1);
1000 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1001 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1003 if (options)
1004 uart_parse_options(options, &baud, &parity, &bits, &flow);
1005 else
1006 atmel_console_get_options(port, &baud, &parity, &bits);
1008 return uart_set_options(port, co, baud, parity, bits, flow);
1011 static struct uart_driver atmel_uart;
1013 static struct console atmel_console = {
1014 .name = ATMEL_DEVICENAME,
1015 .write = atmel_console_write,
1016 .device = uart_console_device,
1017 .setup = atmel_console_setup,
1018 .flags = CON_PRINTBUFFER,
1019 .index = -1,
1020 .data = &atmel_uart,
1023 #define ATMEL_CONSOLE_DEVICE &atmel_console
1026 * Early console initialization (before VM subsystem initialized).
1028 static int __init atmel_console_init(void)
1030 if (atmel_default_console_device) {
1031 add_preferred_console(ATMEL_DEVICENAME,
1032 atmel_default_console_device->id, NULL);
1033 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1034 atmel_default_console_device);
1035 register_console(&atmel_console);
1038 return 0;
1041 console_initcall(atmel_console_init);
1044 * Late console initialization.
1046 static int __init atmel_late_console_init(void)
1048 if (atmel_default_console_device
1049 && !(atmel_console.flags & CON_ENABLED))
1050 register_console(&atmel_console);
1052 return 0;
1055 core_initcall(atmel_late_console_init);
1057 static inline bool atmel_is_console_port(struct uart_port *port)
1059 return port->cons && port->cons->index == port->line;
1062 #else
1063 #define ATMEL_CONSOLE_DEVICE NULL
1065 static inline bool atmel_is_console_port(struct uart_port *port)
1067 return false;
1069 #endif
1071 static struct uart_driver atmel_uart = {
1072 .owner = THIS_MODULE,
1073 .driver_name = "atmel_serial",
1074 .dev_name = ATMEL_DEVICENAME,
1075 .major = SERIAL_ATMEL_MAJOR,
1076 .minor = MINOR_START,
1077 .nr = ATMEL_MAX_UART,
1078 .cons = ATMEL_CONSOLE_DEVICE,
1081 #ifdef CONFIG_PM
1082 static int atmel_serial_suspend(struct platform_device *pdev,
1083 pm_message_t state)
1085 struct uart_port *port = platform_get_drvdata(pdev);
1086 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
1088 if (device_may_wakeup(&pdev->dev)
1089 && !at91_suspend_entering_slow_clock())
1090 enable_irq_wake(port->irq);
1091 else {
1092 uart_suspend_port(&atmel_uart, port);
1093 atmel_port->suspended = 1;
1096 return 0;
1099 static int atmel_serial_resume(struct platform_device *pdev)
1101 struct uart_port *port = platform_get_drvdata(pdev);
1102 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
1104 if (atmel_port->suspended) {
1105 uart_resume_port(&atmel_uart, port);
1106 atmel_port->suspended = 0;
1107 } else
1108 disable_irq_wake(port->irq);
1110 return 0;
1112 #else
1113 #define atmel_serial_suspend NULL
1114 #define atmel_serial_resume NULL
1115 #endif
1117 static int __devinit atmel_serial_probe(struct platform_device *pdev)
1119 struct atmel_uart_port *port;
1120 void *data;
1121 int ret;
1123 BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE));
1125 port = &atmel_ports[pdev->id];
1126 atmel_init_port(port, pdev);
1128 ret = -ENOMEM;
1129 data = kmalloc(ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
1130 if (!data)
1131 goto err_alloc_ring;
1132 port->rx_ring.buf = data;
1134 ret = uart_add_one_port(&atmel_uart, &port->uart);
1135 if (ret)
1136 goto err_add_port;
1138 device_init_wakeup(&pdev->dev, 1);
1139 platform_set_drvdata(pdev, port);
1141 return 0;
1143 err_add_port:
1144 kfree(port->rx_ring.buf);
1145 port->rx_ring.buf = NULL;
1146 err_alloc_ring:
1147 if (!atmel_is_console_port(&port->uart)) {
1148 clk_disable(port->clk);
1149 clk_put(port->clk);
1150 port->clk = NULL;
1153 return ret;
1156 static int __devexit atmel_serial_remove(struct platform_device *pdev)
1158 struct uart_port *port = platform_get_drvdata(pdev);
1159 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
1160 int ret = 0;
1162 device_init_wakeup(&pdev->dev, 0);
1163 platform_set_drvdata(pdev, NULL);
1165 ret = uart_remove_one_port(&atmel_uart, port);
1167 tasklet_kill(&atmel_port->tasklet);
1168 kfree(atmel_port->rx_ring.buf);
1170 /* "port" is allocated statically, so we shouldn't free it */
1172 clk_disable(atmel_port->clk);
1173 clk_put(atmel_port->clk);
1175 return ret;
1178 static struct platform_driver atmel_serial_driver = {
1179 .probe = atmel_serial_probe,
1180 .remove = __devexit_p(atmel_serial_remove),
1181 .suspend = atmel_serial_suspend,
1182 .resume = atmel_serial_resume,
1183 .driver = {
1184 .name = "atmel_usart",
1185 .owner = THIS_MODULE,
1189 static int __init atmel_serial_init(void)
1191 int ret;
1193 ret = uart_register_driver(&atmel_uart);
1194 if (ret)
1195 return ret;
1197 ret = platform_driver_register(&atmel_serial_driver);
1198 if (ret)
1199 uart_unregister_driver(&atmel_uart);
1201 return ret;
1204 static void __exit atmel_serial_exit(void)
1206 platform_driver_unregister(&atmel_serial_driver);
1207 uart_unregister_driver(&atmel_uart);
1210 module_init(atmel_serial_init);
1211 module_exit(atmel_serial_exit);
1213 MODULE_AUTHOR("Rick Bronson");
1214 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1215 MODULE_LICENSE("GPL");