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