summaryrefslogtreecommitdiff
path: root/ANDROID_3.4.5/drivers/mmc/host/mmc_atsmb.c
blob: 150b7b12a2a172ddf271c6c9d51a545fc07f444a (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
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
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
/*++
linux/drivers/mmc/host/mmc_atsmb.c

Copyright (c) 2008  WonderMedia Technologies, Inc.

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, see <http://www.gnu.org/licenses/>.

WonderMedia Technologies, Inc.
10F, 529, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C.
--*/
 
//#include <linux/config.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/blkdev.h>
#include <linux/mmc/host.h>
#include <linux/mmc/card.h>
#include <linux/mmc/sd.h>
#include <linux/mmc/mmc.h>
#include <linux/mmc/sdio.h>
#include <linux/completion.h>
#include <linux/pagemap.h>
#include <linux/dma-mapping.h>
#include <asm/dma.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/memory.h>
#include <mach/hardware.h>
#include <asm/scatterlist.h>
#include <asm/sizes.h>
#include "mmc_atsmb.h"
//#include <mach/multicard.h>
#include <mach/irqs.h>
#include <linux/regulator/consumer.h>

#define mprintk  

#if 0
#define DBG(host, fmt, args...)	\
	printk("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
#endif
#define ATSMB_TIMEOUT_TIME (HZ*2)


static	struct delayed_work	mmc_work;

static int g_atsmb_regulator;
static struct regulator *atsmb_regulator;


//add by jay,for modules support
static u64 wmt_sdmmc_dma_mask = 0xffffffffUL;
static struct resource wmt_sdmmc_resources[] = {
	[0] = {
		.start	= SD0_SDIO_MMC_BASE_ADDR,
		.end	= (SD0_SDIO_MMC_BASE_ADDR + 0x3FF),
		.flags	= IORESOURCE_MEM,
	},
	[1] = {
		.start	= IRQ_SDC0,
		.end	= IRQ_SDC0,
		.flags	= IORESOURCE_IRQ,
	},
	[2] = {
		.start  = IRQ_SDC0_DMA,
		.end    = IRQ_SDC0_DMA,
		.flags  = IORESOURCE_IRQ,
	},
	/*2008/10/6 RichardHsu-s*/
	 [3] = {
	     .start    = IRQ_PMC_WAKEUP,
	     .end      = IRQ_PMC_WAKEUP,
	     .flags    = IORESOURCE_IRQ,
	 },
	 /*2008/10/6 RichardHsu-e*/
};

struct kobject *atsmb_kobj;
struct mmc_host *mmc_host_attr  = NULL;
//
static void atsmb_release(struct device * dev) {}

/*#ifdef CONFIG_MMC_DEBUG*/
#define MORE_INFO
#if 0
#define DBG(x...)	printk(KERN_ALERT x)
#define DBGR(x...)	do { } while (0)
#else
#define DBG(x...)	do { } while (0)
#define DBGR(x...)	do { } while (0)
#endif

/* when Read CRC error occur, and clear read CRC error by software reset.*/
void atsmb_copy_reg(int direct)
{
	static u8 CTL, CMD_IDX, RSP_TYPE, BUS_MODE, INT_MASK_0, INT_MASK_1, SD_STS_0, SD_STS_1, SD_STS_2, SD_STS_3;
	static u8 EXT_BUS_MODE, EXT_CTL_1, EXT_CTL_2, MAN_TUNE_VAL, SD_WRI_TUNE;
	static u8 RSP_0, RSP_1, RSP_2, RSP_3, RSP_4, RSP_5, RSP_6, RSP_7;
	static u8 RSP_8, RSP_9, RSP_10, RSP_11, RSP_12, RSP_13, RSP_14, RSP_15;
	static u8 RSP_TOUT, CLK_SEL, EXT_CTL;
	static u16 BLK_LEN, BLK_CNT, SHDW_BLKLEN, TIMER_VAL, CTL2;
	static u32 CMD_ARG, CURBLK_CNT;

	/*direct 0: copy register to memory; 1: copy memory to register.*/
	if (direct == 0) {
		CTL = *ATSMB0_CTL;
		CMD_IDX = *ATSMB0_CMD_IDX;
		RSP_TYPE = *ATSMB0_RSP_TYPE;
		CMD_ARG = *ATSMB0_CMD_ARG;
		BUS_MODE = *ATSMB0_BUS_MODE;
		EXT_BUS_MODE = *ATSMB0_EXT_BUS_MODE;
		CTL2 = *ATSMB0_CTL2;
		BLK_LEN = *ATSMB0_BLK_LEN;
		BLK_CNT = *ATSMB0_BLK_CNT;
		RSP_0 = *ATSMB0_RSP_0;
		RSP_1 = *ATSMB0_RSP_1;
		RSP_2 = *ATSMB0_RSP_2;
		RSP_3 = *ATSMB0_RSP_3;
		RSP_4 = *ATSMB0_RSP_4;
		RSP_5 = *ATSMB0_RSP_5;
		RSP_6 = *ATSMB0_RSP_6;
		RSP_7 = *ATSMB0_RSP_7;
		RSP_8 = *ATSMB0_RSP_8;
		RSP_9 = *ATSMB0_RSP_9;
		RSP_10 = *ATSMB0_RSP_10;
		RSP_11 = *ATSMB0_RSP_11;
		RSP_12 = *ATSMB0_RSP_12;
		RSP_13 = *ATSMB0_RSP_13;
		RSP_14 = *ATSMB0_RSP_14;
		RSP_15 = *ATSMB0_RSP_15;
		CURBLK_CNT = *ATSMB0_CURBLK_CNT;
		INT_MASK_0 = *ATSMB0_INT_MASK_0;
		INT_MASK_1 = *ATSMB0_INT_MASK_1;
		SD_STS_0 = *ATSMB0_SD_STS_0;
		SD_STS_1 = *ATSMB0_SD_STS_1;
		SD_STS_2 = *ATSMB0_SD_STS_2;
		SD_STS_3 = *ATSMB0_SD_STS_3;
		RSP_TOUT = *ATSMB0_RSP_TOUT;
		CLK_SEL = *ATSMB0_CLK_SEL;
		EXT_CTL = *ATSMB0_EXT_CTL;
		EXT_CTL_1 = *ATSMB0_EXT_CTL_1;
		EXT_CTL_2 = *ATSMB0_EXT_CTL_2;
		SHDW_BLKLEN = *ATSMB0_SHDW_BLKLEN;
		MAN_TUNE_VAL = *ATSMB0_MAN_TUNE_VAL;
		SD_WRI_TUNE = *ATSMB0_SD_WRI_TUNE;
		TIMER_VAL = *ATSMB0_TIMER_VAL;
	} else {
		*ATSMB0_CTL = CTL;
		*ATSMB0_CMD_IDX = CMD_IDX;
		*ATSMB0_RSP_TYPE = RSP_TYPE;
		*ATSMB0_CMD_ARG = CMD_ARG;
		*ATSMB0_BUS_MODE = BUS_MODE;
		*ATSMB0_EXT_BUS_MODE = EXT_BUS_MODE;
		*ATSMB0_CTL2 = CTL2;
		*ATSMB0_BLK_LEN = BLK_LEN;
		*ATSMB0_BLK_CNT = BLK_CNT;
		*ATSMB0_RSP_0 = RSP_0;
		*ATSMB0_RSP_1 = RSP_1;
		*ATSMB0_RSP_2 = RSP_2;
		*ATSMB0_RSP_3 = RSP_3;
		*ATSMB0_RSP_4 = RSP_4;
		*ATSMB0_RSP_5 = RSP_5;
		*ATSMB0_RSP_6 = RSP_6;
		*ATSMB0_RSP_7 = RSP_7;
		*ATSMB0_RSP_8 = RSP_8;
		*ATSMB0_RSP_9 = RSP_9;
		*ATSMB0_RSP_10 = RSP_10;
		*ATSMB0_RSP_11 = RSP_11;
		*ATSMB0_RSP_12 = RSP_12;
		*ATSMB0_RSP_13 = RSP_13;
		*ATSMB0_RSP_14 = RSP_14;
		*ATSMB0_RSP_15 = RSP_15;
		*ATSMB0_CURBLK_CNT = CURBLK_CNT;
		*ATSMB0_INT_MASK_0 = INT_MASK_0;
		*ATSMB0_INT_MASK_1 = INT_MASK_1;
		*ATSMB0_SD_STS_0 = SD_STS_0;
		*ATSMB0_SD_STS_1 = SD_STS_1;
		*ATSMB0_SD_STS_2 = SD_STS_2;
		*ATSMB0_SD_STS_3 = SD_STS_3;
		*ATSMB0_RSP_TOUT = RSP_TOUT;
		*ATSMB0_CLK_SEL = CLK_SEL;
		*ATSMB0_EXT_CTL = EXT_CTL;
		*ATSMB0_EXT_CTL_1 = EXT_CTL_1;
		*ATSMB0_EXT_CTL_2 = EXT_CTL_2;
		*ATSMB0_SHDW_BLKLEN = SHDW_BLKLEN;
		*ATSMB0_MAN_TUNE_VAL = MAN_TUNE_VAL;
		*ATSMB0_SD_WRI_TUNE = SD_WRI_TUNE;
		*ATSMB0_TIMER_VAL = TIMER_VAL;
	}
}

#if 0
void atsmb_dump_reg(void)
{
	u8 CTL, CMD_IDX, RSP_TYPE, BUS_MODE, INT_MASK_0, INT_MASK_1, SD_STS_0, SD_STS_1, SD_STS_2, SD_STS_3;
	u8 EXT_BUS_MODE, EXT_CTL_1, EXT_CTL_2, MAN_TUNE_VAL, SD_WRI_TUNE;
	u8 RSP_0, RSP_1, RSP_2, RSP_3, RSP_4, RSP_5, RSP_6, RSP_7;
	u8 RSP_8, RSP_9, RSP_10, RSP_11, RSP_12, RSP_13, RSP_14, RSP_15;
	u8 RSP_TOUT, CLK_SEL, EXT_CTL;
	u16 BLK_LEN, BLK_CNT, SHDW_BLKLEN, TIMER_VAL, CTL2;    
	u32 CMD_ARG, PDMA_GCR, PDMA_IER, PDMA_ISR, PDMA_DESPR, PDMA_RBR, PDMA_DAR, PDMA_BAR, PDMA_CPR, PDMA_CCR;
	u32 CURBLK_CNT;

	CTL = *ATSMB0_CTL;
	CMD_IDX = *ATSMB0_CMD_IDX;
	RSP_TYPE = *ATSMB0_RSP_TYPE;
	CMD_ARG = *ATSMB0_CMD_ARG;
	BUS_MODE = *ATSMB0_BUS_MODE;
	EXT_BUS_MODE = *ATSMB0_EXT_BUS_MODE;
	CTL2 = *ATSMB0_CTL2;
	BLK_LEN = *ATSMB0_BLK_LEN;
	BLK_CNT = *ATSMB0_BLK_CNT;
	RSP_0 = *ATSMB0_RSP_0;
	RSP_1 = *ATSMB0_RSP_1;
	RSP_2 = *ATSMB0_RSP_2;
	RSP_3 = *ATSMB0_RSP_3;
	RSP_4 = *ATSMB0_RSP_4;
	RSP_5 = *ATSMB0_RSP_5;
	RSP_6 = *ATSMB0_RSP_6;
	RSP_7 = *ATSMB0_RSP_7;
	RSP_8 = *ATSMB0_RSP_8;
	RSP_9 = *ATSMB0_RSP_9;
	RSP_10 = *ATSMB0_RSP_10;
	RSP_11 = *ATSMB0_RSP_11;
	RSP_12 = *ATSMB0_RSP_12;
	RSP_13 = *ATSMB0_RSP_13;
	RSP_14 = *ATSMB0_RSP_14;
	RSP_15 = *ATSMB0_RSP_15;
	CURBLK_CNT = *ATSMB0_CURBLK_CNT;
	INT_MASK_0 = *ATSMB0_INT_MASK_0;
	INT_MASK_1 = *ATSMB0_INT_MASK_1;
	SD_STS_0 = *ATSMB0_SD_STS_0;
	SD_STS_1 = *ATSMB0_SD_STS_1;
	SD_STS_2 = *ATSMB0_SD_STS_2;
	SD_STS_3 = *ATSMB0_SD_STS_3;
	RSP_TOUT = *ATSMB0_RSP_TOUT;
	CLK_SEL = *ATSMB0_CLK_SEL;
	EXT_CTL = *ATSMB0_EXT_CTL;
	EXT_CTL_1 = *ATSMB0_EXT_CTL_1;
	EXT_CTL_2 = *ATSMB0_EXT_CTL_2;
	SHDW_BLKLEN = *ATSMB0_SHDW_BLKLEN;
	MAN_TUNE_VAL = *ATSMB0_MAN_TUNE_VAL;
	SD_WRI_TUNE = *ATSMB0_SD_WRI_TUNE;
	TIMER_VAL = *ATSMB0_TIMER_VAL;

	PDMA_GCR = *ATSMB0_PDMA_GCR;
	PDMA_IER = *ATSMB0_PDMA_IER;
	PDMA_ISR = *ATSMB0_PDMA_ISR;
	PDMA_DESPR = *ATSMB0_PDMA_DESPR;
	PDMA_RBR = *ATSMB0_PDMA_RBR;
	PDMA_DAR = *ATSMB0_PDMA_DAR;
	PDMA_BAR = *ATSMB0_PDMA_BAR;
	PDMA_CPR = *ATSMB0_PDMA_CPR;
	PDMA_CCR = *ATSMB0_PDMA_CCR;

    DBGR("\n+---------------------------Registers----------------------------+\n");

	DBGR("%16s = 0x%8x  |", "CTL", CTL);
	DBGR("%16s = 0x%8x\n", "CMD_IDX", CMD_IDX);

	DBGR("%16s = 0x%8x  |", "RSP_TYPE", RSP_TYPE);
	DBGR("%16s = 0x%8x\n", "CMD_ARG", CMD_ARG);

	DBGR("%16s = 0x%8x  |", "BUS_MODE", BUS_MODE);
	DBGR("%16s = 0x%8x\n", "EXT_BUS_MODE", EXT_BUS_MODE);

	DBGR("%16s = 0x%8x  |", "CTL2", CTL2);
	DBGR("%16s = 0x%8x\n", "BLK_LEN", BLK_LEN);

	DBGR("%16s = 0x%8x  |", "BLK_CNT", BLK_CNT);
	DBGR("%16s = 0x%8x\n", "RSP_0", RSP_0);

	DBGR("%16s = 0x%8x  |", "RSP_1", RSP_1);
	DBGR("%16s = 0x%8x\n", "RSP_2", RSP_2);

	DBGR("%16s = 0x%8x  |", "RSP_3", RSP_3);
	DBGR("%16s = 0x%8x\n", "RSP_4", RSP_4);

	DBGR("%16s = 0x%8x  |", "RSP_5", RSP_5);
	DBGR("%16s = 0x%8x\n", "RSP_6", RSP_6);

	DBGR("%16s = 0x%8x  |", "RSP_7", RSP_7);
	DBGR("%16s = 0x%8x\n", "RSP_8", RSP_8);

	DBGR("%16s = 0x%8x  |", "RSP_9", RSP_9);
	DBGR("%16s = 0x%8x\n", "RSP_10", RSP_10);

	DBGR("%16s = 0x%8x  |", "RSP_11", RSP_11);
	DBGR("%16s = 0x%8x\n", "RSP_12", RSP_12);

	DBGR("%16s = 0x%8x  |", "RSP_13", RSP_13);
	DBGR("%16s = 0x%8x\n", "RSP_14", RSP_14);

    DBGR("%16s = 0x%8x\n", "RSP_15", RSP_15);

	DBGR("%16s = 0x%8x  |", "CURBLK_CNT", CURBLK_CNT);
	DBGR("%16s = 0x%8x\n", "INT_MASK_0", INT_MASK_0);

	DBGR("%16s = 0x%8x  |", "INT_MASK_1", INT_MASK_1);
	DBGR("%16s = 0x%8x\n", "SD_STS_0", SD_STS_0);

	DBGR("%16s = 0x%8x  |", "SD_STS_1", SD_STS_1);
	DBGR("%16s = 0x%8x\n", "SD_STS_2", SD_STS_2);

	DBGR("%16s = 0x%8x  |", "SD_STS_3", SD_STS_3);
	DBGR("%16s = 0x%8x\n", "RSP_TOUT", RSP_TOUT);

	DBGR("%16s = 0x%8x  |", "CLK_SEL", CLK_SEL);
	DBGR("%16s = 0x%8x\n", "EXT_CTL", EXT_CTL);

	DBGR("%16s = 0x%8x  |", "EXT_CTL_1", EXT_CTL_1);
	DBGR("%16s = 0x%8x\n", "EXT_CTL_2", EXT_CTL_2);

	DBGR("%16s = 0x%8x  |", "SHDW_BLKLEN", SHDW_BLKLEN);
	DBGR("%16s = 0x%8x\n", "MAN_TUNE_VAL", MAN_TUNE_VAL);

	DBGR("%16s = 0x%8x  |", "SD_WRI_TUNE", SD_WRI_TUNE);
	DBGR("%16s = 0x%8x\n", "TIMER_VAL", TIMER_VAL);

	DBGR("%16s = 0x%8x  |", "PDMA_GCR", PDMA_GCR);
	DBGR("%16s = 0x%8x\n", "PDMA_IER", PDMA_IER);
    
	DBGR("%16s = 0x%8x  |", "PDMA_ISR", PDMA_ISR);
	DBGR("%16s = 0x%8x\n", "PDMA_DESPR", PDMA_DESPR);

    DBGR("%16s = 0x%8x  |", "PDMA_RBR", PDMA_RBR);
	DBGR("%16s = 0x%8x\n", "PDMA_DAR", PDMA_DAR);

    DBGR("%16s = 0x%8x  |", "PDMA_BAR", PDMA_BAR);
	DBGR("%16s = 0x%8x\n", "PDMA_CPR", PDMA_CPR);

    DBGR("%16s = 0x%8x  |", "PDMA_CCR", PDMA_CCR);
	DBGR("\n+----------------------------------------------------------------+\n");
}
#else
void atsmb_dump_reg(void) {}
#endif

unsigned int fmax0 = 515633;
unsigned int MMC0_DRIVER_VERSION;
int SD0_function = 0; /*0: normal SD/MMC card reader , 1: internal SDIO wifi*/
int SDXC0_function; 
static int SD0_detect_pol = 0;
static int SD0_detect_pulldown = 0;
static int SD0_speed = 0;
//add by kevin guan
int SD0_ro_disable = 0;
    
int SCC_ID(void){
	unsigned short val;
    
	val = REG16_VAL(SYSTEM_CFG_CTRL_BASE_ADDR + 0x2);
	return val;
}

int get_chip_version(void) /*2008/05/01 janshiue modify for A1 chip*/
{
	u32 tmp;
    
	tmp = REG32_VAL(SYSTEM_CFG_CTRL_BASE_ADDR);
	tmp = ((tmp & 0xF00) >> 4) + 0x90 + ((tmp & 0xFF) - 1);
	return tmp;
}

void get_driver_version(void)
{
	if (SCC_ID() == 0x3426) {
		if (get_chip_version() < 0xA1)
			MMC0_DRIVER_VERSION = MMC_DRV_3426_A0;
		else if (get_chip_version() == 0xA1)
			MMC0_DRIVER_VERSION = MMC_DRV_3426_A1;
		else
			MMC0_DRIVER_VERSION = MMC_DRV_3426_A2;
	} else if (SCC_ID() == 0x3437) {
		if (get_chip_version() < 0xA1)
			MMC0_DRIVER_VERSION = MMC_DRV_3437_A0;
		else
			MMC0_DRIVER_VERSION = MMC_DRV_3437_A1;
	} else if (SCC_ID() == 0x3429) {
		MMC0_DRIVER_VERSION = MMC_DRV_3429;
	} else if (SCC_ID() == 0x3451) {
		if (get_chip_version() < 0xA1)
			MMC0_DRIVER_VERSION = MMC_DRV_3451_A0;
	} else if (SCC_ID() == 0x3465) {
		MMC0_DRIVER_VERSION = MMC_DRV_3465;
	} else if (SCC_ID() == 0x3445) {
		MMC0_DRIVER_VERSION = MMC_DRV_3445;
	} else if (SCC_ID() == 0x3481) {
		MMC0_DRIVER_VERSION = MMC_DRV_3481;
	} else if (SCC_ID() == 0x3498) {
		MMC0_DRIVER_VERSION = MMC_DRV_3498; 
	}
}
/*2008/10/6 RichardHsu-e*/

/**********************************************************************
Name  	    : atsmb_alloc_desc
Function    : To config PDMA descriptor setting.
Calls       :
Called by   :
Parameter   :
Author 	    : Janshiue Wu
History	    :
***********************************************************************/
static inline int atsmb_alloc_desc(struct atsmb_host *host,
									unsigned int	bytes)
{
	void	*DescPool = NULL;
	DBG("[%s] s\n",__func__);

	DescPool = dma_alloc_coherent(host->mmc->parent, bytes, &(host->DescPhyAddr), GFP_KERNEL);
	if (!DescPool) {
		DBG("can't allocal desc pool=%8X %8X\n", DescPool, host->DescPhyAddr);
		DBG("[%s] e1\n",__func__);
		return -1;
	}
	DBG("allocal desc pool=%8X %8X\n", DescPool, host->DescPhyAddr);
	host->DescVirAddr = (unsigned long *)DescPool;
	host->DescSize = bytes;
	DBG("[%s] e2\n",__func__);
	return 0;
}

/**********************************************************************
Name  	    : atsmb_config_desc
Function    : To config PDMA descriptor setting.
Calls       :
Called by   :
Parameter   :
Author 	    : Janshiue Wu
History	    :
***********************************************************************/
static inline void atsmb_config_desc(unsigned long *DescAddr,
									unsigned long *BufferAddr,
									unsigned long Blk_Cnt)
{

	int i = 0 ;
	unsigned long *CurDes = DescAddr;
	DBG("[%s] s\n",__func__);

	/*	the first Descriptor store for 1 Block data
	 *	(512 bytes)
	 */
	for (i = 0 ; i < 3 ; i++) {
		atsmb_init_short_desc(CurDes, 0x80, BufferAddr, 0);
		BufferAddr += 0x20;
		CurDes += sizeof(struct SD_PDMA_DESC_S)/4;
	}
	if (Blk_Cnt > 1) {
		atsmb_init_long_desc(CurDes, 0x80, BufferAddr, CurDes + (sizeof(struct SD_PDMA_DESC_L)/4), 0);
		BufferAddr += 0x20;
		CurDes +=  sizeof(struct SD_PDMA_DESC_L)/4;
		/*	the following Descriptor store the rest Blocks data
		 *	(Blk_Cnt - 1) * 512 bytes
		 */
		atsmb_init_long_desc(CurDes, (Blk_Cnt - 1) * 512,
			BufferAddr, CurDes + (sizeof(struct SD_PDMA_DESC_L)/4), 1);
	} else {
		atsmb_init_long_desc(CurDes, 0x80, BufferAddr, CurDes + (sizeof(struct SD_PDMA_DESC_L)/4), 1);
	}
	DBG("[%s] e\n",__func__);
}
/**********************************************************************
Name  	 : atsmb_config_dma
Function    : To set des/src address, byte count to transfer, and DMA channel settings,
			 and DMA ctrl. register.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static inline void atsmb_config_dma(unsigned long config_dir,
									unsigned long dma_mask,
									struct atsmb_host *host)
{

    DBG("[%s] s\n",__func__);
	/* Enable DMA */
	*ATSMB0_PDMA_GCR = SD_PDMA_GCR_DMA_EN;
	*ATSMB0_PDMA_GCR = SD_PDMA_GCR_SOFTRESET;
	*ATSMB0_PDMA_GCR = SD_PDMA_GCR_DMA_EN;
	/*open interrupt*/
	*ATSMB0_PDMA_IER = SD_PDMA_IER_INT_EN;
	/*Make sure host could co-work with DMA*/
	*ATSMB0_SD_STS_2 |= ATSMB_CLK_FREEZ_EN;

	/*Set timer timeout value*/
	/*If clock is 390KHz*/
	if (host->current_clock < 400000)
		*ATSMB0_TIMER_VAL = 0x200;		/*1024*512*(1/390K) second*/
	else
		*ATSMB0_TIMER_VAL = 0x1fff;		/*why not to be 0xffff?*/
	
	/*clear all DMA INT status for safety*/
	*ATSMB0_PDMA_ISR |= SD_PDMA_IER_INT_STS;

	/* hook desc */
	*ATSMB0_PDMA_DESPR = host->DescPhyAddr;
	if (config_dir == DMA_CFG_WRITE)
		*ATSMB0_PDMA_CCR &= SD_PDMA_CCR_IF_to_peripheral;
	else
		*ATSMB0_PDMA_CCR |= SD_PDMA_CCR_peripheral_to_IF;

	host->DmaIntMask = dma_mask; /*save success event*/

	*ATSMB0_PDMA_CCR |= SD_PDMA_CCR_RUN;
	DBG("[%s] e\n",__func__);
}

/**********************************************************************
Name  	 : atsmb_set_dma_end
Function    : To set all descriptor end bit for removing card when PDMA 
		don't transfer data done. let PDMA transfer done
Calls		:
Called by	:
Parameter :
Author 	 : Eason Chien
History	: 2013/05/16
***********************************************************************/
void atsmb_set_dma_end(unsigned long *DescAddr, unsigned int bytes)
{
	int i = 0;
	struct SD_PDMA_DESC_S *CurDes_S;
	CurDes_S = (struct SD_PDMA_DESC_S *) DescAddr;
	DBG("[%s] s\n", __func__);
	DBG("[%s] byte = %d, size of %d\n", __func__, bytes, sizeof(struct SD_PDMA_DESC_S));

	CurDes_S += 255;

	for (i = 255; i >=0 ;i--) {
		/* Set end bit of all descriptor and let PDMA finish as soon as possible */
		CurDes_S->end = 1;
		DBG("[%s] %d CurDes 0x%x, 0x%x, e=%x\n", __func__, i, CurDes_S, *CurDes_S);
		CurDes_S -= 1;
	}
	DBG("[%s] e\n", __func__);
}

/**********************************************************************
Name  	 : atsmb_prep_cmd
Function    :
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static inline void atsmb_prep_cmd(struct atsmb_host *host,
									u32 opcode,
									u32 arg,
									unsigned int flags,
									u16  blk_len,
									u16  blk_cnt,
									unsigned char int_maks_0,
									unsigned char int_mask_1,
									unsigned char cmd_type,
									unsigned char op)
{
	DBG("[%s] s\n",__func__);
    DBG("[%s] s opcode = %u arg = 0x%x flags = 0x%x\n",__func__,opcode,arg,flags);
	/*set cmd operation code and arguments.*/
	host->opcode = opcode;
	*ATSMB0_CMD_IDX = opcode; /* host->opcode is set for further use in ISR.*/
	*ATSMB0_CMD_ARG = arg;

#if 0   /* Fixme to support SPI mode, James Tian*/
    if ((flags && MMC_RSP_NONE) == MMC_RSP_NONE)
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R0;
    else if ((flags && MMC_RSP_R1) == MMC_RSP_R1)
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R1;
    else if ((flags && MMC_RSP_R1B) == MMC_RSP_R1B)
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R1b;
    else if ((flags && MMC_RSP_R2) == MMC_RSP_R2)
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R2;
    else if ((flags && MMC_RSP_R3) == MMC_RSP_R3)
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R3;
    else if ((flags && MMC_RSP_R6) == MMC_RSP_R6)
        *ATSMB0_RSP_TYPE = ((opcode != SD_SEND_IF_COND) ? ATMSB_TYPE_R6 : ATMSB_TYPE_R7);
    else
		*ATSMB0_RSP_TYPE = ATMSB_TYPE_R0;
#endif

#if 1
	/*set cmd response type*/
	switch (flags) {
	case MMC_RSP_NONE | MMC_CMD_AC:
	case MMC_RSP_NONE | MMC_CMD_BC:
		*ATSMB0_RSP_TYPE = ATMSB_TYPE_R0;
		break;
	case MMC_RSP_R1 | MMC_CMD_ADTC:
	case MMC_RSP_R1 | MMC_CMD_AC:
		*ATSMB0_RSP_TYPE = ATMSB_TYPE_R1;
		break;
	case MMC_RSP_R1B | MMC_CMD_AC:
		*ATSMB0_RSP_TYPE = ATMSB_TYPE_R1b;
		break;
	case MMC_RSP_R2 | MMC_CMD_BCR:
	case MMC_RSP_R2 | MMC_CMD_AC:
		*ATSMB0_RSP_TYPE = ATMSB_TYPE_R2;
		break;
	case MMC_RSP_R3 | MMC_CMD_BCR:
		*ATSMB0_RSP_TYPE = ATMSB_TYPE_R3;
		break;
	case MMC_RSP_R6 | MMC_CMD_BCR: /*MMC_RSP_R6 = MMC_RSP_R7.*/
		*ATSMB0_RSP_TYPE = ((opcode != SD_SEND_IF_COND) ?
			ATMSB_TYPE_R6 : ATMSB_TYPE_R7);
		break;
    case MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC:
    case MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC:
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R5;
		break;
    case MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR:
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R4;
		break;
    default:
		*ATSMB0_RSP_TYPE = ATMSB_TYPE_R0;
		break;
	}
#endif
    /*SDIO cmd 52 , 53*/
    if ((opcode == SD_IO_RW_DIRECT) ||
        (opcode == SD_IO_RW_EXTENDED)) {
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R5;
        *ATSMB0_RSP_TYPE |= BIT6;
    }
    /*SDIO cmd 5*/
    if ((opcode == SD_IO_SEND_OP_COND) && 
        ((flags & (MMC_RSP_PRESENT|
                  MMC_RSP_136|
                  MMC_RSP_CRC|
                  MMC_RSP_BUSY|
                  MMC_RSP_OPCODE)) == MMC_RSP_R4)) {
        *ATSMB0_RSP_TYPE = ATMSB_TYPE_R4;
        *ATSMB0_RSP_TYPE |= BIT6;
    }
    
	/*reset Response FIFO*/
	*ATSMB0_CTL |= 0x08;

	/* SD Host enable Clock */
    *ATSMB0_BUS_MODE |= ATSMB_CST;    

	/*Set Cmd-Rsp Timeout to be maximum value.*/
	*ATSMB0_RSP_TOUT = 0xFE;

	/*clear all status registers for safety*/
	*ATSMB0_SD_STS_0 |= 0xff;
	*ATSMB0_SD_STS_1 |= 0xff;
	*ATSMB0_SD_STS_2 |= 0xff;
	//*ATSMB0_SD_STS_2 |= 0x7f;
	*ATSMB0_SD_STS_3 |= 0xff;

	//set block length and block count for cmd requesting data
	*ATSMB0_BLK_LEN &=~(0x0fff);
	*ATSMB0_BLK_LEN |= blk_len;
	//*ATSMB0_SHDW_BLKLEN = blk_len;
	*ATSMB0_BLK_CNT = blk_cnt;


	*ATSMB0_INT_MASK_0 |= int_maks_0;
	*ATSMB0_INT_MASK_1 |= int_mask_1;

	//Set Auto stop for Multi-block access
	if(cmd_type == 3 || cmd_type == 4) {
		//auto stop command set.
		*ATSMB0_EXT_CTL |= 0x01;

/*
 * Enable transaction abort.
 * When CRC error occurs, CMD 12 would be automatically issued.
 * That is why we cannot enable R/W CRC error INTs.
 * If we enable CRC error INT, we would handle this INT in ISR and then issue CMD 12 via software.
 */
/* Don't auto issue stop command when write command error,
* because auto stop command can freezs PDMA. msut let PDMA transfer done.
* 2013/05/15 by Eason
*/
		if (cmd_type == 4)
			*ATSMB0_BLK_LEN |= 0x0800; 
	}

	/*Set read or write*/
	if (op == 1)
		*ATSMB0_CTL &=  ~(0x04);
	else if (op == 2)
		*ATSMB0_CTL |=  0x04;

	/*for Non data access command, command type is 0.*/
	*ATSMB0_CTL &= 0x0F;
	*ATSMB0_CTL |= (cmd_type<<4);
	DBG("[%s] e\n",__func__);
}

static inline void atsmb_issue_cmd(void)
{
	*ATSMB0_CTL |= ATSMB_START;
	wmb();
}
/**********************************************************************
Name  	 : atsmb_request_end
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static void
atsmb_request_end(struct atsmb_host *host, struct mmc_request *mrq)
{
	DBG("[%s] s\n",__func__);
	/*
	 * Need to drop the host lock here; mmc_request_done may call
	 * back into the driver...
	 */
	//kevin delete spin lock
	//spin_unlock(&host->lock);
	/*DBG("100");*/
	mmc_request_done(host->mmc, mrq);
	/*DBG("101\n");*/
    //kevin delete spin lock
	//spin_lock(&host->lock);
	DBG("[%s] e\n",__func__);
}

/**********************************************************************
Name  	 : atsmb_data_done
Function    :
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
void atsmb_wait_done(void *data)
{
	struct atsmb_host *host = (struct atsmb_host *) data;
	DBG("[%s] s\n",__func__);

	WARN_ON(host->done_data == NULL);
	complete(host->done_data);
	host->done_data = NULL;
	host->done = NULL;
	DBG("[%s] e\n",__func__);
}

/**********************************************************************
Name  	 : atsmb_start_data
Function    : If we start data, there must be only four cases.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static void atsmb_start_data(struct atsmb_host *host)
{
	DECLARE_COMPLETION(complete);
	unsigned char cmd_type = 0;
	unsigned char op = 0;	/*0: non-operation; 1:read; 2: write*/
	unsigned char mask_0 = 0;
	unsigned char mask_1 = 0;
	unsigned long dma_mask = 0;

	struct mmc_data *data = host->data;
	struct mmc_command *cmd = host->cmd;

	struct scatterlist *sg = NULL;
	unsigned int sg_len = 0;

	unsigned int total_blks = 0;		/*total block number to transfer*/
	u32 card_addr = 0;
	unsigned long dma_len = 0;
	unsigned long total_dma_len = 0;
	dma_addr_t dma_phy = 0;			/* physical address used for DMA*/
	unsigned int dma_times = 0;  /*times dma need to transfer*/
	unsigned int dma_loop = 0;
	unsigned int sg_num = 0;
	int loop_cnt = 10000;
	unsigned int sg_transfer_len = 0; /*record each time dma transfer sg length */

	DBG("[%s] s\n",__func__);
	data->bytes_xfered = 0;
	cmd->error = 0;
	data->error = 0;

	/*for loop*/
	sg = data->sg;
	sg_len = data->sg_len;

	dma_times = (sg_len / MAX_DESC_NUM);
	if (sg_len % MAX_DESC_NUM)
		dma_times++;
	DBG("dma_times = %d  sg_len = %d sg = %x\n", dma_times, sg_len, sg);
	card_addr = cmd->arg; /*may be it is block-addressed, or byte-addressed.*/
	total_blks = data->blocks;
	dma_map_sg(&(host->mmc->class_dev), sg, sg_len,
				((data->flags)&MMC_DATA_READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);


	for (dma_loop = 1 ; dma_loop <= dma_times; dma_loop++, sg_len -= sg_transfer_len) {
		DBG("dma_loop = %d  sg_len = %d  sg_transfer_len = %d\n", dma_loop, sg_len, sg_transfer_len);
		if (dma_loop < dma_times)
			sg_transfer_len = MAX_DESC_NUM;
		else
			sg_transfer_len = sg_len;
		DBG("sg_transfer_len = %d\n", sg_transfer_len);
		/*
		*Firstly, check and wait till card is in the transfer state.
		*For our hardware, we can not consider
		*the card has successfully tranfered its state from data/rcv to trans,
		*when auto stop INT occurs.
		*/
		loop_cnt = 10000;
		do {
            if (host->cmd->opcode == SD_IO_RW_EXTENDED)
                break;
			loop_cnt--;
			WARN_ON(loop_cnt == 0);
			host->done_data = &complete;
			host->done = &atsmb_wait_done;
			host->soft_timeout = 1;
			atsmb_prep_cmd(host,
							MMC_SEND_STATUS,
							(host->mmc->card->rca)<<16,
							MMC_RSP_R1 | MMC_CMD_AC,
							0,
							0,
							0,		/*mask_0*/
							ATSMB_RSP_DONE_EN
							|ATSMB_RSP_CRC_ERR_EN
							|ATSMB_RSP_TIMEOUT_EN,
							0,		/*cmd type*/
							0);		/*read or write*/
			atsmb_issue_cmd();
			DBG("%16s = 0x%8x  |", "INT_MASK_1", *ATSMB0_INT_MASK_1);
			DBG("%16s = 0x%8x \n", "SD_STS_1", *ATSMB0_SD_STS_1);
			/*ISR would completes it.*/
			wait_for_completion_timeout(&complete, ATSMB_TIMEOUT_TIME);

			WARN_ON(host->soft_timeout == 1);
			if (host->soft_timeout == 1) {
				DBG("%s soft_timeout.\n", __func__);
				atsmb_dump_reg();
			}
			if (cmd->error != MMC_ERR_NONE) {
				cmd->error = -EIO; //MMC_ERR_FAILED;
				DBG("Getting Status failed.\n");
				goto end;
			}
		} while ((cmd->resp[0] & 0x1f00) != 0x900 && loop_cnt > 0); /*wait for trans state.*/

		/*
		* Now, we can safely issue read/write command.
		* We can not consider this request as multi-block acess or single one via opcode,
		* as request is splitted into sgs.
		* Some sgs may be single one, some sgs may be multi one.
		*/

		dma_phy = sg_dma_address(sg);
		dma_len = sg_dma_len(sg);
		DBG("dma_len = %d data->blksz = %d sg_len = %d\n", dma_len, data->blksz, sg_len);
        /*SDIO read/write*/
        if (host->cmd->opcode == SD_IO_RW_EXTENDED) {
             /*Single Block read/write*/   
             if ((dma_len / (data->blksz)) == 1 && (sg_len == 1)) {
                /* read operation*/
                if (data->flags & MMC_DATA_READ) {
                    host->opcode = SD_IO_RW_EXTENDED;
        			cmd_type = 2;
        			op = 1;
        			mask_0  = 0;	/*BLOCK_XFER_DONE INT skipped, we use DMA TC INT*/
        			mask_1 = (//ATSMB_SDIO_EN
                                ATSMB_READ_CRC_ERR_EN
        						|ATSMB_DATA_TIMEOUT_EN
        						/*Data Timeout and CRC error */
        						|ATSMB_RSP_CRC_ERR_EN
        						|ATSMB_RSP_TIMEOUT_EN);
        						/*Resp Timeout and CRC error */
        			dma_mask = SD_PDMA_CCR_Evt_success;
                    DBG("[%s]SR opcode = %d type = 0x%x op = 0x%x m_0 = 0x%x m_1 = 0x%x d_m = 0x%x\n",
                        __func__,host->opcode,cmd_type,op,mask_0,mask_1,dma_mask);
                } else {
                /* write operation*/
                    host->opcode = SD_IO_RW_EXTENDED;
        			cmd_type = 1;
        			op = 2;
        			/*====That is what we want===== DMA TC INT skipped*/
        			mask_0  = ATSMB_BLOCK_XFER_DONE_EN;
        			mask_1 = (//ATSMB_SDIO_EN
                                ATSMB_WRITE_CRC_ERR_EN
        						|ATSMB_DATA_TIMEOUT_EN
        						/*Data Timeout and CRC error */
        						|ATSMB_RSP_CRC_ERR_EN
        						|ATSMB_RSP_TIMEOUT_EN);
        						/*Resp Timeout and CRC error */
        			dma_mask = 0;
                    DBG("[%s]SW opcode = %d type = 0x%x op = 0x%x m_0 = 0x%x m_1 = 0x%x d_m = 0x%x\n",
                        __func__,host->opcode,cmd_type,op,mask_0,mask_1,dma_mask);
                }
             } else {
             /*Multiple Block read/write*/
                /* read operation*/
                if (data->flags & MMC_DATA_READ) {
                    host->opcode = SD_IO_RW_EXTENDED;
        			cmd_type = 6;
        			op = 1;
        			mask_0  = 0;	/*MULTI_XFER_DONE_EN skipped*/
        			mask_1 = (//ATSMB_SDIO_EN	/*====That is what we want=====*/
                                ATSMB_READ_CRC_ERR_EN
                                |ATSMB_DATA_TIMEOUT_EN
        						/*Data Timeout and CRC error */
        						|ATSMB_RSP_CRC_ERR_EN
        						|ATSMB_RSP_TIMEOUT_EN);
        						/*Resp Timeout and CRC error */
        			dma_mask = SD_PDMA_CCR_Evt_success;
                    DBG("[%s]MR opcode = %d type = 0x%x op = 0x%x m_0 = 0x%x m_1 = 0x%x d_m = 0x%x\n",
                        __func__,host->opcode,cmd_type,op,mask_0,mask_1,dma_mask);            
                } else {
                /* write operation*/
                    host->opcode = SD_IO_RW_EXTENDED;
        			cmd_type = 5;
        			op = 2;
        			mask_0  = ATSMB_MULTI_XFER_DONE_EN;//ATSMB_BLOCK_XFER_DONE_EN;	/*MULTI_XFER_DONE INT skipped*/
        			mask_1 = (//ATSMB_SDIO_EN		/*====That is what we want=====*/
                                ATSMB_WRITE_CRC_ERR_EN
                                |ATSMB_DATA_TIMEOUT_EN
        						/*Data Timeout and CRC error */
        						|ATSMB_RSP_CRC_ERR_EN
        						|ATSMB_RSP_TIMEOUT_EN);
        						/*Resp Timeout and CRC error */
        			dma_mask = 0;
                    DBG("[%s]MR opcode = %d type = 0x%x op = 0x%x m_0 = 0x%x m_1 = 0x%x d_m = 0x%x\n",
                        __func__,host->opcode,cmd_type,op,mask_0,mask_1,dma_mask);            
                }
             }
            
        } else {
            if ((dma_len / (data->blksz)) == 1 && (sg_len == 1)) {
        		if (data->flags & MMC_DATA_READ) {/* read operation*/
        			host->opcode = MMC_READ_SINGLE_BLOCK;
        			cmd_type = 2;
        			op = 1;
        			mask_0  = 0;	/*BLOCK_XFER_DONE INT skipped, we use DMA TC INT*/
        			mask_1 = (ATSMB_READ_CRC_ERR_EN
        						|ATSMB_DATA_TIMEOUT_EN
        						/*Data Timeout and CRC error */
        						|ATSMB_RSP_CRC_ERR_EN
        						|ATSMB_RSP_TIMEOUT_EN);
        						/*Resp Timeout and CRC error */
        			dma_mask = SD_PDMA_CCR_Evt_success;
        		} else {/*write operation*/
        			host->opcode = MMC_WRITE_BLOCK;
        			cmd_type = 1;
        			op = 2;
        			/*====That is what we want===== DMA TC INT skipped*/
        			mask_0  = ATSMB_BLOCK_XFER_DONE_EN;
        			mask_1 = (ATSMB_WRITE_CRC_ERR_EN
        						|ATSMB_DATA_TIMEOUT_EN
        						/*Data Timeout and CRC error */
        						|ATSMB_RSP_CRC_ERR_EN
        						|ATSMB_RSP_TIMEOUT_EN);
        						/*Resp Timeout and CRC error */
        			dma_mask = 0;
        		}
        	} else {  /*more than one*/
        		if (data->flags&MMC_DATA_READ) {/* read operation*/
        			host->opcode = MMC_READ_MULTIPLE_BLOCK;
        			cmd_type = 4;
        			op = 1;
        			mask_0  = 0;	/*MULTI_XFER_DONE_EN skipped*/
        			mask_1 = (ATSMB_AUTO_STOP_EN	/*====That is what we want=====*/
        						|ATSMB_DATA_TIMEOUT_EN
        						/*Data Timeout and CRC error */
        						|ATSMB_RSP_CRC_ERR_EN
        						|ATSMB_RSP_TIMEOUT_EN);
        						/*Resp Timeout and CRC error */
        			dma_mask = 0;
        		} else {/*write operation*/
        			host->opcode = MMC_WRITE_MULTIPLE_BLOCK;
        			cmd_type = 3;
        			op = 2;
        			mask_0  = 0;	/*MULTI_XFER_DONE INT skipped*/
        			mask_1 = (ATSMB_AUTO_STOP_EN		/*====That is what we want=====*/
        						|ATSMB_DATA_TIMEOUT_EN
        						/*Data Timeout and CRC error */
        						|ATSMB_RSP_CRC_ERR_EN
        						|ATSMB_RSP_TIMEOUT_EN);
        						/*Resp Timeout and CRC error */
        			dma_mask = 0;
        		}
        	}
        }
		/*To controller every sg done*/
		host->done_data = &complete;
		host->done = &atsmb_wait_done;
		/*sleep till ISR wakes us*/
		host->soft_timeout = 1;	/*If INT comes early than software timer, it would be cleared.*/

		total_dma_len = 0;
		DBG("host->DescVirAddr = %x host->DescSize=%x\n", host->DescVirAddr, host->DescSize);
		memset(host->DescVirAddr, 0, host->DescSize);
		for (sg_num = 0 ; sg_num < sg_transfer_len ; sg++, sg_num++) {

			/*
			 * Now, we can safely issue read/write command.
			 * We can not consider this request as multi-block acess or single one via opcode,
			 * as request is splitted into sgs.
			 * Some sgs may be single one, some sgs may be multi one.
			 */

			dma_phy = sg_dma_address(sg);
			dma_len = sg_dma_len(sg);
			total_dma_len = total_dma_len + dma_len;
			DBG("sg_num=%d sg_transfer_len=%d sg=%x sg_len=%d total_dma_len=%d dma_len=%d\n",
				sg_num, sg_transfer_len, sg, sg_len, total_dma_len, dma_len);
			/*2009/01/15 janshiue add*/
			if (sg_num < sg_transfer_len - 1) {
				/* means the last descporitor */
				atsmb_init_short_desc(
				host->DescVirAddr + (sg_num * sizeof(struct SD_PDMA_DESC_S)/4),
				dma_len, (unsigned long *)dma_phy, 0);
			} else {
				atsmb_init_short_desc(
				host->DescVirAddr + (sg_num * sizeof(struct SD_PDMA_DESC_S)/4),
				dma_len, (unsigned long *)dma_phy, 1);
			}
		/*2009/01/15 janshiue add*/

		}
		/*operate our hardware*/
		atsmb_prep_cmd(host,
						host->opcode,
			/*arg, may be byte addressed, may be block addressed.*/
						card_addr,
						cmd->flags,
						data->blksz - 1,	/* in fact, it is useless.*/
			/* for single one, it is useless. but for multi one, */
			/* it would be used to tell auto stop function whether it is done.*/
						total_dma_len/(data->blksz),
						mask_0,
						mask_1,
						cmd_type,
						op);

		atsmb_config_dma((op == 1) ? DMA_CFG_READ : DMA_CFG_WRITE,
						 dma_mask,
						 host);

		atsmb_issue_cmd();
		wait_for_completion_timeout(&complete,
			ATSMB_TIMEOUT_TIME*sg_transfer_len); /*ISR would completes it.*/

		/* When the address of request plus length equal card bound, 
		 * force this stop command response as pass. Eason 2012/4/20 */
		if (cmd->resp[0] == 0x80000b00) {
			/*This caes used for SD2.0 and after MMC4.1 version*/
			if (card_addr+(total_dma_len/data->blksz) == host->mmc->card->csd.capacity) {
			cmd->resp[0] = 0x00000b00;
			/*printk("card_addr = %08X, card_length = %08X,card capacity = %08X\n",
			card_addr,(total_dma_len/data->blksz),host->mmc->card->csd.capacity);
			printk("card_resp[0]=%08X, addr = %08X\n",cmd->resp[0],cmd->resp);*/
			}
		
			/* This caes used for SD1.0 and before MMC 4.1 */
			if ((card_addr/data->blksz)+(total_dma_len/data->blksz) == host->mmc->card->csd.capacity) {
			cmd->resp[0] = 0x00000b00;
			/*printk("Eason test: cmd->arg = %08X, data->blksz = %08X, length = %08X\n",
			card_addr,data->blksz,(total_dma_len/data->blksz));*/
			}
		}
        
		if (host->soft_timeout == 1) {
            atsmb_dump_reg();
            //*ATSMB0_BUS_MODE |= ATSMB_SFTRST;
            *ATSMB0_PDMA_GCR = SD_PDMA_GCR_SOFTRESET;
            /*disable INT */
	        *ATSMB0_INT_MASK_0 &= ~(ATSMB_BLOCK_XFER_DONE_EN | ATSMB_MULTI_XFER_DONE_EN);
	        *ATSMB0_INT_MASK_1 &= ~(ATSMB_WRITE_CRC_ERR_EN|ATSMB_READ_CRC_ERR_EN|ATSMB_RSP_CRC_ERR_EN
		        |ATSMB_DATA_TIMEOUT_EN|ATSMB_AUTO_STOP_EN|ATSMB_RSP_TIMEOUT_EN|ATSMB_RSP_DONE_EN);

            cmd->error = -ETIMEDOUT;
            data->error = -ETIMEDOUT;
        }

        WARN_ON(host->soft_timeout == 1);

        /*check everything goes okay or not*/
		if (cmd->error != MMC_ERR_NONE
			|| data->error != MMC_ERR_NONE) {
			DBG("CMD or Data failed error=%X DescVirAddr=%8X dma_phy=%8X dma_mask = %x\n",
				cmd->error, host->DescVirAddr, dma_phy, host->DmaIntMask);
			goto end;
		}
//		card_addr += total_dma_len>>(mmc_card_blockaddr(host->mmc->card_selected) ? 9 : 0);
		card_addr += total_dma_len>>(mmc_card_blockaddr(host->mmc->card) ? 9 : 0);	//zhf: modified by James Tian
		data->bytes_xfered += total_dma_len;


	}
//	dma_unmap_sg(&(host->mmc->class_dev), data->sg, data->sg_len,
//			((data->flags)&MMC_DATA_READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);

	WARN_ON(total_blks != (data->bytes_xfered / data->blksz));
	host->opcode = 0;
end:
	dma_unmap_sg(&(host->mmc->class_dev), data->sg, data->sg_len,
			((data->flags)&MMC_DATA_READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
	spin_lock(&host->lock);
	atsmb_request_end(host, host->mrq);
	spin_unlock(&host->lock);
	DBG("[%s] e\n",__func__);
}


/**********************************************************************
Name  	 : atsmb_cmd_with_data_back
Function    :
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static void  atsmb_cmd_with_data_back(struct atsmb_host *host)
{
	DECLARE_COMPLETION(complete);

	struct scatterlist *sg = NULL;
	unsigned int sg_len = 0;
	DBG("[%s] s\n",__func__);
	/*for loop*/
	sg = host->data->sg;
	sg_len = host->data->sg_len;
	/*To controller every sg done*/
	host->done_data = &complete;
	host->done = &atsmb_wait_done;
	dma_map_sg(&(host->mmc->class_dev), sg, sg_len, DMA_FROM_DEVICE);

	/*2009/01/15 janshiue add*/
	memset(host->DescVirAddr, 0, host->DescSize);
	atsmb_init_long_desc(host->DescVirAddr, sg_dma_len(sg), (unsigned long *)sg_dma_address(sg), 0, 1);
	/*2009/01/15 janshiue add*/
	/*prepare for cmd*/
	atsmb_prep_cmd(host,					/*host*/
					host->cmd->opcode, 	/*opcode*/
					host->cmd->arg,		/*arg*/
					host->cmd->flags,		/*flags*/
					sg_dma_len(sg)-1,		/*block length*/
					0,		/*block size: It looks like useless*/
					0,		/*int_mask_0*/
					(ATSMB_RSP_CRC_ERR_EN|ATSMB_RSP_TIMEOUT_EN
					|ATSMB_READ_CRC_ERR_EN |ATSMB_DATA_TIMEOUT_EN), /*int_mask_1*/
					2, 					/*cmd_type*/
					1);					/*op*/

	atsmb_config_dma(DMA_CFG_READ,
					 SD_PDMA_CCR_Evt_success,
					 host);
	atsmb_issue_cmd();
	/*ISR would completes it.*/
	wait_for_completion_timeout(&complete, ATSMB_TIMEOUT_TIME);
	
	dma_unmap_sg(&(host->mmc->class_dev), host->data->sg, host->data->sg_len,
			((host->data->flags)&MMC_DATA_READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
	spin_lock(&host->lock);
	atsmb_request_end(host, host->mrq);
	spin_unlock(&host->lock);
	DBG("[%s] e\n",__func__);
}
/**********************************************************************
Name  	 : atsmb_start_cmd
Function    :
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static void atsmb_start_cmd(struct atsmb_host *host)
{
    unsigned char int_mask_0,int_mask_1;
    int_mask_0 = 0;
    int_mask_1 = ATSMB_RSP_DONE_EN|ATSMB_RSP_CRC_ERR_EN|ATSMB_RSP_TIMEOUT_EN;

    DBG("[%s] s\n",__func__);

    atsmb_prep_cmd(host,
					host->cmd->opcode,
					host->cmd->arg,
					host->cmd->flags,
					0,		/*useless*/
					0,		/*useless*/
					int_mask_0,		/*mask_0*/
					int_mask_1,     /*mask_1*/
					0,		/*cmd type*/
					0);		/*read or write*/
	atsmb_issue_cmd();
	DBG("[%s] e\n",__func__);
}
/**********************************************************************
Name  	 : atsmb_fmt_check_rsp
Function    :
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static inline void atsmb_fmt_check_rsp(struct atsmb_host *host)
{

	u8 tmp_resp[4] = {0};
	int i, j, k;
	DBG("[%s] s\n",__func__);
	if (host->cmd->flags != (MMC_RSP_R2 | MMC_CMD_AC)
		&& host->cmd->flags != (MMC_RSP_R2 | MMC_CMD_BCR)) {
		for (j = 0, k = 1; j < 4; j++, k++)
			tmp_resp[j] = *(REG8_PTR(_RSP_0 + MMC_ATSMB0_BASE+k));

		host->cmd->resp[0] = (tmp_resp[0] << 24) |
			(tmp_resp[1]<<16) | (tmp_resp[2]<<8) | (tmp_resp[3]);
	} else {
		for (i = 0, k = 1; i < 4; i++) { /*R2 has 4 u32 response.*/
			for (j = 0; j < 4; j++) {
				if (k < 16)
					tmp_resp[j] = *(REG8_PTR(_RSP_0 + MMC_ATSMB0_BASE+k));
				else /* k =16*/
					tmp_resp[j] = *(ATSMB0_RSP_0);
					k++;
			 }
			host->cmd->resp[i] = (tmp_resp[0]<<24) | (tmp_resp[1]<<16) |
				(tmp_resp[2]<<8) | (tmp_resp[3]);
		}
	}

	/*
	* For the situation that we need response,
	* but response registers give us all zeros, we consider this operation timeout.
	*/
	if (host->cmd->flags != (MMC_RSP_NONE | MMC_CMD_AC)
		&& host->cmd->flags != (MMC_RSP_NONE | MMC_CMD_BC)) {
		if (host->cmd->resp[0] == 0 && host->cmd->resp[1] == 0
			&& host->cmd->resp[2] == 0 && host->cmd->resp[3] == 0) {
			host->cmd->error = -ETIMEDOUT;	//zhf: modified by James Tian
        }
	}
	DBG("[%s] e\n",__func__);
}
/**********************************************************************
Name  	 : atsmb_get_slot_status
Function    : Our host only supports one slot.
Calls		:
Called by	:
Parameter :
returns	: 1: in slot; 0: not in slot.
Author 	 : Leo Lee
History	:
***********************************************************************/
static int atsmb_get_slot_status(struct mmc_host *mmc)
{
//	struct atsmb_host *host = mmc_priv(mmc);
	unsigned char status_0 = 0;
//	unsigned long flags;
	unsigned long ret = 0;
	DBG("[%s] s\n",__func__);
//	spin_lock_irqsave(&host->lock, flags); // Vincent Li mark out for CONFIG_PREEMPT_RT
	status_0 = *ATSMB0_SD_STS_0;
//	spin_unlock_irqrestore(&host->lock, flags); // Vincent Li mark out for CONFIG_PREEMPT_RT
	/* after WM3400 A1 ATSMB_CARD_IN_SLOT_GPI = 1 means not in slot*/
	if (MMC0_DRIVER_VERSION >= MMC_DRV_3426_A0) {
		if(SD0_detect_pol) 
			ret = ((status_0 & ATSMB_CARD_NOT_IN_SLOT_GPI) ? 1 : 0);
		else
			ret = ((status_0 & ATSMB_CARD_NOT_IN_SLOT_GPI) ? 0 : 1);
			
		DBG("[%s] e1\n",__func__);
		return ret;
	} else {
		ret = ((status_0 & ATSMB_CARD_NOT_IN_SLOT_GPI) ? 1 : 0);
		DBG("[%s] e2\n",__func__);
		return ret;
	}
	DBG("[%s] e3\n",__func__);	
		return 0;
}

/**********************************************************************
Name        : atsmb_init_short_desc
Function    :
Calls       :
Called by   :
Parameter   :
Author      : Janshiue Wu
History     :
***********************************************************************/
int atsmb_init_short_desc(unsigned long *DescAddr, unsigned int ReqCount, unsigned long *BufferAddr, int End)
{
	struct SD_PDMA_DESC_S *CurDes_S;
	DBG("[%s] s\n",__func__);
	CurDes_S = (struct SD_PDMA_DESC_S *) DescAddr;
	CurDes_S->ReqCount = ReqCount;
	CurDes_S->i = 0;
	CurDes_S->format = 0;
	CurDes_S->DataBufferAddr = BufferAddr;
	if (End) {
		CurDes_S->end = 1;
		CurDes_S->i = 1;
	}
	DBG("[%s] e\n",__func__);
	return 0;
}

/**********************************************************************
Name        : atsmb_init_long_desc
Function    :
Calls       :
Called by   :
Parameter   :
Author      : Janshiue Wu
History     :
***********************************************************************/
int atsmb_init_long_desc(unsigned long *DescAddr, unsigned int ReqCount,
	unsigned long *BufferAddr, unsigned long *BranchAddr, int End)
{
	struct SD_PDMA_DESC_L *CurDes_L;
	DBG("[%s] s\n",__func__);
	CurDes_L = (struct SD_PDMA_DESC_L *) DescAddr;
	CurDes_L->ReqCount = ReqCount;
	CurDes_L->i = 0;
	CurDes_L->format = 1;
	CurDes_L->DataBufferAddr = BufferAddr;
	CurDes_L->BranchAddr = BranchAddr;
	if (End) {
		CurDes_L->end = 1;
		CurDes_L->i = 1;
	}
	DBG("[%s] e\n",__func__);
	return 0;
}

/**********************************************************************
Name  	 : atsmb_dma_isr
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static irqreturn_t atsmb_dma_isr(int irq, void *dev_id)
{

	struct atsmb_host *host = dev_id;
	u8 status_0, status_1, status_2, status_3;
	u32 pdma_event_code = 0;
	DBG("[%s] s\n",__func__);

	disable_irq_nosync(irq);
	spin_lock(&host->lock);
	/*Get INT status*/
	status_0 = *ATSMB0_SD_STS_0;
	status_1 = *ATSMB0_SD_STS_1;
	status_2 = *ATSMB0_SD_STS_2;
	status_3 = *ATSMB0_SD_STS_3;
	/* after WM3426 A0 using PDMA */
	if (MMC0_DRIVER_VERSION >= MMC_DRV_3426_A0) {

		pdma_event_code  = *ATSMB0_PDMA_CCR & SD_PDMA_CCR_EvtCode;

		/* clear INT status to notify HW clear EventCode*/
		*ATSMB0_PDMA_ISR |= SD_PDMA_IER_INT_STS;

		/*printk("dma_isr event code = %X\n", *ATSMB0_PDMA_CCR);*/
		/*We expect cmd with data sending back or read single block cmd run here.*/
		if (pdma_event_code == SD_PDMA_CCR_Evt_success) {
			/*means need to update the data->error and cmd->error*/
			if (host->DmaIntMask == SD_PDMA_CCR_Evt_success) {
				if (host->data != NULL) {
					host->data->error = MMC_ERR_NONE;
					host->cmd->error = MMC_ERR_NONE;
				} else {
					DBG("dma_isr host->data is NULL\n");
					/*disable INT*/
					*ATSMB0_PDMA_IER &= ~SD_PDMA_IER_INT_EN;
					goto out;
				}
			}
			/*else do nothing*/
			DBG("dma_isr PDMA OK\n");
			/*atsmb_dump_reg();*/
		}
		/*But unluckily, we should also be prepare for all kinds of error situation.*/
		else {
			host->data->error = -EIO; //MMC_ERR_FAILED;
			host->cmd->error = -EIO; //MMC_ERR_FAILED;
			DBG("** dma_isr PDMA fail**  event code = %X\n", *ATSMB0_PDMA_CCR);
			atsmb_dump_reg();
		}
        
        if (host->DmaIntMask == SD_PDMA_CCR_Evt_success)
		    atsmb_fmt_check_rsp(host);

		/*disable INT*/
		*ATSMB0_PDMA_IER &= ~SD_PDMA_IER_INT_EN;
	}

	/*wake up some one who is sleeping.*/
	if ((pdma_event_code != SD_PDMA_CCR_Evt_success) || (host->DmaIntMask == SD_PDMA_CCR_Evt_success)) {
		if (host->done_data) {/* We only use done_data when requesting data.*/
			host->soft_timeout = 0;
			host->done(host);
		} else
			atsmb_request_end(host, host->mrq); /*for cmd with data sending back.*/
	}
	
out:	
	spin_unlock(&host->lock);
    enable_irq(irq);
	DBG("[%s] e\n",__func__);
	return IRQ_HANDLED;
}

/**********************************************************************
Name  	 : atsmb_regular_isr
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
//static irqreturn_t atsmb_regular_isr(int irq, void *dev_id, struct pt_regs *regs)
irqreturn_t atsmb_regular_isr(int irq, void *dev_id)
{

	struct atsmb_host *host = dev_id;
	u8 status_0, status_1, status_2, status_3,mask_0,mask_1;
	u32 pdma_sts;
	
	DBG("[%s] s\n",__func__);
	WARN_ON(host == NULL);

	disable_irq_nosync(irq);
	spin_lock(&host->lock);

	/*Get INT status*/
	status_0 = *ATSMB0_SD_STS_0;
    status_1 = *ATSMB0_SD_STS_1;
	status_2 = *ATSMB0_SD_STS_2;
	status_3 = *ATSMB0_SD_STS_3;
       
    mask_0 = *ATSMB0_INT_MASK_0;
    mask_1 = *ATSMB0_INT_MASK_1;
	/*******************************************************
						card insert interrupt
	********************************************************/
	if ((status_0 & ATSMB_DEVICE_INSERT)      /*Status Set and IRQ enabled*/
	/*To aviod the situation that we intentionally disable IRQ to do rescan.*/
		&& (*ATSMB0_INT_MASK_0 & 0x80)) {

		if (host->mmc->ops->get_slot_status(host->mmc)) {
			host->mmc->scan_retry = 3;
			host->mmc->card_scan_status = false;
		} else {
			host->mmc->scan_retry = 0;
			host->mmc->card_scan_status = false;
		}

		mmc_detect_change(host->mmc, HZ/2);
		/*Taipei Side Request: Disable INSERT IRQ when doing rescan.*/
		//*ATSMB0_INT_MASK_0 &= (~0x80);/* or 40?*/	//zhf: marked by James Tian
		/*Then we clear the INT status*/
		//iowrite32(ATSMB_DEVICE_INSERT, host->base+_SD_STS_0);
        *ATSMB0_SD_STS_0 |= ATSMB_DEVICE_INSERT;  
        spin_unlock(&host->lock);
		enable_irq(irq);
		DBG("[%s] e1\n",__func__);
		return IRQ_HANDLED;
	}
    
    if ((status_1 & mask_1)& ATSMB_SDIO_INT) {
        spin_unlock(&host->lock);
        mmc_signal_sdio_irq(host->mmc);
       
        if (((status_1 & mask_1) == ATSMB_SDIO_INT) && ((status_0 & mask_0) == 0)) {
            
            enable_irq(irq);
            DBG("[%s] e2\n",__func__);

            return IRQ_HANDLED;
        }
        spin_lock(&host->lock);
    }
	pdma_sts = *ATSMB0_PDMA_CCR;
    
    if (((status_0 & mask_0) | (status_1 & mask_1)) == 0) {
        spin_unlock(&host->lock);
		enable_irq(irq);
        DBG("[%s] e3\n",__func__);
        return IRQ_HANDLED;
    }
	/*******************************************************
							command interrupt.
	*******************************************************/
	/*for write single block*/
	if (host->opcode == MMC_WRITE_BLOCK) {
		if ((status_0 & ATSMB_BLOCK_XFER_DONE)
			&& (status_1 & ATSMB_RSP_DONE)) {
			host->data->error = MMC_ERR_NONE;
			host->cmd->error = MMC_ERR_NONE;
			/* okay, what we want.*/
		} else {
			host->data->error = -EIO; //MMC_ERR_FAILED;
			host->cmd->error = -EIO; //MMC_ERR_FAILED;
            DBG("[%s] err1\n",__func__);
			atsmb_dump_reg();
		}
	} else if (host->opcode == MMC_WRITE_MULTIPLE_BLOCK
			|| host->opcode == MMC_READ_MULTIPLE_BLOCK) {
		if ((status_1 & (ATSMB_AUTO_STOP|ATSMB_RSP_DONE))
			&& (status_0 & ATSMB_MULTI_XFER_DONE)) {
			/*If CRC error occurs, I think this INT would not occrs.*/
			/*okay, that is what we want.*/
			host->data->error = MMC_ERR_NONE;
			host->cmd->error = MMC_ERR_NONE;
		} else {
			host->data->error = -EIO; //MMC_ERR_FAILED;
			host->cmd->error = -EIO; //MMC_ERR_FAILED;
            DBG("[%s] err2\n",__func__);
			atsmb_dump_reg();

		}
	} else if (host->opcode == MMC_READ_SINGLE_BLOCK) {/* we want DMA TC, run here, must be error.*/
		host->data->error = -EIO; //MMC_ERR_FAILED;
		host->cmd->error = -EIO; //MMC_ERR_FAILED;
        DBG("[%s] err3\n",__func__);
		atsmb_dump_reg();
	} else if (host->opcode == SD_IO_RW_EXTENDED){
        /*Write operation*/
        if (*ATSMB0_CTL & BIT2) {
            if ((*ATSMB0_CTL & 0xf0) == 0x10) {   /*single block write*/
                if ((status_0 & ATSMB_BLOCK_XFER_DONE)
        		&& (status_1 & ATSMB_RSP_DONE)) {
        			host->data->error = MMC_ERR_NONE;
        			host->cmd->error = MMC_ERR_NONE;
        		/* okay, what we want.*/
        	    } else {
        			host->data->error = -EIO; //MMC_ERR_FAILED;
        			host->cmd->error = -EIO; //MMC_ERR_FAILED;
                    DBG("[%s] err4 status_0 = %x status_1 = %x\n",__func__,status_0,status_1);
        	    }
                
            } else if ((*ATSMB0_CTL & 0xf0) == 0x50) {
                if ((status_0 & ATSMB_MULTI_XFER_DONE)
    			&& (status_1 & ATSMB_RSP_DONE)) {
        			host->data->error = MMC_ERR_NONE;
        			host->cmd->error = MMC_ERR_NONE;
    			/* okay, what we want.*/
    		    } else {
        			host->data->error = -EIO; //MMC_ERR_FAILED;
        			host->cmd->error = -EIO; //MMC_ERR_FAILED;
                    DBG("[%s] err4-2 status_0 = %x status_1 = %x\n",__func__,status_0,status_1);
    		    }
            } else {
                host->data->error = -EIO; //MMC_ERR_FAILED;
    			host->cmd->error = -EIO; //MMC_ERR_FAILED;
                DBG("[%s] err4-3 status_0 = %x status_1 = %x\n",__func__,status_0,status_1);
            }
        }
        else {
        /*Read operation*/
            host->data->error = -EIO; //MMC_ERR_FAILED;
		    host->cmd->error = -EIO; //MMC_ERR_FAILED;
            DBG("[%s] err5\n",__func__);
        }
        
        
    } else {
/* command, not request data*/
/* the command which need data sending back,*/
/* like switch_function, send_ext_csd, send_scr, send_num_wr_blocks.*/
/* NOTICE: we also send status before reading or writing data, so SEND_STATUS should be excluded.*/
		if (host->data && host->opcode != MMC_SEND_STATUS) {
			host->data->error = -EIO; //MMC_ERR_FAILED;
			host->cmd->error = -EIO; //MMC_ERR_FAILED;
            DBG("[%s] err6\n",__func__);
			atsmb_dump_reg();
		} else {			/* Just command, no need data sending back.*/
			if (status_1 & ATSMB_RSP_DONE) {
				/*Firstly, check data-response is busy or not.*/
				if (host->cmd->flags == (MMC_RSP_R1B | MMC_CMD_AC)) {
					int i = 10000;
					
					while (status_2 & ATSMB_RSP_BUSY) {
						status_2 = *ATSMB0_SD_STS_2;
						if (--i == 0)
							break;
						DBG(" IRQ:Status_2 = %d, busy!\n", status_2);
					}
					if (i == 0)
						printk("[MMC driver] Error :SD data-response always busy!");
				}
#if 1
/*for our host, even no card in slot, for SEND_STATUS also returns no error.*/
/*The protocol layer depends on SEND_STATUS to check whether card is in slot or not.*/
/*In fact, we can also avoid this situation by checking the response whether they are all zeros.*/
				if (!atsmb_get_slot_status(host->mmc) && host->opcode == MMC_SEND_STATUS) {
					host->cmd->retries = 0; /* No retry.*/
//					host->cmd->error = MMC_ERR_INVALID;
					host->cmd->error = -EINVAL;
				} else
#endif
				host->cmd->error = MMC_ERR_NONE;
			} else {
				if (status_1 & ATSMB_RSP_TIMEOUT) {/* RSP_Timeout .*/
//					host->cmd->error = MMC_ERR_TIMEOUT;
					host->cmd->error = -ETIMEDOUT;
                    DBG("[%s] err7\n",__func__);
                } else  {/*or RSP CRC error*/
//					host->cmd->error = MMC_ERR_BADCRC;
					host->cmd->error = -EILSEQ;
                    DBG("[%s] err8\n",__func__);
                }
				atsmb_dump_reg();
			}
		}
	}
	atsmb_fmt_check_rsp(host);

	/*disable INT */
	*ATSMB0_INT_MASK_0 &= ~(ATSMB_BLOCK_XFER_DONE_EN | ATSMB_MULTI_XFER_DONE_EN);
	*ATSMB0_INT_MASK_1 &= ~(ATSMB_WRITE_CRC_ERR_EN|ATSMB_READ_CRC_ERR_EN|ATSMB_RSP_CRC_ERR_EN
		|ATSMB_DATA_TIMEOUT_EN|ATSMB_AUTO_STOP_EN|ATSMB_RSP_TIMEOUT_EN|ATSMB_RSP_DONE_EN);


	/*clear INT status. In fact, we will clear again before issuing new command.*/
	*ATSMB0_SD_STS_0 |= status_0;
	*ATSMB0_SD_STS_1 |= status_1;

	/* when read CRC error occur, and the status can't write one to clear.
	 * To clear read CRC error status , can do software reset. This is HW bug. 2013/3/21*/
	if ((*ATSMB0_SD_STS_1 & BIT6) == 0x40) {
		DBG("[%s] host0 CMD%d Read CRC error occur\n",__func__,host->cmd->opcode);
		/* Save SD card register */
		atsmb_copy_reg(0);
		/* Software reset */
		*ATSMB0_BUS_MODE |= BIT7;	
		/* restore SD card register */
		atsmb_copy_reg(1);
	} 

	if (*ATSMB0_PDMA_ISR & SD_PDMA_IER_INT_STS)
		*ATSMB0_PDMA_ISR |= SD_PDMA_IER_INT_STS;

	/*wake up some one who is sleeping.*/
	if (host->done_data) { /* We only use done_data when requesting data.*/
		host->soft_timeout = 0;
		host->done(host);
	} else
		atsmb_request_end(host, host->mrq); /*for cmd without data.*/

	spin_unlock(&host->lock);
	enable_irq(irq);
	DBG("[%s] e4\n",__func__);
	return IRQ_HANDLED;
}
EXPORT_SYMBOL(atsmb_regular_isr);

/**********************************************************************
Name  	 : atsmb_get_ro
Function    :.
Calls		:
Called by	:
Parameter :
returns	: 0 : write protection is disabled. 1: write protection is enabled.
Author 	 : Leo Lee
History	:
***********************************************************************/
int atsmb_get_ro(struct mmc_host *mmc)
{
	struct atsmb_host *host = mmc_priv(mmc);
	unsigned char status_0 = 0;
	unsigned long flags;
	unsigned long ret = 0;
	DBG("[%s] s\n",__func__);
    //add by kevin guan
    if(SD0_ro_disable)
        return 0;
	spin_lock_irqsave(&host->lock, flags);
	status_0 = *ATSMB0_SD_STS_0;
	spin_unlock_irqrestore(&host->lock, flags);
	DBG("[%s]\nstatus_0 = 0x%x\n", __func__,status_0);
	ret = ((status_0 & ATSMB_WRITE_PROTECT) ? 0 : 1);
	DBG("[%s] e\n",__func__);
	return ret;
}
/**********************************************************************
Name  	 : atsmb_dump_host_regs
Function    :
Calls		:
Called by	:
Parameter :
returns	: 
Author 	 : Leo Lee
History	:
***********************************************************************/
void atsmb_dump_host_regs(struct mmc_host *mmc)
{
	DBG("[%s] s\n",__func__);
	atsmb_dump_reg();
	DBG("[%s] e\n",__func__);
}
EXPORT_SYMBOL(atsmb_dump_host_regs);

/**********************************************************************
Name  	 : atsmb_enable_sdio_irq
Function    :
Calls		:
Called by	:
Parameter :
returns	: 
Author 	 : Tommy Huang
History	:
***********************************************************************/
static void atsmb_enable_sdio_irq(struct mmc_host *mmc, int enable)
{
	struct atsmb_host *host = mmc_priv(mmc);
	unsigned long flags;

	DBG("[%s] s enable = %d *ATSMB0_INT_MASK_1 = %x\n",__func__,enable,*ATSMB0_INT_MASK_1);
	spin_lock_irqsave(&host->lock, flags);
    
	if (enable) {
		*ATSMB0_INT_MASK_1 |= ATSMB_SDIO_EN;
	} else {
		*ATSMB0_INT_MASK_1 &= ~ATSMB_SDIO_EN;
	}

	spin_unlock_irqrestore(&host->lock, flags);
    DBG("[%s] e\n",__func__);
    
}
/**********************************************************************
Name  	 : atsmb_request
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static void atsmb_request(struct mmc_host *mmc, struct mmc_request *mrq)
{

	struct atsmb_host *host = mmc_priv(mmc);
	DBG("[%s] s\n",__func__);
	
	/* May retry process comes here.*/
	host->mrq = mrq;
	host->data = mrq->data;
	host->cmd = mrq->cmd;
	host->done_data = NULL;
	host->done = NULL;

	/*for data request*/
	if (host->data) {
		if (host->cmd->opcode == MMC_WRITE_BLOCK
			|| host->cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK
			|| host->cmd->opcode == MMC_READ_SINGLE_BLOCK
			|| host->cmd->opcode == MMC_READ_MULTIPLE_BLOCK
			|| host->cmd->opcode == SD_IO_RW_EXTENDED) {
			atsmb_start_data(host);
        } else {
			atsmb_cmd_with_data_back(host);
        }     
	} else {
		atsmb_start_cmd(host);
    }
	DBG("[%s] e\n",__func__);
}
/**********************************************************************
Name  	 : atsmb_set_clock
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Eason Chien
History	:2012/7/19
***********************************************************************/
static int atsmb_set_clock(struct mmc_host *mmc, unsigned int clock)
{
	int clock_multiplier = 1;
	DBG("clock = %u\n",clock);

	if (*ATSMB0_EXT_BUS_MODE & BIT4) /*Enable DDR50*/
		clock_multiplier = 2;

	if(SD0_speed){
		if (clock == mmc->f_min) {
			DBG("[%s]ATSMB Host 390KHz\n", __func__);  
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
	 		return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 1, 390);
		}
		else if (clock >= 208000000) {
			DBG("[%s]ATSMB Host 208MHz\n", __func__);
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				
				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);

				
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
				DBG("[%s] *DPCTL = 0x%x DPCTL = 0x%x\n", 
					__func__, SD0_DPCTL_4BYTE_ADDR, SD0_DPCTL_4BYTE_VAL);
				DBG("[%s] *DNCTL = 0x%x DNCTL = 0x%x\n", 
					__func__, SD0_DNCTL_4BYTE_ADDR, SD0_DNCTL_4BYTE_VAL);
			}
			return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 2, 80);	
		} else if (clock >= 100000000) {
			DBG("[%s]ATSMB Host 100MHz\n", __func__);
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);

				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);
				
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
			return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 2, 80);
		} else if (clock >= 50000000) {
			DBG("[%s]ATSMB Host 50MHz\n", __func__);
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);

				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
			return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 2, 45 * clock_multiplier);
		} else if ((clock >= 25000000) && (clock < 50000000)) {
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				
				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
			DBG("[%s]ATSMB Host 25MHz\n", __func__);
			return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 2, 24 * clock_multiplier);
		} else if ((clock >= 20000000) && (clock < 25000000)) {
			DBG("[%s]ATSMB Host 20MHz\n", __func__);
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				
				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
			return  auto_pll_divisor(DEV_SDMMC0, SET_DIV, 2, 20 * clock_multiplier);
		} else {
			DBG("[%s]ATSMB Host 390KHz\n", __func__);
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				
				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
			return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 1, 390 * clock_multiplier);
		}
	}else{
		if (clock == mmc->f_min) {
			DBG("[%s]ATSMB Host 390KHz\n", __func__);  
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
	 		return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 1, 390);
		}else if (clock >= 25000000) {
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				
				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
			DBG("[%s]ATSMB Host 25MHz\n", __func__);
			return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 2, 24 * clock_multiplier);
		} else if ((clock >= 20000000) && (clock < 25000000)) {
			DBG("[%s]ATSMB Host 20MHz\n", __func__);
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				
				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
			return  auto_pll_divisor(DEV_SDMMC0, SET_DIV, 2, 20 * clock_multiplier);
		} else {
			DBG("[%s]ATSMB Host 390KHz\n", __func__);
			if ((*ATSMB0_CTL2 & BIT3) == 0) {
				DBG("[%s] 1.8V clock = %u\n",__func__,clock);
				
				/*Set DPCTL 010 DNCTL 001*/
				SD0_DPCTL_4BYTE_VAL = (BIT1 | BIT4 | BIT7 | BIT10 | BIT13 | BIT16 | BIT19);
				SD0_DNCTL_4BYTE_VAL = (BIT0 | BIT3 | BIT6 | BIT9 | BIT12 | BIT15 | BIT18);
				DBG("[%s] *ATSMB0_CTL2 = 0x%x \n", __func__, *ATSMB0_CTL2);
			}
			return auto_pll_divisor(DEV_SDMMC0, SET_DIV, 1, 390 * clock_multiplier);
		}	
	}
}

static void atsmb_enable_power(struct atsmb_host *host, unsigned long flags)
{
	int result;
	spin_unlock_irqrestore(&host->lock, flags);
	if (g_atsmb_regulator) {
		if (regulator_is_enabled(atsmb_regulator) == 0) {
			DBG("[%s] Turn on power\n", __FUNCTION__);
			result = regulator_enable(atsmb_regulator);
			if (result != 0)
				printk(KERN_ALERT "[%s] regulator_enable FAIL\n", __FUNCTION__);
		}
	} else {
		GPIO_OD_GP13_SD0_BYTE_VAL &= ~GPIO_SD0_POWER; 
	}
	spin_lock_irqsave(&host->lock, flags);
}

static void atsmb_disable_power(struct atsmb_host *host, unsigned long flags)
{
	int result;
	spin_unlock_irqrestore(&host->lock, flags);
	if (g_atsmb_regulator) {
		if (regulator_is_enabled(atsmb_regulator) == 1) {
			DBG("[%s] Turn off power\n", __FUNCTION__);
			result = regulator_disable(atsmb_regulator);
			if (result != 0)
				printk(KERN_ALERT "[%s] regulator_disable FAIL\n", __FUNCTION__);
		}
	} else {
		GPIO_CTRL_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
		GPIO_OC_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
		/*set internal pull up*/
		PULL_CTRL_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
		/*set internal pull enable*/
		PULL_EN_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
		/*disable SD0 power*/
		GPIO_OD_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
	}
	spin_lock_irqsave(&host->lock, flags);
}

/**********************************************************************
Name  	 : atsmb_set_ios
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static void atsmb_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
{

	struct atsmb_host *host = mmc_priv(mmc);
	unsigned long flags;
	DBG("[%s] s\n",__func__);
	spin_lock_irqsave(&host->lock, flags);

	if (ios->power_mode == MMC_POWER_OFF) {
		if (MMC0_DRIVER_VERSION == MMC_DRV_3498) {
			/* set all descriptor end bit for removing card when
			 * PDMA don't transfer data done. let PDMA transfer done
			 */
			atsmb_set_dma_end(host->DescVirAddr, host->DescSize);
			
			/* stop SD output clock */
			*ATSMB0_BUS_MODE &= ~(ATSMB_CST);

			/*  disable SD Card power  */			
			/*set SD0 power pin as GPO pin*/
			atsmb_disable_power(host, flags);
                    
			/* Disable Pull up/down resister of SD Bus */
			/*GPIO_PULL_CTRL_GP13_XDIO_BYTE_VAL &=  ~SD0_PIN; marked by eason 2012/3/29*/ 

			/*  Config SD0 to GPIO  */
			GPIO_CTRL_GP13_SD0_BYTE_VAL |= SD0_PIN;

			/*  SD0 all pins output low  */
			GPIO_OD_GP13_SD0_BYTE_VAL &= ~SD0_PIN;

            /*  Config SD0 to GPO   */
            GPIO_OC_GP13_SD0_BYTE_VAL |= SD0_PIN;
			
		}

	} else if (ios->power_mode == MMC_POWER_UP) {
		if (MMC0_DRIVER_VERSION == MMC_DRV_3498) {
			/*  disable SD Card power  */
			/*set SD0 power pin as GPO pin*/
			GPIO_CTRL_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
			GPIO_OC_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
			
			/*set internal pull up*/
			PULL_CTRL_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
			/*set internal pull enable*/
			PULL_EN_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;
			/*disable SD0 power*/
			GPIO_OD_GP13_SD0_BYTE_VAL |= GPIO_SD0_POWER;

            /* Config SD PIN share, WM3498 SD0 no share pin */
			/*PIN_SHARING_SEL_4BYTE_VAL &= ~GPIO_SD0_PinShare; */

            /* do not config GPIO_SD0_CD because ISR has already run,
			 * config card detect will issue ISR storm.
			 */
			/*
			GPIO_OD_GP12_SPI_BYTE_VAL |= GPIO_SD0_18SEL;			
			GPIO_OC_GP12_SPI_BYTE_VAL |= GPIO_SD0_18SEL;
			GPIO_CTRL_GP12_SPI_BYTE_VAL |= GPIO_SD0_18SEL;
			*/
			*ATSMB0_CTL2 |= BIT3; /*Set 3.3V*/
			*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/
			*ATSMB0_CTL2 &= ~(BIT0 | BIT1 | BIT2);
			*ATSMB0_EXT_BUS_MODE &= ~BIT4; /*Disable DDR50*/

			/*Set 18SEL as function pin*/
			GPIO_CTRL_GP12_SPI_BYTE_VAL &= ~GPIO_SD0_18SEL;
			
			
			/*  Config SD to GPIO  */
			GPIO_CTRL_GP13_SD0_BYTE_VAL |= SD0_PIN;

			/*  SD all pins output low  */
			GPIO_OD_GP13_SD0_BYTE_VAL &= ~SD0_PIN;

			/*  Config SD to GPO   */
			GPIO_OC_GP13_SD0_BYTE_VAL |= SD0_PIN;

			/* Pull up/down resister of SD Bus */
			/*Disable Clock & CMD Pull enable*/
			PULL_EN_GP13_SD0_BYTE_VAL &= ~(GPIO_SD0_Clock | GPIO_SD0_Command);

			/*Set CD ,WP ,DATA pin pull up*/
			if (SD0_detect_pulldown)
				PULL_CTRL_GP63_SD02_BYTE_VAL &= ~GPIO_SD0_CD;
			else
				PULL_CTRL_GP63_SD02_BYTE_VAL |= GPIO_SD0_CD;
			PULL_CTRL_GP13_SD0_BYTE_VAL |= (GPIO_SD0_Data | GPIO_SD0_WriteProtect);

			/*Enable CD ,WP ,DATA  internal pull*/
			PULL_EN_GP63_SD02_BYTE_VAL |= GPIO_SD0_CD;
			PULL_EN_GP13_SD0_BYTE_VAL |= (GPIO_SD0_Data | GPIO_SD0_WriteProtect);

			/*Set SD0 PAD signal drive strength as 0*/
			SD0_DPCTL_4BYTE_VAL = 0;
			SD0_DNCTL_4BYTE_VAL = 0;

			spin_unlock_irqrestore(&host->lock, flags);
			msleep(100);
			spin_lock_irqsave(&host->lock, flags);

			/*  enable SD Card Power  */
			atsmb_enable_power(host, flags);

			/* enable SD output clock */
			*ATSMB0_BUS_MODE |= ATSMB_CST;

			spin_unlock_irqrestore(&host->lock, flags);
			msleep(10);
			spin_lock_irqsave(&host->lock, flags);

			/* Config SD0 back to function  */
			GPIO_CTRL_GP13_SD0_BYTE_VAL &= ~SD0_PIN;
			GPIO_CTRL_GP63_SD02CD_BYTE_VAL &= ~GPIO_SD0_CD;
		}
	} else {
		/*nothing to do when powering on.*/
	}

	host->current_clock = atsmb_set_clock(mmc,ios->clock);

	if (ios->bus_width == MMC_BUS_WIDTH_8) {
		*ATSMB0_EXT_CTL |= BIT2;
	} else if (ios->bus_width == MMC_BUS_WIDTH_4) {
		*ATSMB0_BUS_MODE |= ATSMB_BUS_WIDTH_4;
		*ATSMB0_EXT_CTL &= ~BIT2;
	} else {
		*ATSMB0_BUS_MODE &= ~(ATSMB_BUS_WIDTH_4);
		*ATSMB0_EXT_CTL &= ~BIT2;
	}

/*
#define MMC_TIMING_LEGACY	0
#define MMC_TIMING_MMC_HS	1
#define MMC_TIMING_SD_HS	2
#define MMC_TIMING_UHS_SDR12	MMC_TIMING_LEGACY
#define MMC_TIMING_UHS_SDR25	MMC_TIMING_SD_HS
#define MMC_TIMING_UHS_SDR50	3
#define MMC_TIMING_UHS_SDR104	4
#define MMC_TIMING_UHS_DDR50	5
#define MMC_TIMING_MMC_HS200	6
*/
	DBG("[%s]timing =  0x%x clock = %u\n", __func__, ios->timing, ios->clock);

	if (ios->timing == MMC_TIMING_LEGACY) {
		*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/
		*ATSMB0_CTL2 &= ~(BIT0 | BIT1 | BIT2);
	} else if (ios->timing == MMC_TIMING_MMC_HS) {
		*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/
		*ATSMB0_CTL2 &= ~(BIT0 | BIT1 | BIT2);
	} else if (ios->timing == MMC_TIMING_SD_HS) {
		*ATSMB0_EXT_CTL |= BIT7; /*HIGH SPEED MODE*/
		*ATSMB0_CTL2 &= ~(BIT0 | BIT1 | BIT2);
	} else if (ios->timing == MMC_TIMING_UHS_SDR12) {
		*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/
		/*UHS Mode Select = 000 SDR12*/
		*ATSMB0_CTL2 &= ~(BIT0 | BIT1 | BIT2);
	} else if (ios->timing == MMC_TIMING_UHS_SDR25) {
		*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/
		/*UHS Mode Select = 001 SDR25*/
		*ATSMB0_CTL2 |= BIT0;
		*ATSMB0_CTL2 &= ~(BIT1 | BIT2);
	} else if (ios->timing == MMC_TIMING_UHS_SDR50) {
		*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/
		/*UHS Mode Select = 010 SDR50*/
		*ATSMB0_CTL2 |= BIT1;
		*ATSMB0_CTL2 &= ~(BIT0 | BIT2);
	} else if (ios->timing == MMC_TIMING_UHS_SDR104) {
		*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/
		/*UHS Mode Select = 011 SDR104*/
		*ATSMB0_CTL2 |= (BIT0 | BIT1);
		*ATSMB0_CTL2 &= ~BIT2;
	} else if (ios->timing == MMC_TIMING_UHS_DDR50) {
		*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/
		/*UHS Mode Select = 100 DDR50*/
		*ATSMB0_CTL2 |= BIT2;
		*ATSMB0_CTL2 &= ~(BIT0 | BIT1);
		*ATSMB0_EXT_BUS_MODE |= BIT4; /*Enable DDR50*/
		/* enable SD output clock */
		*ATSMB0_BUS_MODE &= ~ATSMB_CST;
		spin_unlock_irqrestore(&host->lock, flags);
		msleep(1);
		spin_lock_irqsave(&host->lock, flags);

		*ATSMB0_EXT_BUS_MODE |= BIT4; /*Enable DDR50*/
		/* enable SD output clock */
		*ATSMB0_BUS_MODE |= ATSMB_CST;
	} else
		*ATSMB0_EXT_CTL &= ~BIT7; /*Disable High Speed Mode*/

/*	spin_unlock_irqrestore(&host->lock, flags);
	msleep(100);
	spin_lock_irqsave(&host->lock, flags); */

	DBG("[%s] *ATSMB0_CTL2 = 0x%x *ATSMB0_EXT_CTL = 0x%x *ATSMB0_EXT_BUS_MODE = 0x%x\n",
		__func__, *ATSMB0_CTL2, *ATSMB0_EXT_CTL, *ATSMB0_EXT_BUS_MODE);
	spin_unlock_irqrestore(&host->lock, flags);
	DBG("[%s] e\n",__func__);
}

int	atsmb_start_signal_voltage_switch(struct mmc_host *host, struct mmc_ios *ios)
{
	DBG("[%s] s\n",__func__);
	
	if(ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
		/*Set CMD ,DATA0~3 pin as GPO low*/
		/*  SD0 all pins output low  */
		GPIO_OD_GP13_SD0_BYTE_VAL &= ~(GPIO_SD0_Command | GPIO_SD0_Data);
        /*  Config SD0 to GPO   */
        GPIO_OC_GP13_SD0_BYTE_VAL |= (GPIO_SD0_Command | GPIO_SD0_Data);;
		/*  Config SD0 to GPIO  */
		GPIO_CTRL_GP13_SD0_BYTE_VAL |= (GPIO_SD0_Command | GPIO_SD0_Data);
		

		/*Wait for 1ms*/
		msleep(1);
		
		/*Stop Clock*/
		*ATSMB0_BUS_MODE &= ~(ATSMB_CST);

		
		/*Enable clock & Set 1.8V*/
		*ATSMB0_CTL2 &= ~BIT3;

		/*Wait for 10ms*/
		msleep(100);

		/*Enable Clock*/
		*ATSMB0_BUS_MODE |= ATSMB_CST;

		
		/*Set CMD DATA0~3 pin as function pin*/
		GPIO_CTRL_GP13_SD0_BYTE_VAL &= ~GPIO_SD0_Command;
		GPIO_CTRL_GP13_SD0_BYTE_VAL &= ~GPIO_SD0_Data;

				
		DBG("Set SD0 1.8v = 0x%x\n",ios->signal_voltage);

		return 0;
	} else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330){
		*ATSMB0_CTL2 |= BIT3;
		DBG("Set SD0 3.3v = 0x%x\n",ios->signal_voltage);
		return 0;
	}
	
	printk("Set voltage error = 0x%x\n",ios->signal_voltage);
	return -EIO;
	DBG("[%s] e\n",__func__);
}

static const struct mmc_host_ops atsmb_ops = {
	.request	= atsmb_request,
	.set_ios	= atsmb_set_ios,
	.get_ro	= atsmb_get_ro,
	.get_slot_status = atsmb_get_slot_status,
	.dump_host_regs = atsmb_dump_host_regs,
	.enable_sdio_irq	= atsmb_enable_sdio_irq,
	.start_signal_voltage_switch = atsmb_start_signal_voltage_switch,
};

extern int wmt_getsyspara(char *varname, unsigned char *varval, int *varlen);

/**********************************************************************
Name  	 :atsmb_probe
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static int __init atsmb_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct mmc_host *mmc_host  = NULL;
	struct atsmb_host *atsmb_host = NULL;
	struct resource *resource = NULL;
	int  irq[2] = {0};
	int ret = 0;

	DBG("[%s] s\n",__func__);
	if (!pdev) {
		ret = -EINVAL; /* Invalid argument */
		goto the_end;
	}

	/*Enable SD host clock*/
    auto_pll_divisor(DEV_SDMMC0, CLK_ENABLE, 0, 0);
    
    if (MMC0_DRIVER_VERSION == MMC_DRV_3498) {
		/* Pull up/down resister of SD CD */
		if(SD0_detect_pulldown)
			PULL_CTRL_GP63_SD02_BYTE_VAL &= ~GPIO_SD0_CD; /*pull down CD*/
		else
			PULL_CTRL_GP63_SD02_BYTE_VAL |= GPIO_SD0_CD; /*pull up CD*/
		PULL_EN_GP63_SD02_BYTE_VAL |= GPIO_SD0_CD;

		/* config CardDetect pin to SD function */
    	GPIO_CTRL_GP63_SD02CD_BYTE_VAL &= ~GPIO_SD0_CD;
	}


    resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!resource) {
		ret = -ENXIO;	/* No such device or address */
		printk(KERN_ALERT "[MMC/SD driver] Getting platform resources failed!\n");
		goto the_end;
	}
#if 0
	if (!request_mem_region(resource->start, SZ_1K, MMC0_DRIVER_NAME)) {
		ret = -EBUSY;
		printk(KERN_ALERT "[MMC/SD driver] Request memory region failed!\n");
		goto the_end ;
	}
#endif
	irq[0] = platform_get_irq(pdev, 0);	/*get IRQ for device*/;
	irq[1] = platform_get_irq(pdev, 1);	/*get IRQ for dma*/;

	if (irq[0] == NO_IRQ || irq[1] == NO_IRQ) {
		ret = -ENXIO;/* No such device or address */
		printk(KERN_ALERT "[MMC/SD driver] Get platform IRQ failed!\n");
		goto rls_region;
	}

	/*allocate a standard msp_host structure attached with a atsmb structure*/
	mmc_host = mmc_alloc_host(sizeof(struct atsmb_host), dev);
	if (!mmc_host) {
		ret = -ENOMEM;
		printk(KERN_ALERT "[MMC/SD driver] Allocating driver's data failed!\n");
		goto rls_region;
	}
	mmc_host->wmt_host_index = 0; /*to identify host number*/
	
	dev_set_drvdata(dev, (void *)mmc_host); /* mmc_host is driver data for the atsmb dev.*/
	atsmb_host = mmc_priv(mmc_host);

	mmc_host->ops = &atsmb_ops;

	mmc_host->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;

	mmc_host->f_min = 390425; /*390.425Hz = 400MHz/64/16*/
	mmc_host->f_max = 100000000; /* Set max frequency  100MHz*/

	if (SDXC0_function == 1) {
		mmc_host->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SDIO_IRQ	
						| MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR;
							/* |MMC_CAP_8_BIT_DATA;*/	//zhf: marked by James Tian
	} else {
		mmc_host->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SDIO_IRQ;
						/* |MMC_CAP_8_BIT_DATA;*/	//zhf: marked by James Tian
	}

	mmc_host->max_segs = 128;	/*we use software sg. so we could manage even larger number.*/

	/*1MB per each request */
	/*we have a 16 bit block number register, and block length is 512 bytes.*/
	mmc_host->max_req_size = 16*512*(mmc_host->max_segs);
	mmc_host->max_seg_size = 65024; /* 0x7F*512 PDMA one descriptor can transfer 64K-1 byte*/
	mmc_host->max_blk_size = 2048;	/* our block length register is 11 bits.*/
	mmc_host->max_blk_count = (mmc_host->max_req_size)/512;

	/*set the specified host -- ATSMB*/
#ifdef CONFIG_MMC_UNSAFE_RESUME
	mmc_host->card_attath_status = card_attach_status_unchange;
#endif
    sema_init(&mmc_host->req_sema,1); /*initial request semaphore*/
#if 0
	atsmb_host->base = ioremap(resource->start, SZ_1K);    
	if (!atsmb_host->base) {
		printk(KERN_ALERT "[MMC/SD driver] IO remap failed!\n");
		ret = -ENOMEM;
		goto fr_host;
	}
#endif
    atsmb_host->base = (void *)resource->start;
	atsmb_host->mmc = mmc_host;
	spin_lock_init(&atsmb_host->lock);
	atsmb_host->res = resource;/* for atsmb_remove*/

	/*disable all interrupt and clear status by resetting controller.*/
	*ATSMB0_BUS_MODE |= ATSMB_SFTRST;
	*ATSMB0_BLK_LEN &= ~(0xa000);
	*ATSMB0_SD_STS_0 |= 0xff;
	*ATSMB0_SD_STS_1 |= 0xff;

	/* WM3437 A0 default not output clock, after SFTRST need to enable SD clock */
	//if (MMC0_DRIVER_VERSION >= MMC_DRV_3437_A0) /* including 3429 */
	*ATSMB0_BUS_MODE |= ATSMB_CST;

	atsmb_host->regular_irq = irq[0];
	atsmb_host->dma_irq = irq[1];

	ret = request_irq(atsmb_host->regular_irq,
					atsmb_regular_isr,
					IRQF_SHARED,			//SA_SHIRQ, /*SA_INTERRUPT, * that is okay?*/	//zhf: modified by James Tian, should be IRQF_SHARED?
					MMC0_DRIVER_NAME,
					(void *)atsmb_host);
	if (ret) {
		printk(KERN_ALERT "[MMC/SD driver] Failed to register regular ISR!\n");
		goto unmap;
	}

	ret = request_irq(atsmb_host->dma_irq,
					atsmb_dma_isr,
					IRQF_DISABLED,	//	SA_INTERRUPT,  //zhf: modified by James Tian
					MMC0_DRIVER_NAME,
					(void *)atsmb_host);
	if (ret) {
		printk(KERN_ALERT "[MMC/SD driver] Failed to register DMA ISR!\n");
		goto fr_regular_isr;
	}

	/* config CardDetect pin to SD function */
    /*GPIO_CTRL_GP63_SD02CD_BYTE_VAL &= ~GPIO_SD0_CD; add by eason 2012/3/29*/

	/*wait card detect status change*/
	//msleep(10);

	/*enable card insertion interrupt and enable DMA and its Global INT*/
	*ATSMB0_BLK_LEN |= (0xa000); /* also, we enable GPIO to detect card.*/
	*ATSMB0_SD_STS_0 |= 0xff;
	*ATSMB0_INT_MASK_0 |= 0x80; /*or 0x40?*/

	/*allocation dma descriptor*/
	ret = atsmb_alloc_desc(atsmb_host, sizeof(struct SD_PDMA_DESC_S) * MAX_DESC_NUM);
	if (ret == -1) {
		printk(KERN_ALERT "[MMC/SD driver] Failed to allocate DMA descriptor!\n");
			goto fr_dma_isr;
	}
	printk(KERN_INFO "WMT ATSMB (AHB To SD/MMC Bus) controller registered!\n");

	if (SD0_function == SDIO_WIFI)
		mmc_add_host(mmc_host,false);
	else
        mmc_add_host(mmc_host,true);

	mmc_host_attr = mmc_host;

	DBG("[%s] e1\n",__func__);
	return 0;

fr_dma_isr:
	free_irq(atsmb_host->dma_irq, atsmb_host);
fr_regular_isr:
	free_irq(atsmb_host->regular_irq, atsmb_host);
unmap:
	//iounmap(atsmb_host->base);
//fr_host:
	dev_set_drvdata(dev, NULL);
	mmc_free_host(mmc_host);
rls_region:
	//release_mem_region(resource->start, SZ_1K);
the_end:
	printk(KERN_ALERT "[MMC/SD driver] ATSMB0 probe Failed!\n") ;
	DBG("[%s] e2\n",__func__);
	return ret;
}
/**********************************************************************
Name  	 : atsmb_remove
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static int atsmb_remove(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct mmc_host *mmc_host = (struct mmc_host *)dev_get_drvdata(dev);
	struct atsmb_host *atsmb_host;

	DBG("[%s] s\n",__func__);
	atsmb_host = mmc_priv(mmc_host);
	if (!mmc_host || !atsmb_host) {
		printk(KERN_ALERT "[MMC/SD driver] ATSMB0 remove method failed!\n");
		DBG("[%s] e1\n",__func__);
		return -ENXIO;
	}
	mmc_remove_host(mmc_host);

	/*disable interrupt by resetting controller -- for safey*/
	*ATSMB0_BUS_MODE |= ATSMB_SFTRST;
	*ATSMB0_BLK_LEN &= ~(0xa000);
	*ATSMB0_SD_STS_0 |= 0xff;
	*ATSMB0_SD_STS_1 |= 0xff;

	(void)free_irq(atsmb_host->regular_irq, atsmb_host);
	(void)free_irq(atsmb_host->dma_irq, atsmb_host);
	(void)iounmap(atsmb_host->base);
	(void)release_mem_region(atsmb_host->res->start, SZ_1K);
	dev_set_drvdata(dev, NULL);
	/*free dma descriptor*/
	dma_free_coherent(atsmb_host->mmc->parent, atsmb_host->DescSize,
	atsmb_host->DescVirAddr, atsmb_host->DescPhyAddr);
	(void)mmc_free_host(mmc_host);/* also free atsmb_host.*/
	DBG("[%s] e2\n",__func__);
	return 0;
}

/**********************************************************************
Name  	 : atsmb_shutdown
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Tommy Huang
History	:
***********************************************************************/
static void atsmb_shutdown(struct platform_device *pdev)
{
	/*atsmb_shutdown don't be used now.*/
	/*struct device *dev = &pdev->dev;
	struct mmc_host *mmc_host = (struct mmc_host *)dev_get_drvdata(dev);*/

	DBG("[%s] s\n",__func__);
    if (SD0_function != SDIO_WIFI) {
	    /*Disable card detect interrupt*/
	    *ATSMB0_INT_MASK_0 &= ~0x80;
    }
	DBG("[%s] e\n",__func__);
	
}

/**********************************************************************
Name  	 : atsmb_suspend
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
#ifdef CONFIG_PM
static int atsmb_suspend(struct platform_device *pdev, pm_message_t state)
{
	struct device *dev = &pdev->dev;
	struct mmc_host *mmc = (struct mmc_host *)dev_get_drvdata(dev);
	int ret = 0;
	DBG("[%s] s\n",__func__);

	if (mmc) {
		/*struct atsmb_host *host = mmc_priv(mmc);*/
		ret = mmc_suspend_host(mmc);
		if (ret == 0) {
			/*disable all interrupt and clear status by resetting controller. */
			*ATSMB0_BUS_MODE |= ATSMB_SFTRST;
			*ATSMB0_BLK_LEN &= ~(0xa000);
			*ATSMB0_SD_STS_0 |= 0xff;
			*ATSMB0_SD_STS_1 |= 0xff;

		}
		/*disable source clock*/
		auto_pll_divisor(DEV_SDMMC0, CLK_DISABLE, 0, 0);
#ifdef CONFIG_MMC_UNSAFE_RESUME
		/*clean SD card attatch status change*/
		PMCWS_VAL |= BIT19;
		mmc->card_attath_status = card_attach_status_unchange;
#endif
	}

	DBG("[%s] e\n",__func__);
	return ret;
}
/**********************************************************************
Name  	 : atsmb_resume
Function    :.
Calls		:
Called by	:
Parameter :
Author 	 : Leo Lee
History	:
***********************************************************************/
static int atsmb_resume(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct mmc_host *mmc = (struct mmc_host *)dev_get_drvdata(dev);
	int ret = 0;
	DBG("[%s] s\n",__func__);

	/*
	 * enable interrupt, DMA, etc.
	 * Supply power to slot.
	 */
	if (mmc) {
		/*enable source clock*/
		auto_pll_divisor(DEV_SDMMC0, CLK_ENABLE, 0, 0);

        udelay(1);
		/*enable card insertion interrupt and enable DMA and its Global INT*/
		*ATSMB0_BUS_MODE |= ATSMB_SFTRST;
		*ATSMB0_BLK_LEN |= (0xa000);
		*ATSMB0_INT_MASK_0 |= 0x80;	/* or 40?*/
#ifdef CONFIG_MMC_UNSAFE_RESUME
		/*modify SD card attatch status change*/
		if ((PMCWS_VAL & BIT19) && !mmc->bus_dead) {
			/*card change when suspend mode*/
			mmc->card_attath_status = card_attach_status_change;
			/*clean SD card attatch status change*/
			PMCWS_VAL |= BIT19;
		}
#endif
		ret = mmc_resume_host(mmc);
	}

	DBG("[%s] e\n",__func__);
	return ret;
}
#else
#define atsmb_suspend	NULL
#define atsmb_resume	NULL
#endif

static struct platform_driver atsmb_driver = {
	.driver.name		= "sdmmc",
	//.probe		= atsmb_probe,
	.remove		= atsmb_remove,
	.shutdown	= atsmb_shutdown,
	.suspend	= atsmb_suspend,
	.resume		= atsmb_resume,
};

static struct platform_device wmt_sdmmc_device = {
	.name			= "sdmmc",
	.id				= 0,
	.dev            = {
	.dma_mask 		= &wmt_sdmmc_dma_mask,
	.coherent_dma_mask = ~0,
	.release = atsmb_release,
	},
	.num_resources  = ARRAY_SIZE(wmt_sdmmc_resources),
	.resource       = wmt_sdmmc_resources,
};


static ssize_t atsmb_state_show(struct kobject *kobj, struct kobj_attribute *attr,
			     char *buf)
{
    int ret = 0;
    int card_state;
    
    DBG("[%s] s\n",__func__);
    if (mmc_host_attr->card != NULL)
        card_state = 1;
    else
        card_state = 0;
    DBG("[%s]card_state = %d\n",__func__,card_state);
    ret = sprintf(buf, "%d\n", card_state);
    DBG("[%s] e\n",__func__);
	return ret;
}

static ssize_t atsmb_state_store(struct kobject *kobj, struct kobj_attribute *attr,
	       const char *buf, size_t n)
{
	int val;
    DBG("[%s] s\n",__func__);
	if (sscanf(buf, "%d", &val) == 1) {
        DBG("[%s] val = %d\n",__func__,val);
        if ((val == 1) && (mmc_host_attr->card == NULL)) {
            DBG("[%s]add card\n",__func__);
            mmc_detect_change(mmc_host_attr, 0);
            msleep(500);
        } else if ((val == 0) && (mmc_host_attr->card != NULL)) {
            DBG("[%s]remove card\n",__func__);
            mmc_force_remove_card(mmc_host_attr);
        }
        DBG("[%s] e1\n",__func__);
        return n;
	}
    DBG("[%s] e2\n",__func__);
	return -EINVAL;
}

static struct kobj_attribute atsmb_state_attr = {	\
	.attr	= {				\
		.name = __stringify(state),	\
		.mode = 0755,			\
	},					\
	.show	= atsmb_state_show,			\
	.store	= atsmb_state_store,		\
};

static struct attribute * g[] = {
	&atsmb_state_attr.attr,
	NULL,
};

static struct attribute_group attr_group = {
	.attrs = g,
};
    //kevin add to check tf card stats every 1 sec

static void wmt_mmc_work(struct work_struct *work)
{
    
    static int card_state_save = -1;
	if(mmc_host_attr!=NULL){
		struct atsmb_host *host = mmc_priv(mmc_host_attr);
        int card_state;
        if (mmc_host_attr->card != NULL)
            card_state = 1;
        else
            card_state = 0;

       
        //printk("check %d %d\n",atsmb_get_slot_status(host->mmc),card_state);
        if(atsmb_get_slot_status(host->mmc)!=card_state){
            //the second time error,goto detect or remove card
            if(card_state_save == card_state){

                //printk("xxxxxxxxxxxxxxxxxxxxxxxxcatch error  %dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",card_state);
                mmc_detect_change(mmc_host_attr, 0);

            }
            //mark status error
            card_state_save =card_state;
        }else
            card_state_save = -1;
		
	}
	schedule_delayed_work(&mmc_work, 1*HZ);
}
static int __init atsmb_init(void)
{
	int ret;

	int retval;
	unsigned char buf[80];
	int varlen = 80;
	char *varname = "wmt.sd0.param";
	int temp = 0, sd_enable = 1; /*0 :disable 1:enable*/
	unsigned char colon;
	DBG("[%s] s\n",__func__);

#ifdef CONFIG_MTD_WMT_SF
	/*Read system param to identify host function 0: SD/MMC 1:SDIO wifi*/
	retval = wmt_getsyspara(varname, buf, &varlen);
	if (retval == 0) {
		sscanf(buf,"%x%c%d%c%d%c%d%c%d", &temp, &colon, &SD0_function,&colon,&SD0_detect_pol,&colon,&SD0_detect_pulldown,&colon,&SD0_speed);
		printk(KERN_ALERT "wmt.sd0.param = %x%c%d%c%d%c%d%c%d\n", temp, colon, SD0_function,colon,SD0_detect_pol,colon,SD0_detect_pulldown,colon,SD0_speed);
		sd_enable = temp & 0xf;
		SDXC0_function = (temp >> 4) & 0xf;
		printk(KERN_ALERT "SD0 ebable = %x, SDXC = %x, function = %x\n",
			sd_enable, SDXC0_function, SD0_function);

		if (SD0_function < 0 || SD0_function >= SD_MAX_FUN) {
			sd_enable = 1;
			SD0_function = 0;
			printk(KERN_ALERT "wmt.sd0.param func err\n");
		}
		if (colon != ':') {
			sd_enable = 1;
			printk(KERN_ALERT "wmt.sd0.param colon err\n");
		}
			
	} else {
		printk(KERN_ALERT "Default wmt.sd0.param = %x:%d\n", temp, SD0_function);
	}

	//add by kevin guan
 	retval = wmt_getsyspara("wmt.sd0.ro.disable", buf, &varlen);
	if (retval == 0) {
		sscanf(buf,"%d", &SD0_ro_disable);
        printk(KERN_ALERT "SD0_ro_disable %d\n",SD0_ro_disable);
	}
    
#endif
	/*SD function disable*/
	if (sd_enable != 1) {
		return -ENODEV;
	}

	
	get_driver_version();
	
	if (platform_device_register(&wmt_sdmmc_device))//add by jay,for modules support
		return -1;
	//ret = platform_driver_register(&atsmb_driver);

	/* Register regulator for SD host power switch */
	atsmb_regulator = regulator_get(NULL, "ldo4");
	if (IS_ERR(atsmb_regulator))
		g_atsmb_regulator = 0;
	else
		g_atsmb_regulator = 1;
	printk("[SD/MMC]use_regulator = %d\n", g_atsmb_regulator);

	/* Force to disable ldo4 regulator, because Power switch is turned on, default */
	if (g_atsmb_regulator) {
		ret = regulator_force_disable(atsmb_regulator);
		if (ret != 0)
			printk(KERN_ALERT "[%s] get regulator Fail, ret = %d\n", __FUNCTION__, ret);
	}

	ret = platform_driver_probe(&atsmb_driver, atsmb_probe);

    atsmb_kobj = kobject_create_and_add("mmc0", NULL);
	if (!atsmb_kobj)
		return -ENOMEM;


	{
		//add by kevin guan
		int detect_disable = 0;
	 	retval = wmt_getsyspara("wmt.sd0.detect.disable", buf, &varlen);
		if (retval == 0) {
			sscanf(buf,"%d", &detect_disable);
	        printk(KERN_ALERT "detect_disable %d\n",detect_disable);
		}

		if(detect_disable){
			//tf slot without detect pin.
			//do nothing;
		}else{
		    //kevin add to check tf card stats every 1 sec
			INIT_DELAYED_WORK(&mmc_work, wmt_mmc_work);
		    schedule_delayed_work(&mmc_work, 1*HZ);
		}
	}

	return sysfs_create_group(atsmb_kobj, &attr_group);
    
	DBG("[%s] e\n",__func__);
	return ret;
}

static void __exit atsmb_exit(void)
{
	DBG("[%s] s\n",__func__);
	(void)platform_driver_unregister(&atsmb_driver);
	(void)platform_device_unregister(&wmt_sdmmc_device);//add by jay,for modules support
	DBG("[%s] e\n",__func__);
}

module_init(atsmb_init);
module_exit(atsmb_exit);
module_param(fmax0, uint, 0444);

MODULE_AUTHOR("WonderMedia Technologies, Inc.");
MODULE_DESCRIPTION("WMT [AHB to SD/MMC Bridge] driver");
MODULE_LICENSE("GPL");