aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/uart/mt6735/platform_uart.c
blob: a6670465fa66a5cc5b209600c86e56d20025474e (plain) (blame)
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
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
/* mediatek/platform/mt6589/kernel/drivers/uart/uart.c
 *
 * (C) Copyright 2008
 * MediaTek <www.mediatek.com>
 * MingHsien Hsieh <minghsien.hsieh@mediatek.com>
 *
 * MT6589 UART Driver
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
/*---------------------------------------------------------------------------*/
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial_core.h>
#include <linux/serial.h>
#include <mach/mt_clkmgr.h>
#include "mach/mt_gpio.h"
#include "linux/delay.h"
#include "mach/mt_idle.h"
#include <cust_gpio_usage.h>
#include <linux/uart/mtk_uart.h>
#include <linux/uart/mtk_uart_intf.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>

static void save_tx_raw_data(struct mtk_uart *uart,void *addr);
static void reset_rx_raw_data(struct mtk_uart *uart);
static void save_rx_raw_data(struct mtk_uart *uart, const unsigned char *chars, size_t size);
#ifdef ENABLE_RAW_DATA_DUMP
volatile unsigned int	stop_update = 0;
unsigned int			curr_record=-1;
volatile unsigned int	curr_idx;
#define			RECORD_NUMBER	10
#define			RECORD_LENGTH	1032
unsigned char 			uart_history[RECORD_NUMBER][RECORD_LENGTH];
unsigned int		uart_history_cnt[RECORD_NUMBER];
spinlock_t		tx_history_lock,rx_history_lock;

unsigned int			curr_rx_record=-1;
volatile unsigned int	curr_rx_idx;
unsigned char 			uart_rx_history[RECORD_NUMBER][RECORD_LENGTH];
unsigned int		uart_rx_history_cnt[RECORD_NUMBER];
#endif
/*---------------------------------------------------------------------------*/
static struct mtk_uart_setting mtk_uart_default_settings[] =
{
    {
        //.tx_mode = UART_NON_DMA, .rx_mode = UART_RX_VFIFO_DMA, .dma_mode = UART_DMA_MODE_0,
        .tx_mode = UART_TX_VFIFO_DMA, .rx_mode = UART_RX_VFIFO_DMA, .dma_mode = UART_DMA_MODE_0,
        //.tx_mode = UART_NON_DMA, .rx_mode = UART_NON_DMA, .dma_mode = UART_DMA_MODE_0,
        .tx_trig = UART_FCR_TXFIFO_1B_TRI, .rx_trig = UART_FCR_RXFIFO_12B_TRI,

        //.uart_base = AP_UART0_BASE, .irq_num = UART0_IRQ_BIT_ID, .irq_sen = MT_LEVEL_SENSITIVE,
        .set_bit = PDN_FOR_UART1,  .clr_bit = PDN_FOR_UART1,  .pll_id = PDN_FOR_UART1,
        .sysrq = TRUE, .hw_flow = TRUE, .vff = TRUE,
    },
    {
        .tx_mode = UART_TX_VFIFO_DMA, .rx_mode = UART_RX_VFIFO_DMA, .dma_mode = UART_DMA_MODE_0,
        .tx_trig = UART_FCR_TXFIFO_1B_TRI, .rx_trig = UART_FCR_RXFIFO_12B_TRI,

        //.uart_base = AP_UART1_BASE, .irq_num = UART1_IRQ_BIT_ID, .irq_sen = MT_LEVEL_SENSITIVE,
        .set_bit = PDN_FOR_UART2, .clr_bit = PDN_FOR_UART2,  .pll_id = PDN_FOR_UART2,
        .sysrq = FALSE, .hw_flow = TRUE, .vff = TRUE,
    },
    {
        .tx_mode = UART_TX_VFIFO_DMA, .rx_mode = UART_RX_VFIFO_DMA, .dma_mode = UART_DMA_MODE_0,
        .tx_trig = UART_FCR_TXFIFO_1B_TRI, .rx_trig = UART_FCR_RXFIFO_12B_TRI,

        //.uart_base = AP_UART2_BASE, .irq_num = UART2_IRQ_BIT_ID, .irq_sen = MT_LEVEL_SENSITIVE,
        .set_bit = PDN_FOR_UART3, .clr_bit = PDN_FOR_UART3,  .pll_id = PDN_FOR_UART3,
        .sysrq = FALSE, .hw_flow = FALSE, .vff = TRUE,        /* UART3 */
    },
    {
        .tx_mode = UART_NON_DMA, .rx_mode = UART_NON_DMA, .dma_mode = UART_DMA_MODE_0,
        .tx_trig = UART_FCR_TXFIFO_1B_TRI, .rx_trig = UART_FCR_RXFIFO_12B_TRI,

        //.uart_base = AP_UART3_BASE, .irq_num = UART3_IRQ_BIT_ID, .irq_sen = MT_LEVEL_SENSITIVE,
        .set_bit = PDN_FOR_UART4, .clr_bit = PDN_FOR_UART4,  .pll_id = PDN_FOR_UART4,
        .sysrq = FALSE, .hw_flow = FALSE, .vff = FALSE,        /* UART4 */
    },
#ifndef CONFIG_DENALI_2
    {
        .tx_mode = UART_NON_DMA, .rx_mode = UART_NON_DMA, .dma_mode = UART_DMA_MODE_0,
        .tx_trig = UART_FCR_TXFIFO_1B_TRI, .rx_trig = UART_FCR_RXFIFO_12B_TRI,

        //.uart_base = AP_UART3_BASE, .irq_num = UART3_IRQ_BIT_ID, .irq_sen = MT_LEVEL_SENSITIVE,
        .set_bit = PDN_FOR_UART5, .clr_bit = PDN_FOR_UART5,  .pll_id = PDN_FOR_UART5,
        .sysrq = FALSE, .hw_flow = FALSE, .vff = FALSE,        /* UART5 */
    },
#endif // CONFIG_DENALI_2
};
/*---------------------------------------------------------------------------*/
static unsigned long mtk_uart_evt_mask[] = {
    DBG_EVT_NONE,
    DBG_EVT_NONE,
    DBG_EVT_NONE,
    DBG_EVT_NONE,
#ifndef CONFIG_DENALI_2
    DBG_EVT_NONE,
#endif // CONFIG_DENALI_2
};
/*---------------------------------------------------------------------------*/
static unsigned long mtk_uart_lsr_status[] = {
    0,	/* UART1 */
    0,	/* UART2 */
    0,	/* UART3 */
    0,	/* UART4 */
#ifndef CONFIG_DENALI_2
    0,	/* UART5 */
#endif // CONFIG_DENALI_2
};
/*---------------------------------------------------------------------------*/
#if defined(CONFIG_MTK_SERIAL_MODEM_TEST)
    //#define HW_MISC     (CONFIG_BASE+0x0020)    // mtk does NOT has this register
    //unsigned char mask[UART_NR] = { 1 << 3, 1 << 4, 1 << 5, 1 << 6};
static unsigned int modem_uart[UART_NR] = {1, 0, 0, 1
#ifndef CONFIG_DENALI_2
    , 1
#endif // CONFIG_DENALI_2
};
#endif
/*---------------------------------------------------------------------------*/
/* uart control blocks */
static struct mtk_uart mtk_uarts[UART_NR];
/*---------------------------------------------------------------------------*/
struct mtk_uart_setting* get_uart_default_settings(int idx)
{
    return &mtk_uart_default_settings[idx];
}
/*---------------------------------------------------------------------------*/
void set_uart_default_settings(int idx)
{
    struct device_node *node = NULL;
    unsigned int irq_info[3] = {0, 0, 0};
    u32 phys_base;

    switch (idx) {
    case 0:
 	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_UART0");
 	break;
    case 1:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_UART1");
 	break;
    case 2:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_UART2");
	break;
    case 3:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_UART3");
 	break;
#ifndef CONFIG_DENALI_2
 	case 4:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_UART4");
 	break;
#endif // CONFIG_DENALI_2
    default:
  	break;
    }

    if(node){
	/* iomap registers */
	mtk_uart_default_settings[idx].uart_base = (unsigned long)of_iomap(node, 0);
	/* get IRQ ID */
	mtk_uart_default_settings[idx].irq_num = irq_of_parse_and_map(node, 0);
    }

    /* phys registers */
    if (of_property_read_u32_index(node, "reg", 0, &phys_base)){
	printk("[UART%d] get phys regs from DTS fail!!\n", idx);
    }
    mtk_uart_default_settings[idx].uart_phys_base = phys_base;

    /* get the interrupt line behaviour */
    if (of_property_read_u32_array(node, "interrupts",
			irq_info, ARRAY_SIZE(irq_info))){
	printk("[UART%d] get irq flags from DTS fail!!\n", idx);
    }
    mtk_uart_default_settings[idx].irq_flags = (unsigned long)irq_info[2];
    printk("[UART%d] phys_regs=0x%lx, regs=0x%lx, irq=%d, irq_flags=0x%lx \n", idx, mtk_uart_default_settings[idx].uart_phys_base, mtk_uart_default_settings[idx].uart_base, mtk_uart_default_settings[idx].irq_num, mtk_uart_default_settings[idx].irq_flags);
}
/*---------------------------------------------------------------------------*/
void* get_apdma_uart0_base(void)
{
    struct device_node *node = NULL;
    struct device_node *apdma_uart0_node = NULL;
    void *base;
    unsigned int apdma_reg;
    unsigned int apdma_uart0_reg;
    unsigned int apdma_uart0_offset;

    node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA");
    apdma_uart0_node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART0_TX");

    base = of_iomap(node, 0);

    if (of_property_read_u32_index(node, "reg", 0, &apdma_reg)){
	printk("[UART] get AP_DMA reg from DTS fail!!\n");
    }
    if (of_property_read_u32_index(apdma_uart0_node, "reg", 0, &apdma_uart0_reg)){
	printk("[UART] get AP_DMA_UART0_TX reg from DTS fail!!\n");
    }

    apdma_uart0_offset = apdma_uart0_reg - apdma_reg;
    base += apdma_uart0_offset;
    printk("[UART] apdma uart0 base=0x%p\n", base);

    return base;
}
/*---------------------------------------------------------------------------*/
unsigned int get_uart_vfifo_irq_id(int idx)
{
    struct device_node *node = NULL;
    unsigned int irq_id;

    switch (idx) {
    case 0:
 	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART0_TX");
 	break;
    case 1:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART0_RX");
 	break;
    case 2:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART1_TX");
	break;
    case 3:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART1_RX");
 	break;
    case 4:
 	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART2_TX");
 	break;
    case 5:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART2_RX");
 	break;
    case 6:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART3_TX");
	break;
    case 7:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART3_RX");
 	break;
#ifndef CONFIG_DENALI_2
    case 8:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART4_TX");
	break;
    case 9:
	node = of_find_compatible_node(NULL, NULL, "mediatek,AP_DMA_UART4_RX");
 	break;
#endif // CONFIG_DENALI_2
    default:
  	break;
    }
    irq_id = irq_of_parse_and_map(node, 0);
    printk("[UART_DMA%d] irq=%d\n", idx, irq_id);

    return irq_id;
}
/*---------------------------------------------------------------------------*/
unsigned long get_uart_evt_mask(int idx)
{
    return mtk_uart_evt_mask[idx];
}
/*---------------------------------------------------------------------------*/
void set_uart_evt_mask(int idx, int value)
{
    mtk_uart_evt_mask[idx] = value;
    return;
}
/*---------------------------------------------------------------------------*/
unsigned long get_uart_lsr_status(int idx)
{
    return mtk_uart_lsr_status[idx];
}
/*---------------------------------------------------------------------------*/
void set_uart_lsr_status(int idx, int value)
{
    mtk_uart_lsr_status[idx] = value;
    return;
}
/*---------------------------------------------------------------------------*/
unsigned int get_modem_uart(int idx)
{
    #if defined(CONFIG_MTK_SERIAL_MODEM_TEST)
    return modem_uart[idx];
    #else
    return 0;
    #endif
}
/*---------------------------------------------------------------------------*/
#ifdef UART_FCR_USING_SW_BACK_UP
inline static void __write_fcr_register(struct mtk_uart *uart, u32 data)
{
    unsigned long base = uart->base;
    uart->fcr_back_up = data&(~(3<<1));
    reg_sync_writel(data, UART_FCR);
}

inline static void sync_write_fcr_register(struct mtk_uart *uart, u32 data)
{
    unsigned long base = uart->base;
    uart->fcr_back_up = data&(~(3<<1));
    reg_sync_writel(data, UART_FCR);
}

inline static u32 __read_fcr_register(struct mtk_uart *uart)
{
    return uart->fcr_back_up;
}

inline static void __set_fcr_register(struct mtk_uart *uart, u32 mask)
{
    unsigned long base = uart->base;
    u32 new_setting = (uart->fcr_back_up)|mask;
    uart->fcr_back_up = new_setting&(~(3<<1));
    reg_sync_writel(new_setting, UART_FCR);
}

inline static void __clr_fcr_register(struct mtk_uart *uart, u32 mask)
{
    unsigned long base = uart->base;
    u32 new_setting = (uart->fcr_back_up)&(~mask);
    uart->fcr_back_up = new_setting&(~(3<<1));
    reg_sync_writel(new_setting, UART_FCR);
}
#else
inline static void __write_fcr_register(struct mtk_uart *uart, u32 data)
{
    unsigned long base = uart->base;
    reg_sync_writel(data, UART_FCR);
}

inline static void sync_write_fcr_register(struct mtk_uart *uart, u32 data)
{
    unsigned long base = uart->base;
    reg_sync_writel(data, UART_FCR);
}

inline static u32 __read_fcr_register(struct mtk_uart *uart)
{
    unsigned long base = uart->base;
    return UART_READ32(UART_FCR_RD);
}

inline static void __set_fcr_register(struct mtk_uart *uart, u32 mask)
{
    unsigned long base = uart->base;
    u32 new_setting = UART_READ32(UART_FCR_RD)|mask;
    reg_sync_writel(new_setting, UART_FCR);
}

inline static void __clr_fcr_register(struct mtk_uart *uart, u32 mask)
{
    unsigned long base = uart->base;
    u32 new_setting = UART_READ32(UART_FCR_RD)&(~mask);
    reg_sync_writel(new_setting, UART_FCR);
}
#endif /* End of UART_FCR_USING_SW_BACK_UP */
/*---------------------------------------------------------------------------*/
inline static void dump_reg(struct mtk_uart *uart, const char* caller)
{
#ifdef ENABLE_DEBUG
	unsigned long flags;
    unsigned long base = uart->base;
    u32 lcr = UART_READ32(UART_LCR);
    u32 uratefix = UART_READ32(UART_RATE_FIX_AD);
    u32 uhspeed  = UART_READ32(UART_HIGHSPEED);
    u32 usamplecnt = UART_READ32(UART_SAMPLE_COUNT);
    u32 usamplepnt = UART_READ32(UART_SAMPLE_POINT);
    u32 udll, udlh;
    u32 ier = UART_READ32(UART_IER);
    spin_lock_irqsave(&mtk_console_lock, flags);
    reg_sync_writel((lcr | UART_LCR_DLAB), UART_LCR);
    udll = UART_READ32(UART_DLL);
    udlh = UART_READ32(UART_DLH);
    mb();                               /* make sure the DLL/DLH have been read */
    reg_sync_writel(lcr, UART_LCR);     /* DLAB end */
    spin_unlock_irqrestore(&mtk_console_lock, flags);
    dsb();

    MSG(CFG, "%s: RATEFIX(%02X); HSPEED(%02X); CNT(%02X); PNT(%02X); DLH(%02X), DLL(%02X), IER(%02X)\n",
            caller, uratefix, uhspeed, usamplecnt, usamplepnt, udlh, udll, ier);
#endif
}

void dump_uart_reg(void)
{
    struct mtk_uart *uart;
    unsigned int i;
    unsigned long base;
    u32 lsr, escape_en;
    for (i = 0; i < UART_NR; i++) {
        uart = &mtk_uarts[i];
   	base = uart->base;
	if (uart->poweron_count > 0)
	{
	    lsr = UART_READ32(UART_LSR);
	    escape_en = UART_READ32(UART_ESCAPE_EN);
	    printk("[UART%d] LSR=0x%x   ESCAPE_EN=0x%x\n", uart->nport, lsr, escape_en);
  	}else
	    printk("[UART%d] clock is off\n", uart->nport);
    }

}
/*---------------------------------------------------------------------------*/
void mtk_uart_console_setting_switch(struct mtk_uart *uart)
{
#ifdef CONFIG_MTK_SERIAL_CONSOLE
    //if(uart->nport == 0){ // UART1 as log port
        uart->setting->tx_mode = UART_NON_DMA;
        uart->setting->rx_mode = UART_NON_DMA;
        uart->tx_mode = UART_NON_DMA;
        uart->rx_mode = UART_NON_DMA;
	mtk_uart_enable_dpidle(uart);
    //}
#endif
}
/******************************************************************************
 * Virtual FIFO implementation
******************************************************************************/
#if defined(ENABLE_VFIFO)
/*---------------------------------------------------------------------------*/
int mtk_uart_vfifo_enable(struct mtk_uart *uart, struct mtk_uart_vfifo *vfifo)
{
    unsigned long base = uart->base;

    if (!vfifo) {
        MSG(ERR, "null\n");
        return -EINVAL;
    } else if (vfifo->type != UART_RX_VFIFO && vfifo->type != UART_TX_VFIFO) {
        MSG(ERR, "unknown type: %d\n", vfifo->type);
        return -EINVAL;
    } else {
    	/*
    	 * NOTE: For FCR is a read only register reason,
    	 *       special read/write/set/clr function need to use
    	 */
        /*UART_SET_BITS(UART_FCR_FIFO_INIT, UART_FCR);
        UART_CLR_BITS(UART_FCR_DMA1, UART_FCR);*/
        __set_fcr_register(uart, UART_FCR_FIFO_INIT);
        __clr_fcr_register(uart, UART_FCR_DMA1);

        if (vfifo->type == UART_RX_VFIFO)
            UART_SET_BITS(UART_RX_DMA_EN|UART_TO_CNT_AUTORST, UART_DMA_EN);
        else if (vfifo->type == UART_TX_VFIFO)
            UART_SET_BITS(UART_TX_DMA_EN, UART_DMA_EN);
        dsb();
    }
    return 0;
}
/*---------------------------------------------------------------------------*/
int mtk_uart_vfifo_disable(struct mtk_uart *uart, struct mtk_uart_vfifo *vfifo)
{
    unsigned long base = uart->base;

    if (!vfifo) {
        MSG(ERR, "null\n");
        return -EINVAL;
    } else if (vfifo->type != UART_RX_VFIFO && vfifo->type != UART_TX_VFIFO) {
        MSG(ERR, "unknown type: %d\n", vfifo->type);
        return -EINVAL;
    } else if (vfifo->type == UART_RX_VFIFO) {
        UART_CLR_BITS(UART_RX_DMA_EN|UART_TO_CNT_AUTORST, UART_DMA_EN);
    } else if (vfifo->type == UART_TX_VFIFO) {
        UART_CLR_BITS(UART_TX_DMA_EN, UART_DMA_EN);
    }
    dsb();
    return 0;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_vfifo_enable_tx_intr(struct mtk_uart *uart)
{
    reg_sync_writel(VFF_TX_INT_EN_B, VFF_INT_EN(uart->tx_vfifo->base));
}
/*---------------------------------------------------------------------------*/
void mtk_uart_vfifo_disable_tx_intr(struct mtk_uart *uart)
{
    reg_sync_writel(0x00, VFF_INT_EN(uart->tx_vfifo->base));
}
/*---------------------------------------------------------------------------*/
void mtk_uart_vfifo_clear_tx_intr(struct mtk_uart_vfifo *vfifo)
{
    reg_sync_writel(0x00, VFF_INT_FLAG(vfifo->base));
}
/*---------------------------------------------------------------------------*/
void mtk_uart_vfifo_clear_rx_intr(struct mtk_uart_vfifo *vfifo)
{
    reg_sync_writel(0x03, VFF_INT_FLAG(vfifo->base));
}
/*---------------------------------------------------------------------------*/
void mtk_uart_vfifo_enable_rx_intr(struct mtk_uart *uart)
{
    reg_sync_writel(VFF_RX_INT_EN0_B|VFF_RX_INT_EN1_B, VFF_INT_EN(uart->rx_vfifo->base));
}
/*---------------------------------------------------------------------------*/
void mtk_uart_vfifo_disable_rx_intr(struct mtk_uart *uart)
{
    reg_sync_writel(0x00, VFF_INT_EN(uart->rx_vfifo->base));
}
/*---------------------------------------------------------------------------*/
int mtk_uart_vfifo_is_full(struct mtk_uart_vfifo *vfifo)
{
    return (UART_READ32(VFF_LEFT_SIZE(vfifo->base)) <= 16) ? (1) : (0);
}
/*---------------------------------------------------------------------------*/
int mtk_uart_vfifo_is_empty(struct mtk_uart_vfifo *vfifo)
{
    return (UART_READ32(VFF_VALID_SIZE(vfifo->base)) == 0) ? (1) : (0);
}
/*---------------------------------------------------------------------------*/
void mtk_uart_vfifo_write_byte(struct mtk_uart *uart, unsigned int byte)
{
    void *addr, *base = uart->tx_vfifo->base;
    unsigned int wpt = UART_READ32(VFF_WPT(base));

    addr = (void*)((wpt&0xffff)+uart->tx_vfifo->addr);
    reg_sync_writeb((unsigned char)byte, addr);
    mb();                                              //make sure write point updated after VFIFO written.
#ifdef ENABLE_RAW_DATA_DUMP
    save_tx_raw_data(uart, addr);
#endif
    if((wpt&0xffff) == (UART_READ32(VFF_LEN(base))-1))
        reg_sync_writel((~wpt)&0x10000, VFF_WPT(base));
    else
        reg_sync_writel( wpt+1, VFF_WPT(base));
}
/*---------------------------------------------------------------------------*/
unsigned int mtk_uart_vfifo_read_byte(struct mtk_uart *uart)
{
    void *addr, *base = uart->rx_vfifo->base;
    unsigned int ch;

    addr = (void*)(UART_READ16(VFF_RPT(base))+uart->rx_vfifo->addr);
    ch = UART_READ8(addr);
    dsb();                                              //make sure read point updated after VFIFO read.
	if(UART_READ16(VFF_RPT(base)) == (UART_READ32(VFF_LEN(base))-1))
		reg_sync_writel(~(UART_READ32(VFF_RPT(base)))&0x10000, VFF_RPT(base));
	else
		reg_sync_writel( UART_READ32(VFF_RPT(base))+1, VFF_RPT(base));

    return ch;
}
/*---------------------------------------------------------------------------*/
int mtk_uart_vfifo_get_counts(struct mtk_uart_vfifo *vfifo)
{
    return UART_READ32(VFF_VALID_SIZE(vfifo->base));
}
/*---------------------------------------------------------------------------*/
void mtk_uart_tx_vfifo_flush(struct mtk_uart* uart, int timeout)
{
    struct mtk_uart_dma *dma = &uart->dma_tx;
    struct mtk_uart_vfifo *vfifo = dma->vfifo;
    void *base = vfifo->base;

#ifdef ENABE_HRTIMER_FLUSH
    if (dma && uart) {
        if (UART_READ32(VFF_FLUSH(base)) == 0) {
            reg_sync_writel(VFF_FLUSH_B, VFF_FLUSH(base));
            if (!timeout)
                hrtimer_try_to_cancel(&vfifo->flush);
            MSG(MSC, "flush [%5X.%5X]\n", UART_READ32(VFF_RPT(base)), UART_READ32(VFF_WPT(base)));
        } else {
            /*the ns used to transfer the data in TX VFIFO*/
            u32 size = UART_READ32(VFF_VALID_SIZE(base));
            s64 t = size*10*(NSEC_PER_SEC/uart->baudrate);
            ktime_t cur = ktime_get();
            ktime_t nxt = ktime_add_ns(cur, t);
        	hrtimer_try_to_cancel(&vfifo->flush);
        	hrtimer_start(&vfifo->flush, nxt, HRTIMER_MODE_ABS);
        #if defined(ENABLE_VFIFO_DEBUG)
        {
            struct timespec a = ktime_to_timespec(cur);
            struct timespec b = ktime_to_timespec(nxt);
            MSG(MSC, "start: [%ld %ld] [%ld %ld] [%d %lld]\n",
                a.tv_sec, a.tv_nsec, b.tv_sec, b.tv_nsec, size, t);
        }
        #endif
        }
    } else {
        MSG(ERR, "%p, %p\n", dma, uart);
        //del_timer(&dma->vfifo->timer);
    }
#else
    if(dma && uart){
        if(UART_READ32(VFF_FLUSH(base)) == 0){
            reg_sync_writel(VFF_FLUSH_B, VFF_FLUSH(base));
            MSG(MSC, "flush [%5X.%5X]\n", UART_READ32(VFF_RPT(base)), UART_READ32(VFF_WPT(base)));
        }
    }else{
        MSG(ERR, "%p, %p\n", dma, uart);
    }
#endif //ENABE_HRTIMER_FLUSH
}
/*---------------------------------------------------------------------------*/
static void mtk_uart_dma_vfifo_tx_tasklet_byte(unsigned long arg)
{
    struct mtk_uart *uart = (struct mtk_uart *)arg;
    struct uart_port   *port = &uart->port;
    //struct mtk_uart_dma *dma = &uart->dma_tx;
    struct mtk_uart_vfifo *vfifo = uart->tx_vfifo;
    struct circ_buf    *xmit = &port->state->xmit;
    unsigned int len, count, size, left, chk = 0;
    ktime_t begin, end;
    struct timespec a, b;

    size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
    left = vfifo->size - mtk_uart_vfifo_get_counts(vfifo);
    left = (left > 16) ? (left-16) : (0);   /*prevent from CPU lock*/
    len  = count = left < size ? left : size;

    if (!len) {
        chk = 1;
        MSG(DMA,">>>>> zero size <<<<< \n");
    }

    DGBUF_INIT(vfifo);
    begin = ktime_get();
    a = ktime_to_timespec(begin);
    while (len--) {
	/*DMA limitation.
	Workaround: Polling flush bit to zero, set 1s timeout*/
	while (UART_READ32(VFF_FLUSH(vfifo->base))){
	    end = ktime_get();
	    b = ktime_to_timespec(end);
	    if ((b.tv_sec - a.tv_sec) > 1 || ((b.tv_sec - a.tv_sec) == 1 && b.tv_nsec > a.tv_nsec)){
		pr_notice("[UART%d] Polling flush timeout\n",port->line);
		return;
	    }
	}
        DGBUF_PUSH_CH(vfifo, (char)xmit->buf[xmit->tail]);
        uart->write_byte(uart, xmit->buf[xmit->tail]);
        xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
        port->icount.tx++;
    }
#if defined(ENABLE_VFIFO_DEBUG)
    if (UART_DEBUG_EVT(DBG_EVT_DMA) && UART_DEBUG_EVT(DBG_EVT_BUF)) {
        char str[4] = {0};
        if (count >= 4) {
            str[0] = vfifo->cur->dat[0];
            str[1] = vfifo->cur->dat[1];
            str[2] = vfifo->cur->dat[vfifo->cur->idx-2];
            str[3] = vfifo->cur->dat[vfifo->cur->idx-1];
        } else {
            int idx;
            for (idx = 0; idx < count; idx++)
                str[idx] = vfifo->cur->dat[idx];
            for (; idx < 4; idx++)
                str[idx] = 0;
        }
        MSG(DMA, "TX[%4d]: %4d/%4d [%05X-%05X] (%02X %02X .. %02X %02X) \n",
            size, count, left, UART_READ32(VFF_WPT(vfifo->base)), UART_READ32(VFF_RPT(vfifo->base)),
            str[0], str[1], str[2], str[3]);
    } else {
        MSG(DMA, "TX[%4d]: %4d/%4d [%05X-%05X] \n",
            size, count, left, UART_READ32(VFF_WPT(vfifo->base)), UART_READ32(VFF_RPT(vfifo->base)));
    }
#endif

#if defined(ENABLE_VFIFO_DEBUG)
    if (UART_DEBUG_EVT(DBG_EVT_DAT) && UART_DEBUG_EVT(DBG_EVT_BUF)) {
        int i;
        printk("[UART%d_TX] %4d bytes:", uart->nport, vfifo->cur->idx);
        for (i = 0; i < vfifo->cur->idx; i++) {
            if (i % 16 == 0)
                printk("\n");
            printk("%.2x ", (unsigned char)vfifo->cur->dat[i]);
        }
        printk("\n");
    }
#endif

    if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
        uart_write_wakeup(port);

}
/*---------------------------------------------------------------------------*/
/*static int mtk_uart_vfifo_write_string(struct mtk_uart *uart, const unsigned char *chars, size_t size)
{
    void *addr, *base = uart->tx_vfifo->base;
    unsigned int wpt = UART_READ32(VFF_WPT(base));
    unsigned int num_to_end;

    addr = (void*)((wpt&0xffff)+uart->tx_vfifo->addr);
    num_to_end = UART_READ32(VFF_LEN(base)) - (wpt&0xffff);
    if(num_to_end >= size){
        memcpy(addr, chars, size);
        mb();                                                  //make sure write point updated after VFIFO written.
        reg_sync_writel( wpt+(unsigned int)size, VFF_WPT(base));
    }else{
        memcpy(addr, chars, num_to_end);
        memcpy(uart->tx_vfifo->addr, &chars[num_to_end], (unsigned int)size - num_to_end);
        mb();                                                  //make sure write point updated after VFIFO written.
        wpt = ((~wpt)&0x10000)+ (unsigned int)size - num_to_end;
        reg_sync_writel(wpt, VFF_WPT(base));
    }

    return size;
}*/
/*---------------------------------------------------------------------------*/
/*static void mtk_uart_dma_vfifo_tx_tasklet_str(unsigned long arg)
{
    struct mtk_uart *uart = (struct mtk_uart *)arg;
    struct uart_port   *port = &uart->port;
    //struct mtk_uart_dma *dma = &uart->dma_tx;
    struct mtk_uart_vfifo *vfifo = uart->tx_vfifo;
    struct circ_buf    *xmit = &port->state->xmit;
    unsigned int len, count, size, left, chk = 0;

    size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
    left = vfifo->size - mtk_uart_vfifo_get_counts(vfifo);
    left = (left > 16) ? (left-16) : (0);
    len  = count = left < size ? left : size;

    if (!len) {
        chk = 1;
        MSG(DMA,">>>>> zero size <<<<< \n");
    }

    DGBUF_INIT(vfifo);

    mtk_uart_vfifo_write_string(uart, &xmit->buf[xmit->tail], size);
    DGBUF_PUSH_STR(vfifo, &xmit->buf[xmit->tail], size);
    xmit->tail = (xmit->tail+size) & (UART_XMIT_SIZE - 1);
    port->icount.tx += size;

#if defined(ENABLE_VFIFO_DEBUG)
    if (UART_DEBUG_EVT(DBG_EVT_DMA) && UART_DEBUG_EVT(DBG_EVT_BUF)) {
        char str[4] = {0};
        if (count >= 4) {
            str[0] = vfifo->cur->dat[0];
            str[1] = vfifo->cur->dat[1];
            str[2] = vfifo->cur->dat[vfifo->cur->idx-2];
            str[3] = vfifo->cur->dat[vfifo->cur->idx-1];
        } else {
            int idx;
            for (idx = 0; idx < count; idx++)
                str[idx] = vfifo->cur->dat[idx];
            for (; idx < 4; idx++)
                str[idx] = 0;
        }
        MSG(DMA, "TX[%4d]: %4d/%4d [%05X-%05X] (%02X %02X .. %02X %02X) \n",
            size, count, left, UART_READ32(VFF_WPT(vfifo->base)), UART_READ32(VFF_RPT(vfifo->base)),
            str[0], str[1], str[2], str[3]);
    } else {
        MSG(DMA, "TX[%4d]: %4d/%4d [%05X-%05X] \n",
            size, count, left, UART_READ32(VFF_WPT(vfifo->base)), UART_READ32(VFF_RPT(vfifo->base)));
    }
#endif

#if defined(ENABLE_VFIFO_DEBUG)
    if (UART_DEBUG_EVT(DBG_EVT_DAT) && UART_DEBUG_EVT(DBG_EVT_BUF)) {
        int i;
        printk("[UART%d_TX] %4d bytes:", uart->nport, vfifo->cur->idx);
        for (i = 0; i < vfifo->cur->idx; i++) {
            if (i % 16 == 0)
                printk("\n");
            printk("%.2x ", (unsigned char)vfifo->cur->dat[i]);
        }
        printk("\n");
    }
#endif

    if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
        uart_write_wakeup(port);

}*/
/*---------------------------------------------------------------------------*/
void mtk_uart_dma_vfifo_tx_tasklet(unsigned long arg)
{
    struct mtk_uart *uart = (struct mtk_uart *)arg;
    struct uart_port   *port = &uart->port;
    struct mtk_uart_dma *dma = &uart->dma_tx;
    struct mtk_uart_vfifo *vfifo = uart->tx_vfifo;
    struct circ_buf    *xmit = &port->state->xmit;
    int txcount = port->icount.tx;
    void *base = vfifo->base;
    unsigned long flags;

    spin_lock_irqsave(&vfifo->iolock, flags);
    if (atomic_inc_and_test(&vfifo->entry) > 1) {
        MSG(ERR, "tx entry!!\n");
        tasklet_schedule(&vfifo->dma->tasklet);
    } else {
        while (UART_READ32(VFF_LEFT_SIZE(base)) >= vfifo->trig) {
            /* deal with x_char first */
            if (unlikely(port->x_char)) {
                MSG(INFO, "detect x_char!!\n");
                uart->write_byte(uart, port->x_char);
                port->icount.tx++;
                port->x_char = 0;
                break;
            }
            if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
                uart->pending_tx_reqs = 0;
                atomic_set(&dma->free, 1);
                complete(&dma->done);
                break;
            }
            mtk_uart_dma_vfifo_tx_tasklet_byte(arg);
        }
        if (txcount != port->icount.tx) {
            mtk_uart_vfifo_enable_tx_intr(uart);
            mtk_uart_tx_vfifo_flush(uart, 0);
        }
    }
    atomic_dec(&vfifo->entry);
    spin_unlock_irqrestore(&vfifo->iolock, flags);
}
/*---------------------------------------------------------------------------*/
/*static void mtk_uart_dma_vfifo_rx_tasklet_byte(unsigned long arg)
{
    struct mtk_uart *uart = (struct mtk_uart*)arg;
    struct uart_port   *port = &uart->port;
    struct mtk_uart_vfifo *vfifo = uart->rx_vfifo;
    struct tty_struct *tty = uart->port.state->port.tty;
    int count, left;
    unsigned int ch, flag, status;
    unsigned long flags;

    MSG_FUNC_ENTRY();

    count = left = mtk_uart_vfifo_get_counts(vfifo);

    spin_lock_irqsave(&port->lock, flags);

    DGBUF_INIT(vfifo);
    while (!mtk_uart_vfifo_is_empty(vfifo) && count > 0) {

        status = uart->read_status(uart);
        status = mtk_uart_filter_line_status(uart);

        ch = uart->read_byte(uart);
        flag = TTY_NORMAL;

        if (status & UART_LSR_BI) {
            MSG(INFO, "Break Interrupt!!!\n");
            port->icount.brk++;
            if (uart_handle_break(port))
                continue;
            flag = TTY_BREAK;
        } else if (status & UART_LSR_PE) {
            MSG(INFO, "Parity Error!!!\n");
            port->icount.parity++;
            flag = TTY_PARITY;
        } else if (status & UART_LSR_FE) {
            MSG(INFO, "Frame Error!!!\n");
            port->icount.frame++;
            flag = TTY_FRAME;
        } else if (status & UART_LSR_OE) {
            MSG(INFO, "Overrun!!!\n");
            port->icount.overrun++;
            flag = TTY_OVERRUN;
        }
        port->icount.rx++;
        count--;
        DGBUF_PUSH_CH(vfifo, ch);
        if (!tty_insert_flip_char(tty, ch, flag))
            MSG(ERR, "tty_insert_flip_char: no space\n");
    }
    tty_flip_buffer_push(tty);

#if defined(ENABLE_VFIFO_DEBUG)
    if (UART_DEBUG_EVT(DBG_EVT_DMA) && UART_DEBUG_EVT(DBG_EVT_BUF)) {
        char str[4] = {0};
        if (count >= 4) {
            str[0] = vfifo->cur->dat[0];
            str[1] = vfifo->cur->dat[1];
            str[2] = vfifo->cur->dat[vfifo->cur->idx-2];
            str[3] = vfifo->cur->dat[vfifo->cur->idx-1];
        } else {
            int idx;
            for (idx = 0; idx < count; idx++)
                str[idx] = vfifo->cur->dat[idx];
            for (; idx < 4; idx++)
                str[idx] = 0;
        }
        MSG(DMA, "RX[%4d]: %4d bytes from VFIFO [%4d] (%02X %02X .. %02X %02X) %d\n",
            left, left - count, mtk_uart_vfifo_get_counts(vfifo), str[0], str[1], str[2], str[3],
            UART_READ32(VFF_VALID_SIZE(vfifo->base)));
    } else {
        MSG(DMA, "RX[%4d]: %4d bytes from VFIFO [%4d] %d\n",
            left, left - count, mtk_uart_vfifo_get_counts(vfifo),
            UART_READ32(VFF_VALID_SIZE(vfifo->base)));
    }
#endif

    spin_unlock_irqrestore(&port->lock, flags);

#if defined(ENABLE_VFIFO_DEBUG)
    if (UART_DEBUG_EVT(DBG_EVT_DAT) && UART_DEBUG_EVT(DBG_EVT_BUF)) {
        int i;
        printk("[UART%d_RX] %4d bytes:", uart->nport, vfifo->cur->idx);

        for (i = 0; i < vfifo->cur->idx; i++) {
            if (i % 16 == 0)
                printk("\n");
            printk("%.2x ", (unsigned char)vfifo->cur->dat[i]);
        }
        printk("\n");

    }
#endif
}*/
/*---------------------------------------------------------------------------*/
/* A duplicate of tty_insert_flip_string.                                    */
/* The only difference is the function will accept one extra variable for    */
/* indicating the current line status.                                       */
/*---------------------------------------------------------------------------*/
static int mtk_uart_tty_insert_flip_string(struct mtk_uart* uart, const unsigned char *chars,
				size_t size)
{
    struct tty_struct *tty = uart->port.state->port.tty;
    struct uart_port   *port = &uart->port;
    u32 status, flag;
    int copied = 0;

    status = uart->read_status(uart);
    status = mtk_uart_filter_line_status(uart);

    flag = TTY_NORMAL;
    /* error handling routine */
    if (status & UART_LSR_BI) {
        MSG(ERR, "Break Interrupt!!\n");
        port->icount.brk++;
        if (uart_handle_break(port))
            return 0;
        flag = TTY_BREAK;
    } else if (status & UART_LSR_PE) {
        MSG(ERR, "Parity Error!!!\n");
        port->icount.parity++;
        flag = TTY_PARITY;
    } else if (status & UART_LSR_FE) {
        MSG(ERR, "Frame Error!!!\n");
        port->icount.frame++;
        flag = TTY_FRAME;
    } else if (status & UART_LSR_OE) {
        MSG(ERR, "Overrun!!!\n");
        port->icount.overrun++;
        flag = TTY_OVERRUN;
    }

#ifdef ENABLE_RAW_DATA_DUMP
    save_rx_raw_data(uart, chars, size);
#endif

    if (flag == TTY_NORMAL) {
        copied = tty_insert_flip_string(tty->port, chars, size);
    } else {
        MSG(ERR, "error occurs\n");
        copied += tty_insert_flip_string(tty->port, chars, size-1);
        copied += tty_insert_flip_char(tty->port, chars[size-1], flag);
    }
    port->icount.rx += copied;
    return copied;
}
/*---------------------------------------------------------------------------*/
static void mtk_uart_dma_vfifo_rx_tasklet_str(unsigned long arg)
{
    struct mtk_uart *uart = (struct mtk_uart*)arg;
    struct uart_port   *port = &uart->port;
    struct mtk_uart_vfifo *vfifo = uart->rx_vfifo;
    struct tty_struct *tty = uart->port.state->port.tty;
    int count, left;
    unsigned int rxptr, txptr, txreg, rxreg;
    unsigned long flags;
    unsigned char *ptr;
    void *base = vfifo->base;

    MSG_FUNC_ENTRY();

    spin_lock_irqsave(&port->lock, flags);

    rxreg = UART_READ32(VFF_RPT(base));
    txreg = UART_READ32(VFF_WPT(base));
    rxptr = rxreg & 0x0000FFFF;
    txptr = txreg & 0x0000FFFF;
    count = left = ((rxreg ^ txreg) & 0x00010000) ? (txptr+vfifo->size-rxptr) : (txptr-rxptr);

    DGBUF_INIT(vfifo);

#ifdef ENABLE_RAW_DATA_DUMP
    reset_rx_raw_data(uart);
#endif

    if ((rxptr+count) <= txptr) {
        ptr = (unsigned char*)(rxptr+vfifo->addr);
        mtk_uart_tty_insert_flip_string(uart, ptr, count);
        DGBUF_PUSH_STR(vfifo, ptr, count);
    } else {
        ptr = (unsigned char*)(rxptr+vfifo->addr);
        mtk_uart_tty_insert_flip_string(uart, ptr, vfifo->size-rxptr);
        DGBUF_PUSH_STR(vfifo, ptr, vfifo->size-rxptr);
        if (txptr) {
            ptr = (unsigned char*)(vfifo->addr);
            mtk_uart_tty_insert_flip_string(uart, ptr, txptr);
            DGBUF_PUSH_STR(vfifo, ptr, txptr);
        }
    }
    dsb();                                                  //make sure read point updated after VFIFO read.
    reg_sync_writel(txreg, VFF_RPT(base));
    tty_flip_buffer_push(tty->port);

#if defined(ENABLE_VFIFO_DEBUG)
    if (UART_DEBUG_EVT(DBG_EVT_DMA) && UART_DEBUG_EVT(DBG_EVT_BUF)) {
        char str[4] = {0};
        if (count >= 4) {
            str[0] = vfifo->cur->dat[0];
            str[1] = vfifo->cur->dat[1];
            str[2] = vfifo->cur->dat[vfifo->cur->idx-2];
            str[3] = vfifo->cur->dat[vfifo->cur->idx-1];
        } else {
            int idx;
            for (idx = 0; idx < count; idx++)
                str[idx] = vfifo->cur->dat[idx];
            for (; idx < 4; idx++)
                str[idx] = 0;
        }
        MSG(DMA, "RX[%4d]: [%5X..%5X] [%5X..%5X] (%02X %02X .. %02X %02X) [%d]\n",
            left, rxreg, txreg, UART_READ32(VFF_RPT(base)), UART_READ32(VFF_WPT(base)),
            str[0], str[1], str[2], str[3], UART_READ32(VFF_FLUSH(base)));
    } else {
        MSG(DMA, "RX[%4d]: [%5X..%5X] [%5X..%5X] [%d] [%4X.%4X]\n",
            left, rxreg, txreg, UART_READ32(VFF_RPT(base)), UART_READ32(VFF_WPT(base)),UART_READ32(VFF_FLUSH(base)),
            UART_READ32(VFF_VALID_SIZE(base)), UART_READ32(VFF_LEFT_SIZE(base)));
    }
#endif
    spin_unlock_irqrestore(&port->lock, flags);


#if defined(ENABLE_VFIFO_DEBUG)
    if (UART_DEBUG_EVT(DBG_EVT_DAT) && UART_DEBUG_EVT(DBG_EVT_BUF)) {
        int i;
        printk("[UART%d_RX] %4d bytes:", uart->nport, vfifo->cur->idx);

        for (i = 0; i < vfifo->cur->idx; i++) {
            if (i % 16 == 0)
                printk("\n");
            printk("%.2x ", (unsigned char)vfifo->cur->dat[i]);
        }
        printk("\n");

    }
#endif
}
/*---------------------------------------------------------------------------*/
void mtk_uart_dma_vfifo_rx_tasklet(unsigned long arg)
{   /*the function will be called through dma irq or tasklet_schedule*/
    struct mtk_uart *uart = (struct mtk_uart*)arg;
    struct mtk_uart_vfifo *vfifo = uart->rx_vfifo;
    unsigned long flags;

    MSG(DMA, "%d, %x, %x\n", uart->read_allow(uart), UART_READ32(VFF_VALID_SIZE(vfifo->base)), vfifo->trig);
    spin_lock_irqsave(&vfifo->iolock, flags);
    if (atomic_inc_and_test(&vfifo->entry) > 1) {
        MSG(ERR, "rx entry!!\n");
        tasklet_schedule(&vfifo->dma->tasklet);
    } else {
        if (uart->read_allow(uart))
            mtk_uart_dma_vfifo_rx_tasklet_str(arg);
    }
    atomic_dec(&vfifo->entry);
    spin_unlock_irqrestore(&vfifo->iolock, flags);
}
/*---------------------------------------------------------------------------*/
void mtk_uart_dma_setup(struct mtk_uart *uart,
                                  struct mtk_uart_dma *dma)
{
    void *base;
    if (!dma)
        return;

    if (dma->mode == UART_RX_VFIFO_DMA || dma->mode == UART_TX_VFIFO_DMA) {
        if (!dma->vfifo) {
            MSG(ERR, "null\n");
            return;
        }
        base = dma->vfifo->base;
        reg_sync_writel(dma->vfifo->dmahd, VFF_ADDR(base));
        reg_sync_writel(dma->vfifo->trig, VFF_THRE(base));
        reg_sync_writel(dma->vfifo->size, VFF_LEN(base));

        if (dma->vfifo->type == UART_RX_VFIFO)
            //reg_sync_writel(VFF_RX_INT_EN0_B, VFF_INT_EN(base));
            reg_sync_writel(VFF_RX_INT_EN0_B|VFF_RX_INT_EN1_B, VFF_INT_EN(base));
        dsb();
    }
}
/*---------------------------------------------------------------------------*/
int mtk_uart_dma_start(struct mtk_uart *uart, struct mtk_uart_dma *dma)
{
    void *base;
    MSG_FUNC_ENTRY();

    if (!dma)
        return -1;

    if (!atomic_read(&dma->free))
        return -1;

    if (dma->mode == UART_TX_VFIFO_DMA || dma->mode == UART_RX_VFIFO_DMA) {
        if (!dma->vfifo) {
            MSG(ERR, "null\n");
            return -EINVAL;
        }
        base = dma->vfifo->base;
        reg_sync_writel(VFF_INT_FLAG_CLR_B, VFF_INT_FLAG(base));
        reg_sync_writel(VFF_EN_B, VFF_EN(base));

        if (UART_READ32(VFF_EN(base)) != VFF_EN_B)
            MSG(ERR, "Start DMA fail\n");
    }

    atomic_set(&dma->free, 0);
    init_completion(&dma->done);

    return 0;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_stop_dma(struct mtk_uart_dma *dma)
{
    int polling_cnt=0;
    struct mtk_uart* uart = dma->uart;
    void *base;
    if (!dma)
        return;
    if (dma->mode == UART_RX_VFIFO_DMA || dma->mode == UART_TX_VFIFO_DMA) {
        MSG(DMA, "stop dma (%d)\n", dma->mode);
        if (!dma->vfifo) {
            MSG(ERR, "null\n");
            return;
        }
        base = dma->vfifo->base;

        /*set flush as 1 -> wait until flush is 0*/
        reg_sync_writel(VFF_FLUSH_CLR_B, VFF_FLUSH(base));
        while (UART_READ32(VFF_FLUSH(base)))
	{
	    polling_cnt++;
	    if (polling_cnt > 10000){
		printk("mtk_uart_stop_dma: polling VFF_FLUSH fail VFF_DEBUG_STATUS=0x%x\n", UART_READ32(VFF_DEBUG_STATUS(base)));
		break;
	    }
	}

	polling_cnt = 0;
        /*set stop as 1 -> wait until en is 0 -> set stop as 0*/
        reg_sync_writel(VFF_STOP_B, VFF_STOP(base));
        while (UART_READ32(VFF_EN(base)))
	{
	    polling_cnt++;
	    if (polling_cnt > 10000){
		printk("mtk_uart_stop_dma: polling VFF_EN fail VFF_DEBUG_STATUS=0x%x\n", UART_READ32(VFF_DEBUG_STATUS(base)));
		break;
	    }
	}
        reg_sync_writel(VFF_STOP_CLR_B, VFF_STOP(base));

        reg_sync_writel(VFF_INT_EN_CLR_B, VFF_INT_EN(base));
        reg_sync_writel(VFF_INT_FLAG_CLR_B, VFF_INT_FLAG(base));
    } else {
        MSG(ERR, "unknown mode: %d\n", dma->mode);
    }
}
/*---------------------------------------------------------------------------*/
void mtk_uart_reset_dma(struct mtk_uart_dma *dma)
{
    struct mtk_uart *uart = dma->uart;
    void* base;
    if (!dma)
        return;

    if (dma->mode == UART_RX_VFIFO_DMA || dma->mode == UART_TX_VFIFO_DMA) {
        if (!dma->vfifo) {
            MSG(ERR, "null\n");
            return;
        }
        base = dma->vfifo->base;
        //mt65xx_req_vff_dma(dma->vfifo->ch, NULL, NULL);
        reg_sync_writel(0, VFF_ADDR(base));
        reg_sync_writel(0, VFF_THRE(base));
        reg_sync_writel(0, VFF_LEN(base));
        /*set warm_rst as 1 -> wait until en is 0*/
        reg_sync_writel(VFF_WARM_RST_B, VFF_RST(base));
        while (UART_READ32(VFF_EN(base)));

        /* Reset write point for tx dma */
        if(dma->mode == UART_TX_VFIFO_DMA){
		reg_sync_writel(0, VFF_WPT(base));
        }else if(dma->mode == UART_RX_VFIFO_DMA){
		reg_sync_writel(0, VFF_RPT(base));
        }
    } else {
        MSG(ERR, "unknown mode: %d\n", dma->mode);
    }
}
#endif /*defined(ENABLE_VFIFO)*/
/*---------------------------------------------------------------------------*/
void mtk_uart_fifo_init(struct mtk_uart *uart)
{
    /*
     * NOTE: For FCR is a read only register reason,
     *       special read/write/set/clr function need to use
     */
    //UART_SET_BITS(UART_FCR_FIFO_INIT, UART_FCR);
    __set_fcr_register(uart, UART_FCR_FIFO_INIT);
    dsb();
}
/*---------------------------------------------------------------------------*/
void mtk_uart_fifo_flush(struct mtk_uart *uart)
{
    /*
     * NOTE: For FCR is a read only register reason,
     *       special read/write/set/clr function need to use
     */
    //UART_SET_BITS(UART_FCR_CLRR | UART_FCR_CLRT, UART_FCR);
    __set_fcr_register(uart, UART_FCR_CLRR | UART_FCR_CLRT);
    dsb();
}
/*---------------------------------------------------------------------------*/
int mtk_uart_data_ready(struct mtk_uart *uart)
{
    if ((uart->read_status(uart) & UART_LSR_DR))
	return 1;
    else
	return 0;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_fifo_set_trig(struct mtk_uart *uart, int tx_level,
                                      int rx_level)
{
    unsigned long base = uart->base;
    unsigned long tmp1;
	unsigned long flags;

    tmp1 = UART_READ32(UART_LCR);
    spin_lock_irqsave(&mtk_console_lock, flags);
    reg_sync_writel(0xbf, UART_LCR);
    UART_SET_BITS(UART_EFR_EN, UART_EFR);
    reg_sync_writel(tmp1, UART_LCR);
    spin_unlock_irqrestore(&mtk_console_lock, flags);
    MSG(INFO, "%s(EFR) =  %04X\n", __func__, UART_READ32(UART_EFR));

    /*
     * NOTE: For FCR is a read only register reason,
     *       special read/write/set/clr function need to use
     */
    //reg_sync_writel(UART_FCR_FIFO_INIT|tx_level|rx_level, UART_FCR);
    sync_write_fcr_register(uart, UART_FCR_FIFO_INIT|tx_level|rx_level);
}
/*---------------------------------------------------------------------------*/
void mtk_uart_set_mode(struct mtk_uart *uart, int mode)
{
    /*
     * NOTE: For FCR is a read only register reason,
     *       special read/write/set/clr function need to use
     */
    if (mode == UART_DMA_MODE_0) {
        //UART_CLR_BITS(UART_FCR_DMA1, UART_FCR);
        __clr_fcr_register(uart, UART_FCR_DMA1);
    } else if (mode == UART_DMA_MODE_1) {
        //UART_SET_BITS(UART_FCR_DMA1, UART_FCR);
        __set_fcr_register(uart, UART_FCR_DMA1);
    }
    dsb();
}
/*---------------------------------------------------------------------------*/
void mtk_uart_set_auto_baud(struct mtk_uart *uart)
{
    unsigned long base = uart->base;

    MSG_FUNC_ENTRY();

    switch (uart->sysclk)
    {
    case MTK_SYSCLK_13:
        reg_sync_writel(UART_AUTOBADUSAM_13M, UART_AUTOBAUD_SAMPLE);
        break;
    case MTK_SYSCLK_26:
         reg_sync_writel(UART_AUTOBADUSAM_26M, UART_AUTOBAUD_SAMPLE);
        break;
    case MTK_SYSCLK_52:
        reg_sync_writel(UART_AUTOBADUSAM_52M, UART_AUTOBAUD_SAMPLE);
        break;
    default:
        dev_err(uart->port.dev, "SYSCLK = %ldMHZ doesn't support autobaud\n",
                uart->sysclk);
        return;
    }
    reg_sync_writel(0x01, UART_AUTOBAUD_EN); /* Enable Auto Baud */
    return;
}
/*---------------------------------------------------------------------------*/
static void mtk_uart_cal_baud(struct mtk_uart *uart, int baudrate, int highspeed)
{
    unsigned long base = uart->base;
    u32 remainder, uartclk = 0, divisor = 0;
    u32 lcr = UART_READ32(UART_LCR);
	unsigned long flags;

    #ifdef UART_USING_FIX_CLK_ENABLE
    if(baudrate <= 1000000){		/* Using 16.25 fix clock */
        uartclk = uart->sysclk>>2;
        reg_sync_writel(0x03, UART_RATE_FIX_AD);
    }else{				/* >1M, Using 65 clock */
        uartclk = uart->sysclk;
        reg_sync_writel(0x00, UART_RATE_FIX_AD);
    }
    if(3 == highspeed)
        UART_SET_BITS(UART_MCR_DCM_EN, UART_MCR);/* Enable UART DCM */
    else
    	UART_CLR_BITS(UART_MCR_DCM_EN, UART_MCR);/* Disable UART DCM */
    #else  /* UART_Fix_clk_DISABLE */
    uartclk = uart->sysclk;
    reg_sync_writel(0x00, UART_RATE_FIX_AD);
    #endif /* UART_USING_FIX_CLK_ENABLE */

    spin_lock_irqsave(&mtk_console_lock, flags);
    if (highspeed == 0) {
        //uartclk = uart->sysclk;
        //reg_sync_writel(0x00, UART_RATE_FIX_AD);
        reg_sync_writel(0x00, UART_HIGHSPEED); /*divider is 16*/
        divisor   = (uartclk >> 4)/(u32)baudrate;
        remainder = (uartclk >> 4)%(u32)baudrate;
        if (remainder >= (u32)(baudrate*8))
            divisor += 1;
        reg_sync_writel(lcr|UART_LCR_DLAB, UART_LCR);
        reg_sync_writel((divisor&0xFF), UART_DLL);
        reg_sync_writel(((divisor>>8) & 0xFF), UART_DLH);
        reg_sync_writel(lcr, UART_LCR);
    } else if (highspeed == 1) {
        //uartclk = uart->sysclk;
        //reg_sync_writel(0x00, UART_RATE_FIX_AD);
        reg_sync_writel(0x01, UART_HIGHSPEED); /*divider is 8*/
        divisor   = (uartclk >> 3)/(u32)baudrate;
        remainder = (uartclk >> 3)%(u32)baudrate;
        if (remainder >= (u32)(baudrate*4))
            divisor += 1;
        reg_sync_writel(lcr|UART_LCR_DLAB, UART_LCR);
        reg_sync_writel((divisor&0xFF), UART_DLL);
        reg_sync_writel(((divisor>>8) & 0xFF), UART_DLH);
        reg_sync_writel(lcr, UART_LCR);
    } else if (highspeed == 2) {
        //uartclk = uart->sysclk;
        //reg_sync_writel(0x00, UART_RATE_FIX_AD);
        reg_sync_writel(0x02, UART_HIGHSPEED); /*divider is 4*/
        divisor   = (uartclk >> 2)/(u32)baudrate;
        remainder = (uartclk >> 2)%(u32)baudrate;
        if (remainder >= (u32)(baudrate*2))
            divisor += 1;
        reg_sync_writel(lcr|UART_LCR_DLAB, UART_LCR);
        reg_sync_writel((divisor&0x00FF), UART_DLL);
        reg_sync_writel(((divisor>>8) & 0x00FF), UART_DLH);
        reg_sync_writel(lcr, UART_LCR);
    } else if (highspeed == 3) {
        u32 sample_count, sample_point, high_div, tmp;
    #if defined(ENABLE_FRACTIONAL)
        u32 fraction;
        u16 fraction_L_mapping[] = {0, 1, 0x5, 0x15, 0x55, 0x57, 0x57, 0x77, 0x7F, 0xFF, 0xFF};
        u16 fraction_M_mapping[] = {0, 0,   0,    0,    0,    0,    1,    1,    1,    1,  0x3};
    #endif

        //uartclk = uart->sysclk;
        //reg_sync_writel(0x00, UART_RATE_FIX_AD);
        reg_sync_writel(0x03, UART_HIGHSPEED);
        tmp = (uartclk)/(u32)baudrate;
        high_div = (tmp >> 8) + 1;
        divisor = (uartclk)/(baudrate*high_div);

    #if defined(ENABLE_FRACTIONAL)
        fraction = ((uartclk*10)/baudrate)%10; /*count fraction to set fractoin register*/
        reg_sync_writel(fraction_L_mapping[fraction], UART_FRACDIV_L);
        reg_sync_writel(fraction_M_mapping[fraction], UART_FRACDIV_M);
    #else
        remainder = (uartclk)%(baudrate*high_div);
        if (remainder >= ((baudrate*high_div) >> 1))
            divisor += 1;
    #endif

        sample_count = divisor - 1;
        sample_point = (sample_count-1) >> 1;
        reg_sync_writel(lcr|UART_LCR_DLAB, UART_LCR);
        reg_sync_writel((high_div & 0x00FF), UART_DLL);
        reg_sync_writel(((high_div>>8) & 0x00FF), UART_DLH);
        reg_sync_writel(lcr, UART_LCR);
        reg_sync_writel(sample_count, UART_SAMPLE_COUNT);
        reg_sync_writel(sample_point, UART_SAMPLE_POINT);
        /*
         * NOTICE: We found some chip, that is using lower clock, may not have enough time to check stop bit.
         *         In order to improve compatibilty, the guard time register is enabled which is used to extend the stop bit.
         */
        if (baudrate >= 3000000)
            reg_sync_writel(0x12, UART_GUARD);
    }
    spin_unlock_irqrestore(&mtk_console_lock, flags);

    MSG(CFG, "BaudRate = %d, SysClk = %d, Divisor = %d, %04X/%04X\n", baudrate, uartclk, divisor, UART_READ32(UART_IER), UART_READ32(UART_LCR));
    dump_reg(uart, __func__);
    dsb();  /*to ensure the setting is written*/
}
/*---------------------------------------------------------------------------*/
void mtk_uart_baud_setting(struct mtk_uart *uart , int baudrate)
{
	  u32 uartclk;
#if defined(CONFIG_MTK_FPGA)
    u32 tmp_div;
#endif
    uartclk = uart->sysclk;

#if defined(CONFIG_MTK_FPGA)
    tmp_div = (uartclk)/(unsigned int)baudrate;
    if (tmp_div > 255)
        mtk_uart_cal_baud(uart, baudrate, 2);
    else
        mtk_uart_cal_baud(uart, baudrate, 3);
#else
    /* Fix clock, using new settings */
    #ifdef UART_USING_FIX_CLK_ENABLE
    if (baudrate < 115200)
        mtk_uart_cal_baud(uart, baudrate, 0);
    else
        mtk_uart_cal_baud(uart, baudrate, 3);
    #else  /* UART_Fix_Clock_DISABLE */
    if (baudrate <= 115200)
        mtk_uart_cal_baud(uart, baudrate, 0);
    else if (baudrate <= 460800)
        mtk_uart_cal_baud(uart, baudrate, 2);
    else
        mtk_uart_cal_baud(uart, baudrate, 3);
    #endif /* End of UART_DCM_CONFIG */
#endif
}
/*---------------------------------------------------------------------------*/
#if defined(ENABLE_DEBUG)
/*---------------------------------------------------------------------------*/
static u32 UART_READ_EFR(struct mtk_uart *uart)
{
    unsigned long base = uart->base;
    u32 efr, lcr = UART_READ32(UART_LCR);
	unsigned long flags;

    spin_lock_irqsave(&mtk_console_lock, flags);
    reg_sync_writel(0xbf, UART_LCR);
    efr = UART_READ32(UART_EFR);
    reg_sync_writel(lcr, UART_LCR);
    spin_unlock_irqrestore(&mtk_console_lock, flags);
    return efr;
}
/*---------------------------------------------------------------------------*/
#endif
/*---------------------------------------------------------------------------*/
void mtk_uart_set_flow_ctrl(struct mtk_uart *uart, int mode)
{
    unsigned long base = uart->base, old;
    unsigned int tmp = UART_READ32(UART_LCR);
	unsigned long flags;

    MSG(CFG, "%s: %04X\n", __func__, UART_READ_EFR(uart));

    spin_lock_irqsave(&mtk_console_lock, flags);
    switch (mode) {
    case UART_FC_NONE:
	reg_sync_writel(UART_ESCAPE_CH, UART_ESCAPE_DAT);
        reg_sync_writel(0x00, UART_ESCAPE_EN);
        reg_sync_writel(0xbf, UART_LCR);
        old = UART_READ32(UART_EFR);
        old &= ~(UART_EFR_AUTO_RTSCTS|UART_EFR_XON12_XOFF12);
        reg_sync_writel(old, UART_EFR);
        reg_sync_writel(tmp, UART_LCR);
        mtk_uart_disable_intrs(uart, UART_IER_XOFFI|UART_IER_RTSI|UART_IER_CTSI);
        break;
    case UART_FC_HW:
	reg_sync_writel(UART_ESCAPE_CH, UART_ESCAPE_DAT);
        reg_sync_writel(0x00, UART_ESCAPE_EN);
        UART_SET_BITS(UART_MCR_RTS, UART_MCR);
        reg_sync_writel(0xbf, UART_LCR);
        /*disable all flow control setting*/
        old = UART_READ32(UART_EFR);
        old &= ~(UART_EFR_AUTO_RTSCTS | UART_EFR_XON12_XOFF12);
        reg_sync_writel(old, UART_EFR);
        /*enable hw flow control*/
        old = UART_READ32(UART_EFR);
        reg_sync_writel(old | UART_EFR_AUTO_RTSCTS, UART_EFR);
        reg_sync_writel(tmp, UART_LCR);
        mtk_uart_disable_intrs(uart, UART_IER_XOFFI);
        mtk_uart_enable_intrs(uart, UART_IER_CTSI|UART_IER_RTSI);
        break;
    case UART_FC_SW: /*MTK software flow control*/
        reg_sync_writel(UART_ESCAPE_CH, UART_ESCAPE_DAT);
        reg_sync_writel(0x01, UART_ESCAPE_EN);
        reg_sync_writel(0xbf, UART_LCR);
        /*dsiable all flow control setting*/
        old = UART_READ32(UART_EFR);
        old &= ~(UART_EFR_AUTO_RTSCTS | UART_EFR_XON12_XOFF12);
        reg_sync_writel(old, UART_EFR);
        /*enable sw flow control*/
        old = UART_READ32(UART_EFR);
        reg_sync_writel(old | UART_EFR_XON1_XOFF1, UART_EFR);
        reg_sync_writel(START_CHAR(uart->port.state->port.tty), UART_XON1);
        reg_sync_writel(STOP_CHAR(uart->port.state->port.tty), UART_XOFF1);
        reg_sync_writel(tmp, UART_LCR);
        mtk_uart_disable_intrs(uart, UART_IER_CTSI|UART_IER_RTSI);
        mtk_uart_enable_intrs(uart, UART_IER_XOFFI);
        break;
    }
    spin_unlock_irqrestore(&mtk_console_lock, flags);
    dsb();  /*to ensure the setting is written*/
    uart->fctl_mode = mode;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_power_up(struct mtk_uart *uart)
{
#ifndef CONFIG_MTK_FPGA
    struct mtk_uart_setting *setting;
    setting = uart->setting;

    if (!uart || uart->nport >= UART_NR)
        return;

    if (uart->poweron_count > 0) {
        MSG(FUC, "%s(%d)\n", __func__, uart->poweron_count);
    } else {
    #ifdef POWER_FEATURE
        if (0 != enable_clock(setting->pll_id,"UART"))
            MSG(ERR, "power on fail!!\n");
        if ((uart != console_port) && (uart->tx_mode == UART_TX_VFIFO_DMA || uart->rx_mode == UART_RX_VFIFO_DMA)){
            if (0 != enable_clock(PDN_FOR_DMA, "VFIFO") )
                MSG(ERR, "power on dma fail!\n");
	}
	uart->poweron_count++;
    #endif
    }
    MSG(FUC, "%s(%d) => up \n", __func__, uart->poweron_count);
#endif /* End of CONFIG_MTK_FPGA */
}
/*---------------------------------------------------------------------------*/
void mtk_uart_power_down(struct mtk_uart *uart)
{
#ifndef CONFIG_MTK_FPGA
    struct mtk_uart_setting *setting;
    setting = uart->setting;

    if (!uart || uart->nport >= UART_NR)
        return;

    if (uart->poweron_count == 0) {
        MSG(FUC, "%s(%d)\n", __func__, uart->poweron_count);
    } else {
    #ifdef POWER_FEATURE
        if (0 != disable_clock(setting->pll_id,"UART"))
            MSG(ERR, "power off fail!!\n");
        if ((uart != console_port) && (uart->tx_mode == UART_TX_VFIFO_DMA || uart->rx_mode == UART_RX_VFIFO_DMA)){
            if (0 != disable_clock(PDN_FOR_DMA, "VFIFO") )
                MSG(ERR, "power off dma fail!\n");
	}
	uart->poweron_count--;
    #endif
        MSG(FUC, "%s(%d) => dn \n", __func__, uart->poweron_count);
    }
#endif /* End of CONFIG_MTK_FPGA */
}
/*---------------------------------------------------------------------------*/
void mtk_uart_config(struct mtk_uart *uart, int datalen, int stop, int parity)
{
    unsigned long base = uart->base;
    unsigned int val = 0;

    switch (datalen)
    {
    case 5:
        val |= UART_WLS_5;
        break;
    case 6:
        val |= UART_WLS_6;
        break;
    case 7:
        val |= UART_WLS_7;
        break;
    case 8:
    default:
        val |= UART_WLS_8;
        break;
    }

    if (stop == 2 || (datalen == 5 && stop == 1))
        val |= UART_2_STOP;

    if (parity == 1)
        val |= UART_ODD_PARITY;
    else if (parity == 2)
        val |= UART_EVEN_PARITY;

    reg_sync_writel(val, UART_LCR);
}
/*---------------------------------------------------------------------------*/
unsigned int mtk_uart_read_status(struct mtk_uart *uart)
{
    unsigned long base = uart->base;

    uart->line_status = UART_READ32(UART_LSR);
    return uart->line_status;
}
/*---------------------------------------------------------------------------*/
unsigned int mtk_uart_read_allow(struct mtk_uart *uart)
{
    return uart->line_status & UART_LSR_DR;
}
/*---------------------------------------------------------------------------*/
/* Note:
 * 1. FIFO mode:
 *    -THRE=1 : when free space in FIFO is reduced blow its trigger level
 *    -THRE=0 : when free space in FIFO is more than its trigger level
 * 2. non-FIFO mode:
 *    -THRE=1 : when tx holding register is empty
 *    -THRE=0 : when tx holding register is not empty
 */
unsigned int mtk_uart_write_allow(struct mtk_uart *uart)
{
    unsigned long base = uart->base;
    return UART_READ32(UART_LSR) & UART_LSR_THRE;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_enable_intrs(struct mtk_uart *uart, long mask)
{   /*assume UART_EFR_EN is on*/
    unsigned long base = uart->base;

    UART_SET_BITS(mask, UART_IER);
    dsb();
}
/*---------------------------------------------------------------------------*/
void mtk_uart_disable_intrs(struct mtk_uart *uart, long mask)
{   /*assume UART_EFR_EN is on*/
    unsigned long base = uart->base;

    UART_CLR_BITS(mask, UART_IER);
    dsb();
}
/*---------------------------------------------------------------------------*/
unsigned int mtk_uart_read_byte(struct mtk_uart *uart)
{
    unsigned long base = uart->base;
    return UART_READ32(UART_RBR);
}
/*---------------------------------------------------------------------------*/
void mtk_uart_write_byte(struct mtk_uart *uart, unsigned int byte)
{
    unsigned long base = uart->base;
    reg_sync_writel(byte, UART_THR);
}
/*---------------------------------------------------------------------------*/
void mtk_uart_usb_rx_sel(unsigned int uart_port, unsigned int enable)
{
    unsigned long base = mtk_uart_default_settings[uart_port-1].uart_base;
    reg_sync_writel(enable, UART_RX_SEL);
}

/*---------------------------------------------------------------------------*/
unsigned int mtk_uart_filter_line_status(struct mtk_uart *uart)
{
    struct uart_port *port = &uart->port;
    unsigned int status;
    unsigned int lsr = uart->line_status;

    mtk_uart_lsr_status[uart->nport] |= lsr;
    status = UART_LSR_BI|UART_LSR_PE|UART_LSR_FE|UART_LSR_OE;

#ifdef ENABLE_DEBUG
    if ((lsr & UART_LSR_BI) || (lsr & UART_LSR_PE) ||
        (lsr & UART_LSR_FE) || (lsr & UART_LSR_OE)) {
        MSG(ERR, "LSR: BI=%d, FE=%d, PE=%d, OE=%d, DR=%d\n",
            (lsr & UART_LSR_BI) >> 4, (lsr & UART_LSR_FE) >> 3,
            (lsr & UART_LSR_PE) >> 2, (lsr & UART_LSR_OE) >> 1,
             lsr & UART_LSR_DR);
    }
#endif
    status &= port->read_status_mask;
    status &= ~port->ignore_status_mask;
    status &= lsr;

    return status;
}
/*---------------------------------------------------------------------------*/
int mtk_uart_get_interrupt(struct mtk_uart *uart)
{
    unsigned int intrs;
    unsigned long base = uart->base;
    intrs = UART_READ32(UART_IIR);
    return intrs;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_intr_last_check(struct mtk_uart *uart, int intrs)
{
	return;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_get_modem_status(struct mtk_uart *uart)
{
    unsigned long base = uart->base;
    struct uart_port *port = &uart->port;
    unsigned int status, delta;

    status  = UART_READ32(UART_MSR);
    status &= UART_MSR_DSR | UART_MSR_CTS | UART_MSR_DCD | UART_MSR_RI;

    MSG(INFO, "MSR: DCD(%d), RI(%d), DSR(%d), CTS(%d)\n",
        status & UART_MSR_DCD ? 1 : 0,
        status & UART_MSR_RI ? 1 : 0,
        status & UART_MSR_DSR ? 1 : 0,
        status & UART_MSR_CTS ? 1 : 0);

    delta = status ^ uart->old_status;

    if (!delta)
        return;

    if (uart->ms_enable) {
        if (delta & UART_MSR_DCD)
            uart_handle_dcd_change(port, status & UART_MSR_DCD);
        if (delta & UART_MSR_CTS)
            uart_handle_cts_change(port, status & UART_MSR_CTS);
        if (delta & UART_MSR_DSR)
            port->icount.dsr++;
        if (delta & UART_MSR_RI)
            port->icount.rng++;
    }

    uart->old_status = status;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_rx_pre_handler(struct mtk_uart *uart, int intrs)
{
    unsigned long base = uart->base;
    u32 tmp, lsr_status;
    if (intrs == UART_IIR_CTI) {
        /* IMPORTANT: this is a fix for HW Bug.
        * Without the function call, the RX data timeout interrupt will be
        * triggered again and again.Hence, the purpose of this function call
        * is to clear Rx data timeout interrupt
        */
        tmp = UART_READ32(UART_DMA_EN);
#if defined(ENABLE_VFIFO)
	MSG(DMA, "rx timeout: %x, %4d\n", tmp, mtk_uart_vfifo_get_counts(uart->rx_vfifo));
#endif
        //mtk_uart_dma_vfifo_rx_tasklet((unsigned long)uart);
    } else if ((intrs == UART_IIR_RLS) && !uart->read_allow(uart)) {
        tmp = UART_READ32(UART_LSR);
        MSG(DMA, "LSR=%X\n", tmp);
        lsr_status = get_uart_lsr_status(uart->nport);
	lsr_status |= tmp;
	set_uart_lsr_status(uart->nport, lsr_status);
    } else {
#if defined(ENABLE_VFIFO)
            MSG(DMA, "RX = %4d, [%4x]\n", mtk_uart_vfifo_get_counts(uart->rx_vfifo), intrs);
#endif
        }
}
/* set the modem control lines. */
void mtk_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
    struct mtk_uart *uart = (struct mtk_uart *)port;
    unsigned long base = uart->base;
    unsigned int val;

    val = UART_READ32(UART_MCR);

    if (mctrl & TIOCM_DTR)
        val |= UART_MCR_DTR;
    else
        val &= ~UART_MCR_DTR;

    if (mctrl & TIOCM_RTS)
        val |= UART_MCR_RTS;
    else
        val &= ~UART_MCR_RTS;

    if (mctrl & TIOCM_OUT1)
        val |= UART_MCR_OUT1;
    else
        val &= ~UART_MCR_OUT1;

    if (mctrl & TIOCM_OUT2)
        val |= UART_MCR_OUT2;
    else
        val &= ~UART_MCR_OUT2;

    if (mctrl & TIOCM_LOOP)
        val |= UART_MCR_LOOP;
    else
        val &= ~UART_MCR_LOOP;

    reg_sync_writel(val, UART_MCR);

    MSG(CFG, "MCR: DTR(%d), RTS(%d), OUT1(%d), OUT2(%d), LOOP(%d)\n",
        val & UART_MCR_DTR ? 1 : 0,
        val & UART_MCR_RTS ? 1 : 0,
        val & UART_MCR_OUT1 ? 1 : 0,
        val & UART_MCR_OUT2 ? 1 : 0,
        val & UART_MCR_LOOP ? 1 : 0);
}
/*---------------------------------------------------------------------------*/
/* return the current state of modem contrl inputs */
unsigned int mtk_uart_get_mctrl(struct uart_port *port)
{
    struct mtk_uart *uart = (struct mtk_uart *)port;
    unsigned long base = uart->base;
    unsigned int status;
    unsigned int result = 0;

    status = UART_READ32(UART_MSR);

    MSG(INFO, "MSR: DCD(%d), RI(%d), DSR(%d), CTS(%d)\n",
        status & UART_MSR_DCD ? 1 : 0,
        status & UART_MSR_RI ? 1 : 0,
        status & UART_MSR_DSR ? 1 : 0,
        status & UART_MSR_CTS ? 1 : 0);

    if (status & UART_MSR_DCD)
        result |= TIOCM_CAR;    /* DCD. (data carrier detect) */
    if (status & UART_MSR_RI)
        result |= TIOCM_RI;
    if (status & UART_MSR_DSR)
        result |= TIOCM_DSR;
    if (status & UART_MSR_CTS)
        result |= TIOCM_CTS;

    status = UART_READ32(UART_MCR);

    MSG(INFO, "MSR: OUT1(%d), OUT2(%d), LOOP(%d)\n",
        status & UART_MCR_OUT1 ? 1 : 0,
        status & UART_MCR_OUT2 ? 1 : 0,
        status & UART_MCR_LOOP ? 1 : 0);

    if (status & UART_MCR_OUT2)
        result |= TIOCM_OUT2;
    if (status & UART_MCR_OUT1)
        result |= TIOCM_OUT1;
    if (status & UART_MCR_LOOP)
        result |= TIOCM_LOOP;

    return result;
}
/*---------------------------------------------------------------------------*/
/* stop receiving characters
 * note: port->lock has been taken by serial core layer
 */
void mtk_uart_stop_rx(struct uart_port *port)
{
    struct mtk_uart *uart = (struct mtk_uart *)port;
    struct mtk_uart_dma *dma = &uart->dma_rx;

    MSG_FUNC_ENTRY();
    if (uart->rx_mode == UART_NON_DMA) {
        mtk_uart_disable_intrs(uart, UART_IER_ERBFI);
    } else {
#if defined(ENABLE_VFIFO)
        /* According to serial_core.c, stop_rx is to stop interrupt
         * Hence, RX received interrupt and dma interrupt is clear
         */
        mtk_uart_disable_intrs(uart, UART_IER_ERBFI);
        reg_sync_writel(VFF_INT_EN_CLR_B, VFF_INT_EN(dma->vfifo->base));
        atomic_set(&dma->free, 1);
        complete(&dma->done);
#endif
    }
    uart->rx_stop = 1;
}
/*---------------------------------------------------------------------------*/
/* control the transmission of a break signal */
void mtk_uart_break_ctl(struct uart_port *port, int break_state)
{
    struct mtk_uart *uart = (struct mtk_uart *)port;
    unsigned long base = uart->base;

    unsigned long flags;

    MSG_FUNC_ENTRY();

    spin_lock_irqsave(&port->lock, flags);

    if (break_state)
        UART_SET_BITS(UART_LCR_BREAK, UART_LCR);
    else
        UART_CLR_BITS(UART_LCR_BREAK, UART_LCR);
    dsb();
    spin_unlock_irqrestore(&port->lock, flags);
}
/*---------------------------------------------------------------------------*/
#ifdef ATE_FACTORY_ENABLE
void mtk_uart_is_ate_factory_mode(struct mtk_uart *uart)
{
    if((0 == uart->nport)&&(ATE_FACTORY_MODE == get_boot_mode()))
    {
        unsigned long base = uart->base;
        /* MD may set these bit, reset it */
        UART_CLR_BITS(UART_RX_DMA_EN|UART_TO_CNT_AUTORST|UART_TX_DMA_EN, UART_DMA_EN);
        dsb();
    }
}
#endif /* ATE_FACTORY_ENABLE */
/*---------------------------------------------------------------------------*/
void mtk_uart_enable_sleep(struct mtk_uart *uart)
{
    unsigned long base = uart->base;
    reg_sync_writel(0x1, UART_SLEEP_EN);
    printk("SLEEP_EN = 0x%x\n",UART_READ32(UART_SLEEP_EN));
}
/*---------------------------------------------------------------------------*/

void mtk_uart_init_debug_spinlock(void)
{
#ifdef ENABLE_RAW_DATA_DUMP
    spin_lock_init(&tx_history_lock);
    spin_lock_init(&rx_history_lock);
#endif
}
void reset_tx_raw_data(struct mtk_uart *uart)
{
#ifdef ENABLE_RAW_DATA_DUMP
    unsigned long flags;
    if(uart->nport==2){
	spin_lock_irqsave(&tx_history_lock, flags);
	if(!stop_update){
		curr_record++;
		curr_idx = 0;
		if(curr_record >= RECORD_NUMBER)
			curr_record = 0;
		uart_history_cnt[curr_record] = 0;
		for(curr_idx=0; curr_idx<RECORD_LENGTH; curr_idx++)
			uart_history[curr_record][curr_idx] = 0;
		curr_idx = 0;
	}
	spin_unlock_irqrestore(&tx_history_lock, flags);
    }
#endif
}
static void save_tx_raw_data(struct mtk_uart *uart,void *addr)
{
#ifdef ENABLE_RAW_DATA_DUMP
    unsigned long flags;
    if(uart->nport==2){
	spin_lock_irqsave(&tx_history_lock, flags);
	if(!stop_update){
		if(curr_idx<RECORD_LENGTH){
			uart_history[curr_record][curr_idx] = UART_READ8(addr);
			curr_idx++;
			uart_history_cnt[curr_record] = curr_idx;
		}
	}
	spin_unlock_irqrestore(&tx_history_lock, flags);
    }
#endif
}
static void reset_rx_raw_data(struct mtk_uart *uart)
{
#ifdef ENABLE_RAW_DATA_DUMP
    unsigned long flags;
    if(uart->nport==2){
		spin_lock_irqsave(&rx_history_lock, flags);
		if(!stop_update){
			curr_rx_record++;
			curr_rx_idx = 0;
			if(curr_rx_record >= RECORD_NUMBER)
				curr_rx_record = 0;
			uart_rx_history_cnt[curr_rx_record] = 0;
			for(curr_rx_idx=0; curr_rx_idx<RECORD_LENGTH; curr_rx_idx++)
				uart_rx_history[curr_rx_record][curr_rx_idx] = 0;
			curr_rx_idx = 0;
		}
		spin_unlock_irqrestore(&rx_history_lock, flags);
	}
#endif
}
static void save_rx_raw_data(struct mtk_uart *uart, const unsigned char *chars, size_t size)
{
#ifdef ENABLE_RAW_DATA_DUMP
    unsigned long flags;
    int i;
    if(uart->nport==2){
		spin_lock_irqsave(&rx_history_lock, flags);
		if(!stop_update){
			for(i=0; (curr_rx_idx<RECORD_LENGTH)&&(i<size); i++,curr_rx_idx++){
				uart_rx_history[curr_rx_record][curr_rx_idx] = chars[i];
			}
			uart_rx_history_cnt[curr_rx_record] = curr_rx_idx;
		}
		spin_unlock_irqrestore(&rx_history_lock, flags);
    }
#endif
}

void stop_log(void)
{
#ifdef ENABLE_RAW_DATA_DUMP
	unsigned long flags;
	unsigned long rx_flags;
	spin_lock_irqsave(&tx_history_lock, flags);
	spin_lock_irqsave(&rx_history_lock, rx_flags);
	stop_update = 1;
	spin_unlock_irqrestore(&rx_history_lock, rx_flags);
	spin_unlock_irqrestore(&tx_history_lock, flags);
#endif
}
EXPORT_SYMBOL(stop_log);
void dump_uart_history(void)
{
#ifdef ENABLE_RAW_DATA_DUMP
	int i,j;
	unsigned long flags;
	unsigned long rx_flags;
	int curr, rx_curr;
	spin_lock_irqsave(&tx_history_lock, flags);
	spin_lock_irqsave(&rx_history_lock, rx_flags);
	stop_update = 1;
	spin_unlock_irqrestore(&rx_history_lock, rx_flags);
	spin_unlock_irqrestore(&tx_history_lock, flags);
	curr = curr_record + 1;
	if(curr>=RECORD_NUMBER)
		curr=0;
	rx_curr = curr_rx_record + 1;
	if(rx_curr>=RECORD_NUMBER)
		rx_curr=0;

	for(i=0; i<RECORD_NUMBER; i++){
		printk("\nTX rec%03d:", i);
		for(j=0; j<uart_history_cnt[curr]; j++){
			if((j%0xF)==0)
				printk("\n");
			printk("%02x ", uart_history[curr][j]);
		}
		msleep(20);
		curr++;
		if(curr>=RECORD_NUMBER)
		curr=0;
	}
	for(i=0; i<RECORD_NUMBER; i++){
		printk("\nRX rec%03d:", i);
		for(j=0; j<uart_rx_history_cnt[rx_curr]; j++){
			if((j%0xF)==0)
				printk("\n");
			printk("%02x ", uart_rx_history[rx_curr][j]);
		}
		msleep(20);
		rx_curr++;
		if(rx_curr>=RECORD_NUMBER)
		rx_curr=0;
	}
#endif
}
EXPORT_SYMBOL(dump_uart_history);


/*---------------------------------------------------------------------------*/
void mtk_uart_save(struct mtk_uart *uart)
{
#ifdef CONFIG_PM
    unsigned long base;
    unsigned long flags;
    base = uart->base;

    //DLL may be changed by console write. To avoid this, use spinlock
    spin_lock_irqsave(&mtk_console_lock, flags);
    uart->registers.lcr = UART_READ32(UART_LCR);

    reg_sync_writel(0xbf, UART_LCR);
    uart->registers.efr = UART_READ32(UART_EFR);
    reg_sync_writel(uart->registers.lcr, UART_LCR);
    uart->registers.fcr = UART_READ32(UART_FCR_RD);

    //baudrate
    uart->registers.highspeed = UART_READ32(UART_HIGHSPEED);
    uart->registers.fracdiv_l = UART_READ32(UART_FRACDIV_L);
    uart->registers.fracdiv_m = UART_READ32(UART_FRACDIV_M);
    reg_sync_writel(uart->registers.lcr | UART_LCR_DLAB, UART_LCR);
    uart->registers.dll = UART_READ32(UART_DLL);
    uart->registers.dlh = UART_READ32(UART_DLH);
    reg_sync_writel(uart->registers.lcr, UART_LCR);
    uart->registers.sample_count = UART_READ32(UART_SAMPLE_COUNT);
    uart->registers.sample_point = UART_READ32(UART_SAMPLE_POINT);
    uart->registers.guard = UART_READ32(UART_GUARD);

    //flow control
    uart->registers.escape_en = UART_READ32(UART_ESCAPE_EN);
    uart->registers.mcr = UART_READ32(UART_MCR);
    uart->registers.ier = UART_READ32(UART_IER);

    uart->registers.rx_sel = UART_READ32(UART_RX_SEL);

    spin_unlock_irqrestore(&mtk_console_lock, flags);
#endif
}

void mtk_uart_restore(void)
{
#ifdef CONFIG_PM
    unsigned long base;
    unsigned long flags;
    struct mtk_uart *uart;

    uart = console_port;
    base = uart->base;

    mtk_uart_power_up(uart);
    spin_lock_irqsave(&mtk_console_lock, flags);
    reg_sync_writel(0xbf, UART_LCR);
    reg_sync_writel(uart->registers.efr, UART_EFR);
    reg_sync_writel(uart->registers.lcr, UART_LCR);
    reg_sync_writel(uart->registers.fcr, UART_FCR);

    //baudrate
    reg_sync_writel(uart->registers.highspeed, UART_HIGHSPEED);
    reg_sync_writel(uart->registers.fracdiv_l, UART_FRACDIV_L);
    reg_sync_writel(uart->registers.fracdiv_m, UART_FRACDIV_M);
    reg_sync_writel(uart->registers.lcr | UART_LCR_DLAB, UART_LCR);
    reg_sync_writel(uart->registers.dll, UART_DLL);
    reg_sync_writel(uart->registers.dlh, UART_DLH);
    reg_sync_writel(uart->registers.lcr, UART_LCR);
    reg_sync_writel(uart->registers.sample_count, UART_SAMPLE_COUNT);
    reg_sync_writel(uart->registers.sample_point, UART_SAMPLE_POINT);
    reg_sync_writel(uart->registers.guard, UART_GUARD);

    //flow control
    reg_sync_writel(uart->registers.escape_en, UART_ESCAPE_EN);
    reg_sync_writel(uart->registers.mcr, UART_MCR);
    reg_sync_writel(uart->registers.ier, UART_IER);

    reg_sync_writel(uart->registers.rx_sel, UART_RX_SEL);

    spin_unlock_irqrestore(&mtk_console_lock, flags);
#endif
}

void mtk_uart_switch_tx_to_gpio(struct mtk_uart *uart)
{
    printk(KERN_ERR "%s port:0x%x\n", __func__, uart->nport);
#ifdef CONFIG_PM
#ifndef CONFIG_MTK_FPGA
    switch(uart->nport){
	case 0:
	    #ifdef GPIO_UART_UTXD0_PIN
	    mt_set_gpio_out(GPIO_UART_UTXD0_PIN, GPIO_OUT_ONE);
	    mt_set_gpio_mode(GPIO_UART_UTXD0_PIN, GPIO_UART_UTXD0_PIN_M_GPIO);
	    #else
	    printk(KERN_ERR "GPIO_UART_UTXD0_PIN is not properly set\n");
	    #endif
	    break;
	case 1:
	    #ifdef GPIO_UART_UTXD1_PIN
	    mt_set_gpio_out(GPIO_UART_UTXD1_PIN, GPIO_OUT_ONE);
	    mt_set_gpio_mode(GPIO_UART_UTXD1_PIN, GPIO_UART_UTXD1_PIN_M_GPIO);
	    #else
	    printk(KERN_ERR "GPIO_UART_UTXD1_PIN is not properly set\n");
	    #endif
	    break;
	case 2:
	    #ifdef GPIO_UART_UTXD2_PIN
	    mt_set_gpio_out(GPIO_UART_UTXD2_PIN, GPIO_OUT_ONE);
	    mt_set_gpio_mode(GPIO_UART_UTXD2_PIN, GPIO_UART_UTXD2_PIN_M_GPIO);
	    #else
	    printk(KERN_ERR "GPIO_UART_UTXD2_PIN is not properly set\n");
	    #endif
	    break;
	case 3:
	    #ifdef GPIO_UART_UTXD3_PIN
	    mt_set_gpio_out(GPIO_UART_UTXD3_PIN, GPIO_OUT_ONE);
	    mt_set_gpio_mode(GPIO_UART_UTXD3_PIN, GPIO_UART_UTXD3_PIN_M_GPIO);
	    #else
	    printk(KERN_ERR "GPIO_UART_UTXD3_PIN is not properly set\n");
	    #endif
	    break;
	default:
	    break;
	}
    return;
#endif
#endif
}
/*---------------------------------------------------------------------------*/
void mtk_uart_switch_to_tx(struct mtk_uart *uart)
{
    printk(KERN_ERR "%s port:0x%x\n", __func__, uart->nport);
#ifdef CONFIG_PM
#ifndef CONFIG_MTK_FPGA
    switch(uart->nport){
	case 0:
	    #ifdef GPIO_UART_UTXD0_PIN
	    mt_set_gpio_mode(GPIO_UART_UTXD0_PIN, GPIO_UART_UTXD0_PIN_M_UTXD);
	    #else
	    printk(KERN_ERR "GPIO_UART_UTXD0_PIN is not properly set p2\n");
	    #endif
	    break;
	case 1:
	    #ifdef GPIO_UART_UTXD1_PIN
	    mt_set_gpio_mode(GPIO_UART_UTXD1_PIN, GPIO_UART_UTXD1_PIN_M_UTXD);
	    #else
	    printk(KERN_ERR "GPIO_UART_UTXD1_PIN is not properly set p2\n");
	    #endif
	    break;
	case 2:
	    #ifdef GPIO_UART_UTXD2_PIN
	    mt_set_gpio_mode(GPIO_UART_UTXD2_PIN, GPIO_UART_UTXD2_PIN_M_UTXD);
	    #else
	    printk(KERN_ERR "GPIO_UART_UTXD2_PIN is not properly set p2\n");
	    #endif
	    break;
	case 3:
	    #ifdef GPIO_UART_UTXD3_PIN
	    mt_set_gpio_mode(GPIO_UART_UTXD3_PIN, GPIO_UART_UTXD3_PIN_M_UTXD);
	    #else
	    printk(KERN_ERR "GPIO_UART_UTXD3_PIN is not properly set p3\n");
	    #endif
	    break;
	default:
	    break;
	}
    return;
#endif
#endif
}
/*---------------------------------------------------------------------------*/
void mtk_uart_switch_rx_to_gpio(struct mtk_uart *uart)
{
    printk(KERN_ERR "%s port:0x%x\n", __func__, uart->nport);
#ifdef CONFIG_PM
#ifndef CONFIG_MTK_FPGA
    switch(uart->nport){
	case 0:
	    #ifdef GPIO_UART_URXD0_PIN
	    mt_set_gpio_mode(GPIO_UART_URXD0_PIN, GPIO_UART_URXD0_PIN_M_GPIO);
	    #else
	    printk(KERN_ERR "GPIO_UART_URXD0_PIN is not properly set\n");
	    #endif
	    break;
	case 1:
	    #ifdef GPIO_UART_URXD1_PIN
	    mt_set_gpio_mode(GPIO_UART_URXD1_PIN, GPIO_UART_URXD1_PIN_M_GPIO);
	    #else
	    printk(KERN_ERR "GPIO_UART_URXD1_PIN is not properly set\n");
	    #endif
	    break;
	case 2:
	    #ifdef GPIO_UART_URXD2_PIN
	    mt_set_gpio_mode(GPIO_UART_URXD2_PIN, GPIO_UART_URXD2_PIN_M_GPIO);
	    #else
	    printk(KERN_ERR "GPIO_UART_URXD2_PIN is not properly set\n");
	    #endif
	    break;
	case 3:
	    #ifdef GPIO_UART_URXD3_PIN
	    mt_set_gpio_mode(GPIO_UART_URXD3_PIN, GPIO_UART_URXD3_PIN_M_GPIO);
	    #else
	    printk(KERN_ERR "GPIO_UART_URXD3_PIN is not properly set\n");
	    #endif
	    break;
	default:
	    break;
	}
    return;
#endif
#endif
}
/*---------------------------------------------------------------------------*/
void mtk_uart_switch_to_rx(struct mtk_uart *uart)
{
    printk(KERN_ERR "%s port:0x%x\n", __func__, uart->nport);
#ifdef CONFIG_PM
#ifndef CONFIG_MTK_FPGA
    switch(uart->nport){
	case 0:
	    #ifdef GPIO_UART_URXD0_PIN
	    mt_set_gpio_mode(GPIO_UART_URXD0_PIN, GPIO_UART_URXD0_PIN_M_URXD);
	    #else
	    printk(KERN_ERR "GPIO_UART_URXD0_PIN is not properly set p2\n");
	    #endif
	    break;
	case 1:
	    #ifdef GPIO_UART_URXD1_PIN
	    mt_set_gpio_mode(GPIO_UART_URXD1_PIN, GPIO_UART_URXD1_PIN_M_URXD);
	    #else
	    printk(KERN_ERR "GPIO_UART_URXD1_PIN is not properly set p2\n");
	    #endif
	    break;
	case 2:
	    #ifdef GPIO_UART_URXD2_PIN
	    mt_set_gpio_mode(GPIO_UART_URXD2_PIN, GPIO_UART_URXD2_PIN_M_URXD);
	    #else
	    printk(KERN_ERR "GPIO_UART_URXD2_PIN is not properly set p2\n");
	    #endif
	    break;
	case 3:
	    #ifdef GPIO_UART_URXD3_PIN
	    mt_set_gpio_mode(GPIO_UART_URXD3_PIN, GPIO_UART_URXD3_PIN_M_URXD);
	    #else
	    printk(KERN_ERR "GPIO_UART_URXD3_PIN is not properly set p2\n");
	    #endif
	    break;
	default:
	    break;
	}
    return;
#endif
#endif
}
/*---------------------------------------------------------------------------*/
void mtk_uart_enable_dpidle(struct mtk_uart *uart)
{
//FIX-ME early porting
#ifndef CONFIG_MTK_FPGA
    enable_dpidle_by_bit(uart->setting->pll_id);
	enable_soidle_by_bit(uart->setting->pll_id);
#endif
    return;
}
/*---------------------------------------------------------------------------*/
void mtk_uart_disable_dpidle(struct mtk_uart *uart)
{
//FIX-ME early porting
#ifndef CONFIG_MTK_FPGA
    disable_dpidle_by_bit(uart->setting->pll_id);
	disable_soidle_by_bit(uart->setting->pll_id);
#endif
    return;
}
/*---------------------------------------------------------------------------*/
int  mtk_uart_plat_info_query(const char str[])
{
    return 0;
}