OSDN Git Service

tty/serial: atmel: reschedule TX after RX was started
[android-x86/kernel.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/tty.h>
26 #include <linux/ioport.h>
27 #include <linux/slab.h>
28 #include <linux/init.h>
29 #include <linux/serial.h>
30 #include <linux/clk.h>
31 #include <linux/console.h>
32 #include <linux/sysrq.h>
33 #include <linux/tty_flip.h>
34 #include <linux/platform_device.h>
35 #include <linux/of.h>
36 #include <linux/of_device.h>
37 #include <linux/of_gpio.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/dmaengine.h>
40 #include <linux/atmel_pdc.h>
41 #include <linux/atmel_serial.h>
42 #include <linux/uaccess.h>
43 #include <linux/platform_data/atmel.h>
44 #include <linux/timer.h>
45 #include <linux/gpio.h>
46 #include <linux/gpio/consumer.h>
47 #include <linux/err.h>
48 #include <linux/irq.h>
49 #include <linux/suspend.h>
50
51 #include <asm/io.h>
52 #include <asm/ioctls.h>
53
54 #define PDC_BUFFER_SIZE         512
55 /* Revisit: We should calculate this based on the actual port settings */
56 #define PDC_RX_TIMEOUT          (3 * 10)                /* 3 bytes */
57
58 /* The minium number of data FIFOs should be able to contain */
59 #define ATMEL_MIN_FIFO_SIZE     8
60 /*
61  * These two offsets are substracted from the RX FIFO size to define the RTS
62  * high and low thresholds
63  */
64 #define ATMEL_RTS_HIGH_OFFSET   16
65 #define ATMEL_RTS_LOW_OFFSET    20
66
67 #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
68 #define SUPPORT_SYSRQ
69 #endif
70
71 #include <linux/serial_core.h>
72
73 #include "serial_mctrl_gpio.h"
74
75 static void atmel_start_rx(struct uart_port *port);
76 static void atmel_stop_rx(struct uart_port *port);
77
78 #ifdef CONFIG_SERIAL_ATMEL_TTYAT
79
80 /* Use device name ttyAT, major 204 and minor 154-169.  This is necessary if we
81  * should coexist with the 8250 driver, such as if we have an external 16C550
82  * UART. */
83 #define SERIAL_ATMEL_MAJOR      204
84 #define MINOR_START             154
85 #define ATMEL_DEVICENAME        "ttyAT"
86
87 #else
88
89 /* Use device name ttyS, major 4, minor 64-68.  This is the usual serial port
90  * name, but it is legally reserved for the 8250 driver. */
91 #define SERIAL_ATMEL_MAJOR      TTY_MAJOR
92 #define MINOR_START             64
93 #define ATMEL_DEVICENAME        "ttyS"
94
95 #endif
96
97 #define ATMEL_ISR_PASS_LIMIT    256
98
99 struct atmel_dma_buffer {
100         unsigned char   *buf;
101         dma_addr_t      dma_addr;
102         unsigned int    dma_size;
103         unsigned int    ofs;
104 };
105
106 struct atmel_uart_char {
107         u16             status;
108         u16             ch;
109 };
110
111 /*
112  * Be careful, the real size of the ring buffer is
113  * sizeof(atmel_uart_char) * ATMEL_SERIAL_RINGSIZE. It means that ring buffer
114  * can contain up to 1024 characters in PIO mode and up to 4096 characters in
115  * DMA mode.
116  */
117 #define ATMEL_SERIAL_RINGSIZE 1024
118
119 /*
120  * at91: 6 USARTs and one DBGU port (SAM9260)
121  * avr32: 4
122  */
123 #define ATMEL_MAX_UART          7
124
125 /*
126  * We wrap our port structure around the generic uart_port.
127  */
128 struct atmel_uart_port {
129         struct uart_port        uart;           /* uart */
130         struct clk              *clk;           /* uart clock */
131         int                     may_wakeup;     /* cached value of device_may_wakeup for times we need to disable it */
132         u32                     backup_imr;     /* IMR saved during suspend */
133         int                     break_active;   /* break being received */
134
135         bool                    use_dma_rx;     /* enable DMA receiver */
136         bool                    use_pdc_rx;     /* enable PDC receiver */
137         short                   pdc_rx_idx;     /* current PDC RX buffer */
138         struct atmel_dma_buffer pdc_rx[2];      /* PDC receier */
139
140         bool                    use_dma_tx;     /* enable DMA transmitter */
141         bool                    use_pdc_tx;     /* enable PDC transmitter */
142         struct atmel_dma_buffer pdc_tx;         /* PDC transmitter */
143
144         spinlock_t                      lock_tx;        /* port lock */
145         spinlock_t                      lock_rx;        /* port lock */
146         struct dma_chan                 *chan_tx;
147         struct dma_chan                 *chan_rx;
148         struct dma_async_tx_descriptor  *desc_tx;
149         struct dma_async_tx_descriptor  *desc_rx;
150         dma_cookie_t                    cookie_tx;
151         dma_cookie_t                    cookie_rx;
152         struct scatterlist              sg_tx;
153         struct scatterlist              sg_rx;
154         struct tasklet_struct   tasklet_rx;
155         struct tasklet_struct   tasklet_tx;
156         atomic_t                tasklet_shutdown;
157         unsigned int            irq_status_prev;
158         unsigned int            tx_len;
159
160         struct circ_buf         rx_ring;
161
162         struct mctrl_gpios      *gpios;
163         unsigned int            tx_done_mask;
164         u32                     fifo_size;
165         u32                     rts_high;
166         u32                     rts_low;
167         bool                    ms_irq_enabled;
168         u32                     rtor;   /* address of receiver timeout register if it exists */
169         bool                    has_frac_baudrate;
170         bool                    has_hw_timer;
171         struct timer_list       uart_timer;
172
173         bool                    suspended;
174         unsigned int            pending;
175         unsigned int            pending_status;
176         spinlock_t              lock_suspended;
177
178         bool                    hd_start_rx;    /* can start RX during half-duplex operation */
179
180         int (*prepare_rx)(struct uart_port *port);
181         int (*prepare_tx)(struct uart_port *port);
182         void (*schedule_rx)(struct uart_port *port);
183         void (*schedule_tx)(struct uart_port *port);
184         void (*release_rx)(struct uart_port *port);
185         void (*release_tx)(struct uart_port *port);
186 };
187
188 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
189 static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
190
191 #ifdef SUPPORT_SYSRQ
192 static struct console atmel_console;
193 #endif
194
195 #if defined(CONFIG_OF)
196 static const struct of_device_id atmel_serial_dt_ids[] = {
197         { .compatible = "atmel,at91rm9200-usart" },
198         { .compatible = "atmel,at91sam9260-usart" },
199         { /* sentinel */ }
200 };
201 #endif
202
203 static inline struct atmel_uart_port *
204 to_atmel_uart_port(struct uart_port *uart)
205 {
206         return container_of(uart, struct atmel_uart_port, uart);
207 }
208
209 static inline u32 atmel_uart_readl(struct uart_port *port, u32 reg)
210 {
211         return __raw_readl(port->membase + reg);
212 }
213
214 static inline void atmel_uart_writel(struct uart_port *port, u32 reg, u32 value)
215 {
216         __raw_writel(value, port->membase + reg);
217 }
218
219 #ifdef CONFIG_AVR32
220
221 /* AVR32 cannot handle 8 or 16bit I/O accesses but only 32bit I/O accesses */
222 static inline u8 atmel_uart_read_char(struct uart_port *port)
223 {
224         return __raw_readl(port->membase + ATMEL_US_RHR);
225 }
226
227 static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
228 {
229         __raw_writel(value, port->membase + ATMEL_US_THR);
230 }
231
232 #else
233
234 static inline u8 atmel_uart_read_char(struct uart_port *port)
235 {
236         return __raw_readb(port->membase + ATMEL_US_RHR);
237 }
238
239 static inline void atmel_uart_write_char(struct uart_port *port, u8 value)
240 {
241         __raw_writeb(value, port->membase + ATMEL_US_THR);
242 }
243
244 #endif
245
246 static inline int atmel_uart_is_half_duplex(struct uart_port *port)
247 {
248         return (port->rs485.flags & SER_RS485_ENABLED) &&
249                 !(port->rs485.flags & SER_RS485_RX_DURING_TX);
250 }
251
252 #ifdef CONFIG_SERIAL_ATMEL_PDC
253 static bool atmel_use_pdc_rx(struct uart_port *port)
254 {
255         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
256
257         return atmel_port->use_pdc_rx;
258 }
259
260 static bool atmel_use_pdc_tx(struct uart_port *port)
261 {
262         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
263
264         return atmel_port->use_pdc_tx;
265 }
266 #else
267 static bool atmel_use_pdc_rx(struct uart_port *port)
268 {
269         return false;
270 }
271
272 static bool atmel_use_pdc_tx(struct uart_port *port)
273 {
274         return false;
275 }
276 #endif
277
278 static bool atmel_use_dma_tx(struct uart_port *port)
279 {
280         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
281
282         return atmel_port->use_dma_tx;
283 }
284
285 static bool atmel_use_dma_rx(struct uart_port *port)
286 {
287         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
288
289         return atmel_port->use_dma_rx;
290 }
291
292 static bool atmel_use_fifo(struct uart_port *port)
293 {
294         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
295
296         return atmel_port->fifo_size;
297 }
298
299 static void atmel_tasklet_schedule(struct atmel_uart_port *atmel_port,
300                                    struct tasklet_struct *t)
301 {
302         if (!atomic_read(&atmel_port->tasklet_shutdown))
303                 tasklet_schedule(t);
304 }
305
306 static unsigned int atmel_get_lines_status(struct uart_port *port)
307 {
308         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
309         unsigned int status, ret = 0;
310
311         status = atmel_uart_readl(port, ATMEL_US_CSR);
312
313         mctrl_gpio_get(atmel_port->gpios, &ret);
314
315         if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
316                                                 UART_GPIO_CTS))) {
317                 if (ret & TIOCM_CTS)
318                         status &= ~ATMEL_US_CTS;
319                 else
320                         status |= ATMEL_US_CTS;
321         }
322
323         if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
324                                                 UART_GPIO_DSR))) {
325                 if (ret & TIOCM_DSR)
326                         status &= ~ATMEL_US_DSR;
327                 else
328                         status |= ATMEL_US_DSR;
329         }
330
331         if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
332                                                 UART_GPIO_RI))) {
333                 if (ret & TIOCM_RI)
334                         status &= ~ATMEL_US_RI;
335                 else
336                         status |= ATMEL_US_RI;
337         }
338
339         if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
340                                                 UART_GPIO_DCD))) {
341                 if (ret & TIOCM_CD)
342                         status &= ~ATMEL_US_DCD;
343                 else
344                         status |= ATMEL_US_DCD;
345         }
346
347         return status;
348 }
349
350 /* Enable or disable the rs485 support */
351 static int atmel_config_rs485(struct uart_port *port,
352                               struct serial_rs485 *rs485conf)
353 {
354         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
355         unsigned int mode;
356
357         /* Disable interrupts */
358         atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
359
360         mode = atmel_uart_readl(port, ATMEL_US_MR);
361
362         /* Resetting serial mode to RS232 (0x0) */
363         mode &= ~ATMEL_US_USMODE;
364
365         port->rs485 = *rs485conf;
366
367         if (rs485conf->flags & SER_RS485_ENABLED) {
368                 dev_dbg(port->dev, "Setting UART to RS485\n");
369                 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
370                 atmel_uart_writel(port, ATMEL_US_TTGR,
371                                   rs485conf->delay_rts_after_send);
372                 mode |= ATMEL_US_USMODE_RS485;
373         } else {
374                 dev_dbg(port->dev, "Setting UART to RS232\n");
375                 if (atmel_use_pdc_tx(port))
376                         atmel_port->tx_done_mask = ATMEL_US_ENDTX |
377                                 ATMEL_US_TXBUFE;
378                 else
379                         atmel_port->tx_done_mask = ATMEL_US_TXRDY;
380         }
381         atmel_uart_writel(port, ATMEL_US_MR, mode);
382
383         /* Enable interrupts */
384         atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
385
386         return 0;
387 }
388
389 /*
390  * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
391  */
392 static u_int atmel_tx_empty(struct uart_port *port)
393 {
394         return (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXEMPTY) ?
395                 TIOCSER_TEMT :
396                 0;
397 }
398
399 /*
400  * Set state of the modem control output lines
401  */
402 static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
403 {
404         unsigned int control = 0;
405         unsigned int mode = atmel_uart_readl(port, ATMEL_US_MR);
406         unsigned int rts_paused, rts_ready;
407         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
408
409         /* override mode to RS485 if needed, otherwise keep the current mode */
410         if (port->rs485.flags & SER_RS485_ENABLED) {
411                 atmel_uart_writel(port, ATMEL_US_TTGR,
412                                   port->rs485.delay_rts_after_send);
413                 mode &= ~ATMEL_US_USMODE;
414                 mode |= ATMEL_US_USMODE_RS485;
415         }
416
417         /* set the RTS line state according to the mode */
418         if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
419                 /* force RTS line to high level */
420                 rts_paused = ATMEL_US_RTSEN;
421
422                 /* give the control of the RTS line back to the hardware */
423                 rts_ready = ATMEL_US_RTSDIS;
424         } else {
425                 /* force RTS line to high level */
426                 rts_paused = ATMEL_US_RTSDIS;
427
428                 /* force RTS line to low level */
429                 rts_ready = ATMEL_US_RTSEN;
430         }
431
432         if (mctrl & TIOCM_RTS)
433                 control |= rts_ready;
434         else
435                 control |= rts_paused;
436
437         if (mctrl & TIOCM_DTR)
438                 control |= ATMEL_US_DTREN;
439         else
440                 control |= ATMEL_US_DTRDIS;
441
442         atmel_uart_writel(port, ATMEL_US_CR, control);
443
444         mctrl_gpio_set(atmel_port->gpios, mctrl);
445
446         /* Local loopback mode? */
447         mode &= ~ATMEL_US_CHMODE;
448         if (mctrl & TIOCM_LOOP)
449                 mode |= ATMEL_US_CHMODE_LOC_LOOP;
450         else
451                 mode |= ATMEL_US_CHMODE_NORMAL;
452
453         atmel_uart_writel(port, ATMEL_US_MR, mode);
454 }
455
456 /*
457  * Get state of the modem control input lines
458  */
459 static u_int atmel_get_mctrl(struct uart_port *port)
460 {
461         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
462         unsigned int ret = 0, status;
463
464         status = atmel_uart_readl(port, ATMEL_US_CSR);
465
466         /*
467          * The control signals are active low.
468          */
469         if (!(status & ATMEL_US_DCD))
470                 ret |= TIOCM_CD;
471         if (!(status & ATMEL_US_CTS))
472                 ret |= TIOCM_CTS;
473         if (!(status & ATMEL_US_DSR))
474                 ret |= TIOCM_DSR;
475         if (!(status & ATMEL_US_RI))
476                 ret |= TIOCM_RI;
477
478         return mctrl_gpio_get(atmel_port->gpios, &ret);
479 }
480
481 /*
482  * Stop transmitting.
483  */
484 static void atmel_stop_tx(struct uart_port *port)
485 {
486         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
487
488         if (atmel_use_pdc_tx(port)) {
489                 /* disable PDC transmit */
490                 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
491         }
492
493         /*
494          * Disable the transmitter.
495          * This is mandatory when DMA is used, otherwise the DMA buffer
496          * is fully transmitted.
497          */
498         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS);
499
500         /* Disable interrupts */
501         atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
502
503         if (atmel_uart_is_half_duplex(port))
504                 atmel_start_rx(port);
505
506 }
507
508 /*
509  * Start transmitting.
510  */
511 static void atmel_start_tx(struct uart_port *port)
512 {
513         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
514
515         if (atmel_use_pdc_tx(port) && (atmel_uart_readl(port, ATMEL_PDC_PTSR)
516                                        & ATMEL_PDC_TXTEN))
517                 /* The transmitter is already running.  Yes, we
518                    really need this.*/
519                 return;
520
521         if (atmel_use_pdc_tx(port) || atmel_use_dma_tx(port))
522                 if (atmel_uart_is_half_duplex(port))
523                         atmel_stop_rx(port);
524
525         if (atmel_use_pdc_tx(port))
526                 /* re-enable PDC transmit */
527                 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
528
529         /* Enable interrupts */
530         atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
531
532         /* re-enable the transmitter */
533         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
534 }
535
536 /*
537  * start receiving - port is in process of being opened.
538  */
539 static void atmel_start_rx(struct uart_port *port)
540 {
541         /* reset status and receiver */
542         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
543
544         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXEN);
545
546         if (atmel_use_pdc_rx(port)) {
547                 /* enable PDC controller */
548                 atmel_uart_writel(port, ATMEL_US_IER,
549                                   ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
550                                   port->read_status_mask);
551                 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
552         } else {
553                 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
554         }
555 }
556
557 /*
558  * Stop receiving - port is in process of being closed.
559  */
560 static void atmel_stop_rx(struct uart_port *port)
561 {
562         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RXDIS);
563
564         if (atmel_use_pdc_rx(port)) {
565                 /* disable PDC receive */
566                 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTDIS);
567                 atmel_uart_writel(port, ATMEL_US_IDR,
568                                   ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
569                                   port->read_status_mask);
570         } else {
571                 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXRDY);
572         }
573 }
574
575 /*
576  * Enable modem status interrupts
577  */
578 static void atmel_enable_ms(struct uart_port *port)
579 {
580         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
581         uint32_t ier = 0;
582
583         /*
584          * Interrupt should not be enabled twice
585          */
586         if (atmel_port->ms_irq_enabled)
587                 return;
588
589         atmel_port->ms_irq_enabled = true;
590
591         if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
592                 ier |= ATMEL_US_CTSIC;
593
594         if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
595                 ier |= ATMEL_US_DSRIC;
596
597         if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
598                 ier |= ATMEL_US_RIIC;
599
600         if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
601                 ier |= ATMEL_US_DCDIC;
602
603         atmel_uart_writel(port, ATMEL_US_IER, ier);
604
605         mctrl_gpio_enable_ms(atmel_port->gpios);
606 }
607
608 /*
609  * Disable modem status interrupts
610  */
611 static void atmel_disable_ms(struct uart_port *port)
612 {
613         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
614         uint32_t idr = 0;
615
616         /*
617          * Interrupt should not be disabled twice
618          */
619         if (!atmel_port->ms_irq_enabled)
620                 return;
621
622         atmel_port->ms_irq_enabled = false;
623
624         mctrl_gpio_disable_ms(atmel_port->gpios);
625
626         if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS))
627                 idr |= ATMEL_US_CTSIC;
628
629         if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DSR))
630                 idr |= ATMEL_US_DSRIC;
631
632         if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_RI))
633                 idr |= ATMEL_US_RIIC;
634
635         if (!mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_DCD))
636                 idr |= ATMEL_US_DCDIC;
637
638         atmel_uart_writel(port, ATMEL_US_IDR, idr);
639 }
640
641 /*
642  * Control the transmission of a break signal
643  */
644 static void atmel_break_ctl(struct uart_port *port, int break_state)
645 {
646         if (break_state != 0)
647                 /* start break */
648                 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTBRK);
649         else
650                 /* stop break */
651                 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STPBRK);
652 }
653
654 /*
655  * Stores the incoming character in the ring buffer
656  */
657 static void
658 atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
659                      unsigned int ch)
660 {
661         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
662         struct circ_buf *ring = &atmel_port->rx_ring;
663         struct atmel_uart_char *c;
664
665         if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
666                 /* Buffer overflow, ignore char */
667                 return;
668
669         c = &((struct atmel_uart_char *)ring->buf)[ring->head];
670         c->status       = status;
671         c->ch           = ch;
672
673         /* Make sure the character is stored before we update head. */
674         smp_wmb();
675
676         ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
677 }
678
679 /*
680  * Deal with parity, framing and overrun errors.
681  */
682 static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
683 {
684         /* clear error */
685         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
686
687         if (status & ATMEL_US_RXBRK) {
688                 /* ignore side-effect */
689                 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
690                 port->icount.brk++;
691         }
692         if (status & ATMEL_US_PARE)
693                 port->icount.parity++;
694         if (status & ATMEL_US_FRAME)
695                 port->icount.frame++;
696         if (status & ATMEL_US_OVRE)
697                 port->icount.overrun++;
698 }
699
700 /*
701  * Characters received (called from interrupt handler)
702  */
703 static void atmel_rx_chars(struct uart_port *port)
704 {
705         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
706         unsigned int status, ch;
707
708         status = atmel_uart_readl(port, ATMEL_US_CSR);
709         while (status & ATMEL_US_RXRDY) {
710                 ch = atmel_uart_read_char(port);
711
712                 /*
713                  * note that the error handling code is
714                  * out of the main execution path
715                  */
716                 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
717                                        | ATMEL_US_OVRE | ATMEL_US_RXBRK)
718                              || atmel_port->break_active)) {
719
720                         /* clear error */
721                         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
722
723                         if (status & ATMEL_US_RXBRK
724                             && !atmel_port->break_active) {
725                                 atmel_port->break_active = 1;
726                                 atmel_uart_writel(port, ATMEL_US_IER,
727                                                   ATMEL_US_RXBRK);
728                         } else {
729                                 /*
730                                  * This is either the end-of-break
731                                  * condition or we've received at
732                                  * least one character without RXBRK
733                                  * being set. In both cases, the next
734                                  * RXBRK will indicate start-of-break.
735                                  */
736                                 atmel_uart_writel(port, ATMEL_US_IDR,
737                                                   ATMEL_US_RXBRK);
738                                 status &= ~ATMEL_US_RXBRK;
739                                 atmel_port->break_active = 0;
740                         }
741                 }
742
743                 atmel_buffer_rx_char(port, status, ch);
744                 status = atmel_uart_readl(port, ATMEL_US_CSR);
745         }
746
747         atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
748 }
749
750 /*
751  * Transmit characters (called from tasklet with TXRDY interrupt
752  * disabled)
753  */
754 static void atmel_tx_chars(struct uart_port *port)
755 {
756         struct circ_buf *xmit = &port->state->xmit;
757         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
758
759         if (port->x_char &&
760             (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
761                 atmel_uart_write_char(port, port->x_char);
762                 port->icount.tx++;
763                 port->x_char = 0;
764         }
765         if (uart_circ_empty(xmit) || uart_tx_stopped(port))
766                 return;
767
768         while (atmel_uart_readl(port, ATMEL_US_CSR) &
769                atmel_port->tx_done_mask) {
770                 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
771                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
772                 port->icount.tx++;
773                 if (uart_circ_empty(xmit))
774                         break;
775         }
776
777         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
778                 uart_write_wakeup(port);
779
780         if (!uart_circ_empty(xmit))
781                 /* Enable interrupts */
782                 atmel_uart_writel(port, ATMEL_US_IER,
783                                   atmel_port->tx_done_mask);
784 }
785
786 static void atmel_complete_tx_dma(void *arg)
787 {
788         struct atmel_uart_port *atmel_port = arg;
789         struct uart_port *port = &atmel_port->uart;
790         struct circ_buf *xmit = &port->state->xmit;
791         struct dma_chan *chan = atmel_port->chan_tx;
792         unsigned long flags;
793
794         spin_lock_irqsave(&port->lock, flags);
795
796         if (chan)
797                 dmaengine_terminate_all(chan);
798         xmit->tail += atmel_port->tx_len;
799         xmit->tail &= UART_XMIT_SIZE - 1;
800
801         port->icount.tx += atmel_port->tx_len;
802
803         spin_lock_irq(&atmel_port->lock_tx);
804         async_tx_ack(atmel_port->desc_tx);
805         atmel_port->cookie_tx = -EINVAL;
806         atmel_port->desc_tx = NULL;
807         spin_unlock_irq(&atmel_port->lock_tx);
808
809         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
810                 uart_write_wakeup(port);
811
812         /*
813          * xmit is a circular buffer so, if we have just send data from
814          * xmit->tail to the end of xmit->buf, now we have to transmit the
815          * remaining data from the beginning of xmit->buf to xmit->head.
816          */
817         if (!uart_circ_empty(xmit))
818                 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
819         else if (atmel_uart_is_half_duplex(port)) {
820                 /*
821                  * DMA done, re-enable TXEMPTY and signal that we can stop
822                  * TX and start RX for RS485
823                  */
824                 atmel_port->hd_start_rx = true;
825                 atmel_uart_writel(port, ATMEL_US_IER,
826                                   atmel_port->tx_done_mask);
827         }
828
829         spin_unlock_irqrestore(&port->lock, flags);
830 }
831
832 static void atmel_release_tx_dma(struct uart_port *port)
833 {
834         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
835         struct dma_chan *chan = atmel_port->chan_tx;
836
837         if (chan) {
838                 dmaengine_terminate_all(chan);
839                 dma_release_channel(chan);
840                 dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
841                                 DMA_TO_DEVICE);
842         }
843
844         atmel_port->desc_tx = NULL;
845         atmel_port->chan_tx = NULL;
846         atmel_port->cookie_tx = -EINVAL;
847 }
848
849 /*
850  * Called from tasklet with TXRDY interrupt is disabled.
851  */
852 static void atmel_tx_dma(struct uart_port *port)
853 {
854         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
855         struct circ_buf *xmit = &port->state->xmit;
856         struct dma_chan *chan = atmel_port->chan_tx;
857         struct dma_async_tx_descriptor *desc;
858         struct scatterlist sgl[2], *sg, *sg_tx = &atmel_port->sg_tx;
859         unsigned int tx_len, part1_len, part2_len, sg_len;
860         dma_addr_t phys_addr;
861
862         /* Make sure we have an idle channel */
863         if (atmel_port->desc_tx != NULL)
864                 return;
865
866         if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
867                 /*
868                  * DMA is idle now.
869                  * Port xmit buffer is already mapped,
870                  * and it is one page... Just adjust
871                  * offsets and lengths. Since it is a circular buffer,
872                  * we have to transmit till the end, and then the rest.
873                  * Take the port lock to get a
874                  * consistent xmit buffer state.
875                  */
876                 tx_len = CIRC_CNT_TO_END(xmit->head,
877                                          xmit->tail,
878                                          UART_XMIT_SIZE);
879
880                 if (atmel_port->fifo_size) {
881                         /* multi data mode */
882                         part1_len = (tx_len & ~0x3); /* DWORD access */
883                         part2_len = (tx_len & 0x3); /* BYTE access */
884                 } else {
885                         /* single data (legacy) mode */
886                         part1_len = 0;
887                         part2_len = tx_len; /* BYTE access only */
888                 }
889
890                 sg_init_table(sgl, 2);
891                 sg_len = 0;
892                 phys_addr = sg_dma_address(sg_tx) + xmit->tail;
893                 if (part1_len) {
894                         sg = &sgl[sg_len++];
895                         sg_dma_address(sg) = phys_addr;
896                         sg_dma_len(sg) = part1_len;
897
898                         phys_addr += part1_len;
899                 }
900
901                 if (part2_len) {
902                         sg = &sgl[sg_len++];
903                         sg_dma_address(sg) = phys_addr;
904                         sg_dma_len(sg) = part2_len;
905                 }
906
907                 /*
908                  * save tx_len so atmel_complete_tx_dma() will increase
909                  * xmit->tail correctly
910                  */
911                 atmel_port->tx_len = tx_len;
912
913                 desc = dmaengine_prep_slave_sg(chan,
914                                                sgl,
915                                                sg_len,
916                                                DMA_MEM_TO_DEV,
917                                                DMA_PREP_INTERRUPT |
918                                                DMA_CTRL_ACK);
919                 if (!desc) {
920                         dev_err(port->dev, "Failed to send via dma!\n");
921                         return;
922                 }
923
924                 dma_sync_sg_for_device(port->dev, sg_tx, 1, DMA_TO_DEVICE);
925
926                 atmel_port->desc_tx = desc;
927                 desc->callback = atmel_complete_tx_dma;
928                 desc->callback_param = atmel_port;
929                 atmel_port->cookie_tx = dmaengine_submit(desc);
930         }
931
932         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
933                 uart_write_wakeup(port);
934 }
935
936 static int atmel_prepare_tx_dma(struct uart_port *port)
937 {
938         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
939         dma_cap_mask_t          mask;
940         struct dma_slave_config config;
941         int ret, nent;
942
943         dma_cap_zero(mask);
944         dma_cap_set(DMA_SLAVE, mask);
945
946         atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
947         if (atmel_port->chan_tx == NULL)
948                 goto chan_err;
949         dev_info(port->dev, "using %s for tx DMA transfers\n",
950                 dma_chan_name(atmel_port->chan_tx));
951
952         spin_lock_init(&atmel_port->lock_tx);
953         sg_init_table(&atmel_port->sg_tx, 1);
954         /* UART circular tx buffer is an aligned page. */
955         BUG_ON(!PAGE_ALIGNED(port->state->xmit.buf));
956         sg_set_page(&atmel_port->sg_tx,
957                         virt_to_page(port->state->xmit.buf),
958                         UART_XMIT_SIZE,
959                         (unsigned long)port->state->xmit.buf & ~PAGE_MASK);
960         nent = dma_map_sg(port->dev,
961                                 &atmel_port->sg_tx,
962                                 1,
963                                 DMA_TO_DEVICE);
964
965         if (!nent) {
966                 dev_dbg(port->dev, "need to release resource of dma\n");
967                 goto chan_err;
968         } else {
969                 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
970                         sg_dma_len(&atmel_port->sg_tx),
971                         port->state->xmit.buf,
972                         &sg_dma_address(&atmel_port->sg_tx));
973         }
974
975         /* Configure the slave DMA */
976         memset(&config, 0, sizeof(config));
977         config.direction = DMA_MEM_TO_DEV;
978         config.dst_addr_width = (atmel_port->fifo_size) ?
979                                 DMA_SLAVE_BUSWIDTH_4_BYTES :
980                                 DMA_SLAVE_BUSWIDTH_1_BYTE;
981         config.dst_addr = port->mapbase + ATMEL_US_THR;
982         config.dst_maxburst = 1;
983
984         ret = dmaengine_slave_config(atmel_port->chan_tx,
985                                      &config);
986         if (ret) {
987                 dev_err(port->dev, "DMA tx slave configuration failed\n");
988                 goto chan_err;
989         }
990
991         return 0;
992
993 chan_err:
994         dev_err(port->dev, "TX channel not available, switch to pio\n");
995         atmel_port->use_dma_tx = 0;
996         if (atmel_port->chan_tx)
997                 atmel_release_tx_dma(port);
998         return -EINVAL;
999 }
1000
1001 static void atmel_complete_rx_dma(void *arg)
1002 {
1003         struct uart_port *port = arg;
1004         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1005
1006         atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
1007 }
1008
1009 static void atmel_release_rx_dma(struct uart_port *port)
1010 {
1011         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1012         struct dma_chan *chan = atmel_port->chan_rx;
1013
1014         if (chan) {
1015                 dmaengine_terminate_all(chan);
1016                 dma_release_channel(chan);
1017                 dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
1018                                 DMA_FROM_DEVICE);
1019         }
1020
1021         atmel_port->desc_rx = NULL;
1022         atmel_port->chan_rx = NULL;
1023         atmel_port->cookie_rx = -EINVAL;
1024 }
1025
1026 static void atmel_rx_from_dma(struct uart_port *port)
1027 {
1028         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1029         struct tty_port *tport = &port->state->port;
1030         struct circ_buf *ring = &atmel_port->rx_ring;
1031         struct dma_chan *chan = atmel_port->chan_rx;
1032         struct dma_tx_state state;
1033         enum dma_status dmastat;
1034         size_t count;
1035
1036
1037         /* Reset the UART timeout early so that we don't miss one */
1038         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1039         dmastat = dmaengine_tx_status(chan,
1040                                 atmel_port->cookie_rx,
1041                                 &state);
1042         /* Restart a new tasklet if DMA status is error */
1043         if (dmastat == DMA_ERROR) {
1044                 dev_dbg(port->dev, "Get residue error, restart tasklet\n");
1045                 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
1046                 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_rx);
1047                 return;
1048         }
1049
1050         /* CPU claims ownership of RX DMA buffer */
1051         dma_sync_sg_for_cpu(port->dev,
1052                             &atmel_port->sg_rx,
1053                             1,
1054                             DMA_FROM_DEVICE);
1055
1056         /*
1057          * ring->head points to the end of data already written by the DMA.
1058          * ring->tail points to the beginning of data to be read by the
1059          * framework.
1060          * The current transfer size should not be larger than the dma buffer
1061          * length.
1062          */
1063         ring->head = sg_dma_len(&atmel_port->sg_rx) - state.residue;
1064         BUG_ON(ring->head > sg_dma_len(&atmel_port->sg_rx));
1065         /*
1066          * At this point ring->head may point to the first byte right after the
1067          * last byte of the dma buffer:
1068          * 0 <= ring->head <= sg_dma_len(&atmel_port->sg_rx)
1069          *
1070          * However ring->tail must always points inside the dma buffer:
1071          * 0 <= ring->tail <= sg_dma_len(&atmel_port->sg_rx) - 1
1072          *
1073          * Since we use a ring buffer, we have to handle the case
1074          * where head is lower than tail. In such a case, we first read from
1075          * tail to the end of the buffer then reset tail.
1076          */
1077         if (ring->head < ring->tail) {
1078                 count = sg_dma_len(&atmel_port->sg_rx) - ring->tail;
1079
1080                 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1081                 ring->tail = 0;
1082                 port->icount.rx += count;
1083         }
1084
1085         /* Finally we read data from tail to head */
1086         if (ring->tail < ring->head) {
1087                 count = ring->head - ring->tail;
1088
1089                 tty_insert_flip_string(tport, ring->buf + ring->tail, count);
1090                 /* Wrap ring->head if needed */
1091                 if (ring->head >= sg_dma_len(&atmel_port->sg_rx))
1092                         ring->head = 0;
1093                 ring->tail = ring->head;
1094                 port->icount.rx += count;
1095         }
1096
1097         /* USART retreives ownership of RX DMA buffer */
1098         dma_sync_sg_for_device(port->dev,
1099                                &atmel_port->sg_rx,
1100                                1,
1101                                DMA_FROM_DEVICE);
1102
1103         /*
1104          * Drop the lock here since it might end up calling
1105          * uart_start(), which takes the lock.
1106          */
1107         spin_unlock(&port->lock);
1108         tty_flip_buffer_push(tport);
1109         spin_lock(&port->lock);
1110
1111         atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_TIMEOUT);
1112 }
1113
1114 static int atmel_prepare_rx_dma(struct uart_port *port)
1115 {
1116         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1117         struct dma_async_tx_descriptor *desc;
1118         dma_cap_mask_t          mask;
1119         struct dma_slave_config config;
1120         struct circ_buf         *ring;
1121         int ret, nent;
1122
1123         ring = &atmel_port->rx_ring;
1124
1125         dma_cap_zero(mask);
1126         dma_cap_set(DMA_CYCLIC, mask);
1127
1128         atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1129         if (atmel_port->chan_rx == NULL)
1130                 goto chan_err;
1131         dev_info(port->dev, "using %s for rx DMA transfers\n",
1132                 dma_chan_name(atmel_port->chan_rx));
1133
1134         spin_lock_init(&atmel_port->lock_rx);
1135         sg_init_table(&atmel_port->sg_rx, 1);
1136         /* UART circular rx buffer is an aligned page. */
1137         BUG_ON(!PAGE_ALIGNED(ring->buf));
1138         sg_set_page(&atmel_port->sg_rx,
1139                     virt_to_page(ring->buf),
1140                     sizeof(struct atmel_uart_char) * ATMEL_SERIAL_RINGSIZE,
1141                     (unsigned long)ring->buf & ~PAGE_MASK);
1142         nent = dma_map_sg(port->dev,
1143                           &atmel_port->sg_rx,
1144                           1,
1145                           DMA_FROM_DEVICE);
1146
1147         if (!nent) {
1148                 dev_dbg(port->dev, "need to release resource of dma\n");
1149                 goto chan_err;
1150         } else {
1151                 dev_dbg(port->dev, "%s: mapped %d@%p to %pad\n", __func__,
1152                         sg_dma_len(&atmel_port->sg_rx),
1153                         ring->buf,
1154                         &sg_dma_address(&atmel_port->sg_rx));
1155         }
1156
1157         /* Configure the slave DMA */
1158         memset(&config, 0, sizeof(config));
1159         config.direction = DMA_DEV_TO_MEM;
1160         config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1161         config.src_addr = port->mapbase + ATMEL_US_RHR;
1162         config.src_maxburst = 1;
1163
1164         ret = dmaengine_slave_config(atmel_port->chan_rx,
1165                                      &config);
1166         if (ret) {
1167                 dev_err(port->dev, "DMA rx slave configuration failed\n");
1168                 goto chan_err;
1169         }
1170         /*
1171          * Prepare a cyclic dma transfer, assign 2 descriptors,
1172          * each one is half ring buffer size
1173          */
1174         desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
1175                                          sg_dma_address(&atmel_port->sg_rx),
1176                                          sg_dma_len(&atmel_port->sg_rx),
1177                                          sg_dma_len(&atmel_port->sg_rx)/2,
1178                                          DMA_DEV_TO_MEM,
1179                                          DMA_PREP_INTERRUPT);
1180         if (!desc) {
1181                 dev_err(port->dev, "Preparing DMA cyclic failed\n");
1182                 goto chan_err;
1183         }
1184         desc->callback = atmel_complete_rx_dma;
1185         desc->callback_param = port;
1186         atmel_port->desc_rx = desc;
1187         atmel_port->cookie_rx = dmaengine_submit(desc);
1188
1189         return 0;
1190
1191 chan_err:
1192         dev_err(port->dev, "RX channel not available, switch to pio\n");
1193         atmel_port->use_dma_rx = 0;
1194         if (atmel_port->chan_rx)
1195                 atmel_release_rx_dma(port);
1196         return -EINVAL;
1197 }
1198
1199 static void atmel_uart_timer_callback(unsigned long data)
1200 {
1201         struct uart_port *port = (void *)data;
1202         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1203
1204         if (!atomic_read(&atmel_port->tasklet_shutdown)) {
1205                 tasklet_schedule(&atmel_port->tasklet_rx);
1206                 mod_timer(&atmel_port->uart_timer,
1207                           jiffies + uart_poll_timeout(port));
1208         }
1209 }
1210
1211 /*
1212  * receive interrupt handler.
1213  */
1214 static void
1215 atmel_handle_receive(struct uart_port *port, unsigned int pending)
1216 {
1217         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1218
1219         if (atmel_use_pdc_rx(port)) {
1220                 /*
1221                  * PDC receive. Just schedule the tasklet and let it
1222                  * figure out the details.
1223                  *
1224                  * TODO: We're not handling error flags correctly at
1225                  * the moment.
1226                  */
1227                 if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
1228                         atmel_uart_writel(port, ATMEL_US_IDR,
1229                                           (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT));
1230                         atmel_tasklet_schedule(atmel_port,
1231                                                &atmel_port->tasklet_rx);
1232                 }
1233
1234                 if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
1235                                 ATMEL_US_FRAME | ATMEL_US_PARE))
1236                         atmel_pdc_rxerr(port, pending);
1237         }
1238
1239         if (atmel_use_dma_rx(port)) {
1240                 if (pending & ATMEL_US_TIMEOUT) {
1241                         atmel_uart_writel(port, ATMEL_US_IDR,
1242                                           ATMEL_US_TIMEOUT);
1243                         atmel_tasklet_schedule(atmel_port,
1244                                                &atmel_port->tasklet_rx);
1245                 }
1246         }
1247
1248         /* Interrupt receive */
1249         if (pending & ATMEL_US_RXRDY)
1250                 atmel_rx_chars(port);
1251         else if (pending & ATMEL_US_RXBRK) {
1252                 /*
1253                  * End of break detected. If it came along with a
1254                  * character, atmel_rx_chars will handle it.
1255                  */
1256                 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
1257                 atmel_uart_writel(port, ATMEL_US_IDR, ATMEL_US_RXBRK);
1258                 atmel_port->break_active = 0;
1259         }
1260 }
1261
1262 /*
1263  * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
1264  */
1265 static void
1266 atmel_handle_transmit(struct uart_port *port, unsigned int pending)
1267 {
1268         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1269
1270         if (pending & atmel_port->tx_done_mask) {
1271                 atmel_uart_writel(port, ATMEL_US_IDR,
1272                                   atmel_port->tx_done_mask);
1273
1274                 /* Start RX if flag was set and FIFO is empty */
1275                 if (atmel_port->hd_start_rx) {
1276                         if (!(atmel_uart_readl(port, ATMEL_US_CSR)
1277                                         & ATMEL_US_TXEMPTY))
1278                                 dev_warn(port->dev, "Should start RX, but TX fifo is not empty\n");
1279
1280                         atmel_port->hd_start_rx = false;
1281                         atmel_start_rx(port);
1282                 }
1283
1284                 atmel_tasklet_schedule(atmel_port, &atmel_port->tasklet_tx);
1285         }
1286 }
1287
1288 /*
1289  * status flags interrupt handler.
1290  */
1291 static void
1292 atmel_handle_status(struct uart_port *port, unsigned int pending,
1293                     unsigned int status)
1294 {
1295         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1296         unsigned int status_change;
1297
1298         if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
1299                                 | ATMEL_US_CTSIC)) {
1300                 status_change = status ^ atmel_port->irq_status_prev;
1301                 atmel_port->irq_status_prev = status;
1302
1303                 if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
1304                                         | ATMEL_US_DCD | ATMEL_US_CTS)) {
1305                         /* TODO: All reads to CSR will clear these interrupts! */
1306                         if (status_change & ATMEL_US_RI)
1307                                 port->icount.rng++;
1308                         if (status_change & ATMEL_US_DSR)
1309                                 port->icount.dsr++;
1310                         if (status_change & ATMEL_US_DCD)
1311                                 uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
1312                         if (status_change & ATMEL_US_CTS)
1313                                 uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
1314
1315                         wake_up_interruptible(&port->state->port.delta_msr_wait);
1316                 }
1317         }
1318 }
1319
1320 /*
1321  * Interrupt handler
1322  */
1323 static irqreturn_t atmel_interrupt(int irq, void *dev_id)
1324 {
1325         struct uart_port *port = dev_id;
1326         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1327         unsigned int status, pending, mask, pass_counter = 0;
1328
1329         spin_lock(&atmel_port->lock_suspended);
1330
1331         do {
1332                 status = atmel_get_lines_status(port);
1333                 mask = atmel_uart_readl(port, ATMEL_US_IMR);
1334                 pending = status & mask;
1335                 if (!pending)
1336                         break;
1337
1338                 if (atmel_port->suspended) {
1339                         atmel_port->pending |= pending;
1340                         atmel_port->pending_status = status;
1341                         atmel_uart_writel(port, ATMEL_US_IDR, mask);
1342                         pm_system_wakeup();
1343                         break;
1344                 }
1345
1346                 atmel_handle_receive(port, pending);
1347                 atmel_handle_status(port, pending, status);
1348                 atmel_handle_transmit(port, pending);
1349         } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
1350
1351         spin_unlock(&atmel_port->lock_suspended);
1352
1353         return pass_counter ? IRQ_HANDLED : IRQ_NONE;
1354 }
1355
1356 static void atmel_release_tx_pdc(struct uart_port *port)
1357 {
1358         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1359         struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1360
1361         dma_unmap_single(port->dev,
1362                          pdc->dma_addr,
1363                          pdc->dma_size,
1364                          DMA_TO_DEVICE);
1365 }
1366
1367 /*
1368  * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
1369  */
1370 static void atmel_tx_pdc(struct uart_port *port)
1371 {
1372         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1373         struct circ_buf *xmit = &port->state->xmit;
1374         struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1375         int count;
1376
1377         /* nothing left to transmit? */
1378         if (atmel_uart_readl(port, ATMEL_PDC_TCR))
1379                 return;
1380
1381         xmit->tail += pdc->ofs;
1382         xmit->tail &= UART_XMIT_SIZE - 1;
1383
1384         port->icount.tx += pdc->ofs;
1385         pdc->ofs = 0;
1386
1387         /* more to transmit - setup next transfer */
1388
1389         /* disable PDC transmit */
1390         atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
1391
1392         if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
1393                 dma_sync_single_for_device(port->dev,
1394                                            pdc->dma_addr,
1395                                            pdc->dma_size,
1396                                            DMA_TO_DEVICE);
1397
1398                 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1399                 pdc->ofs = count;
1400
1401                 atmel_uart_writel(port, ATMEL_PDC_TPR,
1402                                   pdc->dma_addr + xmit->tail);
1403                 atmel_uart_writel(port, ATMEL_PDC_TCR, count);
1404                 /* re-enable PDC transmit */
1405                 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
1406                 /* Enable interrupts */
1407                 atmel_uart_writel(port, ATMEL_US_IER,
1408                                   atmel_port->tx_done_mask);
1409         } else {
1410                 if (atmel_uart_is_half_duplex(port)) {
1411                         /* DMA done, stop TX, start RX for RS485 */
1412                         atmel_start_rx(port);
1413                 }
1414         }
1415
1416         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1417                 uart_write_wakeup(port);
1418 }
1419
1420 static int atmel_prepare_tx_pdc(struct uart_port *port)
1421 {
1422         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1423         struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
1424         struct circ_buf *xmit = &port->state->xmit;
1425
1426         pdc->buf = xmit->buf;
1427         pdc->dma_addr = dma_map_single(port->dev,
1428                                         pdc->buf,
1429                                         UART_XMIT_SIZE,
1430                                         DMA_TO_DEVICE);
1431         pdc->dma_size = UART_XMIT_SIZE;
1432         pdc->ofs = 0;
1433
1434         return 0;
1435 }
1436
1437 static void atmel_rx_from_ring(struct uart_port *port)
1438 {
1439         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1440         struct circ_buf *ring = &atmel_port->rx_ring;
1441         unsigned int flg;
1442         unsigned int status;
1443
1444         while (ring->head != ring->tail) {
1445                 struct atmel_uart_char c;
1446
1447                 /* Make sure c is loaded after head. */
1448                 smp_rmb();
1449
1450                 c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
1451
1452                 ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
1453
1454                 port->icount.rx++;
1455                 status = c.status;
1456                 flg = TTY_NORMAL;
1457
1458                 /*
1459                  * note that the error handling code is
1460                  * out of the main execution path
1461                  */
1462                 if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
1463                                        | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
1464                         if (status & ATMEL_US_RXBRK) {
1465                                 /* ignore side-effect */
1466                                 status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
1467
1468                                 port->icount.brk++;
1469                                 if (uart_handle_break(port))
1470                                         continue;
1471                         }
1472                         if (status & ATMEL_US_PARE)
1473                                 port->icount.parity++;
1474                         if (status & ATMEL_US_FRAME)
1475                                 port->icount.frame++;
1476                         if (status & ATMEL_US_OVRE)
1477                                 port->icount.overrun++;
1478
1479                         status &= port->read_status_mask;
1480
1481                         if (status & ATMEL_US_RXBRK)
1482                                 flg = TTY_BREAK;
1483                         else if (status & ATMEL_US_PARE)
1484                                 flg = TTY_PARITY;
1485                         else if (status & ATMEL_US_FRAME)
1486                                 flg = TTY_FRAME;
1487                 }
1488
1489
1490                 if (uart_handle_sysrq_char(port, c.ch))
1491                         continue;
1492
1493                 uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
1494         }
1495
1496         /*
1497          * Drop the lock here since it might end up calling
1498          * uart_start(), which takes the lock.
1499          */
1500         spin_unlock(&port->lock);
1501         tty_flip_buffer_push(&port->state->port);
1502         spin_lock(&port->lock);
1503 }
1504
1505 static void atmel_release_rx_pdc(struct uart_port *port)
1506 {
1507         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1508         int i;
1509
1510         for (i = 0; i < 2; i++) {
1511                 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1512
1513                 dma_unmap_single(port->dev,
1514                                  pdc->dma_addr,
1515                                  pdc->dma_size,
1516                                  DMA_FROM_DEVICE);
1517                 kfree(pdc->buf);
1518         }
1519 }
1520
1521 static void atmel_rx_from_pdc(struct uart_port *port)
1522 {
1523         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1524         struct tty_port *tport = &port->state->port;
1525         struct atmel_dma_buffer *pdc;
1526         int rx_idx = atmel_port->pdc_rx_idx;
1527         unsigned int head;
1528         unsigned int tail;
1529         unsigned int count;
1530
1531         do {
1532                 /* Reset the UART timeout early so that we don't miss one */
1533                 atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1534
1535                 pdc = &atmel_port->pdc_rx[rx_idx];
1536                 head = atmel_uart_readl(port, ATMEL_PDC_RPR) - pdc->dma_addr;
1537                 tail = pdc->ofs;
1538
1539                 /* If the PDC has switched buffers, RPR won't contain
1540                  * any address within the current buffer. Since head
1541                  * is unsigned, we just need a one-way comparison to
1542                  * find out.
1543                  *
1544                  * In this case, we just need to consume the entire
1545                  * buffer and resubmit it for DMA. This will clear the
1546                  * ENDRX bit as well, so that we can safely re-enable
1547                  * all interrupts below.
1548                  */
1549                 head = min(head, pdc->dma_size);
1550
1551                 if (likely(head != tail)) {
1552                         dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
1553                                         pdc->dma_size, DMA_FROM_DEVICE);
1554
1555                         /*
1556                          * head will only wrap around when we recycle
1557                          * the DMA buffer, and when that happens, we
1558                          * explicitly set tail to 0. So head will
1559                          * always be greater than tail.
1560                          */
1561                         count = head - tail;
1562
1563                         tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
1564                                                 count);
1565
1566                         dma_sync_single_for_device(port->dev, pdc->dma_addr,
1567                                         pdc->dma_size, DMA_FROM_DEVICE);
1568
1569                         port->icount.rx += count;
1570                         pdc->ofs = head;
1571                 }
1572
1573                 /*
1574                  * If the current buffer is full, we need to check if
1575                  * the next one contains any additional data.
1576                  */
1577                 if (head >= pdc->dma_size) {
1578                         pdc->ofs = 0;
1579                         atmel_uart_writel(port, ATMEL_PDC_RNPR, pdc->dma_addr);
1580                         atmel_uart_writel(port, ATMEL_PDC_RNCR, pdc->dma_size);
1581
1582                         rx_idx = !rx_idx;
1583                         atmel_port->pdc_rx_idx = rx_idx;
1584                 }
1585         } while (head >= pdc->dma_size);
1586
1587         /*
1588          * Drop the lock here since it might end up calling
1589          * uart_start(), which takes the lock.
1590          */
1591         spin_unlock(&port->lock);
1592         tty_flip_buffer_push(tport);
1593         spin_lock(&port->lock);
1594
1595         atmel_uart_writel(port, ATMEL_US_IER,
1596                           ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1597 }
1598
1599 static int atmel_prepare_rx_pdc(struct uart_port *port)
1600 {
1601         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1602         int i;
1603
1604         for (i = 0; i < 2; i++) {
1605                 struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
1606
1607                 pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
1608                 if (pdc->buf == NULL) {
1609                         if (i != 0) {
1610                                 dma_unmap_single(port->dev,
1611                                         atmel_port->pdc_rx[0].dma_addr,
1612                                         PDC_BUFFER_SIZE,
1613                                         DMA_FROM_DEVICE);
1614                                 kfree(atmel_port->pdc_rx[0].buf);
1615                         }
1616                         atmel_port->use_pdc_rx = 0;
1617                         return -ENOMEM;
1618                 }
1619                 pdc->dma_addr = dma_map_single(port->dev,
1620                                                 pdc->buf,
1621                                                 PDC_BUFFER_SIZE,
1622                                                 DMA_FROM_DEVICE);
1623                 pdc->dma_size = PDC_BUFFER_SIZE;
1624                 pdc->ofs = 0;
1625         }
1626
1627         atmel_port->pdc_rx_idx = 0;
1628
1629         atmel_uart_writel(port, ATMEL_PDC_RPR, atmel_port->pdc_rx[0].dma_addr);
1630         atmel_uart_writel(port, ATMEL_PDC_RCR, PDC_BUFFER_SIZE);
1631
1632         atmel_uart_writel(port, ATMEL_PDC_RNPR,
1633                           atmel_port->pdc_rx[1].dma_addr);
1634         atmel_uart_writel(port, ATMEL_PDC_RNCR, PDC_BUFFER_SIZE);
1635
1636         return 0;
1637 }
1638
1639 /*
1640  * tasklet handling tty stuff outside the interrupt handler.
1641  */
1642 static void atmel_tasklet_rx_func(unsigned long data)
1643 {
1644         struct uart_port *port = (struct uart_port *)data;
1645         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1646
1647         /* The interrupt handler does not take the lock */
1648         spin_lock(&port->lock);
1649         atmel_port->schedule_rx(port);
1650         spin_unlock(&port->lock);
1651 }
1652
1653 static void atmel_tasklet_tx_func(unsigned long data)
1654 {
1655         struct uart_port *port = (struct uart_port *)data;
1656         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1657
1658         /* The interrupt handler does not take the lock */
1659         spin_lock(&port->lock);
1660         atmel_port->schedule_tx(port);
1661         spin_unlock(&port->lock);
1662 }
1663
1664 static void atmel_init_property(struct atmel_uart_port *atmel_port,
1665                                 struct platform_device *pdev)
1666 {
1667         struct device_node *np = pdev->dev.of_node;
1668         struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1669
1670         if (np) {
1671                 /* DMA/PDC usage specification */
1672                 if (of_property_read_bool(np, "atmel,use-dma-rx")) {
1673                         if (of_property_read_bool(np, "dmas")) {
1674                                 atmel_port->use_dma_rx  = true;
1675                                 atmel_port->use_pdc_rx  = false;
1676                         } else {
1677                                 atmel_port->use_dma_rx  = false;
1678                                 atmel_port->use_pdc_rx  = true;
1679                         }
1680                 } else {
1681                         atmel_port->use_dma_rx  = false;
1682                         atmel_port->use_pdc_rx  = false;
1683                 }
1684
1685                 if (of_property_read_bool(np, "atmel,use-dma-tx")) {
1686                         if (of_property_read_bool(np, "dmas")) {
1687                                 atmel_port->use_dma_tx  = true;
1688                                 atmel_port->use_pdc_tx  = false;
1689                         } else {
1690                                 atmel_port->use_dma_tx  = false;
1691                                 atmel_port->use_pdc_tx  = true;
1692                         }
1693                 } else {
1694                         atmel_port->use_dma_tx  = false;
1695                         atmel_port->use_pdc_tx  = false;
1696                 }
1697
1698         } else {
1699                 atmel_port->use_pdc_rx  = pdata->use_dma_rx;
1700                 atmel_port->use_pdc_tx  = pdata->use_dma_tx;
1701                 atmel_port->use_dma_rx  = false;
1702                 atmel_port->use_dma_tx  = false;
1703         }
1704
1705 }
1706
1707 static void atmel_init_rs485(struct uart_port *port,
1708                                 struct platform_device *pdev)
1709 {
1710         struct device_node *np = pdev->dev.of_node;
1711         struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
1712
1713         if (np) {
1714                 struct serial_rs485 *rs485conf = &port->rs485;
1715                 u32 rs485_delay[2];
1716                 /* rs485 properties */
1717                 if (of_property_read_u32_array(np, "rs485-rts-delay",
1718                                         rs485_delay, 2) == 0) {
1719                         rs485conf->delay_rts_before_send = rs485_delay[0];
1720                         rs485conf->delay_rts_after_send = rs485_delay[1];
1721                         rs485conf->flags = 0;
1722                 }
1723
1724                 if (of_get_property(np, "rs485-rx-during-tx", NULL))
1725                         rs485conf->flags |= SER_RS485_RX_DURING_TX;
1726
1727                 if (of_get_property(np, "linux,rs485-enabled-at-boot-time",
1728                                                                 NULL))
1729                         rs485conf->flags |= SER_RS485_ENABLED;
1730         } else {
1731                 port->rs485       = pdata->rs485;
1732         }
1733
1734 }
1735
1736 static void atmel_set_ops(struct uart_port *port)
1737 {
1738         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1739
1740         if (atmel_use_dma_rx(port)) {
1741                 atmel_port->prepare_rx = &atmel_prepare_rx_dma;
1742                 atmel_port->schedule_rx = &atmel_rx_from_dma;
1743                 atmel_port->release_rx = &atmel_release_rx_dma;
1744         } else if (atmel_use_pdc_rx(port)) {
1745                 atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
1746                 atmel_port->schedule_rx = &atmel_rx_from_pdc;
1747                 atmel_port->release_rx = &atmel_release_rx_pdc;
1748         } else {
1749                 atmel_port->prepare_rx = NULL;
1750                 atmel_port->schedule_rx = &atmel_rx_from_ring;
1751                 atmel_port->release_rx = NULL;
1752         }
1753
1754         if (atmel_use_dma_tx(port)) {
1755                 atmel_port->prepare_tx = &atmel_prepare_tx_dma;
1756                 atmel_port->schedule_tx = &atmel_tx_dma;
1757                 atmel_port->release_tx = &atmel_release_tx_dma;
1758         } else if (atmel_use_pdc_tx(port)) {
1759                 atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
1760                 atmel_port->schedule_tx = &atmel_tx_pdc;
1761                 atmel_port->release_tx = &atmel_release_tx_pdc;
1762         } else {
1763                 atmel_port->prepare_tx = NULL;
1764                 atmel_port->schedule_tx = &atmel_tx_chars;
1765                 atmel_port->release_tx = NULL;
1766         }
1767 }
1768
1769 /*
1770  * Get ip name usart or uart
1771  */
1772 static void atmel_get_ip_name(struct uart_port *port)
1773 {
1774         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1775         int name = atmel_uart_readl(port, ATMEL_US_NAME);
1776         u32 version;
1777         u32 usart, dbgu_uart, new_uart;
1778         /* ASCII decoding for IP version */
1779         usart = 0x55534152;     /* USAR(T) */
1780         dbgu_uart = 0x44424755; /* DBGU */
1781         new_uart = 0x55415254;  /* UART */
1782
1783         /*
1784          * Only USART devices from at91sam9260 SOC implement fractional
1785          * baudrate.
1786          */
1787         atmel_port->has_frac_baudrate = false;
1788         atmel_port->has_hw_timer = false;
1789
1790         if (name == new_uart) {
1791                 dev_dbg(port->dev, "Uart with hw timer");
1792                 atmel_port->has_hw_timer = true;
1793                 atmel_port->rtor = ATMEL_UA_RTOR;
1794         } else if (name == usart) {
1795                 dev_dbg(port->dev, "Usart\n");
1796                 atmel_port->has_frac_baudrate = true;
1797                 atmel_port->has_hw_timer = true;
1798                 atmel_port->rtor = ATMEL_US_RTOR;
1799         } else if (name == dbgu_uart) {
1800                 dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
1801         } else {
1802                 /* fallback for older SoCs: use version field */
1803                 version = atmel_uart_readl(port, ATMEL_US_VERSION);
1804                 switch (version) {
1805                 case 0x302:
1806                 case 0x10213:
1807                 case 0x10302:
1808                         dev_dbg(port->dev, "This version is usart\n");
1809                         atmel_port->has_frac_baudrate = true;
1810                         atmel_port->has_hw_timer = true;
1811                         atmel_port->rtor = ATMEL_US_RTOR;
1812                         break;
1813                 case 0x203:
1814                 case 0x10202:
1815                         dev_dbg(port->dev, "This version is uart\n");
1816                         break;
1817                 default:
1818                         dev_err(port->dev, "Not supported ip name nor version, set to uart\n");
1819                 }
1820         }
1821 }
1822
1823 /*
1824  * Perform initialization and enable port for reception
1825  */
1826 static int atmel_startup(struct uart_port *port)
1827 {
1828         struct platform_device *pdev = to_platform_device(port->dev);
1829         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1830         int retval;
1831
1832         /*
1833          * Ensure that no interrupts are enabled otherwise when
1834          * request_irq() is called we could get stuck trying to
1835          * handle an unexpected interrupt
1836          */
1837         atmel_uart_writel(port, ATMEL_US_IDR, -1);
1838         atmel_port->ms_irq_enabled = false;
1839
1840         /*
1841          * Allocate the IRQ
1842          */
1843         retval = request_irq(port->irq, atmel_interrupt,
1844                              IRQF_SHARED | IRQF_COND_SUSPEND,
1845                              dev_name(&pdev->dev), port);
1846         if (retval) {
1847                 dev_err(port->dev, "atmel_startup - Can't get irq\n");
1848                 return retval;
1849         }
1850
1851         atomic_set(&atmel_port->tasklet_shutdown, 0);
1852         tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
1853                         (unsigned long)port);
1854         tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
1855                         (unsigned long)port);
1856
1857         /*
1858          * Initialize DMA (if necessary)
1859          */
1860         atmel_init_property(atmel_port, pdev);
1861         atmel_set_ops(port);
1862
1863         if (atmel_port->prepare_rx) {
1864                 retval = atmel_port->prepare_rx(port);
1865                 if (retval < 0)
1866                         atmel_set_ops(port);
1867         }
1868
1869         if (atmel_port->prepare_tx) {
1870                 retval = atmel_port->prepare_tx(port);
1871                 if (retval < 0)
1872                         atmel_set_ops(port);
1873         }
1874
1875         /*
1876          * Enable FIFO when available
1877          */
1878         if (atmel_port->fifo_size) {
1879                 unsigned int txrdym = ATMEL_US_ONE_DATA;
1880                 unsigned int rxrdym = ATMEL_US_ONE_DATA;
1881                 unsigned int fmr;
1882
1883                 atmel_uart_writel(port, ATMEL_US_CR,
1884                                   ATMEL_US_FIFOEN |
1885                                   ATMEL_US_RXFCLR |
1886                                   ATMEL_US_TXFLCLR);
1887
1888                 if (atmel_use_dma_tx(port))
1889                         txrdym = ATMEL_US_FOUR_DATA;
1890
1891                 fmr = ATMEL_US_TXRDYM(txrdym) | ATMEL_US_RXRDYM(rxrdym);
1892                 if (atmel_port->rts_high &&
1893                     atmel_port->rts_low)
1894                         fmr |=  ATMEL_US_FRTSC |
1895                                 ATMEL_US_RXFTHRES(atmel_port->rts_high) |
1896                                 ATMEL_US_RXFTHRES2(atmel_port->rts_low);
1897
1898                 atmel_uart_writel(port, ATMEL_US_FMR, fmr);
1899         }
1900
1901         /* Save current CSR for comparison in atmel_tasklet_func() */
1902         atmel_port->irq_status_prev = atmel_get_lines_status(port);
1903
1904         /*
1905          * Finally, enable the serial port
1906          */
1907         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
1908         /* enable xmit & rcvr */
1909         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
1910
1911         setup_timer(&atmel_port->uart_timer,
1912                         atmel_uart_timer_callback,
1913                         (unsigned long)port);
1914
1915         if (atmel_use_pdc_rx(port)) {
1916                 /* set UART timeout */
1917                 if (!atmel_port->has_hw_timer) {
1918                         mod_timer(&atmel_port->uart_timer,
1919                                         jiffies + uart_poll_timeout(port));
1920                 /* set USART timeout */
1921                 } else {
1922                         atmel_uart_writel(port, atmel_port->rtor,
1923                                           PDC_RX_TIMEOUT);
1924                         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1925
1926                         atmel_uart_writel(port, ATMEL_US_IER,
1927                                           ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
1928                 }
1929                 /* enable PDC controller */
1930                 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_RXTEN);
1931         } else if (atmel_use_dma_rx(port)) {
1932                 /* set UART timeout */
1933                 if (!atmel_port->has_hw_timer) {
1934                         mod_timer(&atmel_port->uart_timer,
1935                                         jiffies + uart_poll_timeout(port));
1936                 /* set USART timeout */
1937                 } else {
1938                         atmel_uart_writel(port, atmel_port->rtor,
1939                                           PDC_RX_TIMEOUT);
1940                         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_STTTO);
1941
1942                         atmel_uart_writel(port, ATMEL_US_IER,
1943                                           ATMEL_US_TIMEOUT);
1944                 }
1945         } else {
1946                 /* enable receive only */
1947                 atmel_uart_writel(port, ATMEL_US_IER, ATMEL_US_RXRDY);
1948         }
1949
1950         return 0;
1951 }
1952
1953 /*
1954  * Flush any TX data submitted for DMA. Called when the TX circular
1955  * buffer is reset.
1956  */
1957 static void atmel_flush_buffer(struct uart_port *port)
1958 {
1959         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1960
1961         if (atmel_use_pdc_tx(port)) {
1962                 atmel_uart_writel(port, ATMEL_PDC_TCR, 0);
1963                 atmel_port->pdc_tx.ofs = 0;
1964         }
1965         /*
1966          * in uart_flush_buffer(), the xmit circular buffer has just
1967          * been cleared, so we have to reset tx_len accordingly.
1968          */
1969         atmel_port->tx_len = 0;
1970 }
1971
1972 /*
1973  * Disable the port
1974  */
1975 static void atmel_shutdown(struct uart_port *port)
1976 {
1977         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1978
1979         /* Disable modem control lines interrupts */
1980         atmel_disable_ms(port);
1981
1982         /* Disable interrupts at device level */
1983         atmel_uart_writel(port, ATMEL_US_IDR, -1);
1984
1985         /* Prevent spurious interrupts from scheduling the tasklet */
1986         atomic_inc(&atmel_port->tasklet_shutdown);
1987
1988         /*
1989          * Prevent any tasklets being scheduled during
1990          * cleanup
1991          */
1992         del_timer_sync(&atmel_port->uart_timer);
1993
1994         /* Make sure that no interrupt is on the fly */
1995         synchronize_irq(port->irq);
1996
1997         /*
1998          * Clear out any scheduled tasklets before
1999          * we destroy the buffers
2000          */
2001         tasklet_kill(&atmel_port->tasklet_rx);
2002         tasklet_kill(&atmel_port->tasklet_tx);
2003
2004         /*
2005          * Ensure everything is stopped and
2006          * disable port and break condition.
2007          */
2008         atmel_stop_rx(port);
2009         atmel_stop_tx(port);
2010
2011         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA);
2012
2013         /*
2014          * Shut-down the DMA.
2015          */
2016         if (atmel_port->release_rx)
2017                 atmel_port->release_rx(port);
2018         if (atmel_port->release_tx)
2019                 atmel_port->release_tx(port);
2020
2021         /*
2022          * Reset ring buffer pointers
2023          */
2024         atmel_port->rx_ring.head = 0;
2025         atmel_port->rx_ring.tail = 0;
2026
2027         /*
2028          * Free the interrupts
2029          */
2030         free_irq(port->irq, port);
2031
2032         atmel_flush_buffer(port);
2033 }
2034
2035 /*
2036  * Power / Clock management.
2037  */
2038 static void atmel_serial_pm(struct uart_port *port, unsigned int state,
2039                             unsigned int oldstate)
2040 {
2041         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2042
2043         switch (state) {
2044         case 0:
2045                 /*
2046                  * Enable the peripheral clock for this serial port.
2047                  * This is called on uart_open() or a resume event.
2048                  */
2049                 clk_prepare_enable(atmel_port->clk);
2050
2051                 /* re-enable interrupts if we disabled some on suspend */
2052                 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->backup_imr);
2053                 break;
2054         case 3:
2055                 /* Back up the interrupt mask and disable all interrupts */
2056                 atmel_port->backup_imr = atmel_uart_readl(port, ATMEL_US_IMR);
2057                 atmel_uart_writel(port, ATMEL_US_IDR, -1);
2058
2059                 /*
2060                  * Disable the peripheral clock for this serial port.
2061                  * This is called on uart_close() or a suspend event.
2062                  */
2063                 clk_disable_unprepare(atmel_port->clk);
2064                 break;
2065         default:
2066                 dev_err(port->dev, "atmel_serial: unknown pm %d\n", state);
2067         }
2068 }
2069
2070 /*
2071  * Change the port parameters
2072  */
2073 static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
2074                               struct ktermios *old)
2075 {
2076         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2077         unsigned long flags;
2078         unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
2079
2080         /* save the current mode register */
2081         mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
2082
2083         /* reset the mode, clock divisor, parity, stop bits and data size */
2084         mode &= ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP |
2085                   ATMEL_US_PAR | ATMEL_US_USMODE);
2086
2087         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
2088
2089         /* byte size */
2090         switch (termios->c_cflag & CSIZE) {
2091         case CS5:
2092                 mode |= ATMEL_US_CHRL_5;
2093                 break;
2094         case CS6:
2095                 mode |= ATMEL_US_CHRL_6;
2096                 break;
2097         case CS7:
2098                 mode |= ATMEL_US_CHRL_7;
2099                 break;
2100         default:
2101                 mode |= ATMEL_US_CHRL_8;
2102                 break;
2103         }
2104
2105         /* stop bits */
2106         if (termios->c_cflag & CSTOPB)
2107                 mode |= ATMEL_US_NBSTOP_2;
2108
2109         /* parity */
2110         if (termios->c_cflag & PARENB) {
2111                 /* Mark or Space parity */
2112                 if (termios->c_cflag & CMSPAR) {
2113                         if (termios->c_cflag & PARODD)
2114                                 mode |= ATMEL_US_PAR_MARK;
2115                         else
2116                                 mode |= ATMEL_US_PAR_SPACE;
2117                 } else if (termios->c_cflag & PARODD)
2118                         mode |= ATMEL_US_PAR_ODD;
2119                 else
2120                         mode |= ATMEL_US_PAR_EVEN;
2121         } else
2122                 mode |= ATMEL_US_PAR_NONE;
2123
2124         spin_lock_irqsave(&port->lock, flags);
2125
2126         port->read_status_mask = ATMEL_US_OVRE;
2127         if (termios->c_iflag & INPCK)
2128                 port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2129         if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2130                 port->read_status_mask |= ATMEL_US_RXBRK;
2131
2132         if (atmel_use_pdc_rx(port))
2133                 /* need to enable error interrupts */
2134                 atmel_uart_writel(port, ATMEL_US_IER, port->read_status_mask);
2135
2136         /*
2137          * Characters to ignore
2138          */
2139         port->ignore_status_mask = 0;
2140         if (termios->c_iflag & IGNPAR)
2141                 port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
2142         if (termios->c_iflag & IGNBRK) {
2143                 port->ignore_status_mask |= ATMEL_US_RXBRK;
2144                 /*
2145                  * If we're ignoring parity and break indicators,
2146                  * ignore overruns too (for real raw support).
2147                  */
2148                 if (termios->c_iflag & IGNPAR)
2149                         port->ignore_status_mask |= ATMEL_US_OVRE;
2150         }
2151         /* TODO: Ignore all characters if CREAD is set.*/
2152
2153         /* update the per-port timeout */
2154         uart_update_timeout(port, termios->c_cflag, baud);
2155
2156         /*
2157          * save/disable interrupts. The tty layer will ensure that the
2158          * transmitter is empty if requested by the caller, so there's
2159          * no need to wait for it here.
2160          */
2161         imr = atmel_uart_readl(port, ATMEL_US_IMR);
2162         atmel_uart_writel(port, ATMEL_US_IDR, -1);
2163
2164         /* disable receiver and transmitter */
2165         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
2166
2167         /* mode */
2168         if (port->rs485.flags & SER_RS485_ENABLED) {
2169                 atmel_uart_writel(port, ATMEL_US_TTGR,
2170                                   port->rs485.delay_rts_after_send);
2171                 mode |= ATMEL_US_USMODE_RS485;
2172         } else if (termios->c_cflag & CRTSCTS) {
2173                 /* RS232 with hardware handshake (RTS/CTS) */
2174                 if (atmel_use_fifo(port) &&
2175                     !mctrl_gpio_to_gpiod(atmel_port->gpios, UART_GPIO_CTS)) {
2176                         /*
2177                          * with ATMEL_US_USMODE_HWHS set, the controller will
2178                          * be able to drive the RTS pin high/low when the RX
2179                          * FIFO is above RXFTHRES/below RXFTHRES2.
2180                          * It will also disable the transmitter when the CTS
2181                          * pin is high.
2182                          * This mode is not activated if CTS pin is a GPIO
2183                          * because in this case, the transmitter is always
2184                          * disabled (there must be an internal pull-up
2185                          * responsible for this behaviour).
2186                          * If the RTS pin is a GPIO, the controller won't be
2187                          * able to drive it according to the FIFO thresholds,
2188                          * but it will be handled by the driver.
2189                          */
2190                         mode |= ATMEL_US_USMODE_HWHS;
2191                 } else {
2192                         /*
2193                          * For platforms without FIFO, the flow control is
2194                          * handled by the driver.
2195                          */
2196                         mode |= ATMEL_US_USMODE_NORMAL;
2197                 }
2198         } else {
2199                 /* RS232 without hadware handshake */
2200                 mode |= ATMEL_US_USMODE_NORMAL;
2201         }
2202
2203         /* set the mode, clock divisor, parity, stop bits and data size */
2204         atmel_uart_writel(port, ATMEL_US_MR, mode);
2205
2206         /*
2207          * when switching the mode, set the RTS line state according to the
2208          * new mode, otherwise keep the former state
2209          */
2210         if ((old_mode & ATMEL_US_USMODE) != (mode & ATMEL_US_USMODE)) {
2211                 unsigned int rts_state;
2212
2213                 if ((mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_HWHS) {
2214                         /* let the hardware control the RTS line */
2215                         rts_state = ATMEL_US_RTSDIS;
2216                 } else {
2217                         /* force RTS line to low level */
2218                         rts_state = ATMEL_US_RTSEN;
2219                 }
2220
2221                 atmel_uart_writel(port, ATMEL_US_CR, rts_state);
2222         }
2223
2224         /*
2225          * Set the baud rate:
2226          * Fractional baudrate allows to setup output frequency more
2227          * accurately. This feature is enabled only when using normal mode.
2228          * baudrate = selected clock / (8 * (2 - OVER) * (CD + FP / 8))
2229          * Currently, OVER is always set to 0 so we get
2230          * baudrate = selected clock / (16 * (CD + FP / 8))
2231          * then
2232          * 8 CD + FP = selected clock / (2 * baudrate)
2233          */
2234         if (atmel_port->has_frac_baudrate &&
2235             (mode & ATMEL_US_USMODE) == ATMEL_US_USMODE_NORMAL) {
2236                 div = DIV_ROUND_CLOSEST(port->uartclk, baud * 2);
2237                 cd = div >> 3;
2238                 fp = div & ATMEL_US_FP_MASK;
2239         } else {
2240                 cd = uart_get_divisor(port, baud);
2241         }
2242
2243         if (cd > 65535) {       /* BRGR is 16-bit, so switch to slower clock */
2244                 cd /= 8;
2245                 mode |= ATMEL_US_USCLKS_MCK_DIV8;
2246         }
2247         quot = cd | fp << ATMEL_US_FP_OFFSET;
2248
2249         atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2250         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2251         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2252
2253         /* restore interrupts */
2254         atmel_uart_writel(port, ATMEL_US_IER, imr);
2255
2256         /* CTS flow-control and modem-status interrupts */
2257         if (UART_ENABLE_MS(port, termios->c_cflag))
2258                 atmel_enable_ms(port);
2259         else
2260                 atmel_disable_ms(port);
2261
2262         spin_unlock_irqrestore(&port->lock, flags);
2263 }
2264
2265 static void atmel_set_ldisc(struct uart_port *port, struct ktermios *termios)
2266 {
2267         if (termios->c_line == N_PPS) {
2268                 port->flags |= UPF_HARDPPS_CD;
2269                 spin_lock_irq(&port->lock);
2270                 atmel_enable_ms(port);
2271                 spin_unlock_irq(&port->lock);
2272         } else {
2273                 port->flags &= ~UPF_HARDPPS_CD;
2274                 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2275                         spin_lock_irq(&port->lock);
2276                         atmel_disable_ms(port);
2277                         spin_unlock_irq(&port->lock);
2278                 }
2279         }
2280 }
2281
2282 /*
2283  * Return string describing the specified port
2284  */
2285 static const char *atmel_type(struct uart_port *port)
2286 {
2287         return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
2288 }
2289
2290 /*
2291  * Release the memory region(s) being used by 'port'.
2292  */
2293 static void atmel_release_port(struct uart_port *port)
2294 {
2295         struct platform_device *pdev = to_platform_device(port->dev);
2296         int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2297
2298         release_mem_region(port->mapbase, size);
2299
2300         if (port->flags & UPF_IOREMAP) {
2301                 iounmap(port->membase);
2302                 port->membase = NULL;
2303         }
2304 }
2305
2306 /*
2307  * Request the memory region(s) being used by 'port'.
2308  */
2309 static int atmel_request_port(struct uart_port *port)
2310 {
2311         struct platform_device *pdev = to_platform_device(port->dev);
2312         int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2313
2314         if (!request_mem_region(port->mapbase, size, "atmel_serial"))
2315                 return -EBUSY;
2316
2317         if (port->flags & UPF_IOREMAP) {
2318                 port->membase = ioremap(port->mapbase, size);
2319                 if (port->membase == NULL) {
2320                         release_mem_region(port->mapbase, size);
2321                         return -ENOMEM;
2322                 }
2323         }
2324
2325         return 0;
2326 }
2327
2328 /*
2329  * Configure/autoconfigure the port.
2330  */
2331 static void atmel_config_port(struct uart_port *port, int flags)
2332 {
2333         if (flags & UART_CONFIG_TYPE) {
2334                 port->type = PORT_ATMEL;
2335                 atmel_request_port(port);
2336         }
2337 }
2338
2339 /*
2340  * Verify the new serial_struct (for TIOCSSERIAL).
2341  */
2342 static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
2343 {
2344         int ret = 0;
2345         if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
2346                 ret = -EINVAL;
2347         if (port->irq != ser->irq)
2348                 ret = -EINVAL;
2349         if (ser->io_type != SERIAL_IO_MEM)
2350                 ret = -EINVAL;
2351         if (port->uartclk / 16 != ser->baud_base)
2352                 ret = -EINVAL;
2353         if (port->mapbase != (unsigned long)ser->iomem_base)
2354                 ret = -EINVAL;
2355         if (port->iobase != ser->port)
2356                 ret = -EINVAL;
2357         if (ser->hub6 != 0)
2358                 ret = -EINVAL;
2359         return ret;
2360 }
2361
2362 #ifdef CONFIG_CONSOLE_POLL
2363 static int atmel_poll_get_char(struct uart_port *port)
2364 {
2365         while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_RXRDY))
2366                 cpu_relax();
2367
2368         return atmel_uart_read_char(port);
2369 }
2370
2371 static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
2372 {
2373         while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2374                 cpu_relax();
2375
2376         atmel_uart_write_char(port, ch);
2377 }
2378 #endif
2379
2380 static const struct uart_ops atmel_pops = {
2381         .tx_empty       = atmel_tx_empty,
2382         .set_mctrl      = atmel_set_mctrl,
2383         .get_mctrl      = atmel_get_mctrl,
2384         .stop_tx        = atmel_stop_tx,
2385         .start_tx       = atmel_start_tx,
2386         .stop_rx        = atmel_stop_rx,
2387         .enable_ms      = atmel_enable_ms,
2388         .break_ctl      = atmel_break_ctl,
2389         .startup        = atmel_startup,
2390         .shutdown       = atmel_shutdown,
2391         .flush_buffer   = atmel_flush_buffer,
2392         .set_termios    = atmel_set_termios,
2393         .set_ldisc      = atmel_set_ldisc,
2394         .type           = atmel_type,
2395         .release_port   = atmel_release_port,
2396         .request_port   = atmel_request_port,
2397         .config_port    = atmel_config_port,
2398         .verify_port    = atmel_verify_port,
2399         .pm             = atmel_serial_pm,
2400 #ifdef CONFIG_CONSOLE_POLL
2401         .poll_get_char  = atmel_poll_get_char,
2402         .poll_put_char  = atmel_poll_put_char,
2403 #endif
2404 };
2405
2406 /*
2407  * Configure the port from the platform device resource info.
2408  */
2409 static int atmel_init_port(struct atmel_uart_port *atmel_port,
2410                                       struct platform_device *pdev)
2411 {
2412         int ret;
2413         struct uart_port *port = &atmel_port->uart;
2414         struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2415
2416         atmel_init_property(atmel_port, pdev);
2417         atmel_set_ops(port);
2418
2419         atmel_init_rs485(port, pdev);
2420
2421         port->iotype            = UPIO_MEM;
2422         port->flags             = UPF_BOOT_AUTOCONF;
2423         port->ops               = &atmel_pops;
2424         port->fifosize          = 1;
2425         port->dev               = &pdev->dev;
2426         port->mapbase   = pdev->resource[0].start;
2427         port->irq       = pdev->resource[1].start;
2428         port->rs485_config      = atmel_config_rs485;
2429
2430         memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
2431
2432         if (pdata && pdata->regs) {
2433                 /* Already mapped by setup code */
2434                 port->membase = pdata->regs;
2435         } else {
2436                 port->flags     |= UPF_IOREMAP;
2437                 port->membase   = NULL;
2438         }
2439
2440         /* for console, the clock could already be configured */
2441         if (!atmel_port->clk) {
2442                 atmel_port->clk = clk_get(&pdev->dev, "usart");
2443                 if (IS_ERR(atmel_port->clk)) {
2444                         ret = PTR_ERR(atmel_port->clk);
2445                         atmel_port->clk = NULL;
2446                         return ret;
2447                 }
2448                 ret = clk_prepare_enable(atmel_port->clk);
2449                 if (ret) {
2450                         clk_put(atmel_port->clk);
2451                         atmel_port->clk = NULL;
2452                         return ret;
2453                 }
2454                 port->uartclk = clk_get_rate(atmel_port->clk);
2455                 clk_disable_unprepare(atmel_port->clk);
2456                 /* only enable clock when USART is in use */
2457         }
2458
2459         /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2460         if (port->rs485.flags & SER_RS485_ENABLED)
2461                 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
2462         else if (atmel_use_pdc_tx(port)) {
2463                 port->fifosize = PDC_BUFFER_SIZE;
2464                 atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
2465         } else {
2466                 atmel_port->tx_done_mask = ATMEL_US_TXRDY;
2467         }
2468
2469         return 0;
2470 }
2471
2472 struct platform_device *atmel_default_console_device;   /* the serial console device */
2473
2474 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2475 static void atmel_console_putchar(struct uart_port *port, int ch)
2476 {
2477         while (!(atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY))
2478                 cpu_relax();
2479         atmel_uart_write_char(port, ch);
2480 }
2481
2482 /*
2483  * Interrupts are disabled on entering
2484  */
2485 static void atmel_console_write(struct console *co, const char *s, u_int count)
2486 {
2487         struct uart_port *port = &atmel_ports[co->index].uart;
2488         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2489         unsigned int status, imr;
2490         unsigned int pdc_tx;
2491
2492         /*
2493          * First, save IMR and then disable interrupts
2494          */
2495         imr = atmel_uart_readl(port, ATMEL_US_IMR);
2496         atmel_uart_writel(port, ATMEL_US_IDR,
2497                           ATMEL_US_RXRDY | atmel_port->tx_done_mask);
2498
2499         /* Store PDC transmit status and disable it */
2500         pdc_tx = atmel_uart_readl(port, ATMEL_PDC_PTSR) & ATMEL_PDC_TXTEN;
2501         atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTDIS);
2502
2503         /* Make sure that tx path is actually able to send characters */
2504         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN);
2505
2506         uart_console_write(port, s, count, atmel_console_putchar);
2507
2508         /*
2509          * Finally, wait for transmitter to become empty
2510          * and restore IMR
2511          */
2512         do {
2513                 status = atmel_uart_readl(port, ATMEL_US_CSR);
2514         } while (!(status & ATMEL_US_TXRDY));
2515
2516         /* Restore PDC transmit status */
2517         if (pdc_tx)
2518                 atmel_uart_writel(port, ATMEL_PDC_PTCR, ATMEL_PDC_TXTEN);
2519
2520         /* set interrupts back the way they were */
2521         atmel_uart_writel(port, ATMEL_US_IER, imr);
2522 }
2523
2524 /*
2525  * If the port was already initialised (eg, by a boot loader),
2526  * try to determine the current setup.
2527  */
2528 static void __init atmel_console_get_options(struct uart_port *port, int *baud,
2529                                              int *parity, int *bits)
2530 {
2531         unsigned int mr, quot;
2532
2533         /*
2534          * If the baud rate generator isn't running, the port wasn't
2535          * initialized by the boot loader.
2536          */
2537         quot = atmel_uart_readl(port, ATMEL_US_BRGR) & ATMEL_US_CD;
2538         if (!quot)
2539                 return;
2540
2541         mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_CHRL;
2542         if (mr == ATMEL_US_CHRL_8)
2543                 *bits = 8;
2544         else
2545                 *bits = 7;
2546
2547         mr = atmel_uart_readl(port, ATMEL_US_MR) & ATMEL_US_PAR;
2548         if (mr == ATMEL_US_PAR_EVEN)
2549                 *parity = 'e';
2550         else if (mr == ATMEL_US_PAR_ODD)
2551                 *parity = 'o';
2552
2553         /*
2554          * The serial core only rounds down when matching this to a
2555          * supported baud rate. Make sure we don't end up slightly
2556          * lower than one of those, as it would make us fall through
2557          * to a much lower baud rate than we really want.
2558          */
2559         *baud = port->uartclk / (16 * (quot - 1));
2560 }
2561
2562 static int __init atmel_console_setup(struct console *co, char *options)
2563 {
2564         int ret;
2565         struct uart_port *port = &atmel_ports[co->index].uart;
2566         int baud = 115200;
2567         int bits = 8;
2568         int parity = 'n';
2569         int flow = 'n';
2570
2571         if (port->membase == NULL) {
2572                 /* Port not initialized yet - delay setup */
2573                 return -ENODEV;
2574         }
2575
2576         ret = clk_prepare_enable(atmel_ports[co->index].clk);
2577         if (ret)
2578                 return ret;
2579
2580         atmel_uart_writel(port, ATMEL_US_IDR, -1);
2581         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
2582         atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
2583
2584         if (options)
2585                 uart_parse_options(options, &baud, &parity, &bits, &flow);
2586         else
2587                 atmel_console_get_options(port, &baud, &parity, &bits);
2588
2589         return uart_set_options(port, co, baud, parity, bits, flow);
2590 }
2591
2592 static struct uart_driver atmel_uart;
2593
2594 static struct console atmel_console = {
2595         .name           = ATMEL_DEVICENAME,
2596         .write          = atmel_console_write,
2597         .device         = uart_console_device,
2598         .setup          = atmel_console_setup,
2599         .flags          = CON_PRINTBUFFER,
2600         .index          = -1,
2601         .data           = &atmel_uart,
2602 };
2603
2604 #define ATMEL_CONSOLE_DEVICE    (&atmel_console)
2605
2606 /*
2607  * Early console initialization (before VM subsystem initialized).
2608  */
2609 static int __init atmel_console_init(void)
2610 {
2611         int ret;
2612         if (atmel_default_console_device) {
2613                 struct atmel_uart_data *pdata =
2614                         dev_get_platdata(&atmel_default_console_device->dev);
2615                 int id = pdata->num;
2616                 struct atmel_uart_port *atmel_port = &atmel_ports[id];
2617
2618                 atmel_port->backup_imr = 0;
2619                 atmel_port->uart.line = id;
2620
2621                 add_preferred_console(ATMEL_DEVICENAME, id, NULL);
2622                 ret = atmel_init_port(atmel_port, atmel_default_console_device);
2623                 if (ret)
2624                         return ret;
2625                 register_console(&atmel_console);
2626         }
2627
2628         return 0;
2629 }
2630
2631 console_initcall(atmel_console_init);
2632
2633 /*
2634  * Late console initialization.
2635  */
2636 static int __init atmel_late_console_init(void)
2637 {
2638         if (atmel_default_console_device
2639             && !(atmel_console.flags & CON_ENABLED))
2640                 register_console(&atmel_console);
2641
2642         return 0;
2643 }
2644
2645 core_initcall(atmel_late_console_init);
2646
2647 static inline bool atmel_is_console_port(struct uart_port *port)
2648 {
2649         return port->cons && port->cons->index == port->line;
2650 }
2651
2652 #else
2653 #define ATMEL_CONSOLE_DEVICE    NULL
2654
2655 static inline bool atmel_is_console_port(struct uart_port *port)
2656 {
2657         return false;
2658 }
2659 #endif
2660
2661 static struct uart_driver atmel_uart = {
2662         .owner          = THIS_MODULE,
2663         .driver_name    = "atmel_serial",
2664         .dev_name       = ATMEL_DEVICENAME,
2665         .major          = SERIAL_ATMEL_MAJOR,
2666         .minor          = MINOR_START,
2667         .nr             = ATMEL_MAX_UART,
2668         .cons           = ATMEL_CONSOLE_DEVICE,
2669 };
2670
2671 #ifdef CONFIG_PM
2672 static bool atmel_serial_clk_will_stop(void)
2673 {
2674 #ifdef CONFIG_ARCH_AT91
2675         return at91_suspend_entering_slow_clock();
2676 #else
2677         return false;
2678 #endif
2679 }
2680
2681 static int atmel_serial_suspend(struct platform_device *pdev,
2682                                 pm_message_t state)
2683 {
2684         struct uart_port *port = platform_get_drvdata(pdev);
2685         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2686
2687         if (atmel_is_console_port(port) && console_suspend_enabled) {
2688                 /* Drain the TX shifter */
2689                 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
2690                          ATMEL_US_TXEMPTY))
2691                         cpu_relax();
2692         }
2693
2694         /* we can not wake up if we're running on slow clock */
2695         atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
2696         if (atmel_serial_clk_will_stop()) {
2697                 unsigned long flags;
2698
2699                 spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2700                 atmel_port->suspended = true;
2701                 spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2702                 device_set_wakeup_enable(&pdev->dev, 0);
2703         }
2704
2705         uart_suspend_port(&atmel_uart, port);
2706
2707         return 0;
2708 }
2709
2710 static int atmel_serial_resume(struct platform_device *pdev)
2711 {
2712         struct uart_port *port = platform_get_drvdata(pdev);
2713         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2714         unsigned long flags;
2715
2716         spin_lock_irqsave(&atmel_port->lock_suspended, flags);
2717         if (atmel_port->pending) {
2718                 atmel_handle_receive(port, atmel_port->pending);
2719                 atmel_handle_status(port, atmel_port->pending,
2720                                     atmel_port->pending_status);
2721                 atmel_handle_transmit(port, atmel_port->pending);
2722                 atmel_port->pending = 0;
2723         }
2724         atmel_port->suspended = false;
2725         spin_unlock_irqrestore(&atmel_port->lock_suspended, flags);
2726
2727         uart_resume_port(&atmel_uart, port);
2728         device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
2729
2730         return 0;
2731 }
2732 #else
2733 #define atmel_serial_suspend NULL
2734 #define atmel_serial_resume NULL
2735 #endif
2736
2737 static void atmel_serial_probe_fifos(struct atmel_uart_port *atmel_port,
2738                                      struct platform_device *pdev)
2739 {
2740         atmel_port->fifo_size = 0;
2741         atmel_port->rts_low = 0;
2742         atmel_port->rts_high = 0;
2743
2744         if (of_property_read_u32(pdev->dev.of_node,
2745                                  "atmel,fifo-size",
2746                                  &atmel_port->fifo_size))
2747                 return;
2748
2749         if (!atmel_port->fifo_size)
2750                 return;
2751
2752         if (atmel_port->fifo_size < ATMEL_MIN_FIFO_SIZE) {
2753                 atmel_port->fifo_size = 0;
2754                 dev_err(&pdev->dev, "Invalid FIFO size\n");
2755                 return;
2756         }
2757
2758         /*
2759          * 0 <= rts_low <= rts_high <= fifo_size
2760          * Once their CTS line asserted by the remote peer, some x86 UARTs tend
2761          * to flush their internal TX FIFO, commonly up to 16 data, before
2762          * actually stopping to send new data. So we try to set the RTS High
2763          * Threshold to a reasonably high value respecting this 16 data
2764          * empirical rule when possible.
2765          */
2766         atmel_port->rts_high = max_t(int, atmel_port->fifo_size >> 1,
2767                                atmel_port->fifo_size - ATMEL_RTS_HIGH_OFFSET);
2768         atmel_port->rts_low  = max_t(int, atmel_port->fifo_size >> 2,
2769                                atmel_port->fifo_size - ATMEL_RTS_LOW_OFFSET);
2770
2771         dev_info(&pdev->dev, "Using FIFO (%u data)\n",
2772                  atmel_port->fifo_size);
2773         dev_dbg(&pdev->dev, "RTS High Threshold : %2u data\n",
2774                 atmel_port->rts_high);
2775         dev_dbg(&pdev->dev, "RTS Low Threshold  : %2u data\n",
2776                 atmel_port->rts_low);
2777 }
2778
2779 static int atmel_serial_probe(struct platform_device *pdev)
2780 {
2781         struct atmel_uart_port *atmel_port;
2782         struct device_node *np = pdev->dev.of_node;
2783         struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev);
2784         void *data;
2785         int ret = -ENODEV;
2786         bool rs485_enabled;
2787
2788         BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2789
2790         if (np)
2791                 ret = of_alias_get_id(np, "serial");
2792         else
2793                 if (pdata)
2794                         ret = pdata->num;
2795
2796         if (ret < 0)
2797                 /* port id not found in platform data nor device-tree aliases:
2798                  * auto-enumerate it */
2799                 ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
2800
2801         if (ret >= ATMEL_MAX_UART) {
2802                 ret = -ENODEV;
2803                 goto err;
2804         }
2805
2806         if (test_and_set_bit(ret, atmel_ports_in_use)) {
2807                 /* port already in use */
2808                 ret = -EBUSY;
2809                 goto err;
2810         }
2811
2812         atmel_port = &atmel_ports[ret];
2813         atmel_port->backup_imr = 0;
2814         atmel_port->uart.line = ret;
2815         atmel_serial_probe_fifos(atmel_port, pdev);
2816
2817         atomic_set(&atmel_port->tasklet_shutdown, 0);
2818         spin_lock_init(&atmel_port->lock_suspended);
2819
2820         ret = atmel_init_port(atmel_port, pdev);
2821         if (ret)
2822                 goto err_clear_bit;
2823
2824         atmel_port->gpios = mctrl_gpio_init(&atmel_port->uart, 0);
2825         if (IS_ERR(atmel_port->gpios)) {
2826                 ret = PTR_ERR(atmel_port->gpios);
2827                 goto err_clear_bit;
2828         }
2829
2830         if (!atmel_use_pdc_rx(&atmel_port->uart)) {
2831                 ret = -ENOMEM;
2832                 data = kmalloc(sizeof(struct atmel_uart_char)
2833                                 * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
2834                 if (!data)
2835                         goto err_alloc_ring;
2836                 atmel_port->rx_ring.buf = data;
2837         }
2838
2839         rs485_enabled = atmel_port->uart.rs485.flags & SER_RS485_ENABLED;
2840
2841         ret = uart_add_one_port(&atmel_uart, &atmel_port->uart);
2842         if (ret)
2843                 goto err_add_port;
2844
2845 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2846         if (atmel_is_console_port(&atmel_port->uart)
2847                         && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
2848                 /*
2849                  * The serial core enabled the clock for us, so undo
2850                  * the clk_prepare_enable() in atmel_console_setup()
2851                  */
2852                 clk_disable_unprepare(atmel_port->clk);
2853         }
2854 #endif
2855
2856         device_init_wakeup(&pdev->dev, 1);
2857         platform_set_drvdata(pdev, atmel_port);
2858
2859         /*
2860          * The peripheral clock has been disabled by atmel_init_port():
2861          * enable it before accessing I/O registers
2862          */
2863         clk_prepare_enable(atmel_port->clk);
2864
2865         if (rs485_enabled) {
2866                 atmel_uart_writel(&atmel_port->uart, ATMEL_US_MR,
2867                                   ATMEL_US_USMODE_NORMAL);
2868                 atmel_uart_writel(&atmel_port->uart, ATMEL_US_CR,
2869                                   ATMEL_US_RTSEN);
2870         }
2871
2872         /*
2873          * Get port name of usart or uart
2874          */
2875         atmel_get_ip_name(&atmel_port->uart);
2876
2877         /*
2878          * The peripheral clock can now safely be disabled till the port
2879          * is used
2880          */
2881         clk_disable_unprepare(atmel_port->clk);
2882
2883         return 0;
2884
2885 err_add_port:
2886         kfree(atmel_port->rx_ring.buf);
2887         atmel_port->rx_ring.buf = NULL;
2888 err_alloc_ring:
2889         if (!atmel_is_console_port(&atmel_port->uart)) {
2890                 clk_put(atmel_port->clk);
2891                 atmel_port->clk = NULL;
2892         }
2893 err_clear_bit:
2894         clear_bit(atmel_port->uart.line, atmel_ports_in_use);
2895 err:
2896         return ret;
2897 }
2898
2899 /*
2900  * Even if the driver is not modular, it makes sense to be able to
2901  * unbind a device: there can be many bound devices, and there are
2902  * situations where dynamic binding and unbinding can be useful.
2903  *
2904  * For example, a connected device can require a specific firmware update
2905  * protocol that needs bitbanging on IO lines, but use the regular serial
2906  * port in the normal case.
2907  */
2908 static int atmel_serial_remove(struct platform_device *pdev)
2909 {
2910         struct uart_port *port = platform_get_drvdata(pdev);
2911         struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
2912         int ret = 0;
2913
2914         tasklet_kill(&atmel_port->tasklet_rx);
2915         tasklet_kill(&atmel_port->tasklet_tx);
2916
2917         device_init_wakeup(&pdev->dev, 0);
2918
2919         ret = uart_remove_one_port(&atmel_uart, port);
2920
2921         kfree(atmel_port->rx_ring.buf);
2922
2923         /* "port" is allocated statically, so we shouldn't free it */
2924
2925         clear_bit(port->line, atmel_ports_in_use);
2926
2927         clk_put(atmel_port->clk);
2928         atmel_port->clk = NULL;
2929
2930         return ret;
2931 }
2932
2933 static struct platform_driver atmel_serial_driver = {
2934         .probe          = atmel_serial_probe,
2935         .remove         = atmel_serial_remove,
2936         .suspend        = atmel_serial_suspend,
2937         .resume         = atmel_serial_resume,
2938         .driver         = {
2939                 .name                   = "atmel_usart",
2940                 .of_match_table         = of_match_ptr(atmel_serial_dt_ids),
2941         },
2942 };
2943
2944 static int __init atmel_serial_init(void)
2945 {
2946         int ret;
2947
2948         ret = uart_register_driver(&atmel_uart);
2949         if (ret)
2950                 return ret;
2951
2952         ret = platform_driver_register(&atmel_serial_driver);
2953         if (ret)
2954                 uart_unregister_driver(&atmel_uart);
2955
2956         return ret;
2957 }
2958 device_initcall(atmel_serial_init);