summaryrefslogtreecommitdiff
path: root/drivers/serial/mxs-auart.c
blob: 0791af105f72c9c7ab1ff7aed6f61ad00c31939b (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
/*
 * Freescale STMP37XX/STMP378X Application UART driver
 *
 * Author: dmitry pervushin <dimka@embeddedalley.com>
 *
 * Copyright 2008-2010 Freescale Semiconductor, Inc.
 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/clk.h>
#include <linux/cpufreq.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/uaccess.h>

#include <asm/cacheflush.h>

#include <mach/hardware.h>
#include <mach/device.h>
#include <mach/dmaengine.h>

#include "regs-uartapp.h"

#define MXS_AUART_MAJOR	242
#define MXS_AUART_RX_THRESHOLD 16

struct mxs_auart_port {
	struct uart_port port;

	unsigned int flags;
#define MXS_AUART_PORT_OPEN	0x80000000
#define MXS_AUART_PORT_DMA_MODE	0x80000000
	unsigned int ctrl;

	unsigned int irq[3];

	struct clk *clk;
	struct device *dev;
	unsigned int dma_rx_chan;
	unsigned int dma_tx_chan;
	unsigned int dma_rx_buffer_size;
	struct list_head rx_done;
	struct list_head free;
	struct mxs_dma_desc *tx;
	struct tasklet_struct rx_task;
};

static void mxs_auart_stop_tx(struct uart_port *u);
static void mxs_auart_submit_tx(struct mxs_auart_port *s, int size);
static void mxs_auart_submit_rx(struct mxs_auart_port *s);

static inline struct mxs_auart_port *to_auart_port(struct uart_port *u)
{
	return container_of(u, struct mxs_auart_port, port);
}

static inline void mxs_auart_tx_chars(struct mxs_auart_port *s)
{
	struct circ_buf *xmit = &s->port.info->xmit;

	if (s->flags & MXS_AUART_PORT_DMA_MODE) {
		int i = 0, size;
		char *buffer = s->tx->buffer;

		if (mxs_dma_desc_pending(s->tx))
			return;
		while (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
			if (i >= PAGE_SIZE)
				break;
			if (s->port.x_char) {
				buffer[i++] = s->port.x_char;
				s->port.x_char = 0;
				continue;
			}
			size = min_t(u32, PAGE_SIZE - i,
				     CIRC_CNT_TO_END(xmit->head,
						     xmit->tail,
						     UART_XMIT_SIZE));
			memcpy(buffer + i, xmit->buf + xmit->tail, size);
			xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
			if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
				uart_write_wakeup(&s->port);
			i += size;
		}
		if (i)
			mxs_auart_submit_tx(s, i);
		else {
			if (uart_tx_stopped(&s->port))
				mxs_auart_stop_tx(&s->port);
		}
		return;
	}

	while (!(__raw_readl(s->port.membase + HW_UARTAPP_STAT) &
		 BM_UARTAPP_STAT_TXFF)) {
		if (s->port.x_char) {
			__raw_writel(s->port.x_char,
				     s->port.membase + HW_UARTAPP_DATA);
			s->port.x_char = 0;
			continue;
		}
		if (!uart_circ_empty(xmit) && !uart_tx_stopped(&s->port)) {
			__raw_writel(xmit->buf[xmit->tail],
				     s->port.membase + HW_UARTAPP_DATA);
			xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
			if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
				uart_write_wakeup(&s->port);
		} else
			break;
	}
	if (uart_circ_empty(&(s->port.info->xmit)))
		__raw_writel(BM_UARTAPP_INTR_TXIEN,
			     s->port.membase + HW_UARTAPP_INTR_CLR);
	else
		__raw_writel(BM_UARTAPP_INTR_TXIEN,
			     s->port.membase + HW_UARTAPP_INTR_SET);

	if (uart_tx_stopped(&s->port))
		mxs_auart_stop_tx(&s->port);
}

static inline unsigned int
mxs_auart_rx_char(struct mxs_auart_port *s, unsigned int stat, u8 c)
{
	int flag;

	flag = TTY_NORMAL;
	if (stat & BM_UARTAPP_STAT_BERR) {
		stat &= ~BM_UARTAPP_STAT_BERR;
		s->port.icount.brk++;
		if (uart_handle_break(&s->port))
			return stat;
		flag = TTY_BREAK;
	} else if (stat & BM_UARTAPP_STAT_PERR) {
		stat &= ~BM_UARTAPP_STAT_PERR;
		s->port.icount.parity++;
		flag = TTY_PARITY;
	} else if (stat & BM_UARTAPP_STAT_FERR) {
		stat &= ~BM_UARTAPP_STAT_FERR;
		s->port.icount.frame++;
		flag = TTY_FRAME;
	}

	if (stat & BM_UARTAPP_STAT_OERR)
		s->port.icount.overrun++;

	if (uart_handle_sysrq_char(&s->port, c))
		return stat;

	uart_insert_char(&s->port, stat, BM_UARTAPP_STAT_OERR, c, flag);
	return stat;
}

static void mxs_auart_rx_chars(struct mxs_auart_port *s)
{
	u8 c;
	struct tty_struct *tty = s->port.info->port.tty;
	u32 stat = 0;

	if (s->flags & MXS_AUART_PORT_DMA_MODE) {
		int i, count;
		struct list_head *p, *q;
		LIST_HEAD(list);
		struct mxs_dma_desc *pdesc;
		mxs_dma_cooked(s->dma_rx_chan, &list);
		stat = __raw_readl(s->port.membase + HW_UARTAPP_STAT);
		list_for_each_safe(p, q, &list) {
			u8 *buffer;
			list_del(p);
			pdesc = list_entry(p, struct mxs_dma_desc, node);
			count = stat & BM_UARTAPP_STAT_RXCOUNT;
			buffer = pdesc->buffer;
			for (i = 0; i < count; i++)
				stat = mxs_auart_rx_char(s, stat, buffer[i]);
			list_add(p, &s->free);
			stat = __raw_readl(s->port.membase + HW_UARTAPP_STAT);
		}
		mxs_auart_submit_rx(s);
		goto out;
	}
	for (;;) {
		stat = __raw_readl(s->port.membase + HW_UARTAPP_STAT);
		if (stat & BM_UARTAPP_STAT_RXFE)
			break;
		c = __raw_readl(s->port.membase + HW_UARTAPP_DATA);
		stat = mxs_auart_rx_char(s, stat, c);
		__raw_writel(stat, s->port.membase + HW_UARTAPP_STAT);
	}
out:
	__raw_writel(stat, s->port.membase + HW_UARTAPP_STAT);
	tty_flip_buffer_push(tty);
}

/* Allocate and initialize rx and tx DMA chains */
static int mxs_auart_dma_init(struct mxs_auart_port *s)
{
	int ret, i;
	struct list_head *p, *n;
	struct mxs_dma_desc *pdesc;

	ret = mxs_dma_request(s->dma_rx_chan, s->dev, dev_name(s->dev));
	if (ret)
		goto fail_get_dma_rx;
	ret = mxs_dma_request(s->dma_tx_chan, s->dev, dev_name(s->dev));
	if (ret)
		goto fail_get_dma_tx;
	ret = -ENOMEM;
	INIT_LIST_HEAD(&s->rx_done);
	INIT_LIST_HEAD(&s->free);
	s->tx = NULL;


	for (i = 0; i < 5; i++) {
		pdesc = mxs_dma_alloc_desc();
		if (pdesc == NULL || IS_ERR(pdesc))
			goto fail_alloc_desc;

		if (s->tx == NULL) {
			pdesc->buffer = dma_alloc_coherent(s->dev, PAGE_SIZE,
						   &pdesc->cmd.address,
						   GFP_DMA);
			if (pdesc->buffer == NULL)
				goto fail_alloc_desc;
			s->tx = pdesc;
		} else {
			pdesc->buffer = dma_alloc_coherent(s->dev,
						s->dma_rx_buffer_size,
						&pdesc->cmd.address,
						GFP_DMA);
			if (pdesc->buffer == NULL)
				goto fail_alloc_desc;
			list_add_tail(&pdesc->node, &s->free);
		}
	}
	/*
	   Tell DMA to select UART.
	   Both DMA channels are shared between app UART and IrDA.
	   Target id of 0 means UART, 1 means IrDA
	 */
	mxs_dma_set_target(s->dma_rx_chan, 0);
	mxs_dma_set_target(s->dma_tx_chan, 0);

	mxs_dma_enable_irq(s->dma_rx_chan, 1);
	mxs_dma_enable_irq(s->dma_tx_chan, 1);

	return 0;
fail_alloc_desc:
	if (s->tx) {
		if (s->tx->buffer)
			dma_free_coherent(s->dev,
					  PAGE_SIZE,
					  s->tx->buffer,
					  s->tx->cmd.address);
		s->tx->buffer = NULL;
		mxs_dma_free_desc(s->tx);
		s->tx = NULL;
	}
	list_for_each_safe(p, n, &s->free) {
		list_del(p);
		pdesc = list_entry(p, struct mxs_dma_desc, node);
		if (pdesc->buffer)
			dma_free_coherent(s->dev,
					  s->dma_rx_buffer_size,
					  pdesc->buffer,
					  pdesc->cmd.address);
		pdesc->buffer = NULL;
		mxs_dma_free_desc(pdesc);
	}
	mxs_dma_release(s->dma_tx_chan, s->dev);
fail_get_dma_tx:
	mxs_dma_release(s->dma_rx_chan, s->dev);
fail_get_dma_rx:
	WARN_ON(ret);
	return ret;
}

static void mxs_auart_dma_exit(struct mxs_auart_port *s)
{
	struct list_head *p, *n;
		LIST_HEAD(list);
	struct mxs_dma_desc *pdesc;

	mxs_dma_enable_irq(s->dma_rx_chan, 0);
	mxs_dma_enable_irq(s->dma_tx_chan, 0);

	mxs_dma_disable(s->dma_tx_chan);
	mxs_dma_disable(s->dma_rx_chan);

	mxs_dma_get_cooked(s->dma_tx_chan, &list);
	mxs_dma_get_cooked(s->dma_rx_chan, &s->free);

	mxs_dma_release(s->dma_tx_chan, s->dev);
	mxs_dma_release(s->dma_rx_chan, s->dev);

	if (s->tx) {
		if (s->tx->buffer)
			dma_free_coherent(s->dev,
					  PAGE_SIZE,
					  s->tx->buffer,
					  s->tx->cmd.address);
		s->tx->buffer = NULL;
		mxs_dma_free_desc(s->tx);
		s->tx = NULL;
	}
	list_for_each_safe(p, n, &s->free) {
		list_del(p);
		pdesc = list_entry(p, struct mxs_dma_desc, node);
		if (pdesc->buffer)
			dma_free_coherent(s->dev,
					  s->dma_rx_buffer_size,
					  pdesc->buffer,
					  pdesc->cmd.address);
		pdesc->buffer = NULL;
		mxs_dma_free_desc(pdesc);
	}
}

static void mxs_auart_submit_rx(struct mxs_auart_port *s)
{
	int ret;
	unsigned int pio_value;
	struct list_head *p, *n;
	struct mxs_dma_desc *pdesc;

	pio_value = BM_UARTAPP_CTRL0_RXTO_ENABLE |
		     BF_UARTAPP_CTRL0_RXTIMEOUT(0x80) |
		     BF_UARTAPP_CTRL0_XFER_COUNT(s->dma_rx_buffer_size);

	list_for_each_safe(p, n, &s->free) {
		list_del(p);
		pdesc = list_entry(p, struct mxs_dma_desc, node);
		pdesc->cmd.cmd.bits.bytes = s->dma_rx_buffer_size;
		pdesc->cmd.cmd.bits.terminate_flush = 1;
		pdesc->cmd.cmd.bits.pio_words = 1;
		pdesc->cmd.cmd.bits.wait4end = 1;
		pdesc->cmd.cmd.bits.dec_sem = 1;
		pdesc->cmd.cmd.bits.irq = 1;
		pdesc->cmd.cmd.bits.chain = 1;
		pdesc->cmd.cmd.bits.command = DMA_WRITE;
		pdesc->cmd.pio_words[0] = pio_value;
		ret = mxs_dma_desc_append(s->dma_rx_chan, pdesc);
		if (ret)
			pr_info("%s append dma desc, %d\n", __func__, ret);
	}
	ret = mxs_dma_enable(s->dma_rx_chan);
	if (ret)
		pr_info("%s enable dma desc, %d\n", __func__, ret);
}

static irqreturn_t mxs_auart_irq_dma_rx(int irq, void *context)
{
	struct mxs_auart_port *s = context;

	mxs_dma_ack_irq(s->dma_rx_chan);
	mxs_auart_rx_chars(s);
	return IRQ_HANDLED;
}

static void mxs_auart_submit_tx(struct mxs_auart_port *s, int size)
{
	int ret;
	struct mxs_dma_desc *d = s->tx;

	d->cmd.pio_words[0] = BF_UARTAPP_CTRL1_XFER_COUNT(size);
	d->cmd.cmd.bits.bytes = size;
	d->cmd.cmd.bits.pio_words = 1;
	d->cmd.cmd.bits.wait4end = 1;
	d->cmd.cmd.bits.dec_sem = 1;
	d->cmd.cmd.bits.irq = 1;
	d->cmd.cmd.bits.command = DMA_READ;
	ret = mxs_dma_desc_append(s->dma_tx_chan, s->tx);
	if (ret)
		pr_info("append dma desc, %d\n", ret);

	ret = mxs_dma_enable(s->dma_tx_chan);
	if (ret)
		pr_info("enable dma desc, %d\n", ret);
}

static irqreturn_t mxs_auart_irq_dma_tx(int irq, void *context)
{
	struct mxs_auart_port *s = context;

	LIST_HEAD(list);
	mxs_dma_ack_irq(s->dma_tx_chan);
	mxs_dma_cooked(s->dma_tx_chan, &list);
	mxs_auart_tx_chars(s);
	return IRQ_HANDLED;
}

static int mxs_auart_request_port(struct uart_port *u)
{
	struct mxs_auart_port *s = to_auart_port(u);

	if (!request_mem_region((u32)u->mapbase, SZ_4K, dev_name(s->dev)))
		return -EBUSY;
	return 0;

}

static int mxs_auart_verify_port(struct uart_port *u,
				    struct serial_struct *ser)
{
	if (u->type != PORT_UNKNOWN && u->type != PORT_IMX)
		return -EINVAL;
	return 0;
}

static void mxs_auart_config_port(struct uart_port *u, int flags)
{
}

static const char *mxs_auart_type(struct uart_port *u)
{
	struct mxs_auart_port *s = to_auart_port(u);

	return dev_name(s->dev);
}

static void mxs_auart_release_port(struct uart_port *u)
{
	release_mem_region(u->mapbase, SZ_4K);
}

static void mxs_auart_set_mctrl(struct uart_port *u, unsigned mctrl)
{
	struct mxs_auart_port *s = to_auart_port(u);

	u32 ctrl = __raw_readl(u->membase + HW_UARTAPP_CTRL2);

	ctrl &= ~BM_UARTAPP_CTRL2_RTS;
	if (mctrl & TIOCM_RTS)
		ctrl |= BM_UARTAPP_CTRL2_RTS;
	s->ctrl = mctrl;
	__raw_writel(ctrl, u->membase + HW_UARTAPP_CTRL2);
}

static u32 mxs_auart_get_mctrl(struct uart_port *u)
{
	struct mxs_auart_port *s = to_auart_port(u);
	u32 stat = __raw_readl(u->membase + HW_UARTAPP_STAT);
	int ctrl2 = __raw_readl(u->membase + HW_UARTAPP_CTRL2);
	u32 mctrl = s->ctrl;

	mctrl &= ~TIOCM_CTS;
	if (stat & BM_UARTAPP_STAT_CTS)
		mctrl |= TIOCM_CTS;

	if (ctrl2 & BM_UARTAPP_CTRL2_RTS)
		mctrl |= TIOCM_RTS;

	return mctrl;
}

static void mxs_auart_settermios(struct uart_port *u,
				 struct ktermios *termios,
				 struct ktermios *old)
{
	u32 bm, ctrl, ctrl2, div;
	unsigned int cflag, baud;

	if (termios == NULL) {
		printk(KERN_ERR "Empty ktermios setting:!\n");
		return;
	}

	cflag = termios->c_cflag;

	ctrl = BM_UARTAPP_LINECTRL_FEN;
	ctrl2 = __raw_readl(u->membase + HW_UARTAPP_CTRL2);

	/* byte size */
	switch (cflag & CSIZE) {
	case CS5:
		bm = 0;
		break;
	case CS6:
		bm = 1;
		break;
	case CS7:
		bm = 2;
		break;
	case CS8:
		bm = 3;
		break;
	default:
		return;
	}

	ctrl |= BF_UARTAPP_LINECTRL_WLEN(bm);

	/* parity */
	if (cflag & PARENB) {
		ctrl |= BM_UARTAPP_LINECTRL_PEN;
		if ((cflag & PARODD) == 0)
			ctrl |= BM_UARTAPP_LINECTRL_EPS;
	}

	/* figure out the stop bits requested */
	if (cflag & CSTOPB)
		ctrl |= BM_UARTAPP_LINECTRL_STP2;

	/* figure out the hardware flow control settings */
	if (cflag & CRTSCTS)
		ctrl2 |= BM_UARTAPP_CTRL2_CTSEN /* | BM_UARTAPP_CTRL2_RTSEN */ ;
	else
		ctrl2 &= ~BM_UARTAPP_CTRL2_CTSEN;

	/* set baud rate */
	baud = uart_get_baud_rate(u, termios, old, 0, u->uartclk);
	div = u->uartclk * 32 / baud;
	ctrl |= BF_UARTAPP_LINECTRL_BAUD_DIVFRAC(div & 0x3F);
	ctrl |= BF_UARTAPP_LINECTRL_BAUD_DIVINT(div >> 6);

	if ((cflag & CREAD) != 0)
		ctrl2 |= BM_UARTAPP_CTRL2_RXE;

	__raw_writel(ctrl, u->membase + HW_UARTAPP_LINECTRL);
	__raw_writel(ctrl2, u->membase + HW_UARTAPP_CTRL2);
}

static irqreturn_t mxs_auart_irq_handle(int irq, void *context)
{
	u32 istatus, istat;
	struct mxs_auart_port *s = context;
	u32 stat = __raw_readl(s->port.membase + HW_UARTAPP_STAT);

	istatus = istat = __raw_readl(s->port.membase + HW_UARTAPP_INTR);

	if (istat & BM_UARTAPP_INTR_CTSMIS) {
		uart_handle_cts_change(&s->port, stat & BM_UARTAPP_STAT_CTS);
		__raw_writel(BM_UARTAPP_INTR_CTSMIS,
				s->port.membase + HW_UARTAPP_INTR_CLR);
		istat &= ~BM_UARTAPP_INTR_CTSMIS;
	}
	if (istat & (BM_UARTAPP_INTR_RTIS | BM_UARTAPP_INTR_RXIS)) {
		mxs_auart_rx_chars(s);
		istat &= ~(BM_UARTAPP_INTR_RTIS | BM_UARTAPP_INTR_RXIS);
	}

	if (istat & BM_UARTAPP_INTR_TXIS) {
		mxs_auart_tx_chars(s);
		istat &= ~BM_UARTAPP_INTR_TXIS;
	}
	/* modem status interrupt bits are undefined
	after reset,and the hardware do not support
	DSRMIS,DCDMIS and RIMIS bit,so we should ingore
	them when they are pending. */
	if (istat & (BM_UARTAPP_INTR_ABDIS
		| BM_UARTAPP_INTR_OEIS
		| BM_UARTAPP_INTR_BEIS
		| BM_UARTAPP_INTR_PEIS
		| BM_UARTAPP_INTR_FEIS
		| BM_UARTAPP_INTR_RTIS
		| BM_UARTAPP_INTR_TXIS
		| BM_UARTAPP_INTR_RXIS
		| BM_UARTAPP_INTR_CTSMIS)) {
		dev_info(s->dev, "Unhandled status %x\n", istat);
	}
	__raw_writel(istatus & (BM_UARTAPP_INTR_ABDIS
		| BM_UARTAPP_INTR_OEIS
		| BM_UARTAPP_INTR_BEIS
		| BM_UARTAPP_INTR_PEIS
		| BM_UARTAPP_INTR_FEIS
		| BM_UARTAPP_INTR_RTIS
		| BM_UARTAPP_INTR_TXIS
		| BM_UARTAPP_INTR_RXIS
		| BM_UARTAPP_INTR_DSRMIS
		| BM_UARTAPP_INTR_DCDMIS
		| BM_UARTAPP_INTR_CTSMIS
		| BM_UARTAPP_INTR_RIMIS),
			s->port.membase + HW_UARTAPP_INTR_CLR);

	return IRQ_HANDLED;
}

static int mxs_auart_free_irqs(struct mxs_auart_port *s)
{
	int irqn = 0;

	for (irqn = 0; irqn < ARRAY_SIZE(s->irq); irqn++)
		free_irq(s->irq[irqn], s);
	return 0;
}

static int mxs_auart_request_irqs(struct mxs_auart_port *s)
{
	int err = 0;

	/*
	 * order counts. resources should be listed in the same order
	 */
	irq_handler_t handlers[] = {
		mxs_auart_irq_handle,
		mxs_auart_irq_dma_rx,
		mxs_auart_irq_dma_tx,
	};
	char *handlers_names[] = {
		"auart internal",
		"auart dma rx",
		"auart dma tx",
	};
	int irqn;

	for (irqn = 0; irqn < ARRAY_SIZE(handlers); irqn++) {
		err = request_irq(s->irq[irqn], handlers[irqn],
				  0, handlers_names[irqn], s);
		if (err)
			goto out;
	}
	return 0;
out:
	mxs_auart_free_irqs(s);
	return err;
}

static inline void mxs_auart_reset(struct uart_port *u)
{
	int i;
	unsigned int reg;

	__raw_writel(BM_UARTAPP_CTRL0_SFTRST,
		     u->membase + HW_UARTAPP_CTRL0_CLR);

	for (i = 0; i < 10000; i++) {
		reg = __raw_readl(u->membase + HW_UARTAPP_CTRL0);
		if (!(reg & BM_UARTAPP_CTRL0_SFTRST))
			break;
		udelay(3);
	}

	__raw_writel(BM_UARTAPP_CTRL0_CLKGATE,
		     u->membase + HW_UARTAPP_CTRL0_CLR);
}

static int mxs_auart_startup(struct uart_port *u)
{
	struct mxs_auart_port *s = to_auart_port(u);

	mxs_auart_reset(u);

	__raw_writel(BM_UARTAPP_CTRL2_UARTEN,
		     s->port.membase + HW_UARTAPP_CTRL2_SET);

	/* Enable the Application UART DMA bits. */
	if (s->flags & MXS_AUART_PORT_DMA_MODE) {
		int ret;
		ret = mxs_auart_dma_init(s);
		if (ret) {
			__raw_writel(BM_UARTAPP_CTRL2_UARTEN,
				     s->port.membase + HW_UARTAPP_CTRL2_CLR);
			return ret;
		}
		__raw_writel(BM_UARTAPP_CTRL2_TXDMAE | BM_UARTAPP_CTRL2_RXDMAE
			      | BM_UARTAPP_CTRL2_DMAONERR,
			     s->port.membase + HW_UARTAPP_CTRL2_SET);
		/* clear any pending interrupts */
		__raw_writel(0, s->port.membase + HW_UARTAPP_INTR);

		/* reset all dma channels */
		mxs_dma_reset(s->dma_tx_chan);
		mxs_dma_reset(s->dma_rx_chan);
	} else
		__raw_writel(BM_UARTAPP_INTR_RXIEN | BM_UARTAPP_INTR_RTIEN,
			     s->port.membase + HW_UARTAPP_INTR);

	__raw_writel(BM_UARTAPP_INTR_CTSMIEN,
		     s->port.membase + HW_UARTAPP_INTR_SET);

	/*
	 * Enable fifo so all four bytes of a DMA word are written to
	 * output (otherwise, only the LSB is written, ie. 1 in 4 bytes)
	 */
	__raw_writel(BM_UARTAPP_LINECTRL_FEN,
		     s->port.membase + HW_UARTAPP_LINECTRL_SET);

	if (s->flags & MXS_AUART_PORT_DMA_MODE)
		mxs_auart_submit_rx(s);
	return mxs_auart_request_irqs(s);
}

static void mxs_auart_shutdown(struct uart_port *u)
{
	struct mxs_auart_port *s = to_auart_port(u);

	__raw_writel(BM_UARTAPP_CTRL0_SFTRST,
		     s->port.membase + HW_UARTAPP_CTRL0_SET);

	if (s->flags & MXS_AUART_PORT_DMA_MODE)
		mxs_auart_dma_exit(s);
	else
		__raw_writel(BM_UARTAPP_INTR_RXIEN | BM_UARTAPP_INTR_RTIEN |
				BM_UARTAPP_INTR_CTSMIEN,
			     s->port.membase + HW_UARTAPP_INTR_CLR);
	mxs_auart_free_irqs(s);
}

static unsigned int mxs_auart_tx_empty(struct uart_port *u)
{
	struct mxs_auart_port *s = to_auart_port(u);

	if (s->flags & MXS_AUART_PORT_DMA_MODE)
		return mxs_dma_desc_pending(s->tx) ? 0 : TIOCSER_TEMT;

	if (__raw_readl(u->membase + HW_UARTAPP_STAT) &
	    BM_UARTAPP_STAT_TXFE)
		return TIOCSER_TEMT;
	else
		return 0;
}

static void mxs_auart_start_tx(struct uart_port *u)
{
	struct mxs_auart_port *s = to_auart_port(u);

	/* enable transmitter */
	__raw_writel(BM_UARTAPP_CTRL2_TXE, u->membase + HW_UARTAPP_CTRL2_SET);

	mxs_auart_tx_chars(s);
}

static void mxs_auart_stop_tx(struct uart_port *u)
{
	__raw_writel(BM_UARTAPP_CTRL2_TXE, u->membase + HW_UARTAPP_CTRL2_CLR);
}

static void mxs_auart_stop_rx(struct uart_port *u)
{
	__raw_writel(BM_UARTAPP_CTRL2_RXE, u->membase + HW_UARTAPP_CTRL2_CLR);
}

static void mxs_auart_break_ctl(struct uart_port *u, int ctl)
{
	if (ctl)
		__raw_writel(BM_UARTAPP_LINECTRL_BRK,
			     u->membase + HW_UARTAPP_LINECTRL_SET);
	else
		__raw_writel(BM_UARTAPP_LINECTRL_BRK,
			     u->membase + HW_UARTAPP_LINECTRL_CLR);
}

static void mxs_auart_enable_ms(struct uart_port *port)
{
	/* just empty */
}

static struct uart_ops mxs_auart_ops = {
	.tx_empty       = mxs_auart_tx_empty,
	.start_tx       = mxs_auart_start_tx,
	.stop_tx	= mxs_auart_stop_tx,
	.stop_rx	= mxs_auart_stop_rx,
	.enable_ms      = mxs_auart_enable_ms,
	.break_ctl      = mxs_auart_break_ctl,
	.set_mctrl	= mxs_auart_set_mctrl,
	.get_mctrl      = mxs_auart_get_mctrl,
	.startup	= mxs_auart_startup,
	.shutdown       = mxs_auart_shutdown,
	.set_termios    = mxs_auart_settermios,
	.type	   	= mxs_auart_type,
	.release_port   = mxs_auart_release_port,
	.request_port   = mxs_auart_request_port,
	.config_port    = mxs_auart_config_port,
	.verify_port    = mxs_auart_verify_port,
};

static struct uart_driver auart_driver = {
	.owner = THIS_MODULE,
	.driver_name = "auart",
	.dev_name = "ttySP",
	.major = MXS_AUART_MAJOR,
	.minor = 0,
	.nr = CONFIG_MXS_AUART_PORTS,
};

static int __devinit mxs_auart_probe(struct platform_device *pdev)
{
	struct mxs_auart_plat_data *plat;
	struct mxs_auart_port *s;
	u32 version;
	int i, ret = 0;
	struct resource *r;

	s = kzalloc(sizeof(struct mxs_auart_port), GFP_KERNEL);
	if (!s) {
		ret = -ENOMEM;
		goto out;
	}

	plat = pdev->dev.platform_data;
	if (plat == NULL) {
		ret = -ENOMEM;
		goto out_free;
	}

	if (plat && plat->clk)
		s->clk = clk_get(NULL, plat->clk);
	else
		s->clk = clk_get(NULL, "uart");
	if (IS_ERR(s->clk)) {
		ret = PTR_ERR(s->clk);
		goto out_free;
	}

	clk_enable(s->clk);

	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!r) {
		ret = -ENXIO;
		goto out_free_clk;
	}
	s->port.mapbase = r->start;
	s->port.membase = (void __iomem *)IO_ADDRESS(r->start);
	s->port.ops = &mxs_auart_ops;
	s->port.iotype = UPIO_MEM;
	s->port.line = pdev->id < 0 ? 0 : pdev->id;
	s->port.fifosize = plat->fifo_size;
	s->port.timeout = plat->timeout ? plat->timeout : (HZ / 10);
	s->port.uartclk = clk_get_rate(s->clk);
	s->port.type = PORT_IMX;
	s->port.dev = s->dev = get_device(&pdev->dev);

	s->flags = plat->dma_mode ? MXS_AUART_PORT_DMA_MODE : 0;
	s->ctrl = 0;
	s->dma_rx_buffer_size = plat->dma_rx_buffer_size;

	for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
		s->irq[i] = platform_get_irq(pdev, i);
		if (s->irq[i] < 0) {
			ret = s->irq[i];
			goto out_free_clk;
		}
	}
	s->port.irq = s->irq[0];

	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
	if (!r) {
		ret = -ENXIO;
		goto out_free_clk;
	}
	s->dma_rx_chan = r->start;

	r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
	if (!r) {
		ret = -ENXIO;
		goto out_free_clk;
	}
	s->dma_tx_chan = r->start;

	platform_set_drvdata(pdev, s);

	device_init_wakeup(&pdev->dev, 1);

	ret = uart_add_one_port(&auart_driver, &s->port);
	if (ret)
		goto out_free_clk;

	version = __raw_readl(s->port.membase + HW_UARTAPP_VERSION);
	printk(KERN_INFO "Found APPUART %d.%d.%d\n",
	       (version >> 24) & 0xFF,
	       (version >> 16) & 0xFF, version & 0xFFFF);
	return 0;

out_free_clk:
	if (!IS_ERR(s->clk))
		clk_put(s->clk);
out_free:
	kfree(s);
out:
	return ret;
}

static int __devexit mxs_auart_remove(struct platform_device *pdev)
{
	struct mxs_auart_port *s;

	s = platform_get_drvdata(pdev);
	if (s) {
		put_device(s->dev);
		clk_disable(s->clk);
		clk_put(s->clk);
		uart_remove_one_port(&auart_driver, &s->port);
		kfree(s);
	}
	return 0;
}

static struct platform_driver mxs_auart_driver = {
	.probe = mxs_auart_probe,
	.remove = __devexit_p(mxs_auart_remove),
	.driver = {
		.name = "mxs-auart",
		.owner = THIS_MODULE,
	},
};

static int __init mxs_auart_init(void)
{
	int r;

	r = uart_register_driver(&auart_driver);
	if (r)
		goto out;
	r = platform_driver_register(&mxs_auart_driver);
	if (r)
		goto out_err;
	return 0;
out_err:
	uart_unregister_driver(&auart_driver);
out:
	return r;
}

static void __exit mxs_auart_exit(void)
{
	platform_driver_unregister(&mxs_auart_driver);
	uart_unregister_driver(&auart_driver);
}

module_init(mxs_auart_init)
module_exit(mxs_auart_exit)
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Freescale MXS application uart driver");