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>
41 #include <asm/mach/serial_at91.h>
42 #include <asm/arch/board.h>
45 #include <asm/arch/cpu.h>
46 #include <asm/arch/gpio.h>
49 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
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
60 #define SERIAL_ATMEL_MAJOR 204
61 #define MINOR_START 154
62 #define ATMEL_DEVICENAME "ttyAT"
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"
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)
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
*);
107 * We wrap our port structure around the generic uart_port.
109 struct atmel_uart_port
{
110 struct uart_port uart
; /* uart */
111 struct clk
*clk
; /* uart clock */
112 unsigned short suspended
; /* is port suspended? */
113 int break_active
; /* break being received */
116 static struct atmel_uart_port atmel_ports
[ATMEL_MAX_UART
];
119 static struct console atmel_console
;
123 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
125 static u_int
atmel_tx_empty(struct uart_port
*port
)
127 return (UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
) ? TIOCSER_TEMT
: 0;
131 * Set state of the modem control output lines
133 static void atmel_set_mctrl(struct uart_port
*port
, u_int mctrl
)
135 unsigned int control
= 0;
138 #ifdef CONFIG_ARCH_AT91RM9200
139 if (cpu_is_at91rm9200()) {
141 * AT91RM9200 Errata #39: RTS0 is not internally connected
142 * to PA21. We need to drive the pin manually.
144 if (port
->mapbase
== AT91RM9200_BASE_US0
) {
145 if (mctrl
& TIOCM_RTS
)
146 at91_set_gpio_value(AT91_PIN_PA21
, 0);
148 at91_set_gpio_value(AT91_PIN_PA21
, 1);
153 if (mctrl
& TIOCM_RTS
)
154 control
|= ATMEL_US_RTSEN
;
156 control
|= ATMEL_US_RTSDIS
;
158 if (mctrl
& TIOCM_DTR
)
159 control
|= ATMEL_US_DTREN
;
161 control
|= ATMEL_US_DTRDIS
;
163 UART_PUT_CR(port
, control
);
165 /* Local loopback mode? */
166 mode
= UART_GET_MR(port
) & ~ATMEL_US_CHMODE
;
167 if (mctrl
& TIOCM_LOOP
)
168 mode
|= ATMEL_US_CHMODE_LOC_LOOP
;
170 mode
|= ATMEL_US_CHMODE_NORMAL
;
171 UART_PUT_MR(port
, mode
);
175 * Get state of the modem control input lines
177 static u_int
atmel_get_mctrl(struct uart_port
*port
)
179 unsigned int status
, ret
= 0;
181 status
= UART_GET_CSR(port
);
184 * The control signals are active low.
186 if (!(status
& ATMEL_US_DCD
))
188 if (!(status
& ATMEL_US_CTS
))
190 if (!(status
& ATMEL_US_DSR
))
192 if (!(status
& ATMEL_US_RI
))
201 static void atmel_stop_tx(struct uart_port
*port
)
203 UART_PUT_IDR(port
, ATMEL_US_TXRDY
);
207 * Start transmitting.
209 static void atmel_start_tx(struct uart_port
*port
)
211 UART_PUT_IER(port
, ATMEL_US_TXRDY
);
215 * Stop receiving - port is in process of being closed.
217 static void atmel_stop_rx(struct uart_port
*port
)
219 UART_PUT_IDR(port
, ATMEL_US_RXRDY
);
223 * Enable modem status interrupts
225 static void atmel_enable_ms(struct uart_port
*port
)
227 UART_PUT_IER(port
, ATMEL_US_RIIC
| ATMEL_US_DSRIC
228 | ATMEL_US_DCDIC
| ATMEL_US_CTSIC
);
232 * Control the transmission of a break signal
234 static void atmel_break_ctl(struct uart_port
*port
, int break_state
)
236 if (break_state
!= 0)
237 UART_PUT_CR(port
, ATMEL_US_STTBRK
); /* start break */
239 UART_PUT_CR(port
, ATMEL_US_STPBRK
); /* stop break */
243 * Characters received (called from interrupt handler)
245 static void atmel_rx_chars(struct uart_port
*port
)
247 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
248 struct tty_struct
*tty
= port
->info
->tty
;
249 unsigned int status
, ch
, flg
;
251 status
= UART_GET_CSR(port
);
252 while (status
& ATMEL_US_RXRDY
) {
253 ch
= UART_GET_CHAR(port
);
260 * note that the error handling code is
261 * out of the main execution path
263 if (unlikely(status
& (ATMEL_US_PARE
| ATMEL_US_FRAME
264 | ATMEL_US_OVRE
| ATMEL_US_RXBRK
)
265 || atmel_port
->break_active
)) {
267 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
268 if (status
& ATMEL_US_RXBRK
269 && !atmel_port
->break_active
) {
270 /* ignore side-effect */
271 status
&= ~(ATMEL_US_PARE
| ATMEL_US_FRAME
);
273 atmel_port
->break_active
= 1;
274 UART_PUT_IER(port
, ATMEL_US_RXBRK
);
275 if (uart_handle_break(port
))
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
)
300 else if (status
& ATMEL_US_PARE
)
302 else if (status
& ATMEL_US_FRAME
)
306 if (uart_handle_sysrq_char(port
, ch
))
309 uart_insert_char(port
, status
, ATMEL_US_OVRE
, ch
, flg
);
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
;
326 UART_PUT_CHAR(port
, port
->x_char
);
331 if (uart_circ_empty(xmit
) || uart_tx_stopped(port
)) {
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);
340 if (uart_circ_empty(xmit
))
344 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
345 uart_write_wakeup(port
);
347 if (uart_circ_empty(xmit
))
352 * receive interrupt handler.
355 atmel_handle_receive(struct uart_port
*port
, unsigned int pending
)
357 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
359 /* Interrupt receive */
360 if (pending
& ATMEL_US_RXRDY
)
361 atmel_rx_chars(port
);
362 else if (pending
& ATMEL_US_RXBRK
) {
364 * End of break detected. If it came along with a
365 * character, atmel_rx_chars will handle it.
367 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
368 UART_PUT_IDR(port
, ATMEL_US_RXBRK
);
369 atmel_port
->break_active
= 0;
374 * transmit interrupt handler.
377 atmel_handle_transmit(struct uart_port
*port
, unsigned int pending
)
379 /* Interrupt transmit */
380 if (pending
& ATMEL_US_TXRDY
)
381 atmel_tx_chars(port
);
385 * status flags interrupt handler.
388 atmel_handle_status(struct uart_port
*port
, unsigned int pending
,
391 /* TODO: All reads to CSR will clear these interrupts! */
392 if (pending
& ATMEL_US_RIIC
)
394 if (pending
& ATMEL_US_DSRIC
)
396 if (pending
& ATMEL_US_DCDIC
)
397 uart_handle_dcd_change(port
, !(status
& ATMEL_US_DCD
));
398 if (pending
& ATMEL_US_CTSIC
)
399 uart_handle_cts_change(port
, !(status
& ATMEL_US_CTS
));
400 if (pending
& (ATMEL_US_RIIC
| ATMEL_US_DSRIC
| ATMEL_US_DCDIC
402 wake_up_interruptible(&port
->info
->delta_msr_wait
);
408 static irqreturn_t
atmel_interrupt(int irq
, void *dev_id
)
410 struct uart_port
*port
= dev_id
;
411 unsigned int status
, pending
, pass_counter
= 0;
413 status
= UART_GET_CSR(port
);
414 pending
= status
& UART_GET_IMR(port
);
416 atmel_handle_receive(port
, pending
);
417 atmel_handle_status(port
, pending
, status
);
418 atmel_handle_transmit(port
, pending
);
420 if (pass_counter
++ > ATMEL_ISR_PASS_LIMIT
)
423 status
= UART_GET_CSR(port
);
424 pending
= status
& UART_GET_IMR(port
);
430 * Perform initialization and enable port for reception
432 static int atmel_startup(struct uart_port
*port
)
437 * Ensure that no interrupts are enabled otherwise when
438 * request_irq() is called we could get stuck trying to
439 * handle an unexpected interrupt
441 UART_PUT_IDR(port
, -1);
446 retval
= request_irq(port
->irq
, atmel_interrupt
, IRQF_SHARED
,
447 "atmel_serial", port
);
449 printk("atmel_serial: atmel_startup - Can't get irq\n");
454 * If there is a specific "open" function (to register
455 * control line interrupts)
457 if (atmel_open_hook
) {
458 retval
= atmel_open_hook(port
);
460 free_irq(port
->irq
, port
);
466 * Finally, enable the serial port
468 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
469 /* enable xmit & rcvr */
470 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
472 /* enable receive only */
473 UART_PUT_IER(port
, ATMEL_US_RXRDY
);
481 static void atmel_shutdown(struct uart_port
*port
)
484 * Disable all interrupts, port and break condition.
486 UART_PUT_CR(port
, ATMEL_US_RSTSTA
);
487 UART_PUT_IDR(port
, -1);
492 free_irq(port
->irq
, port
);
495 * If there is a specific "close" function (to unregister
496 * control line interrupts)
498 if (atmel_close_hook
)
499 atmel_close_hook(port
);
503 * Power / Clock management.
505 static void atmel_serial_pm(struct uart_port
*port
, unsigned int state
,
506 unsigned int oldstate
)
508 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
513 * Enable the peripheral clock for this serial port.
514 * This is called on uart_open() or a resume event.
516 clk_enable(atmel_port
->clk
);
520 * Disable the peripheral clock for this serial port.
521 * This is called on uart_close() or a suspend event.
523 clk_disable(atmel_port
->clk
);
526 printk(KERN_ERR
"atmel_serial: unknown pm %d\n", state
);
531 * Change the port parameters
533 static void atmel_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
534 struct ktermios
*old
)
537 unsigned int mode
, imr
, quot
, baud
;
539 /* Get current mode register */
540 mode
= UART_GET_MR(port
) & ~(ATMEL_US_USCLKS
| ATMEL_US_CHRL
541 | ATMEL_US_NBSTOP
| ATMEL_US_PAR
);
543 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/ 16);
544 quot
= uart_get_divisor(port
, baud
);
546 if (quot
> 65535) { /* BRGR is 16-bit, so switch to slower clock */
548 mode
|= ATMEL_US_USCLKS_MCK_DIV8
;
552 switch (termios
->c_cflag
& CSIZE
) {
554 mode
|= ATMEL_US_CHRL_5
;
557 mode
|= ATMEL_US_CHRL_6
;
560 mode
|= ATMEL_US_CHRL_7
;
563 mode
|= ATMEL_US_CHRL_8
;
568 if (termios
->c_cflag
& CSTOPB
)
569 mode
|= ATMEL_US_NBSTOP_2
;
572 if (termios
->c_cflag
& PARENB
) {
573 /* Mark or Space parity */
574 if (termios
->c_cflag
& CMSPAR
) {
575 if (termios
->c_cflag
& PARODD
)
576 mode
|= ATMEL_US_PAR_MARK
;
578 mode
|= ATMEL_US_PAR_SPACE
;
579 } else if (termios
->c_cflag
& PARODD
)
580 mode
|= ATMEL_US_PAR_ODD
;
582 mode
|= ATMEL_US_PAR_EVEN
;
584 mode
|= ATMEL_US_PAR_NONE
;
586 spin_lock_irqsave(&port
->lock
, flags
);
588 port
->read_status_mask
= ATMEL_US_OVRE
;
589 if (termios
->c_iflag
& INPCK
)
590 port
->read_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
591 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
592 port
->read_status_mask
|= ATMEL_US_RXBRK
;
595 * Characters to ignore
597 port
->ignore_status_mask
= 0;
598 if (termios
->c_iflag
& IGNPAR
)
599 port
->ignore_status_mask
|= (ATMEL_US_FRAME
| ATMEL_US_PARE
);
600 if (termios
->c_iflag
& IGNBRK
) {
601 port
->ignore_status_mask
|= ATMEL_US_RXBRK
;
603 * If we're ignoring parity and break indicators,
604 * ignore overruns too (for real raw support).
606 if (termios
->c_iflag
& IGNPAR
)
607 port
->ignore_status_mask
|= ATMEL_US_OVRE
;
609 /* TODO: Ignore all characters if CREAD is set.*/
611 /* update the per-port timeout */
612 uart_update_timeout(port
, termios
->c_cflag
, baud
);
614 /* save/disable interrupts and drain transmitter */
615 imr
= UART_GET_IMR(port
);
616 UART_PUT_IDR(port
, -1);
617 while (!(UART_GET_CSR(port
) & ATMEL_US_TXEMPTY
))
620 /* disable receiver and transmitter */
621 UART_PUT_CR(port
, ATMEL_US_TXDIS
| ATMEL_US_RXDIS
);
623 /* set the parity, stop bits and data size */
624 UART_PUT_MR(port
, mode
);
626 /* set the baud rate */
627 UART_PUT_BRGR(port
, quot
);
628 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
629 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
631 /* restore interrupts */
632 UART_PUT_IER(port
, imr
);
634 /* CTS flow-control and modem-status interrupts */
635 if (UART_ENABLE_MS(port
, termios
->c_cflag
))
636 port
->ops
->enable_ms(port
);
638 spin_unlock_irqrestore(&port
->lock
, flags
);
642 * Return string describing the specified port
644 static const char *atmel_type(struct uart_port
*port
)
646 return (port
->type
== PORT_ATMEL
) ? "ATMEL_SERIAL" : NULL
;
650 * Release the memory region(s) being used by 'port'.
652 static void atmel_release_port(struct uart_port
*port
)
654 struct platform_device
*pdev
= to_platform_device(port
->dev
);
655 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
657 release_mem_region(port
->mapbase
, size
);
659 if (port
->flags
& UPF_IOREMAP
) {
660 iounmap(port
->membase
);
661 port
->membase
= NULL
;
666 * Request the memory region(s) being used by 'port'.
668 static int atmel_request_port(struct uart_port
*port
)
670 struct platform_device
*pdev
= to_platform_device(port
->dev
);
671 int size
= pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1;
673 if (!request_mem_region(port
->mapbase
, size
, "atmel_serial"))
676 if (port
->flags
& UPF_IOREMAP
) {
677 port
->membase
= ioremap(port
->mapbase
, size
);
678 if (port
->membase
== NULL
) {
679 release_mem_region(port
->mapbase
, size
);
688 * Configure/autoconfigure the port.
690 static void atmel_config_port(struct uart_port
*port
, int flags
)
692 if (flags
& UART_CONFIG_TYPE
) {
693 port
->type
= PORT_ATMEL
;
694 atmel_request_port(port
);
699 * Verify the new serial_struct (for TIOCSSERIAL).
701 static int atmel_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
704 if (ser
->type
!= PORT_UNKNOWN
&& ser
->type
!= PORT_ATMEL
)
706 if (port
->irq
!= ser
->irq
)
708 if (ser
->io_type
!= SERIAL_IO_MEM
)
710 if (port
->uartclk
/ 16 != ser
->baud_base
)
712 if ((void *)port
->mapbase
!= ser
->iomem_base
)
714 if (port
->iobase
!= ser
->port
)
721 static struct uart_ops atmel_pops
= {
722 .tx_empty
= atmel_tx_empty
,
723 .set_mctrl
= atmel_set_mctrl
,
724 .get_mctrl
= atmel_get_mctrl
,
725 .stop_tx
= atmel_stop_tx
,
726 .start_tx
= atmel_start_tx
,
727 .stop_rx
= atmel_stop_rx
,
728 .enable_ms
= atmel_enable_ms
,
729 .break_ctl
= atmel_break_ctl
,
730 .startup
= atmel_startup
,
731 .shutdown
= atmel_shutdown
,
732 .set_termios
= atmel_set_termios
,
734 .release_port
= atmel_release_port
,
735 .request_port
= atmel_request_port
,
736 .config_port
= atmel_config_port
,
737 .verify_port
= atmel_verify_port
,
738 .pm
= atmel_serial_pm
,
742 * Configure the port from the platform device resource info.
744 static void __devinit
atmel_init_port(struct atmel_uart_port
*atmel_port
,
745 struct platform_device
*pdev
)
747 struct uart_port
*port
= &atmel_port
->uart
;
748 struct atmel_uart_data
*data
= pdev
->dev
.platform_data
;
750 port
->iotype
= UPIO_MEM
;
751 port
->flags
= UPF_BOOT_AUTOCONF
;
752 port
->ops
= &atmel_pops
;
754 port
->line
= pdev
->id
;
755 port
->dev
= &pdev
->dev
;
757 port
->mapbase
= pdev
->resource
[0].start
;
758 port
->irq
= pdev
->resource
[1].start
;
761 /* Already mapped by setup code */
762 port
->membase
= data
->regs
;
764 port
->flags
|= UPF_IOREMAP
;
765 port
->membase
= NULL
;
768 /* for console, the clock could already be configured */
769 if (!atmel_port
->clk
) {
770 atmel_port
->clk
= clk_get(&pdev
->dev
, "usart");
771 clk_enable(atmel_port
->clk
);
772 port
->uartclk
= clk_get_rate(atmel_port
->clk
);
777 * Register board-specific modem-control line handlers.
779 void __init
atmel_register_uart_fns(struct atmel_port_fns
*fns
)
782 atmel_pops
.enable_ms
= fns
->enable_ms
;
784 atmel_pops
.get_mctrl
= fns
->get_mctrl
;
786 atmel_pops
.set_mctrl
= fns
->set_mctrl
;
787 atmel_open_hook
= fns
->open
;
788 atmel_close_hook
= fns
->close
;
789 atmel_pops
.pm
= fns
->pm
;
790 atmel_pops
.set_wake
= fns
->set_wake
;
793 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
794 static void atmel_console_putchar(struct uart_port
*port
, int ch
)
796 while (!(UART_GET_CSR(port
) & ATMEL_US_TXRDY
))
798 UART_PUT_CHAR(port
, ch
);
802 * Interrupts are disabled on entering
804 static void atmel_console_write(struct console
*co
, const char *s
, u_int count
)
806 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
807 unsigned int status
, imr
;
810 * First, save IMR and then disable interrupts
812 imr
= UART_GET_IMR(port
);
813 UART_PUT_IDR(port
, ATMEL_US_RXRDY
| ATMEL_US_TXRDY
);
815 uart_console_write(port
, s
, count
, atmel_console_putchar
);
818 * Finally, wait for transmitter to become empty
822 status
= UART_GET_CSR(port
);
823 } while (!(status
& ATMEL_US_TXRDY
));
824 /* set interrupts back the way they were */
825 UART_PUT_IER(port
, imr
);
829 * If the port was already initialised (eg, by a boot loader),
830 * try to determine the current setup.
832 static void __init
atmel_console_get_options(struct uart_port
*port
, int *baud
,
833 int *parity
, int *bits
)
835 unsigned int mr
, quot
;
838 * If the baud rate generator isn't running, the port wasn't
839 * initialized by the boot loader.
841 quot
= UART_GET_BRGR(port
);
845 mr
= UART_GET_MR(port
) & ATMEL_US_CHRL
;
846 if (mr
== ATMEL_US_CHRL_8
)
851 mr
= UART_GET_MR(port
) & ATMEL_US_PAR
;
852 if (mr
== ATMEL_US_PAR_EVEN
)
854 else if (mr
== ATMEL_US_PAR_ODD
)
858 * The serial core only rounds down when matching this to a
859 * supported baud rate. Make sure we don't end up slightly
860 * lower than one of those, as it would make us fall through
861 * to a much lower baud rate than we really want.
863 *baud
= port
->uartclk
/ (16 * (quot
- 1));
866 static int __init
atmel_console_setup(struct console
*co
, char *options
)
868 struct uart_port
*port
= &atmel_ports
[co
->index
].uart
;
874 if (port
->membase
== NULL
) {
875 /* Port not initialized yet - delay setup */
879 UART_PUT_IDR(port
, -1);
880 UART_PUT_CR(port
, ATMEL_US_RSTSTA
| ATMEL_US_RSTRX
);
881 UART_PUT_CR(port
, ATMEL_US_TXEN
| ATMEL_US_RXEN
);
884 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
886 atmel_console_get_options(port
, &baud
, &parity
, &bits
);
888 return uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
891 static struct uart_driver atmel_uart
;
893 static struct console atmel_console
= {
894 .name
= ATMEL_DEVICENAME
,
895 .write
= atmel_console_write
,
896 .device
= uart_console_device
,
897 .setup
= atmel_console_setup
,
898 .flags
= CON_PRINTBUFFER
,
903 #define ATMEL_CONSOLE_DEVICE &atmel_console
906 * Early console initialization (before VM subsystem initialized).
908 static int __init
atmel_console_init(void)
910 if (atmel_default_console_device
) {
911 add_preferred_console(ATMEL_DEVICENAME
,
912 atmel_default_console_device
->id
, NULL
);
913 atmel_init_port(&atmel_ports
[atmel_default_console_device
->id
],
914 atmel_default_console_device
);
915 register_console(&atmel_console
);
921 console_initcall(atmel_console_init
);
924 * Late console initialization.
926 static int __init
atmel_late_console_init(void)
928 if (atmel_default_console_device
929 && !(atmel_console
.flags
& CON_ENABLED
))
930 register_console(&atmel_console
);
935 core_initcall(atmel_late_console_init
);
937 static inline bool atmel_is_console_port(struct uart_port
*port
)
939 return port
->cons
&& port
->cons
->index
== port
->line
;
943 #define ATMEL_CONSOLE_DEVICE NULL
945 static inline bool atmel_is_console_port(struct uart_port
*port
)
951 static struct uart_driver atmel_uart
= {
952 .owner
= THIS_MODULE
,
953 .driver_name
= "atmel_serial",
954 .dev_name
= ATMEL_DEVICENAME
,
955 .major
= SERIAL_ATMEL_MAJOR
,
956 .minor
= MINOR_START
,
957 .nr
= ATMEL_MAX_UART
,
958 .cons
= ATMEL_CONSOLE_DEVICE
,
962 static int atmel_serial_suspend(struct platform_device
*pdev
,
965 struct uart_port
*port
= platform_get_drvdata(pdev
);
966 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
968 if (device_may_wakeup(&pdev
->dev
)
969 && !at91_suspend_entering_slow_clock())
970 enable_irq_wake(port
->irq
);
972 uart_suspend_port(&atmel_uart
, port
);
973 atmel_port
->suspended
= 1;
979 static int atmel_serial_resume(struct platform_device
*pdev
)
981 struct uart_port
*port
= platform_get_drvdata(pdev
);
982 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
984 if (atmel_port
->suspended
) {
985 uart_resume_port(&atmel_uart
, port
);
986 atmel_port
->suspended
= 0;
988 disable_irq_wake(port
->irq
);
993 #define atmel_serial_suspend NULL
994 #define atmel_serial_resume NULL
997 static int __devinit
atmel_serial_probe(struct platform_device
*pdev
)
999 struct atmel_uart_port
*port
;
1002 port
= &atmel_ports
[pdev
->id
];
1003 atmel_init_port(port
, pdev
);
1005 ret
= uart_add_one_port(&atmel_uart
, &port
->uart
);
1009 device_init_wakeup(&pdev
->dev
, 1);
1010 platform_set_drvdata(pdev
, port
);
1015 if (!atmel_is_console_port(&port
->uart
)) {
1016 clk_disable(port
->clk
);
1024 static int __devexit
atmel_serial_remove(struct platform_device
*pdev
)
1026 struct uart_port
*port
= platform_get_drvdata(pdev
);
1027 struct atmel_uart_port
*atmel_port
= (struct atmel_uart_port
*)port
;
1030 device_init_wakeup(&pdev
->dev
, 0);
1031 platform_set_drvdata(pdev
, NULL
);
1033 ret
= uart_remove_one_port(&atmel_uart
, port
);
1035 /* "port" is allocated statically, so we shouldn't free it */
1037 clk_disable(atmel_port
->clk
);
1038 clk_put(atmel_port
->clk
);
1043 static struct platform_driver atmel_serial_driver
= {
1044 .probe
= atmel_serial_probe
,
1045 .remove
= __devexit_p(atmel_serial_remove
),
1046 .suspend
= atmel_serial_suspend
,
1047 .resume
= atmel_serial_resume
,
1049 .name
= "atmel_usart",
1050 .owner
= THIS_MODULE
,
1054 static int __init
atmel_serial_init(void)
1058 ret
= uart_register_driver(&atmel_uart
);
1062 ret
= platform_driver_register(&atmel_serial_driver
);
1064 uart_unregister_driver(&atmel_uart
);
1069 static void __exit
atmel_serial_exit(void)
1071 platform_driver_unregister(&atmel_serial_driver
);
1072 uart_unregister_driver(&atmel_uart
);
1075 module_init(atmel_serial_init
);
1076 module_exit(atmel_serial_exit
);
1078 MODULE_AUTHOR("Rick Bronson");
1079 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
1080 MODULE_LICENSE("GPL");