summaryrefslogtreecommitdiff
path: root/drivers/serial/mxc_uart.c
blob: 347a7462c396d6d55fae0b4de34585cc517a618e (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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
/*
 * Copyright 2004-2010 Freescale Semiconductor, 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
 */

/*!
 * @file drivers/serial/mxc_uart.c
 *
 * @brief Driver for the Freescale Semiconductor MXC serial ports based on
 * drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
 *
 * @ingroup UART
 */

/*
 * Include Files
 */
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/serial.h>
#include <linux/console.h>
#include <linux/platform_device.h>
#include <linux/sysrq.h>
#include <linux/dma-mapping.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <asm/irq.h>
#include <asm/dma.h>
#include <asm/div64.h>
#include <mach/mxc_uart.h>

#if defined(CONFIG_SERIAL_MXC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif
#define SERIAL_MXC_MAJOR        207
#define SERIAL_MXC_MINOR        16
#define MXC_ISR_PASS_LIMIT      256
#define UART_CREAD_BIT          256

#define MXC_UART_NR		8

/* IRDA minimum pulse duration in micro seconds */
#define MIN_PULSE_DUR           2
/*
 * Transmit DMA buffer size is set to 1024 bytes, this is limited
 * by UART_XMIT_SIZE.
 */
#define TXDMA_BUFF_SIZE         UART_XMIT_SIZE
/*
 * Receive DMA sub-buffer size
 */
#define RXDMA_BUFF_SIZE         128

/*!
 * This structure is used to store the information for DMA data transfer.
 */
typedef struct {
	/*!
	 * Holds the read channel number.
	 */
	int rd_channel;
	/*!
	 * Holds the write channel number.
	 */
	int wr_channel;
	/*!
	 * UART Transmit Event ID
	 */
	int tx_event_id;
	/*!
	 * UART Receive Event ID
	 */
	int rx_event_id;
	/*!
	 * DMA Transmit tasklet
	 */
	struct tasklet_struct dma_tx_tasklet;
	/*!
	 * Flag indicates if the channel is in use
	 */
	int dma_txchnl_inuse;
} dma_info;

/*!
 * This is used to indicate if we want echo cancellation in the Irda mode.
 */
static int echo_cancel;
extern void gpio_uart_active(int port, int no_irda);
extern void gpio_uart_inactive(int port, int no_irda);
extern void config_uartdma_event(int port);

static uart_mxc_port *mxc_ports[MXC_UART_NR];

/*!
 * This array holds the DMA channel information for each MXC UART
 */
static dma_info dma_list[MXC_UART_NR];

/*!
 * This function is called by the core driver to stop UART transmission.
 * This might be due to the TTY layer indicating that the user wants to stop
 * transmission.
 *
 * @param   port       the port structure for the UART passed in by the core
 *                     driver
 */
static void mxcuart_stop_tx(struct uart_port *port)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;
	volatile unsigned int cr1;

	cr1 = readl(port->membase + MXC_UARTUCR1);
	/* Disable Transmitter rdy interrupt */
	if (umxc->dma_enabled == 1) {
		cr1 &= ~MXC_UARTUCR1_TXDMAEN;
	} else {
		cr1 &= ~MXC_UARTUCR1_TRDYEN;
	}
	writel(cr1, port->membase + MXC_UARTUCR1);
}

/*!
 * DMA Transmit tasklet method is scheduled on completion of a DMA transmit
 * to send out any more data that is available in the UART xmit buffer.
 *
 * @param   arg  driver private data
 */
static void dma_tx_do_tasklet(unsigned long arg)
{
	uart_mxc_port *umxc = (uart_mxc_port *) arg;
	struct circ_buf *xmit = &umxc->port.state->xmit;
	mxc_dma_requestbuf_t writechnl_request;
	int tx_num;
	unsigned long flags;

	spin_lock_irqsave(&umxc->port.lock, flags);
	tx_num = uart_circ_chars_pending(xmit);
	if (tx_num > 0) {
		if (xmit->tail > xmit->head) {
			memcpy(umxc->tx_buf, xmit->buf + xmit->tail,
			       UART_XMIT_SIZE - xmit->tail);
			memcpy(umxc->tx_buf + (UART_XMIT_SIZE - xmit->tail),
			       xmit->buf, xmit->head);
		} else {
			memcpy(umxc->tx_buf, xmit->buf + xmit->tail, tx_num);
		}
		umxc->tx_handle = dma_map_single(umxc->port.dev, umxc->tx_buf,
						 TXDMA_BUFF_SIZE,
						 DMA_TO_DEVICE);

		writechnl_request.dst_addr = umxc->port.mapbase + MXC_UARTUTXD;
		writechnl_request.src_addr = umxc->tx_handle;
		writechnl_request.num_of_bytes = tx_num;

		if ((mxc_dma_config(dma_list[umxc->port.line].wr_channel,
				    &writechnl_request, 1,
				    MXC_DMA_MODE_WRITE)) == 0) {
			mxc_dma_enable(dma_list[umxc->port.line].wr_channel);
		}
	} else {
		/* No more data available in the xmit queue, clear the flag */
		dma_list[umxc->port.line].dma_txchnl_inuse = 0;
	}
	spin_unlock_irqrestore(&umxc->port.lock, flags);
}

/*!
 * DMA Write callback is called by the SDMA controller after it has sent out all
 * the data from the user buffer. This function updates the xmit buffer pointers.
 *
 * @param   arg   driver private data
 * @param   error any DMA error
 * @param   count amount of data that was transferred
 */
static void mxcuart_dma_writecallback(void *arg, int error, unsigned int count)
{
	uart_mxc_port *umxc = arg;
	struct circ_buf *xmit = &umxc->port.state->xmit;
	int tx_num;

	if (error != MXC_DMA_TRANSFER_ERROR) {
		tx_num = count;
		umxc->port.icount.tx += tx_num;
		xmit->tail = (xmit->tail + tx_num) & (UART_XMIT_SIZE - 1);
	}

	dma_unmap_single(umxc->port.dev, umxc->tx_handle, TXDMA_BUFF_SIZE,
			 DMA_TO_DEVICE);
	tx_num = uart_circ_chars_pending(xmit);
	/* Schedule a tasklet to send out the pending characters */
	if (tx_num > 0) {
		tasklet_schedule(&dma_list[umxc->port.line].dma_tx_tasklet);
	} else {
		dma_list[umxc->port.line].dma_txchnl_inuse = 0;
	}
	if (tx_num < WAKEUP_CHARS) {
		uart_write_wakeup(&umxc->port);
	}
}

/*!
 * This function is called by the core driver to start transmitting characters.
 * This function enables the transmit interrupts.
 *
 * @param   port       the port structure for the UART passed in by the core
 *                     driver
 */
static void mxcuart_start_tx(struct uart_port *port)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;
	struct circ_buf *xmit = &umxc->port.state->xmit;
	volatile unsigned int cr1;
	mxc_dma_requestbuf_t writechnl_request;
	int tx_num;

	cr1 = readl(port->membase + MXC_UARTUCR1);
	/* Enable Transmitter rdy interrupt */
	if (umxc->dma_enabled == 1) {
		/*
		 * If the channel is in use then return immediately and use
		 * the dma_tx tasklet to transfer queued data when current DMA
		 * transfer is complete
		 */
		if (dma_list[umxc->port.line].dma_txchnl_inuse == 1) {
			return;
		}
		tx_num = uart_circ_chars_pending(xmit);
		if (tx_num > 0) {
			dma_list[umxc->port.line].dma_txchnl_inuse = 1;
			if (xmit->tail > xmit->head) {
				memcpy(umxc->tx_buf, xmit->buf + xmit->tail,
				       UART_XMIT_SIZE - xmit->tail);
				memcpy(umxc->tx_buf +
				       (UART_XMIT_SIZE - xmit->tail), xmit->buf,
				       xmit->head);
			} else {
				memcpy(umxc->tx_buf, xmit->buf + xmit->tail,
				       tx_num);
			}
			umxc->tx_handle =
			    dma_map_single(umxc->port.dev, umxc->tx_buf,
					   TXDMA_BUFF_SIZE, DMA_TO_DEVICE);

			writechnl_request.dst_addr =
			    umxc->port.mapbase + MXC_UARTUTXD;
			writechnl_request.src_addr = umxc->tx_handle;
			writechnl_request.num_of_bytes = tx_num;
			if ((mxc_dma_config
			     (dma_list[umxc->port.line].wr_channel,
			      &writechnl_request, 1,
			      MXC_DMA_MODE_WRITE)) == 0) {
				mxc_dma_enable(dma_list[umxc->port.line].
					       wr_channel);
			}
			cr1 |= MXC_UARTUCR1_TXDMAEN;
		}
	} else {
		cr1 |= MXC_UARTUCR1_TRDYEN;
	}
	writel(cr1, port->membase + MXC_UARTUCR1);
}

/*!
 * This function is called by the core driver to stop receiving characters; the
 * port is in the process of being closed.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 */
static void mxcuart_stop_rx(struct uart_port *port)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;
	volatile unsigned int cr1;

	cr1 = readl(port->membase + MXC_UARTUCR1);
	if (umxc->dma_enabled == 1) {
		cr1 &= ~MXC_UARTUCR1_RXDMAEN;
	} else {
		cr1 &= ~MXC_UARTUCR1_RRDYEN;
	}
	writel(cr1, port->membase + MXC_UARTUCR1);
}

/*!
 * This function is called by the core driver to enable the modem status
 * interrupts. If the port is configured to be in DTE mode then it enables the
 * DCDDELT and RIDELT interrupts in addition to the DTRDEN interrupt. The RTSDEN
 * interrupt is enabled only for interrupt-driven hardware flow control.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 */
static void mxcuart_enable_ms(struct uart_port *port)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;
	volatile unsigned int cr1, cr3;

	/*
	 * RTS interrupt is enabled only if we are using interrupt-driven
	 * software controlled hardware flow control
	 */
	if (umxc->hardware_flow == 0) {
		cr1 = readl(umxc->port.membase + MXC_UARTUCR1);
		cr1 |= MXC_UARTUCR1_RTSDEN;
		writel(cr1, umxc->port.membase + MXC_UARTUCR1);
	}
	cr3 = readl(umxc->port.membase + MXC_UARTUCR3);
	cr3 |= MXC_UARTUCR3_DTRDEN;
	if (umxc->mode == MODE_DTE) {
		cr3 |= MXC_UARTUCR3_DCD | MXC_UARTUCR3_RI;
	}
	writel(cr3, umxc->port.membase + MXC_UARTUCR3);
}

/*!
 * This function is called from the interrupt service routine if the status bit
 * indicates that the receive fifo data level is above the set threshold. The
 * function reads the character and queues them into the TTY layers read
 * buffer. The function also looks for break characters, parity and framing
 * errors in the received character and sets the appropriate flag in the TTY
 * receive buffer.
 *
 * @param   umxc   the MXC UART port structure, this includes the \b uart_port
 *                 structure and other members that are specific to MXC UARTs
 */
static void mxcuart_rx_chars(uart_mxc_port *umxc)
{
	volatile unsigned int ch, sr2;
	unsigned int status, flag, max_count = 256;

	sr2 = readl(umxc->port.membase + MXC_UARTUSR2);
	while (((sr2 & MXC_UARTUSR2_RDR) == 1) && (max_count-- > 0)) {
		ch = readl(umxc->port.membase + MXC_UARTURXD);

		flag = TTY_NORMAL;
		status = ch | UART_CREAD_BIT;
		ch &= 0xFF;	/* Clear the upper bits */
		umxc->port.icount.rx++;

		/*
		 * Check to see if there is an  error in the received
		 * character. Perform the appropriate actions based on the
		 * error bit that was set.
		 */
		if (status & MXC_UARTURXD_ERR) {
			if (status & MXC_UARTURXD_BRK) {
				/*
				 * Clear the frame and parity error bits
				 * as these always get set on receiving a
				 * break character
				 */
				status &= ~(MXC_UARTURXD_FRMERR |
					    MXC_UARTURXD_PRERR);
				umxc->port.icount.brk++;
				if (uart_handle_break(&umxc->port)) {
					goto ignore_char;
				}
			} else if (status & MXC_UARTURXD_FRMERR) {
				umxc->port.icount.frame++;
			} else if (status & MXC_UARTURXD_PRERR) {
				umxc->port.icount.parity++;
			}
			if (status & MXC_UARTURXD_OVRRUN) {
				umxc->port.icount.overrun++;
			}

			status &= umxc->port.read_status_mask;

			if (status & MXC_UARTURXD_BRK) {
				flag = TTY_BREAK;
			} else if (status & MXC_UARTURXD_FRMERR) {
				flag = TTY_FRAME;
			} else if (status & MXC_UARTURXD_PRERR) {
				flag = TTY_PARITY;
			}
		}

		if (uart_handle_sysrq_char(&umxc->port, ch)) {
			goto ignore_char;
		}

		uart_insert_char(&umxc->port, status, MXC_UARTURXD_OVRRUN, ch,
				 flag);
	      ignore_char:
		sr2 = readl(umxc->port.membase + MXC_UARTUSR2);
	}
	tty_flip_buffer_push(umxc->port.state->port.tty);
}

/*!
 * This function is called from the interrupt service routine if the status bit
 * indicates that the transmit fifo is emptied below its set threshold and
 * requires data. The function pulls characters from the TTY layers write
 * buffer and writes it out to the UART transmit fifo.
 *
 * @param   umxc   the MXC UART port structure, this includes the \b uart_port
 *                 structure and other members that are specific to MXC UARTs
 */
static void mxcuart_tx_chars(uart_mxc_port *umxc)
{
	struct circ_buf *xmit = &umxc->port.state->xmit;
	int count;

	/*
	 * Transmit the XON/XOFF character if required
	 */
	if (umxc->port.x_char) {
		writel(umxc->port.x_char, umxc->port.membase + MXC_UARTUTXD);
		umxc->port.icount.tx++;
		umxc->port.x_char = 0;
		return;
	}

	/*
	 * Check to see if there is any data to be sent and that the
	 * port has not been currently stopped by anything.
	 */
	if (uart_circ_empty(xmit) || uart_tx_stopped(&umxc->port)) {
		mxcuart_stop_tx(&umxc->port);
		return;
	}

	count = umxc->port.fifosize - umxc->tx_threshold;
	do {
		writel(xmit->buf[xmit->tail],
		       umxc->port.membase + MXC_UARTUTXD);
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		umxc->port.icount.tx++;
		if (uart_circ_empty(xmit)) {
			break;
		}
	} while (--count > 0);

	/*
	 * Check to see if we have flushed enough characters to ask for more
	 * to be sent to us, if so, we notify the user space that we can
	 * accept more data
	 */
	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) {
		uart_write_wakeup(&umxc->port);
	}

	if (uart_circ_empty(xmit)) {
		mxcuart_stop_tx(&umxc->port);
	}
}

/*!
 * This function is called from the interrupt service routine if there is a
 * change in the modem signals. This function handles these signal changes and
 * also clears the appropriate status register bits.
 *
 * @param   umxc   the MXC UART port structure, this includes the \b uart_port
 *                 structure and other members that are specific to MXC UARTs
 * @param   sr1    contents of status register 1
 * @param   sr2    contents of status register 2
 */
static void mxcuart_modem_status(uart_mxc_port *umxc, unsigned int sr1,
				 unsigned int sr2)
{
	if (umxc->mode == MODE_DTE) {
		if (sr2 & MXC_UARTUSR2_DCDDELT) {
			uart_handle_dcd_change(&umxc->port,
					       !(sr2 & MXC_UARTUSR2_DCDIN));
		}
		if (sr2 & MXC_UARTUSR2_RIDELT) {
			umxc->port.icount.rng++;
		}
	}
	if (sr1 & MXC_UARTUSR1_DTRD) {
		umxc->port.icount.dsr++;
	}
	if ((umxc->hardware_flow == 0) && (sr1 & MXC_UARTUSR1_RTSD)) {
		uart_handle_cts_change(&umxc->port, sr1 & MXC_UARTUSR1_RTSS);
	}

	wake_up_interruptible(&umxc->port.state->port.delta_msr_wait);
}

/*!
 * Interrupt service routine registered to handle the muxed ANDed interrupts.
 * This routine is registered only in the case where the UART interrupts are
 * muxed.
 *
 * @param   irq    the interrupt number
 * @param   dev_id driver private data
 *
 * @return  The function returns \b IRQ_RETVAL(1) if interrupt was handled,
 *          returns \b IRQ_RETVAL(0) if the interrupt was not handled.
 *          \b IRQ_RETVAL is defined in \b include/linux/interrupt.h.
 */
static irqreturn_t mxcuart_int(int irq, void *dev_id)
{
	uart_mxc_port *umxc = dev_id;
	volatile unsigned int sr1, sr2, cr1, cr;
	unsigned int pass_counter = MXC_ISR_PASS_LIMIT;
	unsigned int term_cond = 0;
	int handled = 0;

	sr1 = readl(umxc->port.membase + MXC_UARTUSR1);
	sr2 = readl(umxc->port.membase + MXC_UARTUSR2);
	cr1 = readl(umxc->port.membase + MXC_UARTUCR1);

	do {
		/* Clear the bits that triggered the interrupt */
		writel(sr1, umxc->port.membase + MXC_UARTUSR1);
		writel(sr2, umxc->port.membase + MXC_UARTUSR2);
		/*
		 * Read if there is data available
		 */
		if (sr2 & MXC_UARTUSR2_RDR) {
			mxcuart_rx_chars(umxc);
		}

		if ((sr1 & (MXC_UARTUSR1_RTSD | MXC_UARTUSR1_DTRD)) ||
		    (sr2 & (MXC_UARTUSR2_DCDDELT | MXC_UARTUSR2_RIDELT))) {
			mxcuart_modem_status(umxc, sr1, sr2);
		}

		/*
		 * Send data if there is data to be sent
		 */
		if ((cr1 & MXC_UARTUCR1_TRDYEN) && (sr1 & MXC_UARTUSR1_TRDY)) {
			/* Echo cancellation for IRDA Transmit chars */
			if (umxc->ir_mode == IRDA && echo_cancel) {
				/* Disable the receiver */
				cr = readl(umxc->port.membase + MXC_UARTUCR2);
				cr &= ~MXC_UARTUCR2_RXEN;
				writel(cr, umxc->port.membase + MXC_UARTUCR2);
				/* Enable Transmit complete intr to reenable RX */
				cr = readl(umxc->port.membase + MXC_UARTUCR4);
				cr |= MXC_UARTUCR4_TCEN;
				writel(cr, umxc->port.membase + MXC_UARTUCR4);
			}
			mxcuart_tx_chars(umxc);
		}

		if (pass_counter-- == 0) {
			break;
		}

		sr1 = readl(umxc->port.membase + MXC_UARTUSR1);
		sr2 = readl(umxc->port.membase + MXC_UARTUSR2);

		/* Is the transmit complete to reenable the receiver? */
		if (umxc->ir_mode == IRDA && echo_cancel) {
			if (sr2 & MXC_UARTUSR2_TXDC) {
				cr = readl(umxc->port.membase + MXC_UARTUCR2);
				cr |= MXC_UARTUCR2_RXEN;
				writel(cr, umxc->port.membase + MXC_UARTUCR2);
				/* Disable the Transmit complete interrupt bit */
				cr = readl(umxc->port.membase + MXC_UARTUCR4);
				cr &= ~MXC_UARTUCR4_TCEN;
				writel(cr, umxc->port.membase + MXC_UARTUCR4);
			}
		}

		/*
		 * If there is no data to send or receive and if there is no
		 * change in the modem status signals then quit the routine
		 */
		term_cond = sr1 & (MXC_UARTUSR1_RTSD | MXC_UARTUSR1_DTRD);
		term_cond |= sr2 & (MXC_UARTUSR2_RDR | MXC_UARTUSR2_DCDDELT);
		term_cond |= !(sr2 & MXC_UARTUSR2_TXFE);
	} while (term_cond > 0);

	handled = 1;
	return IRQ_RETVAL(handled);
}

/*!
 * Interrupt service routine registered to handle the transmit interrupts. This
 * routine is registered only in the case where the UART interrupts are not
 * muxed.
 *
 * @param   irq    the interrupt number
 * @param   dev_id driver private data
 *
 * @return  The function returns \b IRQ_RETVAL(1) if interrupt was handled,
 *          returns \b IRQ_RETVAL(0) if the interrupt was not handled.
 *          \b IRQ_RETVAL is defined in include/linux/interrupt.h.
 */
static irqreturn_t mxcuart_tx_int(int irq, void *dev_id)
{
	uart_mxc_port *umxc = dev_id;
	int handled = 0;
	volatile unsigned int sr2, cr;

	/* Echo cancellation for IRDA Transmit chars */
	if (umxc->ir_mode == IRDA && echo_cancel) {
		/* Disable the receiver */
		cr = readl(umxc->port.membase + MXC_UARTUCR2);
		cr &= ~MXC_UARTUCR2_RXEN;
		writel(cr, umxc->port.membase + MXC_UARTUCR2);
		/* Enable Transmit complete to reenable receiver */
		cr = readl(umxc->port.membase + MXC_UARTUCR4);
		cr |= MXC_UARTUCR4_TCEN;
		writel(cr, umxc->port.membase + MXC_UARTUCR4);
	}

	mxcuart_tx_chars(umxc);

	/* Is the transmit complete to reenable the receiver? */
	if (umxc->ir_mode == IRDA && echo_cancel) {
		sr2 = readl(umxc->port.membase + MXC_UARTUSR2);
		if (sr2 & MXC_UARTUSR2_TXDC) {
			cr = readl(umxc->port.membase + MXC_UARTUCR2);
			cr |= MXC_UARTUCR2_RXEN;
			writel(cr, umxc->port.membase + MXC_UARTUCR2);
			/* Disable the Transmit complete interrupt bit */
			cr = readl(umxc->port.membase + MXC_UARTUCR4);
			cr &= ~MXC_UARTUCR4_TCEN;
			writel(cr, umxc->port.membase + MXC_UARTUCR4);
		}
	}

	handled = 1;

	return IRQ_RETVAL(handled);
}

/*!
 * Interrupt service routine registered to handle the receive interrupts. This
 * routine is registered only in the case where the UART interrupts are not
 * muxed.
 *
 * @param   irq    the interrupt number
 * @param   dev_id driver private data
 *
 * @return  The function returns \b IRQ_RETVAL(1) if interrupt was handled,
 *          returns \b IRQ_RETVAL(0) if the interrupt was not handled.
 *          \b IRQ_RETVAL is defined in include/linux/interrupt.h.
 */
static irqreturn_t mxcuart_rx_int(int irq, void *dev_id)
{
	uart_mxc_port *umxc = dev_id;
	int handled = 0;

	/* Clear the aging timer bit */
	writel(MXC_UARTUSR1_AGTIM, umxc->port.membase + MXC_UARTUSR1);
	mxcuart_rx_chars(umxc);
	handled = 1;

	return IRQ_RETVAL(handled);
}

/*!
 * Interrupt service routine registered to handle the master interrupts. This
 * routine is registered only in the case where the UART interrupts are not
 * muxed.
 *
 * @param   irq    the interrupt number
 * @param   dev_id driver private data
 *
 * @return  The function returns \b IRQ_RETVAL(1) if interrupt was handled,
 *          returns \b IRQ_RETVAL(0) if the interrupt was not handled.
 *          \b IRQ_RETVAL is defined in include/linux/interrupt.h.
 */
static irqreturn_t mxcuart_mint_int(int irq, void *dev_id)
{
	uart_mxc_port *umxc = dev_id;
	int handled = 0;
	volatile unsigned int sr1, sr2;

	sr1 = readl(umxc->port.membase + MXC_UARTUSR1);
	sr2 = readl(umxc->port.membase + MXC_UARTUSR2);
	/* Clear the modem status interrupt bits */
	writel(MXC_UARTUSR1_RTSD | MXC_UARTUSR1_DTRD,
	       umxc->port.membase + MXC_UARTUSR1);
	writel(MXC_UARTUSR2_DCDDELT | MXC_UARTUSR2_RIDELT,
	       umxc->port.membase + MXC_UARTUSR2);
	mxcuart_modem_status(umxc, sr1, sr2);
	handled = 1;

	return IRQ_RETVAL(handled);
}

/*!
 * This function is called by the core driver to test whether the transmitter
 * fifo and shift register for the UART port are empty.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 *
 * @return  The function returns TIOCSER_TEMT if it is empty, else returns 0.
 */
static unsigned int mxcuart_tx_empty(struct uart_port *port)
{
	volatile unsigned int sr2;
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
	sr2 = readl(port->membase + MXC_UARTUSR2);
	spin_unlock_irqrestore(&port->lock, flags);

	return sr2 & MXC_UARTUSR2_TXDC ? TIOCSER_TEMT : 0;
}

/*!
 * This function is called by the core driver to get the current status of the
 * modem input signals. The state of the output signals is not collected.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 *
 * @return  The function returns an integer that contains the ORed value of the
 *          status of all the modem input signals or error.
 */
static unsigned int mxcuart_get_mctrl(struct uart_port *port)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;
	unsigned int result = 0;
	volatile unsigned int sr1, sr2;

	sr1 = readl(umxc->port.membase + MXC_UARTUSR1);
	sr2 = readl(umxc->port.membase + MXC_UARTUSR2);

	if (sr1 & MXC_UARTUSR1_RTSS) {
		result |= TIOCM_CTS;
	}
	if (umxc->mode == MODE_DTE) {
		if (!(sr2 & MXC_UARTUSR2_DCDIN)) {
			result |= TIOCM_CAR;
		}
		if (!(sr2 & MXC_UARTUSR2_RIIN)) {
			result |= TIOCM_RI;
		}
	}
	return result;
}

/*!
 * This function is called by the core driver to set the state of the modem
 * control lines.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 * @param   mctrl  the state that the modem control lines should be changed to
 */
static void mxcuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;
	volatile unsigned int cr2 = 0, cr3 = 0, uts = 0;

	cr2 = readl(port->membase + MXC_UARTUCR2);
	cr3 = readl(port->membase + MXC_UARTUCR3);
	uts = readl(port->membase + MXC_UARTUTS);

	if (mctrl & TIOCM_RTS) {
		/*
		 * Return to hardware-driven hardware flow control if the
		 * option is enabled
		 */
		if (umxc->hardware_flow == 1) {
			cr2 |= MXC_UARTUCR2_CTSC;
		} else {
			cr2 |= MXC_UARTUCR2_CTS;
			cr2 &= ~MXC_UARTUCR2_CTSC;
		}
	} else {
		cr2 &= ~(MXC_UARTUCR2_CTS | MXC_UARTUCR2_CTSC);
	}
	writel(cr2, port->membase + MXC_UARTUCR2);

	if (mctrl & TIOCM_DTR) {
		cr3 |= MXC_UARTUCR3_DSR;
	} else {
		cr3 &= ~MXC_UARTUCR3_DSR;
	}
	writel(cr3, port->membase + MXC_UARTUCR3);

	if (mctrl & TIOCM_LOOP) {
		if (umxc->ir_mode == IRDA) {
			echo_cancel = 0;
		} else {
			uts |= MXC_UARTUTS_LOOP;
		}
	} else {
		if (umxc->ir_mode == IRDA) {
			echo_cancel = 1;
		} else {
			uts &= ~MXC_UARTUTS_LOOP;
		}
	}
	writel(uts, port->membase + MXC_UARTUTS);
}

/*!
 * This function is called by the core driver to control the transmission of
 * the break signal. If break_state is non-zero, the break signal is
 * transmitted, the signal is terminated when another call is made with
 * break_state set to 0.
 *
 * @param   port          the port structure for the UART passed in by the core
 *                        driver
 * @param   break_state   the requested state of the break signal
 */
static void mxcuart_break_ctl(struct uart_port *port, int break_state)
{
	volatile unsigned int cr1;
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
	cr1 = readl(port->membase + MXC_UARTUCR1);
	if (break_state == -1) {
		cr1 |= MXC_UARTUCR1_SNDBRK;
	} else {
		cr1 &= ~MXC_UARTUCR1_SNDBRK;
	}
	writel(cr1, port->membase + MXC_UARTUCR1);
	spin_unlock_irqrestore(&port->lock, flags);
}

/*!
 * The read DMA callback, this method is called when the DMA buffer has received its
 * data. This functions copies the data to the tty buffer and updates the tty buffer
 * pointers. It also queues the DMA buffer back to the DMA system.
 *
 * @param   arg   driver private data
 * @param   error any DMA error
 * @param   cnt   amount of data that was transferred
 */
static void mxcuart_dmaread_callback(void *arg, int error, unsigned int cnt)
{
	uart_mxc_port *umxc = arg;
	struct tty_struct *tty = umxc->port.state->port.tty;
	int buff_id, flip_cnt, num_bufs;
	mxc_dma_requestbuf_t readchnl_request;
	mxc_uart_rxdmamap *rx_buf_elem = NULL;
	unsigned int sr1, sr2;
	char flag;

	num_bufs = umxc->dma_rxbuf_size / RXDMA_BUFF_SIZE;
	/* Clear the aging timer bit */
	writel(MXC_UARTUSR1_AGTIM, umxc->port.membase + MXC_UARTUSR1);

	buff_id = umxc->dma_rxbuf_id;
	flag = TTY_NORMAL;

	umxc->dma_rxbuf_id += 1;
	if (umxc->dma_rxbuf_id >= num_bufs) {
		umxc->dma_rxbuf_id = 0;
	}

	rx_buf_elem = (mxc_uart_rxdmamap *) (umxc->rx_dmamap + buff_id);

	if (error == MXC_DMA_TRANSFER_ERROR) {

		sr1 = __raw_readl(umxc->port.membase + MXC_UARTUSR1);
		sr2 = __raw_readl(umxc->port.membase + MXC_UARTUSR2);

		if (sr2 & MXC_UARTUSR2_BRCD) {
			umxc->port.icount.brk++;
			if (uart_handle_break(&umxc->port)) {
				goto drop_data;
			}
		} else if (sr1 & MXC_UARTUSR1_PARITYERR) {
			umxc->port.icount.parity++;
		} else if (sr1 & MXC_UARTUSR1_FRAMERR) {
			umxc->port.icount.frame++;
		} else if (sr2 & MXC_UARTUSR2_ORE) {
			umxc->port.icount.overrun++;

		}

		if (umxc->port.read_status_mask & MXC_UARTURXD_BRK) {
			if (sr2 & MXC_UARTUSR2_BRCD)
				flag = TTY_BREAK;
		} else if (umxc->port.read_status_mask & MXC_UARTURXD_PRERR) {
			if (sr1 & MXC_UARTUSR1_PARITYERR)
				flag = TTY_PARITY;
		} else if (umxc->port.read_status_mask & MXC_UARTURXD_FRMERR) {
			if (sr1 & MXC_UARTUSR1_FRAMERR)
				flag = TTY_FRAME;
		} else if (umxc->port.read_status_mask & MXC_UARTURXD_OVRRUN) {
			if (sr2 & MXC_UARTUSR2_ORE)
				flag = TTY_OVERRUN;
		}
/* By default clearing all error bits in status reg */
		__raw_writel((MXC_UARTUSR2_BRCD | MXC_UARTUSR2_ORE),
			     umxc->port.membase + MXC_UARTUSR2);
		__raw_writel((MXC_UARTUSR1_PARITYERR | MXC_UARTUSR1_FRAMERR),
			     umxc->port.membase + MXC_UARTUSR1);
	}

	flip_cnt = tty_buffer_request_room(tty, cnt);

	/* Check for space availability in the TTY Flip buffer */
	if (flip_cnt <= 0) {
		goto drop_data;
	}
	umxc->port.icount.rx += flip_cnt;

	tty_insert_flip_string(tty, rx_buf_elem->rx_buf, flip_cnt);

	if (flag != TTY_NORMAL) {
		tty_insert_flip_char(tty, 0, flag);
	}

	tty_flip_buffer_push(tty);
	umxc->port.state->port.tty->real_raw = 1;

      drop_data:
	readchnl_request.src_addr = umxc->port.mapbase;
	readchnl_request.dst_addr = rx_buf_elem->rx_handle;
	readchnl_request.num_of_bytes = RXDMA_BUFF_SIZE;
	mxc_dma_config(dma_list[umxc->port.line].rd_channel, &readchnl_request,
		       1, MXC_DMA_MODE_READ);
	mxc_dma_enable(dma_list[umxc->port.line].rd_channel);
}

/*!
 * Allocates DMA read and write channels, creates DMA read and write buffers and
 * sets the channel specific parameters.
 *
 * @param   d_info the structure that holds all the DMA information for a
 *                 particular MXC UART
 * @param   umxc   the MXC UART port structure, this includes the \b uart_port
 *                 structure and other members that are specific to MXC UARTs
 *
 * @return  The function returns 0 on success and a non-zero value on failure.
 */
static int mxcuart_initdma(dma_info *d_info, uart_mxc_port *umxc)
{
	int ret = 0, rxbufs, i, j;
	mxc_dma_requestbuf_t *readchnl_reqelem;
	mxc_uart_rxdmamap *rx_buf_elem;

	/* Request for the read and write channels */
	d_info->rd_channel = mxc_dma_request(umxc->dma_rx_id, "MXC UART Read");
	if (d_info->rd_channel < 0) {
		printk(KERN_ERR "MXC UART: Cannot allocate DMA read channel\n");
		return -1;
	} else {
		d_info->wr_channel =
		    mxc_dma_request(umxc->dma_tx_id, "MXC UART Write");
		if (d_info->wr_channel < 0) {
			mxc_dma_free(d_info->rd_channel);
			printk(KERN_ERR
			       "MXC UART: Cannot allocate DMA write channel\n");
			return -1;
		}
	}

	/* Allocate the DMA Transmit Buffer */
	umxc->tx_buf = kmalloc(TXDMA_BUFF_SIZE, GFP_KERNEL);
	if (umxc->tx_buf == NULL) {
		ret = -1;
		goto err_dma_tx_buff;
	}
	rxbufs = umxc->dma_rxbuf_size / RXDMA_BUFF_SIZE;
	/* Allocate the DMA Virtual Receive Buffer */
	umxc->rx_dmamap = kmalloc(rxbufs * sizeof(mxc_uart_rxdmamap), GFP_KERNEL);
	if (umxc->rx_dmamap == NULL) {
		ret = -1;
		goto err_dma_rx_buff;
	}

	/* Allocate the DMA Receive Request structures */
	readchnl_reqelem = kmalloc(rxbufs * sizeof(mxc_dma_requestbuf_t),
		     GFP_KERNEL);
	if (readchnl_reqelem == NULL) {
		ret = -1;
		goto err_request;
	}

	for (i = 0; i < rxbufs; i++) {
		rx_buf_elem = (mxc_uart_rxdmamap *) (umxc->rx_dmamap + i);
		rx_buf_elem->rx_buf =
		    dma_alloc_coherent(NULL, RXDMA_BUFF_SIZE,
				       &rx_buf_elem->rx_handle, GFP_DMA);
		if (rx_buf_elem->rx_buf == NULL) {
			for (j = 0; j < i; j++) {
				rx_buf_elem =
				    (mxc_uart_rxdmamap *) (umxc->rx_dmamap + j);
				dma_free_coherent(NULL, RXDMA_BUFF_SIZE,
						  rx_buf_elem->rx_buf,
						  rx_buf_elem->rx_handle);
			}
			ret = -1;
			goto cleanup;
		}
	}

	umxc->dma_rxbuf_id = 0;
	/* Setup the DMA read request structures */
	for (i = 0; i < rxbufs; i++) {
		rx_buf_elem = (mxc_uart_rxdmamap *) (umxc->rx_dmamap + i);
		(readchnl_reqelem + i)->src_addr = umxc->port.mapbase;
		(readchnl_reqelem + i)->dst_addr = rx_buf_elem->rx_handle;
		(readchnl_reqelem + i)->num_of_bytes = RXDMA_BUFF_SIZE;
	}
	mxc_dma_config(d_info->rd_channel, readchnl_reqelem, rxbufs,
		       MXC_DMA_MODE_READ);
	mxc_dma_callback_set(d_info->rd_channel, mxcuart_dmaread_callback,
			     umxc);
	mxc_dma_callback_set(d_info->wr_channel, mxcuart_dma_writecallback,
			     umxc);

	/* Start the read channel */
	mxc_dma_enable(d_info->rd_channel);
	kfree(readchnl_reqelem);
	tasklet_init(&d_info->dma_tx_tasklet, dma_tx_do_tasklet,
		     (unsigned long)umxc);
	d_info->dma_txchnl_inuse = 0;
	return ret;
      cleanup:
	kfree(readchnl_reqelem);
      err_request:
	kfree(umxc->rx_dmamap);
      err_dma_rx_buff:
	kfree(umxc->tx_buf);
      err_dma_tx_buff:
	mxc_dma_free(d_info->rd_channel);
	mxc_dma_free(d_info->wr_channel);

	return ret;
}

/*!
 * Stops DMA and frees the DMA resources
 *
 * @param   d_info the structure that holds all the DMA information for a
 *                 particular MXC UART
 * @param   umxc   the MXC UART port structure, this includes the \b uart_port
 *                 structure and other members that are specific to MXC UARTs
 */
static void mxcuart_freedma(dma_info *d_info, uart_mxc_port *umxc)
{
	int i, rxbufs;
	mxc_uart_rxdmamap *rx_buf_elem;

	rxbufs = umxc->dma_rxbuf_size / RXDMA_BUFF_SIZE;

	for (i = 0; i < rxbufs; i++) {
		rx_buf_elem = (mxc_uart_rxdmamap *) (umxc->rx_dmamap + i);
		dma_free_coherent(NULL, RXDMA_BUFF_SIZE,
				  rx_buf_elem->rx_buf, rx_buf_elem->rx_handle);
	}
	kfree(umxc->rx_dmamap);
	kfree(umxc->tx_buf);
	mxc_dma_free(d_info->rd_channel);
	mxc_dma_free(d_info->wr_channel);
}

/*!
 * This function is called to free the interrupts.
 *
 * @param   umxc   the MXC UART port structure, this includes the \b uart_port
 *                 structure and other members that are specific to MXC UARTs
 */
static void mxcuart_free_interrupts(uart_mxc_port *umxc)
{
	free_irq(umxc->port.irq, umxc);
	if (umxc->ints_muxed == 0) {
		free_irq(umxc->irqs[0], umxc);
		free_irq(umxc->irqs[1], umxc);
	}
}

/*!
 * Calculate and set the UART port clock value
 *
 * @param   umxc     the MXC UART port structure, this includes the \b uart_port
 *                   structure and other members that are specific to MXC UARTs
 * @param   per_clk  peripheral clock coming into the MXC UART module
 * @param   req_baud current baudrate requested
 * @param   div      returns the reference frequency divider value
 */
static void mxcuart_set_ref_freq(uart_mxc_port *umxc, unsigned long per_clk,
				 unsigned int req_baud, int *div)
{
	unsigned int d = 1;

	/*
	 * Choose the smallest possible prescaler to maximize
	 * the chance of using integer scaling.  Ensure that
	 * the calculation won't overflow.  Limit the denom
	 * to 15 bits since a 16-bit denom doesn't work.
	 */
	if (req_baud < (1 << (31 - (4 + 15))))
		d = per_clk / (req_baud << (4 + 15)) + 1;

	umxc->port.uartclk = per_clk / d;

	/*
	 * Set the ONEMS register that is used by IR special case bit and
	 * the Escape character detect logic
	 */
	writel(umxc->port.uartclk / 1000, umxc->port.membase + MXC_UARTONEMS);
	*div = d;
}

/*!
 * This function is called by the core driver to initialize the low-level
 * driver. The function grabs the interrupt resources and registers its
 * interrupt service routines. It then initializes the IOMUX registers to
 * configure the pins for UART signals and finally initializes the various
 * UART registers and enables the port for reception.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 *
 * @return  The function returns 0 on success and a non-zero value on failure.
 */
static int mxcuart_startup(struct uart_port *port)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;
	int retval;
	volatile unsigned int cr, cr1 = 0, cr2 = 0, ufcr = 0;

	/*
	 * Some UARTs need separate registrations for the interrupts as
	 * they do not take the muxed interrupt output to the ARM core
	 */
	if (umxc->ints_muxed == 1) {
		retval = request_irq(umxc->port.irq, mxcuart_int, 0,
				     "mxcintuart", umxc);
		if (retval != 0) {
			return retval;
		}
	} else {
		retval = request_irq(umxc->port.irq, mxcuart_tx_int,
				     0, "mxcintuart", umxc);
		if (retval != 0) {
			return retval;
		} else {
			retval = request_irq(umxc->irqs[0], mxcuart_rx_int,
					     0, "mxcintuart", umxc);
			if (retval != 0) {
				free_irq(umxc->port.irq, umxc);
				return retval;
			} else {
				retval =
				    request_irq(umxc->irqs[1], mxcuart_mint_int,
						0, "mxcintuart", umxc);
				if (retval != 0) {
					free_irq(umxc->port.irq, umxc);
					free_irq(umxc->irqs[0], umxc);
					return retval;
				}
			}
		}
	}

	/* Initialize the DMA if we need SDMA data transfer */
	if (umxc->dma_enabled == 1) {
		retval = mxcuart_initdma(dma_list + umxc->port.line, umxc);
		if (retval != 0) {
			printk
			    (KERN_ERR
			     "MXC UART: Failed to initialize DMA for UART %d\n",
			     umxc->port.line);
			mxcuart_free_interrupts(umxc);
			return retval;
		}
		/* Configure the GPR register to receive SDMA events */
		config_uartdma_event(umxc->port.line);
	}

	/*
	 * Clear Status Registers 1 and 2
	 */
	writel(0xFFFF, umxc->port.membase + MXC_UARTUSR1);
	writel(0xFFFF, umxc->port.membase + MXC_UARTUSR2);

	/* Configure the IOMUX for the UART */
	gpio_uart_active(umxc->port.line, umxc->ir_mode);

	/*
	 * Set the transceiver invert bits if required
	 */
	if (umxc->ir_mode == IRDA) {
		echo_cancel = 1;
		writel(umxc->ir_rx_inv | MXC_UARTUCR4_IRSC, umxc->port.membase
		       + MXC_UARTUCR4);
		writel(umxc->rxd_mux | umxc->ir_tx_inv,
		       umxc->port.membase + MXC_UARTUCR3);
	} else {
		writel(umxc->rxd_mux, umxc->port.membase + MXC_UARTUCR3);
	}

	/*
	 * Initialize UCR1,2 and UFCR registers
	 */
	if (umxc->dma_enabled == 1) {
		cr2 = (MXC_UARTUCR2_TXEN | MXC_UARTUCR2_RXEN);
	} else {
		cr2 =
		    (MXC_UARTUCR2_ATEN | MXC_UARTUCR2_TXEN | MXC_UARTUCR2_RXEN);
	}

	writel(cr2, umxc->port.membase + MXC_UARTUCR2);
	/* Wait till we are out of software reset */
	do {
		cr = readl(umxc->port.membase + MXC_UARTUCR2);
	} while (!(cr & MXC_UARTUCR2_SRST));

	if (umxc->mode == MODE_DTE) {
		ufcr |= ((umxc->tx_threshold << MXC_UARTUFCR_TXTL_OFFSET) |
			 MXC_UARTUFCR_DCEDTE | MXC_UARTUFCR_RFDIV | umxc->
			 rx_threshold);
	} else {
		ufcr |= ((umxc->tx_threshold << MXC_UARTUFCR_TXTL_OFFSET) |
			 MXC_UARTUFCR_RFDIV | umxc->rx_threshold);
	}
	writel(ufcr, umxc->port.membase + MXC_UARTUFCR);

	/*
	 * Finally enable the UART and the Receive interrupts
	 */
	if (umxc->ir_mode == IRDA) {
		cr1 |= MXC_UARTUCR1_IREN;
	}
	if (umxc->dma_enabled == 1) {
		cr1 |= (MXC_UARTUCR1_RXDMAEN | MXC_UARTUCR1_ATDMAEN |
			MXC_UARTUCR1_UARTEN);
	} else {
		cr1 |= (MXC_UARTUCR1_RRDYEN | MXC_UARTUCR1_UARTEN);
	}
	writel(cr1, umxc->port.membase + MXC_UARTUCR1);

	return 0;
}

/*!
 * This function is called by the core driver for the low-level driver to free
 * its resources. The function frees all its interrupts and disables the UART.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 */
static void mxcuart_shutdown(struct uart_port *port)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;

	/* Disable the IOMUX for the UART */
	gpio_uart_inactive(umxc->port.line, umxc->ir_mode);
	mxcuart_free_interrupts(umxc);
	/* Disable all interrupts, port and break condition */
	writel(0, umxc->port.membase + MXC_UARTUCR1);
	writel(0, umxc->port.membase + MXC_UARTUCR3);
	if (umxc->dma_enabled == 1) {
		mxcuart_freedma(dma_list + umxc->port.line, umxc);
	}
}

/*!
 * This function is called by the core driver to change the UART parameters,
 * including baudrate, word length, parity, stop bits. The function also updates
 * the port structures mask registers to indicate the types of events the user is
 * interested in receiving.
 *
 * @param   port    the port structure for the UART passed in by the core driver
 * @param   termios the desired termios settings
 * @param   old     old termios
 */
static void mxcuart_set_termios(struct uart_port *port,
				struct ktermios *termios, struct ktermios *old)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;
	volatile unsigned int cr4 = 0, cr2 = 0, ufcr;
	u_int num, denom, baud;
	u_int cr2_mask;		/* Used to add the changes to CR2 */
	unsigned long flags, per_clk;
	int div;

	cr2_mask = ~(MXC_UARTUCR2_IRTS | MXC_UARTUCR2_CTSC | MXC_UARTUCR2_PREN |
		     MXC_UARTUCR2_PROE | MXC_UARTUCR2_STPB | MXC_UARTUCR2_WS);

	per_clk = clk_get_rate(umxc->clk);

	/*
	 * Ask the core to get the baudrate, if requested baudrate is not
	 * between max and min, then either use the baudrate in old termios
	 * setting. If it's still invalid, we try 9600 baud.
	 */
	baud = uart_get_baud_rate(&umxc->port, termios, old, 0, per_clk / 16);
	/* Set the Reference frequency divider */
	mxcuart_set_ref_freq(umxc, per_clk, baud, &div);

	/* Byte size, default is 8-bit mode */
	switch (termios->c_cflag & CSIZE) {
	case CS7:
		cr2 = 0;
		break;
	default:
		cr2 = MXC_UARTUCR2_WS;
		break;
	}
	/* Check to see if we need 2 Stop bits */
	if (termios->c_cflag & CSTOPB) {
		cr2 |= MXC_UARTUCR2_STPB;
	}

	/* Check to see if we need Parity checking */
	if (termios->c_cflag & PARENB) {
		cr2 |= MXC_UARTUCR2_PREN;
		if (termios->c_cflag & PARODD) {
			cr2 |= MXC_UARTUCR2_PROE;
		}
	}
	spin_lock_irqsave(&umxc->port.lock, flags);

	ufcr = readl(umxc->port.membase + MXC_UARTUFCR);
	ufcr = (ufcr & (~MXC_UARTUFCR_RFDIV_MASK)) |
	    ((6 - div) << MXC_UARTUFCR_RFDIV_OFFSET);
	writel(ufcr, umxc->port.membase + MXC_UARTUFCR);

	/*
	 * Update the per-port timeout
	 */
	uart_update_timeout(&umxc->port, termios->c_cflag, baud);

	umxc->port.read_status_mask = MXC_UARTURXD_OVRRUN;
	/*
	 * Enable appropriate events to be passed to the TTY layer
	 */
	if (termios->c_iflag & INPCK) {
		umxc->port.read_status_mask |= MXC_UARTURXD_FRMERR |
		    MXC_UARTURXD_PRERR;
	}
	if (termios->c_iflag & (BRKINT | PARMRK)) {
		umxc->port.read_status_mask |= MXC_UARTURXD_BRK;
	}

	/*
	 * Characters to ignore
	 */
	umxc->port.ignore_status_mask = 0;
	if (termios->c_iflag & IGNPAR) {
		umxc->port.ignore_status_mask |= MXC_UARTURXD_FRMERR |
		    MXC_UARTURXD_PRERR;
	}
	if (termios->c_iflag & IGNBRK) {
		umxc->port.ignore_status_mask |= MXC_UARTURXD_BRK;
		/*
		 * If we are ignoring parity and break indicators,
		 * ignore overruns too (for real raw support)
		 */
		if (termios->c_iflag & IGNPAR) {
			umxc->port.ignore_status_mask |= MXC_UARTURXD_OVRRUN;
		}
	}

	/*
	 * Ignore all characters if CREAD is not set, still receive characters
	 * from the port, but throw them away.
	 */
	if ((termios->c_cflag & CREAD) == 0) {
		umxc->port.ignore_status_mask |= UART_CREAD_BIT;
	}

	/* Hardware flow control should controled by userspace */
	umxc->hardware_flow = (termios->c_cflag & CRTSCTS) ? 1 : 0;

	cr4 = readl(umxc->port.membase + MXC_UARTUCR4);
	if (UART_ENABLE_MS(port, termios->c_cflag)) {
		mxcuart_enable_ms(port);
		if (umxc->hardware_flow == 1) {
			cr4 = (cr4 & (~MXC_UARTUCR4_CTSTL_MASK)) |
			    (umxc->cts_threshold << MXC_UARTUCR4_CTSTL_OFFSET);
			cr2 |= MXC_UARTUCR2_CTSC;
			umxc->port.state->port.tty->hw_stopped = 0;
		} else {
			cr2 |= MXC_UARTUCR2_IRTS;
		}
	} else {
		cr2 |= MXC_UARTUCR2_IRTS;
	}

	/* Add Parity, character length and stop bits information */
	cr2 |= (readl(umxc->port.membase + MXC_UARTUCR2) & cr2_mask);
	writel(cr2, umxc->port.membase + MXC_UARTUCR2);
	/*
	   if (umxc->ir_mode == IRDA) {
	   ret = mxcuart_setir_special(baud);
	   if (ret == 0) {
	   cr4 &= ~MXC_UARTUCR4_IRSC;
	   } else {
	   cr4 |= MXC_UARTUCR4_IRSC;
	   }
	   } */
	writel(cr4, umxc->port.membase + MXC_UARTUCR4);

	/*
	 * Set baud rate
	 */

	/* Use integer scaling, if possible. Limit the denom to 15 bits. */
	num = 0;
	denom = (umxc->port.uartclk + 8 * baud) / (16 * baud) - 1;

	/* Use fractional scaling if needed to limit the max error to 0.5% */
	if (denom < 100) {
		u64 n64 = (u64) 16 * 0x8000 * baud + (umxc->port.uartclk / 2);
		do_div(n64, umxc->port.uartclk);
		num = (u_int) n64 - 1;
		denom = 0x7fff;
	}
	writel(num, umxc->port.membase + MXC_UARTUBIR);
	writel(denom, umxc->port.membase + MXC_UARTUBMR);

	spin_unlock_irqrestore(&umxc->port.lock, flags);
}

/*!
 * This function is called by the core driver to know the UART type.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 *
 * @return  The function returns a pointer to a string describing the UART port.
 */
static const char *mxcuart_type(struct uart_port *port)
{
	return port->type == PORT_IMX ? "Freescale i.MX" : NULL;
}

/*!
 * This function is called by the core driver to release the memory resources
 * currently in use by the UART port.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 */
static void mxcuart_release_port(struct uart_port *port)
{
	release_mem_region(port->mapbase, SZ_4K);
}

/*!
 * This function is called by the core driver to request memory resources for
 * the UART port.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 *
 * @return  The function returns \b -EBUSY on failure, else it returns 0.
 */
static int mxcuart_request_port(struct uart_port *port)
{
	struct platform_device *pdev = to_platform_device(port->dev);
	struct resource *mmres;
	void *ret;

	mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mmres)
		return -ENODEV;

	ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
							"serial_mxc");

	return  ret ? 0 : -EBUSY;
}

/*!
 * This function is called by the core driver to perform any autoconfiguration
 * steps required for the UART port. This function sets the port->type field.
 *
 * @param   port   the port structure for the UART passed in by the core driver
 * @param   flags  bit mask of the required configuration
 */
static void mxcuart_config_port(struct uart_port *port, int flags)
{
	if ((flags & UART_CONFIG_TYPE) && (mxcuart_request_port(port) == 0)) {
		port->type = PORT_IMX;
	}
}

/*!
 * This function is called by the core driver to verify that the new serial
 * port information contained within \a ser is suitable for this UART port type.
 * The function checks to see if the UART port type specified by the user
 * application while setting the UART port information matches what is stored
 * in the define \b PORT_MXC found in the header file include/linux/serial_core.h
 *
 * @param   port   the port structure for the UART passed in by the core driver
 * @param   ser    the new serial port information
 *
 * @return  The function returns 0 on success or \b -EINVAL if the port type
 *          specified is not equal to \b PORT_MXC.
 */
static int mxcuart_verify_port(struct uart_port *port,
			       struct serial_struct *ser)
{
	int ret = 0;
	if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX) {
		ret = -EINVAL;
	}
	return ret;
}

/*!
 * This function is used to send a high priority XON/XOFF character
 *
 * @param   port   the port structure for the UART passed in by the core driver
 * @param   ch     the character to send
 */
static void mxcuart_send_xchar(struct uart_port *port, char ch)
{
	unsigned long flags;

	port->x_char = ch;
	if (port->state->port.tty->hw_stopped) {
		return;
	}

	if (ch) {
		spin_lock_irqsave(&port->lock, flags);
		port->ops->start_tx(port);
		spin_unlock_irqrestore(&port->lock, flags);
	}
}

/*!
 * This function is used enable/disable the MXC UART clocks
 *
 * @param   port      the port structure for the UART passed in by the core driver
 * @param   state     New PM state
 * @param   oldstate  Current PM state
 */
static void
mxcuart_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
{
	uart_mxc_port *umxc = (uart_mxc_port *) port;

	if (state)
		clk_disable(umxc->clk);
	else
		clk_enable(umxc->clk);
}

/*!
 * This structure contains the pointers to the control functions that are
 * invoked by the core serial driver to access the UART hardware. The
 * structure is passed to serial_core.c file during registration.
 */
static struct uart_ops mxc_ops = {
	.tx_empty = mxcuart_tx_empty,
	.set_mctrl = mxcuart_set_mctrl,
	.get_mctrl = mxcuart_get_mctrl,
	.stop_tx = mxcuart_stop_tx,
	.start_tx = mxcuart_start_tx,
	.stop_rx = mxcuart_stop_rx,
	.enable_ms = mxcuart_enable_ms,
	.break_ctl = mxcuart_break_ctl,
	.startup = mxcuart_startup,
	.shutdown = mxcuart_shutdown,
	.set_termios = mxcuart_set_termios,
	.type = mxcuart_type,
	.pm = mxcuart_pm,
	.release_port = mxcuart_release_port,
	.request_port = mxcuart_request_port,
	.config_port = mxcuart_config_port,
	.verify_port = mxcuart_verify_port,
	.send_xchar = mxcuart_send_xchar,
};

#ifdef CONFIG_SERIAL_MXC_CONSOLE

/*
 * Write out a character once the UART is ready
 */
static inline void mxcuart_console_write_char(struct uart_port *port, char ch)
{
	volatile unsigned int status;

	do {
		status = readl(port->membase + MXC_UARTUSR1);
	} while ((status & MXC_UARTUSR1_TRDY) == 0);
	writel(ch, port->membase + MXC_UARTUTXD);
}

/*!
 * This function is called to write the console messages through the UART port.
 *
 * @param   co    the console structure
 * @param   s     the log message to be written to the UART
 * @param   count length of the message
 */
static void mxcuart_console_write(struct console *co, const char *s,
				  u_int count)
{
	struct uart_port *port = &mxc_ports[co->index]->port;
	volatile unsigned int status, oldcr1, oldcr2, oldcr3, cr2, cr3;
	int i;

	/*
	 * First save the control registers and then disable the interrupts
	 */
	oldcr1 = readl(port->membase + MXC_UARTUCR1);
	oldcr2 = readl(port->membase + MXC_UARTUCR2);
	oldcr3 = readl(port->membase + MXC_UARTUCR3);
	cr2 =
	    oldcr2 & ~(MXC_UARTUCR2_ATEN | MXC_UARTUCR2_RTSEN |
		       MXC_UARTUCR2_ESCI);
	cr3 =
	    oldcr3 & ~(MXC_UARTUCR3_DCD | MXC_UARTUCR3_RI |
		       MXC_UARTUCR3_DTRDEN);
	writel(MXC_UARTUCR1_UARTEN, port->membase + MXC_UARTUCR1);
	writel(cr2, port->membase + MXC_UARTUCR2);
	writel(cr3, port->membase + MXC_UARTUCR3);
	/*
	 * Do each character
	 */
	for (i = 0; i < count; i++) {
		mxcuart_console_write_char(port, s[i]);
		if (s[i] == '\n') {
			mxcuart_console_write_char(port, '\r');
		}
	}
	/*
	 * Finally, wait for the transmitter to become empty
	 */
	do {
		status = readl(port->membase + MXC_UARTUSR2);
	} while (!(status & MXC_UARTUSR2_TXDC));

	/*
	 * Restore the control registers
	 */
	writel(oldcr1, port->membase + MXC_UARTUCR1);
	writel(oldcr2, port->membase + MXC_UARTUCR2);
	writel(oldcr3, port->membase + MXC_UARTUCR3);
}

/*!
 * Initializes the UART port to be used to print console message with the
 * options specified. If no options are specified, then the function
 * initializes the UART with the default options of baudrate=115200, 8 bit
 * word size, no parity, no flow control.
 *
 * @param   co      The console structure
 * @param   options Any console options passed in from the command line
 *
 * @return  The function returns 0 on success or error.
 */
static int __init mxcuart_console_setup(struct console *co, char *options)
{
	uart_mxc_port *umxc;
	int baud = 115200;
	int bits = 8;
	int parity = 'n';
	int flow = 'n';
	volatile unsigned int cr = 0;

	/*
	 * Check whether an invalid uart number had been specified, and if
	 * so, search for the first available port that does have console
	 * support
	 */
	if (co->index >= MXC_UART_NR) {
		co->index = 0;
	}
	umxc = mxc_ports[co->index];

	if (umxc == NULL) {
		return -ENODEV;
	}

	clk_enable(umxc->clk);

	/* initialize port.lock else oops */
	spin_lock_init(&umxc->port.lock);

	/*
	 * Initialize the UART registers
	 */
	writel(MXC_UARTUCR1_UARTEN, umxc->port.membase + MXC_UARTUCR1);
	/* Enable the transmitter and do a software reset */
	writel(MXC_UARTUCR2_TXEN, umxc->port.membase + MXC_UARTUCR2);
	/* Wait till we are out of software reset */
	do {
		cr = readl(umxc->port.membase + MXC_UARTUCR2);
	} while (!(cr & MXC_UARTUCR2_SRST));

	writel(0x0, umxc->port.membase + MXC_UARTUCR3);
	writel(0x0, umxc->port.membase + MXC_UARTUCR4);
	/* Set TXTL to 2, RXTL to 1 and RFDIV to 2 */
	cr = 0x0800 | MXC_UARTUFCR_RFDIV | 0x1;
	if (umxc->mode == MODE_DTE) {
		cr |= MXC_UARTUFCR_DCEDTE;
	}
	writel(cr, umxc->port.membase + MXC_UARTUFCR);
	writel(0xFFFF, umxc->port.membase + MXC_UARTUSR1);
	writel(0xFFFF, umxc->port.membase + MXC_UARTUSR2);

	if (options != NULL) {
		uart_parse_options(options, &baud, &parity, &bits, &flow);
	}
	gpio_uart_active(umxc->port.line, umxc->ir_mode);
	return uart_set_options(&umxc->port, co, baud, parity, bits, flow);
}

static struct uart_driver mxc_reg;

/*!
 * This structure contains the pointers to the UART console functions. It is
 * passed as an argument when registering the console.
 */
static struct console mxc_console = {
	.name = "ttymxc",
	.write = mxcuart_console_write,
	.device = uart_console_device,
	.setup = mxcuart_console_setup,
	.flags = CON_PRINTBUFFER,
	.index = -1,
	.data = &mxc_reg,
};

/*!
 * This function registers the console callback functions with the kernel.
 */
static int __init mxcuart_console_init(void)
{
	register_console(&mxc_console);
	return 0;
}

console_initcall(mxcuart_console_init);

static int __init find_port(struct uart_port *p)
{
	int line;
	struct uart_port *port;
	for (line = 0; line < MXC_UART_NR; line++) {
		if (!mxc_ports[line])
			continue;
		port = &mxc_ports[line]->port;
		if (uart_match_port(p, port))
			return line;
	}
	return -ENODEV;
}

int __init mxc_uart_start_console(struct uart_port *port, char *options)
{
	int line;
	line = find_port(port);
	if (line < 0)
		return -ENODEV;

	add_preferred_console("ttymxc", line, options);
	printk("Switching Console to ttymxc%d at %s 0x%lx (options '%s')\n",
	       line, port->iotype == UPIO_MEM ? "MMIO" : "I/O port",
	       port->iotype ==
	       UPIO_MEM ? (unsigned long)port->mapbase : (unsigned long)port->
	       iobase, options);

	if (!(mxc_console.flags & CON_ENABLED)) {
		mxc_console.flags &= ~CON_PRINTBUFFER;
		register_console(&mxc_console);
	}
	return 0;
}

#define MXC_CONSOLE     (&mxc_console)
#else
#define MXC_CONSOLE     NULL
#endif				/* CONFIG_SERIAL_MXC_CONSOLE */

/*!
 * This structure contains the information such as the name of the UART driver
 * that appears in the /dev folder, major and minor numbers etc. This structure
 * is passed to the serial_core.c file.
 */
static struct uart_driver mxc_reg = {
	.owner = THIS_MODULE,
	.driver_name = "ttymxc",
	.dev_name = "ttymxc",
	.major = SERIAL_MXC_MAJOR,
	.minor = SERIAL_MXC_MINOR,
	.nr = MXC_UART_NR,
	.cons = MXC_CONSOLE,
};

/*!
 * This function is called to put the UART in a low power state. Refer to the
 * document driver-model/driver.txt in the kernel source tree for more
 * information.
 *
 * @param   pdev  the device structure used to give information on which UART
 *                to suspend
 * @param   state the power state the device is entering
 *
 * @return  The function returns 0 on success and -1 on failure
 */
static int mxcuart_suspend(struct platform_device *pdev, pm_message_t state)
{
	uart_mxc_port *umxc = platform_get_drvdata(pdev);

	if (umxc == NULL)
		return 0;	/* skip disabled ports */

	if (umxc && umxc->port.flags & ASYNC_INITIALIZED)
		uart_suspend_port(&mxc_reg, &umxc->port);

	if (umxc && umxc->port.flags & ASYNC_SUSPENDED)
		umxc->port.state->port.tty->hw_stopped = 1;

	return 0;
}

/*!
 * This function is called to bring the UART back from a low power state. Refer
 * to the document driver-model/driver.txt in the kernel source tree for more
 * information.
 *
 * @param   pdev  the device structure used to give information on which UART
 *                to resume
 *
 * @return  The function returns 0 on success and -1 on failure
 */
static int mxcuart_resume(struct platform_device *pdev)
{
	uart_mxc_port *umxc = platform_get_drvdata(pdev);

	if (umxc == NULL)
		return 0;	/* skip disabled ports */

	if (umxc && umxc->port.flags & ASYNC_SUSPENDED) {
		umxc->port.state->port.tty->hw_stopped = 0;
		uart_resume_port(&mxc_reg, &umxc->port);
	}

	return 0;
}

/*!
 * This function is called during the driver binding process. Based on the UART
 * that is being probed this function adds the appropriate UART port structure
 * in the core driver.
 *
 * @param   pdev  the device structure used to store device specific
 *                information that is used by the suspend, resume and remove
 *                functions
 *
 * @return  The function returns 0 if successful; -1 otherwise.
 */
static int mxcuart_probe(struct platform_device *pdev)
{
	int id = pdev->id;
	struct resource *res;
	void __iomem *base;

	mxc_ports[id] = pdev->dev.platform_data;
	mxc_ports[id]->port.ops = &mxc_ops;

	/* Do not use UARTs that are disabled during integration */
	if (mxc_ports[id]->enabled == 1) {
		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
		if (!res)
			return -ENODEV;

		base = ioremap(res->start, res->end - res->start + 1);
		if (!base)
			return -ENOMEM;

		mxc_ports[id]->port.membase = base;
		mxc_ports[id]->port.mapbase = res->start;
		mxc_ports[id]->port.dev = &pdev->dev;
		mxc_ports[id]->port.irq = platform_get_irq(pdev, 0);
		mxc_ports[id]->irqs[0] = platform_get_irq(pdev, 1);
		mxc_ports[id]->irqs[1] = platform_get_irq(pdev, 2);
		spin_lock_init(&mxc_ports[id]->port.lock);
		/* Enable the low latency flag for DMA UART ports */
		if (mxc_ports[id]->dma_enabled == 1) {
			mxc_ports[id]->port.flags |= ASYNC_LOW_LATENCY;
		}

		mxc_ports[id]->clk = clk_get(&pdev->dev, NULL);
		if (mxc_ports[id]->clk == NULL)
			return -1;

		uart_add_one_port(&mxc_reg, &mxc_ports[id]->port);
		platform_set_drvdata(pdev, mxc_ports[id]);
	}
	return 0;
}

/*!
 * Dissociates the driver from the UART device. Removes the appropriate UART
 * port structure from the core driver.
 *
 * @param   pdev  the device structure used to give information on which UART
 *                to remove
 *
 * @return  The function always returns 0.
 */
static int mxcuart_remove(struct platform_device *pdev)
{
	uart_mxc_port *umxc = platform_get_drvdata(pdev);

	platform_set_drvdata(pdev, NULL);

	if (umxc) {
		uart_remove_one_port(&mxc_reg, &umxc->port);
		iounmap(umxc->port.membase);
	}
	return 0;
}

/*!
 * This structure contains pointers to the power management callback functions.
 */
static struct platform_driver mxcuart_driver = {
	.driver = {
		   .name = "mxcintuart",
		   },
	.probe = mxcuart_probe,
	.remove = mxcuart_remove,
	.suspend = mxcuart_suspend,
	.resume = mxcuart_resume,
};

/*!
 * This function is used to initialize the UART driver module. The function
 * registers the power management callback functions with the kernel and also
 * registers the UART callback functions with the core serial driver.
 *
 * @return  The function returns 0 on success and a non-zero value on failure.
 */
static int __init mxcuart_init(void)
{
	int ret = 0;

	printk(KERN_INFO "Serial: MXC Internal UART driver\n");
	ret = uart_register_driver(&mxc_reg);
	if (ret == 0) {
		/* Register the device driver structure. */
		ret = platform_driver_register(&mxcuart_driver);
		if (ret != 0) {
			uart_unregister_driver(&mxc_reg);
		}
	}
	return ret;
}

/*!
 * This function is used to cleanup all resources before the driver exits.
 */
static void __exit mxcuart_exit(void)
{
	platform_driver_unregister(&mxcuart_driver);
	uart_unregister_driver(&mxc_reg);
}

module_init(mxcuart_init);
module_exit(mxcuart_exit);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("MXC serial port driver");
MODULE_LICENSE("GPL");