blob: 3a6da80b081c14405ecfdd60660470928843e84c [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 *
Chip Coldwella6670612008-02-08 04:21:06 -080010 * DMA support added by Chip Coldwell.
11 *
Andrew Victor1e6c9c22006-01-10 16:59:27 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
Andrew Victor1e6c9c22006-01-10 16:59:27 +000027#include <linux/module.h>
28#include <linux/tty.h>
29#include <linux/ioport.h>
30#include <linux/slab.h>
31#include <linux/init.h>
32#include <linux/serial.h>
Andrew Victorafefc412006-06-19 19:53:19 +010033#include <linux/clk.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000034#include <linux/console.h>
35#include <linux/sysrq.h>
36#include <linux/tty_flip.h>
Andrew Victorafefc412006-06-19 19:53:19 +010037#include <linux/platform_device.h>
Chip Coldwella6670612008-02-08 04:21:06 -080038#include <linux/dma-mapping.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010039#include <linux/atmel_pdc.h>
Guennadi Liakhovetskifa3218d2008-01-29 15:43:13 +010040#include <linux/atmel_serial.h>
Andrew Victor1e6c9c22006-01-10 16:59:27 +000041
42#include <asm/io.h>
43
Andrew Victorafefc412006-06-19 19:53:19 +010044#include <asm/mach/serial_at91.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010045#include <mach/board.h>
Andrew Victor93a3ddc2007-02-08 11:31:22 +010046
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +020047#ifdef CONFIG_ARM
Russell Kinga09e64f2008-08-05 16:14:15 +010048#include <mach/cpu.h>
49#include <mach/gpio.h>
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +020050#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +000051
Chip Coldwella6670612008-02-08 04:21:06 -080052#define PDC_BUFFER_SIZE 512
53/* Revisit: We should calculate this based on the actual port settings */
54#define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
55
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020056#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000057#define SUPPORT_SYSRQ
58#endif
59
60#include <linux/serial_core.h>
61
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +020062#ifdef CONFIG_SERIAL_ATMEL_TTYAT
Andrew Victor1e6c9c22006-01-10 16:59:27 +000063
64/* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
65 * should coexist with the 8250 driver, such as if we have an external 16C550
66 * UART. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020067#define SERIAL_ATMEL_MAJOR 204
Andrew Victor1e6c9c22006-01-10 16:59:27 +000068#define MINOR_START 154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020069#define ATMEL_DEVICENAME "ttyAT"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000070
71#else
72
73/* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
74 * name, but it is legally reserved for the 8250 driver. */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020075#define SERIAL_ATMEL_MAJOR TTY_MAJOR
Andrew Victor1e6c9c22006-01-10 16:59:27 +000076#define MINOR_START 64
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020077#define ATMEL_DEVICENAME "ttyS"
Andrew Victor1e6c9c22006-01-10 16:59:27 +000078
79#endif
80
Haavard Skinnemoen7192f922006-10-04 16:02:05 +020081#define ATMEL_ISR_PASS_LIMIT 256
Andrew Victor1e6c9c22006-01-10 16:59:27 +000082
Remy Bohmerb843aa22008-02-08 04:21:01 -080083/* UART registers. CR is write-only, hence no GET macro */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -080084#define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
85#define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
86#define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
87#define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
88#define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
89#define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
90#define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
91#define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
92#define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
93#define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
94#define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
95#define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +000096
Andrew Victor1e6c9c22006-01-10 16:59:27 +000097 /* PDC registers */
Haavard Skinnemoen544fc722007-02-12 00:52:15 -080098#define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
99#define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
Andrew Victorafefc412006-06-19 19:53:19 +0100100
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800101#define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
102#define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
103#define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
104#define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
105#define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000106
Haavard Skinnemoen544fc722007-02-12 00:52:15 -0800107#define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
108#define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
Marc Pignat39d4c922008-04-02 13:04:42 -0700109#define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000110
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200111static int (*atmel_open_hook)(struct uart_port *);
112static void (*atmel_close_hook)(struct uart_port *);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000113
Chip Coldwella6670612008-02-08 04:21:06 -0800114struct atmel_dma_buffer {
115 unsigned char *buf;
116 dma_addr_t dma_addr;
117 unsigned int dma_size;
118 unsigned int ofs;
119};
120
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800121struct atmel_uart_char {
122 u16 status;
123 u16 ch;
124};
125
126#define ATMEL_SERIAL_RINGSIZE 1024
127
Andrew Victorafefc412006-06-19 19:53:19 +0100128/*
129 * We wrap our port structure around the generic uart_port.
130 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200131struct atmel_uart_port {
Andrew Victorafefc412006-06-19 19:53:19 +0100132 struct uart_port uart; /* uart */
133 struct clk *clk; /* uart clock */
134 unsigned short suspended; /* is port suspended? */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700135 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800136
Chip Coldwella6670612008-02-08 04:21:06 -0800137 short use_dma_rx; /* enable PDC receiver */
138 short pdc_rx_idx; /* current PDC RX buffer */
139 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
140
141 short use_dma_tx; /* enable PDC transmitter */
142 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
143
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800144 struct tasklet_struct tasklet;
145 unsigned int irq_status;
146 unsigned int irq_status_prev;
147
148 struct circ_buf rx_ring;
Andrew Victorafefc412006-06-19 19:53:19 +0100149};
150
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200151static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Andrew Victorafefc412006-06-19 19:53:19 +0100152
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000153#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200154static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000155#endif
156
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800157static inline struct atmel_uart_port *
158to_atmel_uart_port(struct uart_port *uart)
159{
160 return container_of(uart, struct atmel_uart_port, uart);
161}
162
Chip Coldwella6670612008-02-08 04:21:06 -0800163#ifdef CONFIG_SERIAL_ATMEL_PDC
164static bool atmel_use_dma_rx(struct uart_port *port)
165{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800166 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800167
168 return atmel_port->use_dma_rx;
169}
170
171static bool atmel_use_dma_tx(struct uart_port *port)
172{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800173 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800174
175 return atmel_port->use_dma_tx;
176}
177#else
178static bool atmel_use_dma_rx(struct uart_port *port)
179{
180 return false;
181}
182
183static bool atmel_use_dma_tx(struct uart_port *port)
184{
185 return false;
186}
187#endif
188
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000189/*
190 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
191 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200192static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000193{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200194 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000195}
196
197/*
198 * Set state of the modem control output lines
199 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200200static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000201{
202 unsigned int control = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100203 unsigned int mode;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000204
Andrew Victorc2f5ccf2006-12-04 09:37:20 +0100205#ifdef CONFIG_ARCH_AT91RM9200
Andrew Victor79da7a62006-11-30 11:58:14 +0100206 if (cpu_is_at91rm9200()) {
Andrew Victorafefc412006-06-19 19:53:19 +0100207 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800208 * AT91RM9200 Errata #39: RTS0 is not internally connected
209 * to PA21. We need to drive the pin manually.
Andrew Victorafefc412006-06-19 19:53:19 +0100210 */
Andrew Victor72729912006-09-27 09:44:11 +0100211 if (port->mapbase == AT91RM9200_BASE_US0) {
Andrew Victorafefc412006-06-19 19:53:19 +0100212 if (mctrl & TIOCM_RTS)
Andrew Victor20e65272006-07-05 14:31:13 +0100213 at91_set_gpio_value(AT91_PIN_PA21, 0);
Andrew Victorafefc412006-06-19 19:53:19 +0100214 else
Andrew Victor20e65272006-07-05 14:31:13 +0100215 at91_set_gpio_value(AT91_PIN_PA21, 1);
Andrew Victorafefc412006-06-19 19:53:19 +0100216 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000217 }
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +0200218#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000219
220 if (mctrl & TIOCM_RTS)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200221 control |= ATMEL_US_RTSEN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000222 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200223 control |= ATMEL_US_RTSDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000224
225 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200226 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000227 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200228 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000229
Andrew Victorafefc412006-06-19 19:53:19 +0100230 UART_PUT_CR(port, control);
231
232 /* Local loopback mode? */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200233 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100234 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200235 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100236 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200237 mode |= ATMEL_US_CHMODE_NORMAL;
Andrew Victorafefc412006-06-19 19:53:19 +0100238 UART_PUT_MR(port, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000239}
240
241/*
242 * Get state of the modem control input lines
243 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200244static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000245{
246 unsigned int status, ret = 0;
247
248 status = UART_GET_CSR(port);
249
250 /*
251 * The control signals are active low.
252 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200253 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000254 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200255 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000256 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200257 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000258 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200259 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000260 ret |= TIOCM_RI;
261
262 return ret;
263}
264
265/*
266 * Stop transmitting.
267 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200268static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000269{
Chip Coldwella6670612008-02-08 04:21:06 -0800270 if (atmel_use_dma_tx(port)) {
271 /* disable PDC transmit */
272 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
273 UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
274 } else
275 UART_PUT_IDR(port, ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000276}
277
278/*
279 * Start transmitting.
280 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200281static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000282{
Chip Coldwella6670612008-02-08 04:21:06 -0800283 if (atmel_use_dma_tx(port)) {
284 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
285 /* The transmitter is already running. Yes, we
286 really need this.*/
287 return;
288
289 UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
290 /* re-enable PDC transmit */
291 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
292 } else
293 UART_PUT_IER(port, ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000294}
295
296/*
297 * Stop receiving - port is in process of being closed.
298 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200299static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000300{
Chip Coldwella6670612008-02-08 04:21:06 -0800301 if (atmel_use_dma_rx(port)) {
302 /* disable PDC receive */
303 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
304 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
305 } else
306 UART_PUT_IDR(port, ATMEL_US_RXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000307}
308
309/*
310 * Enable modem status interrupts
311 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200312static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000313{
Remy Bohmerb843aa22008-02-08 04:21:01 -0800314 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
315 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000316}
317
318/*
319 * Control the transmission of a break signal
320 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200321static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000322{
323 if (break_state != 0)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200324 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000325 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200326 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000327}
328
329/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800330 * Stores the incoming character in the ring buffer
331 */
332static void
333atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
334 unsigned int ch)
335{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800336 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800337 struct circ_buf *ring = &atmel_port->rx_ring;
338 struct atmel_uart_char *c;
339
340 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
341 /* Buffer overflow, ignore char */
342 return;
343
344 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
345 c->status = status;
346 c->ch = ch;
347
348 /* Make sure the character is stored before we update head. */
349 smp_wmb();
350
351 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
352}
353
354/*
Chip Coldwella6670612008-02-08 04:21:06 -0800355 * Deal with parity, framing and overrun errors.
356 */
357static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
358{
359 /* clear error */
360 UART_PUT_CR(port, ATMEL_US_RSTSTA);
361
362 if (status & ATMEL_US_RXBRK) {
363 /* ignore side-effect */
364 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
365 port->icount.brk++;
366 }
367 if (status & ATMEL_US_PARE)
368 port->icount.parity++;
369 if (status & ATMEL_US_FRAME)
370 port->icount.frame++;
371 if (status & ATMEL_US_OVRE)
372 port->icount.overrun++;
373}
374
375/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000376 * Characters received (called from interrupt handler)
377 */
David Howells7d12e782006-10-05 14:55:46 +0100378static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000379{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800380 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800381 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000382
Andrew Victorafefc412006-06-19 19:53:19 +0100383 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200384 while (status & ATMEL_US_RXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000385 ch = UART_GET_CHAR(port);
386
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000387 /*
388 * note that the error handling code is
389 * out of the main execution path
390 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700391 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
392 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
393 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800394
Remy Bohmerb843aa22008-02-08 04:21:01 -0800395 /* clear error */
396 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800397
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700398 if (status & ATMEL_US_RXBRK
399 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700400 atmel_port->break_active = 1;
401 UART_PUT_IER(port, ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700402 } else {
403 /*
404 * This is either the end-of-break
405 * condition or we've received at
406 * least one character without RXBRK
407 * being set. In both cases, the next
408 * RXBRK will indicate start-of-break.
409 */
410 UART_PUT_IDR(port, ATMEL_US_RXBRK);
411 status &= ~ATMEL_US_RXBRK;
412 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100413 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000414 }
415
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800416 atmel_buffer_rx_char(port, status, ch);
Andrew Victorafefc412006-06-19 19:53:19 +0100417 status = UART_GET_CSR(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000418 }
419
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800420 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000421}
422
423/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800424 * Transmit characters (called from tasklet with TXRDY interrupt
425 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000426 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200427static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000428{
429 struct circ_buf *xmit = &port->info->xmit;
430
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800431 if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000432 UART_PUT_CHAR(port, port->x_char);
433 port->icount.tx++;
434 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000435 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800436 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000437 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000438
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200439 while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000440 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
441 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
442 port->icount.tx++;
443 if (uart_circ_empty(xmit))
444 break;
445 }
446
447 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
448 uart_write_wakeup(port);
449
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800450 if (!uart_circ_empty(xmit))
451 UART_PUT_IER(port, ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000452}
453
454/*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800455 * receive interrupt handler.
456 */
457static void
458atmel_handle_receive(struct uart_port *port, unsigned int pending)
459{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800460 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800461
Chip Coldwella6670612008-02-08 04:21:06 -0800462 if (atmel_use_dma_rx(port)) {
463 /*
464 * PDC receive. Just schedule the tasklet and let it
465 * figure out the details.
466 *
467 * TODO: We're not handling error flags correctly at
468 * the moment.
469 */
470 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
471 UART_PUT_IDR(port, (ATMEL_US_ENDRX
472 | ATMEL_US_TIMEOUT));
473 tasklet_schedule(&atmel_port->tasklet);
474 }
475
476 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
477 ATMEL_US_FRAME | ATMEL_US_PARE))
478 atmel_pdc_rxerr(port, pending);
479 }
480
Remy Bohmerb843aa22008-02-08 04:21:01 -0800481 /* Interrupt receive */
482 if (pending & ATMEL_US_RXRDY)
483 atmel_rx_chars(port);
484 else if (pending & ATMEL_US_RXBRK) {
485 /*
486 * End of break detected. If it came along with a
487 * character, atmel_rx_chars will handle it.
488 */
489 UART_PUT_CR(port, ATMEL_US_RSTSTA);
490 UART_PUT_IDR(port, ATMEL_US_RXBRK);
491 atmel_port->break_active = 0;
492 }
493}
494
495/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800496 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -0800497 */
498static void
499atmel_handle_transmit(struct uart_port *port, unsigned int pending)
500{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800501 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800502
Chip Coldwella6670612008-02-08 04:21:06 -0800503 if (atmel_use_dma_tx(port)) {
504 /* PDC transmit */
505 if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) {
506 UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
507 tasklet_schedule(&atmel_port->tasklet);
508 }
509 } else {
510 /* Interrupt transmit */
511 if (pending & ATMEL_US_TXRDY) {
512 UART_PUT_IDR(port, ATMEL_US_TXRDY);
513 tasklet_schedule(&atmel_port->tasklet);
514 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800515 }
Remy Bohmerb843aa22008-02-08 04:21:01 -0800516}
517
518/*
519 * status flags interrupt handler.
520 */
521static void
522atmel_handle_status(struct uart_port *port, unsigned int pending,
523 unsigned int status)
524{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800525 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800526
Remy Bohmerb843aa22008-02-08 04:21:01 -0800527 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800528 | ATMEL_US_CTSIC)) {
529 atmel_port->irq_status = status;
530 tasklet_schedule(&atmel_port->tasklet);
531 }
Remy Bohmerb843aa22008-02-08 04:21:01 -0800532}
533
534/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000535 * Interrupt handler
536 */
David Howells7d12e782006-10-05 14:55:46 +0100537static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000538{
539 struct uart_port *port = dev_id;
540 unsigned int status, pending, pass_counter = 0;
541
Chip Coldwella6670612008-02-08 04:21:06 -0800542 do {
543 status = UART_GET_CSR(port);
544 pending = status & UART_GET_IMR(port);
545 if (!pending)
546 break;
547
Remy Bohmerb843aa22008-02-08 04:21:01 -0800548 atmel_handle_receive(port, pending);
549 atmel_handle_status(port, pending, status);
550 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -0800551 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000552
Haavard Skinnemoen0400b692008-02-23 15:23:36 -0800553 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000554}
555
Chip Coldwella6670612008-02-08 04:21:06 -0800556/*
557 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
558 */
559static void atmel_tx_dma(struct uart_port *port)
560{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800561 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800562 struct circ_buf *xmit = &port->info->xmit;
563 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
564 int count;
565
Michael Trimarchiba0657f2008-04-02 13:04:41 -0700566 /* nothing left to transmit? */
567 if (UART_GET_TCR(port))
568 return;
569
Chip Coldwella6670612008-02-08 04:21:06 -0800570 xmit->tail += pdc->ofs;
571 xmit->tail &= UART_XMIT_SIZE - 1;
572
573 port->icount.tx += pdc->ofs;
574 pdc->ofs = 0;
575
Michael Trimarchiba0657f2008-04-02 13:04:41 -0700576 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -0800577
Michael Trimarchiba0657f2008-04-02 13:04:41 -0700578 /* disable PDC transmit */
579 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
580
581 if (!uart_circ_empty(xmit)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800582 dma_sync_single_for_device(port->dev,
583 pdc->dma_addr,
584 pdc->dma_size,
585 DMA_TO_DEVICE);
586
587 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
588 pdc->ofs = count;
589
590 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
591 UART_PUT_TCR(port, count);
592 /* re-enable PDC transmit and interrupts */
593 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
594 UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
Chip Coldwella6670612008-02-08 04:21:06 -0800595 }
596
597 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
598 uart_write_wakeup(port);
599}
600
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800601static void atmel_rx_from_ring(struct uart_port *port)
602{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800603 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800604 struct circ_buf *ring = &atmel_port->rx_ring;
605 unsigned int flg;
606 unsigned int status;
607
608 while (ring->head != ring->tail) {
609 struct atmel_uart_char c;
610
611 /* Make sure c is loaded after head. */
612 smp_rmb();
613
614 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
615
616 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
617
618 port->icount.rx++;
619 status = c.status;
620 flg = TTY_NORMAL;
621
622 /*
623 * note that the error handling code is
624 * out of the main execution path
625 */
626 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
627 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
628 if (status & ATMEL_US_RXBRK) {
629 /* ignore side-effect */
630 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
631
632 port->icount.brk++;
633 if (uart_handle_break(port))
634 continue;
635 }
636 if (status & ATMEL_US_PARE)
637 port->icount.parity++;
638 if (status & ATMEL_US_FRAME)
639 port->icount.frame++;
640 if (status & ATMEL_US_OVRE)
641 port->icount.overrun++;
642
643 status &= port->read_status_mask;
644
645 if (status & ATMEL_US_RXBRK)
646 flg = TTY_BREAK;
647 else if (status & ATMEL_US_PARE)
648 flg = TTY_PARITY;
649 else if (status & ATMEL_US_FRAME)
650 flg = TTY_FRAME;
651 }
652
653
654 if (uart_handle_sysrq_char(port, c.ch))
655 continue;
656
657 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
658 }
659
660 /*
661 * Drop the lock here since it might end up calling
662 * uart_start(), which takes the lock.
663 */
664 spin_unlock(&port->lock);
Haavard Skinnemoenb1d16192008-07-16 21:56:29 +0100665 tty_flip_buffer_push(port->info->port.tty);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800666 spin_lock(&port->lock);
667}
668
Chip Coldwella6670612008-02-08 04:21:06 -0800669static void atmel_rx_from_dma(struct uart_port *port)
670{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800671 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Haavard Skinnemoenb1d16192008-07-16 21:56:29 +0100672 struct tty_struct *tty = port->info->port.tty;
Chip Coldwella6670612008-02-08 04:21:06 -0800673 struct atmel_dma_buffer *pdc;
674 int rx_idx = atmel_port->pdc_rx_idx;
675 unsigned int head;
676 unsigned int tail;
677 unsigned int count;
678
679 do {
680 /* Reset the UART timeout early so that we don't miss one */
681 UART_PUT_CR(port, ATMEL_US_STTTO);
682
683 pdc = &atmel_port->pdc_rx[rx_idx];
684 head = UART_GET_RPR(port) - pdc->dma_addr;
685 tail = pdc->ofs;
686
687 /* If the PDC has switched buffers, RPR won't contain
688 * any address within the current buffer. Since head
689 * is unsigned, we just need a one-way comparison to
690 * find out.
691 *
692 * In this case, we just need to consume the entire
693 * buffer and resubmit it for DMA. This will clear the
694 * ENDRX bit as well, so that we can safely re-enable
695 * all interrupts below.
696 */
697 head = min(head, pdc->dma_size);
698
699 if (likely(head != tail)) {
700 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
701 pdc->dma_size, DMA_FROM_DEVICE);
702
703 /*
704 * head will only wrap around when we recycle
705 * the DMA buffer, and when that happens, we
706 * explicitly set tail to 0. So head will
707 * always be greater than tail.
708 */
709 count = head - tail;
710
711 tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
712
713 dma_sync_single_for_device(port->dev, pdc->dma_addr,
714 pdc->dma_size, DMA_FROM_DEVICE);
715
716 port->icount.rx += count;
717 pdc->ofs = head;
718 }
719
720 /*
721 * If the current buffer is full, we need to check if
722 * the next one contains any additional data.
723 */
724 if (head >= pdc->dma_size) {
725 pdc->ofs = 0;
726 UART_PUT_RNPR(port, pdc->dma_addr);
727 UART_PUT_RNCR(port, pdc->dma_size);
728
729 rx_idx = !rx_idx;
730 atmel_port->pdc_rx_idx = rx_idx;
731 }
732 } while (head >= pdc->dma_size);
733
734 /*
735 * Drop the lock here since it might end up calling
736 * uart_start(), which takes the lock.
737 */
738 spin_unlock(&port->lock);
739 tty_flip_buffer_push(tty);
740 spin_lock(&port->lock);
741
742 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
743}
744
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800745/*
746 * tasklet handling tty stuff outside the interrupt handler.
747 */
748static void atmel_tasklet_func(unsigned long data)
749{
750 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800751 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800752 unsigned int status;
753 unsigned int status_change;
754
755 /* The interrupt handler does not take the lock */
756 spin_lock(&port->lock);
757
Chip Coldwella6670612008-02-08 04:21:06 -0800758 if (atmel_use_dma_tx(port))
759 atmel_tx_dma(port);
760 else
761 atmel_tx_chars(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800762
763 status = atmel_port->irq_status;
764 status_change = status ^ atmel_port->irq_status_prev;
765
766 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
767 | ATMEL_US_DCD | ATMEL_US_CTS)) {
768 /* TODO: All reads to CSR will clear these interrupts! */
769 if (status_change & ATMEL_US_RI)
770 port->icount.rng++;
771 if (status_change & ATMEL_US_DSR)
772 port->icount.dsr++;
773 if (status_change & ATMEL_US_DCD)
774 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
775 if (status_change & ATMEL_US_CTS)
776 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
777
778 wake_up_interruptible(&port->info->delta_msr_wait);
779
780 atmel_port->irq_status_prev = status;
781 }
782
Chip Coldwella6670612008-02-08 04:21:06 -0800783 if (atmel_use_dma_rx(port))
784 atmel_rx_from_dma(port);
785 else
786 atmel_rx_from_ring(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800787
788 spin_unlock(&port->lock);
789}
790
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000791/*
792 * Perform initialization and enable port for reception
793 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200794static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000795{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800796 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Haavard Skinnemoenb1d16192008-07-16 21:56:29 +0100797 struct tty_struct *tty = port->info->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000798 int retval;
799
800 /*
801 * Ensure that no interrupts are enabled otherwise when
802 * request_irq() is called we could get stuck trying to
803 * handle an unexpected interrupt
804 */
805 UART_PUT_IDR(port, -1);
806
807 /*
808 * Allocate the IRQ
809 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800810 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
Haavard Skinnemoenae161062008-02-08 04:21:08 -0800811 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000812 if (retval) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200813 printk("atmel_serial: atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000814 return retval;
815 }
816
817 /*
Chip Coldwella6670612008-02-08 04:21:06 -0800818 * Initialize DMA (if necessary)
819 */
820 if (atmel_use_dma_rx(port)) {
821 int i;
822
823 for (i = 0; i < 2; i++) {
824 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
825
826 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
827 if (pdc->buf == NULL) {
828 if (i != 0) {
829 dma_unmap_single(port->dev,
830 atmel_port->pdc_rx[0].dma_addr,
831 PDC_BUFFER_SIZE,
832 DMA_FROM_DEVICE);
833 kfree(atmel_port->pdc_rx[0].buf);
834 }
835 free_irq(port->irq, port);
836 return -ENOMEM;
837 }
838 pdc->dma_addr = dma_map_single(port->dev,
839 pdc->buf,
840 PDC_BUFFER_SIZE,
841 DMA_FROM_DEVICE);
842 pdc->dma_size = PDC_BUFFER_SIZE;
843 pdc->ofs = 0;
844 }
845
846 atmel_port->pdc_rx_idx = 0;
847
848 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
849 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
850
851 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
852 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
853 }
854 if (atmel_use_dma_tx(port)) {
855 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
856 struct circ_buf *xmit = &port->info->xmit;
857
858 pdc->buf = xmit->buf;
859 pdc->dma_addr = dma_map_single(port->dev,
860 pdc->buf,
861 UART_XMIT_SIZE,
862 DMA_TO_DEVICE);
863 pdc->dma_size = UART_XMIT_SIZE;
864 pdc->ofs = 0;
865 }
866
867 /*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000868 * If there is a specific "open" function (to register
869 * control line interrupts)
870 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200871 if (atmel_open_hook) {
872 retval = atmel_open_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000873 if (retval) {
874 free_irq(port->irq, port);
875 return retval;
876 }
877 }
878
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000879 /*
880 * Finally, enable the serial port
881 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200882 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800883 /* enable xmit & rcvr */
884 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +0100885
Chip Coldwella6670612008-02-08 04:21:06 -0800886 if (atmel_use_dma_rx(port)) {
887 /* set UART timeout */
888 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
889 UART_PUT_CR(port, ATMEL_US_STTTO);
890
891 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
892 /* enable PDC controller */
893 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
894 } else {
895 /* enable receive only */
896 UART_PUT_IER(port, ATMEL_US_RXRDY);
897 }
Andrew Victorafefc412006-06-19 19:53:19 +0100898
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000899 return 0;
900}
901
902/*
903 * Disable the port
904 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200905static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000906{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800907 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800908 /*
909 * Ensure everything is stopped.
910 */
911 atmel_stop_rx(port);
912 atmel_stop_tx(port);
913
914 /*
915 * Shut-down the DMA.
916 */
917 if (atmel_use_dma_rx(port)) {
918 int i;
919
920 for (i = 0; i < 2; i++) {
921 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
922
923 dma_unmap_single(port->dev,
924 pdc->dma_addr,
925 pdc->dma_size,
926 DMA_FROM_DEVICE);
927 kfree(pdc->buf);
928 }
929 }
930 if (atmel_use_dma_tx(port)) {
931 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
932
933 dma_unmap_single(port->dev,
934 pdc->dma_addr,
935 pdc->dma_size,
936 DMA_TO_DEVICE);
937 }
938
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000939 /*
940 * Disable all interrupts, port and break condition.
941 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200942 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000943 UART_PUT_IDR(port, -1);
944
945 /*
946 * Free the interrupt
947 */
948 free_irq(port->irq, port);
949
950 /*
951 * If there is a specific "close" function (to unregister
952 * control line interrupts)
953 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200954 if (atmel_close_hook)
955 atmel_close_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000956}
957
958/*
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +0100959 * Flush any TX data submitted for DMA. Called when the TX circular
960 * buffer is reset.
961 */
962static void atmel_flush_buffer(struct uart_port *port)
963{
964 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
965
966 if (atmel_use_dma_tx(port)) {
967 UART_PUT_TCR(port, 0);
968 atmel_port->pdc_tx.ofs = 0;
969 }
970}
971
972/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000973 * Power / Clock management.
974 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800975static void atmel_serial_pm(struct uart_port *port, unsigned int state,
976 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000977{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800978 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +0100979
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000980 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800981 case 0:
982 /*
983 * Enable the peripheral clock for this serial port.
984 * This is called on uart_open() or a resume event.
985 */
986 clk_enable(atmel_port->clk);
987 break;
988 case 3:
989 /*
990 * Disable the peripheral clock for this serial port.
991 * This is called on uart_close() or a suspend event.
992 */
993 clk_disable(atmel_port->clk);
994 break;
995 default:
996 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000997 }
998}
999
1000/*
1001 * Change the port parameters
1002 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001003static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1004 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001005{
1006 unsigned long flags;
1007 unsigned int mode, imr, quot, baud;
1008
Andrew Victor03abeac2007-05-03 12:26:24 +01001009 /* Get current mode register */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001010 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1011 | ATMEL_US_NBSTOP | ATMEL_US_PAR);
Andrew Victor03abeac2007-05-03 12:26:24 +01001012
Remy Bohmerb843aa22008-02-08 04:21:01 -08001013 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001014 quot = uart_get_divisor(port, baud);
1015
Remy Bohmerb843aa22008-02-08 04:21:01 -08001016 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01001017 quot /= 8;
1018 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1019 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001020
1021 /* byte size */
1022 switch (termios->c_cflag & CSIZE) {
1023 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001024 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001025 break;
1026 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001027 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001028 break;
1029 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001030 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001031 break;
1032 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001033 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001034 break;
1035 }
1036
1037 /* stop bits */
1038 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001039 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001040
1041 /* parity */
1042 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001043 /* Mark or Space parity */
1044 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001045 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001046 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001047 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001048 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001049 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001050 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001051 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001052 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001053 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001054 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001055
1056 spin_lock_irqsave(&port->lock, flags);
1057
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001058 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001059 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001060 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001061 if (termios->c_iflag & (BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001062 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001063
Chip Coldwella6670612008-02-08 04:21:06 -08001064 if (atmel_use_dma_rx(port))
1065 /* need to enable error interrupts */
1066 UART_PUT_IER(port, port->read_status_mask);
1067
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001068 /*
1069 * Characters to ignore
1070 */
1071 port->ignore_status_mask = 0;
1072 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001073 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001074 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001075 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001076 /*
1077 * If we're ignoring parity and break indicators,
1078 * ignore overruns too (for real raw support).
1079 */
1080 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001081 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001082 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001083 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001084
1085 /* update the per-port timeout */
1086 uart_update_timeout(port, termios->c_cflag, baud);
1087
Remy Bohmerb843aa22008-02-08 04:21:01 -08001088 /* save/disable interrupts and drain transmitter */
1089 imr = UART_GET_IMR(port);
1090 UART_PUT_IDR(port, -1);
1091 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08001092 cpu_relax();
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001093
1094 /* disable receiver and transmitter */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001095 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001096
1097 /* set the parity, stop bits and data size */
1098 UART_PUT_MR(port, mode);
1099
1100 /* set the baud rate */
1101 UART_PUT_BRGR(port, quot);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001102 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1103 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001104
1105 /* restore interrupts */
1106 UART_PUT_IER(port, imr);
1107
1108 /* CTS flow-control and modem-status interrupts */
1109 if (UART_ENABLE_MS(port, termios->c_cflag))
1110 port->ops->enable_ms(port);
1111
1112 spin_unlock_irqrestore(&port->lock, flags);
1113}
1114
1115/*
1116 * Return string describing the specified port
1117 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001118static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001119{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001120 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001121}
1122
1123/*
1124 * Release the memory region(s) being used by 'port'.
1125 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001126static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001127{
Andrew Victorafefc412006-06-19 19:53:19 +01001128 struct platform_device *pdev = to_platform_device(port->dev);
1129 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1130
1131 release_mem_region(port->mapbase, size);
1132
1133 if (port->flags & UPF_IOREMAP) {
1134 iounmap(port->membase);
1135 port->membase = NULL;
1136 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001137}
1138
1139/*
1140 * Request the memory region(s) being used by 'port'.
1141 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001142static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001143{
Andrew Victorafefc412006-06-19 19:53:19 +01001144 struct platform_device *pdev = to_platform_device(port->dev);
1145 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001146
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001147 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01001148 return -EBUSY;
1149
1150 if (port->flags & UPF_IOREMAP) {
1151 port->membase = ioremap(port->mapbase, size);
1152 if (port->membase == NULL) {
1153 release_mem_region(port->mapbase, size);
1154 return -ENOMEM;
1155 }
1156 }
1157
1158 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001159}
1160
1161/*
1162 * Configure/autoconfigure the port.
1163 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001164static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001165{
1166 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001167 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001168 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001169 }
1170}
1171
1172/*
1173 * Verify the new serial_struct (for TIOCSSERIAL).
1174 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001175static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001176{
1177 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001178 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001179 ret = -EINVAL;
1180 if (port->irq != ser->irq)
1181 ret = -EINVAL;
1182 if (ser->io_type != SERIAL_IO_MEM)
1183 ret = -EINVAL;
1184 if (port->uartclk / 16 != ser->baud_base)
1185 ret = -EINVAL;
1186 if ((void *)port->mapbase != ser->iomem_base)
1187 ret = -EINVAL;
1188 if (port->iobase != ser->port)
1189 ret = -EINVAL;
1190 if (ser->hub6 != 0)
1191 ret = -EINVAL;
1192 return ret;
1193}
1194
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001195static struct uart_ops atmel_pops = {
1196 .tx_empty = atmel_tx_empty,
1197 .set_mctrl = atmel_set_mctrl,
1198 .get_mctrl = atmel_get_mctrl,
1199 .stop_tx = atmel_stop_tx,
1200 .start_tx = atmel_start_tx,
1201 .stop_rx = atmel_stop_rx,
1202 .enable_ms = atmel_enable_ms,
1203 .break_ctl = atmel_break_ctl,
1204 .startup = atmel_startup,
1205 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001206 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001207 .set_termios = atmel_set_termios,
1208 .type = atmel_type,
1209 .release_port = atmel_release_port,
1210 .request_port = atmel_request_port,
1211 .config_port = atmel_config_port,
1212 .verify_port = atmel_verify_port,
1213 .pm = atmel_serial_pm,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001214};
1215
Andrew Victorafefc412006-06-19 19:53:19 +01001216/*
1217 * Configure the port from the platform device resource info.
1218 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001219static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1220 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001221{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001222 struct uart_port *port = &atmel_port->uart;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02001223 struct atmel_uart_data *data = pdev->dev.platform_data;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001224
Andrew Victorafefc412006-06-19 19:53:19 +01001225 port->iotype = UPIO_MEM;
Andrew Victora14d5272007-01-09 09:03:42 +01001226 port->flags = UPF_BOOT_AUTOCONF;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001227 port->ops = &atmel_pops;
Andrew Victora14d5272007-01-09 09:03:42 +01001228 port->fifosize = 1;
Andrew Victorafefc412006-06-19 19:53:19 +01001229 port->line = pdev->id;
1230 port->dev = &pdev->dev;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001231
Andrew Victorafefc412006-06-19 19:53:19 +01001232 port->mapbase = pdev->resource[0].start;
1233 port->irq = pdev->resource[1].start;
1234
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001235 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1236 (unsigned long)port);
1237
1238 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1239
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02001240 if (data->regs)
1241 /* Already mapped by setup code */
1242 port->membase = data->regs;
Andrew Victorafefc412006-06-19 19:53:19 +01001243 else {
1244 port->flags |= UPF_IOREMAP;
1245 port->membase = NULL;
1246 }
1247
Remy Bohmerb843aa22008-02-08 04:21:01 -08001248 /* for console, the clock could already be configured */
1249 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001250 atmel_port->clk = clk_get(&pdev->dev, "usart");
1251 clk_enable(atmel_port->clk);
1252 port->uartclk = clk_get_rate(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01001253 }
Chip Coldwella6670612008-02-08 04:21:06 -08001254
1255 atmel_port->use_dma_rx = data->use_dma_rx;
1256 atmel_port->use_dma_tx = data->use_dma_tx;
1257 if (atmel_use_dma_tx(port))
1258 port->fifosize = PDC_BUFFER_SIZE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001259}
1260
Andrew Victorafefc412006-06-19 19:53:19 +01001261/*
1262 * Register board-specific modem-control line handlers.
1263 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +02001264void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001265{
1266 if (fns->enable_ms)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001267 atmel_pops.enable_ms = fns->enable_ms;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001268 if (fns->get_mctrl)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001269 atmel_pops.get_mctrl = fns->get_mctrl;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001270 if (fns->set_mctrl)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001271 atmel_pops.set_mctrl = fns->set_mctrl;
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +02001272 atmel_open_hook = fns->open;
1273 atmel_close_hook = fns->close;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001274 atmel_pops.pm = fns->pm;
1275 atmel_pops.set_wake = fns->set_wake;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001276}
1277
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02001278#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001279static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00001280{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001281 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08001282 cpu_relax();
Russell Kingd3587882006-03-20 20:00:09 +00001283 UART_PUT_CHAR(port, ch);
1284}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001285
1286/*
1287 * Interrupts are disabled on entering
1288 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001289static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001290{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001291 struct uart_port *port = &atmel_ports[co->index].uart;
Russell Kingd3587882006-03-20 20:00:09 +00001292 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07001293 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001294
1295 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001296 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001297 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001298 imr = UART_GET_IMR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001299 UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001300
Marc Pignat39d4c922008-04-02 13:04:42 -07001301 /* Store PDC transmit status and disable it */
1302 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1303 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1304
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001305 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001306
1307 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001308 * Finally, wait for transmitter to become empty
1309 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001310 */
1311 do {
1312 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001313 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07001314
1315 /* Restore PDC transmit status */
1316 if (pdc_tx)
1317 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1318
Remy Bohmerb843aa22008-02-08 04:21:01 -08001319 /* set interrupts back the way they were */
1320 UART_PUT_IER(port, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001321}
1322
1323/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001324 * If the port was already initialised (eg, by a boot loader),
1325 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001326 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001327static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1328 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001329{
1330 unsigned int mr, quot;
1331
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08001332 /*
1333 * If the baud rate generator isn't running, the port wasn't
1334 * initialized by the boot loader.
1335 */
Haavard Skinnemoen9c81c5c2008-06-05 22:46:39 -07001336 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08001337 if (!quot)
1338 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001339
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001340 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1341 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001342 *bits = 8;
1343 else
1344 *bits = 7;
1345
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001346 mr = UART_GET_MR(port) & ATMEL_US_PAR;
1347 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001348 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001349 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001350 *parity = 'o';
1351
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02001352 /*
1353 * The serial core only rounds down when matching this to a
1354 * supported baud rate. Make sure we don't end up slightly
1355 * lower than one of those, as it would make us fall through
1356 * to a much lower baud rate than we really want.
1357 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02001358 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001359}
1360
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001361static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001362{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001363 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001364 int baud = 115200;
1365 int bits = 8;
1366 int parity = 'n';
1367 int flow = 'n';
1368
Remy Bohmerb843aa22008-02-08 04:21:01 -08001369 if (port->membase == NULL) {
1370 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01001371 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001372 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001373
Remy Bohmerb843aa22008-02-08 04:21:01 -08001374 UART_PUT_IDR(port, -1);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001375 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1376 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001377
1378 if (options)
1379 uart_parse_options(options, &baud, &parity, &bits, &flow);
1380 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001381 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001382
1383 return uart_set_options(port, co, baud, parity, bits, flow);
1384}
1385
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001386static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001387
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001388static struct console atmel_console = {
1389 .name = ATMEL_DEVICENAME,
1390 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001391 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001392 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001393 .flags = CON_PRINTBUFFER,
1394 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001395 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001396};
1397
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001398#define ATMEL_CONSOLE_DEVICE &atmel_console
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001399
Andrew Victorafefc412006-06-19 19:53:19 +01001400/*
1401 * Early console initialization (before VM subsystem initialized).
1402 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001403static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001404{
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02001405 if (atmel_default_console_device) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001406 add_preferred_console(ATMEL_DEVICENAME,
1407 atmel_default_console_device->id, NULL);
1408 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1409 atmel_default_console_device);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001410 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01001411 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001412
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001413 return 0;
1414}
Remy Bohmerb843aa22008-02-08 04:21:01 -08001415
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001416console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001417
Andrew Victorafefc412006-06-19 19:53:19 +01001418/*
1419 * Late console initialization.
1420 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001421static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01001422{
Remy Bohmerb843aa22008-02-08 04:21:01 -08001423 if (atmel_default_console_device
1424 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001425 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01001426
1427 return 0;
1428}
Remy Bohmerb843aa22008-02-08 04:21:01 -08001429
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001430core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01001431
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001432static inline bool atmel_is_console_port(struct uart_port *port)
1433{
1434 return port->cons && port->cons->index == port->line;
1435}
1436
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001437#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001438#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001439
1440static inline bool atmel_is_console_port(struct uart_port *port)
1441{
1442 return false;
1443}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001444#endif
1445
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001446static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001447 .owner = THIS_MODULE,
1448 .driver_name = "atmel_serial",
1449 .dev_name = ATMEL_DEVICENAME,
1450 .major = SERIAL_ATMEL_MAJOR,
1451 .minor = MINOR_START,
1452 .nr = ATMEL_MAX_UART,
1453 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001454};
1455
Andrew Victorafefc412006-06-19 19:53:19 +01001456#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01001457static bool atmel_serial_clk_will_stop(void)
1458{
1459#ifdef CONFIG_ARCH_AT91
1460 return at91_suspend_entering_slow_clock();
1461#else
1462 return false;
1463#endif
1464}
1465
Remy Bohmerb843aa22008-02-08 04:21:01 -08001466static int atmel_serial_suspend(struct platform_device *pdev,
1467 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001468{
Andrew Victorafefc412006-06-19 19:53:19 +01001469 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001470 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001471
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01001472 if (atmel_is_console_port(port) && console_suspend_enabled) {
1473 /* Drain the TX shifter */
1474 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1475 cpu_relax();
1476 }
1477
Remy Bohmerb843aa22008-02-08 04:21:01 -08001478 if (device_may_wakeup(&pdev->dev)
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01001479 && !atmel_serial_clk_will_stop())
Andrew Victorafefc412006-06-19 19:53:19 +01001480 enable_irq_wake(port->irq);
1481 else {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001482 uart_suspend_port(&atmel_uart, port);
1483 atmel_port->suspended = 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001484 }
1485
1486 return 0;
1487}
1488
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001489static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01001490{
1491 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001492 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001493
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001494 if (atmel_port->suspended) {
1495 uart_resume_port(&atmel_uart, port);
1496 atmel_port->suspended = 0;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001497 } else
Andrew Victor9b938162007-01-09 13:20:54 +01001498 disable_irq_wake(port->irq);
Andrew Victorafefc412006-06-19 19:53:19 +01001499
1500 return 0;
1501}
1502#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001503#define atmel_serial_suspend NULL
1504#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01001505#endif
1506
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001507static int __devinit atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01001508{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001509 struct atmel_uart_port *port;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001510 void *data;
Andrew Victorafefc412006-06-19 19:53:19 +01001511 int ret;
1512
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001513 BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE));
1514
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001515 port = &atmel_ports[pdev->id];
1516 atmel_init_port(port, pdev);
Andrew Victorafefc412006-06-19 19:53:19 +01001517
Chip Coldwella6670612008-02-08 04:21:06 -08001518 if (!atmel_use_dma_rx(&port->uart)) {
1519 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08001520 data = kmalloc(sizeof(struct atmel_uart_char)
1521 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08001522 if (!data)
1523 goto err_alloc_ring;
1524 port->rx_ring.buf = data;
1525 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001526
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001527 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001528 if (ret)
1529 goto err_add_port;
1530
1531 device_init_wakeup(&pdev->dev, 1);
1532 platform_set_drvdata(pdev, port);
1533
1534 return 0;
1535
1536err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001537 kfree(port->rx_ring.buf);
1538 port->rx_ring.buf = NULL;
1539err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001540 if (!atmel_is_console_port(&port->uart)) {
1541 clk_disable(port->clk);
1542 clk_put(port->clk);
1543 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01001544 }
1545
1546 return ret;
1547}
1548
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001549static int __devexit atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01001550{
1551 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001552 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001553 int ret = 0;
1554
Andrew Victorafefc412006-06-19 19:53:19 +01001555 device_init_wakeup(&pdev->dev, 0);
1556 platform_set_drvdata(pdev, NULL);
1557
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001558 ret = uart_remove_one_port(&atmel_uart, port);
1559
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001560 tasklet_kill(&atmel_port->tasklet);
1561 kfree(atmel_port->rx_ring.buf);
1562
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001563 /* "port" is allocated statically, so we shouldn't free it */
1564
1565 clk_disable(atmel_port->clk);
1566 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01001567
1568 return ret;
1569}
1570
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001571static struct platform_driver atmel_serial_driver = {
1572 .probe = atmel_serial_probe,
1573 .remove = __devexit_p(atmel_serial_remove),
1574 .suspend = atmel_serial_suspend,
1575 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01001576 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02001577 .name = "atmel_usart",
Andrew Victorafefc412006-06-19 19:53:19 +01001578 .owner = THIS_MODULE,
1579 },
1580};
1581
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001582static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01001583{
1584 int ret;
1585
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001586 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01001587 if (ret)
1588 return ret;
1589
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001590 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01001591 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001592 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01001593
1594 return ret;
1595}
1596
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001597static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001598{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001599 platform_driver_unregister(&atmel_serial_driver);
1600 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001601}
1602
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001603module_init(atmel_serial_init);
1604module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001605
1606MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001607MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001608MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001609MODULE_ALIAS("platform:atmel_usart");