blob: 89362d733d6230331c6bead3c8bbce3ef9d28563 [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 */
Anti Sullinf05596d2008-09-22 13:57:54 -0700134 int may_wakeup; /* cached value of device_may_wakeup for times we need to disable it */
135 u32 backup_imr; /* IMR saved during suspend */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700136 int break_active; /* break being received */
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800137
Chip Coldwella6670612008-02-08 04:21:06 -0800138 short use_dma_rx; /* enable PDC receiver */
139 short pdc_rx_idx; /* current PDC RX buffer */
140 struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
141
142 short use_dma_tx; /* enable PDC transmitter */
143 struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
144
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800145 struct tasklet_struct tasklet;
146 unsigned int irq_status;
147 unsigned int irq_status_prev;
148
149 struct circ_buf rx_ring;
Andrew Victorafefc412006-06-19 19:53:19 +0100150};
151
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200152static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
Andrew Victorafefc412006-06-19 19:53:19 +0100153
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000154#ifdef SUPPORT_SYSRQ
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200155static struct console atmel_console;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000156#endif
157
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800158static inline struct atmel_uart_port *
159to_atmel_uart_port(struct uart_port *uart)
160{
161 return container_of(uart, struct atmel_uart_port, uart);
162}
163
Chip Coldwella6670612008-02-08 04:21:06 -0800164#ifdef CONFIG_SERIAL_ATMEL_PDC
165static bool atmel_use_dma_rx(struct uart_port *port)
166{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800167 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800168
169 return atmel_port->use_dma_rx;
170}
171
172static bool atmel_use_dma_tx(struct uart_port *port)
173{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800174 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800175
176 return atmel_port->use_dma_tx;
177}
178#else
179static bool atmel_use_dma_rx(struct uart_port *port)
180{
181 return false;
182}
183
184static bool atmel_use_dma_tx(struct uart_port *port)
185{
186 return false;
187}
188#endif
189
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000190/*
191 * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
192 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200193static u_int atmel_tx_empty(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000194{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200195 return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000196}
197
198/*
199 * Set state of the modem control output lines
200 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200201static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000202{
203 unsigned int control = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100204 unsigned int mode;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000205
Andrew Victorc2f5ccf2006-12-04 09:37:20 +0100206#ifdef CONFIG_ARCH_AT91RM9200
Andrew Victor79da7a62006-11-30 11:58:14 +0100207 if (cpu_is_at91rm9200()) {
Andrew Victorafefc412006-06-19 19:53:19 +0100208 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800209 * AT91RM9200 Errata #39: RTS0 is not internally connected
210 * to PA21. We need to drive the pin manually.
Andrew Victorafefc412006-06-19 19:53:19 +0100211 */
Andrew Victor72729912006-09-27 09:44:11 +0100212 if (port->mapbase == AT91RM9200_BASE_US0) {
Andrew Victorafefc412006-06-19 19:53:19 +0100213 if (mctrl & TIOCM_RTS)
Andrew Victor20e65272006-07-05 14:31:13 +0100214 at91_set_gpio_value(AT91_PIN_PA21, 0);
Andrew Victorafefc412006-06-19 19:53:19 +0100215 else
Andrew Victor20e65272006-07-05 14:31:13 +0100216 at91_set_gpio_value(AT91_PIN_PA21, 1);
Andrew Victorafefc412006-06-19 19:53:19 +0100217 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000218 }
Haavard Skinnemoenacca9b82006-10-04 16:02:09 +0200219#endif
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000220
221 if (mctrl & TIOCM_RTS)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200222 control |= ATMEL_US_RTSEN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000223 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200224 control |= ATMEL_US_RTSDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000225
226 if (mctrl & TIOCM_DTR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200227 control |= ATMEL_US_DTREN;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000228 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200229 control |= ATMEL_US_DTRDIS;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000230
Andrew Victorafefc412006-06-19 19:53:19 +0100231 UART_PUT_CR(port, control);
232
233 /* Local loopback mode? */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200234 mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
Andrew Victorafefc412006-06-19 19:53:19 +0100235 if (mctrl & TIOCM_LOOP)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200236 mode |= ATMEL_US_CHMODE_LOC_LOOP;
Andrew Victorafefc412006-06-19 19:53:19 +0100237 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200238 mode |= ATMEL_US_CHMODE_NORMAL;
Andrew Victorafefc412006-06-19 19:53:19 +0100239 UART_PUT_MR(port, mode);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000240}
241
242/*
243 * Get state of the modem control input lines
244 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200245static u_int atmel_get_mctrl(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000246{
247 unsigned int status, ret = 0;
248
249 status = UART_GET_CSR(port);
250
251 /*
252 * The control signals are active low.
253 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200254 if (!(status & ATMEL_US_DCD))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000255 ret |= TIOCM_CD;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200256 if (!(status & ATMEL_US_CTS))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000257 ret |= TIOCM_CTS;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200258 if (!(status & ATMEL_US_DSR))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000259 ret |= TIOCM_DSR;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200260 if (!(status & ATMEL_US_RI))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000261 ret |= TIOCM_RI;
262
263 return ret;
264}
265
266/*
267 * Stop transmitting.
268 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200269static void atmel_stop_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000270{
Chip Coldwella6670612008-02-08 04:21:06 -0800271 if (atmel_use_dma_tx(port)) {
272 /* disable PDC transmit */
273 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
274 UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
275 } else
276 UART_PUT_IDR(port, ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000277}
278
279/*
280 * Start transmitting.
281 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200282static void atmel_start_tx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000283{
Chip Coldwella6670612008-02-08 04:21:06 -0800284 if (atmel_use_dma_tx(port)) {
285 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
286 /* The transmitter is already running. Yes, we
287 really need this.*/
288 return;
289
290 UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
291 /* re-enable PDC transmit */
292 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
293 } else
294 UART_PUT_IER(port, ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000295}
296
297/*
298 * Stop receiving - port is in process of being closed.
299 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200300static void atmel_stop_rx(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000301{
Chip Coldwella6670612008-02-08 04:21:06 -0800302 if (atmel_use_dma_rx(port)) {
303 /* disable PDC receive */
304 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
305 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
306 } else
307 UART_PUT_IDR(port, ATMEL_US_RXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000308}
309
310/*
311 * Enable modem status interrupts
312 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200313static void atmel_enable_ms(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000314{
Remy Bohmerb843aa22008-02-08 04:21:01 -0800315 UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
316 | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000317}
318
319/*
320 * Control the transmission of a break signal
321 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200322static void atmel_break_ctl(struct uart_port *port, int break_state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000323{
324 if (break_state != 0)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200325 UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000326 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200327 UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000328}
329
330/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800331 * Stores the incoming character in the ring buffer
332 */
333static void
334atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
335 unsigned int ch)
336{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800337 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800338 struct circ_buf *ring = &atmel_port->rx_ring;
339 struct atmel_uart_char *c;
340
341 if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
342 /* Buffer overflow, ignore char */
343 return;
344
345 c = &((struct atmel_uart_char *)ring->buf)[ring->head];
346 c->status = status;
347 c->ch = ch;
348
349 /* Make sure the character is stored before we update head. */
350 smp_wmb();
351
352 ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
353}
354
355/*
Chip Coldwella6670612008-02-08 04:21:06 -0800356 * Deal with parity, framing and overrun errors.
357 */
358static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
359{
360 /* clear error */
361 UART_PUT_CR(port, ATMEL_US_RSTSTA);
362
363 if (status & ATMEL_US_RXBRK) {
364 /* ignore side-effect */
365 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
366 port->icount.brk++;
367 }
368 if (status & ATMEL_US_PARE)
369 port->icount.parity++;
370 if (status & ATMEL_US_FRAME)
371 port->icount.frame++;
372 if (status & ATMEL_US_OVRE)
373 port->icount.overrun++;
374}
375
376/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000377 * Characters received (called from interrupt handler)
378 */
David Howells7d12e782006-10-05 14:55:46 +0100379static void atmel_rx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000380{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800381 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800382 unsigned int status, ch;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000383
Andrew Victorafefc412006-06-19 19:53:19 +0100384 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200385 while (status & ATMEL_US_RXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000386 ch = UART_GET_CHAR(port);
387
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000388 /*
389 * note that the error handling code is
390 * out of the main execution path
391 */
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700392 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
393 | ATMEL_US_OVRE | ATMEL_US_RXBRK)
394 || atmel_port->break_active)) {
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800395
Remy Bohmerb843aa22008-02-08 04:21:01 -0800396 /* clear error */
397 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800398
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700399 if (status & ATMEL_US_RXBRK
400 && !atmel_port->break_active) {
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700401 atmel_port->break_active = 1;
402 UART_PUT_IER(port, ATMEL_US_RXBRK);
Haavard Skinnemoen9e6077b2007-07-15 23:40:36 -0700403 } else {
404 /*
405 * This is either the end-of-break
406 * condition or we've received at
407 * least one character without RXBRK
408 * being set. In both cases, the next
409 * RXBRK will indicate start-of-break.
410 */
411 UART_PUT_IDR(port, ATMEL_US_RXBRK);
412 status &= ~ATMEL_US_RXBRK;
413 atmel_port->break_active = 0;
Andrew Victorafefc412006-06-19 19:53:19 +0100414 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000415 }
416
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800417 atmel_buffer_rx_char(port, status, ch);
Andrew Victorafefc412006-06-19 19:53:19 +0100418 status = UART_GET_CSR(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000419 }
420
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800421 tasklet_schedule(&atmel_port->tasklet);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000422}
423
424/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800425 * Transmit characters (called from tasklet with TXRDY interrupt
426 * disabled)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000427 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200428static void atmel_tx_chars(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000429{
430 struct circ_buf *xmit = &port->info->xmit;
431
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800432 if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000433 UART_PUT_CHAR(port, port->x_char);
434 port->icount.tx++;
435 port->x_char = 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000436 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800437 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000438 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000439
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200440 while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000441 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
442 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
443 port->icount.tx++;
444 if (uart_circ_empty(xmit))
445 break;
446 }
447
448 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
449 uart_write_wakeup(port);
450
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800451 if (!uart_circ_empty(xmit))
452 UART_PUT_IER(port, ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000453}
454
455/*
Remy Bohmerb843aa22008-02-08 04:21:01 -0800456 * receive interrupt handler.
457 */
458static void
459atmel_handle_receive(struct uart_port *port, unsigned int pending)
460{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800461 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800462
Chip Coldwella6670612008-02-08 04:21:06 -0800463 if (atmel_use_dma_rx(port)) {
464 /*
465 * PDC receive. Just schedule the tasklet and let it
466 * figure out the details.
467 *
468 * TODO: We're not handling error flags correctly at
469 * the moment.
470 */
471 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
472 UART_PUT_IDR(port, (ATMEL_US_ENDRX
473 | ATMEL_US_TIMEOUT));
474 tasklet_schedule(&atmel_port->tasklet);
475 }
476
477 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
478 ATMEL_US_FRAME | ATMEL_US_PARE))
479 atmel_pdc_rxerr(port, pending);
480 }
481
Remy Bohmerb843aa22008-02-08 04:21:01 -0800482 /* Interrupt receive */
483 if (pending & ATMEL_US_RXRDY)
484 atmel_rx_chars(port);
485 else if (pending & ATMEL_US_RXBRK) {
486 /*
487 * End of break detected. If it came along with a
488 * character, atmel_rx_chars will handle it.
489 */
490 UART_PUT_CR(port, ATMEL_US_RSTSTA);
491 UART_PUT_IDR(port, ATMEL_US_RXBRK);
492 atmel_port->break_active = 0;
493 }
494}
495
496/*
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800497 * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
Remy Bohmerb843aa22008-02-08 04:21:01 -0800498 */
499static void
500atmel_handle_transmit(struct uart_port *port, unsigned int pending)
501{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800502 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800503
Chip Coldwella6670612008-02-08 04:21:06 -0800504 if (atmel_use_dma_tx(port)) {
505 /* PDC transmit */
506 if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) {
507 UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
508 tasklet_schedule(&atmel_port->tasklet);
509 }
510 } else {
511 /* Interrupt transmit */
512 if (pending & ATMEL_US_TXRDY) {
513 UART_PUT_IDR(port, ATMEL_US_TXRDY);
514 tasklet_schedule(&atmel_port->tasklet);
515 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800516 }
Remy Bohmerb843aa22008-02-08 04:21:01 -0800517}
518
519/*
520 * status flags interrupt handler.
521 */
522static void
523atmel_handle_status(struct uart_port *port, unsigned int pending,
524 unsigned int status)
525{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800526 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800527
Remy Bohmerb843aa22008-02-08 04:21:01 -0800528 if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800529 | ATMEL_US_CTSIC)) {
530 atmel_port->irq_status = status;
531 tasklet_schedule(&atmel_port->tasklet);
532 }
Remy Bohmerb843aa22008-02-08 04:21:01 -0800533}
534
535/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000536 * Interrupt handler
537 */
David Howells7d12e782006-10-05 14:55:46 +0100538static irqreturn_t atmel_interrupt(int irq, void *dev_id)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000539{
540 struct uart_port *port = dev_id;
541 unsigned int status, pending, pass_counter = 0;
542
Chip Coldwella6670612008-02-08 04:21:06 -0800543 do {
544 status = UART_GET_CSR(port);
545 pending = status & UART_GET_IMR(port);
546 if (!pending)
547 break;
548
Remy Bohmerb843aa22008-02-08 04:21:01 -0800549 atmel_handle_receive(port, pending);
550 atmel_handle_status(port, pending, status);
551 atmel_handle_transmit(port, pending);
Chip Coldwella6670612008-02-08 04:21:06 -0800552 } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000553
Haavard Skinnemoen0400b692008-02-23 15:23:36 -0800554 return pass_counter ? IRQ_HANDLED : IRQ_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000555}
556
Chip Coldwella6670612008-02-08 04:21:06 -0800557/*
558 * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
559 */
560static void atmel_tx_dma(struct uart_port *port)
561{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800562 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800563 struct circ_buf *xmit = &port->info->xmit;
564 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
565 int count;
566
Michael Trimarchiba0657f2008-04-02 13:04:41 -0700567 /* nothing left to transmit? */
568 if (UART_GET_TCR(port))
569 return;
570
Chip Coldwella6670612008-02-08 04:21:06 -0800571 xmit->tail += pdc->ofs;
572 xmit->tail &= UART_XMIT_SIZE - 1;
573
574 port->icount.tx += pdc->ofs;
575 pdc->ofs = 0;
576
Michael Trimarchiba0657f2008-04-02 13:04:41 -0700577 /* more to transmit - setup next transfer */
Chip Coldwella6670612008-02-08 04:21:06 -0800578
Michael Trimarchiba0657f2008-04-02 13:04:41 -0700579 /* disable PDC transmit */
580 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
581
Itai Levi1f140812009-01-15 13:50:43 -0800582 if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
Chip Coldwella6670612008-02-08 04:21:06 -0800583 dma_sync_single_for_device(port->dev,
584 pdc->dma_addr,
585 pdc->dma_size,
586 DMA_TO_DEVICE);
587
588 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
589 pdc->ofs = count;
590
591 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
592 UART_PUT_TCR(port, count);
593 /* re-enable PDC transmit and interrupts */
594 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
595 UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
Chip Coldwella6670612008-02-08 04:21:06 -0800596 }
597
598 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
599 uart_write_wakeup(port);
600}
601
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800602static void atmel_rx_from_ring(struct uart_port *port)
603{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800604 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800605 struct circ_buf *ring = &atmel_port->rx_ring;
606 unsigned int flg;
607 unsigned int status;
608
609 while (ring->head != ring->tail) {
610 struct atmel_uart_char c;
611
612 /* Make sure c is loaded after head. */
613 smp_rmb();
614
615 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
616
617 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
618
619 port->icount.rx++;
620 status = c.status;
621 flg = TTY_NORMAL;
622
623 /*
624 * note that the error handling code is
625 * out of the main execution path
626 */
627 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
628 | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
629 if (status & ATMEL_US_RXBRK) {
630 /* ignore side-effect */
631 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
632
633 port->icount.brk++;
634 if (uart_handle_break(port))
635 continue;
636 }
637 if (status & ATMEL_US_PARE)
638 port->icount.parity++;
639 if (status & ATMEL_US_FRAME)
640 port->icount.frame++;
641 if (status & ATMEL_US_OVRE)
642 port->icount.overrun++;
643
644 status &= port->read_status_mask;
645
646 if (status & ATMEL_US_RXBRK)
647 flg = TTY_BREAK;
648 else if (status & ATMEL_US_PARE)
649 flg = TTY_PARITY;
650 else if (status & ATMEL_US_FRAME)
651 flg = TTY_FRAME;
652 }
653
654
655 if (uart_handle_sysrq_char(port, c.ch))
656 continue;
657
658 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
659 }
660
661 /*
662 * Drop the lock here since it might end up calling
663 * uart_start(), which takes the lock.
664 */
665 spin_unlock(&port->lock);
Haavard Skinnemoenb1d16192008-07-16 21:56:29 +0100666 tty_flip_buffer_push(port->info->port.tty);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800667 spin_lock(&port->lock);
668}
669
Chip Coldwella6670612008-02-08 04:21:06 -0800670static void atmel_rx_from_dma(struct uart_port *port)
671{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800672 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Haavard Skinnemoenb1d16192008-07-16 21:56:29 +0100673 struct tty_struct *tty = port->info->port.tty;
Chip Coldwella6670612008-02-08 04:21:06 -0800674 struct atmel_dma_buffer *pdc;
675 int rx_idx = atmel_port->pdc_rx_idx;
676 unsigned int head;
677 unsigned int tail;
678 unsigned int count;
679
680 do {
681 /* Reset the UART timeout early so that we don't miss one */
682 UART_PUT_CR(port, ATMEL_US_STTTO);
683
684 pdc = &atmel_port->pdc_rx[rx_idx];
685 head = UART_GET_RPR(port) - pdc->dma_addr;
686 tail = pdc->ofs;
687
688 /* If the PDC has switched buffers, RPR won't contain
689 * any address within the current buffer. Since head
690 * is unsigned, we just need a one-way comparison to
691 * find out.
692 *
693 * In this case, we just need to consume the entire
694 * buffer and resubmit it for DMA. This will clear the
695 * ENDRX bit as well, so that we can safely re-enable
696 * all interrupts below.
697 */
698 head = min(head, pdc->dma_size);
699
700 if (likely(head != tail)) {
701 dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
702 pdc->dma_size, DMA_FROM_DEVICE);
703
704 /*
705 * head will only wrap around when we recycle
706 * the DMA buffer, and when that happens, we
707 * explicitly set tail to 0. So head will
708 * always be greater than tail.
709 */
710 count = head - tail;
711
712 tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
713
714 dma_sync_single_for_device(port->dev, pdc->dma_addr,
715 pdc->dma_size, DMA_FROM_DEVICE);
716
717 port->icount.rx += count;
718 pdc->ofs = head;
719 }
720
721 /*
722 * If the current buffer is full, we need to check if
723 * the next one contains any additional data.
724 */
725 if (head >= pdc->dma_size) {
726 pdc->ofs = 0;
727 UART_PUT_RNPR(port, pdc->dma_addr);
728 UART_PUT_RNCR(port, pdc->dma_size);
729
730 rx_idx = !rx_idx;
731 atmel_port->pdc_rx_idx = rx_idx;
732 }
733 } while (head >= pdc->dma_size);
734
735 /*
736 * Drop the lock here since it might end up calling
737 * uart_start(), which takes the lock.
738 */
739 spin_unlock(&port->lock);
740 tty_flip_buffer_push(tty);
741 spin_lock(&port->lock);
742
743 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
744}
745
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800746/*
747 * tasklet handling tty stuff outside the interrupt handler.
748 */
749static void atmel_tasklet_func(unsigned long data)
750{
751 struct uart_port *port = (struct uart_port *)data;
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800752 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800753 unsigned int status;
754 unsigned int status_change;
755
756 /* The interrupt handler does not take the lock */
757 spin_lock(&port->lock);
758
Chip Coldwella6670612008-02-08 04:21:06 -0800759 if (atmel_use_dma_tx(port))
760 atmel_tx_dma(port);
761 else
762 atmel_tx_chars(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800763
764 status = atmel_port->irq_status;
765 status_change = status ^ atmel_port->irq_status_prev;
766
767 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
768 | ATMEL_US_DCD | ATMEL_US_CTS)) {
769 /* TODO: All reads to CSR will clear these interrupts! */
770 if (status_change & ATMEL_US_RI)
771 port->icount.rng++;
772 if (status_change & ATMEL_US_DSR)
773 port->icount.dsr++;
774 if (status_change & ATMEL_US_DCD)
775 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
776 if (status_change & ATMEL_US_CTS)
777 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
778
779 wake_up_interruptible(&port->info->delta_msr_wait);
780
781 atmel_port->irq_status_prev = status;
782 }
783
Chip Coldwella6670612008-02-08 04:21:06 -0800784 if (atmel_use_dma_rx(port))
785 atmel_rx_from_dma(port);
786 else
787 atmel_rx_from_ring(port);
Remy Bohmer1ecc26b2008-02-08 04:21:05 -0800788
789 spin_unlock(&port->lock);
790}
791
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000792/*
793 * Perform initialization and enable port for reception
794 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200795static int atmel_startup(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000796{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800797 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Haavard Skinnemoenb1d16192008-07-16 21:56:29 +0100798 struct tty_struct *tty = port->info->port.tty;
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000799 int retval;
800
801 /*
802 * Ensure that no interrupts are enabled otherwise when
803 * request_irq() is called we could get stuck trying to
804 * handle an unexpected interrupt
805 */
806 UART_PUT_IDR(port, -1);
807
808 /*
809 * Allocate the IRQ
810 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800811 retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
Haavard Skinnemoenae161062008-02-08 04:21:08 -0800812 tty ? tty->name : "atmel_serial", port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000813 if (retval) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200814 printk("atmel_serial: atmel_startup - Can't get irq\n");
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000815 return retval;
816 }
817
818 /*
Chip Coldwella6670612008-02-08 04:21:06 -0800819 * Initialize DMA (if necessary)
820 */
821 if (atmel_use_dma_rx(port)) {
822 int i;
823
824 for (i = 0; i < 2; i++) {
825 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
826
827 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
828 if (pdc->buf == NULL) {
829 if (i != 0) {
830 dma_unmap_single(port->dev,
831 atmel_port->pdc_rx[0].dma_addr,
832 PDC_BUFFER_SIZE,
833 DMA_FROM_DEVICE);
834 kfree(atmel_port->pdc_rx[0].buf);
835 }
836 free_irq(port->irq, port);
837 return -ENOMEM;
838 }
839 pdc->dma_addr = dma_map_single(port->dev,
840 pdc->buf,
841 PDC_BUFFER_SIZE,
842 DMA_FROM_DEVICE);
843 pdc->dma_size = PDC_BUFFER_SIZE;
844 pdc->ofs = 0;
845 }
846
847 atmel_port->pdc_rx_idx = 0;
848
849 UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
850 UART_PUT_RCR(port, PDC_BUFFER_SIZE);
851
852 UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
853 UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
854 }
855 if (atmel_use_dma_tx(port)) {
856 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
857 struct circ_buf *xmit = &port->info->xmit;
858
859 pdc->buf = xmit->buf;
860 pdc->dma_addr = dma_map_single(port->dev,
861 pdc->buf,
862 UART_XMIT_SIZE,
863 DMA_TO_DEVICE);
864 pdc->dma_size = UART_XMIT_SIZE;
865 pdc->ofs = 0;
866 }
867
868 /*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000869 * If there is a specific "open" function (to register
870 * control line interrupts)
871 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200872 if (atmel_open_hook) {
873 retval = atmel_open_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000874 if (retval) {
875 free_irq(port->irq, port);
876 return retval;
877 }
878 }
879
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000880 /*
881 * Finally, enable the serial port
882 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200883 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800884 /* enable xmit & rcvr */
885 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victorafefc412006-06-19 19:53:19 +0100886
Chip Coldwella6670612008-02-08 04:21:06 -0800887 if (atmel_use_dma_rx(port)) {
888 /* set UART timeout */
889 UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
890 UART_PUT_CR(port, ATMEL_US_STTTO);
891
892 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
893 /* enable PDC controller */
894 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
895 } else {
896 /* enable receive only */
897 UART_PUT_IER(port, ATMEL_US_RXRDY);
898 }
Andrew Victorafefc412006-06-19 19:53:19 +0100899
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000900 return 0;
901}
902
903/*
904 * Disable the port
905 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200906static void atmel_shutdown(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000907{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800908 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Chip Coldwella6670612008-02-08 04:21:06 -0800909 /*
910 * Ensure everything is stopped.
911 */
912 atmel_stop_rx(port);
913 atmel_stop_tx(port);
914
915 /*
916 * Shut-down the DMA.
917 */
918 if (atmel_use_dma_rx(port)) {
919 int i;
920
921 for (i = 0; i < 2; i++) {
922 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
923
924 dma_unmap_single(port->dev,
925 pdc->dma_addr,
926 pdc->dma_size,
927 DMA_FROM_DEVICE);
928 kfree(pdc->buf);
929 }
930 }
931 if (atmel_use_dma_tx(port)) {
932 struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
933
934 dma_unmap_single(port->dev,
935 pdc->dma_addr,
936 pdc->dma_size,
937 DMA_TO_DEVICE);
938 }
939
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000940 /*
941 * Disable all interrupts, port and break condition.
942 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +0200943 UART_PUT_CR(port, ATMEL_US_RSTSTA);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000944 UART_PUT_IDR(port, -1);
945
946 /*
947 * Free the interrupt
948 */
949 free_irq(port->irq, port);
950
951 /*
952 * If there is a specific "close" function (to unregister
953 * control line interrupts)
954 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +0200955 if (atmel_close_hook)
956 atmel_close_hook(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000957}
958
959/*
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +0100960 * Flush any TX data submitted for DMA. Called when the TX circular
961 * buffer is reset.
962 */
963static void atmel_flush_buffer(struct uart_port *port)
964{
965 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
966
967 if (atmel_use_dma_tx(port)) {
968 UART_PUT_TCR(port, 0);
969 atmel_port->pdc_tx.ofs = 0;
970 }
971}
972
973/*
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000974 * Power / Clock management.
975 */
Remy Bohmerb843aa22008-02-08 04:21:01 -0800976static void atmel_serial_pm(struct uart_port *port, unsigned int state,
977 unsigned int oldstate)
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000978{
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -0800979 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +0100980
Andrew Victor1e6c9c22006-01-10 16:59:27 +0000981 switch (state) {
Remy Bohmerb843aa22008-02-08 04:21:01 -0800982 case 0:
983 /*
984 * Enable the peripheral clock for this serial port.
985 * This is called on uart_open() or a resume event.
986 */
987 clk_enable(atmel_port->clk);
Anti Sullinf05596d2008-09-22 13:57:54 -0700988
989 /* re-enable interrupts if we disabled some on suspend */
990 UART_PUT_IER(port, atmel_port->backup_imr);
Remy Bohmerb843aa22008-02-08 04:21:01 -0800991 break;
992 case 3:
Anti Sullinf05596d2008-09-22 13:57:54 -0700993 /* Back up the interrupt mask and disable all interrupts */
994 atmel_port->backup_imr = UART_GET_IMR(port);
995 UART_PUT_IDR(port, -1);
996
Remy Bohmerb843aa22008-02-08 04:21:01 -0800997 /*
998 * Disable the peripheral clock for this serial port.
999 * This is called on uart_close() or a suspend event.
1000 */
1001 clk_disable(atmel_port->clk);
1002 break;
1003 default:
1004 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001005 }
1006}
1007
1008/*
1009 * Change the port parameters
1010 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001011static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1012 struct ktermios *old)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001013{
1014 unsigned long flags;
1015 unsigned int mode, imr, quot, baud;
1016
Andrew Victor03abeac2007-05-03 12:26:24 +01001017 /* Get current mode register */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001018 mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1019 | ATMEL_US_NBSTOP | ATMEL_US_PAR);
Andrew Victor03abeac2007-05-03 12:26:24 +01001020
Remy Bohmerb843aa22008-02-08 04:21:01 -08001021 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001022 quot = uart_get_divisor(port, baud);
1023
Remy Bohmerb843aa22008-02-08 04:21:01 -08001024 if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
Andrew Victor03abeac2007-05-03 12:26:24 +01001025 quot /= 8;
1026 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1027 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001028
1029 /* byte size */
1030 switch (termios->c_cflag & CSIZE) {
1031 case CS5:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001032 mode |= ATMEL_US_CHRL_5;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001033 break;
1034 case CS6:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001035 mode |= ATMEL_US_CHRL_6;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001036 break;
1037 case CS7:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001038 mode |= ATMEL_US_CHRL_7;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001039 break;
1040 default:
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001041 mode |= ATMEL_US_CHRL_8;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001042 break;
1043 }
1044
1045 /* stop bits */
1046 if (termios->c_cflag & CSTOPB)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001047 mode |= ATMEL_US_NBSTOP_2;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001048
1049 /* parity */
1050 if (termios->c_cflag & PARENB) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001051 /* Mark or Space parity */
1052 if (termios->c_cflag & CMSPAR) {
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001053 if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001054 mode |= ATMEL_US_PAR_MARK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001055 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001056 mode |= ATMEL_US_PAR_SPACE;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001057 } else if (termios->c_cflag & PARODD)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001058 mode |= ATMEL_US_PAR_ODD;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001059 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001060 mode |= ATMEL_US_PAR_EVEN;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001061 } else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001062 mode |= ATMEL_US_PAR_NONE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001063
1064 spin_lock_irqsave(&port->lock, flags);
1065
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001066 port->read_status_mask = ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001067 if (termios->c_iflag & INPCK)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001068 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001069 if (termios->c_iflag & (BRKINT | PARMRK))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001070 port->read_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001071
Chip Coldwella6670612008-02-08 04:21:06 -08001072 if (atmel_use_dma_rx(port))
1073 /* need to enable error interrupts */
1074 UART_PUT_IER(port, port->read_status_mask);
1075
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001076 /*
1077 * Characters to ignore
1078 */
1079 port->ignore_status_mask = 0;
1080 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001081 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001082 if (termios->c_iflag & IGNBRK) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001083 port->ignore_status_mask |= ATMEL_US_RXBRK;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001084 /*
1085 * If we're ignoring parity and break indicators,
1086 * ignore overruns too (for real raw support).
1087 */
1088 if (termios->c_iflag & IGNPAR)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001089 port->ignore_status_mask |= ATMEL_US_OVRE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001090 }
Remy Bohmerb843aa22008-02-08 04:21:01 -08001091 /* TODO: Ignore all characters if CREAD is set.*/
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001092
1093 /* update the per-port timeout */
1094 uart_update_timeout(port, termios->c_cflag, baud);
1095
Remy Bohmerb843aa22008-02-08 04:21:01 -08001096 /* save/disable interrupts and drain transmitter */
1097 imr = UART_GET_IMR(port);
1098 UART_PUT_IDR(port, -1);
1099 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08001100 cpu_relax();
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001101
1102 /* disable receiver and transmitter */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001103 UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001104
1105 /* set the parity, stop bits and data size */
1106 UART_PUT_MR(port, mode);
1107
1108 /* set the baud rate */
1109 UART_PUT_BRGR(port, quot);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001110 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1111 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001112
1113 /* restore interrupts */
1114 UART_PUT_IER(port, imr);
1115
1116 /* CTS flow-control and modem-status interrupts */
1117 if (UART_ENABLE_MS(port, termios->c_cflag))
1118 port->ops->enable_ms(port);
1119
1120 spin_unlock_irqrestore(&port->lock, flags);
1121}
1122
1123/*
1124 * Return string describing the specified port
1125 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001126static const char *atmel_type(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001127{
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001128 return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001129}
1130
1131/*
1132 * Release the memory region(s) being used by 'port'.
1133 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001134static void atmel_release_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001135{
Andrew Victorafefc412006-06-19 19:53:19 +01001136 struct platform_device *pdev = to_platform_device(port->dev);
1137 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1138
1139 release_mem_region(port->mapbase, size);
1140
1141 if (port->flags & UPF_IOREMAP) {
1142 iounmap(port->membase);
1143 port->membase = NULL;
1144 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001145}
1146
1147/*
1148 * Request the memory region(s) being used by 'port'.
1149 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001150static int atmel_request_port(struct uart_port *port)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001151{
Andrew Victorafefc412006-06-19 19:53:19 +01001152 struct platform_device *pdev = to_platform_device(port->dev);
1153 int size = pdev->resource[0].end - pdev->resource[0].start + 1;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001154
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001155 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
Andrew Victorafefc412006-06-19 19:53:19 +01001156 return -EBUSY;
1157
1158 if (port->flags & UPF_IOREMAP) {
1159 port->membase = ioremap(port->mapbase, size);
1160 if (port->membase == NULL) {
1161 release_mem_region(port->mapbase, size);
1162 return -ENOMEM;
1163 }
1164 }
1165
1166 return 0;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001167}
1168
1169/*
1170 * Configure/autoconfigure the port.
1171 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001172static void atmel_config_port(struct uart_port *port, int flags)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001173{
1174 if (flags & UART_CONFIG_TYPE) {
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001175 port->type = PORT_ATMEL;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001176 atmel_request_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001177 }
1178}
1179
1180/*
1181 * Verify the new serial_struct (for TIOCSSERIAL).
1182 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001183static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001184{
1185 int ret = 0;
Haavard Skinnemoen9ab4f882006-10-04 16:02:06 +02001186 if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001187 ret = -EINVAL;
1188 if (port->irq != ser->irq)
1189 ret = -EINVAL;
1190 if (ser->io_type != SERIAL_IO_MEM)
1191 ret = -EINVAL;
1192 if (port->uartclk / 16 != ser->baud_base)
1193 ret = -EINVAL;
1194 if ((void *)port->mapbase != ser->iomem_base)
1195 ret = -EINVAL;
1196 if (port->iobase != ser->port)
1197 ret = -EINVAL;
1198 if (ser->hub6 != 0)
1199 ret = -EINVAL;
1200 return ret;
1201}
1202
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001203static struct uart_ops atmel_pops = {
1204 .tx_empty = atmel_tx_empty,
1205 .set_mctrl = atmel_set_mctrl,
1206 .get_mctrl = atmel_get_mctrl,
1207 .stop_tx = atmel_stop_tx,
1208 .start_tx = atmel_start_tx,
1209 .stop_rx = atmel_stop_rx,
1210 .enable_ms = atmel_enable_ms,
1211 .break_ctl = atmel_break_ctl,
1212 .startup = atmel_startup,
1213 .shutdown = atmel_shutdown,
Haavard Skinnemoen9afd5612008-07-16 21:52:46 +01001214 .flush_buffer = atmel_flush_buffer,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001215 .set_termios = atmel_set_termios,
1216 .type = atmel_type,
1217 .release_port = atmel_release_port,
1218 .request_port = atmel_request_port,
1219 .config_port = atmel_config_port,
1220 .verify_port = atmel_verify_port,
1221 .pm = atmel_serial_pm,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001222};
1223
Andrew Victorafefc412006-06-19 19:53:19 +01001224/*
1225 * Configure the port from the platform device resource info.
1226 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001227static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
1228 struct platform_device *pdev)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001229{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001230 struct uart_port *port = &atmel_port->uart;
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02001231 struct atmel_uart_data *data = pdev->dev.platform_data;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001232
Andrew Victorafefc412006-06-19 19:53:19 +01001233 port->iotype = UPIO_MEM;
Andrew Victora14d5272007-01-09 09:03:42 +01001234 port->flags = UPF_BOOT_AUTOCONF;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001235 port->ops = &atmel_pops;
Andrew Victora14d5272007-01-09 09:03:42 +01001236 port->fifosize = 1;
Andrew Victorafefc412006-06-19 19:53:19 +01001237 port->line = pdev->id;
1238 port->dev = &pdev->dev;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001239
Andrew Victorafefc412006-06-19 19:53:19 +01001240 port->mapbase = pdev->resource[0].start;
1241 port->irq = pdev->resource[1].start;
1242
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001243 tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
1244 (unsigned long)port);
1245
1246 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
1247
Haavard Skinnemoen75d35212006-10-04 16:02:08 +02001248 if (data->regs)
1249 /* Already mapped by setup code */
1250 port->membase = data->regs;
Andrew Victorafefc412006-06-19 19:53:19 +01001251 else {
1252 port->flags |= UPF_IOREMAP;
1253 port->membase = NULL;
1254 }
1255
Remy Bohmerb843aa22008-02-08 04:21:01 -08001256 /* for console, the clock could already be configured */
1257 if (!atmel_port->clk) {
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001258 atmel_port->clk = clk_get(&pdev->dev, "usart");
1259 clk_enable(atmel_port->clk);
1260 port->uartclk = clk_get_rate(atmel_port->clk);
David Brownell06a7f052008-11-06 12:53:40 -08001261 clk_disable(atmel_port->clk);
1262 /* only enable clock when USART is in use */
Andrew Victorafefc412006-06-19 19:53:19 +01001263 }
Chip Coldwella6670612008-02-08 04:21:06 -08001264
1265 atmel_port->use_dma_rx = data->use_dma_rx;
1266 atmel_port->use_dma_tx = data->use_dma_tx;
1267 if (atmel_use_dma_tx(port))
1268 port->fifosize = PDC_BUFFER_SIZE;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001269}
1270
Andrew Victorafefc412006-06-19 19:53:19 +01001271/*
1272 * Register board-specific modem-control line handlers.
1273 */
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +02001274void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001275{
1276 if (fns->enable_ms)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001277 atmel_pops.enable_ms = fns->enable_ms;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001278 if (fns->get_mctrl)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001279 atmel_pops.get_mctrl = fns->get_mctrl;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001280 if (fns->set_mctrl)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001281 atmel_pops.set_mctrl = fns->set_mctrl;
Haavard Skinnemoen71f2e2b2006-10-04 16:02:07 +02001282 atmel_open_hook = fns->open;
1283 atmel_close_hook = fns->close;
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001284 atmel_pops.pm = fns->pm;
1285 atmel_pops.set_wake = fns->set_wake;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001286}
1287
Haavard Skinnemoen749c4e62006-10-04 16:02:02 +02001288#ifdef CONFIG_SERIAL_ATMEL_CONSOLE
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001289static void atmel_console_putchar(struct uart_port *port, int ch)
Russell Kingd3587882006-03-20 20:00:09 +00001290{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001291 while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
Haavard Skinnemoen829dd812008-02-08 04:21:02 -08001292 cpu_relax();
Russell Kingd3587882006-03-20 20:00:09 +00001293 UART_PUT_CHAR(port, ch);
1294}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001295
1296/*
1297 * Interrupts are disabled on entering
1298 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001299static void atmel_console_write(struct console *co, const char *s, u_int count)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001300{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001301 struct uart_port *port = &atmel_ports[co->index].uart;
Russell Kingd3587882006-03-20 20:00:09 +00001302 unsigned int status, imr;
Marc Pignat39d4c922008-04-02 13:04:42 -07001303 unsigned int pdc_tx;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001304
1305 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001306 * First, save IMR and then disable interrupts
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001307 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001308 imr = UART_GET_IMR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001309 UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001310
Marc Pignat39d4c922008-04-02 13:04:42 -07001311 /* Store PDC transmit status and disable it */
1312 pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
1313 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1314
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001315 uart_console_write(port, s, count, atmel_console_putchar);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001316
1317 /*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001318 * Finally, wait for transmitter to become empty
1319 * and restore IMR
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001320 */
1321 do {
1322 status = UART_GET_CSR(port);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001323 } while (!(status & ATMEL_US_TXRDY));
Marc Pignat39d4c922008-04-02 13:04:42 -07001324
1325 /* Restore PDC transmit status */
1326 if (pdc_tx)
1327 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1328
Remy Bohmerb843aa22008-02-08 04:21:01 -08001329 /* set interrupts back the way they were */
1330 UART_PUT_IER(port, imr);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001331}
1332
1333/*
Remy Bohmerb843aa22008-02-08 04:21:01 -08001334 * If the port was already initialised (eg, by a boot loader),
1335 * try to determine the current setup.
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001336 */
Remy Bohmerb843aa22008-02-08 04:21:01 -08001337static void __init atmel_console_get_options(struct uart_port *port, int *baud,
1338 int *parity, int *bits)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001339{
1340 unsigned int mr, quot;
1341
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08001342 /*
1343 * If the baud rate generator isn't running, the port wasn't
1344 * initialized by the boot loader.
1345 */
Haavard Skinnemoen9c81c5c2008-06-05 22:46:39 -07001346 quot = UART_GET_BRGR(port) & ATMEL_US_CD;
Haavard Skinnemoen1c0fd822008-02-08 04:21:03 -08001347 if (!quot)
1348 return;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001349
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001350 mr = UART_GET_MR(port) & ATMEL_US_CHRL;
1351 if (mr == ATMEL_US_CHRL_8)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001352 *bits = 8;
1353 else
1354 *bits = 7;
1355
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001356 mr = UART_GET_MR(port) & ATMEL_US_PAR;
1357 if (mr == ATMEL_US_PAR_EVEN)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001358 *parity = 'e';
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001359 else if (mr == ATMEL_US_PAR_ODD)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001360 *parity = 'o';
1361
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02001362 /*
1363 * The serial core only rounds down when matching this to a
1364 * supported baud rate. Make sure we don't end up slightly
1365 * lower than one of those, as it would make us fall through
1366 * to a much lower baud rate than we really want.
1367 */
Haavard Skinnemoen4d5e3922006-10-04 16:02:11 +02001368 *baud = port->uartclk / (16 * (quot - 1));
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001369}
1370
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001371static int __init atmel_console_setup(struct console *co, char *options)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001372{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001373 struct uart_port *port = &atmel_ports[co->index].uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001374 int baud = 115200;
1375 int bits = 8;
1376 int parity = 'n';
1377 int flow = 'n';
1378
Remy Bohmerb843aa22008-02-08 04:21:01 -08001379 if (port->membase == NULL) {
1380 /* Port not initialized yet - delay setup */
Andrew Victorafefc412006-06-19 19:53:19 +01001381 return -ENODEV;
Remy Bohmerb843aa22008-02-08 04:21:01 -08001382 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001383
David Brownell06a7f052008-11-06 12:53:40 -08001384 clk_enable(atmel_ports[co->index].clk);
1385
Remy Bohmerb843aa22008-02-08 04:21:01 -08001386 UART_PUT_IDR(port, -1);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001387 UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1388 UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001389
1390 if (options)
1391 uart_parse_options(options, &baud, &parity, &bits, &flow);
1392 else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001393 atmel_console_get_options(port, &baud, &parity, &bits);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001394
1395 return uart_set_options(port, co, baud, parity, bits, flow);
1396}
1397
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001398static struct uart_driver atmel_uart;
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001399
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001400static struct console atmel_console = {
1401 .name = ATMEL_DEVICENAME,
1402 .write = atmel_console_write,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001403 .device = uart_console_device,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001404 .setup = atmel_console_setup,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001405 .flags = CON_PRINTBUFFER,
1406 .index = -1,
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001407 .data = &atmel_uart,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001408};
1409
David Brownell06a7f052008-11-06 12:53:40 -08001410#define ATMEL_CONSOLE_DEVICE (&atmel_console)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001411
Andrew Victorafefc412006-06-19 19:53:19 +01001412/*
1413 * Early console initialization (before VM subsystem initialized).
1414 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001415static int __init atmel_console_init(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001416{
Haavard Skinnemoen73e27982006-10-04 16:02:04 +02001417 if (atmel_default_console_device) {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001418 add_preferred_console(ATMEL_DEVICENAME,
1419 atmel_default_console_device->id, NULL);
1420 atmel_init_port(&atmel_ports[atmel_default_console_device->id],
1421 atmel_default_console_device);
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001422 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01001423 }
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001424
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001425 return 0;
1426}
Remy Bohmerb843aa22008-02-08 04:21:01 -08001427
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001428console_initcall(atmel_console_init);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001429
Andrew Victorafefc412006-06-19 19:53:19 +01001430/*
1431 * Late console initialization.
1432 */
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001433static int __init atmel_late_console_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01001434{
Remy Bohmerb843aa22008-02-08 04:21:01 -08001435 if (atmel_default_console_device
1436 && !(atmel_console.flags & CON_ENABLED))
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001437 register_console(&atmel_console);
Andrew Victorafefc412006-06-19 19:53:19 +01001438
1439 return 0;
1440}
Remy Bohmerb843aa22008-02-08 04:21:01 -08001441
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001442core_initcall(atmel_late_console_init);
Andrew Victorafefc412006-06-19 19:53:19 +01001443
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001444static inline bool atmel_is_console_port(struct uart_port *port)
1445{
1446 return port->cons && port->cons->index == port->line;
1447}
1448
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001449#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001450#define ATMEL_CONSOLE_DEVICE NULL
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001451
1452static inline bool atmel_is_console_port(struct uart_port *port)
1453{
1454 return false;
1455}
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001456#endif
1457
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001458static struct uart_driver atmel_uart = {
Remy Bohmerb843aa22008-02-08 04:21:01 -08001459 .owner = THIS_MODULE,
1460 .driver_name = "atmel_serial",
1461 .dev_name = ATMEL_DEVICENAME,
1462 .major = SERIAL_ATMEL_MAJOR,
1463 .minor = MINOR_START,
1464 .nr = ATMEL_MAX_UART,
1465 .cons = ATMEL_CONSOLE_DEVICE,
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001466};
1467
Andrew Victorafefc412006-06-19 19:53:19 +01001468#ifdef CONFIG_PM
Haavard Skinnemoenf826caa2008-02-24 14:34:45 +01001469static bool atmel_serial_clk_will_stop(void)
1470{
1471#ifdef CONFIG_ARCH_AT91
1472 return at91_suspend_entering_slow_clock();
1473#else
1474 return false;
1475#endif
1476}
1477
Remy Bohmerb843aa22008-02-08 04:21:01 -08001478static int atmel_serial_suspend(struct platform_device *pdev,
1479 pm_message_t state)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001480{
Andrew Victorafefc412006-06-19 19:53:19 +01001481 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001482 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001483
Haavard Skinnemoene1c609e2008-03-14 14:54:13 +01001484 if (atmel_is_console_port(port) && console_suspend_enabled) {
1485 /* Drain the TX shifter */
1486 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
1487 cpu_relax();
1488 }
1489
Anti Sullinf05596d2008-09-22 13:57:54 -07001490 /* we can not wake up if we're running on slow clock */
1491 atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
1492 if (atmel_serial_clk_will_stop())
1493 device_set_wakeup_enable(&pdev->dev, 0);
1494
1495 uart_suspend_port(&atmel_uart, port);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001496
1497 return 0;
1498}
1499
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001500static int atmel_serial_resume(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01001501{
1502 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001503 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001504
Anti Sullinf05596d2008-09-22 13:57:54 -07001505 uart_resume_port(&atmel_uart, port);
1506 device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
Andrew Victorafefc412006-06-19 19:53:19 +01001507
1508 return 0;
1509}
1510#else
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001511#define atmel_serial_suspend NULL
1512#define atmel_serial_resume NULL
Andrew Victorafefc412006-06-19 19:53:19 +01001513#endif
1514
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001515static int __devinit atmel_serial_probe(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01001516{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001517 struct atmel_uart_port *port;
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001518 void *data;
Andrew Victorafefc412006-06-19 19:53:19 +01001519 int ret;
1520
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001521 BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE));
1522
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001523 port = &atmel_ports[pdev->id];
Anti Sullinf05596d2008-09-22 13:57:54 -07001524 port->backup_imr = 0;
1525
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001526 atmel_init_port(port, pdev);
Andrew Victorafefc412006-06-19 19:53:19 +01001527
Chip Coldwella6670612008-02-08 04:21:06 -08001528 if (!atmel_use_dma_rx(&port->uart)) {
1529 ret = -ENOMEM;
Haavard Skinnemoen64334712008-02-08 04:21:07 -08001530 data = kmalloc(sizeof(struct atmel_uart_char)
1531 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
Chip Coldwella6670612008-02-08 04:21:06 -08001532 if (!data)
1533 goto err_alloc_ring;
1534 port->rx_ring.buf = data;
1535 }
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001536
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001537 ret = uart_add_one_port(&atmel_uart, &port->uart);
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001538 if (ret)
1539 goto err_add_port;
1540
David Brownell06a7f052008-11-06 12:53:40 -08001541 if (atmel_is_console_port(&port->uart)
1542 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
1543 /*
1544 * The serial core enabled the clock for us, so undo
1545 * the clk_enable() in atmel_console_setup()
1546 */
1547 clk_disable(port->clk);
1548 }
1549
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001550 device_init_wakeup(&pdev->dev, 1);
1551 platform_set_drvdata(pdev, port);
1552
1553 return 0;
1554
1555err_add_port:
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001556 kfree(port->rx_ring.buf);
1557 port->rx_ring.buf = NULL;
1558err_alloc_ring:
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001559 if (!atmel_is_console_port(&port->uart)) {
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001560 clk_put(port->clk);
1561 port->clk = NULL;
Andrew Victorafefc412006-06-19 19:53:19 +01001562 }
1563
1564 return ret;
1565}
1566
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001567static int __devexit atmel_serial_remove(struct platform_device *pdev)
Andrew Victorafefc412006-06-19 19:53:19 +01001568{
1569 struct uart_port *port = platform_get_drvdata(pdev);
Haavard Skinnemoenc811ab82008-02-08 04:21:08 -08001570 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
Andrew Victorafefc412006-06-19 19:53:19 +01001571 int ret = 0;
1572
Andrew Victorafefc412006-06-19 19:53:19 +01001573 device_init_wakeup(&pdev->dev, 0);
1574 platform_set_drvdata(pdev, NULL);
1575
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001576 ret = uart_remove_one_port(&atmel_uart, port);
1577
Remy Bohmer1ecc26b2008-02-08 04:21:05 -08001578 tasklet_kill(&atmel_port->tasklet);
1579 kfree(atmel_port->rx_ring.buf);
1580
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001581 /* "port" is allocated statically, so we shouldn't free it */
1582
Haavard Skinnemoendfa7f342008-02-08 04:21:04 -08001583 clk_put(atmel_port->clk);
Andrew Victorafefc412006-06-19 19:53:19 +01001584
1585 return ret;
1586}
1587
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001588static struct platform_driver atmel_serial_driver = {
1589 .probe = atmel_serial_probe,
1590 .remove = __devexit_p(atmel_serial_remove),
1591 .suspend = atmel_serial_suspend,
1592 .resume = atmel_serial_resume,
Andrew Victorafefc412006-06-19 19:53:19 +01001593 .driver = {
Haavard Skinnemoen1e8ea802006-10-04 16:02:03 +02001594 .name = "atmel_usart",
Andrew Victorafefc412006-06-19 19:53:19 +01001595 .owner = THIS_MODULE,
1596 },
1597};
1598
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001599static int __init atmel_serial_init(void)
Andrew Victorafefc412006-06-19 19:53:19 +01001600{
1601 int ret;
1602
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001603 ret = uart_register_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01001604 if (ret)
1605 return ret;
1606
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001607 ret = platform_driver_register(&atmel_serial_driver);
Andrew Victorafefc412006-06-19 19:53:19 +01001608 if (ret)
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001609 uart_unregister_driver(&atmel_uart);
Andrew Victorafefc412006-06-19 19:53:19 +01001610
1611 return ret;
1612}
1613
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001614static void __exit atmel_serial_exit(void)
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001615{
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001616 platform_driver_unregister(&atmel_serial_driver);
1617 uart_unregister_driver(&atmel_uart);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001618}
1619
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001620module_init(atmel_serial_init);
1621module_exit(atmel_serial_exit);
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001622
1623MODULE_AUTHOR("Rick Bronson");
Haavard Skinnemoen7192f922006-10-04 16:02:05 +02001624MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
Andrew Victor1e6c9c22006-01-10 16:59:27 +00001625MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001626MODULE_ALIAS("platform:atmel_usart");