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