wait_task_zombie: remove ->exit_state/exit_signal checks for WNOWAIT
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / atmel_serial.c
blob60f52904aad0fc4bfe7142d348c35a434dac1731
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 #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
77 #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
78 #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
79 #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
80 #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
81 #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
82 #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
83 #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
84 #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
85 #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
86 #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
87 #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
89 // #define UART_GET_CR(port) __raw_readl((port)->membase + ATMEL_US_CR) // is write-only
91 /* PDC registers */
92 #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
93 #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
95 #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
96 #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
97 #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
98 #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
99 #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
101 #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
102 #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
103 //#define UART_PUT_TNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TNPR)
104 //#define UART_PUT_TNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TNCR)
106 static int (*atmel_open_hook)(struct uart_port *);
107 static void (*atmel_close_hook)(struct uart_port *);
110 * We wrap our port structure around the generic uart_port.
112 struct atmel_uart_port {
113 struct uart_port uart; /* uart */
114 struct clk *clk; /* uart clock */
115 unsigned short suspended; /* is port suspended? */
116 int break_active; /* break being received */
119 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
121 #ifdef SUPPORT_SYSRQ
122 static struct console atmel_console;
123 #endif
126 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
128 static u_int atmel_tx_empty(struct uart_port *port)
130 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
134 * Set state of the modem control output lines
136 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
138 unsigned int control = 0;
139 unsigned int mode;
141 #ifdef CONFIG_ARCH_AT91RM9200
142 if (cpu_is_at91rm9200()) {
144 * AT91RM9200 Errata #39: RTS0 is not internally connected to PA21.
145 * We need to drive the pin manually.
147 if (port->mapbase == AT91RM9200_BASE_US0) {
148 if (mctrl & TIOCM_RTS)
149 at91_set_gpio_value(AT91_PIN_PA21, 0);
150 else
151 at91_set_gpio_value(AT91_PIN_PA21, 1);
154 #endif
156 if (mctrl & TIOCM_RTS)
157 control |= ATMEL_US_RTSEN;
158 else
159 control |= ATMEL_US_RTSDIS;
161 if (mctrl & TIOCM_DTR)
162 control |= ATMEL_US_DTREN;
163 else
164 control |= ATMEL_US_DTRDIS;
166 UART_PUT_CR(port, control);
168 /* Local loopback mode? */
169 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
170 if (mctrl & TIOCM_LOOP)
171 mode |= ATMEL_US_CHMODE_LOC_LOOP;
172 else
173 mode |= ATMEL_US_CHMODE_NORMAL;
174 UART_PUT_MR(port, mode);
178 * Get state of the modem control input lines
180 static u_int atmel_get_mctrl(struct uart_port *port)
182 unsigned int status, ret = 0;
184 status = UART_GET_CSR(port);
187 * The control signals are active low.
189 if (!(status & ATMEL_US_DCD))
190 ret |= TIOCM_CD;
191 if (!(status & ATMEL_US_CTS))
192 ret |= TIOCM_CTS;
193 if (!(status & ATMEL_US_DSR))
194 ret |= TIOCM_DSR;
195 if (!(status & ATMEL_US_RI))
196 ret |= TIOCM_RI;
198 return ret;
202 * Stop transmitting.
204 static void atmel_stop_tx(struct uart_port *port)
206 UART_PUT_IDR(port, ATMEL_US_TXRDY);
210 * Start transmitting.
212 static void atmel_start_tx(struct uart_port *port)
214 UART_PUT_IER(port, ATMEL_US_TXRDY);
218 * Stop receiving - port is in process of being closed.
220 static void atmel_stop_rx(struct uart_port *port)
222 UART_PUT_IDR(port, ATMEL_US_RXRDY);
226 * Enable modem status interrupts
228 static void atmel_enable_ms(struct uart_port *port)
230 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
234 * Control the transmission of a break signal
236 static void atmel_break_ctl(struct uart_port *port, int break_state)
238 if (break_state != 0)
239 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
240 else
241 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
245 * Characters received (called from interrupt handler)
247 static void atmel_rx_chars(struct uart_port *port)
249 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
250 struct tty_struct *tty = port->info->tty;
251 unsigned int status, ch, flg;
253 status = UART_GET_CSR(port);
254 while (status & ATMEL_US_RXRDY) {
255 ch = UART_GET_CHAR(port);
257 port->icount.rx++;
259 flg = TTY_NORMAL;
262 * note that the error handling code is
263 * out of the main execution path
265 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
266 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
267 || atmel_port->break_active)) {
268 UART_PUT_CR(port, ATMEL_US_RSTSTA); /* clear error */
269 if (status & ATMEL_US_RXBRK
270 && !atmel_port->break_active) {
271 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); /* ignore side-effect */
272 port->icount.brk++;
273 atmel_port->break_active = 1;
274 UART_PUT_IER(port, ATMEL_US_RXBRK);
275 if (uart_handle_break(port))
276 goto ignore_char;
277 } else {
279 * This is either the end-of-break
280 * condition or we've received at
281 * least one character without RXBRK
282 * being set. In both cases, the next
283 * RXBRK will indicate start-of-break.
285 UART_PUT_IDR(port, ATMEL_US_RXBRK);
286 status &= ~ATMEL_US_RXBRK;
287 atmel_port->break_active = 0;
289 if (status & ATMEL_US_PARE)
290 port->icount.parity++;
291 if (status & ATMEL_US_FRAME)
292 port->icount.frame++;
293 if (status & ATMEL_US_OVRE)
294 port->icount.overrun++;
296 status &= port->read_status_mask;
298 if (status & ATMEL_US_RXBRK)
299 flg = TTY_BREAK;
300 else if (status & ATMEL_US_PARE)
301 flg = TTY_PARITY;
302 else if (status & ATMEL_US_FRAME)
303 flg = TTY_FRAME;
306 if (uart_handle_sysrq_char(port, ch))
307 goto ignore_char;
309 uart_insert_char(port, status, ATMEL_US_OVRE, ch, flg);
311 ignore_char:
312 status = UART_GET_CSR(port);
315 tty_flip_buffer_push(tty);
319 * Transmit characters (called from interrupt handler)
321 static void atmel_tx_chars(struct uart_port *port)
323 struct circ_buf *xmit = &port->info->xmit;
325 if (port->x_char) {
326 UART_PUT_CHAR(port, port->x_char);
327 port->icount.tx++;
328 port->x_char = 0;
329 return;
331 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
332 atmel_stop_tx(port);
333 return;
336 while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
337 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
338 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
339 port->icount.tx++;
340 if (uart_circ_empty(xmit))
341 break;
344 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
345 uart_write_wakeup(port);
347 if (uart_circ_empty(xmit))
348 atmel_stop_tx(port);
352 * Interrupt handler
354 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
356 struct uart_port *port = dev_id;
357 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
358 unsigned int status, pending, pass_counter = 0;
360 status = UART_GET_CSR(port);
361 pending = status & UART_GET_IMR(port);
362 while (pending) {
363 /* Interrupt receive */
364 if (pending & ATMEL_US_RXRDY)
365 atmel_rx_chars(port);
366 else if (pending & ATMEL_US_RXBRK) {
368 * End of break detected. If it came along
369 * with a character, atmel_rx_chars will
370 * handle it.
372 UART_PUT_CR(port, ATMEL_US_RSTSTA);
373 UART_PUT_IDR(port, ATMEL_US_RXBRK);
374 atmel_port->break_active = 0;
377 // TODO: All reads to CSR will clear these interrupts!
378 if (pending & ATMEL_US_RIIC) port->icount.rng++;
379 if (pending & ATMEL_US_DSRIC) port->icount.dsr++;
380 if (pending & ATMEL_US_DCDIC)
381 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
382 if (pending & ATMEL_US_CTSIC)
383 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
384 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC))
385 wake_up_interruptible(&port->info->delta_msr_wait);
387 /* Interrupt transmit */
388 if (pending & ATMEL_US_TXRDY)
389 atmel_tx_chars(port);
391 if (pass_counter++ > ATMEL_ISR_PASS_LIMIT)
392 break;
394 status = UART_GET_CSR(port);
395 pending = status & UART_GET_IMR(port);
397 return IRQ_HANDLED;
401 * Perform initialization and enable port for reception
403 static int atmel_startup(struct uart_port *port)
405 int retval;
408 * Ensure that no interrupts are enabled otherwise when
409 * request_irq() is called we could get stuck trying to
410 * handle an unexpected interrupt
412 UART_PUT_IDR(port, -1);
415 * Allocate the IRQ
417 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, "atmel_serial", port);
418 if (retval) {
419 printk("atmel_serial: atmel_startup - Can't get irq\n");
420 return retval;
424 * If there is a specific "open" function (to register
425 * control line interrupts)
427 if (atmel_open_hook) {
428 retval = atmel_open_hook(port);
429 if (retval) {
430 free_irq(port->irq, port);
431 return retval;
436 * Finally, enable the serial port
438 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
439 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); /* enable xmit & rcvr */
441 UART_PUT_IER(port, ATMEL_US_RXRDY); /* enable receive only */
443 return 0;
447 * Disable the port
449 static void atmel_shutdown(struct uart_port *port)
452 * Disable all interrupts, port and break condition.
454 UART_PUT_CR(port, ATMEL_US_RSTSTA);
455 UART_PUT_IDR(port, -1);
458 * Free the interrupt
460 free_irq(port->irq, port);
463 * If there is a specific "close" function (to unregister
464 * control line interrupts)
466 if (atmel_close_hook)
467 atmel_close_hook(port);
471 * Power / Clock management.
473 static void atmel_serial_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
475 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
477 switch (state) {
478 case 0:
480 * Enable the peripheral clock for this serial port.
481 * This is called on uart_open() or a resume event.
483 clk_enable(atmel_port->clk);
484 break;
485 case 3:
487 * Disable the peripheral clock for this serial port.
488 * This is called on uart_close() or a suspend event.
490 clk_disable(atmel_port->clk);
491 break;
492 default:
493 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
498 * Change the port parameters
500 static void atmel_set_termios(struct uart_port *port, struct ktermios * termios, struct ktermios * old)
502 unsigned long flags;
503 unsigned int mode, imr, quot, baud;
505 /* Get current mode register */
506 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | ATMEL_US_PAR);
508 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
509 quot = uart_get_divisor(port, baud);
511 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
512 quot /= 8;
513 mode |= ATMEL_US_USCLKS_MCK_DIV8;
516 /* byte size */
517 switch (termios->c_cflag & CSIZE) {
518 case CS5:
519 mode |= ATMEL_US_CHRL_5;
520 break;
521 case CS6:
522 mode |= ATMEL_US_CHRL_6;
523 break;
524 case CS7:
525 mode |= ATMEL_US_CHRL_7;
526 break;
527 default:
528 mode |= ATMEL_US_CHRL_8;
529 break;
532 /* stop bits */
533 if (termios->c_cflag & CSTOPB)
534 mode |= ATMEL_US_NBSTOP_2;
536 /* parity */
537 if (termios->c_cflag & PARENB) {
538 if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
539 if (termios->c_cflag & PARODD)
540 mode |= ATMEL_US_PAR_MARK;
541 else
542 mode |= ATMEL_US_PAR_SPACE;
544 else if (termios->c_cflag & PARODD)
545 mode |= ATMEL_US_PAR_ODD;
546 else
547 mode |= ATMEL_US_PAR_EVEN;
549 else
550 mode |= ATMEL_US_PAR_NONE;
552 spin_lock_irqsave(&port->lock, flags);
554 port->read_status_mask = ATMEL_US_OVRE;
555 if (termios->c_iflag & INPCK)
556 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
557 if (termios->c_iflag & (BRKINT | PARMRK))
558 port->read_status_mask |= ATMEL_US_RXBRK;
561 * Characters to ignore
563 port->ignore_status_mask = 0;
564 if (termios->c_iflag & IGNPAR)
565 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
566 if (termios->c_iflag & IGNBRK) {
567 port->ignore_status_mask |= ATMEL_US_RXBRK;
569 * If we're ignoring parity and break indicators,
570 * ignore overruns too (for real raw support).
572 if (termios->c_iflag & IGNPAR)
573 port->ignore_status_mask |= ATMEL_US_OVRE;
576 // TODO: Ignore all characters if CREAD is set.
578 /* update the per-port timeout */
579 uart_update_timeout(port, termios->c_cflag, baud);
581 /* disable interrupts and drain transmitter */
582 imr = UART_GET_IMR(port); /* get interrupt mask */
583 UART_PUT_IDR(port, -1); /* disable all interrupts */
584 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) { barrier(); }
586 /* disable receiver and transmitter */
587 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
589 /* set the parity, stop bits and data size */
590 UART_PUT_MR(port, mode);
592 /* set the baud rate */
593 UART_PUT_BRGR(port, quot);
594 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
595 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
597 /* restore interrupts */
598 UART_PUT_IER(port, imr);
600 /* CTS flow-control and modem-status interrupts */
601 if (UART_ENABLE_MS(port, termios->c_cflag))
602 port->ops->enable_ms(port);
604 spin_unlock_irqrestore(&port->lock, flags);
608 * Return string describing the specified port
610 static const char *atmel_type(struct uart_port *port)
612 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
616 * Release the memory region(s) being used by 'port'.
618 static void atmel_release_port(struct uart_port *port)
620 struct platform_device *pdev = to_platform_device(port->dev);
621 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
623 release_mem_region(port->mapbase, size);
625 if (port->flags & UPF_IOREMAP) {
626 iounmap(port->membase);
627 port->membase = NULL;
632 * Request the memory region(s) being used by 'port'.
634 static int atmel_request_port(struct uart_port *port)
636 struct platform_device *pdev = to_platform_device(port->dev);
637 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
639 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
640 return -EBUSY;
642 if (port->flags & UPF_IOREMAP) {
643 port->membase = ioremap(port->mapbase, size);
644 if (port->membase == NULL) {
645 release_mem_region(port->mapbase, size);
646 return -ENOMEM;
650 return 0;
654 * Configure/autoconfigure the port.
656 static void atmel_config_port(struct uart_port *port, int flags)
658 if (flags & UART_CONFIG_TYPE) {
659 port->type = PORT_ATMEL;
660 atmel_request_port(port);
665 * Verify the new serial_struct (for TIOCSSERIAL).
667 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
669 int ret = 0;
670 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
671 ret = -EINVAL;
672 if (port->irq != ser->irq)
673 ret = -EINVAL;
674 if (ser->io_type != SERIAL_IO_MEM)
675 ret = -EINVAL;
676 if (port->uartclk / 16 != ser->baud_base)
677 ret = -EINVAL;
678 if ((void *)port->mapbase != ser->iomem_base)
679 ret = -EINVAL;
680 if (port->iobase != ser->port)
681 ret = -EINVAL;
682 if (ser->hub6 != 0)
683 ret = -EINVAL;
684 return ret;
687 static struct uart_ops atmel_pops = {
688 .tx_empty = atmel_tx_empty,
689 .set_mctrl = atmel_set_mctrl,
690 .get_mctrl = atmel_get_mctrl,
691 .stop_tx = atmel_stop_tx,
692 .start_tx = atmel_start_tx,
693 .stop_rx = atmel_stop_rx,
694 .enable_ms = atmel_enable_ms,
695 .break_ctl = atmel_break_ctl,
696 .startup = atmel_startup,
697 .shutdown = atmel_shutdown,
698 .set_termios = atmel_set_termios,
699 .type = atmel_type,
700 .release_port = atmel_release_port,
701 .request_port = atmel_request_port,
702 .config_port = atmel_config_port,
703 .verify_port = atmel_verify_port,
704 .pm = atmel_serial_pm,
708 * Configure the port from the platform device resource info.
710 static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, struct platform_device *pdev)
712 struct uart_port *port = &atmel_port->uart;
713 struct atmel_uart_data *data = pdev->dev.platform_data;
715 port->iotype = UPIO_MEM;
716 port->flags = UPF_BOOT_AUTOCONF;
717 port->ops = &atmel_pops;
718 port->fifosize = 1;
719 port->line = pdev->id;
720 port->dev = &pdev->dev;
722 port->mapbase = pdev->resource[0].start;
723 port->irq = pdev->resource[1].start;
725 if (data->regs)
726 /* Already mapped by setup code */
727 port->membase = data->regs;
728 else {
729 port->flags |= UPF_IOREMAP;
730 port->membase = NULL;
733 if (!atmel_port->clk) { /* for console, the clock could already be configured */
734 atmel_port->clk = clk_get(&pdev->dev, "usart");
735 clk_enable(atmel_port->clk);
736 port->uartclk = clk_get_rate(atmel_port->clk);
741 * Register board-specific modem-control line handlers.
743 void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
745 if (fns->enable_ms)
746 atmel_pops.enable_ms = fns->enable_ms;
747 if (fns->get_mctrl)
748 atmel_pops.get_mctrl = fns->get_mctrl;
749 if (fns->set_mctrl)
750 atmel_pops.set_mctrl = fns->set_mctrl;
751 atmel_open_hook = fns->open;
752 atmel_close_hook = fns->close;
753 atmel_pops.pm = fns->pm;
754 atmel_pops.set_wake = fns->set_wake;
758 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
759 static void atmel_console_putchar(struct uart_port *port, int ch)
761 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
762 barrier();
763 UART_PUT_CHAR(port, ch);
767 * Interrupts are disabled on entering
769 static void atmel_console_write(struct console *co, const char *s, u_int count)
771 struct uart_port *port = &atmel_ports[co->index].uart;
772 unsigned int status, imr;
775 * First, save IMR and then disable interrupts
777 imr = UART_GET_IMR(port); /* get interrupt mask */
778 UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
780 uart_console_write(port, s, count, atmel_console_putchar);
783 * Finally, wait for transmitter to become empty
784 * and restore IMR
786 do {
787 status = UART_GET_CSR(port);
788 } while (!(status & ATMEL_US_TXRDY));
789 UART_PUT_IER(port, imr); /* set interrupts back the way they were */
793 * If the port was already initialised (eg, by a boot loader), try to determine
794 * the current setup.
796 static void __init atmel_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
798 unsigned int mr, quot;
800 // TODO: CR is a write-only register
801 // unsigned int cr;
803 // cr = UART_GET_CR(port) & (ATMEL_US_RXEN | ATMEL_US_TXEN);
804 // if (cr == (ATMEL_US_RXEN | ATMEL_US_TXEN)) {
805 // /* ok, the port was enabled */
806 // }
808 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
809 if (mr == ATMEL_US_CHRL_8)
810 *bits = 8;
811 else
812 *bits = 7;
814 mr = UART_GET_MR(port) & ATMEL_US_PAR;
815 if (mr == ATMEL_US_PAR_EVEN)
816 *parity = 'e';
817 else if (mr == ATMEL_US_PAR_ODD)
818 *parity = 'o';
821 * The serial core only rounds down when matching this to a
822 * supported baud rate. Make sure we don't end up slightly
823 * lower than one of those, as it would make us fall through
824 * to a much lower baud rate than we really want.
826 quot = UART_GET_BRGR(port);
827 *baud = port->uartclk / (16 * (quot - 1));
830 static int __init atmel_console_setup(struct console *co, char *options)
832 struct uart_port *port = &atmel_ports[co->index].uart;
833 int baud = 115200;
834 int bits = 8;
835 int parity = 'n';
836 int flow = 'n';
838 if (port->membase == 0) /* Port not initialized yet - delay setup */
839 return -ENODEV;
841 UART_PUT_IDR(port, -1); /* disable interrupts */
842 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
843 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
845 if (options)
846 uart_parse_options(options, &baud, &parity, &bits, &flow);
847 else
848 atmel_console_get_options(port, &baud, &parity, &bits);
850 return uart_set_options(port, co, baud, parity, bits, flow);
853 static struct uart_driver atmel_uart;
855 static struct console atmel_console = {
856 .name = ATMEL_DEVICENAME,
857 .write = atmel_console_write,
858 .device = uart_console_device,
859 .setup = atmel_console_setup,
860 .flags = CON_PRINTBUFFER,
861 .index = -1,
862 .data = &atmel_uart,
865 #define ATMEL_CONSOLE_DEVICE &atmel_console
868 * Early console initialization (before VM subsystem initialized).
870 static int __init atmel_console_init(void)
872 if (atmel_default_console_device) {
873 add_preferred_console(ATMEL_DEVICENAME, atmel_default_console_device->id, NULL);
874 atmel_init_port(&(atmel_ports[atmel_default_console_device->id]), atmel_default_console_device);
875 register_console(&atmel_console);
878 return 0;
880 console_initcall(atmel_console_init);
883 * Late console initialization.
885 static int __init atmel_late_console_init(void)
887 if (atmel_default_console_device && !(atmel_console.flags & CON_ENABLED))
888 register_console(&atmel_console);
890 return 0;
892 core_initcall(atmel_late_console_init);
894 #else
895 #define ATMEL_CONSOLE_DEVICE NULL
896 #endif
898 static struct uart_driver atmel_uart = {
899 .owner = THIS_MODULE,
900 .driver_name = "atmel_serial",
901 .dev_name = ATMEL_DEVICENAME,
902 .major = SERIAL_ATMEL_MAJOR,
903 .minor = MINOR_START,
904 .nr = ATMEL_MAX_UART,
905 .cons = ATMEL_CONSOLE_DEVICE,
908 #ifdef CONFIG_PM
909 static int atmel_serial_suspend(struct platform_device *pdev, pm_message_t state)
911 struct uart_port *port = platform_get_drvdata(pdev);
912 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
914 if (device_may_wakeup(&pdev->dev) && !at91_suspend_entering_slow_clock())
915 enable_irq_wake(port->irq);
916 else {
917 uart_suspend_port(&atmel_uart, port);
918 atmel_port->suspended = 1;
921 return 0;
924 static int atmel_serial_resume(struct platform_device *pdev)
926 struct uart_port *port = platform_get_drvdata(pdev);
927 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
929 if (atmel_port->suspended) {
930 uart_resume_port(&atmel_uart, port);
931 atmel_port->suspended = 0;
933 else
934 disable_irq_wake(port->irq);
936 return 0;
938 #else
939 #define atmel_serial_suspend NULL
940 #define atmel_serial_resume NULL
941 #endif
943 static int __devinit atmel_serial_probe(struct platform_device *pdev)
945 struct atmel_uart_port *port;
946 int ret;
948 port = &atmel_ports[pdev->id];
949 atmel_init_port(port, pdev);
951 ret = uart_add_one_port(&atmel_uart, &port->uart);
952 if (!ret) {
953 device_init_wakeup(&pdev->dev, 1);
954 platform_set_drvdata(pdev, port);
957 return ret;
960 static int __devexit atmel_serial_remove(struct platform_device *pdev)
962 struct uart_port *port = platform_get_drvdata(pdev);
963 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
964 int ret = 0;
966 clk_disable(atmel_port->clk);
967 clk_put(atmel_port->clk);
969 device_init_wakeup(&pdev->dev, 0);
970 platform_set_drvdata(pdev, NULL);
972 if (port) {
973 ret = uart_remove_one_port(&atmel_uart, port);
974 kfree(port);
977 return ret;
980 static struct platform_driver atmel_serial_driver = {
981 .probe = atmel_serial_probe,
982 .remove = __devexit_p(atmel_serial_remove),
983 .suspend = atmel_serial_suspend,
984 .resume = atmel_serial_resume,
985 .driver = {
986 .name = "atmel_usart",
987 .owner = THIS_MODULE,
991 static int __init atmel_serial_init(void)
993 int ret;
995 ret = uart_register_driver(&atmel_uart);
996 if (ret)
997 return ret;
999 ret = platform_driver_register(&atmel_serial_driver);
1000 if (ret)
1001 uart_unregister_driver(&atmel_uart);
1003 return ret;
1006 static void __exit atmel_serial_exit(void)
1008 platform_driver_unregister(&atmel_serial_driver);
1009 uart_unregister_driver(&atmel_uart);
1012 module_init(atmel_serial_init);
1013 module_exit(atmel_serial_exit);
1015 MODULE_AUTHOR("Rick Bronson");
1016 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1017 MODULE_LICENSE("GPL");