blob: e06c6c8f4dd878dffa8fb28c90a0baebe73cd643 [file] [log] [blame]
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001/*
Andrew Victorc2f5ccf2006-12-04 09:37:20 +01002 * linux/drivers/char/atmel_serial.c
Andrew Victor1e6c9c22006-01-10 16:59:27 +00003 *
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02004 * Driver for Atmel AT91 / AT32 Serial ports
Andrew Victor1e6c9c22006-01-10 16:59:27 +00005 * Copyright (C) 2003 Rick Bronson
6 *
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.
9 *
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.
14 *
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.
19 *
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
23 *
24 */
Andrew Victor1e6c9c22006-01-10 16:59:27 +000025#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>
Andrew Victorafefc412006-06-19 19:53:19 +010031#include <linux/clk.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000032#include <linux/console.h>
33#include <linux/sysrq.h>
34#include <linux/tty_flip.h>
Andrew Victorafefc412006-06-19 19:53:19 +010035#include <linux/platform_device.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010036#include <linux/atmel_pdc.h>
Guennadi Liakhovetskifa3218d2008-01-29 15:43:13 +010037#include <linux/atmel_serial.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000038
39#include <asm/io.h>
40
Andrew Victorafefc412006-06-19 19:53:19 +010041#include <asm/mach/serial_at91.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000042#include <asm/arch/board.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010043
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +020044#ifdef CONFIG_ARM
Andrew Victorc2f5ccf2006-12-04 09:37:20 +010045#include <asm/arch/cpu.h>
Andrew Victor20e65272006-07-05 14:31:13 +010046#include <asm/arch/gpio.h>
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +020047#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +000048
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020049#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000050#define SUPPORT_SYSRQ
51#endif
52
53#include <linux/serial_core.h>
54
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020055#ifdef CONFIG_SERIAL_ATMEL_TTYAT
Andrew Victor1e6c9c22006-01-10 16:59:27 +000056
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. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020060#define SERIAL_ATMEL_MAJOR 204
Andrew Victor1e6c9c22006-01-10 16:59:27 +000061#define MINOR_START 154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020062#define ATMEL_DEVICENAME "ttyAT"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000063
64#else
65
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. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020068#define SERIAL_ATMEL_MAJOR TTY_MAJOR
Andrew Victor1e6c9c22006-01-10 16:59:27 +000069#define MINOR_START 64
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020070#define ATMEL_DEVICENAME "ttyS"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000071
72#endif
73
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020074#define ATMEL_ISR_PASS_LIMIT 256
Andrew Victor1e6c9c22006-01-10 16:59:27 +000075
Remy Bohmerb843aa22008-02-08 04:21:01 -080076/* UART registers. CR is write-only, hence no GET macro */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -080077#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)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000089
Andrew Victor1e6c9c22006-01-10 16:59:27 +000090 /* PDC registers */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -080091#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)
Andrew Victorafefc412006-06-19 19:53:19 +010093
Haavard Skinnemoen544fc722007-02-12 00:52:15 -080094#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)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000099
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800100#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)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000102
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200103static int (*atmel_open_hook)(struct uart_port *);
104static void (*atmel_close_hook)(struct uart_port *);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000105
Andrew Victorafefc412006-06-19 19:53:19 +0100106/*
107 * We wrap our port structure around the generic uart_port.
108 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200109struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100110 struct uart_port uart; /* uart */
111 struct clk *clk; /* uart clock */
112 unsigned short suspended; /* is port suspended? */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700113 int break_active; /* break being received */
Andrew Victorafefc412006-06-19 19:53:19 +0100114};
115
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200116static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Andrew Victorafefc412006-06-19 19:53:19 +0100117
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000118#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200119static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000120#endif
121
122/*
123 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
124 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200125static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000126{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200127 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000128}
129
130/*
131 * Set state of the modem control output lines
132 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200133static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000134{
135 unsigned int control = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100136 unsigned int mode;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000137
Andrew Victorc2f5ccf2006-12-04 09:37:20 +0100138#ifdef CONFIG_ARCH_AT91RM9200
Andrew Victor79da7a62006-11-30 11:58:14 +0100139 if (cpu_is_at91rm9200()) {
Andrew Victorafefc412006-06-19 19:53:19 +0100140 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800141 * AT91RM9200 Errata #39: RTS0 is not internally connected
142 * to PA21. We need to drive the pin manually.
Andrew Victorafefc412006-06-19 19:53:19 +0100143 */
Andrew Victor72729912006-09-27 09:44:11 +0100144 if (port->mapbase == AT91RM9200_BASE_US0) {
Andrew Victorafefc412006-06-19 19:53:19 +0100145 if (mctrl & TIOCM_RTS)
Andrew Victor20e65272006-07-05 14:31:13 +0100146 at91_set_gpio_value(AT91_PIN_PA21, 0);
Andrew Victorafefc412006-06-19 19:53:19 +0100147 else
Andrew Victor20e65272006-07-05 14:31:13 +0100148 at91_set_gpio_value(AT91_PIN_PA21, 1);
Andrew Victorafefc412006-06-19 19:53:19 +0100149 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000150 }
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +0200151#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000152
153 if (mctrl & TIOCM_RTS)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200154 control |= ATMEL_US_RTSEN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000155 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200156 control |= ATMEL_US_RTSDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000157
158 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200159 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000160 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200161 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000162
Andrew Victorafefc412006-06-19 19:53:19 +0100163 UART_PUT_CR(port, control);
164
165 /* Local loopback mode? */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200166 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100167 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200168 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100169 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200170 mode |= ATMEL_US_CHMODE_NORMAL;
Andrew Victorafefc412006-06-19 19:53:19 +0100171 UART_PUT_MR(port, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000172}
173
174/*
175 * Get state of the modem control input lines
176 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200177static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000178{
179 unsigned int status, ret = 0;
180
181 status = UART_GET_CSR(port);
182
183 /*
184 * The control signals are active low.
185 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200186 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000187 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200188 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000189 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200190 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000191 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200192 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000193 ret |= TIOCM_RI;
194
195 return ret;
196}
197
198/*
199 * Stop transmitting.
200 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200201static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000202{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200203 UART_PUT_IDR(port, ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000204}
205
206/*
207 * Start transmitting.
208 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200209static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000210{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200211 UART_PUT_IER(port, ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000212}
213
214/*
215 * Stop receiving - port is in process of being closed.
216 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200217static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000218{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200219 UART_PUT_IDR(port, ATMEL_US_RXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000220}
221
222/*
223 * Enable modem status interrupts
224 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200225static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000226{
Remy Bohmerb843aa22008-02-08 04:21:01 -0800227 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
228 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000229}
230
231/*
232 * Control the transmission of a break signal
233 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200234static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000235{
236 if (break_state != 0)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200237 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000238 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200239 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000240}
241
242/*
243 * Characters received (called from interrupt handler)
244 */
David Howells7d12e782006-10-05 14:55:46 +0100245static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000246{
Remy Bohmerb843aa22008-02-08 04:21:01 -0800247 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000248 struct tty_struct *tty = port->info->tty;
249 unsigned int status, ch, flg;
250
Andrew Victorafefc412006-06-19 19:53:19 +0100251 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200252 while (status & ATMEL_US_RXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000253 ch = UART_GET_CHAR(port);
254
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000255 port->icount.rx++;
256
257 flg = TTY_NORMAL;
258
259 /*
260 * note that the error handling code is
261 * out of the main execution path
262 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700263 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
264 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
265 || atmel_port->break_active)) {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800266 /* clear error */
267 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700268 if (status & ATMEL_US_RXBRK
269 && !atmel_port->break_active) {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800270 /* ignore side-effect */
271 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
Andrew Victorafefc412006-06-19 19:53:19 +0100272 port->icount.brk++;
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700273 atmel_port->break_active = 1;
274 UART_PUT_IER(port, ATMEL_US_RXBRK);
Andrew Victorafefc412006-06-19 19:53:19 +0100275 if (uart_handle_break(port))
276 goto ignore_char;
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700277 } else {
278 /*
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.
284 */
285 UART_PUT_IDR(port, ATMEL_US_RXBRK);
286 status &= ~ATMEL_US_RXBRK;
287 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100288 }
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200289 if (status & ATMEL_US_PARE)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000290 port->icount.parity++;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200291 if (status & ATMEL_US_FRAME)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000292 port->icount.frame++;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200293 if (status & ATMEL_US_OVRE)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000294 port->icount.overrun++;
295
Andrew Victorafefc412006-06-19 19:53:19 +0100296 status &= port->read_status_mask;
297
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200298 if (status & ATMEL_US_RXBRK)
Andrew Victorafefc412006-06-19 19:53:19 +0100299 flg = TTY_BREAK;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200300 else if (status & ATMEL_US_PARE)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000301 flg = TTY_PARITY;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200302 else if (status & ATMEL_US_FRAME)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000303 flg = TTY_FRAME;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000304 }
305
David Howells7d12e782006-10-05 14:55:46 +0100306 if (uart_handle_sysrq_char(port, ch))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000307 goto ignore_char;
308
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200309 uart_insert_char(port, status, ATMEL_US_OVRE, ch, flg);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000310
Remy Bohmerb843aa22008-02-08 04:21:01 -0800311ignore_char:
Andrew Victorafefc412006-06-19 19:53:19 +0100312 status = UART_GET_CSR(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000313 }
314
315 tty_flip_buffer_push(tty);
316}
317
318/*
319 * Transmit characters (called from interrupt handler)
320 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200321static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000322{
323 struct circ_buf *xmit = &port->info->xmit;
324
325 if (port->x_char) {
326 UART_PUT_CHAR(port, port->x_char);
327 port->icount.tx++;
328 port->x_char = 0;
329 return;
330 }
331 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200332 atmel_stop_tx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000333 return;
334 }
335
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200336 while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000337 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;
342 }
343
344 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
345 uart_write_wakeup(port);
346
347 if (uart_circ_empty(xmit))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200348 atmel_stop_tx(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000349}
350
351/*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800352 * receive interrupt handler.
353 */
354static void
355atmel_handle_receive(struct uart_port *port, unsigned int pending)
356{
357 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
358
359 /* Interrupt receive */
360 if (pending & ATMEL_US_RXRDY)
361 atmel_rx_chars(port);
362 else if (pending & ATMEL_US_RXBRK) {
363 /*
364 * End of break detected. If it came along with a
365 * character, atmel_rx_chars will handle it.
366 */
367 UART_PUT_CR(port, ATMEL_US_RSTSTA);
368 UART_PUT_IDR(port, ATMEL_US_RXBRK);
369 atmel_port->break_active = 0;
370 }
371}
372
373/*
374 * transmit interrupt handler.
375 */
376static void
377atmel_handle_transmit(struct uart_port *port, unsigned int pending)
378{
379 /* Interrupt transmit */
380 if (pending & ATMEL_US_TXRDY)
381 atmel_tx_chars(port);
382}
383
384/*
385 * status flags interrupt handler.
386 */
387static void
388atmel_handle_status(struct uart_port *port, unsigned int pending,
389 unsigned int status)
390{
391 /* TODO: All reads to CSR will clear these interrupts! */
392 if (pending & ATMEL_US_RIIC)
393 port->icount.rng++;
394 if (pending & ATMEL_US_DSRIC)
395 port->icount.dsr++;
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
401 | ATMEL_US_CTSIC))
402 wake_up_interruptible(&port->info->delta_msr_wait);
403}
404
405/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000406 * Interrupt handler
407 */
David Howells7d12e782006-10-05 14:55:46 +0100408static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000409{
410 struct uart_port *port = dev_id;
411 unsigned int status, pending, pass_counter = 0;
412
413 status = UART_GET_CSR(port);
Andrew Victorafefc412006-06-19 19:53:19 +0100414 pending = status & UART_GET_IMR(port);
415 while (pending) {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800416 atmel_handle_receive(port, pending);
417 atmel_handle_status(port, pending, status);
418 atmel_handle_transmit(port, pending);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000419
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200420 if (pass_counter++ > ATMEL_ISR_PASS_LIMIT)
Andrew Victorafefc412006-06-19 19:53:19 +0100421 break;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000422
Andrew Victorafefc412006-06-19 19:53:19 +0100423 status = UART_GET_CSR(port);
424 pending = status & UART_GET_IMR(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000425 }
426 return IRQ_HANDLED;
427}
428
429/*
430 * Perform initialization and enable port for reception
431 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200432static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000433{
434 int retval;
435
436 /*
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
440 */
441 UART_PUT_IDR(port, -1);
442
443 /*
444 * Allocate the IRQ
445 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800446 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
447 "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000448 if (retval) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200449 printk("atmel_serial: atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000450 return retval;
451 }
452
453 /*
454 * If there is a specific "open" function (to register
455 * control line interrupts)
456 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200457 if (atmel_open_hook) {
458 retval = atmel_open_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000459 if (retval) {
460 free_irq(port->irq, port);
461 return retval;
462 }
463 }
464
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000465 /*
466 * Finally, enable the serial port
467 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200468 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800469 /* enable xmit & rcvr */
470 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +0100471
Remy Bohmerb843aa22008-02-08 04:21:01 -0800472 /* enable receive only */
473 UART_PUT_IER(port, ATMEL_US_RXRDY);
Andrew Victorafefc412006-06-19 19:53:19 +0100474
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000475 return 0;
476}
477
478/*
479 * Disable the port
480 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200481static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000482{
483 /*
484 * Disable all interrupts, port and break condition.
485 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200486 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000487 UART_PUT_IDR(port, -1);
488
489 /*
490 * Free the interrupt
491 */
492 free_irq(port->irq, port);
493
494 /*
495 * If there is a specific "close" function (to unregister
496 * control line interrupts)
497 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200498 if (atmel_close_hook)
499 atmel_close_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000500}
501
502/*
503 * Power / Clock management.
504 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800505static void atmel_serial_pm(struct uart_port *port, unsigned int state,
506 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000507{
Remy Bohmerb843aa22008-02-08 04:21:01 -0800508 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
Andrew Victorafefc412006-06-19 19:53:19 +0100509
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000510 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800511 case 0:
512 /*
513 * Enable the peripheral clock for this serial port.
514 * This is called on uart_open() or a resume event.
515 */
516 clk_enable(atmel_port->clk);
517 break;
518 case 3:
519 /*
520 * Disable the peripheral clock for this serial port.
521 * This is called on uart_close() or a suspend event.
522 */
523 clk_disable(atmel_port->clk);
524 break;
525 default:
526 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000527 }
528}
529
530/*
531 * Change the port parameters
532 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800533static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
534 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000535{
536 unsigned long flags;
537 unsigned int mode, imr, quot, baud;
538
Andrew Victor03abeac2007-05-03 12:26:24 +0100539 /* Get current mode register */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800540 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
541 | ATMEL_US_NBSTOP | ATMEL_US_PAR);
Andrew Victor03abeac2007-05-03 12:26:24 +0100542
Remy Bohmerb843aa22008-02-08 04:21:01 -0800543 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000544 quot = uart_get_divisor(port, baud);
545
Remy Bohmerb843aa22008-02-08 04:21:01 -0800546 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +0100547 quot /= 8;
548 mode |= ATMEL_US_USCLKS_MCK_DIV8;
549 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000550
551 /* byte size */
552 switch (termios->c_cflag & CSIZE) {
553 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200554 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000555 break;
556 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200557 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000558 break;
559 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200560 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000561 break;
562 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200563 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000564 break;
565 }
566
567 /* stop bits */
568 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200569 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000570
571 /* parity */
572 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800573 /* Mark or Space parity */
574 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000575 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200576 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000577 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200578 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -0800579 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200580 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000581 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200582 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -0800583 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200584 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000585
586 spin_lock_irqsave(&port->lock, flags);
587
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200588 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000589 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200590 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000591 if (termios->c_iflag & (BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200592 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000593
594 /*
595 * Characters to ignore
596 */
597 port->ignore_status_mask = 0;
598 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200599 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000600 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200601 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000602 /*
603 * If we're ignoring parity and break indicators,
604 * ignore overruns too (for real raw support).
605 */
606 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200607 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000608 }
Remy Bohmerb843aa22008-02-08 04:21:01 -0800609 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000610
611 /* update the per-port timeout */
612 uart_update_timeout(port, termios->c_cflag, baud);
613
Remy Bohmerb843aa22008-02-08 04:21:01 -0800614 /* 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))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -0800618 cpu_relax();
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000619
620 /* disable receiver and transmitter */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200621 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000622
623 /* set the parity, stop bits and data size */
624 UART_PUT_MR(port, mode);
625
626 /* set the baud rate */
627 UART_PUT_BRGR(port, quot);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200628 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
629 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000630
631 /* restore interrupts */
632 UART_PUT_IER(port, imr);
633
634 /* CTS flow-control and modem-status interrupts */
635 if (UART_ENABLE_MS(port, termios->c_cflag))
636 port->ops->enable_ms(port);
637
638 spin_unlock_irqrestore(&port->lock, flags);
639}
640
641/*
642 * Return string describing the specified port
643 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200644static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000645{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +0200646 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000647}
648
649/*
650 * Release the memory region(s) being used by 'port'.
651 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200652static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000653{
Andrew Victorafefc412006-06-19 19:53:19 +0100654 struct platform_device *pdev = to_platform_device(port->dev);
655 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
656
657 release_mem_region(port->mapbase, size);
658
659 if (port->flags & UPF_IOREMAP) {
660 iounmap(port->membase);
661 port->membase = NULL;
662 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000663}
664
665/*
666 * Request the memory region(s) being used by 'port'.
667 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200668static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000669{
Andrew Victorafefc412006-06-19 19:53:19 +0100670 struct platform_device *pdev = to_platform_device(port->dev);
671 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000672
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200673 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +0100674 return -EBUSY;
675
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);
680 return -ENOMEM;
681 }
682 }
683
684 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000685}
686
687/*
688 * Configure/autoconfigure the port.
689 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200690static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000691{
692 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +0200693 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200694 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000695 }
696}
697
698/*
699 * Verify the new serial_struct (for TIOCSSERIAL).
700 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200701static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000702{
703 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +0200704 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000705 ret = -EINVAL;
706 if (port->irq != ser->irq)
707 ret = -EINVAL;
708 if (ser->io_type != SERIAL_IO_MEM)
709 ret = -EINVAL;
710 if (port->uartclk / 16 != ser->baud_base)
711 ret = -EINVAL;
712 if ((void *)port->mapbase != ser->iomem_base)
713 ret = -EINVAL;
714 if (port->iobase != ser->port)
715 ret = -EINVAL;
716 if (ser->hub6 != 0)
717 ret = -EINVAL;
718 return ret;
719}
720
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200721static 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,
733 .type = atmel_type,
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,
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000739};
740
Andrew Victorafefc412006-06-19 19:53:19 +0100741/*
742 * Configure the port from the platform device resource info.
743 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800744static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
745 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000746{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200747 struct uart_port *port = &atmel_port->uart;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +0200748 struct atmel_uart_data *data = pdev->dev.platform_data;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000749
Andrew Victorafefc412006-06-19 19:53:19 +0100750 port->iotype = UPIO_MEM;
Andrew Victora14d5272007-01-09 09:03:42 +0100751 port->flags = UPF_BOOT_AUTOCONF;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200752 port->ops = &atmel_pops;
Andrew Victora14d5272007-01-09 09:03:42 +0100753 port->fifosize = 1;
Andrew Victorafefc412006-06-19 19:53:19 +0100754 port->line = pdev->id;
755 port->dev = &pdev->dev;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000756
Andrew Victorafefc412006-06-19 19:53:19 +0100757 port->mapbase = pdev->resource[0].start;
758 port->irq = pdev->resource[1].start;
759
Haavard Skinnemoen75d35212006-10-04 16:02:08 +0200760 if (data->regs)
761 /* Already mapped by setup code */
762 port->membase = data->regs;
Andrew Victorafefc412006-06-19 19:53:19 +0100763 else {
764 port->flags |= UPF_IOREMAP;
765 port->membase = NULL;
766 }
767
Remy Bohmerb843aa22008-02-08 04:21:01 -0800768 /* for console, the clock could already be configured */
769 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200770 atmel_port->clk = clk_get(&pdev->dev, "usart");
771 clk_enable(atmel_port->clk);
772 port->uartclk = clk_get_rate(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +0100773 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000774}
775
Andrew Victorafefc412006-06-19 19:53:19 +0100776/*
777 * Register board-specific modem-control line handlers.
778 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200779void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000780{
781 if (fns->enable_ms)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200782 atmel_pops.enable_ms = fns->enable_ms;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000783 if (fns->get_mctrl)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200784 atmel_pops.get_mctrl = fns->get_mctrl;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000785 if (fns->set_mctrl)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200786 atmel_pops.set_mctrl = fns->set_mctrl;
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200787 atmel_open_hook = fns->open;
788 atmel_close_hook = fns->close;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200789 atmel_pops.pm = fns->pm;
790 atmel_pops.set_wake = fns->set_wake;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000791}
792
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +0200793#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200794static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +0000795{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200796 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -0800797 cpu_relax();
Russell Kingd3587882006-03-20 20:00:09 +0000798 UART_PUT_CHAR(port, ch);
799}
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000800
801/*
802 * Interrupts are disabled on entering
803 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200804static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000805{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200806 struct uart_port *port = &atmel_ports[co->index].uart;
Russell Kingd3587882006-03-20 20:00:09 +0000807 unsigned int status, imr;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000808
809 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800810 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000811 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800812 imr = UART_GET_IMR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200813 UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000814
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200815 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000816
817 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800818 * Finally, wait for transmitter to become empty
819 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000820 */
821 do {
822 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200823 } while (!(status & ATMEL_US_TXRDY));
Remy Bohmerb843aa22008-02-08 04:21:01 -0800824 /* set interrupts back the way they were */
825 UART_PUT_IER(port, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000826}
827
828/*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800829 * If the port was already initialised (eg, by a boot loader),
830 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000831 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800832static void __init atmel_console_get_options(struct uart_port *port, int *baud,
833 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000834{
835 unsigned int mr, quot;
836
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -0800837 /*
838 * If the baud rate generator isn't running, the port wasn't
839 * initialized by the boot loader.
840 */
841 quot = UART_GET_BRGR(port);
842 if (!quot)
843 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000844
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200845 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
846 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000847 *bits = 8;
848 else
849 *bits = 7;
850
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200851 mr = UART_GET_MR(port) & ATMEL_US_PAR;
852 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000853 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200854 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000855 *parity = 'o';
856
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +0200857 /*
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.
862 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +0200863 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000864}
865
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200866static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000867{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200868 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000869 int baud = 115200;
870 int bits = 8;
871 int parity = 'n';
872 int flow = 'n';
873
Remy Bohmerb843aa22008-02-08 04:21:01 -0800874 if (port->membase == NULL) {
875 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +0100876 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -0800877 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000878
Remy Bohmerb843aa22008-02-08 04:21:01 -0800879 UART_PUT_IDR(port, -1);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200880 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
881 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000882
883 if (options)
884 uart_parse_options(options, &baud, &parity, &bits, &flow);
885 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200886 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000887
888 return uart_set_options(port, co, baud, parity, bits, flow);
889}
890
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200891static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000892
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200893static struct console atmel_console = {
894 .name = ATMEL_DEVICENAME,
895 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000896 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200897 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000898 .flags = CON_PRINTBUFFER,
899 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200900 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000901};
902
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200903#define ATMEL_CONSOLE_DEVICE &atmel_console
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000904
Andrew Victorafefc412006-06-19 19:53:19 +0100905/*
906 * Early console initialization (before VM subsystem initialized).
907 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200908static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000909{
Haavard Skinnemoen73e27982006-10-04 16:02:04 +0200910 if (atmel_default_console_device) {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800911 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);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200915 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +0100916 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000917
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000918 return 0;
919}
Remy Bohmerb843aa22008-02-08 04:21:01 -0800920
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200921console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000922
Andrew Victorafefc412006-06-19 19:53:19 +0100923/*
924 * Late console initialization.
925 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200926static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +0100927{
Remy Bohmerb843aa22008-02-08 04:21:01 -0800928 if (atmel_default_console_device
929 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200930 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +0100931
932 return 0;
933}
Remy Bohmerb843aa22008-02-08 04:21:01 -0800934
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200935core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +0100936
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -0800937static inline bool atmel_is_console_port(struct uart_port *port)
938{
939 return port->cons && port->cons->index == port->line;
940}
941
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000942#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200943#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -0800944
945static inline bool atmel_is_console_port(struct uart_port *port)
946{
947 return false;
948}
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000949#endif
950
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200951static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800952 .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,
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000959};
960
Andrew Victorafefc412006-06-19 19:53:19 +0100961#ifdef CONFIG_PM
Remy Bohmerb843aa22008-02-08 04:21:01 -0800962static int atmel_serial_suspend(struct platform_device *pdev,
963 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000964{
Andrew Victorafefc412006-06-19 19:53:19 +0100965 struct uart_port *port = platform_get_drvdata(pdev);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800966 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000967
Remy Bohmerb843aa22008-02-08 04:21:01 -0800968 if (device_may_wakeup(&pdev->dev)
969 && !at91_suspend_entering_slow_clock())
Andrew Victorafefc412006-06-19 19:53:19 +0100970 enable_irq_wake(port->irq);
971 else {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200972 uart_suspend_port(&atmel_uart, port);
973 atmel_port->suspended = 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000974 }
975
976 return 0;
977}
978
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200979static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +0100980{
981 struct uart_port *port = platform_get_drvdata(pdev);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800982 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
Andrew Victorafefc412006-06-19 19:53:19 +0100983
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200984 if (atmel_port->suspended) {
985 uart_resume_port(&atmel_uart, port);
986 atmel_port->suspended = 0;
Remy Bohmerb843aa22008-02-08 04:21:01 -0800987 } else
Andrew Victor9b938162007-01-09 13:20:54 +0100988 disable_irq_wake(port->irq);
Andrew Victorafefc412006-06-19 19:53:19 +0100989
990 return 0;
991}
992#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200993#define atmel_serial_suspend NULL
994#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +0100995#endif
996
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200997static int __devinit atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +0100998{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200999 struct atmel_uart_port *port;
Andrew Victorafefc412006-06-19 19:53:19 +01001000 int ret;
1001
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001002 port = &atmel_ports[pdev->id];
1003 atmel_init_port(port, pdev);
Andrew Victorafefc412006-06-19 19:53:19 +01001004
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001005 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001006 if (ret)
1007 goto err_add_port;
1008
1009 device_init_wakeup(&pdev->dev, 1);
1010 platform_set_drvdata(pdev, port);
1011
1012 return 0;
1013
1014err_add_port:
1015 if (!atmel_is_console_port(&port->uart)) {
1016 clk_disable(port->clk);
1017 clk_put(port->clk);
1018 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01001019 }
1020
1021 return ret;
1022}
1023
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001024static int __devexit atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01001025{
1026 struct uart_port *port = platform_get_drvdata(pdev);
Remy Bohmerb843aa22008-02-08 04:21:01 -08001027 struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
Andrew Victorafefc412006-06-19 19:53:19 +01001028 int ret = 0;
1029
Andrew Victorafefc412006-06-19 19:53:19 +01001030 device_init_wakeup(&pdev->dev, 0);
1031 platform_set_drvdata(pdev, NULL);
1032
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001033 ret = uart_remove_one_port(&atmel_uart, port);
1034
1035 /* "port" is allocated statically, so we shouldn't free it */
1036
1037 clk_disable(atmel_port->clk);
1038 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01001039
1040 return ret;
1041}
1042
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001043static 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,
Andrew Victorafefc412006-06-19 19:53:19 +01001048 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02001049 .name = "atmel_usart",
Andrew Victorafefc412006-06-19 19:53:19 +01001050 .owner = THIS_MODULE,
1051 },
1052};
1053
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001054static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01001055{
1056 int ret;
1057
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001058 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01001059 if (ret)
1060 return ret;
1061
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001062 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01001063 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001064 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01001065
1066 return ret;
1067}
1068
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001069static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001070{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001071 platform_driver_unregister(&atmel_serial_driver);
1072 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001073}
1074
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001075module_init(atmel_serial_init);
1076module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001077
1078MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001079MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001080MODULE_LICENSE("GPL");