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