]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/tty/serial/atmel_serial.c
Merge remote-tracking branch 'input/next'
[karo-tx-linux.git] / drivers / tty / serial / atmel_serial.c
1 /*
2  *  Driver for Atmel AT91 / AT32 Serial ports
3  *  Copyright (C) 2003 Rick Bronson
4  *
5  *  Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
6  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7  *
8  *  DMA support added by Chip Coldwell.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  */
25 #include <linux/module.h>
26 #include <linux/tty.h>
27 #include <linux/ioport.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/serial.h>
31 #include <linux/clk.h>
32 #include <linux/console.h>
33 #include <linux/sysrq.h>
34 #include <linux/tty_flip.h>
35 #include <linux/platform_device.h>
36 #include <linux/of.h>
37 #include <linux/of_device.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/atmel_pdc.h>
40 #include <linux/atmel_serial.h>
41 #include <linux/uaccess.h>
42 #include <linux/platform_data/atmel.h>
43 #include <linux/timer.h>
44
45 #include <asm/io.h>
46 #include <asm/ioctls.h>
47
48 #ifdef CONFIG_ARM
49 #include <mach/cpu.h>
50 #include <asm/gpio.h>
51 #endif
52
53 #define PDC_BUFFER_SIZE         512
54 /* Revisit: We should calculate this based on the actual port settings */
55 #define PDC_RX_TIMEOUT          (3 * 10)                /* 3 bytes */
56
57 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
58 #define SUPPORT_SYSRQ
59 #endif
60
61 #include <linux/serial_core.h>
62
63 static void atmel_start_rx(struct uart_port *port);
64 static void atmel_stop_rx(struct uart_port *port);
65
66 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
67
68 /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
69  * should coexist with the 8250 driver, such as if we have an external 16C550
70  * UART. */
71 #define SERIAL_ATMEL_MAJOR      204
72 #define MINOR_START             154
73 #define ATMEL_DEVICENAME        "ttyAT"
74
75 #else
76
77 /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
78  * name, but it is legally reserved for the 8250 driver. */
79 #define SERIAL_ATMEL_MAJOR      TTY_MAJOR
80 #define MINOR_START             64
81 #define ATMEL_DEVICENAME        "ttyS"
82
83 #endif
84
85 #define ATMEL_ISR_PASS_LIMIT    256
86
87 /* UART registers. CR is write-only, hence no GET macro */
88 #define UART_PUT_CR(port,v)     __raw_writel(v, (port)->membase + ATMEL_US_CR)
89 #define UART_GET_MR(port)       __raw_readl((port)->membase + ATMEL_US_MR)
90 #define UART_PUT_MR(port,v)     __raw_writel(v, (port)->membase + ATMEL_US_MR)
91 #define UART_PUT_IER(port,v)    __raw_writel(v, (port)->membase + ATMEL_US_IER)
92 #define UART_PUT_IDR(port,v)    __raw_writel(v, (port)->membase + ATMEL_US_IDR)
93 #define UART_GET_IMR(port)      __raw_readl((port)->membase + ATMEL_US_IMR)
94 #define UART_GET_CSR(port)      __raw_readl((port)->membase + ATMEL_US_CSR)
95 #define UART_GET_CHAR(port)     __raw_readl((port)->membase + ATMEL_US_RHR)
96 #define UART_PUT_CHAR(port,v)   __raw_writel(v, (port)->membase + ATMEL_US_THR)
97 #define UART_GET_BRGR(port)     __raw_readl((port)->membase + ATMEL_US_BRGR)
98 #define UART_PUT_BRGR(port,v)   __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
99 #define UART_PUT_RTOR(port,v)   __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
100 #define UART_PUT_TTGR(port, v)  __raw_writel(v, (port)->membase + ATMEL_US_TTGR)
101 #define UART_GET_IP_NAME(port)  __raw_readl((port)->membase + ATMEL_US_NAME)
102
103  /* PDC registers */
104 #define UART_PUT_PTCR(port,v)   __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
105 #define UART_GET_PTSR(port)     __raw_readl((port)->membase + ATMEL_PDC_PTSR)
106
107 #define UART_PUT_RPR(port,v)    __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
108 #define UART_GET_RPR(port)      __raw_readl((port)->membase + ATMEL_PDC_RPR)
109 #define UART_PUT_RCR(port,v)    __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
110 #define UART_PUT_RNPR(port,v)   __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
111 #define UART_PUT_RNCR(port,v)   __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
112
113 #define UART_PUT_TPR(port,v)    __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
114 #define UART_PUT_TCR(port,v)    __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
115 #define UART_GET_TCR(port)      __raw_readl((port)->membase + ATMEL_PDC_TCR)
116
117 static int (*atmel_open_hook)(struct uart_port *);
118 static void (*atmel_close_hook)(struct uart_port *);
119
120 struct atmel_dma_buffer {
121         unsigned char   *buf;
122         dma_addr_t      dma_addr;
123         unsigned int    dma_size;
124         unsigned int    ofs;
125 };
126
127 struct atmel_uart_char {
128         u16             status;
129         u16             ch;
130 };
131
132 #define ATMEL_SERIAL_RINGSIZE 1024
133
134 /*
135  * We wrap our port structure around the generic uart_port.
136  */
137 struct atmel_uart_port {
138         struct uart_port        uart;           /* uart */
139         struct clk              *clk;           /* uart clock */
140         int                     may_wakeup;     /* cached value of device_may_wakeup for times we need to disable it */
141         u32                     backup_imr;     /* IMR saved during suspend */
142         int                     break_active;   /* break being received */
143
144         bool                    use_dma_rx;     /* enable DMA receiver */
145         bool                    use_pdc_rx;     /* enable PDC receiver */
146         short                   pdc_rx_idx;     /* current PDC RX buffer */
147         struct atmel_dma_buffer pdc_rx[2];      /* PDC receier */
148
149         bool                    use_dma_tx;     /* enable DMA transmitter */
150         bool                    use_pdc_tx;     /* enable PDC transmitter */
151         struct atmel_dma_buffer pdc_tx;         /* PDC transmitter */
152
153         spinlock_t                      lock_tx;        /* port lock */
154         spinlock_t                      lock_rx;        /* port lock */
155         struct dma_chan                 *chan_tx;
156         struct dma_chan                 *chan_rx;
157         struct dma_async_tx_descriptor  *desc_tx;
158         struct dma_async_tx_descriptor  *desc_rx;
159         dma_cookie_t                    cookie_tx;
160         dma_cookie_t                    cookie_rx;
161         struct scatterlist              sg_tx;
162         struct scatterlist              sg_rx;
163         struct tasklet_struct   tasklet;
164         unsigned int            irq_status;
165         unsigned int            irq_status_prev;
166
167         struct circ_buf         rx_ring;
168
169         struct serial_rs485     rs485;          /* rs485 settings */
170         unsigned int            tx_done_mask;
171         bool                    is_usart;       /* usart or uart */
172         struct timer_list       uart_timer;     /* uart timer */
173         int (*prepare_rx)(struct uart_port *port);
174         int (*prepare_tx)(struct uart_port *port);
175         void (*schedule_rx)(struct uart_port *port);
176         void (*schedule_tx)(struct uart_port *port);
177         void (*release_rx)(struct uart_port *port);
178         void (*release_tx)(struct uart_port *port);
179 };
180
181 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
182 static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
183
184 #ifdef SUPPORT_SYSRQ
185 static struct console atmel_console;
186 #endif
187
188 #if defined(CONFIG_OF)
189 static const struct of_device_id atmel_serial_dt_ids[] = {
190         { .compatible = "atmel,at91rm9200-usart" },
191         { .compatible = "atmel,at91sam9260-usart" },
192         { /* sentinel */ }
193 };
194
195 MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
196 #endif
197
198 static inline struct atmel_uart_port *
199 to_atmel_uart_port(struct uart_port *uart)
200 {
201         return container_of(uart, struct atmel_uart_port, uart);
202 }
203
204 #ifdef CONFIG_SERIAL_ATMEL_PDC
205 static bool atmel_use_pdc_rx(struct uart_port *port)
206 {
207         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
208
209         return atmel_port->use_pdc_rx;
210 }
211
212 static bool atmel_use_pdc_tx(struct uart_port *port)
213 {
214         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
215
216         return atmel_port->use_pdc_tx;
217 }
218 #else
219 static bool atmel_use_pdc_rx(struct uart_port *port)
220 {
221         return false;
222 }
223
224 static bool atmel_use_pdc_tx(struct uart_port *port)
225 {
226         return false;
227 }
228 #endif
229
230 static bool atmel_use_dma_tx(struct uart_port *port)
231 {
232         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
233
234         return atmel_port->use_dma_tx;
235 }
236
237 static bool atmel_use_dma_rx(struct uart_port *port)
238 {
239         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
240
241         return atmel_port->use_dma_rx;
242 }
243
244 /* Enable or disable the rs485 support */
245 void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
246 {
247         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
248         unsigned int mode;
249         unsigned long flags;
250
251         spin_lock_irqsave(&port->lock, flags);
252
253         /* Disable interrupts */
254         UART_PUT_IDR(port, atmel_port->tx_done_mask);
255
256         mode = UART_GET_MR(port);
257
258         /* Resetting serial mode to RS232 (0x0) */
259         mode &= ~ATMEL_US_USMODE;
260
261         atmel_port->rs485 = *rs485conf;
262
263         if (rs485conf->flags & SER_RS485_ENABLED) {
264                 dev_dbg(port->dev, "Setting UART to RS485\n");
265                 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
266                 if ((rs485conf->delay_rts_after_send) > 0)
267                         UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
268                 mode |= ATMEL_US_USMODE_RS485;
269         } else {
270                 dev_dbg(port->dev, "Setting UART to RS232\n");
271                 if (atmel_use_pdc_tx(port))
272                         atmel_port->tx_done_mask = ATMEL_US_ENDTX |
273                                 ATMEL_US_TXBUFE;
274                 else
275                         atmel_port->tx_done_mask = ATMEL_US_TXRDY;
276         }
277         UART_PUT_MR(port, mode);
278
279         /* Enable interrupts */
280         UART_PUT_IER(port, atmel_port->tx_done_mask);
281
282         spin_unlock_irqrestore(&port->lock, flags);
283
284 }
285
286 /*
287  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
288  */
289 static u_int atmel_tx_empty(struct uart_port *port)
290 {
291         return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
292 }
293
294 /*
295  * Set state of the modem control output lines
296  */
297 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
298 {
299         unsigned int control = 0;
300         unsigned int mode;
301         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
302
303 #ifdef CONFIG_ARCH_AT91RM9200
304         if (cpu_is_at91rm9200()) {
305                 /*
306                  * AT91RM9200 Errata #39: RTS0 is not internally connected
307                  * to PA21. We need to drive the pin manually.
308                  */
309                 if (port->mapbase == AT91RM9200_BASE_US0) {
310                         if (mctrl & TIOCM_RTS)
311                                 at91_set_gpio_value(AT91_PIN_PA21, 0);
312                         else
313                                 at91_set_gpio_value(AT91_PIN_PA21, 1);
314                 }
315         }
316 #endif
317
318         if (mctrl & TIOCM_RTS)
319                 control |= ATMEL_US_RTSEN;
320         else
321                 control |= ATMEL_US_RTSDIS;
322
323         if (mctrl & TIOCM_DTR)
324                 control |= ATMEL_US_DTREN;
325         else
326                 control |= ATMEL_US_DTRDIS;
327
328         UART_PUT_CR(port, control);
329
330         /* Local loopback mode? */
331         mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
332         if (mctrl & TIOCM_LOOP)
333                 mode |= ATMEL_US_CHMODE_LOC_LOOP;
334         else
335                 mode |= ATMEL_US_CHMODE_NORMAL;
336
337         /* Resetting serial mode to RS232 (0x0) */
338         mode &= ~ATMEL_US_USMODE;
339
340         if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
341                 dev_dbg(port->dev, "Setting UART to RS485\n");
342                 if ((atmel_port->rs485.delay_rts_after_send) > 0)
343                         UART_PUT_TTGR(port,
344                                         atmel_port->rs485.delay_rts_after_send);
345                 mode |= ATMEL_US_USMODE_RS485;
346         } else {
347                 dev_dbg(port->dev, "Setting UART to RS232\n");
348         }
349         UART_PUT_MR(port, mode);
350 }
351
352 /*
353  * Get state of the modem control input lines
354  */
355 static u_int atmel_get_mctrl(struct uart_port *port)
356 {
357         unsigned int status, ret = 0;
358
359         status = UART_GET_CSR(port);
360
361         /*
362          * The control signals are active low.
363          */
364         if (!(status & ATMEL_US_DCD))
365                 ret |= TIOCM_CD;
366         if (!(status & ATMEL_US_CTS))
367                 ret |= TIOCM_CTS;
368         if (!(status & ATMEL_US_DSR))
369                 ret |= TIOCM_DSR;
370         if (!(status & ATMEL_US_RI))
371                 ret |= TIOCM_RI;
372
373         return ret;
374 }
375
376 /*
377  * Stop transmitting.
378  */
379 static void atmel_stop_tx(struct uart_port *port)
380 {
381         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
382
383         if (atmel_use_pdc_tx(port)) {
384                 /* disable PDC transmit */
385                 UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
386         }
387         /* Disable interrupts */
388         UART_PUT_IDR(port, atmel_port->tx_done_mask);
389
390         if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
391             !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
392                 atmel_start_rx(port);
393 }
394
395 /*
396  * Start transmitting.
397  */
398 static void atmel_start_tx(struct uart_port *port)
399 {
400         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
401
402         if (atmel_use_pdc_tx(port)) {
403                 if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
404                         /* The transmitter is already running.  Yes, we
405                            really need this.*/
406                         return;
407
408                 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
409                     !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
410                         atmel_stop_rx(port);
411
412                 /* re-enable PDC transmit */
413                 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
414         }
415         /* Enable interrupts */
416         UART_PUT_IER(port, atmel_port->tx_done_mask);
417 }
418
419 /*
420  * start receiving - port is in process of being opened.
421  */
422 static void atmel_start_rx(struct uart_port *port)
423 {
424         UART_PUT_CR(port, ATMEL_US_RSTSTA);  /* reset status and receiver */
425
426         UART_PUT_CR(port, ATMEL_US_RXEN);
427
428         if (atmel_use_pdc_rx(port)) {
429                 /* enable PDC controller */
430                 UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
431                         port->read_status_mask);
432                 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
433         } else {
434                 UART_PUT_IER(port, ATMEL_US_RXRDY);
435         }
436 }
437
438 /*
439  * Stop receiving - port is in process of being closed.
440  */
441 static void atmel_stop_rx(struct uart_port *port)
442 {
443         UART_PUT_CR(port, ATMEL_US_RXDIS);
444
445         if (atmel_use_pdc_rx(port)) {
446                 /* disable PDC receive */
447                 UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
448                 UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
449                         port->read_status_mask);
450         } else {
451                 UART_PUT_IDR(port, ATMEL_US_RXRDY);
452         }
453 }
454
455 /*
456  * Enable modem status interrupts
457  */
458 static void atmel_enable_ms(struct uart_port *port)
459 {
460         UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
461                         | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
462 }
463
464 /*
465  * Control the transmission of a break signal
466  */
467 static void atmel_break_ctl(struct uart_port *port, int break_state)
468 {
469         if (break_state != 0)
470                 UART_PUT_CR(port, ATMEL_US_STTBRK);     /* start break */
471         else
472                 UART_PUT_CR(port, ATMEL_US_STPBRK);     /* stop break */
473 }
474
475 /*
476  * Stores the incoming character in the ring buffer
477  */
478 static void
479 atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
480                      unsigned int ch)
481 {
482         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
483         struct circ_buf *ring = &atmel_port->rx_ring;
484         struct atmel_uart_char *c;
485
486         if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
487                 /* Buffer overflow, ignore char */
488                 return;
489
490         c = &((struct atmel_uart_char *)ring->buf)[ring->head];
491         c->status       = status;
492         c->ch           = ch;
493
494         /* Make sure the character is stored before we update head. */
495         smp_wmb();
496
497         ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
498 }
499
500 /*
501  * Deal with parity, framing and overrun errors.
502  */
503 static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
504 {
505         /* clear error */
506         UART_PUT_CR(port, ATMEL_US_RSTSTA);
507
508         if (status & ATMEL_US_RXBRK) {
509                 /* ignore side-effect */
510                 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
511                 port->icount.brk++;
512         }
513         if (status & ATMEL_US_PARE)
514                 port->icount.parity++;
515         if (status & ATMEL_US_FRAME)
516                 port->icount.frame++;
517         if (status & ATMEL_US_OVRE)
518                 port->icount.overrun++;
519 }
520
521 /*
522  * Characters received (called from interrupt handler)
523  */
524 static void atmel_rx_chars(struct uart_port *port)
525 {
526         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
527         unsigned int status, ch;
528
529         status = UART_GET_CSR(port);
530         while (status & ATMEL_US_RXRDY) {
531                 ch = UART_GET_CHAR(port);
532
533                 /*
534                  * note that the error handling code is
535                  * out of the main execution path
536                  */
537                 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
538                                        | ATMEL_US_OVRE | ATMEL_US_RXBRK)
539                              || atmel_port->break_active)) {
540
541                         /* clear error */
542                         UART_PUT_CR(port, ATMEL_US_RSTSTA);
543
544                         if (status & ATMEL_US_RXBRK
545                             && !atmel_port->break_active) {
546                                 atmel_port->break_active = 1;
547                                 UART_PUT_IER(port, ATMEL_US_RXBRK);
548                         } else {
549                                 /*
550                                  * This is either the end-of-break
551                                  * condition or we've received at
552                                  * least one character without RXBRK
553                                  * being set. In both cases, the next
554                                  * RXBRK will indicate start-of-break.
555                                  */
556                                 UART_PUT_IDR(port, ATMEL_US_RXBRK);
557                                 status &= ~ATMEL_US_RXBRK;
558                                 atmel_port->break_active = 0;
559                         }
560                 }
561
562                 atmel_buffer_rx_char(port, status, ch);
563                 status = UART_GET_CSR(port);
564         }
565
566         tasklet_schedule(&atmel_port->tasklet);
567 }
568
569 /*
570  * Transmit characters (called from tasklet with TXRDY interrupt
571  * disabled)
572  */
573 static void atmel_tx_chars(struct uart_port *port)
574 {
575         struct circ_buf *xmit = &port->state->xmit;
576         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
577
578         if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
579                 UART_PUT_CHAR(port, port->x_char);
580                 port->icount.tx++;
581                 port->x_char = 0;
582         }
583         if (uart_circ_empty(xmit) || uart_tx_stopped(port))
584                 return;
585
586         while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
587                 UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
588                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
589                 port->icount.tx++;
590                 if (uart_circ_empty(xmit))
591                         break;
592         }
593
594         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
595                 uart_write_wakeup(port);
596
597         if (!uart_circ_empty(xmit))
598                 /* Enable interrupts */
599                 UART_PUT_IER(port, atmel_port->tx_done_mask);
600 }
601
602 static void atmel_complete_tx_dma(void *arg)
603 {
604         struct atmel_uart_port *atmel_port = arg;
605         struct uart_port *port = &atmel_port->uart;
606         struct circ_buf *xmit = &port->state->xmit;
607         struct dma_chan *chan = atmel_port->chan_tx;
608         unsigned long flags;
609
610         spin_lock_irqsave(&port->lock, flags);
611
612         if (chan)
613                 dmaengine_terminate_all(chan);
614         xmit->tail += sg_dma_len(&atmel_port->sg_tx);
615         xmit->tail &= UART_XMIT_SIZE - 1;
616
617         port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
618
619         spin_lock_irq(&atmel_port->lock_tx);
620         async_tx_ack(atmel_port->desc_tx);
621         atmel_port->cookie_tx = -EINVAL;
622         atmel_port->desc_tx = NULL;
623         spin_unlock_irq(&atmel_port->lock_tx);
624
625         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
626                 uart_write_wakeup(port);
627
628         /* Do we really need this? */
629         if (!uart_circ_empty(xmit))
630                 tasklet_schedule(&atmel_port->tasklet);
631
632         spin_unlock_irqrestore(&port->lock, flags);
633 }
634
635 static void atmel_release_tx_dma(struct uart_port *port)
636 {
637         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
638         struct dma_chan *chan = atmel_port->chan_tx;
639
640         if (chan) {
641                 dmaengine_terminate_all(chan);
642                 dma_release_channel(chan);
643                 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
644                                 DMA_MEM_TO_DEV);
645         }
646
647         atmel_port->desc_tx = NULL;
648         atmel_port->chan_tx = NULL;
649         atmel_port->cookie_tx = -EINVAL;
650 }
651
652 /*
653  * Called from tasklet with TXRDY interrupt is disabled.
654  */
655 static void atmel_tx_dma(struct uart_port *port)
656 {
657         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
658         struct circ_buf *xmit = &port->state->xmit;
659         struct dma_chan *chan = atmel_port->chan_tx;
660         struct dma_async_tx_descriptor *desc;
661         struct scatterlist *sg = &atmel_port->sg_tx;
662
663         /* Make sure we have an idle channel */
664         if (atmel_port->desc_tx != NULL)
665                 return;
666
667         if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
668                 /*
669                  * DMA is idle now.
670                  * Port xmit buffer is already mapped,
671                  * and it is one page... Just adjust
672                  * offsets and lengths. Since it is a circular buffer,
673                  * we have to transmit till the end, and then the rest.
674                  * Take the port lock to get a
675                  * consistent xmit buffer state.
676                  */
677                 sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
678                 sg_dma_address(sg) = (sg_dma_address(sg) &
679                                         ~(UART_XMIT_SIZE - 1))
680                                         + sg->offset;
681                 sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
682                                                 xmit->tail,
683                                                 UART_XMIT_SIZE);
684                 BUG_ON(!sg_dma_len(sg));
685
686                 desc = dmaengine_prep_slave_sg(chan,
687                                                 sg,
688                                                 1,
689                                                 DMA_MEM_TO_DEV,
690                                                 DMA_PREP_INTERRUPT |
691                                                 DMA_CTRL_ACK);
692                 if (!desc) {
693                         dev_err(port->dev, "Failed to send via dma!\n");
694                         return;
695                 }
696
697                 dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
698
699                 atmel_port->desc_tx = desc;
700                 desc->callback = atmel_complete_tx_dma;
701                 desc->callback_param = atmel_port;
702                 atmel_port->cookie_tx = dmaengine_submit(desc);
703
704         } else {
705                 if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
706                         /* DMA done, stop TX, start RX for RS485 */
707                         atmel_start_rx(port);
708                 }
709         }
710
711         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
712                 uart_write_wakeup(port);
713 }
714
715 static int atmel_prepare_tx_dma(struct uart_port *port)
716 {
717         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
718         dma_cap_mask_t          mask;
719         struct dma_slave_config config;
720         int ret, nent;
721
722         dma_cap_zero(mask);
723         dma_cap_set(DMA_SLAVE, mask);
724
725         atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
726         if (atmel_port->chan_tx == NULL)
727                 goto chan_err;
728         dev_info(port->dev, "using %s for tx DMA transfers\n",
729                 dma_chan_name(atmel_port->chan_tx));
730
731         spin_lock_init(&atmel_port->lock_tx);
732         sg_init_table(&atmel_port->sg_tx, 1);
733         /* UART circular tx buffer is an aligned page. */
734         BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
735         sg_set_page(&atmel_port->sg_tx,
736                         virt_to_page(port->state->xmit.buf),
737                         UART_XMIT_SIZE,
738                         (int)port->state->xmit.buf & ~PAGE_MASK);
739         nent = dma_map_sg(port->dev,
740                                 &atmel_port->sg_tx,
741                                 1,
742                                 DMA_MEM_TO_DEV);
743
744         if (!nent) {
745                 dev_dbg(port->dev, "need to release resource of dma\n");
746                 goto chan_err;
747         } else {
748                 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
749                         sg_dma_len(&atmel_port->sg_tx),
750                         port->state->xmit.buf,
751                         sg_dma_address(&atmel_port->sg_tx));
752         }
753
754         /* Configure the slave DMA */
755         memset(&config, 0, sizeof(config));
756         config.direction = DMA_MEM_TO_DEV;
757         config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
758         config.dst_addr = port->mapbase + ATMEL_US_THR;
759
760         ret = dmaengine_device_control(atmel_port->chan_tx,
761                                         DMA_SLAVE_CONFIG,
762                                         (unsigned long)&config);
763         if (ret) {
764                 dev_err(port->dev, "DMA tx slave configuration failed\n");
765                 goto chan_err;
766         }
767
768         return 0;
769
770 chan_err:
771         dev_err(port->dev, "TX channel not available, switch to pio\n");
772         atmel_port->use_dma_tx = 0;
773         if (atmel_port->chan_tx)
774                 atmel_release_tx_dma(port);
775         return -EINVAL;
776 }
777
778 static void atmel_flip_buffer_rx_dma(struct uart_port *port,
779                                         char *buf, size_t count)
780 {
781         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
782         struct tty_port *tport = &port->state->port;
783
784         dma_sync_sg_for_cpu(port->dev,
785                                 &atmel_port->sg_rx,
786                                 1,
787                                 DMA_DEV_TO_MEM);
788
789         tty_insert_flip_string(tport, buf, count);
790
791         dma_sync_sg_for_device(port->dev,
792                                 &atmel_port->sg_rx,
793                                 1,
794                                 DMA_DEV_TO_MEM);
795         /*
796          * Drop the lock here since it might end up calling
797          * uart_start(), which takes the lock.
798          */
799         spin_unlock(&port->lock);
800         tty_flip_buffer_push(tport);
801         spin_lock(&port->lock);
802 }
803
804 static void atmel_complete_rx_dma(void *arg)
805 {
806         struct uart_port *port = arg;
807         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
808
809         tasklet_schedule(&atmel_port->tasklet);
810 }
811
812 static void atmel_release_rx_dma(struct uart_port *port)
813 {
814         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
815         struct dma_chan *chan = atmel_port->chan_rx;
816
817         if (chan) {
818                 dmaengine_terminate_all(chan);
819                 dma_release_channel(chan);
820                 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
821                                 DMA_DEV_TO_MEM);
822         }
823
824         atmel_port->desc_rx = NULL;
825         atmel_port->chan_rx = NULL;
826         atmel_port->cookie_rx = -EINVAL;
827
828         if (!atmel_port->is_usart)
829                 del_timer_sync(&atmel_port->uart_timer);
830 }
831
832 static void atmel_rx_from_dma(struct uart_port *port)
833 {
834         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
835         struct circ_buf *ring = &atmel_port->rx_ring;
836         struct dma_chan *chan = atmel_port->chan_rx;
837         struct dma_tx_state state;
838         enum dma_status dmastat;
839         size_t pending, count;
840
841
842         /* Reset the UART timeout early so that we don't miss one */
843         UART_PUT_CR(port, ATMEL_US_STTTO);
844         dmastat = dmaengine_tx_status(chan,
845                                 atmel_port->cookie_rx,
846                                 &state);
847         /* Restart a new tasklet if DMA status is error */
848         if (dmastat == DMA_ERROR) {
849                 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
850                 UART_PUT_IER(port, ATMEL_US_TIMEOUT);
851                 tasklet_schedule(&atmel_port->tasklet);
852                 return;
853         }
854         /* current transfer size should no larger than dma buffer */
855         pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
856         BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
857
858         /*
859          * This will take the chars we have so far,
860          * ring->head will record the transfer size, only new bytes come
861          * will insert into the framework.
862          */
863         if (pending > ring->head) {
864                 count = pending - ring->head;
865
866                 atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
867
868                 ring->head += count;
869                 if (ring->head == sg_dma_len(&atmel_port->sg_rx))
870                         ring->head = 0;
871
872                 port->icount.rx += count;
873         }
874
875         UART_PUT_IER(port, ATMEL_US_TIMEOUT);
876 }
877
878 static int atmel_prepare_rx_dma(struct uart_port *port)
879 {
880         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
881         struct dma_async_tx_descriptor *desc;
882         dma_cap_mask_t          mask;
883         struct dma_slave_config config;
884         struct circ_buf         *ring;
885         int ret, nent;
886
887         ring = &atmel_port->rx_ring;
888
889         dma_cap_zero(mask);
890         dma_cap_set(DMA_CYCLIC, mask);
891
892         atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
893         if (atmel_port->chan_rx == NULL)
894                 goto chan_err;
895         dev_info(port->dev, "using %s for rx DMA transfers\n",
896                 dma_chan_name(atmel_port->chan_rx));
897
898         spin_lock_init(&atmel_port->lock_rx);
899         sg_init_table(&atmel_port->sg_rx, 1);
900         /* UART circular rx buffer is an aligned page. */
901         BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
902         sg_set_page(&atmel_port->sg_rx,
903                         virt_to_page(ring->buf),
904                         ATMEL_SERIAL_RINGSIZE,
905                         (int)ring->buf & ~PAGE_MASK);
906                         nent = dma_map_sg(port->dev,
907                                         &atmel_port->sg_rx,
908                                         1,
909                                         DMA_DEV_TO_MEM);
910
911         if (!nent) {
912                 dev_dbg(port->dev, "need to release resource of dma\n");
913                 goto chan_err;
914         } else {
915                 dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
916                         sg_dma_len(&atmel_port->sg_rx),
917                         ring->buf,
918                         sg_dma_address(&atmel_port->sg_rx));
919         }
920
921         /* Configure the slave DMA */
922         memset(&config, 0, sizeof(config));
923         config.direction = DMA_DEV_TO_MEM;
924         config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
925         config.src_addr = port->mapbase + ATMEL_US_RHR;
926
927         ret = dmaengine_device_control(atmel_port->chan_rx,
928                                         DMA_SLAVE_CONFIG,
929                                         (unsigned long)&config);
930         if (ret) {
931                 dev_err(port->dev, "DMA rx slave configuration failed\n");
932                 goto chan_err;
933         }
934         /*
935          * Prepare a cyclic dma transfer, assign 2 descriptors,
936          * each one is half ring buffer size
937          */
938         desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
939                                 sg_dma_address(&atmel_port->sg_rx),
940                                 sg_dma_len(&atmel_port->sg_rx),
941                                 sg_dma_len(&atmel_port->sg_rx)/2,
942                                 DMA_DEV_TO_MEM,
943                                 DMA_PREP_INTERRUPT);
944         desc->callback = atmel_complete_rx_dma;
945         desc->callback_param = port;
946         atmel_port->desc_rx = desc;
947         atmel_port->cookie_rx = dmaengine_submit(desc);
948
949         return 0;
950
951 chan_err:
952         dev_err(port->dev, "RX channel not available, switch to pio\n");
953         atmel_port->use_dma_rx = 0;
954         if (atmel_port->chan_rx)
955                 atmel_release_rx_dma(port);
956         return -EINVAL;
957 }
958
959 static void atmel_uart_timer_callback(unsigned long data)
960 {
961         struct uart_port *port = (void *)data;
962         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
963
964         tasklet_schedule(&atmel_port->tasklet);
965         mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port));
966 }
967
968 /*
969  * receive interrupt handler.
970  */
971 static void
972 atmel_handle_receive(struct uart_port *port, unsigned int pending)
973 {
974         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
975
976         if (atmel_use_pdc_rx(port)) {
977                 /*
978                  * PDC receive. Just schedule the tasklet and let it
979                  * figure out the details.
980                  *
981                  * TODO: We're not handling error flags correctly at
982                  * the moment.
983                  */
984                 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
985                         UART_PUT_IDR(port, (ATMEL_US_ENDRX
986                                                 | ATMEL_US_TIMEOUT));
987                         tasklet_schedule(&atmel_port->tasklet);
988                 }
989
990                 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
991                                 ATMEL_US_FRAME | ATMEL_US_PARE))
992                         atmel_pdc_rxerr(port, pending);
993         }
994
995         if (atmel_use_dma_rx(port)) {
996                 if (pending & ATMEL_US_TIMEOUT) {
997                         UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
998                         tasklet_schedule(&atmel_port->tasklet);
999                 }
1000         }
1001
1002         /* Interrupt receive */
1003         if (pending & ATMEL_US_RXRDY)
1004                 atmel_rx_chars(port);
1005         else if (pending & ATMEL_US_RXBRK) {
1006                 /*
1007                  * End of break detected. If it came along with a
1008                  * character, atmel_rx_chars will handle it.
1009                  */
1010                 UART_PUT_CR(port, ATMEL_US_RSTSTA);
1011                 UART_PUT_IDR(port, ATMEL_US_RXBRK);
1012                 atmel_port->break_active = 0;
1013         }
1014 }
1015
1016 /*
1017  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1018  */
1019 static void
1020 atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1021 {
1022         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1023
1024         if (pending & atmel_port->tx_done_mask) {
1025                 /* Either PDC or interrupt transmission */
1026                 UART_PUT_IDR(port, atmel_port->tx_done_mask);
1027                 tasklet_schedule(&atmel_port->tasklet);
1028         }
1029 }
1030
1031 /*
1032  * status flags interrupt handler.
1033  */
1034 static void
1035 atmel_handle_status(struct uart_port *port, unsigned int pending,
1036                     unsigned int status)
1037 {
1038         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1039
1040         if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1041                                 | ATMEL_US_CTSIC)) {
1042                 atmel_port->irq_status = status;
1043                 tasklet_schedule(&atmel_port->tasklet);
1044         }
1045 }
1046
1047 /*
1048  * Interrupt handler
1049  */
1050 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1051 {
1052         struct uart_port *port = dev_id;
1053         unsigned int status, pending, pass_counter = 0;
1054
1055         do {
1056                 status = UART_GET_CSR(port);
1057                 pending = status & UART_GET_IMR(port);
1058                 if (!pending)
1059                         break;
1060
1061                 atmel_handle_receive(port, pending);
1062                 atmel_handle_status(port, pending, status);
1063                 atmel_handle_transmit(port, pending);
1064         } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1065
1066         return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1067 }
1068
1069 static void atmel_release_tx_pdc(struct uart_port *port)
1070 {
1071         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1072         struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1073
1074         dma_unmap_single(port->dev,
1075                          pdc->dma_addr,
1076                          pdc->dma_size,
1077                          DMA_TO_DEVICE);
1078 }
1079
1080 /*
1081  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1082  */
1083 static void atmel_tx_pdc(struct uart_port *port)
1084 {
1085         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1086         struct circ_buf *xmit = &port->state->xmit;
1087         struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1088         int count;
1089
1090         /* nothing left to transmit? */
1091         if (UART_GET_TCR(port))
1092                 return;
1093
1094         xmit->tail += pdc->ofs;
1095         xmit->tail &= UART_XMIT_SIZE - 1;
1096
1097         port->icount.tx += pdc->ofs;
1098         pdc->ofs = 0;
1099
1100         /* more to transmit - setup next transfer */
1101
1102         /* disable PDC transmit */
1103         UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
1104
1105         if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1106                 dma_sync_single_for_device(port->dev,
1107                                            pdc->dma_addr,
1108                                            pdc->dma_size,
1109                                            DMA_TO_DEVICE);
1110
1111                 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1112                 pdc->ofs = count;
1113
1114                 UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
1115                 UART_PUT_TCR(port, count);
1116                 /* re-enable PDC transmit */
1117                 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
1118                 /* Enable interrupts */
1119                 UART_PUT_IER(port, atmel_port->tx_done_mask);
1120         } else {
1121                 if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
1122                     !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
1123                         /* DMA done, stop TX, start RX for RS485 */
1124                         atmel_start_rx(port);
1125                 }
1126         }
1127
1128         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1129                 uart_write_wakeup(port);
1130 }
1131
1132 static int atmel_prepare_tx_pdc(struct uart_port *port)
1133 {
1134         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1135         struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1136         struct circ_buf *xmit = &port->state->xmit;
1137
1138         pdc->buf = xmit->buf;
1139         pdc->dma_addr = dma_map_single(port->dev,
1140                                         pdc->buf,
1141                                         UART_XMIT_SIZE,
1142                                         DMA_TO_DEVICE);
1143         pdc->dma_size = UART_XMIT_SIZE;
1144         pdc->ofs = 0;
1145
1146         return 0;
1147 }
1148
1149 static void atmel_rx_from_ring(struct uart_port *port)
1150 {
1151         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1152         struct circ_buf *ring = &atmel_port->rx_ring;
1153         unsigned int flg;
1154         unsigned int status;
1155
1156         while (ring->head != ring->tail) {
1157                 struct atmel_uart_char c;
1158
1159                 /* Make sure c is loaded after head. */
1160                 smp_rmb();
1161
1162                 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1163
1164                 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1165
1166                 port->icount.rx++;
1167                 status = c.status;
1168                 flg = TTY_NORMAL;
1169
1170                 /*
1171                  * note that the error handling code is
1172                  * out of the main execution path
1173                  */
1174                 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1175                                        | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1176                         if (status & ATMEL_US_RXBRK) {
1177                                 /* ignore side-effect */
1178                                 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1179
1180                                 port->icount.brk++;
1181                                 if (uart_handle_break(port))
1182                                         continue;
1183                         }
1184                         if (status & ATMEL_US_PARE)
1185                                 port->icount.parity++;
1186                         if (status & ATMEL_US_FRAME)
1187                                 port->icount.frame++;
1188                         if (status & ATMEL_US_OVRE)
1189                                 port->icount.overrun++;
1190
1191                         status &= port->read_status_mask;
1192
1193                         if (status & ATMEL_US_RXBRK)
1194                                 flg = TTY_BREAK;
1195                         else if (status & ATMEL_US_PARE)
1196                                 flg = TTY_PARITY;
1197                         else if (status & ATMEL_US_FRAME)
1198                                 flg = TTY_FRAME;
1199                 }
1200
1201
1202                 if (uart_handle_sysrq_char(port, c.ch))
1203                         continue;
1204
1205                 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1206         }
1207
1208         /*
1209          * Drop the lock here since it might end up calling
1210          * uart_start(), which takes the lock.
1211          */
1212         spin_unlock(&port->lock);
1213         tty_flip_buffer_push(&port->state->port);
1214         spin_lock(&port->lock);
1215 }
1216
1217 static void atmel_release_rx_pdc(struct uart_port *port)
1218 {
1219         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1220         int i;
1221
1222         for (i = 0; i < 2; i++) {
1223                 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1224
1225                 dma_unmap_single(port->dev,
1226                                  pdc->dma_addr,
1227                                  pdc->dma_size,
1228                                  DMA_FROM_DEVICE);
1229                 kfree(pdc->buf);
1230         }
1231
1232         if (!atmel_port->is_usart)
1233                 del_timer_sync(&atmel_port->uart_timer);
1234 }
1235
1236 static void atmel_rx_from_pdc(struct uart_port *port)
1237 {
1238         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1239         struct tty_port *tport = &port->state->port;
1240         struct atmel_dma_buffer *pdc;
1241         int rx_idx = atmel_port->pdc_rx_idx;
1242         unsigned int head;
1243         unsigned int tail;
1244         unsigned int count;
1245
1246         do {
1247                 /* Reset the UART timeout early so that we don't miss one */
1248                 UART_PUT_CR(port, ATMEL_US_STTTO);
1249
1250                 pdc = &atmel_port->pdc_rx[rx_idx];
1251                 head = UART_GET_RPR(port) - pdc->dma_addr;
1252                 tail = pdc->ofs;
1253
1254                 /* If the PDC has switched buffers, RPR won't contain
1255                  * any address within the current buffer. Since head
1256                  * is unsigned, we just need a one-way comparison to
1257                  * find out.
1258                  *
1259                  * In this case, we just need to consume the entire
1260                  * buffer and resubmit it for DMA. This will clear the
1261                  * ENDRX bit as well, so that we can safely re-enable
1262                  * all interrupts below.
1263                  */
1264                 head = min(head, pdc->dma_size);
1265
1266                 if (likely(head != tail)) {
1267                         dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1268                                         pdc->dma_size, DMA_FROM_DEVICE);
1269
1270                         /*
1271                          * head will only wrap around when we recycle
1272                          * the DMA buffer, and when that happens, we
1273                          * explicitly set tail to 0. So head will
1274                          * always be greater than tail.
1275                          */
1276                         count = head - tail;
1277
1278                         tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1279                                                 count);
1280
1281                         dma_sync_single_for_device(port->dev, pdc->dma_addr,
1282                                         pdc->dma_size, DMA_FROM_DEVICE);
1283
1284                         port->icount.rx += count;
1285                         pdc->ofs = head;
1286                 }
1287
1288                 /*
1289                  * If the current buffer is full, we need to check if
1290                  * the next one contains any additional data.
1291                  */
1292                 if (head >= pdc->dma_size) {
1293                         pdc->ofs = 0;
1294                         UART_PUT_RNPR(port, pdc->dma_addr);
1295                         UART_PUT_RNCR(port, pdc->dma_size);
1296
1297                         rx_idx = !rx_idx;
1298                         atmel_port->pdc_rx_idx = rx_idx;
1299                 }
1300         } while (head >= pdc->dma_size);
1301
1302         /*
1303          * Drop the lock here since it might end up calling
1304          * uart_start(), which takes the lock.
1305          */
1306         spin_unlock(&port->lock);
1307         tty_flip_buffer_push(tport);
1308         spin_lock(&port->lock);
1309
1310         UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1311 }
1312
1313 static int atmel_prepare_rx_pdc(struct uart_port *port)
1314 {
1315         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1316         int i;
1317
1318         for (i = 0; i < 2; i++) {
1319                 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1320
1321                 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1322                 if (pdc->buf == NULL) {
1323                         if (i != 0) {
1324                                 dma_unmap_single(port->dev,
1325                                         atmel_port->pdc_rx[0].dma_addr,
1326                                         PDC_BUFFER_SIZE,
1327                                         DMA_FROM_DEVICE);
1328                                 kfree(atmel_port->pdc_rx[0].buf);
1329                         }
1330                         atmel_port->use_pdc_rx = 0;
1331                         return -ENOMEM;
1332                 }
1333                 pdc->dma_addr = dma_map_single(port->dev,
1334                                                 pdc->buf,
1335                                                 PDC_BUFFER_SIZE,
1336                                                 DMA_FROM_DEVICE);
1337                 pdc->dma_size = PDC_BUFFER_SIZE;
1338                 pdc->ofs = 0;
1339         }
1340
1341         atmel_port->pdc_rx_idx = 0;
1342
1343         UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
1344         UART_PUT_RCR(port, PDC_BUFFER_SIZE);
1345
1346         UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
1347         UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
1348
1349         return 0;
1350 }
1351
1352 /*
1353  * tasklet handling tty stuff outside the interrupt handler.
1354  */
1355 static void atmel_tasklet_func(unsigned long data)
1356 {
1357         struct uart_port *port = (struct uart_port *)data;
1358         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1359         unsigned int status;
1360         unsigned int status_change;
1361
1362         /* The interrupt handler does not take the lock */
1363         spin_lock(&port->lock);
1364
1365         atmel_port->schedule_tx(port);
1366
1367         status = atmel_port->irq_status;
1368         status_change = status ^ atmel_port->irq_status_prev;
1369
1370         if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1371                                 | ATMEL_US_DCD | ATMEL_US_CTS)) {
1372                 /* TODO: All reads to CSR will clear these interrupts! */
1373                 if (status_change & ATMEL_US_RI)
1374                         port->icount.rng++;
1375                 if (status_change & ATMEL_US_DSR)
1376                         port->icount.dsr++;
1377                 if (status_change & ATMEL_US_DCD)
1378                         uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1379                 if (status_change & ATMEL_US_CTS)
1380                         uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1381
1382                 wake_up_interruptible(&port->state->port.delta_msr_wait);
1383
1384                 atmel_port->irq_status_prev = status;
1385         }
1386
1387         atmel_port->schedule_rx(port);
1388
1389         spin_unlock(&port->lock);
1390 }
1391
1392 static int atmel_init_property(struct atmel_uart_port *atmel_port,
1393                                 struct platform_device *pdev)
1394 {
1395         struct device_node *np = pdev->dev.of_node;
1396         struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1397
1398         if (np) {
1399                 /* DMA/PDC usage specification */
1400                 if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
1401                         if (of_get_property(np, "dmas", NULL)) {
1402                                 atmel_port->use_dma_rx  = true;
1403                                 atmel_port->use_pdc_rx  = false;
1404                         } else {
1405                                 atmel_port->use_dma_rx  = false;
1406                                 atmel_port->use_pdc_rx  = true;
1407                         }
1408                 } else {
1409                         atmel_port->use_dma_rx  = false;
1410                         atmel_port->use_pdc_rx  = false;
1411                 }
1412
1413                 if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
1414                         if (of_get_property(np, "dmas", NULL)) {
1415                                 atmel_port->use_dma_tx  = true;
1416                                 atmel_port->use_pdc_tx  = false;
1417                         } else {
1418                                 atmel_port->use_dma_tx  = false;
1419                                 atmel_port->use_pdc_tx  = true;
1420                         }
1421                 } else {
1422                         atmel_port->use_dma_tx  = false;
1423                         atmel_port->use_pdc_tx  = false;
1424                 }
1425
1426         } else {
1427                 atmel_port->use_pdc_rx  = pdata->use_dma_rx;
1428                 atmel_port->use_pdc_tx  = pdata->use_dma_tx;
1429                 atmel_port->use_dma_rx  = false;
1430                 atmel_port->use_dma_tx  = false;
1431         }
1432
1433         return 0;
1434 }
1435
1436 static void atmel_init_rs485(struct atmel_uart_port *atmel_port,
1437                                 struct platform_device *pdev)
1438 {
1439         struct device_node *np = pdev->dev.of_node;
1440         struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1441
1442         if (np) {
1443                 u32 rs485_delay[2];
1444                 /* rs485 properties */
1445                 if (of_property_read_u32_array(np, "rs485-rts-delay",
1446                                         rs485_delay, 2) == 0) {
1447                         struct serial_rs485 *rs485conf = &atmel_port->rs485;
1448
1449                         rs485conf->delay_rts_before_send = rs485_delay[0];
1450                         rs485conf->delay_rts_after_send = rs485_delay[1];
1451                         rs485conf->flags = 0;
1452
1453                 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1454                         rs485conf->flags |= SER_RS485_RX_DURING_TX;
1455
1456                 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1457                                                                 NULL))
1458                         rs485conf->flags |= SER_RS485_ENABLED;
1459                 }
1460         } else {
1461                 atmel_port->rs485       = pdata->rs485;
1462         }
1463
1464 }
1465
1466 static void atmel_set_ops(struct uart_port *port)
1467 {
1468         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1469
1470         if (atmel_use_dma_rx(port)) {
1471                 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1472                 atmel_port->schedule_rx = &atmel_rx_from_dma;
1473                 atmel_port->release_rx = &atmel_release_rx_dma;
1474         } else if (atmel_use_pdc_rx(port)) {
1475                 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1476                 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1477                 atmel_port->release_rx = &atmel_release_rx_pdc;
1478         } else {
1479                 atmel_port->prepare_rx = NULL;
1480                 atmel_port->schedule_rx = &atmel_rx_from_ring;
1481                 atmel_port->release_rx = NULL;
1482         }
1483
1484         if (atmel_use_dma_tx(port)) {
1485                 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1486                 atmel_port->schedule_tx = &atmel_tx_dma;
1487                 atmel_port->release_tx = &atmel_release_tx_dma;
1488         } else if (atmel_use_pdc_tx(port)) {
1489                 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1490                 atmel_port->schedule_tx = &atmel_tx_pdc;
1491                 atmel_port->release_tx = &atmel_release_tx_pdc;
1492         } else {
1493                 atmel_port->prepare_tx = NULL;
1494                 atmel_port->schedule_tx = &atmel_tx_chars;
1495                 atmel_port->release_tx = NULL;
1496         }
1497 }
1498
1499 /*
1500  * Get ip name usart or uart
1501  */
1502 static void atmel_get_ip_name(struct uart_port *port)
1503 {
1504         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1505         int name = UART_GET_IP_NAME(port);
1506         int usart, uart;
1507         /* usart and uart ascii */
1508         usart = 0x55534152;
1509         uart = 0x44424755;
1510
1511         atmel_port->is_usart = false;
1512
1513         if (name == usart) {
1514                 dev_dbg(port->dev, "This is usart\n");
1515                 atmel_port->is_usart = true;
1516         } else if (name == uart) {
1517                 dev_dbg(port->dev, "This is uart\n");
1518                 atmel_port->is_usart = false;
1519         } else {
1520                 dev_err(port->dev, "Not supported ip name, set to uart\n");
1521         }
1522 }
1523
1524 /*
1525  * Perform initialization and enable port for reception
1526  */
1527 static int atmel_startup(struct uart_port *port)
1528 {
1529         struct platform_device *pdev = to_platform_device(port->dev);
1530         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1531         struct tty_struct *tty = port->state->port.tty;
1532         int retval;
1533
1534         /*
1535          * Ensure that no interrupts are enabled otherwise when
1536          * request_irq() is called we could get stuck trying to
1537          * handle an unexpected interrupt
1538          */
1539         UART_PUT_IDR(port, -1);
1540
1541         /*
1542          * Allocate the IRQ
1543          */
1544         retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
1545                         tty ? tty->name : "atmel_serial", port);
1546         if (retval) {
1547                 printk("atmel_serial: atmel_startup - Can't get irq\n");
1548                 return retval;
1549         }
1550
1551         /*
1552          * Initialize DMA (if necessary)
1553          */
1554         atmel_init_property(atmel_port, pdev);
1555
1556         if (atmel_port->prepare_rx) {
1557                 retval = atmel_port->prepare_rx(port);
1558                 if (retval < 0)
1559                         atmel_set_ops(port);
1560         }
1561
1562         if (atmel_port->prepare_tx) {
1563                 retval = atmel_port->prepare_tx(port);
1564                 if (retval < 0)
1565                         atmel_set_ops(port);
1566         }
1567         /*
1568          * If there is a specific "open" function (to register
1569          * control line interrupts)
1570          */
1571         if (atmel_open_hook) {
1572                 retval = atmel_open_hook(port);
1573                 if (retval) {
1574                         free_irq(port->irq, port);
1575                         return retval;
1576                 }
1577         }
1578
1579         /* Save current CSR for comparison in atmel_tasklet_func() */
1580         atmel_port->irq_status_prev = UART_GET_CSR(port);
1581         atmel_port->irq_status = atmel_port->irq_status_prev;
1582
1583         /*
1584          * Finally, enable the serial port
1585          */
1586         UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1587         /* enable xmit & rcvr */
1588         UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1589
1590         if (atmel_use_pdc_rx(port)) {
1591                 /* set UART timeout */
1592                 if (!atmel_port->is_usart) {
1593                         setup_timer(&atmel_port->uart_timer,
1594                                         atmel_uart_timer_callback,
1595                                         (unsigned long)port);
1596                         mod_timer(&atmel_port->uart_timer,
1597                                         jiffies + uart_poll_timeout(port));
1598                 /* set USART timeout */
1599                 } else {
1600                         UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1601                         UART_PUT_CR(port, ATMEL_US_STTTO);
1602
1603                         UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1604                 }
1605                 /* enable PDC controller */
1606                 UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
1607         } else if (atmel_use_dma_rx(port)) {
1608                 /* set UART timeout */
1609                 if (!atmel_port->is_usart) {
1610                         setup_timer(&atmel_port->uart_timer,
1611                                         atmel_uart_timer_callback,
1612                                         (unsigned long)port);
1613                         mod_timer(&atmel_port->uart_timer,
1614                                         jiffies + uart_poll_timeout(port));
1615                 /* set USART timeout */
1616                 } else {
1617                         UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
1618                         UART_PUT_CR(port, ATMEL_US_STTTO);
1619
1620                         UART_PUT_IER(port, ATMEL_US_TIMEOUT);
1621                 }
1622         } else {
1623                 /* enable receive only */
1624                 UART_PUT_IER(port, ATMEL_US_RXRDY);
1625         }
1626
1627         return 0;
1628 }
1629
1630 /*
1631  * Disable the port
1632  */
1633 static void atmel_shutdown(struct uart_port *port)
1634 {
1635         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1636         /*
1637          * Ensure everything is stopped.
1638          */
1639         atmel_stop_rx(port);
1640         atmel_stop_tx(port);
1641
1642         /*
1643          * Shut-down the DMA.
1644          */
1645         if (atmel_port->release_rx)
1646                 atmel_port->release_rx(port);
1647         if (atmel_port->release_tx)
1648                 atmel_port->release_tx(port);
1649
1650         /*
1651          * Disable all interrupts, port and break condition.
1652          */
1653         UART_PUT_CR(port, ATMEL_US_RSTSTA);
1654         UART_PUT_IDR(port, -1);
1655
1656         /*
1657          * Free the interrupt
1658          */
1659         free_irq(port->irq, port);
1660
1661         /*
1662          * If there is a specific "close" function (to unregister
1663          * control line interrupts)
1664          */
1665         if (atmel_close_hook)
1666                 atmel_close_hook(port);
1667 }
1668
1669 /*
1670  * Flush any TX data submitted for DMA. Called when the TX circular
1671  * buffer is reset.
1672  */
1673 static void atmel_flush_buffer(struct uart_port *port)
1674 {
1675         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1676
1677         if (atmel_use_pdc_tx(port)) {
1678                 UART_PUT_TCR(port, 0);
1679                 atmel_port->pdc_tx.ofs = 0;
1680         }
1681 }
1682
1683 /*
1684  * Power / Clock management.
1685  */
1686 static void atmel_serial_pm(struct uart_port *port, unsigned int state,
1687                             unsigned int oldstate)
1688 {
1689         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1690
1691         switch (state) {
1692         case 0:
1693                 /*
1694                  * Enable the peripheral clock for this serial port.
1695                  * This is called on uart_open() or a resume event.
1696                  */
1697                 clk_prepare_enable(atmel_port->clk);
1698
1699                 /* re-enable interrupts if we disabled some on suspend */
1700                 UART_PUT_IER(port, atmel_port->backup_imr);
1701                 break;
1702         case 3:
1703                 /* Back up the interrupt mask and disable all interrupts */
1704                 atmel_port->backup_imr = UART_GET_IMR(port);
1705                 UART_PUT_IDR(port, -1);
1706
1707                 /*
1708                  * Disable the peripheral clock for this serial port.
1709                  * This is called on uart_close() or a suspend event.
1710                  */
1711                 clk_disable_unprepare(atmel_port->clk);
1712                 break;
1713         default:
1714                 printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
1715         }
1716 }
1717
1718 /*
1719  * Change the port parameters
1720  */
1721 static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
1722                               struct ktermios *old)
1723 {
1724         unsigned long flags;
1725         unsigned int mode, imr, quot, baud;
1726         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1727
1728         /* Get current mode register */
1729         mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
1730                                         | ATMEL_US_NBSTOP | ATMEL_US_PAR
1731                                         | ATMEL_US_USMODE);
1732
1733         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
1734         quot = uart_get_divisor(port, baud);
1735
1736         if (quot > 65535) {     /* BRGR is 16-bit, so switch to slower clock */
1737                 quot /= 8;
1738                 mode |= ATMEL_US_USCLKS_MCK_DIV8;
1739         }
1740
1741         /* byte size */
1742         switch (termios->c_cflag & CSIZE) {
1743         case CS5:
1744                 mode |= ATMEL_US_CHRL_5;
1745                 break;
1746         case CS6:
1747                 mode |= ATMEL_US_CHRL_6;
1748                 break;
1749         case CS7:
1750                 mode |= ATMEL_US_CHRL_7;
1751                 break;
1752         default:
1753                 mode |= ATMEL_US_CHRL_8;
1754                 break;
1755         }
1756
1757         /* stop bits */
1758         if (termios->c_cflag & CSTOPB)
1759                 mode |= ATMEL_US_NBSTOP_2;
1760
1761         /* parity */
1762         if (termios->c_cflag & PARENB) {
1763                 /* Mark or Space parity */
1764                 if (termios->c_cflag & CMSPAR) {
1765                         if (termios->c_cflag & PARODD)
1766                                 mode |= ATMEL_US_PAR_MARK;
1767                         else
1768                                 mode |= ATMEL_US_PAR_SPACE;
1769                 } else if (termios->c_cflag & PARODD)
1770                         mode |= ATMEL_US_PAR_ODD;
1771                 else
1772                         mode |= ATMEL_US_PAR_EVEN;
1773         } else
1774                 mode |= ATMEL_US_PAR_NONE;
1775
1776         /* hardware handshake (RTS/CTS) */
1777         if (termios->c_cflag & CRTSCTS)
1778                 mode |= ATMEL_US_USMODE_HWHS;
1779         else
1780                 mode |= ATMEL_US_USMODE_NORMAL;
1781
1782         spin_lock_irqsave(&port->lock, flags);
1783
1784         port->read_status_mask = ATMEL_US_OVRE;
1785         if (termios->c_iflag & INPCK)
1786                 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1787         if (termios->c_iflag & (BRKINT | PARMRK))
1788                 port->read_status_mask |= ATMEL_US_RXBRK;
1789
1790         if (atmel_use_pdc_rx(port))
1791                 /* need to enable error interrupts */
1792                 UART_PUT_IER(port, port->read_status_mask);
1793
1794         /*
1795          * Characters to ignore
1796          */
1797         port->ignore_status_mask = 0;
1798         if (termios->c_iflag & IGNPAR)
1799                 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
1800         if (termios->c_iflag & IGNBRK) {
1801                 port->ignore_status_mask |= ATMEL_US_RXBRK;
1802                 /*
1803                  * If we're ignoring parity and break indicators,
1804                  * ignore overruns too (for real raw support).
1805                  */
1806                 if (termios->c_iflag & IGNPAR)
1807                         port->ignore_status_mask |= ATMEL_US_OVRE;
1808         }
1809         /* TODO: Ignore all characters if CREAD is set.*/
1810
1811         /* update the per-port timeout */
1812         uart_update_timeout(port, termios->c_cflag, baud);
1813
1814         /*
1815          * save/disable interrupts. The tty layer will ensure that the
1816          * transmitter is empty if requested by the caller, so there's
1817          * no need to wait for it here.
1818          */
1819         imr = UART_GET_IMR(port);
1820         UART_PUT_IDR(port, -1);
1821
1822         /* disable receiver and transmitter */
1823         UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
1824
1825         /* Resetting serial mode to RS232 (0x0) */
1826         mode &= ~ATMEL_US_USMODE;
1827
1828         if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
1829                 dev_dbg(port->dev, "Setting UART to RS485\n");
1830                 if ((atmel_port->rs485.delay_rts_after_send) > 0)
1831                         UART_PUT_TTGR(port,
1832                                         atmel_port->rs485.delay_rts_after_send);
1833                 mode |= ATMEL_US_USMODE_RS485;
1834         } else {
1835                 dev_dbg(port->dev, "Setting UART to RS232\n");
1836         }
1837
1838         /* set the parity, stop bits and data size */
1839         UART_PUT_MR(port, mode);
1840
1841         /* set the baud rate */
1842         UART_PUT_BRGR(port, quot);
1843         UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1844         UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
1845
1846         /* restore interrupts */
1847         UART_PUT_IER(port, imr);
1848
1849         /* CTS flow-control and modem-status interrupts */
1850         if (UART_ENABLE_MS(port, termios->c_cflag))
1851                 port->ops->enable_ms(port);
1852
1853         spin_unlock_irqrestore(&port->lock, flags);
1854 }
1855
1856 static void atmel_set_ldisc(struct uart_port *port, int new)
1857 {
1858         if (new == N_PPS) {
1859                 port->flags |= UPF_HARDPPS_CD;
1860                 atmel_enable_ms(port);
1861         } else {
1862                 port->flags &= ~UPF_HARDPPS_CD;
1863         }
1864 }
1865
1866 /*
1867  * Return string describing the specified port
1868  */
1869 static const char *atmel_type(struct uart_port *port)
1870 {
1871         return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
1872 }
1873
1874 /*
1875  * Release the memory region(s) being used by 'port'.
1876  */
1877 static void atmel_release_port(struct uart_port *port)
1878 {
1879         struct platform_device *pdev = to_platform_device(port->dev);
1880         int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1881
1882         release_mem_region(port->mapbase, size);
1883
1884         if (port->flags & UPF_IOREMAP) {
1885                 iounmap(port->membase);
1886                 port->membase = NULL;
1887         }
1888 }
1889
1890 /*
1891  * Request the memory region(s) being used by 'port'.
1892  */
1893 static int atmel_request_port(struct uart_port *port)
1894 {
1895         struct platform_device *pdev = to_platform_device(port->dev);
1896         int size = pdev->resource[0].end - pdev->resource[0].start + 1;
1897
1898         if (!request_mem_region(port->mapbase, size, "atmel_serial"))
1899                 return -EBUSY;
1900
1901         if (port->flags & UPF_IOREMAP) {
1902                 port->membase = ioremap(port->mapbase, size);
1903                 if (port->membase == NULL) {
1904                         release_mem_region(port->mapbase, size);
1905                         return -ENOMEM;
1906                 }
1907         }
1908
1909         return 0;
1910 }
1911
1912 /*
1913  * Configure/autoconfigure the port.
1914  */
1915 static void atmel_config_port(struct uart_port *port, int flags)
1916 {
1917         if (flags & UART_CONFIG_TYPE) {
1918                 port->type = PORT_ATMEL;
1919                 atmel_request_port(port);
1920         }
1921 }
1922
1923 /*
1924  * Verify the new serial_struct (for TIOCSSERIAL).
1925  */
1926 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
1927 {
1928         int ret = 0;
1929         if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
1930                 ret = -EINVAL;
1931         if (port->irq != ser->irq)
1932                 ret = -EINVAL;
1933         if (ser->io_type != SERIAL_IO_MEM)
1934                 ret = -EINVAL;
1935         if (port->uartclk / 16 != ser->baud_base)
1936                 ret = -EINVAL;
1937         if ((void *)port->mapbase != ser->iomem_base)
1938                 ret = -EINVAL;
1939         if (port->iobase != ser->port)
1940                 ret = -EINVAL;
1941         if (ser->hub6 != 0)
1942                 ret = -EINVAL;
1943         return ret;
1944 }
1945
1946 #ifdef CONFIG_CONSOLE_POLL
1947 static int atmel_poll_get_char(struct uart_port *port)
1948 {
1949         while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
1950                 cpu_relax();
1951
1952         return UART_GET_CHAR(port);
1953 }
1954
1955 static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
1956 {
1957         while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
1958                 cpu_relax();
1959
1960         UART_PUT_CHAR(port, ch);
1961 }
1962 #endif
1963
1964 static int
1965 atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
1966 {
1967         struct serial_rs485 rs485conf;
1968
1969         switch (cmd) {
1970         case TIOCSRS485:
1971                 if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
1972                                         sizeof(rs485conf)))
1973                         return -EFAULT;
1974
1975                 atmel_config_rs485(port, &rs485conf);
1976                 break;
1977
1978         case TIOCGRS485:
1979                 if (copy_to_user((struct serial_rs485 *) arg,
1980                                         &(to_atmel_uart_port(port)->rs485),
1981                                         sizeof(rs485conf)))
1982                         return -EFAULT;
1983                 break;
1984
1985         default:
1986                 return -ENOIOCTLCMD;
1987         }
1988         return 0;
1989 }
1990
1991
1992
1993 static struct uart_ops atmel_pops = {
1994         .tx_empty       = atmel_tx_empty,
1995         .set_mctrl      = atmel_set_mctrl,
1996         .get_mctrl      = atmel_get_mctrl,
1997         .stop_tx        = atmel_stop_tx,
1998         .start_tx       = atmel_start_tx,
1999         .stop_rx        = atmel_stop_rx,
2000         .enable_ms      = atmel_enable_ms,
2001         .break_ctl      = atmel_break_ctl,
2002         .startup        = atmel_startup,
2003         .shutdown       = atmel_shutdown,
2004         .flush_buffer   = atmel_flush_buffer,
2005         .set_termios    = atmel_set_termios,
2006         .set_ldisc      = atmel_set_ldisc,
2007         .type           = atmel_type,
2008         .release_port   = atmel_release_port,
2009         .request_port   = atmel_request_port,
2010         .config_port    = atmel_config_port,
2011         .verify_port    = atmel_verify_port,
2012         .pm             = atmel_serial_pm,
2013         .ioctl          = atmel_ioctl,
2014 #ifdef CONFIG_CONSOLE_POLL
2015         .poll_get_char  = atmel_poll_get_char,
2016         .poll_put_char  = atmel_poll_put_char,
2017 #endif
2018 };
2019
2020 /*
2021  * Configure the port from the platform device resource info.
2022  */
2023 static int atmel_init_port(struct atmel_uart_port *atmel_port,
2024                                       struct platform_device *pdev)
2025 {
2026         int ret;
2027         struct uart_port *port = &atmel_port->uart;
2028         struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2029
2030         if (!atmel_init_property(atmel_port, pdev))
2031                 atmel_set_ops(port);
2032
2033         atmel_init_rs485(atmel_port, pdev);
2034
2035         port->iotype            = UPIO_MEM;
2036         port->flags             = UPF_BOOT_AUTOCONF;
2037         port->ops               = &atmel_pops;
2038         port->fifosize          = 1;
2039         port->dev               = &pdev->dev;
2040         port->mapbase   = pdev->resource[0].start;
2041         port->irq       = pdev->resource[1].start;
2042
2043         tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
2044                         (unsigned long)port);
2045
2046         memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2047
2048         if (pdata && pdata->regs) {
2049                 /* Already mapped by setup code */
2050                 port->membase = pdata->regs;
2051         } else {
2052                 port->flags     |= UPF_IOREMAP;
2053                 port->membase   = NULL;
2054         }
2055
2056         /* for console, the clock could already be configured */
2057         if (!atmel_port->clk) {
2058                 atmel_port->clk = clk_get(&pdev->dev, "usart");
2059                 if (IS_ERR(atmel_port->clk)) {
2060                         ret = PTR_ERR(atmel_port->clk);
2061                         atmel_port->clk = NULL;
2062                         return ret;
2063                 }
2064                 ret = clk_prepare_enable(atmel_port->clk);
2065                 if (ret) {
2066                         clk_put(atmel_port->clk);
2067                         atmel_port->clk = NULL;
2068                         return ret;
2069                 }
2070                 port->uartclk = clk_get_rate(atmel_port->clk);
2071                 clk_disable_unprepare(atmel_port->clk);
2072                 /* only enable clock when USART is in use */
2073         }
2074
2075         /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2076         if (atmel_port->rs485.flags & SER_RS485_ENABLED)
2077                 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
2078         else if (atmel_use_pdc_tx(port)) {
2079                 port->fifosize = PDC_BUFFER_SIZE;
2080                 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2081         } else {
2082                 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2083         }
2084
2085         return 0;
2086 }
2087
2088 struct platform_device *atmel_default_console_device;   /* the serial console device */
2089
2090 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2091 static void atmel_console_putchar(struct uart_port *port, int ch)
2092 {
2093         while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
2094                 cpu_relax();
2095         UART_PUT_CHAR(port, ch);
2096 }
2097
2098 /*
2099  * Interrupts are disabled on entering
2100  */
2101 static void atmel_console_write(struct console *co, const char *s, u_int count)
2102 {
2103         struct uart_port *port = &atmel_ports[co->index].uart;
2104         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2105         unsigned int status, imr;
2106         unsigned int pdc_tx;
2107
2108         /*
2109          * First, save IMR and then disable interrupts
2110          */
2111         imr = UART_GET_IMR(port);
2112         UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2113
2114         /* Store PDC transmit status and disable it */
2115         pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
2116         UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
2117
2118         uart_console_write(port, s, count, atmel_console_putchar);
2119
2120         /*
2121          * Finally, wait for transmitter to become empty
2122          * and restore IMR
2123          */
2124         do {
2125                 status = UART_GET_CSR(port);
2126         } while (!(status & ATMEL_US_TXRDY));
2127
2128         /* Restore PDC transmit status */
2129         if (pdc_tx)
2130                 UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
2131
2132         /* set interrupts back the way they were */
2133         UART_PUT_IER(port, imr);
2134 }
2135
2136 /*
2137  * If the port was already initialised (eg, by a boot loader),
2138  * try to determine the current setup.
2139  */
2140 static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2141                                              int *parity, int *bits)
2142 {
2143         unsigned int mr, quot;
2144
2145         /*
2146          * If the baud rate generator isn't running, the port wasn't
2147          * initialized by the boot loader.
2148          */
2149         quot = UART_GET_BRGR(port) & ATMEL_US_CD;
2150         if (!quot)
2151                 return;
2152
2153         mr = UART_GET_MR(port) & ATMEL_US_CHRL;
2154         if (mr == ATMEL_US_CHRL_8)
2155                 *bits = 8;
2156         else
2157                 *bits = 7;
2158
2159         mr = UART_GET_MR(port) & ATMEL_US_PAR;
2160         if (mr == ATMEL_US_PAR_EVEN)
2161                 *parity = 'e';
2162         else if (mr == ATMEL_US_PAR_ODD)
2163                 *parity = 'o';
2164
2165         /*
2166          * The serial core only rounds down when matching this to a
2167          * supported baud rate. Make sure we don't end up slightly
2168          * lower than one of those, as it would make us fall through
2169          * to a much lower baud rate than we really want.
2170          */
2171         *baud = port->uartclk / (16 * (quot - 1));
2172 }
2173
2174 static int __init atmel_console_setup(struct console *co, char *options)
2175 {
2176         int ret;
2177         struct uart_port *port = &atmel_ports[co->index].uart;
2178         int baud = 115200;
2179         int bits = 8;
2180         int parity = 'n';
2181         int flow = 'n';
2182
2183         if (port->membase == NULL) {
2184                 /* Port not initialized yet - delay setup */
2185                 return -ENODEV;
2186         }
2187
2188         ret = clk_prepare_enable(atmel_ports[co->index].clk);
2189         if (ret)
2190                 return ret;
2191
2192         UART_PUT_IDR(port, -1);
2193         UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2194         UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
2195
2196         if (options)
2197                 uart_parse_options(options, &baud, &parity, &bits, &flow);
2198         else
2199                 atmel_console_get_options(port, &baud, &parity, &bits);
2200
2201         return uart_set_options(port, co, baud, parity, bits, flow);
2202 }
2203
2204 static struct uart_driver atmel_uart;
2205
2206 static struct console atmel_console = {
2207         .name           = ATMEL_DEVICENAME,
2208         .write          = atmel_console_write,
2209         .device         = uart_console_device,
2210         .setup          = atmel_console_setup,
2211         .flags          = CON_PRINTBUFFER,
2212         .index          = -1,
2213         .data           = &atmel_uart,
2214 };
2215
2216 #define ATMEL_CONSOLE_DEVICE    (&atmel_console)
2217
2218 /*
2219  * Early console initialization (before VM subsystem initialized).
2220  */
2221 static int __init atmel_console_init(void)
2222 {
2223         int ret;
2224         if (atmel_default_console_device) {
2225                 struct atmel_uart_data *pdata =
2226                         dev_get_platdata(&atmel_default_console_device->dev);
2227                 int id = pdata->num;
2228                 struct atmel_uart_port *port = &atmel_ports[id];
2229
2230                 port->backup_imr = 0;
2231                 port->uart.line = id;
2232
2233                 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
2234                 ret = atmel_init_port(port, atmel_default_console_device);
2235                 if (ret)
2236                         return ret;
2237                 register_console(&atmel_console);
2238         }
2239
2240         return 0;
2241 }
2242
2243 console_initcall(atmel_console_init);
2244
2245 /*
2246  * Late console initialization.
2247  */
2248 static int __init atmel_late_console_init(void)
2249 {
2250         if (atmel_default_console_device
2251             && !(atmel_console.flags & CON_ENABLED))
2252                 register_console(&atmel_console);
2253
2254         return 0;
2255 }
2256
2257 core_initcall(atmel_late_console_init);
2258
2259 static inline bool atmel_is_console_port(struct uart_port *port)
2260 {
2261         return port->cons && port->cons->index == port->line;
2262 }
2263
2264 #else
2265 #define ATMEL_CONSOLE_DEVICE    NULL
2266
2267 static inline bool atmel_is_console_port(struct uart_port *port)
2268 {
2269         return false;
2270 }
2271 #endif
2272
2273 static struct uart_driver atmel_uart = {
2274         .owner          = THIS_MODULE,
2275         .driver_name    = "atmel_serial",
2276         .dev_name       = ATMEL_DEVICENAME,
2277         .major          = SERIAL_ATMEL_MAJOR,
2278         .minor          = MINOR_START,
2279         .nr             = ATMEL_MAX_UART,
2280         .cons           = ATMEL_CONSOLE_DEVICE,
2281 };
2282
2283 #ifdef CONFIG_PM
2284 static bool atmel_serial_clk_will_stop(void)
2285 {
2286 #ifdef CONFIG_ARCH_AT91
2287         return at91_suspend_entering_slow_clock();
2288 #else
2289         return false;
2290 #endif
2291 }
2292
2293 static int atmel_serial_suspend(struct platform_device *pdev,
2294                                 pm_message_t state)
2295 {
2296         struct uart_port *port = platform_get_drvdata(pdev);
2297         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2298
2299         if (atmel_is_console_port(port) && console_suspend_enabled) {
2300                 /* Drain the TX shifter */
2301                 while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
2302                         cpu_relax();
2303         }
2304
2305         /* we can not wake up if we're running on slow clock */
2306         atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2307         if (atmel_serial_clk_will_stop())
2308                 device_set_wakeup_enable(&pdev->dev, 0);
2309
2310         uart_suspend_port(&atmel_uart, port);
2311
2312         return 0;
2313 }
2314
2315 static int atmel_serial_resume(struct platform_device *pdev)
2316 {
2317         struct uart_port *port = platform_get_drvdata(pdev);
2318         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2319
2320         uart_resume_port(&atmel_uart, port);
2321         device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2322
2323         return 0;
2324 }
2325 #else
2326 #define atmel_serial_suspend NULL
2327 #define atmel_serial_resume NULL
2328 #endif
2329
2330 static int atmel_serial_probe(struct platform_device *pdev)
2331 {
2332         struct atmel_uart_port *port;
2333         struct device_node *np = pdev->dev.of_node;
2334         struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2335         void *data;
2336         int ret = -ENODEV;
2337
2338         BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2339
2340         if (np)
2341                 ret = of_alias_get_id(np, "serial");
2342         else
2343                 if (pdata)
2344                         ret = pdata->num;
2345
2346         if (ret < 0)
2347                 /* port id not found in platform data nor device-tree aliases:
2348                  * auto-enumerate it */
2349                 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
2350
2351         if (ret >= ATMEL_MAX_UART) {
2352                 ret = -ENODEV;
2353                 goto err;
2354         }
2355
2356         if (test_and_set_bit(ret, atmel_ports_in_use)) {
2357                 /* port already in use */
2358                 ret = -EBUSY;
2359                 goto err;
2360         }
2361
2362         port = &atmel_ports[ret];
2363         port->backup_imr = 0;
2364         port->uart.line = ret;
2365
2366         ret = atmel_init_port(port, pdev);
2367         if (ret)
2368                 goto err;
2369
2370         if (!atmel_use_pdc_rx(&port->uart)) {
2371                 ret = -ENOMEM;
2372                 data = kmalloc(sizeof(struct atmel_uart_char)
2373                                 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2374                 if (!data)
2375                         goto err_alloc_ring;
2376                 port->rx_ring.buf = data;
2377         }
2378
2379         ret = uart_add_one_port(&atmel_uart, &port->uart);
2380         if (ret)
2381                 goto err_add_port;
2382
2383 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2384         if (atmel_is_console_port(&port->uart)
2385                         && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2386                 /*
2387                  * The serial core enabled the clock for us, so undo
2388                  * the clk_prepare_enable() in atmel_console_setup()
2389                  */
2390                 clk_disable_unprepare(port->clk);
2391         }
2392 #endif
2393
2394         device_init_wakeup(&pdev->dev, 1);
2395         platform_set_drvdata(pdev, port);
2396
2397         if (port->rs485.flags & SER_RS485_ENABLED) {
2398                 UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
2399                 UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
2400         }
2401
2402         /*
2403          * Get port name of usart or uart
2404          */
2405         atmel_get_ip_name(&port->uart);
2406
2407         return 0;
2408
2409 err_add_port:
2410         kfree(port->rx_ring.buf);
2411         port->rx_ring.buf = NULL;
2412 err_alloc_ring:
2413         if (!atmel_is_console_port(&port->uart)) {
2414                 clk_put(port->clk);
2415                 port->clk = NULL;
2416         }
2417 err:
2418         return ret;
2419 }
2420
2421 static int atmel_serial_remove(struct platform_device *pdev)
2422 {
2423         struct uart_port *port = platform_get_drvdata(pdev);
2424         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2425         int ret = 0;
2426
2427         device_init_wakeup(&pdev->dev, 0);
2428
2429         ret = uart_remove_one_port(&atmel_uart, port);
2430
2431         tasklet_kill(&atmel_port->tasklet);
2432         kfree(atmel_port->rx_ring.buf);
2433
2434         /* "port" is allocated statically, so we shouldn't free it */
2435
2436         clear_bit(port->line, atmel_ports_in_use);
2437
2438         clk_put(atmel_port->clk);
2439
2440         return ret;
2441 }
2442
2443 static struct platform_driver atmel_serial_driver = {
2444         .probe          = atmel_serial_probe,
2445         .remove         = atmel_serial_remove,
2446         .suspend        = atmel_serial_suspend,
2447         .resume         = atmel_serial_resume,
2448         .driver         = {
2449                 .name   = "atmel_usart",
2450                 .owner  = THIS_MODULE,
2451                 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
2452         },
2453 };
2454
2455 static int __init atmel_serial_init(void)
2456 {
2457         int ret;
2458
2459         ret = uart_register_driver(&atmel_uart);
2460         if (ret)
2461                 return ret;
2462
2463         ret = platform_driver_register(&atmel_serial_driver);
2464         if (ret)
2465                 uart_unregister_driver(&atmel_uart);
2466
2467         return ret;
2468 }
2469
2470 static void __exit atmel_serial_exit(void)
2471 {
2472         platform_driver_unregister(&atmel_serial_driver);
2473         uart_unregister_driver(&atmel_uart);
2474 }
2475
2476 module_init(atmel_serial_init);
2477 module_exit(atmel_serial_exit);
2478
2479 MODULE_AUTHOR("Rick Bronson");
2480 MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
2481 MODULE_LICENSE("GPL");
2482 MODULE_ALIAS("platform:atmel_usart");